Prompt specification
Build or customize the Three.js 3D scene "Upside-down feeding sweep" 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: Upside-down feeding sweep
Category: Biology & Nature · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/upside-down-feeding-sweep-x0682
Scene Description Prompt:
Upside-down feeding sweep. Animate a flamingo lowering its head into shallow water and sweeping the bill; use sparse symbolic particles to illustrate filtering without claiming exact feeding rates. Please build this with Three.js from cdnjs in one self-contained HTML file, using low-poly forms.
Working Three.js Starter (upside-down-feeding-sweep-x0682):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta
name="description"
content="A stylized low-poly Three.js animation of a flamingo lowering its head and sweeping its bill in shallow water."
/>
<title>Upside-Down Feeding Sweep</title>
<style>
:root {
color-scheme: light;
--ink: #173b3b;
--muted: #55706d;
--panel: rgba(255, 252, 238, 0.82);
--line: rgba(23, 59, 59, 0.14);
--accent: #df6674;
--water: #319ca5;
--gold: #d99d46;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
font-family:
Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
color: var(--ink);
background:
radial-gradient(circle at 76% 18%, #fff4c8 0 4%, transparent 17%),
linear-gradient(180deg, #cce8e4 0%, #e7eee1 54%, #e7d8b8 100%);
}
#scene {
position: fixed;
inset: 0;
}
canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
cursor: grab;
outline: none;
}
canvas:active {
cursor: grabbing;
}
.panel {
position: fixed;
z-index: 5;
border: 1px solid var(--line);
background: var(--panel);
box-shadow: 0 18px 50px rgba(37, 70, 66, 0.12);
backdrop-filter: blur(14px);
-webkit-backdrop-filter: blur(14px);
}
.title-panel {
top: 24px;
left: 24px;
width: min(390px, calc(100vw - 48px));
padding: 20px 22px 18px;
border-radius: 18px;
pointer-events: none;
}
.eyebrow {
margin: 0 0 8px;
color: var(--accent);
font-size: 0.72rem;
font-weight: 800;
letter-spacing: 0.16em;
text-transform: uppercase;
}
h1 {
margin: 0;
max-width: 330px;
font-size: clamp(1.55rem, 3.4vw, 2.45rem);
font-weight: 750;
line-height: 0.98;
letter-spacing: -0.045em;
}
.subtitle {
margin: 12px 0 16px;
color: var(--muted);
font-size: 0.88rem;
line-height: 1.45;
}
.status-row {
display: flex;
align-items: center;
justify-content: space-between;
gap: 12px;
padding-top: 13px;
border-top: 1px solid var(--line);
font-size: 0.76rem;
}
#status {
font-weight: 800;
letter-spacing: 0.04em;
}
.cycle-label {
color: var(--muted);
white-space: nowrap;
}
.progress {
height: 3px;
margin-top: 12px;
overflow: hidden;
border-radius: 999px;
background: rgba(23, 59, 59, 0.1);
}
#progress-fill {
width: 100%;
height: 100%;
transform: scaleX(0);
transform-origin: left center;
border-radius: inherit;
background: linear-gradient(90deg, var(--accent), #eea065);
}
.legend {
left: 24px;
bottom: 24px;
width: min(370px, calc(100vw - 48px));
padding: 15px 17px;
border-radius: 15px;
pointer-events: none;
}
.legend-title {
margin-bottom: 9px;
font-size: 0.74rem;
font-weight: 800;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.legend-items {
display: flex;
flex-wrap: wrap;
gap: 8px 16px;
margin-bottom: 9px;
color: var(--muted);
font-size: 0.78rem;
}
.legend-item {
display: inline-flex;
align-items: center;
gap: 7px;
}
.symbol {
width: 9px;
height: 9px;
display: inline-block;
transform: rotate(45deg);
border-radius: 2px;
}
.symbol.food {
background: var(--gold);
}
.symbol.water {
border: 2px solid var(--water);
background: rgba(49, 156, 165, 0.12);
}
.disclaimer {
margin: 0;
padding-top: 9px;
border-top: 1px solid var(--line);
color: var(--muted);
font-size: 0.72rem;
line-height: 1.4;
}
.controls {
position: fixed;
right: 24px;
bottom: 24px;
z-index: 6;
display: flex;
flex-wrap: wrap;
justify-content: flex-end;
gap: 8px;
max-width: calc(100vw - 48px);
}
button {
appearance: none;
border: 1px solid rgba(23, 59, 59, 0.18);
border-radius: 999px;
padding: 10px 14px;
color: var(--ink);
background: rgba(255, 252, 238, 0.9);
box-shadow: 0 8px 24px rgba(37, 70, 66, 0.12);
font: inherit;
font-size: 0.76rem;
font-weight: 750;
cursor: pointer;
backdrop-filter: blur(10px);
-webkit-backdrop-filter: blur(10px);
transition:
transform 140ms ease,
background 140ms ease,
border-color 140ms ease;
}
button:hover {
transform: translateY(-2px);
border-color: rgba(223, 102, 116, 0.5);
background: #fffaf0;
}
button:focus-visible {
outline: 3px solid rgba(49, 156, 165, 0.32);
outline-offset: 3px;
}
.hint {
position: fixed;
top: 26px;
right: 26px;
z-index: 5;
padding: 8px 11px;
border: 1px solid rgba(23, 59, 59, 0.1);
border-radius: 999px;
color: rgba(23, 59, 59, 0.68);
background: rgba(255, 252, 238, 0.58);
font-size: 0.7rem;
pointer-events: none;
backdrop-filter: blur(8px);
-webkit-backdrop-filter: blur(8px);
}
@media (max-width: 700px) {
.title-panel {
top: 14px;
left: 14px;
width: calc(100vw - 28px);
padding: 16px 17px;
}
.subtitle {
margin-bottom: 12px;
}
.legend {
left: 14px;
bottom: 76px;
width: calc(100vw - 28px);
}
.controls {
right: 14px;
bottom: 14px;
justify-content: flex-start;
}
.hint {
display: none;
}
button {
padding: 9px 12px;
}
}
</style>
</head>
<body>
<div id="scene"></div>
<section class="panel title-panel" aria-label="Animation information">
<p class="eyebrow">Low-poly motion study</p>
<h1>Upside-down<br />feeding sweep</h1>
<p class="subtitle">
A flamingo lowers its head into shallow water, submerges its bill,
and sweeps from side to side.
</p>
<div class="status-row">
<span id="status">Resting</span>
<span class="cycle-label">Continuous pose loop</span>
</div>
<div class="progress" aria-hidden="true">
<div id="progress-fill"></div>
</div>
</section>
<section class="panel legend" aria-label="Particle legend">
<div class="legend-title">Symbolic filtering cues</div>
<div class="legend-items">
<span class="legend-item">
<span class="symbol food" aria-hidden="true"></span>
possible food cue
</span>
<span class="legend-item">
<span class="symbol water" aria-hidden="true"></span>
water-flow cue
</span>
</div>
<p class="disclaimer">
Particles are qualitative visual cues only. They do not represent
intake, throughput, filtration efficiency, or an exact feeding rate.
</p>
</section>
<nav class="controls" aria-label="Animation controls">
<button id="pause" type="button">Pause</button>
<button id="replay" type="button">Replay</button>
<button id="particles" type="button" aria-pressed="true">Hide particles</button>
<button id="reset-camera" type="button">Reset view</button>
</nav>
<div class="hint" aria-hidden="true">
Drag to orbit · Scroll to zoom · Double-click to reset
</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const host = document.querySelector("#scene");
const statusEl = document.querySelector("#status");
const progressEl = document.querySelector("#progress-fill");
const pauseButton = document.querySelector("#pause");
const replayButton = document.querySelector("#replay");
const particlesButton = document.querySelector("#particles");
const resetCameraButton = document.querySelector("#reset-camera");
const prefersReducedMotion =
window.matchMedia("(prefers-reduced-motion: reduce)").matches;
const renderer = new THREE.WebGLRenderer({
antialias: true,
alpha: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setClearColor(0x000000, 0);
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 low-poly flamingo feeding animation"
);
renderer.domElement.setAttribute("role", "img");
host.appendChild(renderer.domElement);
const scene = new THREE.Scene();
scene.fog = new THREE.Fog(0xdcece6, 9, 18);
const camera = new THREE.PerspectiveCamera(
38,
window.innerWidth / window.innerHeight,
0.1,
50
);
const Y_AXIS = new THREE.Vector3(0, 1, 0);
const tempA = new THREE.Vector3();
const tempB = new THREE.Vector3();
const tempC = new THREE.Vector3();
const tempD = new THREE.Vector3();
const tempMid = new THREE.Vector3();
const tempDir = new THREE.Vector3();
function clamp(value, min, max) {
return Math.max(min, Math.min(max, value));
}
function smoothstep(min, max, value) {
const t = clamp((value - min) / (max - min), 0, 1);
return t * t * (3 - 2 * t);
}
function cubicBezier(out, a, b, c, d, t) {
const s = 1 - t;
const s2 = s * s;
const t2 = t * t;
out.set(
s2 * s * a.x + 3 * s2 * t * b.x + 3 * s * t2 * c.x + t2 * t * d.x,
s2 * s * a.y + 3 * s2 * t * b.y + 3 * s * t2 * c.y + t2 * t * d.y,
s2 * s * a.z + 3 * s2 * t * b.z + 3 * s * t2 * c.z + t2 * t * d.z
);
return out;
}
function makeMaterial(color, options = {}) {
return new THREE.MeshStandardMaterial({
color,
roughness: 0.86,
metalness: 0,
flatShading: true,
...options
});
}
function makeLink(
start,
end,
bottomRadius,
topRadius,
material,
radialSegments = 6
) {
const direction = new THREE.Vector3().subVectors(end, start);
const length = direction.length();
const geometry = new THREE.CylinderGeometry(
topRadius,
bottomRadius,
length,
radialSegments,
1,
false
);
const mesh = new THREE.Mesh(geometry, material);
mesh.position.copy(start).add(end).multiplyScalar(0.5);
mesh.quaternion.setFromUnitVectors(Y_AXIS, direction.normalize());
mesh.castShadow = true;
mesh.receiveShadow = true;
return mesh;
}
// Lighting
const hemisphere = new THREE.HemisphereLight(
0xe9fbff,
0x8a735c,
1.85
);
scene.add(hemisphere);
const sun = new THREE.DirectionalLight(0xfff0cf, 3.1);
sun.position.set(-3.5, 8, 5.5);
sun.castShadow = true;
sun.shadow.mapSize.set(1024, 1024);
sun.shadow.camera.left = -6;
sun.shadow.camera.right = 6;
sun.shadow.camera.top = 7;
sun.shadow.camera.bottom = -4;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 20;
sun.shadow.bias = -0.0008;
scene.add(sun);
const fill = new THREE.DirectionalLight(0xb9e5ec, 0.7);
fill.position.set(4, 3, -5);
scene.add(fill);
// Ground and shallow pond
const sandMaterial = makeMaterial(0xd9c69f);
const bedMaterial = makeMaterial(0xa99c76);
const rockMaterial = makeMaterial(0x777e72);
const reedMaterial = makeMaterial(0x557d58);
const reedLightMaterial = makeMaterial(0x789566);
const ground = new THREE.Mesh(
new THREE.PlaneGeometry(30, 30),
makeMaterial(0xdfd1b1)
);
ground.rotation.x = -Math.PI / 2;
ground.position.y = -0.31;
ground.receiveShadow = true;
scene.add(ground);
const island = new THREE.Mesh(
new THREE.CylinderGeometry(3.25, 3.5, 0.25, 14),
sandMaterial
);
island.position.y = -0.16;
island.receiveShadow = true;
island.castShadow = true;
scene.add(island);
const pondBed = new THREE.Mesh(
new THREE.CircleGeometry(2.55, 28),
bedMaterial
);
pondBed.rotation.x = -Math.PI / 2;
pondBed.scale.set(1.15, 0.79, 1);
pondBed.position.set(0.12, -0.015, 0);
pondBed.receiveShadow = true;
scene.add(pondBed);
const waterMaterial = new THREE.MeshStandardMaterial({
color: 0x53b6b6,
roughness: 0.34,
metalness: 0.04,
transparent: true,
opacity: 0.62,
depthWrite: false,
flatShading: true,
side: THREE.DoubleSide
});
const water = new THREE.Mesh(
new THREE.CircleGeometry(2.52, 28),
waterMaterial
);
water.rotation.x = -Math.PI / 2;
water.scale.set(1.15, 0.79, 1);
water.position.set(0.12, 0.105, 0);
water.receiveShadow = true;
water.renderOrder = 1;
scene.add(water);
// Deterministic decorative placement
let randomSeed = 9173;
function random() {
randomSeed = (randomSeed * 1664525 + 1013904223) >>> 0;
return randomSeed / 4294967296;
}
for (let i = 0; i < 18; i++) {
const angle = (i / 18) * Math.PI * 2 + random() * 0.13;
const radius = 2.7 + random() * 0.35;
const rock = new THREE.Mesh(
new THREE.DodecahedronGeometry(1, 0),
rockMaterial
);
const size = 0.12 + random() * 0.18;
rock.scale.set(size * 1.2, size * 0.72, size);
rock.position.set(
0.12 + Math.cos(angle) * radius * 1.05,
0.015,
Math.sin(angle) * radius * 0.76
);
rock.rotation.set(random(), random(), random());
rock.castShadow = true;
rock.receiveShadow = true;
scene.add(rock);
}
// Reeds around the pond edge
const reeds = new THREE.Group();
scene.add(reeds);
const reedPositions = [
[-2.25, -1.25],
[-1.9, -1.55],
[-1.45, -1.72],
[1.55, -1.62],
[1.92, -1.42],
[2.25, -1.12],
[-2.55, 0.15],
[2.55, 0.28]
];
reedPositions.forEach(([x, z], index) => {
const height = 0.65 + random() * 0.6;
const base = new THREE.Vector3(x, 0, z);
const top = new THREE.Vector3(
x + (random() - 0.5) * 0.16,
height,
z + (random() - 0.5) * 0.12
);
const stem = makeLink(
base,
top,
0.026,
0.012,
index % 2 ? reedMaterial : reedLightMaterial,
5
);
reeds.add(stem);
for (let leafIndex = 0; leafIndex < 2; leafIndex++) {
const leaf = new THREE.Mesh(
new THREE.ConeGeometry(0.085, 0.42, 3),
leafIndex ? reedLightMaterial : reedMaterial
);
leaf.position.set(
x + (leafIndex ? 0.1 : -0.08),
height * (0.52 + leafIndex * 0.2),
z
);
leaf.rotation.z = leafIndex ? 0.95 : -1.0;
leaf.castShadow = true;
reeds.add(leaf);
}
});
// Flamingo
const bird = new THREE.Group();
scene.add(bird);
const bodyMaterial = makeMaterial(0xec7e89);
const wingMaterial = makeMaterial(0xf39ca3);
const wingBackMaterial = makeMaterial(0xd96b79);
const neckMaterial = makeMaterial(0xef858d);
const headMaterial = makeMaterial(0xf39aa1);
const billMaterial = makeMaterial(0xe99a91);
const billTipMaterial = makeMaterial(0x29383a);
const legMaterial = makeMaterial(0xa85e67);
const darkMaterial = makeMaterial(0x243638);
const body = new THREE.Mesh(
new THREE.IcosahedronGeometry(1, 1),
bodyMaterial
);
body.position.set(-0.22, 1.58, 0);
body.scale.set(0.83, 0.5, 0.48);
body.rotation.z = -0.035;
body.castShadow = true;
body.receiveShadow = true;
bird.add(body);
const wingFront = new THREE.Mesh(
new THREE.IcosahedronGeometry(1, 1),
wingMaterial
);
wingFront.position.set(-0.27, 1.61, 0.39);
wingFront.scale.set(0.59, 0.32, 0.13);
wingFront.rotation.z = -0.13;
wingFront.castShadow = true;
bird.add(wingFront);
const wingBack = new THREE.Mesh(
new THREE.IcosahedronGeometry(1, 1),
wingBackMaterial
);
wingBack.position.set(-0.27, 1.61, -0.39);
wingBack.scale.set(0.58, 0.31, 0.12);
wingBack.rotation.z = -0.13;
wingBack.castShadow = true;
bird.add(wingBack);
const tail = new THREE.Mesh(
new THREE.ConeGeometry(0.27, 0.72, 5),
wingBackMaterial
);
tail.position.set(-1.0, 1.65, 0);
tail.rotation.z = Math.PI / 2;
tail.castShadow = true;
bird.add(tail);
// Bent legs and toes
const legFront = [
new THREE.Vector3(-0.12, 1.18, 0.13),
new THREE.Vector3(-0.04, 0.58, 0.14),
new THREE.Vector3(-0.08, 0.035, 0.15)
];
const legBack = [
new THREE.Vector3(-0.3, 1.18, -0.13),
new THREE.Vector3(-0.24, 0.59, -0.14),
new THREE.Vector3(-0.29, 0.035, -0.15)
];
[legFront, legBack].forEach((points, legIndex) => {
bird.add(
makeLink(
points[0],
points[1],
0.043,
0.035,
legMaterial,
5
)
);
bird.add(
makeLink(
points[1],
points[2],
0.034,
0.027,
legMaterial,
5
)
);
const knee = new THREE.Mesh(
new THREE.IcosahedronGeometry(0.055, 0),
legMaterial
);
knee.position.copy(points[1]);
knee.castShadow = true;
bird.add(knee);
if (legIndex === 0) {
for (let toe = -1; toe <= 1; toe++) {
bird.add(
makeLink(
points[2],
new THREE.Vector3(
0.24,
0.028,
points[2].z + toe * 0.075
),
0.018,
0.009,
legMaterial,
5
)
);
}
}
});
// Neck made from overlapping low-poly tapered sections
const neckSegments = [];
const neckGeometry = new THREE.CylinderGeometry(1, 1, 1, 7, 1);
for (let i = 0; i < 9; i++) {
const segment = new THREE.Mesh(neckGeometry, neckMaterial);
segment.castShadow = true;
segment.receiveShadow = true;
bird.add(segment);
neckSegments.push(segment);
}
// Head, eyes and bill
const headGroup = new THREE.Group();
bird.add(headGroup);
const head = new THREE.Mesh(
new THREE.IcosahedronGeometry(0.24, 1),
headMaterial
);
head.scale.set(1.08, 0.78, 0.78);
head.castShadow = true;
headGroup.add(head);
const eyeGeometry = new THREE.SphereGeometry(0.034, 6, 4);
[-1, 1].forEach((side) => {
const eye = new THREE.Mesh(eyeGeometry, darkMaterial);
eye.position.set(0.055, 0.035, side * 0.174);
headGroup.add(eye);
});
const billPivot = new THREE.Group();
headGroup.add(billPivot);
const upperBill = makeLink(
new THREE.Vector3(0.13, -0.005, 0),
new THREE.Vector3(0.52, -0.035, 0),
0.105,
0.072,
billMaterial,
6
);
billPivot.add(upperBill);
const billTip = makeLink(
new THREE.Vector3(0.49, -0.032, 0),
new THREE.Vector3(0.79, -0.165, 0),
0.073,
0.018,
billTipMaterial,
6
);
billPivot.add(billTip);
const lowerBill = makeLink(
new THREE.Vector3(0.14, -0.075, 0),
new THREE.Vector3(0.65, -0.125, 0),
0.052,
0.021,
billMaterial,
5
);
billPivot.add(lowerBill);
// Symbolic, sparse filtering particles
const particleRoot = new THREE.Group();
scene.add(particleRoot);
const foodMaterial = new THREE.MeshBasicMaterial({
color: 0xd99d46,
transparent: true,
opacity: 0,
depthWrite: false
});
const waterCueMaterial = new THREE.MeshBasicMaterial({
color: 0x258c9d,
transparent: true,
opacity: 0,
depthWrite: false
});
const foodGeometry = new THREE.TetrahedronGeometry(0.047, 0);
const waterCueGeometry = new THREE.OctahedronGeometry(0.039, 0);
const foodParticles = [];
const waterCueParticles = [];
for (let i = 0; i < 11; i++) {
const particle = new THREE.Mesh(foodGeometry, foodMaterial);
particle.renderOrder = 3;
particle.userData = {
offset: random(),
angle: random() * Math.PI * 2,
radius: 0.24 + random() * 0.46
};
particleRoot.add(particle);
foodParticles.push(particle);
}
for (let i = 0; i < 8; i++) {
const particle = new THREE.Mesh(
waterCueGeometry,
waterCueMaterial
);
particle.renderOrder = 3;
particle.userData = {
offset: random(),
side: i % 2 === 0 ? -1 : 1
};
particleRoot.add(particle);
waterCueParticles.push(particle);
}
// Subtle ripple cues
const rippleGeometry = new THREE.RingGeometry(0.42, 0.46, 28);
const ripples = [];
for (let i = 0; i < 3; i++) {
const material = new THREE.MeshBasicMaterial({
color: 0xe5ffff,
transparent: true,
opacity: 0,
depthWrite: false,
side: THREE.DoubleSide
});
const ripple = new THREE.Mesh(rippleGeometry, material);
ripple.rotation.x = -Math.PI / 2;
ripple.renderOrder = 4;
scene.add(ripple);
ripples.push(ripple);
}
// Camera controls implemented directly, without external controls.
const defaultCamera = {
yaw: 0.43,
pitch: 0.28,
radius: 7.25
};
const desiredCamera = {
yaw: defaultCamera.yaw,
pitch: defaultCamera.pitch,
radius: defaultCamera.radius
};
const currentCamera = {
yaw: defaultCamera.yaw,
pitch: defaultCamera.pitch,
radius: defaultCamera.radius
};
const cameraTarget = new THREE.Vector3(0.05, 1.38, 0);
let dragging = false;
let pointerX = 0;
let pointerY = 0;
function resetCamera() {
desiredCamera.yaw = defaultCamera.yaw;
desiredCamera.pitch = defaultCamera.pitch;
desiredCamera.radius = defaultCamera.radius;
}
renderer.domElement.addEventListener("pointerdown", (event) => {
dragging = true;
pointerX = event.clientX;
pointerY = event.clientY;
renderer.domElement.setPointerCapture(event.pointerId);
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (!dragging) return;
const dx = event.clientX - pointerX;
const dy = event.clientY - pointerY;
pointerX = event.clientX;
pointerY = event.clientY;
desiredCamera.yaw -= dx * 0.005;
desiredCamera.pitch = clamp(
desiredCamera.pitch - dy * 0.004,
0.08,
1.02
);
});
function endDrag(event) {
dragging = false;
if (
event.pointerId !== undefined &&
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();
desiredCamera.radius = clamp(
desiredCamera.radius * Math.exp(event.deltaY * 0.001),
4.4,
10.5
);
},
{ passive: false }
);
renderer.domElement.addEventListener("dblclick", resetCamera);
renderer.domElement.addEventListener(
"contextmenu",
(event) => event.preventDefault()
);
function updateCamera(dt) {
const damping = 1 - Math.exp(-dt * 8);
currentCamera.yaw = THREE.MathUtils.lerp(
currentCamera.yaw,
desiredCamera.yaw,
damping
);
currentCamera.pitch = THREE.MathUtils.lerp(
currentCamera.pitch,
desiredCamera.pitch,
damping
);
currentCamera.radius = THREE.MathUtils.lerp(
currentCamera.radius,
desiredCamera.radius,
damping
);
const horizontalRadius =
currentCamera.radius * Math.cos(currentCamera.pitch);
camera.position.set(
cameraTarget.x +
horizontalRadius * Math.sin(currentCamera.yaw),
cameraTarget.y +
currentCamera.radius * Math.sin(currentCamera.pitch),
cameraTarget.z +
horizontalRadius * Math.cos(currentCamera.yaw)
);
camera.lookAt(cameraTarget);
}
// Animation state
const cycleDuration = 18;
let elapsed = 0;
let paused = prefersReducedMotion;
let particlesEnabled = true;
let lastStatus = "";
function updatePauseButton() {
pauseButton.textContent = paused ? "Resume" : "Pause";
pauseButton.setAttribute("aria-pressed", String(paused));
}
updatePauseButton();
pauseButton.addEventListener("click", () => {
paused = !paused;
updatePauseButton();
});
replayButton.addEventListener("click", () => {
elapsed = 0;
});
particlesButton.addEventListener("click", () => {
particlesEnabled = !particlesEnabled;
particlesButton.textContent = particlesEnabled
? "Hide particles"
: "Show particles";
particlesButton.setAttribute(
"aria-pressed",
String(particlesEnabled)
);
});
resetCameraButton.addEventListener("click", resetCamera);
const neckBase = new THREE.Vector3(0.39, 1.73, 0);
const neckControl1Rest = new THREE.Vector3(0.55, 2.25, 0);
const neckControl1Down = new THREE.Vector3(0.91, 2.2, 0);
const neckControl2Rest = new THREE.Vector3(0.96, 2.86, 0);
const neckControl2Down = new THREE.Vector3(1.71, 1.18, 0);
const headRest = new THREE.Vector3(0.84, 3.01, 0);
const headDown = new THREE.Vector3(1.31, 0.5, 0);
const neckControl1 = new THREE.Vector3();
const neckControl2 = new THREE.Vector3();
const headPosition = new THREE.Vector3();
function updateNeck(feedAmount) {
neckControl1.lerpVectors(
neckControl1Rest,
neckControl1Down,
feedAmount
);
neckControl2.lerpVectors(
neckControl2Rest,
neckControl2Down,
feedAmount
);
headPosition.lerpVectors(headRest, headDown, feedAmount);
headPosition.y += Math.sin(elapsed * 2.1) * 0.012 * feedAmount;
headGroup.position.copy(headPosition);
const segmentCount = neckSegments.length;
for (let i = 0; i < segmentCount; i++) {
const u0 = i / segmentCount;
const u1 = (i + 1) / segmentCount;
cubicBezier(
tempA,
neckBase,
neckControl1,
neckControl2,
headPosition,
u0
);
cubicBezier(
tempB,
neckBase,
neckControl1,
neckControl2,
headPosition,
u1
);
tempDir.subVectors(tempB, tempA);
const length = tempDir.length();
tempMid.addVectors(tempA, tempB).multiplyScalar(0.5);
const segment = neckSegments[i];
segment.position.copy(tempMid);
segment.quaternion.setFromUnitVectors(
Y_AXIS,
tempDir.normalize()
);
const radius = THREE.MathUtils.lerp(0.145, 0.105, u0);
segment.scale.set(radius, length * 1.035, radius);
}
}
function updateParticles(feedAmount, sweepAmount) {
particleRoot.visible = particlesEnabled && feedAmount > 0.025;
if (!particleRoot.visible) {
ripples.forEach((ripple) => {
ripple.material.opacity = 0;
});
return;
}
foodMaterial.opacity = 0.88 * feedAmount;
waterCueMaterial.opacity = 0.58 * feedAmount;
bird.updateMatrixWorld(true);
const billTipWorld = billPivot.localToWorld(
new THREE.Vector3(0.79, -0.165, 0)
);
const mouthWorld = headGroup.localToWorld(
new THREE.Vector3(0.13, -0.02, 0)
);
foodParticles.forEach((particle) => {
const data = particle.userData;
const progress = (elapsed * 0.145 + data.offset) % 1;
tempA.set(
billTipWorld.x + Math.cos(data.angle) * data.radius,
0.13 + (data.offset % 0.05),
billTipWorld.z + Math.sin(data.angle) * data.radius
);
tempB.copy(mouthWorld);
tempB.x += (data.offset - 0.5) * 0.08;
tempB.z += (data.angle % 0.2) - 0.1;
particle.position.lerpVectors(
tempA,
tempB,
smoothstep(0, 1, progress)
);
particle.position.y += Math.sin(progress * Math.PI) * 0.07;
const scale =
0.2 + Math.sin(progress * Math.PI) * 0.9;
particle.scale.setScalar(scale);
particle.rotation.x = elapsed * 1.6 + data.angle;
particle.rotation.y = elapsed * 2.1 + data.offset * 5;
});
waterCueParticles.forEach((particle) => {
const data = particle.userData;
const progress = (elapsed * 0.19 + data.offset) % 1;
tempA.copy(
billPivot.localToWorld(
new THREE.Vector3(0.27, -0.075, data.side * 0.075)
)
);
tempB.copy(tempA);
tempB.x += 0.12;
tempB.y -= 0.22;
tempB.z += data.side * 0.62;
particle.position.lerpVectors(
tempA,
tempB,
smoothstep(0, 1, progress)
);
const scale =
0.2 + Math.sin(progress * Math.PI) * 0.85;
particle.scale.setScalar(scale);
particle.rotation.x = elapsed * 1.3;
particle.rotation.z = elapsed * 1.8 + data.offset;
});
ripples.forEach((ripple, index) => {
const progress = (elapsed * 0.32 + index / 3) % 1;
ripple.position.set(
billTipWorld.x,
0.116,
billTipWorld.z
);
const scale = 0.48 + progress * 1.25;
ripple.scale.setScalar(scale);
ripple.material.opacity =
(1 - progress) *
0.24 *
feedAmount *
(0.35 + sweepAmount * 0.65);
});
}
function statusForPhase(phase) {
if (phase < 0.1) return "Resting";
if (phase < 0.27) return "Lowering head";
if (phase < 0.38) return "Bill submerged";
if (phase < 0.62) return "Sweeping bill";
if (phase < 0.68) return "Finishing sweep";
if (phase < 0.88) return "Raising head";
return "Resting";
}
function resize() {
const width = window.innerWidth;
const height = window.innerHeight;
camera.aspect = width / height;
camera.updateProjectionMatrix();
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(width, height);
}
window.addEventListener("resize", resize);
let previousTime = performance.now();
function animate(now) {
const dt = Math.min((now - previousTime) / 1000, 0.05);
previousTime = now;
if (!paused) {
elapsed += dt;
}
const phase = (elapsed % cycleDuration) / cycleDuration;
const lowering = smoothstep(0.1, 0.3, phase);
const raising = 1 - smoothstep(0.68, 0.88, phase);
const feedAmount = lowering * raising;
const sweepWindow =
smoothstep(0.27, 0.38, phase) *
(1 - smoothstep(0.62, 0.74, phase));
const sweepAngle =
Math.sin((phase - 0.27) * Math.PI * 6) *
0.34 *
sweepWindow;
updateNeck(feedAmount);
headGroup.rotation.set(
0,
sweepAngle,
-0.035 - feedAmount * 0.045
);
updateParticles(feedAmount, sweepWindow);
updateCamera(dt);
const currentStatus = statusForPhase(phase);
if (currentStatus !== lastStatus) {
statusEl.textContent = currentStatus;
lastStatus = currentStatus;
}
progressEl.style.transform = `scaleX(${phase})`;
renderer.render(scene, camera);
requestAnimationFrame(animate);
}
updateNeck(0);
updateCamera(1);
requestAnimationFrame(animate);
</script>
</body>
</html>
```