Prompt specification
Build or customize the Three.js 3D scene "Amazon floodplain tree-root and waterline tile" 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: Amazon floodplain tree-root and waterline tile
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/amazon-floodplain-tree-root-and-waterline-tile-x3041
Scene Description Prompt:
Please create one self-contained HTML file with Three.js from cdnjs showing Amazon floodplain tree roots and changing waterlines. Build a referenced floodplain tree-root fragment beside a riverbank with two waterline markers. Distinguish submerged ground from permanently higher terrain using a small cutaway. Voxel style.
Working Three.js Starter (amazon-floodplain-tree-root-and-waterline-tile-x3041):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Amazon Floodplain — Voxel Root Zone</title>
<style>
:root {
--ink: #302d28;
--muted: #706b61;
--paper: #f3eedf;
--panel: rgba(247, 242, 226, 0.92);
--border: rgba(48, 45, 40, 0.17);
--blue: #3aa2aa;
--gold: #e9b84d;
--coral: #e66c52;
--olive: #6f8152;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #e9e5d9;
color: var(--ink);
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
canvas {
display: block;
width: 100%;
height: 100%;
outline: none;
touch-action: none;
}
#labels {
position: fixed;
inset: 0;
pointer-events: none;
overflow: hidden;
}
.world-label {
position: absolute;
display: flex;
align-items: center;
gap: 7px;
min-height: 27px;
padding: 5px 9px 5px 7px;
border: 1px solid rgba(48, 45, 40, 0.16);
border-radius: 4px;
background: rgba(248, 244, 232, 0.94);
box-shadow: 0 5px 16px rgba(57, 48, 36, 0.12);
color: #3b3730;
font-size: 10px;
font-weight: 800;
letter-spacing: 0.095em;
line-height: 1;
white-space: nowrap;
transform: translate(-50%, -50%);
transition: opacity 120ms ease;
}
.world-label::after {
content: "";
position: absolute;
left: 50%;
top: 100%;
width: 1px;
height: 15px;
background: rgba(48, 45, 40, 0.42);
}
.world-label .swatch {
width: 10px;
height: 10px;
flex: 0 0 auto;
border-radius: 2px;
background: var(--olive);
box-shadow: inset 0 0 0 1px rgba(0, 0, 0, 0.12);
}
.title-card {
position: fixed;
top: 24px;
left: 26px;
width: min(430px, calc(100vw - 330px));
pointer-events: none;
text-shadow: 0 1px rgba(255, 255, 255, 0.35);
}
.eyebrow {
margin-bottom: 7px;
color: #777166;
font-size: 10px;
font-weight: 900;
letter-spacing: 0.23em;
text-transform: uppercase;
}
h1 {
margin: 0;
font-size: clamp(25px, 3vw, 42px);
font-weight: 850;
letter-spacing: -0.045em;
line-height: 0.98;
}
.subtitle {
max-width: 390px;
margin: 11px 0 0;
color: #686358;
font-size: 12px;
font-weight: 550;
line-height: 1.55;
}
.control-card {
position: fixed;
top: 22px;
right: 22px;
width: 272px;
padding: 16px;
border: 1px solid var(--border);
border-radius: 8px;
background: var(--panel);
box-shadow: 0 14px 40px rgba(62, 53, 41, 0.13);
backdrop-filter: blur(10px);
}
.panel-title {
display: flex;
justify-content: space-between;
align-items: center;
margin-bottom: 13px;
font-size: 10px;
font-weight: 900;
letter-spacing: 0.15em;
text-transform: uppercase;
}
.status {
padding: 4px 6px;
border-radius: 3px;
background: rgba(58, 162, 170, 0.13);
color: #237a80;
font-size: 8px;
letter-spacing: 0.12em;
}
.level-row {
display: flex;
align-items: baseline;
justify-content: space-between;
margin-bottom: 8px;
}
#levelValue {
font-size: 27px;
font-weight: 850;
letter-spacing: -0.04em;
}
.datum {
color: #7b7468;
font-size: 10px;
font-weight: 700;
}
input[type="range"] {
width: 100%;
height: 5px;
margin: 6px 0 4px;
appearance: none;
border-radius: 999px;
background: linear-gradient(
90deg,
var(--blue) 0%,
var(--blue) var(--fill, 50%),
rgba(48, 45, 40, 0.13) var(--fill, 50%),
rgba(48, 45, 40, 0.13) 100%
);
cursor: pointer;
}
input[type="range"]::-webkit-slider-thumb {
width: 15px;
height: 15px;
appearance: none;
border: 2px solid var(--paper);
border-radius: 50%;
background: var(--ink);
box-shadow: 0 1px 5px rgba(0, 0, 0, 0.25);
}
input[type="range"]::-moz-range-thumb {
width: 13px;
height: 13px;
border: 2px solid var(--paper);
border-radius: 50%;
background: var(--ink);
box-shadow: 0 1px 5px rgba(0, 0, 0, 0.25);
}
.scale {
display: flex;
justify-content: space-between;
margin: 4px 0 13px;
color: #81796d;
font-size: 8px;
font-weight: 800;
letter-spacing: 0.1em;
}
.button-row {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 7px;
}
button {
min-height: 34px;
border: 1px solid rgba(48, 45, 40, 0.18);
border-radius: 4px;
background: rgba(255, 255, 255, 0.42);
color: #4d483f;
font: inherit;
font-size: 9px;
font-weight: 850;
letter-spacing: 0.08em;
text-transform: uppercase;
cursor: pointer;
transition: background 120ms ease, transform 120ms ease;
}
button:hover {
background: rgba(255, 255, 255, 0.85);
}
button:active {
transform: translateY(1px);
}
.legend {
position: fixed;
left: 26px;
bottom: 25px;
display: flex;
gap: 16px;
padding: 10px 12px;
border: 1px solid var(--border);
border-radius: 6px;
background: rgba(247, 242, 226, 0.84);
box-shadow: 0 8px 25px rgba(62, 53, 41, 0.08);
backdrop-filter: blur(8px);
}
.legend-item {
display: flex;
align-items: center;
gap: 7px;
color: #5e594f;
font-size: 9px;
font-weight: 800;
letter-spacing: 0.06em;
text-transform: uppercase;
}
.legend-swatch {
width: 12px;
height: 12px;
border-radius: 2px;
box-shadow: inset 0 0 0 1px rgba(0, 0, 0, 0.1);
}
.hint {
position: fixed;
right: 24px;
bottom: 25px;
color: rgba(70, 65, 57, 0.67);
font-size: 9px;
font-weight: 800;
letter-spacing: 0.11em;
text-transform: uppercase;
pointer-events: none;
}
@media (max-width: 760px) {
.title-card {
top: 17px;
left: 17px;
width: calc(100vw - 34px);
}
.subtitle {
display: none;
}
.control-card {
top: auto;
right: 13px;
bottom: 60px;
width: 220px;
padding: 12px;
}
.legend {
left: 13px;
bottom: 14px;
gap: 9px;
padding: 8px;
}
.legend-item {
font-size: 7px;
}
.hint {
display: none;
}
}
</style>
</head>
<body>
<div id="labels">
<div class="world-label" id="rootLabel">
<span class="swatch" style="background:#704b35"></span>
TREE-ROOT FRAGMENT
</div>
<div class="world-label" id="lowLabel">
<span class="swatch" style="background:#e9b84d"></span>
LOW WATERLINE
</div>
<div class="world-label" id="highLabel">
<span class="swatch" style="background:#e66c52"></span>
HIGH WATERLINE
</div>
<div class="world-label" id="higherLabel">
<span class="swatch" style="background:#708253"></span>
PERMANENTLY HIGHER
</div>
<div class="world-label" id="submergedLabel">
<span class="swatch" style="background:#3aa2aa"></span>
SEASONALLY SUBMERGED
</div>
</div>
<header class="title-card">
<div class="eyebrow">Amazon Basin / Floodplain Study</div>
<h1>Root zone<br>at the waterline</h1>
<p class="subtitle">
A voxel study of exposed buttress roots, seasonal inundation, and the
permanent higher margin beside a river channel.
</p>
</header>
<aside class="control-card">
<div class="panel-title">
<span>Current water level</span>
<span class="status" id="status">LIVE</span>
</div>
<div class="level-row">
<span id="levelValue">0.65 m</span>
<span class="datum">above channel datum</span>
</div>
<input
id="waterRange"
type="range"
min="0"
max="100"
value="50"
aria-label="Water level"
>
<div class="scale">
<span>LOW 0.20 M</span>
<span>HIGH 1.10 M</span>
</div>
<div class="button-row">
<button id="playButton" type="button">Pause cycle</button>
<button id="resetButton" type="button">Reset view</button>
</div>
</aside>
<div class="legend">
<div class="legend-item">
<span class="legend-swatch" style="background:#3aa2aa"></span>
Submerged ground
</div>
<div class="legend-item">
<span class="legend-swatch" style="background:#708253"></span>
Permanent higher terrain
</div>
</div>
<div class="hint">Drag to orbit · Scroll or pinch to zoom</div>
<script type="module">
import * as THREE from "https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// ------------------------------------------------------------
// Scene
// ------------------------------------------------------------
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xe9e5d9);
scene.fog = new THREE.Fog(0xe9e5d9, 29, 48);
const camera = new THREE.PerspectiveCamera(
36,
innerWidth / innerHeight,
0.1,
100
);
const renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setPixelRatio(Math.min(devicePixelRatio, 2));
renderer.setSize(innerWidth, innerHeight);
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.08;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.domElement.tabIndex = 0;
document.body.prepend(renderer.domElement);
const hemisphere = new THREE.HemisphereLight(0xfff9e9, 0x596253, 2.25);
scene.add(hemisphere);
const sunlight = new THREE.DirectionalLight(0xfff1d5, 3.1);
sunlight.position.set(-8, 13, 9);
sunlight.castShadow = true;
sunlight.shadow.mapSize.set(2048, 2048);
sunlight.shadow.camera.left = -14;
sunlight.shadow.camera.right = 14;
sunlight.shadow.camera.top = 14;
sunlight.shadow.camera.bottom = -14;
sunlight.shadow.camera.near = 1;
sunlight.shadow.camera.far = 40;
sunlight.shadow.bias = -0.00035;
sunlight.shadow.normalBias = 0.025;
sunlight.target.position.set(0, 1, 0);
scene.add(sunlight, sunlight.target);
const rimLight = new THREE.DirectionalLight(0xcde6df, 0.75);
rimLight.position.set(10, 6, -9);
scene.add(rimLight);
const unitBox = new THREE.BoxGeometry(1, 1, 1);
const materialCache = new Map();
function material(color) {
if (!materialCache.has(color)) {
materialCache.set(
color,
new THREE.MeshStandardMaterial({
color,
roughness: 0.94,
metalness: 0
})
);
}
return materialCache.get(color);
}
function addBox(w, h, d, color, x, y, z, parent = scene) {
const mesh = new THREE.Mesh(unitBox, material(color));
mesh.scale.set(w, h, d);
mesh.position.set(x, y, z);
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
let seed = 8713;
function random() {
seed = (seed * 1664525 + 1013904223) >>> 0;
return seed / 4294967296;
}
function terrainTop(x) {
if (x < 2) return 1.5;
if (x < 3) return 1.0;
if (x < 4) return 0.5;
return 0.0;
}
// ------------------------------------------------------------
// Voxel cutaway terrain
// The front edge is deliberately exposed as a soil section.
// ------------------------------------------------------------
const floor = new THREE.Mesh(
new THREE.PlaneGeometry(80, 80),
new THREE.MeshStandardMaterial({
color: 0xded9cc,
roughness: 1
})
);
floor.rotation.x = -Math.PI / 2;
floor.position.y = -2.025;
floor.receiveShadow = true;
scene.add(floor);
const terrainGroups = new Map();
function terrainInstance(color, x, y, z, sx, sy, sz) {
if (!terrainGroups.has(color)) {
terrainGroups.set(color, []);
}
const matrix = new THREE.Matrix4();
matrix.compose(
new THREE.Vector3(x, y, z),
new THREE.Quaternion(),
new THREE.Vector3(sx, sy, sz)
);
terrainGroups.get(color).push(matrix);
}
const greenPalette = [0x718153, 0x78895a, 0x66764c, 0x819061];
const dryPalette = [0x8a8c60, 0x969064, 0x7e8059];
const bedPalette = [0x7c6d52, 0x88765a, 0x71634d];
const soilPalette = [0x796348, 0x705b43, 0x604d3a];
const deepSoilPalette = [0x55483b, 0x4d4237, 0x5b4b3d];
for (let xi = 0; xi < 14; xi++) {
const x = -6.5 + xi;
for (let zi = 0; zi < 9; zi++) {
const z = -4 + zi;
const top = terrainTop(x);
for (let y = -1.75; y < top - 0.001; y += 0.5) {
let color;
if (y >= top - 0.501) {
if (x >= 4) {
color = bedPalette[Math.floor(random() * bedPalette.length)];
} else if (x >= 3) {
color = dryPalette[Math.floor(random() * dryPalette.length)];
} else {
color = greenPalette[Math.floor(random() * greenPalette.length)];
}
} else if (y < -0.75) {
color = deepSoilPalette[Math.floor(random() * deepSoilPalette.length)];
} else {
color = soilPalette[Math.floor(random() * soilPalette.length)];
}
terrainInstance(color, x, y, z, 0.95, 0.49, 0.95);
}
}
}
for (const [color, matrices] of terrainGroups) {
const mesh = new THREE.InstancedMesh(unitBox, material(color), matrices.length);
matrices.forEach((matrix, index) => {
mesh.setMatrixAt(index, matrix);
});
mesh.instanceMatrix.needsUpdate = true;
mesh.castShadow = true;
mesh.receiveShadow = true;
scene.add(mesh);
}
// A small ochre strip marks the permanent, higher floodplain edge.
addBox(2.2, 0.055, 0.055, 0xd8b65c, -0.25, 1.525, 4.505);
// ------------------------------------------------------------
// Changing voxel water
// ------------------------------------------------------------
const LOW_LEVEL = 0.20;
const HIGH_LEVEL = 1.10;
const waterMaterial = new THREE.MeshStandardMaterial({
color: 0x3aa2aa,
roughness: 0.24,
metalness: 0,
transparent: true,
opacity: 0.72,
depthWrite: false,
side: THREE.DoubleSide
});
const waterMesh = new THREE.InstancedMesh(
unitBox,
waterMaterial,
100
);
waterMesh.instanceMatrix.setUsage(THREE.DynamicDrawUsage);
waterMesh.frustumCulled = false;
waterMesh.renderOrder = 2;
scene.add(waterMesh);
// Vertical water strips reveal the cutaway water column at the front.
const cutawayWaterMaterial = new THREE.MeshStandardMaterial({
color: 0x248f99,
roughness: 0.35,
transparent: true,
opacity: 0.48,
depthWrite: false
});
const cutawayWaterMesh = new THREE.InstancedMesh(
unitBox,
cutawayWaterMaterial,
30
);
cutawayWaterMesh.instanceMatrix.setUsage(THREE.DynamicDrawUsage);
cutawayWaterMesh.frustumCulled = false;
cutawayWaterMesh.renderOrder = 3;
scene.add(cutawayWaterMesh);
const tempMatrix = new THREE.Matrix4();
const tempPosition = new THREE.Vector3();
const tempScale = new THREE.Vector3();
const identityQuaternion = new THREE.Quaternion();
let waterLevel = 0.65;
let firstWetX = 4.5;
function updateWater() {
let waterCount = 0;
let cutawayCount = 0;
firstWetX = 6.5;
for (let xi = 2; xi < 14; xi++) {
const x = -6.5 + xi;
if (terrainTop(x) < waterLevel - 0.01) {
firstWetX = Math.min(firstWetX, x);
}
for (let zi = 0; zi < 9; zi++) {
const z = -4 + zi;
const top = terrainTop(x);
if (top < waterLevel - 0.01) {
tempPosition.set(x, waterLevel - 0.035, z);
tempScale.set(0.97, 0.07, 0.97);
tempMatrix.compose(tempPosition, identityQuaternion, tempScale);
waterMesh.setMatrixAt(waterCount++, tempMatrix);
// Only the front row is included in the exposed cutaway.
if (zi === 8) {
const height = waterLevel - top;
tempPosition.set(x, top + height * 0.5, 4.505);
tempScale.set(0.97, height, 0.045);
tempMatrix.compose(tempPosition, identityQuaternion, tempScale);
cutawayWaterMesh.setMatrixAt(cutawayCount++, tempMatrix);
}
}
}
}
waterMesh.count = waterCount;
cutawayWaterMesh.count = cutawayCount;
waterMesh.instanceMatrix.needsUpdate = true;
cutawayWaterMesh.instanceMatrix.needsUpdate = true;
}
// Square foam pixels emphasize the changing surface.
const foamMaterial = new THREE.MeshStandardMaterial({
color: 0xd9eee3,
roughness: 0.6,
transparent: true,
opacity: 0.78,
depthWrite: false
});
const foamPixels = [];
for (let i = 0; i < 15; i++) {
const foam = new THREE.Mesh(unitBox, foamMaterial);
const x = 4.15 + random() * 2.45;
const z = -4.15 + random() * 8.3;
const width = 0.13 + random() * 0.24;
const depth = 0.06 + random() * 0.09;
foam.scale.set(width, 0.022, depth);
foam.position.set(x, waterLevel + 0.045, z);
foam.renderOrder = 4;
scene.add(foam);
foamPixels.push({ mesh: foam, x, z, phase: random() * Math.PI * 2 });
}
function updateFoam(time) {
for (const foam of foamPixels) {
const submerged = terrainTop(foam.x) < waterLevel - 0.01;
foam.mesh.visible = submerged;
foam.mesh.position.y = waterLevel + 0.047;
foam.mesh.position.z =
foam.z + Math.sin(time * 0.65 + foam.phase) * 0.08;
}
}
// ------------------------------------------------------------
// Two fixed waterline markers and a moving gauge indicator
// ------------------------------------------------------------
const gaugeX = 4.25;
const gaugeZ = -2.65;
addBox(0.16, 1.55, 0.64, 0xf1ead8, gaugeX, 0.67, gaugeZ);
addBox(0.25, 0.10, 0.74, 0x554d42, gaugeX, -0.17, gaugeZ);
addBox(
0.22,
0.085,
0.065,
0xe9b84d,
gaugeX,
LOW_LEVEL,
gaugeZ + 0.345
);
addBox(
0.22,
0.085,
0.065,
0xe66c52,
gaugeX,
HIGH_LEVEL,
gaugeZ + 0.345
);
const currentGauge = addBox(
0.27,
0.055,
0.085,
0xfaf7ed,
gaugeX,
waterLevel,
gaugeZ + 0.375
);
// ------------------------------------------------------------
// Voxel tree-root fragment
// ------------------------------------------------------------
const trunkX = -2;
const trunkZ = -0.8;
// Stepped, leaning trunk.
addBox(1.18, 0.52, 1.02, 0x64442f, trunkX, 1.76, trunkZ);
addBox(1.00, 0.50, 0.91, 0x704a32, -2.02, 2.24, -0.82);
addBox(0.94, 0.50, 0.86, 0x795039, -2.06, 2.74, -0.84);
addBox(0.88, 0.50, 0.82, 0x68442e, -2.02, 3.24, -0.84);
addBox(0.82, 0.48, 0.76, 0x7c5238, -1.96, 3.72, -0.84);
addBox(0.73, 0.44, 0.68, 0x68452f, -1.91, 4.18, -0.84);
addBox(0.60, 0.34, 0.57, 0x81583b, -1.88, 4.55, -0.84);
// Broken pale cut face.
addBox(0.42, 0.075, 0.40, 0xb88958, -1.88, 4.755, -0.84);
// A short broken side limb.
addBox(0.58, 0.37, 0.48, 0x704a31, -2.48, 3.27, -0.82);
addBox(0.43, 0.30, 0.40, 0x805538, -2.92, 3.38, -0.82);
addBox(0.28, 0.25, 0.29, 0x9a6e48, -3.22, 3.45, -0.82);
// Blocky buttress roots around the base.
addBox(0.76, 0.64, 0.94, 0x704a32, -1.48, 1.77, -0.80);
addBox(0.72, 0.48, 0.83, 0x62422e, -0.98, 1.70, -0.76);
addBox(0.62, 0.34, 0.70, 0x795038, -0.48, 1.62, -0.72);
addBox(0.91, 0.60, 0.72, 0x714b32, -2.0, 1.77, -0.25);
addBox(0.79, 0.43, 0.62, 0x60412e, -2.0, 1.69, 0.23);
addBox(0.67, 0.31, 0.55, 0x7b5136, -2.0, 1.62, 0.67);
const rootColors = [0x6c4932, 0x795138, 0x5e402d, 0x845b3e];
function makeVoxelRoot(path) {
const curve = new THREE.CatmullRomCurve3(
path.map((point) => new THREE.Vector3(...point))
);
const length = curve.getLength();
const count = Math.max(9, Math.ceil(length * 4.2));
for (let i = 0; i <= count; i++) {
const t = i / count;
const point = curve.getPoint(t);
const size = 0.56 * (1 - t * 0.70) + 0.13;
const color = rootColors[Math.floor(random() * rootColors.length)];
const rootBlock = addBox(
size * (1.05 + random() * 0.18),
size * 0.76,
size * (0.90 + random() * 0.18),
color,
point.x,
point.y,
point.z
);
rootBlock.rotation.y = Math.floor(random() * 4) * Math.PI * 0.5;
if (i % 3 === 0) {
addBox(
size * 0.65,
size * 0.35,
size * 0.72,
0x4e3728,
point.x + 0.04,
point.y - size * 0.34,
point.z + 0.03
);
}
}
}
// Principal root descending toward the riverbank.
makeVoxelRoot([
[-1.90, 1.82, -0.78],
[-1.28, 1.72, -0.68],
[-0.56, 1.66, -0.52],
[0.18, 1.58, -0.32],
[0.86, 1.47, -0.08],
[1.38, 1.35, 0.08],
[1.83, 1.12, 0.18],
[2.28, 0.70, 0.26],
[2.72, 0.35, 0.29],
[3.18, 0.03, 0.30]
]);
// Branch that enters the seasonally inundated terrace.
makeVoxelRoot([
[-0.20, 1.64, -0.35],
[0.42, 1.50, 0.34],
[0.95, 1.24, 0.90],
[1.55, 0.78, 1.38],
[2.18, 0.25, 1.72],
[2.72, -0.06, 1.95]
]);
// Front and left buttress roots.
makeVoxelRoot([
[-2.04, 1.78, -0.42],
[-2.45, 1.67, 0.30],
[-2.87, 1.62, 1.00],
[-3.30, 1.57, 1.73]
]);
makeVoxelRoot([
[-2.50, 1.76, -0.82],
[-3.02, 1.64, -1.30],
[-3.56, 1.60, -1.84],
[-4.22, 1.50, -2.28],
[-4.85, 1.39, -2.75]
]);
// Rear roots, visible as the camera orbits.
makeVoxelRoot([
[-1.86, 1.75, -1.10],
[-1.40, 1.59, -1.72],
[-0.78, 1.55, -2.28],
[-0.05, 1.43, -2.78],
[0.60, 1.28, -3.20]
]);
makeVoxelRoot([
[-2.48, 1.72, -1.10],
[-3.08, 1.55, -1.72],
[-3.55, 1.34, -2.45],
[-3.88, 1.12, -3.24]
]);
// Moss-like voxel flecks on the larger roots.
addBox(0.30, 0.12, 0.25, 0x657548, -1.15, 1.84, -0.42);
addBox(0.22, 0.10, 0.20, 0x71804c, 0.18, 1.70, -0.18);
addBox(0.25, 0.11, 0.22, 0x5d7044, 1.52, 1.45, 0.13);
addBox(0.20, 0.09, 0.18, 0x778452, -3.28, 1.73, 1.69);
// ------------------------------------------------------------
// Floodplain vegetation and riverbank details
// ------------------------------------------------------------
const grassColors = [0x71804c, 0x82905a, 0x5d7246, 0x8b965d];
for (let i = 0; i < 48; i++) {
const x = -6.65 + random() * 8.25;
const z = -4.15 + random() * 8.3;
if (Math.hypot(x - trunkX, z - trunkZ) < 1.2) continue;
const ground = terrainTop(x);
const height = 0.20 + random() * 0.28;
for (let blade = 0; blade < 2; blade++) {
addBox(
0.055,
height,
0.065,
grassColors[Math.floor(random() * grassColors.length)],
x + (random() - 0.5) * 0.16,
ground + height * 0.5,
z + (random() - 0.5) * 0.16
);
}
}
// Reeds along the intermittently flooded margin.
for (let i = 0; i < 22; i++) {
const x = 3.1 + random() * 0.8;
const z = -4.1 + random() * 8.2;
const ground = terrainTop(x);
const height = 0.45 + random() * 0.55;
addBox(
0.055,
height,
0.065,
i % 3 === 0 ? 0x8b784a : 0x647649,
x,
ground + height * 0.5,
z
);
}
// Square stones scattered over the cutaway ground.
for (let i = 0; i < 22; i++) {
const x = -6.5 + random() * 12.8;
const z = -4.2 + random() * 8.4;
const top = terrainTop(x);
const size = 0.13 + random() * 0.22;
addBox(
size,
size * 0.65,
size,
i % 2 ? 0x81745e : 0x92856c,
x,
top + size * 0.31,
z
);
}
// ------------------------------------------------------------
// Direct camera controls: no external control modules
// ------------------------------------------------------------
const cameraTarget = new THREE.Vector3(-0.15, 1.45, 0);
const cameraState = {
theta: 0.61,
phi: 1.07,
radius: 20.2,
targetTheta: 0.61,
targetPhi: 1.07,
targetRadius: 20.2
};
function resetCamera() {
cameraState.targetTheta = 0.61;
cameraState.targetPhi = 1.07;
cameraState.targetRadius = 20.2;
}
function updateCamera(dt) {
const smoothing = 1 - Math.exp(-dt * 11);
cameraState.theta +=
(cameraState.targetTheta - cameraState.theta) * smoothing;
cameraState.phi +=
(cameraState.targetPhi - cameraState.phi) * smoothing;
cameraState.radius +=
(cameraState.targetRadius - cameraState.radius) * smoothing;
const spherical = new THREE.Spherical(
cameraState.radius,
cameraState.phi,
cameraState.theta
);
camera.position.setFromSpherical(spherical).add(cameraTarget);
camera.lookAt(cameraTarget);
}
const pointers = new Map();
let lastPinchDistance = 0;
renderer.domElement.addEventListener("pointerdown", (event) => {
renderer.domElement.setPointerCapture(event.pointerId);
pointers.set(event.pointerId, {
x: event.clientX,
y: event.clientY
});
if (pointers.size === 2) {
const points = [...pointers.values()];
lastPinchDistance = Math.hypot(
points[0].x - points[1].x,
points[0].y - points[1].y
);
}
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (!pointers.has(event.pointerId)) return;
const previous = pointers.get(event.pointerId);
const dx = event.clientX - previous.x;
const dy = event.clientY - previous.y;
pointers.set(event.pointerId, {
x: event.clientX,
y: event.clientY
});
if (pointers.size === 1) {
cameraState.targetTheta -= dx * 0.006;
cameraState.targetPhi = THREE.MathUtils.clamp(
cameraState.targetPhi - dy * 0.005,
0.36,
1.42
);
} else if (pointers.size === 2) {
const points = [...pointers.values()];
const distance = Math.hypot(
points[0].x - points[1].x,
points[0].y - points[1].y
);
if (lastPinchDistance > 0 && distance > 0) {
cameraState.targetRadius = THREE.MathUtils.clamp(
cameraState.targetRadius * (lastPinchDistance / distance),
12,
30
);
}
lastPinchDistance = distance;
}
});
function releasePointer(event) {
pointers.delete(event.pointerId);
if (pointers.size < 2) lastPinchDistance = 0;
}
renderer.domElement.addEventListener("pointerup", releasePointer);
renderer.domElement.addEventListener("pointercancel", releasePointer);
renderer.domElement.addEventListener("contextmenu", (event) => {
event.preventDefault();
});
renderer.domElement.addEventListener(
"wheel",
(event) => {
event.preventDefault();
cameraState.targetRadius = THREE.MathUtils.clamp(
cameraState.targetRadius * Math.exp(event.deltaY * 0.001),
12,
30
);
},
{ passive: false }
);
renderer.domElement.addEventListener("dblclick", resetCamera);
window.addEventListener("keydown", (event) => {
if (event.target instanceof HTMLInputElement) return;
if (event.key === "ArrowLeft") cameraState.targetTheta -= 0.12;
if (event.key === "ArrowRight") cameraState.targetTheta += 0.12;
if (event.key === "ArrowUp") {
cameraState.targetPhi = Math.max(0.36, cameraState.targetPhi - 0.08);
}
if (event.key === "ArrowDown") {
cameraState.targetPhi = Math.min(1.42, cameraState.targetPhi + 0.08);
}
if (event.key === "+" || event.key === "=") {
cameraState.targetRadius = Math.max(12, cameraState.targetRadius - 1);
}
if (event.key === "-") {
cameraState.targetRadius = Math.min(30, cameraState.targetRadius + 1);
}
if (event.key.toLowerCase() === "r") resetCamera();
});
// ------------------------------------------------------------
// Water controls
// ------------------------------------------------------------
const waterRange = document.getElementById("waterRange");
const levelValue = document.getElementById("levelValue");
const playButton = document.getElementById("playButton");
const resetButton = document.getElementById("resetButton");
const status = document.getElementById("status");
let waterProgress = 0.5;
let playing = true;
let cycleTime = 0;
function setWaterProgress(value) {
waterProgress = THREE.MathUtils.clamp(value, 0, 1);
waterLevel = THREE.MathUtils.lerp(LOW_LEVEL, HIGH_LEVEL, waterProgress);
currentGauge.position.y = waterLevel;
const percent = Math.round(waterProgress * 100);
waterRange.value = percent;
waterRange.style.setProperty("--fill", `${percent}%`);
levelValue.textContent = `${waterLevel.toFixed(2)} m`;
updateWater();
}
waterRange.addEventListener("input", () => {
playing = false;
playButton.textContent = "Play cycle";
status.textContent = "MANUAL";
setWaterProgress(Number(waterRange.value) / 100);
});
playButton.addEventListener("click", () => {
playing = !playing;
if (playing) {
cycleTime = 0;
status.textContent = "LIVE";
playButton.textContent = "Pause cycle";
} else {
status.textContent = "PAUSED";
playButton.textContent = "Play cycle";
}
});
resetButton.addEventListener("click", resetCamera);
// ------------------------------------------------------------
// Projected world labels
// ------------------------------------------------------------
const labels = {
root: {
element: document.getElementById("rootLabel"),
point: new THREE.Vector3(-1.9, 5.22, -0.83),
dx: -25,
dy: -30
},
low: {
element: document.getElementById("lowLabel"),
point: new THREE.Vector3(gaugeX, LOW_LEVEL, gaugeZ + 0.4),
dx: 88,
dy: 19
},
high: {
element: document.getElementById("highLabel"),
point: new THREE.Vector3(gaugeX, HIGH_LEVEL, gaugeZ + 0.4),
dx: 90,
dy: -12
},
higher: {
element: document.getElementById("higherLabel"),
point: new THREE.Vector3(-0.25, 1.58, 4.55),
dx: -65,
dy: -18
},
submerged: {
element: document.getElementById("submergedLabel"),
point: new THREE.Vector3(3.5, 0.7, 4.58),
dx: 65,
dy: 27
}
};
const projected = new THREE.Vector3();
function updateLabels() {
const submergedY = Math.max(
0.13,
Math.min(waterLevel - 0.04, terrainTop(firstWetX) + 0.15)
);
labels.submerged.point.set(firstWetX, submergedY, 4.58);
for (const label of Object.values(labels)) {
projected.copy(label.point).project(camera);
const visible =
projected.z > -1 &&
projected.z < 1 &&
Math.abs(projected.x) < 1.25 &&
Math.abs(projected.y) < 1.25;
label.element.style.opacity = visible ? "1" : "0";
const x = (projected.x * 0.5 + 0.5) * innerWidth + label.dx;
const y = (-projected.y * 0.5 + 0.5) * innerHeight + label.dy;
label.element.style.left = `${x}px`;
label.element.style.top = `${y}px`;
}
}
// ------------------------------------------------------------
// Resize and animation
// ------------------------------------------------------------
window.addEventListener("resize", () => {
camera.aspect = innerWidth / innerHeight;
camera.updateProjectionMatrix();
renderer.setPixelRatio(Math.min(devicePixelRatio, 2));
renderer.setSize(innerWidth, innerHeight);
});
const clock = new THREE.Clock();
let elapsed = 0;
function animate() {
requestAnimationFrame(animate);
const dt = Math.min(clock.getDelta(), 0.05);
elapsed += dt;
if (playing) {
cycleTime += dt;
const wave = 0.5 + 0.5 * Math.sin(cycleTime * 0.43);
setWaterProgress(wave);
}
updateCamera(dt);
updateFoam(elapsed);
updateLabels();
renderer.render(scene, camera);
}
setWaterProgress(0.5);
updateCamera(1);
animate();
</script>
</body>
</html>
```