Prompt specification
Build or customize the Three.js 3D scene "Repair café diagnosis desk" 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é diagnosis desk
Category: Everyday & Industrial · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/repair-caf-diagnosis-desk-x0259
Scene Description Prompt:
Make a standalone HTML scene with Three.js from cdnjs that depicts wind-up toy diagnosis and repair. Compose three simple toys with distinct visible faults and organized part trays; selecting a symptom highlights a relevant mechanism while unrelated parts remain unaltered. Stylized solid forms.
Working Three.js Starter (repair-caf-diagnosis-desk-x0259):
```html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Wind-Up Toy Diagnosis & Repair</title>
<style>
:root {
--ink: #20313b;
--muted: #65747b;
--paper: #fffaf0;
--line: #d8d0c0;
--accent: #eea83b;
--green: #4f9b78;
--red: #d85f55;
}
* { box-sizing: border-box; }
html, body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #e8e2d8;
color: var(--ink);
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
canvas {
position: fixed;
inset: 0;
display: block;
cursor: grab;
touch-action: none;
}
canvas:active { cursor: grabbing; }
.title-card,
.diagnosis,
.actions {
position: fixed;
z-index: 5;
background: rgba(255, 250, 240, 0.91);
border: 1px solid rgba(54, 67, 72, 0.16);
box-shadow: 0 12px 35px rgba(43, 52, 54, 0.12);
backdrop-filter: blur(12px);
}
.title-card {
top: 18px;
left: 18px;
width: min(360px, calc(100vw - 36px));
padding: 17px 19px 16px;
border-radius: 18px;
}
.eyebrow {
color: #b66f32;
font-size: 11px;
font-weight: 800;
letter-spacing: 0.18em;
text-transform: uppercase;
}
h1 {
margin: 5px 0 5px;
font-size: clamp(23px, 2.3vw, 34px);
line-height: 1;
letter-spacing: -0.04em;
}
.subtitle {
margin: 0;
color: var(--muted);
font-size: 13px;
line-height: 1.45;
}
.actions {
top: 18px;
right: 18px;
display: flex;
gap: 8px;
padding: 8px;
border-radius: 14px;
}
button {
border: 0;
font: inherit;
cursor: pointer;
}
.action-button {
min-height: 38px;
padding: 0 15px;
border-radius: 9px;
color: var(--ink);
background: #eee7d9;
font-size: 12px;
font-weight: 800;
transition: transform .15s, background .15s, opacity .15s;
}
.action-button:hover:not(:disabled) {
transform: translateY(-1px);
background: #e3d9c6;
}
.action-button.primary {
color: #fffaf0;
background: #294a50;
}
.action-button.primary:hover:not(:disabled) { background: #355d63; }
.action-button:disabled {
opacity: .42;
cursor: not-allowed;
}
.diagnosis {
top: 83px;
right: 18px;
width: min(300px, calc(100vw - 36px));
padding: 14px 16px;
border-radius: 14px;
}
.diagnosis strong {
display: block;
margin-bottom: 4px;
font-size: 13px;
}
.diagnosis span {
display: block;
color: var(--muted);
font-size: 12px;
line-height: 1.45;
}
.symptom-panel {
position: fixed;
z-index: 5;
left: 50%;
bottom: 16px;
width: min(1040px, calc(100vw - 32px));
transform: translateX(-50%);
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 10px;
}
.symptom {
position: relative;
min-height: 91px;
padding: 14px 15px 12px 52px;
text-align: left;
color: var(--ink);
background: rgba(255, 250, 240, 0.94);
border: 1px solid rgba(54, 67, 72, 0.15);
border-radius: 15px;
box-shadow: 0 12px 30px rgba(43, 52, 54, 0.12);
backdrop-filter: blur(10px);
transition: border-color .18s, transform .18s, box-shadow .18s;
}
.symptom:hover {
transform: translateY(-2px);
box-shadow: 0 16px 34px rgba(43, 52, 54, 0.15);
}
.symptom.active {
border-color: #d68c2f;
box-shadow: 0 0 0 2px rgba(238, 168, 59, .25), 0 16px 34px rgba(43, 52, 54, 0.14);
}
.symptom .number {
position: absolute;
top: 15px;
left: 14px;
width: 27px;
height: 27px;
display: grid;
place-items: center;
border-radius: 8px;
color: white;
background: var(--toy-color);
font-size: 11px;
font-weight: 900;
}
.symptom .toy-name {
display: block;
margin-bottom: 3px;
font-size: 11px;
font-weight: 900;
letter-spacing: .09em;
text-transform: uppercase;
color: var(--muted);
}
.symptom .issue {
display: block;
font-size: 14px;
font-weight: 800;
line-height: 1.25;
}
.symptom .status {
display: block;
margin-top: 6px;
color: #9a6b2f;
font-size: 10px;
font-weight: 850;
letter-spacing: .08em;
text-transform: uppercase;
}
.symptom.repaired .status { color: var(--green); }
.help {
position: fixed;
z-index: 4;
left: 50%;
top: 19px;
transform: translateX(-50%);
padding: 9px 13px;
border-radius: 999px;
color: #4f5c60;
background: rgba(255, 250, 240, .72);
border: 1px solid rgba(54, 67, 72, .10);
font-size: 11px;
font-weight: 700;
pointer-events: none;
white-space: nowrap;
}
@media (max-width: 800px) {
.title-card { width: 250px; }
.subtitle { display: none; }
.help { display: none; }
.diagnosis { top: 82px; width: 250px; }
.actions { top: 18px; }
.action-button { padding: 0 10px; }
.symptom-panel {
grid-template-columns: 1fr;
bottom: 8px;
gap: 6px;
}
.symptom {
min-height: 67px;
padding-top: 10px;
padding-bottom: 9px;
}
.symptom .status { margin-top: 3px; }
}
</style>
</head>
<body>
<header class="title-card">
<div class="eyebrow">Mechanical diagnostics · Bench 03</div>
<h1>Wind-up repair workshop</h1>
<p class="subtitle">
Choose a symptom to illuminate only its related mechanism, then install the matching tray part.
</p>
</header>
<div class="help">Drag to orbit · Scroll to zoom · Click a mechanism</div>
<div class="actions">
<button class="action-button" id="reset-view">Reset view</button>
<button class="action-button" id="reset-workshop">Reset all</button>
<button class="action-button primary" id="repair" disabled>Repair selected</button>
</div>
<aside class="diagnosis" id="diagnosis">
<strong>Diagnosis ready</strong>
<span>Select a symptom below to isolate the responsible mechanism.</span>
</aside>
<nav class="symptom-panel" aria-label="Toy symptoms">
<button class="symptom" data-id="mouse" style="--toy-color:#4f9b91">
<span class="number">01</span>
<span class="toy-name">Clockwork mouse</span>
<span class="issue">“Runs for two seconds, then stops.”</span>
<span class="status">Inspect drive train</span>
</button>
<button class="symptom" data-id="robot" style="--toy-color:#d49b3f">
<span class="number">02</span>
<span class="toy-name">Wind-up robot</span>
<span class="issue">“The key spins, but nothing winds.”</span>
<span class="status">Inspect key spindle</span>
</button>
<button class="symptom" data-id="car" style="--toy-color:#c76555">
<span class="number">03</span>
<span class="toy-name">Pull-back racer</span>
<span class="issue">“It pulls sharply to one side.”</span>
<span class="status">Inspect wheel and axle</span>
</button>
</nav>
<script type="module">
import * as THREE from "https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xe8e2d8);
scene.fog = new THREE.Fog(0xe8e2d8, 28, 48);
const renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(innerWidth, innerHeight);
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.08;
document.body.prepend(renderer.domElement);
const camera = new THREE.PerspectiveCamera(42, innerWidth / innerHeight, 0.1, 100);
// Camera controls are implemented directly, without importing any addons.
const view = {
target: new THREE.Vector3(0, 1.0, 0.45),
radius: 20,
theta: 0.13,
phi: 0.98
};
const defaultView = {
target: view.target.clone(),
radius: view.radius,
theta: view.theta,
phi: view.phi
};
function updateCamera() {
const s = Math.sin(view.phi);
camera.position.set(
view.target.x + view.radius * s * Math.sin(view.theta),
view.target.y + view.radius * Math.cos(view.phi),
view.target.z + view.radius * s * Math.cos(view.theta)
);
camera.lookAt(view.target);
}
updateCamera();
const hemi = new THREE.HemisphereLight(0xfff6e4, 0x718089, 2.15);
scene.add(hemi);
const sun = new THREE.DirectionalLight(0xfff0d4, 3.2);
sun.position.set(7, 14, 10);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -13;
sun.shadow.camera.right = 13;
sun.shadow.camera.top = 12;
sun.shadow.camera.bottom = -12;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 35;
sun.shadow.bias = -0.0004;
scene.add(sun);
const fill = new THREE.DirectionalLight(0x9dc8d8, 1.0);
fill.position.set(-9, 6, -8);
scene.add(fill);
const standard = (color, roughness = 0.72, metalness = 0) =>
new THREE.MeshStandardMaterial({ color, roughness, metalness });
function addMesh(parent, geometry, material, position = [0, 0, 0], rotation = [0, 0, 0]) {
const object = new THREE.Mesh(geometry, material);
object.position.set(...position);
object.rotation.set(...rotation);
object.castShadow = true;
object.receiveShadow = true;
parent.add(object);
return object;
}
const box = (parent, size, material, position, rotation) =>
addMesh(parent, new THREE.BoxGeometry(...size), material, position, rotation);
const cylinder = (parent, radius, height, material, position, rotation) =>
addMesh(parent, new THREE.CylinderGeometry(radius, radius, height, 28), material, position, rotation);
const sphere = (parent, radius, material, position, scale = [1, 1, 1]) => {
const object = addMesh(
parent,
new THREE.SphereGeometry(radius, 32, 20),
material,
position
);
object.scale.set(...scale);
return object;
};
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 makeLabel(text, accent = "#294a50", scale = [2.7, 0.65, 1]) {
const canvas = document.createElement("canvas");
canvas.width = 640;
canvas.height = 150;
const ctx = canvas.getContext("2d");
roundedRect(ctx, 8, 8, 624, 134, 26);
ctx.fillStyle = "rgba(255,250,240,.95)";
ctx.fill();
ctx.lineWidth = 4;
ctx.strokeStyle = accent;
ctx.stroke();
ctx.fillStyle = accent;
ctx.textAlign = "center";
ctx.textBaseline = "middle";
ctx.font = "800 43px system-ui, sans-serif";
ctx.fillText(text, 320, 78);
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(...scale);
sprite.renderOrder = 10;
return sprite;
}
const benchTop = standard(0xc9b998, 0.86);
const benchEdge = standard(0xaa9677, 0.9);
const floorMat = standard(0xd9d5cc, 1);
const floor = addMesh(
scene,
new THREE.PlaneGeometry(70, 70),
floorMat,
[0, -1.82, 0],
[-Math.PI / 2, 0, 0]
);
floor.castShadow = false;
box(scene, [16.4, 0.34, 7.2], benchTop, [0, -0.47, 0.35]);
box(scene, [16.55, 0.12, 7.35], benchEdge, [0, -0.68, 0.35]);
const legMat = standard(0x405a5e, 0.8);
for (const x of [-6.6, 6.6]) {
for (const z of [-2.25, 2.95]) {
box(scene, [0.42, 1.35, 0.42], legMat, [x, -1.28, z]);
}
}
const toyGroups = {
mouse: new THREE.Group(),
robot: new THREE.Group(),
car: new THREE.Group()
};
toyGroups.mouse.position.set(-5.2, 0, -0.95);
toyGroups.robot.position.set(0, 0, -0.95);
toyGroups.car.position.set(5.2, 0, -0.95);
Object.values(toyGroups).forEach(group => scene.add(group));
function addPedestal(x, accent) {
const pedestal = new THREE.Group();
pedestal.position.set(x, 0, -0.95);
scene.add(pedestal);
cylinder(pedestal, 1.65, 0.12, standard(0xeee4d1), [0, -0.22, 0]);
cylinder(pedestal, 1.5, 0.035, standard(accent, 0.82), [0, -0.145, 0]);
cylinder(pedestal, 1.36, 0.025, standard(0xf7f0e3), [0, -0.12, 0]);
return pedestal;
}
addPedestal(-5.2, 0x76aaa2);
addPedestal(0, 0xe3bc72);
addPedestal(5.2, 0xd79284);
const mechanismRoots = {};
const mechanismMaterials = {};
const mechanismInfos = {};
function registerMechanism(group, id) {
mechanismRoots[id] = group;
mechanismMaterials[id] = [];
group.traverse(object => {
if (!object.isMesh) return;
object.userData.symptomId = id;
const clone = object.material.clone();
object.material = clone;
mechanismMaterials[id].push({
material: clone,
emissive: clone.emissive.clone(),
intensity: clone.emissiveIntensity
});
});
}
// ---------------------------------------------------------------------
// CLOCKWORK MOUSE
// ---------------------------------------------------------------------
const mouse = toyGroups.mouse;
const mouseBodyMat = standard(0x4f9b91, 0.68);
const mouseBellyMat = standard(0x9bc4b7, 0.72);
const pinkMat = standard(0xd98e89, 0.74);
const darkMat = standard(0x26363b, 0.58);
const metalMat = standard(0xc5a363, 0.35, 0.62);
const faultMat = standard(0xdf554b, 0.52, 0.18);
sphere(mouse, 1, mouseBodyMat, [0, 0.62, 0], [0.82, 0.49, 1.02]);
sphere(mouse, 0.62, mouseBellyMat, [0, 0.39, 0.42], [0.61, 0.35, 0.66]);
sphere(mouse, 0.27, pinkMat, [0, 0.49, 0.93], [0.86, 0.65, 0.65]);
for (const side of [-1, 1]) {
sphere(mouse, 0.09, darkMat, [side * 0.22, 0.81, 0.79], [0.75, 1, 0.5]);
const ear = cylinder(
mouse,
0.24,
0.08,
pinkMat,
[side * 0.49, 1.02, 0.32],
[Math.PI / 2, 0, 0]
);
ear.scale.y = 0.92;
const inner = cylinder(
mouse,
0.13,
0.095,
standard(0xe8aaa0, 0.8),
[side * 0.49, 1.025, 0.365],
[Math.PI / 2, 0, 0]
);
inner.scale.y = 0.9;
sphere(mouse, 0.14, mouseBodyMat, [side * 0.43, 0.24, 0.62], [1.25, 0.6, 1]);
}
const tailCurve = new THREE.CatmullRomCurve3([
new THREE.Vector3(0, 0.62, -0.91),
new THREE.Vector3(0.12, 0.76, -1.25),
new THREE.Vector3(-0.12, 0.88, -1.46),
new THREE.Vector3(0.08, 0.72, -1.62)
]);
addMesh(mouse, new THREE.TubeGeometry(tailCurve, 24, 0.045, 10, false), pinkMat);
const mouseGear = new THREE.Group();
mouseGear.position.set(0.70, 0.64, -0.08);
mouse.add(mouseGear);
cylinder(mouseGear, 0.32, 0.17, metalMat, [0, 0, 0], [0, 0, Math.PI / 2]);
cylinder(mouseGear, 0.10, 0.21, darkMat, [0, 0, 0], [0, 0, Math.PI / 2]);
for (let i = 0; i < 12; i++) {
const angle = i / 12 * Math.PI * 2;
const toothMat = i % 3 === 0 ? standard(0xd7b86e, 0.38, 0.52) : metalMat;
box(
mouseGear,
[0.18, 0.14, 0.20],
toothMat,
[0, Math.cos(angle) * 0.36, Math.sin(angle) * 0.36],
[-angle, 0, 0]
);
}
// Brightly visible loose drive pin: the mouse's distinct fault.
const looseMousePin = cylinder(
mouseGear,
0.065,
0.30,
faultMat,
[0.18, 0.18, 0.15],
[0, 0, Math.PI / 2]
);
looseMousePin.rotation.z = Math.PI / 2 + 0.34;
registerMechanism(mouseGear, "mouse");
// ---------------------------------------------------------------------
// WIND-UP ROBOT
// ---------------------------------------------------------------------
const robot = toyGroups.robot;
const robotYellow = standard(0xe0ac45, 0.66);
const robotBlue = standard(0x4f7485, 0.72);
const robotDark = standard(0x314047, 0.62);
const robotRed = standard(0xcc6557, 0.68);
for (const x of [-0.28, 0.28]) {
box(robot, [0.31, 0.58, 0.36], robotBlue, [x, 0.34, 0]);
box(robot, [0.42, 0.20, 0.57], robotDark, [x, 0.08, 0.12]);
}
box(robot, [1.02, 0.92, 0.66], robotYellow, [0, 1.13, 0]);
box(robot, [0.68, 0.26, 0.68], robotBlue, [0, 0.70, 0]);
cylinder(robot, 0.13, 0.22, robotDark, [0, 1.68, 0]);
box(robot, [0.86, 0.68, 0.65], robotYellow, [0, 2.04, 0]);
box(robot, [0.69, 0.30, 0.67], robotBlue, [0, 2.36, 0]);
for (const x of [-0.20, 0.20]) {
sphere(robot, 0.075, robotDark, [x, 2.10, 0.335], [1, 1, 0.45]);
}
box(robot, [0.24, 0.035, 0.035], robotDark, [0, 1.93, 0.34]);
for (const side of [-1, 1]) {
cylinder(robot, 0.14, 0.18, robotDark, [side * 0.61, 1.55, 0], [0, 0, Math.PI / 2]);
cylinder(robot, 0.11, 0.64, robotBlue, [side * 0.64, 1.16, 0]);
sphere(robot, 0.15, robotDark, [side * 0.64, 0.82, 0]);
}
cylinder(robot, 0.045, 0.35, robotDark, [0, 2.67, 0]);
sphere(robot, 0.10, robotRed, [0, 2.87, 0]);
const robotMechanism = new THREE.Group();
robotMechanism.position.set(0.62, 1.42, 0);
robot.add(robotMechanism);
cylinder(robotMechanism, 0.13, 0.50, robotDark, [0, 0, 0], [0, 0, Math.PI / 2]);
cylinder(robotMechanism, 0.19, 0.09, metalMat, [-0.25, 0, 0], [0, 0, Math.PI / 2]);
function makeKey(color) {
const key = new THREE.Group();
cylinder(key, 0.065, 0.38, color, [0, 0, 0], [0, 0, Math.PI / 2]);
const loop = addMesh(
key,
new THREE.TorusGeometry(0.20, 0.065, 12, 30),
color,
[0.22, 0, 0],
[0, Math.PI / 2, 0]
);
loop.scale.y = 1.12;
return key;
}
// Detached key and a red coupling gap make the robot's fault obvious.
const faultRobotKey = makeKey(metalMat);
faultRobotKey.position.set(0.64, 0, 0);
robotMechanism.add(faultRobotKey);
const robotGap = cylinder(
robotMechanism,
0.085,
0.13,
faultMat,
[0.34, 0, 0],
[0, 0, Math.PI / 2]
);
registerMechanism(robotMechanism, "robot");
// ---------------------------------------------------------------------
// PULL-BACK RACER
// ---------------------------------------------------------------------
const car = toyGroups.car;
const carRed = standard(0xc96554, 0.57);
const carCream = standard(0xe8dcc0, 0.68);
const carBlue = standard(0x547b8b, 0.5);
const rubber = standard(0x263238, 0.9);
const hubMat = standard(0xb8b4a8, 0.34, 0.65);
box(car, [2.35, 0.38, 1.12], carRed, [0, 0.57, 0]);
box(car, [1.22, 0.55, 0.93], carBlue, [-0.12, 1.00, 0]);
box(car, [1.25, 0.27, 0.96], carRed, [-0.12, 1.29, 0]);
box(car, [0.42, 0.22, 0.96], carCream, [0.83, 0.84, 0]);
box(car, [0.10, 0.17, 0.72], carCream, [1.24, 0.57, 0]);
for (const z of [-0.48, 0.48]) {
box(car, [0.29, 0.16, 0.05], standard(0xffe9ac, 0.38), [1.21, 0.69, z]);
}
function makeWheel(parent, position, tilt = 0) {
const wheel = new THREE.Group();
wheel.position.set(...position);
wheel.rotation.z = tilt;
parent.add(wheel);
cylinder(wheel, 0.34, 0.22, rubber, [0, 0, 0], [Math.PI / 2, 0, 0]);
cylinder(wheel, 0.17, 0.235, hubMat, [0, 0, 0], [Math.PI / 2, 0, 0]);
for (let i = 0; i < 3; i++) {
const angle = i / 3 * Math.PI * 2;
box(
wheel,
[0.055, 0.26, 0.24],
carCream,
[0, 0, 0],
[0, 0, angle]
);
}
return wheel;
}
makeWheel(car, [-0.76, 0.38, -0.59]);
makeWheel(car, [-0.76, 0.38, 0.59]);
makeWheel(car, [0.78, 0.38, -0.59]);
const carMechanism = new THREE.Group();
carMechanism.position.set(0.78, 0.38, 0.67);
car.add(carMechanism);
cylinder(carMechanism, 0.07, 0.70, hubMat, [0, 0, -0.25], [Math.PI / 2, 0, 0]);
const carGap = cylinder(
carMechanism,
0.095,
0.12,
faultMat,
[0, 0, -0.13],
[Math.PI / 2, 0, 0]
);
// A canted wheel is the racer's visible alignment fault.
const faultCarWheel = makeWheel(carMechanism, [0, 0, 0], 0.18);
registerMechanism(carMechanism, "car");
// ---------------------------------------------------------------------
// ORGANIZED PARTS TRAYS
// ---------------------------------------------------------------------
const trayMat = standard(0x9aaeb0, 0.76);
const trayInset = standard(0x71898c, 0.8);
const screwMat = standard(0x9eabb0, 0.3, 0.7);
function createTray(x, label, accent) {
const tray = new THREE.Group();
tray.position.set(x, 0, 2.35);
scene.add(tray);
box(tray, [3.10, 0.13, 1.45], trayInset, [0, -0.18, 0]);
box(tray, [3.20, 0.16, 0.10], trayMat, [0, -0.04, -0.72]);
box(tray, [3.20, 0.16, 0.10], trayMat, [0, -0.04, 0.72]);
box(tray, [0.10, 0.16, 1.45], trayMat, [-1.55, -0.04, 0]);
box(tray, [0.10, 0.16, 1.45], trayMat, [1.55, -0.04, 0]);
// Three physical compartments keep tools, hardware, and replacements separate.
box(tray, [0.045, 0.11, 1.34], trayMat, [-0.52, -0.04, 0]);
box(tray, [0.045, 0.11, 1.34], trayMat, [0.50, -0.04, 0]);
// Small wrench.
box(tray, [0.50, 0.055, 0.12], hubMat, [-1.08, 0.03, 0.10]);
addMesh(
tray,
new THREE.TorusGeometry(0.13, 0.04, 10, 24),
hubMat,
[-0.79, 0.035, 0.10]
);
// Ordered screws.
for (let i = 0; i < 3; i++) {
cylinder(tray, 0.07, 0.28, screwMat, [-0.25 + i * 0.21, 0.035, -0.12]);
cylinder(tray, 0.11, 0.04, screwMat, [-0.25 + i * 0.21, 0.035, -0.12]);
}
const labelSprite = makeLabel(label, accent, [2.55, 0.60, 1]);
labelSprite.position.set(0, 0.77, 0.53);
tray.add(labelSprite);
return tray;
}
const mouseTray = createTray(-5.2, "01 · GEAR TRAY", "#4f9b91");
const robotTray = createTray(0, "02 · KEY TRAY", "#d49b3f");
const carTray = createTray(5.2, "03 · WHEEL TRAY", "#c76555");
const spareMousePin = cylinder(
mouseTray,
0.064,
0.30,
metalMat,
[1.03, 0.035, 0.08],
[0, 0, Math.PI / 2]
);
spareMousePin.scale.setScalar(0.72);
const spareRobotKey = makeKey(metalMat);
spareRobotKey.position.set(1.02, 0.04, 0.08);
spareRobotKey.scale.setScalar(0.57);
const spareCarWheel = makeWheel(carTray, [1.02, 0.04, 0.08]);
spareCarWheel.scale.setScalar(0.56);
const spares = {
mouse: spareMousePin,
robot: spareRobotKey,
car: spareCarWheel
};
function rememberHome(object) {
object.userData.home = {
parent: object.parent,
position: object.position.clone(),
quaternion: object.quaternion.clone(),
scale: object.scale.clone()
};
}
Object.values(spares).forEach(rememberHome);
// Floating workshop labels.
const mouseLabel = makeLabel("CLOCKWORK MOUSE", "#4f9b91", [2.75, 0.64, 1]);
mouseLabel.position.set(0, 2.08, -0.1);
mouse.add(mouseLabel);
const robotLabel = makeLabel("WIND-UP ROBOT", "#b78330", [2.75, 0.64, 1]);
robotLabel.position.set(0, 3.18, -0.1);
robot.add(robotLabel);
const carLabel = makeLabel("PULL-BACK RACER", "#b45547", [2.85, 0.64, 1]);
carLabel.position.set(0, 2.02, -0.1);
car.add(carLabel);
// A camera-facing locator makes the selected mechanism unmistakable.
const halo = new THREE.Mesh(
new THREE.TorusGeometry(0.68, 0.035, 10, 48),
new THREE.MeshBasicMaterial({
color: 0xffb338,
transparent: true,
opacity: 0.92,
depthTest: false,
depthWrite: false
})
);
halo.renderOrder = 20;
halo.visible = false;
scene.add(halo);
const haloPositions = {
mouse: new THREE.Vector3(-4.43, 0.73, -0.86),
robot: new THREE.Vector3(0.98, 1.42, -0.95),
car: new THREE.Vector3(5.98, 0.38, -0.28)
};
const symptoms = {
mouse: {
title: "Clockwork mouse",
finding: "The drive pin has backed out of the gear train.",
status: "Inspect drive train"
},
robot: {
title: "Wind-up robot",
finding: "The wind-up key is no longer coupled to the spindle.",
status: "Inspect key spindle"
},
car: {
title: "Pull-back racer",
finding: "The near wheel is canted and the axle coupling is loose.",
status: "Inspect wheel and axle"
}
};
let selected = null;
let repairing = false;
const repaired = new Set();
let animations = [];
const cards = [...document.querySelectorAll(".symptom")];
const diagnosis = document.getElementById("diagnosis");
const repairButton = document.getElementById("repair");
function resetMechanismColors() {
for (const [id, materials] of Object.entries(mechanismMaterials)) {
for (const entry of materials) {
entry.material.emissive.copy(entry.emissive);
entry.material.emissiveIntensity = entry.intensity;
}
}
}
function updateCards() {
cards.forEach(card => {
const id = card.dataset.id;
card.classList.toggle("active", id === selected);
card.classList.toggle("repaired", repaired.has(id));
const status = card.querySelector(".status");
if (repairing && id === selected) {
status.textContent = "Installing replacement…";
} else if (repaired.has(id)) {
status.textContent = "Repaired · mechanism confirmed";
} else if (id === selected) {
status.textContent = "Related mechanism highlighted";
} else {
status.textContent = symptoms[id].status;
}
});
repairButton.disabled = !selected || repairing || repaired.has(selected);
repairButton.textContent = repaired.has(selected) ? "Repaired ✓" : "Repair selected";
}
function selectSymptom(id) {
selected = id;
resetMechanismColors();
for (const material of mechanismMaterials[id]) {
material.material.emissive.set(0xffa72e);
material.material.emissiveIntensity = 0.38;
}
halo.visible = true;
halo.position.copy(haloPositions[id]);
diagnosis.innerHTML = `
<strong>${symptoms[id].title}</strong>
<span>${symptoms[id].finding} Only this mechanism has been illuminated.</span>
`;
updateCards();
}
cards.forEach(card => {
card.addEventListener("click", () => selectSymptom(card.dataset.id));
});
const repairSpecs = {
mouse: {
owner: mouse,
target: new THREE.Vector3(0.92, 0.76, -0.08),
targetScale: 1,
faults: [looseMousePin]
},
robot: {
owner: robot,
target: new THREE.Vector3(0.98, 1.42, 0),
targetScale: 1,
faults: [faultRobotKey, robotGap]
},
car: {
owner: car,
target: new THREE.Vector3(0.78, 0.38, 0.67),
targetScale: 1,
faults: [faultCarWheel, carGap]
}
};
function beginRepair(id) {
if (repairing || !id || repaired.has(id)) return;
repairing = true;
updateCards();
const spec = repairSpecs[id];
const spare = spares[id];
spec.faults.forEach(fault => fault.visible = false);
spare.visible = true;
const targetHelper = new THREE.Object3D();
spec.owner.add(targetHelper);
targetHelper.position.copy(spec.target);
spec.owner.updateMatrixWorld(true);
const targetPosition = new THREE.Vector3();
const targetQuaternion = new THREE.Quaternion();
targetHelper.getWorldPosition(targetPosition);
targetHelper.getWorldQuaternion(targetQuaternion);
scene.updateMatrixWorld(true);
scene.attach(spare);
const startPosition = spare.position.clone();
const startQuaternion = spare.quaternion.clone();
const startScale = spare.scale.clone();
animations.push({
mesh: spare,
owner: spec.owner,
startPosition,
startQuaternion,
startScale,
targetPosition,
targetQuaternion,
targetScale: new THREE.Vector3().setScalar(spec.targetScale),
startTime: performance.now(),
duration: 1050,
onComplete: () => {
spec.owner.attach(spare);
spare.position.copy(spec.target);
spare.quaternion.identity();
spare.scale.setScalar(spec.targetScale);
repaired.add(id);
repairing = false;
updateCards();
}
});
}
repairButton.addEventListener("click", () => beginRepair(selected));
document.getElementById("reset-view").addEventListener("click", () => {
view.target.copy(defaultView.target);
view.radius = defaultView.radius;
view.theta = defaultView.theta;
view.phi = defaultView.phi;
updateCamera();
});
document.getElementById("reset-workshop").addEventListener("click", () => {
animations = [];
repairing = false;
repaired.clear();
for (const [id, spec] of Object.entries(repairSpecs)) {
spec.faults.forEach(fault => fault.visible = true);
const spare = spares[id];
const home = spare.userData.home;
home.parent.attach(spare);
spare.position.copy(home.position);
spare.quaternion.copy(home.quaternion);
spare.scale.copy(home.scale);
spare.visible = true;
}
selectSymptom("mouse");
});
// Click the visible faulty mechanism as an alternative to using the cards.
const raycaster = new THREE.Raycaster();
const pointer = new THREE.Vector2();
let pointerDown = null;
function hitMechanism(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;
raycaster.setFromCamera(pointer, camera);
const hits = raycaster.intersectObjects(Object.values(mechanismRoots), true);
for (const hit of hits) {
let visible = true;
let node = hit.object;
while (node) {
if (!node.visible) visible = false;
node = node.parent;
}
if (visible && hit.object.userData.symptomId) {
return hit.object.userData.symptomId;
}
}
return null;
}
renderer.domElement.addEventListener("pointerdown", event => {
pointerDown = { x: event.clientX, y: event.clientY };
renderer.domElement.setPointerCapture(event.pointerId);
});
renderer.domElement.addEventListener("pointermove", event => {
if (pointerDown) {
const dx = event.clientX - pointerDown.x;
const dy = event.clientY - pointerDown.y;
if (Math.abs(dx) + Math.abs(dy) > 3) {
view.theta -= dx * 0.006;
view.phi = THREE.MathUtils.clamp(view.phi - dy * 0.005, 0.58, 1.33);
pointerDown = { x: event.clientX, y: event.clientY };
updateCamera();
}
renderer.domElement.style.cursor = "grabbing";
} else {
renderer.domElement.style.cursor = hitMechanism(event) ? "pointer" : "grab";
}
});
renderer.domElement.addEventListener("pointerup", event => {
if (pointerDown) {
const movement = Math.hypot(
event.clientX - pointerDown.x,
event.clientY - pointerDown.y
);
if (movement < 5) {
const id = hitMechanism(event);
if (id) selectSymptom(id);
}
}
pointerDown = null;
renderer.domElement.style.cursor = "grab";
});
renderer.domElement.addEventListener("pointercancel", () => {
pointerDown = null;
});
renderer.domElement.addEventListener("wheel", event => {
event.preventDefault();
view.radius = THREE.MathUtils.clamp(view.radius + event.deltaY * 0.012, 13, 27);
updateCamera();
}, { passive: false });
renderer.domElement.addEventListener("dblclick", () => {
view.target.copy(defaultView.target);
view.radius = defaultView.radius;
view.theta = defaultView.theta;
view.phi = defaultView.phi;
updateCamera();
});
addEventListener("resize", () => {
camera.aspect = innerWidth / innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(innerWidth, innerHeight);
renderer.setPixelRatio(Math.min(devicePixelRatio, 2));
});
function easeInOutCubic(t) {
return t < 0.5
? 4 * t * t * t
: 1 - Math.pow(-2 * t + 2, 3) / 2;
}
function animate(now) {
requestAnimationFrame(animate);
if (selected) {
const pulse = 0.30 + Math.sin(now * 0.005) * 0.12;
for (const entry of mechanismMaterials[selected]) {
entry.material.emissiveIntensity = pulse;
}
halo.quaternion.copy(camera.quaternion);
const haloScale = 1 + Math.sin(now * 0.004) * 0.08;
halo.scale.setScalar(haloScale);
}
for (let i = animations.length - 1; i >= 0; i--) {
const animation = animations[i];
const raw = THREE.MathUtils.clamp(
(now - animation.startTime) / animation.duration,
0,
1
);
const t = easeInOutCubic(raw);
animation.mesh.position.lerpVectors(
animation.startPosition,
animation.targetPosition,
t
);
animation.mesh.quaternion.slerpQuaternions(
animation.startQuaternion,
animation.targetQuaternion,
t
);
animation.mesh.scale.lerpVectors(
animation.startScale,
animation.targetScale,
t
);
if (raw >= 1) {
animation.onComplete();
animations.splice(i, 1);
}
}
renderer.render(scene, camera);
}
selectSymptom("mouse");
requestAnimationFrame(animate);
</script>
</body>
</html>
```