add focus. improve bounds with arrays

This commit is contained in:
Stewart Allen 2021-12-31 16:59:38 -05:00
commit 86b60cc715
4 changed files with 73 additions and 18 deletions

View file

@ -81,20 +81,26 @@ broker.subscribe('space_init', data => {
platform.setColor(platcolor);
},
'keypress', evt => {
switch (evt.code) {
case 'KeyV':
api.selection.focus();
break;
}
},
'keydown', evt => {
let rv = Math.PI / 16;
switch (evt.code) {
case 'ArrowUp':
api.selection.rotate(-0.2,0,0).floor();
api.selection.rotate(-rv,0,0).floor();
break;
case 'ArrowDown':
api.selection.rotate(0.2,0,0).floor();
api.selection.rotate(rv,0,0).floor();
break;
case 'ArrowLeft':
api.selection.rotate(0,0,0.2).floor();
api.selection.rotate(0,0,rv).floor();
break;
case 'ArrowRight':
api.selection.rotate(0,0,-0.2).floor();
api.selection.rotate(0,0,-rv).floor();
break;
}
}
@ -106,7 +112,8 @@ broker.subscribe('space_load', data => {
mesh.api.group.new(data.flat().map(el => new mesh.model(el)))
.centerModels()
.centerXY()
.floor();
.floor()
.focus();
});
// remove version cache bust from url

View file

@ -73,13 +73,21 @@ let selection = {
centerXY() {
for (let s of selected) {
s.floor(...arguments);
s.centerXY(...arguments);
}
return selection;
},
home() {
return selection.centerXY().floor();
},
focus() {
api.focus(selected);
},
bounds() {
return util.bounds(selected.map(s => s.object()));
}
};
@ -118,6 +126,18 @@ let api = mesh.api = {
}
},
// @param object {THREE.Object3D | THREE.Object3D[]}
focus: (object) => {
let { center } = util.bounds(object);
// when no objects supplied
if (isNaN(center.x * center.y * center.z)) {
return;
}
space.view.setFocus(new THREE.Vector3(
center.x, center.z, center.y
));
},
selection,
group,
@ -137,10 +157,22 @@ let api = mesh.api = {
let util = mesh.util = {
// @param object {THREE.Object3D}
// @param object {THREE.Object3D | THREE.Object3D[]}
// @returns bounds modified for moto.Space
bounds: (object) => {
let box = new THREE.Box3().setFromObject(object);
let box = new THREE.Box3();
if (Array.isArray(object)) {
for (let o of object) {
box.union(new THREE.Box3().setFromObject(
o instanceof mesh.object ? o.object() : o
));
}
} else if (object) {
box.setFromObject(
object instanceof mesh.object ? object.object() : object);
} else {
return box;
}
let bnd = {
min: {
x: box.min.x,

View file

@ -27,16 +27,6 @@ mesh.group = class MeshGroup extends mesh.object {
}
}
// 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;
}
@ -62,6 +52,28 @@ mesh.group = class MeshGroup extends mesh.object {
moto.Space.update();
return this;
}
// 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;
}
opacity() {
for (let model of this.models) {
model.opacity(...arguments);
}
}
wireframe() {
for (let model of this.models) {
model.wireframe(...arguments);
}
}
};
})();

View file

@ -23,6 +23,10 @@ mesh.object = class MeshObject {
return mesh.util.bounds(this.object());
}
focus() {
mesh.api.focus(this.object());
}
floor() {
let b = this.bounds();
this.move(0, 0, -b.min.z);