Prompt specification
Build or customize the Three.js 3D scene "Rainfall-timing storage puzzle" 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: Rainfall-timing storage puzzle
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/rainfall-timing-storage-puzzle-x1100
Scene Description Prompt:
Make a fictional voxel garden puzzle with scheduled rain, water requests, adjustable tank capacity, and counted storage, use, and overflow. Three.js from cdnjs, one HTML file.
Working Three.js Starter (rainfall-timing-storage-puzzle-x1100):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Cloudpatch — Voxel Rain Garden</title>
<style>
:root {
--ink: #17332f;
--muted: #708b83;
--panel: rgba(247, 252, 246, 0.9);
--panel-strong: rgba(251, 254, 249, 0.97);
--line: rgba(42, 91, 77, 0.14);
--green: #2f8f68;
--green-dark: #23684f;
--mint: #9fe4c4;
--blue: #3a9fd1;
--blue-dark: #237da8;
--yellow: #e5ad4f;
--red: #d76255;
--shadow: 0 18px 45px rgba(29, 64, 55, 0.13);
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #cfe3dc;
color: var(--ink);
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
button,
input {
font: inherit;
}
button {
border: 0;
}
#scene {
position: fixed;
inset: 0;
width: 100%;
height: 100%;
display: block;
touch-action: none;
cursor: grab;
outline: none;
}
.topbar {
position: fixed;
z-index: 5;
top: 18px;
left: 20px;
right: 20px;
display: flex;
align-items: center;
justify-content: space-between;
gap: 14px;
pointer-events: none;
}
.brand,
.top-controls {
pointer-events: auto;
}
.brand {
display: flex;
align-items: center;
gap: 11px;
padding: 10px 14px 10px 10px;
border: 1px solid rgba(255, 255, 255, 0.58);
border-radius: 15px;
background: rgba(247, 252, 246, 0.79);
box-shadow: var(--shadow);
backdrop-filter: blur(14px);
}
.brand-mark {
width: 34px;
height: 34px;
display: grid;
place-items: center;
border-radius: 9px;
background: var(--green);
box-shadow: inset 0 -4px 0 rgba(0, 0, 0, 0.12);
}
.brand-mark::before {
content: "";
width: 13px;
height: 15px;
background: white;
clip-path: polygon(50% 0, 100% 52%, 78% 52%, 78% 100%, 22% 100%, 22% 52%, 0 52%);
}
.brand-copy strong {
display: block;
font-size: 13px;
line-height: 1.05;
letter-spacing: 0.1em;
}
.brand-copy span {
display: block;
margin-top: 4px;
color: var(--muted);
font-size: 10px;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.top-controls {
display: flex;
align-items: center;
gap: 7px;
padding: 7px;
border: 1px solid rgba(255, 255, 255, 0.58);
border-radius: 14px;
background: rgba(247, 252, 246, 0.85);
box-shadow: var(--shadow);
backdrop-filter: blur(14px);
}
.status-pill {
display: flex;
align-items: center;
gap: 7px;
min-width: 82px;
padding: 0 8px;
color: var(--muted);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.11em;
text-transform: uppercase;
}
.status-dot {
width: 7px;
height: 7px;
border-radius: 50%;
background: #a8b8b2;
box-shadow: 0 0 0 4px rgba(168, 184, 178, 0.15);
}
.status-dot.live {
background: #45aa76;
box-shadow: 0 0 0 4px rgba(69, 170, 118, 0.15);
animation: pulse 1.6s infinite;
}
@keyframes pulse {
50% {
box-shadow: 0 0 0 7px rgba(69, 170, 118, 0.03);
}
}
.small-button,
.speed-button {
height: 32px;
padding: 0 11px;
border-radius: 9px;
color: var(--ink);
background: transparent;
cursor: pointer;
font-size: 11px;
font-weight: 800;
transition: 0.16s ease;
}
.small-button:hover,
.speed-button:hover {
background: rgba(47, 143, 104, 0.1);
}
.small-button:disabled {
opacity: 0.4;
cursor: default;
}
.speed-group {
display: flex;
padding: 3px;
border-radius: 10px;
background: rgba(36, 104, 79, 0.07);
}
.speed-button {
min-width: 31px;
height: 27px;
padding: 0 7px;
}
.speed-button.active {
color: white;
background: var(--green);
box-shadow: 0 3px 8px rgba(47, 143, 104, 0.23);
}
.hud {
position: fixed;
z-index: 4;
top: 82px;
right: 20px;
bottom: 103px;
left: 20px;
display: flex;
align-items: flex-start;
justify-content: space-between;
gap: 16px;
pointer-events: none;
}
.panel {
width: 300px;
max-height: 100%;
overflow: auto;
padding: 17px;
border: 1px solid rgba(255, 255, 255, 0.65);
border-radius: 19px;
background: var(--panel);
box-shadow: var(--shadow);
backdrop-filter: blur(17px);
pointer-events: auto;
scrollbar-width: thin;
scrollbar-color: rgba(47, 143, 104, 0.25) transparent;
}
.panel-title {
display: flex;
align-items: center;
justify-content: space-between;
gap: 10px;
margin-bottom: 14px;
}
.panel-title h2 {
margin: 0;
font-size: 12px;
letter-spacing: 0.11em;
text-transform: uppercase;
}
.eyebrow {
color: var(--green);
font-size: 9px;
font-weight: 900;
letter-spacing: 0.13em;
text-transform: uppercase;
}
.metric-grid {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 7px;
}
.metric {
min-width: 0;
padding: 10px 8px 9px;
border: 1px solid var(--line);
border-radius: 12px;
background: rgba(255, 255, 255, 0.58);
}
.metric-label {
display: block;
overflow: hidden;
color: var(--muted);
font-size: 8px;
font-weight: 850;
letter-spacing: 0.08em;
text-overflow: ellipsis;
text-transform: uppercase;
white-space: nowrap;
}
.metric-value {
display: block;
margin-top: 5px;
font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
font-size: 19px;
font-weight: 800;
letter-spacing: -0.05em;
}
.metric-value small {
margin-left: 2px;
color: var(--muted);
font-size: 9px;
letter-spacing: 0;
}
.metric.storage .metric-value {
color: var(--blue-dark);
}
.metric.used .metric-value {
color: var(--green-dark);
}
.metric.overflow .metric-value {
color: var(--red);
}
.meter {
height: 8px;
margin: 13px 0 7px;
overflow: hidden;
border-radius: 99px;
background: rgba(36, 104, 79, 0.09);
}
.meter > div {
width: 0;
height: 100%;
border-radius: inherit;
background: linear-gradient(90deg, var(--blue), #64c4df);
transition: width 0.25s ease;
}
.meter-caption {
display: flex;
justify-content: space-between;
color: var(--muted);
font-size: 9px;
}
.divider {
height: 1px;
margin: 16px 0;
background: var(--line);
}
.control-heading {
display: flex;
align-items: baseline;
justify-content: space-between;
margin-bottom: 9px;
}
.control-heading label {
font-size: 11px;
font-weight: 800;
}
.capacity-value {
color: var(--green-dark);
font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
font-size: 13px;
font-weight: 800;
}
input[type="range"] {
width: 100%;
height: 20px;
margin: 0;
accent-color: var(--green);
cursor: pointer;
}
.range-labels {
display: flex;
justify-content: space-between;
margin-top: 3px;
color: var(--muted);
font-size: 9px;
}
.hint {
margin: 9px 0 0;
color: var(--muted);
font-size: 10px;
line-height: 1.45;
}
.hint strong {
color: var(--ink);
}
.request-list {
display: grid;
gap: 8px;
}
.request-card {
padding: 10px;
border: 1px solid var(--line);
border-radius: 12px;
background: rgba(255, 255, 255, 0.72);
animation: card-in 0.25s ease;
}
@keyframes card-in {
from {
opacity: 0;
transform: translateY(5px);
}
}
.request-top {
display: flex;
align-items: center;
justify-content: space-between;
gap: 8px;
}
.request-name {
font-size: 11px;
font-weight: 850;
}
.request-amount {
color: var(--blue-dark);
font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
font-size: 10px;
font-weight: 800;
}
.request-bottom {
display: flex;
align-items: center;
justify-content: space-between;
gap: 8px;
margin-top: 8px;
}
.deadline {
color: var(--muted);
font-size: 9px;
}
.serve-button {
padding: 6px 10px;
border-radius: 8px;
color: white;
background: var(--green);
box-shadow: 0 3px 8px rgba(47, 143, 104, 0.2);
cursor: pointer;
font-size: 9px;
font-weight: 850;
transition: 0.15s ease;
}
.serve-button:hover {
background: var(--green-dark);
transform: translateY(-1px);
}
.serve-button.low {
color: var(--red);
background: rgba(215, 98, 85, 0.11);
box-shadow: none;
}
.empty-state {
padding: 15px 12px;
border: 1px dashed rgba(47, 143, 104, 0.2);
border-radius: 12px;
color: var(--muted);
text-align: center;
font-size: 10px;
line-height: 1.5;
}
.next-request {
margin-top: 10px;
color: var(--muted);
font-size: 9px;
line-height: 1.4;
}
.served-summary {
display: flex;
gap: 5px;
flex-wrap: wrap;
margin-top: 10px;
}
.served-chip {
padding: 4px 7px;
border-radius: 7px;
color: var(--green-dark);
background: rgba(47, 143, 104, 0.1);
font-size: 9px;
font-weight: 800;
}
.forecast {
display: flex;
flex-wrap: wrap;
gap: 6px;
}
.rain-chip {
flex: 1 1 76px;
padding: 8px;
border: 1px solid rgba(58, 159, 209, 0.13);
border-radius: 10px;
background: rgba(58, 159, 209, 0.07);
}
.rain-chip.active {
border-color: rgba(58, 159, 209, 0.35);
background: rgba(58, 159, 209, 0.16);
}
.rain-chip.done {
opacity: 0.58;
}
.rain-chip strong {
display: block;
color: var(--blue-dark);
font-size: 10px;
}
.rain-chip span {
display: block;
margin-top: 3px;
color: var(--muted);
font-size: 8px;
}
.timeline-panel {
position: fixed;
z-index: 5;
right: 20px;
bottom: 18px;
left: 20px;
padding: 12px 16px 13px;
border: 1px solid rgba(255, 255, 255, 0.64);
border-radius: 17px;
background: rgba(247, 252, 246, 0.87);
box-shadow: var(--shadow);
backdrop-filter: blur(16px);
pointer-events: auto;
}
.timeline-heading {
display: flex;
align-items: center;
justify-content: space-between;
gap: 12px;
margin-bottom: 10px;
}
.timeline-heading strong {
font-size: 10px;
letter-spacing: 0.1em;
text-transform: uppercase;
}
.timeline-heading span {
color: var(--muted);
font-size: 9px;
}
.timeline-track {
position: relative;
height: 12px;
border-radius: 99px;
background: rgba(36, 104, 79, 0.1);
}
.timeline-fill {
position: absolute;
inset: 0 auto 0 0;
width: 0;
border-radius: inherit;
background: linear-gradient(90deg, #68c69a, #3a9fd1);
}
.timeline-marker {
position: absolute;
top: 50%;
width: 14px;
height: 14px;
display: grid;
place-items: center;
border: 2px solid white;
border-radius: 5px;
color: white;
background: #9aaea7;
box-shadow: 0 2px 5px rgba(23, 51, 47, 0.16);
font-size: 7px;
font-weight: 900;
transform: translate(-50%, -50%) rotate(45deg);
}
.timeline-marker > span {
transform: rotate(-45deg);
}
.timeline-marker.rain {
background: var(--blue);
}
.timeline-marker.request {
background: var(--yellow);
}
.timeline-marker.active {
box-shadow: 0 0 0 4px rgba(58, 159, 209, 0.15);
}
.timeline-marker.done {
opacity: 0.43;
}
.legend {
display: flex;
gap: 14px;
margin-top: 8px;
color: var(--muted);
font-size: 8px;
}
.legend span::before {
content: "";
display: inline-block;
width: 7px;
height: 7px;
margin-right: 5px;
border-radius: 2px;
background: var(--blue);
}
.legend span:last-child::before {
background: var(--yellow);
}
.toast {
position: fixed;
z-index: 30;
top: 82px;
left: 50%;
max-width: min(90vw, 390px);
padding: 11px 15px;
border: 1px solid rgba(255, 255, 255, 0.6);
border-radius: 12px;
color: white;
background: rgba(23, 51, 47, 0.93);
box-shadow: var(--shadow);
font-size: 11px;
font-weight: 700;
text-align: center;
opacity: 0;
pointer-events: none;
transform: translate(-50%, -12px);
transition: 0.22s ease;
}
.toast.show {
opacity: 1;
transform: translate(-50%, 0);
}
.toast.warning {
background: rgba(167, 82, 58, 0.95);
}
.overlay {
position: fixed;
z-index: 20;
inset: 0;
display: grid;
place-items: center;
padding: 20px;
background: rgba(29, 61, 54, 0.18);
backdrop-filter: blur(5px);
}
.overlay.hidden {
display: none;
}
.modal-card {
width: min(520px, 100%);
max-height: 94vh;
overflow: auto;
padding: 27px;
border: 1px solid rgba(255, 255, 255, 0.78);
border-radius: 23px;
background: var(--panel-strong);
box-shadow: 0 28px 90px rgba(23, 51, 47, 0.25);
}
.modal-card h1 {
margin: 8px 0 10px;
font-size: clamp(31px, 6vw, 49px);
line-height: 0.98;
letter-spacing: -0.055em;
}
.modal-card > p {
margin: 0;
color: var(--muted);
font-size: 12px;
line-height: 1.6;
}
.rules {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 8px;
margin: 20px 0;
}
.rule {
padding: 11px;
border: 1px solid var(--line);
border-radius: 12px;
background: rgba(255, 255, 255, 0.67);
}
.rule strong {
display: block;
margin-bottom: 4px;
font-size: 17px;
}
.rule span {
color: var(--muted);
font-size: 9px;
line-height: 1.35;
}
.primary-button {
width: 100%;
margin-top: 18px;
padding: 13px 18px;
border-radius: 12px;
color: white;
background: var(--green);
box-shadow: 0 6px 15px rgba(47, 143, 104, 0.25);
cursor: pointer;
font-size: 12px;
font-weight: 900;
letter-spacing: 0.04em;
transition: 0.16s ease;
}
.primary-button:hover {
background: var(--green-dark);
transform: translateY(-1px);
}
.secondary-button {
width: 100%;
margin-top: 8px;
padding: 11px 18px;
border: 1px solid var(--line);
border-radius: 12px;
color: var(--ink);
background: transparent;
cursor: pointer;
font-size: 11px;
font-weight: 800;
}
.controls-note {
margin-top: 13px;
color: var(--muted);
font-size: 9px;
text-align: center;
}
.end-grid {
display: grid;
grid-template-columns: repeat(4, 1fr);
gap: 7px;
margin: 20px 0;
}
.end-stat {
padding: 11px 7px;
border: 1px solid var(--line);
border-radius: 11px;
text-align: center;
}
.end-stat strong {
display: block;
font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
font-size: 20px;
}
.end-stat span {
display: block;
margin-top: 4px;
color: var(--muted);
font-size: 8px;
text-transform: uppercase;
}
@media (max-width: 820px) {
.panel {
width: 270px;
}
.brand-copy span {
display: none;
}
.status-pill {
display: none;
}
}
@media (max-width: 650px) {
.topbar {
top: 9px;
right: 9px;
left: 9px;
}
.brand {
padding: 7px;
}
.brand-copy {
display: none;
}
.top-controls {
padding: 4px;
}
.small-button {
padding: 0 8px;
font-size: 9px;
}
.hud {
top: 59px;
right: 9px;
bottom: 105px;
left: 9px;
flex-direction: column;
gap: 8px;
}
.panel {
width: 100%;
max-height: 42%;
padding: 12px;
border-radius: 15px;
}
.left-panel {
order: 2;
}
.right-panel {
order: 1;
}
.metric {
padding: 7px;
}
.metric-value {
font-size: 15px;
}
.timeline-panel {
right: 9px;
bottom: 9px;
left: 9px;
padding: 10px 12px;
}
.timeline-heading span {
display: none;
}
.rules {
grid-template-columns: 1fr;
}
.rule {
display: flex;
align-items: center;
gap: 10px;
}
.rule strong {
min-width: 42px;
}
.end-grid {
grid-template-columns: repeat(2, 1fr);
}
}
</style>
</head>
<body>
<canvas id="scene" aria-label="Interactive voxel rain garden"></canvas>
<header class="topbar">
<div class="brand">
<div class="brand-mark" aria-hidden="true"></div>
<div class="brand-copy">
<strong>CLOUDPATCH</strong>
<span>Voxel hydro garden</span>
</div>
</div>
<div class="top-controls">
<div class="status-pill">
<span id="statusDot" class="status-dot"></span>
<span id="statusText">Ready</span>
</div>
<button id="pauseBtn" class="small-button" disabled>Pause</button>
<div class="speed-group" aria-label="Simulation speed">
<button class="speed-button active" data-speed="1">1×</button>
<button class="speed-button" data-speed="2">2×</button>
<button class="speed-button" data-speed="4">4×</button>
</div>
<button id="resetBtn" class="small-button">Restart</button>
</div>
</header>
<main class="hud">
<section class="panel left-panel">
<div class="panel-title">
<h2>Reservoir</h2>
<span class="eyebrow">Live meter</span>
</div>
<div class="metric-grid">
<div class="metric storage">
<span class="metric-label">Storage</span>
<span class="metric-value"><span id="storageMetric">45</span><small>L</small></span>
</div>
<div class="metric used">
<span class="metric-label">Water use</span>
<span class="metric-value"><span id="usedMetric">0</span><small>L</small></span>
</div>
<div class="metric overflow">
<span class="metric-label">Overflow</span>
<span class="metric-value"><span id="overflowMetric">0</span><small>L</small></span>
</div>
</div>
<div class="meter"><div id="storageFill"></div></div>
<div class="meter-caption">
<span id="storageCaption">45 of 80 L stored</span>
<span id="fillPercent">56%</span>
</div>
<div class="divider"></div>
<div class="control-heading">
<label for="capacityRange">Tank capacity</label>
<span id="capacityReadout" class="capacity-value">80 L</span>
</div>
<input id="capacityRange" data-capacity type="range" min="50" max="160" step="5" value="80" />
<div class="range-labels">
<span>50 L</span>
<span>Adjustable during the day</span>
<span>160 L</span>
</div>
<p class="hint">
Lowering capacity immediately spills any water above the new limit.
Extra rain is counted as <strong>overflow</strong>.
</p>
</section>
<section class="panel right-panel">
<div class="panel-title">
<h2>Water requests</h2>
<span id="requestCount" class="eyebrow">0 active</span>
</div>
<div id="requestList" class="request-list"></div>
<div id="nextRequest" class="next-request"></div>
<div id="servedSummary" class="served-summary"></div>
<div class="divider"></div>
<div class="panel-title">
<h2>Rain forecast</h2>
<span class="eyebrow">5 storms</span>
</div>
<div id="forecast" class="forecast"></div>
<p class="hint">
Click a highlighted bed or press <strong>Serve</strong> before its request expires.
Drag the scene to orbit; scroll to zoom.
</p>
</section>
</main>
<section class="timeline-panel" aria-label="Garden timeline">
<div class="timeline-heading">
<strong>Day timeline</strong>
<span id="timeReadout">00:00 / 01:12</span>
</div>
<div class="timeline-track">
<div id="timelineFill" class="timeline-fill"></div>
<div id="timelineMarkers"></div>
</div>
<div class="legend">
<span>Rain storm</span>
<span>Water request</span>
</div>
</section>
<div id="toast" class="toast" role="status" aria-live="polite"></div>
<section id="introOverlay" class="overlay">
<div class="modal-card">
<span class="eyebrow">One-day resource puzzle</span>
<h1>Rain Garden</h1>
<p>
Five scheduled storms will fill your tank, but seven garden plots will ask
for water. Adjust capacity before the day begins, then decide which roots
to save when the reservoir runs low.
</p>
<div class="rules">
<div class="rule">
<strong>45 L</strong>
<span>Starting water</span>
</div>
<div class="rule">
<strong>5</strong>
<span>Scheduled storms</span>
</div>
<div class="rule">
<strong>7</strong>
<span>Garden requests</span>
</div>
</div>
<div class="control-heading">
<label for="introCapacity">Choose tank capacity</label>
<span id="introCapacityValue" class="capacity-value">80 L</span>
</div>
<input id="introCapacity" data-capacity type="range" min="50" max="160" step="5" value="80" />
<div class="range-labels">
<span>50 L</span>
<span>Goal: serve 6 requests with 0 L overflow</span>
<span>160 L</span>
</div>
<button id="startBtn" class="primary-button">Start the garden day</button>
<div class="controls-note">Space pauses · Arrow keys orbit · Scroll zooms</div>
</div>
</section>
<section id="endOverlay" class="overlay hidden">
<div class="modal-card">
<span id="endEyebrow" class="eyebrow">Day complete</span>
<h1 id="endTitle">Good Steward</h1>
<p id="endMessage"></p>
<div class="end-grid">
<div class="end-stat">
<strong id="endServed">0</strong>
<span>Served</span>
</div>
<div class="end-stat">
<strong id="endMissed">0</strong>
<span>Missed</span>
</div>
<div class="end-stat">
<strong id="endUsed">0</strong>
<span>Liters used</span>
</div>
<div class="end-stat">
<strong id="endOverflow">0</strong>
<span>Overflow</span>
</div>
</div>
<button id="againBtn" class="primary-button">Run the day again</button>
<button id="editBtn" class="secondary-button">Adjust capacity and restart</button>
</div>
</section>
<noscript>This puzzle requires JavaScript and WebGL.</noscript>
<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 DAY_LENGTH = 72;
const STARTING_WATER = 45;
const DEFAULT_CAPACITY = 80;
const rainSchedule = [
{ at: 8, amount: 28 },
{ at: 18, amount: 35 },
{ at: 29, amount: 42 },
{ at: 43, amount: 30 },
{ at: 57, amount: 24 }
];
const requestSchedule = [
{ at: 5, deadline: 21, plot: 0, amount: 25 },
{ at: 13, deadline: 30, plot: 1, amount: 30 },
{ at: 22, deadline: 41, plot: 2, amount: 35 },
{ at: 31, deadline: 50, plot: 0, amount: 25 },
{ at: 39, deadline: 60, plot: 3, amount: 40 },
{ at: 50, deadline: 68, plot: 1, amount: 30 },
{ at: 58, deadline: 72, plot: 4, amount: 25 }
];
const plotNames = [
"Clover Bed",
"Sunflower Bed",
"Mint Bed",
"Tomato Bed",
"Berry Bed",
"Herbs Bed"
];
const sim = {
started: false,
running: false,
ended: false,
time: 0,
speed: 1,
capacity: DEFAULT_CAPACITY,
storage: STARTING_WATER,
used: 0,
overflow: 0,
served: 0,
missed: 0,
requests: [],
nextRequest: 0,
nextRain: 0,
rainUntil: 0
};
const canvas = $("scene");
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xcfe3dc);
scene.fog = new THREE.Fog(0xcfe3dc, 28, 60);
const camera = new THREE.OrthographicCamera(-10, 10, 8, -8, 0.1, 100);
const renderer = new THREE.WebGLRenderer({
canvas,
antialias: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.08;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
const hemisphere = new THREE.HemisphereLight(0xf3fff9, 0x71937c, 2.1);
scene.add(hemisphere);
const sun = new THREE.DirectionalLight(0xfff1ce, 3.1);
sun.position.set(-9, 18, 11);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -16;
sun.shadow.camera.right = 16;
sun.shadow.camera.top = 16;
sun.shadow.camera.bottom = -16;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 45;
sun.shadow.normalBias = 0.035;
scene.add(sun);
const cubeGeometry = new THREE.BoxGeometry(1, 1, 1);
const plots = [];
const clickables = [];
const clouds = [];
function makeBox(width, height, depth, color, x, y, z, parent = scene, options = {}) {
const material = new THREE.MeshStandardMaterial({
color,
roughness: 0.82,
metalness: 0,
...options
});
const mesh = new THREE.Mesh(cubeGeometry, material);
mesh.scale.set(width, height, depth);
mesh.position.set(x, y, z);
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
// Voxel terrain.
const tileSize = 1.15;
const columns = 22;
const rows = 16;
const groundMaterial = new THREE.MeshStandardMaterial({
color: 0xffffff,
roughness: 1
});
const ground = new THREE.InstancedMesh(
cubeGeometry,
groundMaterial,
columns * rows
);
ground.receiveShadow = true;
const dummy = new THREE.Object3D();
const tileColor = new THREE.Color();
let tileIndex = 0;
for (let row = 0; row < rows; row++) {
for (let column = 0; column < columns; column++) {
const x = (column - (columns - 1) / 2) * tileSize;
const z = (row - (rows - 1) / 2) * tileSize;
dummy.position.set(x, -0.22, z);
dummy.scale.set(1.1, 0.38, 1.1);
dummy.updateMatrix();
ground.setMatrixAt(tileIndex, dummy.matrix);
const path = Math.abs(x) < 0.65 || Math.abs(z + 0.15) < 0.56;
if (path) {
tileColor.set(0xd7c9a8);
} else {
const variation = ((column * 17 + row * 11) % 7) * 0.006;
tileColor.setHSL(0.285 + variation, 0.28, 0.68 + variation);
}
ground.setColorAt(tileIndex, tileColor);
tileIndex++;
}
}
ground.instanceMatrix.needsUpdate = true;
ground.instanceColor.needsUpdate = true;
scene.add(ground);
makeBox(27, 0.65, 20, 0x789b79, 0, -0.7, 0);
// Low voxel fence around the garden.
const fenceMaterialColor = 0xb79668;
for (let x = -10; x <= 10; x += 2.5) {
makeBox(0.24, 1.25, 0.24, fenceMaterialColor, x, 0.58, -8.7);
makeBox(0.24, 1.25, 0.24, fenceMaterialColor, x, 0.58, 8.7);
}
for (let z = -7.5; z <= 7.5; z += 2.5) {
makeBox(0.24, 1.25, 0.24, fenceMaterialColor, -11.2, 0.58, z);
makeBox(0.24, 1.25, 0.24, fenceMaterialColor, 11.2, 0.58, z);
}
makeBox(22.3, 0.15, 0.16, fenceMaterialColor, 0, 0.76, -8.7);
makeBox(22.3, 0.15, 0.16, fenceMaterialColor, 0, 0.76, 8.7);
makeBox(0.16, 0.15, 17.5, fenceMaterialColor, -11.2, 0.76, 0);
makeBox(0.16, 0.15, 17.5, fenceMaterialColor, 11.2, 0.76, 0);
// Garden beds.
const plotPositions = [
[-4.7, -2.45],
[-1.6, -2.45],
[1.6, -2.45],
[-4.7, 1.5],
[-1.6, 1.5],
[1.6, 1.5]
];
const dryLeafColor = new THREE.Color(0xb08b4d);
const thirstyLeafColor = new THREE.Color(0xd5a74f);
const healthyLeafColor = new THREE.Color(0x4b9f5f);
const temporaryColor = new THREE.Color();
function createPlot(position, index) {
const group = new THREE.Group();
group.position.set(position[0], 0, position[1]);
scene.add(group);
const soil = makeBox(2.15, 0.28, 1.9, 0x6e4935, 0, 0.1, 0, group);
const borderColor = index % 2 === 0 ? 0xb98554 : 0xc29261;
makeBox(2.5, 0.34, 0.18, borderColor, 0, 0.16, -1.02, group);
makeBox(2.5, 0.34, 0.18, borderColor, 0, 0.16, 1.02, group);
makeBox(0.18, 0.34, 1.85, borderColor, -1.16, 0.16, 0, group);
makeBox(0.18, 0.34, 1.85, borderColor, 1.16, 0.16, 0, group);
const sprouts = [];
for (let zIndex = 0; zIndex < 3; zIndex++) {
for (let xIndex = 0; xIndex < 3; xIndex++) {
const sprout = new THREE.Group();
sprout.position.set(
(xIndex - 1) * 0.64,
0.25,
(zIndex - 1) * 0.56
);
group.add(sprout);
const stemMaterial = new THREE.MeshStandardMaterial({
color: 0x3e7f45,
roughness: 0.9
});
const leafMaterial = new THREE.MeshStandardMaterial({
color: 0x4b9f5f,
roughness: 0.88
});
const stem = new THREE.Mesh(cubeGeometry, stemMaterial);
stem.scale.set(0.12, 0.38, 0.12);
stem.position.y = 0.19;
stem.castShadow = true;
sprout.add(stem);
const leafA = new THREE.Mesh(cubeGeometry, leafMaterial);
leafA.scale.set(0.36, 0.12, 0.19);
leafA.position.set(-0.12, 0.37, 0);
leafA.castShadow = true;
sprout.add(leafA);
const leafB = new THREE.Mesh(cubeGeometry, leafMaterial);
leafB.scale.set(0.36, 0.12, 0.19);
leafB.position.set(0.12, 0.49, 0);
leafB.castShadow = true;
sprout.add(leafB);
sprouts.push({ group: sprout, leafMaterial });
}
}
const hoverMaterial = new THREE.MeshBasicMaterial({
color: 0x72e4b0,
transparent: true,
opacity: 0,
depthWrite: false
});
const hover = new THREE.Mesh(cubeGeometry, hoverMaterial);
hover.scale.set(2.75, 0.04, 2.35);
hover.position.y = 0.025;
group.add(hover);
const marker = new THREE.Group();
marker.position.set(0, 2.2, 0);
marker.visible = false;
group.add(marker);
makeBox(0.28, 0.4, 0.28, 0x46b6e8, 0, 0, 0, marker, {
emissive: 0x1e769c,
emissiveIntensity: 0.35
});
makeBox(0.12, 0.12, 0.12, 0xd7f6ff, 0, 0.32, 0, marker);
const plot = {
group,
sprouts,
marker,
hover,
growth: 0.72,
targetGrowth: 0.72,
thirsty: false,
hovered: false
};
group.traverse((object) => {
if (object.isMesh) {
object.userData.plotIndex = index;
clickables.push(object);
}
});
plots.push(plot);
return plot;
}
plotPositions.forEach(createPlot);
// Voxel water tank.
const tank = new THREE.Group();
tank.position.set(6.35, 0, 1.1);
scene.add(tank);
makeBox(3.25, 0.42, 3.25, 0x718f83, 0, 0.12, 0, tank);
makeBox(3.02, 0.2, 3.02, 0x9db4a9, 0, 0.42, 0, tank);
const tankPostColor = 0x496b65;
for (const x of [-1.38, 1.38]) {
for (const z of [-1.38, 1.38]) {
makeBox(0.18, 3.35, 0.18, tankPostColor, x, 2.05, z, tank);
}
}
makeBox(2.95, 0.22, 2.95, 0x5d7d75, 0, 3.73, 0, tank);
makeBox(2.55, 0.16, 2.55, 0x89a79c, 0, 3.9, 0, tank);
const glass = makeBox(2.55, 3.05, 2.55, 0xa8e2ec, 0, 2.1, 0, tank, {
transparent: true,
opacity: 0.075,
depthWrite: false,
roughness: 0.15
});
glass.castShadow = false;
const waterLevels = [];
for (let level = 0; level < 5; level++) {
const group = new THREE.Group();
tank.add(group);
const material = new THREE.MeshStandardMaterial({
color: 0x43add3,
transparent: true,
opacity: 0.78,
roughness: 0.25,
metalness: 0.02
});
for (let x = 0; x < 3; x++) {
for (let z = 0; z < 3; z++) {
const water = new THREE.Mesh(cubeGeometry, material);
water.scale.set(0.57, 0.5, 0.57);
water.position.set((x - 1) * 0.63, 0.75 + level * 0.53, (z - 1) * 0.63);
water.castShadow = true;
group.add(water);
}
}
group.visible = false;
waterLevels.push({ group, material });
}
// Pipe/channel leading from the tank to the beds.
for (let i = 0; i < 7; i++) {
makeBox(
0.58,
0.18,
0.55,
i % 2 ? 0x9b754f : 0xaf855d,
4.85 - i * 0.53,
0.08,
1.1
);
}
// Voxel trees.
function createTree(x, z, scale = 1) {
const group = new THREE.Group();
group.position.set(x, 0, z);
group.scale.setScalar(scale);
scene.add(group);
makeBox(0.55, 2.2, 0.55, 0x8c6546, 0, 1.1, 0, group);
makeBox(2.3, 1.15, 1.9, 0x4f915c, 0, 2.75, 0, group);
makeBox(1.65, 1.2, 2.3, 0x5ca168, -0.2, 3.45, 0.08, group);
makeBox(1.25, 0.8, 1.3, 0x6bb276, 0.55, 4.05, -0.1, group);
makeBox(0.35, 0.35, 0.35, 0xe9c75b, -0.55, 3.2, 0.98, group);
}
createTree(-8.7, -5.7, 0.95);
createTree(8.9, -5.6, 1.05);
createTree(-8.7, 5.6, 1.08);
createTree(9.1, 5.6, 0.88);
// Small voxel utility shed.
const shed = new THREE.Group();
shed.position.set(7.5, 0, -4.4);
scene.add(shed);
makeBox(3.2, 2.4, 2.5, 0xd6b77b, 0, 1.2, 0, shed);
makeBox(3.55, 0.28, 2.85, 0x4f7770, 0, 2.52, 0, shed);
makeBox(0.95, 1.55, 0.12, 0x53766e, -0.45, 0.78, 1.31, shed);
makeBox(0.7, 0.7, 0.1, 0x9fd3cf, 0.78, 1.4, 1.31, shed);
// Voxel clouds.
function createCloud(x, y, z, scale) {
const group = new THREE.Group();
group.position.set(x, y, z);
group.scale.setScalar(scale);
scene.add(group);
makeBox(2.1, 0.65, 1.0, 0xf4fbf7, 0, 0, 0, group);
makeBox(1.3, 0.85, 1.2, 0xffffff, -0.55, 0.25, 0, group);
makeBox(1.35, 0.8, 1.1, 0xeaf5f0, 0.75, 0.18, 0, group);
makeBox(0.9, 0.55, 0.8, 0xffffff, 0.1, 0.48, 0, group);
clouds.push({ group, originX: x, speed: 0.08 + Math.random() * 0.08 });
}
createCloud(-7, 10.8, -8, 1.25);
createCloud(5, 12.8, -12, 1.6);
createCloud(11, 9.7, 2, 1.0);
// Instanced voxel rain.
const rainCount = 120;
const rainMaterial = new THREE.MeshBasicMaterial({
color: 0x5fc2e8,
transparent: true,
opacity: 0.72,
depthWrite: false
});
const rainMesh = new THREE.InstancedMesh(cubeGeometry, rainMaterial, rainCount);
rainMesh.visible = false;
rainMesh.frustumCulled = false;
scene.add(rainMesh);
const rainData = Array.from({ length: rainCount }, () => ({
x: -10 + Math.random() * 20,
y: Math.random() * 15,
z: -8 + Math.random() * 16,
speed: 7 + Math.random() * 5
}));
// Custom camera controls, implemented without addons.
const cameraState = {
azimuth: 0.72,
elevation: 0.72,
zoom: 1,
target: new THREE.Vector3(0, 0.7, 0)
};
function updateCamera() {
const radius = 24;
const horizontal = radius * Math.cos(cameraState.elevation);
camera.position.set(
cameraState.target.x + Math.sin(cameraState.azimuth) * horizontal,
cameraState.target.y + Math.sin(cameraState.elevation) * radius,
cameraState.target.z + Math.cos(cameraState.azimuth) * horizontal
);
camera.lookAt(cameraState.target);
camera.zoom = cameraState.zoom;
camera.updateProjectionMatrix();
}
function resize() {
const width = window.innerWidth;
const height = window.innerHeight;
const aspect = width / height;
const viewHeight = aspect < 0.8 ? 9 / aspect : 8.5;
camera.left = (-viewHeight * aspect) / 2;
camera.right = (viewHeight * aspect) / 2;
camera.top = viewHeight / 2;
camera.bottom = -viewHeight / 2;
camera.updateProjectionMatrix();
renderer.setSize(width, height);
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
}
window.addEventListener("resize", resize);
resize();
updateCamera();
const raycaster = new THREE.Raycaster();
const pointer = new THREE.Vector2();
const drag = {
active: false,
moved: false,
startX: 0,
startY: 0,
lastX: 0,
lastY: 0
};
function raycastPlot(event) {
const rect = canvas.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);
const hits = raycaster.intersectObjects(clickables, false);
return hits.length ? hits[0].object.userData.plotIndex : -1;
}
canvas.addEventListener("pointerdown", (event) => {
if (event.button !== 0) return;
drag.active = true;
drag.moved = false;
drag.startX = drag.lastX = event.clientX;
drag.startY = drag.lastY = event.clientY;
canvas.setPointerCapture(event.pointerId);
canvas.style.cursor = "grabbing";
});
canvas.addEventListener("pointermove", (event) => {
if (drag.active) {
const dx = event.clientX - drag.lastX;
const dy = event.clientY - drag.lastY;
drag.lastX = event.clientX;
drag.lastY = event.clientY;
if (
Math.abs(event.clientX - drag.startX) > 4 ||
Math.abs(event.clientY - drag.startY) > 4
) {
drag.moved = true;
}
if (drag.moved) {
cameraState.azimuth -= dx * 0.008;
cameraState.elevation = THREE.MathUtils.clamp(
cameraState.elevation + dy * 0.006,
0.32,
1.22
);
updateCamera();
}
return;
}
const plotIndex = raycastPlot(event);
plots.forEach((plot, index) => {
plot.hovered = index === plotIndex;
});
canvas.style.cursor = plotIndex >= 0 ? "pointer" : "grab";
});
canvas.addEventListener("pointerup", (event) => {
drag.active = false;
canvas.style.cursor = "grab";
if (!drag.moved) {
const plotIndex = raycastPlot(event);
if (plotIndex >= 0) {
const request = sim.requests.find(
(item) => item.state === "active" && item.plot === plotIndex
);
if (request) {
serveRequest(request.id);
} else {
showToast(`${plotNames[plotIndex]} does not need water right now.`);
}
}
}
if (canvas.hasPointerCapture(event.pointerId)) {
canvas.releasePointerCapture(event.pointerId);
}
});
canvas.addEventListener("pointercancel", () => {
drag.active = false;
canvas.style.cursor = "grab";
});
canvas.addEventListener(
"wheel",
(event) => {
event.preventDefault();
cameraState.zoom = THREE.MathUtils.clamp(
cameraState.zoom * Math.exp(-event.deltaY * 0.001),
0.68,
1.7
);
updateCamera();
},
{ passive: false }
);
// Game flow.
function resetSimulation() {
sim.time = 0;
sim.running = false;
sim.ended = false;
sim.storage = STARTING_WATER;
sim.used = 0;
sim.overflow = 0;
sim.served = 0;
sim.missed = 0;
sim.nextRequest = 0;
sim.nextRain = 0;
sim.rainUntil = 0;
sim.requests = requestSchedule.map((request, id) => ({
...request,
id,
state: "future"
}));
plots.forEach((plot) => {
plot.growth = 0.72;
plot.targetGrowth = 0.72;
plot.thirsty = false;
plot.marker.visible = false;
});
rainMesh.visible = false;
}
function setCapacity(value) {
const newCapacity = Number(value);
const oldCapacity = sim.capacity;
if (sim.started && !sim.ended && sim.storage > newCapacity) {
const spilled = sim.storage - newCapacity;
sim.storage = newCapacity;
sim.overflow += spilled;
showToast(`Tank resized: ${spilled} L spilled as overflow.`, "warning");
}
sim.capacity = newCapacity;
document.querySelectorAll("[data-capacity]").forEach((input) => {
input.value = newCapacity;
});
$("capacityReadout").textContent = `${newCapacity} L`;
$("introCapacityValue").textContent = `${newCapacity} L`;
updateUI();
}
document.querySelectorAll("[data-capacity]").forEach((input) => {
input.addEventListener("input", (event) => {
setCapacity(event.target.value);
});
});
function activateRequest(request) {
request.state = "active";
const plot = plots[request.plot];
plot.thirsty = true;
plot.targetGrowth = 0.54;
plot.marker.visible = true;
plot.marker.scale.setScalar(1);
showToast(`${plotNames[request.plot]} requests ${request.amount} L.`);
}
function applyRain(weather) {
const room = Math.max(0, sim.capacity - sim.storage);
const stored = Math.min(weather.amount, room);
const spilled = weather.amount - stored;
sim.storage += stored;
sim.overflow += spilled;
sim.rainUntil = sim.time + 4.5;
if (spilled > 0) {
showToast(`Storm collected ${stored} L; ${spilled} L overflowed.`, "warning");
} else {
showToast(`Storm collected ${stored} L into the tank.`);
}
}
function serveRequest(id) {
const request = sim.requests.find((item) => item.id === id);
if (!request || request.state !== "active") return;
if (sim.storage < request.amount) {
showToast(
`Not enough water: this bed needs ${request.amount} L.`,
"warning"
);
return;
}
sim.storage -= request.amount;
sim.used += request.amount;
sim.served++;
request.state = "served";
const plot = plots[request.plot];
plot.thirsty = false;
plot.targetGrowth = 1;
plot.marker.visible = false;
showToast(`${plotNames[request.plot]} watered · ${request.amount} L used.`);
updateUI();
}
function missRequest(request) {
request.state = "missed";
sim.missed++;
plots[request.plot].marker.visible = false;
showToast(`${plotNames[request.plot]} missed its water request.`, "warning");
}
function advanceSimulation(delta) {
sim.time += delta;
while (
sim.nextRain < rainSchedule.length &&
sim.time >= rainSchedule[sim.nextRain].at
) {
applyRain(rainSchedule[sim.nextRain]);
sim.nextRain++;
}
while (
sim.nextRequest < sim.requests.length &&
sim.time >= sim.requests[sim.nextRequest].at
) {
activateRequest(sim.requests[sim.nextRequest]);
sim.nextRequest++;
}
sim.requests.forEach((request) => {
if (request.state === "active" && sim.time >= request.deadline) {
missRequest(request);
}
});
if (sim.time >= DAY_LENGTH) {
sim.time = DAY_LENGTH;
endDay();
}
}
function startDay() {
resetSimulation();
sim.started = true;
sim.running = true;
$("introOverlay").classList.add("hidden");
$("endOverlay").classList.add("hidden");
showToast("Day started — the first storm arrives at 8 seconds.");
updateUI();
}
function editSetup() {
resetSimulation();
sim.started = false;
sim.running = false;
$("endOverlay").classList.add("hidden");
$("introOverlay").classList.remove("hidden");
updateUI();
}
function endDay() {
sim.running = false;
sim.ended = true;
let title = "Time to Reseed";
let message = "Try a smaller storage plan and serve urgent requests before the next storm.";
let eyebrow = "Day complete";
if (sim.served >= 6 && sim.overflow === 0) {
title = "Master Gardener";
message = "Excellent water routing. Six beds were saved with no waste and the tank stayed perfectly balanced.";
eyebrow = "Perfect route";
} else if (sim.served >= 5) {
title = "Good Steward";
message = "Most of the garden survived. Adjust the capacity and request order to save one more bed without overflowing.";
eyebrow = "Day complete";
} else if (sim.served >= 3) {
title = "A Few Roots Spent";
message = "The tank had enough total water, but timing and capacity made it difficult to reach the beds.";
eyebrow = "Day complete";
}
$("endEyebrow").textContent = eyebrow;
$("endTitle").textContent = title;
$("endMessage").textContent = message;
$("endServed").textContent = sim.served;
$("endMissed").textContent = sim.missed;
$("endUsed").textContent = `${sim.used} L`;
$("endOverflow").textContent = `${sim.overflow} L`;
$("endOverlay").classList.remove("hidden");
updateUI();
}
function togglePause() {
if (!sim.started || sim.ended) return;
sim.running = !sim.running;
updateUI();
}
$("startBtn").addEventListener("click", startDay);
$("againBtn").addEventListener("click", startDay);
$("editBtn").addEventListener("click", editSetup);
$("resetBtn").addEventListener("click", startDay);
$("pauseBtn").addEventListener("click", togglePause);
document.querySelectorAll("[data-speed]").forEach((button) => {
button.addEventListener("click", () => {
sim.speed = Number(button.dataset.speed);
document.querySelectorAll("[data-speed]").forEach((item) => {
item.classList.toggle("active", item === button);
});
});
});
$("requestList").addEventListener("click", (event) => {
const button = event.target.closest("[data-serve]");
if (button) {
serveRequest(Number(button.dataset.serve));
}
});
window.addEventListener("keydown", (event) => {
if (event.target instanceof HTMLInputElement) return;
if (event.code === "Space") {
event.preventDefault();
togglePause();
} else if (event.code === "ArrowLeft") {
cameraState.azimuth += 0.12;
updateCamera();
} else if (event.code === "ArrowRight") {
cameraState.azimuth -= 0.12;
updateCamera();
} else if (event.code === "ArrowUp") {
cameraState.elevation = Math.min(1.22, cameraState.elevation + 0.08);
updateCamera();
} else if (event.code === "ArrowDown") {
cameraState.elevation = Math.max(0.32, cameraState.elevation - 0.08);
updateCamera();
} else if (event.code === "Equal" || event.code === "NumpadAdd") {
cameraState.zoom = Math.min(1.7, cameraState.zoom + 0.1);
updateCamera();
} else if (event.code === "Minus" || event.code === "NumpadSubtract") {
cameraState.zoom = Math.max(0.68, cameraState.zoom - 0.1);
updateCamera();
}
});
let toastTimeout;
function showToast(message, type = "") {
const toast = $("toast");
toast.textContent = message;
toast.className = `toast show ${type}`;
clearTimeout(toastTimeout);
toastTimeout = setTimeout(() => {
toast.classList.remove("show");
}, 2800);
}
// Timeline markers.
$("timelineMarkers").innerHTML = [
...rainSchedule.map(
(rain, index) =>
`<div class="timeline-marker rain" data-rain="${index}" title="Rain: ${rain.amount} L" style="left:${(rain.at / DAY_LENGTH) * 100}%"><span>R</span></div>`
),
...requestSchedule.map(
(request, index) =>
`<div class="timeline-marker request" data-request="${index}" title="Water request" style="left:${(request.at / DAY_LENGTH) * 100}%"><span>W</span></div>`
)
].join("");
function renderRequests() {
const active = sim.requests.filter((request) => request.state === "active");
$("requestCount").textContent = `${active.length} active`;
if (active.length) {
$("requestList").innerHTML = active
.map((request) => {
const remaining = Math.max(0, request.deadline - sim.time);
const low = sim.storage < request.amount;
return `
<article class="request-card">
<div class="request-top">
<span class="request-name">${plotNames[request.plot]}</span>
<span class="request-amount">${request.amount} L</span>
</div>
<div class="request-bottom">
<span class="deadline">Expires in ${remaining.toFixed(0)}s</span>
<button class="serve-button ${low ? "low" : ""}" data-serve="${request.id}">
${low ? `Need ${request.amount - Math.floor(sim.storage)} L` : "Serve"}
</button>
</div>
</article>
`;
})
.join("");
} else {
$("requestList").innerHTML = `
<div class="empty-state">
${sim.started ? "All current requests are settled." : "The garden is waiting for the day to begin."}
</div>
`;
}
const next = sim.requests.find((request) => request.state === "future");
$("nextRequest").textContent = next
? `Next request in ${Math.max(0, next.at - sim.time).toFixed(0)}s · ${plotNames[next.plot]}`
: "No more requests are scheduled.";
const served = sim.requests.filter((request) => request.state === "served");
$("servedSummary").innerHTML = served.length
? served
.map(
(request) =>
`<span class="served-chip">✓ ${plotNames[request.plot]} · ${request.amount} L</span>`
)
.join("")
: `<span class="served-chip">${sim.served} requests served</span>`;
}
function renderForecast() {
$("forecast").innerHTML = rainSchedule
.map((rain, index) => {
const done = sim.time >= rain.at + 4.5;
const active =
sim.time >= rain.at && sim.time < rain.at + 4.5;
return `
<div class="rain-chip ${done ? "done" : ""} ${active ? "active" : ""}">
<strong>${rain.amount} L</strong>
<span>${done ? "Collected" : active ? "Pouring now" : `At ${rain.at}s`}</span>
</div>
`;
})
.join("");
}
function formatTime(seconds) {
const minutes = Math.floor(seconds / 60);
const remaining = Math.floor(seconds % 60);
return `${String(minutes).padStart(2, "0")}:${String(remaining).padStart(2, "0")}`;
}
function updateUI() {
const fill = sim.capacity > 0 ? (sim.storage / sim.capacity) * 100 : 0;
$("storageMetric").textContent = Math.floor(sim.storage);
$("usedMetric").textContent = sim.used;
$("overflowMetric").textContent = sim.overflow;
$("storageFill").style.width = `${Math.min(100, Math.max(0, fill))}%`;
$("storageCaption").textContent = `${Math.floor(sim.storage)} of ${sim.capacity} L stored`;
$("fillPercent").textContent = `${Math.round(fill)}%`;
$("capacityReadout").textContent = `${sim.capacity} L`;
$("introCapacityValue").textContent = `${sim.capacity} L`;
document.querySelectorAll("[data-capacity]").forEach((input) => {
input.value = sim.capacity;
});
$("pauseBtn").disabled = !sim.started || sim.ended;
$("pauseBtn").textContent = sim.running ? "Pause" : "Resume";
$("statusDot").classList.toggle("live", sim.running);
$("statusText").textContent = !sim.started
? "Ready"
: sim.ended
? "Complete"
: sim.running
? "Day live"
: "Paused";
$("timeReadout").textContent = `${formatTime(sim.time)} / ${formatTime(DAY_LENGTH)}`;
$("timelineFill").style.width = `${(sim.time / DAY_LENGTH) * 100}%`;
document.querySelectorAll("[data-rain]").forEach((marker) => {
const index = Number(marker.dataset.rain);
const rain = rainSchedule[index];
marker.classList.toggle("done", sim.time >= rain.at + 4.5);
marker.classList.toggle(
"active",
sim.time >= rain.at && sim.time < rain.at + 4.5
);
});
document.querySelectorAll("[data-request]").forEach((marker) => {
const index = Number(marker.dataset.request);
const request = sim.requests[index];
marker.classList.toggle("done", request?.state === "served" || request?.state === "missed");
marker.classList.toggle("active", request?.state === "active");
});
renderRequests();
renderForecast();
}
function updateTankVisual() {
const ratio = THREE.MathUtils.clamp(sim.storage / sim.capacity, 0, 1);
const exactLevel = ratio * waterLevels.length;
const fullLevels = Math.floor(exactLevel);
const partial = exactLevel - fullLevels;
waterLevels.forEach((level, index) => {
const isFull = index < fullLevels;
const isPartial = index === fullLevels && partial > 0.001;
level.group.visible = isFull || isPartial;
level.material.opacity = isFull ? 0.78 : 0.28 + partial * 0.48;
});
}
function updatePlotVisuals(delta, elapsed) {
plots.forEach((plot, index) => {
plot.growth = THREE.MathUtils.damp(
plot.growth,
plot.targetGrowth,
4.5,
delta
);
const color = plot.thirsty ? thirstyLeafColor : healthyLeafColor;
plot.sprouts.forEach((sprout, plantIndex) => {
sprout.group.scale.y = THREE.MathUtils.clamp(
plot.growth + Math.sin(elapsed * 2 + plantIndex + index) * 0.025,
0.38,
1.12
);
temporaryColor.copy(dryLeafColor).lerp(color, plot.growth);
sprout.leafMaterial.color.copy(temporaryColor);
});
const targetHover = plot.hovered ? 0.2 : 0;
plot.hover.material.opacity = THREE.MathUtils.damp(
plot.hover.material.opacity,
targetHover,
9,
delta
);
if (plot.marker.visible) {
plot.marker.position.y =
2.25 + Math.sin(elapsed * 4 + index) * 0.14;
plot.marker.rotation.y += delta * 1.6;
}
});
}
function updateRain(delta) {
const raining = sim.time < sim.rainUntil;
rainMesh.visible = raining;
if (!raining) return;
for (let i = 0; i < rainCount; i++) {
const drop = rainData[i];
drop.y -= delta * drop.speed;
if (drop.y < 0) {
drop.y = 13 + Math.random() * 3;
drop.x = -10 + Math.random() * 20;
drop.z = -8 + Math.random() * 16;
}
dummy.position.set(drop.x, drop.y, drop.z);
dummy.scale.set(0.055, 0.48, 0.055);
dummy.rotation.set(0, 0, -0.08);
dummy.updateMatrix();
rainMesh.setMatrixAt(i, dummy.matrix);
}
rainMesh.instanceMatrix.needsUpdate = true;
}
resetSimulation();
updateUI();
updateTankVisual();
const clock = new THREE.Clock();
let uiAccumulator = 0;
function animate() {
requestAnimationFrame(animate);
const delta = Math.min(clock.getDelta(), 0.05);
const elapsed = clock.elapsedTime;
if (sim.running && !sim.ended) {
advanceSimulation(delta * sim.speed);
}
updatePlotVisuals(delta, elapsed);
updateTankVisual();
updateRain(delta);
clouds.forEach((cloud) => {
cloud.group.position.x =
cloud.originX + Math.sin(elapsed * cloud.speed) * 2.4;
});
uiAccumulator += delta;
if (uiAccumulator >= 0.1) {
updateUI();
uiAccumulator = 0;
}
renderer.render(scene, camera);
}
animate();
</script>
</body>
</html>
```