Prompt specification
Build or customize the Three.js 3D scene "Repair café missing-tool puzzle" 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: Repair café missing-tool puzzle
Category: Everyday & Industrial · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/repair-caf-missing-tool-puzzle-x0304
Scene Description Prompt:
Build a self-contained HTML scene with Three.js from cdnjs featuring screwheads and matching driver bits. Create several simple repair stations with shared driver bits; visitors exchange the correct tools and complete assemblies only after compatible profiles are visibly paired. Low-poly.
Working Three.js Starter (repair-caf-missing-tool-puzzle-x0304):
```html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="theme-color" content="#18232b">
<title>Bolt Commons — Shared Tool Exchange</title>
<style>
:root {
--ink: #18232b;
--paper: #f4efe4;
--panel: rgba(24, 35, 43, 0.92);
--line: rgba(255, 255, 255, 0.15);
--green: #55c69b;
--red: #ff806d;
--gold: #efb955;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #e9e1d2;
color: var(--ink);
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
button {
font: inherit;
}
#scene {
position: fixed;
inset: 0;
}
#scene canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
outline: none;
}
.hud {
position: fixed;
inset: 0;
pointer-events: none;
}
.brand {
position: absolute;
top: 24px;
left: 28px;
width: min(390px, calc(100vw - 56px));
}
.brand-kicker,
.panel-kicker {
display: block;
margin-bottom: 7px;
color: #53616a;
font-size: 11px;
font-weight: 800;
letter-spacing: 0.19em;
text-transform: uppercase;
}
.brand h1 {
margin: 0;
font-size: clamp(27px, 3.2vw, 48px);
line-height: 0.95;
letter-spacing: -0.055em;
text-transform: uppercase;
}
.brand h1 span {
color: #d96f43;
}
.brand p {
max-width: 340px;
margin: 12px 0 0;
color: #59666d;
font-size: 13px;
line-height: 1.5;
}
.actions {
position: absolute;
top: 24px;
right: 28px;
display: flex;
align-items: center;
gap: 10px;
pointer-events: auto;
}
.action {
min-height: 42px;
padding: 0 15px;
border: 1px solid rgba(24, 35, 43, 0.14);
border-radius: 12px;
background: rgba(255, 255, 255, 0.72);
color: var(--ink);
cursor: pointer;
box-shadow: 0 8px 30px rgba(35, 40, 42, 0.08);
backdrop-filter: blur(10px);
font-size: 12px;
font-weight: 750;
transition:
transform 150ms ease,
background 150ms ease;
}
.action:hover {
transform: translateY(-2px);
background: white;
}
.action.primary {
border-color: var(--ink);
background: var(--ink);
color: white;
}
.count {
position: absolute;
top: 80px;
right: 28px;
display: flex;
align-items: baseline;
gap: 8px;
padding: 10px 14px;
border: 1px solid rgba(24, 35, 43, 0.1);
border-radius: 12px;
background: rgba(255, 255, 255, 0.65);
backdrop-filter: blur(10px);
}
.count strong {
font-size: 23px;
letter-spacing: -0.04em;
}
.count span {
color: #667178;
font-size: 11px;
font-weight: 800;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.checklist {
position: absolute;
top: 138px;
right: 28px;
width: 268px;
padding: 15px;
border: 1px solid var(--line);
border-radius: 18px;
background: var(--panel);
color: white;
box-shadow: 0 20px 60px rgba(24, 35, 43, 0.18);
pointer-events: auto;
}
.checklist .panel-kicker {
color: #9caab1;
margin-left: 3px;
}
.station-card {
position: relative;
display: grid;
grid-template-columns: 1fr auto;
gap: 2px 10px;
width: 100%;
margin-top: 9px;
padding: 11px 12px;
overflow: hidden;
border: 1px solid rgba(255, 255, 255, 0.09);
border-radius: 12px;
background: rgba(255, 255, 255, 0.045);
color: white;
text-align: left;
cursor: pointer;
transition:
border-color 150ms ease,
background 150ms ease,
transform 150ms ease;
}
.station-card:hover {
transform: translateX(-3px);
background: rgba(255, 255, 255, 0.09);
}
.station-card::before {
content: "";
position: absolute;
inset: 0 auto 0 0;
width: 4px;
background: var(--accent);
}
.station-card.pairing {
border-color: var(--green);
background: rgba(85, 198, 155, 0.16);
}
.station-card.wrong {
border-color: var(--red);
background: rgba(255, 128, 109, 0.14);
}
.station-card.done {
opacity: 0.67;
}
.station-id {
color: #9caab1;
font: 700 10px ui-monospace, SFMono-Regular, Menlo, monospace;
letter-spacing: 0.1em;
}
.profile-name {
display: flex;
align-items: center;
gap: 7px;
font-size: 12px;
font-weight: 850;
letter-spacing: 0.04em;
}
.profile-dot {
width: 8px;
height: 8px;
border-radius: 50%;
background: var(--accent);
box-shadow: 0 0 0 3px color-mix(in srgb, var(--accent) 18%, transparent);
}
.station-state {
grid-column: 1 / -1;
margin-top: 3px;
color: #aab4b9;
font-size: 10px;
}
.pair-readout {
position: absolute;
left: 28px;
bottom: 28px;
width: min(355px, calc(100vw - 56px));
padding: 17px 18px;
border: 1px solid var(--line);
border-radius: 17px;
background: var(--panel);
color: white;
box-shadow: 0 20px 60px rgba(24, 35, 43, 0.17);
transition: border-color 150ms ease;
}
.pair-readout.match {
border-color: var(--green);
}
.pair-readout.mismatch {
border-color: var(--red);
}
.pair-readout .panel-kicker {
color: #9caab1;
margin-bottom: 5px;
}
#pair-title {
display: block;
font-size: 18px;
letter-spacing: -0.025em;
}
#pair-detail {
display: block;
margin-top: 5px;
color: #b4bec3;
font-size: 12px;
line-height: 1.45;
}
.controls-note {
position: absolute;
right: 28px;
bottom: 28px;
padding: 10px 13px;
border: 1px solid rgba(24, 35, 43, 0.1);
border-radius: 11px;
background: rgba(255, 255, 255, 0.67);
color: #53616a;
font-size: 11px;
backdrop-filter: blur(10px);
}
.toast {
position: absolute;
top: 24px;
left: 50%;
max-width: min(460px, calc(100vw - 40px));
padding: 12px 18px;
border-radius: 999px;
background: var(--ink);
color: white;
box-shadow: 0 12px 35px rgba(24, 35, 43, 0.2);
font-size: 12px;
font-weight: 750;
text-align: center;
opacity: 0;
transform: translate(-50%, -12px);
transition:
opacity 180ms ease,
transform 180ms ease;
}
.toast.show {
opacity: 1;
transform: translate(-50%, 0);
}
@media (max-width: 820px) {
.brand {
top: 18px;
left: 18px;
}
.brand p,
.controls-note,
.checklist {
display: none;
}
.actions {
top: 18px;
right: 18px;
}
.action {
padding: 0 11px;
}
.count {
top: 68px;
right: 18px;
}
.pair-readout {
left: 18px;
bottom: 18px;
width: calc(100vw - 36px);
}
.toast {
top: 115px;
}
}
@media (max-width: 500px) {
.brand h1 {
font-size: 25px;
}
.brand-kicker {
font-size: 9px;
}
.action span {
display: none;
}
.action {
min-width: 42px;
padding: 0 11px;
}
}
</style>
</head>
<body>
<main id="scene" aria-label="Interactive low-poly repair workshop"></main>
<div class="hud">
<header class="brand">
<span class="brand-kicker">A hands-on tool exchange</span>
<h1>Bolt <span>Commons</span></h1>
<p>
Choose a shared driver bit and bring it to a repair station.
The tip must visibly match the screwhead profile before the assembly can close.
</p>
</header>
<nav class="actions" aria-label="Scene actions">
<button class="action" id="home-view" title="Restore the overview camera">
<span>↗ </span><span>Home view</span>
</button>
<button class="action primary" id="reset-tools" title="Return every bit and reopen every station">
↺ Reset
</button>
</nav>
<div class="count" aria-live="polite">
<strong id="completed-count">0 / 4</strong>
<span>assemblies sealed</span>
</div>
<aside class="checklist" aria-label="Repair station progress">
<span class="panel-kicker">Repair stations</span>
<div id="station-list"></div>
</aside>
<section class="pair-readout" id="pair-readout" aria-live="polite">
<span class="panel-kicker">Pairing window</span>
<strong id="pair-title">Shared rack ready</strong>
<span id="pair-detail">
Drag a driver bit from the center rack onto a screwhead.
</span>
</section>
<div class="controls-note">
Drag empty space to orbit · wheel to zoom · drag bits to benches
</div>
<div class="toast" id="toast" role="status" aria-live="polite"></div>
</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// -------------------------------------------------------------------------
// Scene setup
// -------------------------------------------------------------------------
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xe9e1d2);
scene.fog = new THREE.Fog(0xe9e1d2, 23, 43);
const camera = new THREE.PerspectiveCamera(
43,
innerWidth / innerHeight,
0.1,
100
);
const renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setSize(innerWidth, innerHeight);
renderer.setPixelRatio(Math.min(devicePixelRatio, 2));
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.06;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.domElement.tabIndex = 0;
renderer.domElement.setAttribute(
"aria-label",
"3D workshop. Drag driver bits onto matching repair stations."
);
document.getElementById("scene").appendChild(renderer.domElement);
const hemi = new THREE.HemisphereLight(0xfff4dc, 0x68767b, 1.75);
scene.add(hemi);
const sun = new THREE.DirectionalLight(0xffedcf, 2.5);
sun.position.set(8, 14, 9);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -14;
sun.shadow.camera.right = 14;
sun.shadow.camera.top = 14;
sun.shadow.camera.bottom = -14;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 38;
sun.shadow.normalBias = 0.025;
scene.add(sun);
const fill = new THREE.DirectionalLight(0x9bc7d5, 0.65);
fill.position.set(-10, 7, -8);
scene.add(fill);
const floor = new THREE.Mesh(
new THREE.PlaneGeometry(60, 60),
new THREE.MeshStandardMaterial({
color: 0xdcd3c2,
roughness: 1,
metalness: 0
})
);
floor.rotation.x = -Math.PI / 2;
floor.receiveShadow = true;
scene.add(floor);
const grid = new THREE.GridHelper(42, 42, 0x9e9586, 0xb6ad9e);
grid.position.y = 0.008;
grid.material.transparent = true;
grid.material.opacity = 0.22;
scene.add(grid);
const platform = new THREE.Mesh(
new THREE.CylinderGeometry(7.7, 8.05, 0.12, 16),
new THREE.MeshStandardMaterial({
color: 0xeee7d9,
roughness: 0.95,
flatShading: true
})
);
platform.position.y = 0.055;
platform.receiveShadow = true;
scene.add(platform);
// -------------------------------------------------------------------------
// Profiles and low-poly geometry
// -------------------------------------------------------------------------
const PROFILES = {
phillips: {
name: "PHILLIPS",
color: 0x4f83cf,
css: "#4f83cf"
},
flat: {
name: "FLAT",
color: 0xeb735f,
css: "#eb735f"
},
torx: {
name: "TORX",
color: 0xe4ad43,
css: "#e4ad43"
},
hex: {
name: "HEX",
color: 0x43a38c,
css: "#43a38c"
}
};
const material = (color, options = {}) =>
new THREE.MeshStandardMaterial({
color,
roughness: 0.72,
metalness: 0.08,
flatShading: true,
...options
});
const MAT = {
floor: material(0xdcd3c2, { roughness: 1 }),
platform: material(0xeee7d9, { roughness: 0.95 }),
dark: material(0x1c2a32, { roughness: 0.65 }),
darkMetal: material(0x35444b, { roughness: 0.46, metalness: 0.45 }),
steel: material(0xb7c1c1, { roughness: 0.35, metalness: 0.72 }),
steelDark: material(0x607077, { roughness: 0.38, metalness: 0.68 }),
cream: material(0xe9e0ce, { roughness: 0.9 }),
orange: material(0xd96f43, { roughness: 0.72 }),
rubber: material(0x253038, { roughness: 0.95 }),
green: material(0x55a987, { roughness: 0.78 }),
recess: new THREE.MeshBasicMaterial({ color: 0x111a20 })
};
function addMesh(parent, geometry, mat, x = 0, y = 0, z = 0) {
const mesh = new THREE.Mesh(geometry, mat);
mesh.position.set(x, y, z);
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
function createTorxGeometry(depth) {
const shape = new THREE.Shape();
const count = 12;
for (let i = 0; i < count; i++) {
const angle = (i / count) * Math.PI * 2 - Math.PI / 2;
const radius = i % 2 === 0 ? 0.32 : 0.205;
const x = Math.cos(angle) * radius;
const y = Math.sin(angle) * radius;
if (i === 0) shape.moveTo(x, y);
else shape.lineTo(x, y);
}
shape.closePath();
const geometry = new THREE.ExtrudeGeometry(shape, {
depth,
bevelEnabled: false,
steps: 1,
curveSegments: 1
});
geometry.rotateX(-Math.PI / 2);
return geometry;
}
function createProfileMark(profile, mode = "mark") {
const group = new THREE.Group();
const thickness = mode === "tip" ? 0.16 : 0.026;
const y = mode === "tip" ? 0.08 : 0.013;
if (profile === "flat") {
addMesh(
group,
new THREE.BoxGeometry(0.56, thickness, 0.12),
mode === "tip" ? MAT.steelDark : MAT.recess,
0,
y,
0
);
} else if (profile === "phillips") {
addMesh(
group,
new THREE.BoxGeometry(0.55, thickness, 0.105),
mode === "tip" ? MAT.steelDark : MAT.recess,
0,
y,
0
);
addMesh(
group,
new THREE.BoxGeometry(0.105, thickness, 0.55),
mode === "tip" ? MAT.steelDark : MAT.recess,
0,
y,
0
);
} else if (profile === "hex") {
addMesh(
group,
new THREE.CylinderGeometry(0.295, 0.295, thickness, 6),
mode === "tip" ? MAT.steelDark : MAT.recess,
0,
y,
0
);
} else {
const geometry = createTorxGeometry(thickness);
const mesh = addMesh(
group,
geometry,
mode === "tip" ? MAT.steelDark : MAT.recess,
0,
mode === "tip" ? 0.015 : 0,
0
);
mesh.castShadow = mode === "tip";
}
return group;
}
function createCoverGeometry() {
const shape = new THREE.Shape();
const count = 12;
for (let i = 0; i < count; i++) {
const angle = (i / count) * Math.PI * 2;
const x = Math.cos(angle) * 0.9;
const y = Math.sin(angle) * 0.9;
if (i === 0) shape.moveTo(x, y);
else shape.lineTo(x, y);
}
shape.closePath();
const hole = new THREE.Path();
hole.absarc(0, 0, 0.355, 0, Math.PI * 2, true);
shape.holes.push(hole);
const geometry = new THREE.ExtrudeGeometry(shape, {
depth: 0.12,
bevelEnabled: false,
steps: 1,
curveSegments: 12
});
geometry.rotateX(-Math.PI / 2);
return geometry;
}
// -------------------------------------------------------------------------
// Canvas-generated labels
// -------------------------------------------------------------------------
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 drawProfileIcon(ctx, profile, cx, cy, size) {
ctx.save();
ctx.translate(cx, cy);
ctx.fillStyle = PROFILES[profile].css;
ctx.strokeStyle = PROFILES[profile].css;
ctx.lineWidth = size * 0.17;
ctx.lineCap = "square";
if (profile === "flat") {
ctx.fillRect(-size * 0.5, -size * 0.11, size, size * 0.22);
} else if (profile === "phillips") {
ctx.fillRect(-size * 0.5, -size * 0.11, size, size * 0.22);
ctx.fillRect(-size * 0.11, -size * 0.5, size * 0.22, size);
} else if (profile === "hex") {
ctx.beginPath();
for (let i = 0; i < 6; i++) {
const a = (i / 6) * Math.PI * 2 - Math.PI / 2;
const x = Math.cos(a) * size * 0.5;
const y = Math.sin(a) * size * 0.5;
if (i === 0) ctx.moveTo(x, y);
else ctx.lineTo(x, y);
}
ctx.closePath();
ctx.stroke();
} else {
ctx.beginPath();
for (let i = 0; i < 12; i++) {
const a = (i / 12) * Math.PI * 2 - Math.PI / 2;
const r = size * (i % 2 === 0 ? 0.5 : 0.31);
const x = Math.cos(a) * r;
const y = Math.sin(a) * r;
if (i === 0) ctx.moveTo(x, y);
else ctx.lineTo(x, y);
}
ctx.closePath();
ctx.fill();
}
ctx.restore();
}
function textureFromCanvas(canvas) {
const texture = new THREE.CanvasTexture(canvas);
texture.colorSpace = THREE.SRGBColorSpace;
texture.anisotropy = Math.min(
8,
renderer.capabilities.getMaxAnisotropy()
);
return texture;
}
function makeProfileLabel(stationId, profile) {
const canvas = document.createElement("canvas");
canvas.width = 512;
canvas.height = 160;
const ctx = canvas.getContext("2d");
roundedRect(ctx, 3, 3, 506, 154, 24);
ctx.fillStyle = "rgba(24,35,43,0.94)";
ctx.fill();
roundedRect(ctx, 3, 3, 18, 154, 8);
ctx.fillStyle = PROFILES[profile].css;
ctx.fill();
drawProfileIcon(ctx, profile, 78, 80, 64);
ctx.fillStyle = "#ffffff";
ctx.font = "800 37px ui-monospace, SFMono-Regular, Menlo, monospace";
ctx.fillText(PROFILES[profile].name, 132, 68);
ctx.fillStyle = "#9eabb1";
ctx.font = "700 24px ui-monospace, SFMono-Regular, Menlo, monospace";
ctx.fillText(stationId, 133, 112);
return textureFromCanvas(canvas);
}
function makeSmallProfileLabel(profile) {
const canvas = document.createElement("canvas");
canvas.width = 384;
canvas.height = 104;
const ctx = canvas.getContext("2d");
roundedRect(ctx, 2, 2, 380, 100, 17);
ctx.fillStyle = "rgba(24,35,43,0.9)";
ctx.fill();
drawProfileIcon(ctx, profile, 53, 52, 45);
ctx.fillStyle = PROFILES[profile].css;
ctx.font = "800 29px ui-monospace, SFMono-Regular, Menlo, monospace";
ctx.fillText(PROFILES[profile].name, 93, 64);
return textureFromCanvas(canvas);
}
function makeBadge(text, cssColor, showCheck = false) {
const canvas = document.createElement("canvas");
canvas.width = 512;
canvas.height = 126;
const ctx = canvas.getContext("2d");
roundedRect(ctx, 4, 4, 504, 118, 24);
ctx.fillStyle = cssColor;
ctx.fill();
if (showCheck) {
ctx.strokeStyle = "#ffffff";
ctx.lineWidth = 10;
ctx.lineCap = "round";
ctx.lineJoin = "round";
ctx.beginPath();
ctx.moveTo(60, 64);
ctx.lineTo(82, 86);
ctx.lineTo(120, 39);
ctx.stroke();
}
ctx.fillStyle = "#ffffff";
ctx.font = "800 31px ui-monospace, SFMono-Regular, Menlo, monospace";
ctx.textAlign = showCheck ? "left" : "center";
ctx.fillText(text, showCheck ? 148 : 256, 75);
ctx.textAlign = "left";
return textureFromCanvas(canvas);
}
function makeTitleLabel(title, subtitle) {
const canvas = document.createElement("canvas");
canvas.width = 640;
canvas.height = 176;
const ctx = canvas.getContext("2d");
roundedRect(ctx, 3, 3, 634, 170, 26);
ctx.fillStyle = "rgba(24,35,43,0.94)";
ctx.fill();
ctx.fillStyle = "#f4b24f";
ctx.font = "800 38px ui-monospace, SFMono-Regular, Menlo, monospace";
ctx.fillText(title, 30, 70);
ctx.fillStyle = "#a7b3b9";
ctx.font = "700 22px ui-monospace, SFMono-Regular, Menlo, monospace";
ctx.fillText(subtitle, 31, 117);
return textureFromCanvas(canvas);
}
function sprite(texture, width, height) {
const material = new THREE.SpriteMaterial({
map: texture,
transparent: true,
depthWrite: false,
depthTest: false
});
const object = new THREE.Sprite(material);
object.scale.set(width, height, 1);
object.renderOrder = 10;
return object;
}
// -------------------------------------------------------------------------
// Shared bit rack
// -------------------------------------------------------------------------
const rack = new THREE.Group();
scene.add(rack);
addMesh(
rack,
new THREE.CylinderGeometry(1.02, 1.24, 0.94, 8),
MAT.dark,
0,
0.49,
0
);
addMesh(
rack,
new THREE.CylinderGeometry(1.34, 1.34, 0.18, 12),
MAT.cream,
0,
1.01,
0
);
addMesh(
rack,
new THREE.TorusGeometry(1.19, 0.035, 6, 24),
MAT.orange,
0,
1.11,
0
).rotation.x = Math.PI / 2;
const rackLabel = sprite(
makeTitleLabel("SHARED TOOL RACK", "RETURN EACH BIT AFTER USE"),
2.75,
0.76
);
rackLabel.position.set(0, 2.87, 0);
rack.add(rackLabel);
const bits = [];
const bitRoots = [];
const slotPositions = [];
const bitProfiles = ["phillips", "flat", "torx", "hex"];
const angles = [Math.PI * 0.75, Math.PI * 0.25, -Math.PI * 0.25, -Math.PI * 0.75];
function createBit(profile) {
const root = new THREE.Group();
scene.add(root);
const collarMaterial = material(PROFILES[profile].color, {
roughness: 0.58,
metalness: 0.18,
emissive: 0x000000,
emissiveIntensity: 0
});
const tipMaterial = material(0x607077, {
roughness: 0.35,
metalness: 0.72,
emissive: 0x000000,
emissiveIntensity: 0
});
addMesh(
root,
new THREE.CylinderGeometry(0.083, 0.083, 0.62, 8),
MAT.steel,
0,
0.44,
0
);
addMesh(
root,
new THREE.CylinderGeometry(0.155, 0.155, 0.15, 6),
collarMaterial,
0,
0.79,
0
);
addMesh(
root,
new THREE.CylinderGeometry(0.115, 0.145, 0.14, 6),
MAT.darkMetal,
0,
0.925,
0
);
const tip = createProfileMark(profile, "tip");
tip.position.y = 0.015;
// Replace the shared recess material with a separate emissive-capable one.
tip.traverse((child) => {
if (child.isMesh) {
child.material = tipMaterial;
}
});
root.add(tip);
const bit = {
root,
profile,
state: "rack",
home: new THREE.Vector3(),
dragTarget: new THREE.Vector3(),
collarMaterial,
tipMaterial,
animationTime: 0,
animationStart: new THREE.Vector3(),
station: null
};
root.userData.bit = bit;
bits.push(bit);
bitRoots.push(root);
return bit;
}
bitProfiles.forEach((profile, index) => {
const angle = angles[index];
const x = Math.cos(angle) * 0.68;
const z = Math.sin(angle) * 0.68;
const home = new THREE.Vector3(x, 1.115, z);
addMesh(
rack,
new THREE.CylinderGeometry(0.18, 0.18, 0.035, 12),
MAT.rubber,
x,
1.115,
z
);
const bit = createBit(profile);
bit.home.copy(home);
bit.root.position.copy(home);
slotPositions.push(home.clone());
const slotLabel = sprite(
makeSmallProfileLabel(profile),
1.38,
0.38
);
slotLabel.position.set(x, 2.23, z);
rack.add(slotLabel);
});
// -------------------------------------------------------------------------
// Repair stations
// -------------------------------------------------------------------------
const stations = [];
const stationPickMeshes = [];
const stationSpecs = [
{
id: "S-01",
profile: "phillips",
position: [-4.55, 0, 3.05]
},
{
id: "S-02",
profile: "flat",
position: [4.55, 0, 3.05]
},
{
id: "S-03",
profile: "torx",
position: [-4.45, 0, -3.0]
},
{
id: "S-04",
profile: "hex",
position: [4.45, 0, -3.0]
}
];
function createStation(spec, index) {
const group = new THREE.Group();
group.position.set(...spec.position);
scene.add(group);
addMesh(
group,
new THREE.BoxGeometry(2.25, 0.72, 1.65),
MAT.dark,
0,
0.38,
0
);
addMesh(
group,
new THREE.BoxGeometry(2.55, 0.12, 1.9),
MAT.orange,
0,
0.73,
0
);
addMesh(
group,
new THREE.BoxGeometry(3.05, 0.19, 2.35),
MAT.cream,
0,
0.86,
0
);
addMesh(
group,
new THREE.BoxGeometry(2.75, 0.055, 0.12),
MAT.darkMetal,
0,
0.785,
1.17
);
const assembly = new THREE.Group();
group.add(assembly);
addMesh(
assembly,
new THREE.CylinderGeometry(0.76, 0.81, 0.15, 12),
MAT.darkMetal,
0,
0.99,
0
);
const gasket = addMesh(
assembly,
new THREE.TorusGeometry(0.67, 0.035, 6, 24),
MAT.green,
0,
1.075,
0
);
gasket.rotation.x = Math.PI / 2;
addMesh(
assembly,
new THREE.CylinderGeometry(0.32, 0.32, 0.12, 16),
MAT.steel,
0,
1.115,
0
);
const profileMark = createProfileMark(spec.profile, "mark");
profileMark.position.y = 1.177;
assembly.add(profileMark);
// Decorative fastener heads make the station read as a real assembly.
for (let i = 0; i < 4; i++) {
const angle = Math.PI / 4 + (i * Math.PI) / 2;
const bolt = addMesh(
assembly,
new THREE.CylinderGeometry(0.065, 0.065, 0.045, 6),
MAT.steelDark,
Math.cos(angle) * 0.51,
1.085,
Math.sin(angle) * 0.51
);
bolt.rotation.y = angle;
}
const cover = new THREE.Group();
const coverMesh = addMesh(
cover,
createCoverGeometry(),
MAT.dark,
0,
0,
0
);
coverMesh.receiveShadow = true;
addMesh(
cover,
new THREE.BoxGeometry(0.34, 0.025, 0.13),
material(PROFILES[spec.profile].color, { roughness: 0.65 }),
0.65,
0.135,
0
);
const coverStart = new THREE.Vector3(1.02, 1.185, 0.12);
const coverEnd = new THREE.Vector3(0, 1.185, 0);
cover.position.copy(coverStart);
assembly.add(cover);
const anchor = new THREE.Object3D();
anchor.position.set(0, 1.18, 0);
assembly.add(anchor);
const haloMaterial = new THREE.MeshBasicMaterial({
color: 0x55c69b,
transparent: true,
opacity: 0,
depthWrite: false
});
const halo = new THREE.Mesh(
new THREE.TorusGeometry(0.49, 0.027, 6, 40),
haloMaterial
);
halo.rotation.x = Math.PI / 2;
halo.position.y = 1.19;
assembly.add(halo);
const label = sprite(
makeProfileLabel(spec.id, spec.profile),
2.22,
0.7
);
label.position.set(0, 2.05, 0);
group.add(label);
const pairSprite = sprite(
makeBadge("PROFILE PAIRED", "#319a73", true),
2.12,
0.52
);
pairSprite.position.set(0, 2.58, 0);
pairSprite.visible = false;
group.add(pairSprite);
const mismatchSprite = sprite(
makeBadge("MISMATCH", "#c95849"),
1.55,
0.52
);
mismatchSprite.position.set(0, 2.58, 0);
mismatchSprite.visible = false;
group.add(mismatchSprite);
const sealedSprite = sprite(
makeBadge("ASSEMBLY SEALED", "#647278", true),
2.2,
0.52
);
sealedSprite.position.set(0, 2.58, 0);
sealedSprite.visible = false;
group.add(sealedSprite);
const checkSprite = sprite(
makeBadge("ASSEMBLY COMPLETE", "#319a73", true),
2.25,
0.52
);
checkSprite.position.set(0, 2.58, 0);
checkSprite.visible = false;
group.add(checkSprite);
const pick = new THREE.Mesh(
new THREE.CylinderGeometry(0.72, 0.72, 0.58, 12),
new THREE.MeshBasicMaterial({
transparent: true,
opacity: 0,
depthWrite: false,
colorWrite: false
})
);
pick.position.y = 1.2;
pick.userData.stationIndex = index;
assembly.add(pick);
stationPickMeshes.push(pick);
const station = {
index,
id: spec.id,
profile: spec.profile,
group,
assembly,
anchor,
cover,
coverStart,
coverEnd,
halo,
pairSprite,
mismatchSprite,
sealedSprite,
checkSprite,
completed: false,
driving: false,
card: null
};
stations.push(station);
return station;
}
stationSpecs.forEach(createStation);
// A physical line appears only when the two profiles are visibly paired.
const pairingLineGeometry = new THREE.BufferGeometry();
pairingLineGeometry.setAttribute(
"position",
new THREE.Float32BufferAttribute([0, 0, 0, 0, 0, 0], 3)
);
const pairingLine = new THREE.Line(
pairingLineGeometry,
new THREE.LineBasicMaterial({
color: 0x55c69b,
transparent: true,
opacity: 0.95
})
);
pairingLine.visible = false;
pairingLine.renderOrder = 8;
scene.add(pairingLine);
// -------------------------------------------------------------------------
// UI and station cards
// -------------------------------------------------------------------------
const stationList = document.getElementById("station-list");
const pairReadout = document.getElementById("pair-readout");
const pairTitle = document.getElementById("pair-title");
const pairDetail = document.getElementById("pair-detail");
const completedCount = document.getElementById("completed-count");
const toast = document.getElementById("toast");
let toastTimer = 0;
function showToast(message) {
toast.textContent = message;
toast.classList.add("show");
clearTimeout(toastTimer);
toastTimer = setTimeout(() => toast.classList.remove("show"), 2300);
}
function setReadout(type, title, detail) {
pairReadout.classList.toggle("match", type === "match");
pairReadout.classList.toggle("mismatch", type === "mismatch");
pairTitle.textContent = title;
pairDetail.textContent = detail;
}
function updateCardState(station) {
if (!station.card) return;
const status = station.card.querySelector(".station-state");
station.card.classList.toggle("done", station.completed);
if (station.completed) {
status.textContent = "Assembly sealed · bit returned";
} else if (station.driving) {
status.textContent = "Profile paired · fastening";
} else {
status.textContent = "Open · choose matching profile";
}
}
function updateCompletedCount() {
const count = stations.filter((station) => station.completed).length;
completedCount.textContent = `${count} / ${stations.length}`;
}
stations.forEach((station) => {
const card = document.createElement("button");
card.className = "station-card";
card.style.setProperty("--accent", PROFILES[station.profile].css);
card.innerHTML = `
<span class="station-id">${station.id}</span>
<span class="profile-name">
<span class="profile-dot"></span>
${PROFILES[station.profile].name}
</span>
<span class="station-state">Open · choose matching profile</span>
`;
card.addEventListener("click", () => {
focusStation(station.index);
});
stationList.appendChild(card);
station.card = card;
updateCardState(station);
});
// -------------------------------------------------------------------------
// Camera controls, implemented without addons
// -------------------------------------------------------------------------
const cameraTarget = new THREE.Vector3(0, 1.0, 0);
const desiredCameraTarget = cameraTarget.clone();
let cameraYaw = 0.56;
let cameraPitch = 0.72;
let cameraRadius = 18.4;
function homeView() {
desiredCameraTarget.set(0, 1.0, 0);
cameraYaw = 0.56;
cameraPitch = 0.72;
cameraRadius = 18.4;
}
function focusStation(index) {
const station = stations[index];
desiredCameraTarget.set(
station.group.position.x,
1.0,
station.group.position.z
);
cameraRadius = Math.min(cameraRadius, 14.5);
showToast(`Focusing ${station.id} · ${PROFILES[station.profile].name}`);
}
function updateCamera(dt) {
cameraTarget.lerp(
desiredCameraTarget,
1 - Math.exp(-4.5 * dt)
);
const spherical = new THREE.Spherical(
cameraRadius,
cameraPitch,
cameraYaw
);
const offset = new THREE.Vector3().setFromSpherical(spherical);
camera.position.copy(cameraTarget).add(offset);
camera.lookAt(cameraTarget);
}
document.getElementById("home-view").addEventListener("click", homeView);
// -------------------------------------------------------------------------
// Pointer interaction and profile pairing
// -------------------------------------------------------------------------
const raycaster = new THREE.Raycaster();
const pointer = new THREE.Vector2(10, 10);
const dragPlane = new THREE.Plane(new THREE.Vector3(0, 1, 0), -1.62);
const planeHit = new THREE.Vector3();
let dragBit = null;
let hoveredStation = null;
let pointerDown = null;
let activePointerId = null;
function updatePointer(event) {
const rect = renderer.domElement.getBoundingClientRect();
pointer.x = ((event.clientX - rect.left) / rect.width) * 2 - 1;
pointer.y = -((event.clientY - rect.top) / rect.height) * 2 + 1;
}
function findBitAtPointer() {
scene.updateMatrixWorld(true);
raycaster.setFromCamera(pointer, camera);
const hits = raycaster.intersectObjects(bitRoots, true);
for (const hit of hits) {
let object = hit.object;
while (object && !object.userData.bit) {
object = object.parent;
}
if (object?.userData.bit?.state === "rack") {
return object.userData.bit;
}
}
return null;
}
function findStationAtPointer() {
scene.updateMatrixWorld(true);
raycaster.setFromCamera(pointer, camera);
const hits = raycaster.intersectObjects(stationPickMeshes, false);
if (!hits.length) return null;
return stations[hits[0].object.userData.stationIndex];
}
function clearStationIndicators() {
for (const station of stations) {
station.halo.material.opacity = 0;
station.pairSprite.visible = false;
station.mismatchSprite.visible = false;
station.sealedSprite.visible = false;
if (station.card) {
station.card.classList.remove("pairing", "wrong");
}
}
}
function setBitGlow(bit, color) {
const glow = new THREE.Color(color);
bit.collarMaterial.emissive.copy(glow);
bit.tipMaterial.emissive.copy(glow);
bit.collarMaterial.emissiveIntensity = color ? 0.72 : 0;
bit.tipMaterial.emissiveIntensity = color ? 0.42 : 0;
}
function updateDragTarget() {
if (!dragBit) return;
hoveredStation = findStationAtPointer();
if (hoveredStation) {
const anchor = new THREE.Vector3();
hoveredStation.anchor.getWorldPosition(anchor);
if (hoveredStation.completed) {
dragBit.dragTarget.copy(anchor).add(new THREE.Vector3(0, 0.48, 0));
setBitGlow(dragBit, 0x76858b);
hoveredStation.sealedSprite.visible = true;
hoveredStation.halo.material.color.setHex(0x8c9a9f);
hoveredStation.halo.material.opacity = 0.65;
setReadout(
"neutral",
"Assembly already sealed",
`${hoveredStation.id} is complete. Choose another open station.`
);
} else if (dragBit.profile === hoveredStation.profile) {
dragBit.dragTarget.copy(anchor).add(new THREE.Vector3(0, 0.19, 0));
setBitGlow(dragBit, 0x55c69b);
hoveredStation.halo.material.color.setHex(0x55c69b);
hoveredStation.halo.material.opacity = 1;
hoveredStation.pairSprite.visible = true;
hoveredStation.card?.classList.add("pairing");
pairingLine.visible = true;
pairingLine.material.color.setHex(0x55c69b);
setReadout(
"match",
"Profile paired",
`${PROFILES[dragBit.profile].name} tip ↔ ${PROFILES[hoveredStation.profile].name} screw. Release to fasten.`
);
} else {
dragBit.dragTarget.copy(anchor).add(new THREE.Vector3(0, 0.5, 0));
setBitGlow(dragBit, 0xff806d);
hoveredStation.halo.material.color.setHex(0xff806d);
hoveredStation.halo.material.opacity = 0.75;
hoveredStation.mismatchSprite.visible = true;
hoveredStation.card?.classList.add("wrong");
setReadout(
"mismatch",
"Profile mismatch",
`${PROFILES[dragBit.profile].name} tip ↔ ${PROFILES[hoveredStation.profile].name} screw. Return this bit and choose another.`
);
}
} else {
raycaster.setFromCamera(pointer, camera);
if (raycaster.ray.intersectPlane(dragPlane, planeHit)) {
dragBit.dragTarget.copy(planeHit);
}
setBitGlow(dragBit, 0x000000);
pairingLine.visible = false;
setReadout(
"neutral",
`${PROFILES[dragBit.profile].name} bit in hand`,
"Move over a screwhead. A green ring means the profiles visibly match."
);
}
}
renderer.domElement.addEventListener("pointerdown", (event) => {
updatePointer(event);
activePointerId = event.pointerId;
const bit = findBitAtPointer();
if (bit) {
dragBit = bit;
bit.state = "drag";
bit.root.scale.setScalar(1.08);
bit.dragTarget.copy(bit.root.position);
renderer.domElement.setPointerCapture(event.pointerId);
renderer.domElement.style.cursor = "grabbing";
updateDragTarget();
event.preventDefault();
return;
}
pointerDown = {
x: event.clientX,
y: event.clientY,
button: event.button
};
renderer.domElement.setPointerCapture(event.pointerId);
renderer.domElement.style.cursor = "grabbing";
event.preventDefault();
});
renderer.domElement.addEventListener("pointermove", (event) => {
updatePointer(event);
if (dragBit) {
updateDragTarget();
return;
}
if (pointerDown && event.pointerId === activePointerId) {
const dx = event.clientX - pointerDown.x;
const dy = event.clientY - pointerDown.y;
if (pointerDown.button === 2) {
const forward = new THREE.Vector3()
.subVectors(cameraTarget, camera.position);
forward.y = 0;
forward.normalize();
const right = new THREE.Vector3()
.crossVectors(forward, new THREE.Vector3(0, 1, 0))
.normalize();
desiredCameraTarget.addScaledVector(right, -dx * 0.012);
desiredCameraTarget.addScaledVector(forward, dy * 0.012);
} else {
cameraYaw -= dx * 0.006;
cameraPitch = THREE.MathUtils.clamp(
cameraPitch + dy * 0.005,
0.34,
1.13
);
}
pointerDown.x = event.clientX;
pointerDown.y = event.clientY;
} else {
const hoveringBit = findBitAtPointer();
renderer.domElement.style.cursor = hoveringBit ? "grab" : "default";
}
});
function returnBitToRack(bit) {
bit.state = "return";
bit.animationTime = 0;
bit.animationStart.copy(bit.root.position);
bit.station = null;
}
function finishDrag() {
if (!dragBit) return;
const bit = dragBit;
const station = hoveredStation;
dragBit = null;
clearStationIndicators();
setBitGlow(bit, 0x000000);
pairingLine.visible = false;
renderer.domElement.style.cursor = "default";
if (
station &&
!station.completed &&
bit.profile === station.profile
) {
startFastening(bit, station);
} else {
returnBitToRack(bit);
}
}
renderer.domElement.addEventListener("pointerup", (event) => {
if (event.pointerId === activePointerId) {
if (dragBit) finishDrag();
pointerDown = null;
activePointerId = null;
if (renderer.domElement.hasPointerCapture(event.pointerId)) {
renderer.domElement.releasePointerCapture(event.pointerId);
}
}
});
renderer.domElement.addEventListener("pointercancel", () => {
if (dragBit) {
const bit = dragBit;
dragBit = null;
clearStationIndicators();
setBitGlow(bit, 0x000000);
pairingLine.visible = false;
returnBitToRack(bit);
}
pointerDown = null;
activePointerId = null;
});
renderer.domElement.addEventListener(
"wheel",
(event) => {
event.preventDefault();
cameraRadius = THREE.MathUtils.clamp(
cameraRadius * Math.exp(event.deltaY * 0.001),
10.5,
25
);
},
{ passive: false }
);
renderer.domElement.addEventListener("contextmenu", (event) => {
event.preventDefault();
});
// -------------------------------------------------------------------------
// Fastening animation and assembly completion
// -------------------------------------------------------------------------
const easeOutCubic = (t) => 1 - Math.pow(1 - t, 3);
const easeInOutCubic = (t) =>
t < 0.5
? 4 * t * t * t
: 1 - Math.pow(-2 * t + 2, 3) / 2;
const confetti = [];
const confettiGeometry = new THREE.TetrahedronGeometry(0.075, 0);
const confettiMaterials = Object.fromEntries(
Object.entries(PROFILES).map(([key, value]) => [
key,
material(value.color, { roughness: 0.7 })
])
);
function emitConfetti(worldPosition, profile) {
for (let i = 0; i < 15; i++) {
const mesh = new THREE.Mesh(
confettiGeometry,
confettiMaterials[profile]
);
mesh.position.copy(worldPosition);
mesh.position.x += (Math.random() - 0.5) * 0.5;
mesh.position.z += (Math.random() - 0.5) * 0.5;
mesh.castShadow = true;
scene.add(mesh);
confetti.push({
mesh,
velocity: new THREE.Vector3(
(Math.random() - 0.5) * 2.8,
1.7 + Math.random() * 2.1,
(Math.random() - 0.5) * 2.8
),
spin: new THREE.Vector3(
Math.random() * 7,
Math.random() * 7,
Math.random() * 7
),
life: 1.15 + Math.random() * 0.4
});
}
}
function startFastening(bit, station) {
bit.state = "drive";
bit.animationTime = 0;
bit.animationStart.copy(bit.root.position);
bit.station = station;
station.driving = true;
station.pairSprite.visible = true;
station.halo.material.color.setHex(0x55c69b);
station.halo.material.opacity = 1;
pairingLine.visible = true;
setReadout(
"match",
"Profiles visibly paired",
`The ${PROFILES[bit.profile].name} tip is aligned with ${station.id}. Fastening the assembly.`
);
showToast(`${station.id} · ${PROFILES[bit.profile].name} profiles paired`);
updateCardState(station);
}
function updateBit(bit, dt) {
if (bit.state === "drag") {
bit.root.position.lerp(
bit.dragTarget,
1 - Math.exp(-18 * dt)
);
bit.root.scale.lerp(
new THREE.Vector3(1.08, 1.08, 1.08),
1 - Math.exp(-14 * dt)
);
} else if (bit.state === "return") {
bit.animationTime += dt;
const t = Math.min(bit.animationTime / 0.55, 1);
const eased = easeInOutCubic(t);
bit.root.position.lerpVectors(
bit.animationStart,
bit.home,
eased
);
bit.root.position.y += Math.sin(t * Math.PI) * 0.7;
bit.root.scale.lerp(
new THREE.Vector3(1, 1, 1),
1 - Math.exp(-12 * dt)
);
if (t >= 1) {
bit.state = "rack";
bit.root.position.copy(bit.home);
bit.root.scale.setScalar(1);
}
} else if (bit.state === "drive") {
const station = bit.station;
if (!station) {
returnBitToRack(bit);
return;
}
bit.animationTime += dt;
const t = bit.animationTime;
const anchor = new THREE.Vector3();
station.anchor.getWorldPosition(anchor);
const hoverPosition = anchor.clone().add(new THREE.Vector3(0, 0.19, 0));
const seatPosition = anchor.clone().add(new THREE.Vector3(0, 0.018, 0));
if (t < 0.22) {
const p = easeOutCubic(t / 0.22);
bit.root.position.lerpVectors(
bit.animationStart,
hoverPosition,
p
);
} else if (t < 0.95) {
const p = easeInOutCubic((t - 0.22) / 0.73);
bit.root.position.lerpVectors(
hoverPosition,
seatPosition,
p
);
bit.root.rotation.y += dt * 16;
} else if (t < 1.18) {
bit.root.position.copy(seatPosition);
bit.root.rotation.y += dt * 7;
} else {
const p = easeInOutCubic(Math.min((t - 1.18) / 0.72, 1));
bit.root.position.lerpVectors(
seatPosition,
bit.home,
p
);
bit.root.position.y += Math.sin(p * Math.PI) * 0.85;
bit.root.rotation.y += dt * 4;
}
const coverProgress = THREE.MathUtils.clamp(
(t - 0.38) / 0.68,
0,
1
);
station.cover.position.lerpVectors(
station.coverStart,
station.coverEnd,
easeOutCubic(coverProgress)
);
if (t >= 1.12 && !station.completed) {
station.completed = true;
station.driving = false;
station.cover.position.copy(station.coverEnd);
station.pairSprite.visible = false;
station.checkSprite.visible = true;
station.halo.material.color.setHex(0x55c69b);
station.halo.material.opacity = 0.48;
const worldPosition = new THREE.Vector3();
station.anchor.getWorldPosition(worldPosition);
worldPosition.y += 0.25;
emitConfetti(worldPosition, station.profile);
updateCardState(station);
updateCompletedCount();
showToast(`${station.id} complete · shared bit returned to rack`);
}
if (t >= 1.9) {
bit.state = "rack";
bit.root.position.copy(bit.home);
bit.root.rotation.set(0, 0, 0);
bit.station = null;
pairingLine.visible = false;
}
}
}
function updatePairingLine() {
if (!pairingLine.visible) return;
const bit = dragBit?.state === "drag"
? dragBit
: bits.find((candidate) => candidate.state === "drive");
if (!bit) {
pairingLine.visible = false;
return;
}
const start = bit.root.position.clone();
start.y += 0.08;
const end = new THREE.Vector3();
const station = dragBit
? hoveredStation
: bits.find((candidate) => candidate.state === "drive")?.station;
if (!station) {
pairingLine.visible = false;
return;
}
station.anchor.getWorldPosition(end);
const position = pairingLine.geometry.attributes.position;
position.setXYZ(0, start.x, start.y, start.z);
position.setXYZ(1, end.x, end.y, end.z);
position.needsUpdate = true;
pairingLine.geometry.computeBoundingSphere();
}
// -------------------------------------------------------------------------
// Reset
// -------------------------------------------------------------------------
function resetWorkshop() {
dragBit = null;
hoveredStation = null;
pointerDown = null;
clearStationIndicators();
pairingLine.visible = false;
for (const station of stations) {
station.completed = false;
station.driving = false;
station.cover.position.copy(station.coverStart);
station.checkSprite.visible = false;
station.pairSprite.visible = false;
station.mismatchSprite.visible = false;
station.sealedSprite.visible = false;
updateCardState(station);
}
for (const bit of bits) {
bit.state = "rack";
bit.station = null;
bit.root.position.copy(bit.home);
bit.root.rotation.set(0, 0, 0);
bit.root.scale.setScalar(1);
setBitGlow(bit, 0x000000);
}
for (const item of confetti) {
scene.remove(item.mesh);
}
confetti.length = 0;
updateCompletedCount();
setReadout(
"neutral",
"Shared rack ready",
"Drag a driver bit from the center rack onto a screwhead."
);
showToast("Workshop reset · all shared bits returned");
}
document.getElementById("reset-tools").addEventListener(
"click",
resetWorkshop
);
// -------------------------------------------------------------------------
// Animation loop
// -------------------------------------------------------------------------
const clock = new THREE.Clock();
function updateConfetti(dt) {
for (let i = confetti.length - 1; i >= 0; i--) {
const item = confetti[i];
item.velocity.y -= 4.1 * dt;
item.mesh.position.addScaledVector(item.velocity, dt);
item.mesh.rotation.x += item.spin.x * dt;
item.mesh.rotation.y += item.spin.y * dt;
item.mesh.rotation.z += item.spin.z * dt;
item.life -= dt;
if (item.life <= 0) {
scene.remove(item.mesh);
confetti.splice(i, 1);
}
}
}
function animate() {
const dt = Math.min(clock.getDelta(), 0.05);
updateCamera(dt);
for (const bit of bits) {
updateBit(bit, dt);
}
updatePairingLine();
updateConfetti(dt);
renderer.render(scene, camera);
}
renderer.setAnimationLoop(animate);
addEventListener("resize", () => {
camera.aspect = innerWidth / innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(innerWidth, innerHeight);
renderer.setPixelRatio(Math.min(devicePixelRatio, 2));
});
updateCompletedCount();
homeView();
updateCamera(1);
</script>
</body>
</html>
```