diff --git a/src/main/mesh.js b/src/main/mesh.js index 1664fd59..0a149ffd 100644 --- a/src/main/mesh.js +++ b/src/main/mesh.js @@ -197,8 +197,12 @@ function space_init(data) { moto.client.fn.model_heal(m.id) .then(data => { if (data) { - m.wireframe(false); - m.reload(data.vertices.toFloat32()); + console.log({data}); + m.load( + // Float32Array.from(data.vertices.toFloat32()), + Float32Array.from([...data.v]), + Uint32Array.from([...data.f]) + ); } }); } diff --git a/src/mesh/group.js b/src/mesh/group.js index 83c04044..2469c249 100644 --- a/src/mesh/group.js +++ b/src/mesh/group.js @@ -89,6 +89,11 @@ mesh.group = class MeshGroup extends mesh.object { return this; } + // update(oldmesh, newmesh) { + // this.group.remove(oldmesh); + // this.group.add(newmesh); + // } + // center objects to group bounds // dependent on first being added to world/scene centerModels() { diff --git a/src/mesh/model.js b/src/mesh/model.js index 91b9f896..64d1fffc 100644 --- a/src/mesh/model.js +++ b/src/mesh/model.js @@ -90,11 +90,14 @@ mesh.model = class MeshModel extends mesh.object { } load(vertices, indices) { + this.wireframe(false); let geo = new THREE.BufferGeometry(); - if (indices) geo.setIndex(new THREE.BufferAttribute(indices, 1)); geo.setAttribute('position', new THREE.BufferAttribute(vertices, 3)); - let meh = new THREE.Mesh(geo, materials.unselected); + if (indices) geo.setIndex(new THREE.BufferAttribute(indices, 1)); geo.computeVertexNormals(); + // reuse mesh if this is a "reload" + let meh = this.mesh || new THREE.Mesh(geo, materials.unselected); + meh.geometry = geo; meh.receiveShadow = true; meh.castShadow = true; meh.renderOrder = 1; @@ -102,21 +105,29 @@ mesh.model = class MeshModel extends mesh.object { meh.model = this; // sync data to worker worker.model_load({id: this.id, name: this.file, vertices, indices}); + // update mesh in group + // if (this.group) this.group.update(oldmesh, meh); return meh; } - reload(vertices, indices) { - let geo = this.mesh.geometry; - geo.attributes.position.array = vertices; - geo.attributes.position.needsUpdate = true; - geo.setAttribute('normal', undefined); - if (indices) { - geo.setIndex(new THREE.BufferAttribute(indices, 1)); - } - geo.computeVertexNormals(); - worker.model_load({id: this.id, name: this.name, vertices, indices}); + optimize() { + let ind = THREE.BufferGeometryUtils.mergeVertices(this.mesh.geometry); + console.log({geo: this.mesh.geometry, ind}); } + // reload(vertices, indices) { + // this.wireframe(false); + // let geo = this.mesh.geometry; + // geo.attributes.position.array = vertices; + // geo.attributes.position.needsUpdate = true; + // geo.setAttribute('normal', undefined); + // if (indices) { + // geo.setIndex(new THREE.BufferAttribute(indices, 1)); + // } + // geo.computeVertexNormals(); + // worker.model_load({id: this.id, name: this.name, vertices, indices}); + // } + get group() { return this._group; } diff --git a/src/mesh/tool.js b/src/mesh/tool.js index 4cd82c37..0e705ce8 100644 --- a/src/mesh/tool.js +++ b/src/mesh/tool.js @@ -13,7 +13,6 @@ if (mesh.tool) return; /** tool for identiying defects and healing them **/ mesh.tool = class MeshTool { constructor(params = {}) { - this.debug = params.debug; this.precision = Math.pow(10, params.precision || 6); if (params.vertices) { this.setData(params.vertices, params.faces); @@ -38,10 +37,8 @@ mesh.tool = class MeshTool { if (vertices.length % 3 !== 0) { throw "invalid vertices"; } - let TEST = this.debug; // when face/index data is missing, vertices need to be normalized if (!faces) { - if (TEST) console.time('generate faces'); faces = []; let fcac = {}; // seen face hash let fnew = []; // accumulate vertex triplets @@ -49,7 +46,7 @@ mesh.tool = class MeshTool { let hash = {}; // find vertex matches let prec = this.precision; let dups = 0; - // let cull = 0; + let cull = 0; for (let i=0, l=vertices.length; i v*3); } this.vertices = vertices; this.faces = faces; @@ -112,9 +113,6 @@ mesh.tool = class MeshTool { * connect lines into polys. earcut polys into new faces. */ heal() { - let TEST = this.debug; - if (TEST) console.time('heal'); - let vertices = this.vertices; let faces = this.faces; let hash = {}; // key to line map @@ -131,7 +129,7 @@ mesh.tool = class MeshTool { addFace(face) { if (this.faces.indexOf(face) < 0) { this.faces.push(face); - } else if (TEST) { + } else if (false) { console.log('adding face to line twice', this, face); } return this; @@ -250,7 +248,7 @@ mesh.tool = class MeshTool { let tmp = adjacent[0].v1; adjacent[0].v1 = adjacent[0].v2; adjacent[0].v2 = tmp; - if (TEST > 1) console.log('chirality mismatch fixed'); + // console.log('chirality mismatch fixed'); } line = adjacent[0]; } else if (adjacent.length === 2) { @@ -259,7 +257,7 @@ mesh.tool = class MeshTool { adjacent[0] : adjacent[1] } else { - if (TEST > 1) console.log('error adjacent', line, adjacent, loops.length, loop.length); + // console.log('error adjacent', line, adjacent, loops.length, loop.length); line.split = adjacent; line = adjacent[0]; } @@ -306,11 +304,11 @@ mesh.tool = class MeshTool { // store invalid loops for display / culling this.shorts = loops.filter(l => l && l.length <= 2); - if (TEST) console.log({edges, loops, shorts: this.shorts}); + // console.log({edges, loops, shorts: this.shorts}); // filter out null and short loops this.loops = loops = loops.filter(l => l && l.length > 2); - if (TEST > 1) console.log({loops_filtered: loops}); + // console.log({loops_filtered: loops}); // rotate/flatten to Z plane and use earcut to generate faces // because earcut emits point indexes, no need to un-rotate @@ -367,7 +365,7 @@ mesh.tool = class MeshTool { let fpoints = points.flat(); let ec = earcut(fpoints, undefined, 3); - if (TEST > 2) console.log({points, fpoints, ec, dx, dy, dz}); + // console.log({points, fpoints, ec, dx, dy, dz}); for (let point of ec) { faces.push(pindex[point]); @@ -381,8 +379,6 @@ mesh.tool = class MeshTool { } this.newFaces = this.faces.length - faceCount; - if (TEST) console.log({newFaces: this.newFaces}); - if (TEST) console.timeEnd('heal'); return this; } }; diff --git a/src/mesh/work.js b/src/mesh/work.js index 47f55125..4373cd15 100644 --- a/src/mesh/work.js +++ b/src/mesh/work.js @@ -29,9 +29,10 @@ function cacheUpdate(id, data) { let model = { load(data) { - let { vertices, name, id } = 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)); cacheUpdate(id, { name, geo, matrix: core_matrix.clone() }); }, @@ -47,15 +48,20 @@ let model = { heal(id) { dbug.log({healing: id}); + let geo = cache[id].geo; let tool = new mesh.tool({ - vertices: cache[id].geo.attributes.position.array + vertices: geo.attributes.position.array, + faces: geo.index ? geo.index.array : undefined, + debug: false }); dbug.log('...imported data'); tool.heal(); dbug.log('...healed'); dbug.log(tool); - return tool.newFaces ? { + return true || tool.newFaces ? { vertices: tool.unrolled(), + v: tool.vertices, + f: tool.faces.map(f => f/3) } : 0; } };