pocketing: speed up shadow calcs, fix poly nesting

This commit is contained in:
Stewart Allen 2022-04-07 14:41:17 -04:00
commit 0d5ad0d74b

View file

@ -947,22 +947,28 @@ class OpPocket extends CamOp {
} }
let shadows = {}; let shadows = {};
function genShadows(zs) { function genShadows(zs) {
let lastShadow, lastZ;
for (let z of zs) { for (let z of zs) {
if (shadows[z]) continue; if (shadows[z]) continue;
let shadow = CAM.shadowAt(widget, z, lastZ); // find closest shadow above and use
lastZ = z; let minzkey;
if (lastShadow) { let zover = Object.keys(shadows).map(v => parseFloat(v)).filter(v => v > z);
shadow = POLY.union([...shadow, ...lastShadow], undefined, true); for (let zkey of zover) {
if (minzkey && zkey < minzkey) {
minzkey = zkey;
} else {
minzkey = zkey;
}
} }
lastShadow = shadow; let shadow = CAM.shadowAt(widget, z + 0.001, minzkey);
shadows[z] = shadow; if (minzkey) {
shadow = POLY.union([...shadow, ...shadows[minzkey]], undefined, true);
}
shadows[z] = POLY.setZ(shadow, z);
} }
} }
function clearZ(polys, z, down) { function clearZ(polys, z, down) {
let zs = down ? base.util.lerp(zTop, z, down) : [ z ]; let zs = down ? base.util.lerp(zTop, z, down) : [ z ];
let nested = POLY.nest(polys); for (let poly of polys) {
for (let poly of nested) {
genShadows(zs); genShadows(zs);
for (let z of zs) { for (let z of zs) {
let shadow = shadows[z]; let shadow = shadows[z];
@ -980,8 +986,11 @@ class OpPocket extends CamOp {
} }
POLY.setWinding(slice.camLines, cutdir, false); POLY.setWinding(slice.camLines, cutdir, false);
slice.output() slice.output()
.setLayer("clear", {line: 0xaa00aa}, false) .setLayer("pocket cut", {line: 0xaa00aa}, false)
.addPolys(slice.camLines) .addPolys(slice.camLines)
if (false) slice.output()
.setLayer("pocket shadow", {line: 0xff8811}, false)
.addPolys(shadow)
} }
} }
} }
@ -999,9 +1008,12 @@ class OpPocket extends CamOp {
.add(vert[i++], vert[i++], zmin = Math.min(zmin, vert[i++])) .add(vert[i++], vert[i++], zmin = Math.min(zmin, vert[i++]))
); );
} }
outline = POLY.union(outline, undefined, true); outline = POLY.union(outline, 0.0001, true);
outline = POLY.setWinding(outline, cutdir, false); outline = POLY.setWinding(outline, cutdir, false);
outline = healPolys(outline); outline = healPolys(outline);
if (false) newSliceOut(zmin).output()
.setLayer("pocket area", {line: 0x1188ff}, false)
.addPolys(outline)
clearZ(outline, zmin, down); clearZ(outline, zmin, down);
} }
} }
@ -1375,6 +1387,11 @@ CAM.shadowAt = function(widget, z, ztop) {
let found = []; let found = [];
let { position } = geo.attributes; let { position } = geo.attributes;
let { itemSize, count, array } = position; let { itemSize, count, array } = position;
if (widget._shadow_array) {
array = widget._shadow_array;
} else {
array = widget._shadow_array = [...array].map(v => v.round(3));
}
for (let i = 0; i<count; i += 3) { for (let i = 0; i<count; i += 3) {
let ip = i * itemSize; let ip = i * itemSize;
let a = new THREE.Vector3(array[ip++], array[ip++], array[ip++]); let a = new THREE.Vector3(array[ip++], array[ip++], array[ip++]);