diff --git a/src/main/mesh.js b/src/main/mesh.js index 72f1aed5..09305852 100644 --- a/src/main/mesh.js +++ b/src/main/mesh.js @@ -103,7 +103,10 @@ broker.subscribe('space_init', data => { // add object loader broker.subscribe('space_load', data => { - mesh.api.group.new(data.flat().map(el => new mesh.model(el))).centerXY().floor(); + mesh.api.group.new(data.flat().map(el => new mesh.model(el))) + .centerModels() + .centerXY() + .floor(); }); // remove version cache bust from url diff --git a/src/mesh/group.js b/src/mesh/group.js index 5056b589..1f6f60a3 100644 --- a/src/mesh/group.js +++ b/src/mesh/group.js @@ -18,15 +18,25 @@ if (mesh.group) return; mesh.group = class MeshGroup extends mesh.object { // @param group {mesh.model[]} - constructor(models) { + constructor(models = []) { super(); this.group = new THREE.Group(); this.models = []; - for (let model of (models || [])) { + for (let model of models) { this.add(model); } } + // center objects to group bounds + // dependent on first being added to world/scene + centerModels() { + let bounds = this.bounds(); + for (let model of this.models) { + model.center(bounds); + } + return this; + } + object() { return this.group; } diff --git a/src/mesh/object.js b/src/mesh/object.js index 1adf82bf..a49458a1 100644 --- a/src/mesh/object.js +++ b/src/mesh/object.js @@ -29,8 +29,14 @@ mesh.object = class MeshObject { return this; } - centerXY() { - let b = this.bounds(); + center(bounds) { + let b = bounds || this.bounds(); + this.move(-b.center.x, b.center.y, -b.center.z); + return this; + } + + centerXY(bounds) { + let b = bounds || this.bounds(); this.move(-b.center.x, b.center.y, 0); return this; }