Prompt specification
Build or customize the Three.js 3D scene "Water-depth feeding-access 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: Water-depth feeding-access model
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/water-depth-feeding-access-model-x0684
Scene Description Prompt:
Please create one self-contained HTML file with Three.js from cdnjs showing flamingo filter feeding. Create a lagoon cross-section with adjustable water level and fixed feeding zones; compare geometric access for several stylized wading birds, labeling all behavior as a simplified habitat illustration. Voxel style.
Working Three.js Starter (water-depth-feeding-access-model-x0684):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width,initial-scale=1" />
<title>Flamingo Filter Feeding — Voxel Lagoon Cross-Section</title>
<style>
:root{
--panel:rgba(10,17,26,.86);
--line:rgba(255,255,255,.13);
--txt:#e9eef2;
--dim:#9fb2bd;
--ok:#5ad06a; --warn:#f2b134; --bad:#f0655a;
}
*{box-sizing:border-box}
html,body{margin:0;height:100%;overflow:hidden;
background:linear-gradient(#08182b 0%,#123246 42%,#2d5f66 78%,#4a7a70 100%);
font:12.5px/1.45 "Inter",ui-sans-serif,system-ui,-apple-system,"Segoe UI",Roboto,sans-serif;
color:var(--txt)}
#scene{position:fixed;inset:0}
canvas{display:block}
.panel{position:fixed;background:var(--panel);border:1px solid var(--line);border-radius:12px;
padding:12px 14px;backdrop-filter:blur(7px);box-shadow:0 10px 30px rgba(0,0,0,.35)}
#left{left:14px;top:14px;width:302px;max-height:calc(100vh - 28px);overflow:auto}
#right{right:14px;top:14px;width:352px;max-height:calc(100vh - 28px);overflow:auto}
#foot{left:50%;bottom:12px;transform:translateX(-50%);padding:6px 14px;font-size:11.5px;color:var(--dim);
white-space:nowrap}
h1{font-size:15.5px;margin:0 0 2px;letter-spacing:.2px}
h2{font-size:11px;text-transform:uppercase;letter-spacing:.14em;color:var(--dim);margin:14px 0 6px;
border-bottom:1px solid var(--line);padding-bottom:4px}
.sub{color:var(--dim);font-size:11px;margin:0 0 8px}
.row{display:flex;align-items:center;gap:8px;margin:7px 0}
.row label{color:var(--dim);min-width:78px}
input[type=range]{flex:1;accent-color:#4fc3f7}
.val{font-variant-numeric:tabular-nums;min-width:76px;text-align:right;font-weight:600}
button{background:rgba(255,255,255,.08);color:var(--txt);border:1px solid var(--line);border-radius:7px;
padding:4px 9px;font:inherit;font-size:11.5px;cursor:pointer}
button:hover{background:rgba(255,255,255,.17)}
button.on{background:#2b7fb8;border-color:#4fc3f7}
.note{font-size:11px;color:var(--dim);border-left:3px solid #f2b134;padding:6px 9px;margin:10px 0;
background:rgba(242,177,52,.08);border-radius:0 7px 7px 0}
.bird{display:grid;grid-template-columns:14px 1fr auto;gap:8px;align-items:center;padding:5px 6px;
border-radius:7px;cursor:pointer;margin:2px 0}
.bird:hover{background:rgba(255,255,255,.07)}
.bird.sel{background:rgba(79,195,247,.16);outline:1px solid rgba(79,195,247,.45)}
.sw{width:12px;height:12px;border-radius:3px;box-shadow:inset 0 0 0 1px rgba(0,0,0,.35)}
.bn{font-weight:600}
.bi{font-style:italic;color:var(--dim);font-size:10.5px}
.bm{color:var(--dim);font-size:10.5px}
.tag{font-size:10px;padding:1px 6px;border-radius:99px;white-space:nowrap;font-weight:700}
.t-ok{background:rgba(90,208,106,.18);color:var(--ok)}
.t-warn{background:rgba(242,177,52,.18);color:var(--warn)}
.t-bad{background:rgba(240,101,90,.18);color:var(--bad)}
table{border-collapse:collapse;width:100%;font-size:11px}
th,td{border:1px solid var(--line);padding:3px 2px;text-align:center}
th{background:rgba(255,255,255,.05);font-weight:600;color:var(--dim)}
td.z{font-weight:700;cursor:pointer}
td.z:hover{background:rgba(79,195,247,.2)}
.cell{cursor:pointer;border-radius:4px;padding:1px 2px;display:block}
.c-ok{background:rgba(90,208,106,.2);color:#bff3c6}
.c-warn{background:rgba(242,177,52,.2);color:#ffe0a3}
.c-bad{background:rgba(240,101,90,.14);color:#e5a49d}
.cap{color:var(--dim);font-size:10.5px;margin-top:6px}
#labels{position:fixed;inset:0;pointer-events:none}
.lbl{position:absolute;transform:translate(-50%,-50%);white-space:nowrap;font-size:10.5px;
padding:2px 7px;border-radius:6px;background:rgba(8,14,22,.72);border:1px solid var(--line);
color:var(--txt);pointer-events:none}
.lbl.zone{background:rgba(8,14,22,.62);font-weight:600}
.lbl.bird{background:rgba(8,14,22,.78)}
.lbl .st{font-weight:700}
.s-ok{color:var(--ok)} .s-warn{color:var(--warn)} .s-bad{color:var(--bad)}
.hint{color:var(--dim);font-size:11px}
kbd{background:rgba(255,255,255,.1);border:1px solid var(--line);border-radius:4px;padding:0 4px;font-size:10px}
</style>
</head>
<body>
<div id="scene"></div>
<div id="labels"></div>
<div class="panel" id="left">
<h1>Flamingo filter feeding — lagoon cross-section</h1>
<p class="sub">Voxel habitat illustration · still water · geometric access only</p>
<h2>Water level</h2>
<div class="row">
<label for="wl">Level</label>
<input id="wl" type="range" min="0" max="0.90" step="0.01" value="0.45" />
<span class="val" id="wlv">0.45 m</span>
</div>
<div class="row">
<button data-lvl="0.10">Low</button>
<button data-lvl="0.45" class="on">Mid</button>
<button data-lvl="0.85">High</button>
<button id="reset">Reset placement</button>
</div>
<p class="cap">Datum 0 = channel bed reference. Zones keep their elevation; only the water surface moves.</p>
<h2>Feeding zones (fixed)</h2>
<div id="zones"></div>
<h2>Bird roster</h2>
<div id="roster"></div>
<div class="note"><b>Simplified habitat illustration.</b> Access is a geometric estimate
(leg depth vs. bill reach vs. still-water depth) for a single bird type — it is
<b>not</b> a behavioural model, and it ignores flow, turbidity, prey density, flocking,
seasonal tide and predation.</div>
</div>
<div class="panel" id="right">
<h2>Geometric access matrix</h2>
<div id="matrix"></div>
<p class="cap">✓ access · ~ marginal (wades but bill cannot work the bottom, or bill cannot be submerged) ·
✗ no access. Cell shows water depth over that zone's floor. Click a cell to place that bird there.</p>
<div id="detail"></div>
</div>
<div class="panel" id="foot">
<span class="hint">drag to orbit · wheel to zoom · shift-drag to pan · click a bird to inspect</span>
| <b>Simplified habitat illustration</b>
</div>
<script type="module">
import * as THREE from 'https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js';
/* ------------------------------------------------------------------ *
* DATA — units are metres; depths are water depth over the zone floor
* ------------------------------------------------------------------ */
const ZONES = [
{id:'Z4', name:'Seagrass channel', x0:-4.0, x1:-2.6, f0:-0.70, f1:-0.50,
sed:'seagrass / gravel', col:0x2f6d55},
{id:'Z3', name:'Shell-hash ridge', x0:-2.6, x1:-1.2, f0:-0.30, f1:-0.20,
sed:'shell gravel', col:0xb08a63},
{id:'Z2', name:'Sand ripple bed', x0:-1.2, x1: 0.4, f0: 0.00, f1: 0.10,
sed:'sand / worms', col:0xd7b169},
{id:'Z1', name:'Upper mudflat', x0: 0.4, x1: 2.0, f0: 0.30, f1: 0.40,
sed:'mud / algae', col:0x6e7a48},
{id:'BK', name:'Dry bank', x0: 2.0, x1: 4.0, f0: 0.80, f1: 1.00,
sed:'reeds / shore', col:0x8e7a53},
];
const zoneById = id => ZONES.find(z=>z.id===id);
const ZONE_ORDER = ['Z4','Z3','Z2','Z1'];
const BIRDS = [
{key:'flamingo', name:'Flamingo', latin:'Phoenicopterus roseus', style:'filter',
leg:0.55, reach:0.45, draft:0.05, billLen:0.15, prefs:['Z1','Z2'],
bodyColor:0xf3a6ba, neckColor:0xf8c6d1, headColor:0xfad6dd, billColor:0xe8c2c9,
legColor:0xe6c8c6, wingColor:0x2f2b2f,
bill:[{dx:0.06,dy:-0.02,th:0.036,color:0xe8c2c9},
{dx:0.02,dy:-0.05,color:0xdfb3bb},
{dx:0.00,dy:-0.05,color:0xdfb3bb},
{dx:-0.03,dy:-0.04,color:0x2b2729},
{dx:-0.01,dy:0.01,color:0x2b2729}],
body:{w:0.42,h:0.25,d:0.22}},
{key:'spoonbill', name:'Spoonbill', latin:'Platalea leucorodia', style:'sweep',
leg:0.40, reach:0.30, draft:0.04, billLen:0.18, prefs:['Z2','Z1'],
bodyColor:0xf2f0ea, neckColor:0xf6f4ef, headColor:0xf7f5f0, billColor:0x2a2a2a,
legColor:0x3f4a3a, wingColor:0xdcd9d2,
bill:[{dx:0.15,dy:-0.01,th:0.030,color:0x242424},
{spoon:true,color:0x242424,dx:0.09}],
body:{w:0.40,h:0.24,d:0.20}},
{key:'avocet', name:'Avocet', latin:'Recurvirostra avosetta', style:'sweep',
leg:0.30, reach:0.22, draft:0.03, billLen:0.16, prefs:['Z1','Z2'],
bodyColor:0xf0eee8, neckColor:0xf3f1ec, headColor:0x2a2a2a, billColor:0x232323,
legColor:0x5d7386, wingColor:0x2a2a2a,
bill:[{dx:0.12,dy:0.00,th:0.028,color:0x232323},
{dx:0.04,dy:0.02,color:0x232323}],
body:{w:0.32,h:0.22,d:0.18}},
{key:'egret', name:'Little egret', latin:'Egretta garzetta', style:'speck',
leg:0.45, reach:0.45, draft:0.02, billLen:0.15, prefs:['Z2','Z3'],
bodyColor:0xf7f6f2, neckColor:0xf9f8f5, headColor:0xfbfaf7, billColor:0xe3c13c,
legColor:0x242424, wingColor:0xe7e6e1,
bill:[{dx:0.14,dy:-0.03,th:0.028,color:0xe3c13c}],
body:{w:0.40,h:0.23,d:0.19}},
{key:'curlew', name:'Curlew', latin:'Numenius arquata', style:'probe',
leg:0.38, reach:0.55, draft:0.00, probeAbove:0.55, billLen:0.18, prefs:['Z2','Z3'],
bodyColor:0xa9824f, neckColor:0xb08b58, headColor:0xc09a66, billColor:0x8a6b3f,
legColor:0x7a6a4c, wingColor:0x8e6c3f,
bill:[{dx:0.10,dy:-0.01,th:0.028,color:0x8a6b3f},
{dx:0.05,dy:-0.035,color:0x7d6038},
{dx:0.02,dy:-0.030,color:0x6a5130}],
body:{w:0.38,h:0.23,d:0.19}},
{key:'heron', name:'Grey heron', latin:'Ardea cinerea', style:'spear',
leg:0.85, reach:0.90, draft:0.05, billLen:0.24, prefs:['Z4','Z3'],
bodyColor:0x9aa7b0, neckColor:0xb4bec6, headColor:0xe9ecee, billColor:0xd9b54f,
legColor:0xa08a52, wingColor:0x7d8a93,
bill:[{dx:0.17,dy:-0.04,th:0.032,color:0xd9b54f},
{dx:0.06,dy:-0.03,color:0xd9b54f}],
body:{w:0.46,h:0.28,d:0.24}},
];
BIRDS.forEach((b,i)=>b.idx=i);
/* ------------------------------------------------------------------ *
* SCENE / RENDERER
* ------------------------------------------------------------------ */
const scene = new THREE.Scene();
const renderer = new THREE.WebGLRenderer({antialias:true, alpha:true});
renderer.setPixelRatio(Math.min(devicePixelRatio,2));
renderer.setSize(innerWidth,innerHeight);
document.getElementById('scene').appendChild(renderer.domElement);
const camera = new THREE.PerspectiveCamera(40, innerWidth/innerHeight, 0.1, 200);
const hemi = new THREE.HemisphereLight(0xbfe4ff, 0x6b5a44, 0.85);
scene.add(hemi);
const sun = new THREE.DirectionalLight(0xfff3d6, 1.15);
sun.position.set(5,8,7); scene.add(sun);
const rim = new THREE.DirectionalLight(0x9fd2ff,0.35); rim.position.set(-6,3,-5); scene.add(rim);
/* --------- voxel helpers --------- */
const unitBox = new THREE.BoxGeometry(1,1,1);
const matCache = new Map();
function mat(c,o={}){const k=c+JSON.stringify(o);
if(!matCache.has(k)) matCache.set(k,new THREE.MeshLambertMaterial(Object.assign({color:c},o)));
return matCache.get(k);}
function boxMesh(w,h,d,c,o){const m=new THREE.Mesh(unitBox,mat(c,o));m.scale.set(w,h,d);return m;}
function hash(n){const s=Math.sin(n*127.1+311.7)*43758.5453;return s-Math.floor(s);}
/* --------- world grid --------- */
const VOX=0.10, X0=-4.0, X1=4.0, Y0=-2.0;
const COLS=Math.round((X1-X0)/VOX);
const ZL=8, ZSTEP=0.20, Z0=-0.80;
let waterLevel=0.45;
function zoneAt(x){for(const z of ZONES){if(x>=z.x0&&x<z.x1)return z;}return ZONES[ZONES.length-1];}
function floorAt(x){
const z=zoneAt(x), t=(x-z.x0)/(z.x1-z.x0);
let f=z.f0+(z.f1-z.f0)*t;
f+=(hash(Math.round((x-X0)*13))*2-1)*0.05;
return Math.round(f/VOX)*VOX;
}
function zoneDepth(z){return waterLevel-(z.f0+z.f1)/2;}
/* --------- terrain (voxel strata) --------- */
const terrainGeo=new THREE.BoxGeometry(VOX,VOX,ZSTEP);
let terrainCount=0;
for(let c=0;c<COLS;c++){const x=X0+(c+0.5)*VOX;const f=floorAt(x);
terrainCount+=ZL*Math.max(1,Math.round((f-Y0)/VOX));}
const terrain=new THREE.InstancedMesh(terrainGeo,mat(0xffffff),terrainCount);
terrain.instanceMatrix.setUsage(THREE.StaticDrawUsage);
scene.add(terrain);
(function buildTerrain(){
let i=0; const m=new THREE.Matrix4(), col=new THREE.Color();
for(let c=0;c<COLS;c++){
const x=X0+(c+0.5)*VOX, f=floorAt(x), z=zoneAt(x);
const rows=Math.max(1,Math.round((f-Y0)/VOX));
for(let l=0;l<ZL;l++){
const zz=Z0+(l+0.5)*ZSTEP;
for(let r=0;r<rows;r++){
const y=Y0+(r+0.5)*VOX;
const below=(rows-1-r)*VOX;
let hex;
if(below<0.001){ hex=z.col; }
else if(below<0.31){ hex=0x7c6244; }
else if(below<0.81){ hex=0x6b5a46; }
else if(below<1.41){ hex=0x5d5449; }
else { hex=0x4a4a52; }
col.setHex(hex);
const j=1+(hash(c*37+l*7+r*3)-0.5)*0.22;
col.multiplyScalar(j);
m.makeTranslation(x,y,zz); terrain.setMatrixAt(i,m); terrain.setColorAt(i,col); i++;
}
}
}
terrain.instanceMatrix.needsUpdate=true; terrain.instanceColor.needsUpdate=true;
})();
/* --------- water (voxel volume + surface) --------- */
const waterGeo=new THREE.BoxGeometry(VOX,1,ZSTEP);
const water=new THREE.InstancedMesh(waterGeo,
mat(0x2f83c4,{transparent:true,opacity:0.44,depthWrite:false}), COLS*ZL);
water.renderOrder=2; scene.add(water);
const surf=new THREE.InstancedMesh(new THREE.BoxGeometry(VOX,0.035,ZSTEP),
mat(0x8fd3ff,{transparent:true,opacity:0.85,depthWrite:false}), COLS*ZL);
surf.renderOrder=3; scene.add(surf);
function updateWater(){
let i=0,s=0; const m=new THREE.Matrix4();
for(let c=0;c<COLS;c++){
const x=X0+(c+0.5)*VOX, f=floorAt(x);
for(let l=0;l<ZL;l++){
const z=Z0+(l+0.5)*ZSTEP;
const h=Math.max(0,waterLevel-f);
m.makeTranslation(x,f+h/2,z); m.scale(new THREE.Vector3(1,h,1));
water.setMatrixAt(i,m);
m.makeTranslation(x,waterLevel,z); m.scale(new THREE.Vector3(1,1,1));
surf.setMatrixAt(s,m); i++; s++;
}
}
water.instanceMatrix.needsUpdate=true; surf.instanceMatrix.needsUpdate=true;
}
/* --------- survey stake (water-level gauge) --------- */
const stake=new THREE.Group(); scene.add(stake);
stake.add(boxMesh(0.045,2.0,0.045,0x6d6a63,0).translateY(-0.2));
for(let y=-0.8;y<=1.0;y+=0.2){
const t=boxMesh(0.10,0.022,0.022,0xded9cd); t.position.set(0.07,y,0); stake.add(t);
}
const wlMark=boxMesh(0.16,0.05,0.16,0xffd34d); wlMark.position.set(0,y0(),0); stake.add(wlMark);
function y0(){return waterLevel;}
stake.position.set(-3.88,floorAt(-3.88),0.55);
/* --------- reeds on the dry bank --------- */
(function reeds(){
const g=new THREE.Group();
for(let i=0;i<26;i++){
const x=2.15+hash(i*3.1)*1.75, z=-0.65+hash(i*7.7)*1.3;
const h=0.28+hash(i*5.3)*0.42, w=0.035+hash(i*2.2)*0.02;
const st=boxMesh(w,h,w, i%3?0x4f7a3a:0x6d8f3f); st.position.set(x,floorAt(x)+h/2,z); g.add(st);
if(i%4===0){const head=boxMesh(0.06,0.14,0.06,0x6b4b2c); head.position.set(x,floorAt(x)+h+0.06,z); g.add(head);}
}
scene.add(g);
})();
/* a couple of rocks in the channel */
[[-3.3,-0.2],[ -2.9,0.35]].forEach((p,i)=>{
const r=boxMesh(0.22+0.05*i,0.13,0.2,0x8a8b90);
r.position.set(p[0],floorAt(p[0])+0.065,p[1]); scene.add(r);
});
/* ------------------------------------------------------------------ *
* ACCESS MODEL (simplified geometric illustration)
* ------------------------------------------------------------------ */
function assess(b,d){
if(d<0){ // exposed substrate
if(b.style==='probe' && -d<=(b.probeAbove||b.reach))
return {code:'ok',reason:'probe',note:'probes exposed mud'};
if(b.style==='probe') return {code:'bad',reason:'exposed',note:'mud exposed, bill out of reach'};
return {code:'bad',reason:'exposed',note:'substrate exposed — bill needs water'};
}
if(d>b.leg) return {code:'bad',reason:'deep',note:'too deep to wade (legs '+b.leg+' m)'};
if(b.style==='filter'||b.style==='sweep'){
if(d>b.reach) return {code:'warn',reason:'reach',note:'bill cannot reach the bed'};
if(d<b.draft) return {code:'warn',reason:'shallow',note:'water too shallow to submerge bill'};
return {code:'ok',reason:'feed',note:'bill works the bed'};
}
if(d>b.reach) return {code:'warn',reason:'reach',note:'bill cannot reach the bed'};
if(d<0.05) return {code:'warn',reason:'shallow',note:'too shallow for a prey-water column'};
return {code:'ok',reason:'feed',note:'can strike / probe prey'};
}
/* ------------------------------------------------------------------ *
* BIRDS
* ------------------------------------------------------------------ */
const birdGroup=new THREE.Group(); scene.add(birdGroup);
const birds=[];
const matTmp=new THREE.Matrix4();
function buildBird(b){
const g=new THREE.Group(); birdGroup.add(g);
const leg=b.leg, H=b.body.h;
/* legs */
for(const s of [-1,1]){
const lx=s*0.09;
const foot=boxMesh(0.085,0.028,0.13,b.legColor); foot.position.set(lx,0.014,0.01); g.add(foot);
const tar=boxMesh(0.028,leg*0.78,0.028,b.legColor); tar.position.set(lx,0.028+leg*0.39,0); g.add(tar);
const th=boxMesh(0.05,leg*0.30,0.05,b.bodyColor); th.position.set(lx,leg*0.85+0.028,0); g.add(th);
}
/* body */
const body=boxMesh(b.body.w,b.body.h,b.body.d,b.bodyColor); body.position.set(0,leg,0); g.add(body);
const breast=boxMesh(b.body.w*0.55,b.body.h*0.72,b.body.d*0.82,b.bodyColor);
breast.position.set(b.body.w*0.30,leg+b.body.h*0.22,0); g.add(breast);
/* tail (stepped voxel) */
const t1=boxMesh(0.10,0.07,0.09,b.wingColor); t1.position.set(-b.body.w*0.52,leg-0.01,0); g.add(t1);
const t2=boxMesh(0.07,0.05,0.06,b.wingColor); t2.position.set(-b.body.w*0.68,leg-0.05,0); g.add(t2);
/* wing patches */
for(const s of [-1,1]){
const w=boxMesh(b.body.w*0.62,b.body.h*0.46,0.03,b.wingColor);
w.position.set(-b.body.w*0.08,leg+0.005,s*(b.body.d*0.5+0.012)); g.add(w);
}
/* head/neck container */
const neck=new THREE.Group(); g.add(neck);
/* status ring */
const ring=boxMesh(0.52,0.022,0.38,0xffffff,{transparent:true,opacity:0.5});
ring.position.y=0.012; g.add(ring);
/* selection ring */
const sel=new THREE.Group();
for(const [dx,dz,w,d] of [[0.28,0,0.04,0.42],[0,0.21,0.56,0.04],[0,-0.21,0.56,0.04]])
sel.add(boxMesh(w,0.02,d,0xffffff,{transparent:true,opacity:0.85}).translateX(dx).translateZ(dz));
sel.position.y=0.02; sel.visible=false; g.add(sel);
const o={g,neck,ring,sel,bodyY:leg}; g.userData=o;
return o;
}
function bez(p0,p1,p2,t){
const u=1-t;
return new THREE.Vector2(u*u*p0.x+2*u*t*p1.x+t*t*p2.x,
u*u*p0.y+2*u*t*p1.y+t*t*p2.y);
}
function poseBird(bird){
const b=bird.b, o=bird.o, neck=o.neck;
while(neck.children.length) neck.remove(neck.children[0]);
const d=bird.depth;
const rest=bird.status.code==='bad';
let tipX,tipY;
if(rest){ tipX=0.30+b.billLen*0.35; tipY=b.leg+0.26; }
else if(bird.status.code==='ok'){ tipX=0.20+b.billLen*0.30; tipY=0.015; }
else if(bird.status.reason==='reach'){ tipX=0.20+b.billLen*0.30; tipY=Math.max(0.04,d-b.reach+0.02); }
else { tipX=0.28; tipY=d+0.05; }
/* bill */
const bill=new THREE.Group(); let cx=0,cy=0;
for(const s of b.bill){
const th=s.th||0.030;
if(s.spoon){
const m=boxMesh(0.075,0.022,0.115,s.color||b.billColor);
m.position.set(cx+0.037,cy,0); bill.add(m); cx+=0.09;
}else{
const w=Math.abs(s.dx)>=Math.abs(s.dy)?Math.abs(s.dx)+th*0.45:th;
const h=Math.abs(s.dy)>=Math.abs(s.dx)?Math.abs(s.dy)+th*0.45:th;
const m=boxMesh(w,h,th,s.color||b.billColor);
m.position.set(cx+s.dx/2,cy+s.dy/2,0); bill.add(m);
cx+=s.dx; cy+=s.dy;
}
}
bill.position.set(tipX-cx,tipY-cy,0); neck.add(bill);
/* head */
const ax=new THREE.Vector2(b.bill[0].dx||0.01,b.bill[0].dy||0).normalize();
const pp=new THREE.Vector2(-ax.y,ax.x);
const headPos=new THREE.Vector2(bill.position.x+pp.x*0.062-ax.x*0.018,
bill.position.y+pp.y*0.062-ax.y*0.018);
const head=boxMesh(0.115,0.105,0.115,b.headColor); head.position.set(headPos.x,headPos.y,0); neck.add(head);
const eye=boxMesh(0.030,0.030,0.020,0x141414);
eye.position.set(headPos.x+ax.x*0.035,headPos.y+ax.y*0.035+0.020,0.052); neck.add(eye);
/* neck curve (S shape) */
const P0=new THREE.Vector2(b.body.w*0.26,b.leg+b.body.h*0.30);
const P2=headPos.clone().add(new THREE.Vector2(-pp.x*0.03,-pp.y*0.03));
const P1=new THREE.Vector2(P0.x+(P2.x-P0.x)*0.35,Math.max(P0.y,P2.y)+(rest?0.16:0.24));
for(let i=0;i<=7;i++){
const p=bez(P0,P1,P2,i/7);
const m=boxMesh(0.088,0.088,0.105,b.neckColor); m.position.set(p.x,p.y,0); neck.add(m);
}
/* status ring colour */
const c=bird.status.code==='ok'?0x5ad06a:(bird.status.code==='warn'?0xf2b134:0xf0655a);
o.ring.material=mat(c,{transparent:true,opacity:0.45});
}
/* placement: pick best accessible zone, avoid overlapping birds */
function assignBirds(){
ZONES.forEach(z=>z.occ=[]);
const used=new Set();
for(const b of BIRDS){
const order=(b.override?[b.override]:b.prefs).filter(z=>z!=='BK');
let pick=null,warnPick=null;
for(const zid of order){
const z=zoneById(zid), st=assess(b,zoneDepth(z));
if(st.code==='ok'){pick={z,st};break;}
if(st.code==='warn'&&!warnPick) warnPick={z,st};
}
pick=pick||warnPick;
if(!pick){ pick={z:zoneById('BK'),st:{code:'bad',reason:'none',note:'no zone within geometric limits'}}; }
const z=pick.z;
let slot=null;
if(z.id!=='BK'){
const cand=[];
for(let k=0;k<4;k++) cand.push(z.x0+0.26+k*0.36);
for(const c of cand){ if(c<z.x1-0.10 && !z.occ.some(o=>Math.abs(o-c)<0.30)){ slot=c; break; } }
if(slot===null){ slot=cand[cand.length-1]; }
z.occ.push(slot);
}else{ slot=2.6+b.idx*0.28; }
let bird=birds.find(x=>x.b===b);
bird.zone=z.id; bird.status=pick.st; bird.x=slot;
const fy=floorAt(slot);
bird.o.g.position.set(slot,fy,0.45);
bird.depth=waterLevel-fy;
bird.baseFloor=fy;
}
}
/* ------------------------------------------------------------------ *
* LABELS
* ------------------------------------------------------------------ */
const labelsEl=document.getElementById('labels');
function mkLabel(cls){const d=document.createElement('div');d.className='lbl '+cls;labelsEl.appendChild(d);return d;}
const zoneLabels={};
for(const z of ZONES) zoneLabels[z.id]=mkLabel('zone');
const birdLabels=BIRDS.map(()=>mkLabel('bird'));
const gaugeLabel=mkLabel('zone');
function project(el,pos,show=true){
const v=pos.clone().project(camera);
if(!show||v.z>1){el.style.display='none';return;}
el.style.display='block';
el.style.left=((v.x*0.5+0.5)*innerWidth)+'px';
el.style.top=((-v.y*0.5+0.5)*innerHeight)+'px';
}
const tmpV=new THREE.Vector3();
/* ------------------------------------------------------------------ *
* UI
* ------------------------------------------------------------------ */
const slider=document.getElementById('wl'), wlv=document.getElementById('wlv');
const rosterEl=document.getElementById('roster'), matrixEl=document.getElementById('matrix');
const zonesEl=document.getElementById('zones'), detailEl=document.getElementById('detail');
let selected=BIRDS[0].key;
function buildZoneList(){
zonesEl.innerHTML='';
for(const id of ZONE_ORDER){
const z=zoneById(id);
const row=document.createElement('div');
row.className='row'; row.style.margin='3px 0';
row.innerHTML=`<span class="sw" style="background:#${z.col.toString(16).padStart(6,'0')}"></span>
<span style="flex:1"><b>${z.name}</b><br><span class="bm">${z.sed} · floor ${z.f0.toFixed(2)}…${z.f1.toFixed(2)} m</span></span>`;
zonesEl.appendChild(row);
}
}
function buildRoster(){
rosterEl.innerHTML='';
BIRDS.forEach(b=>{
const d=document.createElement('div');
d.className='bird'; d.dataset.key=b.key;
d.innerHTML=`<span class="sw" style="background:#${b.bodyColor.toString(16).padStart(6,'0')}"></span>
<span><span class="bn">${b.name}</span> <span class="bi">${b.latin}</span><br>
<span class="bm">legs ${b.leg.toFixed(2)} m · bill reach ${b.reach.toFixed(2)} m · ${b.style}</span></span>
<span class="tag"></span>`;
d.onclick=()=>{selected=b.key;refresh();};
rosterEl.appendChild(d);
});
}
function buildMatrix(){
matrixEl.innerHTML='';
const t=document.createElement('table');
let h='<tr><th style="text-align:left">bird \\ zone</th>';
for(const id of ZONE_ORDER){const z=zoneById(id);
h+=`<th>${z.id}<br><span style="font-weight:400;opacity:.7">${z.name.split(' ')[0]}</span></th>`;}
h+='</tr>';
for(const b of BIRDS){
h+=`<tr><td style="text-align:left"><b>${b.name}</b></td>`;
for(const id of ZONE_ORDER){
const z=zoneById(id), st=assess(b,zoneDepth(z));
const cls=st.code==='ok'?'c-ok':(st.code==='warn'?'c-warn':'c-bad');
const ico=st.code==='ok'?'✓':(st.code==='warn'?'~':'✗');
h+=`<td class="z" data-bird="${b.key}" data-zone="${id}" title="${b.name} @ ${z.name}: ${st.note}">
<span class="cell ${cls}">${ico}<br>${Math.round(zoneDepth(z)*100)}cm</span></td>`;
}
h+='</tr>';
}
t.innerHTML=h+'</table>';
matrixEl.appendChild(t);
matrixEl.querySelectorAll('td.z').forEach(td=>{
td.onclick=()=>{
const b=BIRDS.find(x=>x.key===td.dataset.bird), zid=td.dataset.zone;
const z=zoneById(zid), st=assess(b,zoneDepth(z));
if(st.code==='bad'){
td.title='No geometric access: '+st.note;
td.animate([{transform:'scale(1)'},{transform:'scale(1.12)'},{transform:'scale(1)'}],{duration:260});
return;
}
b.override=zid; selected=b.key; refresh();
};
});
}
function refreshLegend(){
rosterEl.querySelectorAll('.bird').forEach(el=>{
const b=BIRDS.find(x=>x.key===el.dataset.key);
const bird=birds.find(x=>x.b===b);
el.classList.toggle('sel',b.key===selected);
const tag=el.querySelector('.tag');
const code=bird.status.code;
tag.className='tag '+(code==='ok'?'t-ok':(code==='warn'?'t-warn':'t-bad'));
tag.textContent=code==='ok'?'access':(code==='warn'?'marginal':'none');
});
const b=BIRDS.find(x=>x.key===selected), bird=birds.find(x=>x.b===b);
const z=zoneById(bird.zone);
detailEl.innerHTML=`<h2>${b.name} — ${b.latin}</h2>
<div class="bm" style="line-height:1.7">
Feeding style: <b>${b.style}</b> · leg depth <b>${b.leg.toFixed(2)} m</b> ·
bill reach below surface <b>${b.reach.toFixed(2)} m</b>${b.style==='filter'||b.style==='sweep'?
` · bill submersion needed <b>${b.draft.toFixed(2)} m</b>`:''}<br>
Now at: <b>${z.name}</b> · water depth <b>${(bird.depth*100).toFixed(0)} cm</b><br>
Assessment: <b class="s-${bird.status.code}">${bird.status.note}</b>
</div>
<p class="cap">Simplified habitat illustration — geometric estimate only, not a behavioural model.</p>`;
}
function refresh(){
buildMatrix();
refreshLegend();
for(const id of ZONE_ORDER){
const z=zoneById(id), el=zoneLabels[id];
el.innerHTML=`${z.name} <span class="bm">· ${Math.round(zoneDepth(z)*100)} cm</span>`;
}
}
/* ------------------------------------------------------------------ *
* INTERACTION
* ------------------------------------------------------------------ */
slider.addEventListener('input',()=>{waterLevel=parseFloat(slider.value);wlv.textContent=waterLevel.toFixed(2)+' m';
document.querySelectorAll('[data-lvl]').forEach(b=>b.classList.remove('on'));
updateAll();});
document.querySelectorAll('[data-lvl]').forEach(btn=>{
btn.onclick=()=>{waterLevel=parseFloat(btn.dataset.lvl);slider.value=waterLevel;
wlv.textContent=waterLevel.toFixed(2)+' m';
document.querySelectorAll('[data-lvl]').forEach(b=>b.classList.remove('on'));btn.classList.add('on');
updateAll();};
});
document.getElementById('reset').onclick=()=>{BIRDS.forEach(b=>b.override=null);updateAll();};
/* custom camera controls (no addons) */
let az=0, el=0.30, dist=13.6;
const tgt=new THREE.Vector3(0,0.15,0);
let dragging=false,lx=0,ly=0,pan=false;
renderer.domElement.addEventListener('pointerdown',e=>{dragging=true;pan=e.shiftKey||e.button===2;
lx=e.clientX;ly=e.clientY;renderer.domElement.setPointerCapture(e.pointerId);});
renderer.domElement.addEventListener('pointermove',e=>{
if(!dragging)return;const dx=e.clientX-lx,dy=e.clientY-ly;lx=e.clientX;ly=e.clientY;
if(pan){tgt.x-=dx*0.006*Math.cos(az);tgt.z+=dx*0.006*Math.sin(az);tgt.y+=dy*0.006;}
else{az-=dx*0.005;el=Math.max(-0.05,Math.min(0.95,el+dy*0.004));}
});
renderer.domElement.addEventListener('pointerup',()=>{dragging=false;});
renderer.domElement.addEventListener('contextmenu',e=>e.preventDefault());
renderer.domElement.addEventListener('wheel',e=>{e.preventDefault();
dist=Math.max(5.5,Math.min(26,dist*(1+e.deltaY*0.0011)));},{passive:false});
window.addEventListener('resize',()=>{camera.aspect=innerWidth/innerHeight;camera.updateProjectionMatrix();
renderer.setSize(innerWidth,innerHeight);});
/* ------------------------------------------------------------------ *
* BOOT + LOOP
* ------------------------------------------------------------------ */
BIRDS.forEach(b=>{const o=buildBird(b);birds.push({b,o});});
buildZoneList(); buildRoster();
function updateAll(){
updateWater();
assignBirds();
birds.forEach(p=>poseBird(p));
wlMark.position.y=waterLevel;
refresh();
}
updateAll();
const clock=new THREE.Clock();
function animate(){
requestAnimationFrame(animate);
const t=clock.getElapsedTime();
/* camera */
camera.position.set(tgt.x+dist*Math.cos(el)*Math.sin(az),
tgt.y+dist*Math.sin(el),
tgt.z+dist*Math.cos(el)*Math.cos(az));
camera.lookAt(tgt);
/* wave on the front water surface row */
const m=matTmp;
for(let c=0;c<COLS;c++){
const idx=(ZL-1)*COLS+c;
const x=X0+(c+0.5)*VOX;
const y=waterLevel+0.012*Math.sin(t*1.7+x*2.4)+0.008*Math.sin(t*2.9-x*1.3);
m.makeTranslation(x,y,Z0+(ZL-0.5)*ZSTEP); m.scale(new THREE.Vector3(1,1,1));
surf.setMatrixAt(idx,m);
}
surf.instanceMatrix.needsUpdate=true;
/* labels */
for(const z of ZONES){
const el=zoneLabels[z.id];
const x=(z.x0+z.x1)/2, f=floorAt(x);
project(el,new THREE.Vector3(x,f+0.30,0.72));
}
birds.forEach((bird,i)=>{
const el=birdLabels[i], b=bird.b;
const code=bird.status.code;
el.innerHTML=`<b>${b.name}</b> <span class="st s-${code}">${code==='ok'?'✓':(code==='warn'?'~':'✗')}</span>`;
project(el,new THREE.Vector3(bird.x,bird.baseFloor+b.leg+0.42,0.45));
bird.o.sel.visible=(b.key===selected);
if(bird.o.sel.visible){
const s=1+0.06*Math.sin(t*5);
bird.o.sel.scale.set(s,1,s);
}
});
gaugeLabel.innerHTML=`water level <b>${waterLevel.toFixed(2)} m</b>`;
project(gaugeLabel,new THREE.Vector3(-3.88,floorAt(-3.88)+waterLevel+0.22,0.55));
renderer.render(scene,camera);
}
animate();
</script>
</body>
</html>
```