Prompt specification
Build or customize the Three.js 3D scene "Cavity excavation time sequence" 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: Cavity excavation time sequence
Category: Biology & Nature · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/cavity-excavation-time-sequence-x0718
Scene Description Prompt:
Animate a low-poly woodpecker cavity enlarging inside a tree while chips accumulate below. A condensed schematic sequence, Three.js from cdnjs, one HTML file.
Working Three.js Starter (cavity-excavation-time-sequence-x0718):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Woodpecker Cavity Sequence</title>
<style>
:root {
color-scheme: light;
--ink: #26332d;
--paper: rgba(248, 244, 229, 0.9);
--accent: #d65a43;
}
* { box-sizing: border-box; }
html, body {
margin: 0;
width: 100%;
height: 100%;
overflow: hidden;
background: #dbe7df;
font-family: Inter, ui-sans-serif, system-ui, -apple-system, sans-serif;
}
#app, canvas {
width: 100%;
height: 100%;
display: block;
}
canvas { touch-action: none; }
.panel {
position: fixed;
z-index: 5;
pointer-events: none;
color: var(--ink);
background: var(--paper);
border: 1px solid rgba(38, 51, 45, 0.16);
box-shadow: 0 10px 30px rgba(38, 51, 45, 0.12);
backdrop-filter: blur(10px);
}
#title {
top: 22px;
left: 22px;
width: min(350px, calc(100vw - 44px));
padding: 16px 18px;
border-radius: 4px;
}
.eyebrow {
margin-bottom: 6px;
color: #a84938;
font-size: 10px;
font-weight: 800;
letter-spacing: 0.18em;
text-transform: uppercase;
}
h1 {
margin: 0;
font-size: clamp(18px, 2.5vw, 26px);
line-height: 1.05;
letter-spacing: -0.03em;
}
#stage {
margin-top: 13px;
font-size: 13px;
font-weight: 650;
}
#description {
margin-top: 5px;
color: #66736d;
font-size: 12px;
line-height: 1.4;
}
#timeline {
right: 22px;
bottom: 22px;
left: 22px;
padding: 13px 16px;
border-radius: 4px;
}
.timeline-row {
display: flex;
align-items: center;
gap: 12px;
}
#stepText {
min-width: 66px;
font-size: 11px;
font-weight: 800;
letter-spacing: 0.08em;
}
.track {
position: relative;
flex: 1;
height: 6px;
overflow: hidden;
border-radius: 10px;
background: #d8ddd5;
}
#progress {
position: absolute;
inset: 0 auto 0 0;
width: 0%;
border-radius: inherit;
background: linear-gradient(90deg, #9e563e, #d85a43);
}
.marks {
display: flex;
justify-content: space-between;
margin: 8px 78px 0;
color: #718078;
font-size: 9px;
font-weight: 750;
letter-spacing: 0.1em;
text-transform: uppercase;
}
#hint {
position: fixed;
right: 22px;
top: 22px;
z-index: 5;
padding: 8px 10px;
border-radius: 3px;
color: #65736c;
background: rgba(248, 244, 229, 0.72);
font-size: 10px;
letter-spacing: 0.04em;
pointer-events: none;
}
button {
position: relative;
z-index: 7;
padding: 8px 12px;
border: 1px solid rgba(38, 51, 45, 0.22);
border-radius: 3px;
background: #f8f4e5;
color: #34433c;
font: inherit;
font-size: 10px;
font-weight: 800;
letter-spacing: 0.1em;
text-transform: uppercase;
cursor: pointer;
}
button:hover { background: #fff9e9; }
#loading {
position: fixed;
inset: 0;
z-index: 20;
display: grid;
place-items: center;
background: #dbe7df;
color: #4c5b54;
font-size: 12px;
letter-spacing: 0.12em;
text-transform: uppercase;
transition: opacity 0.45s ease;
}
body.ready #loading {
opacity: 0;
pointer-events: none;
}
@media (max-width: 620px) {
#title { top: 12px; left: 12px; }
#timeline { right: 12px; bottom: 12px; left: 12px; }
#hint { display: none; }
.marks { margin-right: 60px; }
}
</style>
</head>
<body>
<div id="app"></div>
<div id="title" class="panel">
<div class="eyebrow">Low-poly field study</div>
<h1>Woodpecker cavity sequence</h1>
<div id="stage">01 · Pilot peck</div>
<div id="description">A small pilot hole opens into the trunk.</div>
</div>
<div id="hint">Drag to orbit · Scroll to zoom · Double-click to reset</div>
<div id="timeline" class="panel">
<div class="timeline-row">
<div id="stepText">01 / 05</div>
<div class="track"><div id="progress"></div></div>
<button id="replay">Replay</button>
</div>
<div class="marks">
<span>Peck</span>
<span>Widen</span>
<span>Deepen</span>
<span>Grow</span>
<span>Chamber</span>
</div>
</div>
<div id="loading">Preparing the woodpecker sequence…</div>
<script type="module">
import * as THREE from "https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const app = document.getElementById("app");
const stageEl = document.getElementById("stage");
const descriptionEl = document.getElementById("description");
const stepText = document.getElementById("stepText");
const progressEl = document.getElementById("progress");
const replayButton = document.getElementById("replay");
const renderer = new THREE.WebGLRenderer({ antialias: true });
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.style.touchAction = "none";
app.appendChild(renderer.domElement);
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xdbe7df);
scene.fog = new THREE.Fog(0xdbe7df, 13, 27);
const camera = new THREE.PerspectiveCamera(
42,
window.innerWidth / window.innerHeight,
0.1,
50
);
const orbit = {
theta: 0.43,
phi: 1.31,
radius: 10.8,
target: new THREE.Vector3(0, 2.65, 0),
dragging: false,
lastX: 0,
lastY: 0
};
function updateCamera() {
const s = Math.sin(orbit.phi);
camera.position.set(
orbit.target.x + orbit.radius * s * Math.sin(orbit.theta),
orbit.target.y + orbit.radius * Math.cos(orbit.phi),
orbit.target.z + orbit.radius * s * Math.cos(orbit.theta)
);
camera.lookAt(orbit.target);
}
updateCamera();
renderer.domElement.addEventListener("pointerdown", event => {
orbit.dragging = true;
orbit.lastX = event.clientX;
orbit.lastY = event.clientY;
renderer.domElement.setPointerCapture(event.pointerId);
});
renderer.domElement.addEventListener("pointermove", event => {
if (!orbit.dragging) return;
const dx = event.clientX - orbit.lastX;
const dy = event.clientY - orbit.lastY;
orbit.lastX = event.clientX;
orbit.lastY = event.clientY;
orbit.theta -= dx * 0.006;
orbit.phi = THREE.MathUtils.clamp(orbit.phi + dy * 0.004, 0.65, 1.48);
updateCamera();
});
function stopDragging() {
orbit.dragging = false;
}
renderer.domElement.addEventListener("pointerup", stopDragging);
renderer.domElement.addEventListener("pointercancel", stopDragging);
renderer.domElement.addEventListener("wheel", event => {
event.preventDefault();
orbit.radius = THREE.MathUtils.clamp(
orbit.radius * Math.exp(event.deltaY * 0.001),
7,
16
);
updateCamera();
}, { passive: false });
renderer.domElement.addEventListener("dblclick", () => {
orbit.theta = 0.43;
orbit.phi = 1.31;
orbit.radius = 10.8;
updateCamera();
});
window.addEventListener("resize", () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
});
const hemi = new THREE.HemisphereLight(0xeaf4e8, 0x6b5740, 2.25);
scene.add(hemi);
const sun = new THREE.DirectionalLight(0xfff0cf, 3.1);
sun.position.set(-5, 10, 7);
sun.castShadow = true;
sun.shadow.mapSize.set(1536, 1536);
sun.shadow.camera.left = -7;
sun.shadow.camera.right = 7;
sun.shadow.camera.top = 8;
sun.shadow.camera.bottom = -3;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 25;
sun.shadow.normalBias = 0.025;
scene.add(sun);
const fill = new THREE.DirectionalLight(0x9fc8da, 1.0);
fill.position.set(6, 4, -5);
scene.add(fill);
const cavityLight = new THREE.PointLight(0xffbd72, 1.3, 5, 2);
cavityLight.position.set(0.1, 3.8, 2.1);
scene.add(cavityLight);
const groundMaterial = new THREE.MeshStandardMaterial({
color: 0x91aa78,
roughness: 1,
flatShading: true
});
const ground = new THREE.Mesh(
new THREE.CylinderGeometry(6.25, 6.5, 0.22, 28),
groundMaterial
);
ground.position.y = -0.11;
ground.receiveShadow = true;
scene.add(ground);
function material(color, extra = {}) {
return new THREE.MeshStandardMaterial({
color,
roughness: 0.92,
flatShading: true,
...extra
});
}
const barkMaterial = material(0x8a5936, { vertexColors: true });
const innerWoodMaterial = material(0xc68b54, { vertexColors: true });
const cutWoodMaterial = material(0xe0ae70);
const darkWoodMaterial = material(0x4b2d1e);
const cavityWallMaterial = material(0x684027);
const cavityBackMaterial = material(0x3d251a);
const cavityGrainMaterial = material(0x8d5b34);
const TREE_RADIUS = 1.42;
const INNER_RADIUS = 1.30;
const TREE_BOTTOM = 0.05;
const TREE_TOP = 5.35;
const CAVITY_CENTER_Y = 3.77;
const outerShellGeometry = new THREE.BufferGeometry();
const innerShellGeometry = new THREE.BufferGeometry();
const outerShell = new THREE.Mesh(outerShellGeometry, barkMaterial);
outerShell.castShadow = true;
outerShell.receiveShadow = true;
outerShell.frustumCulled = false;
scene.add(outerShell);
const innerShell = new THREE.Mesh(innerShellGeometry, innerWoodMaterial);
innerShell.receiveShadow = true;
innerShell.frustumCulled = false;
scene.add(innerShell);
const topCap = new THREE.Mesh(
new THREE.RingGeometry(INNER_RADIUS, TREE_RADIUS, 28),
cutWoodMaterial
);
topCap.rotation.x = -Math.PI / 2;
topCap.position.y = TREE_TOP;
topCap.receiveShadow = true;
scene.add(topCap);
const bottomCap = new THREE.Mesh(
new THREE.RingGeometry(INNER_RADIUS, TREE_RADIUS, 28),
cutWoodMaterial
);
bottomCap.rotation.x = Math.PI / 2;
bottomCap.position.y = TREE_BOTTOM;
scene.add(bottomCap);
function updateGeometryArrays(geometry, positions, colors, indices) {
let position = geometry.getAttribute("position");
if (!position || position.count !== positions.length / 3) {
position = new THREE.BufferAttribute(new Float32Array(positions), 3);
geometry.setAttribute("position", position);
} else {
position.array.set(positions);
position.needsUpdate = true;
}
if (colors) {
let color = geometry.getAttribute("color");
if (!color || color.count !== colors.length / 3) {
color = new THREE.BufferAttribute(new Float32Array(colors), 3);
geometry.setAttribute("color", color);
} else {
color.array.set(colors);
color.needsUpdate = true;
}
}
let index = geometry.getAttribute("index");
if (!index || index.count !== indices.length) {
geometry.setIndex(new THREE.BufferAttribute(new Uint16Array(indices), 1));
} else {
index.array.set(indices);
index.needsUpdate = true;
}
}
function sortedUnique(values) {
values.sort((a, b) => a - b);
const result = [];
for (const value of values) {
if (!result.length || Math.abs(value - result[result.length - 1]) > 0.00001) {
result.push(value);
}
}
return result;
}
function updateShell(geometry, radius, isOuter) {
const width = currentCavity.w;
const height = currentCavity.h;
const thetaHalf = Math.asin(Math.min(0.94, width / (2 * TREE_RADIUS)));
const cavityBottom = CAVITY_CENTER_Y - height / 2;
const cavityTop = CAVITY_CENTER_Y + height / 2;
const leftSegments = 13;
const frontSegments = 8;
const rightSegments = 13;
const angles = [];
for (let i = 0; i <= leftSegments; i++) {
angles.push(-Math.PI + (Math.PI - thetaHalf) * i / leftSegments);
}
for (let i = 1; i < frontSegments; i++) {
angles.push(-thetaHalf + 2 * thetaHalf * i / frontSegments);
}
for (let i = 0; i <= rightSegments; i++) {
angles.push(thetaHalf + (Math.PI - thetaHalf) * i / rightSegments);
}
const yValues = [];
for (let i = 0; i <= 31; i++) {
yValues.push(TREE_BOTTOM + (TREE_TOP - TREE_BOTTOM) * i / 31);
}
yValues.push(cavityBottom, cavityTop);
const rows = sortedUnique(yValues);
const positions = [];
const colors = [];
const indices = [];
const baseColor = new THREE.Color(isOuter ? 0x8a5936 : 0xc68b54);
for (let r = 0; r < rows.length; r++) {
for (let a = 0; a < angles.length; a++) {
const angle = angles[a];
positions.push(
radius * Math.sin(angle),
rows[r],
radius * Math.cos(angle)
);
const variation = 0.045 * Math.sin(angle * 7.3 + (isOuter ? 0.8 : 2.1))
+ 0.022 * Math.sin(rows[r] * 1.7 + angle * 3.1);
const color = baseColor.clone().offsetHSL(
isOuter ? 0.002 * Math.sin(angle * 3) : 0,
isOuter ? 0.025 * Math.cos(angle * 4) : 0,
variation
);
colors.push(color.r, color.g, color.b);
}
}
for (let r = 0; r < rows.length - 1; r++) {
const midY = (rows[r] + rows[r + 1]) / 2;
for (let a = 0; a < angles.length - 1; a++) {
const midAngle = (angles[a] + angles[a + 1]) / 2;
const insideHole =
Math.abs(midAngle) < thetaHalf &&
midY > cavityBottom &&
midY < cavityTop;
if (insideHole) continue;
const v0 = r * angles.length + a;
const v1 = v0 + 1;
const v2 = v0 + angles.length;
const v3 = v2 + 1;
if (isOuter) {
indices.push(v0, v1, v2, v1, v3, v2);
} else {
indices.push(v0, v2, v1, v1, v2, v3);
}
}
}
updateGeometryArrays(geometry, positions, colors, indices);
}
const rimGeometry = new THREE.BufferGeometry();
const rimMaterial = material(0xe2b477, {
emissive: 0x000000,
emissiveIntensity: 0
});
const rim = new THREE.Mesh(rimGeometry, rimMaterial);
rim.castShadow = true;
rim.frustumCulled = false;
scene.add(rim);
function updateRim() {
const width = currentCavity.w;
const height = currentCavity.h;
const thetaHalf = Math.asin(Math.min(0.94, width / (2 * TREE_RADIUS)));
const bottom = CAVITY_CENTER_Y - height / 2;
const top = CAVITY_CENTER_Y + height / 2;
const radius = TREE_RADIUS + 0.025;
const rimWidth = 0.075;
const sideWidth = 0.028;
const positions = [];
const indices = [];
function point(angle, y, r = radius) {
return [r * Math.sin(angle), y, r * Math.cos(angle)];
}
function quad(a, b, c, d) {
const base = positions.length / 3;
positions.push(...a, ...b, ...c, ...d);
indices.push(base, base + 1, base + 2, base, base + 2, base + 3);
}
const segments = 10;
for (let i = 0; i < segments; i++) {
const a = -thetaHalf + 2 * thetaHalf * i / segments;
const b = -thetaHalf + 2 * thetaHalf * (i + 1) / segments;
quad(
point(a, top),
point(b, top),
point(b, top + rimWidth),
point(a, top + rimWidth)
);
quad(
point(a, bottom - rimWidth),
point(b, bottom - rimWidth),
point(b, bottom),
point(a, bottom)
);
}
for (const side of [-1, 1]) {
const innerAngle = side * thetaHalf;
const outerAngle = side * (thetaHalf + sideWidth);
quad(
point(innerAngle, bottom),
point(outerAngle, bottom),
point(outerAngle, top),
point(innerAngle, top)
);
}
updateGeometryArrays(rimGeometry, positions, null, indices);
rimGeometry.computeVertexNormals();
}
const unitBox = new THREE.BoxGeometry(1, 1, 1);
const cavityBack = new THREE.Mesh(unitBox, cavityBackMaterial);
const cavityFloor = new THREE.Mesh(unitBox, cavityWallMaterial);
const cavityCeiling = new THREE.Mesh(unitBox, cavityWallMaterial);
const cavityLeft = new THREE.Mesh(unitBox, cavityWallMaterial);
const cavityRight = new THREE.Mesh(unitBox, cavityWallMaterial);
for (const mesh of [cavityBack, cavityFloor, cavityCeiling, cavityLeft, cavityRight]) {
mesh.castShadow = true;
mesh.receiveShadow = true;
scene.add(mesh);
}
const cavityGrains = [];
for (let i = 0; i < 3; i++) {
const grain = new THREE.Mesh(unitBox, cavityGrainMaterial);
grain.receiveShadow = true;
scene.add(grain);
cavityGrains.push(grain);
}
function setBox(mesh, x, y, z, sx, sy, sz) {
mesh.position.set(x, y, z);
mesh.scale.set(sx, sy, sz);
}
function updateCavityMeshes() {
const { w, h, d } = currentCavity;
const bottom = CAVITY_CENTER_Y - h / 2;
const top = CAVITY_CENTER_Y + h / 2;
const frontZ = Math.sqrt(TREE_RADIUS ** 2 - (w / 2) ** 2);
const backZ = frontZ - d;
const middleZ = (frontZ + backZ) / 2;
setBox(cavityBack, 0, CAVITY_CENTER_Y, backZ, w, h, 0.08);
setBox(cavityFloor, 0, bottom, middleZ, w, 0.075, d);
setBox(cavityCeiling, 0, top, middleZ, w, 0.075, d);
setBox(cavityLeft, -w / 2, CAVITY_CENTER_Y, middleZ, 0.075, h, d);
setBox(cavityRight, w / 2, CAVITY_CENTER_Y, middleZ, 0.075, h, d);
cavityGrains.forEach((grain, i) => {
setBox(
grain,
(i - 1) * w * 0.22,
CAVITY_CENTER_Y + (i % 2 ? 0.08 : -0.06),
backZ + 0.052,
0.025,
h * 0.55,
0.015
);
});
}
const ridgeMaterial = material(0x68422b);
const ridgeGeometry = new THREE.BoxGeometry(0.065, 4.65, 0.035);
[-2.65, -2.1, -1.55, -1.05, 0.95, 1.45, 1.95, 2.45].forEach(angle => {
const ridge = new THREE.Mesh(ridgeGeometry, ridgeMaterial);
ridge.position.set(
(TREE_RADIUS + 0.012) * Math.sin(angle),
2.62,
(TREE_RADIUS + 0.012) * Math.cos(angle)
);
ridge.rotation.y = angle;
ridge.castShadow = true;
scene.add(ridge);
});
function cylinderBetween(a, b, radiusA, radiusB, mat, parent = scene, segments = 7) {
const direction = b.clone().sub(a);
const length = direction.length();
const mesh = new THREE.Mesh(
new THREE.CylinderGeometry(radiusB, radiusA, length, segments),
mat
);
mesh.position.copy(a).add(b).multiplyScalar(0.5);
mesh.quaternion.setFromUnitVectors(
new THREE.Vector3(0, 1, 0),
direction.normalize()
);
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
const branchMaterial = material(0x765033);
cylinderBetween(
new THREE.Vector3(-1.05, 4.65, 0.15),
new THREE.Vector3(-2.15, 5.08, 0.02),
0.20,
0.12,
branchMaterial
);
const branchCut = new THREE.Mesh(
new THREE.CylinderGeometry(0.125, 0.125, 0.035, 8),
cutWoodMaterial
);
branchCut.position.set(-2.15, 5.08, 0.02);
branchCut.rotation.z = Math.PI / 2 - 0.25;
scene.add(branchCut);
const rootMaterial = material(0x765033);
[
[new THREE.Vector3(-0.85, 0.16, 0.7), new THREE.Vector3(-2.1, 0.08, 1.05)],
[new THREE.Vector3(0.85, 0.16, 0.7), new THREE.Vector3(2.15, 0.08, 0.95)],
[new THREE.Vector3(-0.7, 0.15, -0.8), new THREE.Vector3(-1.95, 0.08, -1.3)],
[new THREE.Vector3(0.7, 0.15, -0.8), new THREE.Vector3(1.9, 0.08, -1.35)]
].forEach(([a, b]) => cylinderBetween(a, b, 0.22, 0.07, rootMaterial));
function addBackgroundTree(x, z, scale) {
const group = new THREE.Group();
group.position.set(x, 0, z);
group.scale.setScalar(scale);
const trunk = new THREE.Mesh(
new THREE.CylinderGeometry(0.16, 0.25, 2.5, 7),
material(0x79624a)
);
trunk.position.y = 1.25;
trunk.castShadow = true;
group.add(trunk);
const foliageMaterial = material(0x6f956d);
const crown = new THREE.Mesh(
new THREE.IcosahedronGeometry(0.95, 0),
foliageMaterial
);
crown.position.y = 2.8;
crown.scale.set(1, 1.15, 0.85);
crown.castShadow = true;
group.add(crown);
scene.add(group);
}
addBackgroundTree(-4.4, -3.3, 1.2);
addBackgroundTree(4.6, -2.7, 0.92);
const grassMaterial = material(0x6f925e);
const grassGeometry = new THREE.ConeGeometry(0.11, 0.38, 3);
for (let i = 0; i < 22; i++) {
const angle = i / 22 * Math.PI * 2 + 0.2;
const radius = 2.3 + (i % 5) * 0.48;
const grass = new THREE.Mesh(grassGeometry, grassMaterial);
grass.position.set(
Math.sin(angle) * radius,
0.17,
Math.cos(angle) * radius
);
grass.rotation.y = angle;
grass.rotation.z = (i % 2 ? 1 : -1) * 0.12;
grass.castShadow = true;
scene.add(grass);
}
const bird = new THREE.Group();
const birdBase = new THREE.Vector3(0.96, 3.35, 1.03);
bird.position.copy(birdBase);
scene.add(bird);
const birdBlack = material(0x20262c);
const birdCream = material(0xf0e8d2);
const birdRed = material(0xd84a3d);
const birdBeak = material(0xd6b35e);
const birdWing = material(0x2c3338);
const body = new THREE.Mesh(new THREE.SphereGeometry(0.5, 8, 6), birdBlack);
body.scale.set(0.62, 1.03, 0.58);
body.castShadow = true;
bird.add(body);
const belly = new THREE.Mesh(new THREE.SphereGeometry(0.5, 8, 6), birdCream);
belly.position.set(-0.10, 0.02, 0.24);
belly.scale.set(0.43, 0.79, 0.19);
belly.castShadow = true;
bird.add(belly);
const head = new THREE.Group();
head.position.set(-0.11, 0.40, 0.04);
bird.add(head);
const headMesh = new THREE.Mesh(new THREE.SphereGeometry(0.31, 8, 6), birdBlack);
headMesh.scale.set(0.95, 0.96, 0.92);
headMesh.castShadow = true;
head.add(headMesh);
const cheek = new THREE.Mesh(new THREE.SphereGeometry(0.25, 8, 6), birdCream);
cheek.position.set(-0.14, -0.01, 0.10);
cheek.scale.set(0.84, 0.69, 0.48);
head.add(cheek);
const beak = new THREE.Mesh(new THREE.ConeGeometry(0.10, 0.50, 6), birdBeak);
beak.position.set(-0.53, 0.42, 0.07);
beak.rotation.z = Math.PI / 2;
beak.castShadow = true;
bird.add(beak);
const eye = new THREE.Mesh(new THREE.SphereGeometry(0.047, 7, 5), material(0x080a0b));
eye.position.set(-0.22, 0.52, 0.285);
head.add(eye);
const eyeGlint = new THREE.Mesh(
new THREE.SphereGeometry(0.014, 6, 4),
material(0xffffff, { emissive: 0xffffff, emissiveIntensity: 0.15 })
);
eyeGlint.position.set(-0.232, 0.537, 0.325);
head.add(eyeGlint);
[-0.17, -0.06, 0.05].forEach((x, i) => {
const crest = new THREE.Mesh(new THREE.ConeGeometry(0.105, 0.28, 4), birdRed);
crest.position.set(x, 0.67 + (i === 1 ? 0.035 : 0), 0.0);
crest.rotation.z = -0.12 + i * 0.12;
crest.castShadow = true;
bird.add(crest);
});
const wing = new THREE.Mesh(new THREE.SphereGeometry(0.5, 8, 6), birdWing);
wing.position.set(0.10, -0.03, 0.285);
wing.scale.set(0.25, 0.73, 0.105);
wing.rotation.z = -0.11;
wing.castShadow = true;
bird.add(wing);
[-0.25, -0.02, 0.21].forEach((y, i) => {
const stripe = new THREE.Mesh(unitBox, birdCream);
stripe.position.set(0.075 - i * 0.012, y, 0.392);
stripe.rotation.z = -0.12;
stripe.scale.set(0.31 - i * 0.025, 0.075, 0.018);
bird.add(stripe);
});
const tail = new THREE.Mesh(new THREE.ConeGeometry(0.16, 0.72, 4), birdBlack);
tail.position.set(0.23, -0.82, -0.04);
tail.rotation.z = Math.PI - 0.16;
tail.castShadow = true;
bird.add(tail);
const underTail = new THREE.Mesh(new THREE.ConeGeometry(0.085, 0.28, 4), birdRed);
underTail.position.set(0.17, -0.57, -0.06);
underTail.rotation.z = Math.PI - 0.12;
bird.add(underTail);
const feetMaterial = material(0x9a7748);
cylinderBetween(
new THREE.Vector3(-0.12, -0.44, -0.02),
new THREE.Vector3(-0.20, -0.67, -0.16),
0.055, 0.045, feetMaterial, bird
);
cylinderBetween(
new THREE.Vector3(0.17, -0.42, -0.01),
new THREE.Vector3(0.18, -0.65, -0.15),
0.055, 0.045, feetMaterial, bird
);
for (const foot of [
new THREE.Vector3(-0.20, -0.67, -0.16),
new THREE.Vector3(0.18, -0.65, -0.15)
]) {
cylinderBetween(
foot,
foot.clone().add(new THREE.Vector3(-0.16, -0.03, -0.08)),
0.035, 0.015, feetMaterial, bird, 5
);
}
const chipGeometry = new THREE.BoxGeometry(1, 1, 1);
const chipMaterials = [
material(0xd8a15f),
material(0xe3ba78),
material(0xb77a43),
material(0xc99151)
];
const chips = [];
const chipCenter = new THREE.Vector3(0.02, 0, 1.55);
let settledChips = 0;
let randomSeed = 81723;
function random() {
randomSeed = (randomSeed * 16807) % 2147483647;
return (randomSeed - 1) / 2147483646;
}
function spawnChip(burst = false) {
if (chips.length >= 190) return;
const mesh = new THREE.Mesh(
chipGeometry,
chipMaterials[Math.floor(random() * chipMaterials.length)]
);
const sx = 0.07 + random() * 0.13;
const sy = 0.025 + random() * 0.045;
const sz = 0.05 + random() * 0.12;
mesh.scale.set(sx, sy, sz);
mesh.position.set(
-0.05 + random() * 0.22,
CAVITY_CENTER_Y + random() * 0.10,
1.37 + random() * 0.12
);
mesh.rotation.set(random() * Math.PI, random() * Math.PI, random() * Math.PI);
mesh.castShadow = true;
mesh.receiveShadow = true;
scene.add(mesh);
chips.push({
mesh,
velocity: new THREE.Vector3(
(random() - 0.5) * (burst ? 1.4 : 0.65),
0.12 + random() * 0.45,
0.08 + random() * (burst ? 0.65 : 0.30)
),
spin: new THREE.Vector3(
(random() - 0.5) * 7,
(random() - 0.5) * 7,
(random() - 0.5) * 7
),
settled: false
});
}
function settleChip(chip) {
chip.settled = true;
settledChips++;
const angle = random() * Math.PI * 2;
const radius = Math.sqrt(random()) * 0.68;
const x = chipCenter.x + Math.cos(angle) * radius;
const z = chipCenter.z + Math.sin(angle) * radius * 0.65;
const pileHeight =
0.11 +
0.52 * (1 - Math.exp(-settledChips / 43)) *
(1 - Math.min(1, radius / 0.68));
chip.mesh.position.set(x, 0.10 + pileHeight + random() * 0.035, z);
chip.mesh.rotation.set(random() * Math.PI, random() * Math.PI, random() * Math.PI);
chip.velocity.set(0, 0, 0);
}
function updateChips(dt) {
for (const chip of chips) {
if (chip.settled) continue;
chip.velocity.y -= 5.8 * dt;
chip.mesh.position.addScaledVector(chip.velocity, dt);
chip.mesh.rotation.x += chip.spin.x * dt;
chip.mesh.rotation.y += chip.spin.y * dt;
chip.mesh.rotation.z += chip.spin.z * dt;
if (chip.mesh.position.y <= 0.13) {
settleChip(chip);
}
}
}
const stages = [
{
w: 0.42, h: 0.32, d: 0.24,
label: "Pilot peck",
description: "A small pilot hole opens into the trunk."
},
{
w: 0.62, h: 0.48, d: 0.40,
label: "Wider gouge",
description: "Fresh wood widens around the pecking point."
},
{
w: 0.90, h: 0.68, d: 0.62,
label: "Deepening",
description: "The cavity extends inward as chips fall away."
},
{
w: 1.20, h: 0.88, d: 0.82,
label: "Chamber growth",
description: "The chamber expands both vertically and outward."
},
{
w: 1.50, h: 1.08, d: 0.96,
label: "Nest cavity",
description: "A broad, deep cavity remains in the tree."
}
];
const STEP_DURATION = 3.1;
const TOTAL_DURATION = stages.length * STEP_DURATION;
let currentCavity = { ...stages[0] };
let simulationTime = 0;
let previousBirdPhase = 1;
let lastStage = -1;
let stagePulse = 0;
function smootherStep(value) {
return value * value * value * (value * (value * 6 - 15) + 10);
}
function updateTimeline() {
let index = Math.floor(simulationTime / STEP_DURATION);
let local = (simulationTime % STEP_DURATION) / STEP_DURATION;
if (simulationTime >= TOTAL_DURATION) {
index = stages.length - 1;
local = 1;
}
const from = index === 0 ? stages[0] : stages[index - 1];
const to = stages[index];
const eased = smootherStep(local);
currentCavity = {
w: THREE.MathUtils.lerp(from.w, to.w, eased),
h: THREE.MathUtils.lerp(from.h, to.h, eased),
d: THREE.MathUtils.lerp(from.d, to.d, eased)
};
if (index !== lastStage) {
if (lastStage !== -1) {
stagePulse = 1;
for (let i = 0; i < 9; i++) spawnChip(true);
}
lastStage = index;
stageEl.textContent = `${String(index + 1).padStart(2, "0")} · ${to.label}`;
descriptionEl.textContent = to.description;
stepText.textContent = `${String(index + 1).padStart(2, "0")} / 05`;
}
progressEl.style.width = `${Math.min(1, simulationTime / TOTAL_DURATION) * 100}%`;
stagePulse = Math.max(0, stagePulse - 0.025);
updateShell(outerShellGeometry, TREE_RADIUS, true);
updateShell(innerShellGeometry, INNER_RADIUS, false);
updateRim();
updateCavityMeshes();
rimMaterial.emissive.set(0x9b4b25);
rimMaterial.emissiveIntensity = stagePulse * 0.65;
}
function updateBird() {
const period = simulationTime < TOTAL_DURATION ? 0.72 : 1.05;
const phase = (simulationTime % period) / period;
if (previousBirdPhase > 0.22 && phase <= 0.22) {
const count = simulationTime < TOTAL_DURATION ? 3 : 1;
for (let i = 0; i < count; i++) spawnChip();
}
previousBirdPhase = phase;
const strike = Math.exp(-Math.pow((phase - 0.22) / 0.095, 2));
bird.position.x = birdBase.x - 0.17 * strike;
bird.position.y = birdBase.y + 0.025 * strike;
bird.rotation.z = -0.035 * strike;
head.rotation.z = 0.095 * strike;
body.scale.y = 1.03 - 0.025 * strike;
}
function clearChips() {
for (const chip of chips) {
scene.remove(chip.mesh);
}
chips.length = 0;
settledChips = 0;
}
replayButton.addEventListener("click", () => {
simulationTime = 0;
previousBirdPhase = 1;
lastStage = -1;
stagePulse = 0;
clearChips();
});
let previousFrame = performance.now();
function animate(now) {
requestAnimationFrame(animate);
const dt = THREE.MathUtils.clamp((now - previousFrame) / 1000, 0, 0.05);
previousFrame = now;
simulationTime += dt;
updateTimeline();
updateBird();
updateChips(dt);
renderer.render(scene, camera);
}
updateTimeline();
updateBird();
document.body.classList.add("ready");
requestAnimationFrame(animate);
</script>
</body>
</html>
```