Prompt specification
Build or customize the Three.js 3D scene "Retreating glacier lake landscape" 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: Retreating glacier lake landscape
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/retreating-glacier-lake-landscape-x1151
Scene Description Prompt:
Move a time slider through illustrative voxel glacier retreat, revealing moraine, meltwater stream, and expanding lake. Build one HTML file using Three.js from cdnjs.
Working Three.js Starter (retreating-glacier-lake-landscape-x1151):
```html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Edge of Ice — Voxel Glacier Retreat</title>
<style>
:root {
--ink: #17383d;
--muted: #607477;
--accent: #db6045;
--accent-dark: #b74732;
--glass: rgba(250, 253, 250, 0.82);
--line: rgba(23, 56, 61, 0.14);
--teal: #318b91;
}
* { box-sizing: border-box; }
html, body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #e4ece7;
color: var(--ink);
font-family: Inter, ui-sans-serif, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
button, input { font: inherit; }
#app,
#scene {
position: fixed;
inset: 0;
}
#scene {
background:
radial-gradient(circle at 52% 38%, rgba(255,255,255,.82), transparent 40%),
linear-gradient(155deg, #eef4ef 0%, #dbe8e5 56%, #ccdeda 100%);
}
canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
cursor: grab;
outline: none;
}
canvas:active { cursor: grabbing; }
.hud {
position: fixed;
inset: 0;
pointer-events: none;
z-index: 2;
}
.hero {
position: absolute;
top: 28px;
left: 32px;
max-width: 470px;
pointer-events: none;
text-shadow: 0 1px 12px rgba(255,255,255,.65);
}
.eyebrow {
display: flex;
align-items: center;
gap: 9px;
margin-bottom: 9px;
color: var(--teal);
font-size: 10px;
font-weight: 800;
letter-spacing: .18em;
text-transform: uppercase;
}
.eyebrow::before {
content: "";
width: 24px;
height: 2px;
background: var(--accent);
border-radius: 2px;
}
h1 {
margin: 0;
font-family: Georgia, "Times New Roman", serif;
font-size: clamp(42px, 5vw, 72px);
font-weight: 500;
line-height: .92;
letter-spacing: -.045em;
}
.hero p {
max-width: 410px;
margin: 15px 0 0;
color: #496468;
font-size: 13px;
line-height: 1.55;
}
.model-pill {
display: inline-flex;
align-items: center;
gap: 7px;
margin-top: 16px;
padding: 7px 10px;
border: 1px solid rgba(23,56,61,.12);
border-radius: 999px;
background: rgba(255,255,255,.48);
color: #577075;
font-size: 9px;
font-weight: 800;
letter-spacing: .13em;
text-transform: uppercase;
backdrop-filter: blur(10px);
}
.model-pill i {
width: 6px;
height: 6px;
border-radius: 50%;
background: var(--accent);
box-shadow: 0 0 0 4px rgba(219,96,69,.12);
}
.metrics {
position: absolute;
top: 28px;
right: 28px;
display: grid;
grid-template-columns: repeat(3, minmax(112px, 1fr));
gap: 1px;
overflow: hidden;
border: 1px solid var(--line);
border-radius: 15px;
background: var(--line);
box-shadow: 0 18px 45px rgba(41,68,68,.10);
backdrop-filter: blur(16px);
pointer-events: auto;
}
.metric {
min-height: 92px;
padding: 16px 17px 13px;
background: var(--glass);
}
.metric-label {
display: block;
margin-bottom: 8px;
color: var(--muted);
font-size: 8px;
font-weight: 800;
letter-spacing: .14em;
text-transform: uppercase;
}
.metric-value {
display: block;
font-family: Georgia, "Times New Roman", serif;
font-size: 25px;
line-height: 1;
font-variant-numeric: tabular-nums;
}
.metric-value small {
margin-left: 3px;
color: var(--muted);
font-family: inherit;
font-size: 11px;
}
.metric:nth-child(2) .metric-value { color: var(--teal); }
.metric:nth-child(3) .metric-value { color: var(--accent); }
.timeline {
position: absolute;
left: 50%;
bottom: 24px;
width: min(900px, calc(100% - 56px));
transform: translateX(-50%);
display: grid;
grid-template-columns: 54px 145px 1fr;
align-items: center;
gap: 18px;
padding: 15px 19px 14px;
border: 1px solid rgba(23,56,61,.15);
border-radius: 19px;
background: rgba(249,252,248,.88);
box-shadow: 0 20px 55px rgba(39,65,64,.17);
backdrop-filter: blur(18px);
pointer-events: auto;
}
#play {
display: grid;
place-items: center;
width: 48px;
height: 48px;
border: 0;
border-radius: 50%;
background: var(--ink);
color: white;
cursor: pointer;
box-shadow: 0 8px 20px rgba(23,56,61,.20);
transition: transform .16s ease, background .16s ease;
}
#play:hover {
transform: translateY(-2px);
background: #245158;
}
#play:focus-visible {
outline: 3px solid rgba(219,96,69,.36);
outline-offset: 3px;
}
#play svg {
width: 18px;
height: 18px;
fill: currentColor;
}
.year-block {
border-right: 1px solid var(--line);
padding-right: 18px;
}
.year-block span {
display: block;
margin-bottom: 2px;
color: var(--muted);
font-size: 8px;
font-weight: 800;
letter-spacing: .14em;
text-transform: uppercase;
}
#year {
font-family: Georgia, "Times New Roman", serif;
font-size: 30px;
line-height: 1;
font-variant-numeric: tabular-nums;
}
.track-wrap { min-width: 0; }
.track-head {
display: flex;
justify-content: space-between;
gap: 12px;
margin-bottom: 8px;
color: var(--muted);
font-size: 9px;
font-weight: 700;
letter-spacing: .08em;
text-transform: uppercase;
}
#stage {
overflow: hidden;
color: var(--ink);
text-overflow: ellipsis;
white-space: nowrap;
}
input[type="range"] {
--p: 0%;
display: block;
width: 100%;
height: 5px;
margin: 0;
appearance: none;
border-radius: 99px;
background: linear-gradient(
to right,
var(--accent) 0,
var(--accent) var(--p),
rgba(23,56,61,.15) var(--p),
rgba(23,56,61,.15) 100%
);
cursor: pointer;
}
input[type="range"]::-webkit-slider-thumb {
width: 17px;
height: 17px;
appearance: none;
border: 3px solid white;
border-radius: 50%;
background: var(--accent);
box-shadow: 0 2px 8px rgba(23,56,61,.26);
}
input[type="range"]::-moz-range-thumb {
width: 12px;
height: 12px;
border: 3px solid white;
border-radius: 50%;
background: var(--accent);
box-shadow: 0 2px 8px rgba(23,56,61,.26);
}
input[type="range"]:focus-visible {
outline: 3px solid rgba(219,96,69,.25);
outline-offset: 6px;
}
.ticks {
display: flex;
justify-content: space-between;
margin-top: 8px;
color: #7c8b8c;
font-size: 8px;
font-weight: 700;
font-variant-numeric: tabular-nums;
}
.hint {
position: absolute;
right: 29px;
bottom: 128px;
display: flex;
align-items: center;
gap: 9px;
color: rgba(23,56,61,.62);
font-size: 9px;
font-weight: 700;
letter-spacing: .08em;
text-transform: uppercase;
}
.hint svg {
width: 19px;
height: 19px;
stroke: currentColor;
fill: none;
stroke-width: 1.6;
}
.world-label {
position: absolute;
z-index: 1;
padding: 7px 10px 7px 9px;
border: 1px solid rgba(23,56,61,.14);
border-radius: 8px;
background: rgba(250,252,248,.86);
box-shadow: 0 7px 18px rgba(23,56,61,.11);
color: var(--ink);
font-size: 8px;
font-weight: 800;
letter-spacing: .11em;
text-transform: uppercase;
white-space: nowrap;
opacity: 0;
transform: translate(-50%, -115%);
transition: opacity .2s ease;
backdrop-filter: blur(8px);
}
.world-label::after {
content: "";
position: absolute;
left: 50%;
top: 100%;
width: 1px;
height: 17px;
background: rgba(23,56,61,.42);
}
.world-label::before {
content: "";
position: absolute;
left: calc(50% - 3px);
top: calc(100% + 14px);
width: 6px;
height: 6px;
border: 2px solid rgba(250,252,248,.9);
border-radius: 50%;
background: var(--accent);
box-shadow: 0 0 0 1px rgba(23,56,61,.25);
}
.world-label.glacier { color: #287985; }
.world-label.lake { color: #267a82; }
.world-label.moraine { color: #80664f; }
.world-label.stream { color: #3e7887; }
.loading {
position: fixed;
inset: 0;
z-index: 10;
display: grid;
place-items: center;
background: #e6eee9;
color: var(--ink);
font-size: 10px;
font-weight: 800;
letter-spacing: .15em;
text-transform: uppercase;
transition: opacity .45s ease;
}
.loading.hidden { opacity: 0; pointer-events: none; }
@media (max-width: 780px) {
.hero {
top: 18px;
left: 18px;
}
h1 { font-size: 44px; }
.hero p { display: none; }
.model-pill { margin-top: 10px; }
.metrics {
top: 108px;
left: 16px;
right: 16px;
grid-template-columns: repeat(3, 1fr);
}
.metric {
min-height: 67px;
padding: 10px 11px;
}
.metric-label { margin-bottom: 5px; font-size: 7px; }
.metric-value { font-size: 20px; }
.timeline {
bottom: 12px;
width: calc(100% - 24px);
grid-template-columns: 44px 83px 1fr;
gap: 10px;
padding: 11px 12px;
border-radius: 15px;
}
#play {
width: 40px;
height: 40px;
}
#year { font-size: 24px; }
.year-block { padding-right: 9px; }
.track-head { font-size: 7px; }
#stage { display: none; }
.hint, .world-label { display: none; }
}
@media (max-width: 480px) {
.metrics { top: 102px; }
.metric-label { letter-spacing: .08em; }
.metric-value small { display: none; }
.ticks span:nth-child(even) { display: none; }
.timeline { grid-template-columns: 40px 70px 1fr; }
}
</style>
</head>
<body>
<main id="app">
<div id="scene" aria-label="Interactive voxel glacier retreat model"></div>
<div class="hud">
<header class="hero">
<div class="eyebrow">Cryosphere field study</div>
<h1>Edge of Ice</h1>
<p>
Follow a glacier margin retreat through time. The exposed moraine,
meltwater corridor, and expanding proglacial lake tell the story.
</p>
<div class="model-pill"><i></i> Illustrative voxel model</div>
</header>
<aside class="metrics" aria-label="Model indicators">
<div class="metric">
<span class="metric-label">Terminus retreat</span>
<span class="metric-value"><span id="retreat">0</span><small>m</small></span>
</div>
<div class="metric">
<span class="metric-label">Lake area</span>
<span class="metric-value"><span id="area">0.03</span><small>km²</small></span>
</div>
<div class="metric">
<span class="metric-label">Melt rate</span>
<span class="metric-value"><span id="melt">3.2</span><small>mm/yr</small></span>
</div>
</aside>
<div id="label-glacier" class="world-label glacier">Glacier margin</div>
<div id="label-moraine" class="world-label moraine">Terminal moraine</div>
<div id="label-stream" class="world-label stream">Meltwater stream</div>
<div id="label-lake" class="world-label lake">Proglacial lake</div>
<section class="timeline" aria-label="Timeline controls">
<button id="play" type="button" aria-label="Play timeline">
<svg id="play-icon" viewBox="0 0 24 24" aria-hidden="true">
<path d="M7 4.5v15l12-7.5z"></path>
</svg>
</button>
<div class="year-block">
<span>Year</span>
<div id="year">1980</div>
</div>
<div class="track-wrap">
<div class="track-head">
<span>Glacier chronology</span>
<span id="stage">Maximum observed extent</span>
</div>
<input
id="time"
type="range"
min="0"
max="1"
step="0.001"
value="0"
aria-label="Year from 1980 to 2025"
/>
<div class="ticks" aria-hidden="true">
<span>1980</span>
<span>1990</span>
<span>2000</span>
<span>2010</span>
<span>2020</span>
<span>2025</span>
</div>
</div>
</section>
<div class="hint" aria-hidden="true">
<svg viewBox="0 0 24 24">
<path d="M8 10V7a4 4 0 0 1 8 0v3"></path>
<path d="M6 10h12l-1 10H7L6 10z"></path>
<path d="M12 4v6"></path>
</svg>
Drag to orbit · Scroll to zoom
</div>
</div>
<div id="loading" class="loading">Building the glacier…</div>
</main>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const sceneHost = document.getElementById("scene");
const loading = document.getElementById("loading");
const slider = document.getElementById("time");
const playButton = document.getElementById("play");
const playIcon = document.getElementById("play-icon");
const yearOut = document.getElementById("year");
const stageOut = document.getElementById("stage");
const retreatOut = document.getElementById("retreat");
const areaOut = document.getElementById("area");
const meltOut = document.getElementById("melt");
const labels = {
glacier: document.getElementById("label-glacier"),
moraine: document.getElementById("label-moraine"),
stream: document.getElementById("label-stream"),
lake: document.getElementById("label-lake")
};
// ------------------------------------------------------------
// Renderer, scene, lights
// ------------------------------------------------------------
const renderer = new THREE.WebGLRenderer({ antialias: true, alpha: true });
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 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;
renderer.domElement.setAttribute("tabindex", "0");
sceneHost.appendChild(renderer.domElement);
const scene = new THREE.Scene();
scene.fog = new THREE.Fog(0xdce8e4, 76, 145);
const camera = new THREE.PerspectiveCamera(
34,
window.innerWidth / window.innerHeight,
0.1,
220
);
const hemi = new THREE.HemisphereLight(0xf8fff9, 0x536c63, 2.15);
scene.add(hemi);
const sun = new THREE.DirectionalLight(0xfff3d8, 3.25);
sun.position.set(-28, 48, 26);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -48;
sun.shadow.camera.right = 48;
sun.shadow.camera.top = 48;
sun.shadow.camera.bottom = -48;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 120;
sun.shadow.normalBias = 0.025;
scene.add(sun);
const fill = new THREE.DirectionalLight(0xa9dce3, 0.7);
fill.position.set(30, 18, -30);
scene.add(fill);
const cubeGeometry = new THREE.BoxGeometry(1, 1, 1);
const dummy = new THREE.Object3D();
// ------------------------------------------------------------
// Landscape height and helpers
// ------------------------------------------------------------
const NX = 57;
const NZ = 47;
const CELL = 1;
const BOTTOM = -1.2;
const INITIAL_TERMINUS = 6.5;
const FINAL_TERMINUS = -12;
const RETREAT_RANGE = INITIAL_TERMINUS - FINAL_TERMINUS;
function clamp01(v) {
return Math.max(0, Math.min(1, v));
}
function smoothstep(a, b, x) {
const q = clamp01((x - a) / (b - a));
return q * q * (3 - 2 * q);
}
function hash(i, j) {
const n = Math.sin(i * 127.1 + j * 311.7) * 43758.5453123;
return n - Math.floor(n);
}
function channelX(z) {
return 1.4 * Math.sin((z + 18) * 0.16);
}
function glacierCenter(z) {
return 1.4 * Math.sin((z + 20) * 0.13);
}
function lakeRadiusAt(x, z) {
return Math.sqrt(
Math.pow(x / 12.5, 2) +
Math.pow((z - 10) / 7, 2)
);
}
function heightAt(x, z) {
const sideRise = 0.016 * x * x;
const base = 2.65 - z * 0.055 + sideRise;
const activeChannel =
smoothstep(-20, -16, z) *
(1 - smoothstep(7, 11, z));
const channelCut =
0.5 *
Math.exp(-Math.pow((x - channelX(z)) / 1.35, 2)) *
activeChannel;
let h = base - channelCut;
const r = lakeRadiusAt(x, z);
if (r < 1.18) {
const bowl =
0.22 +
0.86 * r * r +
0.08 * Math.pow(Math.abs(x) / 12.5, 2);
const blend = 1 - smoothstep(1.02, 1.18, r);
h = THREE.MathUtils.lerp(h, Math.min(h, bowl), blend);
}
return Math.max(0.05, h);
}
// ------------------------------------------------------------
// Voxel terrain
// ------------------------------------------------------------
const terrainMaterial = new THREE.MeshStandardMaterial({
color: 0xffffff,
roughness: 0.94,
metalness: 0
});
const terrain = new THREE.InstancedMesh(
cubeGeometry,
terrainMaterial,
NX * NZ
);
terrain.castShadow = true;
terrain.receiveShadow = true;
terrain.instanceMatrix.setUsage(THREE.StaticDrawUsage);
const grassLow = new THREE.Color("#71916d");
const grassHigh = new THREE.Color("#87977b");
const rock = new THREE.Color("#94988e");
const gravel = new THREE.Color("#918c7a");
const lakeBed = new THREE.Color("#57786e");
const tmpColor = new THREE.Color();
let terrainIndex = 0;
const terrainHeight = new Map();
for (let j = 0; j < NZ; j++) {
for (let i = 0; i < NX; i++) {
const x = i - (NX - 1) / 2;
const z = j - (NZ - 1) / 2;
const h = heightAt(x, z);
const columnHeight = h - BOTTOM;
terrainHeight.set(`${i},${j}`, h);
dummy.position.set(x, BOTTOM + columnHeight / 2, z);
dummy.rotation.set(0, 0, 0);
dummy.scale.set(0.985, columnHeight, 0.985);
dummy.updateMatrix();
terrain.setMatrixAt(terrainIndex, dummy.matrix);
const r = lakeRadiusAt(x, z);
const noise = hash(i, j);
const channelDistance = Math.abs(x - channelX(z));
if (r < 1.02) {
tmpColor.copy(lakeBed);
tmpColor.offsetHSL(0, 0, (noise - 0.5) * 0.07);
} else if (z > -17 && z < 9 && channelDistance < 0.95) {
tmpColor.copy(gravel);
tmpColor.offsetHSL(0, 0, (noise - 0.5) * 0.08);
} else {
const altitude = smoothstep(2, 8, h);
tmpColor.copy(grassLow).lerp(grassHigh, altitude * 0.72);
tmpColor.lerp(rock, smoothstep(5, 10, h) * 0.78);
tmpColor.offsetHSL(
(noise - 0.5) * 0.012,
(noise - 0.5) * 0.035,
(noise - 0.5) * 0.075
);
}
terrain.setColorAt(terrainIndex, tmpColor);
terrainIndex++;
}
}
terrain.instanceMatrix.needsUpdate = true;
terrain.instanceColor.needsUpdate = true;
scene.add(terrain);
// A clean plinth and soft shadow floor frame the landscape.
const plinth = new THREE.Mesh(
new THREE.BoxGeometry(NX + 0.25, 0.45, NZ + 0.25),
new THREE.MeshStandardMaterial({
color: 0x526961,
roughness: 0.9
})
);
plinth.position.set(0, BOTTOM - 0.225, 0);
plinth.castShadow = true;
plinth.receiveShadow = true;
scene.add(plinth);
const floor = new THREE.Mesh(
new THREE.PlaneGeometry(220, 220),
new THREE.MeshStandardMaterial({
color: 0xd9e5df,
roughness: 1
})
);
floor.rotation.x = -Math.PI / 2;
floor.position.y = BOTTOM - 0.47;
floor.receiveShadow = true;
scene.add(floor);
// ------------------------------------------------------------
// Glacier voxel inventory
// ------------------------------------------------------------
const iceRecords = [];
for (let j = 0; j < NZ; j++) {
for (let i = 0; i < NX; i++) {
const x = i - (NX - 1) / 2;
const z = j - (NZ - 1) / 2;
if (z > INITIAL_TERMINUS) continue;
const center = glacierCenter(z);
const width = 10.2 - 0.17 * (z + 23);
const edgeDistance = Math.abs(x - center) / width;
if (edgeDistance > 0.96) continue;
const retreatAtCell = clamp01(
(INITIAL_TERMINUS - z) / RETREAT_RANGE +
(hash(i + 11, j + 7) - 0.5) * 0.035
);
const thickness =
1.05 +
2.1 * (1 - edgeDistance) +
hash(i + 23, j + 13) * 0.5 +
Math.max(0, -z - 6) * 0.025;
const color = new THREE.Color("#91d8e3");
color.offsetHSL(
(hash(i + 31, j + 17) - 0.5) * 0.025,
(hash(i + 41, j + 19) - 0.5) * 0.05,
(hash(i + 51, j + 23) - 0.5) * 0.10
);
iceRecords.push({
i,
j,
x,
z,
h: heightAt(x, z),
thickness,
retreatAtCell,
color
});
}
}
const iceMaterial = new THREE.MeshStandardMaterial({
color: 0xffffff,
roughness: 0.34,
metalness: 0.02,
emissive: 0x0a2830,
emissiveIntensity: 0.055
});
const ice = new THREE.InstancedMesh(
cubeGeometry,
iceMaterial,
iceRecords.length
);
ice.castShadow = true;
ice.receiveShadow = true;
ice.frustumCulled = false;
scene.add(ice);
const capMaterial = new THREE.MeshStandardMaterial({
color: 0xf2fbf7,
roughness: 0.72,
metalness: 0
});
const iceCaps = new THREE.InstancedMesh(
cubeGeometry,
capMaterial,
iceRecords.length
);
iceCaps.castShadow = true;
iceCaps.receiveShadow = true;
iceCaps.frustumCulled = false;
scene.add(iceCaps);
// ------------------------------------------------------------
// Moraine ridge
// ------------------------------------------------------------
const moraineRecords = [];
let moraineCounter = 0;
for (let x = -10; x <= 10; x += 0.52) {
const count = 3 + Math.floor(hash(Math.round(x * 10), 2) * 3);
for (let k = 0; k < count; k++) {
const px = x + (hash(moraineCounter + 1, 9) - 0.5) * 0.55;
const pz =
4.7 +
0.06 * px * px +
(hash(moraineCounter + 2, 11) - 0.5) * 0.95;
const size =
0.38 +
hash(moraineCounter + 3, 13) * 0.48;
const reveal = clamp01(
0.18 +
(INITIAL_TERMINUS - pz) / RETREAT_RANGE +
hash(moraineCounter + 4, 17) * 0.065,
0.1,
0.96
);
moraineRecords.push({
x: px,
z: pz,
y: heightAt(px, pz),
size,
reveal,
rotation: hash(moraineCounter + 5, 19) * Math.PI
});
moraineCounter++;
}
}
const moraineMaterial = new THREE.MeshStandardMaterial({
color: 0xffffff,
roughness: 1
});
const moraine = new THREE.InstancedMesh(
cubeGeometry,
moraineMaterial,
moraineRecords.length
);
moraine.castShadow = true;
moraine.receiveShadow = true;
moraine.frustumCulled = false;
scene.add(moraine);
const moraineBrown = new THREE.Color("#88705a");
const moraineGray = new THREE.Color("#a09a8b");
// ------------------------------------------------------------
// Proglacial lake
// ------------------------------------------------------------
const lakeRecords = [];
for (let j = 0; j < NZ; j++) {
for (let i = 0; i < NX; i++) {
const x = i - (NX - 1) / 2;
const z = j - (NZ - 1) / 2;
const r = lakeRadiusAt(x, z);
if (r > 1.08) continue;
const noise = (hash(i + 71, j + 73) - 0.5) * 0.035;
lakeRecords.push({
i,
j,
x,
z,
r,
noise,
h: heightAt(x, z)
});
}
}
const lakeMaterial = new THREE.MeshStandardMaterial({
color: 0xffffff,
roughness: 0.18,
metalness: 0.06,
transparent: true,
opacity: 0.74,
depthWrite: false
});
const lake = new THREE.InstancedMesh(
cubeGeometry,
lakeMaterial,
lakeRecords.length
);
lake.frustumCulled = false;
lake.renderOrder = 3;
scene.add(lake);
const lakeDeep = new THREE.Color("#2b8993");
const lakeShallow = new THREE.Color("#71b8b3");
// ------------------------------------------------------------
// Meltwater stream
// ------------------------------------------------------------
const MAX_STREAM = 240;
const streamMaterial = new THREE.MeshStandardMaterial({
color: 0x63b6bd,
roughness: 0.2,
metalness: 0.03,
transparent: true,
opacity: 0.9,
depthWrite: false
});
const stream = new THREE.InstancedMesh(
cubeGeometry,
streamMaterial,
MAX_STREAM
);
stream.frustumCulled = false;
stream.renderOrder = 4;
scene.add(stream);
// ------------------------------------------------------------
// Timeline state and model updates
// ------------------------------------------------------------
let time = 0;
let playing = false;
function updateIce(t) {
let count = 0;
for (const record of iceRecords) {
if (t > record.retreatAtCell + 0.0001) continue;
dummy.position.set(
record.x,
record.h + record.thickness / 2,
record.z
);
dummy.rotation.set(0, 0, 0);
dummy.scale.set(0.94, record.thickness, 0.94);
dummy.updateMatrix();
ice.setMatrixAt(count, dummy.matrix);
ice.setColorAt(count, record.color);
dummy.position.set(
record.x,
record.h + record.thickness + 0.035,
record.z
);
dummy.scale.set(0.94, 0.07, 0.94);
dummy.updateMatrix();
iceCaps.setMatrixAt(count, dummy.matrix);
count++;
}
ice.count = count;
iceCaps.count = count;
ice.instanceMatrix.needsUpdate = true;
iceCaps.instanceMatrix.needsUpdate = true;
if (ice.instanceColor) ice.instanceColor.needsUpdate = true;
}
function updateMoraine(t) {
moraineRecords.forEach((record, index) => {
const reveal = smoothstep(
record.reveal - 0.065,
record.reveal + 0.035,
t
);
const s = record.size * reveal;
dummy.position.set(record.x, record.y + s * 0.43, record.z);
dummy.rotation.set(
0,
record.rotation,
(hash(index + 4, 29) - 0.5) * 0.18
);
dummy.scale.set(s * 1.2, s * 0.72, s);
dummy.updateMatrix();
moraine.setMatrixAt(index, dummy.matrix);
tmpColor.copy(moraineBrown).lerp(
moraineGray,
hash(index + 9, 31)
);
moraine.setColorAt(index, tmpColor);
});
moraine.instanceMatrix.needsUpdate = true;
if (moraine.instanceColor) moraine.instanceColor.needsUpdate = true;
}
function updateLake(t) {
const radius = 0.12 + 0.86 * t;
const level = 0.42 + 0.72 * t;
let count = 0;
for (const record of lakeRecords) {
if (record.r + record.noise > radius) continue;
dummy.position.set(record.x, level, record.z);
dummy.rotation.set(0, 0, 0);
dummy.scale.set(0.975, 0.13, 0.975);
dummy.updateMatrix();
lake.setMatrixAt(count, dummy.matrix);
tmpColor.copy(lakeDeep).lerp(
lakeShallow,
clamp01(record.r * 0.72 + hash(record.i, record.j) * 0.15)
);
lake.setColorAt(count, tmpColor);
count++;
}
lake.count = count;
lake.instanceMatrix.needsUpdate = true;
if (lake.instanceColor) lake.instanceColor.needsUpdate = true;
return level;
}
function updateStream(t, lakeLevel) {
const terminus = THREE.MathUtils.lerp(
INITIAL_TERMINUS,
FINAL_TERMINUS,
t
);
const lakeRadius = 0.12 + 0.86 * t;
const lakeUpstream = 10 - lakeRadius * 7;
const start = terminus + 0.7;
const end = Math.max(start + 0.2, lakeUpstream);
const length = end - start;
const count = Math.min(MAX_STREAM, Math.max(1, Math.ceil(length / 0.52)));
const width = 0.42 + 0.28 * t;
for (let i = 0; i < count; i++) {
const u = count === 1 ? 0 : i / (count - 1);
const z = THREE.MathUtils.lerp(start, end, u);
const x = channelX(z);
const h = heightAt(x, z);
dummy.position.set(x, h + 0.075, z);
dummy.rotation.set(0, 0, 0);
dummy.scale.set(width, 0.11, 0.56);
dummy.updateMatrix();
stream.setMatrixAt(i, dummy.matrix);
}
stream.count = count;
stream.instanceMatrix.needsUpdate = true;
return { terminus, lakeUpstream };
}
function stageText(t) {
if (t < 0.12) return "Maximum observed extent";
if (t < 0.34) return "Margin begins to retreat";
if (t < 0.58) return "Moraine emerges from ice";
if (t < 0.79) return "Meltwater corridor opens";
return "Proglacial lake expands";
}
function applyTime(t) {
time = clamp01(t);
slider.value = String(time);
slider.style.setProperty("--p", `${time * 100}%`);
const year = Math.round(1980 + time * 45);
yearOut.textContent = year;
stageOut.textContent = stageText(time);
retreatOut.textContent = Math.round(time * 640).toLocaleString();
areaOut.textContent = (0.03 + 1.93 * Math.pow(time, 1.35)).toFixed(2);
meltOut.textContent = (3.2 + time * 9.8).toFixed(1);
updateIce(time);
updateMoraine(time);
const lakeLevel = updateLake(time);
updateStream(time, lakeLevel);
}
// ------------------------------------------------------------
// Custom camera controls — no external controls addon
// ------------------------------------------------------------
const orbit = {
target: new THREE.Vector3(0, 1.2, 0),
theta: 0.62,
phi: 0.78,
radius: 69
};
const pointers = new Map();
let gestureMode = "rotate";
let lastX = 0;
let lastY = 0;
let pinchDistance = 0;
function updateCamera() {
const sinPhi = Math.sin(orbit.phi);
orbit.phi = Math.max(0.28, Math.min(1.32, orbit.phi));
orbit.radius = Math.max(42, Math.min(102, orbit.radius));
camera.position.set(
orbit.target.x + orbit.radius * sinPhi * Math.sin(orbit.theta),
orbit.target.y + orbit.radius * Math.cos(orbit.phi),
orbit.target.z + orbit.radius * sinPhi * Math.cos(orbit.theta)
);
camera.lookAt(orbit.target);
}
function panCamera(dx, dy) {
const scale = orbit.radius * 0.00135;
const right = new THREE.Vector3(
Math.cos(orbit.theta),
0,
-Math.sin(orbit.theta)
);
const forward = new THREE.Vector3(
-Math.sin(orbit.theta),
0,
-Math.cos(orbit.theta)
);
orbit.target.addScaledVector(right, -dx * scale);
orbit.target.addScaledVector(forward, dy * scale);
orbit.target.x = Math.max(-18, Math.min(18, orbit.target.x));
orbit.target.z = Math.max(-18, Math.min(18, orbit.target.z));
}
renderer.domElement.addEventListener("pointerdown", (event) => {
renderer.domElement.setPointerCapture(event.pointerId);
pointers.set(event.pointerId, {
x: event.clientX,
y: event.clientY
});
if (pointers.size === 1) {
gestureMode =
event.button === 2 || event.shiftKey ? "pan" : "rotate";
lastX = event.clientX;
lastY = event.clientY;
} else if (pointers.size === 2) {
const [a, b] = [...pointers.values()];
pinchDistance = Math.hypot(a.x - b.x, a.y - b.y);
}
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (!pointers.has(event.pointerId)) return;
pointers.set(event.pointerId, {
x: event.clientX,
y: event.clientY
});
if (pointers.size === 1) {
const dx = event.clientX - lastX;
const dy = event.clientY - lastY;
if (gestureMode === "pan") {
panCamera(dx, dy);
} else {
orbit.theta -= dx * 0.006;
orbit.phi -= dy * 0.005;
}
lastX = event.clientX;
lastY = event.clientY;
} else if (pointers.size === 2) {
const [a, b] = [...pointers.values()];
const distance = Math.hypot(a.x - b.x, a.y - b.y);
if (pinchDistance > 0) {
orbit.radius *= pinchDistance / Math.max(1, distance);
}
pinchDistance = distance;
}
updateCamera();
});
function endPointer(event) {
pointers.delete(event.pointerId);
if (pointers.size < 2) pinchDistance = 0;
if (pointers.size === 1) {
const remaining = [...pointers.values()][0];
lastX = remaining.x;
lastY = remaining.y;
}
}
renderer.domElement.addEventListener("pointerup", endPointer);
renderer.domElement.addEventListener("pointercancel", endPointer);
renderer.domElement.addEventListener("contextmenu", (event) => event.preventDefault());
renderer.domElement.addEventListener(
"wheel",
(event) => {
event.preventDefault();
orbit.radius *= Math.exp(event.deltaY * 0.001);
updateCamera();
},
{ passive: false }
);
// ------------------------------------------------------------
// Screen-space labels
// ------------------------------------------------------------
const projected = new THREE.Vector3();
function positionLabel(element, x, y, z, visible = true) {
if (!visible) {
element.style.opacity = "0";
return;
}
projected.set(x, y, z).project(camera);
const inView =
projected.z > -1 &&
projected.z < 1 &&
Math.abs(projected.x) < 1.1 &&
Math.abs(projected.y) < 1.1;
if (!inView) {
element.style.opacity = "0";
return;
}
const px = (projected.x * 0.5 + 0.5) * window.innerWidth;
const py = (-projected.y * 0.5 + 0.5) * window.innerHeight;
element.style.left = `${px}px`;
element.style.top = `${py}px`;
element.style.opacity = "1";
}
function updateLabels() {
const terminus = THREE.MathUtils.lerp(
INITIAL_TERMINUS,
FINAL_TERMINUS,
time
);
const terminusX = glacierCenter(terminus);
const terminusH = heightAt(terminusX, terminus);
const moraineX = -6.6;
const moraineZ = 4.7 + 0.06 * moraineX * moraineX;
const streamZ = THREE.MathUtils.lerp(5.5, 0.5, time);
const streamX = channelX(streamZ);
positionLabel(
labels.glacier,
terminusX,
terminusH + 3.7,
terminus,
true
);
positionLabel(
labels.moraine,
moraineX,
heightAt(moraineX, moraineZ) + 1.1,
moraineZ,
time > 0.12
);
positionLabel(
labels.stream,
streamX,
heightAt(streamX, streamZ) + 0.9,
streamZ,
time > 0.04
);
positionLabel(
labels.lake,
6.2,
1.45,
11.1,
true
);
}
// ------------------------------------------------------------
// Playback and interaction
// ------------------------------------------------------------
function setPlaying(value) {
playing = value;
playButton.setAttribute(
"aria-label",
playing ? "Pause timeline" : "Play timeline"
);
playIcon.innerHTML = playing
? '<path d="M6.5 5h4v14h-4zm7 0h4v14h-4z"></path>'
: '<path d="M7 4.5v15l12-7.5z"></path>';
}
playButton.addEventListener("click", () => {
if (!playing && time >= 0.999) applyTime(0);
setPlaying(!playing);
});
slider.addEventListener("input", () => {
setPlaying(false);
applyTime(Number(slider.value));
});
window.addEventListener("keydown", (event) => {
if (event.code === "Space") {
const tag = document.activeElement?.tagName;
if (tag !== "INPUT" && tag !== "BUTTON") {
event.preventDefault();
if (!playing && time >= 0.999) applyTime(0);
setPlaying(!playing);
}
}
if (event.code === "ArrowRight" && document.activeElement !== slider) {
setPlaying(false);
applyTime(time + 0.02);
}
if (event.code === "ArrowLeft" && document.activeElement !== slider) {
setPlaying(false);
applyTime(time - 0.02);
}
});
window.addEventListener("resize", () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
});
// ------------------------------------------------------------
// Render loop
// ------------------------------------------------------------
applyTime(0);
updateCamera();
const clock = new THREE.Clock();
function animate() {
requestAnimationFrame(animate);
const delta = Math.min(clock.getDelta(), 0.05);
if (playing) {
let next = time + delta * 0.035;
if (next >= 1) {
next = 1;
setPlaying(false);
}
applyTime(next);
}
updateLabels();
renderer.render(scene, camera);
}
animate();
requestAnimationFrame(() => {
loading.classList.add("hidden");
setTimeout(() => loading.remove(), 500);
});
</script>
</body>
</html>
```