Prompt specification
Build or customize the Three.js 3D scene "Mangrove Root Structure Above Tidal Ground" 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: Mangrove Root Structure Above Tidal Ground
Category: Everyday & Industrial · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/mangrove-root-structure-above-tidal-ground-x3921
Scene Description Prompt:
Create a standalone stylized scene in one HTML file using Three.js from cdnjs. Model source-informed mangrove root forms appropriate to a specified example species. Distinguish exposed roots, trunk, water level, and sediment without combining incompatible species traits.
Working Three.js Starter (mangrove-root-structure-above-tidal-ground-x3921):
```html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Red Mangrove — Rhizophora mangle</title>
<style>
:root {
--ink: #183b35;
--muted: #547069;
--roots: #a66d45;
--trunk: #654336;
--water: #2f9fb5;
--sediment: #b99262;
--paper: rgba(250, 252, 247, 0.92);
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #d5ebe7;
color: var(--ink);
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
#scene {
position: fixed;
inset: 0;
}
canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
cursor: grab;
}
canvas:active {
cursor: grabbing;
}
.title-panel,
.legend,
.source-panel,
.help {
position: fixed;
z-index: 10;
border: 1px solid rgba(255, 255, 255, 0.68);
background: var(--paper);
box-shadow: 0 12px 35px rgba(21, 60, 55, 0.12);
backdrop-filter: blur(12px);
-webkit-backdrop-filter: blur(12px);
}
.title-panel {
top: 20px;
left: 20px;
width: min(350px, calc(100vw - 40px));
padding: 17px 19px 16px;
border-radius: 18px;
pointer-events: none;
}
.eyebrow {
margin-bottom: 7px;
color: #538277;
font-size: 10px;
font-weight: 800;
letter-spacing: 0.15em;
text-transform: uppercase;
}
h1 {
margin: 0;
font-family: Georgia, "Times New Roman", serif;
font-size: clamp(27px, 4vw, 38px);
font-weight: 500;
line-height: 1;
letter-spacing: -0.025em;
}
.species {
margin: 7px 0 0;
color: var(--muted);
font-family: Georgia, "Times New Roman", serif;
font-size: 16px;
}
.species em {
color: var(--ink);
}
.badge {
display: inline-flex;
align-items: center;
margin-top: 12px;
padding: 5px 9px;
border: 1px solid #c9dcd5;
border-radius: 999px;
background: rgba(230, 242, 236, 0.75);
color: #3e6a5f;
font-size: 10px;
font-weight: 750;
letter-spacing: 0.07em;
text-transform: uppercase;
}
.help {
top: 20px;
right: 20px;
padding: 10px 13px;
border-radius: 12px;
color: #44675f;
font-size: 11px;
pointer-events: none;
}
.help strong {
color: var(--ink);
}
#reset-view {
position: fixed;
z-index: 15;
top: 67px;
right: 20px;
padding: 8px 12px;
border: 1px solid rgba(255, 255, 255, 0.72);
border-radius: 10px;
background: rgba(250, 252, 247, 0.9);
color: var(--ink);
font: inherit;
font-size: 11px;
font-weight: 700;
cursor: pointer;
box-shadow: 0 7px 20px rgba(21, 60, 55, 0.1);
}
#reset-view:hover {
background: white;
}
.legend {
bottom: 20px;
left: 20px;
padding: 13px 15px;
border-radius: 15px;
pointer-events: none;
}
.legend-title {
margin-bottom: 9px;
color: #6b817a;
font-size: 9px;
font-weight: 800;
letter-spacing: 0.13em;
text-transform: uppercase;
}
.legend-items {
display: grid;
grid-template-columns: repeat(2, auto);
gap: 7px 16px;
}
.legend-item {
display: flex;
align-items: center;
gap: 7px;
color: #35564f;
font-size: 11px;
white-space: nowrap;
}
.swatch {
width: 16px;
height: 5px;
border-radius: 4px;
}
.swatch.roots { background: var(--roots); }
.swatch.trunk { background: var(--trunk); }
.swatch.water { background: var(--water); }
.swatch.sediment { background: var(--sediment); }
.source-panel {
right: 20px;
bottom: 20px;
width: min(325px, calc(100vw - 40px));
padding: 0;
border-radius: 14px;
color: #3c5c55;
font-size: 11px;
line-height: 1.45;
}
.source-panel summary {
padding: 11px 14px;
color: var(--ink);
font-weight: 750;
cursor: pointer;
list-style: none;
}
.source-panel summary::-webkit-details-marker {
display: none;
}
.source-panel summary::after {
content: "+";
float: right;
color: #6e8d84;
font-size: 16px;
line-height: 13px;
}
.source-panel[open] summary::after {
content: "−";
}
.source-content {
padding: 0 14px 13px;
}
.source-content p {
margin: 0 0 8px;
}
.source-content p:last-child {
margin-bottom: 0;
}
.source-content em {
color: var(--ink);
}
#leaders {
position: fixed;
z-index: 5;
inset: 0;
width: 100%;
height: 100%;
overflow: visible;
pointer-events: none;
}
#leaders line {
stroke-width: 1.25;
stroke-dasharray: 3 3;
opacity: 0.76;
}
.label {
position: fixed;
z-index: 6;
min-width: 116px;
max-width: 165px;
padding: 8px 10px 8px 12px;
border: 1px solid rgba(255, 255, 255, 0.8);
border-left-width: 4px;
border-radius: 10px;
background: rgba(250, 252, 247, 0.91);
box-shadow: 0 8px 24px rgba(21, 60, 55, 0.11);
color: #294b44;
font-size: 11px;
font-weight: 720;
line-height: 1.15;
pointer-events: none;
transform: translate(-50%, -50%);
white-space: nowrap;
}
.label small {
display: block;
margin-top: 3px;
color: #71847e;
font-size: 9px;
font-weight: 500;
white-space: normal;
}
#label-roots { border-left-color: var(--roots); }
#label-trunk { border-left-color: var(--trunk); }
#label-water { border-left-color: var(--water); }
#label-sediment { border-left-color: var(--sediment); }
@media (max-width: 700px) {
.title-panel {
top: 12px;
left: 12px;
padding: 13px 15px;
}
.help {
top: 12px;
right: 12px;
max-width: 130px;
line-height: 1.35;
}
#reset-view {
top: 62px;
right: 12px;
}
.legend {
bottom: 12px;
left: 12px;
padding: 10px 12px;
}
.legend-items {
gap: 5px 10px;
}
.legend-item {
font-size: 10px;
}
.source-panel {
right: 12px;
bottom: 12px;
width: 220px;
}
.label {
min-width: 92px;
max-width: 125px;
padding: 6px 8px;
font-size: 9px;
}
.label small {
font-size: 8px;
}
}
</style>
</head>
<body>
<main id="scene" aria-label="Stylized red mangrove diorama"></main>
<header class="title-panel">
<div class="eyebrow">Botanical diorama · Intertidal mangrove</div>
<h1>Red mangrove</h1>
<p class="species"><em>Rhizophora mangle</em> L.</p>
<span class="badge">Specified example species</span>
</header>
<div class="help">
<strong>Drag</strong> to orbit · <strong>Scroll / pinch</strong> to zoom
</div>
<button id="reset-view" type="button">Reset view</button>
<aside class="legend" aria-label="Scene legend">
<div class="legend-title">Visual key</div>
<div class="legend-items">
<div class="legend-item"><span class="swatch roots"></span>Exposed roots</div>
<div class="legend-item"><span class="swatch trunk"></span>Trunk</div>
<div class="legend-item"><span class="swatch water"></span>Water level</div>
<div class="legend-item"><span class="swatch sediment"></span>Sediment</div>
</div>
</aside>
<details class="source-panel">
<summary>Species & botanical basis</summary>
<div class="source-content">
<p>
The model uses the characteristic arching stilt/prop roots of
<em>Rhizophora</em>, a branching woody trunk, and an evergreen crown.
</p>
<p>
It intentionally omits the dense upright pneumatophores associated
with black mangroves such as <em>Avicennia</em>, avoiding mixed-species traits.
</p>
<p>
Orientation: P. B. Tomlinson, <em>The Botany of Mangroves</em> (1986);
taxonomic nomenclature: Kew Plants of the World Online.
This is a stylized interpretation, not a measured reconstruction.
</p>
</div>
</details>
<svg id="leaders" aria-hidden="true"></svg>
<div id="label-roots" class="label">
Exposed prop roots
<small>Arching stilt roots descending into the substrate</small>
</div>
<div id="label-trunk" class="label">
Woody trunk
<small>Continuous central stem and branching crown</small>
</div>
<div id="label-water" class="label">
Water level
<small>Translucent intertidal reference plane</small>
</div>
<div id="label-sediment" class="label">
Sediment
<small>Rooted substrate at the tidal margin</small>
</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 host = document.getElementById("scene");
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xd4eae6);
scene.fog = new THREE.Fog(0xd4eae6, 13, 31);
const camera = new THREE.PerspectiveCamera(
42,
window.innerWidth / window.innerHeight,
0.1,
80
);
const renderer = new THREE.WebGLRenderer({
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.05;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.domElement.setAttribute(
"aria-label",
"Interactive stylized model of a red mangrove with exposed prop roots"
);
host.appendChild(renderer.domElement);
const hemi = new THREE.HemisphereLight(0xe7f7ef, 0x5c5742, 2.0);
scene.add(hemi);
const sun = new THREE.DirectionalLight(0xfff2d6, 3.2);
sun.position.set(-4.5, 9, 5.5);
sun.castShadow = true;
sun.shadow.mapSize.set(1024, 1024);
sun.shadow.camera.left = -6;
sun.shadow.camera.right = 6;
sun.shadow.camera.top = 7;
sun.shadow.camera.bottom = -3;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 22;
sun.shadow.bias = -0.0004;
sun.target.position.set(0, 1.4, 0);
scene.add(sun, sun.target);
const fill = new THREE.DirectionalLight(0xaedee0, 1.0);
fill.position.set(5, 4, -5);
scene.add(fill);
// -------------------------------------------------------------------------
// Deterministic helpers
// -------------------------------------------------------------------------
function mulberry32(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;
};
}
const random = mulberry32(24071984);
function sedimentHeight(x, z) {
const r = Math.sqrt((x / 4.3) ** 2 + (z / 3.35) ** 2);
return (
0.16 -
0.31 * r * r +
Math.sin(x * 3.1 + z * 1.7) * 0.015 +
Math.cos(x * 1.9 - z * 2.3) * 0.010
);
}
// Create a tube with smoothly decreasing radius.
function taperedTube(
curve,
material,
tubularSegments = 32,
radialSegments = 8,
radiusStart = 0.1,
radiusEnd = 0.03
) {
const frames = curve.computeFrenetFrames(tubularSegments, false);
const positions = [];
const normals = [];
const indices = [];
for (let i = 0; i <= tubularSegments; i++) {
const t = i / tubularSegments;
const center = curve.getPointAt(t);
const radius = THREE.MathUtils.lerp(radiusStart, radiusEnd, t);
for (let j = 0; j < radialSegments; j++) {
const v = (j / radialSegments) * Math.PI * 2;
const sin = Math.sin(v);
const cos = -Math.cos(v);
const normal = frames.normals[i]
.clone()
.multiplyScalar(cos)
.addScaledVector(frames.binormals[i], sin)
.normalize();
const point = center.clone().addScaledVector(normal, radius);
positions.push(point.x, point.y, point.z);
normals.push(normal.x, normal.y, normal.z);
}
}
for (let i = 0; i < tubularSegments; i++) {
for (let j = 0; j < radialSegments; j++) {
const a = i * radialSegments + j;
const b = i * radialSegments + ((j + 1) % radialSegments);
const c = (i + 1) * radialSegments + j;
const d = (i + 1) * radialSegments + ((j + 1) % radialSegments);
indices.push(a, b, c, b, d, c);
}
}
const geometry = new THREE.BufferGeometry();
geometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(positions, 3)
);
geometry.setAttribute(
"normal",
new THREE.Float32BufferAttribute(normals, 3)
);
geometry.setIndex(indices);
geometry.computeBoundingSphere();
const mesh = new THREE.Mesh(geometry, material);
mesh.castShadow = true;
mesh.receiveShadow = true;
return mesh;
}
// -------------------------------------------------------------------------
// Materials
// -------------------------------------------------------------------------
const trunkMaterial = new THREE.MeshStandardMaterial({
color: 0x654336,
roughness: 0.94,
metalness: 0
});
const rootMaterial = new THREE.MeshStandardMaterial({
color: 0xa66d45,
roughness: 0.9,
metalness: 0
});
const rootletMaterial = new THREE.MeshStandardMaterial({
color: 0x8d5d3d,
roughness: 0.93,
metalness: 0
});
const branchMaterial = new THREE.MeshStandardMaterial({
color: 0x76503d,
roughness: 0.95,
metalness: 0
});
const sedimentMaterial = new THREE.MeshStandardMaterial({
color: 0xffffff,
vertexColors: true,
roughness: 1,
metalness: 0
});
const waterMaterial = new THREE.MeshPhongMaterial({
color: 0x4aa9b0,
transparent: true,
opacity: 0.48,
shininess: 100,
specular: 0xbdeff0,
depthWrite: false,
side: THREE.DoubleSide
});
const leafMaterials = [
new THREE.MeshStandardMaterial({
color: 0x1d5b42,
roughness: 0.95,
flatShading: true
}),
new THREE.MeshStandardMaterial({
color: 0x2c754b,
roughness: 0.96,
flatShading: true
}),
new THREE.MeshStandardMaterial({
color: 0x3d8954,
roughness: 0.95,
flatShading: true
})
];
// -------------------------------------------------------------------------
// Sediment island
// -------------------------------------------------------------------------
function buildSediment() {
const rings = 11;
const segments = 96;
const positions = [0, sedimentHeight(0, 0), 0];
const colors = [];
const indices = [];
const base = new THREE.Color(0xb58d5c);
const light = new THREE.Color(0xc8a16f);
const dark = new THREE.Color(0x9c744c);
function pushColor(x, z) {
const value = 0.5 +
0.22 * Math.sin(x * 2.4 + z * 1.3) +
0.18 * Math.cos(x * 1.1 - z * 3.1);
const c = base.clone().lerp(light, Math.max(0, value));
c.lerp(dark, Math.max(0, -value));
colors.push(c.r, c.g, c.b);
}
pushColor(0, 0);
for (let ring = 1; ring <= rings; ring++) {
const r = ring / rings;
for (let j = 0; j < segments; j++) {
const angle = j / segments * Math.PI * 2;
const x = Math.cos(angle) * 4.3 * r;
const z = Math.sin(angle) * 3.35 * r;
const y = sedimentHeight(x, z);
positions.push(x, y, z);
pushColor(x, z);
}
}
for (let j = 0; j < segments; j++) {
const next = (j + 1) % segments;
indices.push(0, 1 + next, 1 + j);
}
for (let ring = 1; ring < rings; ring++) {
const innerStart = 1 + (ring - 1) * segments;
const outerStart = 1 + ring * segments;
for (let j = 0; j < segments; j++) {
const next = (j + 1) % segments;
const a = innerStart + j;
const b = innerStart + next;
const c = outerStart + j;
const d = outerStart + next;
indices.push(a, b, c, b, d, c);
}
}
const geometry = new THREE.BufferGeometry();
geometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(positions, 3)
);
geometry.setAttribute(
"color",
new THREE.Float32BufferAttribute(colors, 3)
);
geometry.setIndex(indices);
geometry.computeVertexNormals();
const island = new THREE.Mesh(geometry, sedimentMaterial);
island.receiveShadow = true;
scene.add(island);
}
buildSediment();
// Small sediment granules make the substrate visually distinct.
const pebbleGeometry = new THREE.DodecahedronGeometry(0.075, 0);
const pebbleMaterial = new THREE.MeshStandardMaterial({
color: 0x987248,
roughness: 1
});
const pebbles = new THREE.InstancedMesh(
pebbleGeometry,
pebbleMaterial,
34
);
pebbles.castShadow = true;
pebbles.receiveShadow = true;
const dummy = new THREE.Object3D();
for (let i = 0; i < 34; i++) {
const angle = random() * Math.PI * 2;
const radius = Math.sqrt(random()) * 0.88;
const x = Math.cos(angle) * 4.3 * radius;
const z = Math.sin(angle) * 3.35 * radius;
const size = 0.45 + random() * 0.8;
dummy.position.set(x, sedimentHeight(x, z) + 0.025, z);
dummy.rotation.set(random() * Math.PI, random() * Math.PI, random() * Math.PI);
dummy.scale.set(size, size * 0.48, size * 0.82);
dummy.updateMatrix();
pebbles.setMatrixAt(i, dummy.matrix);
}
scene.add(pebbles);
// -------------------------------------------------------------------------
// Water plane and level gauge
// -------------------------------------------------------------------------
const water = new THREE.Mesh(
new THREE.CircleGeometry(24, 128),
waterMaterial
);
water.rotation.x = -Math.PI / 2;
water.position.y = 0;
water.renderOrder = 2;
scene.add(water);
const rippleMeshes = [];
for (const radius of [0.72, 1.2, 1.78]) {
const material = new THREE.MeshBasicMaterial({
color: 0xbdeff0,
transparent: true,
opacity: 0.17,
depthWrite: false,
side: THREE.DoubleSide
});
const ring = new THREE.Mesh(
new THREE.RingGeometry(radius, radius + 0.025, 96),
material
);
ring.rotation.x = -Math.PI / 2;
ring.position.y = 0.014;
ring.renderOrder = 3;
rippleMeshes.push(ring);
scene.add(ring);
}
const gauge = new THREE.Group();
gauge.position.set(-3.45, 0, 0.55);
const gaugeMaterial = new THREE.MeshStandardMaterial({
color: 0xe8f4ec,
roughness: 0.55
});
const pole = new THREE.Mesh(
new THREE.CylinderGeometry(0.018, 0.018, 2.25, 10),
gaugeMaterial
);
pole.position.y = 0.55;
pole.castShadow = true;
gauge.add(pole);
for (let y = 0; y <= 2; y += 0.5) {
const tick = new THREE.Mesh(
new THREE.BoxGeometry(y === 0 ? 0.5 : 0.28, 0.025, 0.035),
y === 0
? new THREE.MeshBasicMaterial({ color: 0x2f9fb5 })
: gaugeMaterial
);
tick.position.set(0.07, y, 0);
gauge.add(tick);
}
scene.add(gauge);
// -------------------------------------------------------------------------
// Trunk and branches
// -------------------------------------------------------------------------
const trunkCurve = new THREE.CatmullRomCurve3([
new THREE.Vector3(0.00, 0.08, 0.00),
new THREE.Vector3(-0.12, 0.64, 0.04),
new THREE.Vector3(0.07, 1.28, -0.03),
new THREE.Vector3(-0.09, 1.95, 0.06),
new THREE.Vector3(0.08, 2.68, 0.00),
new THREE.Vector3(0.01, 3.48, 0.03)
]);
const trunk = taperedTube(
trunkCurve,
trunkMaterial,
54,
12,
0.29,
0.105
);
scene.add(trunk);
const branchDefinitions = [
{
start: 0.55,
end: new THREE.Vector3(-1.75, 3.58, 0.30)
},
{
start: 0.60,
end: new THREE.Vector3(1.72, 3.72, -0.25)
},
{
start: 0.66,
end: new THREE.Vector3(0.30, 3.83, -1.55)
},
{
start: 0.69,
end: new THREE.Vector3(0.52, 3.92, 1.52)
},
{
start: 0.78,
end: new THREE.Vector3(-0.70, 4.10, -0.55)
},
{
start: 0.81,
end: new THREE.Vector3(1.00, 4.08, 0.62)
}
];
const branches = branchDefinitions.map((definition) => {
const start = trunkCurve.getPointAt(definition.start);
const midpoint = start.clone().lerp(definition.end, 0.52);
midpoint.y += 0.24;
const curve = new THREE.CatmullRomCurve3([
start,
midpoint,
definition.end
]);
const mesh = taperedTube(
curve,
branchMaterial,
26,
8,
0.105,
0.035
);
scene.add(mesh);
return { curve, end: definition.end };
});
// -------------------------------------------------------------------------
// Rhizophora-style exposed prop roots
// -------------------------------------------------------------------------
const rootCurves = [];
for (let i = 0; i < 17; i++) {
const angle =
i / 17 * Math.PI * 2 +
(random() - 0.5) * 0.22;
const radial = new THREE.Vector3(
Math.cos(angle),
0,
Math.sin(angle)
);
const trunkT = 0.12 + random() * 0.45;
const start = trunkCurve.getPointAt(trunkT);
const surfaceOffset = THREE.MathUtils.lerp(0.20, 0.11, trunkT);
start.addScaledVector(radial, surfaceOffset);
const desiredRadius = 2.15 + random() * 1.0;
const ellipseLimitX = 3.35 / Math.max(0.18, Math.abs(radial.x));
const ellipseLimitZ = 2.7 / Math.max(0.18, Math.abs(radial.z));
const radius = Math.min(
desiredRadius,
ellipseLimitX,
ellipseLimitZ
);
const c1 = start.clone().addScaledVector(radial, radius * 0.18);
c1.y = start.y + 0.02;
const c2 = start.clone().addScaledVector(radial, radius * 0.47);
c2.y = Math.max(0.43, start.y - 0.38);
const c3 = start.clone().addScaledVector(radial, radius * 0.77);
c3.y = 0.22 + random() * 0.16;
const end = new THREE.Vector3(
radial.x * radius,
-0.13,
radial.z * radius
);
const curve = new THREE.CatmullRomCurve3([
start,
c1,
c2,
c3,
end
]);
rootCurves.push(curve);
const root = taperedTube(
curve,
rootMaterial,
36,
8,
0.095 + random() * 0.025,
0.027
);
scene.add(root);
// A few subordinate rootlets branch from the main prop roots.
if (i % 2 === 0) {
const branchOrigin = curve.getPointAt(0.54);
const side = new THREE.Vector3(-radial.z, 0, radial.x)
.multiplyScalar((random() > 0.5 ? 1 : -1) * (0.35 + random() * 0.4));
const rootletEnd = branchOrigin.clone()
.addScaledVector(radial, 0.5)
.add(side);
rootletEnd.y = -0.11;
const rootletMid = branchOrigin.clone()
.lerp(rootletEnd, 0.5);
rootletMid.y = 0.18;
const rootletCurve = new THREE.CatmullRomCurve3([
branchOrigin,
rootletMid,
rootletEnd
]);
const rootlet = taperedTube(
rootletCurve,
rootletMaterial,
18,
7,
0.045,
0.016
);
scene.add(rootlet);
}
}
// A small number of prop roots descend from branches, another compatible
// Rhizophora growth pattern.
for (let i = 0; i < 3; i++) {
const branch = branches[i];
const start = branch.curve.getPointAt(0.22 + i * 0.06);
const direction = branch.end.clone().sub(start);
direction.y = 0;
direction.normalize();
const radius = 1.8 + i * 0.18;
const c1 = start.clone().addScaledVector(direction, radius * 0.25);
c1.y = start.y - 0.12;
const c2 = start.clone().addScaledVector(direction, radius * 0.62);
c2.y = 0.55;
const end = start.clone().addScaledVector(direction, radius);
end.y = -0.12;
const curve = new THREE.CatmullRomCurve3([
start,
c1,
c2,
end
]);
const root = taperedTube(
curve,
rootMaterial,
30,
8,
0.075,
0.024
);
scene.add(root);
}
// -------------------------------------------------------------------------
// Evergreen crown
// -------------------------------------------------------------------------
const canopy = new THREE.Group();
scene.add(canopy);
const clusterGeometry = new THREE.IcosahedronGeometry(1, 1);
function addLeafCluster(x, y, z, sx, sy, sz, materialIndex = 1) {
const cluster = new THREE.Mesh(
clusterGeometry,
leafMaterials[materialIndex % leafMaterials.length]
);
cluster.position.set(x, y, z);
cluster.scale.set(sx, sy, sz);
cluster.rotation.set(
random() * 0.35,
random() * Math.PI,
random() * 0.28
);
cluster.castShadow = true;
cluster.receiveShadow = true;
canopy.add(cluster);
return cluster;
}
addLeafCluster(0.00, 4.02, 0.00, 1.24, 0.78, 1.06, 1);
addLeafCluster(-1.55, 3.73, 0.22, 1.02, 0.62, 0.84, 0);
addLeafCluster(1.52, 3.80, -0.20, 1.08, 0.66, 0.90, 2);
addLeafCluster(0.18, 3.80, -1.42, 0.94, 0.63, 0.83, 1);
addLeafCluster(0.42, 3.90, 1.40, 1.02, 0.66, 0.88, 0);
addLeafCluster(-0.70, 4.16, -0.53, 0.94, 0.66, 0.78, 2);
addLeafCluster(0.98, 4.14, 0.58, 0.92, 0.63, 0.82, 1);
const crownOffsets = [
[-2.03, 3.95, 0.44, 0.55, 0.44, 0.52],
[-1.78, 4.20, -0.32, 0.57, 0.45, 0.55],
[-0.95, 4.48, 0.28, 0.65, 0.46, 0.58],
[0.02, 4.55, 0.14, 0.65, 0.48, 0.59],
[0.98, 4.43, 0.20, 0.64, 0.45, 0.56],
[1.88, 4.03, -0.16, 0.57, 0.44, 0.52],
[1.62, 3.48, 0.42, 0.48, 0.38, 0.45],
[-1.58, 3.39, 0.44, 0.50, 0.38, 0.47],
[0.10, 3.53, -1.70, 0.55, 0.42, 0.51],
[0.62, 3.55, 1.70, 0.56, 0.42, 0.51],
[-0.38, 3.62, 1.64, 0.50, 0.39, 0.47],
[0.25, 3.75, -1.70, 0.51, 0.39, 0.48]
];
crownOffsets.forEach((entry, index) => {
addLeafCluster(
entry[0],
entry[1],
entry[2],
entry[3],
entry[4],
entry[5],
index % 3
);
});
// -------------------------------------------------------------------------
// Labels and 3D anchor markers
// -------------------------------------------------------------------------
const labelDefinitions = [
{
id: "label-roots",
anchor: rootCurves[3].getPointAt(0.48),
color: 0xa66d45,
offset: [140, -20]
},
{
id: "label-trunk",
anchor: trunkCurve.getPointAt(0.39),
color: 0x654336,
offset: [-150, -65]
},
{
id: "label-water",
anchor: new THREE.Vector3(-3.38, 0, 0.55),
color: 0x2f9fb5,
offset: [-115, 42]
},
{
id: "label-sediment",
anchor: new THREE.Vector3(
2.35,
sedimentHeight(2.35, 0.9),
0.9
),
color: 0xb99262,
offset: [105, 52]
}
];
const svg = document.getElementById("leaders");
const svgNS = "http://www.w3.org/2000/svg";
for (const definition of labelDefinitions) {
definition.element = document.getElementById(definition.id);
const marker = new THREE.Mesh(
new THREE.SphereGeometry(0.038, 12, 8),
new THREE.MeshBasicMaterial({
color: definition.color,
depthTest: false
})
);
marker.position.copy(definition.anchor);
marker.renderOrder = 10;
scene.add(marker);
const line = document.createElementNS(svgNS, "line");
line.setAttribute("stroke", `#${definition.color.toString(16).padStart(6, "0")}`);
svg.appendChild(line);
definition.line = line;
}
// -------------------------------------------------------------------------
// Direct camera controls: orbit, zoom, reset, and pinch
// -------------------------------------------------------------------------
const target = new THREE.Vector3(0, 1.62, 0);
const initialCamera = {
radius: 11.2,
theta: 0.64,
phi: 1.23
};
let radius = initialCamera.radius;
let theta = initialCamera.theta;
let phi = initialCamera.phi;
function updateCamera() {
const sinPhi = Math.sin(phi);
camera.position.set(
target.x + radius * sinPhi * Math.sin(theta),
target.y + radius * Math.cos(phi),
target.z + radius * sinPhi * Math.cos(theta)
);
camera.lookAt(target);
camera.updateMatrixWorld();
}
function resetCamera() {
radius = initialCamera.radius;
theta = initialCamera.theta;
phi = initialCamera.phi;
updateCamera();
}
updateCamera();
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 [a, b] = [...pointers.values()];
lastPinchDistance = Math.hypot(a.x - b.x, a.y - b.y);
}
});
renderer.domElement.addEventListener("pointermove", (event) => {
const previous = pointers.get(event.pointerId);
if (!previous) return;
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) {
theta -= dx * 0.006;
phi = THREE.MathUtils.clamp(phi - dy * 0.005, 0.45, 1.48);
} else if (pointers.size === 2) {
const [a, b] = [...pointers.values()];
const distance = Math.hypot(a.x - b.x, a.y - b.y);
if (lastPinchDistance > 0 && distance > 0) {
radius = THREE.MathUtils.clamp(
radius * (lastPinchDistance / distance),
6.2,
17.5
);
}
lastPinchDistance = distance;
}
updateCamera();
});
function releasePointer(event) {
pointers.delete(event.pointerId);
lastPinchDistance = 0;
}
renderer.domElement.addEventListener("pointerup", releasePointer);
renderer.domElement.addEventListener("pointercancel", releasePointer);
renderer.domElement.addEventListener("lostpointercapture", releasePointer);
renderer.domElement.addEventListener(
"wheel",
(event) => {
event.preventDefault();
radius = THREE.MathUtils.clamp(
radius * Math.exp(event.deltaY * 0.001),
6.2,
17.5
);
updateCamera();
},
{ passive: false }
);
renderer.domElement.addEventListener("dblclick", resetCamera);
document.getElementById("reset-view").addEventListener("click", resetCamera);
// -------------------------------------------------------------------------
// Projected annotation positioning
// -------------------------------------------------------------------------
const projected = new THREE.Vector3();
function updateLabels() {
const width = window.innerWidth;
const height = window.innerHeight;
for (const definition of labelDefinitions) {
projected.copy(definition.anchor).project(camera);
const anchorX = (projected.x * 0.5 + 0.5) * width;
const anchorY = (-projected.y * 0.5 + 0.5) * height;
const cardWidth = definition.element.offsetWidth || 130;
const halfWidth = cardWidth * 0.5 + 8;
const labelX = THREE.MathUtils.clamp(
anchorX + definition.offset[0],
halfWidth,
width - halfWidth
);
const labelY = THREE.MathUtils.clamp(
anchorY + definition.offset[1],
88,
height - 105
);
definition.element.style.left = `${labelX}px`;
definition.element.style.top = `${labelY}px`;
definition.line.setAttribute("x1", anchorX);
definition.line.setAttribute("y1", anchorY);
definition.line.setAttribute("x2", labelX);
definition.line.setAttribute("y2", labelY);
}
}
// -------------------------------------------------------------------------
// Resize and animation
// -------------------------------------------------------------------------
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();
updateLabels();
});
const clock = new THREE.Clock();
function animate() {
const elapsed = clock.getElapsedTime();
canopy.rotation.z = Math.sin(elapsed * 0.55) * 0.009;
canopy.rotation.x = Math.cos(elapsed * 0.42) * 0.006;
rippleMeshes.forEach((ring, index) => {
ring.material.opacity =
0.12 +
0.045 *
(0.5 + 0.5 * Math.sin(elapsed * 0.9 + index * 1.6));
});
renderer.render(scene, camera);
updateLabels();
requestAnimationFrame(animate);
}
updateLabels();
animate();
</script>
</body>
</html>
```