prepare to sync models to worker(s)

This commit is contained in:
Stewart Allen 2022-01-01 22:39:49 -05:00
commit 2f2cb85349
6 changed files with 41 additions and 33 deletions

View file

@ -18,15 +18,12 @@ function init() {
platform = space.platform;
// setup default workspace
space.useDefaultKeys(false);
space.sky.set({
grid: sky,
color: dark ? 0 : 0xffffff
});
space.init($('container'), delta => { }, ortho);
platform.onClick(click => {
// api.selection.clear();
});
platform.onMove(delta => { });
platform.set({
volume: false,
round: false,
@ -38,7 +35,6 @@ function init() {
grid: { major: 25, minor: 5, majorColor: 0x999999, minorColor: 0xcccccc },
});
space.view.setZoom(zoomrev, zoomspd);
space.useDefaultKeys(false);
// trigger space event binding
broker.send.space_init({ space, platform });

View file

@ -6,7 +6,7 @@
// dep: ext.three
// dep: ext.three-bgu
gapp.load(bind, "mesh.api", [
gapp.register("mesh.api", [
"moto.license", // dep: moto.license
"moto.client", // dep: moto.client
"moto.broker", // dep: moto.broker
@ -15,21 +15,12 @@ gapp.load(bind, "mesh.api", [
"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[]}
@ -194,6 +185,18 @@ let group = {
}
};
let model = {
// @returns {MeshModel[]}
list() {
return groups.map(g => g.models).flat();
},
// @param group {MeshModel}
remove: (model) => {
model.group.remove(model);
}
};
let api = mesh.api = {
clear: () => {
for (let group of group.list()) {
@ -220,17 +223,7 @@ let api = mesh.api = {
group,
model: {
// @returns {MeshModel[]}
list() {
return groups.map(g => g.models).flat();
},
// @param group {MeshModel}
remove: (model) => {
model.group.remove(model);
}
},
model,
objects: () => {
// return models, not groups
@ -240,6 +233,14 @@ let api = mesh.api = {
let util = mesh.util = {
uuid: (segs = 1) => {
let uid = [];
while (segs-- > 0) {
uid.push(Math.round(Math.random() * 0xffffffff).toString(36));
}
return uid.join('-');
},
// @param object {THREE.Object3D | THREE.Object3D[] | MeshObject | MeshObject[]}
// @returns bounds modified for moto.Space
bounds: (object) => {
@ -341,4 +342,12 @@ let util = mesh.util = {
};
let 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);
model.remove = broker.wrap('model.remove', model.remove);
group.remove = broker.wrap('group.remove', group.remove);
})();

View file

@ -10,6 +10,7 @@ gapp.register("mesh.group", [
"moto.license", // dep: moto.license
"mesh.object", // dep: mesh.object
"mesh.model", // dep: mesh.model
"mesh.api", // dep: mesh.api
]);
let mesh = self.mesh = self.mesh || {};

View file

@ -56,7 +56,8 @@ 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
matrix: this.mesh.matrixWorld.elements,
id: this.id
}).then(data => {
if (data && data.length)
// for debugging matrix ops
@ -71,9 +72,7 @@ mesh.model = class MeshModel extends mesh.object {
let geo = new THREE.BufferGeometry();
if (indices) geo.setIndex(new THREE.BufferAttribute(indices, 1));
geo.setAttribute('position', new THREE.BufferAttribute(vertices, 3));
geo.setAttribute('normal', undefined);
let meh = new THREE.Mesh(geo, materials.unselected);
geo.computeFaceNormals();
geo.computeVertexNormals();
meh.material.side = THREE.DoubleSide;
meh.receiveShadow = true;

View file

@ -15,6 +15,10 @@ if (mesh.object) return;
mesh.object = class MeshObject {
constructor() {
this.id = mesh.util.uuid();
}
// @returns {THREE.Object3D}
object() {
throw "object() requires implementation";

View file

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