worker model load, remove, sync using cache
This commit is contained in:
parent
116e1b9d9c
commit
b4966bbb6f
5 changed files with 54 additions and 26 deletions
|
|
@ -55,6 +55,7 @@ broker.subscribe('space_init', data => {
|
||||||
let { space, platform } = data;
|
let { space, platform } = data;
|
||||||
let platcolor = 0x00ff00;
|
let platcolor = 0x00ff00;
|
||||||
let api = mesh.api;
|
let api = mesh.api;
|
||||||
|
let { selection } = api;
|
||||||
|
|
||||||
// add file drop handler
|
// add file drop handler
|
||||||
space.event.addHandlers(self, [
|
space.event.addHandlers(self, [
|
||||||
|
|
@ -84,16 +85,21 @@ broker.subscribe('space_init', data => {
|
||||||
'keypress', evt => {
|
'keypress', evt => {
|
||||||
switch (evt.code) {
|
switch (evt.code) {
|
||||||
case 'KeyC':
|
case 'KeyC':
|
||||||
api.selection.centerXY().focus();
|
selection.centerXY().focus();
|
||||||
break;
|
break;
|
||||||
case 'KeyV':
|
case 'KeyV':
|
||||||
api.selection.focus();
|
selection.focus();
|
||||||
break;
|
break;
|
||||||
case 'KeyW':
|
case 'KeyW':
|
||||||
api.selection.wireframe({toggle:true});
|
selection.wireframe({toggle:true});
|
||||||
break;
|
break;
|
||||||
case 'KeyB':
|
case 'KeyB':
|
||||||
api.selection.boundsBox({toggle:true});
|
selection.boundsBox({toggle:true});
|
||||||
|
break;
|
||||||
|
case 'KeyS':
|
||||||
|
for (let m of selection.models()) {
|
||||||
|
m.sync();
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
case 'KeyH':
|
case 'KeyH':
|
||||||
space.view.home();
|
space.view.home();
|
||||||
|
|
@ -115,7 +121,7 @@ broker.subscribe('space_init', data => {
|
||||||
switch (code) {
|
switch (code) {
|
||||||
case 'KeyA':
|
case 'KeyA':
|
||||||
if (metaKey || ctrlKey) {
|
if (metaKey || ctrlKey) {
|
||||||
api.selection.set(api.group.list());
|
selection.set(api.group.list());
|
||||||
estop(evt);
|
estop(evt);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|
@ -123,35 +129,35 @@ broker.subscribe('space_init', data => {
|
||||||
broker.send.space_debug();
|
broker.send.space_debug();
|
||||||
break;
|
break;
|
||||||
case 'Escape':
|
case 'Escape':
|
||||||
api.selection.clear();
|
selection.clear();
|
||||||
estop(evt);
|
estop(evt);
|
||||||
break;
|
break;
|
||||||
case 'Backspace':
|
case 'Backspace':
|
||||||
case 'Delete':
|
case 'Delete':
|
||||||
for (let s of api.selection.list()) {
|
for (let s of selection.list()) {
|
||||||
s.showBounds(false);
|
s.showBounds(false);
|
||||||
s.remove();
|
s.remove();
|
||||||
}
|
}
|
||||||
estop(evt);
|
estop(evt);
|
||||||
break;
|
break;
|
||||||
case 'ArrowUp':
|
case 'ArrowUp':
|
||||||
api.selection.rotate(-rv,0,0).floor();
|
selection.rotate(-rv,0,0).floor();
|
||||||
break;
|
break;
|
||||||
case 'ArrowDown':
|
case 'ArrowDown':
|
||||||
api.selection.rotate(rv,0,0).floor();
|
selection.rotate(rv,0,0).floor();
|
||||||
break;
|
break;
|
||||||
case 'ArrowLeft':
|
case 'ArrowLeft':
|
||||||
if (shiftKey) {
|
if (shiftKey) {
|
||||||
api.selection.rotate(0,-rv,0).floor();
|
selection.rotate(0,-rv,0).floor();
|
||||||
} else {
|
} else {
|
||||||
api.selection.rotate(0,0,rv).floor();
|
selection.rotate(0,0,rv).floor();
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case 'ArrowRight':
|
case 'ArrowRight':
|
||||||
if (shiftKey) {
|
if (shiftKey) {
|
||||||
api.selection.rotate(0,rv,0).floor();
|
selection.rotate(0,rv,0).floor();
|
||||||
} else {
|
} else {
|
||||||
api.selection.rotate(0,0,-rv).floor();
|
selection.rotate(0,0,-rv).floor();
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
@ -176,7 +182,7 @@ broker.subscribe('space_init', data => {
|
||||||
group.qrotation(q);
|
group.qrotation(q);
|
||||||
group.floor();
|
group.floor();
|
||||||
} else {
|
} else {
|
||||||
api.selection.toggle(model.group);
|
selection.toggle(model.group);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
|
@ -186,7 +192,7 @@ broker.subscribe('space_init', data => {
|
||||||
|
|
||||||
space.mouse.onDrag((delta, offset, up = false) => {
|
space.mouse.onDrag((delta, offset, up = false) => {
|
||||||
if (delta) {
|
if (delta) {
|
||||||
api.selection.move(delta.x, delta.y, 0);
|
selection.move(delta.x, delta.y, 0);
|
||||||
} else {
|
} else {
|
||||||
return api.objects().length > 0;
|
return api.objects().length > 0;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -189,11 +189,6 @@ let model = {
|
||||||
// @returns {MeshModel[]}
|
// @returns {MeshModel[]}
|
||||||
list() {
|
list() {
|
||||||
return groups.map(g => g.models).flat();
|
return groups.map(g => g.models).flat();
|
||||||
},
|
|
||||||
|
|
||||||
// @param group {MeshModel}
|
|
||||||
remove(model) {
|
|
||||||
model.group.remove(model);
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -53,6 +53,8 @@ mesh.group = class MeshGroup extends mesh.object {
|
||||||
model.group = undefined;
|
model.group = undefined;
|
||||||
this.models.remove(model);
|
this.models.remove(model);
|
||||||
this.group.remove(model.mesh);
|
this.group.remove(model.mesh);
|
||||||
|
// trigger sync with worker
|
||||||
|
model.remove(true);
|
||||||
// auto-remove group when empty
|
// auto-remove group when empty
|
||||||
if (this.group.children.length === 0) {
|
if (this.group.children.length === 0) {
|
||||||
mesh.api.group.remove(this);
|
mesh.api.group.remove(this);
|
||||||
|
|
|
||||||
|
|
@ -55,14 +55,13 @@ mesh.model = class MeshModel extends mesh.object {
|
||||||
|
|
||||||
sync() {
|
sync() {
|
||||||
moto.client.fn.model_sync({
|
moto.client.fn.model_sync({
|
||||||
vertices: this.mesh.geometry.attributes.position.array,
|
|
||||||
matrix: this.mesh.matrixWorld.elements,
|
matrix: this.mesh.matrixWorld.elements,
|
||||||
id: this.id
|
id: this.id
|
||||||
}).then(data => {
|
}).then(data => {
|
||||||
if (data && data.length)
|
if (data && data.length)
|
||||||
// for debugging matrix ops
|
// for debugging matrix ops
|
||||||
return mesh.api.group.new([new mesh.model({
|
return mesh.api.group.new([new mesh.model({
|
||||||
file: "synth",
|
file: `synth-${this.name}`,
|
||||||
mesh: data[0]
|
mesh: data[0]
|
||||||
})]);
|
})]);
|
||||||
});
|
});
|
||||||
|
|
@ -80,6 +79,8 @@ mesh.model = class MeshModel extends mesh.object {
|
||||||
meh.renderOrder = 1;
|
meh.renderOrder = 1;
|
||||||
// this ref allows clicks to be traced to models and groups
|
// this ref allows clicks to be traced to models and groups
|
||||||
meh.model = this;
|
meh.model = this;
|
||||||
|
// sync data to worker
|
||||||
|
moto.client.fn.model_load({id: this.id, name: this.name, vertices, indices});
|
||||||
return meh;
|
return meh;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -158,7 +159,12 @@ mesh.model = class MeshModel extends mesh.object {
|
||||||
}
|
}
|
||||||
|
|
||||||
remove() {
|
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});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -20,13 +20,32 @@ gapp.finalize("mesh.work", [
|
||||||
// compensation for space/world/platform rotation
|
// compensation for space/world/platform rotation
|
||||||
let space_rotation = new THREE.Matrix4().makeRotationX(Math.PI / 2);
|
let space_rotation = new THREE.Matrix4().makeRotationX(Math.PI / 2);
|
||||||
|
|
||||||
|
let { client, worker } = moto;
|
||||||
let cache = {};
|
let cache = {};
|
||||||
|
|
||||||
moto.worker.bind("model_sync", (data, send) => {
|
worker.bind("debug", (data, send) => {
|
||||||
let { vertices, matrix, id } = data;
|
console.log({cache});
|
||||||
// return send.done(vertices);
|
send.done();
|
||||||
|
});
|
||||||
|
|
||||||
|
worker.bind("model_load", (data, send) => {
|
||||||
|
let { vertices, name, id } = data;
|
||||||
let geo = new THREE.BufferGeometry();
|
let geo = new THREE.BufferGeometry();
|
||||||
geo.setAttribute('position', new THREE.BufferAttribute(vertices, 3));
|
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) );
|
let m4 = space_rotation.clone().multiply( new THREE.Matrix4().fromArray(matrix) );
|
||||||
geo.applyMatrix4(m4);
|
geo.applyMatrix4(m4);
|
||||||
// for debugging matrix ops
|
// for debugging matrix ops
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue