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

@ -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
));
},

View file

@ -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});
}

View file

@ -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;
}

View file

@ -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);
}

View file

@ -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<l;) {
let p0 = new THREE.Vector3(varr[i++], varr[i++], varr[i++]);
let p1 = new THREE.Vector3(varr[i++], varr[i++], varr[i++]);
let p2 = new THREE.Vector3(varr[i++], varr[i++], varr[i++]);
let norm = THREE.computeFaceNormal(p0, p1, p2);
let p0 = new Vector3(varr[i++], varr[i++], varr[i++]);
let p1 = new Vector3(varr[i++], varr[i++], varr[i++]);
let p2 = new Vector3(varr[i++], varr[i++], varr[i++]);
let norm = computeFaceNormal(p0, p1, p2);
dat.setFloat32(pos + 0, norm.x, true);
dat.setFloat32(pos + 4, norm.y, true);
dat.setFloat32(pos + 8, norm.z, true);