avoid rehydrating polygons and points for layer objects

This commit is contained in:
Stewart Allen 2022-10-15 19:09:51 -04:00
commit 6d3a52264b
3 changed files with 21 additions and 13 deletions

View file

@ -365,6 +365,16 @@ registerDecoder(TYPE.POLY, function(v, state) {
// ----- Layers Codec -----
function encodeLayerPolys(polys, state) {
return polys.map(poly => {
return {
open: poly.open,
color: poly.color,
points: codec.encodePointArray(poly.points, state.zeros)
}
});
}
kiri.Layers.prototype.encode = function(state) {
let zeros = state.zeros;
let enc = {
@ -377,7 +387,7 @@ kiri.Layers.prototype.encode = function(state) {
off: layer.off,
color: layer.color,
// 1D lines and polylines
polys: encode(layer.polys, state),
polys: encodeLayerPolys(layer.polys, state),
lines: encodePointArray(layer.lines, state),
// 2D lines and flat areas
faces: codec.allocFloat32Array(layer.faces, zeros),
@ -409,8 +419,8 @@ registerDecoder(TYPE.LAYERS, function(v, state) {
const d = render.layers[layers[i]] = {
off: data.off,
color: data.color,
polys: decode(data.polys, state),
lines: decodePointArray(data.lines),
polys: data.polys,
lines: data.lines,
faces: codec.allocFloat32Array(data.faces),
norms: codec.allocFloat32Array(data.norms),
cface: data.cface,

View file

@ -89,15 +89,13 @@ class Stack {
renderLayer(layer, group) {
function addPoly(vertices, poly) {
const points = poly.points, len = points.length;
for (let i=1; i<len; i++) {
const p1 = points[i-1], p2 = points[i];
vertices.push(new THREE.Vector3(p1.x, p1.y, p1.z));
vertices.push(new THREE.Vector3(p2.x, p2.y, p2.z));
for (let i=3; i<len; i += 3) {
vertices.push(new THREE.Vector3(points[i-3], points[i-2], points[i-1]));
vertices.push(new THREE.Vector3(points[i+0], points[i+1], points[i+2]));
}
if (!poly.open) {
const p1 = points[len - 1], p2 = points[0];
vertices.push(new THREE.Vector3(p1.x, p1.y, p1.z));
vertices.push(new THREE.Vector3(p2.x, p2.y, p2.z));
vertices.push(new THREE.Vector3(points[len-3], points[len-2], points[len-1]));
vertices.push(new THREE.Vector3(points[0], points[1], points[2]));
}
}
@ -134,9 +132,8 @@ class Stack {
}
}
// for now, line segments inherit the last color
for (let i=0, il=lines.length; i<il; i++) {
const p = lines[i];
vert.push(new THREE.Vector3(p.x, p.y, p.z));
for (let i=0, il=lines.length; i<il; i += 3) {
vert.push(new THREE.Vector3(lines[i], lines[i+1], lines[i+1]));
}
// ensure at least one group using the default color settings
if (grp.length === 0) {