Prompt specification
Build or customize the Three.js 3D scene "Soil management teaching plot" 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: Soil management teaching plot
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/soil-management-teaching-plot-x1188
Scene Description Prompt:
Toggle soil cover and compaction on a voxel garden cutaway; compare illustrative infiltration under identical rainfall. Three.js from cdnjs in one HTML file.
Working Three.js Starter (soil-management-teaching-plot-x1188):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Voxel Garden — Infiltration Lab</title>
<style>
:root {
--paper: #f4f1e8;
--panel: #fffdf8;
--ink: #20312b;
--muted: #69736d;
--line: #d8dbd2;
--green: #315c45;
--green-soft: #e4eee6;
--lime: #b9d66b;
--blue: #3ba8bd;
--blue-soft: #ddf1f3;
--sand: #c8b58b;
--shadow: 0 18px 45px rgba(39, 54, 46, 0.12);
}
* {
box-sizing: border-box;
}
body {
margin: 0;
color: var(--ink);
background:
radial-gradient(circle at 10% 0%, rgba(185, 214, 107, 0.12), transparent 28rem),
var(--paper);
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
button,
input {
font: inherit;
}
button {
color: inherit;
}
.app {
width: min(1480px, calc(100% - 32px));
margin: 0 auto;
padding: 26px 0 34px;
}
header {
display: flex;
align-items: flex-end;
justify-content: space-between;
gap: 24px;
margin-bottom: 20px;
}
.eyebrow {
display: flex;
align-items: center;
gap: 9px;
margin-bottom: 9px;
color: var(--green);
font-size: 0.73rem;
font-weight: 800;
letter-spacing: 0.14em;
text-transform: uppercase;
}
.eyebrow::before {
content: "";
width: 23px;
height: 3px;
border-radius: 3px;
background: var(--lime);
}
h1 {
max-width: 850px;
margin: 0;
font-family: Georgia, "Times New Roman", serif;
font-size: clamp(2rem, 4vw, 4rem);
font-weight: 500;
line-height: 0.98;
letter-spacing: -0.045em;
}
.header-note {
max-width: 340px;
margin: 0 0 4px;
color: var(--muted);
font-size: 0.9rem;
line-height: 1.55;
}
.layout {
display: grid;
grid-template-columns: minmax(0, 1fr) 380px;
gap: 18px;
align-items: start;
}
.stage {
position: relative;
height: min(760px, calc(100vh - 150px));
min-height: 590px;
overflow: hidden;
border: 1px solid rgba(89, 101, 93, 0.18);
border-radius: 24px;
background: #e9e8df;
box-shadow: var(--shadow);
isolation: isolate;
}
#scene {
position: absolute;
inset: 0;
}
#scene canvas {
display: block;
width: 100%;
height: 100%;
outline: none;
touch-action: none;
}
.stage-hud {
position: absolute;
z-index: 3;
top: 18px;
left: 18px;
right: 18px;
display: flex;
justify-content: space-between;
align-items: flex-start;
gap: 12px;
pointer-events: none;
}
.pill {
display: inline-flex;
align-items: center;
gap: 8px;
min-height: 34px;
padding: 8px 12px;
border: 1px solid rgba(42, 57, 49, 0.12);
border-radius: 999px;
background: rgba(255, 253, 248, 0.88);
box-shadow: 0 7px 24px rgba(39, 54, 46, 0.08);
backdrop-filter: blur(12px);
color: #394b43;
font-size: 0.75rem;
font-weight: 750;
}
.pill .dot {
width: 7px;
height: 7px;
border-radius: 50%;
background: var(--blue);
box-shadow: 0 0 0 4px rgba(59, 168, 189, 0.13);
}
.stage-footer {
position: absolute;
z-index: 3;
right: 18px;
bottom: 17px;
left: 18px;
display: flex;
align-items: flex-end;
justify-content: space-between;
gap: 14px;
pointer-events: none;
}
.legend {
display: flex;
flex-wrap: wrap;
gap: 7px;
}
.legend span {
display: inline-flex;
align-items: center;
gap: 6px;
padding: 7px 9px;
border-radius: 9px;
background: rgba(255, 253, 248, 0.83);
color: #53615b;
font-size: 0.68rem;
font-weight: 700;
backdrop-filter: blur(10px);
}
.swatch {
width: 8px;
height: 8px;
border-radius: 2px;
}
.camera-hint {
padding: 7px 10px;
border-radius: 9px;
background: rgba(32, 49, 43, 0.78);
color: #f4f7f2;
font-size: 0.67rem;
white-space: nowrap;
backdrop-filter: blur(10px);
}
.camera-hint button {
margin-left: 8px;
padding: 0;
border: 0;
border-bottom: 1px solid rgba(255,255,255,0.65);
background: transparent;
color: #fff;
font-size: inherit;
font-weight: 800;
cursor: pointer;
pointer-events: auto;
}
.panel {
display: grid;
gap: 14px;
}
.card {
padding: 19px;
border: 1px solid rgba(89, 101, 93, 0.16);
border-radius: 20px;
background: rgba(255, 253, 248, 0.92);
box-shadow: 0 9px 28px rgba(39, 54, 46, 0.07);
}
.card-heading {
display: flex;
justify-content: space-between;
align-items: center;
gap: 12px;
margin-bottom: 15px;
}
.card h2 {
margin: 0;
font-size: 0.93rem;
font-weight: 800;
letter-spacing: -0.01em;
}
.mini-label {
color: var(--muted);
font-size: 0.67rem;
font-weight: 750;
text-transform: uppercase;
letter-spacing: 0.09em;
}
.scenario-toggle {
display: flex;
align-items: center;
justify-content: space-between;
gap: 18px;
padding: 13px 0;
border-top: 1px solid var(--line);
cursor: pointer;
}
.scenario-toggle:first-of-type {
border-top: 0;
padding-top: 2px;
}
.scenario-toggle strong {
display: block;
margin-bottom: 4px;
font-size: 0.85rem;
}
.scenario-toggle small {
display: block;
color: var(--muted);
font-size: 0.7rem;
line-height: 1.35;
}
.switch {
position: relative;
flex: 0 0 auto;
width: 43px;
height: 24px;
}
.switch input {
position: absolute;
opacity: 0;
pointer-events: none;
}
.switch-track {
position: absolute;
inset: 0;
border-radius: 999px;
background: #cfd4cd;
transition: background 180ms ease;
cursor: pointer;
}
.switch-track::after {
content: "";
position: absolute;
top: 3px;
left: 3px;
width: 18px;
height: 18px;
border-radius: 50%;
background: #fff;
box-shadow: 0 2px 7px rgba(0,0,0,0.18);
transition: transform 180ms ease;
}
.switch input:checked + .switch-track {
background: var(--green);
}
.switch input:checked + .switch-track::after {
transform: translateX(19px);
}
.rain-facts {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 7px;
margin-bottom: 15px;
}
.fact {
padding: 10px 8px;
border-radius: 11px;
background: #f0f2eb;
text-align: center;
}
.fact b {
display: block;
margin-bottom: 3px;
font-family: Georgia, serif;
font-size: 1.05rem;
font-weight: 600;
}
.fact span {
color: var(--muted);
font-size: 0.61rem;
font-weight: 700;
}
.timeline {
display: flex;
align-items: center;
gap: 11px;
}
.play-button {
flex: 0 0 auto;
min-width: 99px;
padding: 10px 13px;
border: 0;
border-radius: 11px;
background: var(--green);
color: white;
font-size: 0.73rem;
font-weight: 800;
cursor: pointer;
transition: transform 150ms ease, background 150ms ease;
}
.play-button:hover {
background: #274b38;
transform: translateY(-1px);
}
.progress-wrap {
min-width: 0;
flex: 1;
}
.progress-track {
height: 7px;
overflow: hidden;
border-radius: 99px;
background: #dfe3dc;
}
.progress-fill {
width: 0;
height: 100%;
border-radius: inherit;
background: linear-gradient(90deg, var(--blue), #76c8d5);
}
.time-row {
display: flex;
justify-content: space-between;
margin-top: 7px;
color: var(--muted);
font-size: 0.65rem;
font-weight: 700;
}
.metric-main {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 9px;
margin-bottom: 14px;
}
.metric {
padding: 13px;
border-radius: 13px;
background: #f2f3ee;
}
.metric.current {
background: var(--blue-soft);
}
.metric span {
display: block;
margin-bottom: 5px;
color: var(--muted);
font-size: 0.63rem;
font-weight: 750;
text-transform: uppercase;
letter-spacing: 0.06em;
}
.metric b {
font-family: Georgia, serif;
font-size: 1.7rem;
font-weight: 500;
letter-spacing: -0.04em;
}
.metric small {
margin-left: 2px;
color: var(--muted);
font-size: 0.69rem;
font-weight: 700;
}
.bar-row {
margin-top: 12px;
}
.bar-label {
display: flex;
justify-content: space-between;
gap: 12px;
margin-bottom: 6px;
color: #47564f;
font-size: 0.69rem;
font-weight: 750;
}
.bar-track {
height: 9px;
overflow: hidden;
border-radius: 99px;
background: #e3e6df;
}
.bar-fill {
height: 100%;
border-radius: inherit;
transition: width 120ms linear;
}
.bar-fill.current {
background: linear-gradient(90deg, #2c94aa, #62c2d1);
}
.bar-fill.baseline {
background: #879b8d;
}
.delta {
display: flex;
align-items: flex-start;
gap: 10px;
margin-top: 16px;
padding: 12px;
border-radius: 12px;
background: var(--green-soft);
}
.delta-icon {
display: grid;
place-items: center;
flex: 0 0 auto;
width: 25px;
height: 25px;
border-radius: 50%;
background: var(--green);
color: white;
font-size: 0.75rem;
font-weight: 900;
}
.delta b {
display: block;
margin-bottom: 3px;
font-size: 0.72rem;
}
.delta p {
margin: 0;
color: #53625a;
font-size: 0.67rem;
line-height: 1.45;
}
.matrix {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 8px;
}
.matrix-card {
min-width: 0;
padding: 11px;
border: 1px solid var(--line);
border-radius: 12px;
background: #fafaf6;
text-align: left;
cursor: pointer;
transition: border 150ms ease, transform 150ms ease, background 150ms ease;
}
.matrix-card:hover {
transform: translateY(-1px);
border-color: #aeb9ae;
}
.matrix-card.selected {
border-color: var(--green);
background: var(--green-soft);
box-shadow: inset 0 0 0 1px var(--green);
}
.matrix-card .name {
display: block;
min-height: 31px;
margin-bottom: 8px;
color: #4e5e56;
font-size: 0.66rem;
font-weight: 750;
line-height: 1.25;
}
.matrix-card .number {
display: flex;
justify-content: space-between;
align-items: baseline;
margin-bottom: 7px;
}
.matrix-card .number b {
font-family: Georgia, serif;
font-size: 1.18rem;
font-weight: 500;
}
.matrix-card .number span {
color: var(--muted);
font-size: 0.59rem;
}
.matrix-bar {
height: 5px;
overflow: hidden;
border-radius: 99px;
background: #e0e4dc;
}
.matrix-bar i {
display: block;
height: 100%;
border-radius: inherit;
background: var(--blue);
}
.method {
margin: 0;
color: var(--muted);
font-size: 0.66rem;
line-height: 1.5;
}
.method strong {
color: #44544c;
}
.fallback {
position: absolute;
inset: 0;
display: none;
place-items: center;
padding: 30px;
color: var(--ink);
text-align: center;
}
@media (max-width: 1050px) {
.layout {
grid-template-columns: 1fr;
}
.stage {
height: 650px;
}
.panel {
grid-template-columns: repeat(2, minmax(0, 1fr));
}
.panel .card:last-child {
grid-column: 1 / -1;
}
}
@media (max-width: 680px) {
.app {
width: min(100% - 18px, 1480px);
padding-top: 18px;
}
header {
align-items: flex-start;
flex-direction: column;
gap: 12px;
}
.header-note {
max-width: 100%;
}
.stage {
height: 570px;
min-height: 0;
border-radius: 18px;
}
.stage-hud {
top: 11px;
left: 11px;
right: 11px;
}
.stage-hud .pill:last-child {
display: none;
}
.stage-footer {
right: 11px;
bottom: 11px;
left: 11px;
}
.camera-hint {
display: none;
}
.panel {
grid-template-columns: 1fr;
}
.panel .card:last-child {
grid-column: auto;
}
}
</style>
</head>
<body>
<main class="app">
<header>
<div>
<div class="eyebrow">Soil water lab / 01</div>
<h1>What changes when rain hits the garden?</h1>
</div>
<p class="header-note">
Toggle the surface cover and soil structure. Every scenario receives the same
30-minute rainstorm so the water balance can be compared directly.
</p>
</header>
<div class="layout">
<section class="stage" aria-label="Interactive three-dimensional voxel garden cutaway">
<div id="scene"></div>
<div class="stage-hud">
<div class="pill">
<span class="dot"></span>
<span id="sceneScenario">Covered · Loose</span>
</div>
<div class="pill">Matched rain · 40 mm/h</div>
</div>
<div class="stage-footer">
<div class="legend" aria-hidden="true">
<span><i class="swatch" style="background:#7d9b58"></i>Cover</span>
<span><i class="swatch" style="background:#9a704e"></i>Soil</span>
<span><i class="swatch" style="background:#3ba8bd"></i>Water</span>
<span><i class="swatch" style="background:#655144"></i>Compacted</span>
</div>
<div class="camera-hint">
Drag to orbit · Scroll to zoom
<button id="resetCamera" type="button">Reset</button>
</div>
</div>
<div class="fallback" id="fallback">
<p>This interactive cutaway requires a browser with WebGL enabled.</p>
</div>
</section>
<aside class="panel">
<section class="card">
<div class="card-heading">
<h2>Garden bed</h2>
<span class="mini-label">Change conditions</span>
</div>
<label class="scenario-toggle">
<span>
<strong>Soil cover</strong>
<small>Living groundcover + a mulch layer</small>
</span>
<span class="switch">
<input id="coverToggle" type="checkbox" checked aria-label="Toggle soil cover">
<span class="switch-track"></span>
</span>
</label>
<label class="scenario-toggle">
<span>
<strong>Soil compaction</strong>
<small>Denser soil with fewer connected pores</small>
</span>
<span class="switch">
<input id="compactToggle" type="checkbox" aria-label="Toggle soil compaction">
<span class="switch-track"></span>
</span>
</label>
</section>
<section class="card">
<div class="card-heading">
<h2>Identical rainfall</h2>
<span class="mini-label">For both beds</span>
</div>
<div class="rain-facts">
<div class="fact"><b>40</b><span>mm / hour</span></div>
<div class="fact"><b>30</b><span>minutes</span></div>
<div class="fact"><b>20</b><span>mm total</span></div>
</div>
<div class="timeline">
<button class="play-button" id="playButton" type="button">Pause rain</button>
<div class="progress-wrap">
<div class="progress-track">
<div class="progress-fill" id="progressFill"></div>
</div>
<div class="time-row">
<span id="timeLabel">0 min elapsed</span>
<span id="rateLabel">Rain active</span>
</div>
</div>
</div>
</section>
<section class="card">
<div class="card-heading">
<h2>Infiltration comparison</h2>
<span class="mini-label">Cumulative</span>
</div>
<div class="metric-main">
<div class="metric current">
<span>Current bed</span>
<b id="currentInfiltration">0.0</b><small>mm</small>
</div>
<div class="metric">
<span>Baseline</span>
<b id="baselineInfiltration">0.0</b><small>mm</small>
</div>
</div>
<div class="bar-row">
<div class="bar-label">
<span id="currentBarLabel">Current · covered, loose</span>
<span id="currentBarValue">0.0 mm</span>
</div>
<div class="bar-track">
<div class="bar-fill current" id="currentBar"></div>
</div>
</div>
<div class="bar-row">
<div class="bar-label">
<span>Baseline · bare, loose</span>
<span id="baselineBarValue">0.0 mm</span>
</div>
<div class="bar-track">
<div class="bar-fill baseline" id="baselineBar"></div>
</div>
</div>
<div class="delta">
<div class="delta-icon">i</div>
<div>
<b id="deltaTitle">Comparing under matched rainfall</b>
<p id="insightText">Watch how the cover and pore structure change the result.</p>
</div>
</div>
</section>
<section class="card">
<div class="card-heading">
<h2>All four scenarios</h2>
<span class="mini-label">At 30 minutes</span>
</div>
<div class="matrix">
<button class="matrix-card" type="button" data-cover="false" data-compact="false">
<span class="name">Bare + loose<br><small>Baseline</small></span>
<span class="number"><b>13.0</b><span>mm</span></span>
<span class="matrix-bar"><i style="width:65%"></i></span>
</button>
<button class="matrix-card" type="button" data-cover="false" data-compact="true">
<span class="name">Bare + compacted</span>
<span class="number"><b>6.0</b><span>mm</span></span>
<span class="matrix-bar"><i style="width:30%"></i></span>
</button>
<button class="matrix-card" type="button" data-cover="true" data-compact="false">
<span class="name">Covered + loose</span>
<span class="number"><b>19.0</b><span>mm</span></span>
<span class="matrix-bar"><i style="width:95%"></i></span>
</button>
<button class="matrix-card" type="button" data-cover="true" data-compact="true">
<span class="name">Covered + compacted</span>
<span class="number"><b>10.5</b><span>mm</span></span>
<span class="matrix-bar"><i style="width:52.5%"></i></span>
</button>
</div>
</section>
<section class="card">
<p class="method">
<strong>Illustrative model.</strong> Infiltration is estimated from simple
effective-rate assumptions: 26 mm/h for bare loose soil, 38 mm/h for covered
loose soil, 12 mm/h for bare compacted soil, and 21 mm/h for covered compacted soil.
These values support visual comparison only and are not a hydrologic forecast.
</p>
</section>
</aside>
</div>
</main>
<script type="module">
import * as THREE from "https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const $ = (id) => document.getElementById(id);
const stage = $("scene");
const fallback = $("fallback");
const state = {
cover: true,
compacted: false,
playing: true,
simMinutes: 0,
durationMinutes: 30,
rainRate: 40
};
const capacities = {
"bare-loose": 26,
"bare-compact": 12,
"covered-loose": 38,
"covered-compact": 21
};
const baselineCapacity = capacities["bare-loose"];
const totalRain = state.rainRate * state.durationMinutes / 60;
function scenarioKey(cover = state.cover, compact = state.compacted) {
return `${cover ? "covered" : "bare"}-${compact ? "compact" : "loose"}`;
}
function balance(capacity, minutes = state.simMinutes) {
const rainfall = state.rainRate * minutes / 60;
const infiltrated = Math.min(rainfall, capacity * minutes / 60);
return {
rainfall,
infiltrated,
runoff: Math.max(0, rainfall - infiltrated)
};
}
function setScenario(cover, compacted) {
state.cover = cover;
state.compacted = compacted;
$("coverToggle").checked = cover;
$("compactToggle").checked = compacted;
coverGroup.visible = cover;
updateSoilPalette();
rebuildPores();
updateUI();
}
// --- Three.js scene ----------------------------------------------------
let renderer;
try {
renderer = new THREE.WebGLRenderer({ antialias: true, alpha: false });
} catch (error) {
fallback.style.display = "grid";
throw error;
}
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.05;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
stage.appendChild(renderer.domElement);
renderer.domElement.tabIndex = 0;
renderer.domElement.setAttribute(
"aria-label",
"Voxel garden soil cutaway. Drag to orbit, scroll to zoom, or use arrow keys."
);
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xe9e8df);
scene.fog = new THREE.Fog(0xe9e8df, 25, 48);
const camera = new THREE.PerspectiveCamera(38, 1, 0.1, 100);
const cameraTarget = new THREE.Vector3(0, -1.25, 0);
let yaw = 0.68;
let pitch = 0.48;
let radius = 18.2;
function updateCamera() {
const horizontal = Math.cos(pitch) * radius;
camera.position.set(
cameraTarget.x + Math.sin(yaw) * horizontal,
cameraTarget.y + Math.sin(pitch) * radius,
cameraTarget.z + Math.cos(yaw) * horizontal
);
camera.lookAt(cameraTarget);
}
function resetCamera() {
yaw = 0.68;
pitch = 0.48;
radius = 18.2;
updateCamera();
}
updateCamera();
scene.add(new THREE.HemisphereLight(0xf8fbf0, 0x746355, 2.0));
const sun = new THREE.DirectionalLight(0xfff8dc, 3.2);
sun.position.set(-8, 14, 9);
sun.castShadow = true;
sun.shadow.mapSize.set(1024, 1024);
sun.shadow.camera.left = -12;
sun.shadow.camera.right = 12;
sun.shadow.camera.top = 12;
sun.shadow.camera.bottom = -12;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 35;
sun.shadow.normalBias = 0.025;
scene.add(sun);
const fill = new THREE.DirectionalLight(0xbdd7e8, 1.1);
fill.position.set(9, 6, -10);
scene.add(fill);
const ground = new THREE.Mesh(
new THREE.PlaneGeometry(60, 60),
new THREE.MeshStandardMaterial({ color: 0xe8e5da, roughness: 1 })
);
ground.rotation.x = -Math.PI / 2;
ground.position.y = -4.08;
ground.receiveShadow = true;
scene.add(ground);
const bedGroup = new THREE.Group();
scene.add(bedGroup);
const NX = 11;
const NZ = 8;
const LAYERS = 4;
const voxelGeometry = new THREE.BoxGeometry(0.96, 0.96, 0.96);
const soilMaterials = [];
const looseColors = [0x936b4c, 0xa07855, 0x795640, 0x6b4e3b];
const compactColors = [0x695347, 0x70564a, 0x5c473d, 0x523f36];
const variation = [-0.035, -0.012, 0.018, 0.045];
for (let layer = 0; layer < LAYERS; layer++) {
for (let variant = 0; variant < 4; variant++) {
const material = new THREE.MeshStandardMaterial({
color: 0xffffff,
roughness: 0.98,
metalness: 0
});
soilMaterials.push({ material, layer, variant });
}
}
function setPaletteColor(entry, hex) {
const color = new THREE.Color(hex);
color.offsetHSL(0, 0, variation[entry.variant]);
entry.material.color.copy(color);
}
function updateSoilPalette() {
const palette = state.compacted ? compactColors : looseColors;
soilMaterials.forEach((entry) => setPaletteColor(entry, palette[entry.layer]));
}
for (let z = 0; z < NZ; z++) {
for (let x = 0; x < NX; x++) {
for (let layer = 0; layer < LAYERS; layer++) {
const variant = Math.abs((x * 7 + z * 11 + layer * 3) % 4);
const material = soilMaterials[layer * 4 + variant].material;
const cube = new THREE.Mesh(voxelGeometry, material);
cube.position.set(x - (NX - 1) / 2, -0.5 - layer, z - (NZ - 1) / 2);
cube.castShadow = true;
cube.receiveShadow = true;
bedGroup.add(cube);
}
}
}
updateSoilPalette();
const edge = new THREE.LineSegments(
new THREE.EdgesGeometry(new THREE.BoxGeometry(NX, 4, NZ)),
new THREE.LineBasicMaterial({ color: 0x574d42, transparent: true, opacity: 0.26 })
);
edge.position.y = -2;
bedGroup.add(edge);
// Soil pores on the exposed cutaway face.
const poreGroup = new THREE.Group();
bedGroup.add(poreGroup);
const poreGeometry = new THREE.BoxGeometry(1, 1, 1);
const poreMaterial = new THREE.MeshBasicMaterial({ color: 0x302820 });
function rebuildPores() {
while (poreGroup.children.length) {
poreGroup.remove(poreGroup.children[0]);
}
const count = state.compacted ? 10 : 24;
for (let i = 0; i < count; i++) {
const r1 = Math.abs(Math.sin((i + 1) * 12.9898) * 43758.5453) % 1;
const r2 = Math.abs(Math.sin((i + 7) * 78.233) * 12543.123) % 1;
const r3 = Math.abs(Math.sin((i + 13) * 39.425) * 9823.213) % 1;
const layer = Math.floor(r3 * LAYERS);
const pore = new THREE.Mesh(poreGeometry, poreMaterial);
pore.position.set(
-4.8 + r1 * 9.6,
-0.55 - layer - r2 * 0.25,
3.992
);
pore.scale.set(
state.compacted ? 0.045 + r2 * 0.025 : 0.08 + r2 * 0.09,
state.compacted ? 0.055 + r3 * 0.04 : 0.12 + r3 * 0.20,
0.018
);
poreGroup.add(pore);
}
}
rebuildPores();
// Surface cover: mulch and living groundcover voxels.
const coverGroup = new THREE.Group();
bedGroup.add(coverGroup);
const coverGreen = new THREE.MeshStandardMaterial({
color: 0x6f914f,
roughness: 1
});
const coverLight = new THREE.MeshStandardMaterial({
color: 0x91ac61,
roughness: 1
});
const mulchMaterial = new THREE.MeshStandardMaterial({
color: 0xb49a6f,
roughness: 1
});
const coverTileGeometry = new THREE.BoxGeometry(0.93, 0.16, 0.93);
for (let z = 0; z < NZ; z++) {
for (let x = 0; x < NX; x++) {
const material = (x * 3 + z * 5) % 4 === 0
? mulchMaterial
: ((x + z) % 3 === 0 ? coverLight : coverGreen);
const tile = new THREE.Mesh(coverTileGeometry, material);
tile.position.set(
x - (NX - 1) / 2,
0.08,
z - (NZ - 1) / 2
);
tile.castShadow = true;
tile.receiveShadow = true;
coverGroup.add(tile);
}
}
// Small voxel groundcover tufts.
const tuftGeometry = new THREE.BoxGeometry(0.18, 0.24, 0.18);
const tuftMaterial = new THREE.MeshStandardMaterial({
color: 0xb3cd69,
roughness: 1
});
for (let z = 0; z < NZ; z += 1) {
for (let x = 0; x < NX; x += 1) {
if ((x * 7 + z * 3) % 3 !== 0) continue;
const tuft = new THREE.Mesh(tuftGeometry, tuftMaterial);
tuft.position.set(
x - (NX - 1) / 2 + 0.18,
0.25,
z - (NZ - 1) / 2 - 0.1
);
tuft.castShadow = true;
coverGroup.add(tuft);
}
}
// Garden crops remain visible with or without a low cover layer.
const cropGroup = new THREE.Group();
bedGroup.add(cropGroup);
const stemMaterial = new THREE.MeshStandardMaterial({ color: 0x466f3e, roughness: 1 });
const leafMaterial = new THREE.MeshStandardMaterial({ color: 0x6d984c, roughness: 1 });
const fruitMaterial = new THREE.MeshStandardMaterial({ color: 0xd48542, roughness: 0.9 });
const cropPositions = [
[-4, -2], [-2, -2], [0, -2], [2, -2], [4, -2],
[-3, 0], [-1, 0], [1, 0], [3, 0], [3, 1]
];
cropPositions.forEach(([x, z], index) => {
const plant = new THREE.Group();
plant.position.set(x, 0.08, z);
const stem = new THREE.Mesh(new THREE.BoxGeometry(0.11, 0.66, 0.11), stemMaterial);
stem.position.y = 0.33;
stem.castShadow = true;
plant.add(stem);
const leafA = new THREE.Mesh(new THREE.BoxGeometry(0.42, 0.13, 0.24), leafMaterial);
leafA.position.set(-0.13, 0.48, 0);
leafA.rotation.z = 0.18;
leafA.castShadow = true;
plant.add(leafA);
const leafB = new THREE.Mesh(new THREE.BoxGeometry(0.42, 0.13, 0.24), leafMaterial);
leafB.position.set(0.13, 0.61, 0.02);
leafB.rotation.z = -0.18;
leafB.castShadow = true;
plant.add(leafB);
if (index % 3 === 0) {
const fruit = new THREE.Mesh(new THREE.BoxGeometry(0.2, 0.2, 0.2), fruitMaterial);
fruit.position.set(0.1, 0.82, 0.05);
fruit.castShadow = true;
plant.add(fruit);
}
cropGroup.add(plant);
});
// Rain lines.
const RAIN_COUNT = 90;
const rainPositions = new Float32Array(RAIN_COUNT * 2 * 3);
const rainGeometry = new THREE.BufferGeometry();
rainGeometry.setAttribute("position", new THREE.BufferAttribute(rainPositions, 3));
const rainMaterial = new THREE.LineBasicMaterial({
color: 0x7ca8bd,
transparent: true,
opacity: 0.5,
depthWrite: false
});
const rain = new THREE.LineSegments(rainGeometry, rainMaterial);
rain.frustumCulled = false;
scene.add(rain);
const rainDrops = Array.from({ length: RAIN_COUNT }, (_, i) => ({
x: -5.6 + Math.abs(Math.sin(i * 91.73)) * 11.2,
y: 0.5 + Math.abs(Math.sin(i * 43.19)) * 7,
z: -4.2 + Math.abs(Math.sin(i * 27.31)) * 8.4,
speed: 5.2 + Math.abs(Math.sin(i * 11.4)) * 3.7
}));
function updateRain(dt) {
rain.visible = state.simMinutes < state.durationMinutes;
if (!state.playing || !rain.visible) return;
rainDrops.forEach((drop, i) => {
drop.y -= dt * drop.speed;
if (drop.y < 0.05) {
drop.y = 6.5 + (i % 9) * 0.16;
}
const p = i * 6;
rainPositions[p] = drop.x;
rainPositions[p + 1] = drop.y;
rainPositions[p + 2] = drop.z;
rainPositions[p + 3] = drop.x - 0.07;
rainPositions[p + 4] = drop.y + 0.31;
rainPositions[p + 5] = drop.z + 0.02;
});
rainGeometry.attributes.position.needsUpdate = true;
}
// Water tracers moving through the exposed soil face.
const TRACER_COUNT = 100;
const tracerPositions = new Float32Array(TRACER_COUNT * 3);
const tracerGeometry = new THREE.BufferGeometry();
tracerGeometry.setAttribute("position", new THREE.BufferAttribute(tracerPositions, 3));
const tracerMaterial = new THREE.PointsMaterial({
color: 0x65d5e7,
size: 0.115,
transparent: true,
opacity: 0.95,
depthWrite: false,
sizeAttenuation: true
});
const tracers = new THREE.Points(tracerGeometry, tracerMaterial);
tracers.frustumCulled = false;
bedGroup.add(tracers);
const tracerData = Array.from({ length: TRACER_COUNT }, (_, i) => ({
x: -4.8 + Math.abs(Math.sin(i * 31.7)) * 9.6,
offset: Math.abs(Math.sin(i * 19.4)) * 3.7,
speed: 0.65 + Math.abs(Math.sin(i * 7.2)) * 0.85
}));
// Surface runoff pools along the front edge.
const runoffGroup = new THREE.Group();
bedGroup.add(runoffGroup);
const runoffMaterial = new THREE.MeshBasicMaterial({
color: 0x54b8cb,
transparent: true,
opacity: 0.46,
depthWrite: false
});
const runoffGeometry = new THREE.BoxGeometry(0.55, 0.035, 0.42);
for (let i = 0; i < 13; i++) {
const pool = new THREE.Mesh(runoffGeometry, runoffMaterial);
pool.position.set(-4.7 + i * 0.78, 0.025, 4.05 + (i % 3) * 0.18);
pool.visible = false;
runoffGroup.add(pool);
}
function updateWaterVisuals(elapsed) {
const capacity = capacities[scenarioKey()];
const current = balance(capacity);
const tracerCount = state.simMinutes > 0
? Math.floor(18 + capacity / 38 * 82)
: 0;
tracerGeometry.setDrawRange(0, tracerCount);
const verticalSpeed = state.compacted ? 0.52 : 1.0;
tracerData.forEach((tracer, i) => {
const travel = (elapsed * tracer.speed * verticalSpeed + tracer.offset) % 3.75;
tracerPositions[i * 3] = tracer.x;
tracerPositions[i * 3 + 1] = -0.14 - travel;
tracerPositions[i * 3 + 2] = 4.035;
});
tracerGeometry.attributes.position.needsUpdate = true;
tracers.visible = state.simMinutes > 0;
const runoffRatio = totalRain ? current.runoff / totalRain : 0;
runoffGroup.children.forEach((pool, i) => {
const local = Math.max(0, runoffRatio * 1.7 - i * 0.08);
pool.visible = local > 0.015 && state.simMinutes > 0;
pool.scale.set(
Math.max(0.15, Math.min(1.4, local * 1.5)),
1,
Math.max(0.15, Math.min(1.2, local))
);
});
}
// --- Direct camera controls -------------------------------------------
let dragging = false;
let pointerX = 0;
let pointerY = 0;
renderer.domElement.addEventListener("pointerdown", (event) => {
dragging = true;
pointerX = event.clientX;
pointerY = event.clientY;
renderer.domElement.setPointerCapture(event.pointerId);
renderer.domElement.style.cursor = "grabbing";
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (!dragging) return;
const dx = event.clientX - pointerX;
const dy = event.clientY - pointerY;
pointerX = event.clientX;
pointerY = event.clientY;
yaw -= dx * 0.006;
pitch = THREE.MathUtils.clamp(pitch + dy * 0.005, 0.18, 1.13);
updateCamera();
});
function endDrag(event) {
dragging = false;
renderer.domElement.style.cursor = "grab";
if (event && renderer.domElement.hasPointerCapture(event.pointerId)) {
renderer.domElement.releasePointerCapture(event.pointerId);
}
}
renderer.domElement.addEventListener("pointerup", endDrag);
renderer.domElement.addEventListener("pointercancel", endDrag);
renderer.domElement.addEventListener("pointerleave", endDrag);
renderer.domElement.style.cursor = "grab";
renderer.domElement.addEventListener("wheel", (event) => {
event.preventDefault();
radius = THREE.MathUtils.clamp(radius + event.deltaY * 0.012, 11.5, 26);
updateCamera();
}, { passive: false });
renderer.domElement.addEventListener("dblclick", resetCamera);
renderer.domElement.addEventListener("keydown", (event) => {
if (event.key === "ArrowLeft") yaw += 0.08;
if (event.key === "ArrowRight") yaw -= 0.08;
if (event.key === "ArrowUp") pitch = Math.min(1.13, pitch + 0.06);
if (event.key === "ArrowDown") pitch = Math.max(0.18, pitch - 0.06);
if (event.key === "+" || event.key === "=") radius = Math.max(11.5, radius - 1);
if (event.key === "-") radius = Math.min(26, radius + 1);
updateCamera();
event.preventDefault();
});
$("resetCamera").addEventListener("click", resetCamera);
// --- Interface ---------------------------------------------------------
$("coverToggle").addEventListener("change", (event) => {
setScenario(event.target.checked, state.compacted);
});
$("compactToggle").addEventListener("change", (event) => {
setScenario(state.cover, event.target.checked);
});
document.querySelectorAll(".matrix-card").forEach((card) => {
card.addEventListener("click", () => {
setScenario(card.dataset.cover === "true", card.dataset.compact === "true");
});
});
$("playButton").addEventListener("click", () => {
if (state.simMinutes >= state.durationMinutes) {
state.simMinutes = 0;
state.playing = true;
} else {
state.playing = !state.playing;
}
updateUI();
});
function scenarioLabel() {
const cover = state.cover ? "Covered" : "Bare";
const structure = state.compacted ? "Compacted" : "Loose";
return `${cover} · ${structure}`;
}
function updateUI() {
const capacity = capacities[scenarioKey()];
const current = balance(capacity);
const baseline = balance(baselineCapacity);
$("currentInfiltration").textContent = current.infiltrated.toFixed(1);
$("baselineInfiltration").textContent = baseline.infiltrated.toFixed(1);
$("currentBarValue").textContent = `${current.infiltrated.toFixed(1)} mm`;
$("baselineBarValue").textContent = `${baseline.infiltrated.toFixed(1)} mm`;
$("currentBar").style.width = `${(current.infiltrated / totalRain) * 100}%`;
$("baselineBar").style.width = `${(baseline.infiltrated / totalRain) * 100}%`;
$("currentBarLabel").textContent = `Current · ${scenarioLabel().toLowerCase()}`;
$("sceneScenario").textContent = scenarioLabel();
$("rateLabel").textContent = state.playing ? "Rain active" : "Rain paused";
$("progressFill").style.width = `${(state.simMinutes / state.durationMinutes) * 100}%`;
$("timeLabel").textContent = `${state.simMinutes.toFixed(1)} min elapsed`;
if (state.simMinutes >= state.durationMinutes) {
$("playButton").textContent = "Replay rain";
} else {
$("playButton").textContent = state.playing ? "Pause rain" : "Resume rain";
}
const difference = current.infiltrated - baseline.infiltrated;
const percentage = baseline.infiltrated > 0
? difference / baseline.infiltrated * 100
: 0;
if (Math.abs(difference) < 0.05) {
$("deltaTitle").textContent = "Matches the bare, loose baseline";
$("insightText").textContent =
"Both beds currently have the same modeled infiltration under the same storm.";
} else if (difference > 0) {
$("deltaTitle").textContent =
`${difference.toFixed(1)} mm more infiltration (${percentage.toFixed(0)}%)`;
$("insightText").textContent =
state.compacted
? "Cover protects the surface and helps, but compaction still limits how quickly water reaches deeper pores."
: "Cover slows surface sealing while the loose soil provides more space for water to enter.";
} else {
$("deltaTitle").textContent =
`${Math.abs(difference).toFixed(1)} mm less infiltration (${Math.abs(percentage).toFixed(0)}%)`;
$("insightText").textContent =
state.compacted
? "Dense soil reduces connected pore space, so more water remains at the surface or runs off."
: "This bed has fewer effective pores than the baseline, so intake is reduced.";
}
document.querySelectorAll(".matrix-card").forEach((card) => {
const selected =
card.dataset.cover === String(state.cover) &&
card.dataset.compact === String(state.compacted);
card.classList.toggle("selected", selected);
});
}
updateUI();
// --- Resize and animation ----------------------------------------------
function resize() {
const width = stage.clientWidth;
const height = stage.clientHeight;
renderer.setSize(width, height, false);
camera.aspect = width / height;
camera.updateProjectionMatrix();
}
const resizeObserver = new ResizeObserver(resize);
resizeObserver.observe(stage);
resize();
const clock = new THREE.Clock();
let uiTimer = 0;
function animate() {
const dt = Math.min(clock.getDelta(), 0.05);
const elapsed = clock.elapsedTime;
if (state.playing) {
state.simMinutes = Math.min(
state.durationMinutes,
state.simMinutes + dt * 2.5
);
if (state.simMinutes >= state.durationMinutes) {
state.playing = false;
}
}
updateRain(dt);
updateWaterVisuals(elapsed);
uiTimer += dt;
if (uiTimer > 0.08) {
updateUI();
uiTimer = 0;
}
renderer.render(scene, camera);
}
renderer.setAnimationLoop(animate);
</script>
</body>
</html>
```