Prompt specification
Build or customize the Three.js 3D scene "Intergenerational repair-table 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: Intergenerational repair-table story
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/intergenerational-repair-table-story-x3903
Scene Description Prompt:
Create a standalone voxel scene in one HTML file using Three.js from cdnjs. Compose a home worktable with older and younger family members, toy, tools, and photos as simple frames; shared repair ends with the toy moving again.
Working Three.js Starter (intergenerational-repair-table-story-x3903):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>One More Turn — A Voxel Family Story</title>
<style>
:root {
--ink: #283c3a;
--muted: #667b77;
--paper: rgba(255, 250, 238, 0.92);
--accent: #db6f55;
--green: #558879;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #dbe7e4;
color: var(--ink);
font-family: Inter, ui-rounded, "SF Pro Rounded", system-ui, -apple-system,
BlinkMacSystemFont, "Segoe UI", sans-serif;
}
#stage {
position: fixed;
inset: 0;
}
canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
cursor: grab;
}
canvas:active {
cursor: grabbing;
}
.hud {
position: fixed;
inset: 0;
pointer-events: none;
}
.heading {
position: absolute;
top: 28px;
left: 30px;
max-width: 440px;
text-shadow: 0 1px 0 rgba(255, 255, 255, 0.6);
}
.eyebrow {
display: inline-flex;
align-items: center;
gap: 8px;
margin-bottom: 8px;
padding: 7px 11px;
border: 1px solid rgba(40, 60, 58, 0.15);
border-radius: 999px;
background: rgba(255, 250, 238, 0.75);
backdrop-filter: blur(10px);
color: var(--green);
font-size: 11px;
font-weight: 800;
letter-spacing: 0.13em;
text-transform: uppercase;
}
.eyebrow::before {
content: "";
width: 7px;
height: 7px;
border-radius: 2px;
background: var(--accent);
transform: rotate(12deg);
}
h1 {
margin: 0;
font-family: Georgia, "Times New Roman", serif;
font-size: clamp(34px, 5vw, 64px);
font-weight: 500;
line-height: 0.95;
letter-spacing: -0.055em;
}
.subtitle {
max-width: 370px;
margin: 13px 0 0;
color: var(--muted);
font-size: 14px;
line-height: 1.55;
}
.story-card {
position: absolute;
left: 30px;
bottom: 28px;
width: min(470px, calc(100vw - 60px));
padding: 19px 21px 17px;
border: 1px solid rgba(40, 60, 58, 0.12);
border-radius: 18px;
background: var(--paper);
box-shadow: 0 15px 45px rgba(42, 64, 60, 0.13);
backdrop-filter: blur(15px);
}
.story-top {
display: flex;
align-items: center;
justify-content: space-between;
gap: 14px;
margin-bottom: 8px;
}
#chapter {
color: var(--accent);
font-size: 10px;
font-weight: 850;
letter-spacing: 0.14em;
text-transform: uppercase;
}
#time {
color: #8a9b97;
font-size: 10px;
font-variant-numeric: tabular-nums;
white-space: nowrap;
}
#story-title {
margin: 0 0 6px;
font-family: Georgia, "Times New Roman", serif;
font-size: 22px;
font-weight: 500;
letter-spacing: -0.025em;
}
#story-text {
min-height: 38px;
margin: 0;
color: var(--muted);
font-size: 13px;
line-height: 1.5;
}
.steps {
display: grid;
grid-template-columns: repeat(5, 1fr);
gap: 6px;
margin-top: 15px;
}
.step {
height: 4px;
overflow: hidden;
border-radius: 4px;
background: rgba(40, 60, 58, 0.11);
}
.step::after {
content: "";
display: block;
width: 100%;
height: 100%;
border-radius: inherit;
background: var(--accent);
transform: scaleX(0);
transform-origin: left;
transition: transform 250ms ease;
}
.step.done::after {
transform: scaleX(1);
}
.step.active::after {
transform: scaleX(1);
}
.buttons {
position: absolute;
top: 28px;
right: 30px;
display: flex;
gap: 8px;
pointer-events: auto;
}
button {
min-height: 40px;
padding: 0 14px;
border: 1px solid rgba(40, 60, 58, 0.14);
border-radius: 12px;
background: rgba(255, 250, 238, 0.86);
color: var(--ink);
box-shadow: 0 8px 22px rgba(42, 64, 60, 0.09);
backdrop-filter: blur(12px);
font: inherit;
font-size: 12px;
font-weight: 750;
cursor: pointer;
transition: transform 150ms ease, background 150ms ease;
}
button:hover {
transform: translateY(-2px);
background: #fffaf0;
}
button:active {
transform: translateY(0);
}
.camera-hint {
position: absolute;
right: 30px;
bottom: 29px;
padding: 10px 13px;
border-radius: 999px;
background: rgba(255, 250, 238, 0.72);
color: #72837f;
font-size: 11px;
font-weight: 650;
letter-spacing: 0.02em;
backdrop-filter: blur(12px);
}
.camera-hint span {
color: var(--accent);
}
@media (max-width: 700px) {
.heading {
top: 19px;
left: 19px;
}
h1 {
font-size: 39px;
}
.subtitle {
display: none;
}
.buttons {
top: 19px;
right: 19px;
flex-direction: column;
}
button {
min-height: 36px;
padding: 0 11px;
font-size: 11px;
}
.story-card {
left: 14px;
bottom: 14px;
width: calc(100vw - 28px);
padding: 15px 17px 14px;
}
#story-title {
font-size: 19px;
}
#story-text {
font-size: 12px;
}
.camera-hint {
display: none;
}
}
</style>
</head>
<body>
<main id="stage" aria-label="An animated voxel family repairing a toy robot"></main>
<div class="hud">
<header class="heading">
<div class="eyebrow">A little family story</div>
<h1>One More Turn</h1>
<p class="subtitle">
Grandpa and Mia find the family worktable, gather a few tools, and give
an old tin robot the attention it needs.
</p>
</header>
<section class="story-card" aria-live="polite">
<div class="story-top">
<span id="chapter">Chapter 1</span>
<span id="time">0:00</span>
</div>
<h2 id="story-title">The toy has gone quiet</h2>
<p id="story-text">
Mia's robot does not move. Grandpa studies the little wheel and reaches
for the toolbox.
</p>
<div class="steps" aria-hidden="true">
<div class="step active"></div>
<div class="step"></div>
<div class="step"></div>
<div class="step"></div>
<div class="step"></div>
</div>
</section>
<div class="buttons">
<button id="pause" type="button">Pause story</button>
<button id="replay" type="button">Replay</button>
<button id="reset-camera" type="button">Reset view</button>
</div>
<div class="camera-hint">
<span>Drag</span> to orbit · <span>Scroll</span> to zoom
</div>
</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// -------------------------------------------------------------------------
// Renderer and scene
// -------------------------------------------------------------------------
const stage = document.querySelector("#stage");
const renderer = new THREE.WebGLRenderer({
antialias: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.08;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.domElement.setAttribute("role", "img");
renderer.domElement.setAttribute(
"aria-label",
"Voxel diorama of a grandfather and child repairing a toy robot at a home worktable."
);
stage.appendChild(renderer.domElement);
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xdbe7e4);
scene.fog = new THREE.Fog(0xdbe7e4, 16, 29);
const camera = new THREE.PerspectiveCamera(
40,
window.innerWidth / window.innerHeight,
0.1,
60
);
const cameraTarget = new THREE.Vector3(0, 2.25, -0.25);
let cameraYaw = 0.49;
let cameraPitch = 0.51;
let cameraRadius = 11.9;
function updateCamera() {
const horizontal = cameraRadius * Math.cos(cameraPitch);
camera.position.set(
cameraTarget.x + Math.sin(cameraYaw) * horizontal,
cameraTarget.y + Math.sin(cameraPitch) * cameraRadius,
cameraTarget.z + Math.cos(cameraYaw) * horizontal
);
camera.lookAt(cameraTarget);
}
updateCamera();
// -------------------------------------------------------------------------
// Materials and voxel helpers
// -------------------------------------------------------------------------
function material(color, options = {}) {
return new THREE.MeshStandardMaterial({
color,
roughness: 0.78,
metalness: 0,
...options
});
}
const M = {
floor: material(0xc9b89d),
floorLight: material(0xdacbb0),
floorDark: material(0xb8a58a),
wall: material(0xe7e5d7),
wallSide: material(0xd8ddd1),
trim: material(0xf3efe3),
rugEdge: material(0x527a78),
rug: material(0x779b94),
rugPattern: material(0x8baba1),
wood: material(0xb97846),
woodDark: material(0x75472f),
woodLight: material(0xd49a62),
steel: material(0x8d9b99, { metalness: 0.58, roughness: 0.38 }),
darkSteel: material(0x344744, { metalness: 0.32, roughness: 0.5 }),
red: material(0xc95f4a),
redDark: material(0x8e4035),
cream: material(0xf2dfb7),
yellow: material(0xe4b94f),
blue: material(0x4c7187),
navy: material(0x304e61),
green: material(0x66876c),
greenDark: material(0x435f4c),
skin: material(0xdca681),
skinDark: material(0xb97a58),
whiteHair: material(0xe4e1d6),
brownHair: material(0x5a3b2d),
black: material(0x263330),
eye: material(0x76efb4, {
emissive: 0x36c983,
emissiveIntensity: 0.08,
roughness: 0.28
}),
orangeEye: material(0xffad55, {
emissive: 0xe76d28,
emissiveIntensity: 0.08,
roughness: 0.3
}),
pictureSky: material(0xa9cbd0),
pictureSand: material(0xdfbd7f),
pictureGreen: material(0x789c71),
pictureCream: material(0xf3e5c9)
};
function box(parent, size, position, mat, options = {}) {
const mesh = new THREE.Mesh(
new THREE.BoxGeometry(size[0], size[1], size[2]),
mat
);
mesh.position.set(position[0], position[1], position[2]);
mesh.castShadow = options.castShadow !== false;
mesh.receiveShadow = options.receiveShadow !== false;
if (options.rotation) {
mesh.rotation.set(
options.rotation[0] || 0,
options.rotation[1] || 0,
options.rotation[2] || 0
);
}
parent.add(mesh);
return mesh;
}
function group(parent, x = 0, y = 0, z = 0) {
const result = new THREE.Group();
result.position.set(x, y, z);
parent.add(result);
return result;
}
function voxelLimb(parent, size, position, mat) {
return box(parent, size, position, mat);
}
// -------------------------------------------------------------------------
// Lighting
// -------------------------------------------------------------------------
scene.add(new THREE.HemisphereLight(0xeaf5ff, 0x79604a, 1.7));
const keyLight = new THREE.DirectionalLight(0xfff1d6, 2.7);
keyLight.position.set(5.5, 10, 7);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(2048, 2048);
keyLight.shadow.camera.left = -9;
keyLight.shadow.camera.right = 9;
keyLight.shadow.camera.top = 9;
keyLight.shadow.camera.bottom = -7;
keyLight.shadow.camera.near = 1;
keyLight.shadow.camera.far = 26;
keyLight.shadow.bias = -0.0007;
scene.add(keyLight);
const fillLight = new THREE.DirectionalLight(0xc8e2ef, 0.85);
fillLight.position.set(-6, 5, 3);
scene.add(fillLight);
// -------------------------------------------------------------------------
// Room, floor, rug and walls
// -------------------------------------------------------------------------
box(scene, [14, 0.3, 9], [0, -0.15, 0], M.floor, {
castShadow: false
});
for (let x = -6; x <= 6; x += 1.5) {
box(scene, [0.025, 0.018, 8.8], [x, 0.01, 0], M.floorDark, {
castShadow: false
});
}
for (let z = -3.75; z <= 3.75; z += 1.5) {
box(scene, [13.8, 0.018, 0.025], [0, 0.012, z], M.floorDark, {
castShadow: false
});
}
box(scene, [7.2, 0.06, 3.65], [0, 0.04, 0.35], M.rugEdge, {
castShadow: false
});
box(scene, [6.95, 0.075, 3.4], [0, 0.06, 0.35], M.rug, {
castShadow: false
});
for (let x = -2.7; x <= 2.7; x += 0.9) {
box(scene, [0.09, 0.015, 3.05], [x, 0.105, 0.35], M.rugPattern, {
castShadow: false
});
}
box(scene, [14.2, 7.6, 0.3], [0, 3.8, -4.45], M.wall, {
castShadow: false
});
box(scene, [0.3, 7.6, 8.9], [-7, 3.8, 0], M.wallSide, {
castShadow: false
});
box(scene, [14, 0.28, 0.15], [0, 0.14, -4.25], M.trim);
box(scene, [0.15, 0.28, 8.8], [-6.82, 0.14, 0], M.trim);
// Small wall blocks add a subtle handmade voxel texture.
for (let row = 0; row < 5; row++) {
for (let col = 0; col < 10; col++) {
const x = -5.8 + col * 1.27 + (row % 2) * 0.18;
const y = 2.2 + row * 0.8;
if (Math.abs(x) > 6.3) continue;
box(
scene,
[0.035, 0.025, 0.018],
[x, y, -4.285],
M.trim,
{ castShadow: false }
);
}
}
// -------------------------------------------------------------------------
// Framed family photos
// -------------------------------------------------------------------------
function addPhotoFrame(x, y, width, height, type) {
const frame = group(scene, x, y, -4.19);
const border = 0.12;
box(frame, [width, border, 0.12], [0, height / 2, 0], M.woodDark);
box(frame, [width, border, 0.12], [0, -height / 2, 0], M.woodDark);
box(frame, [border, height, 0.12], [-width / 2, 0, 0], M.woodDark);
box(frame, [border, height, 0.12], [width / 2, 0, 0], M.woodDark);
box(
frame,
[width - 0.18, height - 0.18, 0.06],
[0, 0, 0.025],
M.pictureCream
);
const art = group(frame, 0, 0, 0.067);
if (type === "family") {
box(art, [0.95, 0.62, 0.025], [0, 0.03, 0], M.pictureSky);
box(art, [0.2, 0.24, 0.035], [-0.25, 0.03, 0.03], M.skin);
box(art, [0.18, 0.21, 0.035], [0.02, -0.01, 0.03], M.skinDark);
box(art, [0.16, 0.19, 0.035], [0.27, 0.04, 0.03], M.skin);
box(art, [0.28, 0.22, 0.04], [-0.25, -0.22, 0.04], M.green);
box(art, [0.25, 0.2, 0.04], [0.02, -0.22, 0.04], M.red);
box(art, [0.23, 0.19, 0.04], [0.27, -0.2, 0.04], M.blue);
} else if (type === "beach") {
box(art, [0.95, 0.32, 0.025], [0, -0.12, 0], M.pictureSand);
box(art, [0.95, 0.31, 0.025], [0, 0.16, 0], M.pictureSky);
box(art, [0.95, 0.12, 0.03], [0, 0.02, 0.01], M.blue);
box(art, [0.19, 0.19, 0.035], [0.28, 0.23, 0.03], M.yellow);
} else {
box(art, [0.95, 0.63, 0.025], [0, 0, 0], M.pictureSky);
box(art, [0.95, 0.25, 0.03], [0, -0.2, 0.01], M.pictureGreen);
box(art, [0.12, 0.35, 0.035], [-0.25, -0.06, 0.03], M.brownHair);
box(art, [0.12, 0.35, 0.035], [0.24, -0.06, 0.03], M.brownHair);
box(art, [0.31, 0.1, 0.035], [-0.12, -0.22, 0.04], M.wood);
}
// Tiny glass glint.
box(
art,
[0.06, 0.34, 0.012],
[-width * 0.32, height * 0.23, 0.08],
M.pictureCream,
{ castShadow: false }
);
}
addPhotoFrame(-2.65, 4.85, 1.65, 1.25, "family");
addPhotoFrame(0, 4.85, 1.65, 1.25, "beach");
addPhotoFrame(2.65, 4.85, 1.65, 1.25, "garden");
// -------------------------------------------------------------------------
// Wall shelf, books, plant and pendant lamp
// -------------------------------------------------------------------------
box(scene, [3.25, 0.16, 0.55], [3.45, 3.38, -4.05], M.wood);
box(scene, [3.05, 0.14, 0.42], [3.45, 3.29, -4.02], M.woodDark);
const bookColors = [M.red, M.blue, M.yellow, M.green];
for (let i = 0; i < 7; i++) {
box(
scene,
[0.17, 0.48 + (i % 3) * 0.07, 0.34],
[2.55 + i * 0.21, 3.71, -4.02],
bookColors[i % bookColors.length]
);
}
// Voxel plant.
const plant = group(scene, 4.7, 3.47, -4.02);
box(plant, [0.55, 0.45, 0.45], [0, 0.22, 0], M.redDark);
box(plant, [0.07, 0.75, 0.07], [0, 0.75, 0], M.greenDark);
box(plant, [0.42, 0.18, 0.18], [-0.17, 0.86, 0], M.green);
box(plant, [0.4, 0.18, 0.18], [0.18, 1.08, 0], M.green);
box(plant, [0.18, 0.42, 0.18], [-0.19, 1.2, 0], M.greenDark);
box(plant, [0.18, 0.36, 0.18], [0.18, 1.4, 0], M.green);
// Hanging pixel lamp.
box(scene, [0.045, 1.65, 0.045], [0, 6.8, -0.2], M.darkSteel);
box(scene, [1.05, 0.18, 1.05], [0, 5.95, -0.2], M.cream);
box(scene, [0.78, 0.22, 0.78], [0, 5.79, -0.2], M.yellow);
box(scene, [0.55, 0.12, 0.55], [0, 5.66, -0.2], M.cream, {
emissive: 0xffd27c,
emissiveIntensity: 0.25
});
const lampLight = new THREE.PointLight(0xffd6a0, 38, 9, 2);
lampLight.position.set(0, 5.5, -0.2);
scene.add(lampLight);
// -------------------------------------------------------------------------
// Worktable
// -------------------------------------------------------------------------
const table = group(scene, 0, 0, 0);
box(table, [6.4, 0.28, 2.9], [0, 1.73, 0], M.wood);
box(table, [6.42, 0.12, 2.92], [0, 1.55, 0], M.woodDark);
box(table, [6.08, 0.26, 0.18], [0, 1.28, -1.18], M.woodDark);
box(table, [6.08, 0.26, 0.18], [0, 1.28, 1.18], M.woodDark);
const legPositions = [
[-2.83, -1.12],
[2.83, -1.12],
[-2.83, 1.12],
[2.83, 1.12]
];
for (const [x, z] of legPositions) {
box(table, [0.36, 1.55, 0.36], [x, 0.78, z], M.woodDark);
box(table, [0.46, 0.15, 0.46], [x, 0.09, z], M.wood);
}
// Table grain.
for (let i = 0; i < 8; i++) {
box(
table,
[0.035, 0.012, 2.45 - (i % 3) * 0.18],
[-2.65 + i * 0.76, 1.878, 0],
M.woodDark,
{ castShadow: false }
);
}
// Work mat and tools.
box(table, [2.15, 0.035, 1.35], [-0.45, 1.895, -0.05], M.greenDark);
box(table, [1.98, 0.02, 1.18], [-0.45, 1.922, -0.05], M.green);
// Toolbox.
box(table, [0.9, 0.48, 0.58], [-2.45, 2.11, -0.55], M.red);
box(table, [0.96, 0.12, 0.62], [-2.45, 2.4, -0.55], M.redDark);
box(table, [0.33, 0.07, 0.07], [-2.45, 2.35, -0.2], M.woodDark);
box(table, [0.05, 0.3, 0.05], [-2.45, 2.1, -0.225], M.darkSteel);
// Screwdriver.
const screwdriver = group(table, 1.0, 1.96, -0.64);
screwdriver.rotation.y = -0.42;
box(screwdriver, [0.18, 0.14, 0.55], [0, 0, 0], M.red);
box(screwdriver, [0.075, 0.075, 0.55], [0, 0, 0.52], M.steel);
// Hammer.
const hammer = group(table, 1.7, 1.97, 0.5);
hammer.rotation.y = 0.55;
box(hammer, [0.16, 0.16, 0.86], [0, 0, 0], M.woodLight);
box(hammer, [0.48, 0.25, 0.24], [0, 0.02, 0.43], M.steel);
// Wrench lying on the table.
const wrench = group(table, 0.76, 1.98, 0.55);
wrench.rotation.y = -0.55;
box(wrench, [0.13, 0.09, 0.72], [0, 0, 0], M.steel);
box(wrench, [0.35, 0.09, 0.14], [0, 0, 0.38], M.steel);
box(wrench, [0.12, 0.09, 0.18], [-0.1, 0, 0.45], M.steel);
box(wrench, [0.12, 0.09, 0.18], [0.1, 0, 0.45], M.steel);
// Tape measure.
box(table, [0.48, 0.35, 0.38], [2.35, 2.06, -0.54], M.yellow);
box(table, [0.28, 0.03, 0.2], [2.35, 2.25, -0.54], M.darkSteel);
// Spare parts and battery.
box(table, [0.2, 0.08, 0.2], [1.78, 1.98, 0.72], M.steel);
box(table, [0.16, 0.25, 0.16], [2.05, 2.06, 0.73], M.navy);
box(table, [0.16, 0.07, 0.16], [2.05, 2.21, 0.73], M.yellow);
// Small mug.
box(table, [0.34, 0.4, 0.34], [-2.4, 2.08, 0.75], M.cream);
box(table, [0.13, 0.27, 0.1], [-2.13, 2.09, 0.75], M.cream);
box(table, [0.27, 0.025, 0.27], [-2.4, 2.29, 0.75], M.woodDark);
// A small bench vise at the front corner.
box(table, [0.55, 0.28, 0.5], [2.65, 1.78, 1.2], M.darkSteel);
box(table, [0.12, 0.55, 0.55], [2.42, 1.89, 1.25], M.steel);
box(table, [0.12, 0.55, 0.55], [2.85, 1.89, 1.25], M.steel);
box(table, [0.7, 0.12, 0.12], [2.62, 2.0, 1.58], M.darkSteel);
// -------------------------------------------------------------------------
// Family members
// -------------------------------------------------------------------------
function makeAdult() {
const root = group(scene, -1.38, 0, -1.72);
root.rotation.y = 0.08;
// Shoes and trousers.
box(root, [0.4, 0.23, 0.62], [-0.28, 0.13, 0.08], M.woodDark);
box(root, [0.4, 0.23, 0.62], [0.28, 0.13, 0.08], M.woodDark);
box(root, [0.34, 1.25, 0.4], [-0.28, 0.79, 0], M.navy);
box(root, [0.34, 1.25, 0.4], [0.28, 0.79, 0], M.navy);
// Shirt and vest.
box(root, [1.08, 1.05, 0.62], [0, 2.02, 0], M.cream);
box(root, [0.83, 0.93, 0.67], [0, 2.0, 0.025], M.green);
box(root, [0.22, 0.44, 0.035], [0, 2.19, 0.37], M.greenDark);
box(root, [0.29, 0.3, 0.035], [0, 2.43, 0.365], M.cream);
// Neck and head.
box(root, [0.3, 0.23, 0.3], [0, 2.62, 0], M.skin);
const head = group(root, 0, 2.98, 0.01);
box(head, [0.74, 0.72, 0.7], [0, 0, 0], M.skin);
box(head, [0.16, 0.18, 0.16], [-0.39, -0.01, 0], M.skinDark);
box(head, [0.16, 0.18, 0.16], [0.39, -0.01, 0], M.skinDark);
// White hair and beard.
box(head, [0.8, 0.18, 0.74], [0, 0.31, -0.015], M.whiteHair);
box(head, [0.17, 0.47, 0.24], [-0.29, 0.06, -0.21], M.whiteHair);
box(head, [0.17, 0.47, 0.24], [0.29, 0.06, -0.21], M.whiteHair);
box(head, [0.57, 0.2, 0.13], [0, -0.22, 0.34], M.whiteHair);
// Eyes and glasses.
box(head, [0.1, 0.1, 0.035], [-0.16, 0.04, 0.366], M.black);
box(head, [0.1, 0.1, 0.035], [0.16, 0.04, 0.366], M.black);
box(head, [0.28, 0.19, 0.035], [-0.16, 0.04, 0.39], M.darkSteel);
box(head, [0.28, 0.19, 0.035], [0.16, 0.04, 0.39], M.darkSteel);
box(head, [0.1, 0.035, 0.04], [0, 0.04, 0.4], M.darkSteel);
// Arms with shoulder pivots.
function arm(side) {
const pivot = group(root, side * 0.67, 2.42, 0);
box(pivot, [0.25, 0.61, 0.28], [0, -0.29, 0], M.cream);
box(pivot, [0.23, 0.58, 0.25], [0, -0.8, 0], M.skin);
const hand = box(pivot, [0.27, 0.24, 0.28], [0, -1.12, 0], M.skin);
return { pivot, hand };
}
const armL = arm(-1);
const armR = arm(1);
// A small wrench carried during the repair.
const carriedWrench = group(armR.hand, 0, -0.2, 0.08);
box(carriedWrench, [0.07, 0.07, 0.46], [0, 0, 0.13], M.steel);
box(carriedWrench, [0.22, 0.07, 0.12], [0, 0, 0.35], M.steel);
box(carriedWrench, [0.22, 0.07, 0.12], [0, 0, -0.1], M.steel);
return { root, head, armL, armR, carriedWrench };
}
function makeChild() {
const root = group(scene, 0.68, 0, -1.68);
root.scale.setScalar(0.73);
box(root, [0.4, 0.22, 0.58], [-0.27, 0.12, 0.07], M.blue);
box(root, [0.4, 0.22, 0.58], [0.27, 0.12, 0.07], M.blue);
box(root, [0.32, 1.05, 0.38], [-0.27, 0.71, 0], M.navy);
box(root, [0.32, 1.05, 0.38], [0.27, 0.71, 0], M.navy);
box(root, [1.02, 1.0, 0.58], [0, 1.91, 0], M.yellow);
box(root, [0.78, 0.8, 0.66], [0, 1.78, 0.035], M.blue);
box(root, [0.18, 0.45, 0.035], [-0.22, 2.08, 0.37], M.blue);
box(root, [0.18, 0.45, 0.035], [0.22, 2.08, 0.37], M.blue);
box(root, [0.29, 0.12, 0.045], [0, 1.95, 0.38], M.yellow);
box(root, [0.27, 0.22, 0.27], [0, 2.48, 0], M.skin);
const head = group(root, 0, 2.83, 0);
box(head, [0.72, 0.7, 0.68], [0, 0, 0], M.skin);
box(head, [0.16, 0.17, 0.15], [-0.38, 0, 0], M.skinDark);
box(head, [0.16, 0.17, 0.15], [0.38, 0, 0], M.skinDark);
// Tousled brown hair.
box(head, [0.78, 0.17, 0.72], [0, 0.3, -0.015], M.brownHair);
box(head, [0.22, 0.22, 0.22], [-0.29, 0.2, 0.22], M.brownHair);
box(head, [0.23, 0.25, 0.24], [0.27, 0.22, 0.22], M.brownHair);
box(head, [0.2, 0.2, 0.2], [0, 0.39, 0.26], M.brownHair);
box(head, [0.09, 0.1, 0.035], [-0.16, 0.02, 0.355], M.black);
box(head, [0.09, 0.1, 0.035], [0.16, 0.02, 0.355], M.black);
box(head, [0.16, 0.045, 0.04], [0, -0.16, 0.355], M.redDark);
function arm(side) {
const pivot = group(root, side * 0.62, 2.3, 0);
box(pivot, [0.23, 0.55, 0.26], [0, -0.26, 0], M.yellow);
box(pivot, [0.21, 0.54, 0.23], [0, -0.74, 0], M.skin);
const hand = box(pivot, [0.25, 0.23, 0.26], [0, -1.05, 0], M.skin);
return { pivot, hand };
}
const armL = arm(-1);
const armR = arm(1);
// Battery carried in Mia's left hand.
const battery = group(armL.hand, 0, -0.17, 0.08);
box(battery, [0.17, 0.28, 0.17], [0, 0, 0], M.navy);
box(battery, [0.18, 0.06, 0.18], [0, 0.16, 0], M.yellow);
return { root, head, armL, armR, battery };
}
const grandpa = makeAdult();
const mia = makeChild();
// -------------------------------------------------------------------------
// Voxel tin robot
// -------------------------------------------------------------------------
const robot = group(scene, -0.42, 1.91, -0.08);
// The robot's local forward axis is +Z; rotate it to travel along the table.
robot.rotation.y = Math.PI / 2;
const robotWheels = [];
box(robot, [0.84, 0.52, 0.56], [0, 0.63, 0], M.red);
box(robot, [0.88, 0.13, 0.59], [0, 0.39, 0], M.redDark);
box(robot, [0.46, 0.3, 0.035], [0, 0.65, 0.296], M.cream);
for (const side of [-1, 1]) {
const wheel = box(
robot,
[0.18, 0.4, 0.46],
[side * 0.49, 0.23, 0],
M.black
);
box(wheel, [0.2, 0.1, 0.1], [0, 0, 0], M.steel);
robotWheels.push(wheel);
box(
robot,
[0.18, 0.54, 0.19],
[side * 0.51, 0.69, 0],
M.redDark
);
}
const robotHead = group(robot, 0, 1.19, 0);
box(robotHead, [0.68, 0.5, 0.54], [0, 0, 0], M.cream);
box(robotHead, [0.73, 0.12, 0.57], [0, 0.21, 0], M.red);
box(robotHead, [0.13, 0.13, 0.05], [-0.16, 0.03, 0.29], M.eye);
box(robotHead, [0.13, 0.13, 0.05], [0.16, 0.03, 0.29], M.eye);
box(robotHead, [0.2, 0.055, 0.045], [0, -0.13, 0.29], M.redDark);
box(robotHead, [0.05, 0.32, 0.05], [0, 0.39, 0], M.darkSteel);
box(robotHead, [0.13, 0.13, 0.13], [0, 0.58, 0], M.yellow);
// Chest indicator.
const chestLight = box(
robot,
[0.12, 0.12, 0.04],
[0, 0.83, 0.3],
M.orangeEye
);
// Loose wheel beside the robot during the repair.
const looseWheel = group(scene, 0.38, 1.96, -0.55);
box(looseWheel, [0.2, 0.4, 0.46], [0, 0, 0], M.black);
box(looseWheel, [0.22, 0.1, 0.1], [0, 0, 0], M.steel);
// -------------------------------------------------------------------------
// Confetti for the final reveal
// -------------------------------------------------------------------------
let randomSeed = 93841;
function random() {
randomSeed = (randomSeed * 16807) % 2147483647;
return (randomSeed - 1) / 2147483646;
}
const confetti = [];
const confettiMaterials = [M.red, M.yellow, M.blue, M.green];
for (let i = 0; i < 30; i++) {
const piece = box(
scene,
[0.08 + random() * 0.06, 0.045, 0.08],
[0, 0, 0],
confettiMaterials[i % confettiMaterials.length]
);
piece.userData.seed = random() * Math.PI * 2;
piece.userData.speed = 0.8 + random() * 0.8;
piece.userData.x = (random() - 0.5) * 1.7;
piece.userData.z = (random() - 0.5) * 1.1;
piece.visible = false;
confetti.push(piece);
}
// -------------------------------------------------------------------------
// Story animation
// -------------------------------------------------------------------------
const chapterEl = document.querySelector("#chapter");
const titleEl = document.querySelector("#story-title");
const textEl = document.querySelector("#story-text");
const timeEl = document.querySelector("#time");
const stepEls = [...document.querySelectorAll(".step")];
const pauseButton = document.querySelector("#pause");
const chapters = [
{
end: 3,
chapter: "Chapter 1",
title: "The toy has gone quiet",
text: "Mia's robot does not move. Grandpa studies the little wheel and reaches for the toolbox."
},
{
end: 8,
chapter: "Chapter 2",
title: "A little turn at a time",
text: "The wheel has worked loose. Grandpa holds it steady while Mia checks the battery."
},
{
end: 11,
chapter: "Chapter 3",
title: "One last little piece",
text: "Mia fits the wheel into place. The robot stands tall and waits for a nudge."
},
{
end: 17,
chapter: "Chapter 4",
title: "Off it goes!",
text: "The old tin robot rolls across the worktable, one cheerful turn at a time."
},
{
end: Infinity,
chapter: "The end",
title: "It moves again!",
text: "Grandpa and Mia smile. A small repair, and a favorite toy is back in the family."
}
];
let simTime = 0;
let paused = false;
let lastFrame = performance.now();
let currentChapter = -1;
let previousRobotX = robot.position.x;
function smoothstep(edge0, edge1, value) {
const t = THREE.MathUtils.clamp(
(value - edge0) / (edge1 - edge0),
0,
1
);
return t * t * (3 - 2 * t);
}
function updateStoryUI(t) {
let index = chapters.findIndex((chapter) => t < chapter.end);
if (index < 0) index = chapters.length - 1;
if (index !== currentChapter) {
currentChapter = index;
const chapter = chapters[index];
chapterEl.textContent = chapter.chapter;
titleEl.textContent = chapter.title;
textEl.textContent = chapter.text;
stepEls.forEach((step, i) => {
step.classList.toggle("done", i < index);
step.classList.toggle("active", i === index);
});
}
const seconds = Math.floor(Math.min(t, 17));
timeEl.textContent = `0:${String(seconds).padStart(2, "0")}`;
}
function updateScene(t, dt) {
const repairAmount =
smoothstep(2.4, 4.0, t) * (1 - smoothstep(8.0, 10.8, t));
const celebration = smoothstep(16.5, 18.5, t);
// Character poses.
grandpa.armR.pivot.rotation.x =
-0.28 - repairAmount * 0.83 + celebration * 1.18;
grandpa.armL.pivot.rotation.x =
-0.18 + celebration * 1.7;
mia.armL.pivot.rotation.x =
-0.2 - repairAmount * 0.72 + celebration * 1.5;
mia.armR.pivot.rotation.x =
-0.18 + celebration * 1.75;
grandpa.head.rotation.x = 0.12 + repairAmount * 0.11;
mia.head.rotation.x = 0.1 + repairAmount * 0.08;
if (celebration > 0) {
grandpa.head.position.y =
2.98 + Math.sin(t * 7) * 0.025 * celebration;
mia.head.position.y =
2.83 + Math.sin(t * 8 + 0.8) * 0.028 * celebration;
} else {
grandpa.head.position.y = 2.98;
mia.head.position.y = 2.83;
}
grandpa.carriedWrench.visible = t >= 2.6 && t < 8.2;
mia.battery.visible = t >= 3.1 && t < 10.7;
looseWheel.visible = t >= 2.6 && t < 8.2;
if (looseWheel.visible) {
looseWheel.rotation.z = t * 1.4;
looseWheel.position.y = 1.96 + Math.abs(Math.sin(t * 4)) * 0.025;
}
// Robot movement.
let robotX;
let robotZ = -0.08;
if (t < 11) {
robotX = -0.42;
} else if (t < 17) {
const move = smoothstep(11, 16.6, t);
robotX = THREE.MathUtils.lerp(-0.42, 1.75, move);
robotZ += Math.sin(move * Math.PI * 3) * 0.05;
} else {
robotX = 1.75 + Math.sin((t - 17) * 1.35) * 0.82;
robotZ += Math.sin((t - 17) * 2.7) * 0.09;
}
robot.position.x = robotX;
robot.position.z = robotZ;
robot.position.y =
1.91 +
(t >= 11 ? Math.abs(Math.sin(t * 8)) * 0.018 : 0);
const tilt =
t < 2.6
? 0.035
: t < 8.2
? 0.13 * repairAmount
: t < 10.8
? THREE.MathUtils.lerp(0.1, 0, smoothstep(8.2, 10.8, t))
: 0;
robot.rotation.z = tilt;
// The head tips down during the repair and perks up when it runs.
robotHead.rotation.x =
t < 10.8 ? 0.16 * repairAmount : -0.025 * celebration;
const dx = robot.position.x - previousRobotX;
if (t >= 11) {
for (const wheel of robotWheels) {
wheel.rotation.z -= dx / 0.21;
}
}
previousRobotX = robot.position.x;
// Robot lights.
if (t < 2.6) {
M.eye.emissiveIntensity = 0.08;
M.orangeEye.emissiveIntensity = 0.06;
} else if (t < 10.8) {
M.eye.emissiveIntensity = 0.16;
M.orangeEye.emissiveIntensity =
0.3 + Math.abs(Math.sin(t * 4)) * 0.45;
} else {
M.eye.emissiveIntensity = 1.5 + Math.sin(t * 9) * 0.3;
M.orangeEye.emissiveIntensity = 0.08;
}
chestLight.material = t >= 10.8 ? M.eye : M.orangeEye;
// Celebratory confetti.
for (let i = 0; i < confetti.length; i++) {
const piece = confetti[i];
piece.visible = t >= 17.2;
if (!piece.visible) continue;
const localT = t - 17.2;
const cycle = (localT * piece.userData.speed + i * 0.17) % 3;
piece.position.set(
robotX + piece.userData.x,
3.45 - cycle * 0.95,
robotZ + piece.userData.z
);
piece.rotation.set(
localT * 3 + piece.userData.seed,
localT * 2 + i,
piece.userData.seed
);
}
updateStoryUI(t);
}
// -------------------------------------------------------------------------
// Custom camera controls: no addons required
// -------------------------------------------------------------------------
let dragging = false;
let previousPointerX = 0;
let previousPointerY = 0;
renderer.domElement.addEventListener("pointerdown", (event) => {
dragging = true;
previousPointerX = event.clientX;
previousPointerY = event.clientY;
renderer.domElement.setPointerCapture(event.pointerId);
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (!dragging) return;
const deltaX = event.clientX - previousPointerX;
const deltaY = event.clientY - previousPointerY;
previousPointerX = event.clientX;
previousPointerY = event.clientY;
cameraYaw -= deltaX * 0.006;
cameraPitch = THREE.MathUtils.clamp(
cameraPitch + deltaY * 0.0045,
0.22,
1.12
);
updateCamera();
});
function endDrag(event) {
dragging = false;
if (event.pointerId !== undefined) {
if (renderer.domElement.hasPointerCapture(event.pointerId)) {
renderer.domElement.releasePointerCapture(event.pointerId);
}
}
}
renderer.domElement.addEventListener("pointerup", endDrag);
renderer.domElement.addEventListener("pointercancel", endDrag);
renderer.domElement.addEventListener(
"wheel",
(event) => {
event.preventDefault();
cameraRadius = THREE.MathUtils.clamp(
cameraRadius + event.deltaY * 0.008,
7.2,
17
);
updateCamera();
},
{ passive: false }
);
// -------------------------------------------------------------------------
// UI buttons
// -------------------------------------------------------------------------
pauseButton.addEventListener("click", () => {
paused = !paused;
pauseButton.textContent = paused ? "Resume story" : "Pause story";
});
document.querySelector("#replay").addEventListener("click", () => {
simTime = 0;
currentChapter = -1;
previousRobotX = robot.position.x;
});
document.querySelector("#reset-camera").addEventListener("click", () => {
cameraYaw = 0.49;
cameraPitch = 0.51;
cameraRadius = 11.9;
updateCamera();
});
// -------------------------------------------------------------------------
// Resize and render loop
// -------------------------------------------------------------------------
window.addEventListener("resize", () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
});
function animate(now) {
requestAnimationFrame(animate);
const dt = Math.min((now - lastFrame) / 1000, 0.05);
lastFrame = now;
if (!paused) simTime += dt;
updateScene(simTime, dt);
renderer.render(scene, camera);
}
updateScene(0, 0);
requestAnimationFrame(animate);
</script>
</body>
</html>
```