Prompt specification
Build or customize the Three.js 3D scene "Root-bound soil slope section" 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: Root-bound soil slope section
Category: Biology & Nature · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/root-bound-soil-slope-section-x1061
Scene Description Prompt:
Make a standalone HTML scene with Three.js from cdnjs that depicts farm soil erosion control. Show matching sloped soil blocks, one with a visible root network and one bare; mark loose surface particles separately from the deeper continuous substrate. Stylized solid forms.
Working Three.js Starter (root-bound-soil-slope-section-x1061):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Rooted Soil — Erosion Control Comparison</title>
<style>
:root {
--ink: #34251f;
--paper: #f4eddf;
--root: #e1b975;
--loose: #dc9448;
--substrate: #81432f;
--green: #4d7f49;
}
* {
box-sizing: border-box;
}
html,
body,
#app {
width: 100%;
height: 100%;
margin: 0;
}
body {
overflow: hidden;
color: var(--ink);
background: #eee6d6;
font-family:
Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
canvas {
display: block;
touch-action: none;
outline: none;
}
.hud {
position: fixed;
inset: 0;
z-index: 5;
pointer-events: none;
}
header {
position: absolute;
top: 24px;
left: 28px;
max-width: 520px;
padding: 18px 20px 17px;
border: 1px solid rgba(91, 62, 44, 0.16);
border-radius: 18px;
background: rgba(248, 243, 233, 0.9);
box-shadow: 0 12px 35px rgba(66, 42, 26, 0.08);
backdrop-filter: blur(9px);
}
.eyebrow {
display: flex;
align-items: center;
gap: 8px;
margin-bottom: 7px;
color: #7d5a43;
font-size: 10px;
font-weight: 800;
letter-spacing: 0.16em;
text-transform: uppercase;
}
.eyebrow::before {
width: 9px;
height: 9px;
border-radius: 50%;
background: var(--green);
box-shadow: 0 0 0 4px rgba(77, 127, 73, 0.13);
content: "";
}
h1 {
margin: 0;
font-size: clamp(24px, 3vw, 34px);
line-height: 1.05;
letter-spacing: -0.035em;
}
header p {
max-width: 455px;
margin: 9px 0 0;
color: #6b5546;
font-size: 13px;
line-height: 1.5;
}
.controls {
position: absolute;
top: 24px;
right: 28px;
display: flex;
gap: 8px;
pointer-events: auto;
}
button {
appearance: none;
padding: 10px 13px;
border: 1px solid rgba(76, 50, 35, 0.2);
border-radius: 11px;
color: var(--ink);
background: rgba(249, 245, 236, 0.92);
box-shadow: 0 6px 18px rgba(66, 42, 26, 0.07);
cursor: pointer;
font: inherit;
font-size: 11px;
font-weight: 750;
transition:
transform 150ms ease,
background 150ms ease;
}
button:hover {
transform: translateY(-1px);
background: #fffaf0;
}
button:active {
transform: translateY(0);
}
.legend {
position: absolute;
right: 28px;
bottom: 25px;
width: 228px;
padding: 15px 16px;
border: 1px solid rgba(91, 62, 44, 0.16);
border-radius: 16px;
background: rgba(248, 243, 233, 0.91);
box-shadow: 0 12px 35px rgba(66, 42, 26, 0.08);
backdrop-filter: blur(9px);
}
.legend h2 {
margin: 0 0 10px;
color: #775a45;
font-size: 10px;
letter-spacing: 0.15em;
text-transform: uppercase;
}
.legend-row {
display: flex;
align-items: center;
gap: 9px;
margin-top: 8px;
color: #4f3c31;
font-size: 11px;
line-height: 1.25;
}
.swatch {
display: inline-block;
flex: 0 0 auto;
width: 13px;
height: 13px;
border-radius: 4px;
}
.swatch.root {
background: var(--root);
}
.swatch.loose {
background: var(--loose);
}
.swatch.substrate {
background: var(--substrate);
}
.swatch.green {
background: var(--green);
}
.note {
position: absolute;
bottom: 27px;
left: 28px;
max-width: 305px;
color: #624d40;
font-size: 11px;
line-height: 1.55;
}
.note strong {
color: #3d2c23;
}
.hint {
position: absolute;
bottom: 28px;
left: 50%;
padding: 7px 10px;
border-radius: 999px;
color: rgba(66, 45, 34, 0.65);
background: rgba(248, 243, 233, 0.62);
font-size: 10px;
transform: translateX(-50%);
white-space: nowrap;
}
#leaders {
position: absolute;
inset: 0;
width: 100%;
height: 100%;
overflow: visible;
}
#leaders line {
stroke-width: 1.35;
stroke-dasharray: 3 3;
opacity: 0.72;
}
#leaders circle {
stroke: rgba(255, 249, 238, 0.9);
stroke-width: 2;
}
.annotation {
position: absolute;
min-width: 112px;
padding: 8px 10px 8px 11px;
border: 1px solid rgba(83, 55, 39, 0.14);
border-left: 4px solid var(--accent);
border-radius: 9px;
opacity: 0;
color: #3c2c23;
background: rgba(255, 251, 242, 0.94);
box-shadow: 0 7px 20px rgba(66, 42, 26, 0.1);
transform: translate(-50%, -50%);
transition: opacity 160ms ease;
white-space: nowrap;
}
.annotation b {
display: block;
font-size: 10px;
letter-spacing: 0.04em;
text-transform: uppercase;
}
.annotation small {
display: block;
margin-top: 2px;
color: #796456;
font-size: 9px;
}
@media (max-width: 760px) {
header {
top: 12px;
left: 12px;
right: 12px;
max-width: none;
padding: 13px 15px;
}
header p {
font-size: 11px;
}
.controls {
top: 126px;
right: 12px;
}
.legend {
right: 12px;
bottom: 12px;
width: 190px;
padding: 11px 12px;
}
.note,
.hint,
.annotation,
#leaders {
display: none;
}
}
@media (max-height: 650px) {
.note {
display: none;
}
}
</style>
</head>
<body>
<div id="app"></div>
<div class="hud">
<header>
<div class="eyebrow">Soil conservation field study</div>
<h1>Rooted soil holds.<br />Bare soil slips.</h1>
<p>
Matched slopes show how living roots bind loose surface grains while
runoff carries exposed particles downhill.
</p>
</header>
<div class="controls">
<button id="pause" type="button" aria-pressed="false">Pause motion</button>
<button id="reset-view" type="button">Reset view</button>
</div>
<svg id="leaders" aria-hidden="true"></svg>
<div
class="annotation"
id="ann-cover"
style="--accent: var(--green)"
>
<b>Root-bound cover</b>
<small>Crop roots form a living mesh</small>
</div>
<div
class="annotation"
id="ann-roots"
style="--accent: var(--root)"
>
<b>Visible root network</b>
<small>Roots grip the cut face</small>
</div>
<div
class="annotation"
id="ann-bare"
style="--accent: #a65337"
>
<b>Bare / eroding</b>
<small>Nothing anchors the surface</small>
</div>
<div
class="annotation"
id="ann-loose"
style="--accent: var(--loose)"
>
<b>Loose surface particles</b>
<small>Individual grains wash away</small>
</div>
<div
class="annotation"
id="ann-substrate"
style="--accent: var(--substrate)"
>
<b>Continuous substrate</b>
<small>The deeper soil remains connected</small>
</div>
<div class="note">
<strong>Same soil. Same slope. Different protection.</strong><br />
On the left, roots trap the surface layer. On the right, runoff
dislodges grains and deposits them at the toe.
</div>
<div class="legend">
<h2>Material key</h2>
<div class="legend-row">
<span class="swatch green"></span>
Protective crop cover
</div>
<div class="legend-row">
<span class="swatch root"></span>
Root network
</div>
<div class="legend-row">
<span class="swatch loose"></span>
Loose surface particles
</div>
<div class="legend-row">
<span class="swatch substrate"></span>
Continuous substrate
</div>
</div>
<div class="hint">Drag to orbit · Scroll or pinch to zoom</div>
</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// ------------------------------------------------------------
// Scene setup
// ------------------------------------------------------------
const app = document.querySelector("#app");
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xeee6d6);
scene.fog = new THREE.Fog(0xeee6d6, 17, 31);
const camera = new THREE.PerspectiveCamera(
42,
innerWidth / innerHeight,
0.1,
60
);
const renderer = new THREE.WebGLRenderer({
antialias: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(devicePixelRatio, 2));
renderer.setSize(innerWidth, innerHeight);
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.08;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.domElement.setAttribute(
"aria-label",
"Interactive comparison of root-covered and bare sloped soil blocks"
);
app.appendChild(renderer.domElement);
const hemisphere = new THREE.HemisphereLight(0xfff6df, 0x76503c, 2.0);
scene.add(hemisphere);
const sun = new THREE.DirectionalLight(0xffedc9, 3.2);
sun.position.set(-5.5, 10, 7.5);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -10;
sun.shadow.camera.right = 10;
sun.shadow.camera.top = 10;
sun.shadow.camera.bottom = -10;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 28;
sun.shadow.bias = -0.00035;
sun.shadow.normalBias = 0.025;
scene.add(sun);
scene.add(sun.target);
const fill = new THREE.DirectionalLight(0xaed5d2, 0.75);
fill.position.set(7, 5, -6);
scene.add(fill);
// ------------------------------------------------------------
// Shared dimensions and helpers
// ------------------------------------------------------------
const BLOCK_WIDTH = 4.4;
const BLOCK_DEPTH = 2.8;
const FRONT_Z = BLOCK_DEPTH / 2;
const BACK_Z = -BLOCK_DEPTH / 2;
const BOTTOM_Y = 0.18;
const FRONT_TOP_Y = 1.25;
const BACK_TOP_Y = 2.65;
const LEFT_X = -2.85;
const RIGHT_X = 2.85;
function surfaceY(z) {
const t = (z - BACK_Z) / BLOCK_DEPTH;
return BACK_TOP_Y + (FRONT_TOP_Y - BACK_TOP_Y) * t;
}
function mulberry32(seed) {
return function random() {
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 makeWedgeGeometry() {
const w = BLOCK_WIDTH / 2;
const d = BLOCK_DEPTH / 2;
const b = BOTTOM_Y;
const bt = BACK_TOP_Y;
const ft = FRONT_TOP_Y;
const v = [
[-w, b, -d], [ w, b, -d], [ w, b, d], [-w, b, d],
[-w, bt, -d], [ w, bt, -d], [ w, ft, d], [-w, ft, d]
];
const positions = [];
function tri(a, b, c) {
positions.push(...v[a], ...v[b], ...v[c]);
}
// Top, bottom, front, back, right and left faces.
tri(4, 7, 6); tri(4, 6, 5);
tri(0, 1, 2); tri(0, 2, 3);
tri(3, 6, 7); tri(3, 2, 6);
tri(0, 4, 5); tri(0, 5, 1);
tri(1, 5, 6); tri(1, 6, 2);
tri(0, 3, 7); tri(0, 7, 4);
const geometry = new THREE.BufferGeometry();
geometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(positions, 3)
);
geometry.computeVertexNormals();
return geometry;
}
function makeTopGeometry() {
const w = BLOCK_WIDTH / 2;
const positions = [
-w, BACK_TOP_Y + 0.008, BACK_Z,
-w, FRONT_TOP_Y + 0.008, FRONT_Z,
w, FRONT_TOP_Y + 0.008, FRONT_Z,
-w, BACK_TOP_Y + 0.008, BACK_Z,
w, FRONT_TOP_Y + 0.008, FRONT_Z,
w, BACK_TOP_Y + 0.008, BACK_Z
];
const geometry = new THREE.BufferGeometry();
geometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(positions, 3)
);
geometry.computeVertexNormals();
return geometry;
}
// Shared ground and comparison plinth.
const ground = new THREE.Mesh(
new THREE.PlaneGeometry(40, 40),
new THREE.MeshStandardMaterial({
color: 0xeee6d6,
roughness: 1
})
);
ground.rotation.x = -Math.PI / 2;
ground.position.y = -0.015;
ground.receiveShadow = true;
scene.add(ground);
const plinth = new THREE.Mesh(
new THREE.BoxGeometry(11.5, 0.18, 4.9),
new THREE.MeshStandardMaterial({
color: 0xd8cdb9,
roughness: 0.95
})
);
plinth.position.set(0, 0.09, 0.15);
plinth.receiveShadow = true;
plinth.castShadow = true;
scene.add(plinth);
const substrateMaterial = new THREE.MeshStandardMaterial({
color: 0x81432f,
roughness: 0.98,
flatShading: true
});
const topMaterial = new THREE.MeshStandardMaterial({
color: 0xa75b40,
roughness: 1,
flatShading: true,
polygonOffset: true,
polygonOffsetFactor: -1,
polygonOffsetUnits: -1
});
const edgeMaterial = new THREE.LineBasicMaterial({
color: 0x573126,
transparent: true,
opacity: 0.55
});
const furrowMaterial = new THREE.MeshStandardMaterial({
color: 0x6b3529,
roughness: 1,
flatShading: true
});
function addSoilBlock(x) {
const group = new THREE.Group();
group.position.x = x;
const wedge = makeWedgeGeometry();
const soil = new THREE.Mesh(wedge, substrateMaterial);
soil.castShadow = true;
soil.receiveShadow = true;
group.add(soil);
const top = new THREE.Mesh(makeTopGeometry(), topMaterial);
top.receiveShadow = true;
group.add(top);
const edges = new THREE.LineSegments(
new THREE.EdgesGeometry(wedge, 18),
edgeMaterial
);
group.add(edges);
// Three matching cultivation furrows on both blocks.
for (const z of [-0.82, -0.04, 0.74]) {
const points = [];
for (let i = 0; i <= 8; i++) {
const px = -1.98 + (3.96 * i) / 8;
points.push(new THREE.Vector3(px, surfaceY(z) + 0.025, z));
}
const curve = new THREE.CatmullRomCurve3(points);
const furrow = new THREE.Mesh(
new THREE.TubeGeometry(curve, 24, 0.016, 4, false),
furrowMaterial
);
group.add(furrow);
}
scene.add(group);
return group;
}
const leftBlock = addSoilBlock(LEFT_X);
const rightBlock = addSoilBlock(RIGHT_X);
// ------------------------------------------------------------
// Stylized crops and ground cover
// ------------------------------------------------------------
const stemGeometry = new THREE.CylinderGeometry(0.042, 0.062, 1, 6);
stemGeometry.translate(0, 0.5, 0);
const leafGeometry = new THREE.ConeGeometry(0.15, 1, 4);
leafGeometry.translate(0, 0.5, 0);
const cropMaterials = [
new THREE.MeshStandardMaterial({
color: 0x4c814a,
roughness: 0.95,
flatShading: true
}),
new THREE.MeshStandardMaterial({
color: 0x68944f,
roughness: 0.95,
flatShading: true
}),
new THREE.MeshStandardMaterial({
color: 0x376d43,
roughness: 0.95,
flatShading: true
})
];
const stemMaterial = new THREE.MeshStandardMaterial({
color: 0x738b43,
roughness: 1,
flatShading: true
});
function addCrop(parent, x, z, height, scale = 1, rotation = 0) {
const plant = new THREE.Group();
plant.position.set(x, surfaceY(z), z);
plant.rotation.y = rotation;
const stem = new THREE.Mesh(stemGeometry, stemMaterial);
stem.scale.set(scale, height, scale);
stem.castShadow = true;
plant.add(stem);
const nodes = [0.23, 0.4, 0.57, 0.73, 0.86];
nodes.forEach((node, i) => {
const pivot = new THREE.Group();
pivot.position.y = height * node;
pivot.rotation.y = rotation + i * 2.37;
const leaf = new THREE.Mesh(
leafGeometry,
cropMaterials[i % cropMaterials.length]
);
const direction = i % 2 === 0 ? 1 : -1;
leaf.rotation.z = direction * (0.72 + (i % 3) * 0.1);
leaf.rotation.x = -0.14;
leaf.scale.set(
scale * 0.9,
height * (0.30 + (i % 2) * 0.08),
scale * 0.28
);
leaf.castShadow = true;
pivot.add(leaf);
plant.add(pivot);
});
parent.add(plant);
}
// A prominent crop at the cut face connects visually to the roots.
addCrop(leftBlock, 0, FRONT_Z - 0.018, 1.55, 1.18, 0.2);
// Smaller crop rows across the protected block.
const cropRandom = mulberry32(44);
for (const z of [-0.83, 0.02, 0.73]) {
for (const x of [-1.42, -0.48, 0.48, 1.42]) {
addCrop(
leftBlock,
x + (cropRandom() - 0.5) * 0.09,
z + (cropRandom() - 0.5) * 0.07,
0.62 + cropRandom() * 0.25,
0.68 + cropRandom() * 0.12,
cropRandom() * Math.PI
);
}
}
// Instanced grass-like blades make the protected surface visibly covered.
const bladeGeometry = new THREE.ConeGeometry(0.055, 1, 4);
bladeGeometry.translate(0, 0.5, 0);
const bladeMaterial = new THREE.MeshStandardMaterial({
color: 0xffffff,
roughness: 1,
flatShading: true
});
const bladeCount = 150;
const blades = new THREE.InstancedMesh(
bladeGeometry,
bladeMaterial,
bladeCount
);
const dummy = new THREE.Object3D();
const coverRandom = mulberry32(912);
for (let i = 0; i < bladeCount; i++) {
const x = -2.08 + coverRandom() * 4.16;
const z = -1.29 + coverRandom() * 2.55;
const height = 0.22 + coverRandom() * 0.31;
dummy.position.set(x, surfaceY(z) + 0.012, z);
dummy.rotation.set(
(coverRandom() - 0.5) * 0.32,
coverRandom() * Math.PI * 2,
(coverRandom() - 0.5) * 0.32
);
dummy.scale.set(
0.75 + coverRandom() * 0.7,
height,
0.75 + coverRandom() * 0.7
);
dummy.updateMatrix();
blades.setMatrixAt(i, dummy.matrix);
blades.setColorAt(
i,
new THREE.Color().setHSL(
0.27 + coverRandom() * 0.035,
0.32 + coverRandom() * 0.18,
0.31 + coverRandom() * 0.15
)
);
}
blades.instanceMatrix.needsUpdate = true;
blades.instanceColor.needsUpdate = true;
blades.castShadow = true;
blades.receiveShadow = true;
leftBlock.add(blades);
// ------------------------------------------------------------
// Visible root network on the front cut face
// ------------------------------------------------------------
const rootMaterials = [
new THREE.MeshStandardMaterial({
color: 0xefc98b,
roughness: 0.88,
flatShading: true
}),
new THREE.MeshStandardMaterial({
color: 0xd7aa68,
roughness: 0.92,
flatShading: true
})
];
function addRoot(parent, points, radius, material) {
const vectors = points.map(
(point) => new THREE.Vector3(point[0], point[1], point[2])
);
const curve = new THREE.CatmullRomCurve3(
vectors,
false,
"catmullrom",
0.16
);
const root = new THREE.Mesh(
new THREE.TubeGeometry(
curve,
Math.max(14, points.length * 5),
radius,
5,
false
),
material
);
root.castShadow = true;
parent.add(root);
return root;
}
const rootZ = FRONT_Z + 0.035;
addRoot(leftBlock, [
[0, FRONT_TOP_Y + 0.01, rootZ],
[-0.04, 1.03, rootZ],
[0.03, 0.82, rootZ],
[-0.02, 0.58, rootZ],
[0.05, 0.31, rootZ]
], 0.074, rootMaterials[0]);
const rootBranches = [
[[-0.02, 1.02], [-0.27, 0.90], [-0.56, 0.79], [-0.96, 0.75]],
[[0.02, 0.86], [0.29, 0.73], [0.58, 0.61], [0.93, 0.57]],
[[-0.01, 0.63], [-0.22, 0.52], [-0.45, 0.40], [-0.82, 0.35]],
[[0.03, 0.45], [0.27, 0.36], [0.52, 0.26], [0.84, 0.24]],
[[-0.01, 0.34], [-0.19, 0.28], [-0.42, 0.22], [-0.68, 0.205]],
[[0.04, 0.31], [0.19, 0.24], [0.40, 0.205]]
];
rootBranches.forEach((branch) => {
addRoot(
leftBlock,
branch.map(([x, y]) => [x, y, rootZ]),
0.041,
rootMaterials[1]
);
});
const rootForks = [
[[-0.56, 0.79], [-0.72, 0.68], [-0.95, 0.65]],
[[0.58, 0.61], [0.73, 0.49], [0.98, 0.46]],
[[-0.45, 0.40], [-0.59, 0.30], [-0.82, 0.27]],
[[0.52, 0.26], [0.68, 0.205], [0.91, 0.205]]
];
rootForks.forEach((fork) => {
addRoot(
leftBlock,
fork.map(([x, y]) => [x, y, rootZ + 0.002]),
0.023,
rootMaterials[0]
);
});
// ------------------------------------------------------------
// Loose surface particles
// ------------------------------------------------------------
const particleGeometry = new THREE.DodecahedronGeometry(1, 0);
const particleMaterials = [
new THREE.MeshStandardMaterial({
color: 0xd99549,
roughness: 1,
flatShading: true
}),
new THREE.MeshStandardMaterial({
color: 0xc77c3f,
roughness: 1,
flatShading: true
}),
new THREE.MeshStandardMaterial({
color: 0xe3ae68,
roughness: 1,
flatShading: true
})
];
const particleRandom = mulberry32(202503);
const particleRecords = [];
for (let i = 0; i < 135; i++) {
particleRecords.push({
x: -2.08 + particleRandom() * 4.16,
z: -1.28 + particleRandom() * 2.55,
sx: 0.07 + particleRandom() * 0.10,
sy: 0.045 + particleRandom() * 0.055,
sz: 0.07 + particleRandom() * 0.09,
rx: particleRandom() * Math.PI,
ry: particleRandom() * Math.PI,
rz: particleRandom() * Math.PI,
color: Math.floor(particleRandom() * 3),
phase: particleRandom(),
speed: 0.075 + particleRandom() * 0.045
});
}
function addStaticParticles(parent, records, start = 0, end = records.length) {
const count = end - start;
const material = new THREE.MeshStandardMaterial({
color: 0xffffff,
roughness: 1,
flatShading: true
});
const mesh = new THREE.InstancedMesh(
particleGeometry,
material,
count
);
for (let i = start; i < end; i++) {
const record = records[i];
const index = i - start;
dummy.position.set(
record.x,
surfaceY(record.z) + record.sy * 0.58,
record.z
);
dummy.rotation.set(record.rx, record.ry, record.rz);
dummy.scale.set(record.sx, record.sy, record.sz);
dummy.updateMatrix();
mesh.setMatrixAt(index, dummy.matrix);
mesh.setColorAt(
index,
new THREE.Color([
0xd99549,
0xc77c3f,
0xe3ae68
][record.color])
);
}
mesh.instanceMatrix.needsUpdate = true;
mesh.instanceColor.needsUpdate = true;
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
// The protected block retains every surface grain.
addStaticParticles(leftBlock, particleRecords);
// The bare block starts with the same grains, but a subset is now moving.
const movingCount = 26;
addStaticParticles(rightBlock, particleRecords, movingCount);
const movingParticles = [];
for (let i = 0; i < movingCount; i++) {
const record = particleRecords[i];
const mesh = new THREE.Mesh(
particleGeometry,
particleMaterials[record.color]
);
mesh.scale.set(record.sx, record.sy, record.sz);
mesh.castShadow = true;
rightBlock.add(mesh);
movingParticles.push({ mesh, record });
}
// A visible sediment fan at the downhill toe.
const sedimentRandom = mulberry32(705);
const sedimentCount = 30;
const sediment = new THREE.InstancedMesh(
particleGeometry,
new THREE.MeshStandardMaterial({
color: 0xffffff,
roughness: 1,
flatShading: true
}),
sedimentCount
);
for (let i = 0; i < sedimentCount; i++) {
const sx = 0.075 + sedimentRandom() * 0.11;
const sy = 0.045 + sedimentRandom() * 0.06;
const sz = 0.07 + sedimentRandom() * 0.12;
dummy.position.set(
-0.72 + sedimentRandom() * 2.05,
0.18 + sy * 0.55,
FRONT_Z + 0.12 + sedimentRandom() * 1.03
);
dummy.rotation.set(
sedimentRandom() * Math.PI,
sedimentRandom() * Math.PI,
sedimentRandom() * Math.PI
);
dummy.scale.set(sx, sy, sz);
dummy.updateMatrix();
sediment.setMatrixAt(i, dummy.matrix);
sediment.setColorAt(
i,
new THREE.Color([
0xd99549,
0xc77c3f,
0xe3ae68
][i % 3])
);
}
sediment.instanceMatrix.needsUpdate = true;
sediment.instanceColor.needsUpdate = true;
sediment.castShadow = true;
sediment.receiveShadow = true;
rightBlock.add(sediment);
// ------------------------------------------------------------
// Stylized runoff droplets
// ------------------------------------------------------------
const waterGeometry = new THREE.SphereGeometry(1, 6, 4);
const waterMaterial = new THREE.MeshStandardMaterial({
color: 0x70bdc5,
roughness: 0.35,
transparent: true,
opacity: 0.72,
depthWrite: false
});
const runoff = [];
const waterRandom = mulberry32(331);
for (let i = 0; i < 13; i++) {
const mesh = new THREE.Mesh(waterGeometry, waterMaterial);
mesh.scale.set(0.035, 0.025, 0.075);
rightBlock.add(mesh);
runoff.push({
mesh,
x: -1.8 + waterRandom() * 3.6,
phase: waterRandom(),
speed: 0.13 + waterRandom() * 0.07
});
}
// ------------------------------------------------------------
// Camera controls implemented directly in this file
// ------------------------------------------------------------
const target = new THREE.Vector3(0, 1.1, 0);
let theta = 0.28;
let phi = 1.18;
let radius = innerWidth < 700 ? 14.8 : 12.8;
function updateCamera() {
const horizontal = radius * Math.sin(phi);
camera.position.set(
target.x + horizontal * Math.sin(theta),
target.y + radius * Math.cos(phi),
target.z + horizontal * Math.cos(theta)
);
camera.lookAt(target);
camera.updateMatrixWorld();
}
function resetView() {
theta = 0.28;
phi = 1.18;
radius = innerWidth < 700 ? 14.8 : 12.8;
updateCamera();
}
const pointers = new Map();
let pinchDistance = 0;
function currentPinchDistance() {
const values = [...pointers.values()];
if (values.length < 2) return 0;
return Math.hypot(
values[0].x - values[1].x,
values[0].y - values[1].y
);
}
renderer.domElement.addEventListener("pointerdown", (event) => {
renderer.domElement.setPointerCapture(event.pointerId);
pointers.set(event.pointerId, {
x: event.clientX,
y: event.clientY
});
if (pointers.size === 2) {
pinchDistance = currentPinchDistance();
}
renderer.domElement.style.cursor = "grabbing";
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (!pointers.has(event.pointerId)) return;
const previousDistance = currentPinchDistance();
const previous = pointers.get(event.pointerId);
const dx = event.clientX - previous.x;
const dy = event.clientY - previous.y;
pointers.set(event.pointerId, {
x: event.clientX,
y: event.clientY
});
if (pointers.size === 1) {
theta -= dx * 0.0055;
phi = THREE.MathUtils.clamp(
phi + dy * 0.0045,
0.56,
1.43
);
} else if (pointers.size === 2) {
const distance = currentPinchDistance();
if (previousDistance > 0 && distance > 0) {
radius *= previousDistance / distance;
}
pinchDistance = distance;
radius = THREE.MathUtils.clamp(radius, 8.5, 19);
}
});
function endPointer(event) {
pointers.delete(event.pointerId);
if (pointers.size < 2) {
pinchDistance = 0;
}
renderer.domElement.style.cursor = "grab";
}
renderer.domElement.addEventListener("pointerup", endPointer);
renderer.domElement.addEventListener("pointercancel", endPointer);
renderer.domElement.addEventListener("contextmenu", (e) => e.preventDefault());
renderer.domElement.addEventListener(
"wheel",
(event) => {
event.preventDefault();
radius *= Math.exp(event.deltaY * 0.001);
radius = THREE.MathUtils.clamp(radius, 8.5, 19);
},
{ passive: false }
);
renderer.domElement.style.cursor = "grab";
document.querySelector("#reset-view").addEventListener("click", resetView);
// ------------------------------------------------------------
// Annotation lines and labels
// ------------------------------------------------------------
const svg = document.querySelector("#leaders");
const svgNS = "http://www.w3.org/2000/svg";
const annotationDefinitions = [
{
id: "ann-cover",
anchor: new THREE.Vector3(LEFT_X, 3.28, 0.05),
offset: [-35, -30],
color: "#4d7f49"
},
{
id: "ann-roots",
anchor: new THREE.Vector3(LEFT_X + 0.05, 0.78, FRONT_Z + 0.06),
offset: [-105, 13],
color: "#d5aa68"
},
{
id: "ann-bare",
anchor: new THREE.Vector3(RIGHT_X, 2.48, -0.08),
offset: [42, -27],
color: "#a65337"
},
{
id: "ann-loose",
anchor: new THREE.Vector3(RIGHT_X + 1.52, 1.80, 0.34),
offset: [78, -10],
color: "#dc9448"
},
{
id: "ann-substrate",
anchor: new THREE.Vector3(RIGHT_X + 0.67, 0.57, FRONT_Z + 0.02),
offset: [66, 28],
color: "#81432f"
}
];
annotationDefinitions.forEach((definition) => {
definition.element = document.querySelector(`#${definition.id}`);
definition.line = document.createElementNS(svgNS, "line");
definition.line.setAttribute("stroke", definition.color);
svg.appendChild(definition.line);
definition.dot = document.createElementNS(svgNS, "circle");
definition.dot.setAttribute("r", "4.5");
definition.dot.setAttribute("fill", definition.color);
svg.appendChild(definition.dot);
});
function updateAnnotations() {
const width = innerWidth;
const height = innerHeight;
annotationDefinitions.forEach((definition) => {
const projected = definition.anchor.clone().project(camera);
const anchorX = (projected.x * 0.5 + 0.5) * width;
const anchorY = (-projected.y * 0.5 + 0.5) * height;
const labelX = THREE.MathUtils.clamp(
anchorX + definition.offset[0],
70,
width - 70
);
const labelY = THREE.MathUtils.clamp(
anchorY + definition.offset[1],
90,
height - 85
);
const visible =
projected.z > -1 &&
projected.z < 1 &&
anchorX > -100 &&
anchorX < width + 100 &&
anchorY > -100 &&
anchorY < height + 100;
definition.element.style.opacity = visible ? "1" : "0";
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);
definition.line.style.display = visible ? "" : "none";
definition.dot.setAttribute("cx", anchorX);
definition.dot.setAttribute("cy", anchorY);
definition.dot.style.display = visible ? "" : "none";
});
}
// ------------------------------------------------------------
// Animation
// ------------------------------------------------------------
const clock = new THREE.Clock();
const prefersReducedMotion = matchMedia(
"(prefers-reduced-motion: reduce)"
).matches;
let running = !prefersReducedMotion;
let elapsed = 0;
const pauseButton = document.querySelector("#pause");
function updatePauseButton() {
pauseButton.textContent = running ? "Pause motion" : "Resume motion";
pauseButton.setAttribute("aria-pressed", String(!running));
}
updatePauseButton();
pauseButton.addEventListener("click", () => {
running = !running;
updatePauseButton();
});
function updateMovingParticles(time) {
movingParticles.forEach(({ mesh, record }) => {
const phase = (time * record.speed + record.phase) % 1;
if (phase < 0.72) {
const travel = phase / 0.72;
mesh.position.x =
record.x + Math.sin(travel * 8 + record.phase * 9) * 0.035;
mesh.position.z =
THREE.MathUtils.lerp(record.z, FRONT_Z + 0.18, travel);
mesh.position.y =
surfaceY(mesh.position.z) + record.sy * 0.58;
} else {
const fall = (phase - 0.72) / 0.28;
mesh.position.z = FRONT_Z + 0.18 + fall * 0.94;
mesh.position.x = record.x + fall * 0.08;
const startY = FRONT_TOP_Y - 0.02;
mesh.position.y = THREE.MathUtils.lerp(
startY,
0.20,
fall * fall
);
}
mesh.rotation.set(
record.rx + phase * 8,
record.ry + phase * 5,
record.rz + phase * 4
);
});
}
function updateRunoff(time) {
runoff.forEach(({ mesh, x, phase, speed }) => {
const progress = (time * speed + phase) % 1;
const z = -1.28 + progress * 3.02;
mesh.position.x =
x + Math.sin(progress * 8 + phase * 12) * 0.07;
mesh.position.z = z;
if (z <= FRONT_Z) {
mesh.position.y = surfaceY(z) + 0.12;
} else {
const fall = (z - FRONT_Z) / 0.9;
mesh.position.y = THREE.MathUtils.lerp(
FRONT_TOP_Y + 0.05,
0.20,
fall * fall
);
}
mesh.rotation.y = phase * Math.PI * 2 + time;
});
}
function animate() {
requestAnimationFrame(animate);
const delta = Math.min(clock.getDelta(), 0.05);
if (running) elapsed += delta;
updateMovingParticles(elapsed);
updateRunoff(elapsed);
updateCamera();
updateAnnotations();
renderer.render(scene, camera);
}
function resize() {
camera.aspect = innerWidth / innerHeight;
camera.fov = innerWidth < 700 ? 51 : 42;
camera.updateProjectionMatrix();
renderer.setPixelRatio(Math.min(devicePixelRatio, 2));
renderer.setSize(innerWidth, innerHeight);
svg.setAttribute("viewBox", `0 0 ${innerWidth} ${innerHeight}`);
}
addEventListener("resize", resize);
resize();
resetView();
animate();
</script>
</body>
</html>
```