Prompt specification
Build or customize the Three.js 3D scene "Leaf fragment enters a burrow" 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: Leaf fragment enters a burrow
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/leaf-fragment-enters-a-burrow-x0674
Scene Description Prompt:
Make one HTML file using Three.js from cdnjs for this voxel scene: Animate an earthworm moving a small leaf fragment toward a tunnel entrance; a time sequence shows material entering the soil without claiming measured decomposition speed.
Working Three.js Starter (leaf-fragment-enters-a-burrow-x0674):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Into the Soil — Voxel Field Sequence</title>
<style>
:root {
--ink: #18342b;
--muted: #61756c;
--paper: #f5f1e7;
--panel: rgba(250, 247, 238, 0.92);
--line: rgba(24, 52, 43, 0.15);
--accent: #d78a3d;
--green: #39785a;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #e8e4d8;
color: var(--ink);
font-family:
Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
button,
input {
font: inherit;
}
button {
color: inherit;
}
#scene {
position: fixed;
inset: 0;
width: 100%;
height: 100%;
display: block;
cursor: grab;
touch-action: none;
outline: none;
}
#scene:active {
cursor: grabbing;
}
.intro {
position: fixed;
top: 24px;
left: 24px;
width: min(360px, calc(100vw - 48px));
padding: 20px 22px;
border: 1px solid var(--line);
border-radius: 18px;
background: var(--panel);
box-shadow: 0 18px 50px rgba(44, 54, 39, 0.12);
backdrop-filter: blur(12px);
pointer-events: none;
}
.eyebrow {
display: flex;
align-items: center;
gap: 9px;
margin-bottom: 9px;
color: var(--green);
font-size: 0.72rem;
font-weight: 800;
letter-spacing: 0.16em;
text-transform: uppercase;
}
.eyebrow::before {
content: "";
width: 20px;
height: 3px;
border-radius: 3px;
background: var(--accent);
}
h1 {
margin: 0;
font-family: Georgia, "Times New Roman", serif;
font-size: clamp(2rem, 4vw, 3rem);
font-weight: 500;
line-height: 0.98;
letter-spacing: -0.035em;
}
.intro p {
margin: 13px 0 0;
color: var(--muted);
font-size: 0.92rem;
line-height: 1.55;
}
.field-note {
position: fixed;
top: 24px;
right: 24px;
width: min(285px, calc(100vw - 48px));
padding: 16px 17px;
border: 1px solid var(--line);
border-radius: 16px;
background: var(--panel);
box-shadow: 0 14px 42px rgba(44, 54, 39, 0.1);
backdrop-filter: blur(12px);
}
.field-note strong {
display: block;
margin-bottom: 6px;
font-size: 0.78rem;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.field-note p {
margin: 0;
color: var(--muted);
font-size: 0.78rem;
line-height: 1.45;
}
.field-note button {
margin-top: 12px;
padding: 7px 10px;
border: 1px solid var(--line);
border-radius: 9px;
background: transparent;
font-size: 0.74rem;
font-weight: 700;
cursor: pointer;
}
.field-note button:hover {
background: rgba(57, 120, 90, 0.08);
}
.world-label {
position: fixed;
z-index: 3;
padding: 6px 9px;
border: 1px solid rgba(24, 52, 43, 0.15);
border-radius: 999px;
background: rgba(250, 247, 238, 0.9);
color: var(--ink);
box-shadow: 0 6px 20px rgba(24, 52, 43, 0.12);
font-size: 0.68rem;
font-weight: 750;
letter-spacing: 0.035em;
white-space: nowrap;
pointer-events: none;
transform: translate(-50%, -100%);
transition: opacity 160ms ease;
}
.timeline-panel {
position: fixed;
left: 50%;
bottom: 20px;
width: min(900px, calc(100vw - 32px));
padding: 15px 18px 13px;
border: 1px solid var(--line);
border-radius: 18px;
background: var(--panel);
box-shadow: 0 18px 60px rgba(44, 54, 39, 0.15);
backdrop-filter: blur(14px);
transform: translateX(-50%);
}
.timeline-main {
display: grid;
grid-template-columns: auto 1fr;
align-items: center;
gap: 14px;
}
#play {
width: 44px;
height: 44px;
display: grid;
place-items: center;
border: 0;
border-radius: 50%;
background: var(--ink);
color: white;
font-size: 0.9rem;
cursor: pointer;
transition: transform 150ms ease, background 150ms ease;
}
#play:hover {
transform: scale(1.05);
background: var(--green);
}
.stage-copy {
min-width: 0;
}
#stage-kicker {
margin-bottom: 2px;
color: var(--accent);
font-size: 0.65rem;
font-weight: 850;
letter-spacing: 0.12em;
text-transform: uppercase;
}
#stage-title {
overflow: hidden;
font-family: Georgia, "Times New Roman", serif;
font-size: 1.25rem;
text-overflow: ellipsis;
white-space: nowrap;
}
#stage-description {
margin-top: 3px;
overflow: hidden;
color: var(--muted);
font-size: 0.76rem;
text-overflow: ellipsis;
white-space: nowrap;
}
#timeline {
--progress: 0%;
width: 100%;
height: 5px;
margin: 14px 0 11px;
border-radius: 999px;
outline: none;
appearance: none;
background:
linear-gradient(
to right,
var(--green) 0,
var(--green) var(--progress),
rgba(24, 52, 43, 0.13) var(--progress),
rgba(24, 52, 43, 0.13) 100%
);
cursor: pointer;
}
#timeline::-webkit-slider-thumb {
width: 16px;
height: 16px;
border: 3px solid var(--paper);
border-radius: 50%;
appearance: none;
background: var(--green);
box-shadow: 0 1px 6px rgba(24, 52, 43, 0.28);
}
#timeline::-moz-range-thumb {
width: 11px;
height: 11px;
border: 3px solid var(--paper);
border-radius: 50%;
background: var(--green);
box-shadow: 0 1px 6px rgba(24, 52, 43, 0.28);
}
.stage-markers {
display: grid;
grid-template-columns: repeat(5, 1fr);
gap: 5px;
}
.stage-button {
padding: 5px 4px;
border: 0;
border-radius: 7px;
background: transparent;
color: var(--muted);
font-size: 0.65rem;
font-weight: 700;
text-align: left;
cursor: pointer;
}
.stage-button span {
display: block;
margin-bottom: 3px;
color: rgba(24, 52, 43, 0.3);
font-size: 0.58rem;
letter-spacing: 0.08em;
}
.stage-button.active {
background: rgba(57, 120, 90, 0.09);
color: var(--green);
}
.stage-button.active span {
color: var(--accent);
}
@media (max-width: 720px) {
.intro {
top: 12px;
left: 12px;
width: calc(100vw - 24px);
padding: 14px 16px;
}
.intro p {
display: none;
}
h1 {
font-size: 2rem;
}
.field-note {
top: 120px;
right: 12px;
width: 210px;
padding: 11px 12px;
}
.field-note p {
font-size: 0.7rem;
}
.field-note button {
margin-top: 8px;
}
.timeline-panel {
bottom: 10px;
width: calc(100vw - 20px);
padding: 12px 13px 10px;
}
#stage-description {
display: none;
}
#stage-title {
font-size: 1.05rem;
}
.stage-button {
font-size: 0;
text-align: center;
}
.stage-button span {
font-size: 0.65rem;
}
.world-label {
font-size: 0.6rem;
}
}
</style>
</head>
<body>
<canvas
id="scene"
tabindex="0"
aria-label="Interactive voxel scene. Drag to orbit, scroll to zoom, and use the timeline to examine the sequence."
></canvas>
<header class="intro">
<div class="eyebrow">Voxel field sequence · 01</div>
<h1>Into the soil</h1>
<p>
An earthworm carries a small leaf fragment toward a tunnel. The cutaway
view follows the material below the soil surface.
</p>
</header>
<aside class="field-note">
<strong>Illustrative sequence</strong>
<p>
No measured decomposition speed is implied. The timing only demonstrates
how the material enters the soil.
</p>
<button id="reset-view" type="button">Reset camera</button>
</aside>
<div id="entrance-label" class="world-label" aria-hidden="true">
Tunnel entrance
</div>
<div id="cutaway-label" class="world-label" aria-hidden="true">
Soil cutaway
</div>
<section class="timeline-panel" aria-label="Scene sequence controls">
<div class="timeline-main">
<button id="play" type="button" aria-label="Pause sequence">❚❚</button>
<div class="stage-copy" aria-live="polite">
<div id="stage-kicker">Stage 01</div>
<div id="stage-title">Carrying the fragment</div>
<div id="stage-description">
The leaf fragment moves across the voxel surface.
</div>
</div>
</div>
<input
id="timeline"
type="range"
min="0"
max="1000"
step="1"
value="0"
aria-label="Sequence position"
/>
<div class="stage-markers">
<button class="stage-button active" type="button" data-progress="0">
<span>01</span>Surface
</button>
<button class="stage-button" type="button" data-progress="0.29">
<span>02</span>Entrance
</button>
<button class="stage-button" type="button" data-progress="0.44">
<span>03</span>Entry
</button>
<button class="stage-button" type="button" data-progress="0.61">
<span>04</span>Below
</button>
<button class="stage-button" type="button" data-progress="0.82">
<span>05</span>Settling
</button>
</div>
</section>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const canvas = document.querySelector("#scene");
const playButton = document.querySelector("#play");
const timeline = document.querySelector("#timeline");
const stageKicker = document.querySelector("#stage-kicker");
const stageTitle = document.querySelector("#stage-title");
const stageDescription = document.querySelector("#stage-description");
const stageButtons = [...document.querySelectorAll(".stage-button")];
const entranceLabel = document.querySelector("#entrance-label");
const cutawayLabel = document.querySelector("#cutaway-label");
const resetViewButton = document.querySelector("#reset-view");
const stages = [
{
at: 0,
kicker: "Stage 01",
title: "Carrying the fragment",
description: "The leaf fragment moves across the voxel surface."
},
{
at: 0.29,
kicker: "Stage 02",
title: "Reaching the tunnel entrance",
description: "The earthworm aligns the fragment with the dark tunnel mouth."
},
{
at: 0.44,
kicker: "Stage 03",
title: "Crossing the soil boundary",
description: "The fragment passes from the surface into the tunnel."
},
{
at: 0.61,
kicker: "Stage 04",
title: "Moving below the surface",
description: "A cutaway view follows the fragment down the passage."
},
{
at: 0.82,
kicker: "Stage 05",
title: "Settling among soil voxels",
description: "Smaller voxel pieces settle beneath the surface."
}
];
const state = {
progress: 0,
playing: !window.matchMedia("(prefers-reduced-motion: reduce)").matches
};
const renderer = new THREE.WebGLRenderer({
canvas,
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.12;
const scene = new THREE.Scene();
scene.background = new THREE.Color("#e8e4d8");
scene.fog = new THREE.Fog("#e8e4d8", 23, 43);
const camera = new THREE.PerspectiveCamera(
42,
window.innerWidth / window.innerHeight,
0.1,
100
);
const hemisphere = new THREE.HemisphereLight("#fff9e9", "#70513d", 2.1);
scene.add(hemisphere);
const sun = new THREE.DirectionalLight("#fff0cf", 3.1);
sun.position.set(-6, 11, 8);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -12;
sun.shadow.camera.right = 12;
sun.shadow.camera.top = 12;
sun.shadow.camera.bottom = -12;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 32;
sun.shadow.bias = -0.00035;
scene.add(sun);
const fill = new THREE.DirectionalLight("#b8d8df", 1.0);
fill.position.set(8, 4, -8);
scene.add(fill);
const floor = new THREE.Mesh(
new THREE.PlaneGeometry(70, 70),
new THREE.MeshStandardMaterial({
color: "#e8e4d8",
roughness: 1
})
);
floor.rotation.x = -Math.PI / 2;
floor.position.y = -4.05;
floor.receiveShadow = true;
scene.add(floor);
const cubeGeometry = new THREE.BoxGeometry(1, 1, 1);
const dummy = new THREE.Object3D();
function hash(value) {
const v = Math.sin(value * 12.9898 + 78.233) * 43758.5453;
return v - Math.floor(v);
}
function makeInstanced(entries, geometry, material) {
const mesh = new THREE.InstancedMesh(
geometry,
material,
entries.length
);
entries.forEach((entry, index) => {
dummy.position.set(...entry.position);
dummy.rotation.set(
entry.rotation?.[0] ?? 0,
entry.rotation?.[1] ?? 0,
entry.rotation?.[2] ?? 0
);
dummy.scale.set(
entry.scale?.[0] ?? 1,
entry.scale?.[1] ?? 1,
entry.scale?.[2] ?? 1
);
dummy.updateMatrix();
mesh.setMatrixAt(index, dummy.matrix);
mesh.setColorAt(index, new THREE.Color(entry.color));
});
mesh.instanceMatrix.needsUpdate = true;
if (mesh.instanceColor) {
mesh.instanceColor.needsUpdate = true;
}
mesh.castShadow = true;
mesh.receiveShadow = true;
scene.add(mesh);
return mesh;
}
// The cavity is a staircase-like cutaway through the front soil layers.
const caveCells = new Set();
for (let row = 0; row < 5; row++) {
caveCells.add(`14,${row}`);
}
for (let column = 11; column <= 14; column++) {
caveCells.add(`${column},3`);
}
for (let column = 10; column <= 14; column++) {
caveCells.add(`${column},4`);
}
const soilEntries = [];
const caveBackingEntries = [];
const layers = [1.2, 0, -1.2];
layers.forEach((z, layerIndex) => {
for (let row = 0; row < 5; row++) {
for (let column = 0; column < 17; column++) {
const key = `${column},${row}`;
// Open the front and middle layers where the tunnel is visible.
if (caveCells.has(key) && layerIndex < 2) {
continue;
}
const x = (column - 8) * 0.75;
const y = -0.45 - row * 0.75;
const variation = hash(column * 17 + row * 31 + layerIndex * 53);
const colors = ["#75462d", "#855339", "#69412c"];
const color = new THREE.Color(colors[layerIndex]);
color.offsetHSL(
(variation - 0.5) * 0.025,
(variation - 0.5) * 0.07,
(variation - 0.5) * 0.13
);
soilEntries.push({
position: [x, y, z],
color: `#${color.getHexString()}`
});
}
}
});
caveCells.forEach((key) => {
const [column, row] = key.split(",").map(Number);
const x = (column - 8) * 0.75;
const y = -0.45 - row * 0.75;
caveBackingEntries.push({
position: [x, y, 0.42],
color: row === 4 ? "#4a3024" : "#573526"
});
});
makeInstanced(
soilEntries,
new THREE.BoxGeometry(0.72, 0.72, 0.72),
new THREE.MeshStandardMaterial({
color: 0xffffff,
roughness: 0.98
})
);
makeInstanced(
caveBackingEntries,
new THREE.BoxGeometry(0.69, 0.69, 0.69),
new THREE.MeshStandardMaterial({
color: 0xffffff,
roughness: 1
})
);
// Surface tiles, with an opening at the tunnel.
const grassEntries = [];
const grassRows = [-1.5, -0.75, 0, 0.75, 1.5];
grassRows.forEach((z, zIndex) => {
for (let column = 0; column < 17; column++) {
const x = (column - 8) * 0.75;
const isOpening =
Math.abs(x - 4.5) < 0.1 &&
z >= -0.76 &&
z <= 0.76;
if (isOpening) {
continue;
}
const variation = hash(column * 11 + zIndex * 19);
const color = new THREE.Color("#568548");
color.offsetHSL(
(variation - 0.5) * 0.035,
(variation - 0.5) * 0.1,
(variation - 0.5) * 0.13
);
grassEntries.push({
position: [x, 0.12, z],
scale: [1, 0.88 + variation * 0.24, 1],
color: `#${color.getHexString()}`
});
}
});
makeInstanced(
grassEntries,
new THREE.BoxGeometry(0.72, 0.44, 0.72),
new THREE.MeshStandardMaterial({
color: 0xffffff,
roughness: 0.95
})
);
// Dark shaft beneath the opening.
const shaft = new THREE.Mesh(
new THREE.BoxGeometry(0.66, 0.7, 0.66),
new THREE.MeshStandardMaterial({
color: "#38271f",
roughness: 1
})
);
shaft.position.set(4.5, -0.38, 0);
shaft.castShadow = true;
scene.add(shaft);
// Pixel-like surface details.
const pebbleEntries = [];
for (let i = 0; i < 17; i++) {
const x = -5.4 + hash(i * 8.1) * 10.4;
const z = -2.15 + hash(i * 13.7) * 4.2;
const size = 0.13 + hash(i * 19.3) * 0.17;
if (Math.abs(x - 4.5) < 0.8 && Math.abs(z) < 0.8) {
continue;
}
pebbleEntries.push({
position: [x, 0.34 + size * 0.2, z],
rotation: [0, hash(i * 7.4) * Math.PI, 0],
scale: [size, size * 0.7, size * 0.9],
color: i % 3 === 0 ? "#938676" : "#746b60"
});
}
makeInstanced(
pebbleEntries,
cubeGeometry,
new THREE.MeshStandardMaterial({
color: 0xffffff,
roughness: 1
})
);
// Small voxel sprouts.
const sproutEntries = [];
for (let i = 0; i < 28; i++) {
const x = -6 + hash(i * 2.7) * 12;
const z = i % 2 === 0 ? -2.05 : 2.08;
const height = 0.18 + hash(i * 9.2) * 0.2;
if (x > 3.6 && x < 5.4) {
continue;
}
sproutEntries.push({
position: [x, 0.34 + height * 0.5, z],
scale: [0.065, height, 0.065],
color: i % 3 === 0 ? "#83a65d" : "#416d3d"
});
}
makeInstanced(
sproutEntries,
cubeGeometry,
new THREE.MeshStandardMaterial({
color: 0xffffff,
roughness: 1
})
);
// Roots are deliberately blocky and remain close to the soil face.
const rootMaterial = new THREE.MeshStandardMaterial({
color: "#b8925f",
roughness: 1
});
function addRootVoxel(x, y, scale = 0.12) {
const root = new THREE.Mesh(cubeGeometry, rootMaterial);
root.position.set(x, y, 1.585);
root.scale.set(scale, scale, 0.055);
root.castShadow = true;
scene.add(root);
}
[
[-3.8, -0.25], [-3.65, -0.55], [-3.5, -0.8],
[-3.7, -1.1], [-3.85, -1.4], [-3.65, -1.65],
[0.3, -0.3], [0.55, -0.52], [0.42, -0.78],
[0.65, -1.02], [0.48, -1.27],
[5.2, -0.25], [5.35, -0.52], [5.2, -0.8]
].forEach(([x, y]) => addRootVoxel(x, y, 0.1 + hash(x + y) * 0.05));
// Movement paths.
const surfaceCurve = new THREE.CatmullRomCurve3(
[
new THREE.Vector3(-6.15, 0.56, 1.48),
new THREE.Vector3(-4.75, 0.56, 1.58),
new THREE.Vector3(-3.05, 0.56, 1.6),
new THREE.Vector3(-1.15, 0.56, 1.56),
new THREE.Vector3(0.75, 0.56, 1.48),
new THREE.Vector3(2.35, 0.56, 1.3),
new THREE.Vector3(3.65, 0.56, 0.82),
new THREE.Vector3(4.35, 0.56, 0.2),
new THREE.Vector3(4.5, 0.56, 0.02)
],
false,
"catmullrom",
0.35
);
const undergroundCurve = new THREE.CatmullRomCurve3(
[
new THREE.Vector3(4.5, 0.56, 0.02),
new THREE.Vector3(4.5, 0.18, 0.1),
new THREE.Vector3(4.5, -0.35, 0.35),
new THREE.Vector3(4.5, -1.05, 0.62),
new THREE.Vector3(4.5, -1.75, 0.82),
new THREE.Vector3(4.5, -2.4, 0.96),
new THREE.Vector3(3.75, -2.7, 1.02),
new THREE.Vector3(2.25, -2.72, 1.05),
new THREE.Vector3(1.5, -3.22, 1.04)
],
false,
"catmullrom",
0.32
);
function sampleCurve(curve, count) {
const points = [];
for (let i = 0; i <= count; i++) {
points.push(curve.getPointAt(i / count));
}
return points;
}
const surfaceSamples = sampleCurve(surfaceCurve, 110);
const shaftSamples = sampleCurve(undergroundCurve, 70);
const combinedPath = surfaceSamples.concat(shaftSamples.slice(1));
function pathLength(points) {
let length = 0;
for (let i = 1; i < points.length; i++) {
length += points[i].distanceTo(points[i - 1]);
}
return length;
}
const surfaceLength = pathLength(surfaceSamples);
const shaftLength = pathLength(shaftSamples);
const combinedLength = pathLength(combinedPath);
function pointOnPath(points, distance) {
let d = THREE.MathUtils.clamp(distance, 0, pathLength(points));
for (let i = 1; i < points.length; i++) {
const segmentLength = points[i].distanceTo(points[i - 1]);
if (d <= segmentLength || i === points.length - 1) {
const amount = segmentLength === 0 ? 0 : d / segmentLength;
const point = new THREE.Vector3().lerpVectors(
points[i - 1],
points[i],
amount
);
const tangent = new THREE.Vector3()
.subVectors(points[i], points[i - 1])
.normalize();
return { point, tangent };
}
d -= segmentLength;
}
return {
point: points[points.length - 1].clone(),
tangent: new THREE.Vector3(1, 0, 0)
};
}
// Earthworm.
const worm = new THREE.Group();
scene.add(worm);
const segmentGeometry = new THREE.BoxGeometry(1, 1, 1);
const segmentColors = [
"#bd6c69",
"#d7897e",
"#c77673",
"#df9587",
"#c97873",
"#d4867c",
"#b96563",
"#cf7d75"
];
const segments = segmentColors.map((color, index) => {
const material = new THREE.MeshStandardMaterial({
color,
roughness: 0.83
});
const segment = new THREE.Mesh(segmentGeometry, material);
const isHead = index === 0;
segment.scale.set(
isHead ? 0.58 : 0.5 - index * 0.012,
isHead ? 0.39 : 0.34,
isHead ? 0.45 : 0.4
);
segment.castShadow = true;
segment.receiveShadow = true;
worm.add(segment);
return segment;
});
const eyeMaterial = new THREE.MeshBasicMaterial({ color: "#25201d" });
[-0.13, 0.13].forEach((z) => {
const eye = new THREE.Mesh(cubeGeometry, eyeMaterial);
eye.position.set(0.3, 0.1, z);
eye.scale.set(0.065, 0.07, 0.065);
segments[0].add(eye);
});
const mouth = new THREE.Mesh(
cubeGeometry,
new THREE.MeshStandardMaterial({ color: "#6d403a", roughness: 1 })
);
mouth.position.set(0.32, -0.08, 0);
mouth.scale.set(0.06, 0.05, 0.13);
segments[0].add(mouth);
// Leaf fragment made from small voxel pieces.
const leafGroup = new THREE.Group();
scene.add(leafGroup);
const leafMaterial = new THREE.MeshStandardMaterial({
color: "#4c913d",
roughness: 0.9
});
const leafLightMaterial = new THREE.MeshStandardMaterial({
color: "#72ad43",
roughness: 0.9
});
const leafDarkMaterial = new THREE.MeshStandardMaterial({
color: "#357337",
roughness: 0.95
});
const leafPixels = [
[-0.14, 0.04, 0, 0.16, leafDarkMaterial],
[0, 0.08, 0.015, 0.18, leafMaterial],
[0.14, 0.04, -0.01, 0.16, leafLightMaterial],
[-0.07, -0.05, 0.015, 0.15, leafMaterial],
[0.07, -0.05, -0.015, 0.15, leafLightMaterial],
[0, -0.12, 0, 0.13, leafDarkMaterial]
];
leafPixels.forEach(([x, y, z, size, material]) => {
const pixel = new THREE.Mesh(cubeGeometry, material);
pixel.position.set(x, y, z);
pixel.scale.set(size, size, size * 0.65);
pixel.rotation.z = (x + 0.2) * 0.8;
pixel.castShadow = true;
leafGroup.add(pixel);
});
const stem = new THREE.Mesh(
cubeGeometry,
new THREE.MeshStandardMaterial({
color: "#8c6835",
roughness: 1
})
);
stem.position.set(-0.2, -0.02, 0);
stem.scale.set(0.23, 0.07, 0.08);
stem.rotation.z = -0.28;
stem.castShadow = true;
leafGroup.add(stem);
// Smaller pieces used only after the leaf reaches the lower tunnel.
const fragmentGroup = new THREE.Group();
scene.add(fragmentGroup);
fragmentGroup.visible = false;
const fragmentOffsets = [
new THREE.Vector3(-0.22, 0.11, 0.04),
new THREE.Vector3(0.2, 0.08, 0.08),
new THREE.Vector3(0.07, -0.14, 0.02),
new THREE.Vector3(-0.12, -0.17, 0.06),
new THREE.Vector3(0.31, -0.08, 0.02),
new THREE.Vector3(-0.02, 0.22, 0.09),
new THREE.Vector3(-0.31, -0.02, 0.01)
];
const fragments = fragmentOffsets.map((offset, index) => {
const material = new THREE.MeshStandardMaterial({
color: "#4c913d",
roughness: 0.95
});
const fragment = new THREE.Mesh(cubeGeometry, material);
const size = 0.1 + (index % 3) * 0.025;
fragment.scale.set(size, size, size * 0.75);
fragment.castShadow = true;
fragmentGroup.add(fragment);
return fragment;
});
function clamp01(value) {
return Math.max(0, Math.min(1, value));
}
function smoothstep(value) {
const t = clamp01(value);
return t * t * (3 - 2 * t);
}
function easeOutCubic(value) {
const t = clamp01(value);
return 1 - Math.pow(1 - t, 3);
}
const greenFragment = new THREE.Color("#4c913d");
const brownFragment = new THREE.Color("#68462d");
function updateScene(progress) {
let headDistance;
if (progress <= 0.42) {
const move = smoothstep(progress / 0.42);
headDistance = 0.55 + (surfaceLength - 0.55) * move;
} else {
const descend = smoothstep((progress - 0.42) / 0.3);
headDistance = surfaceLength + shaftLength * descend;
}
for (let index = 0; index < segments.length; index++) {
const sample = pointOnPath(
combinedPath,
headDistance - index * 0.37
);
const segment = segments[index];
segment.position.copy(sample.point);
segment.position.y +=
Math.sin(progress * 92 - index * 0.75) * 0.016;
const yaw = -Math.atan2(sample.tangent.z, sample.tangent.x);
segment.rotation.y = yaw;
segment.rotation.z =
Math.sin(progress * 70 - index * 0.7) * 0.035;
}
if (progress < 0.42) {
const leafSample = pointOnPath(
combinedPath,
headDistance + 0.62
);
leafGroup.position.copy(leafSample.point);
leafGroup.position.y += 0.06;
const yaw = -Math.atan2(
leafSample.tangent.z,
leafSample.tangent.x
);
leafGroup.rotation.set(
Math.sin(progress * 55) * 0.1,
yaw,
Math.cos(progress * 48) * 0.12
);
} else {
const undergroundProgress = clamp01(
(progress - 0.42) / 0.4
);
const leafPosition = undergroundCurve.getPointAt(
undergroundProgress
);
const leafTangent = undergroundCurve.getTangentAt(
undergroundProgress
);
leafGroup.position.copy(leafPosition);
leafGroup.position.y += 0.025;
const yaw = -Math.atan2(
leafTangent.z,
leafTangent.x
);
leafGroup.rotation.set(
Math.sin(progress * 45) * 0.18,
yaw,
Math.cos(progress * 39) * 0.25
);
}
const splitStart = 0.82;
const splitProgress = clamp01(
(progress - splitStart) / 0.18
);
const spread = easeOutCubic(splitProgress);
const finalPoint = undergroundCurve.getPointAt(1);
leafGroup.visible = progress < splitStart;
fragmentGroup.visible = progress >= splitStart;
fragments.forEach((fragment, index) => {
const offset = fragmentOffsets[index];
fragment.position.copy(finalPoint);
fragment.position.addScaledVector(offset, spread);
fragment.position.y -= spread * 0.055;
fragment.rotation.set(
splitProgress * (index + 0.4),
splitProgress * (index * 0.7 + 0.2),
splitProgress * (index * 0.35)
);
fragment.material.color.copy(greenFragment).lerp(
brownFragment,
smoothstep(splitProgress)
);
});
}
function updateInterface(progress) {
let stageIndex = 0;
for (let i = 0; i < stages.length; i++) {
if (progress >= stages[i].at) {
stageIndex = i;
}
}
const stage = stages[stageIndex];
stageKicker.textContent = stage.kicker;
stageTitle.textContent = stage.title;
stageDescription.textContent = stage.description;
timeline.value = Math.round(progress * 1000);
timeline.style.setProperty("--progress", `${progress * 100}%`);
timeline.setAttribute("aria-valuetext", stage.title);
stageButtons.forEach((button, index) => {
button.classList.toggle("active", index === stageIndex);
});
if (!state.playing && progress >= 1) {
playButton.textContent = "↻";
playButton.setAttribute("aria-label", "Replay sequence");
} else {
playButton.textContent = state.playing ? "❚❚" : "▶";
playButton.setAttribute(
"aria-label",
state.playing ? "Pause sequence" : "Play sequence"
);
}
}
// Direct camera controls, implemented without Three.js addons.
const cameraState = {
targetYaw: 0.47,
targetPitch: 0.47,
currentYaw: 0.47,
currentPitch: 0.47,
zoom: 1,
currentDistance: 15.5,
fitDistance: 15.5,
dragging: false,
lastX: 0,
lastY: 0
};
function updateFitDistance() {
const aspect = window.innerWidth / window.innerHeight;
cameraState.fitDistance = Math.max(15.5, 16.8 / aspect);
}
function resetCamera() {
cameraState.targetYaw = 0.47;
cameraState.targetPitch = 0.47;
cameraState.zoom = 1;
}
function updateCamera(dt) {
const smoothing = 1 - Math.exp(-dt * 11);
cameraState.currentYaw = THREE.MathUtils.lerp(
cameraState.currentYaw,
cameraState.targetYaw,
smoothing
);
cameraState.currentPitch = THREE.MathUtils.lerp(
cameraState.currentPitch,
cameraState.targetPitch,
smoothing
);
const desiredDistance = cameraState.fitDistance * cameraState.zoom;
cameraState.currentDistance = THREE.MathUtils.lerp(
cameraState.currentDistance,
desiredDistance,
smoothing
);
const target = new THREE.Vector3(0, -0.55, 0.25);
const horizontal =
cameraState.currentDistance *
Math.cos(cameraState.currentPitch);
camera.position.set(
target.x +
horizontal * Math.sin(cameraState.currentYaw),
target.y +
cameraState.currentDistance *
Math.sin(cameraState.currentPitch),
target.z +
horizontal * Math.cos(cameraState.currentYaw)
);
camera.lookAt(target);
}
canvas.addEventListener("pointerdown", (event) => {
if (event.button !== 0) {
return;
}
cameraState.dragging = true;
cameraState.lastX = event.clientX;
cameraState.lastY = event.clientY;
canvas.setPointerCapture(event.pointerId);
});
canvas.addEventListener("pointermove", (event) => {
if (!cameraState.dragging) {
return;
}
const dx = event.clientX - cameraState.lastX;
const dy = event.clientY - cameraState.lastY;
cameraState.targetYaw -= dx * 0.006;
cameraState.targetPitch = THREE.MathUtils.clamp(
cameraState.targetPitch + dy * 0.005,
0.16,
1.15
);
cameraState.lastX = event.clientX;
cameraState.lastY = event.clientY;
});
function endDrag(event) {
cameraState.dragging = false;
if (canvas.hasPointerCapture(event.pointerId)) {
canvas.releasePointerCapture(event.pointerId);
}
}
canvas.addEventListener("pointerup", endDrag);
canvas.addEventListener("pointercancel", endDrag);
canvas.addEventListener(
"wheel",
(event) => {
event.preventDefault();
cameraState.zoom = THREE.MathUtils.clamp(
cameraState.zoom * Math.exp(event.deltaY * 0.001),
0.62,
1.65
);
},
{ passive: false }
);
canvas.addEventListener("keydown", (event) => {
if (event.key === "ArrowLeft") {
cameraState.targetYaw -= 0.12;
} else if (event.key === "ArrowRight") {
cameraState.targetYaw += 0.12;
} else if (event.key === "ArrowUp") {
cameraState.targetPitch = THREE.MathUtils.clamp(
cameraState.targetPitch - 0.08,
0.16,
1.15
);
} else if (event.key === "ArrowDown") {
cameraState.targetPitch = THREE.MathUtils.clamp(
cameraState.targetPitch + 0.08,
0.16,
1.15
);
} else if (event.key === "+" || event.key === "=") {
cameraState.zoom = Math.max(
0.62,
cameraState.zoom * 0.92
);
} else if (event.key === "-") {
cameraState.zoom = Math.min(
1.65,
cameraState.zoom * 1.08
);
} else if (event.code === "Space") {
event.preventDefault();
togglePlayback();
}
});
function togglePlayback() {
if (state.progress >= 1 && !state.playing) {
state.progress = 0;
}
state.playing = !state.playing;
updateInterface(state.progress);
}
playButton.addEventListener("click", togglePlayback);
timeline.addEventListener("input", () => {
state.progress = Number(timeline.value) / 1000;
state.playing = false;
updateScene(state.progress);
updateInterface(state.progress);
});
stageButtons.forEach((button) => {
button.addEventListener("click", () => {
state.progress = Number(button.dataset.progress);
state.playing = false;
updateScene(state.progress);
updateInterface(state.progress);
});
});
resetViewButton.addEventListener("click", resetCamera);
function placeWorldLabel(element, worldPosition) {
const projected = worldPosition.clone().project(camera);
const visible = projected.z > -1 && projected.z < 1;
if (!visible) {
element.style.opacity = "0";
return;
}
const x = (projected.x * 0.5 + 0.5) * window.innerWidth;
const y = (-projected.y * 0.5 + 0.5) * window.innerHeight;
element.style.left = `${x}px`;
element.style.top = `${y}px`;
element.style.opacity = "1";
}
const entrancePosition = new THREE.Vector3(4.5, 0.95, 0.05);
const cutawayPosition = new THREE.Vector3(1.4, -2.55, 1.5);
function resize() {
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
updateFitDistance();
}
window.addEventListener("resize", resize);
updateFitDistance();
const sequenceDuration = 24;
const clock = new THREE.Clock();
function animate() {
requestAnimationFrame(animate);
const dt = Math.min(clock.getDelta(), 0.05);
if (state.playing) {
state.progress += dt / sequenceDuration;
if (state.progress >= 1) {
state.progress = 1;
state.playing = false;
}
}
updateScene(state.progress);
updateInterface(state.progress);
updateCamera(dt);
placeWorldLabel(entranceLabel, entrancePosition);
placeWorldLabel(cutawayLabel, cutawayPosition);
renderer.render(scene, camera);
}
updateScene(0);
updateInterface(0);
animate();
</script>
</body>
</html>
```