Prompt specification
Build or customize the Three.js 3D scene "Stream Bank With Bare and Vegetated Sections" 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 Bank With Bare and Vegetated Sections
Category: Everyday & Industrial · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/stream-bank-with-bare-and-vegetated-sections-x3965
Scene Description Prompt:
Build a self-contained HTML scene with Three.js from cdnjs featuring bare and vegetated stream banks. Model a stream-edge cross-section comparing bare soil and rooted vegetation. Show geometry and root placement while avoiding universal erosion-rate claims. Low-poly.
Working Three.js Starter (stream-bank-with-bare-and-vegetated-sections-x3965):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Bare and Rooted Stream-Edge Banks</title>
<style>
:root {
--ink: #24342d;
--muted: #617068;
--paper: #f4f1e8;
--glass: rgba(248, 246, 238, 0.91);
--line: rgba(36, 52, 45, 0.2);
--green: #4d7459;
--root: #d8a064;
--water: #4e929b;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #e5e9e1;
color: var(--ink);
font-family:
Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
#scene {
position: fixed;
inset: 0;
}
#scene canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
cursor: grab;
}
#scene canvas.dragging {
cursor: grabbing;
}
.ui {
position: fixed;
inset: 0;
pointer-events: none;
z-index: 5;
}
header {
position: absolute;
top: 24px;
left: 28px;
max-width: 470px;
}
.eyebrow {
margin-bottom: 8px;
color: var(--green);
font-size: 11px;
font-weight: 800;
letter-spacing: 0.19em;
text-transform: uppercase;
}
h1 {
margin: 0;
font-family: Georgia, "Times New Roman", serif;
font-size: clamp(28px, 3.3vw, 48px);
font-weight: 500;
line-height: 0.98;
letter-spacing: -0.035em;
}
.subtitle {
margin: 11px 0 0;
color: var(--muted);
font-size: 14px;
line-height: 1.5;
}
.toolbar {
position: absolute;
top: 24px;
right: 24px;
display: flex;
gap: 8px;
pointer-events: auto;
}
button {
appearance: none;
border: 1px solid var(--line);
border-radius: 999px;
padding: 9px 13px;
background: var(--glass);
color: var(--ink);
box-shadow: 0 8px 24px rgba(39, 54, 47, 0.08);
backdrop-filter: blur(10px);
font: inherit;
font-size: 12px;
font-weight: 700;
cursor: pointer;
transition:
background 160ms ease,
color 160ms ease,
transform 160ms ease;
}
button:hover {
transform: translateY(-1px);
background: #fffdf7;
}
button.active {
border-color: transparent;
background: var(--ink);
color: #f9f6ec;
}
.comparison-card {
position: absolute;
bottom: 24px;
left: 24px;
width: min(440px, calc(100vw - 48px));
padding: 18px 19px 17px;
border: 1px solid rgba(36, 52, 45, 0.13);
border-radius: 17px;
background: var(--glass);
box-shadow: 0 18px 50px rgba(40, 55, 48, 0.12);
backdrop-filter: blur(13px);
}
.comparison-card .tag {
display: inline-flex;
align-items: center;
gap: 7px;
margin-bottom: 8px;
color: #8a5537;
font-size: 10px;
font-weight: 850;
letter-spacing: 0.14em;
text-transform: uppercase;
}
.comparison-card .tag::before {
width: 7px;
height: 7px;
border-radius: 50%;
background: #b86d43;
content: "";
}
.comparison-card h2 {
margin: 0 0 8px;
font-family: Georgia, "Times New Roman", serif;
font-size: 21px;
font-weight: 500;
}
.comparison-card p {
margin: 0;
color: #56645d;
font-size: 12.5px;
line-height: 1.55;
}
.fact-row {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 12px;
margin-top: 14px;
padding-top: 13px;
border-top: 1px solid var(--line);
}
.fact-row strong {
display: block;
margin-bottom: 3px;
font-size: 11px;
}
.fact-row span {
display: block;
color: var(--muted);
font-size: 10.5px;
line-height: 1.35;
}
.legend {
position: absolute;
right: 24px;
bottom: 24px;
padding: 13px 15px;
border: 1px solid var(--line);
border-radius: 15px;
background: var(--glass);
box-shadow: 0 14px 38px rgba(40, 55, 48, 0.1);
backdrop-filter: blur(12px);
}
.legend-title {
margin-bottom: 9px;
font-size: 10px;
font-weight: 850;
letter-spacing: 0.13em;
text-transform: uppercase;
}
.legend-row {
display: flex;
align-items: center;
gap: 8px;
margin-top: 6px;
color: #59675f;
font-size: 11px;
}
.swatch {
width: 13px;
height: 7px;
border-radius: 2px;
}
.swatch.soil {
background: #79543a;
}
.swatch.root {
background: var(--root);
}
.swatch.plant {
background: #638454;
}
.swatch.water {
background: var(--water);
}
.hint {
position: absolute;
bottom: 25px;
left: 50%;
padding: 8px 12px;
border-radius: 999px;
background: rgba(250, 248, 240, 0.72);
color: #66736c;
font-size: 10.5px;
transform: translateX(-50%);
backdrop-filter: blur(8px);
}
#annotations {
position: absolute;
inset: 0;
width: 100%;
height: 100%;
overflow: visible;
pointer-events: none;
transition: opacity 180ms ease;
}
#annotations.hidden {
opacity: 0;
}
.annotation {
position: absolute;
z-index: 2;
width: max-content;
max-width: 190px;
padding: 7px 10px;
border: 1px solid rgba(36, 52, 45, 0.15);
border-radius: 9px;
background: rgba(250, 248, 240, 0.92);
color: #35453d;
box-shadow: 0 7px 18px rgba(40, 55, 48, 0.09);
font-size: 10.5px;
font-weight: 700;
line-height: 1.25;
transform: translate(-50%, -50%);
backdrop-filter: blur(7px);
white-space: nowrap;
}
.annotation.title {
padding: 8px 12px;
border-color: transparent;
background: rgba(36, 52, 45, 0.93);
color: #f8f5e9;
font-size: 11px;
font-weight: 850;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.annotation small {
display: block;
margin-top: 2px;
color: #6a776f;
font-size: 9px;
font-weight: 500;
letter-spacing: 0;
text-transform: none;
}
.annotation.title small {
color: #c8d5cd;
}
#loading {
position: fixed;
inset: 0;
z-index: 20;
display: grid;
place-items: center;
background: #e5e9e1;
color: #4d6157;
font-size: 12px;
font-weight: 800;
letter-spacing: 0.14em;
text-transform: uppercase;
}
@media (max-width: 760px) {
header {
top: 16px;
left: 16px;
max-width: 58%;
}
h1 {
font-size: 28px;
}
.subtitle {
font-size: 11px;
}
.toolbar {
top: 16px;
right: 12px;
flex-direction: column;
align-items: flex-end;
}
button {
padding: 8px 10px;
font-size: 10px;
}
.comparison-card {
bottom: 12px;
left: 12px;
width: calc(100vw - 24px);
padding: 14px 15px;
}
.comparison-card h2 {
font-size: 18px;
}
.comparison-card p {
font-size: 11px;
}
.fact-row {
gap: 8px;
}
.legend,
.hint {
display: none;
}
.annotation {
max-width: 145px;
font-size: 9px;
}
}
</style>
</head>
<body>
<div id="scene"></div>
<div class="ui">
<header>
<div class="eyebrow">Stream-edge study / section exhibit</div>
<h1>Bare & rooted banks</h1>
<p class="subtitle">
A low-poly cutaway comparing exposed soil with vegetation and
illustrative root placement.
</p>
</header>
<div class="toolbar">
<button id="rootsToggle" class="active" aria-pressed="true">Roots: on</button>
<button id="labelsToggle" class="active" aria-pressed="true">Labels: on</button>
<button id="resetView" title="Reset camera">Reset view</button>
</div>
<div id="annotations"></div>
<section class="comparison-card">
<div class="tag">Schematic, not predictive</div>
<h2>Read the section</h2>
<p>
Both panels use the same idealized bank profile. The rooted panel
shows one possible network within the upper soil. Actual root depth
and bank response vary with soil, plant species, flow, climate, and
management; no erosion rate is implied.
</p>
<div class="fact-row">
<div>
<strong>Shared geometry</strong>
<span>Identical conceptual bank shape</span>
</div>
<div>
<strong>Root placement</strong>
<span>Projected onto the exposed cut plane</span>
</div>
<div>
<strong>Interpretation</strong>
<span>Qualitative only, not a rate model</span>
</div>
</div>
</section>
<div class="legend">
<div class="legend-title">Section key</div>
<div class="legend-row"><i class="swatch soil"></i>Soil</div>
<div class="legend-row"><i class="swatch root"></i>Illustrative roots</div>
<div class="legend-row"><i class="swatch plant"></i>Vegetation</div>
<div class="legend-row"><i class="swatch water"></i>Stream water</div>
</div>
<div class="hint">Drag to orbit · Scroll to zoom · Double-click to reset</div>
</div>
<div id="loading">Building stream-edge section…</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const sceneHost = document.getElementById("scene");
const annotationHost = document.getElementById("annotations");
const loading = document.getElementById("loading");
// ---------------------------------------------------------------------
// Renderer, scene, camera, and lighting
// ---------------------------------------------------------------------
const renderer = new THREE.WebGLRenderer({
antialias: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.12;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
sceneHost.appendChild(renderer.domElement);
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xe5e9e1);
scene.fog = new THREE.Fog(0xe5e9e1, 23, 42);
const camera = new THREE.PerspectiveCamera(
38,
window.innerWidth / window.innerHeight,
0.1,
80
);
const hemisphere = new THREE.HemisphereLight(0xf8fbef, 0x657165, 2.15);
scene.add(hemisphere);
const sun = new THREE.DirectionalLight(0xfff1d4, 3.25);
sun.position.set(-4, 10, 7);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -10;
sun.shadow.camera.right = 10;
sun.shadow.camera.top = 9;
sun.shadow.camera.bottom = -8;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 30;
sun.shadow.bias = -0.0005;
scene.add(sun);
const fill = new THREE.DirectionalLight(0xc9e0e8, 1.15);
fill.position.set(7, 5, -5);
scene.add(fill);
const ground = new THREE.Mesh(
new THREE.PlaneGeometry(40, 40),
new THREE.MeshStandardMaterial({
color: 0xdce2d8,
roughness: 1
})
);
ground.rotation.x = -Math.PI / 2;
ground.position.y = -1.99;
ground.receiveShadow = true;
scene.add(ground);
// ---------------------------------------------------------------------
// Shared low-poly bank geometry
// ---------------------------------------------------------------------
const PANEL_WIDTH = 5.4;
const PANEL_DEPTH = 3.1;
const HALF_WIDTH = PANEL_WIDTH / 2;
const HALF_DEPTH = PANEL_DEPTH / 2;
const SOIL_BOTTOM = -1.72;
const profileStops = [
[-2.70, -0.20],
[-1.80, -0.07],
[-1.25, 0.06],
[-0.60, 0.22],
[-0.05, 0.70],
[0.55, 1.18],
[1.25, 1.55],
[1.95, 1.68],
[2.70, 1.72]
];
function profileHeight(x) {
if (x <= profileStops[0][0]) return profileStops[0][1];
for (let i = 0; i < profileStops.length - 1; i++) {
const [x0, y0] = profileStops[i];
const [x1, y1] = profileStops[i + 1];
if (x <= x1) {
const t = THREE.MathUtils.clamp((x - x0) / (x1 - x0), 0, 1);
const eased = t * t * (3 - 2 * t);
return THREE.MathUtils.lerp(y0, y1, eased);
}
}
return profileStops.at(-1)[1];
}
function surfaceHeight(x, z) {
return (
profileHeight(x) +
Math.sin(x * 2.75 + z * 1.1) * 0.025 +
Math.cos(z * 3.2 - x * 0.8) * 0.018
);
}
function hash01(value) {
const s = Math.sin(value * 127.1 + 311.7) * 43758.5453123;
return s - Math.floor(s);
}
function createTerrainGeometry() {
const nx = 18;
const nz = 10;
const positions = [];
const indices = [];
for (let iz = 0; iz <= nz; iz++) {
const z = THREE.MathUtils.lerp(-HALF_DEPTH, HALF_DEPTH, iz / nz);
for (let ix = 0; ix <= nx; ix++) {
const x = THREE.MathUtils.lerp(-HALF_WIDTH, HALF_WIDTH, ix / nx);
positions.push(x, surfaceHeight(x, z), z);
}
}
for (let iz = 0; iz < nz; iz++) {
for (let ix = 0; ix < nx; ix++) {
const a = ix + iz * (nx + 1);
const b = a + 1;
const c = a + nx + 1;
const d = c + 1;
if ((ix + iz) % 2 === 0) {
indices.push(a, c, b, b, c, d);
} else {
indices.push(a, c, d, a, d, b);
}
}
}
const indexed = new THREE.BufferGeometry();
indexed.setAttribute(
"position",
new THREE.Float32BufferAttribute(positions, 3)
);
indexed.setIndex(indices);
const geometry = indexed.toNonIndexed();
indexed.dispose();
geometry.computeVertexNormals();
const position = geometry.getAttribute("position");
const colors = new Float32Array(position.count * 3);
const low = new THREE.Color(0x765039);
const high = new THREE.Color(0xa17a52);
const color = new THREE.Color();
for (let i = 0; i < position.count; i += 3) {
const averageY =
(position.getY(i) +
position.getY(i + 1) +
position.getY(i + 2)) /
3;
const t = THREE.MathUtils.clamp((averageY + 0.2) / 2.0, 0, 1);
color.copy(low).lerp(high, t);
color.multiplyScalar(0.93 + hash01(i * 0.71) * 0.13);
for (let j = 0; j < 3; j++) {
colors[(i + j) * 3] = color.r;
colors[(i + j) * 3 + 1] = color.g;
colors[(i + j) * 3 + 2] = color.b;
}
}
geometry.setAttribute("color", new THREE.BufferAttribute(colors, 3));
return geometry;
}
function createCutFaceGeometry(z) {
const nx = 18;
const bands = 5;
const positions = [];
const colors = [];
const bandColors = [
new THREE.Color(0xa77a4e),
new THREE.Color(0x916441),
new THREE.Color(0x80563c),
new THREE.Color(0x704a35),
new THREE.Color(0x624231)
];
function addTriangle(a, b, c, color) {
positions.push(...a, ...b, ...c);
for (let i = 0; i < 3; i++) {
colors.push(color.r, color.g, color.b);
}
}
for (let ix = 0; ix < nx; ix++) {
const x0 = THREE.MathUtils.lerp(-HALF_WIDTH, HALF_WIDTH, ix / nx);
const x1 = THREE.MathUtils.lerp(
-HALF_WIDTH,
HALF_WIDTH,
(ix + 1) / nx
);
const top0 = surfaceHeight(x0, z);
const top1 = surfaceHeight(x1, z);
for (let band = 0; band < bands; band++) {
const f0 = band / bands;
const f1 = (band + 1) / bands;
const y00 = THREE.MathUtils.lerp(SOIL_BOTTOM, top0, f0);
const y10 = THREE.MathUtils.lerp(SOIL_BOTTOM, top1, f0);
const y01 = THREE.MathUtils.lerp(SOIL_BOTTOM, top0, f1);
const y11 = THREE.MathUtils.lerp(SOIL_BOTTOM, top1, f1);
const color = bandColors[band].clone();
color.multiplyScalar(0.96 + hash01(ix * 9 + band) * 0.08);
addTriangle(
[x0, y00, z],
[x1, y10, z],
[x1, y11, z],
color
);
addTriangle(
[x0, y00, z],
[x1, y11, z],
[x0, y01, z],
color
);
}
}
const geometry = new THREE.BufferGeometry();
geometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(positions, 3)
);
geometry.setAttribute(
"color",
new THREE.Float32BufferAttribute(colors, 3)
);
geometry.computeVertexNormals();
return geometry;
}
function createSideGeometry(side) {
const positions = [];
const colors = [];
const nz = 10;
const x = side * HALF_WIDTH;
const lower = new THREE.Color(0x966946);
const upper = new THREE.Color(0x684835);
function tri(a, b, c, color) {
positions.push(...a, ...b, ...c);
for (let i = 0; i < 3; i++) {
colors.push(color.r, color.g, color.b);
}
}
for (let i = 0; i < nz; i++) {
const z0 = THREE.MathUtils.lerp(-HALF_DEPTH, HALF_DEPTH, i / nz);
const z1 = THREE.MathUtils.lerp(
-HALF_DEPTH,
HALF_DEPTH,
(i + 1) / nz
);
const top0 = surfaceHeight(x, z0);
const top1 = surfaceHeight(x, z1);
const color = lower.clone().lerp(upper, 0.55);
tri(
[x, SOIL_BOTTOM, z0],
[x, SOIL_BOTTOM, z1],
[x, top1, z1],
color
);
tri(
[x, SOIL_BOTTOM, z0],
[x, top1, z1],
[x, top0, z0],
color
);
}
const geometry = new THREE.BufferGeometry();
geometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(positions, 3)
);
geometry.setAttribute(
"color",
new THREE.Float32BufferAttribute(colors, 3)
);
geometry.computeVertexNormals();
return geometry;
}
const terrainGeometry = createTerrainGeometry();
const frontGeometry = createCutFaceGeometry(HALF_DEPTH);
const backGeometry = createCutFaceGeometry(-HALF_DEPTH);
const leftGeometry = createSideGeometry(-1);
const rightGeometry = createSideGeometry(1);
const terrainMaterial = new THREE.MeshStandardMaterial({
vertexColors: true,
flatShading: true,
roughness: 1
});
const faceMaterial = new THREE.MeshStandardMaterial({
vertexColors: true,
flatShading: true,
roughness: 1,
side: THREE.DoubleSide
});
const plinthMaterial = new THREE.MeshStandardMaterial({
color: 0x384a42,
roughness: 0.83,
metalness: 0.04
});
const plinthEdgeMaterial = new THREE.LineBasicMaterial({
color: 0x87988e,
transparent: true,
opacity: 0.55
});
const waterFlows = [];
// ---------------------------------------------------------------------
// Water and low-poly surface details
// ---------------------------------------------------------------------
function createWaterGeometry() {
const nx = 4;
const nz = 8;
const positions = [];
const colors = [];
const low = new THREE.Color(0x4a8993);
const high = new THREE.Color(0x73afb2);
function tri(a, b, c, color) {
positions.push(...a, ...b, ...c);
for (let i = 0; i < 3; i++) {
colors.push(color.r, color.g, color.b);
}
}
for (let iz = 0; iz < nz; iz++) {
for (let ix = 0; ix < nx; ix++) {
const x0 = THREE.MathUtils.lerp(-2.69, -1.34, ix / nx);
const x1 = THREE.MathUtils.lerp(-2.69, -1.34, (ix + 1) / nx);
const z0 = THREE.MathUtils.lerp(-HALF_DEPTH, HALF_DEPTH, iz / nz);
const z1 = THREE.MathUtils.lerp(
-HALF_DEPTH,
HALF_DEPTH,
(iz + 1) / nz
);
const y0 = 0.105 + hash01(ix * 11 + iz) * 0.012;
const y1 = 0.105 + hash01(ix * 7 + iz * 3) * 0.012;
const color = low.clone().lerp(high, hash01(ix + iz * 0.37));
tri(
[x0, y0, z0],
[x0, y1, z1],
[x1, y1, z1],
color
);
tri(
[x0, y0, z0],
[x1, y1, z1],
[x1, y0, z0],
color
);
}
}
const geometry = new THREE.BufferGeometry();
geometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(positions, 3)
);
geometry.setAttribute(
"color",
new THREE.Float32BufferAttribute(colors, 3)
);
geometry.computeVertexNormals();
return geometry;
}
const waterGeometry = createWaterGeometry();
const waterMaterial = new THREE.MeshStandardMaterial({
vertexColors: true,
transparent: true,
opacity: 0.84,
roughness: 0.24,
metalness: 0.04,
depthWrite: false,
side: THREE.DoubleSide
});
function addWaterFlow(group, offset) {
const material = new THREE.LineBasicMaterial({
color: 0xd5eeee,
transparent: true,
opacity: 0.62
});
for (let i = 0; i < 3; i++) {
const x = -2.4 + i * 0.32;
const geometry = new THREE.BufferGeometry().setFromPoints([
new THREE.Vector3(x, 0.135, -0.18),
new THREE.Vector3(x + 0.025, 0.135, 0.22)
]);
const line = new THREE.Line(geometry, material);
line.position.z = -1.4 + offset + i * 0.43;
group.add(line);
waterFlows.push({
line,
baseZ: line.position.z,
speed: 0.055 + i * 0.012
});
}
}
function addBareSoilDetails(group) {
const colors = [0x806047, 0x9a7658, 0x665044];
for (let i = 0; i < 17; i++) {
const x = -0.92 + hash01(i * 4.1) * 2.15;
const z = -1.35 + hash01(i * 8.3 + 2) * 2.7;
const radius = 0.035 + hash01(i * 2.7) * 0.055;
const clod = new THREE.Mesh(
new THREE.DodecahedronGeometry(radius, 0),
new THREE.MeshStandardMaterial({
color: colors[i % colors.length],
flatShading: true,
roughness: 1
})
);
clod.position.set(x, surfaceHeight(x, z) + radius * 0.35, z);
clod.rotation.set(
hash01(i + 1) * Math.PI,
hash01(i + 4) * Math.PI,
hash01(i + 8) * Math.PI
);
clod.castShadow = true;
clod.receiveShadow = true;
group.add(clod);
}
}
// ---------------------------------------------------------------------
// Plants and root network
// ---------------------------------------------------------------------
const barkMaterial = new THREE.MeshStandardMaterial({
color: 0x70503a,
flatShading: true,
roughness: 1
});
const leafMaterials = [
new THREE.MeshStandardMaterial({
color: 0x4f7448,
flatShading: true,
roughness: 0.92
}),
new THREE.MeshStandardMaterial({
color: 0x6f8d50,
flatShading: true,
roughness: 0.92
}),
new THREE.MeshStandardMaterial({
color: 0x879b5d,
flatShading: true,
roughness: 0.92
})
];
const grassMaterial = new THREE.MeshStandardMaterial({
color: 0x718c4b,
flatShading: true,
roughness: 0.95,
side: THREE.DoubleSide
});
const rootMaterial = new THREE.MeshStandardMaterial({
color: 0xe0ae72,
flatShading: true,
roughness: 0.92
});
const rootBranchMaterial = new THREE.MeshStandardMaterial({
color: 0xc78b53,
flatShading: true,
roughness: 0.95
});
const up = new THREE.Vector3(0, 1, 0);
function cylinderBetween(a, b, radiusA, radiusB, material, radialSegments = 6) {
const direction = new THREE.Vector3().subVectors(b, a);
const length = direction.length();
const geometry = new THREE.CylinderGeometry(
radiusB,
radiusA,
length,
radialSegments,
1,
false
);
const mesh = new THREE.Mesh(geometry, material);
mesh.position.copy(a).add(b).multiplyScalar(0.5);
mesh.quaternion.setFromUnitVectors(up, direction.normalize());
mesh.castShadow = true;
return mesh;
}
function addShrub(group, x, z, scale = 1) {
const baseY = surfaceHeight(x, z);
const trunkTop = new THREE.Vector3(x, baseY + 0.72 * scale, z);
group.add(
cylinderBetween(
new THREE.Vector3(x, baseY, z),
trunkTop,
0.07 * scale,
0.042 * scale,
barkMaterial
)
);
const branchTargets = [
new THREE.Vector3(x - 0.29 * scale, baseY + 0.98 * scale, z + 0.05),
new THREE.Vector3(x + 0.26 * scale, baseY + 1.05 * scale, z - 0.08),
new THREE.Vector3(x + 0.02 * scale, baseY + 1.15 * scale, z + 0.14)
];
branchTargets.forEach((target, index) => {
group.add(
cylinderBetween(
new THREE.Vector3(
x,
baseY + 0.43 * scale,
z
),
target,
0.038 * scale,
0.018 * scale,
barkMaterial,
5
)
);
const leaf = new THREE.Mesh(
new THREE.IcosahedronGeometry(0.46 * scale, 0),
leafMaterials[index % leafMaterials.length]
);
leaf.position.copy(target);
leaf.scale.set(1.0, 0.72, 0.92);
leaf.rotation.set(
hash01(index * 8.3) * 0.6,
hash01(index * 3.1) * Math.PI,
hash01(index * 4.6) * 0.5
);
leaf.castShadow = true;
leaf.receiveShadow = true;
group.add(leaf);
});
}
function addGrassTuft(group, x, z, scale = 1) {
const baseY = surfaceHeight(x, z);
const positions = [];
for (let i = 0; i < 7; i++) {
const angle = (i / 7) * Math.PI * 2 + hash01(i * 4.3) * 0.5;
const height = (0.28 + hash01(i * 3.7) * 0.28) * scale;
const lean = (0.08 + hash01(i * 8.2) * 0.11) * scale;
const width = 0.032 * scale;
const centerX = x + Math.cos(angle) * 0.045 * scale;
const centerZ = z + Math.sin(angle) * 0.045 * scale;
const sideX = Math.cos(angle + Math.PI / 2) * width;
const sideZ = Math.sin(angle + Math.PI / 2) * width;
positions.push(
centerX - sideX,
baseY,
centerZ - sideZ,
centerX + sideX,
baseY,
centerZ + sideZ,
centerX + Math.cos(angle) * lean,
baseY + height,
centerZ + Math.sin(angle) * lean
);
}
const geometry = new THREE.BufferGeometry();
geometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(positions, 3)
);
geometry.computeVertexNormals();
const grass = new THREE.Mesh(geometry, grassMaterial);
grass.castShadow = true;
group.add(grass);
}
function addRootPath(group, points, widths, material = rootMaterial) {
for (let i = 0; i < points.length - 1; i++) {
const a = points[i];
const b = points[i + 1];
const radius = widths[Math.min(i, widths.length - 1)];
group.add(
cylinderBetween(
a,
b,
radius,
radius * 0.72,
material,
5
)
);
}
}
function addRoots(group) {
const rootGroup = new THREE.Group();
rootGroup.name = "illustrative-root-network";
group.add(rootGroup);
const z = 1.605;
const startY = surfaceHeight(0.72, -0.2) + 0.015;
addRootPath(
rootGroup,
[
new THREE.Vector3(0.72, startY, z),
new THREE.Vector3(0.68, startY - 0.35, z + 0.008),
new THREE.Vector3(0.75, startY - 0.67, z),
new THREE.Vector3(0.60, startY - 0.94, z + 0.008),
new THREE.Vector3(0.48, startY - 1.30, z)
],
[0.057, 0.046, 0.036, 0.026, 0.016]
);
addRootPath(
rootGroup,
[
new THREE.Vector3(0.69, startY - 0.31, z + 0.008),
new THREE.Vector3(1.15, startY - 0.45, z),
new THREE.Vector3(1.63, startY - 0.60, z + 0.008),
new THREE.Vector3(2.02, startY - 0.70, z)
],
[0.031, 0.025, 0.018, 0.011],
rootBranchMaterial
);
addRootPath(
rootGroup,
[
new THREE.Vector3(0.74, startY - 0.65, z),
new THREE.Vector3(0.25, startY - 0.78, z + 0.008),
new THREE.Vector3(-0.20, startY - 0.88, z),
new THREE.Vector3(-0.56, startY - 0.96, z + 0.008)
],
[0.028, 0.022, 0.016, 0.010],
rootBranchMaterial
);
addRootPath(
rootGroup,
[
new THREE.Vector3(0.62, startY - 0.91, z + 0.008),
new THREE.Vector3(1.08, startY - 1.06, z),
new THREE.Vector3(1.42, startY - 1.22, z + 0.008),
new THREE.Vector3(1.65, startY - 1.35, z)
],
[0.023, 0.018, 0.012, 0.008],
rootBranchMaterial
);
addRootPath(
rootGroup,
[
new THREE.Vector3(0.52, startY - 1.16, z),
new THREE.Vector3(0.05, startY - 1.28, z + 0.008),
new THREE.Vector3(-0.39, startY - 1.36, z)
],
[0.018, 0.013, 0.008],
rootBranchMaterial
);
addRootPath(
rootGroup,
[
new THREE.Vector3(1.15, startY - 0.45, z),
new THREE.Vector3(1.29, startY - 0.24, z + 0.01),
new THREE.Vector3(1.43, startY - 0.18, z)
],
[0.014, 0.009, 0.006]
);
addRootPath(
rootGroup,
[
new THREE.Vector3(0.25, startY - 0.78, z + 0.008),
new THREE.Vector3(0.17, startY - 0.99, z),
new THREE.Vector3(0.23, startY - 1.16, z + 0.01)
],
[0.013, 0.009, 0.006]
);
addRootPath(
rootGroup,
[
new THREE.Vector3(1.08, startY - 1.06, z),
new THREE.Vector3(0.94, startY - 1.24, z + 0.01),
new THREE.Vector3(0.91, startY - 1.39, z)
],
[0.012, 0.008, 0.005]
);
addRootPath(
rootGroup,
[
new THREE.Vector3(1.63, startY - 0.60, z + 0.008),
new THREE.Vector3(1.76, startY - 0.39, z),
new THREE.Vector3(1.89, startY - 0.31, z + 0.01)
],
[0.012, 0.008, 0.005]
);
// A qualitative depth bracket, intentionally without a numerical scale.
const bracketX = 2.30;
const bracketZ = 1.645;
const bottomY = startY - 1.42;
const bracketPoints = [
bracketX - 0.10, startY, bracketZ,
bracketX + 0.10, startY, bracketZ,
bracketX, startY, bracketZ,
bracketX, bottomY, bracketZ,
bracketX - 0.10, bottomY, bracketZ,
bracketX + 0.10, bottomY, bracketZ
];
const bracketGeometry = new THREE.BufferGeometry();
bracketGeometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(bracketPoints, 3)
);
const bracket = new THREE.LineSegments(
bracketGeometry,
new THREE.LineBasicMaterial({
color: 0xf2d3a2,
transparent: true,
opacity: 0.8
})
);
rootGroup.add(bracket);
rootGroup.userData.startY = startY;
rootGroup.userData.bottomY = bottomY;
rootGroup.userData.bracketX = bracketX;
return rootGroup;
}
// ---------------------------------------------------------------------
// Build the two section panels
// ---------------------------------------------------------------------
function createPanel(centerX, vegetated) {
const group = new THREE.Group();
group.position.x = centerX;
scene.add(group);
const terrain = new THREE.Mesh(terrainGeometry, terrainMaterial);
terrain.castShadow = true;
terrain.receiveShadow = true;
group.add(terrain);
const front = new THREE.Mesh(frontGeometry, faceMaterial);
front.receiveShadow = true;
group.add(front);
const back = new THREE.Mesh(backGeometry, faceMaterial);
back.receiveShadow = true;
group.add(back);
const left = new THREE.Mesh(leftGeometry, faceMaterial);
const right = new THREE.Mesh(rightGeometry, faceMaterial);
left.receiveShadow = true;
right.receiveShadow = true;
group.add(left, right);
const bottom = new THREE.Mesh(
new THREE.BoxGeometry(PANEL_WIDTH, 0.13, PANEL_DEPTH),
new THREE.MeshStandardMaterial({
color: 0x5f4938,
roughness: 1
})
);
bottom.position.y = SOIL_BOTTOM - 0.065;
bottom.receiveShadow = true;
group.add(bottom);
const plinth = new THREE.Mesh(
new THREE.BoxGeometry(5.75, 0.19, 3.48),
plinthMaterial
);
plinth.position.y = -1.86;
plinth.castShadow = true;
plinth.receiveShadow = true;
group.add(plinth);
const plinthEdges = new THREE.LineSegments(
new THREE.EdgesGeometry(plinth.geometry),
plinthEdgeMaterial
);
plinthEdges.position.copy(plinth.position);
group.add(plinthEdges);
const water = new THREE.Mesh(waterGeometry, waterMaterial);
water.renderOrder = 2;
group.add(water);
addWaterFlow(group, vegetated ? 0.24 : 0);
let rootGroup = null;
if (!vegetated) {
addBareSoilDetails(group);
} else {
addShrub(group, 0.72, -0.20, 1.0);
addShrub(group, 1.88, -0.72, 0.60);
addGrassTuft(group, -0.05, 0.57, 1.0);
addGrassTuft(group, 1.28, 0.74, 0.85);
addGrassTuft(group, 2.22, -0.05, 0.90);
addGrassTuft(group, 0.22, -1.16, 0.72);
rootGroup = addRoots(group);
}
return { group, rootGroup };
}
const barePanel = createPanel(-3.05, false);
const vegetatedPanel = createPanel(3.05, true);
const rootsVisible = vegetatedPanel.rootGroup;
// ---------------------------------------------------------------------
// DOM annotations projected from the 3D scene
// ---------------------------------------------------------------------
const annotations = [];
const svgNamespace = "http://www.w3.org/2000/svg";
function worldAnchor(group, x, y, z) {
return new THREE.Vector3(
group.position.x + x,
group.position.y + y,
group.position.z + z
);
}
function addAnnotation({
anchor,
text,
subtext = "",
dx = 0,
dy = 0,
kind = "callout",
dependsOnRoots = false
}) {
const label = document.createElement("div");
label.className = `annotation ${kind}`;
label.innerHTML = subtext
? `${text}<small>${subtext}</small>`
: text;
annotationHost.appendChild(label);
const line = document.createElementNS(svgNamespace, "line");
line.setAttribute("stroke", kind === "title" ? "#54675d" : "#52675a");
line.setAttribute("stroke-width", kind === "title" ? "0" : "1.15");
line.setAttribute("opacity", "0.78");
annotationHost.appendChild(line);
const dot = document.createElementNS(svgNamespace, "circle");
dot.setAttribute("r", "3.2");
dot.setAttribute("fill", "#f3eee0");
dot.setAttribute("stroke", "#4e6558");
dot.setAttribute("stroke-width", "1.4");
annotationHost.appendChild(dot);
annotations.push({
anchor,
label,
line,
dot,
dx,
dy,
kind,
dependsOnRoots
});
}
addAnnotation({
anchor: worldAnchor(barePanel.group, -1.55, 2.08, 1.28),
text: "01 / Bare soil",
subtext: "Exposed surface",
dx: -5,
dy: -42,
kind: "title"
});
addAnnotation({
anchor: worldAnchor(barePanel.group, -0.52, 0.48, HALF_DEPTH),
text: "Exposed bank surface",
dx: -108,
dy: -12
});
addAnnotation({
anchor: worldAnchor(barePanel.group, 1.65, -0.77, HALF_DEPTH),
text: "Visible soil cut face",
dx: 74,
dy: 16
});
addAnnotation({
anchor: worldAnchor(vegetatedPanel.group, -1.55, 2.08, 1.28),
text: "02 / Rooted vegetation",
subtext: "Vegetation treatment",
dx: -5,
dy: -42,
kind: "title"
});
addAnnotation({
anchor: worldAnchor(vegetatedPanel.group, 0.72, 2.40, -0.20),
text: "Low-poly vegetation",
dx: 83,
dy: -34
});
addAnnotation({
anchor: worldAnchor(vegetatedPanel.group, 0.75, 0.79, 1.62),
text: "Root paths on section plane",
dx: -112,
dy: 0,
dependsOnRoots: true
});
addAnnotation({
anchor: worldAnchor(
vegetatedPanel.group,
rootsVisible.userData.bracketX,
(rootsVisible.userData.startY + rootsVisible.userData.bottomY) / 2,
1.65
),
text: "Illustrative rooting depth",
subtext: "No numerical scale",
dx: 93,
dy: 12,
dependsOnRoots: true
});
let labelsVisible = true;
function updateAnnotations() {
const width = window.innerWidth;
const height = window.innerHeight;
for (const item of annotations) {
const enabled =
labelsVisible &&
(!item.dependsOnRoots || rootsVisible.visible);
if (!enabled) {
item.label.style.display = "none";
item.line.style.display = "none";
item.dot.style.display = "none";
continue;
}
const projected = item.anchor.clone().project(camera);
if (projected.z < -1 || projected.z > 1) {
item.label.style.display = "none";
item.line.style.display = "none";
item.dot.style.display = "none";
continue;
}
item.label.style.display = "block";
item.line.style.display = "";
item.dot.style.display = "";
const sx = (projected.x * 0.5 + 0.5) * width;
const sy = (-projected.y * 0.5 + 0.5) * height;
const lx = sx + item.dx;
const ly = sy + item.dy;
item.label.style.left = `${lx}px`;
item.label.style.top = `${ly}px`;
item.line.setAttribute("x1", sx);
item.line.setAttribute("y1", sy);
item.line.setAttribute("x2", lx);
item.line.setAttribute("y2", ly);
item.dot.setAttribute("cx", sx);
item.dot.setAttribute("cy", sy);
}
}
// ---------------------------------------------------------------------
// Custom camera controls: orbit, zoom, keyboard, touch
// ---------------------------------------------------------------------
const target = new THREE.Vector3(0, 0.25, 0);
let theta = 0.34;
let phi = 1.02;
let radius = defaultRadius();
function defaultRadius() {
if (window.innerWidth < 650) return 30;
if (window.innerWidth < 1000) return 20;
return 15.6;
}
function updateCamera() {
const spherical = new THREE.Spherical(radius, phi, theta);
camera.position.setFromSpherical(spherical).add(target);
camera.lookAt(target);
camera.updateMatrixWorld();
}
function resetCamera() {
theta = 0.34;
phi = 1.02;
radius = defaultRadius();
updateCamera();
}
let dragging = false;
let moved = false;
let previousX = 0;
let previousY = 0;
renderer.domElement.addEventListener("pointerdown", (event) => {
dragging = true;
moved = false;
previousX = event.clientX;
previousY = event.clientY;
renderer.domElement.setPointerCapture(event.pointerId);
renderer.domElement.classList.add("dragging");
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (!dragging) return;
const dx = event.clientX - previousX;
const dy = event.clientY - previousY;
if (Math.abs(dx) + Math.abs(dy) > 2) moved = true;
theta -= dx * 0.0055;
phi = THREE.MathUtils.clamp(phi - dy * 0.0048, 0.43, 1.42);
previousX = event.clientX;
previousY = event.clientY;
updateCamera();
});
function endDrag(event) {
dragging = false;
renderer.domElement.classList.remove("dragging");
if (renderer.domElement.hasPointerCapture(event.pointerId)) {
renderer.domElement.releasePointerCapture(event.pointerId);
}
}
renderer.domElement.addEventListener("pointerup", endDrag);
renderer.domElement.addEventListener("pointercancel", endDrag);
renderer.domElement.addEventListener(
"wheel",
(event) => {
event.preventDefault();
radius = THREE.MathUtils.clamp(
radius * Math.exp(event.deltaY * 0.001),
7.5,
38
);
updateCamera();
},
{ passive: false }
);
renderer.domElement.addEventListener("dblclick", resetCamera);
window.addEventListener("keydown", (event) => {
const key = event.key.toLowerCase();
if (key === "arrowleft") theta -= 0.08;
else if (key === "arrowright") theta += 0.08;
else if (key === "arrowup") phi -= 0.06;
else if (key === "arrowdown") phi += 0.06;
else if (key === "+" || key === "=") radius -= 0.7;
else if (key === "-") radius += 0.7;
else if (key === "r") {
resetCamera();
return;
} else {
return;
}
phi = THREE.MathUtils.clamp(phi, 0.43, 1.42);
radius = THREE.MathUtils.clamp(radius, 7.5, 38);
updateCamera();
event.preventDefault();
});
const rootsButton = document.getElementById("rootsToggle");
const labelsButton = document.getElementById("labelsToggle");
const resetButton = document.getElementById("resetView");
rootsButton.addEventListener("click", () => {
rootsVisible.visible = !rootsVisible.visible;
rootsButton.textContent = rootsVisible.visible ? "Roots: on" : "Roots: off";
rootsButton.classList.toggle("active", rootsVisible.visible);
rootsButton.setAttribute(
"aria-pressed",
String(rootsVisible.visible)
);
});
labelsButton.addEventListener("click", () => {
labelsVisible = !labelsVisible;
annotationHost.classList.toggle("hidden", !labelsVisible);
labelsButton.textContent = labelsVisible ? "Labels: on" : "Labels: off";
labelsButton.classList.toggle("active", labelsVisible);
labelsButton.setAttribute("aria-pressed", String(labelsVisible));
});
resetButton.addEventListener("click", resetCamera);
// ---------------------------------------------------------------------
// 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);
});
const reducedMotion = window.matchMedia(
"(prefers-reduced-motion: reduce)"
).matches;
const clock = new THREE.Clock();
function animate() {
const elapsed = clock.getElapsedTime();
if (!reducedMotion) {
for (const flow of waterFlows) {
const span = PANEL_DEPTH;
const shifted = flow.baseZ + elapsed * flow.speed + HALF_DEPTH;
flow.line.position.z =
((shifted % span) + span) % span - HALF_DEPTH;
}
}
updateCamera();
updateAnnotations();
renderer.render(scene, camera);
requestAnimationFrame(animate);
}
resetCamera();
animate();
loading.remove();
</script>
</body>
</html>
```