Prompt specification
Build or customize the Three.js 3D scene "Stream-edge transition from grassland to gallery forest" 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: Stream-edge transition from grassland to gallery forest
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/stream-edge-transition-from-grassland-to-gallery-fores-x3054
Scene Description Prompt:
Create a standalone voxel scene in one HTML file using Three.js from cdnjs. Create a compact Cerrado vignette with stream, denser riparian vegetation, and open grassland. Show the gradual spatial transition instead of a sharp decorative color boundary.
Working Three.js Starter (stream-edge-transition-from-grassland-to-gallery-fores-x3054):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Cerrado Threshold — Voxel Biome</title>
<style>
:root {
color-scheme: light;
--ink: #243126;
--muted: #687165;
--panel: rgba(247, 245, 231, 0.84);
--line: rgba(45, 60, 43, 0.16);
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #dce4d7;
color: var(--ink);
font-family: Inter, ui-sans-serif, system-ui, -apple-system,
BlinkMacSystemFont, "Segoe UI", sans-serif;
}
#scene {
position: fixed;
inset: 0;
display: block;
width: 100%;
height: 100%;
outline: none;
cursor: grab;
touch-action: none;
}
#scene:active {
cursor: grabbing;
}
.ui {
position: fixed;
z-index: 2;
pointer-events: none;
}
.intro {
top: 28px;
left: 30px;
width: min(390px, calc(100vw - 60px));
}
.eyebrow {
display: flex;
align-items: center;
gap: 9px;
margin-bottom: 11px;
color: #53604f;
font-size: 10px;
font-weight: 800;
letter-spacing: 0.18em;
text-transform: uppercase;
}
.eyebrow::before {
width: 27px;
height: 1px;
content: "";
background: #697661;
}
h1 {
margin: 0;
font-family: Georgia, "Times New Roman", serif;
font-size: clamp(34px, 5vw, 59px);
font-weight: 400;
line-height: 0.96;
letter-spacing: -0.045em;
text-wrap: balance;
}
.subtitle {
max-width: 350px;
margin: 15px 0 0;
color: var(--muted);
font-size: 13px;
line-height: 1.55;
}
.legend {
position: fixed;
left: 30px;
bottom: 27px;
width: min(330px, calc(100vw - 60px));
padding: 13px 15px 12px;
border: 1px solid var(--line);
border-radius: 10px;
background: var(--panel);
box-shadow: 0 12px 35px rgba(51, 61, 44, 0.08);
backdrop-filter: blur(12px);
}
.legend-head {
display: flex;
justify-content: space-between;
margin-bottom: 8px;
color: #596457;
font-size: 9px;
font-weight: 800;
letter-spacing: 0.13em;
text-transform: uppercase;
}
.gradient {
height: 7px;
overflow: hidden;
border-radius: 99px;
background:
linear-gradient(
90deg,
#365f38 0%,
#55743c 25%,
#818545 51%,
#a69b55 76%,
#baa15d 100%
);
}
.legend-labels {
display: flex;
justify-content: space-between;
margin-top: 7px;
color: #5d6659;
font-size: 10px;
}
.controls {
right: 26px;
bottom: 27px;
display: flex;
align-items: center;
gap: 11px;
}
.hint {
padding: 9px 12px;
border: 1px solid var(--line);
border-radius: 99px;
background: var(--panel);
color: #596254;
font-size: 10px;
letter-spacing: 0.04em;
backdrop-filter: blur(10px);
}
button {
pointer-events: auto;
padding: 10px 14px;
border: 1px solid rgba(39, 55, 39, 0.2);
border-radius: 99px;
background: #f5f3e5;
color: #334132;
font: inherit;
font-size: 10px;
font-weight: 750;
letter-spacing: 0.08em;
text-transform: uppercase;
cursor: pointer;
box-shadow: 0 8px 24px rgba(49, 61, 43, 0.08);
transition:
transform 160ms ease,
background 160ms ease;
}
button:hover {
transform: translateY(-1px);
background: #fffdf0;
}
button:focus-visible {
outline: 2px solid #718657;
outline-offset: 3px;
}
.stamp {
top: 30px;
right: 28px;
padding: 9px 12px;
border: 1px solid var(--line);
border-radius: 99px;
background: rgba(247, 245, 231, 0.65);
color: #596453;
font-size: 9px;
font-weight: 750;
letter-spacing: 0.12em;
text-transform: uppercase;
backdrop-filter: blur(9px);
}
#loading {
position: fixed;
inset: 0;
z-index: 10;
display: grid;
place-items: center;
background: #dce4d7;
color: #52604f;
font-size: 10px;
font-weight: 800;
letter-spacing: 0.2em;
text-transform: uppercase;
transition: opacity 500ms ease;
}
#loading.hidden {
opacity: 0;
pointer-events: none;
}
@media (max-width: 700px) {
.intro {
top: 20px;
left: 20px;
}
.subtitle {
max-width: 280px;
font-size: 12px;
}
.legend {
left: 20px;
bottom: 20px;
width: 245px;
}
.controls {
right: 18px;
bottom: 93px;
}
.hint {
display: none;
}
.stamp {
display: none;
}
}
</style>
</head>
<body>
<canvas id="scene" aria-label="Interactive voxel landscape of the Brazilian Cerrado"></canvas>
<header class="ui intro">
<div class="eyebrow">Field study 01 · Central Brazil</div>
<h1>Cerrado<br>Threshold</h1>
<p class="subtitle">
A seasonal biome edge where a winding stream feeds denser riparian growth,
dissolving gradually into open, sun-bleached grassland.
</p>
</header>
<div class="ui stamp">Procedural voxel biome</div>
<aside class="ui legend" aria-label="Moisture gradient legend">
<div class="legend-head">
<span>Continuous moisture field</span>
<span>Field → Riparian</span>
</div>
<div class="gradient"></div>
<div class="legend-labels">
<span>Open grassland</span>
<span>Dense riparian</span>
</div>
</aside>
<div class="ui controls">
<span class="hint">Drag to orbit · Scroll or pinch to zoom</span>
<button id="reset" type="button">Reset view</button>
</div>
<div id="loading">Growing the cerrado…</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 canvas = document.querySelector("#scene");
const loading = document.querySelector("#loading");
const renderer = new THREE.WebGLRenderer({
canvas,
antialias: true,
alpha: false,
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;
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xdce4d7);
scene.fog = new THREE.Fog(0xdce4d7, 43, 78);
const camera = new THREE.PerspectiveCamera(
39,
window.innerWidth / window.innerHeight,
0.1,
150
);
const cameraTarget = new THREE.Vector3(0, 0.25, 0);
const cameraState = {
theta: 0.69,
phi: 1.03,
radius: 37,
targetTheta: 0.69,
targetPhi: 1.03,
targetRadius: 37
};
const hemisphere = new THREE.HemisphereLight(0xf4f8df, 0x72583c, 1.8);
scene.add(hemisphere);
const sun = new THREE.DirectionalLight(0xfff3cf, 3.1);
sun.position.set(-13, 24, 11);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -18;
sun.shadow.camera.right = 18;
sun.shadow.camera.top = 20;
sun.shadow.camera.bottom = -20;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 65;
sun.shadow.bias = -0.00035;
sun.shadow.normalBias = 0.035;
scene.add(sun);
const coolFill = new THREE.DirectionalLight(0xa9c8d0, 0.55);
coolFill.position.set(16, 9, -14);
scene.add(coolFill);
// -------------------------------------------------------------------------
// Deterministic utilities
// -------------------------------------------------------------------------
function mulberry32(seed) {
return function () {
let value = seed += 0x6D2B79F5;
value = Math.imul(value ^ (value >>> 15), value | 1);
value ^= value + Math.imul(value ^ (value >>> 7), value | 61);
return ((value ^ (value >>> 14)) >>> 0) / 4294967296;
};
}
const random = mulberry32(824713);
const clamp = (value, min, max) => Math.max(min, Math.min(max, value));
const lerp = (a, b, t) => a + (b - a) * t;
function smoothstep(edge0, edge1, value) {
const t = clamp((value - edge0) / (edge1 - edge0), 0, 1);
return t * t * (3 - 2 * t);
}
function hash2(x, z) {
let h = Math.imul(x, 374761393) ^ Math.imul(z, 668265263);
h = Math.imul(h ^ (h >>> 13), 1274126177);
return ((h ^ (h >>> 16)) >>> 0) / 4294967295;
}
function valueNoise(x, z) {
const ix = Math.floor(x);
const iz = Math.floor(z);
const fx = x - ix;
const fz = z - iz;
const ux = fx * fx * (3 - 2 * fx);
const uz = fz * fz * (3 - 2 * fz);
const a = hash2(ix, iz);
const b = hash2(ix + 1, iz);
const c = hash2(ix, iz + 1);
const d = hash2(ix + 1, iz + 1);
return lerp(
lerp(a, b, ux),
lerp(c, d, ux),
uz
);
}
function fbm(x, z) {
let sum = 0;
let amplitude = 0.5;
let frequency = 1;
for (let octave = 0; octave < 4; octave++) {
sum += valueNoise(x * frequency, z * frequency) * amplitude;
frequency *= 2.03;
amplitude *= 0.5;
}
return sum / 0.9375;
}
// -------------------------------------------------------------------------
// Biome layout and continuous ecological field
// -------------------------------------------------------------------------
const NX = 32;
const NZ = 38;
const CELL = 0.68;
const CAP_THICKNESS = 0.1;
const BASE_Y = -1.48;
const WIDTH = NX * CELL;
const DEPTH = NZ * CELL;
const X0 = -WIDTH / 2;
const Z0 = -DEPTH / 2;
const heights = new Float32Array(NX * NZ);
const moistures = new Float32Array(NX * NZ);
const streamDistances = new Float32Array(NX * NZ);
function streamCenter(z) {
return (
Math.sin((z + 7.6) * 0.18) * 2.05 +
Math.sin((z + 2.1) * 0.43 + 0.6) * 0.72
);
}
function streamWidth(z) {
return 0.73 + Math.sin(z * 0.31 + 0.7) * 0.075;
}
const terrainBlocks = [];
const surfaceBlocks = [];
const waterBlocks = [];
const dryEarth = new THREE.Color(0x6d4932);
const dryGround = new THREE.Color(0xb39b58);
const middleGround = new THREE.Color(0x818748);
const wetGround = new THREE.Color(0x456d40);
const bankEarth = new THREE.Color(0xa48d62);
const waterColor = new THREE.Color(0x3c817c);
for (let iz = 0; iz < NZ; iz++) {
for (let ix = 0; ix < NX; ix++) {
const index = iz * NX + ix;
const x = X0 + (ix + 0.5) * CELL;
const z = Z0 + (iz + 0.5) * CELL;
const distance = Math.abs(x - streamCenter(z));
const width = streamWidth(z);
const noise = fbm(ix * 0.13, iz * 0.13);
const patch = fbm(ix * 0.075 + 31, iz * 0.075 - 17);
// Moisture is continuous in both space and distance from the stream.
// Broad noise prevents a decorative stripe or hard biome boundary.
const moisture = clamp(
0.7 * Math.exp(-distance / 2.45) +
0.3 * patch * (0.82 + 0.18 * noise),
0,
1
);
const steppedLand =
0.08 +
Math.round((noise * 0.18 + patch * 0.12) / 0.08) * 0.08;
let height = steppedLand;
if (distance < width) {
height = -0.62;
} else if (distance < width + 1.35) {
const bankT = smoothstep(width, width + 1.35, distance);
height = lerp(-0.62, steppedLand, bankT);
}
heights[index] = height;
moistures[index] = moisture;
streamDistances[index] = distance;
const earthColor = dryEarth.clone();
earthColor.offsetHSL(
(random() - 0.5) * 0.018,
(random() - 0.5) * 0.04,
(random() - 0.5) * 0.08
);
addBlock(
terrainBlocks,
x,
BASE_Y + (height - BASE_Y) / 2,
z,
CELL * 0.998,
height - BASE_Y,
CELL * 0.998,
earthColor
);
let topColor = dryGround.clone();
topColor.lerp(middleGround, smoothstep(0.12, 0.56, moisture));
topColor.lerp(wetGround, smoothstep(0.5, 0.91, moisture));
const bankMix =
1 - smoothstep(width + 0.05, width + 1.7, distance);
topColor.lerp(bankEarth, bankMix * 0.43);
topColor.offsetHSL(
(random() - 0.5) * 0.022,
(random() - 0.5) * 0.045,
(random() - 0.5) * 0.065
);
addBlock(
surfaceBlocks,
x,
height + CAP_THICKNESS / 2,
z,
CELL * 0.982,
CAP_THICKNESS,
CELL * 0.982,
topColor
);
if (distance < width) {
const color = waterColor.clone();
color.offsetHSL(
(random() - 0.5) * 0.012,
(random() - 0.5) * 0.05,
(random() - 0.5) * 0.07
);
addBlock(
waterBlocks,
x,
-0.31,
z,
CELL * 1.012,
0.1,
CELL * 1.012,
color
);
}
}
}
// -------------------------------------------------------------------------
// Instanced voxel helpers
// -------------------------------------------------------------------------
const unitBox = new THREE.BoxGeometry(1, 1, 1);
const dummy = new THREE.Object3D();
function addBlock(
list,
x, y, z,
sx, sy, sz,
color,
rx = 0,
ry = 0,
rz = 0
) {
list.push({
x, y, z,
sx, sy, sz,
color,
rx, ry, rz
});
}
function makeInstancedMesh(
blocks,
material,
castShadow = false,
receiveShadow = false
) {
const mesh = new THREE.InstancedMesh(
unitBox,
material,
blocks.length
);
blocks.forEach((block, index) => {
dummy.position.set(block.x, block.y, block.z);
dummy.rotation.set(block.rx, block.ry, block.rz);
dummy.scale.set(block.sx, block.sy, block.sz);
dummy.updateMatrix();
mesh.setMatrixAt(index, dummy.matrix);
mesh.setColorAt(index, block.color);
});
mesh.instanceMatrix.needsUpdate = true;
if (mesh.instanceColor) {
mesh.instanceColor.needsUpdate = true;
}
mesh.castShadow = castShadow;
mesh.receiveShadow = receiveShadow;
return mesh;
}
function cellIndexAt(x, z) {
const ix = clamp(Math.floor((x - X0) / CELL), 0, NX - 1);
const iz = clamp(Math.floor((z - Z0) / CELL), 0, NZ - 1);
return iz * NX + ix;
}
function surfaceYAt(x, z) {
const index = cellIndexAt(x, z);
return heights[index] + CAP_THICKNESS;
}
// Pedestal and backdrop.
const base = new THREE.Mesh(
new THREE.BoxGeometry(WIDTH + 0.35, 0.45, DEPTH + 0.35),
new THREE.MeshStandardMaterial({
color: 0x503828,
roughness: 1
})
);
base.position.y = -1.70;
base.receiveShadow = true;
scene.add(base);
const backdrop = new THREE.Mesh(
new THREE.PlaneGeometry(180, 180),
new THREE.MeshStandardMaterial({
color: 0xd5dece,
roughness: 1
})
);
backdrop.rotation.x = -Math.PI / 2;
backdrop.position.y = -1.935;
backdrop.receiveShadow = true;
scene.add(backdrop);
scene.add(
makeInstancedMesh(
terrainBlocks,
new THREE.MeshStandardMaterial({
color: 0xffffff,
roughness: 1
}),
false,
true
)
);
scene.add(
makeInstancedMesh(
surfaceBlocks,
new THREE.MeshStandardMaterial({
color: 0xffffff,
roughness: 0.94
}),
false,
true
)
);
const waterMesh = makeInstancedMesh(
waterBlocks,
new THREE.MeshStandardMaterial({
color: 0xffffff,
roughness: 0.2,
metalness: 0.08,
transparent: true,
opacity: 0.91,
depthWrite: false
})
);
waterMesh.renderOrder = 2;
scene.add(waterMesh);
// -------------------------------------------------------------------------
// Trees and shrubs
// -------------------------------------------------------------------------
const trunkBlocks = [];
const leafBlocks = [];
const shrubBlocks = [];
const rockBlocks = [];
const moundBlocks = [];
const grassBlocks = [];
const riparianPalette = [
new THREE.Color(0x315d35),
new THREE.Color(0x416d3a),
new THREE.Color(0x557a42),
new THREE.Color(0x648548)
];
const savannaPalette = [
new THREE.Color(0x637936),
new THREE.Color(0x78863a),
new THREE.Color(0x909241),
new THREE.Color(0x9c9b47)
];
const trunkPalette = [
new THREE.Color(0x69462f),
new THREE.Color(0x795237),
new THREE.Color(0x866044)
];
function addTree(x, z, index, riparian) {
const baseY = heights[index] + CAP_THICKNESS;
const trunkHeight = riparian
? 3.15 + random() * 1.15
: 2.45 + random() * 1.0;
const segmentCount = riparian ? 6 : 5;
const phase = random() * Math.PI * 2;
for (let segment = 0; segment < segmentCount; segment++) {
const t = segment / (segmentCount - 1);
const drift = Math.sin(phase + t * 4.6) * (riparian ? 0.13 : 0.23);
const width = (riparian ? 0.21 : 0.29) * (1 - t * 0.35);
const segmentHeight = trunkHeight / segmentCount * 1.24;
addBlock(
trunkBlocks,
x + drift,
baseY + t * trunkHeight * 0.83 + segmentHeight / 2,
z + Math.cos(phase + t * 3.8) * t * 0.08,
width,
segmentHeight,
width,
trunkPalette[Math.floor(random() * trunkPalette.length)],
0,
phase,
Math.sin(phase + t * 5) * 0.035
);
}
const topY = baseY + trunkHeight * 0.83;
// Exposed, angular branches are characteristic of Cerrado trees.
const branchCount = riparian ? 4 : 5;
for (let branch = 0; branch < branchCount; branch++) {
const angle = phase + branch / branchCount * Math.PI * 2;
const reach = riparian ? 0.48 : 0.7;
addBlock(
trunkBlocks,
x + Math.cos(angle) * reach * 0.45,
topY - 0.12 - random() * 0.18,
z + Math.sin(angle) * reach * 0.45,
0.13,
0.14,
reach,
trunkPalette[Math.floor(random() * trunkPalette.length)],
0.08 * Math.sin(angle),
angle,
0.08 * Math.cos(angle)
);
}
const palette = riparian ? riparianPalette : savannaPalette;
const leafCount = riparian ? 21 : 11;
const maxRadius = riparian ? 1.05 : 1.18;
for (let leaf = 0; leaf < leafCount; leaf++) {
const angle = random() * Math.PI * 2;
const radius = Math.sqrt(random()) * maxRadius;
const y = topY +
0.22 * (1 - radius / maxRadius) +
(random() - 0.35) * (riparian ? 0.52 : 0.3);
addBlock(
leafBlocks,
x + Math.cos(angle) * radius,
y,
z + Math.sin(angle) * radius,
0.46 + random() * (riparian ? 0.42 : 0.58),
0.24 + random() * (riparian ? 0.28 : 0.18),
0.46 + random() * (riparian ? 0.42 : 0.58),
palette[Math.floor(random() * palette.length)],
0,
angle + random() * 0.4,
(random() - 0.5) * 0.08
);
}
}
function addShrub(x, z, index) {
const baseY = heights[index] + CAP_THICKNESS;
const moisture = moistures[index];
const count = 3 + Math.floor(random() * 4);
const size = 0.28 + random() * 0.3;
const color = new THREE.Color(0x7b833c).lerp(
new THREE.Color(0x416a39),
smoothstep(0.3, 0.9, moisture)
);
for (let part = 0; part < count; part++) {
const angle = random() * Math.PI * 2;
const radius = random() * 0.42;
addBlock(
shrubBlocks,
x + Math.cos(angle) * radius,
baseY + size * (0.35 + random() * 0.22),
z + Math.sin(angle) * radius,
size * (0.65 + random() * 0.65),
size * (0.5 + random() * 0.55),
size * (0.65 + random() * 0.65),
color.clone().offsetHSL(
(random() - 0.5) * 0.025,
(random() - 0.5) * 0.04,
(random() - 0.5) * 0.08
),
0,
angle,
0
);
}
}
// Sparse, irregular sampling preserves the open grassland while creating
// a much denser, irregular corridor near the stream.
for (let iz = 1; iz < NZ - 1; iz += 2) {
for (let ix = 1; ix < NX - 1; ix += 2) {
const index = iz * NX + ix;
const x = X0 + (ix + 0.5) * CELL;
const z = Z0 + (iz + 0.5) * CELL;
const distanceInCells = streamDistances[index] / CELL;
if (distanceInCells < 1.25) {
continue;
}
const riparianInfluence = Math.exp(
-Math.pow(distanceInCells / 3.05, 2)
);
const cluster = fbm(ix * 0.16 + 4, iz * 0.16 - 8);
const treeProbability =
0.025 +
0.28 * riparianInfluence +
0.025 * cluster;
const shrubProbability =
0.022 +
0.235 * riparianInfluence;
const roll = random();
const riparian =
distanceInCells < 4.1 &&
moistures[index] > 0.43;
if (roll < treeProbability) {
addTree(x, z, index, riparian);
} else if (roll < treeProbability + shrubProbability) {
addShrub(x, z, index);
}
}
}
// -------------------------------------------------------------------------
// Grass, reeds, stones and termite mounds
// -------------------------------------------------------------------------
const dryGrass = new THREE.Color(0xb39a49);
const wetGrass = new THREE.Color(0x4e713a);
const reedColor = new THREE.Color(0x79833e);
function addGrassBlade(x, z, baseY, height, width, color, tilt) {
addBlock(
grassBlocks,
x,
baseY + height / 2,
z,
width,
height,
width,
color,
tilt.x,
random() * Math.PI,
tilt.z
);
}
let grassTufts = 0;
let attempts = 0;
while (grassTufts < 720 && attempts < 12000) {
attempts++;
const x = X0 + random() * WIDTH;
const z = Z0 + random() * DEPTH;
const index = cellIndexAt(x, z);
const distance = streamDistances[index];
const width = streamWidth(z);
if (distance < width + 0.24) {
continue;
}
const moisture = moistures[index];
const baseY = surfaceYAt(x, z);
const blades = moisture > 0.54 ? 3 : 2;
for (let blade = 0; blade < blades; blade++) {
const angle = random() * Math.PI * 2;
const offset = random() * 0.12;
const height =
0.24 +
random() * 0.38 +
moisture * random() * 0.15;
const color = dryGrass.clone().lerp(
wetGrass,
smoothstep(0.18, 0.87, moisture)
);
color.offsetHSL(
(random() - 0.5) * 0.025,
(random() - 0.5) * 0.06,
(random() - 0.5) * 0.07
);
addGrassBlade(
x + Math.cos(angle) * offset,
z + Math.sin(angle) * offset,
baseY,
height,
0.055 + random() * 0.055,
color,
{
x: (random() - 0.5) * 0.09,
z: (random() - 0.5) * 0.09
}
);
}
grassTufts++;
}
// Upright wetland reeds form a broken fringe rather than a hard border.
for (let reed = 0; reed < 105; reed++) {
const z = Z0 + random() * DEPTH;
const side = random() > 0.5 ? 1 : -1;
const distance = streamWidth(z) + 0.1 + random() * 0.82;
const x = streamCenter(z) + side * distance;
const baseY = surfaceYAt(x, z);
for (let blade = 0; blade < 3; blade++) {
const height = 0.45 + random() * 0.46;
addGrassBlade(
x + (random() - 0.5) * 0.22,
z + (random() - 0.5) * 0.22,
baseY,
height,
0.045 + random() * 0.035,
reedColor.clone().offsetHSL(
(random() - 0.5) * 0.025,
(random() - 0.5) * 0.05,
(random() - 0.5) * 0.06
),
{
x: (random() - 0.5) * 0.08,
z: (random() - 0.5) * 0.08
}
);
}
}
// Wet-bank stones.
for (let stone = 0; stone < 52; stone++) {
const z = Z0 + random() * DEPTH;
const side = random() > 0.5 ? 1 : -1;
const distance =
streamWidth(z) +
0.28 +
Math.pow(random(), 1.6) * 1.05;
const x = streamCenter(z) + side * distance;
const baseY = surfaceYAt(x, z);
const size = 0.12 + Math.pow(random(), 2) * 0.32;
const color = new THREE.Color(0x8b806b).lerp(
new THREE.Color(0xb3a88d),
random()
);
addBlock(
rockBlocks,
x,
baseY + size * 0.28,
z,
size * (0.8 + random() * 0.5),
size * (0.45 + random() * 0.35),
size * (0.75 + random() * 0.6),
color,
random() * 0.12,
random() * Math.PI,
random() * 0.12
);
}
// A few reddish termite mounds in the drier field.
const moundPositions = [
{ x: -6.1, z: -4.3 },
{ x: 5.7, z: 4.9 },
{ x: -5.4, z: 7.2 }
];
const moundPalette = [
new THREE.Color(0x9b5636),
new THREE.Color(0xad6540),
new THREE.Color(0x82462f)
];
for (const position of moundPositions) {
const baseY = surfaceYAt(position.x, position.z);
const levels = 6 + Math.floor(random() * 3);
const baseWidth = 0.52 + random() * 0.22;
for (let level = 0; level < levels; level++) {
const t = level / levels;
const width = lerp(baseWidth, 0.18, t);
const height = 0.18 + random() * 0.08;
addBlock(
moundBlocks,
position.x + Math.sin(level * 1.4) * 0.035,
baseY + level * 0.22 + height / 2,
position.z + Math.cos(level * 1.2) * 0.03,
width,
height,
width * (0.85 + random() * 0.15),
moundPalette[Math.floor(random() * moundPalette.length)],
0,
random() * 0.08,
0
);
}
}
scene.add(
makeInstancedMesh(
trunkBlocks,
new THREE.MeshStandardMaterial({
color: 0xffffff,
roughness: 1
}),
true,
true
)
);
scene.add(
makeInstancedMesh(
leafBlocks,
new THREE.MeshStandardMaterial({
color: 0xffffff,
roughness: 0.88
}),
true,
true
)
);
scene.add(
makeInstancedMesh(
shrubBlocks,
new THREE.MeshStandardMaterial({
color: 0xffffff,
roughness: 0.92
}),
true,
true
)
);
scene.add(
makeInstancedMesh(
grassBlocks,
new THREE.MeshStandardMaterial({
color: 0xffffff,
roughness: 0.96
}),
false,
true
)
);
scene.add(
makeInstancedMesh(
rockBlocks,
new THREE.MeshStandardMaterial({
color: 0xffffff,
roughness: 0.96
}),
true,
true
)
);
scene.add(
makeInstancedMesh(
moundBlocks,
new THREE.MeshStandardMaterial({
color: 0xffffff,
roughness: 1
}),
true,
true
)
);
// -------------------------------------------------------------------------
// Moving stream highlights
// -------------------------------------------------------------------------
const flowCount = 22;
const flowBlocks = [];
for (let flow = 0; flow < flowCount; flow++) {
flowBlocks.push({
z: Z0 + random() * DEPTH,
speed: 0.38 + random() * 0.48,
length: 0.12 + random() * 0.24,
phase: random() * Math.PI * 2
});
}
const flowMesh = new THREE.InstancedMesh(
unitBox,
new THREE.MeshStandardMaterial({
color: 0xd2f0df,
roughness: 0.35,
transparent: true,
opacity: 0.62,
depthWrite: false
}),
flowCount
);
flowMesh.renderOrder = 3;
flowMesh.frustumCulled = false;
scene.add(flowMesh);
function updateFlow(time) {
flowBlocks.forEach((flow, index) => {
const z = flow.z + flow.speed * time * 0.35;
const wrappedZ = ((z - Z0) % DEPTH + DEPTH) % DEPTH + Z0;
const x = streamCenter(wrappedZ);
const derivative =
streamCenter(wrappedZ + 0.12) -
streamCenter(wrappedZ - 0.12);
const angle = Math.atan2(derivative, 0.24);
dummy.position.set(
x,
-0.247 + Math.sin(time * 1.8 + flow.phase) * 0.008,
wrappedZ
);
dummy.rotation.set(0, angle, 0);
dummy.scale.set(
0.045 + Math.sin(flow.phase + time) * 0.015,
0.025,
flow.length
);
dummy.updateMatrix();
flowMesh.setMatrixAt(index, dummy.matrix);
});
flowMesh.instanceMatrix.needsUpdate = true;
}
// -------------------------------------------------------------------------
// Custom camera controls: drag, wheel, keyboard and pinch
// -------------------------------------------------------------------------
const pointers = new Map();
let dragging = false;
let previousX = 0;
let previousY = 0;
let previousPinchDistance = 0;
function pointerDistance() {
const values = [...pointers.values()];
if (values.length < 2) {
return 0;
}
return Math.hypot(
values[0].x - values[1].x,
values[0].y - values[1].y
);
}
canvas.addEventListener("pointerdown", (event) => {
canvas.setPointerCapture(event.pointerId);
pointers.set(event.pointerId, {
x: event.clientX,
y: event.clientY
});
if (pointers.size === 1) {
dragging = true;
previousX = event.clientX;
previousY = event.clientY;
} else if (pointers.size === 2) {
dragging = false;
previousPinchDistance = pointerDistance();
}
});
canvas.addEventListener("pointermove", (event) => {
if (!pointers.has(event.pointerId)) {
return;
}
pointers.set(event.pointerId, {
x: event.clientX,
y: event.clientY
});
if (pointers.size === 1 && dragging) {
const deltaX = event.clientX - previousX;
const deltaY = event.clientY - previousY;
cameraState.targetTheta -= deltaX * 0.0055;
cameraState.targetPhi = clamp(
cameraState.targetPhi - deltaY * 0.0045,
0.38,
1.42
);
previousX = event.clientX;
previousY = event.clientY;
} else if (pointers.size === 2) {
const distance = pointerDistance();
if (previousPinchDistance > 0 && distance > 0) {
cameraState.targetRadius = clamp(
cameraState.targetRadius *
(previousPinchDistance / distance),
20,
52
);
}
previousPinchDistance = distance;
}
});
function releasePointer(event) {
pointers.delete(event.pointerId);
dragging = pointers.size === 1;
if (pointers.size === 1) {
const remaining = [...pointers.values()][0];
previousX = remaining.x;
previousY = remaining.y;
}
previousPinchDistance = 0;
}
canvas.addEventListener("pointerup", releasePointer);
canvas.addEventListener("pointercancel", releasePointer);
canvas.addEventListener("lostpointercapture", releasePointer);
canvas.addEventListener("wheel", (event) => {
event.preventDefault();
cameraState.targetRadius = clamp(
cameraState.targetRadius + event.deltaY * 0.025,
20,
52
);
}, { passive: false });
canvas.addEventListener("dblclick", resetCamera);
window.addEventListener("keydown", (event) => {
if (event.key === "ArrowLeft") {
cameraState.targetTheta += 0.08;
} else if (event.key === "ArrowRight") {
cameraState.targetTheta -= 0.08;
} else if (event.key === "ArrowUp") {
cameraState.targetPhi = clamp(
cameraState.targetPhi - 0.06,
0.38,
1.42
);
} else if (event.key === "ArrowDown") {
cameraState.targetPhi = clamp(
cameraState.targetPhi + 0.06,
0.38,
1.42
);
} else if (event.key === "+" || event.key === "=") {
cameraState.targetRadius = clamp(
cameraState.targetRadius - 2,
20,
52
);
} else if (event.key === "-") {
cameraState.targetRadius = clamp(
cameraState.targetRadius + 2,
20,
52
);
}
});
function resetCamera() {
cameraState.targetTheta = 0.69;
cameraState.targetPhi = 1.03;
cameraState.targetRadius = 37;
}
document.querySelector("#reset").addEventListener("click", resetCamera);
// -------------------------------------------------------------------------
// Resize and render loop
// -------------------------------------------------------------------------
function resize() {
const width = window.innerWidth;
const height = window.innerHeight;
camera.aspect = width / height;
camera.updateProjectionMatrix();
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(width, height);
}
window.addEventListener("resize", resize);
const clock = new THREE.Clock();
let firstFrame = true;
function animate() {
const elapsed = clock.getElapsedTime();
cameraState.theta = lerp(
cameraState.theta,
cameraState.targetTheta,
0.1
);
cameraState.phi = lerp(
cameraState.phi,
cameraState.targetPhi,
0.1
);
cameraState.radius = lerp(
cameraState.radius,
cameraState.targetRadius,
0.1
);
const sinPhiRadius =
Math.sin(cameraState.phi) * cameraState.radius;
camera.position.set(
cameraTarget.x +
sinPhiRadius * Math.sin(cameraState.theta),
cameraTarget.y +
Math.cos(cameraState.phi) * cameraState.radius,
cameraTarget.z +
sinPhiRadius * Math.cos(cameraState.theta)
);
camera.lookAt(cameraTarget);
updateFlow(elapsed);
renderer.render(scene, camera);
if (firstFrame) {
firstFrame = false;
loading.classList.add("hidden");
window.setTimeout(() => loading.remove(), 600);
}
}
renderer.setAnimationLoop(animate);
</script>
</body>
</html>
```