worker model load, remove, sync using cache

This commit is contained in:
Stewart Allen 2022-01-02 00:40:43 -05:00
commit b4966bbb6f
5 changed files with 54 additions and 26 deletions

View file

@ -189,11 +189,6 @@ let model = {
// @returns {MeshModel[]}
list() {
return groups.map(g => g.models).flat();
},
// @param group {MeshModel}
remove(model) {
model.group.remove(model);
}
};

View file

@ -53,6 +53,8 @@ mesh.group = class MeshGroup extends mesh.object {
model.group = undefined;
this.models.remove(model);
this.group.remove(model.mesh);
// trigger sync with worker
model.remove(true);
// auto-remove group when empty
if (this.group.children.length === 0) {
mesh.api.group.remove(this);

View file

@ -55,14 +55,13 @@ mesh.model = class MeshModel extends mesh.object {
sync() {
moto.client.fn.model_sync({
vertices: this.mesh.geometry.attributes.position.array,
matrix: this.mesh.matrixWorld.elements,
id: this.id
}).then(data => {
if (data && data.length)
// for debugging matrix ops
return mesh.api.group.new([new mesh.model({
file: "synth",
file: `synth-${this.name}`,
mesh: data[0]
})]);
});
@ -80,6 +79,8 @@ mesh.model = class MeshModel extends mesh.object {
meh.renderOrder = 1;
// this ref allows clicks to be traced to models and groups
meh.model = this;
// sync data to worker
moto.client.fn.model_load({id: this.id, name: this.name, vertices, indices});
return meh;
}
@ -158,7 +159,12 @@ mesh.model = class MeshModel extends mesh.object {
}
remove() {
mesh.api.model.remove(this);
if (arguments.length === 0) {
// direct call requires pass through group
} else {
// called from group
moto.client.fn.model_remove({id: this.id});
}
}
};

View file

@ -20,13 +20,32 @@ gapp.finalize("mesh.work", [
// compensation for space/world/platform rotation
let space_rotation = new THREE.Matrix4().makeRotationX(Math.PI / 2);
let { client, worker } = moto;
let cache = {};
moto.worker.bind("model_sync", (data, send) => {
let { vertices, matrix, id } = data;
// return send.done(vertices);
worker.bind("debug", (data, send) => {
console.log({cache});
send.done();
});
worker.bind("model_load", (data, send) => {
let { vertices, name, id } = data;
let geo = new THREE.BufferGeometry();
geo.setAttribute('position', new THREE.BufferAttribute(vertices, 3));
geo.computeVertexNormals();
cache[id] = { id, name, geo };
send.done();
});
worker.bind("model_remove", (data, send) => {
delete cache[data.id];
send.done();
});
worker.bind("model_sync", (data, send) => {
let { matrix, id } = data;
let rec = cache[id];
let geo = rec.geo.clone();
let m4 = space_rotation.clone().multiply( new THREE.Matrix4().fromArray(matrix) );
geo.applyMatrix4(m4);
// for debugging matrix ops