diff --git a/src/main/mesh.js b/src/main/mesh.js index 09305852..5e165398 100644 --- a/src/main/mesh.js +++ b/src/main/mesh.js @@ -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 diff --git a/src/mesh/api.js b/src/mesh/api.js index 9c615224..0fd701f2 100644 --- a/src/mesh/api.js +++ b/src/mesh/api.js @@ -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, diff --git a/src/mesh/group.js b/src/mesh/group.js index 1f6f60a3..37e7db09 100644 --- a/src/mesh/group.js +++ b/src/mesh/group.js @@ -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); + } + } }; })(); diff --git a/src/mesh/object.js b/src/mesh/object.js index 94f2976b..d210fee5 100644 --- a/src/mesh/object.js +++ b/src/mesh/object.js @@ -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);