add some movement helpers
This commit is contained in:
parent
1a2504a4b4
commit
cb742237b0
3 changed files with 84 additions and 2 deletions
|
|
@ -23,6 +23,7 @@ let groups = [];
|
|||
let api = mesh.api = {
|
||||
|
||||
group: {
|
||||
// @returns {MeshGroup[]}
|
||||
list() {
|
||||
return groups.slice();
|
||||
},
|
||||
|
|
@ -48,14 +49,49 @@ let api = mesh.api = {
|
|||
},
|
||||
|
||||
model: {
|
||||
// @returns {MeshModel[]}
|
||||
list() {
|
||||
return groups.map(g => g.models).flat();
|
||||
},
|
||||
|
||||
// @param group {MeshModel}
|
||||
remove: (model) => {
|
||||
model.group.remove(model);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
let util = mesh.util = {
|
||||
|
||||
// @param object {THREE.Object3D}
|
||||
// @returns bounds modified for moto.Space
|
||||
bounds: (object) => {
|
||||
let box = new THREE.Box3().setFromObject(object);
|
||||
let bnd = {
|
||||
min: {
|
||||
x: box.min.x,
|
||||
y: box.min.z,
|
||||
z: box.min.y
|
||||
},
|
||||
max: {
|
||||
x: box.max.x,
|
||||
y: box.max.z,
|
||||
z: box.max.y
|
||||
}
|
||||
};
|
||||
bnd.size = {
|
||||
x: bnd.max.x - bnd.min.x,
|
||||
y: bnd.max.y - bnd.min.y,
|
||||
z: bnd.max.z - bnd.min.z
|
||||
};
|
||||
bnd.center = {
|
||||
x: (bnd.max.x + bnd.min.x) / 2,
|
||||
y: (bnd.max.y + bnd.min.y) / 2,
|
||||
z: (bnd.max.z + bnd.min.z) / 2
|
||||
};
|
||||
return bnd;
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
})();
|
||||
|
|
|
|||
|
|
@ -26,7 +26,30 @@ mesh.group = class MeshGroup {
|
|||
}
|
||||
|
||||
bounds() {
|
||||
return new THREE.Box3().setFromObject(this.group);
|
||||
return mesh.util.bounds(this.group);
|
||||
}
|
||||
|
||||
floor() {
|
||||
let b = this.bounds();
|
||||
this.move(0, 0, -b.min.z);
|
||||
}
|
||||
|
||||
centerXY() {
|
||||
let b = this.bounds();
|
||||
this.move(-b.center.x, -b.center.y, 0);
|
||||
}
|
||||
|
||||
move(x = 0, y = 0, z = 0) {
|
||||
let pos = this.position();
|
||||
pos.set(pos.x + x, pos.y + y, pos.z + z);
|
||||
}
|
||||
|
||||
position() {
|
||||
let pos = this.group.position;
|
||||
if (arguments.length === 0) {
|
||||
return pos;
|
||||
}
|
||||
pos.set(...arguments);
|
||||
}
|
||||
|
||||
// @param model {MeshModel}
|
||||
|
|
|
|||
|
|
@ -39,7 +39,30 @@ mesh.model = class MeshModel {
|
|||
}
|
||||
|
||||
bounds() {
|
||||
return new THREE.Box3().setFromObject(this.mesh);
|
||||
return mesh.util.bounds(this.mesh);
|
||||
}
|
||||
|
||||
floor() {
|
||||
let b = this.bounds();
|
||||
this.move(0, 0, -b.min.z);
|
||||
}
|
||||
|
||||
centerXY() {
|
||||
let b = this.bounds();
|
||||
this.move(-b.center.x, -b.center.y, 0);
|
||||
}
|
||||
|
||||
move(x = 0, y = 0, z = 0) {
|
||||
let pos = this.position();
|
||||
pos.set(pos.x + x, pos.y + y, pos.z + z);
|
||||
}
|
||||
|
||||
position() {
|
||||
let pos = this.mesh.position;
|
||||
if (arguments.length === 0) {
|
||||
return pos;
|
||||
}
|
||||
pos.set(...this.arguments);
|
||||
}
|
||||
|
||||
load(vertices, indices) {
|
||||
|
|
|
|||
Loading…
Reference in a new issue