From 098fd9bb7e2d59afc0daef370f41b714371a7fdc Mon Sep 17 00:00:00 2001 From: Stewart Allen Date: Wed, 5 Jan 2022 09:29:27 -0500 Subject: [PATCH] add option to load normals with model --- src/main/mesh.js | 12 +++--------- src/mesh/api.js | 1 + src/mesh/model.js | 12 ++++++------ src/mesh/work.js | 4 +--- 4 files changed, 11 insertions(+), 18 deletions(-) diff --git a/src/main/mesh.js b/src/main/mesh.js index 4d937e31..baa36f5a 100644 --- a/src/main/mesh.js +++ b/src/main/mesh.js @@ -203,16 +203,10 @@ function space_init(data) { moto.client.fn.model_heal(m.id) .then(data => { if (data) { - // api.group.new([new mesh.model({ - // file: 'healed', - // mesh: data.vertices.toFloat32(), - // // mesh: Float32Array.from([...data.v]), - // // index: Uint32Array.from([...data.f]) - // })]); m.reload( - Float32Array.from(data.vertices), - // Float32Array.from([...data.v]), - // Uint32Array.from([...data.f]) + data.vertices, + data.indices, + data.normals ); } }); diff --git a/src/mesh/api.js b/src/mesh/api.js index 351db8ef..4e5c6ebc 100644 --- a/src/mesh/api.js +++ b/src/mesh/api.js @@ -329,6 +329,7 @@ let util = mesh.util = { // bounding box workaround adapted from: // https://discourse.threejs.org/t/bounding-box-bigger-than-concave-object-extrudegeometry/26073/2 + // https://discourse.threejs.org/t/invalid-bounds-generated-for-some-orientations/33205 box3expand(box3, object) { let geometry = object.geometry; object.updateWorldMatrix(geometry ? true : false, false); diff --git a/src/mesh/model.js b/src/mesh/model.js index 127fe452..5f1dca53 100644 --- a/src/mesh/model.js +++ b/src/mesh/model.js @@ -49,7 +49,7 @@ let materials = mesh.material = { mesh.model = class MeshModel extends mesh.object { constructor(data, id) { super(id); - let { file, mesh, index, vertices, indices } = data; + let { file, mesh, vertices, indices, normals } = data; if (!mesh) { dbug.error(`'${file}' missing mesh data`); @@ -62,7 +62,7 @@ mesh.model = class MeshModel extends mesh.object { if (dot > 0) file = text.substring(0, dot); this.file = file || 'unnamed'; - this.load(mesh || vertices, index || indices); + this.load(mesh || vertices, indices, normals); // persist in db so it can be restored on page load mapp.db.space.put(this.id, { file, mesh }); @@ -89,11 +89,11 @@ mesh.model = class MeshModel extends mesh.object { }); } - load(vertices, indices) { + 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)); - geo.computeVertexNormals(); + if (!normals) geo.computeVertexNormals(); let meh = this.mesh = new THREE.Mesh(geo, materials.unselected); meh.receiveShadow = true; meh.castShadow = true; @@ -104,14 +104,14 @@ mesh.model = class MeshModel extends mesh.object { worker.model_load({id: this.id, name: this.file, vertices, indices}); } - reload(vertices, indices) { + reload(vertices, indices, normals) { this.wireframe(false); let geo = this.mesh.geometry; geo.setAttribute('position', new THREE.BufferAttribute(vertices, 3)); geo.setAttribute('normal', undefined); if (indices) geo.setIndex(new THREE.BufferAttribute(indices, 1)); geo.attributes.position.needsUpdate = true; - geo.computeVertexNormals(); + if (!normals) geo.computeVertexNormals(); // sync data to worker worker.model_load({id: this.id, name: this.name, vertices, indices}); } diff --git a/src/mesh/work.js b/src/mesh/work.js index dbff6f2e..b0134c32 100644 --- a/src/mesh/work.js +++ b/src/mesh/work.js @@ -59,9 +59,7 @@ let model = { dbug.log('...healed'); dbug.log(tool); return true || tool.newFaces ? { - vertices: tool.unrolled(), - // v: tool.vertices, - // f: tool.faces.map(f => f/3) + vertices: tool.unrolled().toFloat32(), } : 0; } };