Prompt specification
Build or customize the Three.js 3D scene "Allocate a limited courtyard water supply" 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: Allocate a limited courtyard water supply
Category: Art & Culture · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/allocate-a-limited-courtyard-water-supply-x2324
Scene Description Prompt:
Please create one self-contained HTML file with Three.js from cdnjs showing Andalusi water gardens and irrigation channels. Let users divide illustrative inflow between a pool and planted beds. Track volumes and show overflow paths while avoiding claims that the model reproduces historical irrigation law. Low-poly.
Working Three.js Starter (allocate-a-limited-courtyard-water-supply-x2324):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="theme-color" content="#244d4b">
<title>Andalusi Water Garden — Illustrative Flow Study</title>
<style>
:root {
--ink: #173d3a;
--muted: #65736c;
--paper: rgba(250, 246, 235, 0.94);
--paper-solid: #faf6eb;
--line: rgba(23, 61, 58, 0.15);
--teal: #087e8b;
--aqua: #43c3cb;
--terracotta: #b85f3b;
--gold: #c99a42;
--danger: #a43f28;
--shadow: 0 18px 50px rgba(37, 48, 42, 0.22);
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #dfcfad;
color: var(--ink);
font-family:
Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
button,
input {
font: inherit;
}
button {
color: inherit;
}
#scene {
position: fixed;
inset: 0;
width: 100%;
height: 100%;
display: block;
touch-action: none;
cursor: grab;
outline: none;
}
#scene.dragging {
cursor: grabbing;
}
#scene:focus-visible {
box-shadow: inset 0 0 0 3px rgba(255, 255, 255, 0.75);
}
.scene-heading {
position: fixed;
z-index: 3;
top: 22px;
left: 24px;
max-width: min(460px, calc(100vw - 430px));
pointer-events: none;
color: #173d3a;
text-shadow: 0 1px 16px rgba(255, 255, 255, 0.55);
}
.scene-heading .eyebrow {
display: block;
margin-bottom: 7px;
font-size: 0.69rem;
font-weight: 800;
letter-spacing: 0.18em;
text-transform: uppercase;
}
.scene-heading h1 {
margin: 0;
font-family: Georgia, "Times New Roman", serif;
font-size: clamp(1.55rem, 2.7vw, 2.7rem);
font-weight: 500;
line-height: 1.02;
}
.scene-heading p {
margin: 9px 0 0;
max-width: 390px;
color: rgba(23, 61, 58, 0.78);
font-size: 0.85rem;
line-height: 1.45;
}
#labels {
position: fixed;
inset: 0;
z-index: 2;
overflow: hidden;
pointer-events: none;
}
.world-label {
position: absolute;
left: 0;
top: 0;
padding: 5px 9px;
border: 1px solid rgba(255, 255, 255, 0.75);
border-radius: 999px;
background: rgba(248, 243, 226, 0.84);
box-shadow: 0 5px 18px rgba(34, 55, 48, 0.14);
color: #214844;
font-size: 0.68rem;
font-weight: 800;
letter-spacing: 0.08em;
text-transform: uppercase;
white-space: nowrap;
transform: translate(-50%, -50%);
transition: opacity 180ms ease, background 180ms ease;
}
.world-label.overflow {
opacity: 0;
background: #9b3e27;
color: white;
}
.world-label.overflow.active {
opacity: 1;
}
.controls-hint {
position: fixed;
z-index: 3;
left: 22px;
bottom: 20px;
display: flex;
align-items: center;
gap: 9px;
padding: 9px 13px;
border: 1px solid rgba(255, 255, 255, 0.55);
border-radius: 999px;
background: rgba(249, 244, 228, 0.78);
box-shadow: 0 8px 30px rgba(35, 48, 41, 0.12);
color: rgba(23, 61, 58, 0.8);
font-size: 0.72rem;
font-weight: 650;
pointer-events: none;
backdrop-filter: blur(8px);
}
.controls-hint span {
display: inline-block;
width: 7px;
height: 7px;
border-radius: 50%;
background: var(--terracotta);
box-shadow: 0 0 0 4px rgba(184, 95, 59, 0.14);
}
.panel {
position: fixed;
z-index: 5;
top: 16px;
right: 16px;
width: 370px;
max-height: calc(100vh - 32px);
overflow: auto;
border: 1px solid rgba(255, 255, 255, 0.7);
border-radius: 22px;
background: var(--paper);
box-shadow: var(--shadow);
backdrop-filter: blur(18px) saturate(1.12);
scrollbar-width: thin;
scrollbar-color: rgba(23, 61, 58, 0.2) transparent;
}
.panel-inner {
padding: 21px;
}
.kicker {
display: flex;
align-items: center;
justify-content: space-between;
gap: 12px;
margin-bottom: 10px;
color: var(--teal);
font-size: 0.66rem;
font-weight: 850;
letter-spacing: 0.15em;
text-transform: uppercase;
}
.clock {
display: inline-flex;
align-items: center;
gap: 6px;
color: var(--muted);
font-size: 0.65rem;
letter-spacing: 0.03em;
text-transform: none;
white-space: nowrap;
}
.clock::before {
content: "";
width: 7px;
height: 7px;
border-radius: 50%;
background: #4ba56d;
box-shadow: 0 0 0 4px rgba(75, 165, 109, 0.12);
}
.clock.paused::before {
background: var(--gold);
box-shadow: 0 0 0 4px rgba(201, 154, 66, 0.14);
}
.panel h2 {
margin: 0;
font-family: Georgia, "Times New Roman", serif;
font-size: 1.65rem;
font-weight: 500;
line-height: 1.05;
}
.intro {
margin: 9px 0 18px;
color: var(--muted);
font-size: 0.78rem;
line-height: 1.52;
}
.section {
padding: 16px 0;
border-top: 1px solid var(--line);
}
.section:first-of-type {
border-top: 0;
padding-top: 0;
}
.section-title {
display: flex;
align-items: center;
justify-content: space-between;
gap: 10px;
margin-bottom: 13px;
}
.section-title h3 {
margin: 0;
font-size: 0.73rem;
font-weight: 850;
letter-spacing: 0.11em;
text-transform: uppercase;
}
.section-title small {
color: var(--muted);
font-size: 0.68rem;
}
.control + .control {
margin-top: 17px;
}
.control-heading {
display: flex;
align-items: baseline;
justify-content: space-between;
gap: 12px;
margin-bottom: 9px;
}
.control-heading label {
font-size: 0.79rem;
font-weight: 750;
}
.control-heading output {
color: var(--teal);
font-size: 0.78rem;
font-weight: 850;
text-align: right;
}
input[type="range"] {
width: 100%;
height: 5px;
margin: 5px 0 2px;
border-radius: 999px;
appearance: none;
background:
linear-gradient(
90deg,
var(--teal) 0%,
var(--teal) var(--range-progress, 50%),
rgba(23, 61, 58, 0.13) var(--range-progress, 50%),
rgba(23, 61, 58, 0.13) 100%
);
cursor: pointer;
}
input[type="range"]::-webkit-slider-thumb {
width: 18px;
height: 18px;
border: 3px solid var(--paper-solid);
border-radius: 50%;
appearance: none;
background: var(--teal);
box-shadow: 0 2px 8px rgba(23, 61, 58, 0.26);
}
input[type="range"]::-moz-range-thumb {
width: 13px;
height: 13px;
border: 3px solid var(--paper-solid);
border-radius: 50%;
background: var(--teal);
box-shadow: 0 2px 8px rgba(23, 61, 58, 0.26);
}
.range-scale {
display: flex;
justify-content: space-between;
margin-top: 7px;
color: #879089;
font-size: 0.62rem;
}
.presets {
display: grid;
grid-template-columns: repeat(4, 1fr);
gap: 7px;
margin-top: 14px;
}
.preset {
min-height: 43px;
padding: 7px 5px;
border: 1px solid var(--line);
border-radius: 11px;
background: rgba(255, 255, 255, 0.48);
color: var(--ink);
cursor: pointer;
font-size: 0.65rem;
font-weight: 750;
line-height: 1.15;
transition:
transform 120ms ease,
border-color 120ms ease,
background 120ms ease;
}
.preset:hover {
transform: translateY(-1px);
border-color: rgba(8, 126, 139, 0.4);
}
.preset.active {
border-color: rgba(8, 126, 139, 0.55);
background: rgba(67, 195, 203, 0.16);
color: #075f68;
}
.status {
display: flex;
align-items: center;
gap: 10px;
padding: 10px 12px;
border-radius: 12px;
background: rgba(75, 165, 109, 0.12);
color: #286443;
font-size: 0.74rem;
font-weight: 750;
transition: background 180ms ease, color 180ms ease;
}
.status::before {
content: "";
width: 8px;
height: 8px;
flex: 0 0 auto;
border-radius: 50%;
background: currentColor;
box-shadow: 0 0 0 4px rgba(40, 100, 67, 0.1);
}
.status[data-state="pool"],
.status[data-state="bed"],
.status[data-state="both"] {
background: rgba(184, 95, 59, 0.14);
color: #8b3c24;
}
.status[data-state="idle"] {
background: rgba(201, 154, 66, 0.15);
color: #77571d;
}
.meter-grid {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 10px;
}
.meter-card {
padding: 12px;
border: 1px solid var(--line);
border-radius: 14px;
background: rgba(255, 255, 255, 0.4);
}
.meter-card .label {
display: flex;
align-items: center;
gap: 6px;
color: var(--muted);
font-size: 0.65rem;
font-weight: 800;
letter-spacing: 0.07em;
text-transform: uppercase;
}
.dot {
width: 7px;
height: 7px;
border-radius: 50%;
background: var(--aqua);
}
.dot.beds {
background: var(--gold);
}
.meter-value {
display: block;
margin-top: 8px;
font-family: Georgia, "Times New Roman", serif;
font-size: 1.42rem;
line-height: 1;
}
.meter-sub {
display: block;
min-height: 17px;
margin-top: 5px;
color: var(--muted);
font-size: 0.62rem;
}
.meter-bar {
height: 5px;
margin-top: 10px;
overflow: hidden;
border-radius: 999px;
background: rgba(23, 61, 58, 0.1);
}
.meter-fill {
width: 0;
height: 100%;
border-radius: inherit;
background: linear-gradient(90deg, #2daeb7, #087e8b);
transition: width 140ms linear;
}
.meter-fill.beds {
background: linear-gradient(90deg, #e0bd68, #b58229);
}
.overflow-grid {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 10px;
margin-top: 10px;
}
.overflow-card {
padding: 10px 11px;
border-left: 3px solid rgba(164, 63, 40, 0.48);
border-radius: 0 10px 10px 0;
background: rgba(164, 63, 40, 0.055);
}
.overflow-card strong {
display: block;
color: #873923;
font-size: 0.75rem;
}
.overflow-card span {
display: block;
margin-top: 3px;
color: var(--muted);
font-size: 0.62rem;
}
.chart-wrap {
position: relative;
padding: 8px 8px 2px;
border: 1px solid var(--line);
border-radius: 14px;
background: rgba(255, 255, 255, 0.35);
}
#historyChart {
display: block;
width: 100%;
height: 106px;
overflow: visible;
}
.chart-grid {
stroke: rgba(23, 61, 58, 0.1);
stroke-width: 1;
}
.chart-line {
fill: none;
stroke-width: 2.3;
stroke-linecap: round;
stroke-linejoin: round;
}
#poolChart {
stroke: var(--teal);
}
#bedChart {
stroke: var(--gold);
}
.chart-legend {
display: flex;
gap: 16px;
padding: 1px 5px 4px;
color: var(--muted);
font-size: 0.64rem;
}
.chart-legend span {
display: inline-flex;
align-items: center;
gap: 6px;
}
.chart-legend i {
width: 14px;
height: 3px;
border-radius: 999px;
background: var(--teal);
}
.chart-legend .gold {
background: var(--gold);
}
.actions {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 9px;
margin-top: 15px;
}
.action {
min-height: 40px;
border: 1px solid rgba(8, 126, 139, 0.24);
border-radius: 11px;
background: rgba(8, 126, 139, 0.09);
color: #08616b;
cursor: pointer;
font-size: 0.73rem;
font-weight: 800;
transition: transform 120ms ease, background 120ms ease;
}
.action:hover {
transform: translateY(-1px);
background: rgba(8, 126, 139, 0.15);
}
.action.secondary {
border-color: var(--line);
background: rgba(255, 255, 255, 0.48);
color: var(--ink);
}
.toggle-row {
display: flex;
align-items: center;
justify-content: space-between;
gap: 12px;
margin-top: 13px;
color: var(--muted);
font-size: 0.7rem;
}
.toggle {
position: relative;
width: 39px;
height: 22px;
flex: 0 0 auto;
}
.toggle input {
position: absolute;
opacity: 0;
pointer-events: none;
}
.toggle-track {
position: absolute;
inset: 0;
border-radius: 999px;
background: rgba(23, 61, 58, 0.18);
cursor: pointer;
transition: background 150ms ease;
}
.toggle-track::after {
content: "";
position: absolute;
top: 3px;
left: 3px;
width: 16px;
height: 16px;
border-radius: 50%;
background: white;
box-shadow: 0 2px 5px rgba(23, 61, 58, 0.24);
transition: transform 150ms ease;
}
.toggle input:checked + .toggle-track {
background: var(--teal);
}
.toggle input:checked + .toggle-track::after {
transform: translateX(17px);
}
.disclaimer {
margin: 15px 0 0;
padding: 11px 12px;
border-radius: 11px;
background: rgba(23, 61, 58, 0.055);
color: #59665f;
font-size: 0.66rem;
line-height: 1.5;
}
.disclaimer strong {
color: var(--ink);
}
@media (max-width: 760px) {
.scene-heading {
top: 15px;
left: 16px;
max-width: calc(100vw - 32px);
}
.scene-heading h1 {
font-size: 1.55rem;
}
.scene-heading p {
display: none;
}
.panel {
top: auto;
right: 9px;
bottom: 9px;
left: 9px;
width: auto;
max-height: 47vh;
border-radius: 18px;
}
.panel-inner {
padding: 17px;
}
.controls-hint {
display: none;
}
.world-label {
font-size: 0.58rem;
}
.meter-grid,
.overflow-grid {
grid-template-columns: 1fr 1fr;
}
}
@media (prefers-reduced-motion: reduce) {
*,
*::before,
*::after {
scroll-behavior: auto !important;
transition-duration: 0.01ms !important;
}
}
</style>
</head>
<body>
<canvas
id="scene"
tabindex="0"
aria-label="Interactive low-poly Andalusi-inspired garden. Drag to orbit, use the mouse wheel to zoom, and use arrow keys to change the view."
></canvas>
<div id="labels" aria-hidden="true">
<div class="world-label" id="labelPool">Pool</div>
<div class="world-label" id="labelBeds">Planted beds</div>
<div class="world-label" id="labelInlet">Illustrative inflow</div>
<div class="world-label overflow" id="labelPoolOverflow">Pool overflow</div>
<div class="world-label overflow" id="labelBedOverflow">Bed overflow</div>
</div>
<header class="scene-heading">
<span class="eyebrow">Low-poly courtyard · interactive flow study</span>
<h1>Andalusi Water Gardens</h1>
<p>
A stylized garden inspired by Andalusi forms, with a simplified,
accelerated water balance.
</p>
</header>
<div class="controls-hint" aria-hidden="true">
<span></span>
Drag to orbit · wheel or pinch to zoom · double-click to reset view
</div>
<aside class="panel" aria-label="Water balance controls and readings">
<div class="panel-inner">
<div class="kicker">
<span>Illustrative water balance</span>
<span class="clock" id="clock">00:00 · ×120</span>
</div>
<h2>Divide the inflow</h2>
<p class="intro">
Send water toward the pool or the planted beds. Storage fills over time;
anything above the stated capacity leaves through the illustrated
overflow route.
</p>
<section class="section">
<div class="section-title">
<h3>Inflow controls</h3>
<small>Accelerated model time</small>
</div>
<div class="control">
<div class="control-heading">
<label for="flow">Total inflow</label>
<output id="flowValue" for="flow">60 L/min</output>
</div>
<input
id="flow"
type="range"
min="0"
max="120"
value="60"
step="1"
aria-describedby="flowValue"
>
<div class="range-scale">
<span>0 L/min</span>
<span>120 L/min</span>
</div>
</div>
<div class="control">
<div class="control-heading">
<label for="split">Division</label>
<output id="splitValue" for="split">65% pool · 35% beds</output>
</div>
<input
id="split"
type="range"
min="0"
max="100"
value="65"
step="1"
aria-describedby="splitValue"
>
<div class="range-scale">
<span>All to pool</span>
<span>All to beds</span>
</div>
</div>
<div class="presets" aria-label="Flow presets">
<button class="preset" type="button" data-flow="60" data-split="55">
Balanced
</button>
<button class="preset" type="button" data-flow="80" data-split="80">
Pool
</button>
<button class="preset" type="button" data-flow="80" data-split="20">
Beds
</button>
<button class="preset" type="button" data-flow="120" data-split="50">
Surge
</button>
</div>
</section>
<section class="section">
<div class="section-title">
<h3>Current state</h3>
<small id="statusText">Within storage</small>
</div>
<div class="status" id="status" data-state="normal" role="status" aria-live="polite">
Water is entering both destinations.
</div>
<div class="meter-grid" style="margin-top: 12px">
<div class="meter-card">
<div class="label"><i class="dot"></i>Pool storage</div>
<strong class="meter-value" id="poolVolume">1,600 L</strong>
<span class="meter-sub" id="poolRate">39 L/min to pool</span>
<div class="meter-bar">
<div class="meter-fill" id="poolFill"></div>
</div>
</div>
<div class="meter-card">
<div class="label"><i class="dot beds"></i>Bed storage</div>
<strong class="meter-value" id="bedVolume">520 L</strong>
<span class="meter-sub" id="bedRate">21 L/min to beds</span>
<div class="meter-bar">
<div class="meter-fill beds" id="bedFill"></div>
</div>
</div>
</div>
<div class="overflow-grid">
<div class="overflow-card">
<strong id="poolOverflowRate">0 L/min overflow</strong>
<span id="poolOverflowTotal">0 L total</span>
</div>
<div class="overflow-card">
<strong id="bedOverflowRate">0 L/min overflow</strong>
<span id="bedOverflowTotal">0 L total</span>
</div>
</div>
</section>
<section class="section">
<div class="section-title">
<h3>Storage history</h3>
<small>Relative capacity</small>
</div>
<div class="chart-wrap">
<svg
id="historyChart"
viewBox="0 0 260 100"
preserveAspectRatio="none"
role="img"
aria-label="Recent relative storage history for the pool and planted beds"
>
<line class="chart-grid" x1="7" y1="18" x2="253" y2="18"></line>
<line class="chart-grid" x1="7" y1="48" x2="253" y2="48"></line>
<line class="chart-grid" x1="7" y1="78" x2="253" y2="78"></line>
<path class="chart-line" id="poolChart"></path>
<path class="chart-line" id="bedChart"></path>
</svg>
<div class="chart-legend">
<span><i></i>Pool</span>
<span><i class="gold"></i>Beds</span>
</div>
</div>
<div class="actions">
<button class="action" id="pauseBtn" type="button">Pause</button>
<button class="action secondary" id="resetBtn" type="button">Reset volumes</button>
</div>
<div class="toggle-row">
<span>Show overflow routes and moving spill markers</span>
<label class="toggle">
<input id="showOverflow" type="checkbox" checked>
<span class="toggle-track"></span>
</label>
</div>
</section>
<p class="disclaimer">
<strong>Model boundary:</strong> this is an educational visualization,
not a reconstruction of a specific historical garden and not a model of
historical irrigation law, water rights, or hydraulic performance.
“Bed storage” is a simplified illustrative container; infiltration,
evaporation, topography, and legal allocation rules are not represented.
</p>
</div>
</aside>
<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 canvas = $("scene");
const reducedMotion = window.matchMedia(
"(prefers-reduced-motion: reduce)"
).matches;
// ------------------------------------------------------------------------
// Renderer, scene, camera, and light
// ------------------------------------------------------------------------
const renderer = new THREE.WebGLRenderer({
canvas,
antialias: true,
alpha: false
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio || 1, 2));
renderer.setSize(window.innerWidth, window.innerHeight, false);
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.08;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xdfcfad);
scene.fog = new THREE.Fog(0xdfcfad, 34, 70);
const camera = new THREE.PerspectiveCamera(
42,
window.innerWidth / window.innerHeight,
0.1,
100
);
const hemisphere = new THREE.HemisphereLight(0xfff5d7, 0x6c705a, 2.15);
scene.add(hemisphere);
const sun = new THREE.DirectionalLight(0xffedc4, 3.15);
sun.position.set(-12, 19, 9);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -20;
sun.shadow.camera.right = 20;
sun.shadow.camera.top = 20;
sun.shadow.camera.bottom = -20;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 55;
sun.shadow.bias = -0.00035;
scene.add(sun);
const fill = new THREE.DirectionalLight(0x9fc9d1, 0.55);
fill.position.set(12, 8, -10);
scene.add(fill);
// ------------------------------------------------------------------------
// Shared low-poly materials and construction helpers
// ------------------------------------------------------------------------
function standard(color, extra = {}) {
return new THREE.MeshStandardMaterial({
color,
roughness: 0.84,
flatShading: true,
...extra
});
}
const materials = {
ground: standard(0xcbb98f),
stone: standard(0xd9c7a1),
stoneLight: standard(0xeee1bd),
stoneDark: standard(0xa99776),
terracotta: standard(0xa95737),
tile: standard(0x176f7b, { roughness: 0.46 }),
tileLight: standard(0x42b6bd, { roughness: 0.36 }),
wood: standard(0x75533c),
trunk: standard(0x71533b),
leaf: standard(0x486c3d),
leafLight: standard(0x678149),
leafDark: standard(0x314f35),
fruit: standard(0xdc872f),
soil: standard(0x846341),
path: standard(0xe5d7b6)
};
function addBox(parent, size, position, material) {
const mesh = new THREE.Mesh(
new THREE.BoxGeometry(size[0], size[1], size[2]),
material
);
mesh.position.set(position[0], position[1], position[2]);
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
function addCylinder(
parent,
radiusTop,
radiusBottom,
height,
segments,
position,
material
) {
const mesh = new THREE.Mesh(
new THREE.CylinderGeometry(
radiusTop,
radiusBottom,
height,
segments
),
material
);
mesh.position.set(position[0], position[1], position[2]);
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
function addCone(parent, radius, height, segments, position, material) {
const mesh = new THREE.Mesh(
new THREE.ConeGeometry(radius, height, segments),
material
);
mesh.position.set(position[0], position[1], position[2]);
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
function addArch(parent, radius, tube, position, material) {
const mesh = new THREE.Mesh(
new THREE.TorusGeometry(radius, tube, 5, 14, Math.PI),
material
);
mesh.position.set(position[0], position[1], position[2]);
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
// ------------------------------------------------------------------------
// Ground, paving, arcade, and garden furnishings
// ------------------------------------------------------------------------
const ground = new THREE.Mesh(
new THREE.PlaneGeometry(38, 30),
materials.ground
);
ground.rotation.x = -Math.PI / 2;
ground.position.y = -0.04;
ground.receiveShadow = true;
scene.add(ground);
// Paved central walkway and transverse path.
for (let z = -3.8; z <= 6.1; z += 1.05) {
addBox(
scene,
[1.15, 0.08, 0.86],
[0, 0.025, z],
materials.path
);
}
for (let x = -9; x <= 9; x += 1.1) {
addBox(
scene,
[0.91, 0.075, 1.05],
[x, 0.02, 4.2],
materials.path
);
}
// Low courtyard side walls.
addBox(scene, [0.48, 1.05, 19], [-11.2, 0.52, -0.7], materials.stone);
addBox(scene, [0.48, 1.05, 19], [11.2, 0.52, -0.7], materials.stone);
// Rear arcade, built from low-segment columns and half-torus arches.
const arcade = new THREE.Group();
scene.add(arcade);
const columnXs = [-9.5, -5.7, -1.9, 1.9, 5.7, 9.5];
for (const x of columnXs) {
addBox(
arcade,
[0.72, 0.24, 0.78],
[x, 0.12, -9.55],
materials.stoneDark
);
addBox(
arcade,
[0.56, 2.25, 0.64],
[x, 1.35, -9.55],
materials.stoneLight
);
addBox(
arcade,
[0.7, 0.22, 0.74],
[x, 2.56, -9.55],
materials.stone
);
}
for (let i = 0; i < columnXs.length - 1; i++) {
const midpoint = (columnXs[i] + columnXs[i + 1]) / 2;
addArch(
arcade,
1.9,
0.27,
[midpoint, 2.58, -9.55],
materials.stoneLight
);
}
addBox(
arcade,
[20.2, 0.38, 0.8],
[0, 4.52, -9.55],
materials.stone
);
// Small ceramic tile accents beneath the arcade.
for (const x of [-7.6, -3.8, 0, 3.8, 7.6]) {
const tile = addBox(
arcade,
[0.72, 0.62, 0.07],
[x, 0.92, -9.18],
materials.tile
);
tile.rotation.z = Math.PI / 4;
}
// ------------------------------------------------------------------------
// Pool
// ------------------------------------------------------------------------
const poolGroup = new THREE.Group();
poolGroup.position.set(-4.8, 0, 0.2);
scene.add(poolGroup);
addBox(
poolGroup,
[8.4, 0.3, 6.4],
[0, 0.15, 0],
materials.stoneDark
);
addBox(
poolGroup,
[8.4, 0.7, 0.34],
[0, 0.48, 3.03],
materials.stoneLight
);
addBox(
poolGroup,
[8.4, 0.7, 0.34],
[0, 0.48, -3.03],
materials.stoneLight
);
addBox(
poolGroup,
[0.34, 0.7, 5.72],
[4.03, 0.48, 0],
materials.stoneLight
);
addBox(
poolGroup,
[0.34, 0.7, 5.72],
[-4.03, 0.48, 0],
materials.stoneLight
);
// Blue tile lip.
addBox(
poolGroup,
[8.45, 0.08, 0.16],
[0, 0.86, 3.15],
materials.tileLight
);
addBox(
poolGroup,
[8.45, 0.08, 0.16],
[0, 0.86, -3.15],
materials.tileLight
);
addBox(
poolGroup,
[0.16, 0.08, 6.35],
[4.15, 0.86, 0],
materials.tileLight
);
addBox(
poolGroup,
[0.16, 0.08, 6.35],
[-4.15, 0.86, 0],
materials.tileLight
);
const poolGeometry = new THREE.PlaneGeometry(7.72, 5.72, 10, 8);
poolGeometry.rotateX(-Math.PI / 2);
const poolWaterMaterial = standard(0x38b8c3, {
roughness: 0.28,
metalness: 0.03,
transparent: true,
opacity: 0.84,
depthWrite: false,
side: THREE.DoubleSide
});
const poolWater = new THREE.Mesh(poolGeometry, poolWaterMaterial);
poolWater.position.y = 0.68;
poolWater.receiveShadow = true;
poolGroup.add(poolWater);
// ------------------------------------------------------------------------
// Planted beds
// ------------------------------------------------------------------------
const bedGroup = new THREE.Group();
bedGroup.position.set(4.8, 0, 0.3);
scene.add(bedGroup);
const bedSoilMaterials = [];
const plantFoliage = [];
function buildBed(cx, cz, index) {
const soilMaterial = materials.soil.clone();
bedSoilMaterials.push(soilMaterial);
addBox(
bedGroup,
[3.15, 0.24, 2.75],
[cx, 0.18, cz],
soilMaterial
);
// Raised stone borders.
addBox(
bedGroup,
[3.34, 0.34, 0.18],
[cx, 0.28, cz - 1.47],
materials.stone
);
addBox(
bedGroup,
[3.34, 0.34, 0.18],
[cx, 0.28, cz + 1.47],
materials.stone
);
addBox(
bedGroup,
[0.18, 0.34, 2.76],
[cx - 1.58, 0.28, cz],
materials.stone
);
addBox(
bedGroup,
[0.18, 0.34, 2.76],
[cx + 1.58, 0.28, cz],
materials.stone
);
// Low irrigation ribbons running through the bed.
for (const offset of [-0.72, 0, 0.72]) {
addBox(
bedGroup,
[2.95, 0.035, 0.12],
[cx, 0.325, cz + offset],
materials.tileLight
);
}
// Six small low-poly shrubs per bed.
for (let row = 0; row < 2; row++) {
for (let col = 0; col < 3; col++) {
const px = cx - 0.9 + col * 0.9;
const pz = cz - 0.75 + row * 1.5;
addCylinder(
bedGroup,
0.045,
0.065,
0.55,
5,
[px, 0.54, pz],
materials.trunk
);
const foliage = addCone(
bedGroup,
0.28 + (index % 2) * 0.025,
0.76,
5,
[px, 0.93, pz],
index % 2 === 0 ? materials.leaf : materials.leafLight
);
foliage.rotation.y = (row * 3 + col) * 0.73;
foliage.userData.baseRotation = foliage.rotation.y;
foliage.userData.phase = index * 0.8 + row + col * 0.4;
plantFoliage.push(foliage);
}
}
}
buildBed(-1.65, -1.55, 0);
buildBed(1.65, -1.55, 1);
buildBed(-1.65, 1.55, 2);
buildBed(1.65, 1.55, 3);
// Bed wetness ribbons share one transparent material.
const bedWetMaterial = standard(0x3aaeb4, {
transparent: true,
opacity: 0.25,
depthWrite: false,
roughness: 0.38
});
// Find and tint the existing bed ribbon meshes.
bedGroup.traverse((object) => {
if (object.isMesh && object.geometry.type === "BoxGeometry") {
const p = object.geometry.parameters;
if (p.width === 2.95 && p.height === 0.035) {
object.material = bedWetMaterial;
}
}
});
// ------------------------------------------------------------------------
// Trees and low-poly planting
// ------------------------------------------------------------------------
function addCypress(x, z, scale = 1) {
const group = new THREE.Group();
group.position.set(x, 0, z);
group.scale.setScalar(scale);
scene.add(group);
addCylinder(
group,
0.13,
0.19,
1.1,
5,
[0, 0.55, 0],
materials.trunk
);
addCone(
group,
0.74,
3.4,
6,
[0, 2.32, 0],
materials.leafDark
);
addCone(
group,
0.55,
2.5,
6,
[0, 2.72, 0],
materials.leaf
);
}
[
[-10.1, 4.8, 1.05],
[-10.0, 7.4, 0.9],
[10.1, 5.2, 1.0],
[10.0, -4.7, 0.88],
[-6.8, 8.0, 0.78],
[6.8, 8.0, 0.82]
].forEach((args) => addCypress(...args));
function addCitrusTree(x, z, scale = 1) {
const group = new THREE.Group();
group.position.set(x, 0, z);
group.scale.setScalar(scale);
scene.add(group);
addCylinder(
group,
0.16,
0.22,
1.55,
6,
[0, 0.78, 0],
materials.trunk
);
const crown = new THREE.Mesh(
new THREE.IcosahedronGeometry(0.95, 1),
materials.leafLight
);
crown.position.y = 1.9;
crown.castShadow = true;
group.add(crown);
for (let i = 0; i < 5; i++) {
const fruit = new THREE.Mesh(
new THREE.IcosahedronGeometry(0.09, 0),
materials.fruit
);
const angle = i * 1.25;
fruit.position.set(
Math.cos(angle) * 0.72,
1.75 + (i % 2) * 0.25,
Math.sin(angle) * 0.72
);
group.add(fruit);
}
}
addCitrusTree(-9.8, -5.7, 0.9);
addCitrusTree(9.6, -5.9, 0.95);
addCitrusTree(9.8, 8.0, 0.75);
// ------------------------------------------------------------------------
// Inlet, splitter, channels, and overflow routes
// ------------------------------------------------------------------------
function makeTrough(points, width, waterColor = 0x38b8c3) {
const group = new THREE.Group();
scene.add(group);
const waterMaterial = standard(waterColor, {
roughness: 0.3,
transparent: true,
opacity: 0.86,
depthWrite: false,
side: THREE.DoubleSide
});
for (let i = 0; i < points.length - 1; i++) {
const a = points[i];
const b = points[i + 1];
const dx = b[0] - a[0];
const dz = b[2] - a[2];
const length = Math.hypot(dx, dz);
const angle = Math.atan2(dx, dz);
const midpoint = [
(a[0] + b[0]) / 2,
0.16,
(a[2] + b[2]) / 2
];
const trough = addBox(
group,
[width, 0.18, length],
midpoint,
materials.stoneDark
);
trough.rotation.y = angle;
const water = addBox(
group,
[width * 0.62, 0.055, length * 0.96],
[midpoint[0], 0.275, midpoint[2]],
waterMaterial
);
water.rotation.y = angle;
}
return {
group,
waterMaterial,
points
};
}
const V = (x, y, z) => new THREE.Vector3(x, y, z);
const inletChannel = makeTrough(
[[0, 0, -5.85], [0, 0, -5.0]],
0.72
);
const poolChannel = makeTrough(
[[0, 0, -5.0], [-4.8, 0, -5.0], [-4.8, 0, -3.02]],
0.62
);
const bedChannel = makeTrough(
[[0, 0, -5.0], [4.8, 0, -5.0], [4.8, 0, -3.35]],
0.62
);
const poolOverflow = makeTrough(
[
[-8.73, 0, 3.48],
[-9.4, 0, 3.48],
[-9.4, 0, 5.85],
[-2.2, 0, 5.85]
],
0.55,
0x55d0d4
);
const bedOverflow = makeTrough(
[
[8.48, 0, 3.75],
[9.35, 0, 3.75],
[9.35, 0, 5.85],
[5.2, 0, 5.85]
],
0.5,
0x55d0d4
);
// Small vertical spill pieces at the beginning of each overflow path.
const poolSpill = addBox(
poolOverflow.group,
[0.08, 0.55, 0.52],
[-8.72, 0.49, 3.48],
standard(0x5bd3d5, {
transparent: true,
opacity: 0.9,
depthWrite: false
})
);
const bedSpill = addBox(
bedOverflow.group,
[0.08, 0.48, 0.45],
[8.47, 0.45, 3.75],
standard(0x5bd3d5, {
transparent: true,
opacity: 0.9,
depthWrite: false
})
);
// Orange warning markers distinguish overflow from ordinary irrigation.
const warningMaterial = standard(0xb6502e, {
emissive: 0x481207,
emissiveIntensity: 0.2
});
const poolMarker = addCone(
poolOverflow.group,
0.22,
0.52,
4,
[-8.72, 1.0, 3.48],
warningMaterial
);
poolMarker.rotation.z = Math.PI;
const bedMarker = addCone(
bedOverflow.group,
0.22,
0.52,
4,
[8.47, 0.94, 3.75],
warningMaterial
);
bedMarker.rotation.z = Math.PI;
// Octagonal source basin.
const sourceGroup = new THREE.Group();
sourceGroup.position.set(0, 0, -7.25);
scene.add(sourceGroup);
addCylinder(
sourceGroup,
1.28,
1.48,
1.08,
8,
[0, 0.54, 0],
materials.stone
);
addCylinder(
sourceGroup,
1.46,
1.46,
0.18,
8,
[0, 1.08, 0],
materials.stoneLight
);
addCylinder(
sourceGroup,
1.12,
1.12,
0.065,
8,
[0, 1.2, 0],
standard(0x3ebdc5, {
roughness: 0.24,
metalness: 0.03
})
);
const fountainRing = new THREE.Mesh(
new THREE.TorusGeometry(1.12, 0.065, 4, 12),
materials.tileLight
);
fountainRing.rotation.x = Math.PI / 2;
fountainRing.position.y = 1.25;
sourceGroup.add(fountainRing);
// Small source arcade.
addBox(
sourceGroup,
[0.42, 2.25, 0.48],
[-1.45, 1.12, -0.25],
materials.stoneLight
);
addBox(
sourceGroup,
[0.42, 2.25, 0.48],
[1.45, 1.12, -0.25],
materials.stoneLight
);
addArch(
sourceGroup,
1.45,
0.2,
[0, 2.22, -0.25],
materials.stoneLight
);
// Splitter platform and two small gate boards.
addBox(
scene,
[1.75, 0.28, 1.25],
[0, 0.14, -5.0],
materials.stoneLight
);
const poolGateMaterial = standard(0x167d86, { roughness: 0.55 });
const bedGateMaterial = standard(0xb16932, { roughness: 0.58 });
const poolGate = addBox(
scene,
[0.58, 0.46, 0.13],
[-0.38, 0.51, -5.0],
poolGateMaterial
);
const bedGate = addBox(
scene,
[0.58, 0.46, 0.13],
[0.38, 0.51, -5.0],
bedGateMaterial
);
// Low collection basins at the ends of overflow routes.
addBox(
scene,
[1.6, 0.18, 1.0],
[-2.2, 0.09, 5.85],
materials.stoneDark
);
addBox(
scene,
[1.4, 0.18, 1.0],
[5.2, 0.09, 5.85],
materials.stoneDark
);
// ------------------------------------------------------------------------
// Animated water particles
// ------------------------------------------------------------------------
function makeParticlePath(points, color, count = 15, radius = 0.075) {
const segments = [];
let total = 0;
for (let i = 0; i < points.length - 1; i++) {
const a = points[i];
const b = points[i + 1];
const length = a.distanceTo(b);
segments.push({ a, b, length, start: total });
total += length;
}
const particleMaterial = standard(color, {
roughness: 0.28,
emissive: color,
emissiveIntensity: 0.13
});
const particles = [];
for (let i = 0; i < count; i++) {
const mesh = new THREE.Mesh(
new THREE.IcosahedronGeometry(radius, 0),
particleMaterial
);
mesh.castShadow = false;
scene.add(mesh);
particles.push({
mesh,
offset: i / count,
jitter: (i % 3 - 1) * 0.045
});
}
return { segments, total, particles };
}
function samplePath(path, t, output) {
const distance = Math.max(0, Math.min(0.999999, t)) * path.total;
let segment = path.segments[0];
for (let i = 0; i < path.segments.length; i++) {
const candidate = path.segments[i];
if (
distance <= candidate.start + candidate.length ||
i === path.segments.length - 1
) {
segment = candidate;
break;
}
}
const local = THREE.MathUtils.clamp(
(distance - segment.start) / segment.length,
0,
1
);
output.lerpVectors(segment.a, segment.b, local);
return output;
}
const inletParticles = makeParticlePath(
[V(0, 0.38, -5.86), V(0, 0.38, -5.0)],
0xb6f5ed,
13,
0.07
);
const poolParticles = makeParticlePath(
[
V(0, 0.38, -5.0),
V(-4.8, 0.38, -5.0),
V(-4.8, 0.38, -3.0)
],
0x72e4df,
17,
0.075
);
const bedParticles = makeParticlePath(
[
V(0, 0.38, -5.0),
V(4.8, 0.38, -5.0),
V(4.8, 0.38, -3.34)
],
0x9ce8cf,
17,
0.075
);
const poolOverflowParticles = makeParticlePath(
[
V(-8.72, 0.38, 3.48),
V(-9.4, 0.38, 3.48),
V(-9.4, 0.38, 5.85),
V(-2.2, 0.38, 5.85)
],
0xa5f3ed,
22,
0.085
);
const bedOverflowParticles = makeParticlePath(
[
V(8.47, 0.38, 3.75),
V(9.35, 0.38, 3.75),
V(9.35, 0.38, 5.85),
V(5.2, 0.38, 5.85)
],
0xa5f3ed,
20,
0.085
);
const particlePosition = new THREE.Vector3();
function updateParticlePath(path, time, speed, enabled) {
for (const particle of path.particles) {
particle.mesh.visible = enabled;
if (!enabled) continue;
const t = (time * speed + particle.offset) % 1;
samplePath(path, t, particlePosition);
particle.mesh.position.copy(particlePosition);
particle.mesh.position.x += particle.jitter;
particle.mesh.position.y += Math.sin((t + particle.offset) * 19) * 0.025;
}
}
// ------------------------------------------------------------------------
// Custom orbit, zoom, touch, and keyboard camera controls
// ------------------------------------------------------------------------
const cameraState = {
yaw: 0.68,
pitch: 0.58,
distance: 28,
target: new THREE.Vector3(1.2, 0.9, -0.8)
};
const initialCamera = { ...cameraState };
function updateCamera() {
const cp = Math.cos(cameraState.pitch);
camera.position.set(
cameraState.target.x +
cameraState.distance * cp * Math.sin(cameraState.yaw),
cameraState.target.y +
cameraState.distance * Math.sin(cameraState.pitch),
cameraState.target.z +
cameraState.distance * cp * Math.cos(cameraState.yaw)
);
camera.lookAt(cameraState.target);
}
function resetCamera() {
cameraState.yaw = initialCamera.yaw;
cameraState.pitch = initialCamera.pitch;
cameraState.distance = initialCamera.distance;
cameraState.target.copy(initialCamera.target);
updateCamera();
}
const pointers = new Map();
let pinchDistance = 0;
function pointerDistance() {
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
);
}
canvas.addEventListener("pointerdown", (event) => {
canvas.focus({ preventScroll: true });
canvas.setPointerCapture(event.pointerId);
pointers.set(event.pointerId, {
x: event.clientX,
y: event.clientY
});
canvas.classList.add("dragging");
if (pointers.size === 2) {
pinchDistance = pointerDistance();
}
});
canvas.addEventListener("pointermove", (event) => {
const pointer = pointers.get(event.pointerId);
if (!pointer) return;
const dx = event.clientX - pointer.x;
const dy = event.clientY - pointer.y;
pointer.x = event.clientX;
pointer.y = event.clientY;
if (pointers.size === 1) {
cameraState.yaw -= dx * 0.006;
cameraState.pitch = THREE.MathUtils.clamp(
cameraState.pitch + dy * 0.0045,
0.18,
1.22
);
} else if (pointers.size === 2) {
const nextDistance = pointerDistance();
if (pinchDistance > 0 && nextDistance > 0) {
cameraState.distance = THREE.MathUtils.clamp(
cameraState.distance * (pinchDistance / nextDistance),
15,
42
);
}
pinchDistance = nextDistance;
}
});
function endPointer(event) {
pointers.delete(event.pointerId);
if (pointers.size < 2) pinchDistance = 0;
if (pointers.size === 0) canvas.classList.remove("dragging");
}
canvas.addEventListener("pointerup", endPointer);
canvas.addEventListener("pointercancel", endPointer);
canvas.addEventListener("lostpointercapture", endPointer);
canvas.addEventListener(
"wheel",
(event) => {
event.preventDefault();
cameraState.distance = THREE.MathUtils.clamp(
cameraState.distance + event.deltaY * 0.018,
15,
42
);
},
{ passive: false }
);
canvas.addEventListener("dblclick", resetCamera);
canvas.addEventListener("keydown", (event) => {
let handled = true;
switch (event.key) {
case "ArrowLeft":
cameraState.yaw += 0.08;
break;
case "ArrowRight":
cameraState.yaw -= 0.08;
break;
case "ArrowUp":
cameraState.pitch = THREE.MathUtils.clamp(
cameraState.pitch + 0.06,
0.18,
1.22
);
break;
case "ArrowDown":
cameraState.pitch = THREE.MathUtils.clamp(
cameraState.pitch - 0.06,
0.18,
1.22
);
break;
case "+":
case "=":
cameraState.distance = Math.max(15, cameraState.distance - 1);
break;
case "-":
case "_":
cameraState.distance = Math.min(42, cameraState.distance + 1);
break;
case "0":
resetCamera();
break;
default:
handled = false;
}
if (handled) event.preventDefault();
});
// ------------------------------------------------------------------------
// Simplified illustrative water-balance model
// ------------------------------------------------------------------------
const model = {
flow: 60,
split: 65,
pool: 1600,
beds: 520,
poolCapacity: 3000,
bedCapacity: 1200,
overflowPoolRate: 0,
overflowBedRate: 0,
cumulativePoolOverflow: 0,
cumulativeBedOverflow: 0,
modelMinutes: 0,
running: true,
showOverflow: true
};
const SIMULATION_MINUTES_PER_SECOND = 2; // 120× real-time.
const flowClock = { value: 0 };
const poolHistory = [];
const bedHistory = [];
function simulate(realDelta) {
const modelDelta = realDelta * SIMULATION_MINUTES_PER_SECOND;
const poolRate = model.flow * (model.split / 100);
const bedRate = model.flow * (1 - model.split / 100);
model.modelMinutes += modelDelta;
const nextPool = model.pool + poolRate * modelDelta;
const nextBeds = model.beds + bedRate * modelDelta;
if (nextPool > model.poolCapacity) {
const overflow = nextPool - model.poolCapacity;
model.pool = model.poolCapacity;
model.overflowPoolRate = overflow / modelDelta;
model.cumulativePoolOverflow += overflow;
} else {
model.pool = nextPool;
model.overflowPoolRate = 0;
}
if (nextBeds > model.bedCapacity) {
const overflow = nextBeds - model.bedCapacity;
model.beds = model.bedCapacity;
model.overflowBedRate = overflow / modelDelta;
model.cumulativeBedOverflow += overflow;
} else {
model.beds = nextBeds;
model.overflowBedRate = 0;
}
}
// ------------------------------------------------------------------------
// UI bindings
// ------------------------------------------------------------------------
const flowInput = $("flow");
const splitInput = $("split");
const presetButtons = [...document.querySelectorAll(".preset")];
function updateRangeProgress(input) {
const min = Number(input.min);
const max = Number(input.max);
const value = Number(input.value);
const percent = ((value - min) / (max - min)) * 100;
input.style.setProperty("--range-progress", `${percent}%`);
}
function updatePresetSelection() {
for (const button of presetButtons) {
const active =
Number(button.dataset.flow) === model.flow &&
Number(button.dataset.split) === model.split;
button.classList.toggle("active", active);
}
}
flowInput.addEventListener("input", () => {
model.flow = Number(flowInput.value);
updateRangeProgress(flowInput);
updatePresetSelection();
});
splitInput.addEventListener("input", () => {
model.split = Number(splitInput.value);
updateRangeProgress(splitInput);
updatePresetSelection();
});
for (const button of presetButtons) {
button.addEventListener("click", () => {
model.flow = Number(button.dataset.flow);
model.split = Number(button.dataset.split);
flowInput.value = model.flow;
splitInput.value = model.split;
updateRangeProgress(flowInput);
updateRangeProgress(splitInput);
updatePresetSelection();
});
}
$("pauseBtn").addEventListener("click", () => {
model.running = !model.running;
$("pauseBtn").textContent = model.running ? "Pause" : "Resume";
$("clock").classList.toggle("paused", !model.running);
});
$("resetBtn").addEventListener("click", () => {
model.pool = 1600;
model.beds = 520;
model.overflowPoolRate = 0;
model.overflowBedRate = 0;
model.cumulativePoolOverflow = 0;
model.cumulativeBedOverflow = 0;
model.modelMinutes = 0;
flowClock.value = 0;
poolHistory.length = 0;
bedHistory.length = 0;
sampleHistory();
updateUI();
});
$("showOverflow").addEventListener("change", (event) => {
model.showOverflow = event.target.checked;
});
// ------------------------------------------------------------------------
// Readouts and history chart
// ------------------------------------------------------------------------
const numberFormatter = new Intl.NumberFormat("en-US", {
maximumFractionDigits: 0
});
function formatLitres(value) {
return `${numberFormatter.format(Math.round(value))} L`;
}
function chartPath(values) {
if (!values.length) return "";
return values
.map((value, index) => {
const x =
7 + (index / Math.max(1, values.length - 1)) * 246;
const y = 82 - value * 66;
return `${index === 0 ? "M" : "L"}${x.toFixed(2)},${y.toFixed(2)}`;
})
.join(" ");
}
function sampleHistory() {
poolHistory.push(model.pool / model.poolCapacity);
bedHistory.push(model.beds / model.bedCapacity);
if (poolHistory.length > 90) poolHistory.shift();
if (bedHistory.length > 90) bedHistory.shift();
$("poolChart").setAttribute("d", chartPath(poolHistory));
$("bedChart").setAttribute("d", chartPath(bedHistory));
}
let lastStatusState = "";
function updateUI() {
const poolRate = model.flow * (model.split / 100);
const bedRate = model.flow * (1 - model.split / 100);
const poolRatio = model.pool / model.poolCapacity;
const bedRatio = model.beds / model.bedCapacity;
$("flowValue").textContent = `${model.flow} L/min`;
$("splitValue").textContent =
`${model.split}% pool · ${100 - model.split}% beds`;
$("poolVolume").textContent = formatLitres(model.pool);
$("bedVolume").textContent = formatLitres(model.beds);
$("poolRate").textContent = `${Math.round(poolRate)} L/min to pool`;
$("bedRate").textContent = `${Math.round(bedRate)} L/min to beds`;
$("poolFill").style.width = `${THREE.MathUtils.clamp(poolRatio, 0, 1) * 100}%`;
$("bedFill").style.width = `${THREE.MathUtils.clamp(bedRatio, 0, 1) * 100}%`;
$("poolOverflowRate").textContent =
`${Math.round(model.overflowPoolRate)} L/min overflow`;
$("bedOverflowRate").textContent =
`${Math.round(model.overflowBedRate)} L/min overflow`;
$("poolOverflowTotal").textContent =
`${formatLitres(model.cumulativePoolOverflow)} total`;
$("bedOverflowTotal").textContent =
`${formatLitres(model.cumulativeBedOverflow)} total`;
const minutes = Math.floor(model.modelMinutes);
const clockText = `${String(Math.floor(minutes / 60)).padStart(2, "0")}:${String(
minutes % 60
).padStart(2, "0")}`;
$("clock").textContent = `${clockText} · ×120`;
let state = "normal";
let message = "Water is entering both destinations.";
if (model.flow === 0) {
state = "idle";
message = "No inflow is active.";
} else if (
model.overflowPoolRate > 0.01 &&
model.overflowBedRate > 0.01
) {
state = "both";
message = "Both overflow routes are active.";
} else if (model.overflowPoolRate > 0.01) {
state = "pool";
message = "The pool is at capacity; its overflow route is active.";
} else if (model.overflowBedRate > 0.01) {
state = "bed";
message = "Bed storage is full; its overflow route is active.";
} else if (model.split === 100) {
message = "All illustrative inflow is entering the pool.";
} else if (model.split === 0) {
message = "All illustrative inflow is entering the planted beds.";
}
if (state !== lastStatusState) {
$("status").dataset.state = state;
$("status").textContent = message;
$("statusText").textContent =
state === "pool"
? "Pool overflow"
: state === "bed"
? "Bed overflow"
: state === "both"
? "Both active"
: state === "idle"
? "No inflow"
: "Within storage";
lastStatusState = state;
}
}
// ------------------------------------------------------------------------
// World labels
// ------------------------------------------------------------------------
function positionLabel(element, worldPosition) {
const projected = worldPosition.clone().project(camera);
const visible = projected.z > -1 && projected.z < 1;
element.style.left = `${(projected.x * 0.5 + 0.5) * window.innerWidth}px`;
element.style.top = `${(-projected.y * 0.5 + 0.5) * window.innerHeight}px`;
element.style.visibility = visible ? "visible" : "hidden";
}
const poolLabelPosition = new THREE.Vector3();
const bedLabelPosition = new THREE.Vector3(4.8, 1.55, 0.3);
const inletLabelPosition = new THREE.Vector3(0, 1.9, -6.3);
const poolOverflowLabelPosition = new THREE.Vector3(-8.9, 1.4, 3.55);
const bedOverflowLabelPosition = new THREE.Vector3(8.6, 1.25, 3.82);
// ------------------------------------------------------------------------
// Animation
// ------------------------------------------------------------------------
const drySoil = new THREE.Color(0x846341);
const wetSoil = new THREE.Color(0x49392a);
const temporaryColor = new THREE.Color();
let previousTime = performance.now();
let uiAccumulator = 0;
let historyAccumulator = 0;
function animate(timestamp) {
const realDelta = Math.min((timestamp - previousTime) / 1000, 0.05);
previousTime = timestamp;
if (model.running) {
simulate(realDelta);
flowClock.value += realDelta * (reducedMotion ? 0.35 : 1);
}
const poolRatio = model.pool / model.poolCapacity;
const bedRatio = model.beds / model.bedCapacity;
// Pool surface height and subtle low-poly ripples.
poolWater.position.y = 0.45 + poolRatio * 0.42;
const positions = poolGeometry.attributes.position;
for (let i = 0; i < positions.count; i++) {
const x = positions.getX(i);
const z = positions.getZ(i);
const wave =
Math.sin(x * 1.8 + flowClock.value * 1.4) * 0.014 +
Math.cos(z * 2.3 - flowClock.value * 1.1) * 0.01;
positions.setY(i, wave);
}
positions.needsUpdate = true;
// Bed wetness changes the soil and irrigation ribbons.
for (const soilMaterial of bedSoilMaterials) {
temporaryColor.copy(drySoil).lerp(wetSoil, bedRatio);
soilMaterial.color.copy(temporaryColor);
}
bedWetMaterial.opacity = 0.12 + bedRatio * 0.48;
// Gate boards visually reflect the split.
const poolGateScale = 0.35 + (model.split / 100) * 1.35;
const bedGateScale = 0.35 + (1 - model.split / 100) * 1.35;
poolGate.scale.y = poolGateScale;
bedGate.scale.y = bedGateScale;
poolGate.position.y = 0.28 + (0.46 * poolGateScale) / 2;
bedGate.position.y = 0.28 + (0.46 * bedGateScale) / 2;
// Source ring pulse.
const pulse = model.flow > 0
? 1 + Math.sin(flowClock.value * 2.1) * 0.055
: 1;
fountainRing.scale.setScalar(pulse);
// Subtle foliage movement.
const motionTime = reducedMotion ? 0 : timestamp * 0.001;
for (const foliage of plantFoliage) {
foliage.rotation.z =
Math.sin(motionTime * 1.05 + foliage.userData.phase) * 0.025;
}
// Flow particles.
const inletSpeed = 0.08 + (model.flow / 120) * 0.14;
const poolRate = model.flow * (model.split / 100);
const bedRate = model.flow * (1 - model.split / 100);
const poolSpeed = 0.08 + (poolRate / 120) * 0.14;
const bedSpeed = 0.08 + (bedRate / 120) * 0.14;
updateParticlePath(
inletParticles,
flowClock.value,
inletSpeed,
model.flow > 0
);
updateParticlePath(
poolParticles,
flowClock.value,
poolSpeed,
poolRate > 0
);
updateParticlePath(
bedParticles,
flowClock.value,
bedSpeed,
bedRate > 0
);
// Overflow routes and active markers.
const poolOverflowActive = model.overflowPoolRate > 0.01;
const bedOverflowActive = model.overflowBedRate > 0.01;
poolOverflow.group.visible = model.showOverflow;
bedOverflow.group.visible = model.showOverflow;
poolOverflow.waterMaterial.opacity = poolOverflowActive ? 0.88 : 0.12;
bedOverflow.waterMaterial.opacity = bedOverflowActive ? 0.88 : 0.12;
poolSpill.visible = poolOverflowActive;
bedSpill.visible = bedOverflowActive;
poolMarker.visible = poolOverflowActive;
bedMarker.visible = bedOverflowActive;
updateParticlePath(
poolOverflowParticles,
flowClock.value,
0.12 + (model.overflowPoolRate / 120) * 0.2,
model.showOverflow && poolOverflowActive
);
updateParticlePath(
bedOverflowParticles,
flowClock.value,
0.12 + (model.overflowBedRate / 120) * 0.2,
model.showOverflow && bedOverflowActive
);
// Labels.
poolLabelPosition.set(-4.8, poolWater.position.y + 1.1, 0.2);
positionLabel($("labelPool"), poolLabelPosition);
positionLabel($("labelBeds"), bedLabelPosition);
positionLabel($("labelInlet"), inletLabelPosition);
positionLabel($("labelPoolOverflow"), poolOverflowLabelPosition);
positionLabel($("labelBedOverflow"), bedOverflowLabelPosition);
$("labelPoolOverflow").classList.toggle(
"active",
model.showOverflow && poolOverflowActive
);
$("labelBedOverflow").classList.toggle(
"active",
model.showOverflow && bedOverflowActive
);
uiAccumulator += realDelta;
historyAccumulator += realDelta;
if (uiAccumulator >= 0.1) {
updateUI();
uiAccumulator = 0;
}
if (historyAccumulator >= 0.25) {
sampleHistory();
historyAccumulator = 0;
}
updateCamera();
renderer.render(scene, camera);
}
// ------------------------------------------------------------------------
// Resize and startup
// ------------------------------------------------------------------------
function resize() {
const width = window.innerWidth;
const height = window.innerHeight;
renderer.setPixelRatio(Math.min(window.devicePixelRatio || 1, 2));
renderer.setSize(width, height, false);
camera.aspect = width / height;
camera.updateProjectionMatrix();
}
window.addEventListener("resize", resize);
updateRangeProgress(flowInput);
updateRangeProgress(splitInput);
updatePresetSelection();
updateCamera();
sampleHistory();
updateUI();
resize();
renderer.setAnimationLoop(animate);
</script>
</body>
</html>
```