checkpoint

This commit is contained in:
Stewart Allen 2024-08-09 21:53:33 -04:00
commit 7ff172f5f3
7 changed files with 234 additions and 199 deletions

View file

@ -14,10 +14,11 @@ const { mesh, moto } = root;
const { space } = moto;
const worker = moto.client.fn;
const lookUp = new Vector3(0,0,-1);
// broker updates generated by objects
let publish = {
matrix: gapp.broker.bind("object_matrix"),
meta: gapp.broker.bind("object_meta"),
destroy: gapp.broker.bind("object_destroy"),
visible: gapp.broker.bind("object_visible"),
};
@ -26,8 +27,14 @@ 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});
this.log('NEW');
// storage location for meta data like position, scale, rotation
this.meta = {};
}
log() {
console.log(this.id, this.type, ...arguments);
}
get type() {
@ -40,7 +47,7 @@ mesh.object = class MeshObject {
}
get bounds() {
return mesh.util.bounds(this.object);
throw "bounds() requires implementation";
}
// manage lifecycle with worker, mesh app caches, etc
@ -53,16 +60,26 @@ mesh.object = class MeshObject {
publish.destroy(this.id);
}
// used during state restoration
applyMeta(meta = {}) {
this.log('apply-meta', meta);
this.applyMatrix(meta.matrix);
this.visible(meta.visible ?? true);
return this;
}
// used during state restoration
applyMatrix(elements) {
if (elements) {
console.log('matrix', this.type, this.id);
this.log('apply-matrix');
this.object.applyMatrix4(new Matrix4().fromArray(elements));
return this.matrixChanged();
return this.metaChanged();
} else {
return this;
}
}
// used during model splitting
applyMatrix4(matrix) {
return this.applyMatrix(matrix.elements);
}
@ -93,73 +110,58 @@ mesh.object = class MeshObject {
return this;
}
floor(clazz) {
if (!clazz || this instanceof clazz) {
let b = this.bounds;
this.move(0, 0, -b.min.z);
}
return this;
floor() {
let b = this.bounds;
return this.move(0, 0, -b.min.z);
}
center(bounds) {
let b = bounds || this.bounds;
this.move(-b.mid.x, -b.mid.y, -b.mid.z);
return this;
center() {
throw "center() requires implementation";
let b = this.bounds;
return this.move(-b.mid.x, -b.mid.y, -b.mid.z);
}
centerXY(bounds) {
let b = bounds || this.bounds;
this.move(-b.mid.x, -b.mid.y, 0);
return this;
centerXY() {
throw "centerXY() requires implementation";
let b = this.bounds;
return this.move(-b.mid.x, -b.mid.y, 0);
}
move(x = 0, y = 0, z = 0) {
let pos = this.position();
return this.position(pos.x + x, pos.y + y, pos.z + z);
move() {
throw "move() requires implementation";
}
scale() {
let scale = this.object.scale;
if (arguments.length === 0) {
return scale;
}
scale.set(...arguments);
this.matrixChanged();
throw "scale() requires implementation";
}
qrotate(quaternion) {
throw "qrotate() requires implementation";
}
rotate(x = 0, y = 0, z = 0) {
throw "rotate() requires implementation";
console.log('object rotate', ...arguments);
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);
this.matrixChanged();
this.log('object-rotate', ...arguments);
let m = new Matrix4();
if (x) m.multiply(new Matrix4().makeRotationX(x));
if (y) m.multiply(new Matrix4().makeRotationY(y));
if (z) m.multiply(new Matrix4().makeRotationZ(z));
this.qrotate(new Quaternion().setFromRotationMatrix(m));
return this;
}
rotation() {
throw "rotate() requires implementation";
let rotation = this.object.rotation;
if (arguments.length === 0) {
return rotation;
}
return this.qrotation(new Quaternion().setFromEuler(new Euler(...arguments)));
throw "rotation() requires implementation";
}
qrotation(quaternion) {
this.object.setRotationFromQuaternion(quaternion);
this.matrixChanged();
return this;
// rotate object in the directon of normal (place face on Z plane)
rotateTowardZ(normal) {
let q = new Quaternion().setFromUnitVectors(normal, lookUp);
this.qrotate(q);
this.floor();
}
position() {
let pos = this.object.position;
if (arguments.length === 0) {
return pos;
}
pos.set(...arguments);
this.matrixChanged();
return this;
throw "position() requires implementation";
}
showBounds(bool) {
@ -175,6 +177,7 @@ mesh.object = class MeshObject {
}
updateBoundsBox() {
this.log('update-bounds-box', this._showBounds);
let helper = this._boundsBox;
let world = space.world;
if (helper) {
@ -192,12 +195,12 @@ mesh.object = class MeshObject {
}
}
matrixChanged() {
if (!this.updateBoundsBox()) {
this.object.updateMatrix();
}
metaChanged() {
this.object.updateMatrix();
this.updateBoundsBox();
space.update();
publish.matrix({ id: this.id, matrix: this.object.matrix });
this.meta.matrix = this.object.matrix.elements;
publish.meta({ id: this.id, meta: this.meta });
return this;
}
};