From e258e3d916ce357182226d015b17a8341f1484b4 Mon Sep 17 00:00:00 2001 From: Stewart Allen Date: Wed, 12 Jan 2022 11:46:10 -0500 Subject: [PATCH] compact THREE references --- src/main/mesh.js | 5 +++-- src/mesh/api.js | 6 ++++-- src/mesh/model.js | 28 +++++++++++++++------------- src/mesh/object.js | 20 +++++++++++--------- src/mesh/util.js | 14 ++++++++------ src/mesh/work.js | 20 +++++++++++--------- 6 files changed, 52 insertions(+), 41 deletions(-) diff --git a/src/main/mesh.js b/src/main/mesh.js index b64abdd7..c2a69ac7 100644 --- a/src/main/mesh.js +++ b/src/main/mesh.js @@ -7,6 +7,7 @@ let broker = gapp.broker; let call = broker.send; let dbindex = [ "admin", "space" ]; +let { Quaternion, Vector3 } = THREE; function init() { let stores = data.open('mesh', { stores: dbindex, version: 4 }).init(), @@ -259,9 +260,9 @@ function space_init(data) { api.focus({center: { x, y:-z, z:y }}); } else if (ctrlKey) { // lay flat when ctrl clicking a selected face - let q = new THREE.Quaternion(); + let q = new Quaternion(); // find intersecting point, look "up" on Z and rotate to face that - q.setFromUnitVectors(int.face.normal, new THREE.Vector3(0,0,-1)); + q.setFromUnitVectors(int.face.normal, new Vector3(0,0,-1)); group.qrotation(q); group.floor(); } else { diff --git a/src/mesh/api.js b/src/mesh/api.js index e97aabf9..a733326c 100644 --- a/src/mesh/api.js +++ b/src/mesh/api.js @@ -20,6 +20,8 @@ gapp.register("mesh.api", [ let mesh = self.mesh = self.mesh || {}; if (mesh.api) return; +let { Matrix4, Vector3 } = THREE; + let space = moto.Space; let worker = moto.client.fn; let util = mesh.util; @@ -313,7 +315,7 @@ let tool = { api.log.emit('analyzing mesh(es)...').pin(); let promises = []; let newmdl = []; - let mcore = new THREE.Matrix4().makeRotationX(Math.PI / 2); + let mcore = new Matrix4().makeRotationX(Math.PI / 2); for (let m of models) { // todo - translate vertices with source model's matrix let p = worker.model_analyze(m.id).then(data => { @@ -425,7 +427,7 @@ let api = mesh.api = { // sets "home" views (front, back, home, reset) space.platform.setCenter(center.x, -center.y, center.z); // sets camera focus - space.view.setFocus(new THREE.Vector3( + space.view.setFocus(new Vector3( center.x, center.z, -center.y )); }, diff --git a/src/mesh/model.js b/src/mesh/model.js index 8b672331..54ccf487 100644 --- a/src/mesh/model.js +++ b/src/mesh/model.js @@ -20,27 +20,29 @@ if (mesh.model) return; let mapp = mesh; let space = moto.Space; let worker = moto.client.fn; +let { MeshPhongMaterial, MeshBasicMaterial } = THREE; +let { BufferGeometry, BufferAttribute, DoubleSide, Mesh } = THREE; /** default materials **/ let materials = mesh.material = { - unselected: new THREE.MeshPhongMaterial({ - side: THREE.DoubleSide, + unselected: new MeshPhongMaterial({ + side: DoubleSide, transparent: true, shininess: 100, specular: 0x202020, color: 0xffff00, opacity: 1 }), - selected: new THREE.MeshPhongMaterial({ - side: THREE.DoubleSide, + selected: new MeshPhongMaterial({ + side: DoubleSide, transparent: true, shininess: 100, specular: 0x202020, color: 0x00ee00, opacity: 1 }), - wireframe: new THREE.MeshBasicMaterial({ - side: THREE.DoubleSide, + wireframe: new MeshBasicMaterial({ + side: DoubleSide, wireframe: true, color: 0x0 }), @@ -94,11 +96,11 @@ mesh.model = class MeshModel extends mesh.object { } load(vertices, indices, normals) { - let geo = new THREE.BufferGeometry(); - geo.setAttribute('position', new THREE.BufferAttribute(vertices, 3)); - if (indices) geo.setIndex(new THREE.BufferAttribute(indices, 1)); + let geo = new BufferGeometry(); + geo.setAttribute('position', new BufferAttribute(vertices, 3)); + if (indices) geo.setIndex(new BufferAttribute(indices, 1)); if (!normals) geo.computeVertexNormals(); - let meh = this.mesh = new THREE.Mesh(geo, materials.unselected); + let meh = this.mesh = new Mesh(geo, materials.unselected); meh.receiveShadow = true; meh.castShadow = true; meh.renderOrder = 1; @@ -113,9 +115,9 @@ mesh.model = class MeshModel extends mesh.object { reload(vertices, indices, normals) { let was = this.wireframe(false); let geo = this.mesh.geometry; - geo.setAttribute('position', new THREE.BufferAttribute(vertices, 3)); + geo.setAttribute('position', new BufferAttribute(vertices, 3)); geo.setAttribute('normal', undefined); - if (indices) geo.setIndex(new THREE.BufferAttribute(indices, 1)); + if (indices) geo.setIndex(new BufferAttribute(indices, 1)); geo.attributes.position.needsUpdate = true; if (!normals) geo.computeVertexNormals(); // sync data to worker @@ -224,7 +226,7 @@ mesh.model = class MeshModel extends mesh.object { this.opacity({restore: true}); } if (bool) { - this._wire = new THREE.Mesh(this.mesh.geometry.shallowClone(), materials.wireframe); + this._wire = new Mesh(this.mesh.geometry.shallowClone(), materials.wireframe); this.mesh.add(this._wire); this.opacity({temp: opt.opacity || 0}); } diff --git a/src/mesh/object.js b/src/mesh/object.js index 43861914..d8350979 100644 --- a/src/mesh/object.js +++ b/src/mesh/object.js @@ -18,6 +18,8 @@ if (mesh.object) return; let space = moto.Space; let worker = moto.client.fn; +let { Matrix4, Vector3, Box3, Box3Helper, Quaternion, Euler } = THREE; + // broker updates generated by objects let publish = { matrix: gapp.broker.bind("object_matrix"), @@ -57,7 +59,7 @@ mesh.object = class MeshObject { applyMatrix(elements) { if (elements) { - this.object.applyMatrix4(new THREE.Matrix4().fromArray(elements)); + this.object.applyMatrix4(new Matrix4().fromArray(elements)); } return this; } @@ -122,9 +124,9 @@ mesh.object = class MeshObject { } rotate(x = 0, y = 0, z = 0) { - if (x) this.object.rotateOnWorldAxis(new THREE.Vector3(1,0,0), x); - if (y) this.object.rotateOnWorldAxis(new THREE.Vector3(0,1,0), y); - if (z) this.object.rotateOnWorldAxis(new THREE.Vector3(0,0,1), z); + 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(); return this; } @@ -134,7 +136,7 @@ mesh.object = class MeshObject { if (arguments.length === 0) { return rotation; } - return this.qrotation(new THREE.Quaternion().setFromEuler(new THREE.Euler(...arguments))); + return this.qrotation(new Quaternion().setFromEuler(new Euler(...arguments))); } qrotation(quaternion) { @@ -173,11 +175,11 @@ mesh.object = class MeshObject { } if (this._showBounds) { let { mid, dim } = this.bounds; - let b3 = new THREE.Box3().setFromCenterAndSize( - new THREE.Vector3(mid.x, mid.y, mid.z), - new THREE.Vector3(dim.x, dim.y, dim.z) + let b3 = new Box3().setFromCenterAndSize( + new Vector3(mid.x, mid.y, mid.z), + new Vector3(dim.x, dim.y, dim.z) ); - let helper = this._boundsBox = new THREE.Box3Helper(b3, 0x555555); + let helper = this._boundsBox = new Box3Helper(b3, 0x555555); world.add(helper); return true; } diff --git a/src/mesh/util.js b/src/mesh/util.js index 4516e5bc..6ee38ac4 100644 --- a/src/mesh/util.js +++ b/src/mesh/util.js @@ -14,6 +14,8 @@ gapp.register("mesh.util", [ let mesh = self.mesh = self.mesh || {}; if (mesh.util) return; +let { Matrix4, Vector3, Box3 } = THREE; + let deferFn = []; let boundsCache = {}; @@ -62,7 +64,7 @@ let util = mesh.util = { // @param object {THREE.Object3D | THREE.Object3D[] | MeshObject | MeshObject[]} // @returns bounds modified for moto.Space bounds(object) { - let box = new THREE.Box3(); + let box = new Box3(); if (Array.isArray(object)) { for (let o of object) { util.box3expand(box, o instanceof mesh.object ? o.object : o); @@ -110,16 +112,16 @@ let util = mesh.util = { let cached = boundsCache[object.id]; if (!cached || cached.bkey !== bkey) { let position = geometry.attributes.position.clone(); - position.applyMatrix4(new THREE.Matrix4().extractRotation(matrix)); - let bounds = new THREE.Box3().setFromBufferAttribute(position); - // let scale = new THREE.Vector3().setFromMatrixScale(matrix); + position.applyMatrix4(new Matrix4().extractRotation(matrix)); + let bounds = new Box3().setFromBufferAttribute(position); + // let scale = new Vector3().setFromMatrixScale(matrix); // bounds.min.multiply(scale); // bounds.max.multiply(scale); cached = boundsCache[object.id] = { bkey, bounds }; } let bt = cached.bounds.clone(); - let m4 = new THREE.Matrix4(); - m4.setPosition(new THREE.Vector3().setFromMatrixPosition(object.matrixWorld)); + let m4 = new Matrix4(); + m4.setPosition(new Vector3().setFromMatrixPosition(object.matrixWorld)); bt.applyMatrix4(m4); box3.union(bt); } diff --git a/src/mesh/work.js b/src/mesh/work.js index 34fa5ce3..2a5d2407 100644 --- a/src/mesh/work.js +++ b/src/mesh/work.js @@ -17,8 +17,10 @@ gapp.finalize("mesh.work", [ "add.three", // dep: add.three ]); +let { Matrix4, Vector3, BufferGeometry, BufferAttribute, computeFaceNormal } = THREE; + // compensation for space/world/platform rotation -let core_matrix = new THREE.Matrix4().makeRotationX(Math.PI / 2); +let core_matrix = new Matrix4().makeRotationX(Math.PI / 2); let { client, worker } = moto; let cache = {}; @@ -34,7 +36,7 @@ function cacheUpdate(id, data) { function translate_encode(id, matrix) { let rec = cache[id]; let geo = rec.geo.clone(); - geo.applyMatrix4(core_matrix.clone().multiply( new THREE.Matrix4().fromArray(matrix) )); + geo.applyMatrix4(core_matrix.clone().multiply( new Matrix4().fromArray(matrix) )); return geo.attributes.position.array; } @@ -55,9 +57,9 @@ function analyze(id, opt = {}) { let model = { load(data) { let { vertices, indices, name, id } = data; - let geo = new THREE.BufferGeometry(); - geo.setAttribute('position', new THREE.BufferAttribute(vertices, 3)); - if (indices) geo.setIndex(new THREE.BufferAttribute(indices, 1)); + let geo = new BufferGeometry(); + geo.setAttribute('position', new BufferAttribute(vertices, 3)); + if (indices) geo.setIndex(new BufferAttribute(indices, 1)); cacheUpdate(id, { name, geo, matrix: core_matrix.clone() }); }, @@ -156,10 +158,10 @@ let file = { for (let rec of recs) { let { id, matrix, file, varr } = rec; for (let i=0, l=varr.length; i