Prompt specification
Build or customize the Three.js 3D scene "Allocate a fictional oasis water budget across uses" inside the active workspace.
Engineering Constraints:
1. Runtime & Orbit: Use Three.js r160 with OrbitControls (damping enabled) and ACESFilmicToneMapping.
2. Frame Timing: Drive all motion strictly with clock.getDelta() so animation speed is FPS-independent.
3. Viewport Hygiene: Handle window resize cleanly, cap DPR at Math.min(window.devicePixelRatio, 2), and dispose geometries/materials/renderer on unmount.
Scene Metadata:
Scene: Allocate a fictional oasis water budget across uses
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/allocate-a-fictional-oasis-water-budget-across-uses-x3116
Scene Description Prompt:
Allocate a fictional voxel oasis's water between households and crops, tracking remaining storage and unmet demand. Three.js from cdnjs, one HTML file.
Working Three.js Starter (allocate-a-fictional-oasis-water-budget-across-uses-x3116):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8"/>
<meta name="viewport" content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no"/>
<title>Voxel Oasis · Water Allocation</title>
<style>
*{box-sizing:border-box}
html,body{margin:0;height:100%;overflow:hidden;background:#0b1114;color:#e9f2f1;
font-family:ui-sans-serif,system-ui,-apple-system,"Segoe UI",Roboto,sans-serif;-webkit-font-smoothing:antialiased}
#scene{position:fixed;inset:0;width:100%;height:100%;display:block;touch-action:none;cursor:grab}
#scene.drag{cursor:grabbing}
.vignette{position:fixed;inset:0;pointer-events:none;
background:radial-gradient(ellipse at 50% 45%,rgba(0,0,0,0) 45%,rgba(0,0,0,.45) 100%)}
.panel{position:fixed;background:rgba(10,18,22,.80);border:1px solid rgba(255,255,255,.10);
border-radius:16px;padding:12px 14px;backdrop-filter:blur(12px) saturate(1.25);
box-shadow:0 14px 40px rgba(0,0,0,.45);pointer-events:auto;font-size:12px;line-height:1.45}
.head{display:flex;align-items:baseline;justify-content:space-between;margin:0 0 8px}
.head h1{font-size:15px;margin:0;letter-spacing:.4px;font-weight:700}
.head h2{font-size:11px;margin:0;text-transform:uppercase;letter-spacing:1.4px;color:#8fd7d2;font-weight:700}
.sub{font-size:11px;color:#9fb3b4}
.sub b{color:#ffd79a}
.row{display:flex;justify-content:space-between;align-items:center;margin:6px 0}
.row.big{font-size:12.5px;margin-top:4px}
.row.big b{font-size:14px;color:#7fe3da;font-variant-numeric:tabular-nums}
.lbl{font-size:10.5px;text-transform:uppercase;letter-spacing:1px;color:#8aa0a2;margin-bottom:4px}
.val{font-size:11px;color:#cfe0df;margin-top:3px;font-variant-numeric:tabular-nums}
.bar{height:9px;background:rgba(255,255,255,.09);border-radius:6px;overflow:hidden;margin-top:4px}
.bar.sm{height:7px}
.bar.xs{height:6px;flex:1;margin:0}
.bar i{display:block;height:100%;border-radius:6px;transition:width .45s ease;background:#57e0d0}
.bar i.hh{background:linear-gradient(90deg,#2f9fd8,#6fe3ff)}
.bar i.cr{background:linear-gradient(90deg,#c99a2e,#ffd36a)}
.grid2{display:grid;grid-template-columns:1fr 1fr;gap:10px;margin:9px 0 4px}
.muted{color:#7f9496}
.ent{display:grid;grid-template-columns:1fr 58px;grid-template-areas:"n v" "b b";gap:3px 8px;margin:6px 0}
.entName{grid-area:n;font-size:11.5px}
.entVal{grid-area:v;font-size:10.5px;text-align:right;color:#ffd0c8;font-variant-numeric:tabular-nums}
.ent .bar{grid-area:b}
.ent .bar i.bad{background:linear-gradient(90deg,#c8452f,#ff8a6b)}
.fld{display:block;margin:9px 0}
.fld>span{display:flex;justify-content:space-between;font-size:10.5px;text-transform:uppercase;
letter-spacing:.8px;color:#93a9ab;margin-bottom:4px}
.fld>span b{color:#ffd79a;text-transform:none;letter-spacing:0}
select,input[type=range]{width:100%}
select{background:rgba(255,255,255,.07);color:#e9f2f1;border:1px solid rgba(255,255,255,.14);
border-radius:8px;padding:6px 8px;font-size:12px;font-family:inherit}
input[type=range]{-webkit-appearance:none;height:4px;border-radius:3px;background:rgba(255,255,255,.14);outline:none}
input[type=range]::-webkit-slider-thumb{-webkit-appearance:none;width:14px;height:14px;border-radius:50%;
background:#57e0d0;border:2px solid #0d1a1e;cursor:pointer;box-shadow:0 2px 6px rgba(0,0,0,.5)}
input[type=range]::-moz-range-thumb{width:12px;height:12px;border-radius:50%;background:#57e0d0;border:2px solid #0d1a1e;cursor:pointer}
.btnrow{display:flex;gap:6px;flex-wrap:wrap;margin-top:10px;align-items:center}
button{background:rgba(255,255,255,.08);color:#e9f2f1;border:1px solid rgba(255,255,255,.16);
border-radius:9px;padding:6px 10px;font-size:11.5px;cursor:pointer;font-family:inherit;transition:.15s}
button:hover{background:rgba(87,224,208,.18);border-color:rgba(87,224,208,.45)}
button.on{background:rgba(255,180,84,.2);border-color:rgba(255,180,84,.5)}
.chk{display:flex;align-items:center;gap:5px;font-size:11px;color:#9fb3b4}
.logline{display:flex;justify-content:space-between;gap:8px;font-size:11px;padding:3px 0;
border-bottom:1px dashed rgba(255,255,255,.07);font-variant-numeric:tabular-nums}
.logline span:last-child{color:#ffb0a0}
.logline.good span:last-child{color:#9ce6a2}
.hint{margin-top:8px;font-size:10px;color:#6f8587;letter-spacing:.3px}
#p-stats{left:14px;top:14px;width:296px}
#p-ent{left:14px;bottom:14px;width:296px;max-height:44vh;overflow:auto}
#p-ctl{right:14px;top:14px;width:282px}
#p-log{right:14px;bottom:14px;width:282px}
#p-ent::-webkit-scrollbar{width:6px}
#p-ent::-webkit-scrollbar-thumb{background:rgba(255,255,255,.15);border-radius:3px}
@media (max-width:980px){
#p-ent{display:none}
.panel{font-size:11px}
#p-stats,#p-ctl{width:238px}
#p-log{display:none}
}
@media (max-width:620px){
#p-ctl{display:none}
#p-stats{width:200px}
}
</style>
</head>
<body>
<canvas id="scene"></canvas>
<div class="vignette"></div>
<div class="panel" id="p-stats">
<div class="head"><h1>VOXEL OASIS</h1><span class="sub">day <b id="day">1</b></span></div>
<div class="row big"><span>Storage remaining</span><b id="storageTxt">64 / 120 m³</b></div>
<div class="bar"><i id="storageBar" style="width:53%"></i></div>
<div class="grid2">
<div>
<div class="lbl">Households</div>
<div class="bar sm"><i class="hh" id="hhBar" style="width:0%"></i></div>
<div class="val" id="hhTxt">0 / 0 served</div>
</div>
<div>
<div class="lbl">Crops</div>
<div class="bar sm"><i class="cr" id="cropBar" style="width:0%"></i></div>
<div class="val" id="cropTxt">0 / 0 served</div>
</div>
</div>
<div class="row"><span class="muted">Unmet demand</span><b id="unmetTxt" style="color:#ff8a6b">0 m³</b></div>
<div class="row"><span class="muted">Inflow · pump cap</span><b id="flowTxt">24 · 48</b></div>
<div class="row"><span class="muted">Food stock · harvests</span><b id="foodTxt">8 · 0</b></div>
</div>
<div class="panel" id="p-ent">
<div class="head"><h2>Households</h2><span class="muted">served / demand</span></div>
<div id="hhList"></div>
<div class="head" style="margin-top:12px"><h2>Fields</h2><span class="muted">served / demand</span></div>
<div id="fieldList"></div>
</div>
<div class="panel" id="p-ctl">
<div class="head"><h2>Allocation rules</h2></div>
<label class="fld"><span>Policy</span>
<select id="policy">
<option value="proportional">Proportional to demand</option>
<option value="households">Households first</option>
<option value="crops">Crops first</option>
<option value="equal">Equal shares</option>
</select>
</label>
<label class="fld"><span>Evaporation heat <b id="heatV">1.0×</b></span>
<input id="heat" type="range" min="0.6" max="1.8" step="0.05" value="1"></label>
<label class="fld"><span>Aquifer inflow <b id="inflowV">24</b></span>
<input id="inflow" type="range" min="4" max="48" step="1" value="24"></label>
<label class="fld"><span>Pump capacity <b id="pumpV">48</b></span>
<input id="pump" type="range" min="10" max="110" step="1" value="48"></label>
<label class="fld"><span>Network leakage <b id="leakV">8%</b></span>
<input id="leak" type="range" min="0" max="30" step="1" value="8"></label>
<div class="btnrow">
<button id="play">⏸ Pause</button>
<button id="rain">🌧 Storm +40 m³</button>
<button id="drought">Drought: off</button>
</div>
<div class="btnrow">
<button id="storms">Storms: off</button>
<button id="reset">Reset sim</button>
<button id="cam">Reset camera</button>
</div>
</div>
<div class="panel" id="p-log">
<div class="head"><h2>Allocation log</h2></div>
<div id="log"></div>
<div class="hint">drag = orbit · wheel = zoom · right-drag = pan · click a home or field to focus</div>
</div>
<script type="module">
import * as THREE from 'https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js';
/* =====================================================================
0 · helpers
===================================================================== */
const $ = id => document.getElementById(id);
const clamp = (v,a,b) => v < a ? a : v > b ? b : v;
const lerp = (a,b,t) => a + (b-a)*t;
const rand = (a,b) => a + Math.random()*(b-a);
const inRect = (x,z,x0,x1,z0,z1) => x>=x0 && x<=x1 && z>=z0 && z<=z1;
/* =====================================================================
1 · simulation state
===================================================================== */
const S = {
day: 1, cycle: 0,
storage: 64, capacity: 120,
inflow: 24, pumpCap: 48, leak: 0.08, heat: 1.0,
policy: 'proportional', speed: 1, running: true,
drought: false, storms: false, stormBoost: 0,
food: 8, harvests: 0
};
const households = [
{ name:'Al-Rahi', pop:6, base:4.6, x:-11, z:0 },
{ name:'Banu Nur', pop:4, base:3.4, x:-11, z:7 },
{ name:'Qasr', pop:7, base:5.2, x: 11, z:-4 },
{ name:'Tasnim', pop:5, base:4.0, x: 11, z:4 },
{ name:'Firq', pop:3, base:2.8, x: -4, z:11 },
{ name:'Marah', pop:5, base:4.1, x: 4, z:11 }
];
const rectCells = (x0,x1,z0,z1,cx,cz) => {
const a=[]; for(let x=x0;x<=x1;x++) for(let z=z0;z<=z1;z++) a.push([x-cx,z-cz]); return a;
};
const fields = [
{ name:'Sorghum', base: 9, yield:4, cx:-6.5, cz:-5.5, cells:rectCells(-8,-5,-7,-4,-6.5,-5.5) },
{ name:'Dates', base:11, yield:5, cx: 6.5, cz:-4.5, cells:rectCells( 5, 8,-6,-3, 6.5,-4.5) },
{ name:'Wheat', base: 8, yield:3, cx:-6.5, cz: 4.5, cells:rectCells(-8,-5, 3, 6,-6.5, 4.5) },
{ name:'Tomato', base:12, yield:6, cx: 6.5, cz: 5.5, cells:rectCells( 5, 8, 4, 7, 6.5, 5.5) }
];
households.forEach(h=>{ h.demand=h.base; h.served=0; h.unmet=0; h.buffer=h.cap=5.2; h.sat=1; });
fields.forEach(f=>{ f.demand=f.base; f.served=0; f.unmet=0; f.moisture=0.6; f.growth=0.25; f.plants=[]; });
/* =====================================================================
2 · renderer / scene / camera
===================================================================== */
const canvas = $('scene');
const renderer = new THREE.WebGLRenderer({ canvas, antialias:true, powerPreference:'high-performance' });
renderer.setPixelRatio(Math.min(devicePixelRatio, 2));
renderer.setSize(innerWidth, innerHeight);
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.06;
const scene = new THREE.Scene();
scene.fog = new THREE.Fog(0xe9c48f, 60, 210);
const camera = new THREE.PerspectiveCamera(48, innerWidth/innerHeight, 0.1, 600);
/* --- gradient sky dome --- */
{
const c = document.createElement('canvas'); c.width = 4; c.height = 256;
const g = c.getContext('2d');
const grd = g.createLinearGradient(0,0,0,256);
grd.addColorStop(0.00,'#173d63'); grd.addColorStop(0.38,'#5d8fb0');
grd.addColorStop(0.62,'#e6b273'); grd.addColorStop(0.82,'#f0c98f'); grd.addColorStop(1,'#c89a6a');
g.fillStyle = grd; g.fillRect(0,0,4,256);
const tex = new THREE.CanvasTexture(c); tex.colorSpace = THREE.SRGBColorSpace;
const sky = new THREE.Mesh(new THREE.SphereGeometry(300, 24, 16),
new THREE.MeshBasicMaterial({ map:tex, side:THREE.BackSide, fog:false, depthWrite:false }));
scene.add(sky);
const sun = new THREE.Mesh(new THREE.BoxGeometry(16,16,16),
new THREE.MeshBasicMaterial({ color:0xfff0c8, fog:false }));
sun.position.set(-120, 62, -150); scene.add(sun);
}
scene.add(new THREE.HemisphereLight(0xffe9c4, 0x7a5c38, 0.85));
const sunL = new THREE.DirectionalLight(0xfff1d6, 1.9);
sunL.position.set(38, 46, -26); sunL.castShadow = true;
sunL.shadow.mapSize.set(2048,2048);
sunL.shadow.camera.left=-46; sunL.shadow.camera.right=46;
sunL.shadow.camera.top=46; sunL.shadow.camera.bottom=-46;
sunL.shadow.camera.far=160; sunL.shadow.bias=-0.0012; sunL.shadow.normalBias=0.03;
scene.add(sunL);
const fill = new THREE.DirectionalLight(0x9fc6d8, 0.35); fill.position.set(-30,20,40); scene.add(fill);
/* =====================================================================
3 · voxel helpers
===================================================================== */
const geoCache = new Map();
function boxGeo(w,h,d){
const k = w+'|'+h+'|'+d;
if(!geoCache.has(k)) geoCache.set(k, new THREE.BoxGeometry(w,h,d));
return geoCache.get(k);
}
const matCache = new Map();
function mat(color, opts={}){
const k = color + JSON.stringify(opts);
if(!matCache.has(k)) matCache.set(k, new THREE.MeshStandardMaterial(Object.assign({ color, roughness:.85, metalness:0 }, opts)));
return matCache.get(k);
}
function box(w,h,d,color,x=0,y=0,z=0,opts){
const m = new THREE.Mesh(boxGeo(w,h,d), typeof color==='object' ? color : mat(color, opts));
m.position.set(x,y,z); m.castShadow = true; m.receiveShadow = true; return m;
}
const pickables = [];
/* =====================================================================
4 · terrain (voxel columns)
===================================================================== */
const GRID = 29, HALF = 14;
const LAKE = {x0:-4,x1:4,z0:-11,z1:-3}; // lake surface 9×9 at (0,-7)
const CANAL= {x0:-1,x1:1,z0:-2, z1:0}; // canal 3×3 at (0,-1)
const TANK = {x0:-3,x1:3,z0:1, z1:7}; // cistern 7×7 at (0,4)
const LAKE_C = {cx:0, cz:-7}, CANAL_C = {cx:0, cz:-1}, TANK_C = {cx:0, cz:4};
const pads = [];
households.forEach(h=>pads.push({x:h.x,z:h.z,r:2.4}));
fields.forEach(f=>pads.push({x0:f.cx-2.5,x1:f.cx+2.5,z0:f.cz-2.5,z1:f.cz+2.5,r:99}));
const heights = new Int16Array(GRID*GRID);
for(let j=0;j<GRID;j++){
for(let i=0;i<GRID;i++){
const x=i-HALF, z=j-HALF;
let h;
if(inRect(x,z,LAKE.x0,LAKE.x1,LAKE.z0,LAKE.z1)) h=0;
else if(inRect(x,z,CANAL.x0,CANAL.x1,CANAL.z0,CANAL.z1)) h=0;
else if(inRect(x,z,TANK.x0,TANK.x1,TANK.z0,TANK.z1)) h=1;
else if(inRect(x,z,-5,5,-12,-2) || inRect(x,z,-2,2,-3,1)) h=2; // shores
else if(pads.some(p=> p.r!==99 ? Math.hypot(x-p.x,z-p.z)<=p.r
: inRect(x,z,p.x0,p.x1,p.z0,p.z1))) h=2;
else {
const d = Math.hypot(x,z);
h = 2 + 0.95*Math.sin(x*0.33+1.2)*Math.cos(z*0.29)
+ 0.55*Math.sin((x+z)*0.19+0.7) + Math.max(0,d-10)*0.34;
h = clamp(Math.round(h),1,5);
}
heights[j*GRID+i]=h;
}
}
const heightAt = (x,z) => heights[clamp(Math.round(z+HALF),0,GRID-1)*GRID + clamp(Math.round(x+HALF),0,GRID-1)];
/* build instanced sand columns */
{
const cells=[];
for(let j=0;j<GRID;j++) for(let i=0;i<GRID;i++){
const h=heights[j*GRID+i]; if(h<=0) continue; cells.push([i-HALF,h,j-HALF]);
}
const im = new THREE.InstancedMesh(boxGeo(1,1,1), mat(0xffffff), cells.length);
im.castShadow = true; im.receiveShadow = true;
const m = new THREE.Matrix4(), q = new THREE.Quaternion(), s = new THREE.Vector3(), p = new THREE.Vector3();
const c = new THREE.Color();
const palette = [0xa9814e,0xbe9760,0xd0ac72,0xe2c389,0xefd7a6];
cells.forEach((cell,k)=>{
const [x,h,z]=cell;
p.set(x,h/2,z); s.set(1,h,1); m.compose(p,q,s); im.setMatrixAt(k,m);
const n = (Math.sin(x*12.9898+z*78.233)*43758.5453)%1;
c.setHex(palette[clamp(h-1+Math.round(n*1.2),0,4)]);
c.offsetHSL(0,0,(n-0.5)*0.035);
im.setColorAt(k,c);
});
im.instanceMatrix.needsUpdate = true; if(im.instanceColor) im.instanceColor.needsUpdate = true;
scene.add(im);
}
const ground = new THREE.Mesh(new THREE.PlaneGeometry(400,400), mat(0xc09a66));
ground.rotation.x = -Math.PI/2; ground.position.y = -0.06; ground.receiveShadow = true; scene.add(ground);
/* lake bed + water */
const lakeBed = new THREE.Mesh(new THREE.PlaneGeometry(9,9), mat(0x4e6f5c));
lakeBed.rotation.x=-Math.PI/2; lakeBed.position.set(LAKE_C.cx,0.02,LAKE_C.cz); scene.add(lakeBed);
const lakeGeo = new THREE.PlaneGeometry(8.9,8.9,20,20); lakeGeo.rotateX(-Math.PI/2);
const lakeWater = new THREE.Mesh(lakeGeo, new THREE.MeshStandardMaterial({
color:0x2aa6c6, transparent:true, opacity:0.85, roughness:0.14, metalness:0.05 }));
lakeWater.position.set(LAKE_C.cx,0.5,LAKE_C.cz); scene.add(lakeWater);
const lakeBase = Float32Array.from(lakeGeo.attributes.position.array);
const canalBed = new THREE.Mesh(new THREE.PlaneGeometry(3,3), mat(0x4e6f5c));
canalBed.rotation.x=-Math.PI/2; canalBed.position.set(CANAL_C.cx,0.02,CANAL_C.cz); scene.add(canalBed);
const canalWater = new THREE.Mesh(new THREE.PlaneGeometry(2.9,2.9),
new THREE.MeshStandardMaterial({color:0x2aa6c6,transparent:true,opacity:0.85,roughness:0.16}));
canalWater.rotation.x=-Math.PI/2; canalWater.position.set(CANAL_C.cx,0.5,CANAL_C.cz); scene.add(canalWater);
/* =====================================================================
5 · cistern (storage) + pump
===================================================================== */
const tankGroup = new THREE.Group(); scene.add(tankGroup);
{
const stone = 0xd9b383, stoneD = 0xb98d5c;
for(let x=TANK.x0;x<=TANK.x1;x++) for(let z=TANK.z0;z<=TANK.z1;z++){
const edge = (x===TANK.x0||x===TANK.x1||z===TANK.z0||z===TANK.z1);
if(!edge) continue;
for(let k=0;k<3;k++) tankGroup.add(box(1,1,1, k===0?stoneD:stone, x,1.5+k,z));
}
}
const tankWater = new THREE.Mesh(new THREE.BoxGeometry(4.9,1,4.9),
new THREE.MeshStandardMaterial({color:0x35b9cf,transparent:true,opacity:0.8,roughness:0.12}));
tankWater.position.set(TANK_C.cx,1.2,TANK_C.cz); scene.add(tankWater);
/* pump + rotor at the canal end */
const rotor = new THREE.Group(); rotor.position.set(0,2.6,0.8); scene.add(rotor);
{
const g = new THREE.Group(); g.position.set(0,0,0);
g.add(box(2.2,0.35,0.35,0x7b6a52, 0,1.0,0));
g.add(box(0.35,2.4,0.35,0x7b6a52, -1.0,0.6,0));
g.add(box(0.35,2.4,0.35,0x7b6a52, 1.0,0.6,0));
g.add(box(0.35,0.35,0.9,0x5e5140, 0,0.6,0));
g.position.set(0,1.0,0.55);
scene.add(g);
}
rotor.add(box(1.9,0.22,0.22,0xd9a24a, 0,0,0));
rotor.add(box(0.22,1.9,0.22,0xd9a24a, 0,0,0));
const spout = new THREE.Vector3(0, 3.4, 0.9);
/* =====================================================================
6 · labels (canvas sprites)
===================================================================== */
function roundRect(ctx,x,y,w,h,r){
ctx.beginPath();
ctx.moveTo(x+r,y); ctx.arcTo(x+w,y,x+w,y+h,r); ctx.arcTo(x+w,y+h,x,y+h,r);
ctx.arcTo(x,y+h,x,y,r); ctx.arcTo(x,y,x+w,y,r); ctx.closePath();
}
function makeLabel(){
const c = document.createElement('canvas'); c.width=256; c.height=72;
const ctx = c.getContext('2d');
const tex = new THREE.CanvasTexture(c); tex.colorSpace = THREE.SRGBColorSpace;
const spr = new THREE.Sprite(new THREE.SpriteMaterial({map:tex, transparent:true, depthTest:false, depthWrite:false}));
spr.scale.set(3.3,0.93,1); spr.renderOrder = 20;
return {spr, ctx, tex};
}
function drawLabel(L, name, unmet, good){
const ctx = L.ctx; ctx.clearRect(0,0,256,72);
ctx.fillStyle='rgba(8,16,20,.82)'; roundRect(ctx,3,9,250,54,13); ctx.fill();
ctx.strokeStyle = good ? 'rgba(125,220,134,.55)' : 'rgba(255,138,107,.55)';
ctx.lineWidth=2; ctx.stroke();
ctx.font='600 25px ui-sans-serif, system-ui, sans-serif'; ctx.textBaseline='middle'; ctx.textAlign='left';
ctx.fillStyle='#e9f2f1'; ctx.fillText(name, 17, 37);
ctx.textAlign='right';
ctx.fillStyle = good ? '#8fe39a' : '#ff8a6b';
ctx.fillText(good ? '✓' : '▲'+unmet.toFixed(1), 239, 37);
ctx.textAlign='left';
L.tex.needsUpdate = true;
}
/* =====================================================================
7 · houses & fields
===================================================================== */
function buildHouse(h){
const g = new THREE.Group(); g.position.set(h.x, heightAt(h.x,h.z), h.z);
const adobe = 0xe6bd8d, adobe2 = 0xd5a97c, roof = 0x9d5c49, roof2 = 0xb26b53, trim = 0x6a4830;
g.add(box(2.2,1.4,2.2, adobe, 0,0.7,0));
g.add(box(2.36,0.18,2.36, adobe2, 0,1.49,0));
g.add(box(1.85,0.34,1.85, roof, 0,1.75,0));
g.add(box(1.3,0.3,1.3, roof2, 0,2.05,0));
g.add(box(0.75,0.26,0.75, roof, 0,2.3,0));
g.add(box(0.55,0.95,0.12, trim, 0,0.48,1.13));
g.add(box(0.45,0.4,0.12, 0x2f5560, -0.72,0.98,1.13));
g.add(box(0.45,0.4,0.12, 0x2f5560, 0.72,0.98,1.13));
g.add(box(0.16,0.16,0.16, 0xffd27a, 0,2.5,0));
/* household cistern */
const cis = new THREE.Group(); cis.position.set(1.85,0,0.5); g.add(cis);
cis.add(box(1.3,0.24,1.3, 0xbe9a6e, 0,0.12,0));
cis.add(box(1.3,0.46,0.2, 0xd0ab7c, 0,0.47,0.55));
cis.add(box(1.3,0.46,0.2, 0xd0ab7c, 0,0.47,-0.55));
cis.add(box(0.2,0.46,1.3, 0xc6a172, 0.55,0.47,0));
cis.add(box(0.2,0.46,1.3, 0xc6a172,-0.55,0.47,0));
const cw = new THREE.Mesh(new THREE.BoxGeometry(0.92,1,0.92),
new THREE.MeshStandardMaterial({color:0x46c6dc,transparent:true,opacity:0.85,roughness:0.15}));
cw.position.set(0,0.24,0); cw.scale.y = 0.4; cis.add(cw);
h.cistern = cw;
/* unmet indicator */
const ind = new THREE.Mesh(new THREE.BoxGeometry(0.42,0.42,0.42),
new THREE.MeshStandardMaterial({color:0xff5a4d, emissive:0xff2e1f, emissiveIntensity:0.7}));
ind.position.set(0,3.1,0); ind.visible=false; g.add(ind); h.indicator = ind;
const L = makeLabel(); L.spr.position.set(0,4.1,0); g.add(L.spr); h.label = L;
g.userData.entity = h; pickables.push(g);
scene.add(g); h.group = g;
}
households.forEach(buildHouse);
function buildField(f){
const g = new THREE.Group(); g.position.set(f.cx, heightAt(f.cx,f.cz), f.cz);
f.cells.forEach(([lx,lz])=>{
g.add(box(0.96,0.22,0.96, 0x6d4b30, lx,0.11,lz));
const stalk = box(0.16,1,0.16, 0x8a9b45, lx,0.5,lz); stalk.scale.y=0.35;
const leaf = box(0.66,0.16,0.66, 0x5f9440, lx,0.6,lz);
g.add(stalk); g.add(leaf);
f.plants.push({stalk, leaf, lx, lz, k:0.75+Math.random()*0.5, ph:Math.random()*6.28});
});
/* fence posts */
for(let i=0;i<=4;i++){
g.add(box(0.16,0.5,0.16, 0x9c7c52, -2.1,0.25,-2.1+i));
g.add(box(0.16,0.5,0.16, 0x9c7c52, 2.1,0.25,-2.1+i));
g.add(box(0.16,0.5,0.16, 0x9c7c52, -2.1+i,0.25,-2.1));
g.add(box(0.16,0.5,0.16, 0x9c7c52, -2.1+i,0.25, 2.1));
}
const ind = new THREE.Mesh(new THREE.BoxGeometry(0.42,0.42,0.42),
new THREE.MeshStandardMaterial({color:0xff5a4d, emissive:0xff2e1f, emissiveIntensity:0.7}));
ind.position.set(0,2.3,0); ind.visible=false; g.add(ind); f.indicator = ind;
const L = makeLabel(); L.spr.position.set(0,3.1,0); g.add(L.spr); f.label = L;
g.userData.entity = f; pickables.push(g);
scene.add(g); f.group = g;
}
fields.forEach(buildField);
/* =====================================================================
8 · decor: palms, cacti, rocks
===================================================================== */
function palm(x,z){
const g = new THREE.Group(); g.position.set(x, heightAt(x,z), z);
const trunk = mat(0x8a6840);
let tx=0,tz=0;
for(let s=0;s<5;s++){
tx += Math.sin(s*0.7)*0.1; tz += Math.cos(s*0.7)*0.1;
g.add(box(0.34,0.66,0.34, trunk, tx,0.33+s*0.6,tz));
}
const top = new THREE.Group(); top.position.set(tx,3.05,tz); g.add(top);
for(let k=0;k<7;k++){
const leaf = box(1.9,0.13,0.4, mat(k%2?0x4f8f3c:0x5c9c42), 0,0,0);
leaf.rotation.y = k*Math.PI*2/7;
leaf.position.set(Math.cos(k*Math.PI*2/7)*0.82,-0.12,Math.sin(k*Math.PI*2/7)*0.82);
leaf.rotation.z = -0.3; top.add(leaf);
}
scene.add(g);
}
function cactus(x,z){
const g = new THREE.Group(); g.position.set(x, heightAt(x,z), z);
const m = mat(0x3f7d4a);
g.add(box(0.5,1.8,0.5, m, 0,0.9,0));
g.add(box(0.32,0.75,0.32, m, 0.42,1.05,0));
g.add(box(0.3,0.35,0.3, m, 0.55,1.4,0));
g.add(box(0.32,0.6,0.32, m, -0.4,0.85,0));
g.add(box(0.3,0.3,0.3, m, -0.52,1.15,0));
scene.add(g);
}
function rock(x,z,s){
const m = new THREE.Mesh(boxGeo(s,s*0.7,s*0.85), mat(0xa79a86));
m.position.set(x, heightAt(x,z)+s*0.32, z);
m.rotation.y = Math.random()*3.14; m.castShadow=true; m.receiveShadow=true; scene.add(m);
}
[[-6,-9],[6,-10],[6,-5],[-6,-4],[-8,-12],[8,-12],[-10,-8],[10,-9],[-13,3],[13,1],[-8,10],[9,11]].forEach(p=>palm(p[0],p[1]));
for(let i=0;i<40;i++){
const x = Math.round(rand(-13,13)), z = Math.round(rand(-13,13));
if(inRect(x,z,LAKE.x0-1,LAKE.x1+1,LAKE.z0-1,LAKE.z1+1)) continue;
if(inRect(x,z,CANAL.x0-1,CANAL.x1+1,CANAL.z0-1,CANAL.z1+1)) continue;
if(inRect(x,z,TANK.x0-1,TANK.x1+1,TANK.z0-1,TANK.z1+1)) continue;
if(households.some(h=>Math.hypot(x-h.x,z-h.z)<3.4)) continue;
if(fields.some(f=>Math.abs(x-f.cx)<3.6&&Math.abs(z-f.cz)<3.6)) continue;
if(Math.random()<0.45) cactus(x,z); else rock(x,z, rand(0.5,1.4));
}
/* =====================================================================
9 · water droplets (delivery animation)
===================================================================== */
const dropGeo = new THREE.BoxGeometry(0.18,0.18,0.18);
const dropMat = new THREE.MeshStandardMaterial({ color:0x8ef0ff, emissive:0x2ea8c8, emissiveIntensity:0.7, roughness:0.2 });
const drops = [];
for(let i=0;i<170;i++){ const m = new THREE.Mesh(dropGeo, dropMat); m.visible=false; scene.add(m);
drops.push({m, on:false, t:0, dur:1, from:new THREE.Vector3(), to:new THREE.Vector3(), arc:1.6}); }
function spawnDrops(target, amount){
const n = clamp(Math.round(amount*1.4),1,26);
for(let i=0;i<n;i++){
const d = drops.find(p=>!p.on); if(!d) return;
d.on=true; d.t=0; d.dur=rand(0.8,1.7); d.arc=rand(1.0,2.4);
d.from.copy(spout); d.to.copy(target);
d.to.y += rand(0.6,1.6);
d.m.visible=true;
}
}
/* =====================================================================
10 · allocation model
===================================================================== */
function allocate(budget, needH, needC, policy){
let aH=0, aC=0;
if(policy==='households'){ aH=Math.min(needH,budget); aC=Math.min(needC,budget-aH); }
else if(policy==='crops'){ aC=Math.min(needC,budget); aH=Math.min(needH,budget-aC); }
else if(policy==='equal'){
aH=Math.min(needH,budget*0.5); aC=Math.min(needC,budget*0.5);
const left=budget-aH-aC;
const addH=Math.min(needH-aH,left); aH+=addH;
aC+=Math.min(needC-aC,left-addH);
} else {
const total=needH+needC;
const share = total>0 ? budget*needH/total : 0;
aH=Math.min(needH,share); aC=Math.min(needC,budget-aH);
}
return [aH,aC];
}
function runCycle(){
/* recharge */
const inflow = S.inflow*(S.drought?0.32:1) + S.stormBoost; S.stormBoost=0;
S.storage = Math.min(S.capacity, S.storage + inflow);
/* demands (with backlog + heat + food effect) */
const foodFactor = S.food>0 ? 0.78 : 1;
let needH=0, needC=0;
households.forEach(h=>{
const backlog = 1 + Math.min(0.55,(h.unmet/Math.max(h.base,.1))*0.55);
h.demand = h.base*S.heat*foodFactor*backlog; needH+=h.demand;
});
fields.forEach(f=>{
const backlog = 1 + Math.min(0.45,(f.unmet/Math.max(f.base,.1))*0.45);
f.demand = f.base*S.heat*backlog; needC+=f.demand;
});
/* budget limited by tank stock and pump capacity */
const budget = Math.min(S.storage, S.pumpCap);
const [aH,aC] = allocate(budget, needH, needC, S.policy);
const deliveredH = aH*(1-S.leak), deliveredC = aC*(1-S.leak);
S.storage = Math.max(0, S.storage - (aH+aC));
/* distribute inside each sector proportionally to demand */
households.forEach(h=>{
const served = needH>0 ? Math.min(h.demand, aH*h.demand/needH) : 0;
const got = served*(1-S.leak);
h.served=served; h.unmet=Math.max(0,h.demand-served);
h.buffer = clamp(h.buffer + got - h.pop*0.18, 0, h.cap);
h.sat = h.buffer/h.cap;
spawnDrops(new THREE.Vector3(h.x+1.85, heightAt(h.x,h.z)+0.6, h.z+0.5), got);
});
fields.forEach(f=>{
const served = needC>0 ? Math.min(f.demand, aC*f.demand/needC) : 0;
const got = served*(1-S.leak);
f.served=served; f.unmet=Math.max(0,f.demand-served);
const ratio = f.demand>0 ? got/f.demand : 1;
f.moisture = clamp(f.moisture + ratio*0.85 - 0.42, 0, 1);
if(f.moisture>0.55) f.growth += 0.17; else f.growth -= 0.11;
if(f.growth>=1){ f.growth=0.12; S.harvests++; S.food += f.yield; }
f.growth = clamp(f.growth,0,1);
spawnDrops(new THREE.Vector3(f.cx, heightAt(f.cx,f.cz)+0.6, f.cz), got);
});
/* food consumption */
const eat = Math.min(S.food, households.length*0.45);
S.food = Math.max(0, S.food-eat);
S.day++; S.cycle++;
if(S.storms && S.cycle%6===0) S.stormBoost += 32;
logCycle(deliveredH, deliveredC, needH+needC);
syncUI();
}
const logLines = [];
function logCycle(dh, dc, need){
const unmet = need-(dh+dc);
logLines.unshift({ day:S.day, s:(dh+dc).toFixed(1), unmet:unmet.toFixed(1), sto:S.storage.toFixed(1) });
if(logLines.length>9) logLines.pop();
$('log').innerHTML = logLines.map(l=>
`<div class="logline ${l.unmet<0.05?'good':''}"><span>day ${l.day} · ${l.s} m³</span><span>unmet ${l.unmet} · tank ${l.sto}</span></div>`).join('');
}
/* =====================================================================
11 · UI wiring
===================================================================== */
const entRows = new Map();
function buildRows(list, el){
el.innerHTML = list.map(e=>`
<div class="ent">
<div class="entName">${e.name} <span class="muted">${e.pop?e.pop+'p':''}</span></div>
<div class="entVal">0 / 0</div>
<div class="bar xs"><i style="width:0%"></i></div>
</div>`).join('');
list.forEach((e,k)=>{
const row = el.children[k];
entRows.set(e, { bar:row.querySelector('i'), val:row.querySelector('.entVal'), row });
});
}
buildRows(households, $('hhList'));
buildRows(fields, $('fieldList'));
function syncUI(){
$('day').textContent = S.day;
const pct = S.storage/S.capacity*100;
$('storageTxt').textContent = `${S.storage.toFixed(1)} / ${S.capacity} m³`;
$('storageBar').style.width = pct.toFixed(1)+'%';
$('storageBar').style.background = pct<18
? 'linear-gradient(90deg,#c8452f,#ff8a6b)' : 'linear-gradient(90deg,#2f9fd8,#57e0d0)';
let sd=0, su=0, cd=0, cu=0;
households.forEach(h=>{ sd+=h.served; su+=h.unmet; });
fields.forEach(f=>{ cd+=f.served; cu+=f.unmet; });
const hD = sd+su, cD = cd+cu;
$('hhBar').style.width = hD? (sd/hD*100).toFixed(0)+'%':'0%';
$('cropBar').style.width = cD? (cd/cD*100).toFixed(0)+'%':'0%';
$('hhTxt').textContent = `${sd.toFixed(1)} / ${hD.toFixed(1)} served`;
$('cropTxt').textContent = `${cd.toFixed(1)} / ${cD.toFixed(1)} served`;
$('unmetTxt').textContent = `${(su+cu).toFixed(1)} m³ (H ${su.toFixed(1)} · C ${cu.toFixed(1)})`;
$('flowTxt').textContent = `${(S.inflow*(S.drought?0.32:1)).toFixed(0)} · ${S.pumpCap}`;
$('foodTxt').textContent = `${S.food.toFixed(0)} · ${S.harvests}`;
[...households, ...fields].forEach(e=>{
const r = entRows.get(e);
const d = e.demand||1, s = e.served||0, u = e.unmet||0;
r.bar.style.width = clamp(s/d*100,0,100).toFixed(0)+'%';
r.bar.className = u>0.05 ? 'bad' : '';
r.val.textContent = `${s.toFixed(1)}/${d.toFixed(1)}`;
r.val.style.color = u>0.05 ? '#ff8a6b' : '#9ce6a2';
drawLabel(e.label, e.name, u, u<=0.05);
e.indicator.visible = u>0.05;
});
}
const tankLabel = makeLabel(); tankLabel.spr.position.set(TANK_C.cx, 5.2, TANK_C.cz); scene.add(tankLabel.spr);
function updateTankLabel(){ drawLabel(tankLabel, 'CISTERN', 0, true); }
updateTankLabel();
/* controls */
$('policy').onchange = e=>{ S.policy = e.target.value; };
$('heat').oninput = e=>{ S.heat = +e.target.value; $('heatV').textContent = S.heat.toFixed(2)+'×'; };
$('inflow').oninput= e=>{ S.inflow = +e.target.value; $('inflowV').textContent = S.inflow; };
$('pump').oninput = e=>{ S.pumpCap = +e.target.value; $('pumpV').textContent = S.pumpCap; };
$('leak').oninput = e=>{ S.leak = +e.target.value/100; $('leakV').textContent = S.leak*100+'%'; };
$('play').onclick = ()=>{ S.running=!S.running; $('play').textContent = S.running?'⏸ Pause':'▶ Run'; };
$('rain').onclick = ()=>{ S.stormBoost += 40; flash('Storm: +40 m³ to the aquifer'); };
$('drought').onclick=()=>{ S.drought=!S.drought; $('drought').textContent='Drought: '+(S.drought?'on':'off');
$('drought').classList.toggle('on',S.drought); };
$('storms').onclick=()=>{ S.storms=!S.storms; $('storms').textContent='Storms: '+(S.storms?'on':'off');
$('storms').classList.toggle('on',S.storms); };
$('reset').onclick =()=>{ resetSim(); };
$('cam').onclick =()=>{ camGoal.theta=0.75; camGoal.phi=1.0; camGoal.radius=52; camGoal.tx=0; camGoal.tz=-1; };
function flash(msg){
const d = document.createElement('div');
d.textContent = msg;
d.style.cssText='position:fixed;left:50%;top:18px;transform:translateX(-50%);background:rgba(10,18,22,.9);'+
'border:1px solid rgba(87,224,208,.45);color:#dff7f4;padding:8px 16px;border-radius:10px;font-size:12px;z-index:9';
document.body.appendChild(d);
setTimeout(()=>{ d.style.transition='opacity .6s'; d.style.opacity='0'; setTimeout(()=>d.remove(),700); },1400);
}
function resetSim(){
S.day=1; S.cycle=0; S.storage=64; S.inflow=24; S.pumpCap=48; S.leak=0.08; S.heat=1;
S.policy='proportional'; S.running=true; S.drought=false; S.storms=false; S.stormBoost=0; S.food=8; S.harvests=0;
households.forEach(h=>{ h.demand=h.base; h.served=0; h.unmet=0; h.buffer=h.cap=5.2; h.sat=1; });
fields.forEach(f=>{ f.demand=f.base; f.served=0; f.unmet=0; f.moisture=0.6; f.growth=0.25; });
logLines.length=0; $('log').innerHTML='';
$('policy').value='proportional'; $('heat').value=1; $('inflow').value=24; $('pump').value=48; $('leak').value=8;
$('heatV').textContent='1.00×'; $('inflowV').textContent='24'; $('pumpV').textContent='48'; $('leakV').textContent='8%';
$('play').textContent='⏸ Pause'; $('drought').textContent='Drought: off'; $('drought').classList.remove('on');
$('storms').textContent='Storms: off'; $('storms').classList.remove('on');
syncUI();
}
/* =====================================================================
12 · camera (hand-rolled orbit / pan / zoom / picking)
===================================================================== */
const camGoal = { theta:0.75, phi:1.0, radius:52, tx:0, tz:-1 };
const camNow = { theta:0.75, phi:1.0, radius:52, tx:0, tz:-1 };
const camTarget = new THREE.Vector3(0,3,-1);
const raycaster = new THREE.Raycaster();
const pointers = new Map();
let pinchDist = 0, pinchMid = {x:0,y:0}, dragDist = 0;
function panBy(dx,dy){
const s = camGoal.radius*0.0016;
const rx = Math.cos(camGoal.theta), rz = -Math.sin(camGoal.theta);
const fx = Math.sin(camGoal.theta), fz = Math.cos(camGoal.theta);
camGoal.tx = clamp(camGoal.tx - rx*dx*s + fx*dy*s, -16, 16);
camGoal.tz = clamp(camGoal.tz - rz*dx*s + fz*dy*s, -16, 16);
}
canvas.addEventListener('pointerdown', e=>{
canvas.setPointerCapture(e.pointerId);
pointers.set(e.pointerId,{x:e.clientX,y:e.clientY,button:e.button});
dragDist = 0; canvas.classList.add('drag');
});
canvas.addEventListener('pointermove', e=>{
const p = pointers.get(e.pointerId); if(!p) return;
const dx = e.clientX-p.x, dy = e.clientY-p.y;
p.x=e.clientX; p.y=e.clientY; dragDist += Math.abs(dx)+Math.abs(dy);
if(pointers.size===1){
if(p.button===2 || e.shiftKey) panBy(dx,dy);
else { camGoal.theta -= dx*0.006; camGoal.phi = clamp(camGoal.phi - dy*0.005, 0.22, 1.46); }
} else if(pointers.size===2){
const [a,b] = [...pointers.values()];
const d = Math.hypot(a.x-b.x, a.y-b.y);
if(pinchDist) camGoal.radius = clamp(camGoal.radius*(pinchDist/d), 12, 95);
pinchDist = d;
const mx=(a.x+b.x)/2, my=(a.y+b.y)/2;
panBy(mx-pinchMid.x, my-pinchMid.y); pinchMid={x:mx,y:my};
}
});
function endPointer(e){
const p = pointers.get(e.pointerId);
pointers.delete(e.pointerId);
if(pointers.size<2) pinchDist=0;
if(pointers.size===0) canvas.classList.remove('drag');
if(p && dragDist<8 && e.type==='pointerup') pick(e);
}
canvas.addEventListener('pointerup', endPointer);
canvas.addEventListener('pointercancel', endPointer);
canvas.addEventListener('contextmenu', e=>e.preventDefault());
canvas.addEventListener('wheel', e=>{
e.preventDefault();
camGoal.radius = clamp(camGoal.radius*Math.exp(e.deltaY*0.0011), 12, 95);
},{passive:false});
function pick(e){
const r = canvas.getBoundingClientRect();
const nd = new THREE.Vector2(((e.clientX-r.left)/r.width)*2-1, -((e.clientY-r.top)/r.height)*2+1);
raycaster.setFromCamera(nd, camera);
const hits = raycaster.intersectObjects(pickables, true);
if(hits.length){
let o = hits[0].object;
while(o && !o.userData.entity) o = o.parent;
if(o && o.userData.entity){
const en = o.userData.entity;
camGoal.tx = en.x !== undefined ? en.x : en.cx;
camGoal.tz = en.z !== undefined ? en.z : en.cz;
camGoal.radius = 22;
}
}
}
/* =====================================================================
13 · animation loop
===================================================================== */
let acc = 0, last = performance.now();
const CYCLE_TIME = 2.6;
renderer.setAnimationLoop(now=>{
const dt = Math.min(0.05,(now-last)/1000); last = now;
/* simulation tick */
if(S.running){
acc += dt*S.speed;
while(acc >= CYCLE_TIME){ acc -= CYCLE_TIME; runCycle(); }
}
/* camera easing */
camNow.theta = lerp(camNow.theta, camGoal.theta, 0.12);
camNow.phi = lerp(camNow.phi, camGoal.phi, 0.12);
camNow.radius= lerp(camNow.radius,camGoal.radius,0.12);
camNow.tx = lerp(camNow.tx, camGoal.tx, 0.12);
camNow.tz = lerp(camNow.tz, camGoal.tz, 0.12);
camTarget.set(camNow.tx, 3, camNow.tz);
camera.position.setFromSpherical(new THREE.Spherical(camNow.radius, camNow.phi, camNow.theta)).add(camTarget);
camera.lookAt(camTarget);
/* lake waves */
const t = now*0.001;
const pos = lakeGeo.attributes.position;
for(let i=0;i<pos.count;i++){
const x = lakeBase[i*3], z = lakeBase[i*3+2];
pos.setY(i, Math.sin(x*0.85+t*1.3)*0.055 + Math.sin(z*0.65-t*0.95)*0.045);
}
pos.needsUpdate = true;
if((renderer.info.render.frame & 3)===0) lakeGeo.computeVertexNormals();
/* pump rotor */
rotor.rotation.z += dt*1.6;
/* cistern water level */
const targetFill = 2.6*(S.storage/S.capacity);
tankWater.scale.y = lerp(tankWater.scale.y, Math.max(0.02,targetFill), 0.12);
tankWater.position.y = 1 + tankWater.scale.y/2;
/* household cisterns + indicators */
households.forEach(h=>{
const fill = clamp(h.buffer/h.cap,0,1)*0.62;
h.cistern.scale.y = lerp(h.cistern.scale.y, Math.max(0.02,fill), 0.1);
h.cistern.position.y = 0.24 + h.cistern.scale.y/2;
if(h.indicator.visible){
const r = clamp(h.unmet/Math.max(h.demand,.1),0,1);
const s = 0.35 + r*0.55 + Math.sin(t*5+h.pop)*0.05;
h.indicator.scale.setScalar(s);
h.indicator.position.y = 3.1 + Math.sin(t*2.4+h.pop)*0.12;
h.indicator.rotation.y += dt*1.1;
}
h.label.spr.position.y = 4.1 + Math.sin(t*1.7+h.pop)*0.07;
});
/* fields: growth, colour, wilt */
fields.forEach(f=>{
const lush = clamp(f.moisture*0.7 + f.growth*0.5, 0, 1);
const dry = new THREE.Color(0xa8813f), wet = new THREE.Color(0x4c9e3c), tmp = new THREE.Color();
tmp.copy(dry).lerp(wet, lush);
f.plants.forEach(p=>{
const target = (0.3 + f.growth*0.95)*p.k;
const sc = lerp(p.stalk.scale.y, target, 0.07);
p.stalk.scale.y = sc;
p.stalk.position.y = 0.22 + sc/2;
p.leaf.position.y = 0.22 + sc*0.95;
p.leaf.scale.setScalar(0.7 + f.growth*0.45);
p.stalk.material.color.copy(tmp);
p.leaf.material.color.copy(tmp).offsetHSL(0,0.04,0);
p.leaf.rotation.y = Math.sin(t*0.7+p.ph)*0.12;
});
if(f.indicator.visible){
const r = clamp(f.unmet/Math.max(f.demand,.1),0,1);
f.indicator.scale.setScalar(0.35 + r*0.55 + Math.sin(t*5)*0.05);
f.indicator.position.y = 2.3 + Math.sin(t*2.4)*0.12;
f.indicator.rotation.y += dt*1.1;
}
f.label.spr.position.y = 3.1 + Math.sin(t*1.7)*0.07;
});
/* droplets */
drops.forEach(d=>{
if(!d.on) return;
d.t += dt/d.dur;
if(d.t>=1){ d.on=false; d.m.visible=false; return; }
const e = d.t;
d.m.position.lerpVectors(d.from,d.to,e);
d.m.position.y += Math.sin(Math.PI*e)*d.arc;
d.m.rotation.x += dt*6; d.m.rotation.y += dt*4;
});
renderer.render(scene, camera);
});
/* =====================================================================
14 · resize + boot
===================================================================== */
addEventListener('resize', ()=>{
camera.aspect = innerWidth/innerHeight; camera.updateProjectionMatrix();
renderer.setSize(innerWidth, innerHeight);
});
syncUI();
</script>
</body>
</html>
```