add object bounding box visualization, broker function wrappers
This commit is contained in:
parent
8cd2a22a54
commit
a64ef24dbe
6 changed files with 57 additions and 8 deletions
|
|
@ -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;
|
||||
|
|
|
|||
|
|
@ -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 || {};
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
})();
|
||||
|
|
|
|||
Loading…
Reference in a new issue