Prompt specification
Build or customize the Three.js 3D scene "Riverside settlement above the floodplain" 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: Riverside settlement above the floodplain
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/riverside-settlement-above-the-floodplain-x1175
Scene Description Prompt:
Create a single self-contained HTML file using Three.js from cdnjs that shows river meanders. Compose a winding river, elevated homes, fields, and an abandoned channel; mark the floodplain and show a water-level toggle reaching low areas first. Voxel style.
Working Three.js Starter (riverside-settlement-above-the-floodplain-x1175):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>River Meanders — Voxel Floodplain</title>
<style>
:root {
--ink: #173c3d;
--panel: rgba(15, 43, 44, 0.91);
--panel-soft: rgba(21, 55, 55, 0.76);
--cyan: #78e4df;
--cyan-bright: #b5fff4;
--cream: #f3e8c8;
--muted: #a9c6bd;
--amber: #e9b969;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #b9d4ce;
color: var(--cream);
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
canvas {
position: fixed;
inset: 0;
width: 100%;
height: 100%;
display: block;
touch-action: none;
cursor: grab;
}
canvas:active {
cursor: grabbing;
}
.vignette {
position: fixed;
inset: 0;
pointer-events: none;
background:
radial-gradient(circle at 50% 45%, transparent 48%, rgba(20, 48, 46, 0.12) 100%),
linear-gradient(180deg, rgba(18, 51, 50, 0.05), transparent 32%);
z-index: 1;
}
.hero {
position: fixed;
z-index: 5;
top: 28px;
left: 30px;
width: min(410px, calc(100vw - 60px));
pointer-events: none;
text-shadow: 0 2px 18px rgba(17, 54, 51, 0.28);
}
.eyebrow {
display: flex;
align-items: center;
gap: 10px;
margin-bottom: 10px;
color: #285a57;
font-size: 11px;
font-weight: 800;
letter-spacing: 0.19em;
text-transform: uppercase;
}
.eyebrow::before {
content: "";
width: 28px;
height: 3px;
background: var(--cyan);
box-shadow: 0 0 0 1px rgba(16, 63, 62, 0.12);
}
h1 {
margin: 0;
color: #173f3d;
font-size: clamp(36px, 4.5vw, 68px);
font-weight: 780;
line-height: 0.92;
letter-spacing: -0.055em;
}
.hero p {
max-width: 360px;
margin: 17px 0 0;
color: #315f5b;
font-size: 13px;
font-weight: 540;
line-height: 1.55;
}
.hydro-panel {
position: fixed;
z-index: 10;
top: 26px;
right: 26px;
width: 320px;
padding: 19px;
border: 1px solid rgba(188, 239, 225, 0.18);
border-radius: 18px;
background: var(--panel);
box-shadow: 0 18px 50px rgba(18, 45, 43, 0.22);
backdrop-filter: blur(14px);
-webkit-backdrop-filter: blur(14px);
}
.panel-kicker {
color: var(--cyan);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.16em;
text-transform: uppercase;
}
.panel-title {
display: flex;
justify-content: space-between;
align-items: baseline;
gap: 12px;
margin-top: 6px;
}
.panel-title strong {
font-size: 17px;
letter-spacing: -0.02em;
}
.panel-title span {
color: var(--muted);
font-size: 10px;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.level-row {
display: flex;
align-items: center;
justify-content: space-between;
margin-top: 21px;
}
.level-copy {
display: grid;
gap: 3px;
}
.level-copy label {
color: var(--muted);
font-size: 11px;
font-weight: 650;
}
.level-copy strong {
color: var(--cyan-bright);
font-size: 25px;
font-variant-numeric: tabular-nums;
letter-spacing: -0.04em;
}
.switch {
position: relative;
width: 48px;
height: 27px;
padding: 0;
border: 1px solid rgba(188, 239, 225, 0.25);
border-radius: 999px;
outline: none;
background: rgba(255, 255, 255, 0.08);
cursor: pointer;
transition: background 180ms ease, border-color 180ms ease;
}
.switch::after {
content: "";
position: absolute;
top: 4px;
left: 4px;
width: 17px;
height: 17px;
border-radius: 50%;
background: #b6cbc3;
transition: transform 220ms ease, background 220ms ease;
}
.switch[aria-checked="true"] {
border-color: rgba(120, 228, 223, 0.7);
background: rgba(120, 228, 223, 0.24);
}
.switch[aria-checked="true"]::after {
transform: translateX(21px);
background: var(--cyan-bright);
}
.switch:focus-visible {
box-shadow: 0 0 0 3px rgba(120, 228, 223, 0.2);
}
input[type="range"] {
--pct: 0%;
width: 100%;
height: 5px;
margin: 18px 0 0;
border-radius: 99px;
outline: none;
appearance: none;
-webkit-appearance: none;
background: linear-gradient(
90deg,
var(--cyan) 0%,
var(--cyan) var(--pct),
rgba(255, 255, 255, 0.13) var(--pct),
rgba(255, 255, 255, 0.13) 100%
);
cursor: pointer;
}
input[type="range"]::-webkit-slider-thumb {
width: 17px;
height: 17px;
border: 3px solid #174b4b;
border-radius: 50%;
appearance: none;
-webkit-appearance: none;
background: var(--cyan-bright);
box-shadow: 0 0 0 1px var(--cyan);
}
input[type="range"]::-moz-range-thumb {
width: 12px;
height: 12px;
border: 3px solid #174b4b;
border-radius: 50%;
background: var(--cyan-bright);
box-shadow: 0 0 0 1px var(--cyan);
}
.scale {
display: flex;
justify-content: space-between;
margin-top: 7px;
color: #789b91;
font-size: 8px;
font-weight: 800;
letter-spacing: 0.13em;
}
.status {
display: flex;
align-items: center;
gap: 9px;
min-height: 40px;
margin-top: 16px;
padding: 10px 11px;
border-radius: 10px;
background: var(--panel-soft);
color: #d9e8df;
font-size: 11px;
line-height: 1.3;
}
.status-dot {
flex: 0 0 auto;
width: 7px;
height: 7px;
border-radius: 50%;
background: var(--cyan);
box-shadow: 0 0 10px rgba(120, 228, 223, 0.8);
}
.panel-note {
margin: 12px 1px 0;
color: #88aaa1;
font-size: 10px;
line-height: 1.5;
}
.legend {
position: fixed;
z-index: 5;
left: 30px;
bottom: 27px;
display: flex;
flex-wrap: wrap;
gap: 15px;
padding: 11px 14px;
border: 1px solid rgba(20, 63, 60, 0.12);
border-radius: 11px;
background: rgba(236, 242, 221, 0.72);
color: #315b55;
font-size: 10px;
font-weight: 700;
pointer-events: none;
backdrop-filter: blur(8px);
}
.legend span {
display: flex;
align-items: center;
gap: 6px;
}
.swatch {
width: 9px;
height: 9px;
border-radius: 2px;
}
.swatch.water {
background: #299db5;
}
.swatch.flood {
background: #68cfc4;
}
.swatch.channel {
background: #a7794d;
}
.hint {
position: fixed;
z-index: 5;
right: 28px;
bottom: 24px;
color: rgba(29, 73, 68, 0.75);
font-size: 9px;
font-weight: 800;
letter-spacing: 0.12em;
text-transform: uppercase;
pointer-events: none;
}
.annotation {
position: fixed;
z-index: 4;
left: 0;
top: 0;
transform: translate(-50%, -100%);
padding: 7px 9px;
border: 1px solid rgba(188, 239, 225, 0.24);
border-radius: 7px;
background: rgba(17, 52, 50, 0.82);
color: #e9f2e7;
font-size: 9px;
font-weight: 850;
letter-spacing: 0.12em;
white-space: nowrap;
pointer-events: none;
opacity: 0;
transition: opacity 120ms ease;
box-shadow: 0 7px 22px rgba(20, 48, 45, 0.15);
}
.annotation::after {
content: "";
position: absolute;
top: 100%;
left: 50%;
width: 1px;
height: 18px;
background: rgba(222, 246, 235, 0.68);
}
.annotation.flood {
border-color: rgba(120, 228, 223, 0.55);
color: #b5fff4;
}
.annotation.channel {
border-color: rgba(233, 185, 105, 0.55);
color: #f1ca88;
}
@media (max-width: 720px) {
.hero {
top: 18px;
left: 18px;
}
.hero p {
max-width: 280px;
font-size: 11px;
}
.hydro-panel {
top: auto;
right: 12px;
bottom: 12px;
left: 12px;
width: auto;
padding: 15px;
}
.panel-note,
.legend,
.hint {
display: none;
}
.level-row {
margin-top: 12px;
}
.status {
margin-top: 11px;
}
.annotation {
font-size: 8px;
}
}
</style>
</head>
<body>
<canvas id="scene" aria-label="Interactive voxel landscape showing a winding river and floodplain"></canvas>
<div class="vignette"></div>
<header class="hero">
<div class="eyebrow">River systems / Voxel atlas</div>
<h1>River<br>Meanders</h1>
<p>
Elevated homes occupy the dry terraces while an abandoned loop records
the river’s former path. Raise the water to watch low swales fill first.
</p>
</header>
<section class="hydro-panel" aria-label="Water level controls">
<div class="panel-kicker">Live floodplain model</div>
<div class="panel-title">
<strong>Water level</strong>
<span>Model datum</span>
</div>
<div class="level-row">
<div class="level-copy">
<label for="waterSlider">Current elevation</label>
<strong id="levelValue">0.05 m</strong>
</div>
<button
id="waterToggle"
class="switch"
type="button"
role="switch"
aria-label="Toggle high flood level"
aria-checked="false"
></button>
</div>
<input
id="waterSlider"
type="range"
min="0.05"
max="1.35"
step="0.01"
value="0.05"
aria-label="Water level"
>
<div class="scale">
<span>LOW SWALE</span>
<span>TERRACE EDGE</span>
</div>
<div class="status" aria-live="polite">
<span class="status-dot"></span>
<span id="waterStatus">Permanent channel only</span>
</div>
<p class="panel-note">
Low ground connects before the higher bank shelves. The switch cycles between
the permanent channel and a terrace-edge flood.
</p>
</section>
<div class="legend" aria-hidden="true">
<span><i class="swatch water"></i>Active channel</span>
<span><i class="swatch flood"></i>Marked floodplain</span>
<span><i class="swatch channel"></i>Abandoned channel</span>
</div>
<div class="hint">Drag to orbit · Scroll to zoom · Arrow keys rotate</div>
<div id="floodLabel" class="annotation flood">MARKED FLOODPLAIN</div>
<div id="channelLabel" class="annotation channel">ABANDONED CHANNEL</div>
<div id="homeLabel" class="annotation">ELEVATED HOMES</div>
<div id="fieldLabel" class="annotation">TERRACE FIELDS</div>
<script type="module">
import * as THREE from "https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
/* --------------------------------------------------------------------------
Scene setup
-------------------------------------------------------------------------- */
const canvas = document.querySelector("#scene");
const renderer = new THREE.WebGLRenderer({
canvas,
antialias: true,
alpha: false,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.13;
const scene = new THREE.Scene();
scene.background = new THREE.Color("#b9d4ce");
scene.fog = new THREE.Fog("#b9d4ce", 48, 78);
const camera = new THREE.OrthographicCamera(-25, 25, 16, -16, 0.1, 120);
camera.position.set(20, 28, 27);
camera.lookAt(0, 0.3, 0);
scene.add(new THREE.HemisphereLight("#dff6ef", "#5b6547", 2.25));
const sun = new THREE.DirectionalLight("#fff1ce", 3.3);
sun.position.set(-13, 25, 14);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -24;
sun.shadow.camera.right = 24;
sun.shadow.camera.top = 24;
sun.shadow.camera.bottom = -24;
sun.shadow.camera.near = 2;
sun.shadow.camera.far = 55;
sun.shadow.bias = -0.00035;
sun.shadow.normalBias = 0.035;
scene.add(sun);
const fill = new THREE.DirectionalLight("#8fd7d6", 0.65);
fill.position.set(18, 10, -18);
scene.add(fill);
/* --------------------------------------------------------------------------
Landscape mathematics
-------------------------------------------------------------------------- */
const TILE = 1;
const NX = 34;
const NZ = 26;
const MIN_X = -17;
const MIN_Z = -13;
const BOTTOM = -1.7;
function riverCenter(x) {
return (
2.6 * Math.sin(x * 0.19) +
1.25 * Math.sin(x * 0.43 + 0.6)
);
}
const mainPath = [];
for (let x = -17; x <= 17; x += 0.5) {
mainPath.push({ x, z: riverCenter(x) });
}
const oldPath = [
{ x: -8.2, z: -2.9 },
{ x: -8.2, z: -0.2 },
{ x: -6.5, z: 2.6 },
{ x: -3.8, z: 4.8 },
{ x: -0.5, z: 5.2 },
{ x: 2.4, z: 4.3 },
{ x: 4.7, z: 2.3 },
{ x: 4.2, z: 1.9 },
{ x: 3.5, z: 2.45 }
];
function distanceToPath(x, z, path) {
let best = Infinity;
for (let i = 0; i < path.length - 1; i++) {
const a = path[i];
const b = path[i + 1];
const vx = b.x - a.x;
const vz = b.z - a.z;
const wx = x - a.x;
const wz = z - a.z;
const lengthSq = vx * vx + vz * vz;
const t = lengthSq
? THREE.MathUtils.clamp((wx * vx + wz * vz) / lengthSq, 0, 1)
: 0;
const dx = x - (a.x + t * vx);
const dz = z - (a.z + t * vz);
const d = dx * dx + dz * dz;
if (d < best) best = d;
}
return Math.sqrt(best);
}
function noise2(x, z) {
const n = Math.sin(x * 12.9898 + z * 78.233) * 43758.5453123;
return n - Math.floor(n);
}
function groundHeight(x, z) {
const dm = distanceToPath(x, z, mainPath);
const dox = distanceToPath(x, z, oldPath);
const n = noise2(Math.round(x * 2), Math.round(z * 2));
if (dm < 1.05) {
return -0.64 + n * 0.04;
}
if (dox < 0.72) {
return 0.08 + n * 0.025;
}
if (dm < 1.9) {
const t = (dm - 1.05) / 0.85;
return 0.10 + t * 0.30 + n * 0.035;
}
if (dox < 1.45) {
const t = (dox - 0.72) / 0.73;
return 0.20 + t * 0.10 + n * 0.025;
}
if (dm < 5.4) {
const t = (dm - 1.9) / 3.5;
const basin = 0.5 + 0.5 * Math.sin(x * 0.55 + z * 0.31);
return 0.26 + t * 0.32 + basin * 0.10;
}
const terrace = 0.5 + 0.5 * Math.sin(z * 0.58 + x * 0.08);
return 0.88 + n * 0.16 + terrace * 0.08;
}
/* --------------------------------------------------------------------------
Shared geometry and materials
-------------------------------------------------------------------------- */
const unitBox = new THREE.BoxGeometry(1, 1, 1);
const terrainMaterial = new THREE.MeshStandardMaterial({
color: 0xffffff,
roughness: 1,
metalness: 0
});
const soilMaterial = new THREE.MeshStandardMaterial({
color: "#73533b",
roughness: 1
});
const woodMaterial = new THREE.MeshStandardMaterial({
color: "#644832",
roughness: 0.92
});
const wallMaterial = new THREE.MeshStandardMaterial({
color: "#e4d5ae",
roughness: 0.88
});
const wallAltMaterial = new THREE.MeshStandardMaterial({
color: "#c9d2b2",
roughness: 0.9
});
const roofMaterials = [
new THREE.MeshStandardMaterial({ color: "#b95d40", roughness: 0.88 }),
new THREE.MeshStandardMaterial({ color: "#8b5844", roughness: 0.9 }),
new THREE.MeshStandardMaterial({ color: "#c17a45", roughness: 0.88 })
];
const windowMaterial = new THREE.MeshStandardMaterial({
color: "#ffe3a0",
emissive: "#e7a84f",
emissiveIntensity: 0.42,
roughness: 0.42
});
const doorMaterial = new THREE.MeshStandardMaterial({
color: "#456d63",
roughness: 0.88
});
const trunkMaterial = new THREE.MeshStandardMaterial({
color: "#654b35",
roughness: 1
});
const leafMaterial = new THREE.MeshStandardMaterial({
color: 0xffffff,
roughness: 1
});
const waterMaterial = new THREE.MeshStandardMaterial({
color: "#2698b3",
roughness: 0.24,
metalness: 0.05,
transparent: true,
opacity: 0.76,
depthWrite: false
});
const waterTopMaterial = new THREE.MeshStandardMaterial({
color: "#6bd9df",
roughness: 0.25,
metalness: 0.02,
transparent: true,
opacity: 0.46,
depthWrite: false
});
const floodTintMaterial = new THREE.MeshBasicMaterial({
color: "#53d4cb",
transparent: true,
opacity: 0.095,
depthWrite: false
});
const markerMaterial = new THREE.MeshStandardMaterial({
color: "#9cf5e9",
emissive: "#3daea8",
emissiveIntensity: 0.22,
roughness: 0.55
});
const crackMaterial = new THREE.MeshStandardMaterial({
color: "#5e4935",
roughness: 1
});
const cloudMaterial = new THREE.MeshStandardMaterial({
color: "#f5f3dc",
roughness: 1,
transparent: true,
opacity: 0.78,
depthWrite: false
});
function addBox(parent, sx, sy, sz, x, y, z, material, castShadow = true) {
const mesh = new THREE.Mesh(unitBox, material);
mesh.scale.set(sx, sy, sz);
mesh.position.set(x, y, z);
mesh.castShadow = castShadow;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
/* --------------------------------------------------------------------------
Terrain columns
-------------------------------------------------------------------------- */
const base = new THREE.Mesh(
new THREE.BoxGeometry(35.1, 0.36, 27.1),
new THREE.MeshStandardMaterial({ color: "#708c79", roughness: 1 })
);
base.position.y = -1.88;
base.receiveShadow = true;
scene.add(base);
const terrain = new THREE.InstancedMesh(
unitBox,
terrainMaterial,
NX * NZ
);
terrain.receiveShadow = true;
terrain.frustumCulled = false;
const dummy = new THREE.Object3D();
const terrainColor = new THREE.Color();
let terrainIndex = 0;
for (let iz = 0; iz < NZ; iz++) {
for (let ix = 0; ix < NX; ix++) {
const x = MIN_X + ix + 0.5;
const z = MIN_Z + iz + 0.5;
const h = groundHeight(x, z);
const dm = distanceToPath(x, z, mainPath);
const dox = distanceToPath(x, z, oldPath);
const n = noise2(ix * 1.7 + 2, iz * 2.3 - 4);
dummy.position.set(x, (h + BOTTOM) * 0.5, z);
dummy.rotation.set(0, 0, 0);
dummy.scale.set(0.985, h - BOTTOM, 0.985);
dummy.updateMatrix();
terrain.setMatrixAt(terrainIndex, dummy.matrix);
if (dm < 1.05) {
terrainColor.set("#486b62");
} else if (dox < 0.76) {
terrainColor.set("#9b7951");
} else if (dm < 1.9) {
terrainColor.set("#7b9164");
} else if (dm < 5.4) {
terrainColor.set("#619271");
} else if (dox < 2.2) {
terrainColor.set("#829768");
} else {
terrainColor.set("#99ae70");
}
terrainColor.offsetHSL(0, 0, (n - 0.5) * 0.07);
terrain.setColorAt(terrainIndex, terrainColor);
terrainIndex++;
}
}
terrain.instanceMatrix.needsUpdate = true;
terrain.instanceColor.needsUpdate = true;
scene.add(terrain);
/* --------------------------------------------------------------------------
Floodplain marking, dry-channel cracks, and rising water
-------------------------------------------------------------------------- */
const waterCells = [];
for (let iz = 0; iz < NZ; iz++) {
for (let ix = 0; ix < NX; ix++) {
const x = MIN_X + ix + 0.5;
const z = MIN_Z + iz + 0.5;
const dm = distanceToPath(x, z, mainPath);
const dox = distanceToPath(x, z, oldPath);
if (dm < 5.45 || dox < 0.94) {
waterCells.push({
x,
z,
h: groundHeight(x, z),
permanent: dm < 1.04
});
}
}
}
const waterMesh = new THREE.InstancedMesh(
unitBox,
waterMaterial,
waterCells.length
);
waterMesh.instanceMatrix.setUsage(THREE.DynamicDrawUsage);
waterMesh.frustumCulled = false;
waterMesh.renderOrder = 2;
scene.add(waterMesh);
const waterTopMesh = new THREE.InstancedMesh(
unitBox,
waterTopMaterial,
waterCells.length
);
waterTopMesh.instanceMatrix.setUsage(THREE.DynamicDrawUsage);
waterTopMesh.frustumCulled = false;
waterTopMesh.renderOrder = 3;
scene.add(waterTopMesh);
const tintCells = waterCells.filter(cell => !cell.permanent);
const floodTint = new THREE.InstancedMesh(
unitBox,
floodTintMaterial,
tintCells.length
);
floodTint.frustumCulled = false;
floodTint.renderOrder = 1;
tintCells.forEach((cell, i) => {
dummy.position.set(cell.x, cell.h + 0.014, cell.z);
dummy.rotation.set(0, 0, 0);
dummy.scale.set(0.97, 0.018, 0.97);
dummy.updateMatrix();
floodTint.setMatrixAt(i, dummy.matrix);
});
floodTint.instanceMatrix.needsUpdate = true;
scene.add(floodTint);
const boundaryCells = [];
for (let x = -16.5; x <= 16.5; x += 1.05) {
boundaryCells.push(
{ x, z: riverCenter(x) - 5.36 },
{ x, z: riverCenter(x) + 5.36 }
);
}
const boundaryMarkers = new THREE.InstancedMesh(
unitBox,
markerMaterial,
boundaryCells.length
);
boundaryMarkers.frustumCulled = false;
boundaryCells.forEach((cell, i) => {
dummy.position.set(
cell.x,
groundHeight(cell.x, cell.z) + 0.075,
cell.z
);
dummy.rotation.set(0, 0, 0);
dummy.scale.set(0.22, 0.14, 0.62);
dummy.updateMatrix();
boundaryMarkers.setMatrixAt(i, dummy.matrix);
});
boundaryMarkers.instanceMatrix.needsUpdate = true;
scene.add(boundaryMarkers);
const crackCells = [];
for (let iz = 0; iz < NZ; iz++) {
for (let ix = 0; ix < NX; ix++) {
const x = MIN_X + ix + 0.5;
const z = MIN_Z + iz + 0.5;
const dm = distanceToPath(x, z, mainPath);
const dox = distanceToPath(x, z, oldPath);
const n = noise2(ix * 3.1 - 8, iz * 2.9 + 1);
if (dm > 1.05 && dox < 0.68 && n > 0.47) {
crackCells.push({ x, z, h: groundHeight(x, z), rotation: n * Math.PI });
}
}
}
const cracks = new THREE.InstancedMesh(
unitBox,
crackMaterial,
crackCells.length
);
cracks.frustumCulled = false;
crackCells.forEach((cell, i) => {
dummy.position.set(cell.x, cell.h + 0.025, cell.z);
dummy.rotation.set(0, cell.rotation, 0);
dummy.scale.set(0.3, 0.025, 0.055);
dummy.updateMatrix();
cracks.setMatrixAt(i, dummy.matrix);
});
cracks.instanceMatrix.needsUpdate = true;
scene.add(cracks);
/* --------------------------------------------------------------------------
Elevated voxel homes
-------------------------------------------------------------------------- */
const houseSpecs = [
{ x: -12.1, z: 2.5, roof: 0, rotation: 0.04 },
{ x: -10.3, z: -4.7, roof: 1, rotation: -0.03 },
{ x: -4.1, z: -7.2, roof: 2, rotation: 0.02 },
{ x: 0.7, z: -4.7, roof: 0, rotation: -0.05 },
{ x: 10.7, z: -3.6, roof: 1, rotation: 0.03 },
{ x: 12.7, z: 3.2, roof: 2, rotation: -0.04 },
{ x: 3.5, z: 8.6, roof: 0, rotation: 0.06 }
];
function createHouse(spec, index) {
const group = new THREE.Group();
group.position.set(spec.x, 0, spec.z);
group.rotation.y = spec.rotation;
const ground = groundHeight(spec.x, spec.z);
const floorY = ground + 1.28;
// Stilts and cross bracing.
for (const sx of [-0.65, 0.65]) {
for (const sz of [-0.55, 0.55]) {
addBox(group, 0.19, 1.28, 0.19, sx, ground + 0.64, sz, woodMaterial);
}
}
addBox(group, 1.55, 0.13, 0.16, 0, ground + 0.58, -0.55, woodMaterial);
addBox(group, 1.55, 0.13, 0.16, 0, ground + 0.58, 0.55, woodMaterial);
// Raised floor, body and porch.
addBox(group, 2.05, 0.18, 1.85, 0, floorY, 0, woodMaterial);
addBox(
group,
1.75,
1.25,
1.48,
0,
floorY + 0.72,
0,
index % 2 ? wallAltMaterial : wallMaterial
);
addBox(group, 1.05, 0.12, 0.62, 0, floorY + 0.04, 1.08, woodMaterial);
// Stepped voxel gable roof.
const roofY = floorY + 1.39;
const roofLayers = [
{ sx: 2.05, sz: 1.91 },
{ sx: 1.66, sz: 1.37 },
{ sx: 1.22, sz: 0.79 }
];
roofLayers.forEach((layer, i) => {
addBox(
group,
layer.sx,
0.24,
layer.sz,
0,
roofY + i * 0.19,
0,
roofMaterials[spec.roof]
);
});
addBox(group, 0.24, 0.62, 0.26, 0.55, roofY + 0.72, -0.25, woodMaterial);
// Windows and door on the camera-facing side.
addBox(group, 0.34, 0.42, 0.045, -0.52, floorY + 0.88, 0.765, windowMaterial);
addBox(group, 0.34, 0.42, 0.045, 0.52, floorY + 0.88, 0.765, windowMaterial);
addBox(group, 0.38, 0.72, 0.05, 0, floorY + 0.47, 0.77, doorMaterial);
// Small stair blocks descend from the porch.
for (let i = 0; i < 3; i++) {
addBox(
group,
0.68,
0.16,
0.38,
0,
floorY - 0.13 - i * 0.25,
1.08 + i * 0.31,
woodMaterial
);
}
scene.add(group);
}
houseSpecs.forEach(createHouse);
/* --------------------------------------------------------------------------
Terrace fields
-------------------------------------------------------------------------- */
const fieldSpecs = [
{ x: -12.0, z: -8.3, w: 5, d: 4, color: "#b8c65f" },
{ x: 8.2, z: 7.6, w: 6, d: 4, color: "#d1ad55" },
{ x: 11.5, z: -7.2, w: 5, d: 3, color: "#87b963" }
];
const fieldCells = [];
fieldSpecs.forEach(field => {
for (let iz = 0; iz < field.d; iz++) {
for (let ix = 0; ix < field.w; ix++) {
const x = field.x - field.w * 0.5 + ix + 0.5;
const z = field.z - field.d * 0.5 + iz + 0.5;
fieldCells.push({
x,
z,
h: groundHeight(x, z),
color: field.color
});
}
}
// Small field markers.
const h = groundHeight(field.x, field.z);
addBox(
scene,
0.12,
1.05,
0.12,
field.x - field.w * 0.5,
h + 0.52,
field.z + field.d * 0.5,
woodMaterial
);
addBox(
scene,
0.78,
0.42,
0.12,
field.x - field.w * 0.5 + 0.33,
h + 0.92,
field.z + field.d * 0.5,
soilMaterial
);
});
const fieldSoil = new THREE.InstancedMesh(
unitBox,
soilMaterial,
fieldCells.length
);
const fieldCrops = new THREE.InstancedMesh(
unitBox,
leafMaterial,
fieldCells.length
);
fieldSoil.frustumCulled = false;
fieldCrops.frustumCulled = false;
fieldCrops.castShadow = true;
const cropColor = new THREE.Color();
fieldCells.forEach((cell, i) => {
dummy.position.set(cell.x, cell.h + 0.045, cell.z);
dummy.rotation.set(0, 0, 0);
dummy.scale.set(0.9, 0.09, 0.9);
dummy.updateMatrix();
fieldSoil.setMatrixAt(i, dummy.matrix);
dummy.position.set(cell.x, cell.h + 0.22, cell.z);
dummy.scale.set(0.36, 0.29, 0.36);
dummy.updateMatrix();
fieldCrops.setMatrixAt(i, dummy.matrix);
cropColor.set(cell.color);
cropColor.offsetHSL(0, 0, (noise2(i, 2) - 0.5) * 0.08);
fieldCrops.setColorAt(i, cropColor);
});
fieldSoil.instanceMatrix.needsUpdate = true;
fieldCrops.instanceMatrix.needsUpdate = true;
fieldCrops.instanceColor.needsUpdate = true;
scene.add(fieldSoil, fieldCrops);
/* --------------------------------------------------------------------------
Voxel trees
-------------------------------------------------------------------------- */
function mulberry32(seed) {
return function () {
let t = seed += 0x6D2B79F5;
t = Math.imul(t ^ t >>> 15, t | 1);
t ^= t + Math.imul(t ^ t >>> 7, t | 61);
return ((t ^ t >>> 14) >>> 0) / 4294967296;
};
}
const random = mulberry32(70421);
const trees = [];
function nearHouse(x, z, margin = 2.1) {
return houseSpecs.some(h => Math.hypot(h.x - x, h.z - z) < margin);
}
function insideField(x, z, margin = 0.65) {
return fieldSpecs.some(f =>
Math.abs(x - f.x) < f.w * 0.5 + margin &&
Math.abs(z - f.z) < f.d * 0.5 + margin
);
}
for (let attempt = 0; attempt < 500 && trees.length < 34; attempt++) {
const x = -16 + random() * 32;
const z = -12 + random() * 24;
const dm = distanceToPath(x, z, mainPath);
const dox = distanceToPath(x, z, oldPath);
if (dm < 2.25 || dox < 1.45) continue;
if (nearHouse(x, z) || insideField(x, z)) continue;
if (trees.some(tree => Math.hypot(tree.x - x, tree.z - z) < 1.15)) continue;
trees.push({
x,
z,
h: groundHeight(x, z),
scale: 0.78 + random() * 0.4,
tint: random()
});
}
const trunks = new THREE.InstancedMesh(
unitBox,
trunkMaterial,
trees.length
);
const leaves = new THREE.InstancedMesh(
unitBox,
leafMaterial,
trees.length * 3
);
trunks.frustumCulled = false;
leaves.frustumCulled = false;
trunks.castShadow = true;
leaves.castShadow = true;
const leafColor = new THREE.Color();
trees.forEach((tree, i) => {
const s = tree.scale;
dummy.position.set(tree.x, tree.h + 0.53 * s, tree.z);
dummy.rotation.set(0, 0, 0);
dummy.scale.set(0.26 * s, 1.06 * s, 0.26 * s);
dummy.updateMatrix();
trunks.setMatrixAt(i, dummy.matrix);
const layers = [
{ y: 1.22, sx: 1.02, sy: 0.72, sz: 1.02 },
{ y: 1.67, sx: 0.78, sy: 0.62, sz: 0.78 },
{ y: 2.05, sx: 0.52, sy: 0.48, sz: 0.52 }
];
layers.forEach((layer, j) => {
dummy.position.set(tree.x, tree.h + layer.y * s, tree.z);
dummy.rotation.set(0, 0, 0);
dummy.scale.set(
layer.sx * s,
layer.sy * s,
layer.sz * s
);
dummy.updateMatrix();
leaves.setMatrixAt(i * 3 + j, dummy.matrix);
leafColor.set(tree.tint > 0.5 ? "#477b55" : "#5f925b");
leafColor.offsetHSL(0, 0, (j - 1) * 0.035);
leaves.setColorAt(i * 3 + j, leafColor);
});
});
trunks.instanceMatrix.needsUpdate = true;
leaves.instanceMatrix.needsUpdate = true;
leaves.instanceColor.needsUpdate = true;
scene.add(trunks, leaves);
/* --------------------------------------------------------------------------
Block clouds
-------------------------------------------------------------------------- */
const cloudBlocks = [
[-12, 9.2, -5, 3.2, 0.8, 1.5],
[-10.3, 9.4, -5, 1.8, 1.0, 1.6],
[-13.6, 8.7, -4.6, 1.5, 0.65, 1.1],
[1, 10.1, -8, 3.8, 0.75, 1.5],
[3.2, 10.3, -8.2, 1.8, 0.9, 1.2],
[10.8, 8.8, 5, 2.8, 0.7, 1.3],
[13.2, 8.6, 4.8, 1.5, 0.8, 1.1],
[-4, 10.4, 8, 2.5, 0.75, 1.4]
];
const clouds = new THREE.InstancedMesh(
unitBox,
cloudMaterial,
cloudBlocks.length
);
clouds.frustumCulled = false;
cloudBlocks.forEach((block, i) => {
dummy.position.set(block[0], block[1], block[2]);
dummy.rotation.set(0, 0, 0);
dummy.scale.set(block[3], block[4], block[5]);
dummy.updateMatrix();
clouds.setMatrixAt(i, dummy.matrix);
});
clouds.instanceMatrix.needsUpdate = true;
scene.add(clouds);
/* --------------------------------------------------------------------------
Flowing foam blocks
-------------------------------------------------------------------------- */
const foamMaterial = new THREE.MeshStandardMaterial({
color: "#d8fff4",
emissive: "#8cddd5",
emissiveIntensity: 0.15,
transparent: true,
opacity: 0.72,
roughness: 0.45
});
const foam = [];
for (let i = 0; i < 12; i++) {
const mesh = new THREE.Mesh(unitBox, foamMaterial);
mesh.scale.set(0.25 + random() * 0.18, 0.035, 0.11);
mesh.castShadow = false;
scene.add(mesh);
foam.push({ mesh, phase: random() * 34, lateral: (random() - 0.5) * 0.8 });
}
/* --------------------------------------------------------------------------
Direct camera controls: no addons
-------------------------------------------------------------------------- */
let azimuth = 0.68;
let polar = 0.82;
let zoom = 1;
let targetAzimuth = azimuth;
let targetPolar = polar;
let targetZoom = zoom;
let dragging = false;
let previousX = 0;
let previousY = 0;
const cameraRadius = 42;
canvas.addEventListener("pointerdown", event => {
dragging = true;
previousX = event.clientX;
previousY = event.clientY;
canvas.setPointerCapture(event.pointerId);
});
canvas.addEventListener("pointermove", event => {
if (!dragging) return;
const dx = event.clientX - previousX;
const dy = event.clientY - previousY;
targetAzimuth -= dx * 0.006;
targetPolar = THREE.MathUtils.clamp(
targetPolar - dy * 0.005,
0.45,
1.25
);
previousX = event.clientX;
previousY = event.clientY;
});
function endDrag(event) {
dragging = false;
if (event.pointerId !== undefined && canvas.hasPointerCapture(event.pointerId)) {
canvas.releasePointerCapture(event.pointerId);
}
}
canvas.addEventListener("pointerup", endDrag);
canvas.addEventListener("pointercancel", endDrag);
canvas.addEventListener("wheel", event => {
event.preventDefault();
targetZoom = THREE.MathUtils.clamp(
targetZoom * Math.exp(-event.deltaY * 0.001),
0.68,
1.85
);
}, { passive: false });
window.addEventListener("keydown", event => {
if (event.target instanceof HTMLInputElement) return;
if (event.key === "ArrowLeft") targetAzimuth += 0.1;
if (event.key === "ArrowRight") targetAzimuth -= 0.1;
if (event.key === "ArrowUp") targetPolar -= 0.08;
if (event.key === "ArrowDown") targetPolar += 0.08;
targetPolar = THREE.MathUtils.clamp(targetPolar, 0.45, 1.25);
});
function resize() {
const width = window.innerWidth;
const height = window.innerHeight;
const aspect = width / height;
const viewHeight = aspect < 1 ? 42 / aspect : 31;
camera.left = -viewHeight * aspect * 0.5;
camera.right = viewHeight * aspect * 0.5;
camera.top = viewHeight * 0.5;
camera.bottom = -viewHeight * 0.5;
camera.updateProjectionMatrix();
renderer.setSize(width, height);
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
}
window.addEventListener("resize", resize);
resize();
/* --------------------------------------------------------------------------
Water controls and annotations
-------------------------------------------------------------------------- */
const slider = document.querySelector("#waterSlider");
const toggle = document.querySelector("#waterToggle");
const levelValue = document.querySelector("#levelValue");
const waterStatus = document.querySelector("#waterStatus");
let currentLevel = 0.05;
let targetLevel = 0.05;
let scrubbing = false;
slider.addEventListener("pointerdown", () => {
scrubbing = true;
});
window.addEventListener("pointerup", () => {
scrubbing = false;
});
slider.addEventListener("input", () => {
targetLevel = Number(slider.value);
toggle.setAttribute("aria-checked", String(targetLevel > 0.65));
});
toggle.addEventListener("click", () => {
targetLevel = targetLevel > 0.65 ? 0.05 : 1.28;
});
const annotationElements = [
{
element: document.querySelector("#floodLabel"),
position: new THREE.Vector3(6.8, 1.05, -3.8)
},
{
element: document.querySelector("#channelLabel"),
position: new THREE.Vector3(-2.6, 0.9, 5.25)
},
{
element: document.querySelector("#homeLabel"),
position: new THREE.Vector3(-11.2, 3.0, -4.8)
},
{
element: document.querySelector("#fieldLabel"),
position: new THREE.Vector3(9.8, 1.1, 7.7)
}
];
const projected = new THREE.Vector3();
function updateAnnotations() {
const width = window.innerWidth;
const height = window.innerHeight;
annotationElements.forEach(annotation => {
projected.copy(annotation.position).project(camera);
const visible = projected.z > -1 && projected.z < 1;
annotation.element.style.opacity = visible ? "1" : "0";
annotation.element.style.left = `${(projected.x * 0.5 + 0.5) * width}px`;
annotation.element.style.top = `${(-projected.y * 0.5 + 0.5) * height}px`;
});
}
function statusForLevel(level) {
if (level < 0.13) return "Permanent channel only";
if (level < 0.30) return "Lowest swales are beginning to fill";
if (level < 0.52) return "First low shelves are submerged";
if (level < 0.78) return "Floodplain channels are connecting";
if (level < 1.08) return "Upper bank storage is filling";
return "Terrace edge reached; homes remain elevated";
}
/* --------------------------------------------------------------------------
Animation
-------------------------------------------------------------------------- */
function updateWater(level) {
waterCells.forEach((cell, i) => {
let active = cell.permanent || level > cell.h + 0.003;
let top = level;
if (cell.permanent) {
top = Math.max(0.05, level);
active = top > cell.h;
}
if (active) {
const bottom = cell.h;
const height = Math.max(0.012, top - bottom);
dummy.position.set(cell.x, (top + bottom) * 0.5, cell.z);
dummy.rotation.set(0, 0, 0);
dummy.scale.set(0.985, height, 0.985);
dummy.updateMatrix();
waterMesh.setMatrixAt(i, dummy.matrix);
dummy.position.set(cell.x, top + 0.017, cell.z);
dummy.scale.set(0.985, 0.025, 0.985);
dummy.updateMatrix();
waterTopMesh.setMatrixAt(i, dummy.matrix);
} else {
dummy.position.set(cell.x, cell.h - 0.04, cell.z);
dummy.rotation.set(0, 0, 0);
dummy.scale.set(0.985, 0.001, 0.985);
dummy.updateMatrix();
waterMesh.setMatrixAt(i, dummy.matrix);
dummy.position.set(cell.x, cell.h - 0.05, cell.z);
dummy.scale.set(0.985, 0.001, 0.985);
dummy.updateMatrix();
waterTopMesh.setMatrixAt(i, dummy.matrix);
}
});
waterMesh.instanceMatrix.needsUpdate = true;
waterTopMesh.instanceMatrix.needsUpdate = true;
}
const clock = new THREE.Clock();
let elapsed = 0;
renderer.setAnimationLoop(() => {
const dt = Math.min(clock.getDelta(), 0.05);
elapsed += dt;
// Smoothly approach the selected level.
const easing = 1 - Math.exp(-dt * 2.4);
currentLevel += (targetLevel - currentLevel) * easing;
if (Math.abs(currentLevel - targetLevel) < 0.0005) {
currentLevel = targetLevel;
}
// Smooth direct camera controls.
const cameraEase = 1 - Math.exp(-dt * 8);
azimuth += (targetAzimuth - azimuth) * cameraEase;
polar += (targetPolar - polar) * cameraEase;
zoom += (targetZoom - zoom) * cameraEase;
const horizontal = Math.sin(polar) * cameraRadius;
camera.position.set(
Math.sin(azimuth) * horizontal,
Math.cos(polar) * cameraRadius,
Math.cos(azimuth) * horizontal
);
camera.lookAt(0, 0.3, 0);
camera.zoom = zoom;
camera.updateProjectionMatrix();
updateWater(currentLevel);
const waterTop = Math.max(0.05, currentLevel);
foam.forEach(item => {
const x = -16.5 + ((elapsed * 1.55 + item.phase) % 33);
const z = riverCenter(x) + item.lateral;
item.mesh.position.set(x, waterTop + 0.05, z);
item.mesh.rotation.y = -Math.atan(
(riverCenter(x + 0.1) - riverCenter(x - 0.1)) / 0.2
);
});
waterTopMaterial.opacity = 0.42 + Math.sin(elapsed * 1.5) * 0.045;
levelValue.textContent = `${currentLevel.toFixed(2)} m`;
waterStatus.textContent = statusForLevel(currentLevel);
toggle.setAttribute("aria-checked", String(targetLevel > 0.65));
if (!scrubbing) {
slider.value = currentLevel.toFixed(2);
}
const percentage = ((currentLevel - 0.05) / (1.35 - 0.05)) * 100;
slider.style.setProperty("--pct", `${percentage}%`);
updateAnnotations();
renderer.render(scene, camera);
});
</script>
</body>
</html>
```