flatten layer path encodings. became single element array a long time ago

This commit is contained in:
Stewart Allen 2022-10-15 14:36:25 -04:00
commit a753e12d3a
3 changed files with 41 additions and 45 deletions

View file

@ -331,6 +331,7 @@ kiri.Layers.prototype.encode = function(state) {
type: TYPE.LAYERS, type: TYPE.LAYERS,
layers: Object.keys(this.layers), layers: Object.keys(this.layers),
data: Object.values(this.layers).map(layer => { data: Object.values(this.layers).map(layer => {
const paths = layer.paths;
return { return {
// defaults // defaults
off: layer.off, off: layer.off,
@ -343,14 +344,12 @@ kiri.Layers.prototype.encode = function(state) {
norms: codec.allocFloat32Array(layer.norms, zeros), norms: codec.allocFloat32Array(layer.norms, zeros),
cface: layer.cface || codec.undef, cface: layer.cface || codec.undef,
// 3D lines // 3D lines
paths: layer.paths.map(lp => { paths: paths ? {
return { z: paths.z,
z: lp.z, index: paths.index,
index: lp.index, faces: codec.allocFloat32Array(paths.faces, zeros),
faces: codec.allocFloat32Array(lp.faces, zeros), norms: codec.allocFloat32Array(paths.norms, zeros)
norms: codec.allocFloat32Array(lp.norms, zeros) } : undefined,
}
}),
cpath: layer.cpath || codec.undef cpath: layer.cpath || codec.undef
} }
}) })
@ -366,6 +365,7 @@ registerDecoder(TYPE.LAYERS, function(v, state) {
for (let i=0; i<layers.length; i++) { for (let i=0; i<layers.length; i++) {
const data = v.data[i]; const data = v.data[i];
const paths = data.paths;
const d = render.layers[layers[i]] = { const d = render.layers[layers[i]] = {
off: data.off, off: data.off,
color: data.color, color: data.color,
@ -374,14 +374,12 @@ registerDecoder(TYPE.LAYERS, function(v, state) {
faces: codec.allocFloat32Array(data.faces), faces: codec.allocFloat32Array(data.faces),
norms: codec.allocFloat32Array(data.norms), norms: codec.allocFloat32Array(data.norms),
cface: data.cface, cface: data.cface,
paths: data.paths.map(lp => { paths: paths ? {
return { z: paths.z,
z: lp.z, index: paths.index,
index: lp.index, faces: codec.allocFloat32Array(paths.faces),
faces: codec.allocFloat32Array(lp.faces), norms: codec.allocFloat32Array(paths.norms)
norms: codec.allocFloat32Array(lp.norms) } : undefined,
};
}),
cpath: data.cpath cpath: data.cpath
}; };
// fixup null -> Infinity in material counts (JSON stringify sucks) // fixup null -> Infinity in material counts (JSON stringify sucks)

View file

@ -56,7 +56,7 @@ class Layers {
faces: [], // triangles for areas and flats faces: [], // triangles for areas and flats
norms: undefined, // flats vertex normals norms: undefined, // flats vertex normals
cface: undefined, // flats face color indices 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 cpath: undefined, // path colors indices
color: colors || { color: colors || {
check: 0, check: 0,
@ -216,7 +216,7 @@ class Layers {
const z = opts.z || poly.getZ(); const z = opts.z || poly.getZ();
const { faces, normals } = poly.toPath3D(offset, height, 0); const { faces, normals } = poly.toPath3D(offset, height, 0);
const cur = this.current; const cur = this.current;
const one = cur.paths[0]; const one = cur.paths;
if (one) { if (one) {
// merge all contour geometry for massive speed gain // merge all contour geometry for massive speed gain
// todo: recode to concat all geometries at once, and not incrementally // todo: recode to concat all geometries at once, and not incrementally
@ -242,7 +242,7 @@ class Layers {
one.norms.appendAll(normals); one.norms.appendAll(normals);
} }
} else { } else {
cur.paths.push({ index: [], faces, z, norms: normals }); cur.paths = { faces, z, norms: normals };
if (opts.color) { if (opts.color) {
cur.cpath = [ Object.assign({ start: 0, count: Infinity }, opts.color) ]; cur.cpath = [ Object.assign({ start: 0, count: Infinity }, opts.color) ];
} }

View file

@ -186,7 +186,7 @@ class Stack {
group.add(mesh); group.add(mesh);
mats.appendAll(mat); mats.appendAll(mat);
} }
if (paths.length) { if (paths) {
const mat = []; const mat = [];
if (cpath) { if (cpath) {
cpath.forEach(c => { mat.push(newMat(c)) }); cpath.forEach(c => { mat.push(newMat(c)) });
@ -195,31 +195,29 @@ class Stack {
} }
mat.forEach(m => m.visible = defstate); mat.forEach(m => m.visible = defstate);
// const mat = newMat(data); // const mat = newMat(data);
paths.forEach((path, i) => { const { index, faces, norms, z, colors } = paths;
const { index, faces, norms, z, colors } = path; const geo = new THREE.BufferGeometry();
const geo = new THREE.BufferGeometry(); geo.setAttribute('position', new THREE.BufferAttribute(faces, 3));
geo.setAttribute('position', new THREE.BufferAttribute(faces, 3)); if (index && index.length) {
if (index.length) { geo.setIndex(index);
geo.setIndex(index); }
} if (norms) {
if (norms) { geo.setAttribute('normal', new THREE.BufferAttribute(norms, 3));
geo.setAttribute('normal', new THREE.BufferAttribute(norms, 3)); // geo.normalizeNormals();
// geo.normalizeNormals(); } else {
} else { geo.computeVertexNormals();
geo.computeVertexNormals(); }
} if (cpath) {
if (cpath) { cpath.forEach((c, i) => geo.addGroup(c.start, c.count, i));
cpath.forEach((c, i) => geo.addGroup(c.start, c.count, i)); } else {
} else { geo.addGroup(0, Infinity, 0);
geo.addGroup(0, Infinity, 0); }
} const mesh = new THREE.Mesh(geo, mat);
const mesh = new THREE.Mesh(geo, mat); mesh.position.z = z;
mesh.position.z = z; // mesh.castShadow = true;
// mesh.castShadow = true; // mesh.receiveShadow = true;
// mesh.receiveShadow = true; meshes.push(mesh);
meshes.push(mesh); group.add(mesh);
group.add(mesh);
});
mats.appendAll(mat); mats.appendAll(mat);
} }