Prompt specification
Build or customize the Three.js 3D scene "Wetland village beside a delta" 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: Wetland village beside a delta
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/wetland-village-beside-a-delta-x1131
Scene Description Prompt:
Make a standalone HTML scene with Three.js from cdnjs that depicts a small voxel wetland village beside a branching delta. Build reed-covered islands, muddy banks, a few raised timber walkways, fishing docks, and compact houses on stilts. The water channels must form a continuous route between a landing near the foreground and a wider channel behind the village. Animate one simple boat along that route, slowing at bends and staying clear of banks, reeds, and walkway supports. Let the stepped terrain expose the branching channel pattern from above, while the raised paths connect the inhabited islands without filling in the waterways. At the route endpoint, give the boat a small berth beside a ladder descending from the walkway, tying the water and pedestrian networks together.
Working Three.js Starter (wetland-village-beside-a-delta-x1131):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Reedmere Delta — Voxel Wetland Village</title>
<style>
html, body { margin:0; height:100%; overflow:hidden; background:#a9cadd;
font-family: system-ui,-apple-system,"Segoe UI",Roboto,sans-serif; color:#e9f4f8; }
#hud { position:fixed; left:14px; top:14px; max-width:300px; padding:11px 14px;
background:rgba(9,20,27,.55); backdrop-filter:blur(7px);
border:1px solid rgba(255,255,255,.14); border-radius:10px; font-size:12.5px;
line-height:1.55; pointer-events:none; }
#hud b { font-size:14px; letter-spacing:.6px; }
#hud em { color:#ffd98a; font-style:normal; }
.k { display:inline-block; background:rgba(255,255,255,.16); border-radius:4px;
padding:0 5px; margin:0 2px; font-size:11.5px; }
#btn { position:fixed; right:14px; top:14px; cursor:pointer; color:#eaf4f8;
background:rgba(9,20,27,.55); border:1px solid rgba(255,255,255,.2);
border-radius:8px; padding:9px 13px; font-size:12px; letter-spacing:.3px; }
#btn:hover { background:rgba(18,38,48,.8); }
#legend { position:fixed; left:14px; bottom:14px; font-size:11.5px; line-height:1.7;
background:rgba(9,20,27,.45); border:1px solid rgba(255,255,255,.1);
border-radius:9px; padding:9px 12px; pointer-events:none; }
#legend span { display:inline-block; width:11px; height:11px; border-radius:2px;
margin-right:6px; vertical-align:-1px; }
</style>
</head>
<body>
<div id="hud">
<b>REEDMERE DELTA</b><br />
A voxel wetland village on a branching delta.<br />
The boat runs the <em>continuous water route</em> from the foreground
<em>landing & berth</em> to the wider channel behind the village.<br /><br />
<span class="k">drag</span> orbit <span class="k">scroll</span> zoom
<span class="k">T</span> plan view <span class="k">R</span> reset
</div>
<button id="btn">Plan view (T)</button>
<div id="legend">
<div><span style="background:#2e4d55"></span>deep channel</div>
<div><span style="background:#6d5b42"></span>muddy bank</div>
<div><span style="background:#7e9c3f"></span>reeds</div>
<div><span style="background:#8a6a45"></span>timber walkway / berth</div>
</div>
<script type="module">
import * as THREE from 'https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js';
/* ─────────────────────────── utilities ─────────────────────────── */
const clamp=(v,a,b)=>v<a?a:(v>b?b:v);
const lerp=(a,b,t)=>a+(b-a)*t;
const ease=t=>t*t*(3-2*t);
function mulberry32(a){return function(){a|=0;a=a+0x6D2B79F5|0;let t=Math.imul(a^a>>>15,1|a);
t=t+Math.imul(t^t>>>7,61|t)^t;return((t^t>>>14)>>>0)/4294967296;};}
const rnd=mulberry32(20240917);
function hash2(x,z){const s=Math.sin(x*127.1+z*311.7)*43758.5453;return s-Math.floor(s);}
function vnoise(x,z){const xi=Math.floor(x),zi=Math.floor(z),xf=x-xi,zf=z-zi;
const u=xf*xf*(3-2*xf),v=zf*zf*(3-2*zf);
const a=hash2(xi,zi),b=hash2(xi+1,zi),c=hash2(xi,zi+1),d=hash2(xi+1,zi+1);
return a+(b-a)*u+(c-a)*v+(a-b-c+d)*u*v;}
function wrapPi(a){a=(a+Math.PI)%(2*Math.PI);if(a<0)a+=2*Math.PI;return a-Math.PI;}
/* ─────────────────────── world / grid constants ─────────────────── */
const N=56, HALF=N/2, WATER_Y=-0.06, BED=-4;
const cellX=i=>-HALF+i+0.5, cellZ=j=>-HALF+j+0.5;
const cidx=(i,j)=>j*N+i;
function gridCell(x,z){ return [Math.floor(x+HALF), Math.floor(z+HALF)]; }
/* ───────────────────────── channel network ─────────────────────────
Everything is defined in world X/Z. The main route is the boat's path. */
const routeRaw=[[-5,26.5],[-5.5,20],[-7,14.5],[-6.5,8],[-2.5,3.5],
[1.5,-1],[4.5,-8],[3.5,-15],[1,-21],[0.5,-27]];
const routeCurve=new THREE.CatmullRomCurve3(
routeRaw.map(p=>new THREE.Vector3(p[0],0,p[1])),false,'catmullrom',0.5);
const routeLine=routeCurve.getSpacedPoints(260);
function smoothLine(pts,samples){
const c=new THREE.CatmullRomCurve3(pts.map(p=>new THREE.Vector3(p[0],0,p[1])),false,'catmullrom',0.5);
return c.getSpacedPoints(samples);
}
const branches=[
{name:'back', hw:5.2, line:smoothLine([[-27,-23],[-14,-25.5],[0,-27],[14,-24],[27,-21]],120)},
{name:'westA', hw:1.9, line:smoothLine([[-6.5,8],[-12,10.5],[-18,8.5],[-24,5]],90)},
{name:'eastB', hw:2.0, line:smoothLine([[1.5,-1],[8,0.5],[14,-2.5],[21,-4]],90)},
{name:'nwC', hw:1.9, line:smoothLine([[-2.5,3.5],[-7,-1],[-11,-6],[-15,-11]],90)},
{name:'seD', hw:2.0, line:smoothLine([[4.5,-8],[10,-10.5],[16,-13.5],[23,-17]],90)},
{name:'foreE', hw:1.8, line:smoothLine([[-5.5,20],[0,19.5],[7,17.5],[14,15.5]],90)},
{name:'swF', hw:1.7, line:smoothLine([[3.5,-15],[-2,-16.5],[-7,-18.5],[-13,-20]],90)},
{name:'seG', hw:1.7, line:smoothLine([[1,-21],[7,-19.5],[13,-17],[17,-14]],90)},
{name:'westH', hw:1.7, line:smoothLine([[-7,14.5],[-13,16],[-20,19]],90)}
];
/* reed-covered islets: each gets a wobbly ring of open water around it */
const islands=[
{x:-16,z: 22,r:3.4,h:2},
{x:-14,z: 5,r:3.6,h:2},
{x: 8,z: 7,r:3.8,h:2},
{x: -9,z:-13,r:3.4,h:2},
{x:-19,z: -6,r:3.0,h:2},
{x: 18,z: 10,r:3.4,h:3},
{x: -1,z:-11,r:3.0,h:2}
];
/* main-network centreline segments (for spurs & clearance tests) */
const netSegs=[];
function pushSegs(line,hw){
for(let i=0;i<line.length-1;i++){
const a=line[i],b=line[i+1],dx=b.x-a.x,dz=b.z-a.z,l2=dx*dx+dz*dz;
if(l2<1e-9) continue;
netSegs.push({ax:a.x,az:a.z,bx:b.x,bz:b.z,dx,dz,inv:1/l2,hw});
}
}
pushSegs(routeLine,2.7);
for(const b of branches) pushSegs(b.line,b.hw);
/* ring channels around the islets + a short spur joining each to the network */
const allLines=[]; // {line,hw}
for(const b of branches) allLines.push({line:b.line,hw:b.hw});
allLines.push({line:routeLine,hw:2.7});
function nearestOnNetwork(x,z){
let best=1e9,bx=x,bz=z;
for(const s of netSegs){
let t=((x-s.ax)*s.dx+(z-s.az)*s.dz)*s.inv; t=clamp(t,0,1);
const cx=s.ax+s.dx*t, cz=s.az+s.dz*t, d=Math.hypot(x-cx,z-cz);
if(d<best){best=d;bx=cx;bz=cz;}
}
return {d:best,x:bx,z:bz};
}
for(const isl of islands){
const rr=isl.r+1.15, pts=[];
for(let k=0;k<48;k++){
const a=k/48*Math.PI*2;
const wob=0.45*(vnoise(Math.cos(a)*2.2+isl.x,Math.sin(a)*2.2+isl.z)-0.5)*2;
const R=rr+wob;
pts.push(new THREE.Vector3(isl.x+Math.cos(a)*R,0,isl.z+Math.sin(a)*R));
}
allLines.push({line:pts,hw:1.0});
/* spur from the ring outwards to the nearest network point */
const np=nearestOnNetwork(isl.x,isl.z);
const dx=np.x-isl.x, dz=np.z-isl.z, L=Math.hypot(dx,dz)||1;
const qx=isl.x+dx/L*rr, qz=isl.z+dz/L*rr;
if(np.d>rr+0.6 && L>rr+0.6){
allLines.push({line:[new THREE.Vector3(qx,0,qz),new THREE.Vector3(np.x,0,np.z)],hw:0.9});
}
}
/* ───────────────── rasterise the signed distance field ───────────────── */
const segs=[];
for(const L of allLines){
for(let i=0;i<L.line.length-1;i++){
const a=L.line[i],b=L.line[i+1],dx=b.x-a.x,dz=b.z-a.z,l2=dx*dx+dz*dz;
if(l2<1e-9) continue;
segs.push({ax:a.x,az:a.z,dx,dz,inv:1/l2,hw:L.hw});
}
}
const sgn=new Float32Array(N*N).fill(1e9);
for(const s of segs){
const pad=s.hw+7.5;
const i0=Math.max(0,Math.floor(Math.min(s.ax,s.ax+s.dx)-pad+HALF));
const i1=Math.min(N-1,Math.ceil (Math.max(s.ax,s.ax+s.dx)+pad+HALF));
const j0=Math.max(0,Math.floor(Math.min(s.az,s.az+s.dz)-pad+HALF));
const j1=Math.min(N-1,Math.ceil (Math.max(s.az,s.az+s.dz)+pad+HALF));
for(let j=j0;j<=j1;j++)for(let i=i0;i<=i1;i++){
const px=cellX(i),pz=cellZ(j);
let t=((px-s.ax)*s.dx+(pz-s.az)*s.dz)*s.inv; t=clamp(t,0,1);
const d=Math.hypot(px-(s.ax+s.dx*t),pz-(s.az+s.dz*t))-s.hw;
const k=cidx(i,j); if(d<sgn[k]) sgn[k]=d;
}
}
/* distance to the boat's route centreline (clearance test) */
const routeSegs=[];
for(let i=0;i<routeLine.length-1;i++){
const a=routeLine[i],b=routeLine[i+1],dx=b.x-a.x,dz=b.z-a.z;
routeSegs.push({ax:a.x,az:a.z,dx,dz,inv:1/(dx*dx+dz*dz)});
}
function routeDist(x,z){
let best=1e9;
for(const s of routeSegs){
let t=((x-s.ax)*s.dx+(z-s.az)*s.dz)*s.inv; t=clamp(t,0,1);
const d=Math.hypot(x-(s.ax+s.dx*t),z-(s.az+s.dz*t));
if(d<best) best=d;
}
return best;
}
/* ───────────────────────── stepped voxel terrain ───────────────────────── */
const heights=new Float32Array(N*N);
const islet=new Uint8Array(N*N);
for(let j=0;j<N;j++)for(let i=0;i<N;i++){
const x=cellX(i),z=cellZ(j),s=sgn[cidx(i,j)];
let h;
if(s<=-1.25) h=-2; else if(s<=-0.45) h=-1; else if(s<=0.95) h=0;
else if(s<=2.2) h=1; else if(s<=3.8) h=2; else if(s<=6.0) h=3; else h=4;
const n=vnoise(x*0.22+31.7,z*0.22-17.3);
if(h>=3){ if(n>0.66) h++; else if(n<0.30) h--; }
else if(h===2 && n>0.78) h++;
for(const isl of islands){
const d=Math.hypot(x-isl.x,z-isl.z);
if(d<isl.r*0.98 && s>0.9){ h=Math.min(h,isl.h+(d<isl.r*0.5?1:0)); islet[cidx(i,j)]=1; }
}
heights[cidx(i,j)]=h;
}
function terrainAt(x,z){
const [i,j]=gridCell(x,z);
if(i<0||j<0||i>=N||j>=N) return -2;
return heights[cidx(i,j)];
}
/* ───────────────────────── instanced box batches ───────────────────────── */
class Batch{
constructor(){this.items=[];}
add(x,y,z,sx,sy,sz,color,rot){
this.items.push({p:[x,y,z],s:[sx,sy,sz],c:color,r:rot||[0,0,0]});
}
build(mat,cast=true){
const mesh=new THREE.InstancedMesh(new THREE.BoxGeometry(1,1,1),mat,this.items.length);
const d=new THREE.Object3D(), col=new THREE.Color();
this.items.forEach((it,i)=>{
d.position.set(it.p[0],it.p[1],it.p[2]);
d.rotation.set(it.r[0],it.r[1],it.r[2]);
d.scale.set(it.s[0],it.s[1],it.s[2]);
d.updateMatrix(); mesh.setMatrixAt(i,d.matrix);
col.set(it.c); mesh.setColorAt(i,col);
});
mesh.instanceMatrix.needsUpdate=true;
if(mesh.instanceColor) mesh.instanceColor.needsUpdate=true;
mesh.castShadow=cast; mesh.receiveShadow=true;
return mesh;
}
}
const MAT_SOLID = new THREE.MeshStandardMaterial({roughness:0.92, metalness:0.02});
const MAT_GLOW = new THREE.MeshBasicMaterial({});
const terrainB=new Batch(), reedB=new Batch(), woodB=new Batch(), glowB=new Batch();
/* terrain columns */
const C_BED=[0x33443c,0x3f4d40], C_SHAL=[0x4d5a41], C_MUD0=[0x6d5b42,0x776447],
C_MUD1=[0x7d6a4c,0x8a7553], C_MARSH=[0x5f7343,0x6b7c48],
C_GRASS=[0x55683c,0x607340], C_HIGH=[0x4d6236,0x577038], C_DRY=[0x877a4f];
for(let j=0;j<N;j++)for(let i=0;i<N;i++){
const k=cidx(i,j), h=heights[k], x=cellX(i), z=cellZ(j);
const v=vnoise(x*1.7+5.5,z*1.7-9.5);
let col;
if(h<=-2) col=C_BED[(v*2|0)%2]; else if(h<=-1) col=C_SHAL[0];
else if(h===0) col=C_MUD0[(v*2|0)%2]; else if(h===1) col=C_MUD1[(v*2|0)%2];
else if(h===2) col=C_MARSH[(v*2|0)%2]; else if(h===3) col=C_GRASS[(v*2|0)%2];
else col=(v>0.72?C_DRY[0]:C_HIGH[(v*2|0)%2]);
const top=h, H=top-BED;
terrainB.add(x,(top+BED)/2,z,0.965,H,0.965,col);
}
/* reeds — marsh grass on banks and thick cover on the islets */
for(let j=0;j<N;j++)for(let i=0;i<N;i++){
const k=cidx(i,j), h=heights[k];
if(h>2) continue;
const s=sgn[k];
const wet = (s<4.5)||islet[k];
if(!wet) continue;
const p=(h===0?0.62:h===1?0.36:0.26)*(0.55+0.9*vnoise(cellX(i)*2.3,cellZ(j)*2.3));
if(rnd()>p) continue;
const x=cellX(i),z=cellZ(j);
const n=3+((rnd()*5)|0);
for(let q=0;q<n;q++){
const px=x+(rnd()-0.5)*0.92, pz=z+(rnd()-0.5)*0.92;
const hh=0.65+rnd()*1.15;
const tint=rnd();
const col= tint<0.42?0x7f9c3f : tint<0.75?0x93ad48 : tint<0.9?0xb0a24e : 0x8a7f43;
reedB.add(px,heights[k]+hh/2,pz,0.085+rnd()*0.055,hh,0.085+rnd()*0.055,col,
[(rnd()-0.5)*0.22,rnd()*3.14,(rnd()-0.5)*0.22]);
if(rnd()<0.22) reedB.add(px,heights[k]+hh+0.12,pz,0.15,0.3,0.15,0x7d5c33);
}
}
/* a few voxel trees on the drier ground */
for(let j=0;j<N;j++)for(let i=0;i<N;i++){
const k=cidx(i,j), h=heights[k];
if(h<3||islet[k]) continue;
if(rnd()>0.045) continue;
const x=cellX(i)+(rnd()-0.5)*0.7, z=cellZ(j)+(rnd()-0.5)*0.7;
const th=1.1+rnd()*0.7;
woodB.add(x,h+th/2,z,0.24,th,0.24,0x5a4632);
const lc=rnd()<0.5?0x4f6b34:0x5d7a3c;
woodB.add(x,h+th+0.3,z,1.25,0.72,1.25,lc);
woodB.add(x+0.28,h+th+0.78,z-0.18,0.85,0.55,0.85,lc);
woodB.add(x-0.3,h+th+0.6,z+0.26,0.7,0.45,0.7,0x57703a);
}
/* ──────────────────── raised timber walkways ────────────────────
deck at 2.0, posts only on dry ground and never within 3.9 of the route. */
const DECK_Y=2.0;
const WOOD_A=0x8a6a45, WOOD_B=0x7b5e3d, WOOD_C=0x94754e, RAIL=0x6d4f34;
function addWalkway(raw,opts={}){
const deck=opts.deck||DECK_Y, width=opts.width||1.15;
const line=smoothLine(raw,Math.max(10,Math.round(raw.length*3)));
/* planks */
for(let i=1;i<line.length;i++){
const a=line[i-1],b=line[i],dx=b.x-a.x,dz=b.z-a.z,len=Math.hypot(dx,dz);
if(len<1e-4) continue;
const ang=Math.atan2(dx,dz), mx=(a.x+b.x)/2, mz=(a.z+b.z)/2;
const c=(i%3===0)?WOOD_B:(i%3===1?WOOD_A:WOOD_C);
woodB.add(mx,deck,mz,width,0.16,len*0.86,c,[0,ang,0]);
}
/* rails */
for(let i=1;i<line.length;i+=3){
const a=line[i-1],b=line[Math.min(i+2,line.length-1)];
const dx=b.x-a.x,dz=b.z-a.z,len=Math.hypot(dx,dz); if(len<1e-4) continue;
const ang=Math.atan2(dx,dz),px=-dz/len,pz=dx/len;
const mx=(a.x+b.x)/2,mz=(a.z+b.z)/2;
for(const s of [-1,1]){
woodB.add(mx+px*s*(width/2+0.05),deck+0.44,mz+pz*s*(width/2+0.05),0.1,0.42,len,RAIL,[0,ang,0]);
}
}
/* supports: land only, clear of the boat lane */
let acc=0;
for(let i=1;i<line.length;i++){
const a=line[i-1],b=line[i],len=a.distanceTo(b); acc+=len;
if(acc<1.9) continue; acc=0;
const x=b.x,z=b.z,g=terrainAt(x,z);
if(g<0) continue;
if(routeDist(x,z)<3.9) continue;
const ph=deck-0.08-g+0.15;
if(ph<=0.2) continue;
woodB.add(x,g+ph/2,z,0.22,ph,0.22,0x6b4b32);
woodB.add(x,g+0.1,z,0.4,0.2,0.4,0x5e422c);
}
/* end posts (the landings are always founded) */
for(const p of [line[0],line[line.length-1]]){
const g=Math.min(terrainAt(p.x,p.z),-1.4);
const ph=deck-0.08-g;
woodB.add(p.x,g+ph/2,p.z,0.26,ph,0.26,0x6b4b32);
}
}
/* village + island walkways (bridges cross the water; the water stays open) */
addWalkway([[-8.6,24.4],[-11.5,23.2],[-14,22.6],[-16,22]]); // landing → west islet
addWalkway([[-16,22],[-21,17],[-22,10],[-18,5],[-14,5]]); // west → south-west islet
addWalkway([[-14,5],[-9,6],[-4,6.5],[1,7],[5,7.5],[8,7]]); // crosses the main route
addWalkway([[8,7],[12.5,8.5],[18,10]]); // → east islet
addWalkway([[8,7],[4,4],[1.5,2.5],[0.6,1.0],[0.15,0],[ -0.5,-3],[-1,-7],[-1,-11]]); // crosses the route again
addWalkway([[-1,-11],[-4,-11.5],[-7,-12],[-9,-13]]); // → south-west islet
addWalkway([[-3.2,13.5],[-2.4,10],[-1.2,6.8],[1.5,4.5]]); // mainland stilt village
addWalkway([[-9,-13],[-5,-14.5],[-2.5,-16],[0,-17.5]]); // islet → south bank
addWalkway([[1.5,4.5],[3.5,5],[7,6.5]]); // link to the east islet
/* ─────────────────────────── fishing docks ─────────────────────────── */
function addDock(p0,p1,crates){
const deck=0.95;
const line=smoothLine([p0,p1],Math.max(8,Math.round(Math.hypot(p1[0]-p0[0],p1[1]-p0[1])*4)));
for(let i=1;i<line.length;i++){
const a=line[i-1],b=line[i],dx=b.x-a.x,dz=b.z-a.z,len=Math.hypot(dx,dz);
const ang=Math.atan2(dx,dz);
woodB.add((a.x+b.x)/2,deck,(a.z+b.z)/2,1.3,0.15,len*0.85,(i%2?WOOD_A:WOOD_C),[0,ang,0]);
}
let acc=0;
for(let i=1;i<line.length;i++){
const a=line[i-1],b=line[i],len=a.distanceTo(b); acc+=len;
if(acc<1.7) continue; acc=0;
const g=Math.min(terrainAt(b.x,b.z),-1.6);
if(routeDist(b.x,b.z)<4.2) continue;
woodB.add(b.x,(g+deck)/2,b.z,0.2,deck-g,0.2,0x63482e);
}
const dx=p1[0]-p0[0],dz=p1[1]-p0[1],L=Math.hypot(dx,dz),ang=Math.atan2(dx,dz);
const ex=p0[0]+dx*0.82,ez=p0[1]+dz*0.82;
for(let c=0;c<(crates||1);c++){
const ox=(rnd()-0.5)*0.5,oz=(rnd()-0.5)*0.5;
woodB.add(ex+ox,deck+0.22,ez+oz,0.55,0.44,0.55,c%2?0x7a5b3a:0x6a4d30,[0,ang+rnd()*0.4,0]);
}
woodB.add(ex+0.1,deck+0.55,ez,0.07,1.15,0.07,0x4a3a26,[0.35,ang,0.12]); // fishing rod
woodB.add(ex+0.42,deck+1.0,ez+0.22,0.05,0.05,0.5,0xd8d3c0,[0.35,ang,0.12]); // line
}
addDock([-15.5,11.5],[-17.6,9.2],2);
addDock([11,1.5],[13.2,-1.1],1);
addDock([13.5,-11],[15.2,-12.8],2);
addDock([-4,-16],[-6.1,-17.6],1);
addDock([-8,-4],[-10.2,-5.6],2);
addDock([-1,17],[0.6,18.7],1);
addDock([-1.5,-17.5],[-3.2,-18.6],2);
/* ───────────────────────── stilt houses ───────────────────────── */
const WALLS=[0xd9c8a8,0xc9b596,0xbfb294,0xcfc7ad], ROOFS=[0x8a4a3a,0x7a5a3a,0x5f6b6a,0x7c4636];
function addHouse(x,z,rot,opts={}){
const g=terrainAt(x,z);
const floor=Math.max(1.75, g+1.05);
const W=2.0,D=1.8;
const cs=Math.cos(rot),sn=Math.sin(rot);
const T=(lx,lz)=>[x+lx*cs+lz*sn, z-lx*sn+lz*cs];
const wallCol=WALLS[(rnd()*WALLS.length)|0], roofCol=ROOFS[(rnd()*ROOFS.length)|0];
/* stilts down into water / mud */
const base=Math.min(g,-1.5);
for(const [sx,sz] of [[-0.85,-0.65],[0.85,-0.65],[-0.85,0.65],[0.85,0.65]]){
const [wx,wz]=T(sx,sz);
woodB.add(wx,(base+floor)/2,wz,0.2,floor-base,0.2,0x6b4b32);
}
const [fx,fz]=T(0,0);
woodB.add(fx,floor+0.09,fz,W+0.24,0.18,D+0.24,0x7b5e3d,[0,rot,0]);
woodB.add(fx,floor+0.18+0.58,fz,W,1.16,D,wallCol,[0,rot,0]);
const wallTop=floor+0.18+1.16;
woodB.add(fx,wallTop+0.11,fz,W+0.42,0.22,D+0.42,roofCol,[0,rot,0]);
woodB.add(fx,wallTop+0.32,fz,W-0.24,0.2,D-0.24,roofCol,[0,rot,0]);
woodB.add(fx,wallTop+0.51,fz,W-0.96,0.18,D-0.96,roofCol,[0,rot,0]);
/* door + lit windows on the front */
const [dxw,dzw]=T(-0.35,D/2+0.03);
woodB.add(dxw,floor+0.58,dzw,0.46,0.82,0.08,0x4b3526,[0,rot,0]);
const [wx1,wz1]=T(0.55,D/2+0.04);
glowB.add(wx1,floor+0.78,wz1,0.5,0.44,0.08,0xffd27a,[0,rot,0]);
const [wx2,wz2]=T(-W/2-0.04,0.1);
glowB.add(wx2,floor+0.78,wz2,0.08,0.4,0.5,0xffcf72,[0,rot,0]);
/* a crate and a lantern by the door */
const [cx,cz]=T(0.75,D/2+0.35);
woodB.add(cx,floor+0.28,cz,0.5,0.4,0.5,0x6a4d30,[0,rot+rnd()*0.5,0]);
}
addHouse(-15.2,21.4,0.35); addHouse(-17.3,23.1,-0.5);
addHouse(-13.4,4.6,-0.2); addHouse(-15.2,6.3,1.1);
addHouse(6.6,6.3,0.2); addHouse(9.4,8.1,2.3);
addHouse(-8.2,-12.2,0.9); addHouse(-10.4,-14.0,-0.7);
addHouse(-19,-6,0.6); addHouse(18,10,-0.9); addHouse(-1,-11,1.4);
/* houses standing over the water on the mainland bank */
addHouse(-3.2,12.6,0.15); addHouse(-2.7,9.6,-0.35);
addHouse(7.6,-6.5,2.1); addHouse(13.6,-8.2,0.9);
/* ───────────────── the landing, berth and ladder ───────────────── */
(function landing(){
/* raised timber platform (the walkway's end) */
const px=-8.6,pz=24.4;
for(let i=0;i<5;i++){
woodB.add(px-1.05+i*0.525,2.0,pz,0.46,0.18,2.6,(i%2?WOOD_A:WOOD_C),[0,0,0]);
}
for(const [sx,sz] of [[-1.1,-1.1],[1.1,-1.1],[-1.1,1.1],[1.1,1.1]]){
const g=terrainAt(px+sx,pz+sz);
woodB.add(px+sx,(g+2.0)/2,pz+sz,0.28,2.0-g+0.15,0.28,0x6b4b32);
}
/* low berth jetty reaching out over the water */
const jz=26.6;
for(let i=0;i<8;i++){
woodB.add(-9.2+i*0.37,0.95,jz,0.31,0.16,1.45,(i%2?WOOD_A:WOOD_C),[0,0,0]);
}
for(const [sx,sz] of [[-9.0,-0.6],[-9.0,0.6],[-6.8,-0.6],[-6.8,0.6]]){
woodB.add(sx,-1.2, jz+sz,0.22,3.0,0.22,0x5d422c);
}
/* ladder descending from the platform into the water, right by the berth */
for(const lx of [-7.42,-6.92]){
woodB.add(lx,0.62,25.72,0.1,3.0,0.1,0x8a6a45);
}
for(let y=-0.75;y<1.95;y+=0.3){
woodB.add(-7.17,y,25.72,0.62,0.09,0.09,0x9a7a52);
}
/* lanterns */
for(const [lx,lz] of [[-7.6,24.0],[-9.4,25.0]]){
woodB.add(lx,2.55,lz,0.12,1.1,0.12,0x5d422c);
glowB.add(lx,3.18,lz,0.3,0.34,0.3,0xffd27a);
}
/* bollards on the berth */
for(const bx of [-8.6,-7.4]) woodB.add(bx,1.18,27.25,0.18,0.4,0.18,0x4b3526);
})();
/* ─────────────────────────── scene setup ─────────────────────────── */
const scene=new THREE.Scene();
scene.background=new THREE.Color(0xa9cadd);
scene.fog=new THREE.Fog(0xa9cadd,110,230);
const camera=new THREE.PerspectiveCamera(42,innerWidth/innerHeight,0.5,400);
const renderer=new THREE.WebGLRenderer({antialias:true});
renderer.setPixelRatio(Math.min(devicePixelRatio,2));
renderer.setSize(innerWidth,innerHeight);
renderer.shadowMap.enabled=true;
renderer.shadowMap.type=THREE.PCFSoftShadowMap;
renderer.outputColorSpace=THREE.SRGBColorSpace;
document.body.appendChild(renderer.domElement);
scene.add(new THREE.HemisphereLight(0xbcd8e8,0x4a4130,0.62));
const sun=new THREE.DirectionalLight(0xfff1d4,1.15);
sun.position.set(-30,46,26); sun.castShadow=true;
sun.shadow.mapSize.set(2048,2048);
sun.shadow.camera.left=-44; sun.shadow.camera.right=44;
sun.shadow.camera.top=44; sun.shadow.camera.bottom=-44;
sun.shadow.camera.near=1; sun.shadow.camera.far=170;
sun.shadow.bias=-0.0006; sun.shadow.normalBias=0.03;
scene.add(sun);
const fill=new THREE.DirectionalLight(0x9fc4e0,0.28); fill.position.set(34,22,-32); scene.add(fill);
/* terrain + props */
scene.add(terrainB.build(MAT_SOLID));
scene.add(reedB.build(MAT_SOLID));
scene.add(woodB.build(MAT_SOLID));
scene.add(glowB.build(MAT_GLOW,false));
/* ─────────────────────────── water surface ─────────────────────────── */
const wGeo=new THREE.PlaneGeometry(N,N,N,N);
wGeo.rotateX(-Math.PI/2);
const wPos=wGeo.attributes.position;
const wCol=new Float32Array(wPos.count*3);
const cA=new THREE.Color(0x6fa7a6), cB=new THREE.Color(0x1d4650), tmpC=new THREE.Color();
for(let k=0;k<wPos.count;k++){
const x=wPos.getX(k), z=wPos.getZ(k);
const g=terrainAt(x,z);
const depth=clamp((WATER_Y-g)/3.2,0,1);
tmpC.copy(cA).lerp(cB,depth*0.95);
wCol[k*3]=tmpC.r; wCol[k*3+1]=tmpC.g; wCol[k*3+2]=tmpC.b;
}
wGeo.setAttribute('color',new THREE.BufferAttribute(wCol,3));
const water=new THREE.Mesh(wGeo,new THREE.MeshStandardMaterial({
vertexColors:true, transparent:true, opacity:0.80, roughness:0.13, metalness:0.12,
side:THREE.DoubleSide }));
water.position.y=WATER_Y; water.renderOrder=2; scene.add(water);
/* ──────────────────────────── the boat ──────────────────────────── */
const boat=new THREE.Group();
const boatMat=c=>new THREE.MeshStandardMaterial({color:c,roughness:0.78,metalness:0.03});
function bBox(w,h,d,col,x,y,z,ry){
const m=new THREE.Mesh(new THREE.BoxGeometry(w,h,d),boatMat(col));
m.position.set(x,y,z); if(ry)m.rotation.y=ry;
m.castShadow=true; m.receiveShadow=true; boat.add(m); return m;
}
bBox(1.00,0.40,2.55,0x41301f,0,-0.16,0); // hull bottom
bBox(1.36,0.42,2.92,0x6b4a2e,0, 0.18,0); // hull topside
bBox(1.10,0.08,2.60,0x8a6a45,0, 0.40,0); // deck
bBox(0.12,0.20,2.94,0xc9a25e,-0.63,0.44,0); // gunwales
bBox(0.12,0.20,2.94,0xc9a25e, 0.63,0.44,0);
bBox(1.38,0.20,0.12,0xc9a25e,0,0.44, 1.42);
bBox(1.38,0.20,0.12,0xc9a25e,0,0.44,-1.42);
bBox(0.70,0.42,0.55,0x6b4a2e,0,0.16,1.52); // bow block
bBox(0.80,0.58,0.86,0xd8c49a,0,0.72,-0.78); // cabin
bBox(0.96,0.12,1.02,0x7a4a35,0,1.05,-0.78);
bBox(0.09,1.00,0.09,0x6b4a2e,0,0.90, 0.55); // mast
bBox(0.34,0.22,0.04,0xd06a4a,0.18,1.32,0.55); // flag
bBox(0.44,0.34,0.44,0x6a4d30,0,0.58,-1.22); // stern crate
bBox(0.10,0.62,0.10,0x4a3a26,0.42,0.70,-0.4); // tiller
scene.add(boat);
/* wake */
const WAKE=44;
const wakeMesh=new THREE.InstancedMesh(new THREE.BoxGeometry(1,0.05,1),
new THREE.MeshBasicMaterial({color:0xe6f6f2,transparent:true,opacity:0.55}),WAKE);
wakeMesh.instanceMatrix.setUsage(THREE.DynamicDrawUsage);
scene.add(wakeMesh);
const wakeParts=[];
for(let i=0;i<WAKE;i++) wakeParts.push({x:0,z:0,age:1e9,life:1,s:0});
/* ───────────── boat route: arc length, curvature, speed profile ───────────── */
const NS=800;
const pathPts=routeCurve.getSpacedPoints(NS);
let totalLen=0; const cum=[0];
for(let i=1;i<pathPts.length;i++){ totalLen+=pathPts[i].distanceTo(pathPts[i-1]); cum.push(totalLen); }
const tang=[];
for(let i=0;i<pathPts.length;i++){
const a=pathPts[Math.max(0,i-1)],b=pathPts[Math.min(pathPts.length-1,i+1)];
const t=new THREE.Vector3(b.x-a.x,0,b.z-a.z).normalize(); tang.push(t);
}
const curv=new Float32Array(pathPts.length);
for(let i=1;i<pathPts.length-1;i++){
const a=tang[i-1],b=tang[i+1];
const ang=Math.acos(clamp(a.dot(b),-1,1));
const d=pathPts[i+1].distanceTo(pathPts[i-1]);
curv[i]=d>1e-5?ang/d:0;
}
const VMAX=3.4,VMIN=0.55,ACC=1.35;
const spd=new Float32Array(pathPts.length);
for(let i=0;i<spd.length;i++) spd[i]=clamp(VMAX/(1+13*curv[i]),VMIN,VMAX);
for(let i=1;i<spd.length;i++){
const ds=cum[i]-cum[i-1];
spd[i]=Math.min(spd[i],Math.sqrt(spd[i-1]*spd[i-1]+2*ACC*ds));
}
for(let i=spd.length-2;i>=0;i--){
const ds=cum[i+1]-cum[i];
spd[i]=Math.min(spd[i],Math.sqrt(spd[i+1]*spd[i+1]+2*ACC*ds));
}
for(let i=0;i<spd.length;i++){ // ease into berth and into the turn
const s=cum[i], e=Math.min(s,totalLen-s);
if(e<4) spd[i]*=clamp(e/4,0.12,1);
}
function speedAt(s){
const f=clamp(s/totalLen,0,1)*(pathPts.length-1);
const i=Math.floor(f),j=Math.min(i+1,pathPts.length-1);
return lerp(spd[i],spd[j],f-i);
}
function pointAt(s){
const f=clamp(s/totalLen,0,1)*(pathPts.length-1);
const i=Math.floor(f),j=Math.min(i+1,pathPts.length-1);
const t=(f-i);
return new THREE.Vector3().lerpVectors(pathPts[i],pathPts[j],t);
}
function tangentAt(s){
const f=clamp(s/totalLen,0,1)*(pathPts.length-1);
const i=Math.floor(f),j=Math.min(i+1,pathPts.length-1);
return new THREE.Vector3().lerpVectors(tang[i],tang[j],f-i).normalize();
}
/* boat state machine: run out → turn in the wide channel → run back → dock */
let sPos=0, dirSign=1, phase='out', turnT=0, turnFrom=0, turnTo=0, dwellT=0, yaw=0;
const TURN_T=2.6, DWELL=4.0;
function startTurn(targetYaw){
turnFrom=yaw; turnTo=yaw+wrapPi(targetYaw-yaw); turnT=0; phase='turn';
}
const clock=new THREE.Clock();
let wakeTimer=0;
function animateBoat(dt,time){
if(phase==='out'||phase==='back'){
sPos=clamp(sPos+dirSign*speedAt(sPos)*dt,0,totalLen);
const t=tangentAt(sPos);
const targetYaw=dirSign>0?Math.atan2(t.x,t.z):Math.atan2(-t.x,-t.z);
yaw+=wrapPi(targetYaw-yaw)*(1-Math.exp(-dt*3.4));
if(dirSign>0 && sPos>=totalLen-0.001){ sPos=totalLen; startTurn(Math.atan2(-t.x,-t.z)); }
if(dirSign<0 && sPos<=0.001){ sPos=0; phase='dock'; dwellT=0; }
}else if(phase==='turn'){
turnT+=dt;
const k=clamp(turnT/TURN_T,0,1);
yaw=turnFrom+(turnTo-turnFrom)*ease(k);
if(k>=1){ if(dirSign>0){ dirSign=-1; phase='back'; } else { dirSign=1; phase='out'; } }
}else if(phase==='dock'){
dwellT+=dt;
const t=tangentAt(0);
yaw+=wrapPi(Math.atan2(t.x,t.z)-yaw)*(1-Math.exp(-dt*2.2));
if(dwellT>=DWELL) startTurn(Math.atan2(t.x,t.z));
}
/* place the boat */
const p=pointAt(sPos), t=tangentAt(sPos);
const perp=new THREE.Vector3(-t.z,0,t.x);
const wob=0.13*Math.sin(time*1.25+sPos*0.25);
boat.position.set(p.x+perp.x*wob, WATER_Y+0.02+0.045*Math.sin(time*1.7+sPos*0.4), p.z+perp.z*wob);
boat.rotation.y=yaw;
boat.rotation.z=0.035*Math.sin(time*1.1);
/* wake */
wakeTimer+=dt;
if(wakeTimer>0.09 && phase!=='dock'){
wakeTimer=0;
const slot=wakeParts.find(w=>w.age>=w.life);
if(slot){
const back=new THREE.Vector3(Math.sin(yaw),0,Math.cos(yaw));
slot.x=boat.position.x-back.x*1.55+(rnd()-0.5)*0.5;
slot.z=boat.position.z-back.z*1.55+(rnd()-0.5)*0.5;
slot.age=0; slot.life=1.5+rnd()*1.3; slot.s=0.35+rnd()*0.35;
}
}
const d=new THREE.Object3D();
for(let i=0;i<WAKE;i++){
const w=wakeParts[i]; w.age+=dt;
const k=clamp(w.age/w.life,0,1);
if(k>=1){ d.scale.set(0,0,0); d.position.set(0,-50,0); }
else{
const sz=w.s*(1+k*2.4);
d.position.set(w.x,WATER_Y+0.03,w.z);
d.rotation.set(0,0,0); d.scale.set(sz,1,sz*0.6);
}
d.updateMatrix(); wakeMesh.setMatrixAt(i,d.matrix);
}
wakeMesh.instanceMatrix.needsUpdate=true;
}
/* ───────────────────────── camera & controls ───────────────────────── */
const cam={az:0.38,polar:0.92,dist:80,tx:0,ty:0,tz:-2};
const cur={...cam};
function applyCamera(){
const sp=Math.sin(cur.polar),cp=Math.cos(cur.polar);
camera.position.set(cur.tx+cur.dist*sp*Math.sin(cur.az), cur.ty+cur.dist*cp, cur.tz+cur.dist*sp*Math.cos(cur.az));
camera.lookAt(cur.tx,cur.ty,cur.tz);
}
let dragging=false,lastX=0,lastY=0;
const cv=renderer.domElement;
cv.addEventListener('pointerdown',e=>{dragging=true;lastX=e.clientX;lastY=e.clientY;cv.setPointerCapture(e.pointerId);});
cv.addEventListener('pointerup',e=>{dragging=false;});
cv.addEventListener('pointermove',e=>{
if(!dragging) return;
const dx=e.clientX-lastX, dy=e.clientY-lastY; lastX=e.clientX; lastY=e.clientY;
cam.az-=dx*0.006; cam.polar=clamp(cam.polar-dy*0.005,0.06,1.45);
});
cv.addEventListener('wheel',e=>{e.preventDefault();cam.dist=clamp(cam.dist*(1+e.deltaY*0.0012),26,140);},{passive:false});
function planView(){ cam.az=0.0; cam.polar=0.045; cam.dist=74; cam.tx=0; cam.ty=0; cam.tz=-2; }
function resetView(){ cam.az=0.38; cam.polar=0.92; cam.dist=80; cam.tx=0; cam.ty=0; cam.tz=-2; }
document.getElementById('btn').addEventListener('click',planView);
addEventListener('keydown',e=>{
const k=e.key.toLowerCase();
if(k==='t') planView();
if(k==='r') resetView();
});
/* ───────────────────────────── main loop ───────────────────────────── */
let time=0;
function tick(){
requestAnimationFrame(tick);
const dt=Math.min(clock.getDelta(),0.05); time+=dt;
animateBoat(dt,time);
/* gentle water swell */
const p=wGeo.attributes.position;
for(let k=0;k<p.count;k++){
const x=p.getX(k),z=p.getZ(k);
p.setY(k,0.055*Math.sin(x*0.55+time*1.15)*Math.cos(z*0.47-time*0.85));
}
p.needsUpdate=true;
/* camera damping */
const f=1-Math.exp(-dt*7);
cur.az+=wrapPi(cam.az-cur.az)*f;
cur.polar=lerp(cur.polar,cam.polar,f);
cur.dist=lerp(cur.dist,cam.dist,f);
cur.tx=lerp(cur.tx,cam.tx,f); cur.ty=lerp(cur.ty,cam.ty,f); cur.tz=lerp(cur.tz,cam.tz,f);
applyCamera();
renderer.render(scene,camera);
}
applyCamera();
tick();
addEventListener('resize',()=>{
camera.aspect=innerWidth/innerHeight; camera.updateProjectionMatrix();
renderer.setSize(innerWidth,innerHeight);
});
</script>
</body>
</html>
```