improve flat layer additions

This commit is contained in:
Stewart Allen 2022-10-15 22:16:12 -04:00
commit 766b1ef2cc
2 changed files with 18 additions and 19 deletions

View file

@ -196,7 +196,6 @@ function pointsToPath(points, offset, open, miter = 1.5) {
const left = []; const left = [];
const right = []; const right = [];
const faces = []; const faces = [];
// const normals = new FloatPacker(points.length * 10, 1.25);
const normals = []; const normals = [];
const zn = -1; const zn = -1;
// calculate vertex normals from segments normals // calculate vertex normals from segments normals
@ -331,7 +330,6 @@ function pointsToPath(points, offset, open, miter = 1.5) {
normals.push(ln.dx, ln.dy, zn); normals.push(ln.dx, ln.dy, zn);
} }
// return { left, right, faces, normals: normals.finalize(), open };
return { left, right, faces, normals, open }; return { left, right, faces, normals, open };
} }
@ -537,15 +535,16 @@ class FloatPacker {
push() { push() {
const array = this.array; const array = this.array;
const size = this.size; const size = this.size;
for (let i=0, l=arguments.length; i<l; i++) { const args = arguments.length;
if (this.pos + args >= size) {
let nusize = ((size * this.factor) | 0) + args;
let nuarray = new Float32Array(nusize);
nuarray.set(array);
this.array = nuarray;
this.size = nusize;
}
for (let i=0; i<args; i++) {
array[this.pos++] = arguments[i]; array[this.pos++] = arguments[i];
if (this.pos >= size) {
let nusize = ((size * this.factor) | 0) + 1;
let nuarray = new Float32Array(nusize);
nuarray.set(array);
this.array = nuarray;
this.size = nusize;
}
} }
} }

View file

@ -156,16 +156,16 @@ class Layers {
if (!polys.length) { if (!polys.length) {
return; return;
} }
const cur = this.current; const cur = this.current, faces = cur.faces;
const z = polys[0].getZ(), faces = cur.faces; const norms = [];
const off_opt = { z, flat: true };
for (let poly of polys) { for (let poly of polys) {
const faceidx = faces.length / 3; const faceidx = faces.length / 3;
const path = poly.toPath2D(offset * 0.95); const path = poly.toPath2D(offset * 0.95);
const { left, right, normals } = path; const { left, right, normals } = path;
for (let p of path.faces) { for (let p of path.faces) {
faces.push(p.x, p.y, poly.z || p.z); faces.push(p.x, p.y, p.z);
} }
norms.push(normals);
if (opts.outline) { if (opts.outline) {
let p1 = newPolygon().addPoints(left).setOpenValue(poly.open); let p1 = newPolygon().addPoints(left).setOpenValue(poly.open);
let p2 = newPolygon().addPoints(right).setOpenValue(poly.open); let p2 = newPolygon().addPoints(right).setOpenValue(poly.open);
@ -176,11 +176,6 @@ class Layers {
this.addPolys(p1); this.addPolys(p1);
this.addPolys(p2); this.addPolys(p2);
} }
if (cur.norms) {
cur.norms.appendAll(normals);
} else {
cur.norms = normals;
}
const color = opts.color ? const color = opts.color ?
(typeof(opts.color) === 'number' ? { line: opts.color, face: opts.color } : opts.color) : (typeof(opts.color) === 'number' ? { line: opts.color, face: opts.color } : opts.color) :
cur.color; cur.color;
@ -195,6 +190,11 @@ class Layers {
} }
} }
} }
if (cur.norms) {
cur.norms.appendAll(norms.flat());
} else {
cur.norms = norms.flat();
}
return this; return this;
} }