grid-apps-cmms/src/mesh/object.js

203 lines
5 KiB
JavaScript
Raw Normal View History

/** Copyright Stewart Allen <sa@grid.space> -- All Rights Reserved */
"use strict";
// dep: add.three
// dep: moto.license
// dep: moto.broker
// dep: mesh.api
// use: mesh.util
gapp.register("mesh.object", [], (root, exports) => {
const { Matrix4, Vector3, Box3, Box3Helper, Quaternion, Euler } = THREE;
const { mesh, moto } = root;
const { space } = moto;
const worker = moto.client.fn;
2022-01-12 11:46:10 -05:00
// broker updates generated by objects
let publish = {
matrix: gapp.broker.bind("object_matrix"),
destroy: gapp.broker.bind("object_destroy"),
2024-08-02 10:17:40 -04:00
visible: gapp.broker.bind("object_visible"),
};
mesh.object = class MeshObject {
constructor(id) {
this.id = id || mesh.util.uuid();
this.deferUBB = () => { this.updateBoundsBox() };
worker.object_create({id: this.id, type: this.type});
}
get type() {
throw "type() requires implementation";
2022-01-01 22:39:49 -05:00
}
// @returns {THREE.Object3D}
get object() {
throw "object() requires implementation";
}
get bounds() {
return mesh.util.bounds(this.object);
}
// manage lifecycle with worker, mesh app caches, etc
destroy() {
// update worker state
worker.object_destroy({id: this.id});
// update object store
mesh.db.space.remove(this.id);
// main app cache workspace updates
publish.destroy(this.id);
}
applyMatrix(elements) {
if (elements) {
console.log('matrix', this.type, this.id);
2022-01-12 11:46:10 -05:00
this.object.applyMatrix4(new Matrix4().fromArray(elements));
2024-08-02 10:17:40 -04:00
return this.matrixChanged();
} else {
return this;
}
}
2022-01-11 21:04:24 -05:00
applyMatrix4(matrix) {
2022-01-15 16:38:50 -05:00
return this.applyMatrix(matrix.elements);
2022-01-11 21:04:24 -05:00
}
2022-01-12 09:32:13 -05:00
select() {
2024-07-23 22:42:20 -04:00
throw "select() requires implementation";
}
remove() {
throw "remove() requires implementation";
2022-01-12 09:32:13 -05:00
}
2021-12-31 16:59:38 -05:00
focus() {
mesh.api.focus(this.object);
return this;
2021-12-31 16:59:38 -05:00
}
2022-01-12 00:28:33 -05:00
visible(opt) {
if (opt === undefined) {
return this.object.visible;
}
if (opt.toggle) {
this.visible(!this.visible());
} else {
this.object.visible = opt;
}
2024-08-02 10:17:40 -04:00
publish.visible({ id: this.id, visible: this.object.visible });
2022-01-12 00:28:33 -05:00
return this;
}
floor(clazz) {
if (!clazz || this instanceof clazz) {
let b = this.bounds;
this.move(0, 0, -b.min.z);
}
return this;
}
center(bounds) {
let b = bounds || this.bounds;
this.move(-b.mid.x, -b.mid.y, -b.mid.z);
return this;
}
centerXY(bounds) {
let b = bounds || this.bounds;
this.move(-b.mid.x, -b.mid.y, 0);
return this;
}
move(x = 0, y = 0, z = 0) {
let pos = this.position();
2022-01-09 23:58:59 -05:00
return this.position(pos.x + x, pos.y + y, pos.z + z);
}
scale() {
let scale = this.object.scale;
if (arguments.length === 0) {
return scale;
}
scale.set(...arguments);
2022-01-09 23:58:59 -05:00
this.matrixChanged();
2021-12-31 14:12:42 -05:00
}
rotate(x = 0, y = 0, z = 0) {
2024-08-03 10:26:25 -04:00
console.log('rotate', ...arguments);
2022-01-12 11:46:10 -05:00
if (x) this.object.rotateOnWorldAxis(new Vector3(1,0,0), x);
if (y) this.object.rotateOnWorldAxis(new Vector3(0,1,0), y);
if (z) this.object.rotateOnWorldAxis(new Vector3(0,0,1), z);
2022-01-09 23:58:59 -05:00
this.matrixChanged();
return this;
}
2021-12-31 14:12:42 -05:00
rotation() {
let rotation = this.object.rotation;
2021-12-31 14:12:42 -05:00
if (arguments.length === 0) {
return rotation;
2021-12-31 14:12:42 -05:00
}
2022-01-12 11:46:10 -05:00
return this.qrotation(new Quaternion().setFromEuler(new Euler(...arguments)));
}
qrotation(quaternion) {
this.object.setRotationFromQuaternion(quaternion);
2022-01-09 23:58:59 -05:00
this.matrixChanged();
2022-01-01 10:44:06 -05:00
return this;
}
position() {
let pos = this.object.position;
if (arguments.length === 0) {
return pos;
}
pos.set(...arguments);
2022-01-09 23:58:59 -05:00
this.matrixChanged();
return this;
}
showBounds(bool) {
let was = this._showBounds;
if (bool && bool.toggle) {
bool = !this._showBounds;
}
if (was === bool) {
return;
}
this._showBounds = bool;
this.updateBoundsBox();
}
updateBoundsBox() {
let helper = this._boundsBox;
let world = space.world;
if (helper) {
world.remove(helper);
}
if (this._showBounds) {
let { mid, dim } = this.bounds;
2022-01-12 11:46:10 -05:00
let b3 = new Box3().setFromCenterAndSize(
new Vector3(mid.x, mid.y, mid.z),
new Vector3(dim.x, dim.y, dim.z)
);
2022-01-12 11:46:10 -05:00
let helper = this._boundsBox = new Box3Helper(b3, 0x555555);
world.add(helper);
2022-01-10 23:11:15 -05:00
return true;
}
}
2022-01-09 23:58:59 -05:00
matrixChanged() {
2022-01-10 23:11:15 -05:00
if (!this.updateBoundsBox()) {
this.object.updateMatrix();
}
2022-01-09 23:58:59 -05:00
space.update();
publish.matrix({ id: this.id, matrix: this.object.matrix });
return this;
2022-01-09 23:58:59 -05:00
}
};
});