Prompt specification
Build or customize the Three.js 3D scene "Household small-repair evening" 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: Household small-repair evening
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/household-small-repair-evening-x0271
Scene Description Prompt:
Animate a voxel kitchen repair: a character removes, matches, and reattaches a loose drawer handle. Three.js from cdnjs, one HTML file.
Working Three.js Starter (household-small-repair-evening-x0271):
```html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Voxel Kitchen Repair</title>
<style>
:root {
color-scheme: dark;
font-family: Inter, ui-sans-serif, system-ui, -apple-system,
BlinkMacSystemFont, "Segoe UI", sans-serif;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #111b23;
color: #f5f1e8;
}
#app,
canvas {
width: 100%;
height: 100%;
display: block;
}
canvas {
touch-action: none;
cursor: grab;
}
canvas:active {
cursor: grabbing;
}
.vignette {
position: fixed;
inset: 0;
pointer-events: none;
background:
radial-gradient(circle at 50% 42%, transparent 40%, rgba(7, 13, 18, 0.36) 100%);
z-index: 2;
}
.panel {
position: fixed;
z-index: 4;
border: 1px solid rgba(255, 255, 255, 0.12);
background: rgba(15, 25, 33, 0.78);
box-shadow: 0 18px 50px rgba(0, 0, 0, 0.24);
backdrop-filter: blur(14px);
-webkit-backdrop-filter: blur(14px);
}
#title-card {
top: 22px;
left: 22px;
width: min(360px, calc(100vw - 44px));
padding: 18px 19px 16px;
border-radius: 18px;
pointer-events: none;
}
.eyebrow {
display: flex;
align-items: center;
gap: 9px;
margin-bottom: 9px;
color: #8fe1b4;
font-size: 11px;
font-weight: 800;
letter-spacing: 0.15em;
text-transform: uppercase;
}
.eyebrow::before {
content: "";
width: 8px;
height: 8px;
border-radius: 2px;
background: #8fe1b4;
box-shadow: 0 0 18px rgba(143, 225, 180, 0.75);
}
h1 {
margin: 0;
font-size: clamp(24px, 3vw, 35px);
line-height: 1;
letter-spacing: -0.045em;
}
#title-card p {
margin: 11px 0 0;
color: #aebbc4;
font-size: 13px;
line-height: 1.5;
}
#step-card {
top: 22px;
right: 22px;
width: min(330px, calc(100vw - 44px));
padding: 15px 16px;
border-radius: 16px;
pointer-events: none;
}
.step-heading {
display: flex;
justify-content: space-between;
align-items: center;
gap: 14px;
}
#step-number {
color: #8fe1b4;
font-size: 11px;
font-weight: 800;
letter-spacing: 0.13em;
text-transform: uppercase;
}
#step-title {
margin-top: 5px;
font-size: 16px;
font-weight: 750;
letter-spacing: -0.015em;
}
.progress-track {
height: 5px;
margin-top: 13px;
overflow: hidden;
border-radius: 999px;
background: rgba(255, 255, 255, 0.1);
}
#progress-fill {
width: 0%;
height: 100%;
border-radius: inherit;
background: linear-gradient(90deg, #f5bc65, #8fe1b4);
transition: width 80ms linear;
}
#transport {
left: 50%;
bottom: 20px;
width: min(680px, calc(100vw - 32px));
transform: translateX(-50%);
display: grid;
grid-template-columns: auto 1fr auto;
align-items: center;
gap: 14px;
padding: 12px 13px;
border-radius: 17px;
}
button {
border: 0;
border-radius: 11px;
color: #102019;
background: #8fe1b4;
font: inherit;
font-size: 13px;
font-weight: 800;
cursor: pointer;
transition: transform 120ms ease, background 120ms ease;
}
button:hover {
transform: translateY(-1px);
background: #a4ebc3;
}
button:active {
transform: translateY(1px);
}
#play-pause {
min-width: 78px;
height: 39px;
}
#replay {
min-width: 66px;
height: 39px;
color: #e7edf0;
background: rgba(255, 255, 255, 0.1);
}
#replay:hover {
background: rgba(255, 255, 255, 0.17);
}
#timeline {
width: 100%;
accent-color: #8fe1b4;
cursor: pointer;
}
.time-readout {
min-width: 76px;
color: #aebbc4;
font-variant-numeric: tabular-nums;
font-size: 12px;
text-align: right;
}
#camera-hint {
position: fixed;
right: 22px;
bottom: 22px;
z-index: 3;
padding: 9px 12px;
border-radius: 10px;
color: rgba(239, 245, 247, 0.65);
background: rgba(11, 19, 25, 0.55);
font-size: 11px;
pointer-events: none;
}
.world-label {
position: fixed;
z-index: 3;
padding: 6px 9px;
border: 1px solid rgba(255, 255, 255, 0.14);
border-radius: 8px;
color: #f6f8f8;
background: rgba(12, 21, 28, 0.82);
box-shadow: 0 8px 22px rgba(0, 0, 0, 0.22);
font-size: 10px;
font-weight: 850;
letter-spacing: 0.08em;
text-transform: uppercase;
pointer-events: none;
white-space: nowrap;
transform: translate(-50%, -50%);
opacity: 0;
transition: opacity 160ms ease;
}
#loose-label {
color: #ffcf9c;
border-color: rgba(255, 190, 126, 0.3);
}
#reference-label {
color: #9de7be;
border-color: rgba(143, 225, 180, 0.3);
}
#match-label {
color: #092318;
background: #8fe1b4;
border-color: #b8f3d1;
font-size: 11px;
padding: 8px 11px;
}
#loading {
position: fixed;
inset: 0;
z-index: 10;
display: grid;
place-items: center;
color: #dce6e9;
background: #111b23;
font-weight: 750;
letter-spacing: 0.08em;
text-transform: uppercase;
}
@media (max-width: 760px) {
#title-card {
top: 12px;
left: 12px;
width: calc(100vw - 24px);
}
#title-card p,
#step-card {
display: none;
}
#transport {
bottom: 12px;
width: calc(100vw - 24px);
grid-template-columns: auto 1fr;
}
#replay,
.time-readout,
#camera-hint {
display: none;
}
}
</style>
</head>
<body>
<div id="app"></div>
<div class="vignette"></div>
<section id="title-card" class="panel">
<div class="eyebrow">Voxel home repair</div>
<h1>Handle / Reset</h1>
<p>
Remove a loose cabinet handle, match its profile and mounting spacing,
then reattach and test it.
</p>
</section>
<section id="step-card" class="panel">
<div class="step-heading">
<div>
<div id="step-number">Step 01 / 05</div>
<div id="step-title">Approach the drawer</div>
</div>
<div id="step-icon">▣</div>
</div>
<div class="progress-track"><div id="progress-fill"></div></div>
</section>
<div id="transport" class="panel">
<button id="play-pause" type="button">Pause</button>
<input
id="timeline"
type="range"
min="0"
max="18"
step="0.01"
value="0"
aria-label="Animation timeline"
/>
<button id="replay" type="button">Replay</button>
<div class="time-readout"><span id="current-time">0.0</span> / 18.0s</div>
</div>
<div id="camera-hint">Drag to orbit · Scroll to zoom</div>
<div id="loose-label" class="world-label">Loose handle</div>
<div id="reference-label" class="world-label">Matching reference</div>
<div id="match-label" class="world-label">Profile + holes match ✓</div>
<div id="loading">Building the kitchen…</div>
<script type="module">
import * as THREE from "https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const app = document.querySelector("#app");
const loading = document.querySelector("#loading");
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.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.08;
app.appendChild(renderer.domElement);
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x17232c);
scene.fog = new THREE.Fog(0x17232c, 14, 28);
const camera = new THREE.PerspectiveCamera(
42,
window.innerWidth / window.innerHeight,
0.1,
60
);
const cameraState = {
target: new THREE.Vector3(-0.15, 1.22, 0.25),
radius: 10.7,
theta: 0.48,
phi: 1.08
};
function updateCamera() {
const { target, radius, theta, phi } = cameraState;
const sinPhi = Math.sin(phi);
camera.position.set(
target.x + radius * sinPhi * Math.sin(theta),
target.y + radius * Math.cos(phi),
target.z + radius * sinPhi * Math.cos(theta)
);
camera.lookAt(target);
}
updateCamera();
// Direct, dependency-free orbit controls.
let dragging = false;
let pointerX = 0;
let pointerY = 0;
renderer.domElement.addEventListener("pointerdown", (event) => {
dragging = true;
pointerX = event.clientX;
pointerY = event.clientY;
renderer.domElement.setPointerCapture(event.pointerId);
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (!dragging) return;
const dx = event.clientX - pointerX;
const dy = event.clientY - pointerY;
pointerX = event.clientX;
pointerY = event.clientY;
cameraState.theta -= dx * 0.006;
cameraState.phi = THREE.MathUtils.clamp(
cameraState.phi + dy * 0.005,
0.68,
1.32
);
updateCamera();
});
function stopDragging(event) {
dragging = false;
if (event.pointerId !== undefined && renderer.domElement.hasPointerCapture(event.pointerId)) {
renderer.domElement.releasePointerCapture(event.pointerId);
}
}
renderer.domElement.addEventListener("pointerup", stopDragging);
renderer.domElement.addEventListener("pointercancel", stopDragging);
renderer.domElement.addEventListener(
"wheel",
(event) => {
event.preventDefault();
cameraState.radius = THREE.MathUtils.clamp(
cameraState.radius + event.deltaY * 0.007,
6.5,
15
);
updateCamera();
},
{ passive: false }
);
const hemisphere = new THREE.HemisphereLight(0xcce8ff, 0x52402d, 2.0);
scene.add(hemisphere);
const sun = new THREE.DirectionalLight(0xfff1d6, 3.3);
sun.position.set(-4, 8, 6);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -8;
sun.shadow.camera.right = 8;
sun.shadow.camera.top = 8;
sun.shadow.camera.bottom = -5;
sun.shadow.camera.near = 0.5;
sun.shadow.camera.far = 22;
sun.shadow.normalBias = 0.035;
scene.add(sun);
const rimLight = new THREE.DirectionalLight(0x8cc9ff, 1.1);
rimLight.position.set(5, 4, -5);
scene.add(rimLight);
const materials = {
floorA: new THREE.MeshStandardMaterial({ color: 0xb7aa96, roughness: 0.92 }),
floorB: new THREE.MeshStandardMaterial({ color: 0xc8bca8, roughness: 0.92 }),
wall: new THREE.MeshStandardMaterial({ color: 0xd7ddd8, roughness: 0.88 }),
tile: new THREE.MeshStandardMaterial({ color: 0xe4e8df, roughness: 0.7 }),
tileAlt: new THREE.MeshStandardMaterial({ color: 0xcbd9d4, roughness: 0.75 }),
cabinet: new THREE.MeshStandardMaterial({ color: 0x315d68, roughness: 0.72 }),
cabinetDark: new THREE.MeshStandardMaterial({ color: 0x24444e, roughness: 0.75 }),
drawer: new THREE.MeshStandardMaterial({ color: 0x3d7480, roughness: 0.66 }),
counter: new THREE.MeshStandardMaterial({ color: 0xeee5d3, roughness: 0.82 }),
wood: new THREE.MeshStandardMaterial({ color: 0x986542, roughness: 0.8 }),
woodDark: new THREE.MeshStandardMaterial({ color: 0x684832, roughness: 0.86 }),
metal: new THREE.MeshStandardMaterial({
color: 0xc9b06c,
roughness: 0.3,
metalness: 0.68
}),
metalDark: new THREE.MeshStandardMaterial({
color: 0x3e4b50,
roughness: 0.4,
metalness: 0.55
}),
black: new THREE.MeshStandardMaterial({ color: 0x172127, roughness: 0.72 }),
white: new THREE.MeshStandardMaterial({ color: 0xf5f3e9, roughness: 0.66 }),
green: new THREE.MeshStandardMaterial({
color: 0x83d9a8,
roughness: 0.42,
emissive: 0x193e2a,
emissiveIntensity: 0.35
})
};
function box(
width,
height,
depth,
material,
parent = scene,
position = [0, 0, 0],
castShadow = true,
receiveShadow = true
) {
const mesh = new THREE.Mesh(
new THREE.BoxGeometry(width, height, depth),
material
);
mesh.position.set(...position);
mesh.castShadow = castShadow;
mesh.receiveShadow = receiveShadow;
parent.add(mesh);
return mesh;
}
// Floor.
const floor = new THREE.Group();
scene.add(floor);
const tileSize = 1.05;
for (let x = -7; x <= 7; x++) {
for (let z = -6; z <= 7; z++) {
box(
tileSize - 0.035,
0.12,
tileSize - 0.035,
(x + z) % 2 === 0 ? materials.floorA : materials.floorB,
floor,
[x * tileSize, -0.08, z * tileSize],
false,
true
);
}
}
// Walls and tiled backsplash.
box(18, 7, 0.18, materials.wall, scene, [0, 3.4, -2.1], false, true);
box(0.18, 7, 13, materials.wall, scene, [-7.4, 3.4, 3.5], false, true);
for (let row = 0; row < 5; row++) {
for (let col = 0; col < 11; col++) {
const tileMaterial = (row + col) % 4 === 0
? materials.tileAlt
: materials.tile;
box(
0.53,
0.31,
0.055,
tileMaterial,
scene,
[-5.6 + col * 0.55, 2.63 + row * 0.34, -1.96],
false,
true
);
}
}
// Base cabinet.
const cabinetRoot = new THREE.Group();
cabinetRoot.position.set(-1.15, 0, 0);
scene.add(cabinetRoot);
box(4.6, 2.25, 1.2, materials.cabinetDark, cabinetRoot, [0, 1.18, 0]);
box(4.85, 0.18, 1.42, materials.counter, cabinetRoot, [0, 2.4, 0]);
box(4.58, 0.15, 0.12, materials.cabinet, cabinetRoot, [0, 0.18, 0.63]);
// Left cabinet door and inset panel details.
box(1.45, 1.95, 0.12, materials.cabinet, cabinetRoot, [-1.46, 1.23, 0.63]);
box(0.34, 0.08, 0.09, materials.metalDark, cabinetRoot, [-1.02, 1.3, 0.73]);
box(1.45, 0.62, 0.12, materials.cabinet, cabinetRoot, [-1.46, 0.5, 0.63]);
box(0.34, 0.08, 0.09, materials.metalDark, cabinetRoot, [-1.02, 0.58, 0.73]);
// The repair drawer.
const drawerGroup = new THREE.Group();
drawerGroup.position.set(0.4, 1.35, 0.61);
cabinetRoot.add(drawerGroup);
box(3.7, 1.04, 0.14, materials.drawer, drawerGroup, [0, 0, 0]);
box(3.25, 0.76, 0.025, materials.cabinet, drawerGroup, [0, 0, 0.078]);
const screwMaterial = new THREE.MeshStandardMaterial({
color: 0x657174,
roughness: 0.35,
metalness: 0.75,
emissive: 0x000000
});
const screwLeft = box(
0.08,
0.08,
0.035,
screwMaterial,
drawerGroup,
[-0.27, 0, 0.105]
);
const screwRight = box(
0.08,
0.08,
0.035,
screwMaterial,
drawerGroup,
[0.27, 0, 0.105]
);
// Countertop objects: cutting board, jar, bowl and faucet.
box(0.85, 0.055, 0.56, materials.wood, cabinetRoot, [0.7, 2.52, 0.06]);
box(0.38, 0.52, 0.38, materials.white, cabinetRoot, [1.62, 2.76, -0.2]);
box(0.42, 0.08, 0.42, materials.woodDark, cabinetRoot, [1.62, 3.05, -0.2]);
box(0.72, 0.15, 0.42, materials.tileAlt, cabinetRoot, [-1.72, 2.57, 0.05]);
box(0.09, 0.62, 0.09, materials.metalDark, cabinetRoot, [-1.65, 2.85, -0.34]);
box(0.09, 0.09, 0.48, materials.metalDark, cabinetRoot, [-1.65, 3.15, -0.12]);
box(0.13, 0.18, 0.13, materials.metalDark, cabinetRoot, [-1.65, 3.03, 0.1]);
// Upper cabinets and shelf.
box(2.35, 1.25, 0.58, materials.cabinet, scene, [-3.3, 4.18, -1.63]);
box(1.55, 1.25, 0.58, materials.cabinet, scene, [-0.88, 4.18, -1.63]);
box(2.12, 0.11, 0.55, materials.wood, scene, [1.22, 4.05, -1.67]);
for (let i = 0; i < 5; i++) {
box(
0.27,
0.38 + (i % 2) * 0.1,
0.27,
i % 2 ? materials.tileAlt : materials.white,
scene,
[0.42 + i * 0.39, 4.29 + (i % 2) * 0.05, -1.61]
);
}
// Workbench.
const bench = new THREE.Group();
bench.position.set(2.72, 0, 0.35);
scene.add(bench);
box(1.85, 0.16, 1.12, materials.wood, bench, [0, 1.03, 0]);
box(0.16, 1.0, 0.16, materials.woodDark, bench, [-0.72, 0.5, -0.36]);
box(0.16, 1.0, 0.16, materials.woodDark, bench, [0.72, 0.5, -0.36]);
box(0.16, 1.0, 0.16, materials.woodDark, bench, [-0.72, 0.5, 0.36]);
box(0.16, 1.0, 0.16, materials.woodDark, bench, [0.72, 0.5, 0.36]);
// Toolbox.
box(0.56, 0.22, 0.34, materials.cabinetDark, bench, [0.52, 1.22, -0.08]);
box(0.26, 0.09, 0.08, materials.metalDark, bench, [0.52, 1.38, -0.08]);
box(0.42, 0.045, 0.16, materials.metal, bench, [0.48, 1.35, 0.1]);
function makeHandle() {
const group = new THREE.Group();
box(0.76, 0.13, 0.13, materials.metal, group, [0, 0, 0]);
box(0.12, 0.13, 0.28, materials.metal, group, [-0.27, 0, -0.145]);
box(0.12, 0.13, 0.28, materials.metal, group, [0.27, 0, -0.145]);
box(0.19, 0.17, 0.08, materials.metalDark, group, [-0.27, 0, -0.275]);
box(0.19, 0.17, 0.08, materials.metalDark, group, [0.27, 0, -0.275]);
return group;
}
const looseHandle = makeHandle();
const referenceHandle = makeHandle();
scene.add(looseHandle, referenceHandle);
const referenceBenchPosition = new THREE.Vector3(2.48, 1.21, 0.59);
const comparisonOldPosition = new THREE.Vector3(0.36, 1.82, 1.46);
const comparisonReferencePosition = new THREE.Vector3(0.88, 1.82, 1.46);
const carryPosition = new THREE.Vector3(0.36, 1.82, 1.46);
referenceHandle.position.copy(referenceBenchPosition);
referenceHandle.rotation.set(0.04, 0.28, 0.08);
// Small voxel check marks used during comparison.
const matchGroup = new THREE.Group();
scene.add(matchGroup);
const checkMaterial = new THREE.MeshBasicMaterial({ color: 0x8fe1b4 });
for (const x of [-0.09, 0, 0.09]) {
box(
0.055,
0.11,
0.055,
checkMaterial,
matchGroup,
[x, 2.13, 1.46],
false,
false
);
}
// Voxel character.
const character = new THREE.Group();
scene.add(character);
const skin = new THREE.MeshStandardMaterial({ color: 0xd99d72, roughness: 0.86 });
const shirt = new THREE.MeshStandardMaterial({ color: 0xe78b45, roughness: 0.82 });
const overalls = new THREE.MeshStandardMaterial({ color: 0x315e78, roughness: 0.8 });
const shoe = new THREE.MeshStandardMaterial({ color: 0x263239, roughness: 0.88 });
const hair = new THREE.MeshStandardMaterial({ color: 0x3a2925, roughness: 0.94 });
box(0.92, 0.86, 0.52, shirt, character, [0, 1.48, 0]);
box(0.69, 0.62, 0.08, overalls, character, [0, 1.27, 0.285]);
box(0.18, 0.45, 0.075, overalls, character, [-0.27, 1.6, 0.285]);
box(0.18, 0.45, 0.075, overalls, character, [0.27, 1.6, 0.285]);
box(0.85, 0.28, 0.48, overalls, character, [0, 0.98, 0]);
box(0.62, 0.62, 0.58, skin, character, [0, 2.26, 0]);
box(0.66, 0.18, 0.62, hair, character, [0, 2.53, -0.015]);
box(0.19, 0.36, 0.18, hair, character, [-0.27, 2.44, -0.12]);
box(0.09, 0.09, 0.045, materials.black, character, [-0.14, 2.31, 0.305]);
box(0.09, 0.09, 0.045, materials.black, character, [0.14, 2.31, 0.305]);
box(0.09, 0.05, 0.055, skin, character, [0, 2.2, 0.32]);
box(0.18, 0.035, 0.035, materials.black, character, [0, 2.08, 0.305]);
const legLeft = new THREE.Group();
const legRight = new THREE.Group();
legLeft.position.set(-0.24, 0.91, 0);
legRight.position.set(0.24, 0.91, 0);
character.add(legLeft, legRight);
box(0.31, 0.69, 0.34, overalls, legLeft, [0, -0.34, 0]);
box(0.31, 0.69, 0.34, overalls, legRight, [0, -0.34, 0]);
box(0.36, 0.2, 0.55, shoe, legLeft, [0, -0.79, 0.09]);
box(0.36, 0.2, 0.55, shoe, legRight, [0, -0.79, 0.09]);
function makeArm(side) {
const shoulderX = side === "left" ? -0.48 : 0.48;
const upper = box(
0.24,
1,
0.24,
shirt,
character,
[shoulderX, 1.7, 0]
);
const lower = box(
0.21,
1,
0.21,
skin,
character,
[shoulderX, 1.2, 0]
);
const hand = new THREE.Group();
hand.position.set(shoulderX, 0.8, 0);
character.add(hand);
box(0.25, 0.23, 0.23, skin, hand, [0, 0, 0]);
box(0.09, 0.16, 0.1, skin, hand, [side === "left" ? -0.14 : 0.14, 0.03, 0]);
return {
side,
shoulder: new THREE.Vector3(shoulderX, 1.77, 0),
upper,
lower,
hand
};
}
const leftArm = makeArm("left");
const rightArm = makeArm("right");
const yAxis = new THREE.Vector3(0, 1, 0);
function setSegment(mesh, start, end) {
const direction = new THREE.Vector3().subVectors(end, start);
const length = direction.length();
mesh.position.copy(start).add(end).multiplyScalar(0.5);
mesh.quaternion.setFromUnitVectors(yAxis, direction.normalize());
mesh.scale.set(1, length, 1);
}
function setArmTarget(arm, targetWorld) {
character.updateMatrixWorld(true);
const targetLocal = character.worldToLocal(targetWorld.clone());
const shoulder = arm.shoulder.clone();
const rawDirection = new THREE.Vector3().subVectors(targetLocal, shoulder);
const distance = THREE.MathUtils.clamp(rawDirection.length(), 0.18, 1.42);
const direction = rawDirection.normalize();
const upperLength = 0.7;
const lowerLength = 0.72;
const cosine = THREE.MathUtils.clamp(
(upperLength * upperLength + distance * distance - lowerLength * lowerLength) /
(2 * upperLength * distance),
-1,
1
);
const angle = Math.acos(cosine);
const bendDirection = new THREE.Vector3(0, 0, 1);
bendDirection.addScaledVector(direction, -bendDirection.dot(direction));
if (bendDirection.lengthSq() < 0.001) {
bendDirection.set(1, 0, 0);
bendDirection.addScaledVector(direction, -bendDirection.dot(direction));
}
bendDirection.normalize();
const elbow = shoulder
.clone()
.addScaledVector(direction, Math.cos(angle) * upperLength)
.addScaledVector(bendDirection, Math.sin(angle) * upperLength);
const actualTarget = shoulder.clone().addScaledVector(direction, distance);
setSegment(arm.upper, shoulder, elbow);
setSegment(arm.lower, elbow, actualTarget);
arm.hand.position.copy(actualTarget);
}
// Completion sparkles.
const sparkleMaterial = new THREE.MeshBasicMaterial({ color: 0x9ff0bd });
const sparkles = [];
for (let i = 0; i < 12; i++) {
const angle = (i / 12) * Math.PI * 2;
const sparkle = box(
0.07,
0.07,
0.07,
sparkleMaterial,
scene,
[0, 0, 0],
false,
false
);
sparkle.visible = false;
sparkles.push({ mesh: sparkle, angle });
}
const clock = new THREE.Clock();
const duration = 18;
let animationTime = 0;
let playing = true;
let lastStep = -1;
const playPauseButton = document.querySelector("#play-pause");
const replayButton = document.querySelector("#replay");
const timeline = document.querySelector("#timeline");
const currentTime = document.querySelector("#current-time");
const progressFill = document.querySelector("#progress-fill");
const stepNumber = document.querySelector("#step-number");
const stepTitle = document.querySelector("#step-title");
const stepIcon = document.querySelector("#step-icon");
playPauseButton.addEventListener("click", () => {
if (animationTime >= duration) {
animationTime = 0;
playing = true;
} else {
playing = !playing;
}
playPauseButton.textContent = playing ? "Pause" : "Play";
});
replayButton.addEventListener("click", () => {
animationTime = 0;
playing = true;
lastStep = -1;
playPauseButton.textContent = "Pause";
});
timeline.addEventListener("input", () => {
animationTime = Number(timeline.value);
playing = false;
lastStep = -1;
playPauseButton.textContent = "Play";
});
function smoothRange(value, start, end) {
const x = THREE.MathUtils.clamp((value - start) / (end - start), 0, 1);
return x * x * (3 - 2 * x);
}
function lerpVector(a, b, t) {
return a.clone().lerp(b, t);
}
function arcBetween(a, b, t, height = 0.35) {
const result = a.clone().lerp(b, t);
result.y += Math.sin(t * Math.PI) * height;
return result;
}
function mountedPosition() {
drawerGroup.updateMatrixWorld(true);
return drawerGroup.localToWorld(new THREE.Vector3(0, 0, 0.28));
}
function updateStep(t) {
let step;
if (t < 2) {
step = ["01", "Approach the drawer", "▣"];
} else if (t < 4.2) {
step = ["02", "Grasp the loose handle", "↗"];
} else if (t < 6.2) {
step = ["03", "Remove the loose handle", "↕"];
} else if (t < 8.4) {
step = ["04", "Match profile and hole spacing", "✓"];
} else if (t < 12) {
step = ["05", "Align and reattach the handle", "●"];
} else if (t < 15.3) {
step = ["06", "Test the repaired drawer", "↔"];
} else {
step = ["✓", "Repair complete", "★"];
}
if (step[0] !== String(lastStep)) {
stepNumber.textContent = `Step ${step[0]} / 06`;
stepTitle.textContent = step[1];
stepIcon.textContent = step[2];
lastStep = Number(step[0]) || 99;
}
}
function updateAnimation(t) {
const drawerOpen =
smoothRange(t, 12, 13.2) -
smoothRange(t, 14.3, 15.3);
drawerGroup.position.z = 0.61 + drawerOpen * 0.58;
const mount = mountedPosition();
// Character movement.
const approach = smoothRange(t, 0, 2);
const walking = t < 2 ? 1 - smoothRange(t, 1.55, 2) : 0;
character.position.set(
THREE.MathUtils.lerp(4.3, 0.95, approach),
Math.abs(Math.sin(t * 8)) * 0.025 * walking,
1.42
);
const stride = Math.sin(t * 8) * 0.46 * walking;
legLeft.rotation.x = stride;
legRight.rotation.x = -stride;
// Loose handle animation.
if (t < 4.2) {
const wobble = Math.sin(t * 7.2);
looseHandle.position.copy(mount);
looseHandle.position.x += wobble * 0.025;
looseHandle.rotation.set(
0.04 * wobble,
0.08 * Math.sin(t * 4.4),
0.13 * wobble
);
} else if (t < 6.2) {
const p = smoothRange(t, 4.2, 6.2);
looseHandle.position.copy(arcBetween(mount, carryPosition, p, 0.38));
looseHandle.rotation.set(
THREE.MathUtils.lerp(0.04, 0.12, p),
THREE.MathUtils.lerp(0.08, 0.42, p),
THREE.MathUtils.lerp(0.13, -0.2, p)
);
} else if (t < 8.4) {
looseHandle.position.copy(comparisonOldPosition);
looseHandle.rotation.set(0.05, 0.12, -0.08);
} else if (t < 9.3) {
const p = smoothRange(t, 8.4, 9.3);
looseHandle.position.copy(
lerpVector(comparisonOldPosition, new THREE.Vector3(0.1, 1.72, 1.37), p)
);
looseHandle.rotation.set(0.03, 0.04 * p, -0.04 * p);
} else if (t < 12) {
const p = smoothRange(t, 9.3, 11.55);
const target = mount.clone();
looseHandle.position.copy(arcBetween(
new THREE.Vector3(0.1, 1.72, 1.37),
target,
p,
0.24
));
const align = smoothRange(t, 10.2, 11.65);
looseHandle.rotation.set(
THREE.MathUtils.lerp(0.03, 0, align),
THREE.MathUtils.lerp(0.04, 0, align),
THREE.MathUtils.lerp(-0.04, 0, align)
);
} else {
looseHandle.position.copy(mount);
looseHandle.rotation.set(0, 0, 0);
}
// Matching reference handle.
if (t < 6.2) {
referenceHandle.position.copy(referenceBenchPosition);
referenceHandle.rotation.set(0.04, 0.28, 0.08);
} else if (t < 8.4) {
const p = smoothRange(t, 6.2, 7.2);
referenceHandle.position.copy(
arcBetween(referenceBenchPosition, comparisonReferencePosition, p, 0.22)
);
referenceHandle.rotation.set(
THREE.MathUtils.lerp(0.04, 0, p),
THREE.MathUtils.lerp(0.28, 0.02, p),
THREE.MathUtils.lerp(0.08, 0, p)
);
} else {
const p = smoothRange(t, 8.4, 9.3);
referenceHandle.position.copy(
arcBetween(comparisonReferencePosition, referenceBenchPosition, p, 0.18)
);
referenceHandle.rotation.set(
0.04 * p,
THREE.MathUtils.lerp(0.02, 0.28, p),
0.08 * p
);
}
// Highlight the screws once the handle is seated.
const tightened = smoothRange(t, 11.4, 12.1);
screwMaterial.color.setRGB(
THREE.MathUtils.lerp(0.396, 0.30, tightened),
THREE.MathUtils.lerp(0.443, 0.78, tightened),
THREE.MathUtils.lerp(0.455, 0.52, tightened)
);
screwMaterial.emissive.setRGB(0, tightened * 0.13, tightened * 0.07);
// Match indicator.
matchGroup.visible = t >= 6.7 && t <= 8.55;
if (matchGroup.visible) {
const pulse = 1 + Math.sin(t * 10) * 0.08;
matchGroup.scale.setScalar(pulse);
matchGroup.position.y = Math.sin(t * 7) * 0.025;
}
// Arm targets.
character.updateMatrixWorld(true);
const idleLeft = character.localToWorld(new THREE.Vector3(-0.69, 0.91, 0.12));
const idleRight = character.localToWorld(new THREE.Vector3(0.69, 0.91, 0.12));
let leftTarget = idleLeft;
let rightTarget = idleRight;
if (t >= 1.7 && t < 15.3) {
leftTarget = looseHandle.position;
}
if (t >= 6.2 && t < 9.3) {
rightTarget = referenceHandle.position;
}
if (t < 1.7) {
leftTarget.y += Math.sin(t * 8 + Math.PI) * 0.28 * walking;
rightTarget.y += Math.sin(t * 8) * 0.28 * walking;
}
if (t >= 15.3) {
rightTarget = character.localToWorld(new THREE.Vector3(0.7, 2.14, 0.32));
rightArm.hand.rotation.z = -0.62;
rightArm.hand.rotation.x = 0.18;
} else {
rightArm.hand.rotation.z = 0;
rightArm.hand.rotation.x = 0;
}
if (t >= 8.4 && t < 12) {
rightTarget = character.localToWorld(new THREE.Vector3(0.62, 1.22, 0.42));
}
setArmTarget(leftArm, leftTarget);
setArmTarget(rightArm, rightTarget);
// Head and body settle.
const headLook = t >= 6.2 && t < 9.3 ? 0.1 : 0;
character.children.forEach((child) => {
if (child.geometry && child.geometry.type === "BoxGeometry") {
// No-op: retained to keep all character pieces in the same root group.
}
});
// Completion sparkle burst.
const sparkleProgress = smoothRange(t, 15.25, 15.8);
sparkles.forEach(({ mesh, angle }, index) => {
mesh.visible = t >= 15.25;
if (!mesh.visible) return;
const radius = 0.25 + sparkleProgress * (0.65 + (index % 3) * 0.1);
mesh.position.set(
mount.x + Math.cos(angle) * radius,
mount.y + 0.35 + Math.sin(angle) * radius * 0.6,
mount.z + Math.sin(angle) * radius
);
mesh.scale.setScalar(1 - sparkleProgress * 0.72);
mesh.rotation.set(t * 3, t * 4, angle);
});
updateStep(t);
timeline.value = String(t);
currentTime.textContent = t.toFixed(1);
progressFill.style.width = `${(t / duration) * 100}%`;
}
const looseLabel = document.querySelector("#loose-label");
const referenceLabel = document.querySelector("#reference-label");
const matchLabel = document.querySelector("#match-label");
function placeLabel(element, worldPosition, visible) {
if (!visible) {
element.style.opacity = "0";
return;
}
const projected = worldPosition.clone().project(camera);
const inView =
projected.z > -1 &&
projected.z < 1 &&
Math.abs(projected.x) < 1.15 &&
Math.abs(projected.y) < 1.15;
if (!inView) {
element.style.opacity = "0";
return;
}
const x = (projected.x * 0.5 + 0.5) * window.innerWidth;
const y = (-projected.y * 0.5 + 0.5) * window.innerHeight;
element.style.left = `${x}px`;
element.style.top = `${y}px`;
element.style.opacity = "1";
}
function updateLabels() {
const looseVisible = animationTime >= 1.2 && animationTime < 12.2;
const referenceVisible = animationTime >= 5.8 && animationTime < 9.5;
placeLabel(
looseLabel,
looseHandle.position.clone().add(new THREE.Vector3(0, 0.38, 0)),
looseVisible
);
placeLabel(
referenceLabel,
referenceHandle.position.clone().add(new THREE.Vector3(0, 0.36, 0)),
referenceVisible
);
placeLabel(
matchLabel,
new THREE.Vector3(0.62, 2.23, 1.46),
animationTime >= 6.9 && animationTime <= 8.45
);
}
function onResize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
}
window.addEventListener("resize", onResize);
loading.style.display = "none";
function animate() {
requestAnimationFrame(animate);
const delta = Math.min(clock.getDelta(), 0.05);
if (playing) {
animationTime = Math.min(duration, animationTime + delta);
if (animationTime >= duration) {
playing = false;
playPauseButton.textContent = "Replay";
}
}
updateAnimation(animationTime);
updateLabels();
renderer.render(scene, camera);
}
animate();
</script>
</body>
</html>
```