Prompt specification
Build or customize the Three.js 3D scene "Neighbor helps gather groceries" 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: Neighbor helps gather groceries
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/neighbor-helps-gather-groceries-x3910
Scene Description Prompt:
Make a voxel Three.js scene in a self-contained HTML file using cdnjs: neighbor helps gather groceries. Animate one person stabilizing the bag while another collects escaped apples; each apple moves along a visible path into the bag without duplicating or disappearing.
Working Three.js Starter (neighbor-helps-gather-groceries-x3910):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>The Great Apple Rescue</title>
<style>
:root {
--ink: #26332f;
--muted: #68736f;
--paper: rgba(255, 251, 244, 0.93);
--border: rgba(55, 72, 66, 0.14);
--green: #287d69;
--gold: #e4a83b;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #efe6d8;
color: var(--ink);
font-family:
Inter, ui-rounded, "SF Pro Rounded", "Segoe UI", system-ui, sans-serif;
}
#scene {
position: fixed;
inset: 0;
}
canvas {
display: block;
touch-action: none;
outline: none;
}
.panel {
background: var(--paper);
border: 1px solid var(--border);
box-shadow: 0 14px 40px rgba(53, 64, 59, 0.12);
backdrop-filter: blur(12px);
}
#story {
position: fixed;
top: 22px;
left: 22px;
width: min(390px, calc(100vw - 44px));
padding: 20px 22px 18px;
border-radius: 20px;
pointer-events: none;
z-index: 5;
}
.eyebrow {
color: var(--green);
font-size: 11px;
font-weight: 850;
letter-spacing: 0.16em;
text-transform: uppercase;
}
h1 {
margin: 6px 0 8px;
font-size: clamp(25px, 3vw, 36px);
line-height: 1.02;
letter-spacing: -0.045em;
}
#story p {
margin: 0;
color: var(--muted);
font-size: 14px;
line-height: 1.5;
}
.progress-header {
display: flex;
align-items: center;
justify-content: space-between;
gap: 12px;
margin-top: 17px;
padding-top: 14px;
border-top: 1px solid var(--border);
}
#status {
font-size: 13px;
font-weight: 750;
}
#count {
color: var(--green);
font-variant-numeric: tabular-nums;
white-space: nowrap;
}
#dots {
display: flex;
gap: 7px;
margin-top: 10px;
}
#dots span {
width: 22px;
height: 7px;
border-radius: 99px;
background: #d9ddd9;
transition:
background 300ms ease,
transform 300ms ease;
}
#dots span.done {
transform: translateY(-1px);
background: var(--green);
}
.legend {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 10px;
margin-top: 16px;
}
.legend div {
display: flex;
align-items: center;
gap: 8px;
color: var(--muted);
font-size: 11px;
line-height: 1.25;
}
.legend i {
width: 10px;
height: 10px;
flex: 0 0 auto;
border-radius: 3px;
}
.legend strong {
display: block;
color: var(--ink);
font-size: 12px;
}
#toolbar {
position: fixed;
top: 22px;
right: 22px;
z-index: 10;
display: flex;
gap: 8px;
}
button {
appearance: none;
border: 1px solid var(--border);
border-radius: 12px;
padding: 10px 13px;
background: var(--paper);
color: var(--ink);
box-shadow: 0 8px 22px rgba(53, 64, 59, 0.1);
font: inherit;
font-size: 12px;
font-weight: 750;
cursor: pointer;
transition:
transform 150ms ease,
background 150ms ease;
}
button:hover {
transform: translateY(-1px);
background: #fffdf8;
}
button:active {
transform: translateY(1px);
}
#hint {
position: fixed;
left: 50%;
bottom: 20px;
transform: translateX(-50%);
padding: 10px 15px;
border-radius: 999px;
color: #5f6c67;
font-size: 11px;
font-weight: 700;
letter-spacing: 0.02em;
white-space: nowrap;
pointer-events: none;
z-index: 4;
}
#loading {
position: fixed;
inset: 0;
z-index: 30;
display: grid;
place-items: center;
background: #efe6d8;
color: var(--green);
font-size: 13px;
font-weight: 800;
letter-spacing: 0.12em;
text-transform: uppercase;
transition: opacity 300ms ease;
}
#loading.hidden {
opacity: 0;
pointer-events: none;
}
@media (max-width: 680px) {
#story {
top: 12px;
left: 12px;
width: calc(100vw - 24px);
padding: 16px;
}
#story p {
font-size: 12px;
}
.legend {
display: none;
}
#toolbar {
top: auto;
right: 12px;
bottom: 58px;
}
button {
padding: 9px 10px;
font-size: 11px;
}
#hint {
bottom: 12px;
font-size: 10px;
}
}
</style>
</head>
<body>
<div id="scene"></div>
<section id="story" class="panel">
<div class="eyebrow">A small neighborhood favor</div>
<h1>The Great Apple Rescue</h1>
<p>
One neighbor holds the grocery bag steady while the other guides five
escaped apples home. Every colored dash shows an apple's exact route.
</p>
<div class="progress-header">
<div id="status">Following the first apple…</div>
<div id="count">0 / 5 gathered</div>
</div>
<div id="dots" aria-label="Apple progress"></div>
<div class="legend">
<div>
<i style="background:#e4a83b"></i>
<span><strong>Alex</strong>holds the bag</span>
</div>
<div>
<i style="background:#3d9278"></i>
<span><strong>Maya</strong>gathers the apples</span>
</div>
</div>
</section>
<nav id="toolbar" aria-label="Scene controls">
<button id="pathsButton" type="button">Hide paths</button>
<button id="cameraButton" type="button">Reset camera</button>
<button id="replayButton" type="button">Replay</button>
</nav>
<div id="hint" class="panel">
Drag to orbit · Scroll to zoom · Colored dashes are visible apple routes
</div>
<div id="loading">Building the neighborhood…</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// -------------------------------------------------------------------------
// Renderer, camera, lights
// -------------------------------------------------------------------------
const container = document.getElementById("scene");
const statusElement = document.getElementById("status");
const countElement = document.getElementById("count");
const dotsElement = document.getElementById("dots");
const loadingElement = document.getElementById("loading");
const renderer = new THREE.WebGLRenderer({
antialias: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.08;
renderer.domElement.setAttribute(
"aria-label",
"Two voxel neighbors gathering escaped apples into a grocery bag"
);
container.appendChild(renderer.domElement);
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xefe6d8);
scene.fog = new THREE.Fog(0xefe6d8, 13, 25);
const camera = new THREE.PerspectiveCamera(
43,
window.innerWidth / window.innerHeight,
0.1,
100
);
const cameraTarget = new THREE.Vector3(0, 0.85, 0.1);
let cameraYaw = 0.68;
let cameraPitch = 0.52;
let cameraRadius = 11.8;
function updateCamera() {
const horizontalRadius = Math.cos(cameraPitch) * cameraRadius;
camera.position.set(
cameraTarget.x + Math.sin(cameraYaw) * horizontalRadius,
cameraTarget.y + Math.sin(cameraPitch) * cameraRadius,
cameraTarget.z + Math.cos(cameraYaw) * horizontalRadius
);
camera.lookAt(cameraTarget);
}
updateCamera();
const hemisphere = new THREE.HemisphereLight(0xfff5e6, 0x8e806d, 2.25);
scene.add(hemisphere);
const sunlight = new THREE.DirectionalLight(0xfff2d6, 3.25);
sunlight.position.set(-5, 10, 7);
sunlight.castShadow = true;
sunlight.shadow.mapSize.set(2048, 2048);
sunlight.shadow.camera.left = -8;
sunlight.shadow.camera.right = 8;
sunlight.shadow.camera.top = 8;
sunlight.shadow.camera.bottom = -8;
sunlight.shadow.camera.near = 0.5;
sunlight.shadow.camera.far = 30;
sunlight.shadow.bias = -0.0005;
scene.add(sunlight);
const fillLight = new THREE.DirectionalLight(0xb8d9ff, 1.1);
fillLight.position.set(7, 5, -6);
scene.add(fillLight);
// -------------------------------------------------------------------------
// Small helpers
// -------------------------------------------------------------------------
function material(color, options = {}) {
return new THREE.MeshStandardMaterial({
color,
roughness: options.roughness ?? 0.82,
metalness: options.metalness ?? 0,
emissive: options.emissive ?? 0x000000,
emissiveIntensity: options.emissiveIntensity ?? 0,
transparent: options.transparent ?? false,
opacity: options.opacity ?? 1
});
}
function addBox(parent, size, position, mat, castShadow = true) {
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 = castShadow;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
const palette = {
floor: material(0xd9cbb6),
wall: material(0xf3ebdf),
wallTrim: material(0xcdb99d),
counter: material(0x416b63),
counterTop: material(0xf0d7b3),
wood: material(0xa56d43),
darkWood: material(0x70472f),
rug: material(0xe9c99d),
bag: material(0xc98c55),
bagDark: material(0xa66d3d),
cream: material(0xfff4d8),
green: material(0x397f68),
greenDark: material(0x285a4b),
skin: material(0xd99a70),
skinDark: material(0xb97956),
hair: material(0x3d302a),
navy: material(0x344b61),
mustard: material(0xe4a83b),
orange: material(0xdb7544),
black: material(0x242827)
};
// -------------------------------------------------------------------------
// Voxel kitchen environment
// -------------------------------------------------------------------------
const environment = new THREE.Group();
scene.add(environment);
addBox(
environment,
[14, 0.18, 12],
[0, -0.1, 0],
palette.floor,
false
);
const tileGrid = new THREE.GridHelper(14, 14, 0xbba98f, 0xc6b59d);
tileGrid.position.y = 0.002;
tileGrid.material.transparent = true;
tileGrid.material.opacity = 0.28;
environment.add(tileGrid);
addBox(
environment,
[14, 4.2, 0.22],
[0, 2.05, -4.85],
palette.wall,
false
);
addBox(
environment,
[14, 0.14, 0.28],
[0, 0.08, -4.7],
palette.wallTrim,
false
);
// Kitchen cabinets.
addBox(
environment,
[10.6, 0.92, 0.82],
[0, 0.46, -4.22],
palette.counter
);
addBox(
environment,
[10.9, 0.12, 0.94],
[0, 0.98, -4.2],
palette.counterTop
);
for (let i = -4; i <= 4; i++) {
addBox(
environment,
[1.04, 0.68, 0.055],
[i * 1.14, 0.47, -3.785],
material(i % 2 === 0 ? 0x4b756c : 0x3b665e)
);
addBox(
environment,
[0.28, 0.055, 0.07],
[i * 1.14, 0.66, -3.735],
palette.cream
);
}
// Voxel window.
addBox(
environment,
[2.55, 1.55, 0.08],
[-3.45, 2.52, -4.68],
material(0x9bc9d2, { roughness: 0.35 })
);
addBox(
environment,
[2.8, 0.12, 0.12],
[-3.45, 3.32, -4.61],
palette.cream
);
addBox(
environment,
[2.8, 0.12, 0.12],
[-3.45, 1.72, -4.61],
palette.cream
);
addBox(
environment,
[0.12, 1.68, 0.12],
[-3.45, 2.52, -4.6],
palette.cream
);
// Shelves and jars.
addBox(
environment,
[2.4, 0.1, 0.45],
[3.6, 2.1, -4.53],
palette.wood
);
const jarColors = [0xe6b451, 0x71966e, 0xd17b57, 0xb8a36c];
for (let i = 0; i < 4; i++) {
addBox(
environment,
[0.31, 0.48, 0.31],
[2.72 + i * 0.58, 2.39, -4.5],
material(jarColors[i])
);
addBox(
environment,
[0.34, 0.08, 0.34],
[2.72 + i * 0.58, 2.67, -4.5],
palette.cream
);
}
// A grocery crate on the counter-side floor.
const crate = new THREE.Group();
crate.position.set(3.9, 0, -2.65);
environment.add(crate);
addBox(crate, [1.45, 0.12, 1.05], [0, 0.08, 0], palette.darkWood);
addBox(crate, [1.45, 0.58, 0.09], [0, 0.38, -0.48], palette.wood);
addBox(crate, [1.45, 0.42, 0.09], [0, 0.29, 0.48], palette.wood);
addBox(crate, [0.09, 0.5, 1.0], [-0.68, 0.35, 0], palette.wood);
addBox(crate, [0.09, 0.5, 1.0], [0.68, 0.35, 0], palette.wood);
addBox(crate, [0.32, 0.56, 0.34], [-0.35, 0.5, 0.08], palette.green);
addBox(crate, [0.28, 0.45, 0.31], [0.03, 0.43, -0.12], palette.orange);
addBox(
crate,
[0.24, 0.72, 0.25],
[0.39, 0.5, 0.08],
palette.cream
);
// Rug under the gathering area.
addBox(
environment,
[3.7, 0.025, 2.75],
[0.65, 0.012, 0.35],
palette.rug,
false
);
// -------------------------------------------------------------------------
// Grocery bag
// -------------------------------------------------------------------------
const bag = new THREE.Group();
bag.position.set(0.8, 0, 0);
scene.add(bag);
addBox(bag, [1.35, 0.12, 0.95], [0, 0.09, 0], palette.bagDark);
addBox(bag, [0.09, 0.82, 0.95], [-0.63, 0.53, 0], palette.bag);
addBox(bag, [0.09, 0.82, 0.95], [0.63, 0.53, 0], palette.bag);
addBox(bag, [1.25, 0.82, 0.09], [0, 0.53, -0.43], palette.bag);
addBox(bag, [1.25, 0.43, 0.09], [0, 0.31, 0.43], palette.bag);
// Top rim.
addBox(bag, [0.12, 0.08, 1.0], [-0.64, 0.95, 0], palette.bagDark);
addBox(bag, [0.12, 0.08, 1.0], [0.64, 0.95, 0], palette.bagDark);
addBox(bag, [1.28, 0.08, 0.12], [0, 0.95, -0.43], palette.bagDark);
// Rectangular handles.
for (const side of [-1, 1]) {
addBox(
bag,
[0.07, 0.4, 0.07],
[side * 0.65, 0.96, -0.26],
palette.bagDark
);
addBox(
bag,
[0.07, 0.4, 0.07],
[side * 0.65, 0.96, 0.26],
palette.bagDark
);
addBox(
bag,
[0.07, 0.07, 0.58],
[side * 0.65, 1.16, 0],
palette.bagDark
);
}
// Bag label with a tiny voxel apple emblem.
addBox(
bag,
[0.46, 0.27, 0.025],
[0, 0.32, 0.482],
palette.cream
);
addBox(
bag,
[0.13, 0.12, 0.025],
[0, 0.32, 0.503],
material(0xd94c3d)
);
addBox(
bag,
[0.045, 0.065, 0.026],
[0.025, 0.405, 0.506],
palette.darkWood
);
// -------------------------------------------------------------------------
// Voxel people
// -------------------------------------------------------------------------
function createPerson({
shirt,
pants,
skin = palette.skin,
hair = palette.hair,
apron = null
}) {
const root = new THREE.Group();
const torso = new THREE.Group();
root.add(torso);
addBox(torso, [0.64, 0.73, 0.38], [0, 1.18, 0], shirt);
addBox(torso, [0.57, 0.2, 0.37], [0, 0.78, 0], pants);
addBox(torso, [0.49, 0.49, 0.46], [0, 1.78, 0], skin);
addBox(torso, [0.52, 0.13, 0.48], [0, 2.04, -0.015], hair);
addBox(torso, [0.52, 0.12, 0.14], [0, 1.98, 0.235], hair);
addBox(torso, [0.07, 0.08, 0.035], [-0.115, 1.82, 0.241], palette.black);
addBox(torso, [0.07, 0.08, 0.035], [0.115, 1.82, 0.241], palette.black);
addBox(torso, [0.13, 0.045, 0.035], [0, 1.67, 0.242], palette.skinDark);
if (apron) {
addBox(torso, [0.48, 0.57, 0.045], [0, 1.13, 0.214], apron);
addBox(torso, [0.1, 0.34, 0.045], [-0.18, 1.46, 0.214], apron);
addBox(torso, [0.1, 0.34, 0.045], [0.18, 1.46, 0.214], apron);
}
function createLeg(x) {
const pivot = new THREE.Group();
pivot.position.set(x, 0.84, 0);
root.add(pivot);
addBox(pivot, [0.23, 0.65, 0.25], [0, -0.325, 0], pants);
addBox(pivot, [0.27, 0.14, 0.39], [0, -0.69, 0.055], palette.black);
return pivot;
}
function createArm(x) {
const pivot = new THREE.Group();
pivot.position.set(x, 1.52, 0);
root.add(pivot);
addBox(pivot, [0.19, 0.57, 0.22], [0, -0.285, 0], shirt);
addBox(pivot, [0.2, 0.18, 0.21], [0, -0.61, 0], skin);
return pivot;
}
return {
root,
torso,
legL: createLeg(-0.18),
legR: createLeg(0.18),
armL: createArm(-0.43),
armR: createArm(0.43)
};
}
// Alex stands beside the bag and holds its top open.
const holder = createPerson({
shirt: palette.mustard,
pants: palette.navy,
skin: palette.skinDark,
hair: palette.black,
apron: palette.cream
});
holder.root.position.set(1.87, 0, 0.04);
holder.root.rotation.y = -Math.PI / 2;
holder.root.scale.setScalar(1.03);
scene.add(holder.root);
// Maya follows and ushers each apple toward the bag.
const collector = createPerson({
shirt: palette.green,
pants: palette.navy,
skin: palette.skin,
hair: palette.hair,
apron: palette.orange
});
collector.root.scale.setScalar(1.02);
scene.add(collector.root);
// -------------------------------------------------------------------------
// Apples, visible routes, and deterministic timeline
// -------------------------------------------------------------------------
function createApple() {
const group = new THREE.Group();
const red = material(0xd94c3d, { roughness: 0.62 });
const darkRed = material(0xb73531, { roughness: 0.72 });
const highlight = material(0xf47b5c, { roughness: 0.55 });
addBox(group, [0.34, 0.31, 0.34], [0, 0, 0], red);
addBox(group, [0.12, 0.25, 0.34], [-0.19, 0.015, 0], darkRed);
addBox(group, [0.12, 0.25, 0.34], [0.19, 0.015, 0], darkRed);
addBox(group, [0.34, 0.25, 0.12], [0, 0.015, -0.19], darkRed);
addBox(group, [0.34, 0.25, 0.12], [0, 0.015, 0.19], darkRed);
addBox(group, [0.1, 0.09, 0.035], [-0.07, 0.08, 0.182], highlight);
addBox(group, [0.055, 0.13, 0.055], [0.035, 0.225, 0], palette.darkWood);
addBox(group, [0.15, 0.035, 0.09], [0.11, 0.235, 0], palette.green);
return group;
}
const bagCenter = bag.position;
const targetOffsets = [
[-0.3, 0.6, 0.14],
[0.27, 0.61, 0.12],
[0.02, 0.57, -0.17],
[-0.29, 0.46, -0.16],
[0.29, 0.47, -0.16]
];
const routeColors = [
0xe76f51,
0x2a9d8f,
0xe9c46a,
0x7b6d9d,
0x3d7ea6
];
const sources = [
new THREE.Vector3(-3.8, 0.23, 2.5),
new THREE.Vector3(-2.5, 0.23, -1.75),
new THREE.Vector3(-0.2, 0.23, 3.0),
new THREE.Vector3(3.55, 0.23, 2.1),
new THREE.Vector3(-4.35, 0.23, -0.35)
];
const middlePoints = [
[
[-2.7, 0.23, 2.35],
[-0.9, 0.23, 1.55],
[0.15, 0.23, 0.85]
],
[
[-1.5, 0.23, -1.55],
[-0.4, 0.23, -0.85],
[0.2, 0.23, -0.3]
],
[
[-0.3, 0.23, 2.2],
[0.55, 0.23, 1.4],
[0.8, 0.23, 0.55]
],
[
[2.7, 0.23, 1.85],
[1.9, 0.23, 1.15],
[1.3, 0.23, 0.45]
],
[
[-3.4, 0.23, -0.55],
[-1.8, 0.23, -0.55],
[-0.35, 0.23, -0.25]
]
];
const pathRoot = new THREE.Group();
scene.add(pathRoot);
const apples = [];
const events = [];
for (let i = 0; i < sources.length; i++) {
const target = new THREE.Vector3(
bagCenter.x + targetOffsets[i][0],
targetOffsets[i][1],
bagCenter.z + targetOffsets[i][2]
);
const points = [
sources[i].clone(),
...middlePoints[i].map((point) => new THREE.Vector3(...point)),
new THREE.Vector3(target.x, 0.23, target.z),
target.clone()
];
const motionCurve = new THREE.CatmullRomCurve3(
points,
false,
"catmullrom",
0.18
);
// The visible route lies on the floor, then rises into the bag.
const displayPoints = points.map((point, index) => {
if (index === points.length - 1) {
return point.clone();
}
return new THREE.Vector3(point.x, 0.055, point.z);
});
const displayCurve = new THREE.CatmullRomCurve3(
displayPoints,
false,
"catmullrom",
0.18
);
const routeMaterial = new THREE.LineDashedMaterial({
color: routeColors[i],
dashSize: 0.17,
gapSize: 0.11,
transparent: true,
opacity: 0.88,
depthWrite: false
});
const routeLine = new THREE.Line(
new THREE.BufferGeometry().setFromPoints(
displayCurve.getPoints(120)
),
routeMaterial
);
routeLine.computeLineDistances();
pathRoot.add(routeLine);
// Voxel dashes make the route obvious even on lower-resolution displays.
const markerMaterial = material(routeColors[i], {
emissive: routeColors[i],
emissiveIntensity: 0.12
});
for (let markerIndex = 1; markerIndex < 15; markerIndex++) {
const point = displayCurve.getPointAt(markerIndex / 15);
const marker = addBox(
pathRoot,
[0.065, 0.035, 0.065],
[point.x, point.y + 0.01, point.z],
markerMaterial,
false
);
marker.rotation.y = markerIndex * 0.37;
}
const appleMesh = createApple();
appleMesh.position.copy(sources[i]);
scene.add(appleMesh);
apples.push({
mesh: appleMesh,
source: sources[i].clone(),
target,
curve: motionCurve,
settled: false
});
const dot = document.createElement("span");
dot.style.background = `#${routeColors[i].toString(16).padStart(6, "0")}`;
dotsElement.appendChild(dot);
}
// Build a sequential, non-overlapping timeline for approach + guidance.
let timelineTime = 0.55;
let collectorPosition = new THREE.Vector3(-3.25, 0, 3.05);
for (let i = 0; i < apples.length; i++) {
const apple = apples[i];
const tangent = apple.curve.getTangentAt(0).normalize();
// Keep the collector to one side of the route.
const side = new THREE.Vector3(-tangent.z, 0, tangent.x).normalize();
const destination =
apple.source.clone()
.addScaledVector(side, 0.72)
.addScaledVector(tangent, -0.18);
const approachDuration = Math.max(
1.15,
collectorPosition.distanceTo(destination) / 2.45
);
const guidanceDuration = Math.max(
2.65,
apple.curve.getLength() / 2.05
);
const event = {
appleIndex: i,
from: collectorPosition.clone(),
to: destination.clone(),
side,
tangent,
approachStart: timelineTime,
approachEnd: timelineTime + approachDuration,
guidanceEnd: timelineTime + approachDuration + guidanceDuration,
collectorEnd:
apple.target.clone()
.addScaledVector(side, 0.72)
.addScaledVector(tangent, -0.18)
};
events.push(event);
collectorPosition = event.collectorEnd.clone();
timelineTime = event.guidanceEnd;
}
collector.root.position.copy(events[0].from);
// -------------------------------------------------------------------------
// Animation helpers
// -------------------------------------------------------------------------
function clamp01(value) {
return Math.max(0, Math.min(1, value));
}
function smooth01(value) {
const t = clamp01(value);
return t * t * (3 - 2 * t);
}
function dampAngle(current, target, smoothing, delta) {
const difference = Math.atan2(
Math.sin(target - current),
Math.cos(target - current)
);
return current + difference * (1 - Math.exp(-smoothing * delta));
}
let animationEpoch = performance.now();
let lastFrame = animationEpoch;
let gatheredCount = 0;
let previousCount = -1;
let pathsVisible = true;
function updateProgress() {
countElement.textContent = `${gatheredCount} / 5 gathered`;
const dots = [...dotsElement.children];
dots.forEach((dot, index) => {
dot.classList.toggle("done", index < gatheredCount);
});
if (gatheredCount === previousCount) {
return;
}
previousCount = gatheredCount;
if (gatheredCount === 0) {
statusElement.textContent = "Following the first apple…";
} else if (gatheredCount < 5) {
statusElement.textContent = `Apple ${gatheredCount + 1} is on its way…`;
} else {
statusElement.textContent = "All five apples are safely in the bag!";
}
}
function animate(now) {
const delta = Math.min(0.05, (now - lastFrame) / 1000);
lastFrame = now;
const elapsed = (now - animationEpoch) / 1000;
// Alex keeps the bag open with a subtle, steady motion.
const holdBob = Math.sin(elapsed * 2.1);
holder.root.position.y = holdBob * 0.008;
holder.armL.rotation.x = -1.13 + holdBob * 0.025;
holder.armR.rotation.x = -1.13 - holdBob * 0.025;
holder.armL.rotation.z = 0.08;
holder.armR.rotation.z = -0.08;
bag.position.y = holdBob * 0.003;
bag.rotation.z = holdBob * 0.005;
bag.rotation.x = Math.cos(elapsed * 1.7) * 0.003;
// Move every apple only once, along its own curve.
for (let i = 0; i < apples.length; i++) {
const apple = apples[i];
const event = events[i];
if (elapsed >= event.guidanceEnd) {
if (!apple.settled) {
apple.mesh.position.copy(apple.target);
apple.settled = true;
gatheredCount++;
updateProgress();
}
} else if (elapsed >= event.approachEnd) {
const rawProgress =
(elapsed - event.approachEnd) /
(event.guidanceEnd - event.approachEnd);
const progress = smooth01(rawProgress);
const point = apple.curve.getPointAt(progress);
const tangent = apple.curve.getTangentAt(progress);
apple.mesh.position.copy(point);
apple.mesh.rotation.x += delta * 5.2;
apple.mesh.rotation.z += delta * 3.5;
apple.mesh.rotation.y += delta * 1.2;
// A subtle forward lean suggests guiding the apple.
apple.mesh.rotation.z += Math.sin(progress * Math.PI) * 0.12;
} else {
// Before being collected, the apple stays at its unique origin.
apple.mesh.position.copy(apple.source);
apple.mesh.rotation.y += delta * 0.32;
}
}
// Move Maya toward the current apple and then alongside its route.
let activeEvent = null;
for (const event of events) {
if (
elapsed >= event.approachStart &&
elapsed <= event.guidanceEnd
) {
activeEvent = event;
break;
}
}
if (activeEvent) {
const index = activeEvent.appleIndex;
const apple = apples[index];
let walkingAmount = 0;
if (elapsed <= activeEvent.approachEnd) {
const rawProgress =
(elapsed - activeEvent.approachStart) /
(activeEvent.approachEnd - activeEvent.approachStart);
const progress = smooth01(rawProgress);
collector.root.position.lerpVectors(
activeEvent.from,
activeEvent.to,
progress
);
const direction = activeEvent.to.clone().sub(activeEvent.from);
const desiredAngle = Math.atan2(direction.x, direction.z);
collector.root.rotation.y = dampAngle(
collector.root.rotation.y,
desiredAngle,
9,
delta
);
walkingAmount = 1;
} else {
const rawProgress =
(elapsed - activeEvent.approachEnd) /
(activeEvent.guidanceEnd - activeEvent.approachEnd);
const progress = smooth01(rawProgress);
const tangent = apple.curve.getTangentAt(progress);
const collectorDestination =
apple.mesh.position.clone()
.addScaledVector(activeEvent.side, 0.72)
.addScaledVector(activeEvent.tangent, -0.18);
collector.root.position.copy(collectorDestination);
const faceDirection =
apple.mesh.position.clone().sub(collector.root.position);
const desiredAngle = Math.atan2(
faceDirection.x,
faceDirection.z
);
collector.root.rotation.y = dampAngle(
collector.root.rotation.y,
desiredAngle,
11,
delta
);
// The right arm reaches toward the apple while guiding it.
collector.armR.rotation.x =
-1.08 + Math.sin(elapsed * 8) * 0.055;
collector.armR.rotation.z = -0.12;
collector.armL.rotation.x =
Math.sin(elapsed * 8) * 0.18;
walkingAmount = 0.38;
}
const walkPhase = elapsed * 9;
collector.legL.rotation.x =
Math.sin(walkPhase) * 0.58 * walkingAmount;
collector.legR.rotation.x =
-Math.sin(walkPhase) * 0.58 * walkingAmount;
collector.root.position.y =
Math.abs(Math.sin(walkPhase)) * 0.035 * walkingAmount;
} else if (elapsed > events[events.length - 1].guidanceEnd) {
collector.root.position.copy(
events[events.length - 1].collectorEnd
);
const bagDirection =
bag.position.clone().sub(collector.root.position);
const desiredAngle = Math.atan2(
bagDirection.x,
bagDirection.z
);
collector.root.rotation.y = dampAngle(
collector.root.rotation.y,
desiredAngle,
5,
delta
);
collector.armR.rotation.x = -0.35;
collector.armL.rotation.x = 0.05;
collector.legL.rotation.x *= 0.8;
collector.legR.rotation.x *= 0.8;
}
updateCamera();
renderer.render(scene, camera);
requestAnimationFrame(animate);
}
// -------------------------------------------------------------------------
// Custom camera controls — no Three.js addons
// -------------------------------------------------------------------------
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.25,
1.05
);
});
function endDrag(event) {
dragging = false;
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 * Math.exp(event.deltaY * 0.001),
7,
17
);
},
{ passive: false }
);
renderer.domElement.addEventListener("dblclick", () => {
cameraYaw = 0.68;
cameraPitch = 0.52;
cameraRadius = 11.8;
});
// -------------------------------------------------------------------------
// UI controls
// -------------------------------------------------------------------------
document.getElementById("cameraButton").addEventListener("click", () => {
cameraYaw = 0.68;
cameraPitch = 0.52;
cameraRadius = 11.8;
cameraTarget.set(0, 0.85, 0.1);
});
document.getElementById("pathsButton").addEventListener("click", () => {
pathsVisible = !pathsVisible;
pathRoot.visible = pathsVisible;
document.getElementById("pathsButton").textContent =
pathsVisible ? "Hide paths" : "Show paths";
});
document.getElementById("replayButton").addEventListener("click", () => {
animationEpoch = performance.now();
lastFrame = animationEpoch;
gatheredCount = 0;
previousCount = -1;
for (let i = 0; i < apples.length; i++) {
const apple = apples[i];
apple.settled = false;
apple.mesh.position.copy(apple.source);
apple.mesh.rotation.set(0, i * 0.6, 0);
}
collector.root.position.copy(events[0].from);
collector.root.rotation.y = 0;
collector.armR.rotation.x = 0;
collector.armL.rotation.x = 0;
collector.legL.rotation.x = 0;
collector.legR.rotation.x = 0;
updateProgress();
});
// -------------------------------------------------------------------------
// Resize and start
// -------------------------------------------------------------------------
window.addEventListener("resize", () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
});
updateProgress();
requestAnimationFrame(animate);
requestAnimationFrame(() => {
loadingElement.classList.add("hidden");
setTimeout(() => loadingElement.remove(), 350);
});
</script>
</body>
</html>
```