From a64ef24dbeb50f9b7b643bf168ae5bb195dfee1a Mon Sep 17 00:00:00 2001 From: Stewart Allen Date: Sat, 1 Jan 2022 12:12:57 -0500 Subject: [PATCH] add object bounding box visualization, broker function wrappers --- package.json | 2 +- src/main/mesh.js | 9 ++++----- src/mesh/api.js | 17 +++++++++++++++-- src/mesh/model.js | 1 + src/mesh/object.js | 27 +++++++++++++++++++++++++++ src/moto/broker.js | 9 +++++++++ 6 files changed, 57 insertions(+), 8 deletions(-) diff --git a/package.json b/package.json index 0683056a..bc9e7631 100644 --- a/package.json +++ b/package.json @@ -33,7 +33,7 @@ "moment": "^2.24.0", "pixi.js": "^4.8.9", "serve-static": "^1.14.1", - "three": "^0.126.0", + "three": "^0.128.0", "uglify-es": "3.3.9", "validator": ">=13.7.0", "ws": "^7.5.5" diff --git a/src/main/mesh.js b/src/main/mesh.js index bc38b458..bc63db1b 100644 --- a/src/main/mesh.js +++ b/src/main/mesh.js @@ -113,7 +113,6 @@ broker.subscribe('space_init', data => { // mouse hover/click handlers space.mouse.downSelect((int, event) => { - // mesh array including platform return api.objects(); }); @@ -127,11 +126,11 @@ broker.subscribe('space_init', data => { let q = new THREE.Quaternion(); // find intersecting point, look "up" on Z and rotate to face that q.setFromUnitVectors(int.face.normal, new THREE.Vector3(0,0,-1)); - // let e = new THREE.Euler().setFromQuaternion(q); - // let n = int.face.normal; + // group.qrotate(q); + let e = new THREE.Euler().setFromQuaternion(q); + let n = int.face.normal; // console.log({n,q,e}); - // group.rotation(e.x, e.y, e.z); - group.qrotate(q); + group.rotation(e.x, e.y, e.z); group.floor(); } else { api.selection.toggle(model.group); diff --git a/src/mesh/api.js b/src/mesh/api.js index d7bc6888..dafd1580 100644 --- a/src/mesh/api.js +++ b/src/mesh/api.js @@ -6,20 +6,30 @@ // dep: ext.three // dep: ext.three-bgu -gapp.register("mesh.api", [ +gapp.load(bind, "mesh.api", [ "moto.license", // dep: moto.license "moto.client", // dep: moto.client + "moto.broker", // dep: moto.broker "moto.space", // dep: moto.space "mesh.tool", // dep: mesh.tool "add.array", // dep: add.array ]); +function bind() { + broker = gapp.broker; + // publish messages with results of function call + // selection.move = broker.wrap('selection.move', selection.move); + // selection.rotate = broker.wrap('selection.rotate', selection.rotate); + // selection.update = broker.wrap('selection.update', selection.update); +} + let mesh = self.mesh = self.mesh || {}; if (mesh.api) return; let space = moto.Space; let groups = []; let selected = []; +let broker; let selection = { // @returns {MeshObject[]} @@ -58,10 +68,13 @@ let selection = { update() { for (let group of groups) { group.material(mesh.material.unselected); + group.showBounds(true); } for (let object of selected) { object.material(mesh.material.selected); + object.showBounds(true); } + return selection; }, move(dx = 0, dy = 0, dz = 0) { @@ -231,7 +244,7 @@ let util = mesh.util = { }; bnd.center = { x: (bnd.max.x + bnd.min.x) / 2, - y: (bnd.max.y + bnd.min.y) / 2, + y: -(bnd.max.y + bnd.min.y) / 2, z: (bnd.max.z + bnd.min.z) / 2 }; return bnd; diff --git a/src/mesh/model.js b/src/mesh/model.js index c82efaf1..71dc8cfb 100644 --- a/src/mesh/model.js +++ b/src/mesh/model.js @@ -10,6 +10,7 @@ gapp.register("mesh.model", [ "moto.license", // dep: moto.license "mesh.object", // dep: mesh.object "mesh.group", // dep: mesh.group + "mesh.api", // dep: mesh.api ]); let mesh = self.mesh = self.mesh || {}; diff --git a/src/mesh/object.js b/src/mesh/object.js index ab87d49a..b80a1f8d 100644 --- a/src/mesh/object.js +++ b/src/mesh/object.js @@ -7,6 +7,7 @@ gapp.register("mesh.object", [ "add.three", // dep: add.three "moto.license", // dep: moto.license + "mesh.api", // dep: mesh.api ]); let mesh = self.mesh = self.mesh || {}; @@ -48,6 +49,7 @@ mesh.object = class MeshObject { move(x = 0, y = 0, z = 0) { let pos = this.position(); pos.set(pos.x + x, pos.y + y, pos.z + z); + this.updateBoundsBox(); moto.Space.update(); return this; } @@ -58,6 +60,7 @@ mesh.object = class MeshObject { return scale; } scale.set(...arguments); + this.updateBoundsBox(); moto.Space.update(); } @@ -68,6 +71,7 @@ mesh.object = class MeshObject { return rot; } rot.set(...arguments); + this.updateBoundsBox(); moto.Space.update(); return this; } @@ -76,12 +80,14 @@ mesh.object = class MeshObject { if (x) this.object().rotateOnWorldAxis(new THREE.Vector3(1,0,0), x); if (y) this.object().rotateOnWorldAxis(new THREE.Vector3(0,1,0), y); if (z) this.object().rotateOnWorldAxis(new THREE.Vector3(0,0,1), z); + this.updateBoundsBox(); moto.Space.update(); return this; } qrotate(quaternion) { this.object().setRotationFromQuaternion(quaternion); + this.updateBoundsBox(); moto.Space.update(); return this; } @@ -94,6 +100,27 @@ mesh.object = class MeshObject { pos.set(...arguments); moto.Space.update(); } + + showBounds(bool) { + this._showBounds = bool; + } + + updateBoundsBox() { + let helper = this._boundsBox; + let world = moto.Space.world; + if (helper) { + world.remove(helper); + } + if (this._showBounds) { + let { center, size } = this.bounds(); + let b3 = new THREE.Box3().setFromCenterAndSize( + new THREE.Vector3(center.x, center.y, center.z), + new THREE.Vector3(size.x, size.y, size.z) + ); + let helper = this._boundsBox = new THREE.Box3Helper(b3, 0xff00ff); + world.add(helper); + } + } }; })(); diff --git a/src/moto/broker.js b/src/moto/broker.js index f1222e12..98279029 100644 --- a/src/moto/broker.js +++ b/src/moto/broker.js @@ -82,6 +82,7 @@ gapp.broker = new class Broker { } else if (topic.indexOf(".topic.") < 0) { dbug.debug(`undelivered '${topic}'`, message); } + return message; } // return function bound to a topic @@ -91,6 +92,14 @@ gapp.broker = new class Broker { broker.publish(topic, message || msg, options || opt); } } + + // return function wrapper that publishes a message to a topic + wrap(topic, fn) { + let broker = this; + return function() { + return broker.publish(topic, fn(...arguments)); + } + } } })();