resolve centering model vs group so rotation works

This commit is contained in:
Stewart Allen 2021-12-31 15:30:21 -05:00
commit 9bb758f323
3 changed files with 24 additions and 5 deletions

View file

@ -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

View file

@ -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;
}

View file

@ -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;
}