Prompt specification
Build or customize the Three.js 3D scene "Small-Batch Drilling Jig Workshop" 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-Batch Drilling Jig Workshop
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/small-batch-drilling-jig-workshop-x2039
Scene Description Prompt:
Please use Three.js from cdnjs for a voxel scene based on this idea: small-batch drilling jig workshop. Build a fictional workshop using a jig to position repeated hole patterns. Keep clamp clearance, drill path, and part replacement sequence visually consistent. Return one self-contained HTML file.
Working Three.js Starter (small-batch-drilling-jig-workshop-x2039):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Tiny Fixtures — Small-Batch Drilling Jig</title>
<style>
:root {
--ink: #18303a;
--muted: #61747b;
--panel: rgba(248, 250, 247, 0.92);
--line: rgba(30, 65, 72, 0.16);
--teal: #168d83;
--teal-light: #71d4c7;
--orange: #ef8a47;
--red: #d95b50;
--shadow: 0 14px 40px rgba(25, 52, 59, 0.16);
}
* { box-sizing: border-box; }
html, body {
margin: 0;
width: 100%;
height: 100%;
overflow: hidden;
background: #cbd7d6;
color: var(--ink);
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}
button, input { font: inherit; }
#app, canvas {
position: fixed;
inset: 0;
width: 100%;
height: 100%;
display: block;
}
canvas { touch-action: none; cursor: grab; }
canvas:active { cursor: grabbing; }
.hud {
position: fixed;
inset: 0;
pointer-events: none;
z-index: 10;
}
.hud > * { pointer-events: auto; }
.panel {
background: var(--panel);
border: 1px solid rgba(255,255,255,.72);
box-shadow: var(--shadow);
backdrop-filter: blur(14px);
-webkit-backdrop-filter: blur(14px);
}
.brand {
position: absolute;
top: 22px;
left: 22px;
width: min(370px, calc(100vw - 44px));
border-radius: 18px;
padding: 17px 19px 16px;
}
.eyebrow {
display: flex;
align-items: center;
gap: 9px;
color: var(--teal);
font-size: 11px;
font-weight: 850;
letter-spacing: .16em;
text-transform: uppercase;
}
.eyebrow::before {
content: "";
width: 9px;
height: 9px;
border-radius: 2px;
background: var(--teal);
box-shadow: 0 0 0 4px rgba(22,141,131,.12);
}
h1 {
margin: 9px 0 7px;
font-size: clamp(21px, 2.3vw, 30px);
line-height: 1;
letter-spacing: -.035em;
}
.subtitle {
margin: 0;
color: var(--muted);
font-size: 13px;
line-height: 1.45;
}
.specs {
display: flex;
flex-wrap: wrap;
gap: 7px;
margin-top: 12px;
}
.spec {
padding: 6px 8px;
border-radius: 7px;
background: #e8eeea;
color: #425b62;
font-size: 10px;
font-weight: 800;
letter-spacing: .08em;
text-transform: uppercase;
}
.status {
position: absolute;
top: 22px;
right: 22px;
width: min(320px, calc(100vw - 44px));
border-radius: 18px;
padding: 16px 17px;
}
.status-top {
display: flex;
align-items: center;
justify-content: space-between;
gap: 12px;
}
.status-label {
color: var(--muted);
font-size: 10px;
font-weight: 800;
letter-spacing: .14em;
text-transform: uppercase;
}
#cycleCount {
color: var(--muted);
font-size: 11px;
font-weight: 800;
}
.status-main {
display: flex;
align-items: center;
gap: 10px;
margin-top: 10px;
}
#statusDot {
width: 11px;
height: 11px;
flex: 0 0 auto;
border-radius: 50%;
background: var(--teal);
box-shadow: 0 0 0 5px rgba(22,141,131,.12);
}
#phaseTitle {
font-size: 17px;
font-weight: 850;
letter-spacing: -.01em;
}
#phaseDetail {
min-height: 34px;
margin-top: 8px;
color: var(--muted);
font-size: 12px;
line-height: 1.4;
}
.progress {
height: 7px;
overflow: hidden;
margin-top: 13px;
border-radius: 999px;
background: #dfe7e3;
}
#progressFill {
width: 0;
height: 100%;
border-radius: inherit;
background: linear-gradient(90deg, var(--teal), var(--teal-light));
transition: width .08s linear;
}
.controls {
position: absolute;
left: 22px;
bottom: 22px;
width: 285px;
border-radius: 18px;
padding: 15px;
}
.button-row {
display: grid;
grid-template-columns: 1fr auto;
gap: 9px;
}
button {
border: 0;
cursor: pointer;
color: var(--ink);
font-weight: 800;
transition: transform .12s ease, filter .12s ease, background .12s ease;
}
button:hover { filter: brightness(1.03); }
button:active { transform: translateY(1px); }
.primary {
min-height: 43px;
padding: 0 15px;
border-radius: 11px;
background: var(--ink);
color: white;
font-size: 13px;
}
.secondary {
min-height: 43px;
padding: 0 13px;
border-radius: 11px;
background: #e4ebe7;
font-size: 13px;
}
.control-row {
display: flex;
align-items: center;
justify-content: space-between;
gap: 14px;
margin-top: 13px;
color: var(--muted);
font-size: 12px;
font-weight: 700;
}
.control-row input[type="range"] {
width: 125px;
accent-color: var(--teal);
}
.toggle {
display: flex;
align-items: center;
gap: 8px;
cursor: pointer;
user-select: none;
}
.toggle input {
width: 16px;
height: 16px;
accent-color: var(--teal);
}
.timeline {
position: absolute;
left: 50%;
bottom: 22px;
transform: translateX(-50%);
width: min(600px, calc(100vw - 660px));
min-width: 430px;
border-radius: 18px;
padding: 13px 15px 12px;
}
.timeline-head {
display: flex;
justify-content: space-between;
margin-bottom: 9px;
color: var(--muted);
font-size: 9px;
font-weight: 850;
letter-spacing: .13em;
text-transform: uppercase;
}
.steps {
display: grid;
grid-template-columns: repeat(6, 1fr);
gap: 6px;
}
.step {
position: relative;
overflow: hidden;
padding: 9px 4px 8px;
border-radius: 8px;
background: #e5ebe8;
color: #7b8b8f;
text-align: center;
font-size: 9px;
font-weight: 900;
letter-spacing: .08em;
transition: background .18s, color .18s;
}
.step.active {
background: var(--ink);
color: white;
}
.step.done {
background: rgba(22,141,131,.17);
color: var(--teal);
}
.views {
position: absolute;
right: 22px;
bottom: 22px;
display: flex;
gap: 7px;
border-radius: 16px;
padding: 8px;
}
.view {
min-width: 43px;
height: 37px;
padding: 0 10px;
border-radius: 10px;
background: #e6ece8;
color: #52686e;
font-size: 10px;
letter-spacing: .05em;
}
.help {
position: absolute;
right: 24px;
top: 190px;
padding: 8px 10px;
border-radius: 9px;
background: rgba(250,252,249,.72);
color: #65777b;
font-size: 10px;
font-weight: 750;
letter-spacing: .04em;
backdrop-filter: blur(8px);
}
#loading {
position: fixed;
inset: 0;
z-index: 100;
display: grid;
place-items: center;
background: #d4dfdc;
transition: opacity .35s ease, visibility .35s ease;
}
#loading.hidden {
opacity: 0;
visibility: hidden;
}
.loader-card {
padding: 20px 24px;
border-radius: 16px;
background: rgba(255,255,255,.8);
box-shadow: var(--shadow);
text-align: center;
}
.loader-card strong {
display: block;
font-size: 14px;
letter-spacing: .02em;
}
.loader-card span {
display: block;
margin-top: 6px;
color: var(--muted);
font-size: 12px;
}
.loader-cube {
width: 18px;
height: 18px;
margin: 0 auto 13px;
background: var(--teal);
animation: pulse 1s infinite alternate ease-in-out;
}
@keyframes pulse {
to { transform: rotate(45deg) scale(.72); opacity: .48; }
}
@media (max-width: 1050px) {
.timeline {
left: 22px;
right: 22px;
bottom: 190px;
width: auto;
min-width: 0;
transform: none;
}
.controls { width: 265px; }
.views { bottom: 22px; }
}
@media (max-width: 680px) {
.brand { top: 12px; left: 12px; width: calc(100vw - 24px); }
.status { top: auto; right: 12px; bottom: 270px; width: calc(100vw - 24px); }
.controls { left: 12px; bottom: 12px; width: calc(100vw - 24px); }
.timeline { left: 12px; right: 12px; bottom: 184px; }
.views { display: none; }
.help { display: none; }
.steps { gap: 3px; }
.step { font-size: 8px; }
}
</style>
</head>
<body>
<div id="app"></div>
<div class="hud">
<section class="brand panel">
<div class="eyebrow">Tiny Fixtures / Cell 04</div>
<h1>Small-batch drilling jig</h1>
<p class="subtitle">A fictional one-off workshop where every blank follows the same guarded, repeatable route.</p>
<div class="specs">
<span class="spec">4 × Ø18 repeat</span>
<span class="spec">60 mm clamp clearance</span>
<span class="spec">Batch 07</span>
</div>
</section>
<section class="status panel">
<div class="status-top">
<span class="status-label">Machine state</span>
<span id="cycleCount">Cycle 00</span>
</div>
<div class="status-main">
<span id="statusDot"></span>
<span id="phaseTitle">READY</span>
</div>
<div id="phaseDetail">Blank seated in the jig. Clamps are closed and the spindle is at safe height.</div>
<div class="progress"><div id="progressFill"></div></div>
</section>
<section class="controls panel">
<div class="button-row">
<button id="runButton" class="primary">Run batch cycle</button>
<button id="resetButton" class="secondary" title="Reset workshop">Reset</button>
</div>
<div class="control-row">
<span>Cycle speed</span>
<input id="speed" type="range" min="0.5" max="2" step="0.1" value="1" />
<span id="speedValue">1.0×</span>
</div>
<label class="control-row toggle">
<span>Guides + clearance envelope</span>
<input id="guides" type="checkbox" checked />
</label>
</section>
<section class="timeline panel">
<div class="timeline-head">
<span>Interlocked replacement sequence</span>
<span id="sequenceState">Idle</span>
</div>
<div class="steps">
<div class="step" data-step="0">DRILL</div>
<div class="step" data-step="1">HOME</div>
<div class="step" data-step="2">RELEASE</div>
<div class="step" data-step="3">UNLOAD</div>
<div class="step" data-step="4">LOAD</div>
<div class="step" data-step="5">CLAMP</div>
</div>
</section>
<nav class="views panel" aria-label="Camera views">
<button class="view" data-view="iso">ISO</button>
<button class="view" data-view="top">TOP</button>
<button class="view" data-view="side">SIDE</button>
<button class="view" data-view="front">FRONT</button>
</nav>
<div class="help">Drag to orbit · Scroll to zoom</div>
</div>
<div id="loading">
<div class="loader-card">
<div class="loader-cube"></div>
<strong>Opening the jig workshop</strong>
<span>Assembling voxel tools, stock and safety guides…</span>
</div>
</div>
<script type="module">
import * as THREE from "https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// -------------------------------------------------------------------------
// DOM
// -------------------------------------------------------------------------
const app = document.getElementById("app");
const runButton = document.getElementById("runButton");
const resetButton = document.getElementById("resetButton");
const speedInput = document.getElementById("speed");
const speedValue = document.getElementById("speedValue");
const guidesInput = document.getElementById("guides");
const phaseTitle = document.getElementById("phaseTitle");
const phaseDetail = document.getElementById("phaseDetail");
const statusDot = document.getElementById("statusDot");
const cycleCount = document.getElementById("cycleCount");
const progressFill = document.getElementById("progressFill");
const sequenceState = document.getElementById("sequenceState");
const stepElements = [...document.querySelectorAll(".step")];
// -------------------------------------------------------------------------
// Renderer and scene
// -------------------------------------------------------------------------
const renderer = new THREE.WebGLRenderer({ antialias: true, alpha: false });
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 1.8));
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.08;
app.appendChild(renderer.domElement);
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xcbd8d7);
scene.fog = new THREE.Fog(0xcbd8d7, 17, 31);
const camera = new THREE.PerspectiveCamera(
39,
window.innerWidth / window.innerHeight,
0.1,
60
);
const hemi = new THREE.HemisphereLight(0xeaf7ff, 0x8b7560, 2.0);
scene.add(hemi);
const sun = new THREE.DirectionalLight(0xfff3db, 3.2);
sun.position.set(7, 11, 7);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -11;
sun.shadow.camera.right = 11;
sun.shadow.camera.top = 10;
sun.shadow.camera.bottom = -10;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 30;
sun.shadow.bias = -0.00018;
scene.add(sun);
const fill = new THREE.DirectionalLight(0xb9e8ff, 1.1);
fill.position.set(-8, 6, 4);
scene.add(fill);
// -------------------------------------------------------------------------
// Materials and geometry helpers
// -------------------------------------------------------------------------
const material = (color, roughness = 0.75, metalness = 0.0, extra = {}) =>
new THREE.MeshStandardMaterial({ color, roughness, metalness, ...extra });
const M = {
floor: material(0xb8c2ba, 0.96),
floorEdge: material(0x71817e, 0.9),
wall: material(0xd9ded7, 0.95),
wallLower: material(0x9eb1ab, 0.88),
wood: material(0xb77d50, 0.84),
woodDark: material(0x785139, 0.9),
navy: material(0x263d50, 0.56, 0.24),
navyLight: material(0x38566a, 0.58, 0.18),
teal: material(0x238f85, 0.48, 0.18),
tealLight: material(0x76c9ba, 0.55, 0.12),
orange: material(0xef8b49, 0.63),
yellow: material(0xf0bf55, 0.72),
steel: material(0x8ea1a3, 0.42, 0.62),
steelDark: material(0x46565c, 0.5, 0.58),
black: material(0x202a30, 0.7, 0.25),
part: material(0x7ca6a4, 0.38, 0.58),
partEdge: material(0x668d8c, 0.43, 0.5),
brass: material(0xc89c53, 0.38, 0.65),
red: material(0xd85d4e, 0.65),
skin: material(0xd6a27f, 0.9),
shirt: material(0x3e7180, 0.9),
white: material(0xf2f0df, 0.82)
};
function addBox(parent, size, position, mat, shadow = true) {
const mesh = new THREE.Mesh(new THREE.BoxGeometry(...size), mat);
mesh.position.set(...position);
mesh.castShadow = shadow;
mesh.receiveShadow = shadow;
parent.add(mesh);
return mesh;
}
function addCylinder(parent, radius, height, position, mat, radialSegments = 12) {
const mesh = new THREE.Mesh(
new THREE.CylinderGeometry(radius, radius, height, radialSegments),
mat
);
mesh.position.set(...position);
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
function roundedRect(ctx, x, y, w, h, r) {
ctx.beginPath();
ctx.moveTo(x + r, y);
ctx.arcTo(x + w, y, x + w, y + h, r);
ctx.arcTo(x + w, y + h, x, y + h, r);
ctx.arcTo(x, y + h, x, y, r);
ctx.arcTo(x, y, x + w, y, r);
ctx.closePath();
}
function makeWallPanel(text, subtitle, width, height, position, accent = "#238f85") {
const canvas = document.createElement("canvas");
canvas.width = 1024;
canvas.height = 256;
const ctx = canvas.getContext("2d");
ctx.fillStyle = "#17313c";
ctx.fillRect(0, 0, canvas.width, canvas.height);
ctx.fillStyle = accent;
ctx.fillRect(0, 0, 18, canvas.height);
ctx.fillStyle = "#eef5ed";
ctx.font = "900 74px system-ui, sans-serif";
ctx.fillText(text, 54, 108);
ctx.fillStyle = "#93aaa9";
ctx.font = "700 27px system-ui, sans-serif";
ctx.fillText(subtitle, 57, 165);
ctx.fillStyle = "#4f6d70";
for (let i = 0; i < 10; i++) ctx.fillRect(770 + i * 22, 190, 12, 8);
const texture = new THREE.CanvasTexture(canvas);
texture.colorSpace = THREE.SRGBColorSpace;
texture.anisotropy = renderer.capabilities.getMaxAnisotropy();
const mesh = new THREE.Mesh(
new THREE.PlaneGeometry(width, height),
new THREE.MeshBasicMaterial({ map: texture })
);
mesh.position.set(...position);
scene.add(mesh);
return mesh;
}
function makeSpriteLabel(text, color = "#18303a", background = "rgba(248,250,247,.94)", width = 1.15) {
const canvas = document.createElement("canvas");
canvas.width = 512;
canvas.height = 128;
const ctx = canvas.getContext("2d");
roundedRect(ctx, 8, 14, 496, 100, 22);
ctx.fillStyle = background;
ctx.fill();
ctx.strokeStyle = "rgba(24,48,58,.15)";
ctx.lineWidth = 3;
ctx.stroke();
ctx.fillStyle = color;
ctx.font = "900 42px system-ui, sans-serif";
ctx.textAlign = "center";
ctx.textBaseline = "middle";
ctx.fillText(text, 256, 65);
const texture = new THREE.CanvasTexture(canvas);
texture.colorSpace = THREE.SRGBColorSpace;
const sprite = new THREE.Sprite(
new THREE.SpriteMaterial({
map: texture,
transparent: true,
depthTest: false,
depthWrite: false
})
);
sprite.scale.set(width, width * 0.25, 1);
sprite.renderOrder = 20;
return sprite;
}
function makeDashedLine(points, color = 0x238f85, opacity = 0.8) {
const geometry = new THREE.BufferGeometry().setFromPoints(
points.map(p => new THREE.Vector3(...p))
);
const line = new THREE.Line(
geometry,
new THREE.LineDashedMaterial({
color,
dashSize: 0.12,
gapSize: 0.08,
transparent: true,
opacity
})
);
line.computeLineDistances();
return line;
}
// -------------------------------------------------------------------------
// Workshop room
// -------------------------------------------------------------------------
addBox(scene, [16, 0.22, 10], [0, -0.13, 0], M.floor);
addBox(scene, [16.2, 0.18, 10.2], [0, -0.25, 0], M.floorEdge);
addBox(scene, [16, 5, 0.24], [0, 2.5, -4.35], M.wall);
addBox(scene, [0.24, 5, 10], [-8.05, 2.5, 0], M.wall);
addBox(scene, [16, 1.25, 0.08], [0, 0.62, -4.18], M.wallLower);
addBox(scene, [0.08, 1.25, 10], [-7.9, 0.62, 0], M.wallLower);
const grid = new THREE.GridHelper(16, 32, 0x758985, 0x9baaa4);
grid.position.y = 0.005;
grid.material.transparent = true;
grid.material.opacity = 0.22;
scene.add(grid);
// Floor safety boundary.
for (const x of [-4.25, 4.25]) {
addBox(scene, [0.10, 0.035, 4.9], [x, 0.015, 0.1], M.yellow, false);
}
for (const z of [-2.3, 2.5]) {
addBox(scene, [8.6, 0.035, 0.10], [0, 0.015, z], M.yellow, false);
}
// Workbench.
const bench = new THREE.Group();
scene.add(bench);
addBox(bench, [8.1, 0.22, 3.55], [0, 1.04, 0.08], M.wood);
addBox(bench, [8.2, 0.09, 3.65], [0, 1.18, 0.08], M.woodDark);
addBox(bench, [7.65, 0.13, 3.12], [0, 0.47, 0.08], M.woodDark);
for (const x of [-3.55, 3.55]) {
for (const z of [-1.28, 1.38]) {
addBox(bench, [0.25, 1.02, 0.25], [x, 0.51, z], M.navy);
}
}
addBox(bench, [2.5, 0.12, 0.12], [0, 0.78, -1.45], M.steelDark);
// Wall signage.
makeWallPanel(
"TINY FIXTURES",
"SMALL-BATCH TOOLING / CELL 04",
4.5,
1.05,
[0.35, 4.12, -4.19],
"#ef8b49"
);
// Pegboard and hanging tools.
addBox(scene, [2.55, 1.65, 0.10], [3.45, 2.65, -4.14], M.woodDark);
addBox(scene, [2.38, 1.48, 0.06], [3.45, 2.65, -4.06], M.navyLight);
for (let i = 0; i < 6; i++) {
const x = 2.55 + i * 0.36;
addBox(scene, [0.09, 0.46 + (i % 3) * 0.11, 0.10], [x, 2.78, -3.99], M.steel);
addBox(scene, [0.16, 0.10, 0.16], [x, 3.06, -3.98], M.orange);
}
// Shelf and storage bins.
addBox(scene, [3.2, 0.12, 0.72], [-4.7, 3.10, -3.88], M.woodDark);
for (const x of [-5.75, -4.75, -3.75]) {
addBox(scene, [0.68, 0.52, 0.55], [x, 3.42, -3.83], M.teal);
addBox(scene, [0.48, 0.06, 0.05], [x, 3.43, -3.53], M.yellow);
}
// Tool cabinet.
addBox(scene, [2.15, 2.15, 1.05], [-5.7, 1.08, -2.93], M.navy);
for (let i = 0; i < 4; i++) {
addBox(scene, [1.95, 0.43, 0.05], [-5.7, 0.42 + i * 0.51, -2.38], M.navyLight);
addBox(scene, [0.52, 0.07, 0.08], [-5.7, 0.42 + i * 0.51, -2.32], M.steel);
}
// Voxel technician for scale.
const worker = new THREE.Group();
worker.position.set(-4.75, 0, -1.55);
worker.rotation.y = 0.55;
scene.add(worker);
addBox(worker, [0.34, 0.20, 0.55], [-0.22, 0.10, 0.05], M.black);
addBox(worker, [0.34, 0.20, 0.55], [0.22, 0.10, 0.05], M.black);
addBox(worker, [0.27, 0.72, 0.29], [-0.22, 0.56, 0], M.navy);
addBox(worker, [0.27, 0.72, 0.29], [0.22, 0.56, 0], M.navy);
addBox(worker, [0.80, 0.78, 0.43], [0, 1.30, 0], M.shirt);
addBox(worker, [0.82, 0.48, 0.46], [0, 1.31, 0.01], M.orange);
addBox(worker, [0.20, 0.68, 0.24], [-0.52, 1.28, 0.02], M.shirt);
addBox(worker, [0.20, 0.68, 0.24], [0.52, 1.28, 0.02], M.shirt);
addBox(worker, [0.52, 0.52, 0.49], [0, 1.98, 0], M.skin);
addBox(worker, [0.66, 0.24, 0.60], [0, 2.31, 0], M.yellow);
addBox(worker, [0.67, 0.08, 0.25], [0, 2.28, 0.37], M.yellow);
// -------------------------------------------------------------------------
// Voxel plate: one physical cell for each hole
// 1 world unit represents approximately 100 mm.
// -------------------------------------------------------------------------
const CELL = 0.20;
const COLS = 14;
const ROWS = 8;
const HOLES = [
{ x: -0.7, z: -0.3 },
{ x: 0.7, z: -0.3 },
{ x: 0.7, z: 0.3 },
{ x: -0.7, z: 0.3 }
];
const holeKeys = new Set(HOLES.map(h => `${h.x.toFixed(2)},${h.z.toFixed(2)}`));
function makePart() {
const group = new THREE.Group();
const cells = [];
for (let row = 0; row < ROWS; row++) {
for (let col = 0; col < COLS; col++) {
const x = (col - (COLS - 1) / 2) * CELL;
const z = (row - (ROWS - 1) / 2) * CELL;
const key = `${x.toFixed(2)},${z.toFixed(2)}`;
if (!holeKeys.has(key)) cells.push({ x, z });
}
}
const geometry = new THREE.BoxGeometry(CELL * 0.965, 0.14, CELL * 0.965);
const body = new THREE.InstancedMesh(geometry, M.part, cells.length);
body.castShadow = true;
body.receiveShadow = true;
const matrix = new THREE.Matrix4();
cells.forEach((cell, i) => {
matrix.makeTranslation(cell.x, 0.07, cell.z);
body.setMatrixAt(i, matrix);
});
body.instanceMatrix.needsUpdate = true;
group.add(body);
const plugs = [];
for (const hole of HOLES) {
const plug = addBox(
group,
[CELL * 0.965, 0.145, CELL * 0.965],
[hole.x, 0.07, hole.z],
M.part
);
plugs.push(plug);
}
group.userData.plugs = plugs;
group.userData.body = body;
return group;
}
// -------------------------------------------------------------------------
// Drilling jig and machine frame
// -------------------------------------------------------------------------
const jig = new THREE.Group();
scene.add(jig);
addBox(jig, [4.25, 0.16, 2.62], [0, 1.24, 0], M.navy);
addBox(jig, [3.86, 0.035, 2.28], [0, 1.335, 0], M.steelDark);
addBox(jig, [3.55, 0.045, 1.94], [0, 1.36, 0], M.navyLight);
// Locating shoulders remain clear of the drill rectangle.
for (const x of [-1.50, 1.50]) {
for (const z of [-0.89, 0.89]) {
addBox(jig, [0.14, 0.18, 0.14], [x, 1.43, z], M.brass);
}
}
// Four-post gantry.
for (const x of [-1.95, 1.95]) {
for (const z of [-1.09, 1.09]) {
addBox(jig, [0.22, 2.55, 0.22], [x, 2.52, z], M.navy);
addBox(jig, [0.34, 0.11, 0.34], [x, 1.36, z], M.steelDark);
}
}
for (const z of [-1.09, 1.09]) {
addBox(jig, [4.14, 0.20, 0.22], [0, 3.78, z], M.navyLight);
}
addBox(jig, [0.18, 0.18, 2.35], [-1.95, 3.78, 0], M.teal);
addBox(jig, [0.18, 0.18, 2.35], [1.95, 3.78, 0], M.teal);
// Side clamps. Closed inner faces are x = ±1.39.
const clampJaws = [];
for (const side of [-1, 1]) {
const fixed = new THREE.Group();
jig.add(fixed);
addBox(fixed, [0.48, 0.18, 0.64], [side * 1.78, 1.45, 0.3], M.steelDark);
addBox(fixed, [0.18, 0.58, 0.22], [side * 1.83, 1.75, 0.3], M.navy);
addBox(fixed, [0.34, 0.10, 0.34], [side * 1.83, 2.06, 0.3], M.orange);
const jaw = new THREE.Group();
jig.add(jaw);
addBox(jaw, [0.18, 0.18, 0.42], [side * 1.48, 1.56, 0.3], M.teal);
addBox(jaw, [0.50, 0.13, 0.30], [side * 1.74, 1.56, 0.3], M.steel);
addBox(jaw, [0.10, 0.10, 0.48], [side * 1.60, 1.77, 0.3], M.brass);
clampJaws.push({ jaw, side });
}
// Moving bridge and drill carriage.
const movingX = new THREE.Group();
jig.add(movingX);
addBox(movingX, [0.24, 0.24, 2.36], [0, 3.64, 0], M.teal);
addBox(movingX, [0.13, 0.13, 2.45], [0, 3.49, 0], M.steelDark);
const carriage = new THREE.Group();
movingX.add(carriage);
addBox(carriage, [0.60, 0.40, 0.58], [0, 3.53, 0], M.navy);
addBox(carriage, [0.40, 0.16, 0.40], [0, 3.28, 0], M.steel);
const telescopingShaft = addBox(
carriage,
[0.16, 1, 0.16],
[0, 3.1, 0],
M.steel
);
const head = new THREE.Group();
carriage.add(head);
const bitAssembly = new THREE.Group();
head.add(bitAssembly);
addCylinder(bitAssembly, 0.045, 0.72, [0, 0.36, 0], M.steelDark, 8);
for (let i = 0; i < 4; i++) {
addBox(
bitAssembly,
[0.13, 0.055, 0.055],
[0, 0.17 + i * 0.15, 0],
M.brass
);
}
addCylinder(head, 0.16, 0.18, [0, 0.78, 0], M.orange, 10);
addBox(head, [0.50, 0.58, 0.50], [0, 1.00, 0], M.teal);
addBox(head, [0.26, 0.12, 0.26], [0, 1.34, 0], M.steelDark);
// Control console.
addBox(jig, [0.82, 0.52, 0.58], [2.43, 1.46, -0.90], M.navy);
addBox(jig, [0.56, 0.28, 0.06], [2.43, 1.58, -0.59], M.black);
for (const x of [2.22, 2.44, 2.66]) {
addCylinder(jig, 0.055, 0.06, [x, 1.30, -0.57], x === 2.44 ? M.red : M.tealLight, 10);
}
// -------------------------------------------------------------------------
// Stock rack, output tray, and active workpiece
// -------------------------------------------------------------------------
const STOCK_X = -3.05;
const STOCK_Z = 0.65;
const OUTPUT_X = 3.05;
const OUTPUT_Z = 0.65;
const JIG_Y = 1.33;
addBox(scene, [1.25, 0.13, 1.32], [STOCK_X, 1.27, STOCK_Z], M.navy);
for (const z of [STOCK_Z - 0.48, STOCK_Z + 0.48]) {
addBox(scene, [1.12, 0.10, 0.12], [STOCK_X, 1.36, z], M.steelDark);
}
const stockA = makePart();
stockA.position.set(STOCK_X, 1.40, STOCK_Z);
scene.add(stockA);
const stockB = makePart();
stockB.position.set(STOCK_X, 1.61, STOCK_Z);
scene.add(stockB);
const stockLabel = makeSpriteLabel("BLANK STOCK", "#18303a", "rgba(248,250,247,.94)", 1.15);
stockLabel.position.set(STOCK_X, 2.16, STOCK_Z);
scene.add(stockLabel);
addBox(scene, [1.42, 0.13, 1.45], [OUTPUT_X, 1.26, OUTPUT_Z], M.navy);
addBox(scene, [1.23, 0.06, 1.23], [OUTPUT_X, 1.35, OUTPUT_Z], M.steelDark);
for (const x of [OUTPUT_X - 0.69, OUTPUT_X + 0.69]) {
addBox(scene, [0.08, 0.18, 1.40], [x, 1.39, OUTPUT_Z], M.teal);
}
const outputLabel = makeSpriteLabel("FINISHED", "#18303a", "rgba(248,250,247,.94)", 1.02);
outputLabel.position.set(OUTPUT_X, 2.02, OUTPUT_Z);
scene.add(outputLabel);
const activePart = makePart();
activePart.position.set(0, JIG_Y, 0);
scene.add(activePart);
const outputPart = makePart();
outputPart.userData.plugs.forEach(p => p.visible = false);
outputPart.position.set(OUTPUT_X, 1.38, OUTPUT_Z);
outputPart.visible = false;
scene.add(outputPart);
// -------------------------------------------------------------------------
// Clearance and path guides
// -------------------------------------------------------------------------
const guidesGroup = new THREE.Group();
scene.add(guidesGroup);
const SAFE_TIP = 2.60;
const WORK_TIP = 1.31;
for (const hole of HOLES) {
const envelope = new THREE.Mesh(
new THREE.CylinderGeometry(0.09, 0.09, SAFE_TIP - 1.48, 16, 1, true),
new THREE.MeshBasicMaterial({
color: 0x36b9a7,
transparent: true,
opacity: 0.10,
depthWrite: false,
side: THREE.DoubleSide
})
);
envelope.position.set(hole.x, (SAFE_TIP + 1.48) / 2, hole.z);
guidesGroup.add(envelope);
const vertical = new THREE.Line(
new THREE.BufferGeometry().setFromPoints([
new THREE.Vector3(hole.x, 1.47, hole.z),
new THREE.Vector3(hole.x, SAFE_TIP, hole.z)
]),
new THREE.LineDashedMaterial({
color: 0x168d83,
dashSize: 0.08,
gapSize: 0.055,
transparent: true,
opacity: 0.78
})
);
vertical.computeLineDistances();
guidesGroup.add(vertical);
const target = new THREE.LineLoop(
new THREE.BufferGeometry().setFromPoints([
new THREE.Vector3(hole.x - 0.14, 1.478, hole.z - 0.14),
new THREE.Vector3(hole.x + 0.14, 1.478, hole.z - 0.14),
new THREE.Vector3(hole.x + 0.14, 1.478, hole.z + 0.14),
new THREE.Vector3(hole.x - 0.14, 1.478, hole.z + 0.14)
]),
new THREE.LineBasicMaterial({ color: 0xef8b49 })
);
guidesGroup.add(target);
}
// True dimension from the outer edge of the drill envelope to the clamp face.
const dimY = 1.58;
const dimZ = 0.3;
const dimension = new THREE.Group();
dimension.add(
makeDashedLine([[0.80, dimY, dimZ], [1.39, dimY, dimZ]], 0x168d83, 1)
);
dimension.add(
makeDashedLine([[0.80, dimY, dimZ - 0.09], [0.80, dimY, dimZ + 0.09]], 0x168d83, 1)
);
dimension.add(
makeDashedLine([[1.39, dimY, dimZ - 0.09], [1.39, dimY, dimZ + 0.09]], 0x168d83, 1)
);
const clearanceLabel = makeSpriteLabel(
"60 mm MIN",
"#116f68",
"rgba(235,255,249,.96)",
1.10
);
clearanceLabel.position.set(1.10, 1.90, dimZ);
dimension.add(clearanceLabel);
guidesGroup.add(dimension);
const drillSizeLabel = makeSpriteLabel(
"Ø18 mm",
"#a65a2e",
"rgba(255,244,229,.96)",
0.88
);
drillSizeLabel.position.set(-0.7, 1.78, -0.3);
guidesGroup.add(drillSizeLabel);
// Overhead blank and finished routes.
const inRoute = makeDashedLine([
[STOCK_X, 2.21, STOCK_Z],
[0, 2.21, STOCK_Z]
], 0x168d83, 0.5);
guidesGroup.add(inRoute);
const outRoute = makeDashedLine([
[0, 2.21, 0],
[0, 2.21, 1.48],
[OUTPUT_X, 2.21, 1.48],
[OUTPUT_X, 2.21, OUTPUT_Z]
], 0xef8b49, 0.5);
guidesGroup.add(outRoute);
const traverseGeometry = new THREE.BufferGeometry().setFromPoints([
new THREE.Vector3(0, SAFE_TIP, 0),
new THREE.Vector3(0, SAFE_TIP, 0)
]);
const traverseLine = new THREE.Line(
traverseGeometry,
new THREE.LineDashedMaterial({
color: 0xef8b49,
dashSize: 0.09,
gapSize: 0.055,
transparent: true,
opacity: 0.9
})
);
traverseLine.computeLineDistances();
traverseLine.visible = false;
guidesGroup.add(traverseLine);
// -------------------------------------------------------------------------
// Voxel drill chips
// -------------------------------------------------------------------------
const chipGroup = new THREE.Group();
scene.add(chipGroup);
const chips = [];
for (let i = 0; i < 24; i++) {
const chip = addBox(chipGroup, [0.045, 0.045, 0.045], [0, -5, 0], M.brass, false);
chip.visible = false;
chips.push(chip);
}
// -------------------------------------------------------------------------
// Animation state
// -------------------------------------------------------------------------
const phases = [
{ key: "drill", title: "DRILLING", duration: 4.8 },
{ key: "home", title: "SPINDLE HOME", duration: 0.7 },
{ key: "release", title: "RELEASING CLAMPS", duration: 0.8 },
{ key: "unload", title: "UNLOADING FINISHED PART", duration: 2.4 },
{ key: "load", title: "LOADING BLANK", duration: 2.4 },
{ key: "clamp", title: "CLAMPING BLANK", duration: 0.8 }
];
const totalDuration = phases.reduce((sum, p) => sum + p.duration, 0);
let phaseIndex = -1;
let phaseTime = 0;
let running = false;
let speed = 1;
let cycle = 0;
let simulationTime = 0;
const headState = { x: 0, z: 0, y: SAFE_TIP };
const clamp = (value, min = 0, max = 1) => Math.max(min, Math.min(max, value));
const smooth = value => {
const t = clamp(value);
return t * t * (3 - 2 * t);
};
function interpolateKeys(keys, t) {
if (t <= keys[0].t) return new THREE.Vector3(...keys[0].p);
for (let i = 1; i < keys.length; i++) {
if (t <= keys[i].t) {
const a = keys[i - 1];
const b = keys[i];
const local = smooth((t - a.t) / (b.t - a.t));
return new THREE.Vector3(
THREE.MathUtils.lerp(a.p[0], b.p[0], local),
THREE.MathUtils.lerp(a.p[1], b.p[1], local),
THREE.MathUtils.lerp(a.p[2], b.p[2], local)
);
}
}
return new THREE.Vector3(...keys[keys.length - 1].p);
}
const unloadKeys = [
{ t: 0.00, p: [0, JIG_Y, 0] },
{ t: 0.25, p: [0, 2.05, 0] },
{ t: 0.45, p: [0, 2.05, 1.45] },
{ t: 0.72, p: [OUTPUT_X, 2.05, 1.45] },
{ t: 0.90, p: [OUTPUT_X, 1.72, OUTPUT_Z] },
{ t: 1.00, p: [OUTPUT_X, 1.38, OUTPUT_Z] }
];
const loadKeys = [
{ t: 0.00, p: [STOCK_X, 1.72, STOCK_Z] },
{ t: 0.18, p: [STOCK_X, 2.15, STOCK_Z] },
{ t: 0.50, p: [0, 2.15, STOCK_Z] },
{ t: 0.67, p: [0, 2.15, 1.45] },
{ t: 0.84, p: [0, 2.15, 0] },
{ t: 1.00, p: [0, JIG_Y, 0] }
];
function preparePhase(index) {
if (index === 0) {
activePart.visible = true;
activePart.position.set(0, JIG_Y, 0);
activePart.userData.plugs.forEach(p => p.visible = true);
headState.x = 0;
headState.z = 0;
headState.y = SAFE_TIP;
}
if (index === 3) {
activePart.visible = true;
activePart.position.set(0, JIG_Y, 0);
}
if (index === 4) {
activePart.visible = true;
activePart.position.set(STOCK_X, 1.72, STOCK_Z);
activePart.userData.plugs.forEach(p => p.visible = true);
outputPart.visible = true;
}
if (index === 5) {
activePart.visible = true;
activePart.position.set(0, JIG_Y, 0);
}
}
function advancePhase() {
phaseIndex++;
phaseTime = 0;
if (phaseIndex >= phases.length) {
running = false;
phaseIndex = phases.length;
updateUI();
return;
}
preparePhase(phaseIndex);
updateUI();
}
function startCycle() {
cycle++;
phaseIndex = 0;
phaseTime = 0;
running = true;
preparePhase(0);
updateUI();
}
function resetWorkshop() {
running = false;
phaseIndex = -1;
phaseTime = 0;
cycle = 0;
simulationTime = 0;
activePart.visible = true;
activePart.position.set(0, JIG_Y, 0);
activePart.userData.plugs.forEach(p => p.visible = true);
outputPart.visible = false;
headState.x = 0;
headState.z = 0;
headState.y = SAFE_TIP;
clampJaws.forEach(({ jaw, side }) => jaw.position.x = side * 0.24);
updateUI();
}
function setClampOpening(amount) {
for (const { jaw, side } of clampJaws) {
jaw.position.x = side * 0.24 * amount;
}
}
function updateDrillVisual() {
const t = phaseTime;
const holeIndex = Math.min(3, Math.floor(t / 1.2));
const local = clamp(t - holeIndex * 1.2, 0, 1.2);
const hole = HOLES[holeIndex];
const previous = holeIndex === 0 ? { x: 0, z: 0 } : HOLES[holeIndex - 1];
if (local < 0.25) {
const k = smooth(local / 0.25);
headState.x = THREE.MathUtils.lerp(previous.x, hole.x, k);
headState.z = THREE.MathUtils.lerp(previous.z, hole.z, k);
headState.y = SAFE_TIP;
traverseLine.visible = guidesGroup.visible;
const positions = traverseGeometry.attributes.position;
positions.setXYZ(0, previous.x, SAFE_TIP, previous.z);
positions.setXYZ(1, hole.x, SAFE_TIP, hole.z);
positions.needsUpdate = true;
traverseLine.computeLineDistances();
} else {
traverseLine.visible = false;
headState.x = hole.x;
headState.z = hole.z;
if (local < 0.55) {
const k = smooth((local - 0.25) / 0.30);
headState.y = THREE.MathUtils.lerp(SAFE_TIP, WORK_TIP, k);
} else if (local < 0.95) {
headState.y = WORK_TIP;
activePart.userData.plugs[holeIndex].visible = false;
} else {
const k = smooth((local - 0.95) / 0.25);
headState.y = THREE.MathUtils.lerp(WORK_TIP, SAFE_TIP, k);
}
}
updateChips(holeIndex, local);
}
function updateChips(holeIndex, local) {
const cutting = local >= 0.55 && local < 0.95;
chips.forEach(chip => chip.visible = false);
if (!cutting || !running) return;
const hole = HOLES[holeIndex];
for (let i = 0; i < chips.length; i++) {
const angle = i * 2.399 + holeIndex * 0.7;
const fraction = (simulationTime * 1.75 + i * 0.173) % 1;
const spread = 0.04 + fraction * 0.24;
const chip = chips[i];
chip.visible = true;
chip.position.set(
hole.x + Math.cos(angle) * spread,
1.49 + Math.sin(fraction * Math.PI) * 0.18 - fraction * 0.06,
hole.z + Math.sin(angle) * spread
);
chip.rotation.set(
simulationTime * 3 + i,
angle,
simulationTime * 2 + i * 0.4
);
}
}
function updatePhaseVisual() {
if (phaseIndex === 0) {
setClampOpening(0);
updateDrillVisual();
} else if (phaseIndex === 1) {
const k = smooth(phaseTime / phases[1].duration);
headState.x = THREE.MathUtils.lerp(HOLES[3].x, 0, k);
headState.z = THREE.MathUtils.lerp(HOLES[3].z, 0, k);
headState.y = SAFE_TIP;
traverseLine.visible = false;
chips.forEach(chip => chip.visible = false);
} else if (phaseIndex === 2) {
setClampOpening(smooth(phaseTime / phases[2].duration));
traverseLine.visible = false;
} else if (phaseIndex === 3) {
setClampOpening(1);
const p = interpolateKeys(unloadKeys, phaseTime / phases[3].duration);
activePart.position.copy(p);
traverseLine.visible = false;
chips.forEach(chip => chip.visible = false);
if (phaseTime >= phases[3].duration) {
outputPart.position.copy(activePart.position);
outputPart.visible = true;
}
} else if (phaseIndex === 4) {
setClampOpening(1);
const p = interpolateKeys(loadKeys, phaseTime / phases[4].duration);
activePart.position.copy(p);
traverseLine.visible = false;
chips.forEach(chip => chip.visible = false);
} else if (phaseIndex === 5) {
const k = smooth(phaseTime / phases[5].duration);
setClampOpening(1 - k);
activePart.position.set(0, JIG_Y, 0);
traverseLine.visible = false;
chips.forEach(chip => chip.visible = false);
} else {
setClampOpening(0);
headState.x = 0;
headState.z = 0;
headState.y = SAFE_TIP;
traverseLine.visible = false;
chips.forEach(chip => chip.visible = false);
}
movingX.position.x = headState.x;
carriage.position.z = headState.z;
head.position.y = headState.y;
bitAssembly.rotation.y = simulationTime * 24;
const shaftTop = headState.y + 1.29;
const shaftBottom = 3.30;
const shaftLength = Math.max(0.12, shaftBottom - shaftTop);
telescopingShaft.scale.y = shaftLength;
telescopingShaft.position.y = (shaftTop + shaftBottom) / 2;
}
function updateUI() {
cycleCount.textContent = `Cycle ${String(cycle).padStart(2, "0")}`;
let progress = 1;
let title = "READY";
let detail = "Blank seated in the jig. Clamps are closed and the spindle is at safe height.";
let dot = "#168d83";
let currentStep = -1;
if (phaseIndex >= 0 && phaseIndex < phases.length) {
const phase = phases[phaseIndex];
title = phase.title;
currentStep = phaseIndex;
let completed = 0;
for (let i = 0; i < phaseIndex; i++) completed += phases[i].duration;
progress = clamp((completed + phaseTime) / totalDuration);
if (phase.key === "drill") {
const holeIndex = Math.min(3, Math.floor(phaseTime / 1.2));
const local = phaseTime - holeIndex * 1.2;
if (local < 0.25) {
detail = `Safe-height transfer to hole ${holeIndex + 1} of 4.`;
} else if (local < 0.55) {
detail = `Descending into repeat position ${holeIndex + 1} / 4.`;
} else if (local < 0.95) {
detail = `Ø18 mm cut active — hole ${holeIndex + 1} of 4.`;
dot = "#ef8b49";
} else {
detail = `Retracting from hole ${holeIndex + 1}; spindle remains inside the safe envelope.`;
}
} else if (phase.key === "home") {
detail = "Spindle travels at safe height and parks clear of the part.";
} else if (phase.key === "release") {
detail = "Clamp jaws retract 24 mm, clearing the plate edge before removal.";
dot = "#ef8b49";
} else if (phase.key === "unload") {
detail = "Finished plate lifts, routes around the frame and enters the output tray.";
dot = "#ef8b49";
} else if (phase.key === "load") {
detail = "A fresh blank leaves the stock rack and follows the overhead loading route.";
dot = "#d59b32";
} else if (phase.key === "clamp") {
detail = "Locators close on the seated blank. Drilling remains locked out until complete.";
}
} else if (phaseIndex === phases.length) {
progress = 1;
title = "READY";
detail = "Replacement blank is seated and clamped. The completed part is in the output tray.";
}
phaseTitle.textContent = title;
phaseDetail.textContent = detail;
statusDot.style.background = dot;
statusDot.style.boxShadow = `0 0 0 5px ${dot}22`;
progressFill.style.width = `${progress * 100}%`;
sequenceState.textContent = running ? "Running" : phaseIndex >= 0 && phaseIndex < phases.length ? "Paused" : "Idle";
stepElements.forEach((element, index) => {
element.classList.toggle("active", index === currentStep);
element.classList.toggle("done", currentStep >= 0 && index < currentStep);
});
runButton.textContent = running
? "Pause cycle"
: phaseIndex >= 0 && phaseIndex < phases.length
? "Resume cycle"
: "Run batch cycle";
}
// -------------------------------------------------------------------------
// Controls
// -------------------------------------------------------------------------
runButton.addEventListener("click", () => {
if (running) {
running = false;
} else if (phaseIndex >= 0 && phaseIndex < phases.length) {
running = true;
} else {
startCycle();
}
updateUI();
});
resetButton.addEventListener("click", resetWorkshop);
speedInput.addEventListener("input", () => {
speed = Number(speedInput.value);
speedValue.textContent = `${speed.toFixed(1)}×`;
});
guidesInput.addEventListener("change", () => {
guidesGroup.visible = guidesInput.checked;
});
// -------------------------------------------------------------------------
// Direct camera controls: no OrbitControls or addons are used.
// -------------------------------------------------------------------------
const cameraState = {
target: new THREE.Vector3(0, 1.65, 0),
desiredTarget: new THREE.Vector3(0, 1.65, 0),
theta: 0.76,
desiredTheta: 0.76,
phi: 1.01,
desiredPhi: 1.01,
radius: 11.9,
desiredRadius: 11.9
};
let dragging = false;
let pointerX = 0;
let pointerY = 0;
renderer.domElement.addEventListener("pointerdown", event => {
dragging = true;
pointerX = event.clientX;
pointerY = event.clientY;
renderer.domElement.setPointerCapture(event.pointerId);
});
renderer.domElement.addEventListener("pointermove", event => {
if (!dragging) return;
const dx = event.clientX - pointerX;
const dy = event.clientY - pointerY;
pointerX = event.clientX;
pointerY = event.clientY;
cameraState.desiredTheta -= dx * 0.006;
cameraState.desiredPhi = clamp(cameraState.desiredPhi - dy * 0.005, 0.18, 1.43);
});
function endDrag(event) {
dragging = false;
if (renderer.domElement.hasPointerCapture(event.pointerId)) {
renderer.domElement.releasePointerCapture(event.pointerId);
}
}
renderer.domElement.addEventListener("pointerup", endDrag);
renderer.domElement.addEventListener("pointercancel", endDrag);
renderer.domElement.addEventListener("wheel", event => {
event.preventDefault();
cameraState.desiredRadius = clamp(
cameraState.desiredRadius * Math.exp(event.deltaY * 0.001),
5.5,
22
);
}, { passive: false });
document.querySelectorAll(".view").forEach(button => {
button.addEventListener("click", () => {
const view = button.dataset.view;
if (view === "iso") {
cameraState.desiredTheta = 0.76;
cameraState.desiredPhi = 1.01;
cameraState.desiredRadius = 11.9;
cameraState.desiredTarget.set(0, 1.65, 0);
} else if (view === "top") {
cameraState.desiredTheta = 0;
cameraState.desiredPhi = 0.20;
cameraState.desiredRadius = 10.8;
cameraState.desiredTarget.set(0, 1.35, 0);
} else if (view === "side") {
cameraState.desiredTheta = Math.PI / 2;
cameraState.desiredPhi = 1.27;
cameraState.desiredRadius = 10.8;
cameraState.desiredTarget.set(0, 1.85, 0);
} else if (view === "front") {
cameraState.desiredTheta = 0;
cameraState.desiredPhi = 1.19;
cameraState.desiredRadius = 10.4;
cameraState.desiredTarget.set(0, 1.85, 0);
}
});
});
function updateCamera() {
const damping = 0.09;
cameraState.theta += (cameraState.desiredTheta - cameraState.theta) * damping;
cameraState.phi += (cameraState.desiredPhi - cameraState.phi) * damping;
cameraState.radius += (cameraState.desiredRadius - cameraState.radius) * damping;
cameraState.target.lerp(cameraState.desiredTarget, damping);
const sinPhi = Math.sin(cameraState.phi);
camera.position.set(
cameraState.target.x + cameraState.radius * sinPhi * Math.sin(cameraState.theta),
cameraState.target.y + cameraState.radius * Math.cos(cameraState.phi),
cameraState.target.z + cameraState.radius * sinPhi * Math.cos(cameraState.theta)
);
camera.lookAt(cameraState.target);
}
window.addEventListener("resize", () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
});
// -------------------------------------------------------------------------
// Render loop
// -------------------------------------------------------------------------
const clock = new THREE.Clock();
function animate() {
requestAnimationFrame(animate);
const dt = Math.min(clock.getDelta(), 0.05);
if (running) {
simulationTime += dt * speed;
phaseTime += dt * speed;
while (running && phaseIndex < phases.length && phaseTime >= phases[phaseIndex].duration) {
phaseTime -= phases[phaseIndex].duration;
advancePhase();
}
}
updatePhaseVisual();
updateCamera();
renderer.render(scene, camera);
}
updateUI();
animate();
requestAnimationFrame(() => {
document.getElementById("loading").classList.add("hidden");
});
</script>
</body>
</html>
```