diff --git a/src/mesh/api.js b/src/mesh/api.js index f9ca3899..d75b1c69 100644 --- a/src/mesh/api.js +++ b/src/mesh/api.js @@ -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; + } + +}; + })(); diff --git a/src/mesh/group.js b/src/mesh/group.js index 33d627f4..7fa54c5d 100644 --- a/src/mesh/group.js +++ b/src/mesh/group.js @@ -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} diff --git a/src/mesh/model.js b/src/mesh/model.js index 94c05e54..b649f98c 100644 --- a/src/mesh/model.js +++ b/src/mesh/model.js @@ -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) {