Prompt specification
Build or customize the Three.js 3D scene "After-play room search 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: After-play room search story
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/after-play-room-search-story-x3875
Scene Description Prompt:
Compose a playroom with rug, toy bin, reading chair, and blanket; a caregiver and child retrace recent activities before locating the misplaced object. Use Three.js from cdnjs in a single HTML file, with visible voxel blocks.
Working Three.js Starter (after-play-room-search-story-x3875):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>The Missing Red Block</title>
<style>
:root {
--ink: #26333b;
--cream: #fffaf0;
--accent: #e96f61;
--teal: #3f9d98;
--yellow: #f2c14e;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #eee7dc;
color: var(--ink);
font-family: Inter, ui-rounded, "SF Pro Rounded", system-ui, -apple-system,
BlinkMacSystemFont, "Segoe UI", sans-serif;
}
#stage,
#stage canvas {
position: fixed;
inset: 0;
width: 100%;
height: 100%;
display: block;
touch-action: none;
}
.panel {
position: fixed;
z-index: 10;
border: 1px solid rgba(38, 51, 59, 0.12);
background: rgba(255, 250, 240, 0.92);
box-shadow: 0 14px 40px rgba(63, 49, 37, 0.15);
backdrop-filter: blur(12px);
}
#header {
top: 18px;
left: 18px;
width: min(370px, calc(100vw - 36px));
padding: 17px 19px;
border-radius: 18px;
}
#header .eyebrow {
color: var(--accent);
font-size: 11px;
font-weight: 900;
letter-spacing: 0.16em;
text-transform: uppercase;
}
#header h1 {
margin: 5px 0 4px;
font-size: clamp(22px, 3vw, 30px);
line-height: 1.08;
}
#header p {
margin: 0;
color: #647078;
font-size: 13px;
line-height: 1.45;
}
#view-controls {
top: 18px;
right: 18px;
display: flex;
gap: 8px;
padding: 8px;
border-radius: 15px;
}
button {
appearance: none;
border: 0;
border-radius: 10px;
padding: 10px 13px;
background: #e7e0d4;
color: var(--ink);
font: inherit;
font-size: 13px;
font-weight: 800;
cursor: pointer;
transition:
transform 120ms ease,
background 120ms ease,
box-shadow 120ms ease;
}
button:hover {
transform: translateY(-1px);
background: #ded4c5;
box-shadow: 0 5px 12px rgba(48, 39, 30, 0.1);
}
button:active {
transform: translateY(1px);
}
#play {
min-width: 74px;
background: var(--teal);
color: white;
}
#play:hover {
background: #348b87;
}
#story {
left: 50%;
bottom: 18px;
width: min(720px, calc(100vw - 36px));
transform: translateX(-50%);
padding: 17px 20px 16px;
border-radius: 19px;
}
#story-header {
display: flex;
align-items: center;
gap: 10px;
margin-bottom: 5px;
}
#step {
flex: 0 0 auto;
border-radius: 999px;
padding: 5px 9px;
background: var(--yellow);
color: #3b3427;
font-size: 10px;
font-weight: 950;
letter-spacing: 0.12em;
text-transform: uppercase;
}
#story-title {
margin: 0;
font-size: clamp(18px, 2.4vw, 23px);
line-height: 1.15;
}
#story-body {
margin: 5px 0 12px;
color: #5d686e;
font-size: 13px;
line-height: 1.45;
}
#timeline {
position: relative;
height: 22px;
margin: 0 2px 10px;
}
#timeline input {
--progress: 0%;
position: absolute;
z-index: 2;
top: 6px;
left: 0;
width: 100%;
height: 7px;
margin: 0;
appearance: none;
border-radius: 999px;
outline: none;
background:
linear-gradient(
to right,
var(--accent) 0 var(--progress),
#ded7cb var(--progress) 100%
);
cursor: pointer;
}
#timeline input::-webkit-slider-thumb {
width: 18px;
height: 18px;
appearance: none;
border: 3px solid var(--cream);
border-radius: 50%;
background: var(--accent);
box-shadow: 0 2px 7px rgba(48, 39, 30, 0.25);
}
#timeline input::-moz-range-thumb {
width: 13px;
height: 13px;
border: 3px solid var(--cream);
border-radius: 50%;
background: var(--accent);
box-shadow: 0 2px 7px rgba(48, 39, 30, 0.25);
}
#markers {
position: absolute;
z-index: 1;
inset: 0 8px;
}
.marker {
position: absolute;
top: 7px;
width: 5px;
height: 5px;
transform: translateX(-50%);
border-radius: 50%;
background: #a69e91;
}
.marker.active {
background: var(--accent);
transform: translateX(-50%) scale(1.45);
}
#story-controls {
display: flex;
align-items: center;
gap: 8px;
}
#time {
margin-left: auto;
color: #747d82;
font-size: 12px;
font-variant-numeric: tabular-nums;
}
#hint {
position: fixed;
z-index: 8;
right: 18px;
bottom: 18px;
padding: 8px 11px;
border-radius: 10px;
background: rgba(38, 51, 59, 0.78);
color: white;
font-size: 11px;
pointer-events: none;
}
@media (max-width: 760px) {
#header {
top: 10px;
left: 10px;
width: calc(100vw - 20px);
padding: 12px 14px;
}
#header p {
display: none;
}
#view-controls {
top: 92px;
right: 10px;
}
#story {
bottom: 10px;
width: calc(100vw - 20px);
padding: 13px 14px;
}
#story-body {
margin-bottom: 9px;
}
#hint {
display: none;
}
button {
padding: 9px 10px;
font-size: 12px;
}
}
</style>
</head>
<body>
<div id="stage"></div>
<header id="header" class="panel">
<div class="eyebrow">A voxel playroom story</div>
<h1>The Missing Red Block</h1>
<p>
A caregiver and child retrace their recent play, checking each place
before they find the misplaced toy.
</p>
</header>
<nav id="view-controls" class="panel" aria-label="Animation and view controls">
<button id="play" type="button">Pause</button>
<button id="replay" type="button">Replay</button>
<button id="reset-view" type="button">Reset view</button>
</nav>
<section id="story" class="panel" aria-live="polite">
<div id="story-header">
<span id="step">Arrival</span>
<h2 id="story-title">A small clue is missing</h2>
</div>
<p id="story-body">
The caregiver and child decide to retrace their recent play.
</p>
<div id="timeline">
<div id="markers"></div>
<input
id="scrubber"
type="range"
min="0"
max="24"
step="0.01"
value="0"
aria-label="Story timeline"
/>
</div>
<div id="story-controls">
<button id="play-mobile" type="button">Pause</button>
<button id="replay-mobile" type="button">Replay</button>
<span id="time">0:00 / 0:24</span>
</div>
</section>
<div id="hint">Drag to orbit · Shift-drag to pan · Scroll to zoom</div>
<script type="module">
import * as THREE from "https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const stage = document.getElementById("stage");
const renderer = new THREE.WebGLRenderer({
antialias: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 1.8));
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;
stage.appendChild(renderer.domElement);
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xeee7dc);
scene.fog = new THREE.Fog(0xeee7dc, 30, 58);
const camera = new THREE.OrthographicCamera(-10, 10, 10, -10, 0.1, 100);
const ambient = new THREE.HemisphereLight(0xfff7e7, 0x8a7565, 2.2);
scene.add(ambient);
const sunlight = new THREE.DirectionalLight(0xfff1d6, 3.1);
sunlight.position.set(-7, 13, 8);
sunlight.castShadow = true;
sunlight.shadow.mapSize.set(2048, 2048);
sunlight.shadow.camera.left = -13;
sunlight.shadow.camera.right = 13;
sunlight.shadow.camera.top = 13;
sunlight.shadow.camera.bottom = -13;
sunlight.shadow.camera.near = 1;
sunlight.shadow.camera.far = 35;
sunlight.shadow.bias = -0.0004;
scene.add(sunlight);
const fillLight = new THREE.DirectionalLight(0xb9ddff, 0.75);
fillLight.position.set(8, 7, -6);
scene.add(fillLight);
const palette = {
cream: 0xfff3dd,
wall: 0xf1dfc5,
wallLight: 0xf7ead5,
wood: 0xb9794f,
woodDark: 0x865138,
teal: 0x3f9d98,
tealDark: 0x2c7775,
coral: 0xe97868,
coralDark: 0xb64f49,
yellow: 0xf2c14e,
blue: 0x5b83d6,
purple: 0x8c69ac,
green: 0x75ad65,
ink: 0x28343b,
white: 0xfffdf6
};
const materialCache = new Map();
function material(color) {
if (!materialCache.has(color)) {
materialCache.set(
color,
new THREE.MeshStandardMaterial({
color,
roughness: 0.78,
metalness: 0.02
})
);
}
return materialCache.get(color);
}
function box(
width,
height,
depth,
color,
x = 0,
y = 0,
z = 0,
parent = scene,
customMaterial = null
) {
const mesh = new THREE.Mesh(
new THREE.BoxGeometry(width, height, depth),
customMaterial || material(color)
);
mesh.position.set(x, y, z);
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
// -----------------------------------------------------------------------
// Room: floor, walls, trim, window, shelves and wall art.
// -----------------------------------------------------------------------
box(16.4, 0.36, 12.4, palette.woodDark, 0, -0.23, 0);
const plankColors = [0xd5a373, 0xc89461, 0xe0b17f, 0xce9a67];
for (let ix = 0; ix < 16; ix++) {
for (let iz = 0; iz < 12; iz++) {
const color = plankColors[(ix * 5 + iz * 3) % plankColors.length];
box(
0.985,
0.12,
0.985,
color,
-7.5 + ix,
-0.02,
-5.5 + iz
);
}
}
box(16, 6.4, 0.24, palette.wall, 0, 3.2, -6.12);
box(0.24, 6.4, 12, palette.wallLight, -8.12, 3.2, 0);
// Lower wall panels and baseboards.
for (let x = -7; x <= 7; x += 2) {
box(
1.84,
0.82,
0.09,
x % 4 === 0 ? 0xe6cdb0 : 0xe9d3b9,
x,
0.45,
-5.94
);
}
box(15.8, 0.14, 0.16, palette.cream, 0, 0.12, -5.91);
box(0.16, 0.14, 11.8, palette.cream, -7.94, 0.12, 0);
// Window.
const windowGroup = new THREE.Group();
scene.add(windowGroup);
const skyMaterial = new THREE.MeshStandardMaterial({
color: 0x8fc9e8,
emissive: 0x4b8bad,
emissiveIntensity: 0.25,
roughness: 0.5
});
box(2.55, 2.05, 0.09, 0x8fc9e8, -3.15, 3.8, -5.94, windowGroup, skyMaterial);
box(2.9, 0.18, 0.18, palette.cream, -3.15, 4.9, -5.83, windowGroup);
box(2.9, 0.18, 0.18, palette.cream, -3.15, 2.7, -5.83, windowGroup);
box(0.18, 2.38, 0.18, palette.cream, -4.48, 3.8, -5.83, windowGroup);
box(0.18, 2.38, 0.18, palette.cream, -1.82, 3.8, -5.83, windowGroup);
box(0.13, 2.05, 0.15, palette.cream, -3.15, 3.8, -5.78, windowGroup);
box(2.55, 0.13, 0.15, palette.cream, -3.15, 3.8, -5.78, windowGroup);
// Pixel-art picture.
const picture = new THREE.Group();
picture.position.set(2.15, 3.85, -5.86);
scene.add(picture);
box(1.8, 1.65, 0.12, palette.wood, 0, 0, 0, picture);
box(1.5, 1.35, 0.08, palette.cream, 0, 0, 0.09, picture);
box(0.48, 0.48, 0.08, palette.blue, -0.32, 0.22, 0.15, picture);
box(0.42, 0.65, 0.08, palette.green, 0.2, 0.06, 0.15, picture);
box(0.38, 0.36, 0.08, palette.yellow, 0.35, 0.48, 0.15, picture);
// Small wall shelf with books.
box(2.7, 0.16, 0.55, palette.wood, 5.65, 2.15, -5.76);
const bookColors = [palette.coral, palette.teal, palette.yellow, palette.blue, palette.purple];
for (let i = 0; i < 5; i++) {
box(
0.28,
0.72 + (i % 2) * 0.12,
0.3,
bookColors[i],
4.65 + i * 0.46,
2.58,
-5.7
);
}
// Decorative blocks on the wall.
box(0.28, 0.28, 0.16, palette.coral, -6.75, 4.35, -5.93);
box(0.28, 0.28, 0.16, palette.yellow, -6.38, 4.35, -5.93);
box(0.28, 0.28, 0.16, palette.teal, -6.01, 4.35, -5.93);
box(0.95, 0.12, 0.13, palette.wood, -6.4, 3.98, -5.95);
// -----------------------------------------------------------------------
// Rug: individual voxel tiles.
// -----------------------------------------------------------------------
const rugCenterX = -0.6;
const rugCenterZ = 0.65;
const tileSize = 0.64;
for (let ix = 0; ix < 12; ix++) {
for (let iz = 0; iz < 8; iz++) {
const edge = ix === 0 || ix === 11 || iz === 0 || iz === 7;
const centerMark =
(ix >= 4 && ix <= 7 && iz >= 2 && iz <= 5) ||
(ix >= 5 && ix <= 6 && iz >= 1 && iz <= 6);
let color;
if (edge) {
color = palette.coral;
} else if (centerMark) {
color = palette.teal;
} else if ((ix + iz) % 3 === 0) {
color = 0xf5d9a9;
} else {
color = 0xf9e6ca;
}
box(
0.59,
0.08,
0.59,
color,
rugCenterX + (ix - 5.5) * tileSize,
0.085,
rugCenterZ + (iz - 3.5) * tileSize
);
}
}
// -----------------------------------------------------------------------
// Toy bin.
// -----------------------------------------------------------------------
const toyBin = new THREE.Group();
toyBin.position.set(-6.05, 0, -3.45);
scene.add(toyBin);
box(2.5, 0.18, 1.75, palette.tealDark, 0, 0.13, 0, toyBin);
for (let i = 0; i < 6; i++) {
const x = -1.03 + i * 0.41;
box(0.32, 0.93, 0.18, palette.teal, x, 0.63, 0.77, toyBin);
box(0.32, 0.93, 0.18, palette.teal, x, 0.63, -0.77, toyBin);
}
box(0.18, 0.93, 1.62, palette.teal, -1.17, 0.63, 0, toyBin);
box(0.18, 0.93, 1.62, palette.teal, 1.17, 0.63, 0, toyBin);
// Low front rail so the contents remain visible.
for (let i = 0; i < 5; i++) {
box(0.38, 0.45, 0.18, palette.teal, -0.9 + i * 0.45, 0.38, 0.79, toyBin);
}
box(0.48, 0.48, 0.48, palette.yellow, -0.55, 0.45, 0.2, toyBin);
box(0.45, 0.45, 0.45, palette.coral, 0.16, 0.42, -0.15, toyBin);
box(0.4, 0.4, 0.4, palette.blue, 0.67, 0.39, 0.28, toyBin);
box(0.44, 0.44, 0.44, palette.purple, -0.03, 0.8, 0.25, toyBin);
box(0.35, 0.35, 0.35, palette.green, 0.48, 0.72, -0.38, toyBin);
// -----------------------------------------------------------------------
// Block tower activity station.
// -----------------------------------------------------------------------
const tower = new THREE.Group();
tower.position.set(-3.25, 0.13, -1.28);
scene.add(tower);
box(0.88, 0.12, 0.88, palette.woodDark, 0, 0.06, 0, tower);
const towerColors = [
palette.blue,
palette.yellow,
palette.green,
palette.purple
];
for (let i = 0; i < towerColors.length; i++) {
box(
0.57,
0.57,
0.57,
towerColors[i],
i % 2 === 0 ? -0.03 : 0.03,
0.4 + i * 0.57,
i % 2 === 0 ? 0.02 : -0.02,
tower
);
}
box(0.46, 0.46, 0.46, palette.coral, 0.9, 0.35, 0.3, tower);
box(0.4, 0.4, 0.4, palette.teal, -0.75, 0.31, 0.55, tower);
// -----------------------------------------------------------------------
// Reading chair, pillow, book and block lamp.
// -----------------------------------------------------------------------
const chair = new THREE.Group();
chair.position.set(5.0, 0, -3.45);
scene.add(chair);
box(2.15, 0.34, 1.9, palette.coralDark, 0, 0.29, 0, chair);
box(1.85, 0.32, 1.48, palette.coral, 0, 0.62, 0.08, chair);
box(1.92, 2.05, 0.45, palette.coralDark, 0, 1.55, -0.62, chair);
box(1.6, 1.65, 0.18, palette.coral, 0, 1.61, -0.36, chair);
box(0.34, 0.9, 1.6, palette.coralDark, -0.88, 0.93, 0.02, chair);
box(0.34, 0.9, 1.6, palette.coralDark, 0.88, 0.93, 0.02, chair);
box(1.38, 0.24, 1.15, palette.cream, 0, 0.88, 0.13, chair);
box(0.72, 0.6, 0.25, palette.teal, 0, 1.37, -0.19, chair);
box(0.45, 0.4, 0.18, palette.yellow, 0, 1.36, -0.02, chair);
for (const x of [-0.78, 0.78]) {
for (const z of [-0.55, 0.55]) {
box(0.22, 0.28, 0.22, palette.woodDark, x, 0.16, z, chair);
}
}
const sideTable = new THREE.Group();
sideTable.position.set(6.65, 0, -3.2);
scene.add(sideTable);
box(1.05, 0.15, 1.05, palette.wood, 0, 0.92, 0, sideTable);
box(0.2, 0.88, 0.2, palette.woodDark, 0, 0.46, 0, sideTable);
box(0.72, 0.1, 0.72, palette.woodDark, 0, 0.1, 0, sideTable);
// Open book on the side table.
box(0.52, 0.08, 0.68, palette.blue, -0.16, 1.06, 0.02, sideTable);
box(0.52, 0.08, 0.68, palette.coral, 0.18, 1.06, 0.02, sideTable);
box(0.44, 0.05, 0.6, palette.cream, 0, 1.12, 0.02, sideTable);
// Voxel lamp.
box(0.12, 0.68, 0.12, palette.ink, 0.26, 1.34, -0.12, sideTable);
box(0.56, 0.32, 0.5, palette.yellow, 0.26, 1.78, -0.12, sideTable);
box(0.38, 0.08, 0.34, palette.cream, 0.26, 1.6, -0.12, sideTable);
// -----------------------------------------------------------------------
// Patchwork blanket. The middle row is hinged so it can lift.
// -----------------------------------------------------------------------
const blanketFixed = new THREE.Group();
blanketFixed.position.set(4.6, 0, 0);
scene.add(blanketFixed);
const blanketFlap = new THREE.Group();
blanketFlap.position.set(4.6, 0, 3.15);
scene.add(blanketFlap);
const blanketColors = [
palette.blue,
palette.cream,
palette.green,
palette.yellow,
palette.purple,
palette.teal
];
for (let ix = 0; ix < 4; ix++) {
const x = -0.9 + ix * 0.6;
box(
0.55,
0.11,
0.55,
blanketColors[ix % blanketColors.length],
x,
0.105,
1.95,
blanketFixed
);
box(
0.55,
0.11,
0.55,
blanketColors[(ix + 2) % blanketColors.length],
x,
0.105,
3.15,
blanketFixed
);
box(
0.55,
0.11,
0.55,
blanketColors[(ix + 4) % blanketColors.length],
x,
0.105,
-0.6,
blanketFlap
);
}
// Subtle blanket edge blocks.
for (let ix = 0; ix < 4; ix++) {
box(
0.56,
0.16,
0.12,
palette.coral,
-0.9 + ix * 0.6,
0.11,
1.64,
blanketFixed
);
}
// Small corner hints glow during the blanket search.
const blanketHint = new THREE.Group();
scene.add(blanketHint);
const hintMaterial = new THREE.MeshStandardMaterial({
color: palette.yellow,
emissive: palette.yellow,
emissiveIntensity: 0.45,
roughness: 0.45
});
const hintPositions = [
[3.58, 0.22, 1.58],
[5.62, 0.22, 1.58],
[3.58, 0.22, 3.51],
[5.62, 0.22, 3.51]
];
for (const [x, y, z] of hintPositions) {
box(0.16, 0.16, 0.16, palette.yellow, x, y, z, blanketHint, hintMaterial);
}
// The missing block starts hidden under the blanket flap.
const missingBlock = new THREE.Group();
missingBlock.position.set(5.15, -0.22, 2.55);
scene.add(missingBlock);
box(0.48, 0.48, 0.48, palette.coral, 0, 0, 0, missingBlock);
box(0.27, 0.06, 0.27, palette.coralDark, 0, 0.255, 0, missingBlock);
// Celebration pixels around the found block.
const sparkles = new THREE.Group();
sparkles.position.set(5.15, 0.72, 2.55);
scene.add(sparkles);
const sparkleData = [];
const sparkleColors = [palette.yellow, palette.teal, palette.blue, palette.coral];
for (let i = 0; i < 8; i++) {
const mesh = box(
0.12,
0.12,
0.12,
sparkleColors[i % sparkleColors.length],
0,
0,
0,
sparkles
);
sparkleData.push({
mesh,
angle: (i / 8) * Math.PI * 2,
radius: 0.45 + (i % 3) * 0.1,
speed: 0.7 + (i % 4) * 0.13
});
}
// -----------------------------------------------------------------------
// Voxel caregiver and child.
// -----------------------------------------------------------------------
function createCharacter(isChild) {
const root = new THREE.Group();
const skin = isChild ? 0xc98f65 : 0xd5a178;
const shirt = isChild ? palette.yellow : palette.teal;
const pants = isChild ? palette.purple : palette.ink;
const hair = isChild ? 0x533b31 : 0x3f302b;
const leftLeg = new THREE.Group();
const rightLeg = new THREE.Group();
leftLeg.position.set(-0.19, 0.69, 0);
rightLeg.position.set(0.19, 0.69, 0);
root.add(leftLeg, rightLeg);
box(0.22, 0.58, 0.24, pants, 0, -0.29, 0, leftLeg);
box(0.22, 0.58, 0.24, pants, 0, -0.29, 0, rightLeg);
box(0.27, 0.15, 0.39, palette.cream, 0, -0.62, 0.08, leftLeg);
box(0.27, 0.15, 0.39, palette.cream, 0, -0.62, 0.08, rightLeg);
box(0.69, 0.78, 0.42, shirt, 0, 1.06, 0, root);
box(0.53, 0.13, 0.46, isChild ? palette.coral : palette.tealDark, 0, 0.72, 0, root);
box(0.56, 0.56, 0.5, skin, 0, 1.72, 0, root);
box(0.59, 0.17, 0.54, hair, 0, 1.97, -0.015, root);
box(0.13, 0.48, 0.18, hair, -0.24, 1.77, -0.12, root);
if (!isChild) {
box(0.3, 0.3, 0.28, hair, 0, 2.07, -0.22, root);
}
box(0.065, 0.07, 0.035, palette.ink, -0.12, 1.75, 0.263, root);
box(0.065, 0.07, 0.035, palette.ink, 0.12, 1.75, 0.263, root);
box(0.08, 0.055, 0.05, 0xd89572, 0, 1.62, 0.27, root);
const leftArm = new THREE.Group();
const rightArm = new THREE.Group();
leftArm.position.set(-0.46, 1.38, 0);
rightArm.position.set(0.46, 1.38, 0);
root.add(leftArm, rightArm);
box(0.2, 0.58, 0.23, shirt, 0, -0.27, 0, leftArm);
box(0.2, 0.58, 0.23, shirt, 0, -0.27, 0, rightArm);
box(0.21, 0.2, 0.22, skin, 0, -0.61, 0, leftArm);
box(0.21, 0.2, 0.22, skin, 0, -0.61, 0, rightArm);
if (isChild) {
root.scale.setScalar(0.73);
}
root.userData = {
leftLeg,
rightLeg,
leftArm,
rightArm
};
return root;
}
const caregiver = createCharacter(false);
const child = createCharacter(true);
scene.add(caregiver, child);
// -----------------------------------------------------------------------
// Story timeline and character movement.
// -----------------------------------------------------------------------
const duration = 24;
let currentTime = 0;
let playing = true;
let previousPhase = -1;
const phases = [
{
start: 0,
badge: "Arrival",
title: "A small clue is missing",
body: "The caregiver and child decide to retrace their recent play."
},
{
start: 2.2,
badge: "Stop 1 · Toy bin",
title: "First, they check the toy bin",
body: "The blocks were sorted here, so they begin with the colorful toy bin."
},
{
start: 5.2,
badge: "Stop 2 · Tower",
title: "Next, the block tower",
body: "They rebuild the route to the little tower and check the play rug."
},
{
start: 8.5,
badge: "Stop 3 · Reading chair",
title: "Then they visit the reading chair",
body: "A storybook was read here, but the missing block is not beside the chair."
},
{
start: 12,
badge: "Stop 4 · Blanket",
title: "The blanket was used last",
body: "The child remembers building a blanket fort, so they head toward the patchwork."
},
{
start: 15.5,
badge: "Searching",
title: "They lift the blanket edge",
body: "The caregiver watches carefully while the child checks beneath the soft squares."
},
{
start: 19,
badge: "Found",
title: "There is the missing red block!",
body: "It was tucked under the blanket. Together, they celebrate the successful search."
}
];
const track = [
{ t: 0, c: [-1.15, 2.75], k: [-1.95, 3.0], a: [-3.2, -1.2] },
{ t: 2.2, c: [-1.15, 2.75], k: [-1.95, 3.0], a: [-3.2, -1.2] },
{ t: 5.0, c: [-4.72, -1.08], k: [-5.43, -1.0], a: [-5.95, -3.4] },
{ t: 8.3, c: [-2.5, 0.23], k: [-3.32, 0.48], a: [-3.25, -1.28] },
{ t: 11.7, c: [4.05, -1.7], k: [3.28, -1.45], a: [5.0, -3.4] },
{ t: 15.2, c: [4.22, 1.18], k: [3.32, 1.42], a: [4.65, 2.55] },
{ t: 18.5, c: [4.9, 1.62], k: [3.62, 1.63], a: [5.15, 2.55] },
{ t: 24, c: [4.9, 1.62], k: [3.62, 1.63], a: [5.15, 2.55] }
];
function clamp01(value) {
return Math.max(0, Math.min(1, value));
}
function smooth01(value) {
const x = clamp01(value);
return x * x * (3 - 2 * x);
}
function lerp(a, b, t) {
return a + (b - a) * t;
}
function sampleTrack(time, field) {
if (time <= track[0].t) {
return [...track[0][field]];
}
for (let i = 0; i < track.length - 1; i++) {
const a = track[i];
const b = track[i + 1];
if (time >= a.t && time <= b.t) {
const raw = (time - a.t) / (b.t - a.t);
const t = smooth01(raw);
return [
lerp(a[field][0], b[field][0], t),
lerp(a[field][1], b[field][1], t)
];
}
}
return [...track[track.length - 1][field]];
}
function distance2D(a, b) {
return Math.hypot(a[0] - b[0], a[1] - b[1]);
}
function groundHeight(x, z) {
const onRug =
x > -4.48 &&
x < 3.28 &&
z > -1.95 &&
z < 3.25;
return onRug ? 0.135 : 0.045;
}
function phaseAt(time) {
let index = 0;
for (let i = 0; i < phases.length; i++) {
if (time >= phases[i].start) {
index = i;
}
}
return index;
}
function updateCharacter(character, position, aim, time, phase, isChild) {
const future = sampleTrack(Math.min(duration, time + 0.08), isChild ? "k" : "c");
const moving = distance2D(position, future) > 0.025;
const walk = Math.sin(time * 10.5);
const celebrate = phase === 6 ? smooth01((time - 19) / 1.4) : 0;
character.position.x = position[0];
character.position.z = position[1];
character.position.y =
groundHeight(position[0], position[1]) +
(moving ? Math.abs(walk) * 0.035 : 0) +
celebrate * Math.abs(Math.sin((time - 19) * 5.2)) * 0.16;
const yaw = Math.atan2(aim[0] - position[0], aim[1] - position[1]);
character.rotation.y = yaw;
const data = character.userData;
data.leftLeg.rotation.x = moving ? walk * 0.42 : 0;
data.rightLeg.rotation.x = moving ? -walk * 0.42 : 0;
if (isChild) {
data.leftArm.rotation.x = moving ? -walk * 0.34 : 0.05;
data.rightArm.rotation.x = moving ? walk * 0.34 : -0.05;
data.leftArm.rotation.z = celebrate * -2.15;
data.rightArm.rotation.z = celebrate * 2.15;
} else {
const pointing = phase >= 1 && phase <= 5;
const searchDown = phase === 5;
data.leftArm.rotation.x = moving ? -walk * 0.28 : -0.04;
data.leftArm.rotation.z = 0;
if (phase === 6) {
data.rightArm.rotation.x = lerp(-0.05, -2.45, celebrate);
data.rightArm.rotation.z = lerp(0, -0.22, celebrate);
} else if (pointing) {
data.rightArm.rotation.x = searchDown ? -0.48 : -1.05;
data.rightArm.rotation.z = -0.05;
} else {
data.rightArm.rotation.x = moving ? walk * 0.28 : -0.04;
data.rightArm.rotation.z = 0;
}
}
}
const markersContainer = document.getElementById("markers");
const markerElements = [];
for (const phase of phases) {
const marker = document.createElement("span");
marker.className = "marker";
marker.style.left = `${(phase.start / duration) * 100}%`;
marker.title = phase.badge;
markersContainer.appendChild(marker);
markerElements.push(marker);
}
const stepElement = document.getElementById("step");
const titleElement = document.getElementById("story-title");
const bodyElement = document.getElementById("story-body");
const scrubber = document.getElementById("scrubber");
const timeElement = document.getElementById("time");
const playButton = document.getElementById("play");
const playMobileButton = document.getElementById("play-mobile");
function formatTime(time) {
const seconds = Math.floor(time);
return `0:${String(seconds).padStart(2, "0")}`;
}
function updateStoryUI(phase) {
if (phase === previousPhase) {
return;
}
previousPhase = phase;
const data = phases[phase];
stepElement.textContent = data.badge;
titleElement.textContent = data.title;
bodyElement.textContent = data.body;
markerElements.forEach((marker, index) => {
marker.classList.toggle("active", index === phase);
});
}
function updateButtonLabels() {
const label = playing ? "Pause" : currentTime >= duration - 0.01 ? "Replay" : "Play";
playButton.textContent = label;
playMobileButton.textContent = label;
}
function togglePlayback() {
if (currentTime >= duration - 0.01) {
currentTime = 0;
}
playing = !playing;
updateButtonLabels();
}
playButton.addEventListener("click", togglePlayback);
playMobileButton.addEventListener("click", togglePlayback);
document.getElementById("replay").addEventListener("click", () => {
currentTime = 0;
playing = true;
updateButtonLabels();
});
document.getElementById("replay-mobile").addEventListener("click", () => {
currentTime = 0;
playing = true;
updateButtonLabels();
});
scrubber.addEventListener("input", () => {
currentTime = Number(scrubber.value);
playing = false;
updateButtonLabels();
});
window.addEventListener("keydown", (event) => {
if (event.code === "Space" && event.target !== scrubber) {
event.preventDefault();
togglePlayback();
}
});
// -----------------------------------------------------------------------
// Custom camera controls: orbit, pan and zoom, without addons.
// -----------------------------------------------------------------------
const cameraState = {
azimuth: 0.62,
polar: 0.98,
zoom: 1,
target: new THREE.Vector3(0, 0.95, -0.2)
};
function resetCamera() {
cameraState.azimuth = 0.62;
cameraState.polar = 0.98;
cameraState.zoom = 1;
cameraState.target.set(0, 0.95, -0.2);
}
document.getElementById("reset-view").addEventListener("click", resetCamera);
renderer.domElement.addEventListener("dblclick", resetCamera);
function updateCamera() {
const radius = 28;
const sinPolar = Math.sin(cameraState.polar);
camera.position.set(
cameraState.target.x + radius * sinPolar * Math.sin(cameraState.azimuth),
cameraState.target.y + radius * Math.cos(cameraState.polar),
cameraState.target.z + radius * sinPolar * Math.cos(cameraState.azimuth)
);
camera.lookAt(cameraState.target);
camera.zoom = cameraState.zoom;
camera.updateProjectionMatrix();
}
let drag = null;
renderer.domElement.addEventListener("contextmenu", (event) => {
event.preventDefault();
});
renderer.domElement.addEventListener("pointerdown", (event) => {
renderer.domElement.setPointerCapture(event.pointerId);
drag = {
id: event.pointerId,
x: event.clientX,
y: event.clientY,
pan: event.shiftKey || event.button === 2
};
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (!drag || drag.id !== event.pointerId) {
return;
}
const dx = event.clientX - drag.x;
const dy = event.clientY - drag.y;
drag.x = event.clientX;
drag.y = event.clientY;
if (drag.pan) {
const worldPerPixel =
((camera.top - camera.bottom) / camera.zoom) / window.innerHeight;
const right = new THREE.Vector3(
Math.cos(cameraState.azimuth),
0,
-Math.sin(cameraState.azimuth)
);
const forward = new THREE.Vector3(
-Math.sin(cameraState.azimuth),
0,
-Math.cos(cameraState.azimuth)
);
cameraState.target.addScaledVector(right, -dx * worldPerPixel);
cameraState.target.addScaledVector(forward, dy * worldPerPixel);
cameraState.target.x = THREE.MathUtils.clamp(
cameraState.target.x,
-6,
6
);
cameraState.target.z = THREE.MathUtils.clamp(
cameraState.target.z,
-5,
5
);
} else {
cameraState.azimuth -= dx * 0.006;
cameraState.polar = THREE.MathUtils.clamp(
cameraState.polar + dy * 0.0045,
0.55,
1.3
);
}
});
function endDrag(event) {
if (drag && drag.id === event.pointerId) {
drag = null;
}
}
renderer.domElement.addEventListener("pointerup", endDrag);
renderer.domElement.addEventListener("pointercancel", endDrag);
renderer.domElement.addEventListener(
"wheel",
(event) => {
event.preventDefault();
cameraState.zoom = THREE.MathUtils.clamp(
cameraState.zoom * Math.exp(-event.deltaY * 0.001),
0.65,
2.25
);
},
{ passive: false }
);
function resize() {
const width = window.innerWidth;
const height = window.innerHeight;
const aspect = width / height;
const viewHeight = Math.max(11.2, 21.5 / aspect);
renderer.setSize(width, height);
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 1.8));
camera.left = (-viewHeight * aspect) / 2;
camera.right = (viewHeight * aspect) / 2;
camera.top = viewHeight / 2;
camera.bottom = -viewHeight / 2;
camera.updateProjectionMatrix();
}
window.addEventListener("resize", resize);
resize();
updateCamera();
// -----------------------------------------------------------------------
// Main animation loop.
// -----------------------------------------------------------------------
function updateScene(time) {
const phase = phaseAt(time);
updateStoryUI(phase);
const caregiverPosition = sampleTrack(time, "c");
const childPosition = sampleTrack(Math.max(0, time - 0.16), "k");
const caregiverAim = sampleTrack(time, "a");
const childAim = sampleTrack(Math.max(0, time - 0.16), "a");
updateCharacter(
caregiver,
caregiverPosition,
caregiverAim,
time,
phase,
false
);
updateCharacter(
child,
childPosition,
childAim,
time,
phase,
true
);
// The blanket flap opens and the block rises into view.
const reveal = smooth01((time - 19) / 2.0);
blanketFlap.rotation.x = reveal * 1.18;
missingBlock.visible = time >= 18.98;
missingBlock.position.y = -0.22 + reveal * 0.83;
missingBlock.rotation.y = (time - 19) * 1.55;
missingBlock.rotation.z = Math.sin((time - 19) * 2.2) * 0.08;
blanketHint.visible = phase === 4 || phase === 5;
const hintPulse = 1 + Math.sin(time * 6) * 0.18;
blanketHint.scale.setScalar(hintPulse);
sparkles.visible = reveal > 0.25;
for (const sparkle of sparkleData) {
const angle = sparkle.angle + time * sparkle.speed;
const radius = sparkle.radius * (0.75 + reveal * 0.35);
sparkle.mesh.position.set(
Math.cos(angle) * radius,
Math.sin(time * 3 + sparkle.angle) * 0.14,
Math.sin(angle) * radius
);
sparkle.mesh.rotation.set(
time * 1.7 + sparkle.angle,
time * 1.2,
time + sparkle.angle
);
}
scrubber.value = time;
scrubber.style.setProperty("--progress", `${(time / duration) * 100}%`);
timeElement.textContent = `${formatTime(time)} / 0:24`;
}
let lastTime = performance.now();
function animate(now) {
requestAnimationFrame(animate);
const delta = Math.min((now - lastTime) / 1000, 0.05);
lastTime = now;
if (playing) {
currentTime += delta;
if (currentTime >= duration) {
currentTime = duration;
playing = false;
updateButtonLabels();
}
}
updateScene(currentTime);
updateCamera();
renderer.render(scene, camera);
}
updateButtonLabels();
updateScene(0);
requestAnimationFrame(animate);
</script>
</body>
</html>
```