Prompt specification
Build or customize the Three.js 3D scene "Attachment versus wind travel" 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: Attachment versus wind travel
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/attachment-versus-wind-travel-x0782
Scene Description Prompt:
Create a standalone voxel scene in one HTML file using Three.js from cdnjs. Animate a hooked fruit catching on a passing fabric swatch and a winged seed following a prescribed drifting path; label both motions as illustrative dispersal modes.
Working Three.js Starter (attachment-versus-wind-travel-x0782):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Voxel Dispersal Modes</title>
<style>
:root {
--ink: #18332e;
--muted: #5f756d;
--paper: rgba(250, 249, 238, 0.9);
--line: rgba(24, 51, 46, 0.16);
--teal: #238f83;
--orange: #e88736;
--gold: #d7a13b;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #dce9dc;
color: var(--ink);
font-family:
Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
body {
position: relative;
}
#stage {
position: absolute;
inset: 0;
}
canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
cursor: grab;
}
canvas.dragging {
cursor: grabbing;
}
.intro {
position: absolute;
top: 24px;
left: 24px;
z-index: 5;
width: min(360px, calc(100vw - 48px));
padding: 18px 20px 16px;
border: 1px solid var(--line);
border-radius: 18px;
background: var(--paper);
box-shadow: 0 14px 45px rgba(36, 68, 55, 0.12);
backdrop-filter: blur(12px);
pointer-events: none;
}
.eyebrow {
display: flex;
align-items: center;
gap: 8px;
margin-bottom: 8px;
color: var(--teal);
font-size: 11px;
font-weight: 800;
letter-spacing: 0.16em;
text-transform: uppercase;
}
.eyebrow::before {
width: 8px;
height: 8px;
content: "";
border-radius: 2px;
background: var(--teal);
box-shadow: 11px 0 0 var(--orange);
margin-right: 11px;
}
h1 {
margin: 0;
font-size: clamp(24px, 3vw, 36px);
line-height: 1;
letter-spacing: -0.045em;
}
.intro p {
margin: 10px 0 0;
color: var(--muted);
font-size: 13px;
line-height: 1.5;
}
.legend {
position: absolute;
top: 25px;
right: 25px;
z-index: 5;
display: flex;
flex-direction: column;
gap: 8px;
padding: 12px 14px;
border: 1px solid var(--line);
border-radius: 14px;
background: rgba(250, 249, 238, 0.82);
color: var(--muted);
font-size: 11px;
box-shadow: 0 10px 30px rgba(36, 68, 55, 0.08);
backdrop-filter: blur(10px);
pointer-events: none;
}
.legend-row {
display: flex;
align-items: center;
gap: 9px;
}
.swatch {
width: 18px;
height: 6px;
border-radius: 2px;
}
.swatch.fabric {
background: repeating-linear-gradient(
90deg,
#54b7aa 0 4px,
#8bd0c5 4px 7px
);
}
.swatch.route {
background: repeating-linear-gradient(
90deg,
#d7a13b 0 4px,
transparent 4px 8px
);
}
.mode-label {
position: absolute;
z-index: 4;
width: 224px;
padding: 12px 14px;
border: 1px solid var(--line);
border-radius: 13px;
background: rgba(252, 250, 239, 0.93);
box-shadow: 0 12px 34px rgba(31, 60, 49, 0.14);
backdrop-filter: blur(10px);
pointer-events: none;
transition: opacity 120ms ease;
}
.mode-label::after {
position: absolute;
top: 50%;
width: 10px;
height: 10px;
content: "";
border: 2px solid #faf9ee;
border-radius: 2px;
background: currentColor;
transform: translateY(-50%) rotate(45deg);
}
.fruit-label {
color: var(--orange);
transform: translate(calc(-100% - 16px), -50%);
}
.fruit-label::after {
right: -19px;
}
.seed-label {
color: var(--gold);
transform: translate(16px, -50%);
}
.seed-label::after {
left: -19px;
}
.mode-index {
margin-bottom: 4px;
font-size: 9px;
font-weight: 900;
letter-spacing: 0.15em;
text-transform: uppercase;
}
.mode-title {
color: var(--ink);
font-size: 17px;
font-weight: 800;
letter-spacing: -0.025em;
}
.mode-type {
margin-top: 3px;
color: var(--teal);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.04em;
text-transform: uppercase;
}
.mode-note {
margin-top: 7px;
color: var(--muted);
font-size: 11px;
line-height: 1.35;
}
#fruit-status {
color: var(--orange);
font-weight: 800;
}
.controls {
position: absolute;
bottom: 22px;
left: 50%;
z-index: 6;
display: flex;
align-items: center;
gap: 12px;
padding: 9px 10px 9px 15px;
border: 1px solid var(--line);
border-radius: 999px;
background: rgba(250, 249, 238, 0.9);
box-shadow: 0 12px 35px rgba(36, 68, 55, 0.12);
backdrop-filter: blur(10px);
transform: translateX(-50%);
}
.controls span {
color: var(--muted);
font-size: 10px;
font-weight: 700;
letter-spacing: 0.07em;
white-space: nowrap;
}
button {
min-width: 70px;
padding: 8px 13px;
border: 0;
border-radius: 999px;
background: var(--ink);
color: #fffdf0;
font: inherit;
font-size: 11px;
font-weight: 800;
cursor: pointer;
}
button:hover {
background: var(--teal);
}
button + button {
min-width: 58px;
background: transparent;
color: var(--ink);
}
button + button:hover {
background: rgba(35, 143, 131, 0.1);
}
@media (max-width: 720px) {
.intro {
top: 14px;
left: 14px;
width: calc(100vw - 28px);
padding: 14px 16px;
}
.intro p {
display: none;
}
.legend {
top: 108px;
right: 14px;
}
.mode-label {
width: 178px;
padding: 10px 11px;
}
.mode-title {
font-size: 14px;
}
.mode-note {
display: none;
}
.controls {
bottom: 12px;
}
.controls span {
display: none;
}
}
</style>
</head>
<body>
<main id="stage" aria-label="Animated voxel dispersal scene"></main>
<header class="intro">
<div class="eyebrow">Field Notes / Dispersal</div>
<h1>Two ways to travel.</h1>
<p>
A hooked fruit catches a moving fabric swatch, while a winged seed
follows a fixed airborne route through the voxel landscape.
</p>
</header>
<aside class="legend" aria-label="Scene legend">
<div class="legend-row">
<span class="swatch fabric"></span>
<span>Moving fabric swatch</span>
</div>
<div class="legend-row">
<span class="swatch route"></span>
<span>Prescribed seed route</span>
</div>
</aside>
<section id="fruit-label" class="mode-label fruit-label">
<div class="mode-index">01 / Hook Catch</div>
<div class="mode-title">Hooked fruit</div>
<div class="mode-type">Illustrative dispersal mode</div>
<div class="mode-note">
<span id="fruit-status">Approach</span> ·
catches a passing fabric swatch
</div>
</section>
<section id="seed-label" class="mode-label seed-label">
<div class="mode-index">02 / Winged Drift</div>
<div class="mode-title">Winged seed</div>
<div class="mode-type">Illustrative dispersal mode</div>
<div class="mode-note">
Dotted voxels mark the prescribed drifting path.
</div>
</section>
<nav class="controls" aria-label="Animation controls">
<span>DRAG TO ORBIT · SCROLL TO ZOOM</span>
<button id="pause" type="button" aria-pressed="false">Pause</button>
<button id="restart" type="button">Restart</button>
</nav>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// -----------------------------------------------------------------------
// Renderer, scene, and camera
// -----------------------------------------------------------------------
const stage = document.getElementById("stage");
const renderer = new THREE.WebGLRenderer({
antialias: true,
alpha: false,
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;
stage.appendChild(renderer.domElement);
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xdce9dc);
scene.fog = new THREE.Fog(0xdce9dc, 19, 43);
const camera = new THREE.PerspectiveCamera(
39,
window.innerWidth / window.innerHeight,
0.1,
100
);
const cameraTarget = new THREE.Vector3(0, 2.35, 0);
let cameraYaw = 0.64;
let cameraPitch = 0.48;
let cameraRadius = 20.5;
function updateCamera() {
const horizontal = Math.cos(cameraPitch) * cameraRadius;
camera.position.set(
cameraTarget.x + Math.sin(cameraYaw) * horizontal,
cameraTarget.y + Math.sin(cameraPitch) * cameraRadius,
cameraTarget.z + Math.cos(cameraYaw) * horizontal
);
camera.lookAt(cameraTarget);
}
updateCamera();
const hemisphere = new THREE.HemisphereLight(0xf4fbef, 0x6d8270, 2.3);
scene.add(hemisphere);
const sunLight = new THREE.DirectionalLight(0xfff0c4, 3.2);
sunLight.position.set(-7, 13, 8);
sunLight.castShadow = true;
sunLight.shadow.mapSize.set(2048, 2048);
sunLight.shadow.camera.left = -18;
sunLight.shadow.camera.right = 18;
sunLight.shadow.camera.top = 16;
sunLight.shadow.camera.bottom = -16;
sunLight.shadow.camera.near = 1;
sunLight.shadow.camera.far = 40;
sunLight.shadow.normalBias = 0.025;
scene.add(sunLight);
const fillLight = new THREE.DirectionalLight(0xa8d9d0, 0.8);
fillLight.position.set(8, 7, -8);
scene.add(fillLight);
// -----------------------------------------------------------------------
// Shared voxel geometry and materials
// -----------------------------------------------------------------------
const unitBox = new THREE.BoxGeometry(1, 1, 1);
const material = (color, extra = {}) =>
new THREE.MeshStandardMaterial({
color,
roughness: 0.86,
metalness: 0,
flatShading: true,
...extra
});
const materials = {
trunk: material(0x78583c),
trunkLight: material(0x96714d),
leaf: material(0x4f8f61),
leafLight: material(0x68a873),
leafDark: material(0x3f7754),
mountain: material(0x9eb8a5),
mountainLight: material(0xb3c9b4),
cloud: material(0xf5f4df),
cloudShade: material(0xdce8d5),
sun: material(0xf3c85f, {
emissive: 0x8a5b00,
emissiveIntensity: 0.25
}),
fruit: material(0xe88832),
fruitLight: material(0xf2a342),
fruitDark: material(0xce6e27),
hook: material(0x745039),
hookLight: material(0x946640),
leafSmall: material(0x5a9a58),
seed: material(0x765039),
seedLight: material(0x9a6a43),
wing: material(0xf1dfae),
wingGold: material(0xd9ad54)
};
function addBox(
parent,
size,
position,
meshMaterial,
castShadow = true
) {
const mesh = new THREE.Mesh(unitBox, meshMaterial);
mesh.scale.set(size[0], size[1], size[2]);
mesh.position.set(position[0], position[1], position[2]);
mesh.castShadow = castShadow;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
// Deterministic pseudo-random helper.
let randomState = 728314;
function random() {
randomState = (randomState * 1664525 + 1013904223) >>> 0;
return randomState / 4294967296;
}
// -----------------------------------------------------------------------
// Voxel landscape
// -----------------------------------------------------------------------
const columns = 31;
const rows = 25;
const ground = new THREE.InstancedMesh(
unitBox,
material(0xffffff, { roughness: 1 }),
columns * rows
);
const dummy = new THREE.Object3D();
const groundColor = new THREE.Color();
let groundIndex = 0;
for (let z = 0; z < rows; z++) {
for (let x = 0; x < columns; x++) {
const px = (x - (columns - 1) / 2) * 0.92;
const pz = (z - (rows - 1) / 2) * 0.92;
dummy.position.set(
px,
-0.13 + (random() - 0.5) * 0.035,
pz
);
dummy.rotation.set(0, 0, 0);
dummy.scale.set(0.89, 0.18, 0.89);
dummy.updateMatrix();
ground.setMatrixAt(groundIndex, dummy.matrix);
groundColor.setHSL(
0.255 + random() * 0.025,
0.19 + random() * 0.07,
0.69 + random() * 0.08
);
ground.setColorAt(groundIndex, groundColor);
groundIndex++;
}
}
ground.instanceMatrix.needsUpdate = true;
ground.instanceColor.needsUpdate = true;
ground.receiveShadow = true;
scene.add(ground);
function createTree(x, z, scale = 1) {
const tree = new THREE.Group();
tree.position.set(x, 0, z);
tree.scale.setScalar(scale);
addBox(tree, [0.52, 2.5, 0.52], [0, 1.25, 0], materials.trunk);
addBox(tree, [0.34, 1.1, 0.34], [0.45, 1.65, 0], materials.trunkLight);
addBox(tree, [0.32, 1.0, 0.32], [-0.35, 1.95, 0], materials.trunk);
const leaves = [
[0, 2.85, 0, 1.55, 1.15, 1.35, materials.leaf],
[-0.85, 2.55, 0.08, 1.25, 1.1, 1.15, materials.leafDark],
[0.9, 2.6, 0.05, 1.2, 1.15, 1.15, materials.leafLight],
[0.2, 3.45, 0, 1.3, 0.85, 1.1, materials.leafLight],
[0.3, 2.9, -0.62, 1.2, 0.85, 0.85, materials.leaf],
[0.35, 2.95, 0.62, 1.15, 0.85, 0.8, materials.leafDark]
];
for (const leaf of leaves) {
addBox(
tree,
[leaf[3], leaf[4], leaf[5]],
[leaf[0], leaf[1], leaf[2]],
leaf[6]
);
}
scene.add(tree);
}
createTree(-10.5, -5.8, 1.1);
createTree(10.4, -6.5, 1.25);
createTree(-11.3, 3.8, 0.85);
createTree(11.1, 3.2, 0.9);
// Distant voxel hills.
for (let i = 0; i < 7; i++) {
const hill = new THREE.Group();
hill.position.set(-12 + i * 4, 0, -13 - (i % 2) * 1.4);
addBox(hill, [3.6, 2.2, 2.5], [0, 1.1, 0], materials.mountain);
addBox(hill, [2.5, 2.0, 2.0], [0.7, 2.2, 0], materials.mountainLight);
addBox(hill, [1.8, 1.6, 1.6], [-0.75, 2.0, 0], materials.mountain);
scene.add(hill);
}
// Cubic clouds.
function createCloud(x, y, z, scale) {
const cloud = new THREE.Group();
cloud.position.set(x, y, z);
cloud.scale.setScalar(scale);
addBox(cloud, [2.0, 0.65, 0.75], [0, 0, 0], materials.cloud);
addBox(cloud, [1.1, 0.9, 0.8], [-0.65, 0.28, 0], materials.cloudShade);
addBox(cloud, [1.2, 1.05, 0.8], [0.2, 0.4, 0], materials.cloud);
addBox(cloud, [1.2, 0.65, 0.75], [1.1, 0.05, 0], materials.cloudShade);
scene.add(cloud);
}
createCloud(-7, 9.1, -11, 1.2);
createCloud(4.5, 10.2, -13, 0.9);
createCloud(10, 7.8, -9, 0.7);
// Block sun.
const sun = new THREE.Group();
sun.position.set(-8.2, 9.5, -11.5);
addBox(sun, [1.45, 1.45, 0.45], [0, 0, 0], materials.sun, false);
addBox(sun, [0.48, 0.48, 0.48], [0.95, 0.7, 0], materials.sun, false);
addBox(sun, [0.48, 0.48, 0.48], [-0.8, -0.8, 0], materials.sun, false);
scene.add(sun);
// -----------------------------------------------------------------------
// Passing fabric swatch
// -----------------------------------------------------------------------
const fabric = new THREE.Group();
const fabricPatch = [];
const fabricMaterials = [
material(0x2f9d91),
material(0x49afa2),
material(0x69c0b2),
material(0x86cfc1)
];
for (let row = 0; row < 5; row++) {
for (let column = 0; column < 7; column++) {
const x = (column - 3) * 0.28;
const y = (row - 2) * 0.28;
const baseZ = Math.sin(column * 1.3 + row * 0.7) * 0.025;
const mesh = addBox(
fabric,
[0.27, 0.27, 0.11],
[x, y, baseZ],
fabricMaterials[(row + column) % fabricMaterials.length]
);
fabricPatch.push({
mesh,
x,
y,
baseZ,
phase: column * 0.8 + row * 1.2
});
}
}
// Loose thread cubes along one edge.
for (let i = 0; i < 5; i++) {
addBox(
fabric,
[0.12, 0.25, 0.09],
[-1.03, -0.56 + i * 0.28, 0],
fabricMaterials[2]
);
}
scene.add(fabric);
// -----------------------------------------------------------------------
// Hooked voxel fruit
// -----------------------------------------------------------------------
const fruit = new THREE.Group();
const fruitVoxels = [
[-0.52, -0.57, -0.04, materials.fruitDark],
[-0.25, -0.57, 0.05, materials.fruit],
[0.02, -0.58, 0.00, materials.fruitLight],
[-0.61, -0.84, 0.03, materials.fruit],
[-0.34, -0.84, -0.06, materials.fruitLight],
[-0.07, -0.84, 0.06, materials.fruit],
[-0.53, -1.11, 0.05, materials.fruitDark],
[-0.26, -1.11, -0.04, materials.fruit],
[0.01, -1.10, 0.03, materials.fruitLight],
[-0.34, -1.38, 0.02, materials.fruitDark],
[-0.07, -1.37, 0.05, materials.fruit]
];
for (const voxel of fruitVoxels) {
addBox(
fruit,
[0.29, 0.29, 0.29],
[voxel[0], voxel[1], voxel[2]],
voxel[3]
);
}
// Hooked stem, with the attachment point at local X = 0.
const hookVoxels = [
[-0.30, -0.39, 0.00],
[-0.30, -0.20, 0.00],
[-0.17, -0.05, 0.00],
[0.00, 0.00, 0.00],
[0.13, -0.07, 0.00],
[0.17, -0.21, 0.00],
[0.07, -0.29, 0.00]
];
for (let i = 0; i < hookVoxels.length; i++) {
addBox(
fruit,
[0.13, 0.13, 0.13],
hookVoxels[i],
i % 2 === 0 ? materials.hook : materials.hookLight
);
}
addBox(
fruit,
[0.28, 0.10, 0.16],
[-0.58, -0.28, 0.02],
materials.leafSmall
);
scene.add(fruit);
// -----------------------------------------------------------------------
// Winged seed and its prescribed closed route
// -----------------------------------------------------------------------
const seed = new THREE.Group();
const leftWing = new THREE.Group();
const rightWing = new THREE.Group();
const leftWingVoxels = [
[-0.24, 0.10, 0.00],
[-0.47, 0.20, 0.02],
[-0.72, 0.16, 0.03],
[-0.91, 0.02, 0.02],
[-0.63, -0.02, 0.00],
[-0.38, -0.08, 0.00]
];
const rightWingVoxels = leftWingVoxels.map(([x, y, z]) => [
-x,
y,
z
]);
for (let i = 0; i < leftWingVoxels.length; i++) {
addBox(
leftWing,
[0.17, 0.15, 0.10],
leftWingVoxels[i],
i % 2 === 0 ? materials.wing : materials.wingGold
);
addBox(
rightWing,
[0.17, 0.15, 0.10],
rightWingVoxels[i],
i % 2 === 0 ? materials.wing : materials.wingGold
);
}
seed.add(leftWing, rightWing);
addBox(seed, [0.27, 0.47, 0.27], [0, -0.05, 0], materials.seed);
addBox(seed, [0.18, 0.18, 0.20], [0, 0.20, 0], materials.seedLight);
addBox(seed, [0.08, 0.40, 0.08], [0, -0.06, 0.15], materials.wingGold);
scene.add(seed);
const seedRoute = new THREE.CatmullRomCurve3(
[
new THREE.Vector3(-7.7, 3.2, 2.4),
new THREE.Vector3(-4.8, 5.5, 1.3),
new THREE.Vector3(-1.2, 4.1, -1.7),
new THREE.Vector3(2.5, 6.5, -2.4),
new THREE.Vector3(6.5, 4.6, -0.4),
new THREE.Vector3(7.1, 2.55, 2.5),
new THREE.Vector3(3.1, 3.2, 3.3),
new THREE.Vector3(-1.3, 5.6, 2.8)
],
true,
"catmullrom",
0.5
);
const routeMaterial = new THREE.MeshBasicMaterial({
color: 0xd5a13c,
transparent: true,
opacity: 0.34,
depthWrite: false
});
const routeWaypointMaterial = new THREE.MeshBasicMaterial({
color: 0xe0ae45,
transparent: true,
opacity: 0.68,
depthWrite: false
});
const route = new THREE.Group();
for (let i = 0; i < 92; i++) {
const point = seedRoute.getPointAt(i / 92);
const waypoint = i % 12 === 0;
const marker = new THREE.Mesh(
unitBox,
waypoint ? routeWaypointMaterial : routeMaterial
);
marker.position.copy(point);
marker.scale.setScalar(waypoint ? 0.15 : 0.075);
route.add(marker);
}
scene.add(route);
// -----------------------------------------------------------------------
// Catch burst: small voxel particles
// -----------------------------------------------------------------------
const particleGeometry = new THREE.BoxGeometry(1, 1, 1);
const particleMaterials = [
new THREE.MeshBasicMaterial({ color: 0xf2b44c }),
new THREE.MeshBasicMaterial({ color: 0x55b7a8 }),
new THREE.MeshBasicMaterial({ color: 0xffe29a })
];
const particles = [];
for (let i = 0; i < 14; i++) {
const mesh = new THREE.Mesh(
particleGeometry,
particleMaterials[i % particleMaterials.length]
);
mesh.visible = false;
mesh.scale.setScalar(0.06 + random() * 0.07);
scene.add(mesh);
particles.push({
mesh,
velocity: new THREE.Vector3(),
life: 0
});
}
function emitCatch(position) {
particles.forEach((particle, index) => {
particle.life = 0.72 + random() * 0.22;
particle.mesh.visible = true;
particle.mesh.position.copy(position);
particle.mesh.position.x += (random() - 0.5) * 0.22;
particle.mesh.position.y += (random() - 0.5) * 0.15;
particle.mesh.rotation.set(
random() * Math.PI,
random() * Math.PI,
random() * Math.PI
);
particle.velocity.set(
(random() - 0.5) * 2.8,
0.9 + random() * 2.0,
(random() - 0.5) * 2.2
);
particle.mesh.scale.setScalar(0.07 + index * 0.002);
});
}
// -----------------------------------------------------------------------
// Animation
// -----------------------------------------------------------------------
const CYCLE_DURATION = 12;
const reducedMotion = window.matchMedia(
"(prefers-reduced-motion: reduce)"
).matches;
const timeScale = reducedMotion ? 0.42 : 1;
let elapsed = 0;
let lastTime = performance.now();
let paused = false;
let currentCycle = -1;
let catchFired = false;
const fruitLabel = document.getElementById("fruit-label");
const seedLabel = document.getElementById("seed-label");
const fruitStatus = document.getElementById("fruit-status");
function updateFabric(localTime) {
let x;
if (localTime < 4) {
const approach = localTime / 4;
x = THREE.MathUtils.lerp(8, -0.95, approach);
} else {
const transport = (localTime - 4) / 8;
x = THREE.MathUtils.lerp(-0.95, -10, transport);
}
fabric.position.set(
x,
2.75 + Math.sin(localTime * 1.4) * 0.08,
0.65
);
fabric.rotation.z = Math.sin(localTime * 1.4) * 0.035;
for (const patch of fabricPatch) {
patch.mesh.position.z =
patch.baseZ +
Math.sin(localTime * 3.1 + patch.phase) * 0.045;
}
}
function updateFruit(localTime) {
if (localTime < 4) {
fruit.visible = true;
fruit.position.set(
0,
fabric.position.y + 0.45,
fabric.position.z
);
fruit.rotation.z = Math.sin(localTime * Math.PI * 0.5) * 0.035;
fruit.rotation.x = 0;
} else {
fruit.visible = localTime < 11.85;
fruit.position.set(
fabric.position.x + 0.95,
fabric.position.y + 0.45,
fabric.position.z
);
// The attached fruit gently swings from the caught hook.
fruit.rotation.z = Math.sin((localTime - 4) * 4.2) * 0.055;
fruit.rotation.x = Math.sin((localTime - 4) * 2.7) * 0.025;
}
}
function updateSeed(time) {
const progress = (time / 15) % 1;
const point = seedRoute.getPointAt(progress);
const tangent = seedRoute.getTangentAt(progress).normalize();
seed.position.copy(point);
seed.position.y += Math.sin(time * 2.4) * 0.055;
seed.quaternion.setFromUnitVectors(
new THREE.Vector3(0, 0, 1),
tangent
);
const flutter = Math.sin(time * 12);
leftWing.rotation.z = 0.08 + flutter * 0.22;
rightWing.rotation.z = -0.08 - flutter * 0.22;
}
function updateParticles(dt) {
for (const particle of particles) {
if (particle.life <= 0) continue;
particle.life -= dt;
particle.velocity.y -= 2.4 * dt;
particle.mesh.position.addScaledVector(particle.velocity, dt);
particle.mesh.rotation.x += dt * 4;
particle.mesh.rotation.y += dt * 5;
const scale = Math.max(0, particle.life / 0.9);
particle.mesh.scale.setScalar(0.09 * scale);
if (particle.life <= 0) {
particle.mesh.visible = false;
}
}
}
function placeLabel(element, worldPosition, horizontalOffset = 0) {
const projected = worldPosition.clone().project(camera);
const visible =
projected.z > -1 &&
projected.z < 1 &&
projected.x > -1.35 &&
projected.x < 1.35 &&
projected.y > -1.35 &&
projected.y < 1.35;
element.style.opacity = visible ? "1" : "0";
if (!visible) return;
const labelWidth = element.offsetWidth;
const halfWidth = labelWidth / 2;
let x = (projected.x * 0.5 + 0.5) * window.innerWidth;
let y = (-projected.y * 0.5 + 0.5) * window.innerHeight;
x = THREE.MathUtils.clamp(
x + horizontalOffset,
halfWidth + 12,
window.innerWidth - halfWidth - 12
);
y = THREE.MathUtils.clamp(y, 90, window.innerHeight - 70);
element.style.left = `${x}px`;
element.style.top = `${y}px`;
}
function updateLabels() {
scene.updateMatrixWorld(true);
const fruitAnchor = fruit.localToWorld(
new THREE.Vector3(-0.28, -1.25, 0)
);
const seedAnchor = seed.localToWorld(
new THREE.Vector3(0, 0.22, 0)
);
placeLabel(fruitLabel, fruitAnchor, -8);
placeLabel(seedLabel, seedAnchor, 8);
}
function animate(now) {
requestAnimationFrame(animate);
const rawDt = Math.max(0, Math.min((now - lastTime) / 1000, 0.05));
lastTime = now;
const dt = paused ? 0 : rawDt * timeScale;
elapsed += dt;
const cycle = Math.floor(elapsed / CYCLE_DURATION);
const localTime = elapsed % CYCLE_DURATION;
if (cycle !== currentCycle) {
currentCycle = cycle;
catchFired = false;
for (const particle of particles) {
particle.life = 0;
particle.mesh.visible = false;
}
}
updateFabric(localTime);
updateFruit(localTime);
updateSeed(elapsed);
if (localTime >= 4 && !catchFired) {
catchFired = true;
emitCatch(
new THREE.Vector3(
0,
fabric.position.y + 0.45,
fabric.position.z
)
);
}
if (localTime < 3.55) {
fruitStatus.textContent = "Approach";
} else if (localTime < 4.45) {
fruitStatus.textContent = "Caught";
} else {
fruitStatus.textContent = "Attached";
}
updateParticles(dt);
updateCamera();
updateLabels();
renderer.render(scene, camera);
}
requestAnimationFrame(animate);
// -----------------------------------------------------------------------
// Direct camera controls: drag to orbit, wheel to zoom
// -----------------------------------------------------------------------
let dragging = false;
let pointerX = 0;
let pointerY = 0;
renderer.domElement.addEventListener("pointerdown", (event) => {
dragging = true;
pointerX = event.clientX;
pointerY = event.clientY;
renderer.domElement.setPointerCapture(event.pointerId);
renderer.domElement.classList.add("dragging");
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (!dragging) return;
const dx = event.clientX - pointerX;
const dy = event.clientY - pointerY;
pointerX = event.clientX;
pointerY = event.clientY;
cameraYaw -= dx * 0.005;
cameraPitch = THREE.MathUtils.clamp(
cameraPitch + dy * 0.0045,
0.2,
1.18
);
});
function stopDragging(event) {
dragging = false;
renderer.domElement.classList.remove("dragging");
if (event.pointerId !== undefined) {
if (renderer.domElement.hasPointerCapture(event.pointerId)) {
renderer.domElement.releasePointerCapture(event.pointerId);
}
}
}
renderer.domElement.addEventListener("pointerup", stopDragging);
renderer.domElement.addEventListener("pointercancel", stopDragging);
renderer.domElement.addEventListener(
"wheel",
(event) => {
event.preventDefault();
cameraRadius = THREE.MathUtils.clamp(
cameraRadius + event.deltaY * 0.012,
11,
29
);
},
{ passive: false }
);
// -----------------------------------------------------------------------
// Buttons and resizing
// -----------------------------------------------------------------------
const pauseButton = document.getElementById("pause");
const restartButton = document.getElementById("restart");
pauseButton.addEventListener("click", () => {
paused = !paused;
pauseButton.textContent = paused ? "Resume" : "Pause";
pauseButton.setAttribute("aria-pressed", String(paused));
});
restartButton.addEventListener("click", () => {
elapsed = 0;
currentCycle = -1;
catchFired = false;
for (const particle of particles) {
particle.life = 0;
particle.mesh.visible = false;
}
});
window.addEventListener("resize", () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
});
</script>
</body>
</html>
```