compact THREE references

This commit is contained in:
Stewart Allen 2022-01-12 11:46:10 -05:00
commit e258e3d916
6 changed files with 52 additions and 41 deletions

View file

@ -7,6 +7,7 @@
let broker = gapp.broker; let broker = gapp.broker;
let call = broker.send; let call = broker.send;
let dbindex = [ "admin", "space" ]; let dbindex = [ "admin", "space" ];
let { Quaternion, Vector3 } = THREE;
function init() { function init() {
let stores = data.open('mesh', { stores: dbindex, version: 4 }).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 }}); api.focus({center: { x, y:-z, z:y }});
} else if (ctrlKey) { } else if (ctrlKey) {
// lay flat when ctrl clicking a selected face // 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 // 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.qrotation(q);
group.floor(); group.floor();
} else { } else {

View file

@ -20,6 +20,8 @@ gapp.register("mesh.api", [
let mesh = self.mesh = self.mesh || {}; let mesh = self.mesh = self.mesh || {};
if (mesh.api) return; if (mesh.api) return;
let { Matrix4, Vector3 } = THREE;
let space = moto.Space; let space = moto.Space;
let worker = moto.client.fn; let worker = moto.client.fn;
let util = mesh.util; let util = mesh.util;
@ -313,7 +315,7 @@ let tool = {
api.log.emit('analyzing mesh(es)...').pin(); api.log.emit('analyzing mesh(es)...').pin();
let promises = []; let promises = [];
let newmdl = []; let newmdl = [];
let mcore = new THREE.Matrix4().makeRotationX(Math.PI / 2); let mcore = new Matrix4().makeRotationX(Math.PI / 2);
for (let m of models) { for (let m of models) {
// todo - translate vertices with source model's matrix // todo - translate vertices with source model's matrix
let p = worker.model_analyze(m.id).then(data => { let p = worker.model_analyze(m.id).then(data => {
@ -425,7 +427,7 @@ let api = mesh.api = {
// sets "home" views (front, back, home, reset) // sets "home" views (front, back, home, reset)
space.platform.setCenter(center.x, -center.y, center.z); space.platform.setCenter(center.x, -center.y, center.z);
// sets camera focus // sets camera focus
space.view.setFocus(new THREE.Vector3( space.view.setFocus(new Vector3(
center.x, center.z, -center.y center.x, center.z, -center.y
)); ));
}, },

View file

@ -20,27 +20,29 @@ if (mesh.model) return;
let mapp = mesh; let mapp = mesh;
let space = moto.Space; let space = moto.Space;
let worker = moto.client.fn; let worker = moto.client.fn;
let { MeshPhongMaterial, MeshBasicMaterial } = THREE;
let { BufferGeometry, BufferAttribute, DoubleSide, Mesh } = THREE;
/** default materials **/ /** default materials **/
let materials = mesh.material = { let materials = mesh.material = {
unselected: new THREE.MeshPhongMaterial({ unselected: new MeshPhongMaterial({
side: THREE.DoubleSide, side: DoubleSide,
transparent: true, transparent: true,
shininess: 100, shininess: 100,
specular: 0x202020, specular: 0x202020,
color: 0xffff00, color: 0xffff00,
opacity: 1 opacity: 1
}), }),
selected: new THREE.MeshPhongMaterial({ selected: new MeshPhongMaterial({
side: THREE.DoubleSide, side: DoubleSide,
transparent: true, transparent: true,
shininess: 100, shininess: 100,
specular: 0x202020, specular: 0x202020,
color: 0x00ee00, color: 0x00ee00,
opacity: 1 opacity: 1
}), }),
wireframe: new THREE.MeshBasicMaterial({ wireframe: new MeshBasicMaterial({
side: THREE.DoubleSide, side: DoubleSide,
wireframe: true, wireframe: true,
color: 0x0 color: 0x0
}), }),
@ -94,11 +96,11 @@ mesh.model = class MeshModel extends mesh.object {
} }
load(vertices, indices, normals) { load(vertices, indices, normals) {
let geo = new THREE.BufferGeometry(); let geo = new BufferGeometry();
geo.setAttribute('position', new THREE.BufferAttribute(vertices, 3)); geo.setAttribute('position', new BufferAttribute(vertices, 3));
if (indices) geo.setIndex(new THREE.BufferAttribute(indices, 1)); if (indices) geo.setIndex(new BufferAttribute(indices, 1));
if (!normals) geo.computeVertexNormals(); 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.receiveShadow = true;
meh.castShadow = true; meh.castShadow = true;
meh.renderOrder = 1; meh.renderOrder = 1;
@ -113,9 +115,9 @@ mesh.model = class MeshModel extends mesh.object {
reload(vertices, indices, normals) { reload(vertices, indices, normals) {
let was = this.wireframe(false); let was = this.wireframe(false);
let geo = this.mesh.geometry; let geo = this.mesh.geometry;
geo.setAttribute('position', new THREE.BufferAttribute(vertices, 3)); geo.setAttribute('position', new BufferAttribute(vertices, 3));
geo.setAttribute('normal', undefined); 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; geo.attributes.position.needsUpdate = true;
if (!normals) geo.computeVertexNormals(); if (!normals) geo.computeVertexNormals();
// sync data to worker // sync data to worker
@ -224,7 +226,7 @@ mesh.model = class MeshModel extends mesh.object {
this.opacity({restore: true}); this.opacity({restore: true});
} }
if (bool) { 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.mesh.add(this._wire);
this.opacity({temp: opt.opacity || 0}); this.opacity({temp: opt.opacity || 0});
} }

View file

@ -18,6 +18,8 @@ if (mesh.object) return;
let space = moto.Space; let space = moto.Space;
let worker = moto.client.fn; let worker = moto.client.fn;
let { Matrix4, Vector3, Box3, Box3Helper, Quaternion, Euler } = THREE;
// broker updates generated by objects // broker updates generated by objects
let publish = { let publish = {
matrix: gapp.broker.bind("object_matrix"), matrix: gapp.broker.bind("object_matrix"),
@ -57,7 +59,7 @@ mesh.object = class MeshObject {
applyMatrix(elements) { applyMatrix(elements) {
if (elements) { if (elements) {
this.object.applyMatrix4(new THREE.Matrix4().fromArray(elements)); this.object.applyMatrix4(new Matrix4().fromArray(elements));
} }
return this; return this;
} }
@ -122,9 +124,9 @@ mesh.object = class MeshObject {
} }
rotate(x = 0, y = 0, z = 0) { rotate(x = 0, y = 0, z = 0) {
if (x) this.object.rotateOnWorldAxis(new THREE.Vector3(1,0,0), x); if (x) this.object.rotateOnWorldAxis(new Vector3(1,0,0), x);
if (y) this.object.rotateOnWorldAxis(new THREE.Vector3(0,1,0), y); if (y) this.object.rotateOnWorldAxis(new Vector3(0,1,0), y);
if (z) this.object.rotateOnWorldAxis(new THREE.Vector3(0,0,1), z); if (z) this.object.rotateOnWorldAxis(new Vector3(0,0,1), z);
this.matrixChanged(); this.matrixChanged();
return this; return this;
} }
@ -134,7 +136,7 @@ mesh.object = class MeshObject {
if (arguments.length === 0) { if (arguments.length === 0) {
return rotation; return rotation;
} }
return this.qrotation(new THREE.Quaternion().setFromEuler(new THREE.Euler(...arguments))); return this.qrotation(new Quaternion().setFromEuler(new Euler(...arguments)));
} }
qrotation(quaternion) { qrotation(quaternion) {
@ -173,11 +175,11 @@ mesh.object = class MeshObject {
} }
if (this._showBounds) { if (this._showBounds) {
let { mid, dim } = this.bounds; let { mid, dim } = this.bounds;
let b3 = new THREE.Box3().setFromCenterAndSize( let b3 = new Box3().setFromCenterAndSize(
new THREE.Vector3(mid.x, mid.y, mid.z), new Vector3(mid.x, mid.y, mid.z),
new THREE.Vector3(dim.x, dim.y, dim.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); world.add(helper);
return true; return true;
} }

View file

@ -14,6 +14,8 @@ gapp.register("mesh.util", [
let mesh = self.mesh = self.mesh || {}; let mesh = self.mesh = self.mesh || {};
if (mesh.util) return; if (mesh.util) return;
let { Matrix4, Vector3, Box3 } = THREE;
let deferFn = []; let deferFn = [];
let boundsCache = {}; let boundsCache = {};
@ -62,7 +64,7 @@ let util = mesh.util = {
// @param object {THREE.Object3D | THREE.Object3D[] | MeshObject | MeshObject[]} // @param object {THREE.Object3D | THREE.Object3D[] | MeshObject | MeshObject[]}
// @returns bounds modified for moto.Space // @returns bounds modified for moto.Space
bounds(object) { bounds(object) {
let box = new THREE.Box3(); let box = new Box3();
if (Array.isArray(object)) { if (Array.isArray(object)) {
for (let o of object) { for (let o of object) {
util.box3expand(box, o instanceof mesh.object ? o.object : o); util.box3expand(box, o instanceof mesh.object ? o.object : o);
@ -110,16 +112,16 @@ let util = mesh.util = {
let cached = boundsCache[object.id]; let cached = boundsCache[object.id];
if (!cached || cached.bkey !== bkey) { if (!cached || cached.bkey !== bkey) {
let position = geometry.attributes.position.clone(); let position = geometry.attributes.position.clone();
position.applyMatrix4(new THREE.Matrix4().extractRotation(matrix)); position.applyMatrix4(new Matrix4().extractRotation(matrix));
let bounds = new THREE.Box3().setFromBufferAttribute(position); let bounds = new Box3().setFromBufferAttribute(position);
// let scale = new THREE.Vector3().setFromMatrixScale(matrix); // let scale = new Vector3().setFromMatrixScale(matrix);
// bounds.min.multiply(scale); // bounds.min.multiply(scale);
// bounds.max.multiply(scale); // bounds.max.multiply(scale);
cached = boundsCache[object.id] = { bkey, bounds }; cached = boundsCache[object.id] = { bkey, bounds };
} }
let bt = cached.bounds.clone(); let bt = cached.bounds.clone();
let m4 = new THREE.Matrix4(); let m4 = new Matrix4();
m4.setPosition(new THREE.Vector3().setFromMatrixPosition(object.matrixWorld)); m4.setPosition(new Vector3().setFromMatrixPosition(object.matrixWorld));
bt.applyMatrix4(m4); bt.applyMatrix4(m4);
box3.union(bt); box3.union(bt);
} }

View file

@ -17,8 +17,10 @@ gapp.finalize("mesh.work", [
"add.three", // dep: add.three "add.three", // dep: add.three
]); ]);
let { Matrix4, Vector3, BufferGeometry, BufferAttribute, computeFaceNormal } = THREE;
// compensation for space/world/platform rotation // 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 { client, worker } = moto;
let cache = {}; let cache = {};
@ -34,7 +36,7 @@ function cacheUpdate(id, data) {
function translate_encode(id, matrix) { function translate_encode(id, matrix) {
let rec = cache[id]; let rec = cache[id];
let geo = rec.geo.clone(); 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; return geo.attributes.position.array;
} }
@ -55,9 +57,9 @@ function analyze(id, opt = {}) {
let model = { let model = {
load(data) { load(data) {
let { vertices, indices, name, id } = data; let { vertices, indices, name, id } = data;
let geo = new THREE.BufferGeometry(); let geo = new BufferGeometry();
geo.setAttribute('position', new THREE.BufferAttribute(vertices, 3)); geo.setAttribute('position', new BufferAttribute(vertices, 3));
if (indices) geo.setIndex(new THREE.BufferAttribute(indices, 1)); if (indices) geo.setIndex(new BufferAttribute(indices, 1));
cacheUpdate(id, { name, geo, matrix: core_matrix.clone() }); cacheUpdate(id, { name, geo, matrix: core_matrix.clone() });
}, },
@ -156,10 +158,10 @@ let file = {
for (let rec of recs) { for (let rec of recs) {
let { id, matrix, file, varr } = rec; let { id, matrix, file, varr } = rec;
for (let i=0, l=varr.length; i<l;) { for (let i=0, l=varr.length; i<l;) {
let p0 = new THREE.Vector3(varr[i++], varr[i++], varr[i++]); let p0 = new Vector3(varr[i++], varr[i++], varr[i++]);
let p1 = new THREE.Vector3(varr[i++], varr[i++], varr[i++]); let p1 = new Vector3(varr[i++], varr[i++], varr[i++]);
let p2 = new THREE.Vector3(varr[i++], varr[i++], varr[i++]); let p2 = new Vector3(varr[i++], varr[i++], varr[i++]);
let norm = THREE.computeFaceNormal(p0, p1, p2); let norm = computeFaceNormal(p0, p1, p2);
dat.setFloat32(pos + 0, norm.x, true); dat.setFloat32(pos + 0, norm.x, true);
dat.setFloat32(pos + 4, norm.y, true); dat.setFloat32(pos + 4, norm.y, true);
dat.setFloat32(pos + 8, norm.z, true); dat.setFloat32(pos + 8, norm.z, true);