Prompt specification
Build or customize the Three.js 3D scene "Time-step habitat establishment" 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: Time-step habitat establishment
Category: Biology & Nature · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/time-step-habitat-establishment-x0730
Scene Description Prompt:
Build a self-contained HTML scene with Three.js from cdnjs featuring ecological succession. Animate plants appearing, growing, and altering shade on a fixed terrain patch; retain earlier survivors so the scene shows community change instead of replacing entire landscapes instantly. Low-poly.
Working Three.js Starter (time-step-habitat-establishment-x0730):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="theme-color" content="#10211e">
<title>Ecological Succession — Three.js</title>
<style>
:root {
--accent: #b9df87;
--panel: rgba(10, 27, 24, 0.76);
--line: rgba(255, 255, 255, 0.12);
--text: #f4f7ef;
--muted: #a8b9ad;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #10211e;
color: var(--text);
font-family:
Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
button,
select,
input {
font: inherit;
}
#scene {
position: fixed;
inset: 0;
}
#scene canvas {
display: block;
width: 100%;
height: 100%;
outline: none;
touch-action: none;
}
.glass {
background: var(--panel);
border: 1px solid var(--line);
box-shadow: 0 18px 50px rgba(0, 0, 0, 0.25);
backdrop-filter: blur(14px);
-webkit-backdrop-filter: blur(14px);
}
#hud {
position: fixed;
inset: 0;
pointer-events: none;
}
.intro {
position: absolute;
top: 24px;
left: 24px;
width: min(390px, calc(100vw - 48px));
padding: 20px 22px;
border-radius: 18px;
}
.eyebrow {
margin-bottom: 9px;
color: var(--accent);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.19em;
text-transform: uppercase;
}
h1 {
margin: 0;
font-size: clamp(26px, 3vw, 39px);
line-height: 1;
letter-spacing: -0.045em;
}
.intro p {
margin: 12px 0 0;
color: var(--muted);
font-size: 13px;
line-height: 1.55;
}
.stats {
position: absolute;
top: 24px;
right: 24px;
width: 220px;
padding: 16px 18px;
border-radius: 18px;
}
.stat {
display: flex;
align-items: baseline;
justify-content: space-between;
gap: 12px;
padding: 7px 0;
}
.stat + .stat {
border-top: 1px solid rgba(255, 255, 255, 0.07);
}
.stat span:first-child {
color: var(--muted);
font-size: 11px;
}
.stat strong {
color: var(--accent);
font-size: 18px;
font-variant-numeric: tabular-nums;
}
#reset-camera {
width: 100%;
margin-top: 9px;
padding: 8px;
color: var(--muted);
background: rgba(255, 255, 255, 0.05);
border: 1px solid rgba(255, 255, 255, 0.09);
border-radius: 9px;
cursor: pointer;
pointer-events: auto;
font-size: 10px;
letter-spacing: 0.08em;
text-transform: uppercase;
}
#reset-camera:hover {
color: white;
background: rgba(255, 255, 255, 0.1);
}
#legend {
position: absolute;
left: 24px;
bottom: 170px;
width: 188px;
padding: 14px 16px;
border-radius: 15px;
}
.legend-title {
margin-bottom: 9px;
color: var(--muted);
font-size: 9px;
font-weight: 800;
letter-spacing: 0.15em;
text-transform: uppercase;
}
.legend-item {
display: flex;
align-items: center;
gap: 9px;
margin: 7px 0;
color: #d8e1d7;
font-size: 11px;
}
.legend-dot {
width: 8px;
height: 8px;
flex: 0 0 auto;
border-radius: 50%;
box-shadow: 0 0 10px currentColor;
}
#timeline-panel {
position: absolute;
left: 50%;
bottom: 18px;
width: min(780px, calc(100vw - 36px));
padding: 14px 17px 13px;
border-radius: 18px;
transform: translateX(-50%);
pointer-events: auto;
}
.timeline-top {
display: flex;
align-items: center;
gap: 13px;
}
.control-button {
min-width: 82px;
padding: 8px 11px;
color: #12211b;
background: var(--accent);
border: 0;
border-radius: 9px;
cursor: pointer;
font-size: 11px;
font-weight: 800;
}
.control-button.secondary {
color: var(--muted);
background: rgba(255, 255, 255, 0.06);
border: 1px solid rgba(255, 255, 255, 0.1);
}
.control-button:hover {
filter: brightness(1.08);
}
.stage-readout {
min-width: 0;
flex: 1;
}
#stage-number {
display: block;
margin-bottom: 2px;
color: var(--accent);
font-size: 9px;
font-weight: 800;
letter-spacing: 0.14em;
text-transform: uppercase;
}
#stage-name {
display: block;
overflow: hidden;
font-size: 13px;
font-weight: 750;
text-overflow: ellipsis;
white-space: nowrap;
}
#clock {
color: var(--muted);
font-size: 11px;
font-variant-numeric: tabular-nums;
white-space: nowrap;
}
#speed {
padding: 7px 8px;
color: var(--text);
background: #172a25;
border: 1px solid rgba(255, 255, 255, 0.12);
border-radius: 8px;
outline: none;
cursor: pointer;
font-size: 10px;
}
#scrub {
width: 100%;
height: 5px;
margin: 14px 0 12px;
padding: 0;
appearance: none;
-webkit-appearance: none;
border: 0;
border-radius: 99px;
cursor: pointer;
}
#scrub::-webkit-slider-thumb {
width: 14px;
height: 14px;
appearance: none;
-webkit-appearance: none;
background: #fff;
border: 3px solid var(--accent);
border-radius: 50%;
box-shadow: 0 2px 8px rgba(0, 0, 0, 0.4);
}
#scrub::-moz-range-thumb {
width: 10px;
height: 10px;
background: #fff;
border: 3px solid var(--accent);
border-radius: 50%;
box-shadow: 0 2px 8px rgba(0, 0, 0, 0.4);
}
.stage-marks {
display: grid;
grid-template-columns: repeat(5, 1fr);
gap: 5px;
}
.stage-jump {
padding: 4px 2px;
overflow: hidden;
color: #83978b;
background: transparent;
border: 0;
cursor: pointer;
font-size: 9px;
text-align: left;
text-overflow: ellipsis;
white-space: nowrap;
}
.stage-jump:hover,
.stage-jump.active {
color: var(--accent);
}
#hint {
position: absolute;
right: 24px;
bottom: 170px;
color: rgba(230, 240, 231, 0.66);
font-size: 10px;
letter-spacing: 0.04em;
text-shadow: 0 1px 8px #000;
}
#loading {
position: fixed;
inset: 0;
z-index: 20;
display: grid;
place-items: center;
color: #dce9dd;
background: #10211e;
transition: opacity 0.45s ease;
}
#loading.hidden {
opacity: 0;
pointer-events: none;
}
.loader {
display: flex;
align-items: center;
gap: 12px;
font-size: 12px;
letter-spacing: 0.08em;
}
.loader-dot {
width: 10px;
height: 10px;
background: var(--accent);
border-radius: 50%;
animation: pulse 1s infinite alternate;
}
@keyframes pulse {
to {
transform: scale(1.8);
opacity: 0.35;
}
}
@media (max-width: 760px) {
.intro {
top: 12px;
left: 12px;
width: calc(100vw - 24px);
padding: 15px 17px;
}
.intro p {
display: none;
}
.stats {
top: 108px;
right: 12px;
width: 160px;
padding: 10px 13px;
}
.stat {
padding: 4px 0;
}
.stat strong {
font-size: 15px;
}
#legend,
#hint {
display: none;
}
#timeline-panel {
bottom: 10px;
width: calc(100vw - 20px);
padding: 11px;
}
.timeline-top {
gap: 8px;
}
.control-button {
min-width: 67px;
padding: 8px;
font-size: 10px;
}
#clock {
display: none;
}
.stage-jump {
font-size: 8px;
}
}
</style>
</head>
<body>
<div id="scene"></div>
<div id="hud">
<section class="intro glass">
<div class="eyebrow">Living landscape / 60 second study</div>
<h1>Ecological<br>succession</h1>
<p>
A fixed patch of terrain fills with successive plant communities.
Earlier survivors remain in place as newer cohorts establish above and
around them, gradually reshaping the light and shade.
</p>
</section>
<section class="stats glass">
<div class="stat">
<span>Established</span>
<strong id="established-count">0</strong>
</div>
<div class="stat">
<span>Pioneer survivors</span>
<strong id="survivor-count">0</strong>
</div>
<div class="stat">
<span>Estimated canopy</span>
<strong id="canopy-count">0%</strong>
</div>
<button id="reset-camera" type="button">Reset camera</button>
</section>
<section id="legend" class="glass">
<div class="legend-title">Successional cohorts</div>
<div class="legend-item">
<i class="legend-dot" style="color:#83ad50;background:#83ad50"></i>
Pioneer grasses
</div>
<div class="legend-item">
<i class="legend-dot" style="color:#d2c85a;background:#d2c85a"></i>
Sun-loving herbs
</div>
<div class="legend-item">
<i class="legend-dot" style="color:#528557;background:#528557"></i>
Shrubs
</div>
<div class="legend-item">
<i class="legend-dot" style="color:#326f50;background:#326f50"></i>
Young trees
</div>
<div class="legend-item">
<i class="legend-dot" style="color:#67a76d;background:#67a76d"></i>
Shade understory
</div>
</section>
<div id="hint">Drag to orbit · Scroll to zoom · Right-drag not required</div>
<section id="timeline-panel" class="glass">
<div class="timeline-top">
<button id="play" class="control-button" type="button">❚❚ Pause</button>
<div class="stage-readout">
<span id="stage-number">Stage 01</span>
<span id="stage-name">Colonization</span>
</div>
<span id="clock">00s / 60s</span>
<select id="speed" aria-label="Playback speed">
<option value="0.5">0.5×</option>
<option value="1" selected>1×</option>
<option value="1.5">1.5×</option>
<option value="2">2×</option>
</select>
<button id="restart" class="control-button secondary" type="button">
Restart
</button>
</div>
<input
id="scrub"
type="range"
min="0"
max="60"
step="0.01"
value="0"
aria-label="Succession timeline"
>
<div class="stage-marks">
<button class="stage-jump active" data-time="0" type="button">
01 · Colonization
</button>
<button class="stage-jump" data-time="8" type="button">
02 · Early growth
</button>
<button class="stage-jump" data-time="20" type="button">
03 · Shrub recovery
</button>
<button class="stage-jump" data-time="33" type="button">
04 · Canopy
</button>
<button class="stage-jump" data-time="49" type="button">
05 · Mature mix
</button>
</div>
</section>
</div>
<div id="loading">
<div class="loader">
<span class="loader-dot"></span>
Growing the first community…
</div>
</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// ---------------------------------------------------------------------
// Renderer and scene
// ---------------------------------------------------------------------
const sceneHost = document.querySelector("#scene");
const loading = document.querySelector("#loading");
const renderer = new THREE.WebGLRenderer({
antialias: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.08;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
sceneHost.appendChild(renderer.domElement);
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x10211e);
scene.fog = new THREE.Fog(0x10211e, 30, 58);
const camera = new THREE.PerspectiveCamera(
39,
window.innerWidth / window.innerHeight,
0.1,
100
);
// ---------------------------------------------------------------------
// Direct camera controls: no OrbitControls or addons are used.
// ---------------------------------------------------------------------
const cameraTarget = new THREE.Vector3(0, 0.65, 0);
const cameraState = {
azimuth: 0.64,
polar: 1.02,
radius: 27
};
const defaultCamera = { ...cameraState };
let dragging = false;
let previousPointerX = 0;
let previousPointerY = 0;
function updateCamera() {
const { azimuth, polar, radius } = cameraState;
camera.position.set(
cameraTarget.x + radius * Math.sin(polar) * Math.sin(azimuth),
cameraTarget.y + radius * Math.cos(polar),
cameraTarget.z + radius * Math.sin(polar) * Math.cos(azimuth)
);
camera.lookAt(cameraTarget);
}
renderer.domElement.addEventListener("pointerdown", (event) => {
dragging = true;
previousPointerX = event.clientX;
previousPointerY = event.clientY;
renderer.domElement.setPointerCapture(event.pointerId);
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (!dragging) return;
const deltaX = event.clientX - previousPointerX;
const deltaY = event.clientY - previousPointerY;
previousPointerX = event.clientX;
previousPointerY = event.clientY;
cameraState.azimuth -= deltaX * 0.006;
cameraState.polar = THREE.MathUtils.clamp(
cameraState.polar - deltaY * 0.005,
0.42,
1.43
);
});
function endDragging(event) {
dragging = false;
if (
event &&
renderer.domElement.hasPointerCapture?.(event.pointerId)
) {
renderer.domElement.releasePointerCapture(event.pointerId);
}
}
renderer.domElement.addEventListener("pointerup", endDragging);
renderer.domElement.addEventListener("pointercancel", endDragging);
renderer.domElement.addEventListener(
"wheel",
(event) => {
event.preventDefault();
cameraState.radius = THREE.MathUtils.clamp(
cameraState.radius * Math.exp(event.deltaY * 0.001),
15,
43
);
},
{ passive: false }
);
renderer.domElement.addEventListener("dblclick", () => {
Object.assign(cameraState, defaultCamera);
});
window.addEventListener("keydown", (event) => {
if (event.target instanceof HTMLInputElement) return;
if (event.key === "ArrowLeft") {
cameraState.azimuth -= 0.12;
} else if (event.key === "ArrowRight") {
cameraState.azimuth += 0.12;
} else if (event.key === "ArrowUp") {
cameraState.polar = Math.max(0.42, cameraState.polar - 0.08);
} else if (event.key === "ArrowDown") {
cameraState.polar = Math.min(1.43, cameraState.polar + 0.08);
}
});
document.querySelector("#reset-camera").addEventListener("click", () => {
Object.assign(cameraState, defaultCamera);
});
// ---------------------------------------------------------------------
// Lighting. Plant growth changes the real shadow-map coverage.
// ---------------------------------------------------------------------
const hemisphere = new THREE.HemisphereLight(
0xc7eadf,
0x4f3425,
1.55
);
scene.add(hemisphere);
const sun = new THREE.DirectionalLight(0xffe3ad, 3.25);
sun.position.set(-9, 15.5, 8);
sun.castShadow = true;
sun.shadow.mapSize.set(1024, 1024);
sun.shadow.camera.left = -15;
sun.shadow.camera.right = 15;
sun.shadow.camera.top = 15;
sun.shadow.camera.bottom = -15;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 48;
sun.shadow.bias = -0.00035;
sun.shadow.normalBias = 0.025;
scene.add(sun);
scene.add(sun.target);
const fill = new THREE.DirectionalLight(0x86b9c7, 0.45);
fill.position.set(8, 7, -10);
scene.add(fill);
// ---------------------------------------------------------------------
// Deterministic randomness
// ---------------------------------------------------------------------
let randomSeed = 718392;
function random() {
randomSeed =
(Math.imul(randomSeed, 1664525) + 1013904223) >>> 0;
return randomSeed / 4294967296;
}
function randomRange(min, max) {
return min + (max - min) * random();
}
function choose(array) {
return array[Math.floor(random() * array.length)];
}
// ---------------------------------------------------------------------
// Fixed low-poly terrain patch
// ---------------------------------------------------------------------
const PATCH_WIDTH = 20;
const PATCH_DEPTH = 18;
function terrainHeight(x, z) {
return (
0.34 +
Math.sin(x * 0.42 + z * 0.18) * 0.17 +
Math.cos(z * 0.55 - x * 0.13) * 0.1 +
Math.sin((x + z) * 1.08) * 0.045
);
}
const terrainGeometry = new THREE.PlaneGeometry(
PATCH_WIDTH,
PATCH_DEPTH,
20,
18
);
terrainGeometry.rotateX(-Math.PI / 2);
const terrainPositions = terrainGeometry.attributes.position;
const terrainColors = [];
for (let i = 0; i < terrainPositions.count; i++) {
const x = terrainPositions.getX(i);
const z = terrainPositions.getZ(i);
const y = terrainHeight(x, z);
terrainPositions.setY(i, y);
const moisture =
0.5 +
0.5 * Math.sin(x * 0.32 - z * 0.27 + 0.8);
const color = new THREE.Color().setHSL(
0.22 + Math.sin(x * 0.6 + z * 0.4) * 0.018,
0.25 + moisture * 0.11,
0.24 + moisture * 0.085 + Math.sin(x * 2.7 + z * 1.9) * 0.012
);
terrainColors.push(color.r, color.g, color.b);
}
terrainGeometry.setAttribute(
"color",
new THREE.Float32BufferAttribute(terrainColors, 3)
);
terrainGeometry.computeVertexNormals();
const terrainMaterial = new THREE.MeshStandardMaterial({
vertexColors: true,
roughness: 1,
metalness: 0,
flatShading: true
});
const terrain = new THREE.Mesh(terrainGeometry, terrainMaterial);
terrain.receiveShadow = true;
scene.add(terrain);
const soilLayers = [
{
size: [20.05, 0.55, 18.05],
y: -0.3,
color: 0x45372a
},
{
size: [19.8, 0.48, 17.8],
y: -0.78,
color: 0x302c25
}
];
for (const layer of soilLayers) {
const geometry = new THREE.BoxGeometry(...layer.size);
const material = new THREE.MeshStandardMaterial({
color: layer.color,
roughness: 1,
flatShading: true
});
const mesh = new THREE.Mesh(geometry, material);
mesh.position.y = layer.y;
mesh.receiveShadow = true;
mesh.castShadow = true;
scene.add(mesh);
const edge = new THREE.LineSegments(
new THREE.EdgesGeometry(geometry),
new THREE.LineBasicMaterial({
color: 0x82725b,
transparent: true,
opacity: 0.2
})
);
edge.position.copy(mesh.position);
scene.add(edge);
}
// Low-poly moss/lichen patches and stones establish an initial substrate.
const mossGeometry = new THREE.CircleGeometry(1, 9);
const mossMaterial = new THREE.MeshStandardMaterial({
color: 0x5d7041,
roughness: 1,
transparent: true,
opacity: 0.58,
depthWrite: false,
flatShading: true
});
for (let i = 0; i < 18; i++) {
const x = randomRange(-9.1, 9.1);
const z = randomRange(-8.1, 8.1);
const moss = new THREE.Mesh(mossGeometry, mossMaterial);
moss.position.set(x, terrainHeight(x, z) + 0.012, z);
moss.rotation.x = -Math.PI / 2;
moss.rotation.z = randomRange(0, Math.PI);
moss.scale.set(
randomRange(0.22, 0.65),
randomRange(0.16, 0.48),
1
);
scene.add(moss);
}
const rockGeometry = new THREE.DodecahedronGeometry(0.22, 0);
const rockMaterial = new THREE.MeshStandardMaterial({
color: 0x77766b,
roughness: 0.95,
flatShading: true
});
for (let i = 0; i < 17; i++) {
const x = randomRange(-9, 9);
const z = randomRange(-8, 8);
const rock = new THREE.Mesh(rockGeometry, rockMaterial);
rock.position.set(x, terrainHeight(x, z) + 0.08, z);
rock.scale.set(
randomRange(0.5, 1.5),
randomRange(0.35, 1.05),
randomRange(0.5, 1.4)
);
rock.rotation.set(
randomRange(0, Math.PI),
randomRange(0, Math.PI),
randomRange(0, Math.PI)
);
rock.castShadow = true;
rock.receiveShadow = true;
scene.add(rock);
}
// ---------------------------------------------------------------------
// Low-poly plant geometry and materials
// ---------------------------------------------------------------------
const grassMaterial = new THREE.MeshStandardMaterial({
color: 0x83ad50,
roughness: 0.95,
flatShading: true
});
const grassLightMaterial = new THREE.MeshStandardMaterial({
color: 0xa5bd58,
roughness: 0.95,
flatShading: true
});
const stemMaterial = new THREE.MeshStandardMaterial({
color: 0x46633c,
roughness: 1,
flatShading: true
});
const leafMaterial = new THREE.MeshStandardMaterial({
color: 0x638f49,
roughness: 0.9,
flatShading: true
});
const leafLightMaterial = new THREE.MeshStandardMaterial({
color: 0x7da457,
roughness: 0.9,
flatShading: true
});
const fernMaterial = new THREE.MeshStandardMaterial({
color: 0x4f895b,
roughness: 0.95,
flatShading: true
});
const flowerMaterial = new THREE.MeshStandardMaterial({
color: 0xe0d274,
roughness: 0.8,
flatShading: true
});
const barkMaterial = new THREE.MeshStandardMaterial({
color: 0x614533,
roughness: 1,
flatShading: true
});
const canopyMaterials = [
new THREE.MeshStandardMaterial({
color: 0x376e4c,
roughness: 0.95,
flatShading: true
}),
new THREE.MeshStandardMaterial({
color: 0x447f52,
roughness: 0.95,
flatShading: true
}),
new THREE.MeshStandardMaterial({
color: 0x588b55,
roughness: 0.95,
flatShading: true
})
];
const bladeGeometry = new THREE.ConeGeometry(0.038, 1, 3);
const herbLeafGeometry = new THREE.ConeGeometry(0.12, 0.31, 3);
const flowerGeometry = new THREE.IcosahedronGeometry(0.075, 0);
const herbStemGeometry = new THREE.CylinderGeometry(
0.018,
0.026,
0.55,
5
);
const shrubTrunkGeometry = new THREE.CylinderGeometry(
0.05,
0.075,
0.7,
6
);
const shrubCrownGeometry = new THREE.IcosahedronGeometry(0.43, 0);
const treeTrunkGeometry = new THREE.CylinderGeometry(
0.11,
0.18,
2.1,
6
);
const branchGeometry = new THREE.CylinderGeometry(
0.035,
0.06,
0.85,
5
);
const treeCrownGeometry = new THREE.IcosahedronGeometry(0.86, 0);
const fernLeafGeometry = new THREE.ConeGeometry(0.09, 0.72, 3);
function enableShadows(object) {
object.traverse((child) => {
if (child.isMesh) {
child.castShadow = true;
child.receiveShadow = true;
}
});
}
function makeGrass() {
const group = new THREE.Group();
for (let i = 0; i < 6; i++) {
const blade = new THREE.Mesh(
bladeGeometry,
i % 2 === 0 ? grassMaterial : grassLightMaterial
);
const height = randomRange(0.36, 0.67);
blade.position.set(
randomRange(-0.13, 0.13),
height * 0.5,
randomRange(-0.13, 0.13)
);
blade.scale.y = height;
blade.rotation.z = randomRange(-0.42, 0.42);
blade.rotation.x = randomRange(-0.28, 0.28);
blade.rotation.y = randomRange(0, Math.PI * 2);
group.add(blade);
}
return group;
}
function makeHerb() {
const group = new THREE.Group();
const stem = new THREE.Mesh(herbStemGeometry, stemMaterial);
stem.position.y = 0.275;
group.add(stem);
for (let i = 0; i < 3; i++) {
const leaf = new THREE.Mesh(
herbLeafGeometry,
i % 2 ? leafMaterial : leafLightMaterial
);
const side = i % 2 === 0 ? -1 : 1;
leaf.position.set(side * 0.1, 0.18 + i * 0.07, 0);
leaf.rotation.z = side * 1.05;
leaf.rotation.y = randomRange(0, Math.PI * 2);
group.add(leaf);
}
const flower = new THREE.Mesh(flowerGeometry, flowerMaterial);
flower.position.y = 0.59;
flower.scale.y = 0.7;
group.add(flower);
return group;
}
function makeShrub() {
const group = new THREE.Group();
const trunk = new THREE.Mesh(shrubTrunkGeometry, barkMaterial);
trunk.position.y = 0.35;
group.add(trunk);
const crownData = [
[-0.2, 0.72, 0.05, 0.9],
[0.2, 0.78, -0.08, 0.82],
[0.02, 1.0, 0.03, 0.86]
];
for (const [x, y, z, scale] of crownData) {
const crown = new THREE.Mesh(
shrubCrownGeometry,
choose(canopyMaterials)
);
crown.position.set(x, y, z);
crown.scale.set(scale, scale * 0.85, scale);
crown.rotation.set(
randomRange(0, 1),
randomRange(0, Math.PI),
randomRange(0, 1)
);
group.add(crown);
}
return group;
}
function makeTree() {
const group = new THREE.Group();
const trunk = new THREE.Mesh(treeTrunkGeometry, barkMaterial);
trunk.position.y = 1.05;
group.add(trunk);
for (const side of [-1, 1]) {
const branch = new THREE.Mesh(branchGeometry, barkMaterial);
branch.position.set(side * 0.2, 1.55, 0);
branch.rotation.z = side * 0.62;
group.add(branch);
}
const crownData = [
[0, 2.25, 0, 0.96],
[-0.43, 2.02, 0.16, 0.72],
[0.42, 2.12, -0.2, 0.77],
[0.03, 2.77, -0.03, 0.68]
];
for (const [x, y, z, scale] of crownData) {
const crown = new THREE.Mesh(
treeCrownGeometry,
choose(canopyMaterials)
);
crown.position.set(x, y, z);
crown.scale.set(scale, scale * 0.9, scale);
crown.rotation.set(
randomRange(-0.3, 0.3),
randomRange(0, Math.PI),
randomRange(-0.3, 0.3)
);
group.add(crown);
}
return group;
}
function makeFern() {
const group = new THREE.Group();
for (let i = 0; i < 5; i++) {
const leaf = new THREE.Mesh(fernLeafGeometry, fernMaterial);
const angle = (i / 5) * Math.PI * 2 + randomRange(-0.2, 0.2);
leaf.position.set(
Math.sin(angle) * 0.12,
randomRange(0.22, 0.32),
Math.cos(angle) * 0.12
);
leaf.scale.y = randomRange(0.7, 1.05);
leaf.rotation.z = Math.sin(angle) * 0.75;
leaf.rotation.x = Math.cos(angle) * 0.75;
leaf.rotation.y = angle;
group.add(leaf);
}
return group;
}
function makePlant(type) {
if (type === "grass") return makeGrass();
if (type === "herb") return makeHerb();
if (type === "shrub") return makeShrub();
if (type === "tree") return makeTree();
return makeFern();
}
// ---------------------------------------------------------------------
// Spatial planting. Plants are added rather than replacing one another.
// ---------------------------------------------------------------------
const occupiedPositions = [];
const treePositions = [];
function isInsidePatch(x, z) {
return (
x > -9.1 &&
x < 9.1 &&
z > -8.1 &&
z < 8.1
);
}
function reserveSpot(minimumDistance, clustered = false) {
const minimumDistanceSquared = minimumDistance * minimumDistance;
let best = null;
let bestDistance = -1;
const clusters = [
[-3.8, -1.5],
[3.1, -0.7],
[0.5, 4.0]
];
for (let attempt = 0; attempt < 180; attempt++) {
let x;
let z;
if (clustered) {
const center = choose(clusters);
x = center[0] + randomRange(-2.9, 2.9);
z = center[1] + randomRange(-2.7, 2.7);
} else {
x = randomRange(-9, 9);
z = randomRange(-8, 8);
}
if (!isInsidePatch(x, z)) continue;
let nearest = Infinity;
for (const occupied of occupiedPositions) {
const dx = x - occupied.x;
const dz = z - occupied.z;
nearest = Math.min(nearest, dx * dx + dz * dz);
}
if (nearest > minimumDistanceSquared) {
occupiedPositions.push({ x, z });
return { x, z };
}
if (nearest > bestDistance) {
bestDistance = nearest;
best = { x, z };
}
}
occupiedPositions.push(best);
return best;
}
function reserveNearTree() {
for (let attempt = 0; attempt < 100; attempt++) {
const tree = choose(treePositions);
const x = tree.x + randomRange(-1.5, 1.5);
const z = tree.z + randomRange(-1.5, 1.5);
if (!isInsidePatch(x, z)) continue;
const available = occupiedPositions.every((occupied) => {
const dx = x - occupied.x;
const dz = z - occupied.z;
return dx * dx + dz * dz > 0.3 * 0.3;
});
if (available) {
occupiedPositions.push({ x, z });
return { x, z };
}
}
return reserveSpot(0.45);
}
const species = {
tree: {
count: 12,
birth: [32, 53],
duration: [10, 15],
scale: [0.82, 1.2],
spacing: 1.45
},
shrub: {
count: 11,
birth: [20, 35],
duration: [6, 9],
scale: [0.75, 1.15],
spacing: 0.8
},
grass: {
count: 25,
birth: [3, 13],
duration: [3, 5],
scale: [0.66, 1.05],
spacing: 0.52
},
herb: {
count: 17,
birth: [10, 24],
duration: [4, 6],
scale: [0.72, 1.1],
spacing: 0.58
},
fern: {
count: 11,
birth: [43, 58],
duration: [6, 9],
scale: [0.72, 1.08],
spacing: 0.45
}
};
const plants = [];
for (const [type, settings] of Object.entries(species)) {
for (let i = 0; i < settings.count; i++) {
let spot;
if (type === "tree") {
spot = reserveSpot(settings.spacing, true);
treePositions.push(spot);
} else if (type === "fern") {
spot = reserveNearTree();
} else {
spot = reserveSpot(settings.spacing);
}
const group = makePlant(type);
group.position.set(
spot.x,
terrainHeight(spot.x, spot.z) + 0.015,
spot.z
);
group.visible = false;
enableShadows(group);
scene.add(group);
plants.push({
group,
type,
birth: randomRange(settings.birth[0], settings.birth[1]),
duration: randomRange(settings.duration[0], settings.duration[1]),
finalScale: randomRange(settings.scale[0], settings.scale[1]),
phase: randomRange(0, Math.PI * 2),
yaw: randomRange(0, Math.PI * 2)
});
}
}
// ---------------------------------------------------------------------
// Atmospheric pollen
// ---------------------------------------------------------------------
const pollenPositions = [];
for (let i = 0; i < 90; i++) {
pollenPositions.push(
randomRange(-10, 10),
randomRange(0.8, 6.5),
randomRange(-9, 9)
);
}
const pollenGeometry = new THREE.BufferGeometry();
pollenGeometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(pollenPositions, 3)
);
const pollen = new THREE.Points(
pollenGeometry,
new THREE.PointsMaterial({
color: 0xe9e6bb,
size: 0.055,
transparent: true,
opacity: 0.48,
depthWrite: false,
sizeAttenuation: true
})
);
scene.add(pollen);
// ---------------------------------------------------------------------
// Timeline and interface
// ---------------------------------------------------------------------
const DURATION = 60;
const stages = [
{
start: 0,
name: "Colonization",
description: "Lichens, moss, and pioneer grasses",
color: "#b9df87"
},
{
start: 8,
name: "Early succession",
description: "Sun-loving herbs join the surviving grasses",
color: "#d8ce68"
},
{
start: 20,
name: "Shrub recovery",
description: "Dense shrubs expand beneath the pioneer layer",
color: "#86b66b"
},
{
start: 33,
name: "Canopy establishment",
description: "Young trees grow above the older community",
color: "#62a478"
},
{
start: 49,
name: "Mature mixed community",
description: "Shade plants establish beneath a closing canopy",
color: "#7dc69b"
}
];
const playButton = document.querySelector("#play");
const restartButton = document.querySelector("#restart");
const speedSelect = document.querySelector("#speed");
const scrubber = document.querySelector("#scrub");
const stageNumber = document.querySelector("#stage-number");
const stageName = document.querySelector("#stage-name");
const clock = document.querySelector("#clock");
const establishedCount = document.querySelector("#established-count");
const survivorCount = document.querySelector("#survivor-count");
const canopyCount = document.querySelector("#canopy-count");
const stageButtons = [
...document.querySelectorAll(".stage-jump")
];
let simulationTime = 0;
let playing = true;
let playbackSpeed = 1;
let scrubbing = false;
let resumeAfterScrub = false;
let currentStageIndex = -1;
function syncPlayButton() {
if (playing) {
playButton.textContent = "❚❚ Pause";
} else if (simulationTime >= DURATION) {
playButton.textContent = "↻ Replay";
} else {
playButton.textContent = "▶ Play";
}
}
playButton.addEventListener("click", () => {
if (simulationTime >= DURATION) {
simulationTime = 0;
playing = true;
} else {
playing = !playing;
}
syncPlayButton();
});
restartButton.addEventListener("click", () => {
simulationTime = 0;
playing = true;
syncPlayButton();
});
speedSelect.addEventListener("change", () => {
playbackSpeed = Number(speedSelect.value);
});
scrubber.addEventListener("pointerdown", () => {
scrubbing = true;
resumeAfterScrub = playing;
playing = false;
syncPlayButton();
});
scrubber.addEventListener("input", () => {
simulationTime = Number(scrubber.value);
});
window.addEventListener("pointerup", () => {
if (!scrubbing) return;
scrubbing = false;
playing = resumeAfterScrub && simulationTime < DURATION;
syncPlayButton();
});
for (const button of stageButtons) {
button.addEventListener("click", () => {
simulationTime = Number(button.dataset.time) + 0.05;
});
}
function updatePlants(time) {
const establishedByType = {
grass: 0,
herb: 0,
shrub: 0,
tree: 0,
fern: 0
};
let totalEstablished = 0;
let treeGrowth = 0;
let shrubGrowth = 0;
for (const plant of plants) {
const age = time - plant.birth;
if (age < 0) {
plant.group.visible = false;
continue;
}
plant.group.visible = true;
const rawGrowth = THREE.MathUtils.clamp(
age / plant.duration,
0,
1
);
const easedGrowth = 1 - Math.pow(1 - rawGrowth, 3);
const scale =
plant.finalScale * (0.001 + easedGrowth);
plant.group.scale.set(
scale,
scale * (0.72 + easedGrowth * 0.28),
scale
);
const swayAmount =
plant.type === "tree"
? 0.006
: plant.type === "shrub"
? 0.012
: plant.type === "fern"
? 0.018
: 0.035;
plant.group.rotation.x =
Math.sin(time * 1.1 + plant.phase) * swayAmount;
plant.group.rotation.y =
plant.yaw +
Math.sin(time * 0.7 + plant.phase) * swayAmount * 0.4;
plant.group.rotation.z =
Math.cos(time * 0.9 + plant.phase) * swayAmount * 0.8;
if (rawGrowth >= 0.9) {
establishedByType[plant.type]++;
totalEstablished++;
}
if (plant.type === "tree") {
treeGrowth += easedGrowth;
} else if (plant.type === "shrub") {
shrubGrowth += easedGrowth;
}
}
const treeCount = species.tree.count;
const shrubCount = species.shrub.count;
const canopyEstimate = Math.min(
100,
(treeGrowth / treeCount) * 92 +
(shrubGrowth / shrubCount) * 8
);
establishedCount.textContent = totalEstablished;
survivorCount.textContent =
establishedByType.grass + establishedByType.herb;
canopyCount.textContent = `${Math.round(canopyEstimate)}%`;
}
function updateInterface(time) {
let stageIndex = 0;
for (let i = 0; i < stages.length; i++) {
if (time >= stages[i].start) stageIndex = i;
}
if (stageIndex !== currentStageIndex) {
currentStageIndex = stageIndex;
const stage = stages[stageIndex];
stageNumber.textContent = `Stage ${String(stageIndex + 1).padStart(
2,
"0"
)}`;
stageName.textContent = stage.name;
document.documentElement.style.setProperty(
"--accent",
stage.color
);
stageButtons.forEach((button, index) => {
button.classList.toggle("active", index === stageIndex);
});
}
scrubber.value = String(time);
const progress = (time / DURATION) * 100;
scrubber.style.background = `linear-gradient(
90deg,
var(--accent) 0%,
var(--accent) ${progress}%,
rgba(255,255,255,0.13) ${progress}%,
rgba(255,255,255,0.13) 100%
)`;
clock.textContent = `${Math.floor(time)
.toString()
.padStart(2, "0")}s / 60s`;
}
// ---------------------------------------------------------------------
// Animation loop
// ---------------------------------------------------------------------
let previousFrameTime = performance.now();
function animate(now) {
requestAnimationFrame(animate);
const delta = Math.min((now - previousFrameTime) / 1000, 0.05);
previousFrameTime = now;
if (playing && !scrubbing) {
simulationTime = Math.min(
DURATION,
simulationTime + delta * playbackSpeed
);
if (simulationTime >= DURATION) {
playing = false;
syncPlayButton();
}
}
updateCamera();
updatePlants(simulationTime);
updateInterface(simulationTime);
// A small solar movement causes the canopy shadows to shift as well.
const solarAngle = simulationTime * 0.018;
sun.position.set(
-9 + Math.sin(solarAngle) * 2.2,
15.5,
7.5 + Math.cos(solarAngle) * 1.8
);
sun.target.position.set(0, 0, 0);
pollen.rotation.y = simulationTime * 0.012;
pollen.position.y = Math.sin(simulationTime * 0.28) * 0.09;
renderer.render(scene, camera);
}
window.addEventListener("resize", () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
});
updateCamera();
updatePlants(0);
updateInterface(0);
syncPlayButton();
requestAnimationFrame(animate);
requestAnimationFrame(() => {
loading.classList.add("hidden");
setTimeout(() => loading.remove(), 500);
});
</script>
</body>
</html>
```