Prompt specification
Build or customize the Three.js 3D scene "Mountain water supply timeline" 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: Mountain water supply timeline
Category: Space & Earth · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/mountain-water-supply-timeline-x1152
Scene Description Prompt:
Create a standalone low-poly scene in one HTML file using Three.js from cdnjs. Build a village below a glacier-fed valley; toggle schematic seasonal snow and meltwater storage states, separating visible seasonal changes from long-term retreat labels.
Working Three.js Starter (mountain-water-supply-timeline-x1152):
```html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Glacier Valley — Seasonal and Long-Term Context</title>
<style>
:root {
color-scheme: dark;
--ink: #ecf4f2;
--muted: #a9bbb8;
--panel: rgba(17, 31, 34, 0.9);
--line: rgba(220, 239, 235, 0.16);
--snow: #dff4f3;
--water: #55c4d2;
--retreat: #f2ad59;
--ground: #6f865f;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #dbe7e9;
font-family:
Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
button,
input {
font: inherit;
}
#app,
#app canvas {
position: fixed;
inset: 0;
width: 100%;
height: 100%;
}
#app canvas {
display: block;
touch-action: none;
outline: none;
}
#labels {
position: fixed;
inset: 0;
z-index: 2;
pointer-events: none;
overflow: hidden;
}
.panel {
position: fixed;
z-index: 10;
top: 18px;
left: 18px;
width: min(365px, calc(100vw - 36px));
max-height: calc(100vh - 36px);
overflow: auto;
padding: 20px;
border: 1px solid var(--line);
border-radius: 18px;
color: var(--ink);
background:
linear-gradient(145deg, rgba(255,255,255,0.035), transparent 45%),
var(--panel);
box-shadow: 0 20px 60px rgba(20, 35, 38, 0.24);
backdrop-filter: blur(16px) saturate(120%);
scrollbar-width: thin;
}
.eyebrow {
display: flex;
align-items: center;
gap: 8px;
margin-bottom: 10px;
color: var(--water);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.16em;
text-transform: uppercase;
}
.eyebrow::before {
width: 22px;
height: 2px;
content: "";
background: currentColor;
}
h1 {
margin: 0;
font-size: clamp(28px, 4vw, 39px);
font-weight: 720;
letter-spacing: -0.045em;
line-height: 0.98;
}
.intro {
margin: 13px 0 20px;
color: var(--muted);
font-size: 12.5px;
line-height: 1.55;
}
.section {
padding-top: 15px;
border-top: 1px solid var(--line);
}
.section:first-of-type {
border-top: 0;
}
.section-title {
display: flex;
align-items: center;
justify-content: space-between;
gap: 12px;
margin-bottom: 10px;
font-size: 11px;
font-weight: 750;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.section-title span:last-child {
color: var(--muted);
font-size: 10px;
font-weight: 600;
letter-spacing: 0.02em;
text-align: right;
text-transform: none;
}
.segmented {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 5px;
padding: 4px;
border: 1px solid var(--line);
border-radius: 12px;
background: rgba(0, 0, 0, 0.13);
}
.season-button {
min-height: 43px;
padding: 7px 5px;
border: 0;
border-radius: 9px;
color: var(--muted);
background: transparent;
cursor: pointer;
font-size: 11px;
font-weight: 700;
transition:
color 160ms ease,
background 160ms ease,
transform 160ms ease;
}
.season-button:hover {
color: var(--ink);
background: rgba(255,255,255,0.055);
}
.season-button[aria-pressed="true"] {
color: #10282c;
background: #d9efec;
box-shadow: 0 3px 12px rgba(0,0,0,0.16);
}
.season-button:active {
transform: translateY(1px);
}
.toggles {
display: grid;
gap: 9px;
margin-top: 13px;
}
.toggle {
display: flex;
align-items: center;
justify-content: space-between;
gap: 12px;
color: #d5e0de;
cursor: pointer;
font-size: 12px;
}
.toggle input {
position: absolute;
width: 1px;
height: 1px;
opacity: 0;
pointer-events: none;
}
.switch {
position: relative;
flex: 0 0 auto;
width: 34px;
height: 19px;
border: 1px solid rgba(255,255,255,0.2);
border-radius: 20px;
background: rgba(0,0,0,0.24);
transition: 160ms ease;
}
.switch::after {
position: absolute;
top: 3px;
left: 3px;
width: 11px;
height: 11px;
border-radius: 50%;
content: "";
background: #849694;
transition: 160ms ease;
}
.toggle input:checked + .switch {
border-color: rgba(85, 196, 210, 0.55);
background: rgba(85, 196, 210, 0.22);
}
.toggle input:checked + .switch::after {
left: 18px;
background: var(--water);
box-shadow: 0 0 10px rgba(85, 196, 210, 0.4);
}
.toggle input:focus-visible + .switch {
outline: 2px solid var(--water);
outline-offset: 3px;
}
.toggle.retreat-toggle input:checked + .switch {
border-color: rgba(242, 173, 89, 0.55);
background: rgba(242, 173, 89, 0.18);
}
.toggle.retreat-toggle input:checked + .switch::after {
background: var(--retreat);
box-shadow: 0 0 10px rgba(242, 173, 89, 0.35);
}
.meters {
display: grid;
gap: 9px;
margin-top: 14px;
}
.meter {
display: grid;
grid-template-columns: 75px 1fr 28px;
align-items: center;
gap: 8px;
color: var(--muted);
font-size: 10px;
}
.meter-track {
height: 4px;
overflow: hidden;
border-radius: 10px;
background: rgba(255,255,255,0.09);
}
.meter-fill {
width: 0;
height: 100%;
border-radius: inherit;
background: var(--water);
transition: width 120ms linear;
}
#snowMeter {
background: var(--snow);
}
#storageMeter {
background: #4b9fae;
}
.meter-value {
color: #d8e5e2;
font-variant-numeric: tabular-nums;
text-align: right;
}
.context-box {
margin-top: 13px;
padding: 11px 12px;
border: 1px solid rgba(242, 173, 89, 0.22);
border-radius: 10px;
color: #d7c5aa;
background: rgba(242, 173, 89, 0.06);
font-size: 10.5px;
line-height: 1.5;
}
.context-box strong {
color: #ffd095;
}
.panel-footer {
display: flex;
align-items: center;
justify-content: space-between;
gap: 12px;
margin-top: 15px;
color: #819794;
font-size: 9.5px;
letter-spacing: 0.03em;
}
.reset-button {
padding: 5px 8px;
border: 1px solid var(--line);
border-radius: 7px;
color: #bfcfcc;
background: transparent;
cursor: pointer;
font-size: 10px;
}
.reset-button:hover {
color: var(--ink);
background: rgba(255,255,255,0.05);
}
.mode-badge {
position: fixed;
z-index: 5;
top: 20px;
right: 20px;
padding: 9px 12px;
border: 1px solid rgba(255,255,255,0.42);
border-radius: 30px;
color: #294344;
background: rgba(239, 247, 245, 0.74);
box-shadow: 0 8px 30px rgba(38, 65, 67, 0.12);
backdrop-filter: blur(10px);
font-size: 9px;
font-weight: 800;
letter-spacing: 0.12em;
text-transform: uppercase;
pointer-events: none;
}
.legend {
position: fixed;
z-index: 5;
right: 20px;
bottom: 18px;
display: flex;
flex-wrap: wrap;
justify-content: flex-end;
gap: 8px;
max-width: min(520px, calc(100vw - 40px));
}
.legend-item {
display: flex;
align-items: center;
gap: 7px;
padding: 7px 10px;
border: 1px solid rgba(255,255,255,0.45);
border-radius: 8px;
color: #3e5554;
background: rgba(240, 247, 245, 0.79);
box-shadow: 0 5px 18px rgba(40, 60, 62, 0.08);
backdrop-filter: blur(9px);
font-size: 9.5px;
font-weight: 650;
}
.legend-swatch {
width: 17px;
height: 4px;
border-radius: 5px;
background: var(--water);
}
.legend-swatch.snow {
background: #eef7f4;
box-shadow: 0 0 0 1px #a8c5c4;
}
.legend-swatch.retreat {
height: 0;
border-top: 2px dashed var(--retreat);
border-radius: 0;
background: transparent;
}
.hint {
position: fixed;
z-index: 4;
left: 50%;
bottom: 17px;
transform: translateX(-50%);
color: rgba(38, 64, 65, 0.72);
font-size: 9px;
font-weight: 650;
letter-spacing: 0.04em;
pointer-events: none;
white-space: nowrap;
}
.scene-label {
position: absolute;
z-index: 3;
min-width: 110px;
padding: 7px 9px;
border: 1px solid rgba(255,255,255,0.72);
border-left: 3px solid #70827f;
border-radius: 7px;
color: #294041;
background: rgba(245, 249, 247, 0.83);
box-shadow: 0 7px 22px rgba(25, 48, 50, 0.13);
backdrop-filter: blur(7px);
transform: translate(-50%, -50%);
font-size: 10px;
font-weight: 760;
line-height: 1.15;
white-space: nowrap;
pointer-events: none;
}
.scene-label .label-kicker {
display: block;
margin-bottom: 3px;
color: #788986;
font-size: 7.5px;
font-weight: 850;
letter-spacing: 0.13em;
text-transform: uppercase;
}
.scene-label.seasonal {
border-left-color: var(--water);
}
.scene-label.seasonal .label-kicker {
color: #218b9c;
}
.scene-label.retreat {
border-color: rgba(242, 173, 89, 0.68);
border-left-color: var(--retreat);
color: #62451f;
background: rgba(255, 245, 226, 0.9);
}
.scene-label.retreat .label-kicker {
color: #b66c19;
}
.scene-label.place {
border-left-color: #647f6d;
background: rgba(238, 244, 237, 0.78);
}
@media (max-width: 720px) {
.panel {
top: 10px;
left: 10px;
width: min(340px, calc(100vw - 20px));
max-height: calc(100vh - 76px);
padding: 16px;
border-radius: 14px;
}
.intro {
margin-bottom: 14px;
}
.mode-badge {
top: auto;
right: 10px;
bottom: 50px;
}
.legend {
right: 10px;
bottom: 10px;
max-width: calc(100vw - 20px);
}
.legend-item {
padding: 5px 7px;
font-size: 8px;
}
.hint {
display: none;
}
}
</style>
</head>
<body>
<div id="app"></div>
<div id="labels" aria-hidden="true"></div>
<aside class="panel" aria-label="Valley visualization controls">
<div class="eyebrow">Glacier Valley · Schematic</div>
<h1>Ice above.<br>Village below.</h1>
<p class="intro">
Explore a low-poly glacier-fed valley. Seasonal controls change only the
annual snowpack, runoff, and stored water—not the underlying glacier.
</p>
<section class="section">
<div class="section-title">
<span>Seasonal state</span>
<span id="seasonReadout">Winter · snow stored</span>
</div>
<div class="segmented" role="group" aria-label="Seasonal state">
<button class="season-button" data-season="winter" aria-pressed="true">
Winter
</button>
<button class="season-button" data-season="thaw" aria-pressed="false">
Melt pulse
</button>
<button class="season-button" data-season="summer" aria-pressed="false">
Summer
</button>
</div>
<div class="toggles">
<label class="toggle">
<span>Seasonal snowpack layer</span>
<input id="snowToggle" type="checkbox" checked>
<span class="switch"></span>
</label>
<label class="toggle">
<span>Meltwater & storage layer</span>
<input id="waterToggle" type="checkbox" checked>
<span class="switch"></span>
</label>
<label class="toggle retreat-toggle">
<span>Long-term retreat context</span>
<input id="retreatToggle" type="checkbox" checked>
<span class="switch"></span>
</label>
</div>
<div class="meters" aria-label="Current schematic values">
<div class="meter">
<span>Snowpack</span>
<span class="meter-track"><span id="snowMeter" class="meter-fill"></span></span>
<span id="snowValue" class="meter-value">100%</span>
</div>
<div class="meter">
<span>Runoff</span>
<span class="meter-track"><span id="runoffMeter" class="meter-fill"></span></span>
<span id="runoffValue" class="meter-value">18%</span>
</div>
<div class="meter">
<span>Storage</span>
<span class="meter-track"><span id="storageMeter" class="meter-fill"></span></span>
<span id="storageValue" class="meter-value">92%</span>
</div>
</div>
</section>
<section class="section">
<div class="section-title">
<span>Reading the scene</span>
<span>Two separate time scales</span>
</div>
<div class="context-box">
<strong>Seasonal:</strong> pale snow cover, blue runoff, and cistern levels
respond to the state buttons.<br>
<strong>Long-term context:</strong> amber dashed margin markers remain
fixed while the seasons change. The glacier outline is the fixed baseline.
</div>
<div class="panel-footer">
<span>SCHEMATIC · NOT TO SCALE</span>
<button id="resetView" class="reset-button" type="button">Reset view</button>
</div>
</section>
</aside>
<div id="modeBadge" class="mode-badge">Schematic · Winter</div>
<div class="legend" aria-label="Scene legend">
<div class="legend-item"><span class="legend-swatch snow"></span>Seasonal snow</div>
<div class="legend-item"><span class="legend-swatch"></span>Seasonal meltwater</div>
<div class="legend-item"><span class="legend-swatch retreat"></span>Long-term retreat context</div>
</div>
<div class="hint">Drag to orbit · Shift/right-drag to pan · Scroll to zoom</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// -------------------------------------------------------------------------
// Renderer, scene, camera
// -------------------------------------------------------------------------
const app = document.getElementById("app");
const labelLayer = document.getElementById("labels");
const renderer = new THREE.WebGLRenderer({
antialias: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio || 1, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.08;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
app.appendChild(renderer.domElement);
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xdce8e9);
scene.fog = new THREE.Fog(0xdce8e9, 82, 155);
const camera = new THREE.PerspectiveCamera(
42,
window.innerWidth / window.innerHeight,
0.1,
260
);
const hemisphere = new THREE.HemisphereLight(0xe5f4ff, 0x667154, 2.15);
scene.add(hemisphere);
const sun = new THREE.DirectionalLight(0xfff2d7, 3.0);
sun.position.set(-32, 58, 24);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -58;
sun.shadow.camera.right = 58;
sun.shadow.camera.top = 58;
sun.shadow.camera.bottom = -58;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 145;
sun.shadow.bias = -0.00035;
sun.shadow.normalBias = 0.035;
scene.add(sun);
const world = new THREE.Group();
scene.add(world);
// -------------------------------------------------------------------------
// Deterministic helpers and terrain height field
// -------------------------------------------------------------------------
const clamp = (v, a = 0, b = 1) => Math.min(b, Math.max(a, v));
const lerp = (a, b, t) => a + (b - a) * t;
function smoothstep(edge0, edge1, value) {
const t = clamp((value - edge0) / (edge1 - edge0));
return t * t * (3 - 2 * t);
}
let randomSeed = 1937;
function random() {
randomSeed =
(randomSeed * 1664525 + 1013904223) >>> 0;
return randomSeed / 4294967296;
}
function heightAt(x, z) {
const north = clamp((-z + 8) / 44);
const valleyFloor = 0.9 + 10.8 * Math.pow(north, 1.35);
const sideDistance = Math.max(0, Math.abs(x) - 6);
const sideRise =
Math.pow(sideDistance, 1.18) *
0.56 *
(0.72 + north * 0.54);
const roughness =
(
Math.sin(x * 0.47 + z * 0.21) * 0.64 +
Math.sin(x * 0.19 - z * 0.37) * 0.38 +
Math.sin(x * 0.83 + z * 0.11) * 0.22
) *
Math.min(2.7, sideDistance * 0.16 + 0.13);
let height = valleyFloor + sideRise + roughness;
// Flatten the village terrace.
const villageDistance = Math.hypot((x + 0.5) / 11.5, (z - 13) / 8.0);
const villageBlend = 1 - smoothstep(0.55, 1.0, villageDistance);
height = lerp(height, 1.05 + Math.sin(x * 0.4) * 0.025, villageBlend * 0.9);
// Create a shallow reservoir basin.
const basinDistance = Math.hypot(x - 7.2, z - 2.0);
const basinBlend = 1 - smoothstep(2.3, 4.8, basinDistance);
height = lerp(height, 1.14, basinBlend * 0.93);
return height;
}
function terrainColor(height, x, z) {
let color;
if (height > 19) {
color = new THREE.Color(0x7e8581);
} else if (height > 13) {
color = new THREE.Color(0x888a78);
} else if (height > 7) {
color = new THREE.Color(0x788570);
} else {
color = new THREE.Color(0x78966c);
}
const moisture = Math.sin(x * 0.55 + z * 0.16) * 0.025;
color.offsetHSL(moisture, 0, (random() - 0.5) * 0.055);
return color;
}
// -------------------------------------------------------------------------
// Faceted terrain and matching seasonal snow overlay
// -------------------------------------------------------------------------
const terrainGroup = new THREE.Group();
world.add(terrainGroup);
const nx = 37;
const nz = 49;
const xMin = -34;
const xMax = 34;
const zMin = -48;
const zMax = 26;
const grid = [];
for (let iz = 0; iz <= nz; iz++) {
const row = [];
for (let ix = 0; ix <= nx; ix++) {
const edgeX = ix === 0 || ix === nx;
const edgeZ = iz === 0 || iz === nz;
const x =
lerp(xMin, xMax, ix / nx) +
(edgeX ? 0 : (random() - 0.5) * 0.66);
const z =
lerp(zMin, zMax, iz / nz) +
(edgeZ ? 0 : (random() - 0.5) * 0.58);
row.push({ x, z, y: heightAt(x, z) });
}
grid.push(row);
}
const terrainPositions = [];
const terrainColors = [];
const terrainTriangles = [];
function pushTerrainTriangle(a, b, c) {
const averageHeight = (a.y + b.y + c.y) / 3;
const color = terrainColor(averageHeight, (a.x + b.x + c.x) / 3, (a.z + b.z + c.z) / 3);
terrainPositions.push(
a.x, a.y, a.z,
b.x, b.y, b.z,
c.x, c.y, c.z
);
for (let i = 0; i < 3; i++) {
terrainColors.push(color.r, color.g, color.b);
}
terrainTriangles.push([a, b, c]);
}
for (let iz = 0; iz < nz; iz++) {
for (let ix = 0; ix < nx; ix++) {
const a = grid[iz][ix];
const b = grid[iz][ix + 1];
const c = grid[iz + 1][ix];
const d = grid[iz + 1][ix + 1];
pushTerrainTriangle(a, c, b);
pushTerrainTriangle(b, c, d);
}
}
const terrainGeometry = new THREE.BufferGeometry();
terrainGeometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(terrainPositions, 3)
);
terrainGeometry.setAttribute(
"color",
new THREE.Float32BufferAttribute(terrainColors, 3)
);
terrainGeometry.computeVertexNormals();
const terrain = new THREE.Mesh(
terrainGeometry,
new THREE.MeshStandardMaterial({
vertexColors: true,
flatShading: true,
roughness: 0.96,
metalness: 0
})
);
terrain.receiveShadow = true;
terrain.castShadow = true;
terrainGroup.add(terrain);
// Group candidate triangles by elevation. This permits a moving snow line
// without changing the underlying terrain or glacier.
const snowBins = new Map();
for (const triangle of terrainTriangles) {
const altitude =
(triangle[0].y + triangle[1].y + triangle[2].y) / 3;
if (altitude < 4.5) continue;
const bin = Math.floor(altitude / 1.5);
if (!snowBins.has(bin)) snowBins.set(bin, []);
snowBins.get(bin).push(triangle);
}
const snowGroups = [];
for (const [bin, triangles] of snowBins) {
const positions = [];
for (const triangle of triangles) {
for (const vertex of triangle) {
positions.push(vertex.x, vertex.y + 0.075, vertex.z);
}
}
const geometry = new THREE.BufferGeometry();
geometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(positions, 3)
);
geometry.computeVertexNormals();
const material = new THREE.MeshBasicMaterial({
color: 0xf0f6f3,
transparent: true,
opacity: 0,
depthWrite: false,
side: THREE.DoubleSide,
polygonOffset: true,
polygonOffsetFactor: -1,
polygonOffsetUnits: -1
});
const mesh = new THREE.Mesh(geometry, material);
mesh.renderOrder = 2;
terrainGroup.add(mesh);
snowGroups.push({
mesh,
material,
altitude: bin * 1.5 + 0.75
});
}
// -------------------------------------------------------------------------
// Glacier geometry: fixed long-term baseline
// -------------------------------------------------------------------------
const glacierGroup = new THREE.Group();
world.add(glacierGroup);
function glacierPoint(angle, radius) {
const irregular = 1 + Math.sin(angle * 5 + 0.7) * 0.055;
const x = Math.cos(angle) * 12.8 * radius * irregular;
const z = -32.1 + Math.sin(angle) * 10.7 * radius * irregular;
const crown = radius < 0.5 ? 2.75 : 1.45;
const y = heightAt(x, z) + 0.5 + crown * (1 - radius * 0.68);
return { x, y, z };
}
const glacierPositions = [];
const glacierColors = [];
const glacierFaces = [];
const glacierSegments = 15;
function addGlacierFace(a, b, c) {
glacierPositions.push(
a.x, a.y, a.z,
b.x, b.y, b.z,
c.x, c.y, c.z
);
const palette = [0xaedfe4, 0xc9eceb, 0x8dcdd8, 0xb4e2e5];
const color = new THREE.Color(
palette[Math.floor(random() * palette.length)]
).offsetHSL(0, 0, (random() - 0.5) * 0.035);
for (let i = 0; i < 3; i++) {
glacierColors.push(color.r, color.g, color.b);
}
glacierFaces.push([a, b, c]);
}
const glacierCenter = {
x: 0,
y: heightAt(0, -32.1) + 3.05,
z: -32.1
};
const innerRing = [];
const outerRing = [];
for (let i = 0; i < glacierSegments; i++) {
const angle = (i / glacierSegments) * Math.PI * 2;
innerRing.push(glacierPoint(angle, 0.46));
outerRing.push(glacierPoint(angle, 1));
}
for (let i = 0; i < glacierSegments; i++) {
const next = (i + 1) % glacierSegments;
addGlacierFace(glacierCenter, innerRing[next], innerRing[i]);
addGlacierFace(innerRing[i], innerRing[next], outerRing[next]);
addGlacierFace(innerRing[i], outerRing[next], outerRing[i]);
}
const glacierGeometry = new THREE.BufferGeometry();
glacierGeometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(glacierPositions, 3)
);
glacierGeometry.setAttribute(
"color",
new THREE.Float32BufferAttribute(glacierColors, 3)
);
glacierGeometry.computeVertexNormals();
const glacier = new THREE.Mesh(
glacierGeometry,
new THREE.MeshStandardMaterial({
vertexColors: true,
flatShading: true,
roughness: 0.55,
metalness: 0.04
})
);
glacier.castShadow = true;
glacier.receiveShadow = true;
glacierGroup.add(glacier);
// Short blue fracture marks on the ice.
const fractureMaterial = new THREE.LineBasicMaterial({
color: 0x4d9eaf,
transparent: true,
opacity: 0.58
});
for (let i = 0; i < 7; i++) {
const angle = -0.8 + i * 0.32;
const points = [];
for (let j = 0; j < 5; j++) {
const r = 0.22 + j * 0.12;
const p = glacierPoint(angle + Math.sin(j * 1.8 + i) * 0.035, r);
points.push(new THREE.Vector3(p.x, p.y + 0.04, p.z));
}
const fracture = new THREE.Line(
new THREE.BufferGeometry().setFromPoints(points),
fractureMaterial
);
glacierGroup.add(fracture);
}
// -------------------------------------------------------------------------
// Village and fields
// -------------------------------------------------------------------------
const villageGroup = new THREE.Group();
world.add(villageGroup);
const wallMaterials = [
0xd9c3a1,
0xc7d0bd,
0xd6b899,
0xe2d6bd,
0xb9c5b3
].map(color =>
new THREE.MeshStandardMaterial({
color,
flatShading: true,
roughness: 0.9
})
);
const roofMaterials = [
0x785548,
0x675c51,
0x8a5b43
].map(color =>
new THREE.MeshStandardMaterial({
color,
flatShading: true,
roughness: 0.92
})
);
const roofSnowMaterial = new THREE.MeshStandardMaterial({
color: 0xf0f6f3,
transparent: true,
opacity: 0.9,
depthWrite: false,
flatShading: true,
roughness: 0.8
});
const windowMaterial = new THREE.MeshStandardMaterial({
color: 0xf3d58b,
emissive: 0x5b3514,
emissiveIntensity: 0.18,
roughness: 0.6
});
const doorMaterial = new THREE.MeshStandardMaterial({
color: 0x5c493d,
flatShading: true,
roughness: 1
});
function makeRoofGeometry(width, depth, roofHeight) {
const w = width / 2;
const d = depth / 2;
const geometry = new THREE.BufferGeometry();
geometry.setAttribute(
"position",
new THREE.Float32BufferAttribute([
-w, 0, -d,
w, 0, -d,
0, roofHeight, -d,
-w, 0, d,
w, 0, d,
0, roofHeight, d
], 3)
);
geometry.setIndex([
0, 2, 1,
3, 4, 5,
0, 3, 5,
0, 5, 2,
1, 2, 5,
1, 5, 4,
0, 1, 4,
0, 4, 3
]);
geometry.computeVertexNormals();
return geometry;
}
function addHouse(x, z, width, depth, height, index) {
const baseY = heightAt(x, z);
const body = new THREE.Mesh(
new THREE.BoxGeometry(width, height, depth),
wallMaterials[index % wallMaterials.length]
);
body.position.set(x, baseY + height / 2, z);
body.castShadow = true;
body.receiveShadow = true;
villageGroup.add(body);
const roofHeight = Math.max(0.65, width * 0.42);
const roofGeometry = makeRoofGeometry(width * 1.1, depth * 1.12, roofHeight);
const roof = new THREE.Mesh(
roofGeometry,
roofMaterials[index % roofMaterials.length]
);
roof.position.set(x, baseY + height, z);
roof.castShadow = true;
villageGroup.add(roof);
const roofSnow = new THREE.Mesh(
roofGeometry.clone(),
roofSnowMaterial
);
roofSnow.position.set(x, baseY + height + 0.055, z);
roofSnow.scale.set(1.025, 1.01, 1.025);
roofSnow.castShadow = true;
villageGroup.add(roofSnow);
const door = new THREE.Mesh(
new THREE.BoxGeometry(0.34, 0.68, 0.055),
doorMaterial
);
door.position.set(x, baseY + 0.35, z + depth / 2 + 0.035);
villageGroup.add(door);
for (const side of [-1, 1]) {
const windowMesh = new THREE.Mesh(
new THREE.BoxGeometry(0.27, 0.34, 0.045),
windowMaterial
);
windowMesh.position.set(
x + side * width * 0.27,
baseY + height * 0.58,
z + depth / 2 + 0.035
);
villageGroup.add(windowMesh);
}
}
const houses = [
[-5.6, 9.3, 1.8, 1.6, 1.35],
[-2.6, 9.8, 2.0, 1.7, 1.55],
[ 2.9, 9.5, 1.7, 1.6, 1.35],
[-6.1, 13.9, 1.9, 1.8, 1.45],
[-2.8, 16.1, 2.1, 1.8, 1.65],
[ 0.1, 14.0, 1.6, 1.5, 1.35],
[ 4.3, 14.2, 1.8, 1.7, 1.50],
[ 7.0, 17.4, 1.7, 1.5, 1.35],
[-8.0, 17.8, 1.8, 1.6, 1.45],
[ 2.1, 18.4, 1.7, 1.5, 1.40]
];
houses.forEach((house, i) => addHouse(...house, i));
// Small chapel.
const chapelX = -4.6;
const chapelZ = 12.0;
const chapelY = heightAt(chapelX, chapelZ);
const chapel = new THREE.Mesh(
new THREE.BoxGeometry(1.55, 2.05, 2.2),
wallMaterials[2]
);
chapel.position.set(chapelX, chapelY + 1.025, chapelZ);
chapel.castShadow = true;
villageGroup.add(chapel);
const chapelRoof = new THREE.Mesh(
makeRoofGeometry(1.75, 2.35, 0.9),
roofMaterials[1]
);
chapelRoof.position.set(chapelX, chapelY + 2.05, chapelZ);
chapelRoof.castShadow = true;
villageGroup.add(chapelRoof);
const chapelSnow = new THREE.Mesh(
makeRoofGeometry(1.79, 2.39, 0.91),
roofSnowMaterial
);
chapelSnow.position.set(chapelX, chapelY + 2.105, chapelZ);
villageGroup.add(chapelSnow);
const tower = new THREE.Mesh(
new THREE.BoxGeometry(0.75, 3.1, 0.75),
wallMaterials[0]
);
tower.position.set(chapelX, chapelY + 1.55, chapelZ - 0.6);
tower.castShadow = true;
villageGroup.add(tower);
const steeple = new THREE.Mesh(
new THREE.ConeGeometry(0.59, 1.15, 4),
roofMaterials[2]
);
steeple.position.set(chapelX, chapelY + 3.65, chapelZ - 0.6);
steeple.rotation.y = Math.PI / 4;
steeple.castShadow = true;
villageGroup.add(steeple);
// Farm plots.
const fieldMaterials = [
0xa89a58,
0x7e9255,
0xb39a62
].map(color =>
new THREE.MeshStandardMaterial({
color,
roughness: 1,
flatShading: true
})
);
function addField(x, z, width, depth, index) {
const field = new THREE.Mesh(
new THREE.BoxGeometry(width, 0.07, depth),
fieldMaterials[index % fieldMaterials.length]
);
field.position.set(x, heightAt(x, z) + 0.045, z);
field.receiveShadow = true;
villageGroup.add(field);
const furrowMaterial = new THREE.MeshStandardMaterial({
color: 0x716a45,
roughness: 1
});
for (let i = 1; i < 5; i++) {
const furrow = new THREE.Mesh(
new THREE.BoxGeometry(width * 0.92, 0.025, 0.035),
furrowMaterial
);
furrow.position.set(
x,
heightAt(x, z) + 0.09,
z - depth / 2 + (depth * i) / 5
);
villageGroup.add(furrow);
}
}
addField(-9.2, 10.2, 5.2, 3.8, 0);
addField(9.4, 13.0, 5.4, 4.4, 1);
addField(-7.8, 20.0, 5.8, 3.3, 2);
// -------------------------------------------------------------------------
// Trees, using instancing to retain a low draw-call count
// -------------------------------------------------------------------------
const treePositions = [];
for (let attempt = 0; attempt < 300 && treePositions.length < 78; attempt++) {
const x = (random() > 0.5 ? 1 : -1) * (8.5 + random() * 18);
const z = -22 + random() * 43;
if (z < -21 && Math.abs(x) < 14) continue;
if (z > 6 && x > -12 && x < 12) continue;
treePositions.push({ x, z, scale: 0.72 + random() * 0.58 });
}
const trunkGeometry = new THREE.CylinderGeometry(0.16, 0.23, 1.25, 5);
const trunkMaterial = new THREE.MeshStandardMaterial({
color: 0x72533d,
flatShading: true,
roughness: 1
});
const trunks = new THREE.InstancedMesh(
trunkGeometry,
trunkMaterial,
treePositions.length
);
const foliageGeometry = new THREE.ConeGeometry(1.15, 3.25, 6);
const foliageMaterial = new THREE.MeshStandardMaterial({
color: 0xffffff,
flatShading: true,
roughness: 0.95
});
const foliage = new THREE.InstancedMesh(
foliageGeometry,
foliageMaterial,
treePositions.length
);
const treeSnowMaterial = new THREE.MeshStandardMaterial({
color: 0xf1f6f2,
transparent: true,
opacity: 0,
depthWrite: false,
flatShading: true,
roughness: 0.9
});
const treeSnow = new THREE.InstancedMesh(
foliageGeometry.clone(),
treeSnowMaterial,
treePositions.length
);
const dummy = new THREE.Object3D();
const treeColor = new THREE.Color();
treePositions.forEach((tree, i) => {
const y = heightAt(tree.x, tree.z);
dummy.position.set(tree.x, y + 0.62 * tree.scale, tree.z);
dummy.scale.set(tree.scale, tree.scale, tree.scale);
dummy.rotation.set(0, random() * Math.PI, 0);
dummy.updateMatrix();
trunks.setMatrixAt(i, dummy.matrix);
dummy.position.set(tree.x, y + 2.0 * tree.scale, tree.z);
dummy.scale.set(tree.scale, tree.scale, tree.scale);
dummy.updateMatrix();
foliage.setMatrixAt(i, dummy.matrix);
treeColor.setHSL(
0.28 + random() * 0.045,
0.23 + random() * 0.08,
0.26 + random() * 0.08
);
foliage.setColorAt(i, treeColor);
dummy.position.set(tree.x, y + 2.12 * tree.scale, tree.z);
dummy.scale.set(tree.scale * 0.98, tree.scale * 0.94, tree.scale * 0.98);
dummy.updateMatrix();
treeSnow.setMatrixAt(i, dummy.matrix);
});
trunks.instanceMatrix.needsUpdate = true;
foliage.instanceMatrix.needsUpdate = true;
foliage.instanceColor.needsUpdate = true;
treeSnow.instanceMatrix.needsUpdate = true;
trunks.castShadow = true;
foliage.castShadow = true;
treeSnow.castShadow = true;
world.add(trunks, foliage, treeSnow);
// -------------------------------------------------------------------------
// Dynamic seasonal meltwater ribbons
// -------------------------------------------------------------------------
const waterMaterial = new THREE.MeshStandardMaterial({
color: 0x55b9ca,
transparent: true,
opacity: 0.86,
roughness: 0.28,
metalness: 0.03,
depthWrite: false,
side: THREE.DoubleSide
});
function createStream(config) {
const count = config.count || 60;
const positions = new Float32Array((count + 1) * 2 * 3);
const indices = [];
for (let i = 0; i < count; i++) {
const a = i * 2;
const b = (i + 1) * 2;
indices.push(a, b, b + 1, a, b + 1, a + 1);
}
const geometry = new THREE.BufferGeometry();
geometry.setAttribute(
"position",
new THREE.BufferAttribute(positions, 3).setUsage(THREE.DynamicDrawUsage)
);
geometry.setIndex(indices);
const mesh = new THREE.Mesh(geometry, waterMaterial);
mesh.frustumCulled = false;
mesh.renderOrder = 1;
world.add(mesh);
return {
mesh,
update(runoff) {
const array = geometry.attributes.position.array;
for (let i = 0; i <= count; i++) {
const t = i / count;
const z = lerp(config.zStart, config.zEnd, t);
const centerX = config.xAt(t, z);
const halfWidth =
config.width *
(0.64 + runoff * 0.58) *
0.5;
const leftX = centerX - halfWidth;
const rightX = centerX + halfWidth;
const leftY = heightAt(leftX, z) + 0.16;
const rightY = heightAt(rightX, z) + 0.16;
const leftIndex = i * 6;
array[leftIndex] = leftX;
array[leftIndex + 1] = leftY;
array[leftIndex + 2] = z;
array[leftIndex + 3] = rightX;
array[leftIndex + 4] = rightY;
array[leftIndex + 5] = z;
}
geometry.attributes.position.needsUpdate = true;
}
};
}
function mainRiverX(t, z) {
return (
Math.sin((z + 18) * 0.15) * 1.15 +
Math.sin(z * 0.37) * 0.38
);
}
const streams = [
createStream({
zStart: -22.3,
zEnd: 26,
width: 2.45,
count: 82,
xAt: (t, z) => mainRiverX(t, z)
}),
createStream({
zStart: -11,
zEnd: -4,
width: 1.05,
count: 26,
xAt: (t) => lerp(-12, mainRiverX(t, -7), t)
}),
createStream({
zStart: -6,
zEnd: 2,
width: 1.15,
count: 30,
xAt: (t) => lerp(12.5, mainRiverX(t, -2), t)
})
];
// Village access road.
const roadPositions = [];
const roadIndices = [];
const roadCount = 34;
for (let i = 0; i <= roadCount; i++) {
const t = i / roadCount;
const z = lerp(3.8, 24.5, t);
const x = -1.25 + Math.sin(t * 4.2) * 0.35;
const halfWidth = 0.48;
roadPositions.push(
x - halfWidth, heightAt(x - halfWidth, z) + 0.1, z,
x + halfWidth, heightAt(x + halfWidth, z) + 0.1, z
);
if (i < roadCount) {
const a = i * 2;
const b = (i + 1) * 2;
roadIndices.push(a, b, b + 1, a, b + 1, a + 1);
}
}
const roadGeometry = new THREE.BufferGeometry();
roadGeometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(roadPositions, 3)
);
roadGeometry.setIndex(roadIndices);
roadGeometry.computeVertexNormals();
const road = new THREE.Mesh(
roadGeometry,
new THREE.MeshStandardMaterial({
color: 0xa99a79,
flatShading: true,
roughness: 1,
side: THREE.DoubleSide
})
);
road.receiveShadow = true;
world.add(road);
// -------------------------------------------------------------------------
// Seasonal reservoir and open village cisterns
// -------------------------------------------------------------------------
const reservoirGroup = new THREE.Group();
world.add(reservoirGroup);
const lake = new THREE.Mesh(
new THREE.CircleGeometry(3.18, 24),
new THREE.MeshStandardMaterial({
color: 0x4aa8b8,
transparent: true,
opacity: 0.86,
roughness: 0.24,
metalness: 0.03,
side: THREE.DoubleSide
})
);
lake.rotation.x = -Math.PI / 2;
lake.position.set(7.2, 1.32, 2.0);
lake.receiveShadow = true;
reservoirGroup.add(lake);
const shore = new THREE.Mesh(
new THREE.TorusGeometry(3.22, 0.18, 5, 24),
new THREE.MeshStandardMaterial({
color: 0x8c8b76,
flatShading: true,
roughness: 1
})
);
shore.rotation.x = Math.PI / 2;
shore.position.set(7.2, 1.27, 2.0);
shore.receiveShadow = true;
reservoirGroup.add(shore);
const cisterns = [];
function addCistern(x, z, radius, height) {
const group = new THREE.Group();
const baseY = heightAt(x, z);
const wall = new THREE.Mesh(
new THREE.CylinderGeometry(
radius,
radius * 1.06,
height,
12,
1,
true
),
new THREE.MeshStandardMaterial({
color: 0x9b9c8b,
flatShading: true,
roughness: 0.92,
side: THREE.DoubleSide
})
);
wall.position.y = height / 2;
wall.castShadow = true;
wall.receiveShadow = true;
group.add(wall);
const bottom = new THREE.Mesh(
new THREE.CircleGeometry(radius * 0.94, 12),
new THREE.MeshStandardMaterial({
color: 0x6f756c,
roughness: 1
})
);
bottom.rotation.x = -Math.PI / 2;
bottom.position.y = 0.04;
group.add(bottom);
const rim = new THREE.Mesh(
new THREE.TorusGeometry(radius, 0.075, 4, 12),
new THREE.MeshStandardMaterial({
color: 0x777e72,
flatShading: true,
roughness: 1
})
);
rim.rotation.x = Math.PI / 2;
rim.position.y = height;
group.add(rim);
const water = new THREE.Mesh(
new THREE.CircleGeometry(radius * 0.88, 12),
new THREE.MeshStandardMaterial({
color: 0x48a7b7,
transparent: true,
opacity: 0.9,
roughness: 0.25,
side: THREE.DoubleSide
})
);
water.rotation.x = -Math.PI / 2;
group.add(water);
group.position.set(x, baseY, z);
reservoirGroup.add(group);
cisterns.push({ group, water, height, radius });
}
addCistern(-4.6, 8.0, 0.68, 1.15);
addCistern(-5.7, 8.6, 0.53, 0.92);
addCistern(6.1, 13.1, 0.64, 1.08);
// -------------------------------------------------------------------------
// Long-term retreat context: fixed dashed markers, separate from seasons
// -------------------------------------------------------------------------
const retreatGroup = new THREE.Group();
world.add(retreatGroup);
const retreatLineMaterial = new THREE.LineDashedMaterial({
color: 0xd98d34,
dashSize: 0.8,
gapSize: 0.48,
transparent: true,
opacity: 0.92
});
const markerMaterial = new THREE.MeshStandardMaterial({
color: 0xe2a04e,
flatShading: true,
roughness: 0.8
});
function addRetreatMargin(z) {
const points = [];
for (let x = -6.2; x <= 6.2; x += 0.42) {
points.push(
new THREE.Vector3(x, heightAt(x, z) + 0.34, z)
);
}
const line = new THREE.Line(
new THREE.BufferGeometry().setFromPoints(points),
retreatLineMaterial
);
line.computeLineDistances();
retreatGroup.add(line);
for (const x of [-5.7, 5.7]) {
const pole = new THREE.Mesh(
new THREE.CylinderGeometry(0.055, 0.075, 1.55, 5),
markerMaterial
);
pole.position.set(x, heightAt(x, z) + 0.78, z);
pole.castShadow = true;
retreatGroup.add(pole);
const flag = new THREE.Mesh(
new THREE.PlaneGeometry(0.62, 0.32),
new THREE.MeshStandardMaterial({
color: 0xe2a04e,
side: THREE.DoubleSide,
roughness: 0.9
})
);
flag.position.set(x + 0.28, heightAt(x, z) + 1.34, z);
retreatGroup.add(flag);
}
}
addRetreatMargin(-13.4);
addRetreatMargin(-18.1);
const retreatAxisPoints = [];
for (let z = -20.2; z <= -13.4; z += 0.4) {
retreatAxisPoints.push(
new THREE.Vector3(0, heightAt(0, z) + 0.42, z)
);
}
const retreatAxis = new THREE.Line(
new THREE.BufferGeometry().setFromPoints(retreatAxisPoints),
retreatLineMaterial
);
retreatAxis.computeLineDistances();
retreatGroup.add(retreatAxis);
const retreatArrow = new THREE.Mesh(
new THREE.ConeGeometry(0.38, 1.15, 6),
markerMaterial
);
retreatArrow.rotation.x = Math.PI / 2;
retreatArrow.position.set(0, heightAt(0, -12.9) + 0.52, -12.9);
retreatGroup.add(retreatArrow);
// -------------------------------------------------------------------------
// DOM labels projected directly from world positions
// -------------------------------------------------------------------------
const labels = [];
function addLabel(text, category, position, visible = () => true) {
const element = document.createElement("div");
element.className = `scene-label ${category}`;
const kicker =
category === "seasonal"
? "Seasonal layer"
: category === "retreat"
? "Long-term context"
: "Landscape";
element.innerHTML =
`<span class="label-kicker">${kicker}</span>${text}`;
labelLayer.appendChild(element);
labels.push({ element, position, visible });
}
addLabel(
"Glacier · fixed baseline",
"place",
new THREE.Vector3(0, 16.0, -32)
);
addLabel(
"Village below the ice",
"place",
new THREE.Vector3(-0.5, 4.4, 14.3)
);
addLabel(
"Seasonal snowpack",
"seasonal",
new THREE.Vector3(-13.5, 12.3, -12.5),
() => snowEnabled && seasonal.snow > 0.12
);
addLabel(
"Seasonal meltwater pulse",
"seasonal",
new THREE.Vector3(6.3, 5.8, -4.8),
() => waterEnabled && seasonal.runoff > 0.35
);
addLabel(
"Seasonal water storage",
"seasonal",
new THREE.Vector3(6.4, 3.7, 12.2),
() => waterEnabled
);
addLabel(
"Earlier ice margin",
"retreat",
new THREE.Vector3(-2.3, heightAt(-2.3, -13.4) + 2.1, -13.4),
() => retreatEnabled
);
addLabel(
"Later ice margin",
"retreat",
new THREE.Vector3(2.4, heightAt(2.4, -18.1) + 2.1, -18.1),
() => retreatEnabled
);
addLabel(
"Long-term retreat direction",
"retreat",
new THREE.Vector3(0, heightAt(0, -15.7) + 3.1, -15.7),
() => retreatEnabled
);
const projected = new THREE.Vector3();
function updateLabels() {
const width = window.innerWidth;
const height = window.innerHeight;
for (const label of labels) {
projected.copy(label.position).project(camera);
const inView =
projected.z > -1 &&
projected.z < 1 &&
Math.abs(projected.x) < 1.15 &&
Math.abs(projected.y) < 1.15;
if (!label.visible() || !inView) {
label.element.style.display = "none";
continue;
}
label.element.style.display = "block";
label.element.style.left =
`${(projected.x * 0.5 + 0.5) * width}px`;
label.element.style.top =
`${(-projected.y * 0.5 + 0.5) * height}px`;
}
}
// -------------------------------------------------------------------------
// Seasonal state model and UI
// -------------------------------------------------------------------------
const states = {
winter: {
name: "Winter · snow stored",
badge: "Winter",
snow: 1.0,
snowline: 6.4,
runoff: 0.18,
storage: 0.92
},
thaw: {
name: "Melt pulse · runoff rising",
badge: "Melt pulse",
snow: 0.48,
snowline: 13.5,
runoff: 1.0,
storage: 0.74
},
summer: {
name: "Summer · snow depleted",
badge: "Summer",
snow: 0.045,
snowline: 21.0,
runoff: 0.30,
storage: 0.48
}
};
const seasonal = { ...states.winter };
let selectedSeason = "winter";
let snowEnabled = true;
let waterEnabled = true;
let retreatEnabled = true;
const seasonButtons = [
...document.querySelectorAll(".season-button")
];
const seasonReadout = document.getElementById("seasonReadout");
const modeBadge = document.getElementById("modeBadge");
const snowToggle = document.getElementById("snowToggle");
const waterToggle = document.getElementById("waterToggle");
const retreatToggle = document.getElementById("retreatToggle");
const snowMeter = document.getElementById("snowMeter");
const runoffMeter = document.getElementById("runoffMeter");
const storageMeter = document.getElementById("storageMeter");
const snowValue = document.getElementById("snowValue");
const runoffValue = document.getElementById("runoffValue");
const storageValue = document.getElementById("storageValue");
function selectSeason(key) {
selectedSeason = key;
seasonButtons.forEach(button => {
button.setAttribute(
"aria-pressed",
String(button.dataset.season === key)
);
});
seasonReadout.textContent = states[key].name;
modeBadge.textContent = `Schematic · ${states[key].badge}`;
}
seasonButtons.forEach(button => {
button.addEventListener("click", () => {
selectSeason(button.dataset.season);
});
});
snowToggle.addEventListener("change", () => {
snowEnabled = snowToggle.checked;
});
waterToggle.addEventListener("change", () => {
waterEnabled = waterToggle.checked;
});
retreatToggle.addEventListener("change", () => {
retreatEnabled = retreatToggle.checked;
});
// -------------------------------------------------------------------------
// Custom camera controls: orbit, pan, pinch zoom
// -------------------------------------------------------------------------
const cameraState = {
theta: 0.52,
phi: 1.03,
radius: 78,
target: new THREE.Vector3(0, 4.5, -7),
desiredTheta: 0.52,
desiredPhi: 1.03,
desiredRadius: 78,
desiredTarget: new THREE.Vector3(0, 4.5, -7)
};
const pointers = new Map();
const canvas = renderer.domElement;
function resetCamera() {
cameraState.desiredTheta = 0.52;
cameraState.desiredPhi = 1.03;
cameraState.desiredRadius = 78;
cameraState.desiredTarget.set(0, 4.5, -7);
}
document.getElementById("resetView").addEventListener("click", resetCamera);
canvas.addEventListener("dblclick", resetCamera);
canvas.addEventListener("contextmenu", event => event.preventDefault());
canvas.addEventListener("pointerdown", event => {
canvas.setPointerCapture(event.pointerId);
pointers.set(event.pointerId, {
x: event.clientX,
y: event.clientY,
button: event.button
});
});
canvas.addEventListener("pointermove", event => {
const previous = pointers.get(event.pointerId);
if (!previous) return;
const dx = event.clientX - previous.x;
const dy = event.clientY - previous.y;
const before = [...pointers.values()];
pointers.set(event.pointerId, {
x: event.clientX,
y: event.clientY,
button: previous.button
});
if (pointers.size === 1) {
const panning =
previous.button === 2 ||
event.shiftKey;
if (panning) {
const forward = new THREE.Vector3();
camera.getWorldDirection(forward);
forward.y = 0;
forward.normalize();
const right = new THREE.Vector3()
.crossVectors(forward, camera.up)
.normalize();
const scale = cameraState.desiredRadius * 0.00135;
cameraState.desiredTarget.addScaledVector(right, -dx * scale);
cameraState.desiredTarget.addScaledVector(forward, -dy * scale);
} else {
cameraState.desiredTheta -= dx * 0.005;
cameraState.desiredPhi = clamp(
cameraState.desiredPhi - dy * 0.0045,
0.48,
1.34
);
}
} else if (pointers.size === 2 && before.length === 2) {
const after = [...pointers.values()];
const oldDistance = Math.hypot(
before[0].x - before[1].x,
before[0].y - before[1].y
);
const newDistance = Math.hypot(
after[0].x - after[1].x,
after[0].y - after[1].y
);
if (newDistance > 0) {
cameraState.desiredRadius = clamp(
cameraState.desiredRadius * (oldDistance / newDistance),
34,
125
);
}
}
});
function endPointer(event) {
pointers.delete(event.pointerId);
}
canvas.addEventListener("pointerup", endPointer);
canvas.addEventListener("pointercancel", endPointer);
canvas.addEventListener(
"wheel",
event => {
event.preventDefault();
cameraState.desiredRadius = clamp(
cameraState.desiredRadius * Math.exp(event.deltaY * 0.001),
34,
125
);
},
{ passive: false }
);
function updateCamera(dt) {
const damping = 1 - Math.exp(-dt * 8);
cameraState.theta = lerp(
cameraState.theta,
cameraState.desiredTheta,
damping
);
cameraState.phi = lerp(
cameraState.phi,
cameraState.desiredPhi,
damping
);
cameraState.radius = lerp(
cameraState.radius,
cameraState.desiredRadius,
damping
);
cameraState.target.lerp(
cameraState.desiredTarget,
damping
);
const sinPhi = Math.sin(cameraState.phi);
camera.position.set(
cameraState.target.x +
cameraState.radius * sinPhi * Math.sin(cameraState.theta),
cameraState.target.y +
cameraState.radius * Math.cos(cameraState.phi),
cameraState.target.z +
cameraState.radius * sinPhi * Math.cos(cameraState.theta)
);
camera.lookAt(cameraState.target);
camera.updateMatrixWorld();
}
// -------------------------------------------------------------------------
// Animation and state application
// -------------------------------------------------------------------------
function updateSeasonalVisuals(dt) {
const desired = states[selectedSeason];
const damping = 1 - Math.exp(-dt * 3.2);
seasonal.snow = lerp(seasonal.snow, desired.snow, damping);
seasonal.snowline = lerp(
seasonal.snowline,
desired.snowline,
damping
);
seasonal.runoff = lerp(
seasonal.runoff,
desired.runoff,
damping
);
seasonal.storage = lerp(
seasonal.storage,
desired.storage,
damping
);
// Seasonal snow cover follows a moving elevation threshold.
for (const group of snowGroups) {
const altitudeFactor = smoothstep(
seasonal.snowline - 1.8,
seasonal.snowline + 0.45,
group.altitude
);
group.material.opacity =
seasonal.snow * altitudeFactor * 0.94;
group.mesh.visible =
snowEnabled &&
group.material.opacity > 0.012;
}
roofSnowMaterial.opacity = seasonal.snow * 0.93;
roofSnowMaterial.visible =
snowEnabled && seasonal.snow > 0.02;
treeSnowMaterial.opacity = seasonal.snow * 0.88;
treeSnow.visible =
snowEnabled && seasonal.snow > 0.03;
// Meltwater ribbons become wider and more prominent.
for (const stream of streams) {
stream.update(seasonal.runoff);
stream.mesh.visible = waterEnabled;
}
waterMaterial.opacity = 0.55 + seasonal.runoff * 0.4;
const reservoirScale = 0.66 + seasonal.storage * 0.34;
lake.scale.set(reservoirScale, reservoirScale, reservoirScale);
lake.visible = waterEnabled;
for (const cistern of cisterns) {
cistern.water.position.y =
0.12 + seasonal.storage * (cistern.height - 0.25);
cistern.water.visible = waterEnabled;
}
retreatGroup.visible = retreatEnabled;
// Readouts.
const snowPercent = Math.round(seasonal.snow * 100);
const runoffPercent = Math.round(seasonal.runoff * 100);
const storagePercent = Math.round(seasonal.storage * 100);
snowMeter.style.width = `${snowPercent}%`;
runoffMeter.style.width = `${runoffPercent}%`;
storageMeter.style.width = `${storagePercent}%`;
snowValue.textContent = `${snowPercent}%`;
runoffValue.textContent = `${runoffPercent}%`;
storageValue.textContent = `${storagePercent}%`;
}
let previousTime = performance.now();
function animate(time) {
const dt = Math.min(0.05, (time - previousTime) / 1000);
previousTime = time;
updateCamera(dt);
updateSeasonalVisuals(dt);
updateLabels();
renderer.render(scene, camera);
requestAnimationFrame(animate);
}
window.addEventListener("resize", () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setPixelRatio(Math.min(window.devicePixelRatio || 1, 2));
});
selectSeason("winter");
updateCamera(1);
requestAnimationFrame(animate);
</script>
</body>
</html>
```