Prompt specification
Build or customize the Three.js 3D scene "Tide around young mangrove roots" 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: Tide around young mangrove roots
Category: Biology & Nature · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/tide-around-young-mangrove-roots-x0750
Scene Description Prompt:
Raise and lower water around low-poly mangrove roots as symbolic sediment settles in sheltered areas. Qualitative behavior only. Three.js from cdnjs, one HTML file.
Working Three.js Starter (tide-around-young-mangrove-roots-x0750):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Mangrove Tides — Qualitative Sediment Study</title>
<style>
:root {
color-scheme: dark;
--ink: #e8f2e8;
--muted: #a8c2bd;
--glass: rgba(10, 30, 32, 0.66);
--glass-strong: rgba(8, 25, 27, 0.82);
--line: rgba(190, 225, 216, 0.17);
--mint: #83d5c4;
--sand: #e3b86d;
--water: #67c8c1;
}
* { box-sizing: border-box; }
html, body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background:
radial-gradient(circle at 52% 35%, #315f5d 0%, #173c40 40%, #0b2228 100%);
color: var(--ink);
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
canvas {
position: fixed;
inset: 0;
width: 100%;
height: 100%;
display: block;
touch-action: none;
cursor: grab;
}
canvas:active { cursor: grabbing; }
.glass {
border: 1px solid var(--line);
background: var(--glass);
box-shadow: 0 18px 55px rgba(0, 0, 0, 0.22);
backdrop-filter: blur(16px) saturate(120%);
-webkit-backdrop-filter: blur(16px) saturate(120%);
}
header {
position: fixed;
top: 22px;
left: 22px;
width: min(370px, calc(100vw - 44px));
padding: 18px 20px 17px;
border-radius: 18px;
pointer-events: none;
}
.eyebrow {
color: var(--mint);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.2em;
text-transform: uppercase;
}
h1 {
margin: 7px 0 6px;
font-family: Georgia, "Times New Roman", serif;
font-size: clamp(26px, 3vw, 39px);
font-weight: 500;
line-height: 1.02;
letter-spacing: -0.025em;
}
header p {
margin: 0;
max-width: 310px;
color: var(--muted);
font-size: 12px;
line-height: 1.55;
}
.status {
position: fixed;
top: 24px;
right: 22px;
display: flex;
flex-direction: column;
align-items: flex-end;
gap: 8px;
pointer-events: none;
}
.pill {
display: flex;
align-items: center;
gap: 8px;
padding: 8px 11px;
border-radius: 999px;
border: 1px solid var(--line);
background: var(--glass-strong);
color: #dcece6;
font-size: 10px;
font-weight: 750;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.dot {
width: 7px;
height: 7px;
border-radius: 50%;
background: var(--water);
box-shadow: 0 0 12px var(--water);
}
.dot.sand {
background: var(--sand);
box-shadow: 0 0 12px var(--sand);
}
.legend {
position: fixed;
right: 22px;
bottom: 155px;
width: 190px;
padding: 14px 15px;
border-radius: 15px;
pointer-events: none;
}
.legend-title {
margin-bottom: 10px;
color: var(--muted);
font-size: 9px;
font-weight: 800;
letter-spacing: 0.16em;
text-transform: uppercase;
}
.legend-row {
display: flex;
align-items: center;
gap: 9px;
margin-top: 8px;
color: #d8e8e1;
font-size: 11px;
}
.swatch {
width: 11px;
height: 11px;
border-radius: 2px;
background: var(--sand);
transform: rotate(45deg);
}
.swatch.water {
background: var(--water);
box-shadow: inset 0 0 0 1px rgba(255,255,255,.25);
}
.swatch.root {
width: 13px;
height: 8px;
border-radius: 6px 6px 2px 2px;
background: #725039;
transform: none;
}
.controls {
position: fixed;
left: 50%;
bottom: 20px;
transform: translateX(-50%);
width: min(940px, calc(100vw - 28px));
padding: 12px;
border-radius: 20px;
display: flex;
align-items: center;
gap: 9px;
flex-wrap: wrap;
}
button {
appearance: none;
border: 1px solid rgba(210, 238, 229, 0.16);
border-radius: 11px;
padding: 10px 13px;
background: rgba(219, 243, 235, 0.08);
color: #edf7f1;
font: inherit;
font-size: 11px;
font-weight: 720;
letter-spacing: 0.015em;
cursor: pointer;
transition: background 160ms ease, transform 160ms ease, border-color 160ms ease;
white-space: nowrap;
}
button:hover {
background: rgba(219, 243, 235, 0.15);
border-color: rgba(210, 238, 229, 0.31);
transform: translateY(-1px);
}
button:active { transform: translateY(0); }
button.primary {
background: rgba(107, 207, 190, 0.18);
border-color: rgba(127, 220, 201, 0.34);
color: #c7f5e8;
}
.button-pair {
display: flex;
gap: 5px;
}
.button-pair button {
padding-inline: 11px;
}
.divider {
width: 1px;
height: 30px;
background: var(--line);
margin-inline: 2px;
}
.current-control {
min-width: 190px;
display: grid;
grid-template-columns: auto 1fr auto;
align-items: center;
gap: 9px;
padding: 0 5px;
color: var(--muted);
font-size: 10px;
font-weight: 750;
letter-spacing: 0.06em;
text-transform: uppercase;
}
#currentLabel {
min-width: 43px;
color: #dcebe6;
text-align: right;
}
input[type="range"] {
width: 90px;
accent-color: var(--mint);
cursor: pointer;
}
.view-buttons {
display: flex;
gap: 5px;
margin-left: auto;
}
.view-buttons button {
padding: 9px 10px;
color: var(--muted);
}
.hint {
position: fixed;
left: 22px;
bottom: 23px;
color: rgba(221, 240, 232, 0.62);
font-size: 10px;
letter-spacing: 0.04em;
pointer-events: none;
}
.qualitative {
position: fixed;
right: 22px;
bottom: 22px;
color: rgba(221, 240, 232, 0.55);
font-size: 9px;
letter-spacing: 0.09em;
text-transform: uppercase;
pointer-events: none;
}
@media (max-width: 760px) {
header {
top: 12px;
left: 12px;
width: calc(100vw - 24px);
padding: 14px 16px;
}
header p { display: none; }
.status {
top: 126px;
right: 12px;
align-items: flex-start;
}
.pill {
padding: 7px 9px;
font-size: 8px;
}
.legend, .hint, .qualitative { display: none; }
.controls {
bottom: 10px;
width: calc(100vw - 20px);
gap: 7px;
padding: 10px;
}
.divider { display: none; }
.current-control {
order: 5;
width: 100%;
min-width: 0;
padding: 5px 3px;
}
input[type="range"] { flex: 1; width: auto; }
.view-buttons {
margin-left: 0;
}
button { font-size: 10px; }
}
</style>
</head>
<body>
<canvas id="scene" aria-label="Interactive mangrove tide and sediment scene"></canvas>
<header class="glass">
<div class="eyebrow">Tidal field notes / 01</div>
<h1>Mangrove<br />Tides</h1>
<p>
Watch water rise around tangled roots while symbolic sediment gathers in
sheltered pockets.
</p>
</header>
<div class="status" aria-live="polite">
<div class="pill"><span class="dot"></span><span id="waterState">water rising</span></div>
<div class="pill"><span class="dot sand"></span><span id="sedimentState">sediment drifting</span></div>
<div class="pill"><span id="shelterState">sheltered by roots</span></div>
</div>
<aside class="legend glass">
<div class="legend-title">Qualitative key</div>
<div class="legend-row"><span class="swatch water"></span> moving water</div>
<div class="legend-row"><span class="swatch sand"></span> symbolic sediment</div>
<div class="legend-row"><span class="swatch root"></span> mangrove roots</div>
</aside>
<section class="controls glass" aria-label="Scene controls">
<button id="tideToggle" class="primary">Pause tide</button>
<div class="button-pair">
<button id="raise" title="Raise the water level">↑ Raise water</button>
<button id="lower" title="Lower the water level">↓ Lower water</button>
</div>
<div class="divider"></div>
<label class="current-control">
<span>Current</span>
<input id="current" type="range" min="0.15" max="1" step="0.01" value="0.56" />
<span id="currentLabel">gentle</span>
</label>
<button id="release">Release sediment</button>
<div class="view-buttons">
<button id="rootView">Root view</button>
<button id="wideView">Wide view</button>
</div>
</section>
<div class="hint">Drag to orbit · Scroll to move closer</div>
<div class="qualitative">Illustrative behavior only · not a physical forecast</div>
<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 renderer = new THREE.WebGLRenderer({
canvas,
antialias: true,
alpha: 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.12;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.setClearColor(0x102c31, 0);
const scene = new THREE.Scene();
scene.fog = new THREE.FogExp2(0x173b3e, 0.026);
const camera = new THREE.PerspectiveCamera(
43,
window.innerWidth / window.innerHeight,
0.1,
120
);
const clock = new THREE.Clock();
const hemisphere = new THREE.HemisphereLight(0xc8eee2, 0x403025, 2.0);
scene.add(hemisphere);
const sun = new THREE.DirectionalLight(0xffdfad, 3.0);
sun.position.set(-9, 16, 8);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -17;
sun.shadow.camera.right = 17;
sun.shadow.camera.top = 17;
sun.shadow.camera.bottom = -17;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 45;
sun.shadow.bias = -0.0008;
scene.add(sun);
const rimLight = new THREE.DirectionalLight(0x73d7ce, 1.1);
rimLight.position.set(10, 7, -12);
scene.add(rimLight);
const barkMaterials = [
new THREE.MeshStandardMaterial({
color: 0x674934,
roughness: 0.95,
flatShading: true
}),
new THREE.MeshStandardMaterial({
color: 0x76543b,
roughness: 0.94,
flatShading: true
}),
new THREE.MeshStandardMaterial({
color: 0x503d30,
roughness: 1,
flatShading: true
})
];
const leafMaterials = [
new THREE.MeshStandardMaterial({
color: 0x315f45,
roughness: 0.9,
flatShading: true
}),
new THREE.MeshStandardMaterial({
color: 0x3f7550,
roughness: 0.92,
flatShading: true
}),
new THREE.MeshStandardMaterial({
color: 0x284e3d,
roughness: 0.95,
flatShading: true
})
];
const mudMaterial = new THREE.MeshStandardMaterial({
vertexColors: true,
roughness: 1,
flatShading: true
});
const trees = [];
const foliage = [];
const shelterPoints = [];
function terrainHeight(x, z) {
return (
-1.48 +
0.11 * Math.sin(x * 0.43) * Math.cos(z * 0.37) +
0.065 * Math.sin((x + z) * 0.77) +
0.025 * Math.cos(x * 1.1 - z * 0.4)
);
}
function randomGenerator(seed) {
return function () {
let t = seed += 0x6D2B79F5;
t = Math.imul(t ^ t >>> 15, t | 1);
t ^= t + Math.imul(t ^ t >>> 7, t | 61);
return ((t ^ t >>> 14) >>> 0) / 4294967296;
};
}
function cylinderBetween(a, b, bottomRadius, topRadius, material, sides = 7) {
const direction = new THREE.Vector3().subVectors(b, a);
const length = direction.length();
const geometry = new THREE.CylinderGeometry(
topRadius,
bottomRadius,
length,
sides,
1,
false
);
const mesh = new THREE.Mesh(geometry, material);
mesh.position.copy(a).add(b).multiplyScalar(0.5);
mesh.quaternion.setFromUnitVectors(
new THREE.Vector3(0, 1, 0),
direction.normalize()
);
mesh.castShadow = true;
mesh.receiveShadow = true;
return mesh;
}
function buildTerrain() {
const geometry = new THREE.PlaneGeometry(54, 54, 28, 28);
geometry.rotateX(-Math.PI / 2);
const positions = geometry.attributes.position;
const colors = [];
const dark = new THREE.Color(0x3a3027);
const light = new THREE.Color(0x806448);
const green = new THREE.Color(0x455344);
const color = new THREE.Color();
for (let i = 0; i < positions.count; i++) {
const x = positions.getX(i);
const z = positions.getZ(i);
const y = terrainHeight(x, z);
positions.setY(i, y);
const variation = THREE.MathUtils.clamp(
0.48 +
0.24 * Math.sin(x * 0.8 + z * 0.37) +
0.12 * Math.cos(z * 1.17),
0,
1
);
color.copy(dark).lerp(light, variation);
const algae = THREE.MathUtils.clamp(
0.4 + 0.6 * Math.sin(x * 0.31 - z * 0.27),
0,
1
);
color.lerp(green, algae * 0.12);
colors.push(color.r, color.g, color.b);
}
geometry.setAttribute("color", new THREE.Float32BufferAttribute(colors, 3));
geometry.computeVertexNormals();
const terrain = new THREE.Mesh(geometry, mudMaterial);
terrain.receiveShadow = true;
scene.add(terrain);
}
function createMangrove(x, z, scale, seed) {
const random = randomGenerator(seed);
const treeIndex = trees.length;
const baseY = terrainHeight(x, z);
const bark = barkMaterials[treeIndex % barkMaterials.length];
const tree = {
x,
z,
scale,
baseY,
deposits: 0
};
trees.push(tree);
const p0 = new THREE.Vector3(x, baseY + 0.02, z);
const p1 = new THREE.Vector3(
x + (random() - 0.5) * 0.3 * scale,
baseY + 1.45 * scale,
z + (random() - 0.5) * 0.3 * scale
);
const p2 = new THREE.Vector3(
x + (random() - 0.5) * 0.45 * scale,
baseY + 2.65 * scale,
z + (random() - 0.5) * 0.45 * scale
);
const p3 = new THREE.Vector3(
x + (random() - 0.5) * 0.8 * scale,
baseY + 3.65 * scale,
z + (random() - 0.5) * 0.8 * scale
);
scene.add(
cylinderBetween(p0, p1, 0.34 * scale, 0.25 * scale, bark),
cylinderBetween(p1, p2, 0.25 * scale, 0.16 * scale, bark),
cylinderBetween(p2, p3, 0.16 * scale, 0.07 * scale, bark)
);
const rootCount = 7;
for (let i = 0; i < rootCount; i++) {
const angle = i / rootCount * Math.PI * 2 + (random() - 0.5) * 0.32;
const reach = (1.65 + random() * 1.0) * scale;
const cos = Math.cos(angle);
const sin = Math.sin(angle);
const r0 = new THREE.Vector3(
x + cos * 0.12 * scale,
baseY + (0.88 + random() * 0.2) * scale,
z + sin * 0.12 * scale
);
const r1 = new THREE.Vector3(
x + cos * 0.58 * scale,
baseY + (0.53 + random() * 0.16) * scale,
z + sin * 0.58 * scale
);
const r2x = x + cos * 1.22 * scale;
const r2z = z + sin * 1.22 * scale;
const r2 = new THREE.Vector3(
r2x,
Math.max(baseY + 0.12 * scale, terrainHeight(r2x, r2z) + 0.34 * scale),
r2z
);
const r3x = x + cos * reach;
const r3z = z + sin * reach;
const r3 = new THREE.Vector3(
r3x,
terrainHeight(r3x, r3z) + 0.045,
r3z
);
scene.add(
cylinderBetween(r0, r1, 0.13 * scale, 0.105 * scale, bark, 6),
cylinderBetween(r1, r2, 0.105 * scale, 0.07 * scale, bark, 6),
cylinderBetween(r2, r3, 0.07 * scale, 0.025 * scale, bark, 6)
);
shelterPoints.push(
{ x: r1.x, z: r1.z, treeIndex },
{ x: r2.x, z: r2.z, treeIndex },
{ x: r3.x, z: r3.z, treeIndex }
);
if (random() > 0.28) {
const bx = x + cos * reach * 0.72 - sin * 0.62 * scale;
const bz = z + sin * reach * 0.72 + cos * 0.62 * scale;
const branchEnd = new THREE.Vector3(
bx,
terrainHeight(bx, bz) + 0.035,
bz
);
scene.add(
cylinderBetween(r2, branchEnd, 0.055 * scale, 0.018 * scale, bark, 5)
);
shelterPoints.push({ x: bx, z: bz, treeIndex });
}
}
for (let i = 0; i < 5; i++) {
const angle = i / 5 * Math.PI * 2 + random() * 0.7;
const start = p2.clone().lerp(p3, random() * 0.55);
const end = new THREE.Vector3(
x + Math.cos(angle) * (1.1 + random() * 1.2) * scale,
baseY + (3.85 + random() * 1.25) * scale,
z + Math.sin(angle) * (1.1 + random() * 1.2) * scale
);
scene.add(
cylinderBetween(start, end, 0.095 * scale, 0.035 * scale, bark, 6)
);
for (let j = 0; j < 2; j++) {
const geometry = new THREE.IcosahedronGeometry(1, 0);
const mesh = new THREE.Mesh(
geometry,
leafMaterials[(treeIndex + i + j) % leafMaterials.length]
);
mesh.position.copy(end);
mesh.position.x += (random() - 0.5) * 0.65 * scale;
mesh.position.y += (random() - 0.2) * 0.5 * scale;
mesh.position.z += (random() - 0.5) * 0.65 * scale;
mesh.scale.set(
(0.85 + random() * 0.55) * scale,
(0.55 + random() * 0.32) * scale,
(0.8 + random() * 0.55) * scale
);
mesh.rotation.set(random(), random(), random());
mesh.castShadow = true;
mesh.receiveShadow = true;
mesh.userData.phase = random() * Math.PI * 2;
foliage.push(mesh);
scene.add(mesh);
}
}
return tree;
}
buildTerrain();
createMangrove(-5.5, -1.7, 1.04, 12);
createMangrove(2.2, 2.6, 1.16, 88);
createMangrove(5.7, -3.6, 0.83, 31);
createMangrove(-1.5, 5.6, 0.93, 57);
createMangrove(-8.0, 4.1, 0.72, 5);
const waterGeometry = new THREE.PlaneGeometry(54, 54, 36, 36);
waterGeometry.rotateX(-Math.PI / 2);
const waterUniforms = {
uTime: { value: 0 },
uFlow: { value: new THREE.Vector2(1, 0.25).normalize() },
uDeep: { value: new THREE.Color(0x0b4f59) },
uShallow: { value: new THREE.Color(0x59b8ac) },
uSky: { value: new THREE.Color(0xbfe8db) }
};
const waterMaterial = new THREE.ShaderMaterial({
uniforms: waterUniforms,
transparent: true,
depthWrite: false,
side: THREE.DoubleSide,
vertexShader: `
uniform float uTime;
uniform vec2 uFlow;
varying vec3 vWorldPosition;
varying float vWave;
void main() {
vec3 p = position;
float flowPhase = dot(p.xz, uFlow) * 1.25;
float waveA = sin(p.x * 0.63 + uTime * 0.70 + flowPhase) * 0.065;
float waveB = sin(p.z * 0.48 - uTime * 0.43 - flowPhase * 0.7) * 0.055;
float waveC = sin((p.x + p.z) * 0.31 + uTime * 0.24) * 0.028;
p.y += waveA + waveB + waveC;
vWave = waveA + waveB + waveC;
vec4 worldPosition = modelMatrix * vec4(p, 1.0);
vWorldPosition = worldPosition.xyz;
gl_Position = projectionMatrix * viewMatrix * worldPosition;
}
`,
fragmentShader: `
uniform float uTime;
uniform vec2 uFlow;
uniform vec3 uDeep;
uniform vec3 uShallow;
uniform vec3 uSky;
varying vec3 vWorldPosition;
varying float vWave;
void main() {
float dx =
cos(vWorldPosition.x * 0.63 + uTime * 0.70 +
dot(vWorldPosition.xz, uFlow) * 1.25) * 0.04095 +
cos((vWorldPosition.x + vWorldPosition.z) * 0.31 +
uTime * 0.24) * 0.00868;
float dz =
cos(vWorldPosition.z * 0.48 - uTime * 0.43 -
dot(vWorldPosition.xz, uFlow) * 0.875) * 0.0264 +
cos((vWorldPosition.x + vWorldPosition.z) * 0.31 +
uTime * 0.24) * 0.00868;
vec3 normal = normalize(vec3(-dx, 1.0, -dz));
vec3 viewDirection = normalize(cameraPosition - vWorldPosition);
float fresnel = pow(1.0 - max(dot(normal, viewDirection), 0.0), 2.2);
float movingBand = sin(
dot(vWorldPosition.xz, uFlow) * 2.4 -
uTime * 0.85 +
sin(vWorldPosition.x * 0.22 + vWorldPosition.z * 0.18)
);
movingBand = pow(max(movingBand, 0.0), 9.0);
float facet = 0.5 + 0.5 * sin(
vWorldPosition.x * 0.74 +
vWorldPosition.z * 0.61 +
vWave * 17.0
);
vec3 color = mix(uDeep, uShallow, 0.32 + facet * 0.18);
color = mix(color, uSky, fresnel * 0.42);
color += movingBand * vec3(0.045, 0.085, 0.075);
float alpha = 0.57 + fresnel * 0.20 + movingBand * 0.035;
gl_FragColor = vec4(color, alpha);
}
`
});
const water = new THREE.Mesh(waterGeometry, waterMaterial);
water.renderOrder = 3;
water.frustumCulled = false;
scene.add(water);
const MAX_PARTICLES = 230;
const MAX_SETTLED = 480;
const particles = [];
const suspendedPositions = new Float32Array(MAX_PARTICLES * 3);
const suspendedColors = new Float32Array(MAX_PARTICLES * 3);
const suspendedGeometry = new THREE.BufferGeometry();
suspendedGeometry.setAttribute(
"position",
new THREE.BufferAttribute(suspendedPositions, 3)
);
suspendedGeometry.setAttribute(
"color",
new THREE.BufferAttribute(suspendedColors, 3)
);
suspendedGeometry.setDrawRange(0, 0);
const suspendedMaterial = new THREE.PointsMaterial({
size: 0.082,
sizeAttenuation: true,
vertexColors: true,
transparent: true,
opacity: 0.94,
depthWrite: false
});
const suspendedSediment = new THREE.Points(
suspendedGeometry,
suspendedMaterial
);
suspendedSediment.renderOrder = 4;
suspendedSediment.frustumCulled = false;
scene.add(suspendedSediment);
const settledPositions = new Float32Array(MAX_SETTLED * 3);
const settledColors = new Float32Array(MAX_SETTLED * 3);
const settledGeometry = new THREE.BufferGeometry();
settledGeometry.setAttribute(
"position",
new THREE.BufferAttribute(settledPositions, 3)
);
settledGeometry.setAttribute(
"color",
new THREE.BufferAttribute(settledColors, 3)
);
settledGeometry.setDrawRange(0, 0);
const settledMaterial = new THREE.PointsMaterial({
size: 0.073,
sizeAttenuation: true,
vertexColors: true,
transparent: true,
opacity: 0.98,
depthWrite: false
});
const settledSediment = new THREE.Points(
settledGeometry,
settledMaterial
);
settledSediment.renderOrder = 2;
settledSediment.frustumCulled = false;
scene.add(settledSediment);
const moundGeometry = new THREE.IcosahedronGeometry(1, 0);
const moundMaterial = new THREE.MeshStandardMaterial({
color: 0x725438,
roughness: 1,
flatShading: true
});
const sedimentMounds = new THREE.InstancedMesh(
moundGeometry,
moundMaterial,
trees.length
);
sedimentMounds.castShadow = true;
sedimentMounds.receiveShadow = true;
scene.add(sedimentMounds);
const flowDirection = new THREE.Vector2(1, 0.25).normalize();
let flowAngle = Math.atan2(flowDirection.y, flowDirection.x);
const tide = {
auto: true,
phase: 0.25,
level: 0,
manualLevel: 0
};
let currentEnergy = 0.56;
let recentSettles = 0;
let settledCount = 0;
const sedimentGold = new THREE.Color(0xe1ad58);
const sedimentLight = new THREE.Color(0xf3cf83);
const sedimentDark = new THREE.Color(0xa96f35);
function nearestShelter(x, z) {
let bestDistance = Infinity;
let bestTree = -1;
for (const point of shelterPoints) {
const dx = x - point.x;
const dz = z - point.z;
const distance = dx * dx + dz * dz;
if (distance < bestDistance) {
bestDistance = distance;
bestTree = point.treeIndex;
}
}
return {
distance: Math.sqrt(bestDistance),
treeIndex: bestTree
};
}
function spawnParticle() {
if (particles.length >= MAX_PARTICLES) return;
const distance = 12 + Math.random() * 8;
const lateral = (Math.random() - 0.5) * 15;
const x = -flowDirection.x * distance - flowDirection.y * lateral;
const z = -flowDirection.y * distance + flowDirection.x * lateral;
particles.push({
x,
y: terrainHeight(x, z) + 0.25 + Math.random() * 2.0,
z,
vx: 0,
vy: 0,
vz: 0,
age: 0,
life: 18 + Math.random() * 15,
phase: Math.random() * Math.PI * 2
});
}
function addSettledParticle(x, z, treeIndex = -1) {
if (settledCount >= MAX_SETTLED) return;
const index = settledCount++;
const y = terrainHeight(x, z) + 0.04;
settledPositions[index * 3] = x;
settledPositions[index * 3 + 1] = y;
settledPositions[index * 3 + 2] = z;
const color = sedimentDark.clone().lerp(
sedimentGold,
0.35 + Math.random() * 0.65
);
settledColors[index * 3] = color.r;
settledColors[index * 3 + 1] = color.g;
settledColors[index * 3 + 2] = color.b;
if (treeIndex >= 0 && trees[treeIndex]) {
trees[treeIndex].deposits++;
}
}
function settleParticle(particle, treeIndex) {
addSettledParticle(particle.x, particle.z, treeIndex);
recentSettles++;
}
function updateParticles(dt) {
const tidalCurrent = 0.42 + 0.58 * Math.abs(Math.cos(tide.phase));
const baseSpeed = 0.12 + currentEnergy * 0.76 * tidalCurrent;
for (let i = particles.length - 1; i >= 0; i--) {
const particle = particles[i];
particle.age += dt;
const shelter = nearestShelter(particle.x, particle.z);
const rootProximity = THREE.MathUtils.clamp(
1 - shelter.distance / 2.0,
0,
1
);
const localCurrent = 1 - rootProximity * 0.9;
const desiredVX = flowDirection.x * baseSpeed * localCurrent;
const desiredVZ = flowDirection.y * baseSpeed * localCurrent;
const blend = 1 - Math.exp(-dt * 2.4);
particle.vx += (desiredVX - particle.vx) * blend;
particle.vz += (desiredVZ - particle.vz) * blend;
particle.vx += Math.sin(particle.age * 1.4 + particle.phase) * dt * 0.014;
particle.vz += Math.cos(particle.age * 1.1 + particle.phase) * dt * 0.014;
particle.x += particle.vx * dt;
particle.z += particle.vz * dt;
const settlingRate =
0.042 +
(1 - localCurrent) * 0.11 +
(rootProximity > 0.2 ? 0.035 : 0);
particle.y -= settlingRate * dt;
const bed = terrainHeight(particle.x, particle.z);
const exposed = tide.level - bed < 0.065;
if (particle.y <= bed + 0.055) {
if (rootProximity > 0.28 || exposed) {
settleParticle(particle, shelter.treeIndex);
particles.splice(i, 1);
continue;
}
particle.y = bed + 0.057;
particle.vy = 0.006;
}
if (
particle.age > particle.life ||
Math.abs(particle.x) > 27 ||
Math.abs(particle.z) > 27
) {
const replacement = particles[i];
const distance = 12 + Math.random() * 8;
const lateral = (Math.random() - 0.5) * 15;
replacement.x = -flowDirection.x * distance - flowDirection.y * lateral;
replacement.z = -flowDirection.y * distance + flowDirection.x * lateral;
replacement.y = terrainHeight(replacement.x, replacement.z) + 0.3 + Math.random();
replacement.age = 0;
replacement.life = 18 + Math.random() * 15;
replacement.vx = 0;
replacement.vz = 0;
}
}
}
function renderParticles() {
const count = Math.min(particles.length, MAX_PARTICLES);
for (let i = 0; i < count; i++) {
const particle = particles[i];
suspendedPositions[i * 3] = particle.x;
suspendedPositions[i * 3 + 1] = particle.y;
suspendedPositions[i * 3 + 2] = particle.z;
const shelter = nearestShelter(particle.x, particle.z);
const proximity = THREE.MathUtils.clamp(
1 - shelter.distance / 2.0,
0,
1
);
const color = sedimentGold.clone().lerp(
sedimentLight,
proximity * 0.65
);
suspendedColors[i * 3] = color.r;
suspendedColors[i * 3 + 1] = color.g;
suspendedColors[i * 3 + 2] = color.b;
}
suspendedGeometry.setDrawRange(0, count);
suspendedGeometry.attributes.position.needsUpdate = true;
suspendedGeometry.attributes.color.needsUpdate = true;
settledGeometry.setDrawRange(0, settledCount);
settledGeometry.attributes.position.needsUpdate = true;
settledGeometry.attributes.color.needsUpdate = true;
}
const dummy = new THREE.Object3D();
function updateMounds() {
trees.forEach((tree, index) => {
const amount = Math.min(tree.deposits, 100);
dummy.position.set(
tree.x,
terrainHeight(tree.x, tree.z) + 0.045,
tree.z
);
dummy.scale.set(
0.20 + amount * 0.009,
0.035 + amount * 0.0028,
0.20 + amount * 0.009
);
dummy.rotation.y = index * 0.7;
dummy.updateMatrix();
sedimentMounds.setMatrixAt(index, dummy.matrix);
});
sedimentMounds.instanceMatrix.needsUpdate = true;
}
for (let i = 0; i < 120; i++) {
spawnParticle();
}
for (let i = 0; i < 28; i++) {
const tree = trees[i % trees.length];
const particle = particles[i];
particle.x = tree.x - flowDirection.x * 2.6 + (Math.random() - 0.5);
particle.z = tree.z - flowDirection.y * 2.6 + (Math.random() - 0.5);
particle.y = terrainHeight(particle.x, particle.z) + 0.2 + Math.random() * 0.9;
}
trees.forEach((tree) => {
for (let i = 0; i < 12; i++) {
const angle = Math.random() * Math.PI * 2;
const radius = 0.45 + Math.random() * 1.0;
const x = tree.x + Math.cos(angle) * radius;
const z = tree.z + Math.sin(angle) * radius;
addSettledParticle(x, z, trees.indexOf(tree));
}
});
updateMounds();
const cameraState = {
target: new THREE.Vector3(0, 1.0, 0),
desiredTarget: new THREE.Vector3(0, 1.0, 0),
theta: 0.72,
desiredTheta: 0.72,
phi: 1.02,
desiredPhi: 1.02,
radius: 18.5,
desiredRadius: 18.5
};
function updateCamera(dt) {
const smoothing = 1 - Math.exp(-dt * 8);
cameraState.theta +=
(cameraState.desiredTheta - cameraState.theta) * smoothing;
cameraState.phi +=
(cameraState.desiredPhi - cameraState.phi) * smoothing;
cameraState.radius +=
(cameraState.desiredRadius - cameraState.radius) * smoothing;
cameraState.target.lerp(
cameraState.desiredTarget,
smoothing
);
const sinPhi = Math.sin(cameraState.phi);
camera.position.set(
cameraState.target.x +
cameraState.radius * sinPhi * Math.sin(cameraState.theta),
cameraState.target.y +
cameraState.radius * Math.cos(cameraState.phi),
cameraState.target.z +
cameraState.radius * sinPhi * Math.cos(cameraState.theta)
);
camera.lookAt(cameraState.target);
}
let dragging = false;
let previousX = 0;
let previousY = 0;
canvas.addEventListener("pointerdown", (event) => {
dragging = true;
previousX = event.clientX;
previousY = event.clientY;
canvas.setPointerCapture(event.pointerId);
});
canvas.addEventListener("pointermove", (event) => {
if (!dragging) return;
const dx = event.clientX - previousX;
const dy = event.clientY - previousY;
previousX = event.clientX;
previousY = event.clientY;
cameraState.desiredTheta -= dx * 0.006;
cameraState.desiredPhi = THREE.MathUtils.clamp(
cameraState.desiredPhi - dy * 0.005,
0.38,
1.43
);
});
function stopDragging(event) {
dragging = false;
if (event.pointerId !== undefined && canvas.hasPointerCapture(event.pointerId)) {
canvas.releasePointerCapture(event.pointerId);
}
}
canvas.addEventListener("pointerup", stopDragging);
canvas.addEventListener("pointercancel", stopDragging);
canvas.addEventListener(
"wheel",
(event) => {
event.preventDefault();
cameraState.desiredRadius = THREE.MathUtils.clamp(
cameraState.desiredRadius * Math.exp(event.deltaY * 0.001),
8.5,
31
);
},
{ passive: false }
);
const waterState = document.querySelector("#waterState");
const sedimentState = document.querySelector("#sedimentState");
const shelterState = document.querySelector("#shelterState");
const tideToggle = document.querySelector("#tideToggle");
const currentSlider = document.querySelector("#current");
const currentLabel = document.querySelector("#currentLabel");
function updateCurrentLabel() {
if (currentEnergy < 0.34) currentLabel.textContent = "slack";
else if (currentEnergy < 0.72) currentLabel.textContent = "gentle";
else currentLabel.textContent = "brisk";
}
currentSlider.addEventListener("input", () => {
currentEnergy = Number(currentSlider.value);
updateCurrentLabel();
});
tideToggle.addEventListener("click", () => {
tide.auto = !tide.auto;
if (!tide.auto) {
tide.manualLevel = tide.level;
}
tideToggle.textContent = tide.auto ? "Pause tide" : "Resume tide";
});
document.querySelector("#raise").addEventListener("click", () => {
tide.auto = false;
tide.manualLevel = THREE.MathUtils.clamp(tide.level + 0.28, -0.95, 1.1);
tideToggle.textContent = "Resume tide";
});
document.querySelector("#lower").addEventListener("click", () => {
tide.auto = false;
tide.manualLevel = THREE.MathUtils.clamp(tide.level - 0.28, -0.95, 1.1);
tideToggle.textContent = "Resume tide";
});
document.querySelector("#release").addEventListener("click", () => {
const burst = Math.min(48, MAX_PARTICLES - particles.length);
for (let i = 0; i < burst; i++) {
spawnParticle();
}
});
document.querySelector("#rootView").addEventListener("click", () => {
cameraState.desiredTarget.set(0, 0.05, 0);
cameraState.desiredRadius = 12.2;
cameraState.desiredPhi = 1.27;
cameraState.desiredTheta = 0.83;
});
document.querySelector("#wideView").addEventListener("click", () => {
cameraState.desiredTarget.set(0, 1.1, 0);
cameraState.desiredRadius = 19.5;
cameraState.desiredPhi = 1.0;
cameraState.desiredTheta = 0.72;
});
let uiTimer = 0;
let moundTimer = 0;
function updateStatus() {
if (tide.auto) {
waterState.textContent =
Math.cos(tide.phase) >= 0 ? "water rising" : "water falling";
} else {
waterState.textContent = "water held";
}
if (recentSettles > 0) {
sedimentState.textContent = "sediment settling";
} else if (particles.length > 0) {
sedimentState.textContent = "sediment drifting";
} else {
sedimentState.textContent = "sediment resting";
}
shelterState.textContent =
settledCount > 0 ? "sheltered by roots" : "roots intercept the current";
}
function resize() {
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
}
window.addEventListener("resize", resize);
function animate() {
const dt = Math.min(clock.getDelta(), 0.05);
const elapsed = clock.elapsedTime;
if (tide.auto) {
tide.phase += dt * 0.13;
}
const targetLevel = tide.auto
? -0.12 + Math.sin(tide.phase) * 0.72
: tide.manualLevel;
tide.level +=
(targetLevel - tide.level) * (1 - Math.exp(-dt * 1.65));
water.position.y = tide.level;
waterUniforms.uTime.value = elapsed;
flowAngle += dt * (0.004 + currentEnergy * 0.006);
flowDirection.set(Math.cos(flowAngle), Math.sin(flowAngle));
waterUniforms.uFlow.value.copy(flowDirection);
updateParticles(dt);
renderParticles();
foliage.forEach((leaf) => {
leaf.rotation.z =
Math.sin(elapsed * 0.62 + leaf.userData.phase) * 0.012;
leaf.rotation.x =
Math.cos(elapsed * 0.49 + leaf.userData.phase) * 0.008;
});
recentSettles = Math.max(0, recentSettles - dt * 0.75);
moundTimer += dt;
if (moundTimer > 0.25) {
updateMounds();
moundTimer = 0;
}
uiTimer += dt;
if (uiTimer > 0.15) {
updateStatus();
uiTimer = 0;
}
updateCamera(dt);
renderer.render(scene, camera);
}
updateCurrentLabel();
updateCamera(1);
renderer.setAnimationLoop(animate);
</script>
</body>
</html>
```