Prompt specification
Build or customize the Three.js 3D scene "Small Workshop Toolpath Planning Table" inside the active workspace.
Engineering Constraints:
1. Runtime & Orbit: Use Three.js r160 with OrbitControls (damping enabled) and ACESFilmicToneMapping.
2. Frame Timing: Drive all motion strictly with clock.getDelta() so animation speed is FPS-independent.
3. Viewport Hygiene: Handle window resize cleanly, cap DPR at Math.min(window.devicePixelRatio, 2), and dispose geometries/materials/renderer on unmount.
Scene Metadata:
Scene: Small Workshop Toolpath Planning Table
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/small-workshop-toolpath-planning-table-x2079
Scene Description Prompt:
Please create one self-contained HTML file with Three.js from cdnjs showing milling toolpaths and material removal. Build a toy machining bench with a workpiece, clamps, and a visible path. Validate geometric clearance and distinguish the illustration from production-ready machine instructions. Voxel style.
Working Three.js Starter (small-workshop-toolpath-planning-table-x2079):
```html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Voxel Milling Bench — Illustrative Toolpath</title>
<style>
:root {
color-scheme: light;
--ink: #18302e;
--muted: #55706c;
--panel: rgba(250, 252, 244, 0.94);
--line: rgba(24, 48, 46, 0.15);
--cyan: #16aaa8;
--lime: #b9ed5a;
--amber: #f0a23a;
--red: #e35f4f;
--green: #278b61;
}
* { box-sizing: border-box; }
html, body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #dce8e2;
color: var(--ink);
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
cursor: grab;
}
canvas:active { cursor: grabbing; }
.title-card {
position: fixed;
top: 18px;
left: 18px;
z-index: 5;
max-width: min(430px, calc(100vw - 36px));
padding: 16px 18px 15px;
border: 1px solid rgba(24, 48, 46, 0.12);
border-radius: 16px;
background: rgba(250, 252, 244, 0.93);
box-shadow: 0 16px 45px rgba(35, 58, 53, 0.13);
backdrop-filter: blur(12px);
pointer-events: none;
}
.eyebrow {
display: flex;
align-items: center;
gap: 8px;
margin-bottom: 5px;
color: var(--cyan);
font-size: 11px;
font-weight: 850;
letter-spacing: 0.15em;
text-transform: uppercase;
}
.eyebrow::before {
content: "";
width: 8px;
height: 8px;
background: var(--lime);
border: 2px solid var(--ink);
box-shadow: 3px 3px 0 rgba(24, 48, 46, 0.18);
}
h1 {
margin: 0;
font-size: clamp(22px, 3vw, 34px);
line-height: 1.02;
letter-spacing: -0.045em;
}
.subtitle {
margin: 8px 0 0;
color: var(--muted);
font-size: 13px;
line-height: 1.45;
}
.warning-badge {
display: inline-flex;
align-items: center;
gap: 7px;
margin-top: 12px;
padding: 7px 9px;
border: 1px solid rgba(227, 95, 79, 0.35);
border-radius: 8px;
background: rgba(227, 95, 79, 0.09);
color: #a43c31;
font-size: 10px;
font-weight: 850;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.warning-badge::before {
content: "!";
display: grid;
width: 17px;
height: 17px;
place-items: center;
border-radius: 4px;
background: var(--red);
color: white;
font-size: 12px;
}
.panel {
position: fixed;
top: 18px;
right: 18px;
z-index: 6;
width: min(340px, calc(100vw - 36px));
max-height: calc(100vh - 36px);
overflow: auto;
padding: 17px;
border: 1px solid rgba(24, 48, 46, 0.13);
border-radius: 16px;
background: var(--panel);
box-shadow: 0 16px 45px rgba(35, 58, 53, 0.15);
backdrop-filter: blur(14px);
}
.panel h2 {
margin: 0 0 5px;
font-size: 13px;
letter-spacing: 0.09em;
text-transform: uppercase;
}
.panel-intro {
margin: 0 0 13px;
color: var(--muted);
font-size: 12px;
line-height: 1.45;
}
.button-row {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 8px;
}
button {
min-height: 39px;
padding: 9px 11px;
border: 1px solid rgba(24, 48, 46, 0.18);
border-radius: 9px;
background: #fffef8;
color: var(--ink);
font: inherit;
font-size: 12px;
font-weight: 800;
cursor: pointer;
transition: transform 0.12s ease, background 0.12s ease;
}
button:hover {
transform: translateY(-1px);
background: #f3f7e9;
}
button.primary {
border-color: var(--ink);
background: var(--ink);
color: #fbfff3;
}
.control {
margin-top: 14px;
}
.control-label {
display: flex;
justify-content: space-between;
margin-bottom: 7px;
color: var(--muted);
font-size: 11px;
font-weight: 750;
}
input[type="range"] {
width: 100%;
accent-color: var(--cyan);
}
.toggles {
display: grid;
gap: 8px;
margin-top: 14px;
padding-top: 12px;
border-top: 1px solid var(--line);
}
.toggle {
display: flex;
align-items: center;
gap: 9px;
color: #35514e;
font-size: 12px;
cursor: pointer;
}
.toggle input {
width: 15px;
height: 15px;
accent-color: var(--cyan);
}
.legend {
display: flex;
flex-wrap: wrap;
gap: 9px 13px;
margin-top: 13px;
color: var(--muted);
font-size: 10px;
}
.legend span {
display: inline-flex;
align-items: center;
gap: 5px;
}
.swatch {
width: 10px;
height: 10px;
border-radius: 2px;
box-shadow: 2px 2px 0 rgba(24, 48, 46, 0.16);
}
.section {
margin-top: 15px;
padding-top: 14px;
border-top: 1px solid var(--line);
}
.section-title {
display: flex;
align-items: center;
justify-content: space-between;
margin-bottom: 10px;
font-size: 11px;
font-weight: 850;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.section-title small {
color: var(--muted);
font-size: 9px;
font-weight: 700;
letter-spacing: 0;
text-transform: none;
}
.check {
display: grid;
grid-template-columns: 18px 1fr auto;
align-items: center;
gap: 7px;
padding: 6px 0;
font-size: 11px;
}
.check-icon {
display: grid;
width: 16px;
height: 16px;
place-items: center;
border-radius: 4px;
background: rgba(39, 139, 97, 0.13);
color: var(--green);
font-size: 10px;
font-weight: 900;
}
.check-value {
color: var(--muted);
font-variant-numeric: tabular-nums;
text-align: right;
}
.release {
margin-top: 9px;
padding: 9px 10px;
border: 1px solid rgba(227, 95, 79, 0.25);
border-radius: 8px;
background: rgba(227, 95, 79, 0.08);
color: #9f3e34;
font-size: 11px;
font-weight: 800;
}
.disclaimer {
margin: 11px 0 0;
color: #667c78;
font-size: 10px;
line-height: 1.48;
}
.progress-wrap {
margin-top: 14px;
}
.progress-track {
height: 8px;
overflow: hidden;
border: 1px solid rgba(24, 48, 46, 0.14);
border-radius: 3px;
background: #e7ece0;
}
.progress-fill {
width: 0;
height: 100%;
background: linear-gradient(90deg, var(--cyan), var(--lime));
}
.progress-meta {
display: flex;
justify-content: space-between;
margin-top: 6px;
color: var(--muted);
font-size: 10px;
font-variant-numeric: tabular-nums;
}
.bottom-note {
position: fixed;
left: 18px;
bottom: 18px;
z-index: 4;
max-width: min(560px, calc(100vw - 36px));
padding: 10px 13px;
border: 1px solid rgba(24, 48, 46, 0.12);
border-radius: 10px;
background: rgba(250, 252, 244, 0.88);
color: #47635f;
font-size: 11px;
line-height: 1.4;
pointer-events: none;
backdrop-filter: blur(10px);
}
.bottom-note strong { color: var(--ink); }
#loading {
position: fixed;
inset: 0;
z-index: 20;
display: grid;
place-items: center;
background: #dce8e2;
color: var(--ink);
font-size: 12px;
font-weight: 800;
letter-spacing: 0.08em;
text-transform: uppercase;
}
@media (max-width: 760px) {
.title-card {
top: 10px;
left: 10px;
padding: 12px 13px;
}
.subtitle { display: none; }
.warning-badge { margin-top: 9px; }
.panel {
top: auto;
right: 10px;
bottom: 10px;
left: 10px;
width: auto;
max-height: 46vh;
padding: 13px;
}
.bottom-note { display: none; }
}
</style>
</head>
<body>
<canvas id="scene" aria-label="Interactive voxel milling bench visualization"></canvas>
<header class="title-card">
<div class="eyebrow">Voxel machining lab / 01</div>
<h1>Milling toolpath study</h1>
<p class="subtitle">
A toy bench with a voxel workpiece, edge clamps, a visible raster path,
and animated top-layer material removal.
</p>
<div class="warning-badge">Illustrative only — not machine instructions</div>
</header>
<aside class="panel" aria-label="Simulation controls and geometric checks">
<h2>Animated cut</h2>
<p class="panel-intro">
The cyan centerline is a visualization path. Green segments show travel already cut.
</p>
<div class="button-row">
<button id="play" class="primary" type="button">Pause</button>
<button id="reset" type="button">Reset stock</button>
</div>
<div class="control">
<label class="control-label" for="speed">
<span>Playback speed</span>
<span id="speed-value">1.0×</span>
</label>
<input id="speed" type="range" min="0.4" max="2.2" step="0.1" value="1">
</div>
<div class="toggles">
<label class="toggle">
<input id="show-path" type="checkbox" checked>
Show toolpath
</label>
<label class="toggle">
<input id="show-envelope" type="checkbox" checked>
Show clearance envelope
</label>
<label class="toggle">
<input id="show-chips" type="checkbox" checked>
Show voxel chips
</label>
</div>
<div class="legend" aria-label="Scene legend">
<span><i class="swatch" style="background:#16aaa8"></i>Path</span>
<span><i class="swatch" style="background:#b9ed5a"></i>Cut</span>
<span><i class="swatch" style="background:#e35f4f"></i>Fixture</span>
<span><i class="swatch" style="background:#efb94f"></i>Stock</span>
</div>
<div class="progress-wrap">
<div class="progress-track"><div id="progress" class="progress-fill"></div></div>
<div class="progress-meta">
<span id="progress-label">0% cut</span>
<span id="removed-label">0 voxels removed</span>
</div>
</div>
<section class="section">
<div class="section-title">
Model geometry checks
<small>scene units</small>
</div>
<div class="check">
<span class="check-icon">✓</span>
<span>Path inside stock</span>
<span id="stock-check" class="check-value">—</span>
</div>
<div class="check">
<span class="check-icon">✓</span>
<span>Holder to fixture</span>
<span id="fixture-check" class="check-value">—</span>
</div>
<div class="check">
<span class="check-icon">✓</span>
<span>Bench vertical envelope</span>
<span id="bench-check" class="check-value">—</span>
</div>
<div class="check">
<span class="check-icon">✓</span>
<span>Centerline continuity</span>
<span id="continuity-check" class="check-value">—</span>
</div>
<div class="release">PRODUCTION RELEASE: NOT APPROVED</div>
<p class="disclaimer">
These checks only test this simplified model’s sampled centerline and nominal
envelopes. This is not G-code and not a machine-specific collision proof.
It omits tool compensation, datum strategy, feeds and speeds, runout,
gouging, chip evacuation, fixturing forces, machine kinematics, alarms,
and material behavior. Revalidate in production CAM/CAD, simulate it,
and prove out safely before running a real machine.
</p>
</section>
</aside>
<div class="bottom-note">
<strong>Controls:</strong> drag to orbit · wheel to zoom · space to play/pause.
All dimensions are illustrative scene units.
</div>
<div id="loading">Loading voxel bench…</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const canvas = document.querySelector("#scene");
const loading = document.querySelector("#loading");
const renderer = new THREE.WebGLRenderer({
canvas,
antialias: true,
alpha: false
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight, false);
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.outputColorSpace = THREE.SRGBColorSpace;
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xdce8e2);
scene.fog = new THREE.Fog(0xdce8e2, 42, 72);
const camera = new THREE.PerspectiveCamera(
38,
window.innerWidth / window.innerHeight,
0.1,
120
);
const hemi = new THREE.HemisphereLight(0xf7fff1, 0x718983, 2.15);
scene.add(hemi);
const key = new THREE.DirectionalLight(0xfff4d6, 3.1);
key.position.set(-14, 24, 12);
key.castShadow = true;
key.shadow.mapSize.set(2048, 2048);
key.shadow.camera.left = -24;
key.shadow.camera.right = 24;
key.shadow.camera.top = 22;
key.shadow.camera.bottom = -22;
key.shadow.camera.near = 1;
key.shadow.camera.far = 65;
key.shadow.bias = -0.0005;
scene.add(key);
const rim = new THREE.DirectionalLight(0x9fe4dd, 1.25);
rim.position.set(18, 10, -17);
scene.add(rim);
const palette = {
bench: 0x466b65,
benchEdge: 0x2e514c,
fixture: 0x68807b,
clamp: 0xe35f4f,
clampDark: 0x963c34,
metal: 0x263d3b,
steel: 0xa8bab5,
cutter: 0xf0a23a,
dark: 0x18302e
};
function material(color, extra = {}) {
return new THREE.MeshStandardMaterial({
color,
roughness: 0.78,
metalness: 0.06,
flatShading: true,
...extra
});
}
function box(parent, w, h, d, mat, x = 0, y = 0, z = 0, shadow = true) {
const mesh = new THREE.Mesh(new THREE.BoxGeometry(w, h, d), mat);
mesh.position.set(x, y, z);
mesh.castShadow = shadow;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
// ---------------------------------------------------------------------
// Voxel bench and fixture
// ---------------------------------------------------------------------
const bench = new THREE.Group();
scene.add(bench);
const benchMat = material(palette.bench);
const benchEdgeMat = material(palette.benchEdge);
const fixtureMat = material(palette.fixture);
box(bench, 28, 1.2, 19, benchMat, 0, -0.65, 0);
box(bench, 28.4, 0.3, 19.4, benchEdgeMat, 0, -1.34, 0);
const legMat = material(0x355852);
for (const x of [-11.4, 11.4]) {
for (const z of [-7.3, 7.3]) {
box(bench, 1.4, 5.2, 1.4, legMat, x, -4.1, z);
}
}
const grid = new THREE.GridHelper(26, 26, 0x6e8982, 0x58766f);
grid.position.y = -0.035;
grid.scale.z = 0.69;
grid.material.transparent = true;
grid.material.opacity = 0.36;
bench.add(grid);
box(bench, 22, 0.6, 14, fixtureMat, 0, 0.25, 0);
box(bench, 22.5, 0.18, 14.5, material(0x78918b), 0, -0.06, 0);
const ground = new THREE.Mesh(
new THREE.PlaneGeometry(120, 120),
material(0xcbded7, { roughness: 1 })
);
ground.rotation.x = -Math.PI / 2;
ground.position.y = -6.72;
ground.receiveShadow = true;
scene.add(ground);
// ---------------------------------------------------------------------
// Workpiece: 20 × 3 × 12 voxel stock
// ---------------------------------------------------------------------
const NX = 20;
const NY = 3;
const NZ = 12;
const PITCH = 1;
const VOXEL_SIZE = 0.88;
const voxelGeometry = new THREE.BoxGeometry(VOXEL_SIZE, VOXEL_SIZE, VOXEL_SIZE);
const voxelMaterial = material(0xffffff);
const voxelMesh = new THREE.InstancedMesh(voxelGeometry, voxelMaterial, NX * NY * NZ);
voxelMesh.castShadow = true;
voxelMesh.receiveShadow = true;
scene.add(voxelMesh);
const records = [];
const topCells = [];
const dummy = new THREE.Object3D();
function cellIndex(ix, iy, iz) {
return (iy * NZ + iz) * NX + ix;
}
for (let iy = 0; iy < NY; iy++) {
for (let iz = 0; iz < NZ; iz++) {
for (let ix = 0; ix < NX; ix++) {
const index = cellIndex(ix, iy, iz);
const x = ix - (NX - 1) / 2;
const y = 1 + iy;
const z = iz - (NZ - 1) / 2;
const base = new THREE.Color(
iy === 0 ? 0xc58d42 :
iy === 1 ? 0xdba84f :
0xefbf5d
);
const variation = ((ix * 7 + iz * 11 + iy * 3) % 5) * 0.012;
base.offsetHSL(0, 0, variation);
dummy.position.set(x, y, z);
dummy.rotation.set(0, 0, 0);
dummy.scale.set(1, 1, 1);
dummy.updateMatrix();
voxelMesh.setMatrixAt(index, dummy.matrix);
voxelMesh.setColorAt(index, base);
records[index] = {
index, ix, iy, iz, x, y, z,
baseColor: base.clone(),
cutColor: base.clone().multiplyScalar(0.69),
removed: false,
cut: false
};
}
}
}
for (let iz = 0; iz < NZ; iz++) {
for (let ix = 0; ix < NX; ix++) {
topCells.push({
ix, iz,
x: ix - (NX - 1) / 2,
z: iz - (NZ - 1) / 2,
index: cellIndex(ix, NY - 1, iz),
belowIndex: cellIndex(ix, NY - 2, iz)
});
}
}
voxelMesh.instanceMatrix.needsUpdate = true;
voxelMesh.instanceColor.needsUpdate = true;
// ---------------------------------------------------------------------
// Voxel clamps and their nominal horizontal footprints
// ---------------------------------------------------------------------
const clampMat = material(palette.clamp);
const clampDarkMat = material(palette.clampDark);
const steelMat = material(palette.steel, { metalness: 0.42, roughness: 0.48 });
const clampFootprints = [];
function createClamp(x, side) {
const g = new THREE.Group();
scene.add(g);
// Outside upright.
box(g, 2.0, 3.3, 0.72, clampMat, x, 1.62, side * 7.02);
box(g, 2.35, 0.35, 1.0, clampDarkMat, x, 0.14, side * 7.02);
// Arm reaching inward over the stock edge.
box(g, 2.0, 0.55, 1.9, clampMat, x, 3.27, side * 6.36);
box(g, 2.35, 0.22, 1.95, clampDarkMat, x, 3.61, side * 6.36);
// Voxel-style screw and pressure pad.
box(g, 0.46, 1.05, 0.46, steelMat, x, 2.85, side * 5.60);
box(g, 0.9, 0.28, 0.9, clampDarkMat, x, 2.21, side * 5.60);
box(g, 0.76, 0.25, 0.76, clampMat, x, 1.96, side * 5.60);
// Small handle.
box(g, 1.5, 0.16, 0.16, steelMat, x, 3.43, side * 5.60);
clampFootprints.push({
x0: x - 1.18,
x1: x + 1.18,
z0: side > 0 ? 5.25 : -7.40,
z1: side > 0 ? 7.40 : -5.25
});
}
createClamp(-4.6, 1);
createClamp(4.6, 1);
createClamp(-4.6, -1);
createClamp(4.6, -1);
// ---------------------------------------------------------------------
// Rounded serpentine centerline
// ---------------------------------------------------------------------
const route = [];
const ROWS = [-4, -2, 0, 2, 4];
const TURN_RADIUS = 0.75;
function addLine(x0, z0, x1, z1) {
const length = Math.hypot(x1 - x0, z1 - z0);
const steps = Math.max(1, Math.ceil(length / 0.16));
for (let i = 1; i <= steps; i++) {
const t = i / steps;
route.push({ x: x0 + (x1 - x0) * t, z: z0 + (z1 - z0) * t });
}
}
function addArc(cx, cz, radius, a0, a1) {
const length = Math.abs(a1 - a0) * radius;
const steps = Math.max(2, Math.ceil(length / 0.16));
for (let i = 1; i <= steps; i++) {
const t = i / steps;
const a = a0 + (a1 - a0) * t;
route.push({
x: cx + Math.cos(a) * radius,
z: cz + Math.sin(a) * radius
});
}
}
let currentX = -7.25;
let currentZ = ROWS[0];
ROWS.forEach((z, row) => {
const goingRight = row % 2 === 0;
const endX = goingRight ? 7.25 : -7.25;
addLine(currentX, currentZ, endX, z);
currentX = endX;
currentZ = z;
if (row === ROWS.length - 1) return;
if (goingRight) {
// Right turn: +X to +Z.
addArc(7.25, z + 0.75, TURN_RADIUS, -Math.PI / 2, 0);
addLine(8.0, z + 0.75, 8.0, z + 1.25);
// +Z to -X.
addArc(7.25, z + 1.25, TURN_RADIUS, 0, Math.PI / 2);
} else {
// Left turn: -X to +Z.
addArc(-7.25, z + 0.75, TURN_RADIUS, -Math.PI / 2, -Math.PI);
addLine(-8.0, z + 0.75, -8.0, z + 1.25);
// +Z to +X.
addArc(-7.25, z + 1.25, TURN_RADIUS, Math.PI, Math.PI / 2);
}
currentX = goingRight ? 7.25 : -7.25;
currentZ = z + 2;
});
route.forEach((p, i) => {
p.distance = i === 0 ? 0 : p.distance = 0;
});
for (let i = 1; i < route.length; i++) {
route[i].distance =
route[i - 1].distance +
Math.hypot(
route[i].x - route[i - 1].x,
route[i].z - route[i - 1].z
);
}
const routeLength = route[route.length - 1].distance;
// ---------------------------------------------------------------------
// Visible path made from small rectangular blocks
// ---------------------------------------------------------------------
const pathGroup = new THREE.Group();
scene.add(pathGroup);
const pathMaterial = material(0xffffff, {
emissive: 0x0a4c4b,
emissiveIntensity: 0.28,
roughness: 0.55
});
const pathMesh = new THREE.InstancedMesh(
new THREE.BoxGeometry(1, 1, 1),
pathMaterial,
route.length - 1
);
pathMesh.castShadow = false;
pathMesh.receiveShadow = false;
pathGroup.add(pathMesh);
const segmentEndDistance = [];
for (let i = 0; i < route.length - 1; i++) {
const a = route[i];
const b = route[i + 1];
const dx = b.x - a.x;
const dz = b.z - a.z;
const length = Math.hypot(dx, dz);
dummy.position.set((a.x + b.x) / 2, 3.51, (a.z + b.z) / 2);
dummy.rotation.set(0, Math.atan2(dx, dz), 0);
dummy.scale.set(0.075, 0.045, length + 0.018);
dummy.updateMatrix();
pathMesh.setMatrixAt(i, dummy.matrix);
pathMesh.setColorAt(i, new THREE.Color(0x16aaa8));
segmentEndDistance.push(b.distance);
}
pathMesh.instanceMatrix.needsUpdate = true;
pathMesh.instanceColor.needsUpdate = true;
// A visual perimeter around the raster region. It is decorative and is
// deliberately not presented as a machining contour.
const borderGroup = new THREE.Group();
pathGroup.add(borderGroup);
const borderMat = material(0xf0a23a, {
emissive: 0x6d3500,
emissiveIntensity: 0.12
});
function addBorderSegment(x0, z0, x1, z1) {
const length = Math.hypot(x1 - x0, z1 - z0);
dummy.position.set((x0 + x1) / 2, 3.515, (z0 + z1) / 2);
dummy.rotation.set(0, Math.atan2(x1 - x0, z1 - z0), 0);
dummy.scale.set(0.06, 0.035, length);
dummy.updateMatrix();
const mesh = new THREE.Mesh(new THREE.BoxGeometry(1, 1, 1), borderMat);
mesh.applyMatrix4(dummy.matrix);
borderGroup.add(mesh);
}
addBorderSegment(-8.55, -4.6, 8.55, -4.6);
addBorderSegment(8.55, -4.6, 8.55, 4.6);
addBorderSegment(8.55, 4.6, -8.55, 4.6);
addBorderSegment(-8.55, 4.6, -8.55, -4.6);
// ---------------------------------------------------------------------
// Tool assembly and clearance envelope
// ---------------------------------------------------------------------
const tool = new THREE.Group();
scene.add(tool);
const toolMetal = material(palette.metal, { metalness: 0.35, roughness: 0.42 });
const toolSteel = material(palette.steel, { metalness: 0.55, roughness: 0.34 });
const cutterMat = material(palette.cutter, { metalness: 0.18, roughness: 0.5 });
box(tool, 1.12, 1.28, 1.12, toolMetal, 0, 2.10, 0);
box(tool, 1.34, 0.18, 1.34, cutterMat, 0, 2.78, 0);
box(tool, 0.72, 1.34, 0.72, toolSteel, 0, 1.08, 0);
box(tool, 0.46, 0.60, 0.46, toolMetal, 0, 0.48, 0);
const rotor = new THREE.Group();
tool.add(rotor);
box(rotor, 0.20, 0.62, 0.20, toolSteel, 0, 0.18, 0);
for (let level = 0; level < 4; level++) {
for (let blade = 0; blade < 4; blade++) {
const angle = blade * Math.PI / 2 + level * Math.PI / 4;
const tooth = box(
rotor,
0.16,
0.20,
0.16,
cutterMat,
Math.cos(angle) * 0.19,
0.02 + level * 0.13,
Math.sin(angle) * 0.19
);
tooth.rotation.y = angle;
}
}
const cutLight = new THREE.PointLight(0xffb34d, 1.8, 2.4, 2);
cutLight.position.set(0, 0.05, 0);
tool.add(cutLight);
const envelope = new THREE.Group();
scene.add(envelope);
const envelopeMaterial = new THREE.MeshBasicMaterial({
color: 0x5ee09a,
transparent: true,
opacity: 0.23,
wireframe: true,
depthWrite: false
});
const envelopeCylinder = new THREE.Mesh(
new THREE.CylinderGeometry(0.68, 0.68, 1.55, 12, 1, true),
envelopeMaterial
);
envelopeCylinder.position.y = 5.02;
envelope.add(envelopeCylinder);
const envelopeRing = new THREE.Mesh(
new THREE.TorusGeometry(0.68, 0.025, 4, 24),
new THREE.MeshBasicMaterial({
color: 0x5ee09a,
transparent: true,
opacity: 0.85
})
);
envelopeRing.rotation.x = Math.PI / 2;
envelopeRing.position.y = 3.48;
envelope.add(envelopeRing);
// ---------------------------------------------------------------------
// Voxel chips
// ---------------------------------------------------------------------
const CHIP_COUNT = 96;
const chipMesh = new THREE.InstancedMesh(
new THREE.BoxGeometry(0.16, 0.16, 0.16),
material(0xffffff),
CHIP_COUNT
);
chipMesh.castShadow = true;
chipMesh.receiveShadow = true;
scene.add(chipMesh);
const chips = Array.from({ length: CHIP_COUNT }, () => ({
active: false,
position: new THREE.Vector3(),
velocity: new THREE.Vector3(),
rotation: new THREE.Vector3(),
spin: new THREE.Vector3(),
life: 0
}));
let chipCursor = 0;
function spawnChip(x, y, z) {
const chip = chips[chipCursor];
chipCursor = (chipCursor + 1) % CHIP_COUNT;
chip.active = true;
chip.position.set(x, y, z);
chip.velocity.set(
(Math.random() - 0.5) * 1.8,
1.4 + Math.random() * 1.8,
(Math.random() - 0.5) * 1.8
);
chip.rotation.set(Math.random() * 3, Math.random() * 3, Math.random() * 3);
chip.spin.set(
(Math.random() - 0.5) * 6,
(Math.random() - 0.5) * 6,
(Math.random() - 0.5) * 6
);
chip.life = 1.4 + Math.random() * 1.4;
chipMesh.setColorAt(
chipCursor === 0 ? CHIP_COUNT - 1 : chipCursor - 1,
new THREE.Color().setHSL(0.105 + Math.random() * 0.025, 0.62, 0.48 + Math.random() * 0.12)
);
}
// ---------------------------------------------------------------------
// Model-level geometric checks
// ---------------------------------------------------------------------
const STOCK_HALF_X = NX * PITCH / 2;
const STOCK_HALF_Z = NZ * PITCH / 2;
const HOLDER_RADIUS = 0.68;
const CUTTER_RADIUS = 0.32;
const SAFETY_MARGIN = 0.10;
const TIP_Y = 2.95;
const BENCH_TOP_Y = -0.05;
let minEdgeDistance = Infinity;
let maxRouteGap = 0;
for (let i = 0; i < route.length; i++) {
const p = route[i];
minEdgeDistance = Math.min(
minEdgeDistance,
STOCK_HALF_X - Math.abs(p.x),
STOCK_HALF_Z - Math.abs(p.z)
);
if (i > 0) {
const q = route[i - 1];
maxRouteGap = Math.max(
maxRouteGap,
Math.hypot(p.x - q.x, p.z - q.z)
);
}
}
function pointRectDistance(x, z, rect) {
const dx = Math.max(rect.x0 - x, 0, x - rect.x1);
const dz = Math.max(rect.z0 - z, 0, z - rect.z1);
return Math.hypot(dx, dz);
}
let minFixtureDistance = Infinity;
for (const p of route) {
for (const rect of clampFootprints) {
minFixtureDistance = Math.min(
minFixtureDistance,
pointRectDistance(p.x, p.z, rect)
);
}
}
const stockClearance = minEdgeDistance - HOLDER_RADIUS - SAFETY_MARGIN;
const fixtureClearance = minFixtureDistance - HOLDER_RADIUS - SAFETY_MARGIN;
const turnClearance = TURN_RADIUS - CUTTER_RADIUS;
const benchClearance = TIP_Y - BENCH_TOP_Y;
function setCheck(element, value, pass = true) {
element.textContent = `${pass ? "PASS" : "FAIL"} · ${value}`;
const icon = element.parentElement.querySelector(".check-icon");
icon.textContent = pass ? "✓" : "!";
icon.style.background = pass
? "rgba(39, 139, 97, 0.13)"
: "rgba(227, 95, 79, 0.14)";
icon.style.color = pass ? "var(--green)" : "var(--red)";
}
setCheck(
document.querySelector("#stock-check"),
`${stockClearance.toFixed(2)} margin`,
stockClearance >= 0
);
setCheck(
document.querySelector("#fixture-check"),
`${fixtureClearance.toFixed(2)} margin`,
fixtureClearance >= 0
);
setCheck(
document.querySelector("#bench-check"),
`${benchClearance.toFixed(2)} above`,
benchClearance >= 0
);
setCheck(
document.querySelector("#continuity-check"),
`gap ${maxRouteGap.toFixed(2)}`,
maxRouteGap < 0.35
);
// ---------------------------------------------------------------------
// Animation state and material removal
// ---------------------------------------------------------------------
const playButton = document.querySelector("#play");
const resetButton = document.querySelector("#reset");
const speedInput = document.querySelector("#speed");
const speedValue = document.querySelector("#speed-value");
const progressElement = document.querySelector("#progress");
const progressLabel = document.querySelector("#progress-label");
const removedLabel = document.querySelector("#removed-label");
const showPathInput = document.querySelector("#show-path");
const showEnvelopeInput = document.querySelector("#show-envelope");
const showChipsInput = document.querySelector("#show-chips");
let playing = true;
let speed = 1;
let travel = 0;
let removedCount = 0;
let lastToolPosition = new THREE.Vector2(route[0].x, route[0].z);
let lastPathColorUpdate = -1;
function setRemovedMatrix(index, removed) {
const record = records[index];
dummy.position.set(record.x, record.y, record.z);
dummy.rotation.set(0, 0, 0);
dummy.scale.setScalar(removed ? 0 : 1);
dummy.updateMatrix();
voxelMesh.setMatrixAt(index, dummy.matrix);
voxelMesh.instanceMatrix.needsUpdate = true;
}
function pointSegmentDistance(x, z, ax, az, bx, bz) {
const vx = bx - ax;
const vz = bz - az;
const lengthSq = vx * vx + vz * vz;
if (lengthSq === 0) return Math.hypot(x - ax, z - az);
const t = THREE.MathUtils.clamp(
((x - ax) * vx + (z - az) * vz) / lengthSq,
0,
1
);
return Math.hypot(x - (ax + vx * t), z - (az + vz * t));
}
function removeMaterialAlongSegment(ax, az, bx, bz) {
const removalRadius = 0.62;
for (const cell of topCells) {
if (records[cell.index].removed) continue;
const distance = pointSegmentDistance(
cell.x, cell.z, ax, az, bx, bz
);
if (distance <= removalRadius) {
const topRecord = records[cell.index];
const belowRecord = records[cell.belowIndex];
topRecord.removed = true;
topRecord.cut = true;
belowRecord.cut = true;
setRemovedMatrix(topRecord.index, true);
voxelMesh.setColorAt(belowRecord.index, belowRecord.cutColor);
voxelMesh.instanceColor.needsUpdate = true;
spawnChip(cell.x, 3.35, cell.z);
removedCount++;
}
}
}
function resetStock() {
travel = 0;
removedCount = 0;
lastToolPosition.set(route[0].x, route[0].z);
for (const record of records) {
record.removed = false;
record.cut = false;
setRemovedMatrix(record.index, false);
voxelMesh.setColorAt(record.index, record.baseColor);
}
voxelMesh.instanceColor.needsUpdate = true;
for (let i = 0; i < CHIP_COUNT; i++) {
chips[i].active = false;
dummy.position.set(0, -50, 0);
dummy.scale.setScalar(0);
dummy.updateMatrix();
chipMesh.setMatrixAt(i, dummy.matrix);
}
chipMesh.instanceMatrix.needsUpdate = true;
lastPathColorUpdate = -1;
updateTool(0);
updateInterface();
}
function sampleRoute(distance) {
const d = THREE.MathUtils.clamp(distance, 0, routeLength);
let lo = 0;
let hi = route.length - 1;
while (lo < hi) {
const mid = Math.floor((lo + hi) / 2);
if (route[mid].distance < d) lo = mid + 1;
else hi = mid;
}
const index = Math.max(1, lo);
const a = route[index - 1];
const b = route[index];
const span = Math.max(0.00001, b.distance - a.distance);
const t = (d - a.distance) / span;
return {
x: THREE.MathUtils.lerp(a.x, b.x, t),
z: THREE.MathUtils.lerp(a.z, b.z, t),
dx: b.x - a.x,
dz: b.z - a.z
};
}
function updateTool(distance) {
const p = sampleRoute(distance);
tool.position.set(p.x, TIP_Y, p.z);
envelope.position.set(p.x, 0, p.z);
if (distance > 0) {
removeMaterialAlongSegment(
lastToolPosition.x,
lastToolPosition.y,
p.x,
p.z
);
}
lastToolPosition.set(p.x, p.z);
}
function updatePathColors(distance) {
if (Math.abs(distance - lastPathColorUpdate) < 0.08) return;
lastPathColorUpdate = distance;
for (let i = 0; i < segmentEndDistance.length; i++) {
const traversed = segmentEndDistance[i] <= distance + 0.12;
pathMesh.setColorAt(
i,
new THREE.Color(traversed ? 0xb9ed5a : 0x16aaa8)
);
}
pathMesh.instanceColor.needsUpdate = true;
}
function updateInterface() {
const fraction = travel / routeLength;
progressElement.style.width = `${(fraction * 100).toFixed(1)}%`;
progressLabel.textContent = `${Math.round(fraction * 100)}% cut`;
removedLabel.textContent = `${removedCount} voxels removed`;
}
playButton.addEventListener("click", () => {
if (travel >= routeLength - 0.01) travel = 0;
playing = !playing;
playButton.textContent = playing ? "Pause" : "Play";
});
resetButton.addEventListener("click", () => {
resetStock();
playing = false;
playButton.textContent = "Play";
});
speedInput.addEventListener("input", () => {
speed = Number(speedInput.value);
speedValue.textContent = `${speed.toFixed(1)}×`;
});
showPathInput.addEventListener("change", () => {
pathGroup.visible = showPathInput.checked;
});
showEnvelopeInput.addEventListener("change", () => {
envelope.visible = showEnvelopeInput.checked;
});
showChipsInput.addEventListener("change", () => {
chipMesh.visible = showChipsInput.checked;
});
window.addEventListener("keydown", (event) => {
if (event.code === "Space" && event.target === document.body) {
event.preventDefault();
playButton.click();
}
});
// ---------------------------------------------------------------------
// Manual orbit camera: no addons or external controls are used.
// ---------------------------------------------------------------------
const cameraTarget = new THREE.Vector3(0, 1.5, 0);
let cameraRadius = 29;
let cameraTheta = 0.62;
let cameraPhi = 0.96;
let dragging = false;
let pointerX = 0;
let pointerY = 0;
function updateCamera() {
cameraPhi = THREE.MathUtils.clamp(cameraPhi, 0.38, 1.38);
cameraRadius = THREE.MathUtils.clamp(cameraRadius, 17, 43);
camera.position.set(
cameraTarget.x + cameraRadius * Math.sin(cameraPhi) * Math.sin(cameraTheta),
cameraTarget.y + cameraRadius * Math.cos(cameraPhi),
cameraTarget.z + cameraRadius * Math.sin(cameraPhi) * Math.cos(cameraTheta)
);
camera.lookAt(cameraTarget);
}
canvas.addEventListener("pointerdown", (event) => {
dragging = true;
pointerX = event.clientX;
pointerY = event.clientY;
canvas.setPointerCapture(event.pointerId);
});
canvas.addEventListener("pointermove", (event) => {
if (!dragging) return;
const dx = event.clientX - pointerX;
const dy = event.clientY - pointerY;
pointerX = event.clientX;
pointerY = event.clientY;
cameraTheta -= dx * 0.006;
cameraPhi -= dy * 0.005;
updateCamera();
});
canvas.addEventListener("pointerup", (event) => {
dragging = false;
if (canvas.hasPointerCapture(event.pointerId)) {
canvas.releasePointerCapture(event.pointerId);
}
});
canvas.addEventListener("pointercancel", () => {
dragging = false;
});
canvas.addEventListener("wheel", (event) => {
event.preventDefault();
cameraRadius *= Math.exp(event.deltaY * 0.001);
updateCamera();
}, { passive: false });
window.addEventListener("resize", () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight, false);
});
updateCamera();
resetStock();
playing = true;
// ---------------------------------------------------------------------
// Render loop
// ---------------------------------------------------------------------
const clock = new THREE.Clock();
let rotorTime = 0;
function animate() {
const dt = Math.min(clock.getDelta(), 0.05);
rotorTime += dt;
if (playing) {
travel += dt * 6.4 * speed;
if (travel >= routeLength) {
travel = routeLength;
playing = false;
playButton.textContent = "Replay";
}
updateTool(travel);
updatePathColors(travel);
updateInterface();
}
rotor.rotation.y = rotorTime * 8;
rotor.position.y = Math.sin(rotorTime * 15) * 0.018;
cutLight.intensity = 1.35 + Math.sin(rotorTime * 18) * 0.35;
for (let i = 0; i < CHIP_COUNT; i++) {
const chip = chips[i];
if (chip.active) {
chip.life -= dt;
chip.velocity.y -= 7.2 * dt;
chip.position.addScaledVector(chip.velocity, dt);
chip.rotation.addScaledVector(chip.spin, dt);
if (chip.position.y < 0.67) {
chip.position.y = 0.67;
chip.velocity.y = Math.abs(chip.velocity.y) * 0.28;
chip.velocity.x *= 0.72;
chip.velocity.z *= 0.72;
}
if (chip.life <= 0) chip.active = false;
}
dummy.position.copy(chip.position);
dummy.rotation.set(chip.rotation.x, chip.rotation.y, chip.rotation.z);
dummy.scale.setScalar(chip.active ? Math.max(0.12, Math.min(1, chip.life)) : 0);
dummy.updateMatrix();
chipMesh.setMatrixAt(i, dummy.matrix);
}
chipMesh.instanceMatrix.needsUpdate = true;
renderer.render(scene, camera);
}
renderer.setAnimationLoop(animate);
loading.remove();
</script>
</body>
</html>
```