Prompt specification
Build or customize the Three.js 3D scene "Lost-button repair story" 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: Lost-button repair story
Category: Everyday & Industrial · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/lost-button-repair-story-x0572
Scene Description Prompt:
Please use Three.js from cdnjs for a low-poly scene based on this idea: lost-button repair story. Create a small bedroom-to-repair-table sequence where a missing coat button is matched and reattached; the final closure should align with the unchanged neighboring buttons. Return one self-contained HTML file.
Working Three.js Starter (lost-button-repair-story-x0572):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta name="theme-color" content="#e9dfd0" />
<title>The Missing Button — A Small Repair Story</title>
<style>
:root {
--ink: #302d28;
--muted: #777066;
--paper: rgba(250, 247, 239, 0.91);
--line: rgba(57, 48, 38, 0.16);
--accent: #b76743;
--green: #557c69;
--cream: #f4efe4;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #e9dfd0;
color: var(--ink);
font-family: Inter, ui-sans-serif, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}
canvas {
position: fixed;
inset: 0;
width: 100%;
height: 100%;
display: block;
touch-action: none;
outline: none;
}
button {
font: inherit;
}
#interface {
position: fixed;
inset: 0;
z-index: 5;
pointer-events: none;
}
.masthead {
position: absolute;
top: 28px;
left: 32px;
max-width: 470px;
text-shadow: 0 1px 18px rgba(255, 255, 255, 0.46);
}
.eyebrow {
display: flex;
align-items: center;
gap: 9px;
margin-bottom: 9px;
color: var(--accent);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.19em;
text-transform: uppercase;
}
.eyebrow::before {
width: 26px;
height: 2px;
content: "";
background: currentColor;
}
h1 {
margin: 0;
font-family: Georgia, "Times New Roman", serif;
font-size: clamp(38px, 4.5vw, 67px);
font-weight: 400;
line-height: 0.95;
letter-spacing: -0.045em;
}
h1 em {
color: var(--accent);
font-weight: 400;
}
.masthead p {
margin: 12px 0 0;
color: #625c52;
font-size: 13px;
letter-spacing: 0.015em;
}
.controls {
position: absolute;
top: 28px;
right: 30px;
display: flex;
gap: 8px;
pointer-events: auto;
}
.control {
min-width: 76px;
height: 38px;
padding: 0 15px;
border: 1px solid var(--line);
border-radius: 999px;
background: rgba(250, 247, 239, 0.78);
color: var(--ink);
cursor: pointer;
font-size: 10px;
font-weight: 800;
letter-spacing: 0.13em;
text-transform: uppercase;
backdrop-filter: blur(12px);
transition: transform 160ms ease, background 160ms ease;
}
.control:hover {
transform: translateY(-2px);
background: #fffaf0;
}
.control:focus-visible,
.chapter:focus-visible {
outline: 2px solid var(--accent);
outline-offset: 3px;
}
#status-pill {
position: absolute;
top: 80px;
right: 30px;
display: flex;
align-items: center;
gap: 8px;
padding: 8px 12px;
border: 1px solid var(--line);
border-radius: 999px;
background: rgba(250, 247, 239, 0.72);
color: #6a6257;
font-size: 9px;
font-weight: 800;
letter-spacing: 0.15em;
text-transform: uppercase;
backdrop-filter: blur(12px);
transition: color 300ms ease, border-color 300ms ease;
}
#status-pill::before {
width: 7px;
height: 7px;
border-radius: 50%;
background: var(--accent);
content: "";
box-shadow: 0 0 0 4px rgba(183, 103, 67, 0.12);
transition: background 300ms ease, box-shadow 300ms ease;
}
#status-pill.complete {
color: var(--green);
border-color: rgba(85, 124, 105, 0.34);
}
#status-pill.complete::before {
background: var(--green);
box-shadow: 0 0 0 4px rgba(85, 124, 105, 0.13);
}
.evidence {
position: absolute;
top: 132px;
right: 30px;
width: 188px;
padding: 15px 16px 14px;
border: 1px solid var(--line);
border-radius: 16px;
background: rgba(250, 247, 239, 0.72);
box-shadow: 0 14px 40px rgba(75, 59, 39, 0.08);
backdrop-filter: blur(14px);
}
.evidence h3 {
margin: 0 0 11px;
color: var(--muted);
font-size: 9px;
font-weight: 800;
letter-spacing: 0.17em;
text-transform: uppercase;
}
.check {
display: flex;
align-items: center;
justify-content: space-between;
min-height: 27px;
border-top: 1px solid rgba(57, 48, 38, 0.08);
color: #91897d;
font-size: 10px;
transition: color 300ms ease;
}
.check:first-of-type {
border-top: 0;
}
.check span {
display: flex;
align-items: center;
gap: 8px;
letter-spacing: 0.1em;
text-transform: uppercase;
}
.check span::before {
width: 7px;
height: 7px;
border: 1px solid #aaa195;
border-radius: 50%;
content: "";
transition: background 300ms ease, border-color 300ms ease;
}
.check b {
font-weight: 700;
}
.check.good {
color: var(--green);
}
.check.good span::before {
border-color: var(--green);
background: var(--green);
box-shadow: 0 0 0 3px rgba(85, 124, 105, 0.12);
}
#caption {
position: absolute;
bottom: 34px;
left: 30px;
width: min(510px, calc(100vw - 60px));
display: grid;
grid-template-columns: 46px 1fr;
gap: 15px;
padding: 20px 23px 21px 19px;
border: 1px solid var(--line);
border-radius: 20px;
background: var(--paper);
box-shadow: 0 20px 60px rgba(66, 49, 30, 0.12);
backdrop-filter: blur(18px);
transition: border-color 400ms ease, background 400ms ease;
}
#caption.complete {
border-color: rgba(85, 124, 105, 0.38);
background: rgba(244, 248, 240, 0.93);
}
.chapter-number {
display: grid;
width: 40px;
height: 40px;
place-items: center;
border: 1px solid rgba(183, 103, 67, 0.25);
border-radius: 50%;
color: var(--accent);
font-family: Georgia, "Times New Roman", serif;
font-size: 15px;
transition: color 400ms ease, border-color 400ms ease;
}
.complete .chapter-number {
color: var(--green);
border-color: rgba(85, 124, 105, 0.35);
}
#chapter-kicker {
display: block;
margin: 1px 0 4px;
color: var(--accent);
font-size: 9px;
font-weight: 800;
letter-spacing: 0.17em;
text-transform: uppercase;
}
#caption h2 {
margin: 0;
font-family: Georgia, "Times New Roman", serif;
font-size: 25px;
font-weight: 400;
line-height: 1.1;
letter-spacing: -0.02em;
}
#chapter-copy {
margin: 8px 0 0;
color: #6d655a;
font-size: 12px;
line-height: 1.55;
}
#chapters {
position: absolute;
right: 30px;
bottom: 35px;
display: flex;
gap: 7px;
pointer-events: auto;
}
.chapter {
position: relative;
width: 39px;
height: 39px;
padding: 0;
border: 1px solid var(--line);
border-radius: 12px;
background: rgba(250, 247, 239, 0.78);
color: #81786c;
cursor: pointer;
font-size: 10px;
font-weight: 800;
backdrop-filter: blur(12px);
transition: transform 160ms ease, color 160ms ease, background 160ms ease;
}
.chapter:hover {
transform: translateY(-3px);
background: #fffaf0;
}
.chapter.active {
border-color: var(--accent);
background: var(--accent);
color: white;
}
.chapter:last-child.active {
border-color: var(--green);
background: var(--green);
}
#gesture-hint {
position: absolute;
right: 32px;
bottom: 88px;
color: rgba(65, 57, 47, 0.65);
font-size: 9px;
font-weight: 700;
letter-spacing: 0.12em;
text-transform: uppercase;
text-shadow: 0 1px 8px rgba(255, 255, 255, 0.8);
}
#timeline {
position: absolute;
right: 0;
bottom: 0;
left: 0;
height: 4px;
background: rgba(69, 57, 43, 0.1);
}
#progress {
width: 100%;
height: 100%;
transform: scaleX(0);
transform-origin: left;
background: var(--accent);
transition: background 400ms ease;
}
#loading {
position: fixed;
inset: 0;
z-index: 20;
display: grid;
place-items: center;
background: #e9dfd0;
color: #766c5f;
font-size: 10px;
font-weight: 800;
letter-spacing: 0.2em;
text-transform: uppercase;
transition: opacity 500ms ease;
}
#loading.out {
opacity: 0;
pointer-events: none;
}
@media (max-width: 760px) {
.masthead {
top: 18px;
left: 18px;
}
.masthead p,
.evidence,
#gesture-hint {
display: none;
}
.controls {
top: 18px;
right: 16px;
}
.control {
min-width: 64px;
height: 34px;
padding: 0 12px;
font-size: 9px;
}
#status-pill {
top: 62px;
right: 16px;
}
#caption {
right: 14px;
bottom: 68px;
left: 14px;
width: auto;
grid-template-columns: 38px 1fr;
gap: 12px;
padding: 15px 16px 16px 14px;
border-radius: 16px;
}
.chapter-number {
width: 34px;
height: 34px;
}
#caption h2 {
font-size: 21px;
}
#chapter-copy {
font-size: 11px;
}
#chapters {
right: 14px;
bottom: 17px;
left: 14px;
justify-content: center;
}
.chapter {
width: 36px;
height: 36px;
border-radius: 10px;
}
}
@media (prefers-reduced-motion: reduce) {
*,
*::before,
*::after {
scroll-behavior: auto !important;
}
}
</style>
</head>
<body>
<canvas id="scene" aria-label="Animated low-poly bedroom and repair-table story about finding and reattaching a missing coat button"></canvas>
<div id="interface">
<header class="masthead">
<div class="eyebrow">A small object story</div>
<h1>The missing<br /><em>button.</em></h1>
<p>A bedroom, a workbench, and one very careful repair.</p>
</header>
<div class="controls">
<button class="control" id="play-button" type="button">Pause</button>
<button class="control" id="reset-button" type="button">Reset</button>
</div>
<div id="status-pill">Missing</div>
<aside class="evidence" aria-label="Button matching evidence">
<h3>Match notes</h3>
<div class="check" id="check-holes"><span>Holes</span><b>—</b></div>
<div class="check" id="check-rim"><span>Rim</span><b>—</b></div>
<div class="check" id="check-size"><span>Size</span><b>—</b></div>
<div class="check" id="check-space"><span>Spacing</span><b>—</b></div>
</aside>
<section id="caption" aria-live="polite">
<div class="chapter-number" id="chapter-number">01</div>
<div>
<span id="chapter-kicker">The empty place</span>
<h2 id="chapter-title">One space in a row.</h2>
<p id="chapter-copy">Three button positions should follow one quiet line. One small place is missing from the coat.</p>
</div>
</section>
<nav id="chapters" aria-label="Story chapters">
<button class="chapter active" type="button" data-time="0" aria-label="Chapter 1: The empty place">01</button>
<button class="chapter" type="button" data-time="4" aria-label="Chapter 2: The lost one">02</button>
<button class="chapter" type="button" data-time="8.5" aria-label="Chapter 3: The repair table">03</button>
<button class="chapter" type="button" data-time="13" aria-label="Chapter 4: Match the memory">04</button>
<button class="chapter" type="button" data-time="18.5" aria-label="Chapter 5: Reattach">05</button>
<button class="chapter" type="button" data-time="24" aria-label="Chapter 6: Aligned">06</button>
</nav>
<div id="gesture-hint">Drag to orbit · Scroll to zoom · Space to pause</div>
<div id="timeline"><div id="progress"></div></div>
</div>
<div id="loading">Preparing the little room…</div>
<script type="module">
import * as THREE from "https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const canvas = document.querySelector("#scene");
const renderer = new THREE.WebGLRenderer({
canvas,
antialias: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight, false);
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.08;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xe9dfd0);
scene.fog = new THREE.Fog(0xe9dfd0, 17, 31);
const camera = new THREE.PerspectiveCamera(
36,
window.innerWidth / window.innerHeight,
0.1,
80
);
const hemisphere = new THREE.HemisphereLight(0xfff4df, 0x806d5e, 1.8);
scene.add(hemisphere);
const sun = new THREE.DirectionalLight(0xffe6c1, 3.25);
sun.position.set(-4.5, 10, 7);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -12;
sun.shadow.camera.right = 12;
sun.shadow.camera.top = 10;
sun.shadow.camera.bottom = -10;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 30;
sun.shadow.bias = -0.00035;
scene.add(sun);
const fill = new THREE.DirectionalLight(0xc8dce2, 0.85);
fill.position.set(8, 5, -7);
scene.add(fill);
const roomLight = new THREE.PointLight(0xffc979, 18, 7, 2);
roomLight.position.set(-7.8, 2.7, -0.4);
scene.add(roomLight);
const deskGlow = new THREE.PointLight(0xffd9a0, 24, 6, 2);
deskGlow.position.set(7.8, 3.1, -0.6);
scene.add(deskGlow);
const Y_AXIS = new THREE.Vector3(0, 1, 0);
const flatQuaternion = new THREE.Quaternion().setFromEuler(
new THREE.Euler(-Math.PI / 2, 0, 0)
);
function material(color, options = {}) {
return new THREE.MeshStandardMaterial({
color,
roughness: 0.82,
metalness: 0,
flatShading: true,
...options
});
}
const materials = {
ground: material(0xd7cbbb),
platform: material(0xcab9a4),
wall: material(0xeadfcd),
wallSide: material(0xd5c5af),
wood: material(0x80573d),
darkWood: material(0x583b2d),
cream: material(0xf0e7d6),
blue: material(0x8eaaa9),
blueDark: material(0x607d7c),
pink: material(0xc68f77),
coat: material(0xa86643),
coatDark: material(0x8f5137),
seam: material(0x7c4934),
mat: material(0x385d55),
tray: material(0xc8b292),
metal: material(0x777b7b, { metalness: 0.65, roughness: 0.35 }),
creamButton: material(0xd6b47e),
buttonRim: material(0xaa7749),
hole: material(0x4b382d),
accent: material(0xc36f48),
green: material(0x628771),
thread: new THREE.LineBasicMaterial({
color: 0xf1c295,
transparent: true,
opacity: 0.9
}),
missing: new THREE.MeshBasicMaterial({
color: 0xd87950,
transparent: true,
opacity: 0.78,
depthWrite: false
})
};
function mesh(geometry, mat, x = 0, y = 0, z = 0, parent = scene) {
const object = new THREE.Mesh(geometry, mat);
object.position.set(x, y, z);
object.castShadow = true;
object.receiveShadow = true;
parent.add(object);
return object;
}
function box(parent, width, height, depth, x, y, z, mat) {
return mesh(new THREE.BoxGeometry(width, height, depth), mat, x, y, z, parent);
}
function cylinder(parent, top, bottom, height, segments, x, y, z, mat) {
return mesh(
new THREE.CylinderGeometry(top, bottom, height, segments),
mat,
x,
y,
z,
parent
);
}
function rodBetween(parent, start, end, radius, mat, segments = 8) {
const direction = new THREE.Vector3().subVectors(end, start);
const object = mesh(
new THREE.CylinderGeometry(radius, radius, direction.length(), segments),
mat,
0,
0,
0,
parent
);
object.position.copy(start).add(end).multiplyScalar(0.5);
object.quaternion.setFromUnitVectors(Y_AXIS, direction.normalize());
return object;
}
function polygonGeometry(points, depth = 0.14) {
const shape = new THREE.Shape();
shape.moveTo(points[0][0], points[0][1]);
for (let i = 1; i < points.length; i++) {
shape.lineTo(points[i][0], points[i][1]);
}
shape.closePath();
return new THREE.ExtrudeGeometry(shape, {
depth,
bevelEnabled: false,
steps: 1,
curveSegments: 1
});
}
function createButton(color = 0xd6b47e, holes = 4, radius = 0.13) {
const group = new THREE.Group();
const bodyMaterial = material(color);
const rimMaterial = material(0xaa7749);
const holeMaterial = material(0x4b382d);
const geometry = new THREE.CylinderGeometry(radius, radius, 0.065, 12);
geometry.rotateX(Math.PI / 2);
mesh(geometry, bodyMaterial, 0, 0, 0, group);
mesh(
new THREE.TorusGeometry(radius * 0.79, 0.011, 4, 16),
rimMaterial,
0,
0,
0.038,
group
);
const holePositions = [];
if (holes === 4) {
holePositions.push(
[-0.034, -0.034], [0.034, -0.034],
[-0.034, 0.034], [0.034, 0.034]
);
} else if (holes === 3) {
holePositions.push([
0, 0.042,
-0.039, -0.026,
0.039, -0.026
]);
} else {
holePositions.push([[-0.038, 0], [0.038, 0]]);
}
holePositions.forEach(([x, y]) => {
mesh(
new THREE.CircleGeometry(0.018, 8),
holeMaterial,
x,
y,
0.035,
group
);
});
group.userData.materials = [bodyMaterial, rimMaterial, holeMaterial];
return group;
}
function setButtonOpacity(button, opacity) {
button.traverse((object) => {
if (!object.material) return;
object.material.transparent = opacity < 0.999;
object.material.opacity = opacity;
object.material.depthWrite = opacity > 0.45;
});
}
function createMissingMarker() {
const group = new THREE.Group();
const ring = mesh(
new THREE.TorusGeometry(0.151, 0.009, 4, 24),
materials.missing.clone(),
0,
0,
0,
group
);
const holeMaterial = material(0x75442f);
[-0.027, 0.027].forEach((x) => {
[-0.027, 0.027].forEach((y) => {
mesh(
new THREE.CircleGeometry(0.010, 6),
holeMaterial,
x,
y,
0,
group
);
});
});
group.userData.ring = ring;
return group;
}
function createCoat(withSleeves = true) {
const group = new THREE.Group();
const bodyPoints = [
[-0.78, -1.35], [0.78, -1.35], [0.9, 0.95],
[0.56, 1.48], [0, 1.7], [-0.56, 1.48], [-0.9, 0.95]
];
const bodyGeometry = polygonGeometry(bodyPoints, 0.14);
const body = mesh(bodyGeometry, materials.coat, 0, 0, 0, group);
const edgeMaterial = new THREE.LineBasicMaterial({
color: 0x6f3e2b,
transparent: true,
opacity: 0.32
});
const edges = new THREE.LineSegments(
new THREE.EdgesGeometry(bodyGeometry),
edgeMaterial
);
group.add(edges);
if (withSleeves) {
const leftSleeve = polygonGeometry([
[-0.54, 1.46], [-0.93, 1.25], [-1.14, 0.18],
[-0.87, 0.1], [-0.58, 0.88]
], 0.14);
const rightSleeve = polygonGeometry([
[0.54, 1.46], [0.93, 1.25], [1.14, 0.18],
[0.87, 0.1], [0.58, 0.88]
], 0.14);
mesh(leftSleeve, materials.coatDark, 0, 0, -0.005, group);
mesh(rightSleeve, materials.coatDark, 0, 0, -0.005, group);
}
const collarLeft = polygonGeometry([
[-0.04, 1.62], [-0.55, 1.4], [-0.14, 1.24]
], 0.04);
const collarRight = polygonGeometry([
[0.04, 1.62], [0.55, 1.4], [0.14, 1.24]
], 0.04);
mesh(collarLeft, materials.coatDark, 0, 0, 0.145, group);
mesh(collarRight, materials.coatDark, 0, 0, 0.145, group);
box(group, 0.15, 2.62, 0.025, 0, 0.13, 0.15, materials.coatDark);
const topButton = createButton();
const bottomButton = createButton();
topButton.position.set(0.05, 0.64, 0.205);
bottomButton.position.set(0.05, -0.4, 0.205);
group.add(topButton, bottomButton);
const missing = createMissingMarker();
missing.position.set(0.05, 0.12, 0.215);
group.add(missing);
return { group, topButton, bottomButton, missing };
}
// Shared ground and two small diorama platforms.
const ground = mesh(
new THREE.PlaneGeometry(60, 60),
materials.ground,
0,
-0.48,
0
);
ground.rotation.x = -Math.PI / 2;
ground.castShadow = false;
box(scene, 6.0, 0.28, 5.4, -6.2, -0.33, 0, materials.platform);
box(scene, 5.7, 0.26, 4.4, 6.25, -0.33, -0.1, materials.platform);
// Bedroom.
const bedroom = new THREE.Group();
scene.add(bedroom);
box(bedroom, 6.0, 4.6, 0.18, -6.2, 2.0, -2.55, materials.wall);
box(bedroom, 0.16, 4.6, 5.4, -9.12, 2.0, 0, materials.wallSide);
const sky = material(0xb8d4d1);
box(bedroom, 1.75, 1.58, 0.045, -7.1, 2.55, -2.42, sky);
box(bedroom, 1.93, 0.11, 0.12, -7.1, 3.38, -2.34, materials.cream);
box(bedroom, 1.93, 0.11, 0.12, -7.1, 1.72, -2.34, materials.cream);
box(bedroom, 0.11, 1.75, 0.12, -8.0, 2.55, -2.34, materials.cream);
box(bedroom, 0.11, 1.75, 0.12, -6.2, 2.55, -2.34, materials.cream);
box(bedroom, 0.07, 1.55, 0.09, -7.1, 2.55, -2.29, materials.cream);
const bed = new THREE.Group();
bed.position.set(-7.0, 0, 0.05);
bedroom.add(bed);
box(bed, 2.85, 0.48, 3.15, 0, 0.15, 0, materials.wood);
box(bed, 2.72, 0.25, 3.0, 0, 0.48, 0, materials.cream);
box(bed, 2.78, 0.16, 2.13, 0, 0.66, 0.4, materials.blue);
box(bed, 2.82, 0.11, 0.58, 0, 0.72, 1.14, materials.blueDark);
box(bed, 1.92, 0.25, 0.62, 0, 0.73, -1.0, materials.cream);
box(bed, 2.95, 1.18, 0.16, 0, 0.73, -1.52, materials.darkWood);
box(bed, 0.17, 0.42, 3.22, -1.4, -0.02, 0, materials.darkWood);
box(bed, 0.17, 0.42, 3.22, 1.4, -0.02, 0, materials.darkWood);
const rug = mesh(
new THREE.CircleGeometry(1.38, 16),
material(0xb78369),
-4.72,
-0.176,
1.22,
bedroom
);
rug.rotation.x = -Math.PI / 2;
rug.castShadow = false;
const nightstand = new THREE.Group();
nightstand.position.set(-8.65, 0, -1.25);
bedroom.add(nightstand);
box(nightstand, 0.95, 1.08, 0.7, 0, 0.34, 0, materials.wood);
box(nightstand, 1.05, 0.1, 0.78, 0, 0.91, 0, materials.darkWood);
box(nightstand, 0.78, 0.34, 0.06, 0, 0.52, 0.38, materials.cream);
cylinder(nightstand, 0.04, 0.04, 0.05, 8, 0, 0.52, 0.43, materials.metal).rotation.x = Math.PI / 2;
cylinder(nightstand, 0.24, 0.28, 0.07, 10, 0, 1.0, 0, materials.metal);
cylinder(nightstand, 0.055, 0.055, 0.58, 8, 0, 1.31, 0, materials.metal);
const shade = mesh(
new THREE.CylinderGeometry(0.25, 0.43, 0.5, 8, 1, true),
material(0xe1b77f),
0,
1.68,
0,
nightstand
);
shade.material.side = THREE.DoubleSide;
const rack = new THREE.Group();
rack.position.set(-4.2, 0, -0.76);
bedroom.add(rack);
cylinder(rack, 0.055, 0.055, 3.6, 8, 0, 1.58, 0, materials.darkWood);
box(rack, 1.28, 0.11, 0.13, 0, 3.33, 0, materials.darkWood);
box(rack, 1.15, 0.1, 0.13, 0, -0.07, 0, materials.darkWood);
cylinder(rack, 0.055, 0.055, 1.18, 8, 0, -0.07, 0, materials.darkWood).rotation.z = Math.PI / 2;
const bedroomCoat = createCoat(true);
bedroomCoat.group.position.set(-4.2, 1.54, -0.49);
bedroom.add(bedroomCoat.group);
// Repair workspace.
const workshop = new THREE.Group();
scene.add(workshop);
box(workshop, 4.7, 2.55, 0.14, 6.1, 2.73, -1.58, material(0xc9b598));
[-2.25, 2.25].forEach((x) => {
box(workshop, 0.13, 2.75, 0.18, 6.1 + x, 2.65, -1.62, materials.darkWood);
});
const pegHoleMaterial = material(0x9a826a);
for (let row = 0; row < 4; row++) {
for (let col = 0; col < 8; col++) {
const hole = mesh(
new THREE.CircleGeometry(0.022, 6),
pegHoleMaterial,
4.25 + col * 0.52,
1.78 + row * 0.52,
-1.495,
workshop
);
hole.castShadow = false;
}
}
const table = new THREE.Group();
workshop.add(table);
box(table, 5.0, 0.26, 2.65, 6.0, 1.18, 0.28, materials.wood);
box(table, 4.75, 0.38, 0.13, 6.0, 0.92, 1.48, materials.darkWood);
[-2.18, 2.18].forEach((x) => {
[-0.82, 1.38].forEach((z) => {
box(table, 0.19, 1.65, 0.19, 6.0 + x, 0.27, z, materials.darkWood);
});
});
box(table, 2.22, 0.025, 2.72, 5.25, 1.324, 0.39, materials.mat);
const tableCoat = createCoat(false);
tableCoat.group.position.set(5.15, 1.315, 0.4);
tableCoat.group.rotation.x = -Math.PI / 2;
tableCoat.group.scale.setScalar(0.78);
table.add(tableCoat.group);
const tray = new THREE.Group();
tray.position.set(7.25, 0, 0.3);
table.add(tray);
box(tray, 2.25, 0.055, 0.92, 0, 1.35, 0, materials.tray);
box(tray, 2.25, 0.12, 0.055, 0, 1.4, -0.46, materials.cream);
box(tray, 2.25, 0.12, 0.055, 0, 1.4, 0.46, materials.cream);
box(tray, 0.055, 0.12, 0.92, -1.12, 1.4, 0, materials.cream);
box(tray, 0.055, 0.12, 0.92, 1.12, 1.4, 0, materials.cream);
const correctHome = new THREE.Vector3(6.55, 1.425, 0.3);
const decoyHomes = [
new THREE.Vector3(7.08, 1.425, 0.48),
new THREE.Vector3(7.65, 1.425, 0.3),
new THREE.Vector3(8.18, 1.425, 0.48)
];
const lostButton = createButton(0xd6b47e, 4);
lostButton.position.copy(correctHome);
lostButton.quaternion.copy(flatQuaternion);
scene.add(lostButton);
const decoyButtons = [
createButton(0xa68a64, 2),
createButton(0xe1cfad, 3),
createButton(0x7f7569, 4)
];
decoyButtons.forEach((button, index) => {
button.position.copy(decoyHomes[index]);
button.quaternion.copy(flatQuaternion);
scene.add(button);
const sample = mesh(
new THREE.CylinderGeometry(0.19, 0.19, 0.012, 16),
material(index === 2 ? 0xb9a58c : 0xcabca6),
decoyHomes[index].x,
1.393,
decoyHomes[index].z
);
sample.castShadow = false;
});
function createSpool(x, z, color) {
const group = new THREE.Group();
group.position.set(x, 1.43, z);
table.add(group);
cylinder(group, 0.14, 0.14, 0.28, 10, 0, 0, 0, material(color));
cylinder(group, 0.17, 0.17, 0.04, 10, 0, 0.15, 0, materials.cream);
cylinder(group, 0.17, 0.17, 0.04, 10, 0, -0.15, 0, materials.cream);
}
createSpool(8.08, -0.67, 0xc47d59);
createSpool(8.48, -0.67, 0x6b897a);
const lamp = new THREE.Group();
lamp.position.set(8.13, 1.32, -1.0);
table.add(lamp);
cylinder(lamp, 0.24, 0.3, 0.07, 10, 0, 0.035, 0, materials.darkWood);
rodBetween(
lamp,
new THREE.Vector3(0, 0.1, 0),
new THREE.Vector3(-0.28, 0.9, 0.05),
0.035,
materials.metal
);
rodBetween(
lamp,
new THREE.Vector3(-0.28, 0.9, 0.05),
new THREE.Vector3(-0.85, 1.25, 0.16),
0.035,
materials.metal
);
const lampShade = mesh(
new THREE.CylinderGeometry(0.2, 0.4, 0.42, 8, 1, true),
material(0x53766b),
-0.85,
1.23,
0.16,
lamp
);
lampShade.material.side = THREE.DoubleSide;
const stool = new THREE.Group();
stool.position.set(6.05, 0, 1.95);
workshop.add(stool);
cylinder(stool, 0.52, 0.52, 0.13, 12, 0, 0.68, 0, materials.wood);
[-0.3, 0.3].forEach((x) => {
[-0.3, 0.3].forEach((z) => {
rodBetween(
stool,
new THREE.Vector3(x * 1.25, -0.16, z * 1.25),
new THREE.Vector3(x, 0.62, z),
0.035,
materials.darkWood
);
});
});
// Exact world-space positions used to guarantee final alignment.
tableCoat.group.updateMatrixWorld(true);
const finalPosition = tableCoat.group.localToWorld(
new THREE.Vector3(0.05, 0.12, 0.205)
);
const topPosition = tableCoat.group.localToWorld(
new THREE.Vector3(0.05, 0.64, 0.205)
);
const bottomPosition = tableCoat.group.localToWorld(
new THREE.Vector3(0.05, -0.4, 0.205)
);
const panelQuaternion = tableCoat.group.getWorldQuaternion(new THREE.Quaternion());
// A comparison ring appears beside the unchanged upper button.
const comparisonMaterial = new THREE.MeshBasicMaterial({
color: 0x6f9b82,
transparent: true,
opacity: 0,
depthWrite: false
});
const comparisonRing = mesh(
new THREE.TorusGeometry(0.18, 0.011, 4, 28),
comparisonMaterial,
0,
0,
0
);
comparisonRing.rotation.x = -Math.PI / 2;
comparisonRing.visible = false;
comparisonRing.castShadow = false;
const matchPosition = new THREE.Vector3(
topPosition.x + 0.67,
topPosition.y + 0.39,
topPosition.z
);
comparisonRing.position.copy(matchPosition);
// Subtle floor trail guides the eye from the bedroom to the workbench.
const startPosition = new THREE.Vector3(-4.72, -0.135, 1.22);
const trailCurve = new THREE.CatmullRomCurve3([
startPosition.clone(),
new THREE.Vector3(-3.35, -0.22, 1.62),
new THREE.Vector3(-1.15, -0.385, 2.08),
new THREE.Vector3(0.95, -0.385, 2.08)
]);
const travelCurve = new THREE.CatmullRomCurve3([
new THREE.Vector3(0.95, -0.385, 2.08),
new THREE.Vector3(2.55, -0.37, 1.86),
new THREE.Vector3(4.48, 0.36, 1.02),
new THREE.Vector3(5.85, 1.29, 0.55),
correctHome.clone()
]);
const trailDots = [];
for (let i = 0; i < 17; i++) {
const point = trailCurve.getPointAt(i / 16);
const dotMaterial = material(0xc4875f, {
transparent: true,
opacity: 0
});
const dot = mesh(
new THREE.SphereGeometry(0.025, 6, 4),
dotMaterial,
point.x,
point.y,
point.z
);
dot.castShadow = false;
trailDots.push(dot);
}
const looseHaloMaterial = new THREE.MeshBasicMaterial({
color: 0xf0d1a5,
transparent: true,
opacity: 0.5,
depthWrite: false
});
const looseHalo = mesh(
new THREE.TorusGeometry(0.19, 0.012, 4, 24),
looseHaloMaterial,
startPosition.x,
startPosition.y - 0.045,
startPosition.z
);
looseHalo.rotation.x = -Math.PI / 2;
looseHalo.visible = false;
looseHalo.castShadow = false;
// Needle and live thread.
const needleMaterial = material(0xaeb6b8, {
metalness: 0.75,
roughness: 0.26,
transparent: true,
opacity: 1
});
const needle = mesh(
new THREE.CylinderGeometry(0.011, 0.011, 0.48, 6),
needleMaterial,
0,
0,
0
);
needle.visible = false;
needle.castShadow = false;
const threadGeometry = new THREE.BufferGeometry();
threadGeometry.setAttribute(
"position",
new THREE.BufferAttribute(new Float32Array(9), 3)
);
const threadLine = new THREE.Line(threadGeometry, materials.thread.clone());
threadLine.visible = false;
scene.add(threadLine);
const spoolThreadPosition = new THREE.Vector3(8.05, 1.56, -0.66);
// Final alignment guide, drawn in the coat panel's local coordinates.
const alignmentGroup = new THREE.Group();
alignmentGroup.visible = false;
tableCoat.group.add(alignmentGroup);
const alignmentMaterial = new THREE.LineBasicMaterial({
color: 0x628f76,
transparent: true,
opacity: 0
});
const alignmentLine = new THREE.Line(
new THREE.BufferGeometry().setFromPoints([
new THREE.Vector3(0.05, 0.64, 0.29),
new THREE.Vector3(0.05, -0.4, 0.29)
]),
alignmentMaterial
);
alignmentLine.computeLineDistances();
alignmentGroup.add(alignmentLine);
const tickPoints = [];
[0.64, 0.12, -0.4].forEach((y) => {
tickPoints.push(
new THREE.Vector3(-0.06, y, 0.29),
new THREE.Vector3(0.16, y, 0.29)
);
});
alignmentGroup.add(new THREE.LineSegments(
new THREE.BufferGeometry().setFromPoints(tickPoints),
alignmentMaterial
));
const haloMaterials = [];
[0.64, 0.12, -0.4].forEach((y) => {
const haloMaterial = new THREE.MeshBasicMaterial({
color: 0x76a38c,
transparent: true,
opacity: 0,
depthWrite: false
});
const halo = mesh(
new THREE.TorusGeometry(0.158, 0.008, 4, 24),
haloMaterial,
0.05,
y,
0.295,
alignmentGroup
);
halo.castShadow = false;
haloMaterials.push(haloMaterial);
});
// Story timing and UI.
const chapters = [
{
start: 0,
end: 4,
kicker: "The empty place",
title: "One space in a row.",
copy: "Three button positions should follow one quiet line. One small place is missing from the coat.",
status: "Missing"
},
{
start: 4,
end: 8.5,
kicker: "The lost one",
title: "Beneath the hem, a circle waits.",
copy: "The fallen button is found on the bedroom rug. Its tiny rim catches the light.",
status: "Found"
},
{
start: 8.5,
end: 13,
kicker: "The repair table",
title: "Same button, new place.",
copy: "The search carries the button to a quiet workbench, where possibilities are laid out in a tray.",
status: "At the table"
},
{
start: 13,
end: 18.5,
kicker: "Match the memory",
title: "Four holes. One warm rim.",
copy: "Hole count, rim shape, and diameter all agree. This is the button that belongs.",
status: "Comparing"
},
{
start: 18.5,
end: 24,
kicker: "Reattach",
title: "The thread remembers the way.",
copy: "The needle passes through the familiar holes and draws the button back into the coat.",
status: "Sewing"
},
{
start: 24,
end: 32,
kicker: "Aligned",
title: "Three buttons, one straight line.",
copy: "The two neighboring buttons never moved. The restored button settles exactly between them.",
status: "Aligned"
}
];
const TOTAL = 32;
const reducedMotion = window.matchMedia("(prefers-reduced-motion: reduce)").matches;
let storyTime = 0;
let playing = !reducedMotion;
let currentChapter = -1;
let dragging = false;
let pointerX = 0;
let pointerY = 0;
const orbit = { yaw: 0, pitch: 0, zoom: 1 };
const cameraKeys = [
{ t: 0, p: [-2.15, 2.55, 4.85], look: [-4.18, 1.72, -0.25] },
{ t: 3.7, p: [-2.15, 2.55, 4.85], look: [-4.18, 1.72, -0.25] },
{ t: 4.7, p: [-0.45, 3.8, 8.45], look: [-3.05, 0.72, 0.55] },
{ t: 8.1, p: [-0.45, 3.8, 8.45], look: [-3.05, 0.72, 0.55] },
{ t: 10.6, p: [7.4, 4.45, 7.25], look: [6.05, 1.35, 0.28] },
{ t: 12.8, p: [7.4, 4.45, 7.25], look: [6.05, 1.35, 0.28] },
{ t: 15.2, p: [6.45, 5.65, 5.5], look: [6.0, 1.4, 0.27] },
{ t: 18.0, p: [6.45, 5.65, 5.5], look: [6.0, 1.4, 0.27] },
{ t: 19.6, p: [5.7, 4.05, 4.1], look: [5.3, 1.55, 0.3] },
{ t: 23.3, p: [5.7, 4.05, 4.1], look: [5.3, 1.55, 0.3] },
{ t: 25.1, p: [5.2, 6.75, 1.2], look: [5.2, 1.48, 0.31] },
{ t: 32, p: [5.2, 6.75, 1.2], look: [5.2, 1.48, 0.31] }
];
function smoothstep(value) {
const x = THREE.MathUtils.clamp(value, 0, 1);
return x * x * (3 - 2 * x);
}
function sampleCamera(time) {
let index = 0;
while (
index < cameraKeys.length - 2 &&
time > cameraKeys[index + 1].t
) {
index++;
}
const a = cameraKeys[index];
const b = cameraKeys[index + 1];
const progress = smoothstep((time - a.t) / (b.t - a.t));
const position = new THREE.Vector3(...a.p).lerp(
new THREE.Vector3(...b.p),
progress
);
const target = new THREE.Vector3(...a.look).lerp(
new THREE.Vector3(...b.look),
progress
);
return { position, target };
}
const desiredCameraPosition = new THREE.Vector3();
const desiredCameraTarget = new THREE.Vector3();
const actualCameraPosition = new THREE.Vector3(...cameraKeys[0].p);
const actualCameraTarget = new THREE.Vector3(...cameraKeys[0].look);
camera.position.copy(actualCameraPosition);
function updateStory(time) {
// Bedroom missing-position pulse.
const bedroomMarker = bedroomCoat.missing.userData.ring;
bedroomMarker.material.opacity = 0.56 + Math.sin(time * 3.2) * 0.16;
bedroomMarker.scale.setScalar(1 + Math.sin(time * 3.2) * 0.045);
// The button's movement from the rug to the repair tray.
if (time < 4) {
lostButton.position.copy(startPosition);
lostButton.quaternion.copy(flatQuaternion);
} else if (time < 8.5) {
const progress = smoothstep((time - 4) / 4.5);
lostButton.position.copy(trailCurve.getPointAt(progress));
const spin = new THREE.Quaternion().setFromAxisAngle(
new THREE.Vector3(0, 0, 1),
progress * Math.PI * 6
);
lostButton.quaternion.copy(flatQuaternion).multiply(spin);
} else if (time < 11.8) {
const progress = smoothstep((time - 8.5) / 3.3);
lostButton.position.copy(travelCurve.getPointAt(progress));
const spin = new THREE.Quaternion().setFromAxisAngle(
new THREE.Vector3(0, 0, 1),
progress * Math.PI * 5
);
lostButton.quaternion.copy(flatQuaternion).multiply(spin);
} else if (time < 13) {
lostButton.position.copy(correctHome);
lostButton.quaternion.copy(flatQuaternion);
}
looseHalo.visible = time >= 4 && time < 11.8;
looseHalo.position.set(
lostButton.position.x,
lostButton.position.y - 0.052,
lostButton.position.z
);
looseHalo.scale.setScalar(1 + Math.sin(time * 4) * 0.09);
looseHaloMaterial.opacity = 0.38 + Math.sin(time * 4) * 0.12;
trailDots.forEach((dot, index) => {
const visible = time >= 4 && time < 12.3;
dot.visible = visible;
if (visible) {
const reveal = THREE.MathUtils.clamp(
(time - 4) / 4.5 * 1.25 - index * 0.045,
0,
1
);
dot.material.opacity = reveal * 0.55;
}
});
// Match phase: lift and dismiss the three incorrect possibilities.
decoyButtons.forEach((button, index) => {
const home = decoyHomes[index];
let lift = 0;
let opacity = 1;
if (time >= 13) {
const matchProgress = THREE.MathUtils.clamp((time - 13) / 5.5, 0, 1);
const individual = THREE.MathUtils.clamp(
(matchProgress - index * 0.15) / 0.42,
0,
1
);
lift = Math.sin(individual * Math.PI) * 0.16;
opacity = 1 - smoothstep(individual) * 0.58;
}
button.position.set(home.x, home.y + lift, home.z);
setButtonOpacity(button, opacity);
});
const missingHover = finalPosition.clone().add(new THREE.Vector3(0, 0.48, 0));
if (time >= 13 && time < 18.5) {
const matchProgress = THREE.MathUtils.clamp((time - 13) / 5.5, 0, 1);
if (matchProgress < 0.52) {
const p = smoothstep(matchProgress / 0.52);
lostButton.position.lerpVectors(correctHome, matchPosition, p);
} else if (matchProgress < 0.74) {
lostButton.position.copy(matchPosition);
lostButton.position.y += Math.sin((matchProgress - 0.52) * 28) * 0.025;
} else {
const p = smoothstep((matchProgress - 0.74) / 0.26);
lostButton.position.lerpVectors(matchPosition, missingHover, p);
}
lostButton.quaternion.copy(flatQuaternion);
}
comparisonRing.visible = time >= 13.3 && time < 20.6;
if (comparisonRing.visible) {
const appear = smoothstep((time - 13.3) / 0.8);
const fade = 1 - smoothstep((time - 19.4) / 1.2);
comparisonMaterial.opacity = appear * fade * 0.88;
comparisonRing.scale.setScalar(1 + Math.sin(time * 4.2) * 0.055);
}
// Sewing and reattachment.
const tableMarkerMaterial = tableCoat.missing.userData.ring.material;
let markerVisibility = 1;
if (time >= 18.5 && time < 22) {
const descend = THREE.MathUtils.clamp((time - 20.4) / 1.6, 0, 1);
lostButton.position.lerpVectors(missingHover, finalPosition, smoothstep(descend));
const angle = (1 - smoothstep(descend)) * 0.1;
const settle = new THREE.Quaternion().setFromAxisAngle(
new THREE.Vector3(0, 0, 1),
angle
);
lostButton.quaternion.copy(flatQuaternion).multiply(settle);
markerVisibility = 1 - smoothstep(descend);
} else if (time >= 22) {
lostButton.position.copy(finalPosition);
lostButton.quaternion.copy(panelQuaternion);
markerVisibility = 0;
}
tableCoat.missing.userData.ring.material.opacity =
0.76 * markerVisibility + Math.sin(time * 3.4) * 0.08 * markerVisibility;
tableCoat.missing.userData.ring.scale.setScalar(
1 + Math.sin(time * 3.4) * 0.045 * markerVisibility
);
// Needle motion and thread path.
needle.visible = time >= 18.6 && time < 22.6;
threadLine.visible = needle.visible;
if (needle.visible) {
const holeA = missingHover.clone().add(new THREE.Vector3(-0.24, 0.08, -0.13));
const holeB = missingHover.clone().add(new THREE.Vector3(0.24, 0.08, 0.13));
let needlePosition;
if (time < 20.4) {
const stitch = (time - 18.6) / 1.8;
const travel = (1 - Math.cos(stitch * Math.PI * 4)) * 0.5;
needlePosition = holeA.clone().lerp(holeB, travel);
needlePosition.y += Math.sin(stitch * Math.PI * 4) * 0.035;
} else if (time < 22) {
const p = smoothstep((time - 20.4) / 1.6);
needlePosition = holeB.clone().lerp(missingHover, p);
} else {
const p = smoothstep((time - 22) / 0.6);
needlePosition = missingHover.clone().lerp(
new THREE.Vector3(6.4, 2.4, -0.25),
p
);
}
needle.position.copy(needlePosition);
const needleDirection = time < 20.4
? holeB.clone().sub(holeA).normalize()
: new THREE.Vector3(0.6, -0.75, 0.25).normalize();
needle.quaternion.setFromUnitVectors(Y_AXIS, needleDirection);
needleMaterial.opacity = time > 22
? 1 - smoothstep((time - 22) / 0.6)
: 1;
const tip = needle.position.clone().add(
needleDirection.clone().multiplyScalar(0.23)
);
const middle = spoolThreadPosition.clone().lerp(tip, 0.55);
middle.y += 0.12;
const positions = threadGeometry.attributes.position.array;
positions[0] = spoolThreadPosition.x;
positions[1] = spoolThreadPosition.y;
positions[2] = spoolThreadPosition.z;
positions[3] = middle.x;
positions[4] = middle.y;
positions[5] = middle.z;
positions[6] = tip.x;
positions[7] = tip.y;
positions[8] = tip.z;
threadGeometry.attributes.position.needsUpdate = true;
materials.thread.opacity = time > 22
? 1 - smoothstep((time - 22) / 0.6)
: 0.9;
}
// Final equal-spacing guide.
const guideVisibility = smoothstep((time - 23.65) / 1.35);
alignmentGroup.visible = guideVisibility > 0;
alignmentMaterial.opacity = guideVisibility * 0.72;
haloMaterials.forEach((haloMaterial, index) => {
haloMaterial.opacity = guideVisibility * (0.7 + Math.sin(time * 3 + index) * 0.12);
});
}
const playButton = document.querySelector("#play-button");
const resetButton = document.querySelector("#reset-button");
const statusPill = document.querySelector("#status-pill");
const caption = document.querySelector("#caption");
const chapterNumber = document.querySelector("#chapter-number");
const chapterKicker = document.querySelector("#chapter-kicker");
const chapterTitle = document.querySelector("#chapter-title");
const chapterCopy = document.querySelector("#chapter-copy");
const progress = document.querySelector("#progress");
const chapterButtons = [...document.querySelectorAll(".chapter")];
const checks = {
holes: document.querySelector("#check-holes"),
rim: document.querySelector("#check-rim"),
size: document.querySelector("#check-size"),
space: document.querySelector("#check-space")
};
function updateCheck(element, value, active) {
element.classList.toggle("good", active);
element.querySelector("b").textContent = active ? value : "—";
}
function updateUI(time) {
let activeIndex = 0;
chapters.forEach((chapter, index) => {
if (time >= chapter.start) activeIndex = index;
});
const chapter = chapters[activeIndex];
if (activeIndex !== currentChapter) {
currentChapter = activeIndex;
chapterNumber.textContent = String(activeIndex + 1).padStart(2, "0");
chapterKicker.textContent = chapter.kicker;
chapterTitle.textContent = chapter.title;
chapterCopy.textContent = chapter.copy;
statusPill.textContent = chapter.status;
statusPill.classList.toggle("complete", activeIndex === chapters.length - 1);
caption.classList.toggle("complete", activeIndex === chapters.length - 1);
chapterButtons.forEach((button, index) => {
button.classList.toggle("active", index === activeIndex);
});
caption.animate(
[
{ opacity: 0.45, transform: "translateY(7px)" },
{ opacity: 1, transform: "translateY(0)" }
],
{ duration: 420, easing: "ease-out" }
);
}
progress.style.transform = `scaleX(${THREE.MathUtils.clamp(time / TOTAL, 0, 1)})`;
progress.style.background = activeIndex === chapters.length - 1
? "#557c69"
: "#b76743";
playButton.textContent = playing
? "Pause"
: time >= TOTAL
? "Replay"
: "Play";
updateCheck(checks.holes, "4", time >= 15.2);
updateCheck(checks.rim, "warm horn", time >= 16.1);
updateCheck(checks.size, "matched", time >= 17.1);
updateCheck(checks.space, "equal", time >= 24);
}
playButton.addEventListener("click", () => {
if (storyTime >= TOTAL) {
storyTime = 0;
playing = true;
} else {
playing = !playing;
}
});
resetButton.addEventListener("click", () => {
storyTime = 0;
playing = true;
orbit.yaw = 0;
orbit.pitch = 0;
orbit.zoom = 1;
});
chapterButtons.forEach((button) => {
button.addEventListener("click", () => {
storyTime = Number(button.dataset.time);
playing = true;
});
});
canvas.addEventListener("pointerdown", (event) => {
dragging = true;
pointerX = event.clientX;
pointerY = event.clientY;
canvas.setPointerCapture(event.pointerId);
});
canvas.addEventListener("pointermove", (event) => {
if (!dragging) return;
const dx = event.clientX - pointerX;
const dy = event.clientY - pointerY;
pointerX = event.clientX;
pointerY = event.clientY;
orbit.yaw -= dx * 0.005;
orbit.pitch = THREE.MathUtils.clamp(
orbit.pitch + dy * 0.004,
-0.7,
0.7
);
});
function endDrag(event) {
dragging = false;
if (canvas.hasPointerCapture(event.pointerId)) {
canvas.releasePointerCapture(event.pointerId);
}
}
canvas.addEventListener("pointerup", endDrag);
canvas.addEventListener("pointercancel", endDrag);
canvas.addEventListener("dblclick", () => {
orbit.yaw = 0;
orbit.pitch = 0;
orbit.zoom = 1;
});
canvas.addEventListener(
"wheel",
(event) => {
event.preventDefault();
orbit.zoom = THREE.MathUtils.clamp(
orbit.zoom * Math.exp(event.deltaY * 0.001),
0.68,
1.55
);
},
{ passive: false }
);
window.addEventListener("keydown", (event) => {
if (event.code === "Space" && event.target === document.body) {
event.preventDefault();
playButton.click();
}
});
window.addEventListener("resize", () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight, false);
});
const clock = new THREE.Clock();
function animate() {
const delta = Math.min(clock.getDelta(), 0.05);
if (playing) {
storyTime = Math.min(TOTAL, storyTime + delta);
if (storyTime >= TOTAL) playing = false;
}
updateStory(storyTime);
updateUI(storyTime);
const shot = sampleCamera(storyTime);
desiredCameraPosition.copy(shot.position);
desiredCameraTarget.copy(shot.target);
const offset = desiredCameraPosition.clone().sub(desiredCameraTarget);
const spherical = new THREE.Spherical().setFromVector3(offset);
spherical.theta += orbit.yaw;
spherical.phi = THREE.MathUtils.clamp(
spherical.phi + orbit.pitch,
0.14,
1.48
);
spherical.radius *= orbit.zoom;
desiredCameraPosition
.copy(desiredCameraTarget)
.add(new THREE.Vector3().setFromSpherical(spherical));
const cameraEase = 1 - Math.exp(-delta * 4.1);
actualCameraPosition.lerp(desiredCameraPosition, cameraEase);
actualCameraTarget.lerp(desiredCameraTarget, cameraEase);
camera.position.copy(actualCameraPosition);
camera.lookAt(actualCameraTarget);
renderer.render(scene, camera);
}
renderer.setAnimationLoop(animate);
requestAnimationFrame(() => {
const loading = document.querySelector("#loading");
loading.classList.add("out");
setTimeout(() => loading.remove(), 550);
});
</script>
</body>
</html>
```