speed up new indexing, remove unneeded / redundant data

This commit is contained in:
Stewart Allen 2022-04-13 21:15:07 -04:00
commit 7baecb20ac

View file

@ -149,20 +149,17 @@ mesh.tool = class MeshTool {
// n = normal // n = normal
// af = adjacent face index // af = adjacent face index
index(vertices) { index(vertices) {
this.vertices = this.checkVertices(vertices, 3);
const prec = this.precision; const prec = this.precision;
const vcount = vertices.length / 3; const vcount = vertices.length / 3;
const fcount = vcount / 3; const fcount = vcount / 3;
const vround = vertices.map(v => (v * prec) | 0); const vround = vertices.map(v => (v * prec) | 0);
// new compressed vertex array
const verts = [];
// new compressed normals array
const norms = [];
// side records [ fcount, f1, f2 ] // side records [ fcount, f1, f2 ]
const sides = new Uint32Array(fcount * 3); const sides = new Uint32Array(fcount * 3);
// side extended records when face count exceeds 2 (bad mesh) // side extended records when face count exceeds 2 (bad mesh)
const sideExt = {}; const sideExt = {};
// face record array [ v0, v1, v2, ni, s0, s1, s2 ] // face record array [ ni, s0, s1, s2 ]
const faces = new Uint32Array(fcount * 7); const faces = new Float32Array(fcount * 6);
// vertex key to vertex index // vertex key to vertex index
const vimap = {}; const vimap = {};
// normal key to normal index // normal key to normal index
@ -184,25 +181,17 @@ mesh.tool = class MeshTool {
let vid = vimap[key]; let vid = vimap[key];
if (vid === undefined) { if (vid === undefined) {
vid = vimap[key] = vn++; vid = vimap[key] = vn++;
verts.push(x / prec, y / prec, z / prec);
} }
vinds[vi++] = vid; vinds[vi++] = vid;
// completed record for a face // completed record for a face
if (vi === 3) { if (vi === 3) {
// create indexed unique normal map // create indexed unique normal map
const vn = THREE.computeFaceNormal(...vects); const cfn = THREE.computeFaceNormal(...vects);
const vnk = [ vn.x, vn.y, vn.z ].map(v => (v * prec) | 0).join(',');
let vni = nimap[vnk];
if (vni === undefined) {
vni = nimap[vnk] = norms.length / 3;
norms.push(vn.x, vn.y, vn.z);
}
const [ v0, v1, v2 ] = vinds; const [ v0, v1, v2 ] = vinds;
// create consistent side order for index key // create consistent side order for index key
const s0 = (v0 < v1 ? [ v0, v1 ] : [ v1, v0 ]).join(','); const s0 = (v0 < v1 ? [ v0, v1 ] : [ v1, v0 ]).join(',');
const s1 = (v1 < v2 ? [ v1, v2 ] : [ v2, v1 ]).join(','); const s1 = (v1 < v2 ? [ v1, v2 ] : [ v2, v1 ]).join(',');
const s2 = (v2 < v0 ? [ v2, v0 ] : [ v0, v2 ]).join(','); const s2 = (v2 < v0 ? [ v2, v0 ] : [ v0, v2 ]).join(',');
const vv = [ v0, v1, v1, v2, v2, v0 ];
// store face indexes into sdrec array for each side // store face indexes into sdrec array for each side
const smap = [ s0, s1, s2 ].map(key => { const smap = [ s0, s1, s2 ].map(key => {
let sid = simap[key]; let sid = simap[key];
@ -218,10 +207,9 @@ mesh.tool = class MeshTool {
} }
return sid; return sid;
}); });
faces[fi++] = v0; faces[fi++] = cfn.x;
faces[fi++] = v1; faces[fi++] = cfn.y;
faces[fi++] = v2; faces[fi++] = cfn.z;
faces[fi++] = vni;
faces[fi++] = smap[0]; faces[fi++] = smap[0];
faces[fi++] = smap[1]; faces[fi++] = smap[1];
faces[fi++] = smap[2]; faces[fi++] = smap[2];
@ -230,9 +218,8 @@ mesh.tool = class MeshTool {
} }
} }
this.indexed = { this.indexed = {
faces, verts, norms, sides, sideExt faces, sides, sideExt
}; };
// console.log({ fcount, faces, sides, sideExt, norms, vround, verts });
} }
getAdjacentFaces(face) { getAdjacentFaces(face) {