This commit is contained in:
2024-03-03 21:51:57 +01:00
parent 470d93233f
commit c83695281b
2 changed files with 65 additions and 26 deletions

View File

@@ -3,20 +3,28 @@
import Scene from './Scene.svelte'; import Scene from './Scene.svelte';
let progress = 1; let progress = 1;
let showSlices = 1;
let progressLayer = 0;
</script> </script>
<Canvas> <Canvas>
<Scene bind:progress /> <Scene bind:progress bind:showSlices bind:progressLayer />
</Canvas> </Canvas>
<div class="controls"> <div class="controls">
<input type="range" min="0" max="1" step="0.01" bind:value={progress} /> <input type="range" min="0" max="1" step="0.01" bind:value={showSlices} />
<label>Layer {progressLayer} <progress value={progress} /></label>
</div> </div>
<style> <style>
.controls { .controls {
display: flex;
flex-direction: column;
position: absolute; position: absolute;
top: 0; top: 0;
right: 0; right: 0;
} }
label {
color: white;
}
</style> </style>

View File

@@ -24,6 +24,8 @@
export let nozzleSize = 0.4; export let nozzleSize = 0.4;
export let tolerance = 0.005; export let tolerance = 0.005;
export let progress = 1; export let progress = 1;
export let progressLayer = 0;
export let showSlices = 1;
export let maxNonPlanarAngle = MathUtils.degToRad(20); export let maxNonPlanarAngle = MathUtils.degToRad(20);
export let bedNormal = new Vector3(0, 0, 1); export let bedNormal = new Vector3(0, 0, 1);
@@ -32,10 +34,26 @@
const stl: Readable<BufferGeometry> = useLoader(STLLoader).load('/benchy.stl'); const stl: Readable<BufferGeometry> = useLoader(STLLoader).load('/benchy.stl');
let mesh: Mesh; const enum LayerType {
let layers: { type: 'line' | 'surface'; geometry: BufferGeometry }[] = []; Line,
Surface
}
let mesh: Mesh;
let layers: { type: LayerType; geometry: BufferGeometry }[] = [];
$: if ($stl) { $: if ($stl) {
(async () => {
progress = 0;
progressLayer = 0;
await new Promise((resolve) => requestAnimationFrame(resolve));
const generator = slice();
while (!generator.next().done) {
await new Promise((resolve) => requestAnimationFrame(resolve));
}
})();
}
function* slice() {
const bvh = new MeshBVH($stl); const bvh = new MeshBVH($stl);
const positions = $stl.getAttribute('position'); const positions = $stl.getAttribute('position');
const normals = $stl.getAttribute('normal'); const normals = $stl.getAttribute('normal');
@@ -117,8 +135,10 @@
}); });
const activeNonPlanarSurfaces: [number, MeshBVH][] = []; const activeNonPlanarSurfaces: [number, MeshBVH][] = [];
const consumedNonPlanarSurfaces = nonPlanarSurfaces.map(() => false); const consumedNonPlanarSurfaces = nonPlanarSurfaces.map(() => false);
const withheldLines: number[][][] = nonPlanarSurfaces.map(() => [[]]); const withheld: Array<
const withheldSurfaces: [number, MeshBVH][][] = nonPlanarSurfaces.map(() => []); | { type: LayerType.Line; geometry: number[] }
| { type: LayerType.Surface; id: [number, MeshBVH] }
>[] = nonPlanarSurfaces.map(() => [{ type: LayerType.Line, geometry: [] }]);
const blacklist = Array.from({ length: index.count / 3 }).map(() => false); const blacklist = Array.from({ length: index.count / 3 }).map(() => false);
const line = new Line3(); const line = new Line3();
@@ -129,17 +149,21 @@
const hit2: HitPointInfo = { point: new Vector3(), distance: 0, faceIndex: 0 }; const hit2: HitPointInfo = { point: new Vector3(), distance: 0, faceIndex: 0 };
const layerPlane = new Plane(); const layerPlane = new Plane();
function deactivateSurface(surface: MeshBVH, index: number) { function deactivateSurface(surface: MeshBVH, index: number) {
layers.push({ type: 'surface', geometry: surface.geometry }); layers.push({ type: LayerType.Surface, geometry: surface.geometry });
for (const lines of withheldLines[index]) { for (const thing of withheld[index]) {
if (lines.length === 0) continue; if (thing.type === LayerType.Line) {
const additionalGeometry = new BufferGeometry(); if (thing.geometry.length === 0) continue;
additionalGeometry.setAttribute('position', new Float32BufferAttribute(lines, 3)); const additionalGeometry = new BufferGeometry();
layers.push({ type: 'line', geometry: additionalGeometry }); additionalGeometry.setAttribute(
'position',
new Float32BufferAttribute(thing.geometry, 3)
);
layers.push({ type: LayerType.Line, geometry: additionalGeometry });
} else if (thing.type === LayerType.Surface) {
deactivateSurface(thing.id[1], thing.id[0]);
}
} }
for (const surface of withheldSurfaces[index]) { delete withheld[index];
deactivateSurface(surface[1], surface[0]);
}
delete withheldLines[index];
} }
for (let layer = 0; layer < $stl.boundingBox!.max.z; layer += layerHeight) { for (let layer = 0; layer < $stl.boundingBox!.max.z; layer += layerHeight) {
layerPlane.set(bedNormal, -layer); layerPlane.set(bedNormal, -layer);
@@ -154,7 +178,6 @@
} }
deactivate: for (let i = 0; i < activeNonPlanarSurfaces.length; i++) { deactivate: for (let i = 0; i < activeNonPlanarSurfaces.length; i++) {
const [index, surface] = activeNonPlanarSurfaces[i]; const [index, surface] = activeNonPlanarSurfaces[i];
withheldLines[index].push([]);
if (surface.geometry.boundingBox!.max.z <= layer) { if (surface.geometry.boundingBox!.max.z <= layer) {
activeNonPlanarSurfaces.splice(i, 1); activeNonPlanarSurfaces.splice(i, 1);
i--; i--;
@@ -167,12 +190,14 @@
hit1.point.z < hit2.point.z && hit1.point.z < hit2.point.z &&
hit1.point.clone().sub(hit2.point).angleTo(bedNormal) > maxNonPlanarAngle hit1.point.clone().sub(hit2.point).angleTo(bedNormal) > maxNonPlanarAngle
) { ) {
withheldSurfaces[activeIndex].push([index, surface]); withheld[activeIndex].push({ type: LayerType.Surface, id: [index, surface] });
withheld[activeIndex].push({ type: LayerType.Line, geometry: [] });
continue deactivate; continue deactivate;
} }
} }
deactivateSurface(surface, index); deactivateSurface(surface, index);
} }
withheld[index]?.push({ type: LayerType.Line, geometry: [] });
} }
bvh.shapecast({ bvh.shapecast({
@@ -192,13 +217,15 @@
function add(a: Vector3, b: Vector3) { function add(a: Vector3, b: Vector3) {
for (let i = 0; i < activeNonPlanarSurfaces.length; i++) { for (let i = 0; i < activeNonPlanarSurfaces.length; i++) {
const [index, surface] = activeNonPlanarSurfaces[i]; const [index, surface] = activeNonPlanarSurfaces[i];
const withheldLayer = withheld[index].at(-1)!;
if (withheldLayer.type === LayerType.Surface) throw new Error('Unexpected surface');
const h1 = surface.closestPointToPoint(a); const h1 = surface.closestPointToPoint(a);
if ( if (
h1 && h1 &&
h1.point.z < a.z && h1.point.z < a.z &&
h1.point.clone().sub(a).angleTo(bedNormal) > maxNonPlanarAngle h1.point.clone().sub(a).angleTo(bedNormal) > maxNonPlanarAngle
) { ) {
withheldLines[index].at(-1)!.push(a.x, a.y, a.z, b.x, b.y, b.z); withheldLayer.geometry.push(a.x, a.y, a.z, b.x, b.y, b.z);
return; return;
} }
const h2 = surface.closestPointToPoint(b); const h2 = surface.closestPointToPoint(b);
@@ -207,7 +234,7 @@
h2.point.z < b.z && h2.point.z < b.z &&
h2.point.clone().sub(b).angleTo(bedNormal) > maxNonPlanarAngle h2.point.clone().sub(b).angleTo(bedNormal) > maxNonPlanarAngle
) { ) {
withheldLines[index].at(-1)!.push(a.x, a.y, a.z, b.x, b.y, b.z); withheldLayer.geometry.push(a.x, a.y, a.z, b.x, b.y, b.z);
return; return;
} }
} }
@@ -224,13 +251,17 @@
} }
}); });
layerGeometry.setAttribute('position', new Float32BufferAttribute(positions, 3)); layerGeometry.setAttribute('position', new Float32BufferAttribute(positions, 3));
layers.push({ type: 'line', geometry: layerGeometry }); layers.push({ type: LayerType.Line, geometry: layerGeometry });
console.log('layer', Math.round(layer / layerHeight), positions.length); layers = layers;
progress = layer / $stl.boundingBox!.max.z;
progressLayer = Math.round(layer / layerHeight);
yield;
} }
for (const [index, surface] of activeNonPlanarSurfaces) { for (const [index, surface] of activeNonPlanarSurfaces) {
deactivateSurface(surface, index); deactivateSurface(surface, index);
} }
layers = [...layers]; progress = 1;
layers = layers;
} }
</script> </script>
@@ -252,13 +283,13 @@
/> />
{#each layers as { geometry, type }, i} {#each layers as { geometry, type }, i}
{@const visible = progress >= i / layers.length} {@const visible = showSlices >= i / layers.length}
{@const color = new Color(0, i / layers.length, 0.2)} {@const color = new Color(0, i / layers.length, 0.2)}
{#if type === 'line'} {#if type === LayerType.Line}
<T.LineSegments {geometry} {visible}> <T.LineSegments {geometry} {visible}>
<T.LineBasicMaterial {color} /> <T.LineBasicMaterial {color} />
</T.LineSegments> </T.LineSegments>
{:else if type === 'surface'} {:else if type === LayerType.Surface}
<T.Mesh {geometry} {visible}> <T.Mesh {geometry} {visible}>
<T.MeshMatcapMaterial {color} side={DoubleSide} /> <T.MeshMatcapMaterial {color} side={DoubleSide} />
</T.Mesh> </T.Mesh>