From 2f2cb85349c533a0308ae7ab1611e71ac26b2825 Mon Sep 17 00:00:00 2001 From: Stewart Allen Date: Sat, 1 Jan 2022 22:39:49 -0500 Subject: [PATCH] prepare to sync models to worker(s) --- src/main/mesh.js | 6 +----- src/mesh/api.js | 51 +++++++++++++++++++++++++++------------------- src/mesh/group.js | 1 + src/mesh/model.js | 5 ++--- src/mesh/object.js | 4 ++++ src/mesh/work.js | 7 +++---- 6 files changed, 41 insertions(+), 33 deletions(-) diff --git a/src/main/mesh.js b/src/main/mesh.js index 5d5fed1d..5dc82b2a 100644 --- a/src/main/mesh.js +++ b/src/main/mesh.js @@ -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 }); diff --git a/src/mesh/api.js b/src/mesh/api.js index cf42c15a..b35f98eb 100644 --- a/src/mesh/api.js +++ b/src/mesh/api.js @@ -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); + })(); diff --git a/src/mesh/group.js b/src/mesh/group.js index c3bddb22..57ea35fa 100644 --- a/src/mesh/group.js +++ b/src/mesh/group.js @@ -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 || {}; diff --git a/src/mesh/model.js b/src/mesh/model.js index a8babaf2..97b6b793 100644 --- a/src/mesh/model.js +++ b/src/mesh/model.js @@ -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; diff --git a/src/mesh/object.js b/src/mesh/object.js index f7c6b713..51811a13 100644 --- a/src/mesh/object.js +++ b/src/mesh/object.js @@ -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"; diff --git a/src/mesh/work.js b/src/mesh/work.js index 86ed010e..16d78bf5 100644 --- a/src/mesh/work.js +++ b/src/mesh/work.js @@ -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