Prompt specification
Build or customize the Three.js 3D scene "Valley population interruption diorama" 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: Valley population interruption diorama
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/valley-population-interruption-diorama-x0887
Scene Description Prompt:
Show a fictional population bottleneck in a voxel valley, tracking survivors and their descendants. Use Three.js from cdnjs in standalone HTML.
Working Three.js Starter (valley-population-interruption-diorama-x0887):
```html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>The Narrow Pass — A Fictional Population Bottleneck</title>
<style>
:root {
color-scheme: dark;
--panel: rgba(9, 20, 20, 0.84);
--panel-border: rgba(198, 255, 225, 0.16);
--text: #e8fff2;
--muted: #91aa9f;
--accent: #79f2bd;
--warning: #ffb15c;
--danger: #ff765e;
}
* { box-sizing: border-box; }
html, body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #071110;
color: var(--text);
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
button, select, input { font: inherit; }
#stage {
position: fixed;
inset: 0;
}
#stage canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
cursor: grab;
}
#stage canvas:active { cursor: grabbing; }
.glass {
background: var(--panel);
border: 1px solid var(--panel-border);
box-shadow: 0 18px 60px rgba(0, 0, 0, 0.28);
backdrop-filter: blur(14px);
-webkit-backdrop-filter: blur(14px);
}
#title {
position: fixed;
left: 24px;
top: 22px;
pointer-events: none;
max-width: 420px;
text-shadow: 0 2px 18px #000;
}
#title .eyebrow {
color: var(--accent);
font-size: 10px;
letter-spacing: 0.24em;
text-transform: uppercase;
font-weight: 800;
}
#title h1 {
margin: 5px 0 4px;
font-size: clamp(25px, 3vw, 42px);
line-height: 0.95;
letter-spacing: -0.045em;
font-weight: 850;
}
#title p {
margin: 9px 0 0;
color: #b7cbc2;
font-size: 12px;
line-height: 1.45;
max-width: 360px;
}
#stats {
position: fixed;
left: 24px;
top: 142px;
width: 260px;
padding: 15px;
border-radius: 15px;
pointer-events: none;
}
.stat-grid {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 12px;
}
.stat-label {
color: var(--muted);
font-size: 9px;
letter-spacing: 0.14em;
text-transform: uppercase;
}
.stat-value {
margin-top: 3px;
font-size: 23px;
font-weight: 800;
letter-spacing: -0.04em;
font-variant-numeric: tabular-nums;
}
.stat-value.accent { color: var(--accent); }
.stat-value.warning { color: var(--warning); }
#phase {
margin-top: 13px;
padding-top: 11px;
border-top: 1px solid rgba(255,255,255,0.09);
display: flex;
justify-content: space-between;
align-items: center;
gap: 12px;
font-size: 11px;
}
#phase strong {
color: var(--accent);
font-size: 10px;
letter-spacing: 0.1em;
text-transform: uppercase;
text-align: right;
}
#sidePanel {
position: fixed;
top: 22px;
right: 22px;
width: 360px;
max-height: calc(100vh - 118px);
border-radius: 17px;
overflow: hidden;
pointer-events: auto;
}
.panel-head {
padding: 14px 16px 10px;
display: flex;
justify-content: space-between;
align-items: center;
gap: 12px;
}
.panel-head h2 {
margin: 0;
font-size: 12px;
letter-spacing: 0.13em;
text-transform: uppercase;
}
.panel-head span {
color: var(--muted);
font-size: 10px;
}
#chart {
display: block;
width: 100%;
height: 142px;
}
.chart-caption {
padding: 0 17px 10px;
color: var(--muted);
font-size: 10px;
line-height: 1.4;
}
.registry-head {
display: flex;
justify-content: space-between;
align-items: center;
padding: 11px 16px;
border-top: 1px solid rgba(255,255,255,0.09);
border-bottom: 1px solid rgba(255,255,255,0.09);
}
.registry-head strong {
font-size: 10px;
letter-spacing: 0.14em;
text-transform: uppercase;
}
.text-button {
border: 0;
background: transparent;
color: var(--accent);
font-size: 10px;
cursor: pointer;
padding: 4px;
}
#lineages {
max-height: 254px;
overflow-y: auto;
scrollbar-width: thin;
scrollbar-color: #365e51 transparent;
}
.lineage-row {
width: 100%;
display: grid;
grid-template-columns: 10px 1fr auto;
align-items: center;
gap: 10px;
border: 0;
border-bottom: 1px solid rgba(255,255,255,0.055);
background: transparent;
color: var(--text);
text-align: left;
padding: 9px 16px;
cursor: pointer;
transition: background 120ms, opacity 120ms;
}
.lineage-row:hover,
.lineage-row.selected {
background: rgba(121, 242, 189, 0.09);
}
.lineage-row.muted { opacity: 0.48; }
.lineage-dot {
width: 8px;
height: 8px;
border-radius: 2px;
box-shadow: 0 0 12px currentColor;
}
.lineage-name {
font-size: 11px;
font-weight: 700;
}
.lineage-detail {
color: var(--muted);
font-size: 9px;
margin-top: 2px;
}
.lineage-count {
font-size: 11px;
color: var(--accent);
font-variant-numeric: tabular-nums;
}
#eventBanner {
position: fixed;
top: 24px;
left: 50%;
transform: translate(-50%, -18px);
width: min(520px, calc(100vw - 48px));
padding: 15px 20px;
border-radius: 14px;
text-align: center;
opacity: 0;
pointer-events: none;
transition: opacity 250ms, transform 250ms;
border-color: rgba(255, 177, 92, 0.36);
}
#eventBanner.show {
opacity: 1;
transform: translate(-50%, 0);
}
#eventTitle {
color: var(--warning);
font-size: 12px;
font-weight: 900;
letter-spacing: 0.18em;
text-transform: uppercase;
}
#eventText {
margin-top: 5px;
color: #e8d7bd;
font-size: 12px;
line-height: 1.4;
}
#controls {
position: fixed;
left: 50%;
bottom: 20px;
transform: translateX(-50%);
width: min(940px, calc(100vw - 40px));
border-radius: 17px;
padding: 12px 14px;
display: grid;
grid-template-columns: auto auto auto 1fr auto;
align-items: center;
gap: 10px;
}
.button {
border: 1px solid rgba(255,255,255,0.12);
border-radius: 10px;
padding: 9px 12px;
color: var(--text);
background: rgba(255,255,255,0.055);
cursor: pointer;
font-size: 11px;
font-weight: 700;
transition: background 120ms, border-color 120ms;
}
.button:hover {
background: rgba(121,242,189,0.11);
border-color: rgba(121,242,189,0.3);
}
#play {
min-width: 82px;
background: var(--accent);
color: #092017;
border-color: transparent;
}
select {
border: 1px solid rgba(255,255,255,0.12);
border-radius: 9px;
padding: 8px 26px 8px 9px;
color: var(--text);
background: #102520;
font-size: 11px;
}
#timeline {
width: 100%;
accent-color: var(--accent);
cursor: pointer;
}
#yearReadout {
min-width: 76px;
color: var(--accent);
font-size: 11px;
font-weight: 800;
text-align: right;
font-variant-numeric: tabular-nums;
}
.timeline-labels {
grid-column: 4;
display: flex;
justify-content: space-between;
color: var(--muted);
font-size: 8px;
margin-top: -8px;
pointer-events: none;
}
#hint {
position: fixed;
left: 24px;
bottom: 25px;
color: rgba(220,255,238,0.58);
font-size: 9px;
pointer-events: none;
line-height: 1.5;
}
#passLabel,
#survivorLabel {
position: fixed;
pointer-events: none;
transform: translate(-50%, -100%);
white-space: nowrap;
opacity: 0;
transition: opacity 150ms;
}
#passLabel {
padding: 5px 8px;
border-radius: 6px;
color: #ffe3b4;
background: rgba(38, 22, 10, 0.78);
border: 1px solid rgba(255,177,92,0.28);
font-size: 9px;
font-weight: 800;
letter-spacing: 0.13em;
}
#survivorLabel {
padding: 6px 9px;
border-radius: 6px;
color: #d9ffef;
background: rgba(5, 35, 26, 0.84);
border: 1px solid rgba(121,242,189,0.35);
font-size: 9px;
font-weight: 800;
letter-spacing: 0.08em;
}
@media (max-width: 900px) {
#sidePanel {
width: 300px;
}
#title p, #stats, #hint {
display: none;
}
}
@media (max-width: 650px) {
#sidePanel {
top: auto;
bottom: 106px;
right: 12px;
width: 245px;
max-height: 220px;
}
#chart {
height: 100px;
}
.registry-head, .chart-caption {
display: none;
}
#lineages {
max-height: 145px;
}
#title {
left: 14px;
top: 14px;
}
#title h1 {
font-size: 25px;
}
#controls {
width: calc(100vw - 20px);
bottom: 10px;
grid-template-columns: auto auto 1fr auto;
gap: 6px;
padding: 9px;
}
#replay, #speed, .timeline-labels {
display: none;
}
#eventBanner {
top: 80px;
}
}
</style>
</head>
<body>
<div id="stage"></div>
<div id="title">
<div class="eyebrow">A fictional population history</div>
<h1>The Narrow Pass</h1>
<p>
A valley population crashes to twelve survivors, then rebuilds.
Colors identify the bloodlines carried through the bottleneck.
</p>
</div>
<div id="stats" class="glass">
<div class="stat-grid">
<div>
<div class="stat-label">Living population</div>
<div id="population" class="stat-value">180</div>
</div>
<div>
<div class="stat-label">Survivors</div>
<div id="survivorCount" class="stat-value warning">—</div>
</div>
<div>
<div class="stat-label">Descendants</div>
<div id="descendantCount" class="stat-value accent">0</div>
</div>
<div>
<div class="stat-label">Generations</div>
<div id="generationCount" class="stat-value">1</div>
</div>
</div>
<div id="phase">
<span>Current chapter</span>
<strong id="phaseName">Founding census</strong>
</div>
</div>
<div id="sidePanel" class="glass">
<div class="panel-head">
<h2>Population record</h2>
<span id="chartYear">Year 0</span>
</div>
<svg id="chart" viewBox="0 0 380 142" aria-label="Population over forty years">
<defs>
<linearGradient id="areaGradient" x1="0" y1="0" x2="0" y2="1">
<stop offset="0%" stop-color="#79f2bd" stop-opacity="0.24"/>
<stop offset="100%" stop-color="#79f2bd" stop-opacity="0"/>
</linearGradient>
</defs>
<line x1="27" y1="112" x2="360" y2="112" stroke="rgba(255,255,255,.12)"/>
<line x1="27" y1="66" x2="360" y2="66" stroke="rgba(255,255,255,.07)"/>
<line x1="27" y1="20" x2="360" y2="20" stroke="rgba(255,255,255,.07)"/>
<text x="2" y="24" fill="#718b80" font-size="9">240</text>
<text x="10" y="70" fill="#718b80" font-size="9">120</text>
<text x="18" y="115" fill="#718b80" font-size="9">0</text>
<line
id="eventLine"
x1="0" x2="0" y1="13" y2="112"
stroke="#ff765e"
stroke-width="1"
stroke-dasharray="3 4"
opacity=".8"
/>
<path id="areaPath" fill="url(#areaGradient)"/>
<path id="futurePath" fill="none" stroke="rgba(160,200,185,.22)" stroke-width="1.5"/>
<path id="pastPath" fill="none" stroke="#79f2bd" stroke-width="2.4"/>
<circle id="chartDot" r="4" fill="#eafff5" stroke="#79f2bd" stroke-width="2"/>
<text x="27" y="132" fill="#718b80" font-size="9">0</text>
<text x="125" y="132" fill="#ff9e84" font-size="9">13 · bottleneck</text>
<text x="337" y="132" fill="#718b80" font-size="9">40</text>
</svg>
<div class="chart-caption">
The dashed line marks the pass collapse. Faint values show the possible future.
</div>
<div class="registry-head">
<strong>Survivor registry</strong>
<button id="allLineages" class="text-button">Show all</button>
</div>
<div id="lineages"></div>
</div>
<div id="eventBanner" class="glass">
<div id="eventTitle">The pass collapses</div>
<div id="eventText"></div>
</div>
<div id="passLabel">NARROW PASS</div>
<div id="survivorLabel">12 SURVIVORS · 12 BLOODLINES</div>
<div id="controls" class="glass">
<button id="play" class="button">Pause</button>
<button id="replay" class="button">Replay</button>
<select id="speed" aria-label="Playback speed">
<option value="0.5">0.5×</option>
<option value="1" selected>1×</option>
<option value="2">2×</option>
<option value="4">4×</option>
</select>
<div>
<input id="timeline" type="range" min="0" max="40" step="0.01" value="0" />
<div class="timeline-labels">
<span>Founding valley</span>
<span>Pass collapse</span>
<span>Recovery</span>
</div>
</div>
<div id="yearReadout">Year 0.0</div>
</div>
<div id="hint">
Drag to orbit · Shift/right-drag to pan · Scroll to zoom<br />
Double-click to inspect the pass · Click a bloodline to track it
</div>
<script type="module">
import * as THREE from "https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
/* ------------------------------------------------------------------ */
/* Utilities */
/* ------------------------------------------------------------------ */
const $ = id => document.getElementById(id);
const clamp = THREE.MathUtils.clamp;
const lerp = THREE.MathUtils.lerp;
const smooth = THREE.MathUtils.smoothstep;
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(913204);
const randomRange = (a, b) => a + (b - a) * random();
function hash2(x, z) {
const s = Math.sin(x * 127.1 + z * 311.7) * 43758.5453123;
return s - Math.floor(s);
}
/* ------------------------------------------------------------------ */
/* Fictional demographic simulation */
/* ------------------------------------------------------------------ */
const END_YEAR = 40;
const COLLAPSE_YEAR = 13;
const LINEAGE_COUNT = 12;
const survivorNames = [
"Aster", "Brin", "Cinder", "Dawn", "Ember", "Fern",
"Gale", "Hollow", "Iris", "Juniper", "Kite", "Lumen"
];
const lineageColors = Array.from({ length: LINEAGE_COUNT }, (_, i) =>
new THREE.Color().setHSL((i / LINEAGE_COUNT + 0.03) % 1, 0.72, 0.57)
);
const agents = [];
let nextAgentId = 0;
function addAgent(born, parent, lineage, generation) {
const agent = {
id: nextAgentId++,
born,
died: null,
parent: parent ? parent.id : null,
lineage,
generation,
survivor: false,
home: new THREE.Vector3(),
postHome: new THREE.Vector3()
};
agents.push(agent);
return agent;
}
function activeAt(year) {
return agents.filter(a =>
a.born <= year && (a.died === null || year < a.died)
);
}
/* Twelve founder bloodlines, with an initial valley population of 180. */
for (let lineage = 0; lineage < LINEAGE_COUNT; lineage++) {
const root = addAgent(0, null, lineage, 0);
for (let child = 0; child < 14; child++) {
addAgent(0, root, lineage, 1);
}
}
/* Four net residents per year before the pass collapse. */
for (let year = 1; year <= 12; year++) {
const pool = activeAt(year - 1);
for (let i = 0; i < 5; i++) {
const parent = pool[Math.floor(random() * pool.length)];
const born = year - 1 + 0.16 + i * 0.13 + random() * 0.025;
addAgent(born, parent, parent.lineage, parent.generation + 1);
}
const deathCandidates = pool.filter(a => a.id >= LINEAGE_COUNT);
const victim = deathCandidates[Math.floor(random() * deathCandidates.length)];
victim.died = year - 1 + 0.93;
}
/* Select one survivor from each bloodline at the collapse. */
const preCollapse = activeAt(12);
const survivors = [];
for (let lineage = 0; lineage < LINEAGE_COUNT; lineage++) {
const candidates = preCollapse.filter(a => a.lineage === lineage);
const survivor = candidates[Math.floor(random() * candidates.length)];
survivor.survivor = true;
survivors.push(survivor);
}
for (const agent of preCollapse) {
if (!agent.survivor) {
agent.died = 12.82 + random() * 0.16;
}
}
/* Logistic recovery after the bottleneck. */
for (let year = 14; year <= END_YEAR; year++) {
const previousCount = activeAt(year - 1).length;
const targetCount = Math.round(
12 + 146 * (1 - Math.exp(-(year - COLLAPSE_YEAR) / 14))
);
const births = Math.max(0, targetCount - previousCount);
const pool = activeAt(year - 1);
for (let i = 0; i < births; i++) {
const parent = pool[Math.floor(random() * pool.length)];
const born = year - 1 + 0.16 + (i / Math.max(1, births)) * 0.65 + random() * 0.08;
addAgent(born, parent, parent.lineage, parent.generation + 1);
}
}
const annualCounts = Array.from({ length: END_YEAR + 1 }, (_, year) =>
activeAt(year).length
);
/* Pre-collapse homes are scattered; later descendants gather near the pass. */
const camp = new THREE.Vector3(-3.2, 0, 7.2);
function riverCenter(z) {
return Math.sin(z * 0.22) * 2.2 + Math.sin(z * 0.07) * 1.1;
}
for (const agent of agents) {
let x, z;
do {
x = randomRange(-16, 16);
z = randomRange(-19, 19);
} while (
Math.abs(x - riverCenter(z)) < 2.2 &&
random() > 0.15
);
agent.home.set(x, 0, z);
const angle = random() * Math.PI * 2;
const radius = randomRange(1.2, 5.8);
let px = camp.x + Math.cos(angle) * radius;
let pz = camp.z + Math.sin(angle) * radius;
if (Math.abs(px - riverCenter(pz)) < 2) {
px = riverCenter(pz) - 2.5;
}
agent.postHome.set(px, 0, pz);
}
/* ------------------------------------------------------------------ */
/* Three.js scene */
/* ------------------------------------------------------------------ */
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x9fc4ba);
scene.fog = new THREE.FogExp2(0x9fc4ba, 0.018);
const camera = new THREE.PerspectiveCamera(
46,
innerWidth / innerHeight,
0.1,
220
);
const renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setPixelRatio(Math.min(devicePixelRatio, 1.75));
renderer.setSize(innerWidth, innerHeight);
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.12;
$("stage").appendChild(renderer.domElement);
const hemi = new THREE.HemisphereLight(0xd9fff0, 0x25382e, 2.15);
scene.add(hemi);
const sun = new THREE.DirectionalLight(0xffe5b8, 3.1);
sun.position.set(-22, 36, -18);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -42;
sun.shadow.camera.right = 42;
sun.shadow.camera.top = 42;
sun.shadow.camera.bottom = -42;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 100;
sun.shadow.bias = -0.0008;
scene.add(sun);
const rimLight = new THREE.DirectionalLight(0x85d9ff, 0.75);
rimLight.position.set(20, 12, 24);
scene.add(rimLight);
/* ------------------------------------------------------------------ */
/* Voxel terrain */
/* ------------------------------------------------------------------ */
function terrainHeight(x, z) {
const center = riverCenter(z);
const distance = Math.abs(x - center);
if (distance < 1.45) return 0.1;
if (distance < 2.25) return 0.45;
const passPinching = 1 - 0.36 * Math.exp(-Math.pow((z - 7) / 3.4, 2));
const normalizedDistance = distance / (25 * passPinching);
let height =
0.55 +
Math.pow(Math.max(0, normalizedDistance), 1.68) * 13.5;
const noise = (hash2(Math.floor(x * 0.72), Math.floor(z * 0.72)) - 0.5) * 2;
height += noise * (0.45 + Math.min(height * 0.12, 1.15));
if (z > 19) height += (z - 19) * 0.22;
if (z < -19) height += (-19 - z) * 0.17;
return Math.floor(Math.max(0.1, height) * 2) / 2;
}
const NX = 54;
const NZ = 46;
const SPACING = 1.1;
const terrainGeometry = new THREE.BoxGeometry(1.06, 1, 1.06);
const terrainMaterial = new THREE.MeshStandardMaterial({
color: 0xffffff,
roughness: 0.96,
metalness: 0
});
const terrain = new THREE.InstancedMesh(
terrainGeometry,
terrainMaterial,
NX * NZ
);
terrain.receiveShadow = true;
terrain.castShadow = true;
scene.add(terrain);
const terrainDummy = new THREE.Object3D();
const terrainColor = new THREE.Color();
let terrainIndex = 0;
const waterCells = [];
const treeCandidates = [];
for (let iz = 0; iz < NZ; iz++) {
for (let ix = 0; ix < NX; ix++) {
const x = (ix - (NX - 1) / 2) * SPACING;
const z = (iz - (NZ - 1) / 2) * SPACING;
const height = terrainHeight(x, z);
terrainDummy.position.set(x, (height - 1) / 2, z);
terrainDummy.scale.set(1, height + 1, 1);
terrainDummy.updateMatrix();
terrain.setMatrixAt(terrainIndex, terrainDummy.matrix);
const variation = hash2(ix * 3.1, iz * 5.7);
if (height < 1.1) {
terrainColor.setHSL(0.31 + variation * 0.025, 0.36, 0.31 + variation * 0.05);
} else if (height < 6.5) {
terrainColor.setHSL(0.27 + variation * 0.035, 0.34, 0.25 + variation * 0.07);
} else {
terrainColor.setHSL(0.12 + variation * 0.035, 0.16, 0.37 + variation * 0.08);
}
terrain.setColorAt(terrainIndex, terrainColor);
terrainIndex++;
if (Math.abs(x - riverCenter(z)) < 1.55) {
waterCells.push({ x, z });
} else if (
height > 0.6 &&
height < 6.7 &&
Math.abs(z - camp.z) > 3.2 &&
variation > 0.77
) {
treeCandidates.push({ x, z, height, variation });
}
}
}
terrain.instanceMatrix.needsUpdate = true;
terrain.instanceColor.needsUpdate = true;
/* Voxel river. */
const waterGeometry = new THREE.BoxGeometry(1.07, 0.12, 1.07);
const waterMaterial = new THREE.MeshStandardMaterial({
color: 0x4ca9b9,
transparent: true,
opacity: 0.76,
roughness: 0.2,
metalness: 0.12
});
const water = new THREE.InstancedMesh(
waterGeometry,
waterMaterial,
waterCells.length
);
waterCells.forEach((cell, i) => {
terrainDummy.position.set(cell.x, 0.19, cell.z);
terrainDummy.scale.set(1, 1, 1);
terrainDummy.updateMatrix();
water.setMatrixAt(i, terrainDummy.matrix);
});
water.receiveShadow = true;
scene.add(water);
/* Block trees. */
const treeCount = Math.min(135, treeCandidates.length);
const trunks = new THREE.InstancedMesh(
new THREE.BoxGeometry(0.24, 1.1, 0.24),
new THREE.MeshStandardMaterial({ color: 0x60452f, roughness: 1 }),
treeCount
);
const crowns = new THREE.InstancedMesh(
new THREE.BoxGeometry(0.95, 0.95, 0.95),
new THREE.MeshStandardMaterial({ color: 0xffffff, roughness: 0.92 }),
treeCount
);
const crownTops = new THREE.InstancedMesh(
new THREE.BoxGeometry(0.65, 0.65, 0.65),
new THREE.MeshStandardMaterial({ color: 0xffffff, roughness: 0.92 }),
treeCount
);
for (let i = 0; i < treeCount; i++) {
const tree = treeCandidates[i];
const scale = 0.8 + tree.variation * 0.6;
terrainDummy.position.set(tree.x, tree.height + 0.55 * scale, tree.z);
terrainDummy.scale.set(scale, scale, scale);
terrainDummy.updateMatrix();
trunks.setMatrixAt(i, terrainDummy.matrix);
terrainDummy.position.set(tree.x, tree.height + 1.35 * scale, tree.z);
terrainDummy.scale.set(scale, scale, scale);
terrainDummy.updateMatrix();
crowns.setMatrixAt(i, terrainDummy.matrix);
terrainDummy.position.set(tree.x, tree.height + 2.0 * scale, tree.z);
terrainDummy.scale.set(scale, scale, scale);
terrainDummy.updateMatrix();
crownTops.setMatrixAt(i, terrainDummy.matrix);
const color = new THREE.Color().setHSL(
0.34 + tree.variation * 0.04,
0.42,
0.23 + tree.variation * 0.07
);
crowns.setColorAt(i, color);
crownTops.setColorAt(i, color.clone().offsetHSL(0, 0, 0.035));
}
trunks.castShadow = true;
crowns.castShadow = true;
crownTops.castShadow = true;
scene.add(trunks, crowns, crownTops);
/* Small voxel rocks. */
const rockCount = 42;
const rocks = new THREE.InstancedMesh(
new THREE.BoxGeometry(0.65, 0.55, 0.65),
new THREE.MeshStandardMaterial({ color: 0xffffff, roughness: 1 }),
rockCount
);
for (let i = 0; i < rockCount; i++) {
const x = randomRange(-24, 24);
const z = randomRange(-21, 21);
const h = terrainHeight(x, z);
const s = randomRange(0.45, 1.25);
terrainDummy.position.set(x, h + 0.2 * s, z);
terrainDummy.scale.set(s, s * 0.75, s);
terrainDummy.rotation.set(0, random() * Math.PI, 0);
terrainDummy.updateMatrix();
rocks.setMatrixAt(i, terrainDummy.matrix);
rocks.setColorAt(
i,
new THREE.Color().setHSL(0.11, 0.1, 0.34 + random() * 0.12)
);
}
rocks.castShadow = true;
scene.add(rocks);
/* ------------------------------------------------------------------ */
/* Waystation and expanding settlement */
/* ------------------------------------------------------------------ */
const campY = terrainHeight(camp.x, camp.z);
camp.y = campY;
const waystation = new THREE.Group();
waystation.position.copy(camp);
const hutMaterial = new THREE.MeshStandardMaterial({
color: 0xe2d2a5,
roughness: 0.9
});
const roofMaterial = new THREE.MeshStandardMaterial({
color: 0x3c8d76,
roughness: 0.85
});
function makeHut(x, z, scale = 1) {
const group = new THREE.Group();
group.position.set(x, terrainHeight(camp.x + x, camp.z + z), camp.z + z);
const body = new THREE.Mesh(
new THREE.BoxGeometry(1.05 * scale, 0.65 * scale, 0.9 * scale),
hutMaterial
);
body.position.y = 0.33 * scale;
body.castShadow = true;
const roof = new THREE.Mesh(
new THREE.BoxGeometry(1.25 * scale, 0.22 * scale, 1.08 * scale),
roofMaterial
);
roof.position.y = 0.76 * scale;
roof.castShadow = true;
group.add(body, roof);
return group;
}
waystation.add(makeHut(-2.4, 0.8, 0.75));
waystation.add(makeHut(1.9, 1.2, 0.65));
scene.add(waystation);
const beacon = new THREE.Group();
beacon.position.set(camp.x, campY, camp.z);
const beaconPost = new THREE.Mesh(
new THREE.BoxGeometry(0.18, 3.2, 0.18),
new THREE.MeshStandardMaterial({ color: 0x29463d, roughness: 0.8 })
);
beaconPost.position.y = 1.6;
const beaconTop = new THREE.Mesh(
new THREE.BoxGeometry(0.48, 0.48, 0.48),
new THREE.MeshStandardMaterial({
color: 0x8affc7,
emissive: 0x2d9f70,
emissiveIntensity: 1.2
})
);
beaconTop.position.y = 3.25;
const beaconLight = new THREE.PointLight(0x7fffbf, 1.3, 10);
beaconLight.position.y = 3.3;
beacon.add(beaconPost, beaconTop, beaconLight);
scene.add(beacon);
/* Descendant settlement appears as the population recovers. */
const settlementHuts = [];
for (let i = 0; i < 24; i++) {
const angle = i * 2.39996;
const radius = 3.4 + (i % 5) * 1.15;
const x = camp.x + Math.cos(angle) * radius;
const z = camp.z + Math.sin(angle) * radius;
settlementHuts.push({ x, z, height: terrainHeight(x, z) });
}
const settlementMaterial = new THREE.MeshStandardMaterial({
color: 0xffffff,
roughness: 0.92
});
const settlement = new THREE.InstancedMesh(
new THREE.BoxGeometry(0.65, 0.5, 0.65),
settlementMaterial,
settlementHuts.length
);
settlementHuts.forEach((hut, i) => {
terrainDummy.position.set(hut.x, hut.height + 0.25, hut.z);
terrainDummy.scale.set(1, 1, 1);
terrainDummy.rotation.set(0, i * 0.71, 0);
terrainDummy.updateMatrix();
settlement.setMatrixAt(i, terrainDummy.matrix);
settlement.setColorAt(
i,
new THREE.Color().setHSL(0.11 + (i % 3) * 0.018, 0.32, 0.62)
);
});
settlement.castShadow = true;
settlement.count = 0;
scene.add(settlement);
/* ------------------------------------------------------------------ */
/* Population agents */
/* ------------------------------------------------------------------ */
const maxAgents = agents.length;
const bodyMesh = new THREE.InstancedMesh(
new THREE.BoxGeometry(0.3, 0.4, 0.27),
new THREE.MeshStandardMaterial({ color: 0xffffff, roughness: 0.78 }),
maxAgents
);
const headMesh = new THREE.InstancedMesh(
new THREE.BoxGeometry(0.23, 0.22, 0.23),
new THREE.MeshStandardMaterial({ color: 0xffffff, roughness: 0.78 }),
maxAgents
);
const earMesh = new THREE.InstancedMesh(
new THREE.BoxGeometry(0.09, 0.17, 0.08),
new THREE.MeshStandardMaterial({ color: 0xffffff, roughness: 0.8 }),
maxAgents * 2
);
for (const mesh of [bodyMesh, headMesh, earMesh]) {
mesh.instanceMatrix.setUsage(THREE.DynamicDrawUsage);
mesh.frustumCulled = false;
mesh.castShadow = true;
scene.add(mesh);
}
const agentDummy = new THREE.Object3D();
const agentColor = new THREE.Color();
const mutedColor = new THREE.Color(0x52665d);
const headColor = new THREE.Color();
let selectedLineage = null;
function agentPosition(agent, year, out) {
let x, z;
if (year < 12) {
x = agent.home.x;
z = agent.home.z;
} else if (agent.born > 13 || agent.survivor) {
const migration = agent.survivor
? smooth(year, 12, 13.35)
: 1;
x = lerp(agent.home.x, agent.postHome.x, migration);
z = lerp(agent.home.z, agent.postHome.z, migration);
} else {
x = agent.home.x;
z = agent.home.z;
}
const wander = Math.sin(year * 1.4 + agent.id * 0.71) * 0.11;
out.set(x + wander, terrainHeight(x, z), z);
return out;
}
const agentPositionVector = new THREE.Vector3();
function updateAgents(year) {
const active = activeAt(year);
let visible = 0;
for (const agent of active) {
agentPosition(agent, year, agentPositionVector);
const age = Math.max(0, year - agent.born);
const juvenile = clamp(0.68 + age * 0.22, 0.68, 1);
const bob = Math.sin(year * 5.2 + agent.id * 1.37) * 0.035;
agentDummy.position.set(
agentPositionVector.x,
agentPositionVector.y + 0.21 * juvenile + bob,
agentPositionVector.z
);
agentDummy.rotation.set(0, agent.id * 0.37, 0);
agentDummy.scale.set(juvenile, juvenile, juvenile);
agentDummy.updateMatrix();
bodyMesh.setMatrixAt(visible, agentDummy.matrix);
agentDummy.position.y += 0.29 * juvenile;
agentDummy.scale.setScalar(juvenile);
agentDummy.updateMatrix();
headMesh.setMatrixAt(visible, agentDummy.matrix);
agentDummy.position.set(
agentPositionVector.x - 0.085 * juvenile,
agentPositionVector.y + 0.46 * juvenile + bob,
agentPositionVector.z
);
agentDummy.updateMatrix();
earMesh.setMatrixAt(visible * 2, agentDummy.matrix);
agentDummy.position.x = agentPositionVector.x + 0.085 * juvenile;
agentDummy.updateMatrix();
earMesh.setMatrixAt(visible * 2 + 1, agentDummy.matrix);
agentColor.copy(lineageColors[agent.lineage]);
if (selectedLineage !== null && agent.lineage !== selectedLineage) {
agentColor.lerp(mutedColor, 0.74);
}
bodyMesh.setColorAt(visible, agentColor);
headColor.copy(agentColor).offsetHSL(0, -0.08, 0.1);
headMesh.setColorAt(visible, headColor);
earMesh.setColorAt(visible * 2, headColor);
earMesh.setColorAt(visible * 2 + 1, headColor);
visible++;
}
bodyMesh.count = visible;
headMesh.count = visible;
earMesh.count = visible * 2;
bodyMesh.instanceMatrix.needsUpdate = true;
headMesh.instanceMatrix.needsUpdate = true;
earMesh.instanceMatrix.needsUpdate = true;
if (bodyMesh.instanceColor) bodyMesh.instanceColor.needsUpdate = true;
if (headMesh.instanceColor) headMesh.instanceColor.needsUpdate = true;
if (earMesh.instanceColor) earMesh.instanceColor.needsUpdate = true;
return active;
}
/* Survivor rings. */
const survivorRings = survivors.map(() => {
const ring = new THREE.Mesh(
new THREE.TorusGeometry(0.42, 0.035, 6, 20),
new THREE.MeshBasicMaterial({
color: 0xffd27b,
transparent: true,
opacity: 0
})
);
ring.rotation.x = Math.PI / 2;
scene.add(ring);
return ring;
});
/* ------------------------------------------------------------------ */
/* Collapse dust and shock wave */
/* ------------------------------------------------------------------ */
const dustCount = 190;
const dustPositions = new Float32Array(dustCount * 3);
const dustData = [];
for (let i = 0; i < dustCount; i++) {
dustData.push({
angle: random() * Math.PI * 2,
radius: randomRange(0.2, 1.8),
height: randomRange(0.2, 5.5),
speed: randomRange(0.7, 1.8),
drift: randomRange(-1.5, 1.5)
});
}
const dustGeometry = new THREE.BufferGeometry();
dustGeometry.setAttribute(
"position",
new THREE.BufferAttribute(dustPositions, 3)
);
const dustMaterial = new THREE.PointsMaterial({
color: 0xffa24f,
size: 0.18,
transparent: true,
opacity: 0,
depthWrite: false
});
const dust = new THREE.Points(dustGeometry, dustMaterial);
dust.frustumCulled = false;
scene.add(dust);
const shockRing = new THREE.Mesh(
new THREE.TorusGeometry(1, 0.055, 6, 64),
new THREE.MeshBasicMaterial({
color: 0xffb15c,
transparent: true,
opacity: 0
})
);
shockRing.rotation.x = Math.PI / 2;
shockRing.position.set(camp.x, campY + 0.18, camp.z);
scene.add(shockRing);
/* ------------------------------------------------------------------ */
/* Clouds and distant voxel sun */
/* ------------------------------------------------------------------ */
const cloudMaterial = new THREE.MeshLambertMaterial({
color: 0xf0fff5,
transparent: true,
opacity: 0.58,
depthWrite: false
});
const clouds = [];
for (let i = 0; i < 9; i++) {
const cloud = new THREE.Group();
cloud.position.set(randomRange(-42, 42), randomRange(15, 23), randomRange(-42, 42));
for (let j = 0; j < 4; j++) {
const block = new THREE.Mesh(
new THREE.BoxGeometry(randomRange(2, 5), randomRange(0.6, 1.5), randomRange(1.5, 3.5)),
cloudMaterial
);
block.position.set(j * 1.8, randomRange(-0.2, 0.45), randomRange(-0.5, 0.5));
cloud.add(block);
}
scene.add(cloud);
clouds.push(cloud);
}
const voxelSun = new THREE.Mesh(
new THREE.BoxGeometry(3.6, 3.6, 3.6),
new THREE.MeshBasicMaterial({ color: 0xffe3a1 })
);
voxelSun.position.set(-28, 24, -36);
scene.add(voxelSun);
/* ------------------------------------------------------------------ */
/* Camera controls implemented directly */
/* ------------------------------------------------------------------ */
const cameraState = {
target: new THREE.Vector3(0, 2, 0),
desiredTarget: new THREE.Vector3(0, 2, 0),
radius: 48,
desiredRadius: 48,
theta: 0.72,
desiredTheta: 0.72,
phi: 1.0,
desiredPhi: 1.0
};
const pointers = new Map();
let pinchDistance = 0;
let dragging = false;
let dragButton = 0;
const canvas = renderer.domElement;
function updateCamera() {
const s = Math.sin(cameraState.phi);
const desiredPosition = new THREE.Vector3(
cameraState.target.x + cameraState.radius * s * Math.sin(cameraState.theta),
cameraState.target.y + cameraState.radius * Math.cos(cameraState.phi),
cameraState.target.z + cameraState.radius * s * Math.cos(cameraState.theta)
);
camera.position.lerp(desiredPosition, 0.08);
cameraState.target.lerp(cameraState.desiredTarget, 0.08);
camera.lookAt(cameraState.target);
}
function focusPass() {
cameraState.desiredTarget.set(camp.x, campY + 1.2, camp.z);
cameraState.desiredRadius = 23;
cameraState.desiredTheta = 0.7;
cameraState.desiredPhi = 1.05;
}
function focusValley() {
cameraState.desiredTarget.set(0, 2, 0);
cameraState.desiredRadius = 48;
cameraState.desiredTheta = 0.72;
cameraState.desiredPhi = 1.0;
}
canvas.addEventListener("contextmenu", event => event.preventDefault());
canvas.addEventListener("pointerdown", event => {
canvas.setPointerCapture(event.pointerId);
pointers.set(event.pointerId, { x: event.clientX, y: event.clientY });
dragging = true;
dragButton = event.button;
if (pointers.size === 2) {
const [a, b] = [...pointers.values()];
pinchDistance = Math.hypot(a.x - b.x, a.y - b.y);
}
});
canvas.addEventListener("pointermove", event => {
if (!pointers.has(event.pointerId)) return;
const previous = pointers.get(event.pointerId);
const dx = event.clientX - previous.x;
const dy = event.clientY - previous.y;
pointers.set(event.pointerId, { x: event.clientX, y: event.clientY });
if (pointers.size === 2) {
const [a, b] = [...pointers.values()];
const distance = Math.hypot(a.x - b.x, a.y - b.y);
if (pinchDistance > 0) {
cameraState.desiredRadius = clamp(
cameraState.desiredRadius * pinchDistance / distance,
11,
82
);
}
pinchDistance = distance;
return;
}
if (event.shiftKey || dragButton === 2) {
const forward = new THREE.Vector3()
.subVectors(cameraState.desiredTarget, camera.position);
forward.y = 0;
forward.normalize();
const right = new THREE.Vector3()
.crossVectors(forward, camera.up)
.normalize();
const panScale = cameraState.desiredRadius * 0.0016;
cameraState.desiredTarget.addScaledVector(right, -dx * panScale);
cameraState.desiredTarget.addScaledVector(forward, dy * panScale);
cameraState.desiredTarget.x = clamp(cameraState.desiredTarget.x, -25, 25);
cameraState.desiredTarget.z = clamp(cameraState.desiredTarget.z, -24, 24);
} else {
cameraState.desiredTheta -= dx * 0.005;
cameraState.desiredPhi = clamp(cameraState.desiredPhi - dy * 0.005, 0.35, 1.42);
}
});
function releasePointer(event) {
pointers.delete(event.pointerId);
if (pointers.size < 2) pinchDistance = 0;
if (pointers.size === 0) dragging = false;
}
canvas.addEventListener("pointerup", releasePointer);
canvas.addEventListener("pointercancel", releasePointer);
canvas.addEventListener("wheel", event => {
event.preventDefault();
cameraState.desiredRadius = clamp(
cameraState.desiredRadius * Math.exp(event.deltaY * 0.001),
11,
82
);
}, { passive: false });
canvas.addEventListener("dblclick", focusPass);
/* ------------------------------------------------------------------ */
/* UI */
/* ------------------------------------------------------------------ */
let simulationYear = 0;
let playing = true;
let playbackSpeed = 1;
const timeline = $("timeline");
const playButton = $("play");
playButton.addEventListener("click", () => {
if (simulationYear >= END_YEAR) simulationYear = 0;
playing = !playing;
playButton.textContent = playing ? "Pause" : "Play";
});
$("replay").addEventListener("click", () => {
simulationYear = 0;
playing = true;
playButton.textContent = "Pause";
selectedLineage = null;
focusValley();
updateLineageSelection();
});
$("speed").addEventListener("change", event => {
playbackSpeed = Number(event.target.value);
});
timeline.addEventListener("input", event => {
simulationYear = Number(event.target.value);
playing = false;
playButton.textContent = "Play";
});
$("allLineages").addEventListener("click", () => {
selectedLineage = null;
updateLineageSelection();
});
const lineageRows = [];
for (let lineage = 0; lineage < LINEAGE_COUNT; lineage++) {
const row = document.createElement("button");
row.className = "lineage-row";
row.innerHTML = `
<span
class="lineage-dot"
style="background:#${lineageColors[lineage].getHexString()};
color:#${lineageColors[lineage].getHexString()}">
</span>
<span>
<span class="lineage-name">${survivorNames[lineage]}</span>
<span class="lineage-detail">Bloodline ${String(lineage + 1).padStart(2, "0")}</span>
</span>
<span class="lineage-count">—</span>
`;
row.addEventListener("click", () => {
selectedLineage = selectedLineage === lineage ? null : lineage;
updateLineageSelection();
if (selectedLineage !== null) focusPass();
});
$("lineages").appendChild(row);
lineageRows.push(row);
}
function updateLineageSelection() {
lineageRows.forEach((row, index) => {
row.classList.toggle("selected", selectedLineage === index);
row.classList.toggle(
"muted",
selectedLineage !== null && selectedLineage !== index
);
});
}
/* ------------------------------------------------------------------ */
/* Chart and labels */
/* ------------------------------------------------------------------ */
const chartX = year => 27 + (year / END_YEAR) * 333;
const chartY = count => 112 - (count / 240) * 92;
const fullPoints = annualCounts.map((count, year) =>
`${chartX(year)},${chartY(count)}`
).join(" ");
$("futurePath").setAttribute("d", `M ${fullPoints}`);
$("eventLine").setAttribute("x1", chartX(COLLAPSE_YEAR));
$("eventLine").setAttribute("x2", chartX(COLLAPSE_YEAR));
function updateChart(active) {
const visibleYear = Math.min(END_YEAR, simulationYear);
const wholeYear = Math.floor(visibleYear);
const fractional = visibleYear - wholeYear;
const currentCount = active.length;
const interpolated =
lerp(annualCounts[wholeYear], annualCounts[Math.min(END_YEAR, wholeYear + 1)], fractional);
const past = annualCounts
.slice(0, wholeYear + 1)
.map((count, year) => `${chartX(year)},${chartY(count)}`);
if (fractional > 0.001) {
past.push(`${chartX(visibleYear)},${chartY(interpolated)}`);
}
const path = `M ${past.join(" L ")}`;
$("pastPath").setAttribute("d", path);
$("areaPath").setAttribute(
"d",
`${path} L ${chartX(visibleYear)},112 L ${chartX(0)},112 Z`
);
$("chartDot").setAttribute("cx", chartX(visibleYear));
$("chartDot").setAttribute("cy", chartY(currentCount));
$("chartYear").textContent = `Year ${visibleYear.toFixed(1)}`;
}
function projectLabel(element, worldPosition) {
const projected = worldPosition.clone().project(camera);
const visible = projected.z > -1 && projected.z < 1;
element.style.opacity = visible ? "1" : "0";
if (!visible) return;
const x = (projected.x * 0.5 + 0.5) * innerWidth;
const y = (-projected.y * 0.5 + 0.5) * innerHeight;
element.style.left = `${x}px`;
element.style.top = `${y}px`;
}
const passLabelPosition = new THREE.Vector3(
camp.x,
campY + 5.4,
camp.z
);
const survivorLabelPosition = new THREE.Vector3(
camp.x,
campY + 4.2,
camp.z
);
/* ------------------------------------------------------------------ */
/* Main update */
/* ------------------------------------------------------------------ */
function updateEvent(active) {
let title = "";
let text = "";
let show = false;
if (simulationYear >= 12.35 && simulationYear < 13.35) {
show = true;
title = "The Narrow Pass collapses";
text = "A fictional rockfall seals the valley route. Twelve individuals reach the waystation alive.";
} else if (simulationYear >= 13.35 && simulationYear < 15.5) {
show = true;
title = "Twelve bloodlines, one future";
text = "Every living descendant now carries one of the survivor bloodlines.";
}
$("eventBanner").classList.toggle("show", show);
$("eventTitle").textContent = title;
$("eventText").textContent = text;
const dustProgress = (simulationYear - 12.72) / 1.25;
const dustVisible = dustProgress >= 0 && dustProgress <= 1;
dust.visible = dustVisible;
if (dustVisible) {
dustMaterial.opacity = Math.sin(dustProgress * Math.PI) * 0.92;
for (let i = 0; i < dustCount; i++) {
const d = dustData[i];
const angle = d.angle + dustProgress * d.speed;
const radius = d.radius + dustProgress * 4.8;
dustPositions[i * 3] = camp.x + Math.cos(angle) * radius;
dustPositions[i * 3 + 1] = campY + d.height * dustProgress + 0.15;
dustPositions[i * 3 + 2] = camp.z + Math.sin(angle) * radius + d.drift * dustProgress;
}
dustGeometry.attributes.position.needsUpdate = true;
}
const shockProgress = (simulationYear - 12.86) / 0.75;
shockRing.visible = shockProgress >= 0 && shockProgress <= 1;
if (shockRing.visible) {
shockRing.scale.setScalar(1 + shockProgress * 10);
shockRing.material.opacity = (1 - shockProgress) * 0.8;
}
const ringOpacity = smooth(simulationYear, 12.94, 13.35);
const ringPosition = new THREE.Vector3();
survivors.forEach((survivor, index) => {
agentPosition(survivor, simulationYear, ringPosition);
survivorRings[index].position.set(
ringPosition.x,
ringPosition.y + 0.12,
ringPosition.z
);
survivorRings[index].material.opacity = ringOpacity * 0.92;
survivorRings[index].rotation.z = simulationYear * 0.6 + index;
});
}
function updateStats(active) {
const descendants = active.filter(a => a.born > 13).length;
const maximumGeneration = active.reduce(
(maximum, agent) => Math.max(maximum, agent.generation),
0
);
$("population").textContent = active.length.toLocaleString();
$("survivorCount").textContent =
simulationYear >= 12.35 ? "12" : "—";
$("descendantCount").textContent = descendants.toLocaleString();
$("generationCount").textContent = maximumGeneration + 1;
let phase;
if (simulationYear < 7) phase = "Founding census";
else if (simulationYear < 12.35) phase = "Valley growth";
else if (simulationYear < 13.5) phase = "Bottleneck";
else if (simulationYear < 25) phase = "Recovery";
else phase = "Re-founded valley";
$("phaseName").textContent = phase;
for (let lineage = 0; lineage < LINEAGE_COUNT; lineage++) {
const lineagePopulation = active.filter(a => a.lineage === lineage).length;
const descendantCount = simulationYear >= 13
? Math.max(0, lineagePopulation - 1)
: 0;
const countElement = lineageRows[lineage].querySelector(".lineage-count");
const detailElement = lineageRows[lineage].querySelector(".lineage-detail");
countElement.textContent =
simulationYear >= 13 ? descendantCount.toLocaleString() : "—";
detailElement.textContent =
simulationYear >= 13
? `S-${String(lineage + 1).padStart(2, "0")} · ${descendantCount} descendants`
: `Bloodline ${String(lineage + 1).padStart(2, "0")}`;
}
settlement.count = simulationYear >= 13
? Math.min(settlementHuts.length, Math.floor(descendants / 8))
: 0;
}
const clock = new THREE.Clock();
function animate() {
requestAnimationFrame(animate);
const delta = Math.min(clock.getDelta(), 0.05);
if (playing) {
simulationYear += delta * playbackSpeed * 1.1;
if (simulationYear >= END_YEAR) {
simulationYear = END_YEAR;
playing = false;
playButton.textContent = "Replay";
}
}
timeline.value = simulationYear;
$("yearReadout").textContent = `Year ${simulationYear.toFixed(1)}`;
const active = updateAgents(simulationYear);
updateStats(active);
updateEvent(active);
updateChart(active);
updateCamera();
for (const cloud of clouds) {
cloud.position.x += delta * 0.28;
if (cloud.position.x > 48) cloud.position.x = -48;
}
beaconTop.rotation.y += delta * 0.8;
beaconTop.position.y = 3.25 + Math.sin(performance.now() * 0.002) * 0.08;
projectLabel($("passLabel"), passLabelPosition);
if (simulationYear > 12.65) {
projectLabel($("survivorLabel"), survivorLabelPosition);
} else {
$("survivorLabel").style.opacity = "0";
}
renderer.render(scene, camera);
}
window.addEventListener("resize", () => {
camera.aspect = innerWidth / innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(innerWidth, innerHeight);
renderer.setPixelRatio(Math.min(devicePixelRatio, 1.75));
});
updateCamera();
updateLineageSelection();
animate();
</script>
</body>
</html>
```