Prompt specification
Build or customize the Three.js 3D scene "Limited-water distribution game" 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: Limited-water distribution game
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/limited-water-distribution-game-x1060
Scene Description Prompt:
Make a standalone HTML scene with Three.js from cdnjs that depicts drip irrigation. Create a small garden network with finite reservoir tokens and plant requests; users open valves and allocate units while the total withdrawn amount matches visible deliveries. Voxel style.
Working Three.js Starter (limited-water-distribution-game-x1060):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Drip Field — Voxel Irrigation</title>
<style>
:root {
--ink: #173d34;
--ink-soft: #587068;
--paper: #f4f1e7;
--panel: #fffdf7;
--line: #d8d8c9;
--blue: #1789d1;
--blue-dark: #09689f;
--orange: #e76b42;
--green: #32865b;
--shadow: 0 12px 32px rgba(23, 61, 52, 0.12);
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: var(--paper);
color: var(--ink);
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
button {
font: inherit;
}
button:focus-visible {
outline: 3px solid rgba(23, 137, 209, 0.35);
outline-offset: 2px;
}
.topbar {
position: fixed;
inset: 0 0 auto 0;
z-index: 20;
height: 76px;
display: flex;
align-items: center;
justify-content: space-between;
gap: 24px;
padding: 0 24px;
background: var(--panel);
border-bottom: 1px solid var(--line);
box-shadow: 0 4px 16px rgba(23, 61, 52, 0.05);
}
.brand {
display: flex;
align-items: center;
gap: 12px;
min-width: 220px;
}
.brand-mark {
width: 38px;
height: 38px;
display: grid;
place-items: center;
border-radius: 10px;
background: var(--ink);
color: #bde5f5;
font-size: 22px;
box-shadow: inset 0 -4px 0 rgba(0, 0, 0, 0.12);
}
.brand h1 {
margin: 0;
font-size: 18px;
line-height: 1;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.brand p {
margin: 5px 0 0;
color: var(--ink-soft);
font-size: 11px;
font-weight: 700;
letter-spacing: 0.13em;
text-transform: uppercase;
}
.metrics {
display: flex;
align-items: stretch;
justify-content: center;
gap: 10px;
}
.metric {
min-width: 128px;
padding: 9px 14px;
border: 1px solid var(--line);
border-radius: 12px;
background: #f7f6ef;
}
.metric-label {
display: block;
color: var(--ink-soft);
font-size: 9px;
font-weight: 800;
letter-spacing: 0.13em;
text-transform: uppercase;
}
.metric-value {
display: flex;
align-items: baseline;
gap: 5px;
margin-top: 2px;
font-size: 22px;
font-weight: 850;
line-height: 1;
}
.metric-value small {
color: var(--ink-soft);
font-size: 11px;
font-weight: 700;
}
.token-pips {
display: flex;
flex-wrap: wrap;
gap: 3px;
width: 88px;
margin-top: 7px;
}
.token-pip {
width: 8px;
height: 8px;
border-radius: 2px;
background: var(--blue);
box-shadow: inset 0 -2px 0 rgba(0, 0, 0, 0.12);
}
.token-pip.spent {
background: #d7d9d1;
box-shadow: none;
}
.reset-button {
min-width: 108px;
padding: 11px 15px;
border: 1px solid var(--line);
border-radius: 11px;
background: white;
color: var(--ink);
cursor: pointer;
font-size: 12px;
font-weight: 800;
transition: transform 150ms ease, background 150ms ease;
}
.reset-button:hover {
transform: translateY(-1px);
background: #eef4ec;
}
#stage {
position: fixed;
inset: 76px 356px 0 0;
overflow: hidden;
background:
radial-gradient(circle at 50% 40%, rgba(255, 255, 255, 0.55), transparent 48%),
#dfe8dd;
}
#canvasHost,
#labels {
position: absolute;
inset: 0;
}
#canvasHost canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
}
#labels {
pointer-events: none;
overflow: hidden;
}
.scene-heading {
position: absolute;
top: 22px;
left: 24px;
z-index: 3;
pointer-events: none;
}
.scene-heading .eyebrow {
color: var(--ink-soft);
font-size: 10px;
font-weight: 850;
letter-spacing: 0.16em;
text-transform: uppercase;
}
.scene-heading h2 {
margin: 4px 0 0;
font-size: 24px;
letter-spacing: -0.03em;
}
.scene-heading p {
margin: 4px 0 0;
color: var(--ink-soft);
font-size: 12px;
}
.world-label {
position: absolute;
min-width: 98px;
padding: 7px 9px;
border: 1px solid rgba(23, 61, 52, 0.18);
border-radius: 9px;
background: rgba(255, 253, 247, 0.9);
box-shadow: 0 5px 14px rgba(23, 61, 52, 0.1);
color: var(--ink);
text-align: center;
transform: translate(-50%, -100%);
backdrop-filter: blur(5px);
transition: border-color 150ms ease, background 150ms ease;
}
.world-label.selected {
border-color: var(--orange);
background: #fffaf0;
}
.world-label b {
display: block;
font-size: 11px;
}
.world-label span {
display: block;
margin-top: 2px;
color: var(--ink-soft);
font-size: 9px;
font-weight: 800;
letter-spacing: 0.06em;
text-transform: uppercase;
}
.world-label.reservoir-label {
border-color: rgba(23, 137, 209, 0.35);
}
.camera-tools {
position: absolute;
left: 20px;
bottom: 22px;
z-index: 5;
display: flex;
gap: 6px;
padding: 6px;
border: 1px solid rgba(23, 61, 52, 0.14);
border-radius: 12px;
background: rgba(255, 253, 247, 0.88);
box-shadow: var(--shadow);
backdrop-filter: blur(6px);
}
.camera-tools button {
width: 34px;
height: 32px;
border: 0;
border-radius: 7px;
background: #edf1e9;
color: var(--ink);
cursor: pointer;
font-size: 18px;
font-weight: 850;
}
.camera-tools button:hover {
background: #dce8dc;
}
.scene-legend {
position: absolute;
right: 20px;
bottom: 22px;
z-index: 4;
display: flex;
align-items: center;
gap: 13px;
padding: 10px 13px;
border: 1px solid rgba(23, 61, 52, 0.14);
border-radius: 12px;
background: rgba(255, 253, 247, 0.88);
box-shadow: var(--shadow);
color: var(--ink-soft);
font-size: 10px;
font-weight: 750;
pointer-events: none;
backdrop-filter: blur(6px);
}
.legend-item {
display: flex;
align-items: center;
gap: 6px;
}
.legend-square {
width: 10px;
height: 10px;
border-radius: 2px;
}
.legend-square.water {
background: var(--blue);
}
.legend-square.valve {
background: var(--orange);
}
.legend-square.request {
background: var(--ink-soft);
}
#panel {
position: fixed;
inset: 76px 0 0 auto;
z-index: 15;
width: 356px;
overflow-y: auto;
padding: 20px;
background: var(--panel);
border-left: 1px solid var(--line);
box-shadow: -8px 0 26px rgba(23, 61, 52, 0.07);
}
.panel-heading {
display: flex;
align-items: center;
justify-content: space-between;
margin-bottom: 16px;
}
.panel-heading h2 {
margin: 0;
font-size: 15px;
letter-spacing: 0.09em;
text-transform: uppercase;
}
.finite-badge {
padding: 5px 8px;
border-radius: 99px;
background: #e4f1e8;
color: var(--green);
font-size: 9px;
font-weight: 850;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.selected-card {
padding: 17px;
border: 1px solid var(--line);
border-radius: 16px;
background: #f7f6ee;
box-shadow: 0 7px 20px rgba(23, 61, 52, 0.06);
}
.selected-top {
display: flex;
align-items: center;
gap: 12px;
}
.crop-swatch {
width: 42px;
height: 42px;
flex: 0 0 auto;
display: grid;
place-items: center;
border-radius: 12px;
color: white;
font-size: 21px;
box-shadow: inset 0 -4px 0 rgba(0, 0, 0, 0.12);
}
.selected-title {
min-width: 0;
flex: 1;
}
.selected-title small {
display: block;
margin-bottom: 2px;
color: var(--ink-soft);
font-size: 9px;
font-weight: 850;
letter-spacing: 0.11em;
text-transform: uppercase;
}
.selected-title h3 {
margin: 0;
font-size: 22px;
letter-spacing: -0.03em;
}
.request-badge {
padding: 6px 8px;
border-radius: 8px;
background: white;
color: var(--ink-soft);
font-size: 10px;
font-weight: 800;
white-space: nowrap;
}
.crop-description {
margin: 12px 0;
color: var(--ink-soft);
font-size: 12px;
line-height: 1.45;
}
.progress-row {
display: flex;
align-items: center;
justify-content: space-between;
margin-bottom: 7px;
font-size: 11px;
font-weight: 800;
}
.progress-row span:last-child {
color: var(--blue-dark);
}
.progress-track {
height: 9px;
overflow: hidden;
border-radius: 99px;
background: #dfe2d9;
}
.progress-fill {
width: 0;
height: 100%;
border-radius: inherit;
background: var(--blue);
transition: width 300ms cubic-bezier(0.2, 0.8, 0.2, 1);
}
.action-button {
width: 100%;
margin-top: 13px;
padding: 12px 14px;
border: 0;
border-radius: 10px;
cursor: pointer;
font-size: 12px;
font-weight: 850;
transition: transform 150ms ease, filter 150ms ease, opacity 150ms ease;
}
.action-button:hover:not(:disabled) {
transform: translateY(-1px);
filter: brightness(1.03);
}
.action-button:disabled {
cursor: not-allowed;
opacity: 0.42;
}
#valveToggle {
display: flex;
align-items: center;
justify-content: center;
gap: 8px;
background: #ecece4;
color: var(--ink);
box-shadow: inset 0 -3px 0 rgba(0, 0, 0, 0.08);
}
#valveToggle.open {
background: #dcefe1;
color: #246d45;
}
.status-dot {
width: 9px;
height: 9px;
border-radius: 50%;
background: var(--orange);
box-shadow: 0 0 0 3px rgba(231, 107, 66, 0.13);
}
#valveToggle.open .status-dot {
background: var(--green);
box-shadow: 0 0 0 3px rgba(50, 134, 91, 0.13);
}
#allocateButton {
background: var(--blue);
color: white;
box-shadow: inset 0 -4px 0 var(--blue-dark);
}
.action-note {
min-height: 31px;
margin: 11px 1px 0;
color: var(--ink-soft);
font-size: 10px;
line-height: 1.4;
}
.section-title {
display: flex;
align-items: center;
justify-content: space-between;
margin: 22px 0 10px;
font-size: 10px;
font-weight: 850;
letter-spacing: 0.13em;
text-transform: uppercase;
}
.section-title span:last-child {
color: var(--ink-soft);
letter-spacing: 0;
text-transform: none;
font-weight: 700;
}
#plantList {
display: grid;
gap: 8px;
}
.plant-row {
width: 100%;
display: grid;
grid-template-columns: 10px 1fr auto auto;
align-items: center;
gap: 10px;
padding: 11px 12px;
border: 1px solid var(--line);
border-radius: 11px;
background: white;
color: var(--ink);
text-align: left;
cursor: pointer;
transition: border-color 150ms ease, transform 150ms ease;
}
.plant-row:hover {
transform: translateX(-2px);
border-color: #aebdaf;
}
.plant-row.selected {
border-color: var(--orange);
box-shadow: inset 3px 0 0 var(--orange);
}
.plant-row-dot {
width: 10px;
height: 10px;
border-radius: 3px;
}
.plant-row-name strong {
display: block;
font-size: 12px;
}
.plant-row-name small {
display: block;
margin-top: 2px;
color: var(--ink-soft);
font-size: 9px;
}
.mini-valve {
padding: 4px 6px;
border-radius: 6px;
background: #ecece5;
color: var(--ink-soft);
font-size: 8px;
font-weight: 850;
text-transform: uppercase;
}
.mini-valve.open {
background: #dcefe1;
color: #246d45;
}
.mini-count {
color: var(--blue-dark);
font-size: 11px;
font-weight: 850;
}
.audit-card {
padding: 14px;
border: 1px solid #bdd8c4;
border-radius: 13px;
background: #edf6ec;
}
.audit-heading {
display: flex;
align-items: center;
justify-content: space-between;
gap: 8px;
font-size: 11px;
font-weight: 850;
}
.audit-ok {
padding: 4px 7px;
border-radius: 99px;
background: #cde8d0;
color: #286944;
font-size: 8px;
letter-spacing: 0.08em;
}
.audit-equation {
margin: 10px 0 6px;
color: var(--ink);
font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
font-size: 11px;
line-height: 1.5;
}
.audit-detail {
margin: 0;
color: var(--ink-soft);
font-size: 10px;
line-height: 1.45;
}
@media (max-width: 980px) {
.topbar {
gap: 12px;
padding: 0 16px;
}
.brand {
min-width: auto;
}
.brand p,
.metric.requests-metric {
display: none;
}
.metric {
min-width: 104px;
}
#stage {
right: 326px;
}
#panel {
width: 326px;
padding: 16px;
}
}
@media (max-width: 720px) {
.topbar {
height: 64px;
}
.brand-mark {
width: 34px;
height: 34px;
}
.brand h1 {
font-size: 14px;
}
.metrics .metric {
min-width: 84px;
padding: 7px 9px;
}
.metric-value {
font-size: 17px;
}
.token-pips,
.reset-button span {
display: none;
}
.reset-button {
min-width: 38px;
width: 38px;
height: 38px;
padding: 0;
font-size: 0;
}
.reset-button::after {
content: "↺";
font-size: 20px;
}
#stage {
inset: 64px 0 310px 0;
}
#panel {
inset: auto 0 0 0;
width: 100%;
height: 310px;
padding: 14px 16px 24px;
border-top: 1px solid var(--line);
border-left: 0;
}
.scene-heading {
top: 15px;
left: 16px;
}
.scene-heading h2 {
font-size: 19px;
}
.scene-heading p,
.scene-legend {
display: none;
}
.camera-tools {
left: 14px;
bottom: 13px;
}
.panel-heading {
margin-bottom: 10px;
}
.selected-card {
padding: 13px;
}
.crop-description,
.action-note {
display: none;
}
.action-button {
margin-top: 9px;
padding: 10px;
}
.section-title {
margin-top: 14px;
}
.world-label {
min-width: 76px;
padding: 5px 7px;
}
.world-label b {
font-size: 9px;
}
.world-label span {
font-size: 7px;
}
}
</style>
</head>
<body>
<header class="topbar">
<div class="brand">
<div class="brand-mark" aria-hidden="true">▦</div>
<div>
<h1>Drip Field</h1>
<p>Voxel irrigation control</p>
</div>
</div>
<div class="metrics">
<div class="metric">
<span class="metric-label">Reservoir</span>
<div class="metric-value">
<span id="remainingCount">12</span>
<small>/ 12 units</small>
</div>
<div class="token-pips" id="tokenPips" aria-hidden="true"></div>
</div>
<div class="metric">
<span class="metric-label">Withdrawn</span>
<div class="metric-value">
<span id="withdrawnCount">0</span>
<small>units</small>
</div>
</div>
<div class="metric requests-metric">
<span class="metric-label">Plant requests</span>
<div class="metric-value">
<span id="requestCount">12</span>
<small>total</small>
</div>
</div>
</div>
<button class="reset-button" id="resetDemo" title="Restart the irrigation allocation">
↺ <span>Restart demo</span>
</button>
</header>
<main id="stage">
<div id="canvasHost"></div>
<div id="labels"></div>
<div class="scene-heading">
<div class="eyebrow">North garden / plot 04</div>
<h2>Four beds, one finite supply.</h2>
<p>Open a valve, then allocate water units.</p>
</div>
<div class="camera-tools" aria-label="Camera controls">
<button id="zoomOut" title="Zoom out" aria-label="Zoom out">−</button>
<button id="zoomIn" title="Zoom in" aria-label="Zoom in">+</button>
<button id="resetCamera" title="Reset camera" aria-label="Reset camera">⌂</button>
</div>
<div class="scene-legend">
<span class="legend-item"><i class="legend-square water"></i>delivery unit</span>
<span class="legend-item"><i class="legend-square valve"></i>closed valve</span>
<span class="legend-item"><i class="legend-square request"></i>request</span>
</div>
</main>
<aside id="panel">
<div class="panel-heading">
<h2>Allocation desk</h2>
<span class="finite-badge">Finite source</span>
</div>
<section class="selected-card">
<div class="selected-top">
<div class="crop-swatch" id="selectedSwatch">●</div>
<div class="selected-title">
<small>Selected bed</small>
<h3 id="selectedName">Tomato</h3>
</div>
<div class="request-badge" id="selectedRequest">3 requested</div>
</div>
<p class="crop-description" id="selectedDescription"></p>
<div class="progress-row">
<span>Visible deliveries</span>
<span id="selectedProgressText">0 / 3</span>
</div>
<div class="progress-track">
<div class="progress-fill" id="selectedProgress"></div>
</div>
<button class="action-button" id="valveToggle" aria-pressed="false">
<span class="status-dot"></span>
<span id="valveToggleText">Open valve</span>
</button>
<button class="action-button" id="allocateButton">Deliver 1 unit</button>
<p class="action-note" id="actionNote"></p>
</section>
<div class="section-title">
<span>Plant requests</span>
<span id="fulfilledCount">0 / 4 fulfilled</span>
</div>
<div id="plantList"></div>
<div class="section-title">
<span>Water ledger</span>
<span>live check</span>
</div>
<section class="audit-card">
<div class="audit-heading">
<span>Conservation check</span>
<span class="audit-ok">✓ Balanced</span>
</div>
<div class="audit-equation" id="auditEquation"></div>
<p class="audit-detail" id="auditDetail"></p>
</section>
</aside>
<script type="module">
import * as THREE from "https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const PLANTS = [
{
id: "tomato",
name: "Tomato",
icon: "●",
request: 3,
x: -2.25,
z: -3.15,
cropColor: 0xe6533e,
description: "Deep watering for fruiting stems. Three units will fill the bed."
},
{
id: "basil",
name: "Basil",
icon: "◆",
request: 2,
x: 2.25,
z: -3.15,
cropColor: 0x50a65b,
description: "A shallow drink is enough. Basil prefers two measured units."
},
{
id: "carrot",
name: "Carrot",
icon: "▲",
request: 4,
x: -2.25,
z: 3.15,
cropColor: 0xed8a32,
description: "Root crops need the longest run. Carrots request four units."
},
{
id: "lavender",
name: "Lavender",
icon: "✦",
request: 3,
x: 2.25,
z: 3.15,
cropColor: 0x8d67bd,
description: "A measured sip keeps the flowers vivid. Three units requested."
}
];
const SOURCE_TOTAL = PLANTS.reduce((sum, plant) => sum + plant.request, 0);
const state = {
remaining: SOURCE_TOTAL,
selected: "tomato",
open: Object.fromEntries(PLANTS.map((plant) => [plant.id, false])),
delivered: Object.fromEntries(PLANTS.map((plant) => [plant.id, 0]))
};
const stage = document.getElementById("stage");
const canvasHost = document.getElementById("canvasHost");
const labelsRoot = document.getElementById("labels");
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xdfe8dd);
scene.fog = new THREE.Fog(0xdfe8dd, 27, 48);
const renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.domElement.setAttribute("aria-label", "Interactive voxel garden");
canvasHost.appendChild(renderer.domElement);
const camera = new THREE.OrthographicCamera(-10, 10, 8, -8, 0.1, 100);
const cameraState = {
azimuth: 0.63,
polar: 0.82,
zoom: 1,
target: new THREE.Vector3(-0.35, 0.35, 0)
};
const hemisphere = new THREE.HemisphereLight(0xf6fbff, 0x6d7656, 2.1);
scene.add(hemisphere);
const sun = new THREE.DirectionalLight(0xfff2d3, 3.25);
sun.position.set(-8, 16, 10);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -13;
sun.shadow.camera.right = 13;
sun.shadow.camera.top = 13;
sun.shadow.camera.bottom = -13;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 40;
sun.shadow.bias = -0.00035;
scene.add(sun);
const fill = new THREE.DirectionalLight(0xc7e7ff, 0.75);
fill.position.set(10, 8, -9);
scene.add(fill);
const materialCache = new Map();
function getMaterial(color, emissive = 0) {
const key = `${color}-${emissive}`;
if (!materialCache.has(key)) {
materialCache.set(
key,
new THREE.MeshStandardMaterial({
color,
roughness: 0.82,
metalness: 0.02,
flatShading: true,
emissive,
emissiveIntensity: emissive ? 0.2 : 0
})
);
}
return materialCache.get(key);
}
function addBox(
parent,
width,
height,
depth,
color,
x = 0,
y = 0,
z = 0,
options = {}
) {
const mesh = new THREE.Mesh(
new THREE.BoxGeometry(width, height, depth),
options.material || getMaterial(color)
);
mesh.position.set(x, y, z);
mesh.castShadow = options.castShadow !== false;
mesh.receiveShadow = options.receiveShadow !== false;
parent.add(mesh);
return mesh;
}
// Garden platform and voxel landscaping.
addBox(scene, 14.4, 0.62, 11.25, 0xbeb49d, 0, -0.43, 0);
addBox(scene, 13.95, 0.17, 10.8, 0x86b36c, 0, -0.025, 0);
// Central service path.
addBox(scene, 13.3, 0.08, 0.72, 0xd8d0b8, 0, 0.105, 0);
addBox(scene, 0.72, 0.075, 9.7, 0xd8d0b8, 0, 0.102, 0);
// Small voxel stones beside the path.
const stoneColors = [0xc8c2b1, 0xb9b4a4, 0xd8d1bd];
for (let i = 0; i < 14; i++) {
const side = i % 2 ? 1 : -1;
addBox(
scene,
0.18 + (i % 3) * 0.035,
0.08,
0.15 + (i % 2) * 0.035,
stoneColors[i % stoneColors.length],
side * (1.05 + (i % 3) * 0.25),
0.15,
-4.45 + Math.floor(i / 2) * 0.55
);
}
// Back fence.
for (let i = 0; i < 12; i++) {
const x = -6.2 + i * 1.12;
addBox(scene, 0.18, 0.9, 0.18, 0xe8e1cc, x, 0.45, -5.05);
}
addBox(scene, 13.1, 0.13, 0.12, 0xe8e1cc, 0, 0.63, -5.05);
addBox(scene, 13.1, 0.13, 0.12, 0xe8e1cc, 0, 0.29, -5.05);
// Reservoir: an open-front voxel tank with twelve physical blue tokens.
const reservoir = new THREE.Group();
reservoir.position.set(-5.15, 0, 0);
scene.add(reservoir);
addBox(reservoir, 2.55, 0.28, 2.35, 0x69786f, 0, 0.07, 0);
addBox(reservoir, 2.3, 1.3, 0.16, 0x55776c, 0, 0.82, -1.03);
addBox(reservoir, 0.16, 1.3, 2.05, 0x55776c, -1.08, 0.82, 0);
addBox(reservoir, 0.16, 1.3, 2.05, 0x55776c, 1.08, 0.82, 0);
addBox(reservoir, 2.25, 0.28, 0.16, 0x6e897b, 0, 0.32, 1.03);
addBox(reservoir, 2.45, 0.2, 2.25, 0xd7c77e, 0, 1.56, 0);
addBox(reservoir, 0.2, 1.35, 0.2, 0x34594d, -1.02, 0.83, -0.96);
addBox(reservoir, 0.2, 1.35, 0.2, 0x34594d, 1.02, 0.83, -0.96);
addBox(reservoir, 0.66, 0.3, 0.12, 0xe76b42, 0, 1.84, -0.92);
const reservoirTokens = [];
const tokenX = [-0.62, -0.21, 0.21, 0.62];
const tokenZ = [-0.55, 0, 0.55];
for (const z of tokenZ) {
for (const x of tokenX) {
const token = addBox(
reservoir,
0.32,
0.32,
0.32,
0x1789d1,
x,
0.55,
z,
{ material: getMaterial(0x1789d1, 0x063a60) }
);
token.userData.baseY = token.position.y;
reservoirTokens.push(token);
}
}
// Pipe network: main manifold plus four branches.
const pipeColor = 0x506864;
const pipeMetal = 0x72847e;
addBox(scene, 6.75, 0.18, 0.18, pipeColor, -0.92, 0.3, 0);
addBox(scene, 0.34, 0.24, 0.34, pipeMetal, -4.0, 0.3, 0);
for (const x of [-2.25, 2.25]) {
addBox(scene, 0.25, 0.24, 0.25, pipeMetal, x, 0.3, 0);
}
const plantVisuals = new Map();
const valveVisuals = new Map();
const interactiveObjects = [];
const worldLabels = new Map();
function tagInteractive(object, plantId = null, valveId = null) {
object.userData.plantId = plantId;
object.userData.valveId = valveId;
interactiveObjects.push(object);
}
function createPlantShape(plant) {
const group = new THREE.Group();
if (plant.id === "tomato") {
addBox(group, 0.14, 1.35, 0.14, 0x397a42, 0, 0.7, 0);
addBox(group, 0.58, 0.12, 0.22, 0x4c984c, -0.23, 0.9, 0);
addBox(group, 0.58, 0.12, 0.22, 0x4c984c, 0.23, 1.13, 0);
addBox(group, 0.48, 0.12, 0.2, 0x3f8947, -0.2, 0.53, 0);
addBox(group, 0.24, 0.24, 0.24, 0xe6533e, -0.26, 1.25, 0.03);
addBox(group, 0.22, 0.22, 0.22, 0xf06a3b, 0.24, 0.8, 0.02);
} else if (plant.id === "basil") {
for (const [x, z, h] of [
[-0.34, 0.05, 0.92],
[0.3, 0.08, 1.08],
[0, -0.22, 0.82]
]) {
addBox(group, 0.11, h, 0.11, 0x347743, x, h / 2 + 0.05, z);
addBox(group, 0.5, 0.16, 0.24, 0x55a956, x, h * 0.72, z);
addBox(group, 0.28, 0.16, 0.22, 0x67b967, x + 0.13, h, z);
}
} else if (plant.id === "carrot") {
addBox(group, 0.42, 0.38, 0.42, 0xe88732, 0, 0.21, 0);
addBox(group, 0.16, 0.8, 0.16, 0x3e8d47, -0.12, 0.66, 0);
addBox(group, 0.16, 0.68, 0.16, 0x4a9d4e, 0.13, 0.6, 0.05);
addBox(group, 0.16, 0.6, 0.16, 0x62aa52, 0.03, 0.56, -0.17);
addBox(group, 0.42, 0.12, 0.2, 0x68ae52, 0, 0.95, 0);
} else {
for (const [x, z, h] of [
[-0.28, 0.03, 1.12],
[0.02, -0.08, 1.32],
[0.31, 0.11, 1.02]
]) {
addBox(group, 0.11, h, 0.11, 0x497d4e, x, h / 2 + 0.05, z);
addBox(group, 0.26, 0.25, 0.26, 0x8d67bd, x, h + 0.05, z);
addBox(group, 0.2, 0.15, 0.2, 0xa98bd2, x, h + 0.25, z);
}
}
return group;
}
function createWorldLabel(plantId, title, subtitle, reservoirLabel = false) {
const label = document.createElement("div");
label.className = `world-label${reservoirLabel ? " reservoir-label" : ""}`;
label.innerHTML = `<b>${title}</b><span>${subtitle}</span>`;
labelsRoot.appendChild(label);
worldLabels.set(plantId, label);
return label;
}
for (const plant of PLANTS) {
const sign = Math.sign(plant.z);
// Branch pipe and coupling.
addBox(scene, 0.18, 0.18, 2.42, pipeColor, plant.x, 0.3, sign * 1.21);
addBox(scene, 0.28, 0.24, 0.28, pipeMetal, plant.x, 0.3, sign * 0.25);
// Raised soil bed.
const plot = new THREE.Group();
plot.position.set(plant.x, 0, plant.z);
scene.add(plot);
addBox(plot, 2.55, 0.24, 2.55, 0x795940, 0, 0.11, 0);
addBox(plot, 2.72, 0.18, 0.16, 0xa97c51, 0, 0.25, -1.28);
addBox(plot, 2.72, 0.18, 0.16, 0xa97c51, 0, 0.25, 1.28);
addBox(plot, 0.16, 0.18, 2.42, 0xa97c51, -1.28, 0.25, 0);
addBox(plot, 0.16, 0.18, 2.42, 0xa97c51, 1.28, 0.25, 0);
// Plant geometry.
const plantGroup = createPlantShape(plant);
plantGroup.position.y = 0.24;
plantGroup.scale.setScalar(0.52);
plot.add(plantGroup);
plantGroup.traverse((object) => {
if (object.isMesh) tagInteractive(object, plant.id);
});
// A visible tray holds one solid cube per delivered unit.
addBox(plot, 1.46, 0.08, 0.38, 0x536963, 0, 0.29, 0.89);
const markers = [];
const markerMaterial = getMaterial(0x1789d1, 0x063f67);
for (let i = 0; i < plant.request; i++) {
const spacing = 0.31;
const x = (i - (plant.request - 1) / 2) * spacing;
const marker = addBox(
plot,
0.25,
0.25,
0.25,
0x1789d1,
x,
0.45,
0.89,
{ material: markerMaterial }
);
marker.visible = false;
marker.scale.setScalar(0);
tagInteractive(marker, plant.id);
markers.push(marker);
}
// Voxel valve.
const valve = new THREE.Group();
valve.position.set(plant.x, 0.3, sign * 1.28);
scene.add(valve);
const valveBody = addBox(valve, 0.42, 0.28, 0.42, pipeMetal, 0, 0, 0);
const valveStem = addBox(valve, 0.1, 0.45, 0.1, 0x405d52, 0, 0.32, 0);
const wheelMaterial = new THREE.MeshStandardMaterial({
color: 0xe76b42,
roughness: 0.75,
flatShading: true
});
const wheelA = addBox(
valve,
0.64,
0.11,
0.13,
0xe76b42,
0,
0.56,
0,
{ material: wheelMaterial }
);
const wheelB = addBox(
valve,
0.13,
0.11,
0.64,
0xe76b42,
0,
0.56,
0,
{ material: wheelMaterial }
);
tagInteractive(valveBody, null, plant.id);
tagInteractive(valveStem, null, plant.id);
tagInteractive(wheelA, null, plant.id);
tagInteractive(wheelB, null, plant.id);
valveVisuals.set(plant.id, {
wheelMaterial,
valve,
wheelA,
wheelB
});
const label = createWorldLabel(
plant.id,
plant.name,
`${plant.request} requested · 0 delivered`
);
label.classList.add("selected");
plantVisuals.set(plant.id, {
plot,
plantGroup,
markers,
label,
targetScale: 0.52,
phase: PLANTS.indexOf(plant) * 1.7
});
}
// Selection frame around the active bed.
const selectionFrame = new THREE.Group();
scene.add(selectionFrame);
addBox(selectionFrame, 2.9, 0.07, 0.08, 0xe76b42, 0, 0.35, -1.4);
addBox(selectionFrame, 2.9, 0.07, 0.08, 0xe76b42, 0, 0.35, 1.4);
addBox(selectionFrame, 0.08, 0.07, 2.72, 0xe76b42, -1.4, 0.35, 0);
addBox(selectionFrame, 0.08, 0.07, 2.72, 0xe76b42, 1.4, 0.35, 0);
const reservoirLabel = createWorldLabel(
"reservoir",
"Reservoir",
"12 source tokens",
true
);
// UI construction.
const plantList = document.getElementById("plantList");
for (const plant of PLANTS) {
const row = document.createElement("button");
row.className = "plant-row";
row.dataset.id = plant.id;
row.innerHTML = `
<span class="plant-row-dot" style="background:#${plant.cropColor.toString(16).padStart(6, "0")}"></span>
<span class="plant-row-name">
<strong>${plant.name}</strong>
<small>${plant.request} units requested</small>
</span>
<span class="mini-valve">closed</span>
<span class="mini-count">0/${plant.request}</span>
`;
row.addEventListener("click", () => selectPlant(plant.id));
plantList.appendChild(row);
}
const tokenPips = document.getElementById("tokenPips");
for (let i = 0; i < SOURCE_TOTAL; i++) {
const pip = document.createElement("span");
pip.className = "token-pip";
tokenPips.appendChild(pip);
}
function selectedPlant() {
return PLANTS.find((plant) => plant.id === state.selected);
}
function visibleMarkerCount() {
let count = 0;
for (const visual of plantVisuals.values()) {
count += visual.markers.filter((marker) => marker.visible).length;
}
return count;
}
function updateSelectionFrame() {
const plant = selectedPlant();
selectionFrame.position.set(plant.x, 0, plant.z);
}
function updateUI() {
const plant = selectedPlant();
const delivered = state.delivered[plant.id];
const isOpen = state.open[plant.id];
const isComplete = delivered >= plant.request;
document.getElementById("remainingCount").textContent = state.remaining;
document.getElementById("withdrawnCount").textContent =
SOURCE_TOTAL - state.remaining;
document.getElementById("requestCount").textContent = SOURCE_TOTAL;
[...tokenPips.children].forEach((pip, index) => {
pip.classList.toggle("spent", index >= state.remaining);
});
document.getElementById("selectedName").textContent = plant.name;
document.getElementById("selectedRequest").textContent =
`${plant.request} requested`;
document.getElementById("selectedDescription").textContent =
plant.description;
const swatch = document.getElementById("selectedSwatch");
swatch.textContent = plant.icon;
swatch.style.background = `#${plant.cropColor.toString(16).padStart(6, "0")}`;
document.getElementById("selectedProgressText").textContent =
`${delivered} / ${plant.request}`;
document.getElementById("selectedProgress").style.width =
`${(delivered / plant.request) * 100}%`;
const valveButton = document.getElementById("valveToggle");
valveButton.classList.toggle("open", isOpen);
valveButton.setAttribute("aria-pressed", String(isOpen));
document.getElementById("valveToggleText").textContent = isOpen
? "Close valve"
: "Open valve";
const allocateButton = document.getElementById("allocateButton");
allocateButton.disabled = !isOpen || isComplete || state.remaining <= 0;
if (isComplete) {
allocateButton.textContent = "Request fulfilled";
document.getElementById("actionNote").textContent =
"This bed has received its full request. Closing the valve stops further flow.";
} else if (!isOpen) {
allocateButton.textContent = "Deliver 1 unit";
document.getElementById("actionNote").textContent =
"Open the valve before allocating water to this plant.";
} else if (state.remaining <= 0) {
allocateButton.textContent = "Reservoir empty";
document.getElementById("actionNote").textContent =
"The finite reservoir has no remaining units.";
} else {
allocateButton.textContent = "Deliver 1 unit";
document.getElementById("actionNote").textContent =
"Each unit removes one reservoir token and creates one solid blue delivery marker.";
}
let fulfilled = 0;
for (const item of PLANTS) {
const itemDelivered = state.delivered[item.id];
const itemComplete = itemDelivered >= item.request;
if (itemComplete) fulfilled++;
const row = plantList.querySelector(`[data-id="${item.id}"]`);
row.classList.toggle("selected", item.id === state.selected);
const miniValve = row.querySelector(".mini-valve");
miniValve.textContent = state.open[item.id] ? "open" : "closed";
miniValve.classList.toggle("open", state.open[item.id]);
row.querySelector(".mini-count").textContent =
`${itemDelivered}/${item.request}`;
const visual = plantVisuals.get(item.id);
visual.targetScale = 0.52 + 0.48 * (itemDelivered / item.request);
visual.label.classList.toggle("selected", item.id === state.selected);
visual.label.querySelector("span").textContent =
`${item.request} requested · ${itemDelivered} delivered`;
visual.markers.forEach((marker, index) => {
marker.visible = index < itemDelivered;
if (!marker.visible) marker.scale.setScalar(0);
});
const valveVisual = valveVisuals.get(item.id);
const valveColor = state.open[item.id] ? 0x32865b : 0xe76b42;
valveVisual.wheelMaterial.color.setHex(valveColor);
valveVisual.wheelMaterial.emissive.setHex(
state.open[item.id] ? 0x0b351d : 0x3b1008
);
valveVisual.wheelMaterial.emissiveIntensity = 0.14;
}
reservoirTokens.forEach((token, index) => {
token.visible = index < state.remaining;
});
const withdrawn = SOURCE_TOTAL - state.remaining;
const visible = visibleMarkerCount();
document.getElementById("fulfilledCount").textContent =
`${fulfilled} / ${PLANTS.length} fulfilled`;
document.getElementById("auditEquation").textContent =
`${SOURCE_TOTAL} source − ${state.remaining} remaining = ${withdrawn} withdrawn`;
document.getElementById("auditDetail").textContent =
`${visible} solid blue delivery marker${visible === 1 ? "" : "s"} visible. Withdrawn amount and visible deliveries match exactly.`;
reservoirLabel.querySelector("span").textContent =
`${state.remaining} source token${state.remaining === 1 ? "" : "s"}`;
updateSelectionFrame();
}
function selectPlant(id) {
state.selected = id;
updateUI();
}
function toggleValve(id) {
state.open[id] = !state.open[id];
updateUI();
}
function allocateUnit() {
const id = state.selected;
const plant = PLANTS.find((item) => item.id === id);
if (
!state.open[id] ||
state.remaining <= 0 ||
state.delivered[id] >= plant.request
) {
return;
}
state.remaining -= 1;
state.delivered[id] += 1;
updateUI();
}
document.getElementById("valveToggle").addEventListener("click", () => {
toggleValve(state.selected);
});
document.getElementById("allocateButton").addEventListener("click", allocateUnit);
document.getElementById("resetDemo").addEventListener("click", () => {
state.remaining = SOURCE_TOTAL;
for (const plant of PLANTS) {
state.open[plant.id] = false;
state.delivered[plant.id] = 0;
}
updateUI();
});
// Camera controls implemented directly, without addons.
function updateCamera() {
const radius = 22;
const sinPolar = Math.sin(cameraState.polar);
camera.position.set(
cameraState.target.x + radius * sinPolar * Math.sin(cameraState.azimuth),
cameraState.target.y + radius * Math.cos(cameraState.polar),
cameraState.target.z + radius * sinPolar * Math.cos(cameraState.azimuth)
);
camera.lookAt(cameraState.target);
camera.zoom = cameraState.zoom;
camera.updateProjectionMatrix();
camera.updateMatrixWorld();
}
function resetCamera() {
cameraState.azimuth = 0.63;
cameraState.polar = 0.82;
cameraState.zoom = 1;
cameraState.target.set(-0.35, 0.35, 0);
updateCamera();
}
document.getElementById("zoomIn").addEventListener("click", () => {
cameraState.zoom = Math.min(1.65, cameraState.zoom * 1.12);
updateCamera();
});
document.getElementById("zoomOut").addEventListener("click", () => {
cameraState.zoom = Math.max(0.68, cameraState.zoom / 1.12);
updateCamera();
});
document.getElementById("resetCamera").addEventListener("click", resetCamera);
const raycaster = new THREE.Raycaster();
const pointer = new THREE.Vector2();
let drag = null;
function raycastAt(event) {
const rect = renderer.domElement.getBoundingClientRect();
pointer.x = ((event.clientX - rect.left) / rect.width) * 2 - 1;
pointer.y = -((event.clientY - rect.top) / rect.height) * 2 + 1;
raycaster.setFromCamera(pointer, camera);
return raycaster.intersectObjects(interactiveObjects, false)[0]?.object || null;
}
renderer.domElement.addEventListener("pointerdown", (event) => {
if (event.button !== 0 && event.button !== 2) return;
drag = {
pointerId: event.pointerId,
startX: event.clientX,
startY: event.clientY,
lastX: event.clientX,
lastY: event.clientY,
moved: 0
};
renderer.domElement.setPointerCapture(event.pointerId);
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (!drag) {
const hit = raycastAt(event);
renderer.domElement.style.cursor = hit ? "pointer" : "grab";
return;
}
const dx = event.clientX - drag.lastX;
const dy = event.clientY - drag.lastY;
drag.moved += Math.abs(dx) + Math.abs(dy);
drag.lastX = event.clientX;
drag.lastY = event.clientY;
cameraState.azimuth -= dx * 0.006;
cameraState.polar = THREE.MathUtils.clamp(
cameraState.polar + dy * 0.005,
0.48,
1.22
);
updateCamera();
});
renderer.domElement.addEventListener("pointerup", (event) => {
if (!drag) return;
const wasClick = drag.moved < 7;
drag = null;
if (renderer.domElement.hasPointerCapture(event.pointerId)) {
renderer.domElement.releasePointerCapture(event.pointerId);
}
if (wasClick) {
const hit = raycastAt(event);
if (hit?.userData.valveId) {
const id = hit.userData.valveId;
state.selected = id;
toggleValve(id);
} else if (hit?.userData.plantId) {
selectPlant(hit.userData.plantId);
}
}
});
renderer.domElement.addEventListener("pointercancel", () => {
drag = null;
});
renderer.domElement.addEventListener(
"wheel",
(event) => {
event.preventDefault();
cameraState.zoom = THREE.MathUtils.clamp(
cameraState.zoom * Math.exp(-event.deltaY * 0.001),
0.68,
1.65
);
updateCamera();
},
{ passive: false }
);
renderer.domElement.addEventListener("contextmenu", (event) => {
event.preventDefault();
});
window.addEventListener("keydown", (event) => {
if (event.target instanceof HTMLButtonElement) return;
if (event.key.toLowerCase() === "v") {
toggleValve(state.selected);
} else if (event.key.toLowerCase() === "a") {
allocateUnit();
} else if (event.key === "ArrowLeft") {
cameraState.azimuth -= 0.08;
updateCamera();
} else if (event.key === "ArrowRight") {
cameraState.azimuth += 0.08;
updateCamera();
} else if (event.key === "ArrowUp") {
cameraState.polar = Math.max(0.48, cameraState.polar - 0.05);
updateCamera();
} else if (event.key === "ArrowDown") {
cameraState.polar = Math.min(1.22, cameraState.polar + 0.05);
updateCamera();
}
});
function resize() {
const width = Math.max(1, stage.clientWidth);
const height = Math.max(1, stage.clientHeight);
const aspect = width / height;
const viewHeight = Math.max(14.7, 18 / aspect);
camera.left = (-viewHeight * aspect) / 2;
camera.right = (viewHeight * aspect) / 2;
camera.top = viewHeight / 2;
camera.bottom = -viewHeight / 2;
camera.updateProjectionMatrix();
renderer.setSize(width, height, false);
}
new ResizeObserver(resize).observe(stage);
resize();
updateCamera();
updateUI();
const projected = new THREE.Vector3();
const clock = new THREE.Clock();
function positionWorldLabels() {
const width = stage.clientWidth;
const height = stage.clientHeight;
for (const plant of PLANTS) {
const visual = plantVisuals.get(plant.id);
projected.set(plant.x, 2.12, plant.z).project(camera);
const x = (projected.x * 0.5 + 0.5) * width;
const y = (-projected.y * 0.5 + 0.5) * height;
visual.label.style.left = `${x}px`;
visual.label.style.top = `${y}px`;
visual.label.style.display =
projected.z > -1 && projected.z < 1 ? "block" : "none";
}
projected.set(-5.15, 2.15, 0).project(camera);
reservoirLabel.style.left =
`${(projected.x * 0.5 + 0.5) * width}px`;
reservoirLabel.style.top =
`${(-projected.y * 0.5 + 0.5) * height}px`;
}
function animate() {
const elapsed = clock.getElapsedTime();
for (const plant of PLANTS) {
const visual = plantVisuals.get(plant.id);
const scale = THREE.MathUtils.lerp(
visual.plantGroup.scale.x,
visual.targetScale,
0.09
);
visual.plantGroup.scale.setScalar(scale);
visual.plantGroup.rotation.z =
Math.sin(elapsed * 1.15 + visual.phase) * 0.018;
visual.markers.forEach((marker) => {
if (marker.visible) {
const s = THREE.MathUtils.lerp(marker.scale.x, 1, 0.18);
marker.scale.setScalar(s);
marker.rotation.y = elapsed * 0.45 + visual.phase;
}
});
const valveVisual = valveVisuals.get(plant.id);
if (state.open[plant.id]) {
valveVisual.wheelA.rotation.y = elapsed * 0.7;
valveVisual.wheelB.rotation.y = elapsed * 0.7;
} else {
valveVisual.wheelA.rotation.y = 0;
valveVisual.wheelB.rotation.y = 0;
}
}
reservoirTokens.forEach((token, index) => {
if (token.visible) {
token.position.y =
token.userData.baseY + Math.sin(elapsed * 1.8 + index * 0.4) * 0.015;
}
});
const framePulse = 1 + Math.sin(elapsed * 2.1) * 0.018;
selectionFrame.scale.set(framePulse, 1, framePulse);
positionWorldLabels();
renderer.render(scene, camera);
requestAnimationFrame(animate);
}
animate();
</script>
</body>
</html>
```