Prompt specification
Build or customize the Three.js 3D scene "Storage filling and depletion model" 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: Storage filling and depletion model
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/storage-filling-and-depletion-model-x0794
Scene Description Prompt:
Please create one self-contained HTML file with Three.js from cdnjs showing succulent water storage. Animate a symbolic internal water reservoir increasing after a rain event and decreasing during a dry interval; keep the leaf's external geometry mostly stable. Voxel style.
Working Three.js Starter (storage-filling-and-depletion-model-x0794):
```html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Succulent Water Storage — Voxel Botanical Study</title>
<style>
:root {
--bg: #071719;
--panel: rgba(8, 29, 31, 0.82);
--panel-border: rgba(180, 231, 181, 0.16);
--text: #eef8e9;
--muted: #91aaa2;
--leaf: #9bd56a;
--water: #49d8ff;
--sun: #ffd66d;
--rain: #79aaff;
}
* { box-sizing: border-box; }
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: var(--bg);
color: var(--text);
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
canvas {
position: fixed;
inset: 0;
width: 100%;
height: 100%;
display: block;
touch-action: none;
cursor: grab;
}
canvas:active { cursor: grabbing; }
.hud {
position: fixed;
inset: 0;
pointer-events: none;
z-index: 2;
}
.eyebrow {
display: flex;
align-items: center;
gap: 10px;
color: var(--leaf);
font-size: 11px;
font-weight: 800;
letter-spacing: 0.18em;
text-transform: uppercase;
}
.eyebrow::before {
content: "";
width: 24px;
height: 3px;
background: var(--leaf);
box-shadow: 0 0 16px rgba(155, 213, 106, 0.45);
}
.intro {
position: absolute;
top: clamp(24px, 4.5vw, 58px);
left: clamp(24px, 4.5vw, 70px);
width: min(440px, calc(100vw - 48px));
}
h1 {
margin: 18px 0 14px;
font-size: clamp(38px, 5.6vw, 78px);
line-height: 0.95;
letter-spacing: -0.055em;
font-weight: 760;
text-wrap: balance;
text-shadow: 0 4px 35px rgba(0, 0, 0, 0.25);
}
.intro p {
max-width: 380px;
margin: 0;
color: var(--muted);
font-size: clamp(13px, 1.25vw, 16px);
line-height: 1.6;
}
.stable-badge {
display: inline-flex;
align-items: center;
gap: 8px;
margin-top: 18px;
padding: 8px 11px;
border: 1px solid rgba(155, 213, 106, 0.24);
border-radius: 8px;
background: rgba(34, 75, 48, 0.25);
color: #b9dc9d;
font-size: 10px;
font-weight: 800;
letter-spacing: 0.12em;
text-transform: uppercase;
}
.stable-badge::before {
content: "";
width: 7px;
height: 7px;
background: var(--leaf);
box-shadow: 0 0 10px rgba(155, 213, 106, 0.65);
}
.status-card {
position: absolute;
top: clamp(24px, 4.5vw, 58px);
right: clamp(24px, 4.5vw, 70px);
width: min(285px, calc(100vw - 48px));
padding: 17px 18px 16px;
border: 1px solid var(--panel-border);
border-radius: 13px;
background: var(--panel);
box-shadow: 0 18px 50px rgba(0, 0, 0, 0.2);
backdrop-filter: blur(12px);
}
.status-row {
display: flex;
align-items: center;
justify-content: space-between;
gap: 14px;
}
.status-label {
color: var(--muted);
font-size: 9px;
font-weight: 800;
letter-spacing: 0.16em;
text-transform: uppercase;
}
.phase-name {
margin-top: 5px;
color: var(--water);
font-size: 14px;
font-weight: 800;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.phase-detail {
min-height: 19px;
margin-top: 4px;
color: #afc1b9;
font-size: 11px;
}
.percentage {
color: var(--water);
font-size: 30px;
line-height: 1;
font-weight: 780;
letter-spacing: -0.05em;
font-variant-numeric: tabular-nums;
}
.percentage small {
color: var(--muted);
font-size: 10px;
letter-spacing: 0;
}
.meter {
height: 7px;
margin-top: 15px;
overflow: hidden;
border-radius: 4px;
background: rgba(255, 255, 255, 0.08);
}
.meter-fill {
width: 18%;
height: 100%;
border-radius: inherit;
background: linear-gradient(90deg, #237ca4, var(--water));
box-shadow: 0 0 16px rgba(73, 216, 255, 0.45);
transition: width 80ms linear;
}
.legend {
position: absolute;
left: clamp(24px, 4.5vw, 70px);
bottom: clamp(108px, 12vh, 140px);
width: min(330px, calc(100vw - 48px));
padding: 15px 17px;
border-left: 3px solid var(--water);
background: linear-gradient(90deg, rgba(7, 25, 27, 0.86), rgba(7, 25, 27, 0.3));
border-radius: 0 10px 10px 0;
}
.legend-row {
display: flex;
align-items: center;
gap: 10px;
margin: 7px 0;
font-size: 12px;
font-weight: 700;
}
.swatch {
width: 11px;
height: 11px;
box-shadow: 0 0 12px currentColor;
}
.swatch.green {
color: var(--leaf);
background: var(--leaf);
}
.swatch.blue {
color: var(--water);
background: var(--water);
}
.legend p {
margin: 10px 0 0;
color: var(--muted);
font-size: 11px;
line-height: 1.55;
}
.timeline-wrap {
position: absolute;
left: 50%;
bottom: 24px;
width: min(660px, calc(100vw - 48px));
transform: translateX(-50%);
}
.timeline-meta {
display: flex;
justify-content: space-between;
margin-bottom: 8px;
color: var(--muted);
font-size: 9px;
font-weight: 800;
letter-spacing: 0.12em;
text-transform: uppercase;
}
.cycle-track {
position: relative;
height: 3px;
overflow: hidden;
border-radius: 4px;
background: rgba(255, 255, 255, 0.09);
}
.cycle-progress {
width: 0;
height: 100%;
background: linear-gradient(90deg, var(--rain), var(--water), var(--sun));
}
.segments {
display: grid;
grid-template-columns: 1.15fr 1.8fr 2.25fr 1fr;
gap: 6px;
margin-top: 10px;
}
.segment {
position: relative;
padding-top: 8px;
color: #627a74;
font-size: 9px;
font-weight: 800;
letter-spacing: 0.09em;
text-transform: uppercase;
transition: color 180ms ease;
}
.segment::before {
content: "";
position: absolute;
top: 0;
left: 0;
width: 5px;
height: 5px;
background: currentColor;
}
.segment.active { color: var(--text); }
.segment.active.rain { color: var(--rain); }
.segment.active.recharge { color: var(--water); }
.segment.active.dry { color: var(--sun); }
.control-hint {
position: absolute;
right: clamp(24px, 4.5vw, 70px);
bottom: 124px;
color: rgba(187, 208, 198, 0.55);
font-size: 10px;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.cutaway-label {
position: absolute;
left: 55%;
top: 29%;
color: rgba(190, 229, 183, 0.72);
font-size: 9px;
font-weight: 800;
letter-spacing: 0.14em;
text-transform: uppercase;
}
.cutaway-label::after {
content: "";
display: block;
width: 42px;
height: 1px;
margin-top: 7px;
background: rgba(190, 229, 183, 0.35);
}
@media (max-width: 820px) {
.intro {
top: 22px;
left: 20px;
}
h1 {
max-width: 310px;
font-size: clamp(34px, 10vw, 52px);
}
.intro p,
.stable-badge,
.legend,
.cutaway-label {
display: none;
}
.status-card {
top: auto;
right: 20px;
bottom: 108px;
width: 220px;
padding: 13px 14px;
}
.percentage { font-size: 25px; }
.phase-detail { display: none; }
.control-hint { display: none; }
.timeline-wrap { bottom: 22px; }
.segments { grid-template-columns: repeat(4, 1fr); }
.segment { font-size: 7px; }
}
@media (max-width: 480px) {
.eyebrow { font-size: 9px; }
.status-card {
right: 16px;
bottom: 98px;
width: 190px;
}
.timeline-wrap { width: calc(100vw - 32px); }
.segment { letter-spacing: 0.03em; }
}
</style>
</head>
<body>
<div class="hud">
<header class="intro">
<div class="eyebrow">Voxel botany / water storage</div>
<h1>A rainy day,<br />kept in reserve.</h1>
<p>
A cutaway succulent stores incoming rain in its thick tissues.
The leaf silhouette stays fixed while its internal blue reservoir fills and drains.
</p>
<div class="stable-badge">Fixed external leaf geometry</div>
</header>
<section class="status-card" aria-live="polite">
<div class="status-row">
<div>
<div class="status-label">Current condition</div>
<div class="phase-name" id="phaseName">Rain event</div>
<div class="phase-detail" id="phaseDetail">Rain reaches the roots</div>
</div>
<div class="percentage"><span id="levelValue">18</span><small>%</small></div>
</div>
<div class="meter" aria-label="Internal water reservoir level">
<div class="meter-fill" id="meterFill"></div>
</div>
</section>
<aside class="legend">
<div class="legend-row">
<span class="swatch green"></span>
Stable green voxels: leaf tissue
</div>
<div class="legend-row">
<span class="swatch blue"></span>
Blue voxels: internal water reservoir
</div>
<p>
The blue column rises after the rain event and falls during the dry interval,
showing water moving into and out of storage.
</p>
</aside>
<div class="cutaway-label">Voxel cutaway view</div>
<div class="control-hint">Drag to orbit · Scroll to zoom · Double-click to reset</div>
<div class="timeline-wrap">
<div class="timeline-meta">
<span>16 second environmental cycle</span>
<span id="cycleTime">00:00</span>
</div>
<div class="cycle-track">
<div class="cycle-progress" id="cycleProgress"></div>
</div>
<div class="segments">
<div class="segment rain active" data-segment="0">01 Rain</div>
<div class="segment recharge" data-segment="1">02 Recharge</div>
<div class="segment dry" data-segment="2">03 Dry interval</div>
<div class="segment" data-segment="3">04 Wait</div>
</div>
</div>
</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// -------------------------------------------------------------------------
// Renderer, camera and scene
// -------------------------------------------------------------------------
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x071719);
scene.fog = new THREE.FogExp2(0x071719, 0.027);
const camera = new THREE.PerspectiveCamera(
42,
innerWidth / innerHeight,
0.1,
100
);
const renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setPixelRatio(Math.min(devicePixelRatio, 2));
renderer.setSize(innerWidth, innerHeight);
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.08;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
document.body.prepend(renderer.domElement);
const hemisphere = new THREE.HemisphereLight(0xa5e7ff, 0x162117, 2.15);
scene.add(hemisphere);
const keyLight = new THREE.DirectionalLight(0xfff0d0, 3.3);
keyLight.position.set(5.5, 10, 7);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(2048, 2048);
keyLight.shadow.camera.left = -9;
keyLight.shadow.camera.right = 9;
keyLight.shadow.camera.top = 10;
keyLight.shadow.camera.bottom = -5;
keyLight.shadow.bias = -0.0008;
scene.add(keyLight);
const rimLight = new THREE.DirectionalLight(0x4bb8dc, 1.55);
rimLight.position.set(-6, 5, -5);
scene.add(rimLight);
const unitBox = new THREE.BoxGeometry(1, 1, 1);
const dummy = new THREE.Object3D();
// Deterministic pseudo-random values keep the composition stable.
let randomSeed = 9137;
function random() {
randomSeed = (randomSeed * 16807) % 2147483647;
return (randomSeed - 1) / 2147483646;
}
function buildInstances(items, material, parent = scene, shadows = true) {
const mesh = new THREE.InstancedMesh(unitBox, material, items.length);
items.forEach((item, index) => {
dummy.position.set(...item.position);
dummy.scale.set(...item.scale);
dummy.rotation.set(...(item.rotation || [0, 0, 0]));
dummy.updateMatrix();
mesh.setMatrixAt(index, dummy.matrix);
if (item.color !== undefined) {
mesh.setColorAt(index, new THREE.Color(item.color));
}
});
mesh.instanceMatrix.needsUpdate = true;
if (mesh.instanceColor) mesh.instanceColor.needsUpdate = true;
mesh.castShadow = shadows;
mesh.receiveShadow = shadows;
parent.add(mesh);
return mesh;
}
// -------------------------------------------------------------------------
// Materials
// -------------------------------------------------------------------------
const material = {
green: new THREE.MeshStandardMaterial({
color: 0xffffff,
roughness: 0.82,
metalness: 0.02
}),
membrane: new THREE.MeshStandardMaterial({
color: 0x79bf72,
roughness: 0.72,
transparent: true,
opacity: 0.18,
depthWrite: false,
side: THREE.DoubleSide
}),
vein: new THREE.MeshStandardMaterial({
color: 0x285f3c,
roughness: 0.9
}),
pot: new THREE.MeshStandardMaterial({
color: 0xffffff,
roughness: 0.88
}),
soil: new THREE.MeshStandardMaterial({
color: 0xffffff,
roughness: 1
}),
ground: new THREE.MeshStandardMaterial({
color: 0xffffff,
roughness: 0.95
}),
water: new THREE.MeshStandardMaterial({
color: 0xffffff,
emissive: 0x0c6587,
emissiveIntensity: 0.42,
roughness: 0.25,
metalness: 0.06
}),
cloud: new THREE.MeshStandardMaterial({
color: 0xffffff,
roughness: 1
}),
rain: new THREE.MeshStandardMaterial({
color: 0x77b9ff,
emissive: 0x245b9e,
emissiveIntensity: 0.55,
roughness: 0.35,
transparent: true,
opacity: 0.88
}),
sun: new THREE.MeshStandardMaterial({
color: 0xffd66d,
emissive: 0xb87513,
emissiveIntensity: 0.45,
roughness: 0.7
})
};
// -------------------------------------------------------------------------
// Voxel ground
// -------------------------------------------------------------------------
const groundItems = [];
const groundColors = [0x0b2728, 0x0e2e2e, 0x103333, 0x123939];
for (let x = -8; x <= 8; x++) {
for (let z = -8; z <= 8; z++) {
const distance = Math.hypot(x, z);
if (distance > 8.6) continue;
groundItems.push({
position: [x * 0.56, -0.19, z * 0.56],
scale: [0.535, 0.27, 0.535],
color: groundColors[Math.floor(random() * groundColors.length)]
});
}
}
buildInstances(groundItems, material.ground);
// -------------------------------------------------------------------------
// Voxel planter
// -------------------------------------------------------------------------
const potItems = [];
const potColors = [0xb96549, 0xc96f4f, 0xd77b57, 0xe18a63];
for (let row = 0; row < 4; row++) {
for (let ix = 0; ix < 9; ix++) {
for (let iz = 0; iz < 7; iz++) {
const edge =
ix === 0 || ix === 8 || iz === 0 || iz === 6;
if (!edge) continue;
potItems.push({
position: [
(ix - 4) * 0.27,
0.11 + row * 0.235,
(iz - 3) * 0.27
],
scale: [0.255, 0.22, 0.255],
color: potColors[(ix + iz + row) % potColors.length]
});
}
}
}
// Wider top rim.
for (let ix = 0; ix < 10; ix++) {
for (let iz = 0; iz < 8; iz++) {
const edge = ix === 0 || ix === 9 || iz === 0 || iz === 7;
if (!edge) continue;
potItems.push({
position: [
(ix - 4.5) * 0.29,
0.99,
(iz - 3.5) * 0.29
],
scale: [0.275, 0.22, 0.275],
color: potColors[(ix * 2 + iz) % potColors.length]
});
}
}
buildInstances(potItems, material.pot);
const soilItems = [];
for (let ix = 0; ix < 8; ix++) {
for (let iz = 0; iz < 6; iz++) {
soilItems.push({
position: [
(ix - 3.5) * 0.27,
1.04 + random() * 0.025,
(iz - 2.5) * 0.27
],
scale: [0.255, 0.12, 0.255],
color: random() > 0.5 ? 0x493a2d : 0x5c4935
});
}
}
buildInstances(soilItems, material.soil);
// -------------------------------------------------------------------------
// Static surrounding leaves
// -------------------------------------------------------------------------
const leafPalette = [
0x285f3d,
0x347448,
0x438452,
0x57965b,
0x6ca866
];
function createStaticLeaf(direction, rows, maxWidth) {
const group = new THREE.Group();
group.position.set(0, 1.02, 0);
const directionVector = new THREE.Vector3(...direction).normalize();
group.quaternion.setFromUnitVectors(
new THREE.Vector3(0, 1, 0),
directionVector
);
const items = [];
for (let row = 0; row < rows; row++) {
const t = (row + 1) / (rows + 1);
const half = Math.max(
0,
Math.round(Math.sin(t * Math.PI) * maxWidth)
);
for (let column = -half; column <= half; column++) {
items.push({
position: [
column * 0.225,
(row + 0.5) * 0.225,
0
],
scale: [0.211, 0.211, 0.211],
color: leafPalette[
(Math.abs(column) + row + Math.floor(random() * 3)) %
leafPalette.length
]
});
}
}
buildInstances(items, material.green, group);
scene.add(group);
}
createStaticLeaf([-0.92, 0.43, -0.12], 11, 4);
createStaticLeaf([-0.70, 0.38, -0.56], 10, 4);
createStaticLeaf([-0.32, 0.34, -0.78], 9, 3);
createStaticLeaf([0.92, 0.43, -0.16], 11, 4);
createStaticLeaf([0.68, 0.38, -0.59], 10, 4);
createStaticLeaf([0.31, 0.34, -0.80], 9, 3);
createStaticLeaf([-0.96, 0.25, 0.24], 7, 3);
createStaticLeaf([0.96, 0.25, 0.20], 7, 3);
// -------------------------------------------------------------------------
// Main cutaway leaf: fixed external silhouette, variable inner reservoir
// -------------------------------------------------------------------------
const heroGroup = new THREE.Group();
heroGroup.position.set(0, 1.07, 0.42);
heroGroup.rotation.set(0, -0.10, -0.055);
scene.add(heroGroup);
const CELL = 0.26;
const heroRows = [
0, 1, 2, 3, 4, 5, 6, 6, 6,
6, 6, 5, 5, 4, 4, 3, 2, 1, 0
];
const heroGreen = [];
const heroMembrane = [];
const heroVeins = [];
const waterSlots = [];
heroRows.forEach((halfWidth, row) => {
const y = (row + 0.5) * CELL;
for (let column = -halfWidth; column <= halfWidth; column++) {
const isEdge = Math.abs(column) === halfWidth;
if (isEdge) {
// Opaque contour voxels define the leaf's stable outer geometry.
heroGreen.push({
position: [column * CELL, y, 0],
scale: [CELL * 0.94, CELL * 0.94, 0.50],
color: leafPalette[
(row + Math.abs(column) + Math.floor(random() * 2)) %
leafPalette.length
]
});
} else {
// A translucent back layer suggests tissue behind the cutaway.
heroMembrane.push({
position: [column * CELL, y, -0.17],
scale: [CELL * 0.92, CELL * 0.92, 0.12]
});
}
}
// A central vein remains visible behind the water.
if (halfWidth > 0) {
heroVeins.push({
position: [0, y, -0.055],
scale: [0.052, CELL * 0.86, 0.075]
});
}
// The reservoir occupies a tapered channel within the leaf.
let waterHalfWidth = 0;
if (halfWidth >= 5) waterHalfWidth = 2;
else if (halfWidth >= 2) waterHalfWidth = 1;
else if (halfWidth >= 1) waterHalfWidth = 1;
for (let column = -waterHalfWidth; column <= waterHalfWidth; column++) {
waterSlots.push({
row,
column,
baseY: row * CELL,
shade: column === 0 ? 0x50ddff : 0x32bfea
});
}
});
buildInstances(heroGreen, material.green, heroGroup);
buildInstances(heroMembrane, material.membrane, heroGroup, false);
buildInstances(heroVeins, material.vein, heroGroup);
const waterMesh = new THREE.InstancedMesh(
unitBox,
material.water,
waterSlots.length
);
waterMesh.instanceMatrix.setUsage(THREE.DynamicDrawUsage);
waterMesh.castShadow = true;
waterMesh.receiveShadow = true;
waterMesh.frustumCulled = false;
heroGroup.add(waterMesh);
waterSlots.forEach((slot, index) => {
waterMesh.setColorAt(index, new THREE.Color(slot.shade));
});
if (waterMesh.instanceColor) waterMesh.instanceColor.needsUpdate = true;
const waterGlow = new THREE.PointLight(0x2bcaff, 1.1, 3.4, 2);
waterGlow.position.set(0, 2, 0.65);
heroGroup.add(waterGlow);
// -------------------------------------------------------------------------
// Voxel rain cloud
// -------------------------------------------------------------------------
const cloudGroup = new THREE.Group();
cloudGroup.position.set(-1.25, 7.18, -0.25);
scene.add(cloudGroup);
const cloudItems = [];
const cloudColors = [0x48646d, 0x55747b, 0x63848a, 0x719398];
for (let x = -4; x <= 4; x++) {
for (let z = -2; z <= 1; z++) {
const layers = random() > 0.56 ? 2 : 1;
for (let layer = 0; layer < layers; layer++) {
cloudItems.push({
position: [
x * 0.39,
layer * 0.30 + random() * 0.07,
z * 0.31
],
scale: [0.43, 0.36, 0.40],
color: cloudColors[Math.floor(random() * cloudColors.length)]
});
}
}
}
buildInstances(cloudItems, material.cloud, cloudGroup);
// -------------------------------------------------------------------------
// Voxel rain
// -------------------------------------------------------------------------
const RAIN_COUNT = 76;
const rainMesh = new THREE.InstancedMesh(
unitBox,
material.rain,
RAIN_COUNT
);
rainMesh.instanceMatrix.setUsage(THREE.DynamicDrawUsage);
rainMesh.frustumCulled = false;
scene.add(rainMesh);
const rainSeeds = Array.from({ length: RAIN_COUNT }, () => ({
x: -3.7 + random() * 6.8,
z: -1.45 + random() * 3.25,
offset: random(),
speed: 0.72 + random() * 0.48,
length: 0.18 + random() * 0.25
}));
// -------------------------------------------------------------------------
// Voxel sun for the dry interval
// -------------------------------------------------------------------------
const sunGroup = new THREE.Group();
sunGroup.position.set(4.85, 6.35, -1.05);
scene.add(sunGroup);
const sunItems = [
{ position: [0, 0, 0], scale: [0.48, 0.48, 0.48] },
{ position: [0.49, 0, 0], scale: [0.18, 0.18, 0.18] },
{ position: [-0.49, 0, 0], scale: [0.18, 0.18, 0.18] },
{ position: [0, 0.49, 0], scale: [0.18, 0.18, 0.18] },
{ position: [0, -0.49, 0], scale: [0.18, 0.18, 0.18] },
{ position: [0.34, 0.34, 0], scale: [0.15, 0.15, 0.15] },
{ position: [-0.34, 0.34, 0], scale: [0.15, 0.15, 0.15] },
{ position: [0.34, -0.34, 0], scale: [0.15, 0.15, 0.15] },
{ position: [-0.34, -0.34, 0], scale: [0.15, 0.15, 0.15] }
];
buildInstances(sunItems, material.sun, sunGroup);
const sunLight = new THREE.PointLight(0xffc85d, 0, 7, 2);
sunLight.position.copy(sunGroup.position);
scene.add(sunLight);
// -------------------------------------------------------------------------
// Uptake particles: visible only while the leaf is recharging
// -------------------------------------------------------------------------
const UPTAKE_COUNT = 16;
const uptakeMesh = new THREE.InstancedMesh(
unitBox,
material.water,
UPTAKE_COUNT
);
uptakeMesh.instanceMatrix.setUsage(THREE.DynamicDrawUsage);
uptakeMesh.frustumCulled = false;
scene.add(uptakeMesh);
const uptakeSeeds = Array.from({ length: UPTAKE_COUNT }, () => ({
offset: random(),
x: (random() - 0.5) * 0.42,
z: 0.38 + random() * 0.22,
speed: 0.8 + random() * 0.7
}));
// -------------------------------------------------------------------------
// Direct camera controls — no addons
// -------------------------------------------------------------------------
const cameraTarget = new THREE.Vector3(0, 3.05, 0);
const defaultCamera = { yaw: 0.48, phi: 1.20, radius: 13.6 };
let desiredYaw = defaultCamera.yaw;
let desiredPhi = defaultCamera.phi;
let desiredRadius = defaultCamera.radius;
let currentYaw = desiredYaw;
let currentPhi = desiredPhi;
let currentRadius = desiredRadius;
let dragging = false;
let previousX = 0;
let previousY = 0;
const canvas = renderer.domElement;
canvas.addEventListener("pointerdown", (event) => {
dragging = true;
previousX = event.clientX;
previousY = event.clientY;
canvas.setPointerCapture(event.pointerId);
});
canvas.addEventListener("pointermove", (event) => {
if (!dragging) return;
const dx = event.clientX - previousX;
const dy = event.clientY - previousY;
desiredYaw -= dx * 0.006;
desiredPhi = THREE.MathUtils.clamp(
desiredPhi - dy * 0.005,
0.56,
1.48
);
previousX = event.clientX;
previousY = event.clientY;
});
function stopDragging(event) {
dragging = false;
if (event.pointerId !== undefined && canvas.hasPointerCapture(event.pointerId)) {
canvas.releasePointerCapture(event.pointerId);
}
}
canvas.addEventListener("pointerup", stopDragging);
canvas.addEventListener("pointercancel", stopDragging);
canvas.addEventListener("wheel", (event) => {
event.preventDefault();
desiredRadius = THREE.MathUtils.clamp(
desiredRadius * Math.exp(event.deltaY * 0.001),
8.2,
20
);
}, { passive: false });
canvas.addEventListener("dblclick", () => {
desiredYaw = defaultCamera.yaw;
desiredPhi = defaultCamera.phi;
desiredRadius = defaultCamera.radius;
});
function updateCamera() {
currentYaw += (desiredYaw - currentYaw) * 0.09;
currentPhi += (desiredPhi - currentPhi) * 0.09;
currentRadius += (desiredRadius - currentRadius) * 0.09;
const sinPhi = Math.sin(currentPhi);
camera.position.set(
cameraTarget.x + currentRadius * sinPhi * Math.sin(currentYaw),
cameraTarget.y + currentRadius * Math.cos(currentPhi),
cameraTarget.z + currentRadius * sinPhi * Math.cos(currentYaw)
);
camera.lookAt(cameraTarget);
}
// -------------------------------------------------------------------------
// HUD and animation state
// -------------------------------------------------------------------------
const phaseName = document.getElementById("phaseName");
const phaseDetail = document.getElementById("phaseDetail");
const levelValue = document.getElementById("levelValue");
const meterFill = document.getElementById("meterFill");
const cycleProgress = document.getElementById("cycleProgress");
const cycleTimeLabel = document.getElementById("cycleTime");
const segments = [...document.querySelectorAll(".segment")];
const CYCLE = 16;
const phaseInfo = [
["Rain event", "Rain reaches the roots", "var(--rain)"],
["Recharging", "Rain ends • stored water rises", "var(--water)"],
["Dry interval", "Water leaves the reservoir", "var(--sun)"],
["Reservoir low", "Waiting for the next rain", "var(--leaf)"]
];
function smoothstep(value) {
const x = THREE.MathUtils.clamp(value, 0, 1);
return x * x * (3 - 2 * x);
}
function lerp(a, b, t) {
return a + (b - a) * t;
}
function updateWater(level) {
waterSlots.forEach((slot, index) => {
const fill = THREE.MathUtils.clamp(
level * heroRows.length - slot.row,
0,
1
);
dummy.position.set(
slot.column * CELL,
slot.baseY + fill * CELL * 0.49,
0.055
);
dummy.scale.set(
CELL * 0.88,
Math.max(0.001, fill * CELL * 0.98),
0.34
);
dummy.rotation.set(0, 0, 0);
dummy.updateMatrix();
waterMesh.setMatrixAt(index, dummy.matrix);
});
waterMesh.instanceMatrix.needsUpdate = true;
waterGlow.position.y = 0.65 + level * 4.2;
}
function updateRain(cycleTime) {
rainSeeds.forEach((seed, index) => {
const fall = (cycleTime * seed.speed + seed.offset) % 1;
const y = 6.75 - fall * 5.5;
dummy.position.set(
seed.x + fall * 0.22,
y,
seed.z
);
dummy.scale.set(0.027, seed.length, 0.027);
dummy.rotation.set(0, 0, -0.08);
dummy.updateMatrix();
rainMesh.setMatrixAt(index, dummy.matrix);
});
rainMesh.instanceMatrix.needsUpdate = true;
}
function updateUptake(cycleTime, visible) {
uptakeMesh.visible = visible;
if (!visible) return;
uptakeSeeds.forEach((seed, index) => {
const progress = (cycleTime * seed.speed + seed.offset) % 1;
const rise = smoothstep(progress);
dummy.position.set(
seed.x * (1 - rise * 0.45),
1.10 + rise * 2.15,
seed.z
);
dummy.scale.set(0.065, 0.065, 0.065);
dummy.rotation.set(0, rise * Math.PI, 0);
dummy.updateMatrix();
uptakeMesh.setMatrixAt(index, dummy.matrix);
});
uptakeMesh.instanceMatrix.needsUpdate = true;
}
function updateHUD(cycleTime, phase, level) {
const info = phaseInfo[phase];
phaseName.textContent = info[0];
phaseDetail.textContent = info[1];
phaseName.style.color = info[2];
levelValue.textContent = String(Math.round(level * 100)).padStart(2, "0");
meterFill.style.width = `${level * 100}%`;
cycleProgress.style.width = `${(cycleTime / CYCLE) * 100}%`;
const seconds = Math.floor(cycleTime);
cycleTimeLabel.textContent = `00:${String(seconds).padStart(2, "0")}`;
segments.forEach((segment, index) => {
segment.classList.toggle("active", index === phase);
});
}
const clock = new THREE.Clock();
function animate() {
const elapsed = clock.getElapsedTime();
const cycleTime = elapsed % CYCLE;
let phase;
let level;
if (cycleTime < 2.4) {
phase = 0;
level = lerp(0.18, 0.22, smoothstep(cycleTime / 2.4));
} else if (cycleTime < 7.0) {
phase = 1;
level = lerp(
0.22,
0.88,
smoothstep((cycleTime - 2.4) / 4.6)
);
} else if (cycleTime < 13.0) {
phase = 2;
level = lerp(
0.88,
0.18,
smoothstep((cycleTime - 7.0) / 6.0)
);
} else {
phase = 3;
level = 0.18;
}
// The rain event is visible before and just after the recharge starts.
const rainVisible = cycleTime < 3.25;
cloudGroup.visible = cycleTime < 3.6;
rainMesh.visible = rainVisible;
// Sunlight and heat become prominent during the dry interval.
const sunVisible = cycleTime >= 6.7 && cycleTime < 13.25;
sunGroup.visible = sunVisible;
sunLight.intensity = sunVisible ? 2.2 : 0;
updateWater(level);
updateRain(cycleTime);
updateUptake(cycleTime, phase === 1);
material.water.emissiveIntensity =
phase === 1
? 0.46 + Math.sin(elapsed * 4.2) * 0.12
: 0.40;
updateHUD(cycleTime, phase, level);
updateCamera();
renderer.render(scene, camera);
}
renderer.setAnimationLoop(animate);
addEventListener("resize", () => {
camera.aspect = innerWidth / innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(innerWidth, innerHeight);
renderer.setPixelRatio(Math.min(devicePixelRatio, 2));
});
</script>
</body>
</html>
```