Prompt specification
Build or customize the Three.js 3D scene "Furniture leg repair station" 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: Furniture leg repair station
Category: Everyday & Industrial · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/furniture-leg-repair-station-x0319
Scene Description Prompt:
A stylized workbench where a loose table leg is repaired using bolts and washers in order. Three.js from cdnjs, one HTML file.
Working Three.js Starter (furniture-leg-repair-station-x0319):
```html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Leg & Bolt — Workbench Repair</title>
<style>
:root {
--ink: #20312f;
--muted: #667a76;
--paper: rgba(255, 252, 244, 0.93);
--line: rgba(32, 49, 47, 0.13);
--teal: #26a99a;
--amber: #e69a4e;
--red: #d85e52;
--shadow: 0 18px 55px rgba(40, 52, 47, 0.18);
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #dce3dd;
color: var(--ink);
font-family: Inter, ui-sans-serif, system-ui, -apple-system,
BlinkMacSystemFont, "Segoe UI", sans-serif;
}
#app,
#scene {
position: fixed;
inset: 0;
}
canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
outline: none;
}
.hud {
position: fixed;
inset: 0;
pointer-events: none;
z-index: 5;
}
.brand {
position: absolute;
top: 24px;
left: 28px;
display: flex;
align-items: center;
gap: 13px;
}
.brand-mark {
width: 43px;
height: 43px;
display: grid;
place-items: center;
border-radius: 13px;
color: white;
background: var(--ink);
box-shadow: 0 8px 22px rgba(25, 43, 40, 0.2);
font-weight: 900;
letter-spacing: -1px;
}
.eyebrow {
margin: 0 0 3px;
color: var(--muted);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.19em;
text-transform: uppercase;
}
h1 {
margin: 0;
font-size: clamp(21px, 2.4vw, 31px);
line-height: 1;
letter-spacing: -0.045em;
}
.panel {
position: absolute;
top: 24px;
right: 24px;
width: min(340px, calc(100vw - 48px));
padding: 21px;
overflow: hidden;
border: 1px solid rgba(255, 255, 255, 0.8);
border-radius: 24px;
background: var(--paper);
box-shadow: var(--shadow);
backdrop-filter: blur(16px);
pointer-events: auto;
}
.panel::after {
content: "";
position: absolute;
top: 0;
right: 0;
width: 90px;
height: 90px;
border-radius: 0 0 0 90px;
background: rgba(38, 169, 154, 0.08);
pointer-events: none;
}
.panel-head {
display: flex;
align-items: flex-start;
justify-content: space-between;
gap: 14px;
margin-bottom: 17px;
}
.panel h2 {
margin: 0 0 5px;
font-size: 17px;
letter-spacing: -0.025em;
}
.panel-copy {
margin: 0;
color: var(--muted);
font-size: 12px;
line-height: 1.5;
}
.counter {
flex: 0 0 auto;
min-width: 48px;
padding: 7px 9px;
border-radius: 12px;
background: #eaf2ed;
color: var(--teal);
font-size: 12px;
font-weight: 850;
text-align: center;
}
.progress {
height: 5px;
margin-bottom: 17px;
overflow: hidden;
border-radius: 10px;
background: #e4e8e2;
}
.progress > span {
display: block;
width: 0;
height: 100%;
border-radius: inherit;
background: linear-gradient(90deg, var(--teal), #57c5a7);
transition: width 450ms cubic-bezier(.2, .8, .2, 1);
}
.steps {
display: grid;
gap: 8px;
margin: 0 0 18px;
padding: 0;
list-style: none;
}
.step {
display: flex;
align-items: center;
gap: 11px;
min-height: 54px;
padding: 8px 10px;
border: 1px solid transparent;
border-radius: 15px;
transition:
background 200ms ease,
border-color 200ms ease,
transform 200ms ease;
}
.step-number {
flex: 0 0 auto;
width: 29px;
height: 29px;
display: grid;
place-items: center;
border: 1px solid #d7ddd7;
border-radius: 10px;
background: #f3f2eb;
color: #81908c;
font-size: 11px;
font-weight: 850;
transition: all 220ms ease;
}
.step strong {
display: block;
margin-bottom: 2px;
font-size: 12px;
}
.step small {
display: block;
color: var(--muted);
font-size: 10px;
line-height: 1.25;
}
.step.active {
border-color: rgba(38, 169, 154, 0.3);
background: rgba(38, 169, 154, 0.09);
transform: translateX(-3px);
}
.step.active .step-number {
border-color: var(--teal);
background: var(--teal);
color: white;
box-shadow: 0 6px 13px rgba(38, 169, 154, 0.23);
}
.step.done {
opacity: 0.65;
}
.step.done .step-number {
border-color: rgba(38, 169, 154, 0.22);
background: #dff1e9;
color: var(--teal);
}
.actions {
display: grid;
grid-template-columns: 1fr auto;
gap: 9px;
}
button {
min-height: 42px;
border: 0;
border-radius: 13px;
padding: 0 15px;
color: white;
background: var(--ink);
font: inherit;
font-size: 12px;
font-weight: 800;
cursor: pointer;
transition:
transform 160ms ease,
box-shadow 160ms ease,
background 160ms ease;
}
button:hover {
transform: translateY(-2px);
box-shadow: 0 9px 18px rgba(32, 49, 47, 0.18);
}
button:active {
transform: translateY(0);
}
button.secondary {
width: 42px;
padding: 0;
color: var(--ink);
background: #eceee8;
font-size: 19px;
}
button:focus-visible {
outline: 3px solid rgba(38, 169, 154, 0.35);
outline-offset: 2px;
}
.success {
display: none;
margin-top: 14px;
padding: 12px;
border-radius: 14px;
background: #dff3e7;
color: #276849;
font-size: 11px;
font-weight: 700;
line-height: 1.45;
}
.success.show {
display: block;
animation: pop 450ms cubic-bezier(.2, .9, .2, 1);
}
@keyframes pop {
from {
opacity: 0;
transform: scale(0.94);
}
to {
opacity: 1;
transform: scale(1);
}
}
.hint {
position: absolute;
left: 28px;
bottom: 24px;
display: flex;
align-items: center;
gap: 10px;
padding: 10px 13px;
border: 1px solid rgba(255, 255, 255, 0.72);
border-radius: 13px;
background: rgba(255, 252, 244, 0.78);
box-shadow: 0 8px 24px rgba(40, 52, 47, 0.1);
color: var(--muted);
font-size: 10px;
font-weight: 700;
backdrop-filter: blur(10px);
}
.hint-dot {
width: 7px;
height: 7px;
border-radius: 50%;
background: var(--teal);
box-shadow: 0 0 0 5px rgba(38, 169, 154, 0.12);
}
.toast {
position: absolute;
left: 50%;
bottom: 25px;
max-width: min(460px, calc(100vw - 32px));
padding: 12px 17px;
border-radius: 14px;
background: rgba(32, 49, 47, 0.92);
box-shadow: 0 12px 30px rgba(25, 40, 38, 0.24);
color: white;
font-size: 12px;
font-weight: 750;
text-align: center;
transform: translate(-50%, 90px);
opacity: 0;
transition:
transform 300ms cubic-bezier(.2, .8, .2, 1),
opacity 200ms ease;
}
.toast.show {
transform: translate(-50%, 0);
opacity: 1;
}
.toast.error {
background: rgba(185, 69, 61, 0.94);
}
.part-label {
position: absolute;
z-index: 7;
padding: 7px 10px;
border: 1px solid rgba(255, 255, 255, 0.7);
border-radius: 10px;
background: rgba(32, 49, 47, 0.9);
box-shadow: 0 8px 18px rgba(31, 45, 42, 0.2);
color: white;
font-size: 10px;
font-weight: 800;
white-space: nowrap;
pointer-events: none;
transform: translate(-50%, -100%);
opacity: 0;
transition: opacity 150ms ease;
}
.part-label.show {
opacity: 1;
}
.part-label::after {
content: "";
position: absolute;
left: 50%;
top: 100%;
width: 1px;
height: 17px;
background: rgba(32, 49, 47, 0.55);
}
@media (max-width: 720px) {
.brand {
top: 15px;
left: 15px;
}
.brand-mark {
width: 37px;
height: 37px;
border-radius: 11px;
}
.panel {
top: auto;
right: 12px;
bottom: 12px;
width: calc(100vw - 24px);
padding: 15px;
border-radius: 20px;
}
.panel-head {
margin-bottom: 10px;
}
.panel-copy,
.step small {
display: none;
}
.steps {
grid-template-columns: repeat(4, 1fr);
gap: 5px;
margin-bottom: 11px;
}
.step {
min-height: 42px;
padding: 5px;
}
.step-number {
width: 25px;
height: 25px;
}
.step strong {
display: none;
}
.hint {
display: none;
}
.toast {
bottom: 265px;
}
}
</style>
</head>
<body>
<div id="app">
<div id="scene"></div>
<div class="hud">
<header class="brand">
<div class="brand-mark">L&B</div>
<div>
<p class="eyebrow">Bench No. 04</p>
<h1>Repair the wobble</h1>
</div>
</header>
<section class="panel" aria-label="Repair instructions">
<div class="panel-head">
<div>
<p class="eyebrow">Assembly order</p>
<h2 id="panelTitle">Secure the loose leg</h2>
<p class="panel-copy" id="panelCopy">
Click the glowing hardware in the listed order.
</p>
</div>
<div class="counter" id="counter">0 / 4</div>
</div>
<div class="progress" aria-hidden="true">
<span id="progressFill"></span>
</div>
<ol class="steps" id="steps">
<li class="step active" data-step="0">
<span class="step-number">1</span>
<div>
<strong>Lower washer</strong>
<small>Seat it against the bracket.</small>
</div>
</li>
<li class="step" data-step="1">
<span class="step-number">2</span>
<div>
<strong>Lower bolt</strong>
<small>Align the leg and start the bolt.</small>
</div>
</li>
<li class="step" data-step="2">
<span class="step-number">3</span>
<div>
<strong>Upper washer</strong>
<small>Add the second spacer.</small>
</div>
</li>
<li class="step" data-step="3">
<span class="step-number">4</span>
<div>
<strong>Upper bolt</strong>
<small>Tighten and stop the wobble.</small>
</div>
</li>
</ol>
<div class="actions">
<button id="autoButton" type="button">Guided repair</button>
<button
id="resetButton"
class="secondary"
type="button"
title="Reset repair"
aria-label="Reset repair"
>↻</button>
</div>
<div id="success" class="success">
Repair complete. Both washers are seated and both bolts are secure.
The table leg no longer wobbles.
</div>
</section>
<div class="hint">
<span class="hint-dot"></span>
Drag to orbit · Scroll to zoom · Click highlighted hardware
</div>
<div id="toast" class="toast" role="status" aria-live="polite"></div>
</div>
<div id="partLabel" class="part-label"></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 and renderer
// -------------------------------------------------------------------------
const sceneHost = document.querySelector("#scene");
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xdce3dd);
scene.fog = new THREE.Fog(0xdce3dd, 13, 24);
const camera = new THREE.PerspectiveCamera(
42,
window.innerWidth / window.innerHeight,
0.1,
60
);
const renderer = new THREE.WebGLRenderer({
antialias: true,
alpha: false,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.05;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
sceneHost.appendChild(renderer.domElement);
const hemisphere = new THREE.HemisphereLight(0xf9fff8, 0x776752, 2.1);
scene.add(hemisphere);
const sun = new THREE.DirectionalLight(0xfff1d9, 3.4);
sun.position.set(-4.5, 8.5, 6.5);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -7;
sun.shadow.camera.right = 7;
sun.shadow.camera.top = 8;
sun.shadow.camera.bottom = -6;
sun.shadow.camera.near = 0.5;
sun.shadow.camera.far = 22;
sun.shadow.normalBias = 0.025;
scene.add(sun);
const rimLight = new THREE.PointLight(0x9de9dc, 16, 9, 2);
rimLight.position.set(4.5, 4.8, -1.5);
scene.add(rimLight);
// -------------------------------------------------------------------------
// Materials and modeling helpers
// -------------------------------------------------------------------------
const material = (color, roughness = 0.72, metalness = 0) =>
new THREE.MeshStandardMaterial({ color, roughness, metalness });
const mats = {
wall: material(0xcfd8d0, 0.96),
floor: material(0xb9c1b8, 0.95),
benchTop: material(0xa96839, 0.78),
benchEdge: material(0x7f482d, 0.82),
benchLeg: material(0x365b59, 0.68),
tableTop: material(0xc98b51, 0.72),
tableSide: material(0xa95f33, 0.78),
leg: material(0xb36d3c, 0.76),
legDark: material(0x814a2d, 0.8),
bracket: material(0x345a59, 0.45, 0.38),
steel: material(0xbcc8c6, 0.3, 0.72),
darkSteel: material(0x40504f, 0.46, 0.55),
foam: material(0x263b3a, 0.95),
rubber: material(0x1e2928, 0.92),
brass: material(0xb88743, 0.35, 0.62),
paper: material(0xf3ead7, 0.92),
teal: material(0x26a99a, 0.46),
amber: material(0xe69a4e, 0.5)
};
function addBox(
size,
position,
mat,
parent = scene,
edgeColor = null,
castShadow = true
) {
const mesh = new THREE.Mesh(new THREE.BoxGeometry(...size), mat);
mesh.position.set(...position);
mesh.castShadow = castShadow;
mesh.receiveShadow = true;
parent.add(mesh);
if (edgeColor) {
const edge = new THREE.LineSegments(
new THREE.EdgesGeometry(mesh.geometry),
new THREE.LineBasicMaterial({
color: edgeColor,
transparent: true,
opacity: 0.26
})
);
mesh.add(edge);
}
return mesh;
}
function addCylinder(
radiusTop,
radiusBottom,
height,
position,
mat,
parent = scene,
segments = 24
) {
const mesh = new THREE.Mesh(
new THREE.CylinderGeometry(radiusTop, radiusBottom, height, segments),
mat
);
mesh.position.set(...position);
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
function addTorus(
radius,
tube,
position,
mat,
parent = scene,
rotation = [0, 0, 0]
) {
const mesh = new THREE.Mesh(
new THREE.TorusGeometry(radius, tube, 10, 36),
mat
);
mesh.position.set(...position);
mesh.rotation.set(...rotation);
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
// -------------------------------------------------------------------------
// Room and workbench
// -------------------------------------------------------------------------
const floor = new THREE.Mesh(new THREE.PlaneGeometry(30, 30), mats.floor);
floor.rotation.x = -Math.PI / 2;
floor.position.y = -0.025;
floor.receiveShadow = true;
scene.add(floor);
addBox([15, 7, 0.22], [0, 3.45, -4.1], mats.wall);
// Subtle wall panels.
for (let x = -6; x <= 6; x += 2) {
addBox([0.025, 6.5, 0.02], [x, 3.35, -3.975], mats.benchEdge, scene, null, false);
}
// Pegboard and tools.
addBox([6.8, 2.2, 0.13], [-0.3, 4.45, -3.91], material(0xb28b62, 0.9));
for (let x = -3.25; x <= 2.75; x += 0.28) {
for (let y = 3.55; y <= 5.35; y += 0.28) {
const hole = new THREE.Mesh(
new THREE.CircleGeometry(0.025, 8),
new THREE.MeshBasicMaterial({ color: 0x745a42 })
);
hole.position.set(x, y, -3.835);
scene.add(hole);
}
}
// Stylized hammer.
const hammer = new THREE.Group();
hammer.position.set(-2.35, 4.25, -3.7);
addBox([0.14, 1.0, 0.13], [0, 0, 0], mats.benchLeg, hammer);
addBox([0.72, 0.22, 0.18], [0, 0.52, 0], mats.darkSteel, hammer);
scene.add(hammer);
// Stylized wrench.
const wrench = new THREE.Group();
wrench.position.set(-1.35, 4.48, -3.69);
wrench.rotation.z = -0.22;
addBox([0.12, 1.15, 0.11], [0, 0, 0], mats.steel, wrench);
addTorus(0.2, 0.065, [0, 0.66, 0], mats.steel, wrench, [Math.PI / 2, 0, 0]);
addTorus(0.14, 0.055, [0, -0.65, 0], mats.steel, wrench, [Math.PI / 2, 0, 0]);
scene.add(wrench);
// Bench.
const bench = new THREE.Group();
scene.add(bench);
addBox([6.6, 0.35, 4.1], [0, 1.45, 0], mats.benchTop, bench, 0x633b29);
addBox([6.35, 0.19, 3.9], [0, 1.22, 0], mats.benchEdge, bench);
for (const x of [-2.8, 2.8]) {
for (const z of [-1.48, 1.48]) {
addBox([0.42, 1.4, 0.42], [x, 0.68, z], mats.benchLeg, bench, 0x1f3d3b);
}
}
addBox([5.25, 0.45, 0.16], [0, 0.73, 1.64], mats.benchLeg, bench);
addBox([5.25, 0.45, 0.16], [0, 0.73, -1.64], mats.benchLeg, bench);
addBox([0.16, 0.45, 2.8], [-2.67, 0.73, 0], mats.benchLeg, bench);
addBox([0.16, 0.45, 2.8], [2.67, 0.73, 0], mats.benchLeg, bench);
// Wood plank seams on the bench.
for (const z of [-1.15, -0.35, 0.45, 1.25]) {
addBox([6.2, 0.008, 0.012], [0, 1.629, z], mats.benchEdge, scene, null, false);
}
// Lower shelf.
addBox([5.45, 0.16, 2.8], [0, 0.45, 0], mats.benchEdge, bench);
// Small vise on the left.
const vise = new THREE.Group();
vise.position.set(-2.45, 1.72, -0.12);
addBox([0.72, 0.16, 0.62], [0, 0, 0], mats.darkSteel, vise);
addBox([0.16, 0.65, 0.48], [-0.18, 0.31, 0], mats.bracket, vise);
addBox([0.16, 0.65, 0.48], [0.18, 0.31, 0], mats.bracket, vise);
addCylinder(0.07, 0.07, 0.9, [0, 0.27, 0], mats.steel, vise).rotation.z = Math.PI / 2;
addCylinder(0.035, 0.035, 0.65, [0.57, 0.27, 0], mats.steel, vise).rotation.z = Math.PI / 2;
scene.add(vise);
// Toolbox on the rear shelf.
addBox([1.15, 0.55, 0.62], [2.45, 0.83, -0.85], mats.bracket, scene, 0x1e4140);
addBox([1.2, 0.09, 0.65], [2.45, 1.14, -0.85], mats.darkSteel, scene);
addBox([0.24, 0.08, 0.08], [2.45, 1.25, -0.85], mats.steel, scene);
// Mug.
addCylinder(0.24, 0.21, 0.48, [2.67, 1.87, -1.38], mats.paper);
addTorus(0.16, 0.045, [2.91, 1.88, -1.38], mats.paper, scene, [Math.PI / 2, 0, 0]);
addCylinder(0.205, 0.205, 0.015, [2.67, 2.115, -1.38], material(0x49352c, 0.9));
// -------------------------------------------------------------------------
// Table under repair
// -------------------------------------------------------------------------
const table = new THREE.Group();
scene.add(table);
addBox([3.45, 0.3, 2.35], [0, 3.82, 0], mats.tableTop, table, 0x764329);
addBox([3.35, 0.18, 2.25], [0, 3.59, 0], mats.tableSide, table);
// Subtle top grain.
for (let z = -0.8; z <= 0.81; z += 0.4) {
addBox([3.15, 0.008, 0.014], [0, 3.975, z], mats.benchEdge, table, null, false);
}
function createLeg(pivotPosition) {
const pivot = new THREE.Group();
pivot.position.copy(pivotPosition);
addBox([0.43, 1.9, 0.43], [0, -0.95, 0], mats.leg, pivot, 0x6f4028);
addBox([0.47, 0.12, 0.47], [0, -1.83, 0], mats.rubber, pivot);
addBox([0.47, 0.13, 0.47], [0, -0.12, 0], mats.legDark, pivot);
table.add(pivot);
return pivot;
}
createLeg(new THREE.Vector3(-1.38, 3.67, 0.95));
createLeg(new THREE.Vector3(1.38, 3.67, -0.95));
createLeg(new THREE.Vector3(-1.38, 3.67, -0.95));
const looseLegStartPosition = new THREE.Vector3(1.62, 3.72, 1.13);
const looseLegStartRotation = new THREE.Euler(0.11, 0.02, -0.23);
const looseLegTargetPosition = new THREE.Vector3(1.38, 3.67, 0.95);
const looseLegTargetRotation = new THREE.Euler(0, 0, 0);
const looseLeg = createLeg(looseLegStartPosition);
looseLeg.rotation.copy(looseLegStartRotation);
// Fixed mounting bracket on the outside of the table.
addBox([0.12, 0.79, 0.58], [1.64, 3.31, 0.95], mats.bracket, table, 0x183c3a);
const lowerMountY = 3.04;
const upperMountY = 3.47;
function addMountMarker(y, color) {
const dark = new THREE.Mesh(
new THREE.CylinderGeometry(0.105, 0.105, 0.018, 24),
mats.rubber
);
dark.position.set(1.707, y, 0.95);
dark.rotation.z = Math.PI / 2;
table.add(dark);
const markerMat = new THREE.MeshBasicMaterial({
color,
transparent: true,
opacity: 0.9
});
const marker = addTorus(
0.145,
0.018,
[1.721, y, 0.95],
markerMat,
table,
[0, 0, -Math.PI / 2]
);
marker.castShadow = false;
marker.receiveShadow = false;
}
addMountMarker(lowerMountY, 0x26a99a);
addMountMarker(upperMountY, 0xe69a4e);
// -------------------------------------------------------------------------
// Hardware tray and numbered badges
// -------------------------------------------------------------------------
const tray = new THREE.Group();
scene.add(tray);
addBox([2.95, 0.08, 0.88], [0, 1.665, 1.42], mats.foam, tray, 0x172b2a);
const slotPositions = [
[-1.05, 1.77, 1.39],
[-0.35, 1.78, 1.39],
[0.35, 1.77, 1.39],
[1.05, 1.78, 1.39]
];
slotPositions.forEach(([x, y, z]) => {
addBox(
[0.57, 0.025, 0.43],
[x, 1.714, z],
material(0x1a2d2c, 0.98),
tray,
null,
false
);
});
function createBadgeTexture(number, color) {
const canvas = document.createElement("canvas");
canvas.width = 128;
canvas.height = 128;
const ctx = canvas.getContext("2d");
ctx.clearRect(0, 0, 128, 128);
ctx.shadowColor = "rgba(0,0,0,.25)";
ctx.shadowBlur = 8;
ctx.shadowOffsetY = 4;
ctx.fillStyle = color;
ctx.beginPath();
ctx.arc(64, 60, 43, 0, Math.PI * 2);
ctx.fill();
ctx.shadowColor = "transparent";
ctx.fillStyle = "#ffffff";
ctx.font = "800 53px system-ui";
ctx.textAlign = "center";
ctx.textBaseline = "middle";
ctx.fillText(String(number), 64, 63);
const texture = new THREE.CanvasTexture(canvas);
texture.colorSpace = THREE.SRGBColorSpace;
return texture;
}
function createBadge(number, position, color) {
const badge = new THREE.Mesh(
new THREE.PlaneGeometry(0.27, 0.27),
new THREE.MeshBasicMaterial({
map: createBadgeTexture(number, color),
transparent: true,
depthWrite: false
})
);
badge.rotation.x = -Math.PI / 2;
badge.position.set(position[0], 1.716, position[1] + 0.27);
badge.userData.ignorePick = true;
scene.add(badge);
return badge;
}
// -------------------------------------------------------------------------
// Interactive parts
// -------------------------------------------------------------------------
const parts = {};
const steps = [
{ id: "lower-washer", label: "Lower washer", color: 0x26a99a },
{ id: "lower-bolt", label: "Lower bolt", color: 0x26a99a },
{ id: "upper-washer", label: "Upper washer", color: 0xe69a4e },
{ id: "upper-bolt", label: "Upper bolt", color: 0xe69a4e }
];
const sideQuaternion = new THREE.Quaternion().setFromEuler(
new THREE.Euler(0, 0, -Math.PI / 2)
);
function registerPart(id, group, startPosition, targetPosition, targetQuaternion, badge) {
group.traverse((child) => {
if (!child.isMesh) return;
child.userData.partId = id;
if (child.material && child.material.emissive) {
child.material = child.material.clone();
child.material.emissive = new THREE.Color(0x000000);
child.material.emissiveIntensity = 0;
}
});
parts[id] = {
id,
group,
startPosition: startPosition.clone(),
startQuaternion: group.quaternion.clone(),
targetPosition: targetPosition.clone(),
targetQuaternion: targetQuaternion.clone(),
badge,
installed: false,
animation: null
};
scene.add(group);
}
function createWasher(id, start, target, badge) {
const group = new THREE.Group();
group.position.copy(start);
const washerMat = mats.steel.clone();
const washer = new THREE.Mesh(
new THREE.TorusGeometry(0.135, 0.042, 12, 36),
washerMat
);
// Torus normal is local Y, so it lies flat on the bench.
washer.rotation.x = Math.PI / 2;
washer.castShadow = true;
washer.receiveShadow = true;
group.add(washer);
registerPart(
id,
group,
start,
target,
sideQuaternion,
badge
);
}
function createBolt(id, start, target, badge) {
const group = new THREE.Group();
group.position.copy(start);
group.quaternion.copy(sideQuaternion);
const boltMat = mats.steel.clone();
const shaft = new THREE.Mesh(
new THREE.CylinderGeometry(0.054, 0.054, 0.57, 18),
boltMat
);
shaft.position.y = -0.12;
shaft.castShadow = true;
shaft.receiveShadow = true;
group.add(shaft);
const head = new THREE.Mesh(
new THREE.CylinderGeometry(0.115, 0.115, 0.13, 6),
boltMat
);
head.position.y = 0.18;
head.castShadow = true;
head.receiveShadow = true;
group.add(head);
const tip = new THREE.Mesh(
new THREE.CylinderGeometry(0.054, 0.012, 0.1, 18),
boltMat
);
tip.position.y = -0.455;
tip.castShadow = true;
group.add(tip);
registerPart(
id,
group,
start,
target,
sideQuaternion,
badge
);
}
const lowerWasherBadge = createBadge(1, slotPositions[0], "#26a99a");
const lowerBoltBadge = createBadge(2, slotPositions[1], "#26a99a");
const upperWasherBadge = createBadge(3, slotPositions[2], "#e69a4e");
const upperBoltBadge = createBadge(4, slotPositions[3], "#e69a4e");
createWasher(
"lower-washer",
new THREE.Vector3(...slotPositions[0]),
new THREE.Vector3(1.77, lowerMountY, 0.95),
lowerWasherBadge
);
createBolt(
"lower-bolt",
new THREE.Vector3(...slotPositions[1]),
new THREE.Vector3(1.84, lowerMountY, 0.95),
lowerBoltBadge
);
createWasher(
"upper-washer",
new THREE.Vector3(...slotPositions[2]),
new THREE.Vector3(1.77, upperMountY, 0.95),
upperWasherBadge
);
createBolt(
"upper-bolt",
new THREE.Vector3(...slotPositions[3]),
new THREE.Vector3(1.84, upperMountY, 0.95),
upperBoltBadge
);
// -------------------------------------------------------------------------
// Loose-leg wobble indicator
// -------------------------------------------------------------------------
const wobbleMaterial = new THREE.MeshBasicMaterial({
color: 0xe69a4e,
transparent: true,
opacity: 0.72,
depthWrite: false
});
const wobbleArcs = [];
for (let i = 0; i < 2; i++) {
const arc = new THREE.Mesh(
new THREE.TorusGeometry(0.29 + i * 0.13, 0.012, 5, 30, Math.PI * 1.25),
wobbleMaterial.clone()
);
arc.rotation.x = Math.PI / 2;
arc.rotation.z = i * 0.75;
scene.add(arc);
wobbleArcs.push(arc);
}
function updateWobblePosition(time) {
const footLocal = new THREE.Vector3(0, -1.78, 0);
const foot = footLocal.applyEuler(looseLeg.rotation).add(looseLeg.position);
foot.y = 1.66;
wobbleArcs.forEach((arc, index) => {
arc.position.copy(foot);
arc.position.x += index * 0.025;
arc.position.z += index * 0.035;
arc.rotation.z += time * (index ? -0.9 : 0.9);
arc.material.opacity =
(currentStep >= steps.length ? 0 : 0.5) +
Math.sin(time * 4 + index) * 0.13;
});
}
// -------------------------------------------------------------------------
// Interaction state
// -------------------------------------------------------------------------
let currentStep = 0;
let hoveredPart = null;
let legAnimation = null;
let autoTimer = null;
let completionTimer = null;
let toastTimer = null;
let autoRunning = false;
const toast = document.querySelector("#toast");
const panelTitle = document.querySelector("#panelTitle");
const panelCopy = document.querySelector("#panelCopy");
const counter = document.querySelector("#counter");
const progressFill = document.querySelector("#progressFill");
const success = document.querySelector("#success");
const autoButton = document.querySelector("#autoButton");
const resetButton = document.querySelector("#resetButton");
const partLabel = document.querySelector("#partLabel");
const stepElements = [...document.querySelectorAll(".step")];
function showToast(message, error = false) {
clearTimeout(toastTimer);
toast.textContent = message;
toast.classList.toggle("error", error);
toast.classList.add("show");
toastTimer = setTimeout(() => {
toast.classList.remove("show");
}, 2600);
}
function updateUI() {
counter.textContent = `${currentStep} / ${steps.length}`;
progressFill.style.width = `${(currentStep / steps.length) * 100}%`;
stepElements.forEach((element, index) => {
element.classList.toggle("done", index < currentStep);
element.classList.toggle("active", index === currentStep);
});
if (currentStep >= steps.length) {
panelTitle.textContent = "Repair complete";
panelCopy.textContent = "The leg is seated, aligned, and firmly fastened.";
} else {
const next = steps[currentStep];
panelTitle.textContent = next.label;
panelCopy.textContent = "Click the glowing hardware in the listed order.";
}
}
function highlightParts(time) {
const activeId = currentStep < steps.length ? steps[currentStep].id : null;
Object.values(parts).forEach((part) => {
const active = part.id === activeId;
const hovered = part.id === hoveredPart && !active;
part.group.traverse((child) => {
if (!child.isMesh || !child.material?.emissive) return;
if (active) {
const pulse = 0.26 + Math.sin(time * 4.5) * 0.1;
child.material.emissive.setHex(0x36d7bd);
child.material.emissiveIntensity = pulse;
} else if (hovered) {
child.material.emissive.setHex(0xfff0c9);
child.material.emissiveIntensity = 0.22;
} else {
child.material.emissive.setHex(0x000000);
child.material.emissiveIntensity = 0;
}
});
if (part.badge) {
part.badge.material.opacity = part.installed ? 0.18 : active ? 1 : 0.55;
const scale = active ? 1 + Math.sin(time * 4) * 0.06 : 1;
part.badge.scale.setScalar(scale);
}
});
}
function beginLegAlignment() {
legAnimation = {
elapsed: 0,
duration: 1.05,
fromPosition: looseLeg.position.clone(),
fromQuaternion: looseLeg.quaternion.clone(),
toPosition: looseLegTargetPosition.clone(),
toQuaternion: new THREE.Quaternion().setFromEuler(
looseLegTargetRotation
)
};
}
function attemptPart(id) {
if (currentStep >= steps.length) return;
const expected = steps[currentStep].id;
const part = parts[id];
if (!part || part.installed) return;
if (id !== expected) {
showToast(`Not yet — fit the ${steps[currentStep].label.toLowerCase()} first.`, true);
return;
}
part.animation = {
elapsed: 0,
duration: 0.92,
fromPosition: part.group.position.clone(),
fromQuaternion: part.group.quaternion.clone()
};
currentStep += 1;
updateUI();
showToast(`${steps[currentStep - 1].label} fitted.`);
if (currentStep === steps.length) {
clearTimeout(completionTimer);
completionTimer = setTimeout(() => {
success.classList.add("show");
showToast("Wobble eliminated. The loose leg is secure.");
autoRunning = false;
autoButton.textContent = "Guided repair";
}, 1100);
}
}
function updatePartAnimations(delta) {
Object.values(parts).forEach((part) => {
if (!part.animation) return;
part.animation.elapsed += delta;
const raw = Math.min(part.animation.elapsed / part.animation.duration, 1);
const eased =
raw < 0.5
? 4 * raw * raw * raw
: 1 - Math.pow(-2 * raw + 2, 3) / 2;
const position = new THREE.Vector3().lerpVectors(
part.animation.fromPosition,
part.targetPosition,
eased
);
position.y += Math.sin(Math.PI * eased) * 0.58;
part.group.position.copy(position);
part.group.quaternion.slerpQuaternions(
part.animation.fromQuaternion,
part.targetQuaternion,
eased
);
if (raw >= 1) {
part.group.position.copy(part.targetPosition);
part.group.quaternion.copy(part.targetQuaternion);
part.animation = null;
part.installed = true;
if (part.id === "lower-bolt") {
beginLegAlignment();
}
}
});
if (legAnimation) {
legAnimation.elapsed += delta;
const raw = Math.min(legAnimation.elapsed / legAnimation.duration, 1);
const eased = 1 - Math.pow(1 - raw, 3);
looseLeg.position.lerpVectors(
legAnimation.fromPosition,
legAnimation.toPosition,
eased
);
looseLeg.quaternion.slerpQuaternions(
legAnimation.fromQuaternion,
legAnimation.toQuaternion,
eased
);
if (raw >= 1) {
legAnimation = null;
}
}
}
// -------------------------------------------------------------------------
// Custom camera controls — no addon imports
// -------------------------------------------------------------------------
const orbit = {
theta: 0.68,
phi: 1.12,
radius: window.innerWidth < 720 ? 11.5 : 9.8,
target: new THREE.Vector3(0, 2.55, 0.28)
};
function updateCamera() {
const sinPhi = Math.sin(orbit.phi);
camera.position.set(
orbit.target.x + orbit.radius * sinPhi * Math.sin(orbit.theta),
orbit.target.y + orbit.radius * Math.cos(orbit.phi),
orbit.target.z + orbit.radius * sinPhi * Math.cos(orbit.theta)
);
camera.lookAt(orbit.target);
}
let pointerDown = false;
let pointerMoved = 0;
let previousPointer = { x: 0, y: 0 };
const raycaster = new THREE.Raycaster();
const pointer = new THREE.Vector2();
function partAt(clientX, clientY) {
const rect = renderer.domElement.getBoundingClientRect();
pointer.x = ((clientX - rect.left) / rect.width) * 2 - 1;
pointer.y = -((clientY - rect.top) / rect.height) * 2 + 1;
raycaster.setFromCamera(pointer, camera);
const hits = raycaster.intersectObjects(
Object.values(parts).map((part) => part.group),
true
);
for (const hit of hits) {
if (hit.object.userData.partId) {
return hit.object.userData.partId;
}
}
return null;
}
renderer.domElement.addEventListener("pointerdown", (event) => {
pointerDown = true;
pointerMoved = 0;
previousPointer = { x: event.clientX, y: event.clientY };
renderer.domElement.setPointerCapture(event.pointerId);
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (pointerDown) {
const dx = event.clientX - previousPointer.x;
const dy = event.clientY - previousPointer.y;
pointerMoved += Math.abs(dx) + Math.abs(dy);
previousPointer = { x: event.clientX, y: event.clientY };
orbit.theta -= dx * 0.006;
orbit.phi = THREE.MathUtils.clamp(orbit.phi + dy * 0.0045, 0.55, 1.43);
} else {
hoveredPart = partAt(event.clientX, event.clientY);
renderer.domElement.style.cursor = hoveredPart ? "pointer" : "grab";
}
});
renderer.domElement.addEventListener("pointerup", (event) => {
pointerDown = false;
if (pointerMoved < 7) {
const id = partAt(event.clientX, event.clientY);
if (id) attemptPart(id);
}
renderer.domElement.style.cursor = hoveredPart ? "pointer" : "grab";
});
renderer.domElement.addEventListener("pointercancel", () => {
pointerDown = false;
});
renderer.domElement.addEventListener(
"wheel",
(event) => {
event.preventDefault();
orbit.radius = THREE.MathUtils.clamp(
orbit.radius * Math.exp(event.deltaY * 0.001),
6.5,
15
);
},
{ passive: false }
);
renderer.domElement.addEventListener("dblclick", () => {
orbit.theta = 0.68;
orbit.phi = 1.12;
orbit.radius = window.innerWidth < 720 ? 11.5 : 9.8;
orbit.target.set(0, 2.55, 0.28);
});
renderer.domElement.addEventListener("contextmenu", (event) => {
event.preventDefault();
});
// -------------------------------------------------------------------------
// UI controls
// -------------------------------------------------------------------------
function resetRepair() {
clearTimeout(autoTimer);
clearTimeout(completionTimer);
autoRunning = false;
autoButton.textContent = "Guided repair";
currentStep = 0;
hoveredPart = null;
legAnimation = null;
Object.values(parts).forEach((part) => {
part.animation = null;
part.installed = false;
part.group.position.copy(part.startPosition);
part.group.quaternion.copy(part.startQuaternion);
});
looseLeg.position.copy(looseLegStartPosition);
looseLeg.rotation.copy(looseLegStartRotation);
success.classList.remove("show");
updateUI();
showToast("Bench reset. Start with the lower washer.");
}
function startGuidedRepair() {
if (currentStep >= steps.length) {
resetRepair();
}
if (autoRunning) return;
autoRunning = true;
autoButton.textContent = "Repairing…";
function runNext() {
if (!autoRunning || currentStep >= steps.length) return;
const id = steps[currentStep].id;
attemptPart(id);
autoTimer = setTimeout(runNext, 1250);
}
runNext();
}
autoButton.addEventListener("click", startGuidedRepair);
resetButton.addEventListener("click", resetRepair);
// -------------------------------------------------------------------------
// Projected label for the active part
// -------------------------------------------------------------------------
const projected = new THREE.Vector3();
function updateProjectedLabel() {
if (currentStep >= steps.length) {
partLabel.classList.remove("show");
return;
}
const part = parts[steps[currentStep].id];
part.group.getWorldPosition(projected);
projected.y += 0.34;
projected.project(camera);
const visible =
projected.z > -1 &&
projected.z < 1 &&
Math.abs(projected.x) < 1.2 &&
Math.abs(projected.y) < 1.2;
if (!visible) {
partLabel.classList.remove("show");
return;
}
partLabel.textContent = steps[currentStep].label;
partLabel.style.left = `${(projected.x * 0.5 + 0.5) * window.innerWidth}px`;
partLabel.style.top = `${(-projected.y * 0.5 + 0.5) * window.innerHeight}px`;
partLabel.classList.add("show");
}
// -------------------------------------------------------------------------
// Resize and render loop
// -------------------------------------------------------------------------
window.addEventListener("resize", () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
});
const clock = new THREE.Clock();
function animate() {
requestAnimationFrame(animate);
const delta = Math.min(clock.getDelta(), 0.05);
const elapsed = clock.elapsedTime;
updateCamera();
updatePartAnimations(delta);
highlightParts(elapsed);
updateWobblePosition(elapsed);
updateProjectedLabel();
renderer.render(scene, camera);
}
updateUI();
updateCamera();
animate();
showToast("Click the glowing lower washer to begin.");
</script>
</body>
</html>
```