Prompt specification
Build or customize the Three.js 3D scene "Neighborhood Lost Toy Reunion" 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: Neighborhood Lost Toy Reunion
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/neighborhood-lost-toy-reunion-x3703
Scene Description Prompt:
A voxel neighborhood story: a character finds a lost toy, checks ownership cues, and returns it. One HTML file using Three.js from cdnjs.
Working Three.js Starter (neighborhood-lost-toy-reunion-x3703):
```html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>One Small Thing — A Voxel Neighborhood Story</title>
<style>
:root {
--ink: #24384a;
--muted: #607483;
--paper: #fffdf5;
--mint: #62c6b2;
--coral: #ed7867;
--gold: #f2bf5b;
--shadow: rgba(35, 56, 70, 0.18);
}
* { box-sizing: border-box; }
html, body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #dce9e3;
color: var(--ink);
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}
button { font: inherit; }
canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
outline: none;
}
#loading {
position: fixed;
inset: 0;
display: grid;
place-items: center;
z-index: 20;
background: #dce9e3;
font: 700 13px/1.4 ui-monospace, SFMono-Regular, Menlo, monospace;
letter-spacing: .12em;
text-transform: uppercase;
}
#loading span {
padding: 16px 20px;
background: var(--paper);
border: 3px solid var(--ink);
box-shadow: 6px 6px 0 rgba(36, 56, 74, .12);
}
#brand {
position: fixed;
top: 22px;
left: 24px;
z-index: 5;
display: flex;
align-items: center;
gap: 12px;
pointer-events: none;
}
#brandMark {
width: 42px;
height: 42px;
display: grid;
place-items: center;
background: var(--coral);
border: 3px solid var(--ink);
box-shadow: 4px 4px 0 rgba(36, 56, 74, .12);
font-size: 23px;
transform: rotate(-4deg);
}
#brand strong {
display: block;
font: 800 13px/1.1 ui-monospace, SFMono-Regular, Menlo, monospace;
letter-spacing: .12em;
}
#brand small {
display: block;
margin-top: 4px;
color: var(--muted);
font-size: 11px;
font-weight: 650;
}
#progress {
position: fixed;
top: 26px;
left: 50%;
z-index: 5;
display: flex;
gap: 5px;
transform: translateX(-50%);
pointer-events: none;
}
.progress-step {
position: relative;
width: 52px;
height: 7px;
overflow: hidden;
background: rgba(255, 253, 245, .72);
border: 1px solid rgba(36, 56, 74, .18);
}
.progress-step::after {
content: "";
position: absolute;
inset: 0;
background: var(--mint);
transform: scaleX(0);
transform-origin: left;
transition: transform .4s ease;
}
.progress-step.active::after { transform: scaleX(1); }
.progress-step.done::after { background: var(--gold); }
#cameraControls {
position: fixed;
top: 20px;
right: 22px;
z-index: 6;
display: flex;
align-items: center;
gap: 6px;
padding: 6px;
background: rgba(255, 253, 245, .92);
border: 2px solid rgba(36, 56, 74, .18);
box-shadow: 0 6px 20px var(--shadow);
}
#cameraControls button {
width: 34px;
height: 34px;
border: 0;
background: #eef3ec;
color: var(--ink);
cursor: pointer;
font-weight: 800;
}
#cameraControls button:hover { background: #d9ebe3; }
#cameraControls button:active { transform: translateY(1px); }
#cameraControls span {
padding: 0 7px;
color: var(--muted);
font: 650 10px/1 ui-monospace, SFMono-Regular, Menlo, monospace;
letter-spacing: .04em;
}
#storyCard {
position: fixed;
z-index: 7;
left: 50%;
bottom: 22px;
width: min(680px, calc(100% - 32px));
padding: 20px 22px 18px;
transform: translateX(-50%);
background: rgba(255, 253, 245, .96);
border: 3px solid var(--ink);
box-shadow: 8px 8px 0 rgba(36, 56, 74, .13);
}
#storyCard::before {
content: "";
position: absolute;
top: -8px;
left: 22px;
width: 42px;
height: 8px;
background: var(--coral);
border: 2px solid var(--ink);
border-bottom: 0;
}
#chapter {
color: var(--coral);
font: 800 10px/1 ui-monospace, SFMono-Regular, Menlo, monospace;
letter-spacing: .14em;
text-transform: uppercase;
}
#storyTitle {
margin: 8px 0 6px;
font-size: clamp(20px, 3vw, 28px);
line-height: 1.08;
letter-spacing: -.035em;
}
#storyText {
margin: 0;
max-width: 590px;
color: #526675;
font-size: 14px;
line-height: 1.55;
}
.storyActions {
display: flex;
align-items: center;
gap: 13px;
margin-top: 15px;
}
#actionButton {
flex: 0 0 auto;
min-height: 43px;
padding: 10px 17px;
border: 2px solid var(--ink);
background: var(--ink);
color: #fffdf5;
box-shadow: 4px 4px 0 var(--gold);
cursor: pointer;
font-weight: 800;
font-size: 13px;
transition: transform .12s ease, box-shadow .12s ease, opacity .2s ease;
}
#actionButton:hover:not(:disabled) {
transform: translate(-1px, -1px);
box-shadow: 6px 6px 0 var(--gold);
}
#actionButton:active:not(:disabled) {
transform: translate(2px, 2px);
box-shadow: 2px 2px 0 var(--gold);
}
#actionButton:disabled {
cursor: wait;
opacity: .62;
}
#hint {
color: #71828e;
font-size: 11px;
line-height: 1.35;
}
#bubble {
position: fixed;
z-index: 8;
max-width: min(290px, calc(100% - 30px));
padding: 11px 13px;
transform: translate(-50%, -105%);
background: #fffdf5;
border: 2px solid var(--ink);
box-shadow: 5px 5px 0 rgba(36, 56, 74, .14);
color: var(--ink);
font-size: 12px;
font-weight: 700;
line-height: 1.4;
pointer-events: none;
opacity: 0;
transition: opacity .2s ease;
}
#bubble::after {
content: "";
position: absolute;
left: 50%;
bottom: -8px;
width: 12px;
height: 12px;
transform: translateX(-50%) rotate(45deg);
background: #fffdf5;
border-right: 2px solid var(--ink);
border-bottom: 2px solid var(--ink);
}
#bubble.visible { opacity: 1; }
#clueCard {
position: fixed;
z-index: 5;
left: 24px;
bottom: 30px;
width: 178px;
padding: 13px;
background: rgba(255, 253, 245, .9);
border: 2px solid rgba(36, 56, 74, .22);
box-shadow: 5px 5px 0 rgba(36, 56, 74, .1);
opacity: 0;
transform: translateY(12px);
pointer-events: none;
transition: opacity .35s ease, transform .35s ease;
}
#clueCard.visible {
opacity: 1;
transform: translateY(0);
}
#clueCard strong {
display: block;
margin-bottom: 9px;
font: 800 10px/1 ui-monospace, SFMono-Regular, Menlo, monospace;
letter-spacing: .08em;
text-transform: uppercase;
}
.clueRow {
display: flex;
align-items: center;
gap: 8px;
margin-top: 7px;
color: #5f7280;
font-size: 11px;
font-weight: 700;
}
.swatch {
width: 20px;
height: 12px;
background: var(--mint);
border: 2px solid var(--ink);
}
.stars {
color: var(--gold);
font-size: 15px;
letter-spacing: 1px;
text-shadow: 1px 1px 0 var(--ink);
}
@media (max-width: 760px) {
#brand { top: 13px; left: 13px; }
#brandMark { width: 34px; height: 34px; font-size: 18px; }
#brand strong { font-size: 10px; }
#brand small { display: none; }
#progress { top: 22px; left: auto; right: 13px; transform: none; }
.progress-step { width: 22px; }
#cameraControls { top: 55px; right: 13px; }
#cameraControls span { display: none; }
#storyCard { bottom: 12px; padding: 17px 17px 15px; }
#storyText { font-size: 13px; }
.storyActions { align-items: flex-start; flex-direction: column; gap: 10px; }
#actionButton { width: 100%; }
#clueCard { display: none; }
}
@media (max-height: 650px) {
#storyCard { padding: 13px 16px; bottom: 10px; }
#storyTitle { margin: 5px 0; font-size: 19px; }
#storyText { font-size: 12px; line-height: 1.4; }
.storyActions { margin-top: 9px; }
#actionButton { min-height: 36px; padding: 7px 13px; }
}
</style>
</head>
<body>
<div id="loading"><span>Building the neighborhood…</span></div>
<header id="brand" aria-label="Story title">
<div id="brandMark" aria-hidden="true">◆</div>
<div>
<strong>ONE SMALL THING</strong>
<small>A voxel neighborhood story</small>
</div>
</header>
<nav id="progress" aria-label="Story progress">
<i class="progress-step"></i>
<i class="progress-step"></i>
<i class="progress-step"></i>
<i class="progress-step"></i>
<i class="progress-step"></i>
</nav>
<nav id="cameraControls" aria-label="Camera controls">
<button id="zoomOut" aria-label="Zoom out">−</button>
<button id="zoomIn" aria-label="Zoom in">+</button>
<button id="resetView" aria-label="Reset camera view">↺</button>
<span>DRAG · ORBIT</span>
</nav>
<div id="bubble" role="status" aria-live="polite"></div>
<aside id="clueCard" aria-label="Ownership clues">
<strong>Two matching clues</strong>
<div class="clueRow"><span class="swatch"></span> Mint-green scarf</div>
<div class="clueRow"><span class="stars">✦✦✦</span> Three gold stars</div>
</aside>
<main id="storyCard">
<div id="chapter">Chapter 1 of 5</div>
<h1 id="storyTitle">A little thing, left behind</h1>
<p id="storyText" aria-live="polite">
Milo is taking the long way home when a coral shape catches the afternoon light near the park bench.
</p>
<div class="storyActions">
<button id="actionButton">Pick up Pip</button>
<span id="hint">You can also tap the little fox in the grass.</span>
</div>
</main>
<script type="module">
import * as THREE from "https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const reducedMotion = window.matchMedia("(prefers-reduced-motion: reduce)").matches;
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xdce9e3);
const camera = new THREE.OrthographicCamera(-10, 10, 8, -8, 0.1, 100);
const renderer = new THREE.WebGLRenderer({ antialias: true });
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("aria-label", "Interactive voxel neighborhood");
renderer.domElement.setAttribute("tabindex", "0");
document.body.prepend(renderer.domElement);
const clock = new THREE.Clock();
const unitBox = new THREE.BoxGeometry(1, 1, 1);
const materialCache = new Map();
function material(color) {
if (!materialCache.has(color)) {
materialCache.set(color, new THREE.MeshStandardMaterial({
color,
roughness: 0.86,
metalness: 0
}));
}
return materialCache.get(color);
}
function box(parent, size, position, color, options = {}) {
const mesh = new THREE.Mesh(
options.geometry || unitBox,
options.material || material(color)
);
mesh.scale.set(size[0], size[1], size[2]);
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);
parent.add(mesh);
return mesh;
}
function canvasTexture(canvas) {
const texture = new THREE.CanvasTexture(canvas);
texture.colorSpace = THREE.SRGBColorSpace;
texture.magFilter = THREE.NearestFilter;
texture.minFilter = THREE.LinearMipmapLinearFilter;
texture.anisotropy = renderer.capabilities.getMaxAnisotropy();
return texture;
}
function signTexture(title, subtitle, background = "#fff9df", foreground = "#24384a", accent = "#ed7867") {
const canvas = document.createElement("canvas");
canvas.width = 1024;
canvas.height = 256;
const ctx = canvas.getContext("2d");
ctx.imageSmoothingEnabled = false;
ctx.fillStyle = background;
ctx.fillRect(0, 0, canvas.width, canvas.height);
ctx.fillStyle = accent;
ctx.fillRect(0, 0, canvas.width, 18);
ctx.fillRect(0, canvas.height - 18, canvas.width, 18);
ctx.strokeStyle = foreground;
ctx.lineWidth = 10;
ctx.strokeRect(10, 10, canvas.width - 20, canvas.height - 20);
ctx.textAlign = "center";
ctx.textBaseline = "middle";
ctx.fillStyle = foreground;
ctx.font = "800 74px monospace";
ctx.fillText(title, 512, subtitle ? 102 : 128);
if (subtitle) {
ctx.fillStyle = accent;
ctx.font = "700 34px monospace";
ctx.fillText(subtitle, 512, 182);
}
return canvasTexture(canvas);
}
function posterTexture() {
const canvas = document.createElement("canvas");
canvas.width = 512;
canvas.height = 640;
const ctx = canvas.getContext("2d");
ctx.imageSmoothingEnabled = false;
ctx.fillStyle = "#fff8df";
ctx.fillRect(0, 0, 512, 640);
ctx.strokeStyle = "#24384a";
ctx.lineWidth = 14;
ctx.strokeRect(10, 10, 492, 620);
ctx.fillStyle = "#ed7867";
ctx.fillRect(10, 10, 492, 78);
ctx.textAlign = "center";
ctx.fillStyle = "#fff8df";
ctx.font = "800 44px monospace";
ctx.fillText("LOST TOY", 256, 61);
ctx.fillStyle = "#ed7867";
ctx.fillRect(178, 313, 156, 160);
ctx.fillRect(156, 187, 200, 154);
ctx.fillRect(153, 118, 49, 102);
ctx.fillRect(310, 118, 49, 102);
ctx.fillStyle = "#fff0cf";
ctx.fillRect(191, 237, 130, 61);
ctx.fillRect(235, 410, 42, 69);
ctx.fillRect(158, 453, 51, 25);
ctx.fillRect(303, 453, 51, 25);
ctx.fillStyle = "#24384a";
ctx.fillRect(194, 207, 18, 22);
ctx.fillRect(300, 207, 18, 22);
ctx.fillRect(246, 264, 20, 10);
ctx.fillStyle = "#62c6b2";
ctx.fillRect(150, 325, 212, 31);
ctx.fillRect(326, 346, 28, 100);
ctx.fillStyle = "#fff0cf";
ctx.fillRect(290, 407, 61, 47);
ctx.fillStyle = "#f2bf5b";
ctx.fillRect(298, 412, 10, 37);
ctx.fillRect(285, 424, 36, 12);
ctx.fillStyle = "#24384a";
ctx.font = "800 64px monospace";
ctx.fillText("PIP", 256, 546);
ctx.font = "700 21px monospace";
ctx.fillText("MINT SCARF · 3 GOLD STARS", 256, 588);
ctx.fillStyle = "#ed7867";
ctx.beginPath();
ctx.arc(42, 42, 10, 0, Math.PI * 2);
ctx.arc(470, 42, 10, 0, Math.PI * 2);
ctx.fill();
return canvasTexture(canvas);
}
function label(parent, texture, size, position, rotation = [0, 0, 0]) {
const mesh = new THREE.Mesh(
new THREE.PlaneGeometry(size[0], size[1]),
new THREE.MeshBasicMaterial({
map: texture,
side: THREE.DoubleSide,
toneMapped: false
})
);
mesh.position.set(...position);
mesh.rotation.set(...rotation);
parent.add(mesh);
return mesh;
}
// Lighting
scene.add(new THREE.HemisphereLight(0xfffbef, 0x758b82, 2.2));
const sun = new THREE.DirectionalLight(0xfff1cf, 3.4);
sun.position.set(-8, 14, 10);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -18;
sun.shadow.camera.right = 18;
sun.shadow.camera.top = 18;
sun.shadow.camera.bottom = -18;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 45;
sun.shadow.bias = -0.0008;
scene.add(sun);
const fill = new THREE.DirectionalLight(0xb9dfff, 0.8);
fill.position.set(10, 7, -8);
scene.add(fill);
// Diorama base and tiled ground
box(scene, [25, 1.1, 15], [0, -0.58, 0], 0x687d78);
box(scene, [24.5, 0.24, 14.5], [0, -0.14, 0], 0xb7c5b7);
const tileCountX = 24;
const tileCountZ = 14;
const tiles = new THREE.InstancedMesh(
unitBox,
new THREE.MeshStandardMaterial({ color: 0xffffff, roughness: 1 }),
tileCountX * tileCountZ
);
tiles.receiveShadow = true;
const dummy = new THREE.Object3D();
const tileColor = new THREE.Color();
let tileIndex = 0;
for (let x = 0; x < tileCountX; x++) {
for (let z = 0; z < tileCountZ; z++) {
const wx = -11.5 + x;
const wz = -6.5 + z;
dummy.position.set(wx, 0.015, wz);
dummy.scale.set(0.985, 0.09, 0.985);
dummy.updateMatrix();
tiles.setMatrixAt(tileIndex, dummy.matrix);
if (wz > 3.65 && wz < 6.35) {
tileColor.set((x + z) % 2 ? 0x718890 : 0x6b8189);
} else if (wx < -4.1 && wz < 2.8) {
tileColor.set((x + z) % 3 ? 0x8fbd87 : 0x98c391);
} else {
const shades = [0xe4ddc8, 0xded9c5, 0xebe3ce, 0xd9d6c5];
tileColor.set(shades[(x * 7 + z * 3) % shades.length]);
}
tiles.setColorAt(tileIndex++, tileColor);
}
}
tiles.instanceMatrix.needsUpdate = true;
tiles.instanceColor.needsUpdate = true;
scene.add(tiles);
// Road markings and curb
for (let x = -10; x <= 10; x += 2.4) {
box(scene, [1.05, 0.025, 0.09], [x, 0.085, 5.0], 0xf4e7bd, { castShadow: false });
}
box(scene, [24.5, 0.16, 0.25], [0, 0.09, 3.58], 0xf2ead6);
box(scene, [24.5, 0.16, 0.25], [0, 0.09, 6.36], 0xf2ead6);
// Background neighborhood
function createHouse(x, z, width, height, depth, wallColor, roofColor) {
const group = new THREE.Group();
group.position.set(x, 0.06, z);
scene.add(group);
box(group, [width, height, depth], [0, height / 2, 0], wallColor);
box(group, [width + 0.22, 0.28, depth + 0.22], [0, 0.14, 0], 0x77837e);
box(group, [width + 0.35, 0.36, depth + 0.35], [0, height + 0.16, 0], roofColor);
box(group, [width * 0.78, 0.34, depth * 0.88], [0, height + 0.47, 0], roofColor);
box(group, [width * 0.5, 0.32, depth * 0.75], [0, height + 0.77, 0], roofColor);
const windowMat = new THREE.MeshStandardMaterial({
color: 0x9acbd2,
emissive: 0x4f7f85,
emissiveIntensity: 0.15,
roughness: 0.5
});
function windowPanel(wx, wy, ww = 0.85, wh = 1.0) {
box(group, [ww + 0.16, wh + 0.16, 0.12], [wx, wy, depth / 2 + 0.03], 0xf7f0d9);
box(group, [ww, wh, 0.08], [wx, wy, depth / 2 + 0.105], 0xffffff, { material: windowMat });
box(group, [0.06, wh, 0.03], [wx, wy, depth / 2 + 0.155], 0x748b89, { castShadow: false });
box(group, [ww, 0.06, 0.03], [wx, wy, depth / 2 + 0.155], 0x748b89, { castShadow: false });
}
windowPanel(-width * 0.27, height * 0.57);
windowPanel(width * 0.27, height * 0.57);
box(group, [0.82, 1.48, 0.16], [0, 0.74, depth / 2 + 0.06], 0x536b6f);
box(group, [0.62, 0.58, 0.1], [0, 1.03, depth / 2 + 0.16], 0x91b9bd, { material: windowMat });
box(group, [0.08, 0.08, 0.08], [0.25, 0.7, depth / 2 + 0.17], 0xf2bf5b);
return group;
}
createHouse(-4.3, -4.9, 4.15, 3.65, 3.1, 0xe8a68e, 0x546b7b);
createHouse(-0.2, -5.0, 3.75, 4.15, 3.1, 0xead5a7, 0x8b5960);
createHouse(9.65, -4.85, 3.5, 4.2, 3.25, 0xc6b7cf, 0x56677d);
// Little Sprout toy shop
const shop = new THREE.Group();
shop.position.set(5.25, 0.06, -4.45);
shop.userData.action = "owner";
scene.add(shop);
box(shop, [5.0, 4.45, 3.45], [0, 2.225, 0], 0x85cdb9);
box(shop, [5.18, 0.25, 3.6], [0, 0.13, 0], 0x71877e);
box(shop, [5.35, 0.38, 3.7], [0, 4.55, 0], 0x3f6870);
box(shop, [4.25, 0.34, 3.15], [0, 4.86, 0], 0x507982);
box(shop, [3.0, 0.32, 2.55], [0, 5.16, 0], 0x638b91);
const shopTexture = signTexture("LITTLE SPROUT", "TOYS & GOOD THINGS", "#fff8dc", "#294c50", "#ed7867");
label(shop, shopTexture, [4.25, 0.96], [0, 3.73, 1.735]);
const shopGlass = new THREE.MeshStandardMaterial({
color: 0xb8e3df,
emissive: 0x426f69,
emissiveIntensity: 0.16,
roughness: 0.45
});
box(shop, [1.75, 1.65, 0.12], [-1.05, 1.72, 1.75], 0xf8f0d9);
box(shop, [1.54, 1.44, 0.08], [-1.05, 1.72, 1.83], 0xffffff, { material: shopGlass });
box(shop, [0.07, 1.45, 0.04], [-1.05, 1.72, 1.89], 0x5b7774, { castShadow: false });
box(shop, [1.08, 2.05, 0.15], [1.18, 1.04, 1.75], 0x48686a);
box(shop, [0.77, 0.72, 0.08], [1.18, 1.47, 1.85], 0xa8d5d0, { material: shopGlass });
box(shop, [0.09, 0.09, 0.09], [1.53, 0.98, 1.87], 0xf2bf5b);
for (let i = 0; i < 10; i++) {
box(shop, [0.5, 0.16, 1.0], [-2.25 + i * 0.5, 3.08, 1.98], i % 2 ? 0xfff4d8 : 0xed7867);
}
box(shop, [5.05, 0.16, 1.06], [0, 2.96, 1.98], 0xf9edcf);
// Tiny toys in the shop window
box(shop, [0.28, 0.32, 0.25], [-1.55, 1.42, 2.03], 0xed7867);
box(shop, [0.3, 0.2, 0.25], [-1.15, 1.3, 2.03], 0xf2bf5b);
box(shop, [0.24, 0.45, 0.24], [-0.72, 1.33, 2.03], 0x7ba8c7);
// Notice board
const board = new THREE.Group();
board.position.set(-0.65, 0.06, -1.35);
board.rotation.y = 0.04;
board.userData.action = "board";
scene.add(board);
box(board, [0.16, 1.7, 0.18], [-0.72, 0.85, 0], 0x526961);
box(board, [0.16, 1.7, 0.18], [0.72, 0.85, 0], 0x526961);
box(board, [2.3, 2.35, 0.2], [0, 2.05, 0], 0x6e584e);
box(board, [2.08, 2.13, 0.08], [0, 2.05, 0.13], 0x496f62);
box(board, [2.48, 0.18, 0.45], [0, 3.32, 0.03], 0x9b6c4f);
label(board, posterTexture(), [1.72, 2.08], [0, 2.07, 0.185]);
// Small pinned notices
box(board, [0.32, 0.38, 0.025], [-0.86, 2.74, 0.2], 0xf4cf76, { rotation: [0, 0, 0.09] });
box(board, [0.3, 0.34, 0.025], [0.83, 1.3, 0.2], 0xaed6d0, { rotation: [0, 0, -0.08] });
// Trees and park
function createTree(x, z, scale = 1) {
const tree = new THREE.Group();
tree.position.set(x, 0.06, z);
tree.scale.setScalar(scale);
scene.add(tree);
box(tree, [0.48, 2.25, 0.48], [0, 1.12, 0], 0x8a6750);
box(tree, [2.25, 0.9, 1.8], [0, 2.45, 0], 0x6fa16e);
box(tree, [1.65, 0.85, 1.45], [-0.3, 3.05, 0.05], 0x82b67b);
box(tree, [1.35, 0.75, 1.2], [0.5, 3.12, -0.1], 0x75a970);
box(tree, [0.9, 0.65, 0.9], [0.05, 3.72, 0], 0x91bf82);
return tree;
}
createTree(-9.1, -2.15, 1.02);
createTree(-9.1, 1.85, 0.72);
createTree(3.1, -6.0, 0.64);
createTree(11.0, 1.7, 0.85);
// Bench
const bench = new THREE.Group();
bench.position.set(-6.05, 0.06, -0.1);
scene.add(bench);
box(bench, [2.45, 0.16, 0.65], [0, 0.72, 0], 0xb77a51);
box(bench, [2.45, 0.16, 0.65], [0, 1.28, -0.42], 0xc98b5b);
box(bench, [2.45, 0.14, 0.2], [0, 1.03, -0.16], 0xd19a68);
box(bench, [0.18, 0.85, 0.18], [-0.9, 0.36, 0], 0x4e6461);
box(bench, [0.18, 0.85, 0.18], [0.9, 0.36, 0], 0x4e6461);
// Lamp posts, planters, and flowers
function createLamp(x, z) {
const lamp = new THREE.Group();
lamp.position.set(x, 0.06, z);
scene.add(lamp);
box(lamp, [0.14, 3.0, 0.14], [0, 1.5, 0], 0x455e61);
box(lamp, [0.65, 0.12, 0.65], [0, 3.0, 0], 0x455e61);
box(lamp, [0.42, 0.46, 0.42], [0, 3.27, 0], 0xffdf8c, {
material: new THREE.MeshStandardMaterial({
color: 0xffdf8c,
emissive: 0xf2bf5b,
emissiveIntensity: 0.6,
roughness: 0.5
})
});
box(lamp, [0.55, 0.16, 0.55], [0, 3.56, 0], 0x455e61);
}
createLamp(-2.8, 2.45);
createLamp(3.05, 2.25);
function createFlower(x, z, color) {
const flower = new THREE.Group();
flower.position.set(x, 0.08, z);
scene.add(flower);
box(flower, [0.06, 0.34, 0.06], [0, 0.17, 0], 0x477c55);
box(flower, [0.22, 0.12, 0.22], [0, 0.38, 0], color);
box(flower, [0.12, 0.12, 0.12], [0, 0.47, 0], 0xf2bf5b);
}
const flowerColors = [0xed7867, 0xf2bf5b, 0xdbcbe8, 0xffffff];
for (let i = 0; i < 13; i++) {
createFlower(
-10.3 + (i % 5) * 0.48,
-3.2 + Math.floor(i / 5) * 0.65,
flowerColors[i % flowerColors.length]
);
}
// Voxel clouds
function createCloud(x, y, z, scale) {
const cloud = new THREE.Group();
cloud.position.set(x, y, z);
cloud.scale.setScalar(scale);
scene.add(cloud);
box(cloud, [2.6, 0.7, 0.75], [0, 0, 0], 0xf7fbf4, { castShadow: false });
box(cloud, [1.3, 0.8, 0.8], [-0.75, 0.45, 0], 0xf7fbf4, { castShadow: false });
box(cloud, [1.6, 0.9, 0.8], [0.35, 0.5, 0], 0xf7fbf4, { castShadow: false });
}
createCloud(-5.5, 7.2, -7.0, 0.85);
createCloud(4.0, 8.0, -7.5, 0.7);
createCloud(10.5, 6.8, -8.0, 0.65);
// Character creator
function createPerson(options) {
const root = new THREE.Group();
const model = new THREE.Group();
root.add(model);
scene.add(root);
const leftLeg = new THREE.Group();
const rightLeg = new THREE.Group();
leftLeg.position.set(-0.21, 0.72, 0);
rightLeg.position.set(0.21, 0.72, 0);
model.add(leftLeg, rightLeg);
for (const leg of [leftLeg, rightLeg]) {
box(leg, [0.24, 0.56, 0.27], [0, -0.26, 0], options.trousers);
box(leg, [0.3, 0.16, 0.43], [0, -0.58, 0.07], 0x33464b);
}
box(model, [0.78, 0.76, 0.47], [0, 1.14, 0], options.coat);
box(model, [0.56, 0.38, 0.08], [0, 1.05, 0.26], options.shirt || 0xf2ead5);
box(model, [0.67, 0.62, 0.59], [0, 1.9, 0], options.skin);
box(model, [0.72, 0.19, 0.63], [0, 2.2, -0.02], options.hair);
box(model, [0.18, 0.44, 0.58], [-0.27, 2.02, -0.03], options.hair);
box(model, [0.18, 0.44, 0.58], [0.27, 2.02, -0.03], options.hair);
box(model, [0.1, 0.11, 0.07], [-0.16, 1.96, 0.315], 0x27383d);
box(model, [0.1, 0.11, 0.07], [0.16, 1.96, 0.315], 0x27383d);
box(model, [0.13, 0.05, 0.05], [0, 1.78, 0.32], 0x9a554f);
box(model, [0.08, 0.08, 0.04], [-0.25, 1.79, 0.315], 0xe89887);
box(model, [0.08, 0.08, 0.04], [0.25, 1.79, 0.315], 0xe89887);
const leftArm = new THREE.Group();
const rightArm = new THREE.Group();
leftArm.position.set(-0.52, 1.43, 0);
rightArm.position.set(0.52, 1.43, 0);
model.add(leftArm, rightArm);
for (const arm of [leftArm, rightArm]) {
box(arm, [0.25, 0.53, 0.29], [0, -0.23, 0], options.coat);
box(arm, [0.22, 0.2, 0.24], [0, -0.57, 0], options.skin);
}
if (options.hat) {
box(model, [0.76, 0.17, 0.67], [0, 2.28, 0], options.hat);
box(model, [0.69, 0.13, 0.34], [0, 2.18, 0.39], options.hat);
}
const handAnchor = new THREE.Group();
handAnchor.position.set(options.handSide === "left" ? -0.61 : 0.61, 0.88, 0.28);
model.add(handAnchor);
return { root, model, leftLeg, rightLeg, leftArm, rightArm, handAnchor };
}
const milo = createPerson({
coat: 0x4e7f9e,
trousers: 0x3d596a,
shirt: 0xf5d9a5,
skin: 0xc98765,
hair: 0x3a302e,
hat: 0x365c73
});
milo.root.position.set(-3.8, 0.08, 2.15);
const ada = createPerson({
coat: 0xe99b68,
trousers: 0x66536a,
shirt: 0xffefc7,
skin: 0x81553f,
hair: 0x2e292b,
hat: 0
});
ada.root.position.set(5.55, 0.08, -0.72);
ada.root.rotation.y = 0.03;
ada.root.userData.action = "owner";
scene.add(ada.root);
// Pip, the little coral fox
const toy = new THREE.Group();
toy.position.set(-6.05, 0.08, 1.12);
toy.userData.action = "toy";
scene.add(toy);
box(toy, [0.68, 0.62, 0.49], [0, 0.43, 0], 0xed7867);
box(toy, [0.76, 0.6, 0.58], [0, 0.9, 0.03], 0xed7867);
box(toy, [0.19, 0.43, 0.2], [-0.27, 1.32, 0.01], 0xed7867);
box(toy, [0.19, 0.43, 0.2], [0.27, 1.32, 0.01], 0xed7867);
box(toy, [0.12, 0.19, 0.13], [-0.27, 1.35, 0.02], 0xf8d5bd);
box(toy, [0.12, 0.19, 0.13], [0.27, 1.35, 0.02], 0xf8d5bd);
box(toy, [0.45, 0.2, 0.12], [0, 0.79, 0.34], 0xffe3c6);
box(toy, [0.09, 0.1, 0.07], [-0.17, 0.99, 0.335], 0x293b40);
box(toy, [0.09, 0.1, 0.07], [0.17, 0.99, 0.335], 0x293b40);
box(toy, [0.08, 0.06, 0.05], [0, 0.78, 0.42], 0x744742);
box(toy, [0.22, 0.21, 0.32], [-0.2, 0.1, 0.09], 0xed7867);
box(toy, [0.22, 0.21, 0.32], [0.2, 0.1, 0.09], 0xed7867);
box(toy, [0.31, 0.27, 0.3], [0, 0.48, -0.4], 0xed7867);
box(toy, [0.25, 0.25, 0.25], [0.12, 0.72, -0.5], 0xed7867);
// Mint scarf
box(toy, [0.8, 0.13, 0.63], [0, 0.61, 0.02], 0x62c6b2);
box(toy, [0.15, 0.38, 0.08], [0.26, 0.46, 0.35], 0x62c6b2);
// Three-star maker's mark on Pip's back
box(toy, [0.62, 0.27, 0.045], [0, 0.98, -0.29], 0xffedc6);
for (let i = 0; i < 3; i++) {
const x = -0.19 + i * 0.19;
box(toy, [0.06, 0.16, 0.035], [x, 0.98, -0.325], 0xf2bf5b);
box(toy, [0.15, 0.055, 0.035], [x, 0.98, -0.328], 0xf2bf5b);
}
// Highlight mesh
const highlight = new THREE.LineSegments(
new THREE.EdgesGeometry(new THREE.BoxGeometry(1, 1, 1)),
new THREE.LineBasicMaterial({
color: 0xffe28a,
transparent: true,
opacity: 0.9,
depthTest: false
})
);
highlight.renderOrder = 10;
function setHighlight(target, size, offset = [0, 0, 0]) {
target.add(highlight);
highlight.position.set(...offset);
highlight.scale.set(...size);
highlight.visible = true;
}
function clearHighlight() {
highlight.visible = false;
}
setHighlight(toy, [1.2, 1.65, 0.95], [0, 0.72, 0]);
// Story state
let phase = 0;
let busy = false;
let walkState = null;
let pickupState = null;
let handoffState = null;
let inspectState = null;
const confetti = [];
const chapter = document.getElementById("chapter");
const title = document.getElementById("storyTitle");
const text = document.getElementById("storyText");
const actionButton = document.getElementById("actionButton");
const hint = document.getElementById("hint");
const clueCard = document.getElementById("clueCard");
const bubble = document.getElementById("bubble");
const progressSteps = [...document.querySelectorAll(".progress-step")];
const phaseCopy = [
{
chapter: "Chapter 1 of 5",
title: "A little thing, left behind",
text: "Milo is taking the long way home when a coral shape catches the afternoon light near the park bench.",
button: "Pick up Pip",
hint: "You can also tap the little fox in the grass."
},
{
chapter: "Chapter 2 of 5",
title: "Two clues in the fur",
text: "Pip has a mint-green scarf and three gold stars sewn into one ear. Milo checks both before assuming anything.",
button: "Turn Pip over",
hint: "The maker's mark is on Pip's back."
},
{
chapter: "Chapter 3 of 5",
title: "The neighborhood remembers",
text: "Milo reads the lost-and-found notices by the community board. One poster shows the same scarf and the same three stars.",
button: "Read the notice board",
hint: "Milo will walk over to compare the clues."
},
{
chapter: "Chapter 4 of 5",
title: "A name, not a guess",
text: "The poster says: “Lost toy Pip — mint scarf, three gold stars. Owner: Ada, Little Sprout.” Now Milo knows where Pip belongs.",
button: "Bring Pip to Ada",
hint: "Tap the mint-green shop on the right."
},
{
chapter: "Chapter 5 of 5",
title: "The right doorstep",
text: "Ada has been looking for Pip. Milo checks the name once more, then holds the little fox out carefully.",
button: "Return Pip to Ada",
hint: "A small kindness starts with paying attention."
},
{
chapter: "The end",
title: "No small thing gets left behind",
text: "Pip is home, with the scarf, the stars, and one more friend. Milo walks home too—slower this time, just in case.",
button: "Tell the story again",
hint: "Every detail helped Pip find the way home."
}
];
function updateButton() {
if (busy) {
actionButton.disabled = true;
if (phase === 0) actionButton.textContent = "Picking up Pip…";
if (phase === 2) actionButton.textContent = "Comparing the notices…";
if (phase === 3) actionButton.textContent = "Walking to Little Sprout…";
if (phase === 4) actionButton.textContent = "Returning Pip…";
return;
}
actionButton.disabled = false;
actionButton.textContent = phaseCopy[phase].button;
hint.textContent = phaseCopy[phase].hint;
}
function setPhase(nextPhase) {
phase = nextPhase;
const copy = phaseCopy[phase];
chapter.textContent = copy.chapter;
title.textContent = copy.title;
text.textContent = copy.text;
clueCard.classList.toggle("visible", phase >= 2);
const progressIndex = [0, 1, 2, 3, 3, 4][phase];
progressSteps.forEach((step, index) => {
step.classList.toggle("active", index <= progressIndex);
step.classList.toggle("done", index < progressIndex);
});
if (phase === 0 || phase === 1) {
setHighlight(toy, [1.2, 1.65, 0.95], [0, 0.72, 0]);
} else if (phase === 2) {
setHighlight(board, [2.48, 2.7, 0.4], [0, 1.55, 0]);
} else if (phase === 3 || phase === 4) {
setHighlight(ada.root, [1.4, 2.65, 1.0], [0, 1.25, 0]);
} else {
clearHighlight();
}
updateButton();
focusStory(phase);
}
// Camera: custom orbit, pan, and zoom without external controls
let azimuth = 0.61;
let elevation = 0.59;
let cameraTarget = new THREE.Vector3(0, 1.25, -0.35);
let desiredTarget = cameraTarget.clone();
let desiredZoom = 1;
let cameraZoom = 1;
let baseViewHeight = 15;
function focusStory(currentPhase) {
if (window.innerWidth >= 760) {
desiredTarget.set(0, 1.25, -0.35);
desiredZoom = 1;
return;
}
if (currentPhase <= 1) {
desiredTarget.set(-5.55, 0.95, 0.9);
desiredZoom = 1.28;
} else if (currentPhase === 2) {
desiredTarget.set(-0.65, 1.25, -1.25);
desiredZoom = 1.2;
} else if (currentPhase <= 4) {
desiredTarget.set(5.3, 1.05, -0.8);
desiredZoom = 1.22;
} else {
desiredTarget.set(5.2, 1.1, -0.65);
desiredZoom = 1.15;
}
}
function resize() {
const width = window.innerWidth;
const height = window.innerHeight;
const aspect = width / height;
baseViewHeight = aspect < 0.8 ? 16 : 15;
camera.left = -baseViewHeight * aspect / 2;
camera.right = baseViewHeight * aspect / 2;
camera.top = baseViewHeight / 2;
camera.bottom = -baseViewHeight / 2;
camera.updateProjectionMatrix();
renderer.setSize(width, height);
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
focusStory(phase);
}
window.addEventListener("resize", resize);
resize();
function updateCamera() {
cameraTarget.lerp(desiredTarget, reducedMotion ? 1 : 0.075);
cameraZoom += (desiredZoom - cameraZoom) * (reducedMotion ? 1 : 0.09);
camera.zoom = cameraZoom;
camera.updateProjectionMatrix();
const distance = 25;
camera.position.set(
cameraTarget.x + Math.sin(azimuth) * Math.cos(elevation) * distance,
cameraTarget.y + Math.sin(elevation) * distance,
cameraTarget.z + Math.cos(azimuth) * Math.cos(elevation) * distance
);
camera.lookAt(cameraTarget);
}
// Character movement
function walkTo(x, z, onComplete) {
const start = milo.root.position.clone();
const end = new THREE.Vector3(x, 0.08, z);
const distance = start.distanceTo(end);
walkState = {
start,
end,
elapsed: 0,
duration: reducedMotion ? 0.15 : Math.max(0.8, distance * 0.46),
onComplete
};
milo.root.rotation.y = Math.atan2(end.x - start.x, end.z - start.z);
}
function updateCharacter(dt, time) {
if (walkState) {
walkState.elapsed += dt;
const t = Math.min(1, walkState.elapsed / walkState.duration);
const smooth = t * t * (3 - 2 * t);
milo.root.position.lerpVectors(walkState.start, walkState.end, smooth);
const gait = time * 11;
milo.model.position.y = Math.abs(Math.sin(gait)) * 0.055;
milo.leftLeg.rotation.x = Math.sin(gait) * 0.62;
milo.rightLeg.rotation.x = -Math.sin(gait) * 0.62;
milo.leftArm.rotation.x = -Math.sin(gait) * 0.34;
milo.rightArm.rotation.x = Math.sin(gait) * 0.34;
if (t >= 1) {
const callback = walkState.onComplete;
walkState = null;
milo.model.position.y = 0;
milo.leftLeg.rotation.x = 0;
milo.rightLeg.rotation.x = 0;
milo.leftArm.rotation.x = 0;
milo.rightArm.rotation.x = 0;
if (callback) callback();
}
}
}
// Speech bubble
let bubbleTarget = null;
let bubbleOffset = new THREE.Vector3();
let bubbleTimer = null;
function showBubble(target, offset, message, duration = 0) {
bubbleTarget = target;
bubbleOffset.set(...offset);
bubble.innerHTML = message;
bubble.classList.add("visible");
clearTimeout(bubbleTimer);
if (duration > 0) {
bubbleTimer = setTimeout(() => bubble.classList.remove("visible"), duration);
}
}
function updateBubble() {
if (!bubbleTarget || !bubble.classList.contains("visible")) return;
const world = bubbleTarget.localToWorld(bubbleOffset.clone());
const projected = world.project(camera);
const visible = projected.z > -1 && projected.z < 1;
bubble.style.display = visible ? "block" : "none";
if (!visible) return;
bubble.style.left = `${(projected.x * 0.5 + 0.5) * window.innerWidth}px`;
bubble.style.top = `${(-projected.y * 0.5 + 0.5) * window.innerHeight}px`;
}
// Pip pickup, inspection, and handoff
function beginPickup() {
const start = toy.getWorldPosition(new THREE.Vector3());
const end = milo.handAnchor.getWorldPosition(new THREE.Vector3());
pickupState = { start, end, elapsed: 0, duration: reducedMotion ? 0.12 : 0.8 };
}
function updateToyAnimations(dt) {
if (pickupState) {
pickupState.elapsed += dt;
const t = Math.min(1, pickupState.elapsed / pickupState.duration);
const smooth = 1 - Math.pow(1 - t, 3);
toy.position.lerpVectors(pickupState.start, pickupState.end, smooth);
toy.rotation.y = smooth * Math.PI * 0.65;
if (t >= 1) {
pickupState = null;
milo.handAnchor.add(toy);
toy.position.set(0, -0.04, 0);
toy.rotation.set(0, 0, 0);
toy.scale.setScalar(0.82);
busy = false;
setPhase(1);
showBubble(toy, [0, 1.55, 0], "A mint scarf… and three little gold stars.");
}
}
if (inspectState) {
inspectState.elapsed += dt;
const t = Math.min(1, inspectState.elapsed / inspectState.duration);
const smooth = t * t * (3 - 2 * t);
toy.rotation.y = inspectState.start + (inspectState.end - inspectState.start) * smooth;
if (t >= 1) {
inspectState = null;
busy = false;
setPhase(2);
showBubble(toy, [0, 1.55, 0], "Mint-green scarf. <b>Three gold stars.</b>", 2400);
}
}
if (handoffState) {
handoffState.elapsed += dt;
const t = Math.min(1, handoffState.elapsed / handoffState.duration);
const smooth = t * t * (3 - 2 * t);
toy.position.lerpVectors(handoffState.start, handoffState.end, smooth);
toy.rotation.y = Math.PI + smooth * Math.PI * 0.35;
if (t >= 1) {
handoffState = null;
ada.handAnchor.add(toy);
toy.position.set(0, -0.04, 0);
toy.rotation.set(0, 0, 0);
toy.scale.setScalar(0.82);
busy = false;
setPhase(5);
showBubble(ada.root, [0, 2.95, 0], "“Thank you, Milo!”");
createConfetti();
}
}
}
function performAction(action = null) {
if (busy || phase === 5) return;
if (phase === 0 && action !== "board" && action !== "owner") {
busy = true;
updateButton();
showBubble(milo.root, [0, 2.7, 0], "There—something coral!");
walkTo(-5.25, 1.85, beginPickup);
return;
}
if (phase === 1 && (action === "toy" || action === null)) {
busy = true;
updateButton();
inspectState = {
start: toy.rotation.y,
end: Math.PI,
elapsed: 0,
duration: reducedMotion ? 0.12 : 0.9
};
return;
}
if (phase === 2 && (action === "board" || action === null)) {
busy = true;
updateButton();
walkTo(-0.65, 0.25, () => {
showBubble(board, [0, 3.7, 0], "<b>LOST TOY: PIP</b><br>Mint scarf · Three gold stars<br>Owner: Ada");
setTimeout(() => {
busy = false;
setPhase(3);
}, reducedMotion ? 350 : 1550);
});
return;
}
if (phase === 3 && (action === "owner" || action === null)) {
busy = true;
updateButton();
walkTo(4.65, 0.35, () => {
showBubble(ada.root, [0, 2.95, 0], "“You found my Pip!”");
setTimeout(() => {
busy = false;
setPhase(4);
}, reducedMotion ? 350 : 1200);
});
return;
}
if (phase === 4 && (action === "owner" || action === null)) {
busy = true;
updateButton();
scene.attach(toy);
const start = toy.getWorldPosition(new THREE.Vector3());
const end = ada.handAnchor.getWorldPosition(new THREE.Vector3());
handoffState = {
start,
end,
elapsed: 0,
duration: reducedMotion ? 0.12 : 0.9
};
return;
}
}
actionButton.addEventListener("click", () => {
if (phase === 5) {
window.location.reload();
return;
}
performAction();
});
// Confetti
function createConfetti() {
if (reducedMotion) return;
const colors = [0xed7867, 0x62c6b2, 0xf2bf5b, 0xfff4d8, 0x9a8dc5];
for (let i = 0; i < 42; i++) {
const piece = box(
scene,
[0.11, 0.11, 0.11],
[5.45 + (Math.random() - 0.5) * 0.8, 2.7 + Math.random() * 0.5, -0.5],
colors[i % colors.length],
{ castShadow: false }
);
confetti.push({
mesh: piece,
velocity: new THREE.Vector3(
(Math.random() - 0.5) * 3.4,
2.2 + Math.random() * 2.8,
(Math.random() - 0.5) * 3.4
),
spin: new THREE.Vector3(Math.random() * 7, Math.random() * 7, Math.random() * 7),
life: 3.8
});
}
}
function updateConfetti(dt) {
for (let i = confetti.length - 1; i >= 0; i--) {
const piece = confetti[i];
piece.life -= dt;
piece.velocity.y -= 5.2 * dt;
piece.mesh.position.addScaledVector(piece.velocity, dt);
piece.mesh.rotation.x += piece.spin.x * dt;
piece.mesh.rotation.y += piece.spin.y * dt;
piece.mesh.rotation.z += piece.spin.z * dt;
if (piece.life <= 0 || piece.mesh.position.y < 0.05) {
scene.remove(piece.mesh);
confetti.splice(i, 1);
}
}
}
// Pointer interaction and camera drag
const raycaster = new THREE.Raycaster();
const pointer = new THREE.Vector2();
const clickableRoots = [toy, board, ada.root, shop];
let drag = null;
function setPointer(event) {
const rect = renderer.domElement.getBoundingClientRect();
pointer.x = ((event.clientX - rect.left) / rect.width) * 2 - 1;
pointer.y = -((event.clientY - rect.top) / rect.height) * 2 + 1;
}
function findAction(object) {
let current = object;
while (current) {
if (current.userData && current.userData.action) return current.userData.action;
current = current.parent;
}
return null;
}
function actionAllowed(action) {
if (busy) return false;
if (phase <= 1 && action === "toy") return true;
if (phase === 2 && action === "board") return true;
if ((phase === 3 || phase === 4) && action === "owner") return true;
return false;
}
function pick(event) {
setPointer(event);
raycaster.setFromCamera(pointer, camera);
const hits = raycaster.intersectObjects(clickableRoots, true);
if (!hits.length) return null;
const action = findAction(hits[0].object);
return actionAllowed(action) ? action : null;
}
renderer.domElement.addEventListener("pointerdown", event => {
renderer.domElement.setPointerCapture(event.pointerId);
drag = {
x: event.clientX,
y: event.clientY,
lastX: event.clientX,
lastY: event.clientY,
moved: 0,
button: event.button
};
});
renderer.domElement.addEventListener("pointermove", event => {
if (drag) {
const dx = event.clientX - drag.lastX;
const dy = event.clientY - drag.lastY;
drag.moved += Math.abs(dx) + Math.abs(dy);
drag.lastX = event.clientX;
drag.lastY = event.clientY;
if (drag.button === 2) {
const right = new THREE.Vector3().setFromMatrixColumn(camera.matrix, 0);
right.y = 0;
right.normalize();
const forward = new THREE.Vector3().subVectors(cameraTarget, camera.position);
forward.y = 0;
forward.normalize();
desiredTarget.addScaledVector(right, -dx * 0.018);
desiredTarget.addScaledVector(forward, dy * 0.018);
} else {
azimuth -= dx * 0.006;
elevation = THREE.MathUtils.clamp(elevation + dy * 0.004, 0.34, 1.12);
}
} else {
renderer.domElement.style.cursor = pick(event) ? "pointer" : "grab";
}
});
renderer.domElement.addEventListener("pointerup", event => {
if (drag && drag.moved < 7) {
const action = pick(event);
if (action) performAction(action);
}
drag = null;
renderer.domElement.style.cursor = "grab";
});
renderer.domElement.addEventListener("pointercancel", () => { drag = null; });
renderer.domElement.addEventListener("contextmenu", event => event.preventDefault());
renderer.domElement.addEventListener("wheel", event => {
event.preventDefault();
desiredZoom = THREE.MathUtils.clamp(desiredZoom * (event.deltaY > 0 ? 0.9 : 1.1), 0.72, 1.65);
}, { passive: false });
document.getElementById("zoomIn").addEventListener("click", () => {
desiredZoom = THREE.MathUtils.clamp(desiredZoom * 1.12, 0.72, 1.65);
});
document.getElementById("zoomOut").addEventListener("click", () => {
desiredZoom = THREE.MathUtils.clamp(desiredZoom * 0.89, 0.72, 1.65);
});
document.getElementById("resetView").addEventListener("click", () => {
azimuth = 0.61;
elevation = 0.59;
desiredZoom = 1;
focusStory(phase);
});
renderer.domElement.addEventListener("keydown", event => {
if (event.key === "ArrowLeft") azimuth -= 0.1;
if (event.key === "ArrowRight") azimuth += 0.1;
if (event.key === "ArrowUp") elevation = Math.min(1.12, elevation + 0.08);
if (event.key === "ArrowDown") elevation = Math.max(0.34, elevation - 0.08);
if (event.key === "+" || event.key === "=") desiredZoom = Math.min(1.65, desiredZoom * 1.1);
if (event.key === "-") desiredZoom = Math.max(0.72, desiredZoom * 0.9);
});
// Idle character motion
function updateOwner(time) {
ada.model.position.y = Math.sin(time * 2.1) * 0.012;
if (phase >= 3) {
ada.leftArm.rotation.z = -0.25 - Math.sin(time * 5.2) * 0.25;
} else {
ada.leftArm.rotation.z = 0;
}
}
function updateHighlight(time) {
if (!highlight.visible) return;
const pulse = 1 + Math.sin(time * 4.2) * 0.025;
highlight.scale.multiplyScalar(1);
const base = highlight.userData.baseScale || highlight.scale.clone();
highlight.scale.set(base.x * pulse, base.y * pulse, base.z * pulse);
highlight.material.opacity = 0.68 + Math.sin(time * 4.2) * 0.22;
}
const originalSetHighlight = setHighlight;
setHighlight = function(target, size, offset = [0, 0, 0]) {
originalSetHighlight(target, size, offset);
highlight.userData.baseScale = highlight.scale.clone();
};
function animate() {
requestAnimationFrame(animate);
const dt = Math.min(clock.getDelta(), 0.05);
const time = clock.elapsedTime;
updateCharacter(dt, time);
updateToyAnimations(dt);
updateOwner(time);
updateConfetti(dt);
updateCamera();
updateBubble();
updateHighlight(time);
renderer.render(scene, camera);
}
setPhase(0);
animate();
document.getElementById("loading").remove();
</script>
</body>
</html>
```