From a753e12d3afc460493bc4d71b039e0e90493f904 Mon Sep 17 00:00:00 2001 From: Stewart Allen Date: Sat, 15 Oct 2022 14:36:25 -0400 Subject: [PATCH] flatten layer path encodings. became single element array a long time ago --- src/kiri/codec.js | 30 +++++++++++++--------------- src/kiri/layers.js | 6 +++--- src/kiri/stack.js | 50 ++++++++++++++++++++++------------------------ 3 files changed, 41 insertions(+), 45 deletions(-) diff --git a/src/kiri/codec.js b/src/kiri/codec.js index 24c9b243..19d546ae 100644 --- a/src/kiri/codec.js +++ b/src/kiri/codec.js @@ -331,6 +331,7 @@ kiri.Layers.prototype.encode = function(state) { type: TYPE.LAYERS, layers: Object.keys(this.layers), data: Object.values(this.layers).map(layer => { + const paths = layer.paths; return { // defaults off: layer.off, @@ -343,14 +344,12 @@ kiri.Layers.prototype.encode = function(state) { norms: codec.allocFloat32Array(layer.norms, zeros), cface: layer.cface || codec.undef, // 3D lines - paths: layer.paths.map(lp => { - return { - z: lp.z, - index: lp.index, - faces: codec.allocFloat32Array(lp.faces, zeros), - norms: codec.allocFloat32Array(lp.norms, zeros) - } - }), + paths: paths ? { + z: paths.z, + index: paths.index, + faces: codec.allocFloat32Array(paths.faces, zeros), + norms: codec.allocFloat32Array(paths.norms, zeros) + } : undefined, cpath: layer.cpath || codec.undef } }) @@ -366,6 +365,7 @@ registerDecoder(TYPE.LAYERS, function(v, state) { for (let i=0; i { - return { - z: lp.z, - index: lp.index, - faces: codec.allocFloat32Array(lp.faces), - norms: codec.allocFloat32Array(lp.norms) - }; - }), + paths: paths ? { + z: paths.z, + index: paths.index, + faces: codec.allocFloat32Array(paths.faces), + norms: codec.allocFloat32Array(paths.norms) + } : undefined, cpath: data.cpath }; // fixup null -> Infinity in material counts (JSON stringify sucks) diff --git a/src/kiri/layers.js b/src/kiri/layers.js index df2bbb4c..b16d9060 100644 --- a/src/kiri/layers.js +++ b/src/kiri/layers.js @@ -56,7 +56,7 @@ class Layers { faces: [], // triangles for areas and flats norms: undefined, // flats vertex normals cface: undefined, // flats face color indices - paths: [], // 3d extrusion / tube paths (used to be an array, now merged - FIX) + paths: undefined, // 3d extrusion / tube paths (used to be an array, now merged - FIX) cpath: undefined, // path colors indices color: colors || { check: 0, @@ -216,7 +216,7 @@ class Layers { const z = opts.z || poly.getZ(); const { faces, normals } = poly.toPath3D(offset, height, 0); const cur = this.current; - const one = cur.paths[0]; + const one = cur.paths; if (one) { // merge all contour geometry for massive speed gain // todo: recode to concat all geometries at once, and not incrementally @@ -242,7 +242,7 @@ class Layers { one.norms.appendAll(normals); } } else { - cur.paths.push({ index: [], faces, z, norms: normals }); + cur.paths = { faces, z, norms: normals }; if (opts.color) { cur.cpath = [ Object.assign({ start: 0, count: Infinity }, opts.color) ]; } diff --git a/src/kiri/stack.js b/src/kiri/stack.js index f960cd46..9fdf9c45 100644 --- a/src/kiri/stack.js +++ b/src/kiri/stack.js @@ -186,7 +186,7 @@ class Stack { group.add(mesh); mats.appendAll(mat); } - if (paths.length) { + if (paths) { const mat = []; if (cpath) { cpath.forEach(c => { mat.push(newMat(c)) }); @@ -195,31 +195,29 @@ class Stack { } mat.forEach(m => m.visible = defstate); // const mat = newMat(data); - paths.forEach((path, i) => { - const { index, faces, norms, z, colors } = path; - const geo = new THREE.BufferGeometry(); - geo.setAttribute('position', new THREE.BufferAttribute(faces, 3)); - if (index.length) { - geo.setIndex(index); - } - if (norms) { - geo.setAttribute('normal', new THREE.BufferAttribute(norms, 3)); - // geo.normalizeNormals(); - } else { - geo.computeVertexNormals(); - } - if (cpath) { - cpath.forEach((c, i) => geo.addGroup(c.start, c.count, i)); - } else { - geo.addGroup(0, Infinity, 0); - } - const mesh = new THREE.Mesh(geo, mat); - mesh.position.z = z; - // mesh.castShadow = true; - // mesh.receiveShadow = true; - meshes.push(mesh); - group.add(mesh); - }); + const { index, faces, norms, z, colors } = paths; + const geo = new THREE.BufferGeometry(); + geo.setAttribute('position', new THREE.BufferAttribute(faces, 3)); + if (index && index.length) { + geo.setIndex(index); + } + if (norms) { + geo.setAttribute('normal', new THREE.BufferAttribute(norms, 3)); + // geo.normalizeNormals(); + } else { + geo.computeVertexNormals(); + } + if (cpath) { + cpath.forEach((c, i) => geo.addGroup(c.start, c.count, i)); + } else { + geo.addGroup(0, Infinity, 0); + } + const mesh = new THREE.Mesh(geo, mat); + mesh.position.z = z; + // mesh.castShadow = true; + // mesh.receiveShadow = true; + meshes.push(mesh); + group.add(mesh); mats.appendAll(mat); }