Prompt specification
Build or customize the Three.js 3D scene "Trace a schematic water parcel across connected habitats" 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: Trace a schematic water parcel across connected habitats
Category: Space & Earth · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/trace-a-schematic-water-parcel-across-connected-habita-x3076
Scene Description Prompt:
Please create one self-contained HTML file with Three.js from cdnjs showing the Great Barrier Reef's connected coastal habitats. Let users follow an illustrative current path through reef and lagoon zones. Show connectivity while keeping flow speed, ecological effects, and water-quality assumptions explicitly simplified. Low-poly.
Working Three.js Starter (trace-a-schematic-water-parcel-across-connected-habita-x3076):
```html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>ReefLinks — Great Barrier Reef Habitat Connectivity</title>
<style>
:root {
color-scheme: dark;
--bg: #061f29;
--panel: rgba(5, 30, 39, 0.88);
--panel-solid: #092832;
--line: rgba(171, 225, 220, 0.18);
--text: #edf9f6;
--muted: #9bbab8;
--aqua: #60e0d2;
--aqua-dark: #167d7b;
--coral: #ff806c;
--sand: #e8c98e;
--green: #8bd681;
--shadow: 0 16px 45px rgba(0, 14, 20, 0.32);
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: var(--bg);
color: var(--text);
font-family:
Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
button,
input {
font: inherit;
}
button {
color: inherit;
}
button:focus-visible,
input:focus-visible {
outline: 2px solid var(--aqua);
outline-offset: 3px;
}
#scene {
position: fixed;
inset: 0;
}
#scene canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
cursor: grab;
}
#scene canvas:active {
cursor: grabbing;
}
.topbar {
position: fixed;
z-index: 20;
top: 0;
left: 0;
right: 0;
height: 68px;
display: flex;
align-items: center;
justify-content: space-between;
gap: 20px;
padding: 0 22px;
background: linear-gradient(
180deg,
rgba(3, 22, 29, 0.95),
rgba(3, 22, 29, 0.66) 76%,
transparent
);
pointer-events: none;
}
.brand {
display: flex;
align-items: center;
gap: 12px;
min-width: 0;
}
.brand-mark {
width: 37px;
height: 37px;
flex: 0 0 auto;
display: grid;
place-items: center;
border: 1px solid rgba(96, 224, 210, 0.38);
border-radius: 11px;
background: rgba(96, 224, 210, 0.1);
color: var(--aqua);
}
.brand-mark svg {
width: 23px;
height: 23px;
}
.brand h1 {
margin: 0;
font-size: 17px;
line-height: 1.2;
letter-spacing: 0.02em;
}
.brand p {
margin: 3px 0 0;
color: var(--muted);
font-size: 11px;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.top-actions {
display: flex;
align-items: center;
gap: 9px;
pointer-events: auto;
}
.model-badge {
padding: 7px 10px;
border: 1px solid rgba(232, 201, 142, 0.3);
border-radius: 999px;
background: rgba(232, 201, 142, 0.09);
color: #f1dcae;
font-size: 10px;
font-weight: 750;
letter-spacing: 0.08em;
text-transform: uppercase;
white-space: nowrap;
}
.icon-button {
border: 1px solid var(--line);
border-radius: 10px;
background: rgba(9, 40, 50, 0.82);
padding: 8px 11px;
cursor: pointer;
}
.icon-button:hover {
border-color: rgba(96, 224, 210, 0.48);
background: rgba(17, 60, 68, 0.9);
}
.panel {
position: fixed;
z-index: 15;
border: 1px solid var(--line);
border-radius: 18px;
background: var(--panel);
box-shadow: var(--shadow);
backdrop-filter: blur(15px);
-webkit-backdrop-filter: blur(15px);
}
.control-panel {
top: 84px;
left: 18px;
width: 338px;
max-height: calc(100vh - 176px);
overflow: auto;
padding: 18px;
scrollbar-width: thin;
scrollbar-color: #2c6670 transparent;
}
.eyebrow {
margin: 0 0 7px;
color: var(--aqua);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.14em;
text-transform: uppercase;
}
.control-panel h2 {
margin: 0;
font-size: 25px;
line-height: 1.08;
letter-spacing: -0.035em;
}
.intro {
margin: 10px 0 16px;
color: #bfd2cf;
font-size: 12px;
line-height: 1.55;
}
.section {
padding-top: 15px;
margin-top: 15px;
border-top: 1px solid var(--line);
}
.section-title {
display: flex;
justify-content: space-between;
align-items: center;
gap: 10px;
margin-bottom: 12px;
color: #dcebea;
font-size: 11px;
font-weight: 800;
letter-spacing: 0.09em;
text-transform: uppercase;
}
.section-title span {
color: var(--muted);
font-weight: 600;
letter-spacing: 0;
text-transform: none;
}
.slider-row + .slider-row {
margin-top: 14px;
}
.slider-label {
display: flex;
justify-content: space-between;
align-items: baseline;
gap: 12px;
margin-bottom: 8px;
font-size: 12px;
}
.slider-label output {
color: var(--aqua);
font-variant-numeric: tabular-nums;
font-weight: 750;
}
input[type="range"] {
width: 100%;
height: 4px;
margin: 0;
border-radius: 999px;
appearance: none;
-webkit-appearance: none;
background:
linear-gradient(
90deg,
var(--aqua) 0%,
var(--aqua) var(--fill, 50%),
rgba(169, 213, 211, 0.18) var(--fill, 50%),
rgba(169, 213, 211, 0.18) 100%
);
cursor: pointer;
}
input[type="range"]::-webkit-slider-thumb {
width: 16px;
height: 16px;
border: 3px solid #0b3138;
border-radius: 50%;
appearance: none;
-webkit-appearance: none;
background: var(--aqua);
box-shadow: 0 0 0 1px rgba(96, 224, 210, 0.48);
}
input[type="range"]::-moz-range-thumb {
width: 12px;
height: 12px;
border: 3px solid #0b3138;
border-radius: 50%;
background: var(--aqua);
}
.presets {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 6px;
margin-top: 12px;
}
.preset {
min-height: 44px;
padding: 7px 6px;
border: 1px solid var(--line);
border-radius: 10px;
background: rgba(255, 255, 255, 0.025);
color: #b9cecb;
font-size: 10px;
line-height: 1.25;
cursor: pointer;
}
.preset:hover,
.preset.active {
border-color: rgba(96, 224, 210, 0.48);
background: rgba(96, 224, 210, 0.1);
color: var(--text);
}
.preset small {
display: block;
margin-top: 3px;
color: var(--muted);
font-size: 9px;
}
.metric-grid {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 8px;
}
.metric {
min-height: 75px;
padding: 11px;
border: 1px solid rgba(168, 221, 218, 0.12);
border-radius: 12px;
background: rgba(255, 255, 255, 0.035);
}
.metric-label {
color: var(--muted);
font-size: 9px;
font-weight: 750;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.metric-value {
display: block;
margin-top: 6px;
font-size: 20px;
font-weight: 760;
letter-spacing: -0.03em;
font-variant-numeric: tabular-nums;
}
.metric-value small {
color: var(--muted);
font-size: 10px;
font-weight: 600;
letter-spacing: 0;
}
.metric-sub {
display: block;
margin-top: 3px;
color: var(--muted);
font-size: 9px;
}
.button-row {
display: flex;
gap: 8px;
}
.button-row button {
flex: 1;
padding: 10px 11px;
border: 1px solid var(--line);
border-radius: 10px;
background: rgba(255, 255, 255, 0.035);
font-size: 11px;
font-weight: 750;
cursor: pointer;
}
.button-row button:hover {
border-color: rgba(96, 224, 210, 0.5);
}
.button-row .primary {
border-color: rgba(96, 224, 210, 0.55);
background: var(--aqua);
color: #06262b;
}
.toggle-row {
display: flex;
flex-wrap: wrap;
gap: 10px 16px;
margin-top: 13px;
}
.toggle {
display: inline-flex;
align-items: center;
gap: 7px;
color: #bfd2cf;
font-size: 11px;
cursor: pointer;
}
.toggle input {
width: 15px;
height: 15px;
accent-color: var(--aqua);
}
.selected-card {
padding: 12px;
border-left: 3px solid var(--aqua);
border-radius: 4px 10px 10px 4px;
background: rgba(96, 224, 210, 0.075);
}
.selected-title {
display: flex;
justify-content: space-between;
gap: 10px;
font-size: 12px;
font-weight: 780;
}
.selected-title span:last-child {
color: var(--aqua);
white-space: nowrap;
}
.selected-card p {
margin: 5px 0 0;
color: var(--muted);
font-size: 10px;
line-height: 1.45;
}
.notes-panel {
top: 84px;
right: 18px;
width: 318px;
max-height: calc(100vh - 176px);
overflow: auto;
padding: 17px;
scrollbar-width: thin;
scrollbar-color: #2c6670 transparent;
}
body.notes-closed .notes-panel {
display: none;
}
.notes-panel h3 {
margin: 0;
font-size: 15px;
}
.notes-panel > p {
margin: 8px 0 15px;
color: var(--muted);
font-size: 11px;
line-height: 1.5;
}
.assumption {
padding: 10px 0;
border-top: 1px solid var(--line);
}
.assumption b {
display: block;
margin-bottom: 4px;
color: #e5f2ef;
font-size: 11px;
}
.assumption p {
margin: 0;
color: #a9c1be;
font-size: 10px;
line-height: 1.48;
}
.formula {
margin-top: 12px;
padding: 11px;
border: 1px solid rgba(232, 201, 142, 0.18);
border-radius: 10px;
background: rgba(232, 201, 142, 0.055);
color: #ddcfad;
font: 10px/1.55 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
}
.legend {
position: fixed;
z-index: 12;
left: 20px;
bottom: 74px;
display: flex;
flex-wrap: wrap;
gap: 10px 15px;
max-width: 360px;
padding: 10px 12px;
border: 1px solid var(--line);
border-radius: 12px;
background: rgba(5, 30, 39, 0.73);
color: #c1d3d0;
font-size: 10px;
backdrop-filter: blur(10px);
pointer-events: none;
}
.legend-item {
display: inline-flex;
align-items: center;
gap: 6px;
}
.swatch {
width: 8px;
height: 8px;
border-radius: 50%;
box-shadow: 0 0 8px currentColor;
}
.zone-strip {
position: fixed;
z-index: 16;
left: 50%;
bottom: 17px;
display: flex;
gap: 5px;
max-width: calc(100vw - 36px);
padding: 5px;
transform: translateX(-50%);
overflow-x: auto;
border: 1px solid var(--line);
border-radius: 14px;
background: rgba(5, 30, 39, 0.87);
box-shadow: var(--shadow);
backdrop-filter: blur(13px);
scrollbar-width: none;
}
.zone-strip::-webkit-scrollbar {
display: none;
}
.zone-button {
flex: 0 0 auto;
padding: 8px 11px;
border: 1px solid transparent;
border-radius: 9px;
background: transparent;
color: #b9cecb;
font-size: 10px;
cursor: pointer;
white-space: nowrap;
}
.zone-button:hover,
.zone-button.journey-active {
border-color: rgba(96, 224, 210, 0.28);
background: rgba(96, 224, 210, 0.1);
color: var(--text);
}
.zone-button.selected {
color: var(--aqua);
}
#labels {
position: fixed;
z-index: 10;
inset: 0;
overflow: hidden;
pointer-events: none;
}
.map-label {
position: absolute;
min-width: 90px;
padding: 6px 8px;
transform: translate(-50%, -100%);
border: 1px solid rgba(181, 228, 224, 0.2);
border-radius: 9px;
background: rgba(3, 24, 31, 0.76);
box-shadow: 0 6px 18px rgba(0, 0, 0, 0.18);
color: #edf9f6;
font-size: 10px;
font-weight: 750;
line-height: 1.2;
text-align: center;
backdrop-filter: blur(8px);
will-change: left, top;
}
.map-label::after {
position: absolute;
left: 50%;
bottom: -5px;
width: 8px;
height: 8px;
content: "";
transform: translateX(-50%) rotate(45deg);
border-right: 1px solid rgba(181, 228, 224, 0.2);
border-bottom: 1px solid rgba(181, 228, 224, 0.2);
background: rgba(3, 24, 31, 0.76);
}
.map-label small {
display: block;
margin-top: 3px;
color: #9ebbb8;
font-size: 8px;
font-weight: 600;
}
.compass {
position: fixed;
z-index: 10;
top: 84px;
left: 50%;
transform: translateX(-50%);
display: grid;
place-items: center;
width: 46px;
height: 46px;
border: 1px solid rgba(177, 226, 221, 0.18);
border-radius: 50%;
background: rgba(5, 30, 39, 0.48);
color: #d5e9e5;
font-size: 10px;
font-weight: 800;
pointer-events: none;
}
.compass::after {
position: absolute;
top: 5px;
content: "▲";
color: var(--coral);
font-size: 9px;
}
.sr-only {
position: absolute;
width: 1px;
height: 1px;
padding: 0;
margin: -1px;
overflow: hidden;
clip: rect(0, 0, 0, 0);
white-space: nowrap;
border: 0;
}
@media (max-width: 1000px) {
.control-panel {
width: 306px;
}
.notes-panel {
width: 285px;
}
.legend {
display: none;
}
}
@media (max-width: 760px) {
.topbar {
height: 60px;
padding: 0 12px;
}
.brand p,
.model-badge {
display: none;
}
.brand h1 {
font-size: 14px;
}
.control-panel {
top: 70px;
left: 10px;
width: min(330px, calc(100vw - 20px));
max-height: calc(100vh - 150px);
padding: 15px;
}
.control-panel h2 {
font-size: 21px;
}
.notes-panel {
display: none;
top: 70px;
right: 10px;
width: min(340px, calc(100vw - 20px));
max-height: calc(100vh - 90px);
}
body.notes-open .notes-panel {
display: block;
}
.compass {
display: none;
}
.zone-strip {
bottom: 9px;
max-width: calc(100vw - 20px);
}
.map-label {
min-width: 72px;
padding: 5px 6px;
font-size: 9px;
}
.map-label small {
display: none;
}
}
</style>
</head>
<body>
<main id="scene" aria-label="Interactive low-poly map of connected Great Barrier Reef coastal habitats"></main>
<header class="topbar">
<div class="brand">
<div class="brand-mark" aria-hidden="true">
<svg viewBox="0 0 24 24" fill="none">
<path d="M3 15c3-4 6-4 9 0s6 4 9 0" stroke="currentColor" stroke-width="1.7" stroke-linecap="round"/>
<path d="M3 19c3-4 6-4 9 0s6 4 9 0" stroke="currentColor" stroke-width="1.2" stroke-linecap="round" opacity=".55"/>
<path d="M12 3v7M9.5 5.5 12 3l2.5 2.5" stroke="currentColor" stroke-width="1.7" stroke-linecap="round" stroke-linejoin="round"/>
</svg>
</div>
<div>
<h1>ReefLinks / Great Barrier Reef</h1>
<p>Connected coastal habitats · schematic explorer</p>
</div>
</div>
<div class="top-actions">
<span class="model-badge">Simplified model · not a forecast</span>
<button id="notesToggle" class="icon-button" type="button" aria-expanded="true">
Model notes
</button>
</div>
</header>
<section class="panel control-panel" aria-label="Current journey and scenario controls">
<p class="eyebrow">Illustrative current journey</p>
<h2>Follow the water between habitats</h2>
<p class="intro">
Travel through a mangrove edge, lagoon, coral gardens, reef passages,
outer reef, and seagrass meadow. Explore the network, then change the
simplified flow and water-quality assumptions.
</p>
<div class="section">
<div class="section-title">
Current assumption
<span>Uniform near-surface flow</span>
</div>
<div class="slider-row">
<label class="slider-label" for="flowRange">
<span>Mean flow speed</span>
<output id="flowValue">0.35 m/s</output>
</label>
<input id="flowRange" type="range" min="0.15" max="0.65" step="0.05" value="0.35">
</div>
<div class="slider-row">
<label class="slider-label" for="wqRange">
<span>Water-quality index</span>
<output id="wqValue">82 / 100</output>
</label>
<input id="wqRange" type="range" min="0" max="100" step="1" value="82">
</div>
<div class="presets" aria-label="Water-quality scenarios">
<button class="preset active" type="button" data-wq="82">
Healthy baseline
<small>WQI 82</small>
</button>
<button class="preset" type="button" data-wq="42">
Nutrient pulse
<small>WQI 42</small>
</button>
<button class="preset" type="button" data-wq="64">
Recovery
<small>WQI 64</small>
</button>
</div>
</div>
<div class="section">
<div class="section-title">
Illustrative outcomes
<span>Heuristic, not measured</span>
</div>
<div class="metric-grid" aria-live="polite">
<div class="metric">
<span class="metric-label">Estimated loop transit</span>
<strong class="metric-value" id="transitValue">43.7 <small>hours</small></strong>
<span class="metric-sub">55 km conceptual route</span>
</div>
<div class="metric">
<span class="metric-label">Mean habitat condition</span>
<strong class="metric-value" id="conditionValue">77 <small>%</small></strong>
<span class="metric-sub" id="conditionLabel">Good</span>
</div>
<div class="metric">
<span class="metric-label">Habitat links</span>
<strong class="metric-value">9 <small>links</small></strong>
<span class="metric-sub">Across 6 habitat nodes</span>
</div>
<div class="metric">
<span class="metric-label">Current position</span>
<strong class="metric-value" id="journeyPercent">4 <small>%</small></strong>
<span class="metric-sub" id="journeyZone">Mangrove edge</span>
</div>
</div>
</div>
<div class="section">
<div class="section-title">
Navigation
<span>Drag · Shift-drag · Scroll</span>
</div>
<div class="button-row">
<button id="playButton" class="primary" type="button">Pause journey</button>
<button id="restartButton" type="button">Restart</button>
<button id="resetCameraButton" type="button">Reset view</button>
</div>
<div class="toggle-row">
<label class="toggle">
<input id="followToggle" type="checkbox">
Follow current
</label>
<label class="toggle">
<input id="linksToggle" type="checkbox" checked>
Show links
</label>
</div>
</div>
<div class="section">
<div class="selected-card" aria-live="polite">
<div class="selected-title">
<span id="selectedTitle">Mangrove edge</span>
<span id="selectedCondition">Condition 77%</span>
</div>
<p id="selectedDescription">
A schematic transition from a coastal freshwater plume into protected
shallow habitat.
</p>
</div>
</div>
</section>
<aside class="panel notes-panel" aria-label="Model assumptions">
<p class="eyebrow">Read before interpreting</p>
<h3>What this explorer simplifies</h3>
<p>
This is a conceptual teaching model. It is not hydrodynamic, ecological,
or management advice and should not be used to infer real conditions.
</p>
<div class="assumption">
<b>1. Geography is compressed</b>
<p>
The coast, reef arc, habitats, and links are schematic. They are not
traced from surveyed coordinates or real bathymetry. The displayed
55 km route is an illustrative planning distance, not a measured
Great Barrier Reef current path.
</p>
</div>
<div class="assumption">
<b>2. Flow is one uniform speed</b>
<p>
The slider applies one mean near-surface speed along every segment.
There is no tide, wave, wind, eddy, depth, or seasonal variation.
Particle motion is visually scaled and is not a time-accurate simulation.
</p>
</div>
<div class="assumption">
<b>3. Water quality is user-set</b>
<p>
The 0–100 water-quality index is an illustrative input, not observed
data. Nutrient concentrations, sediment, temperature, salinity,
pathogens, and reef-specific thresholds are not modeled.
</p>
</div>
<div class="assumption">
<b>4. Ecological effects are heuristic</b>
<p>
Condition combines water quality with fixed illustrative connectivity
weights. It does not represent a calibrated coral-growth, mangrove,
seagrass, mortality, or recovery model.
</p>
</div>
<div class="assumption">
<b>5. Links show adjacency, not dispersal</b>
<p>
Network lines connect habitats for visual exploration. They do not
prove larval transport, genetic exchange, animal movement, or the
effectiveness of a proposed restoration action.
</p>
</div>
<div class="formula">
condition ≈ 0.12 + 0.68 × (WQI / 100)<br>
+ 0.18 × fixed connectivity<br>
− 0.35 × water stress × sensitivity
</div>
<div class="assumption">
<b>Camera controls</b>
<p>
Drag to orbit, shift-drag or right-drag to pan, scroll to zoom, and
select a habitat marker to focus. Touch: drag to orbit and pinch to zoom.
</p>
</div>
</aside>
<div id="labels" aria-hidden="true"></div>
<div class="compass" aria-hidden="true">N</div>
<div class="legend" aria-hidden="true">
<span class="legend-item"><i class="swatch" style="color:#60e0d2;background:#60e0d2"></i>Illustrative current</span>
<span class="legend-item"><i class="swatch" style="color:#76aee8;background:#76aee8"></i>Habitat links</span>
<span class="legend-item"><i class="swatch" style="color:#ff806c;background:#ff806c"></i>Coral habitat</span>
<span class="legend-item"><i class="swatch" style="color:#8bd681;background:#8bd681"></i>Seagrass / mangrove</span>
</div>
<nav id="zoneStrip" class="zone-strip" aria-label="Habitat selection"></nav>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// ------------------------------------------------------------
// Application state
// ------------------------------------------------------------
const state = {
flow: 0.35,
wqi: 82,
playing: true,
follow: false,
linksVisible: true,
progress: 0.045,
selectedZone: null,
journeyZone: null
};
const conceptualRouteKm = 55;
const sceneHost = document.getElementById("scene");
const renderer = new THREE.WebGLRenderer({
antialias: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.15;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.domElement.setAttribute(
"aria-label",
"Low-poly interactive model of Great Barrier Reef coastal habitats"
);
sceneHost.appendChild(renderer.domElement);
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x0b5360);
scene.fog = new THREE.Fog(0x0b5360, 24, 48);
const camera = new THREE.PerspectiveCamera(
43,
window.innerWidth / window.innerHeight,
0.1,
100
);
const cameraTarget = new THREE.Vector3(0, 0, 0);
const desiredTarget = new THREE.Vector3(0, 0, 0);
let desiredRadius = 27;
let focusing = false;
const spherical = new THREE.Spherical(27, 1.02, 0.62);
camera.position.setFromSpherical(spherical).add(cameraTarget);
camera.lookAt(cameraTarget);
const hemisphere = new THREE.HemisphereLight(0xd9ffff, 0x173c38, 2.1);
scene.add(hemisphere);
const sun = new THREE.DirectionalLight(0xfff3d1, 3.1);
sun.position.set(-9, 18, 11);
sun.castShadow = true;
sun.shadow.mapSize.set(1024, 1024);
sun.shadow.camera.left = -20;
sun.shadow.camera.right = 20;
sun.shadow.camera.top = 18;
sun.shadow.camera.bottom = -18;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 50;
sun.shadow.bias = -0.0008;
scene.add(sun);
const fill = new THREE.DirectionalLight(0x58d8df, 0.7);
fill.position.set(12, 7, -12);
scene.add(fill);
const world = new THREE.Group();
scene.add(world);
const networkGroup = new THREE.Group();
world.add(networkGroup);
// ------------------------------------------------------------
// Deterministic low-poly world construction
// ------------------------------------------------------------
let seed = 918273;
function random() {
seed = (seed * 1664525 + 1013904223) >>> 0;
return seed / 4294967296;
}
function randomRange(min, max) {
return min + random() * (max - min);
}
function polygonMesh(points, color, opacity, y, renderOrder = 0) {
const shape = new THREE.Shape();
points.forEach(([x, z], index) => {
if (index === 0) shape.moveTo(x, -z);
else shape.lineTo(x, -z);
});
shape.closePath();
const geometry = new THREE.ShapeGeometry(shape);
geometry.rotateX(-Math.PI / 2);
const material = new THREE.MeshBasicMaterial({
color,
transparent: opacity < 1,
opacity,
depthWrite: opacity >= 1,
side: THREE.DoubleSide
});
const mesh = new THREE.Mesh(geometry, material);
mesh.position.y = y;
mesh.renderOrder = renderOrder;
return mesh;
}
const waterGeometry = new THREE.PlaneGeometry(38, 30, 32, 24);
waterGeometry.rotateX(-Math.PI / 2);
const waterMaterial = new THREE.MeshStandardMaterial({
color: 0x087f8d,
roughness: 0.88,
metalness: 0.02,
flatShading: true
});
const water = new THREE.Mesh(waterGeometry, waterMaterial);
water.receiveShadow = true;
world.add(water);
const waterPositions = waterGeometry.attributes.position;
const waterBase = new Float32Array(waterPositions.count * 2);
for (let i = 0; i < waterPositions.count; i++) {
waterBase[i * 2] = waterPositions.getX(i);
waterBase[i * 2 + 1] = waterPositions.getZ(i);
}
// Schematic Australian coastal edge.
const coastPoints = [
[-17, -11],
[-12.8, -11.2],
[-10.2, -8.4],
[-9.3, -4.8],
[-10.1, -1.8],
[-8.7, 1.1],
[-9.4, 4.3],
[-8.4, 7.1],
[-11.1, 10.8],
[-16.8, 11.1]
];
const coastShape = new THREE.Shape();
coastPoints.forEach(([x, z], index) => {
if (index === 0) coastShape.moveTo(x, -z);
else coastShape.lineTo(x, -z);
});
coastShape.closePath();
const coastGeometry = new THREE.ExtrudeGeometry(coastShape, {
depth: 0.68,
bevelEnabled: false,
steps: 1
});
coastGeometry.rotateX(-Math.PI / 2);
const coastMaterial = new THREE.MeshStandardMaterial({
color: 0x617a58,
roughness: 1,
flatShading: true
});
const coast = new THREE.Mesh(coastGeometry, coastMaterial);
coast.position.y = -0.05;
coast.castShadow = true;
coast.receiveShadow = true;
world.add(coast);
const coastOutlineGeometry = new THREE.BufferGeometry().setFromPoints(
coastPoints.map(([x, z]) => new THREE.Vector3(x, 0.66, z))
);
const coastOutline = new THREE.LineLoop(
coastOutlineGeometry,
new THREE.LineBasicMaterial({ color: 0xd9c58d, transparent: true, opacity: 0.75 })
);
world.add(coastOutline);
// A schematic river / freshwater plume entering the coastal habitat.
world.add(
polygonMesh(
[
[-13, 5.6],
[-8.3, 4.7],
[-6.8, 3.8],
[-10.6, 4.1],
[-13.6, 4.7]
],
0x64b8b2,
0.78,
0.67,
2
)
);
// Low-poly lagoon, seagrass and reef shelf areas.
world.add(
polygonMesh(
[
[-6.3, -1.9],
[-2.8, -3.8],
[2.4, -4.5],
[7.7, -3.7],
[10.2, 0.1],
[8.1, 4.9],
[2.8, 5.4],
[-2.7, 3.8],
[-5.7, 1.1]
],
0x48c0b2,
0.13,
-0.11,
1
)
);
world.add(
polygonMesh(
[
[7.2, 0.8],
[10.6, 0.6],
[11.5, 3.5],
[8.6, 4.7],
[6.5, 3.1]
],
0x55a968,
0.34,
-0.08,
2
)
);
// Faceted bathymetric-style contours.
for (let ring = 0; ring < 3; ring++) {
const points = [];
const radiusX = 5.3 + ring * 2.0;
const radiusZ = 4.1 + ring * 1.55;
for (let i = 0; i < 32; i++) {
const angle = (i / 32) * Math.PI * 2;
const wobble = 1 + Math.sin(angle * 3 + ring) * 0.05;
points.push(
new THREE.Vector3(
3.7 + Math.cos(angle) * radiusX * wobble,
-0.075,
0.1 + Math.sin(angle) * radiusZ * wobble
)
);
}
const line = new THREE.LineLoop(
new THREE.BufferGeometry().setFromPoints(points),
new THREE.LineBasicMaterial({
color: 0x8ce0d4,
transparent: true,
opacity: 0.14 - ring * 0.025
})
);
world.add(line);
}
// Reef platforms.
const reefPlatforms = [
[-0.9, -0.3, 0.8, 0.28],
[0.4, -1.4, 0.65, 0.22],
[2.4, -2.3, 0.85, 0.32],
[3.7, -3.0, 0.58, 0.22],
[6.2, -2.0, 0.95, 0.38],
[7.2, -0.9, 0.62, 0.26],
[6.8, -3.4, 0.48, 0.2]
];
const platformMaterial = new THREE.MeshStandardMaterial({
color: 0xc4a76f,
roughness: 1,
flatShading: true
});
const rockMaterial = new THREE.MeshStandardMaterial({
color: 0x687d6a,
roughness: 1,
flatShading: true
});
reefPlatforms.forEach(([x, z, radius, height], index) => {
const platform = new THREE.Mesh(
new THREE.CylinderGeometry(radius * 0.7, radius, height, 7),
platformMaterial
);
platform.position.set(x, height / 2 - 0.02, z);
platform.rotation.y = random() * Math.PI;
platform.castShadow = true;
platform.receiveShadow = true;
world.add(platform);
if (index % 2 === 0) {
const rock = new THREE.Mesh(
new THREE.DodecahedronGeometry(radius * 0.48, 0),
rockMaterial
);
rock.position.set(x + randomRange(-0.2, 0.2), height + 0.1, z);
rock.scale.set(1.2, 0.8, 0.9);
rock.rotation.set(random(), random(), random());
rock.castShadow = true;
world.add(rock);
}
});
// Instanced low-poly coral colonies.
const coralAnchors = [
[-0.8, -0.3],
[0.4, -1.4],
[2.4, -2.3],
[3.7, -3.0],
[6.2, -2.0],
[7.2, -0.9],
[6.8, -3.4]
];
const coralCount = 126;
const coralGeometry = new THREE.IcosahedronGeometry(0.27, 0);
const coralMaterial = new THREE.MeshStandardMaterial({
color: 0xffffff,
roughness: 0.88,
flatShading: true
});
const coralMesh = new THREE.InstancedMesh(
coralGeometry,
coralMaterial,
coralCount
);
coralMesh.castShadow = true;
coralMesh.receiveShadow = true;
const dummy = new THREE.Object3D();
const coralBases = [];
const coralPalette = [
new THREE.Color(0xff806c),
new THREE.Color(0xffb15f),
new THREE.Color(0xd77ac1),
new THREE.Color(0x65d4c1),
new THREE.Color(0xe9dd8b)
];
for (let i = 0; i < coralCount; i++) {
const anchor = coralAnchors[i % coralAnchors.length];
const x = anchor[0] + randomRange(-1.0, 1.0);
const z = anchor[1] + randomRange(-0.9, 0.9);
const scale = randomRange(0.48, 1.25);
dummy.position.set(x, randomRange(0.17, 0.38), z);
dummy.rotation.set(random() * 0.5, random() * Math.PI, random() * 0.35);
dummy.scale.set(
scale * randomRange(0.8, 1.3),
scale * randomRange(0.65, 1.3),
scale * randomRange(0.8, 1.3)
);
dummy.updateMatrix();
coralMesh.setMatrixAt(i, dummy.matrix);
const base = coralPalette[Math.floor(random() * coralPalette.length)].clone();
coralBases.push(base);
coralMesh.setColorAt(i, base);
}
coralMesh.instanceMatrix.needsUpdate = true;
coralMesh.instanceColor.needsUpdate = true;
world.add(coralMesh);
// Seagrass meadow.
const seagrassMaterial = new THREE.MeshStandardMaterial({
color: 0x55ad72,
roughness: 1,
flatShading: true
});
const seagrassCount = 70;
const seagrass = new THREE.InstancedMesh(
new THREE.ConeGeometry(0.075, 0.72, 4),
seagrassMaterial,
seagrassCount
);
for (let i = 0; i < seagrassCount; i++) {
const angle = random() * Math.PI * 2;
const radius = Math.sqrt(random());
dummy.position.set(
8.6 + Math.cos(angle) * radius * 1.65,
randomRange(0.04, 0.17),
2.45 + Math.sin(angle) * radius * 1.45
);
dummy.rotation.set(
randomRange(-0.2, 0.2),
random() * Math.PI,
randomRange(-0.2, 0.2)
);
dummy.scale.set(
randomRange(0.65, 1.25),
randomRange(0.65, 1.3),
randomRange(0.65, 1.25)
);
dummy.updateMatrix();
seagrass.setMatrixAt(i, dummy.matrix);
}
seagrass.instanceMatrix.needsUpdate = true;
world.add(seagrass);
// Mangrove-style trees along the schematic river mouth.
const mangroveTrunkMaterial = new THREE.MeshStandardMaterial({
color: 0x654c3c,
roughness: 1,
flatShading: true
});
const mangroveCanopyMaterial = new THREE.MeshStandardMaterial({
color: 0x4c9668,
roughness: 1,
flatShading: true
});
for (let i = 0; i < 13; i++) {
const x = -8.4 + randomRange(-0.65, 1.6);
const z = 4.1 + randomRange(-1.15, 1.15);
const height = randomRange(0.65, 1.05);
const trunk = new THREE.Mesh(
new THREE.CylinderGeometry(0.045, 0.08, height, 5),
mangroveTrunkMaterial
);
trunk.position.set(x, 0.68 + height / 2, z);
trunk.castShadow = true;
world.add(trunk);
const canopy = new THREE.Mesh(
new THREE.IcosahedronGeometry(randomRange(0.32, 0.48), 0),
mangroveCanopyMaterial
);
canopy.position.set(x, 0.68 + height, z);
canopy.scale.y = 0.82;
canopy.castShadow = true;
world.add(canopy);
}
// ------------------------------------------------------------
// Habitat nodes and network
// ------------------------------------------------------------
const zones = [
{
id: "mangrove",
name: "Mangrove edge",
short: "Mangrove",
color: 0x8bd681,
position: new THREE.Vector3(-7.2, 0.85, 4.0),
sensitivity: 0.72,
connectivity: 0.78,
description:
"A schematic transition from a coastal freshwater plume into protected shallow habitat."
},
{
id: "lagoon",
name: "Lagoon",
short: "Lagoon",
color: 0x5ed5cf,
position: new THREE.Vector3(-3.6, 0.72, 2.45),
sensitivity: 0.55,
connectivity: 0.84,
description:
"A sheltered mixing zone connecting the coastal edge with the inner reef habitats."
},
{
id: "coral",
name: "Coral garden",
short: "Coral garden",
color: 0xff806c,
position: new THREE.Vector3(-0.7, 0.82, 0.2),
sensitivity: 1.0,
connectivity: 0.88,
description:
"A compact coral habitat used here to show sensitivity to simplified water stress."
},
{
id: "gap",
name: "Reef passage",
short: "Reef passage",
color: 0x76aee8,
position: new THREE.Vector3(2.8, 0.84, -2.5),
sensitivity: 0.65,
connectivity: 0.92,
description:
"A conceptual channel between reef sections, emphasizing exchange rather than a measured pathway."
},
{
id: "outer",
name: "Outer reef",
short: "Outer reef",
color: 0xffb15f,
position: new THREE.Vector3(6.6, 0.95, -1.8),
sensitivity: 1.0,
connectivity: 0.72,
description:
"An exposed reef habitat in the schematic network, with lower assumed local connectivity."
},
{
id: "seagrass",
name: "Seagrass meadow",
short: "Seagrass",
color: 0x8bd681,
position: new THREE.Vector3(8.6, 0.82, 2.35),
sensitivity: 0.84,
connectivity: 0.84,
description:
"A shallow coastal meadow shown as a habitat node connected to the outer reef and lagoon."
}
];
const zoneById = new Map(zones.map((zone) => [zone.id, zone]));
const clickableMarkers = [];
const labelElements = new Map();
zones.forEach((zone) => {
const marker = new THREE.Mesh(
new THREE.IcosahedronGeometry(0.25, 0),
new THREE.MeshStandardMaterial({
color: zone.color,
emissive: zone.color,
emissiveIntensity: 0.2,
roughness: 0.55,
flatShading: true
})
);
marker.position.copy(zone.position);
marker.castShadow = true;
marker.userData.zoneId = zone.id;
world.add(marker);
clickableMarkers.push(marker);
const ring = new THREE.Mesh(
new THREE.TorusGeometry(0.42, 0.035, 5, 18),
new THREE.MeshBasicMaterial({
color: zone.color,
transparent: true,
opacity: 0.75
})
);
ring.rotation.x = Math.PI / 2;
ring.position.copy(zone.position);
ring.position.y -= 0.08;
world.add(ring);
const label = document.createElement("div");
label.className = "map-label";
label.innerHTML = `${zone.name}<small>Condition —</small>`;
document.getElementById("labels").appendChild(label);
labelElements.set(zone.id, label);
});
const edgePairs = [
["mangrove", "lagoon"],
["mangrove", "seagrass"],
["lagoon", "coral"],
["lagoon", "seagrass"],
["coral", "gap"],
["coral", "outer"],
["gap", "outer"],
["gap", "seagrass"],
["outer", "seagrass"]
];
const networkCurves = [];
const networkParticles = [];
const networkLineMaterial = new THREE.LineBasicMaterial({
color: 0x76aee8,
transparent: true,
opacity: 0.38
});
const networkParticleGeometry = new THREE.IcosahedronGeometry(0.065, 0);
const networkParticleMaterial = new THREE.MeshBasicMaterial({
color: 0x9dd5ff
});
edgePairs.forEach(([aId, bId], edgeIndex) => {
const a = zoneById.get(aId).position;
const b = zoneById.get(bId).position;
const midpoint = a.clone().add(b).multiplyScalar(0.5);
const direction = b.clone().sub(a).setY(0).normalize();
const perpendicular = new THREE.Vector3(-direction.z, 0, direction.x);
midpoint.addScaledVector(perpendicular, (edgeIndex % 2 ? 1 : -1) * 0.65);
midpoint.y = 0.27;
const curve = new THREE.QuadraticBezierCurve3(
a.clone().setY(0.22),
midpoint,
b.clone().setY(0.22)
);
const line = new THREE.Line(
new THREE.BufferGeometry().setFromPoints(curve.getPoints(48)),
networkLineMaterial
);
networkGroup.add(line);
networkCurves.push(curve);
for (let i = 0; i < 2; i++) {
const particle = new THREE.Mesh(
networkParticleGeometry,
networkParticleMaterial
);
networkGroup.add(particle);
networkParticles.push({
mesh: particle,
curve,
offset: i * 0.5 + edgeIndex * 0.07
});
}
});
// ------------------------------------------------------------
// Illustrative current route
// ------------------------------------------------------------
const routePoints = [
new THREE.Vector3(-10.8, 0.82, 5.1),
new THREE.Vector3(-7.2, 0.67, 4.0),
new THREE.Vector3(-4.6, 0.53, 2.9),
new THREE.Vector3(-1.8, 0.58, 1.0),
new THREE.Vector3(0.2, 0.60, -0.9),
new THREE.Vector3(2.8, 0.63, -2.5),
new THREE.Vector3(4.7, 0.61, -2.8),
new THREE.Vector3(6.6, 0.70, -1.8),
new THREE.Vector3(8.6, 0.55, 0.5),
new THREE.Vector3(8.6, 0.50, 2.35),
new THREE.Vector3(6.1, 0.47, 3.55),
new THREE.Vector3(2.5, 0.45, 3.2),
new THREE.Vector3(-1.0, 0.49, 2.8),
new THREE.Vector3(-3.6, 0.51, 2.45),
new THREE.Vector3(-5.7, 0.56, 3.15)
];
const routeCurve = new THREE.CatmullRomCurve3(
routePoints,
true,
"catmullrom",
0.32
);
const routeMaterial = new THREE.MeshStandardMaterial({
color: 0x60e0d2,
emissive: 0x1eaaa3,
emissiveIntensity: 0.45,
roughness: 0.35,
transparent: true,
opacity: 0.9
});
const routeTube = new THREE.Mesh(
new THREE.TubeGeometry(routeCurve, 240, 0.055, 5, true),
routeMaterial
);
world.add(routeTube);
const currentParticleCount = 52;
const currentParticleGeometry = new THREE.IcosahedronGeometry(0.085, 0);
const currentParticleMaterial = new THREE.MeshBasicMaterial({
color: 0xb9fff4
});
const currentParticles = new THREE.InstancedMesh(
currentParticleGeometry,
currentParticleMaterial,
currentParticleCount
);
const particleOffsets = Array.from(
{ length: currentParticleCount },
(_, index) => (index / currentParticleCount + random() * 0.012) % 1
);
world.add(currentParticles);
const arrowGeometry = new THREE.ConeGeometry(0.13, 0.4, 5);
const arrowMaterial = new THREE.MeshBasicMaterial({
color: 0x8ffff0
});
const routeArrows = [];
for (let i = 0; i < 18; i++) {
const arrow = new THREE.Mesh(arrowGeometry, arrowMaterial);
const t = (i + 0.5) / 18;
const point = routeCurve.getPointAt(t);
const tangent = routeCurve.getTangentAt(t).normalize();
arrow.position.copy(point);
arrow.position.y += 0.035;
arrow.quaternion.setFromUnitVectors(
new THREE.Vector3(0, 1, 0),
tangent
);
world.add(arrow);
routeArrows.push(arrow);
}
// Main current marker.
const currentMarker = new THREE.Group();
const markerCore = new THREE.Mesh(
new THREE.IcosahedronGeometry(0.22, 1),
new THREE.MeshStandardMaterial({
color: 0xffffff,
emissive: 0x60e0d2,
emissiveIntensity: 1.25,
roughness: 0.25
})
);
currentMarker.add(markerCore);
const markerHalo = new THREE.Mesh(
new THREE.TorusGeometry(0.42, 0.045, 6, 24),
new THREE.MeshBasicMaterial({
color: 0x9ffff2,
transparent: true,
opacity: 0.85
})
);
markerHalo.rotation.x = Math.PI / 2;
currentMarker.add(markerHalo);
const markerStem = new THREE.Mesh(
new THREE.CylinderGeometry(0.025, 0.025, 0.8, 6),
new THREE.MeshBasicMaterial({
color: 0x9ffff2,
transparent: true,
opacity: 0.45
})
);
markerStem.position.y = -0.4;
currentMarker.add(markerStem);
world.add(currentMarker);
const markerArrow = new THREE.Mesh(
new THREE.ConeGeometry(0.12, 0.34, 5),
new THREE.MeshBasicMaterial({ color: 0xffffff })
);
world.add(markerArrow);
// Schematic river plume particles become more visible under poor water quality.
const plumeMaterial = new THREE.MeshBasicMaterial({
color: 0x9b8b58,
transparent: true,
opacity: 0.08,
depthWrite: false
});
const plumeParticles = [];
for (let i = 0; i < 10; i++) {
const plume = new THREE.Mesh(
new THREE.IcosahedronGeometry(randomRange(0.25, 0.55), 0),
plumeMaterial
);
plume.position.set(
randomRange(-9.7, -7.0),
randomRange(0.5, 0.9),
randomRange(3.4, 5.0)
);
plume.scale.set(1.5, 0.45, 1.1);
world.add(plume);
plumeParticles.push(plume);
}
// ------------------------------------------------------------
// UI
// ------------------------------------------------------------
const flowRange = document.getElementById("flowRange");
const wqRange = document.getElementById("wqRange");
const flowValue = document.getElementById("flowValue");
const wqValue = document.getElementById("wqValue");
const transitValue = document.getElementById("transitValue");
const conditionValue = document.getElementById("conditionValue");
const conditionLabel = document.getElementById("conditionLabel");
const journeyPercent = document.getElementById("journeyPercent");
const journeyZone = document.getElementById("journeyZone");
const selectedTitle = document.getElementById("selectedTitle");
const selectedCondition = document.getElementById("selectedCondition");
const selectedDescription = document.getElementById("selectedDescription");
const playButton = document.getElementById("playButton");
const restartButton = document.getElementById("restartButton");
const resetCameraButton = document.getElementById("resetCameraButton");
const followToggle = document.getElementById("followToggle");
const linksToggle = document.getElementById("linksToggle");
const notesToggle = document.getElementById("notesToggle");
const zoneStrip = document.getElementById("zoneStrip");
const zoneButtons = new Map();
zones.forEach((zone) => {
const button = document.createElement("button");
button.type = "button";
button.className = "zone-button";
button.textContent = zone.short;
button.addEventListener("click", () => selectZone(zone.id, true));
zoneStrip.appendChild(button);
zoneButtons.set(zone.id, button);
});
function setRangeFill(input) {
const percent =
((Number(input.value) - Number(input.min)) /
(Number(input.max) - Number(input.min))) *
100;
input.style.setProperty("--fill", `${percent}%`);
}
function updateSelectedPanel() {
const zone = state.selectedZone;
if (!zone) return;
selectedTitle.textContent = zone.name;
selectedCondition.textContent = `Condition ${Math.round(
zone.condition * 100
)}%`;
selectedDescription.textContent = zone.description;
zones.forEach((candidate) => {
zoneButtons.get(candidate.id).classList.toggle(
"selected",
candidate.id === zone.id
);
});
}
function selectZone(id, focus = false) {
state.selectedZone = zoneById.get(id);
updateSelectedPanel();
if (focus) {
state.follow = false;
followToggle.checked = false;
focusing = true;
desiredTarget.copy(state.selectedZone.position);
desiredRadius = 15;
}
}
function updateModel() {
state.flow = Number(flowRange.value);
state.wqi = Number(wqRange.value);
flowValue.textContent = `${state.flow.toFixed(2)} m/s`;
wqValue.textContent = `${state.wqi} / 100`;
setRangeFill(flowRange);
setRangeFill(wqRange);
const transitHours = conceptualRouteKm / (state.flow * 3.6);
transitValue.innerHTML = `${transitHours.toFixed(1)} <small>hours</small>`;
const waterNorm = state.wqi / 100;
const waterStress = Math.max(0, (70 - state.wqi) / 70);
zones.forEach((zone) => {
zone.condition = THREE.MathUtils.clamp(
0.12 +
0.68 * waterNorm +
0.18 * zone.connectivity -
0.35 * waterStress * zone.sensitivity,
0.05,
0.98
);
const label = labelElements.get(zone.id);
label.querySelector("small").textContent =
`Condition ${Math.round(zone.condition * 100)}%`;
});
const overall =
zones.reduce((sum, zone) => sum + zone.condition, 0) / zones.length;
conditionValue.innerHTML = `${Math.round(overall * 100)} <small>%</small>`;
conditionLabel.textContent =
overall >= 0.7 ? "Good" : overall >= 0.45 ? "Moderate" : "Stressed";
const cleanWater = new THREE.Color(0x087f8d);
const murkyWater = new THREE.Color(0x596e54);
const murk = Math.pow(1 - waterNorm, 1.35);
waterMaterial.color.copy(cleanWater).lerp(murkyWater, murk * 0.78);
const stressedCoral = new THREE.Color(0x756b50);
const colorTemp = new THREE.Color();
coralBases.forEach((base, index) => {
colorTemp.copy(base).lerp(stressedCoral, (1 - overall) * 0.78);
coralMesh.setColorAt(index, colorTemp);
});
coralMesh.instanceColor.needsUpdate = true;
seagrassMaterial.color
.set(0x55ad72)
.lerp(new THREE.Color(0xa28d4e), (1 - overall) * 0.65);
mangroveCanopyMaterial.color
.set(0x4c9668)
.lerp(new THREE.Color(0x85834e), (1 - overall) * 0.48);
plumeMaterial.opacity = 0.07 + murk * 0.24;
document.querySelectorAll(".preset").forEach((button) => {
button.classList.toggle("active", Number(button.dataset.wq) === state.wqi);
});
updateSelectedPanel();
}
flowRange.addEventListener("input", updateModel);
wqRange.addEventListener("input", updateModel);
document.querySelectorAll(".preset").forEach((button) => {
button.addEventListener("click", () => {
wqRange.value = button.dataset.wq;
updateModel();
});
});
playButton.addEventListener("click", () => {
state.playing = !state.playing;
playButton.textContent = state.playing ? "Pause journey" : "Play journey";
});
restartButton.addEventListener("click", () => {
state.progress = 0.045;
state.journeyZone = null;
});
resetCameraButton.addEventListener("click", () => {
state.follow = false;
followToggle.checked = false;
focusing = true;
desiredTarget.set(0, 0, 0);
desiredRadius = 27;
spherical.theta = 0.62;
spherical.phi = 1.02;
});
followToggle.addEventListener("change", () => {
state.follow = followToggle.checked;
focusing = false;
});
linksToggle.addEventListener("change", () => {
state.linksVisible = linksToggle.checked;
networkGroup.visible = state.linksVisible;
});
notesToggle.addEventListener("click", () => {
const closed = document.body.classList.toggle("notes-closed");
notesToggle.setAttribute("aria-expanded", String(!closed));
});
selectZone("mangrove");
updateModel();
// ------------------------------------------------------------
// Camera controls implemented directly, without addons
// ------------------------------------------------------------
const canvas = renderer.domElement;
const pointers = new Map();
const raycaster = new THREE.Raycaster();
const pointerNDC = new THREE.Vector2();
let lastPointer = null;
let lastPinchDistance = 0;
let pointerDownPosition = null;
let dragged = false;
function setCameraFromSpherical() {
camera.position
.setFromSpherical(spherical)
.add(cameraTarget);
camera.lookAt(cameraTarget);
}
function panCamera(dx, dy) {
const right = new THREE.Vector3(1, 0, 0)
.applyQuaternion(camera.quaternion)
.setY(0)
.normalize();
const forward = cameraTarget
.clone()
.sub(camera.position)
.setY(0)
.normalize();
const panScale = spherical.radius * 0.0016;
cameraTarget.addScaledVector(right, -dx * panScale);
cameraTarget.addScaledVector(forward, -dy * panScale);
desiredTarget.copy(cameraTarget);
}
canvas.addEventListener("contextmenu", (event) => event.preventDefault());
canvas.addEventListener("pointerdown", (event) => {
canvas.setPointerCapture(event.pointerId);
pointers.set(event.pointerId, {
x: event.clientX,
y: event.clientY
});
pointerDownPosition = {
x: event.clientX,
y: event.clientY
};
dragged = false;
focusing = false;
if (pointers.size === 1) {
lastPointer = {
x: event.clientX,
y: event.clientY,
button: event.button
};
} else if (pointers.size === 2) {
const values = [...pointers.values()];
lastPinchDistance = Math.hypot(
values[0].x - values[1].x,
values[0].y - values[1].y
);
}
});
canvas.addEventListener("pointermove", (event) => {
if (!pointers.has(event.pointerId)) return;
pointers.set(event.pointerId, {
x: event.clientX,
y: event.clientY
});
if (pointers.size >= 2) {
const values = [...pointers.values()];
const distance = Math.hypot(
values[0].x - values[1].x,
values[0].y - values[1].y
);
if (lastPinchDistance > 0 && distance > 0) {
spherical.radius = THREE.MathUtils.clamp(
spherical.radius * (lastPinchDistance / distance),
9,
48
);
}
lastPinchDistance = distance;
dragged = true;
return;
}
if (!lastPointer) return;
const dx = event.clientX - lastPointer.x;
const dy = event.clientY - lastPointer.y;
if (
Math.abs(event.clientX - pointerDownPosition.x) +
Math.abs(event.clientY - pointerDownPosition.y) >
5
) {
dragged = true;
}
const panning =
lastPointer.button === 2 ||
event.shiftKey ||
event.buttons === 4;
if (panning) {
panCamera(dx, dy);
} else {
spherical.theta -= dx * 0.005;
spherical.phi = THREE.MathUtils.clamp(
spherical.phi - dy * 0.005,
0.24,
1.45
);
}
lastPointer = {
x: event.clientX,
y: event.clientY,
button: lastPointer.button
};
});
function endPointer(event) {
pointers.delete(event.pointerId);
if (
pointers.size < 2 &&
pointers.size > 0
) {
const [remaining] = [...pointers.values()];
lastPointer = {
...remaining,
button: 0
};
}
if (pointers.size === 0) {
lastPointer = null;
lastPinchDistance = 0;
}
}
canvas.addEventListener("pointerup", (event) => {
if (!dragged && pointerDownPosition) {
pointerNDC.x =
(event.clientX / window.innerWidth) * 2 - 1;
pointerNDC.y =
-(event.clientY / window.innerHeight) * 2 + 1;
raycaster.setFromCamera(pointerNDC, camera);
const hits = raycaster.intersectObjects(clickableMarkers, false);
if (hits.length) {
selectZone(hits[0].object.userData.zoneId, true);
}
}
endPointer(event);
});
canvas.addEventListener("pointercancel", endPointer);
canvas.addEventListener(
"wheel",
(event) => {
event.preventDefault();
focusing = false;
spherical.radius = THREE.MathUtils.clamp(
spherical.radius * Math.exp(event.deltaY * 0.001),
9,
48
);
},
{ passive: false }
);
canvas.addEventListener("dblclick", () => {
state.follow = false;
followToggle.checked = false;
focusing = true;
desiredTarget.set(0, 0, 0);
desiredRadius = 27;
});
// ------------------------------------------------------------
// Animation
// ------------------------------------------------------------
const clock = new THREE.Clock();
const tempPoint = new THREE.Vector3();
const tempTangent = new THREE.Vector3();
const up = new THREE.Vector3(0, 1, 0);
let frame = 0;
function nearestZone(position) {
let closest = zones[0];
let closestDistance = Infinity;
zones.forEach((zone) => {
const distance = zone.position.distanceToSquared(position);
if (distance < closestDistance) {
closest = zone;
closestDistance = distance;
}
});
return closest;
}
function updateLabels() {
zones.forEach((zone) => {
const label = labelElements.get(zone.id);
tempPoint.copy(zone.position);
tempPoint.y += 0.45;
tempPoint.project(camera);
const visible =
tempPoint.z > -1 &&
tempPoint.z < 1 &&
Math.abs(tempPoint.x) < 1.15 &&
Math.abs(tempPoint.y) < 1.15;
label.style.display = visible ? "block" : "none";
if (visible) {
const x = (tempPoint.x * 0.5 + 0.5) * window.innerWidth;
const y = (-tempPoint.y * 0.5 + 0.5) * window.innerHeight;
label.style.left = `${x}px`;
label.style.top = `${y}px`;
}
});
}
function animate() {
requestAnimationFrame(animate);
const delta = Math.min(clock.getDelta(), 0.05);
frame++;
// Journey progression is visual, not a time-accurate hydrodynamic simulation.
if (state.playing) {
state.progress =
(state.progress + delta * 0.018 * (state.flow / 0.35)) % 1;
}
routeCurve.getPointAt(state.progress, currentMarker.position);
routeCurve.getTangentAt(state.progress, tempTangent).normalize();
currentMarker.position.y += 0.17;
const pulse = 1 + Math.sin(clock.elapsedTime * 4.2) * 0.13;
markerHalo.scale.setScalar(pulse);
markerHalo.material.opacity = 0.66 + Math.sin(clock.elapsedTime * 4.2) * 0.16;
markerArrow.position.copy(currentMarker.position);
markerArrow.quaternion.setFromUnitVectors(up, tempTangent);
const nearest = nearestZone(currentMarker.position);
if (nearest.id !== state.journeyZone) {
state.journeyZone = nearest.id;
journeyZone.textContent = nearest.name;
zones.forEach((zone) => {
zoneButtons.get(zone.id).classList.toggle(
"journey-active",
zone.id === nearest.id
);
});
}
journeyPercent.innerHTML = `${Math.round(
state.progress * 100
)} <small>%</small>`;
// Moving current particles.
const flowVisualRate = 0.035 * (0.55 + state.flow / 0.65);
for (let i = 0; i < currentParticleCount; i++) {
const t =
(particleOffsets[i] + clock.elapsedTime * flowVisualRate) % 1;
routeCurve.getPointAt(t, tempPoint);
dummy.position.copy(tempPoint);
dummy.position.y += 0.07;
dummy.rotation.set(0, t * Math.PI * 2, 0);
dummy.scale.setScalar(0.75 + (i % 4) * 0.1);
dummy.updateMatrix();
currentParticles.setMatrixAt(i, dummy.matrix);
}
currentParticles.instanceMatrix.needsUpdate = true;
// Network particles indicate links, not measured transport.
if (state.linksVisible) {
networkParticles.forEach((particle, index) => {
const t =
(particle.offset + clock.elapsedTime * 0.055) % 1;
particle.curve.getPoint(t, particle.mesh.position);
particle.mesh.position.y += 0.025;
particle.mesh.scale.setScalar(0.8 + Math.sin(index) * 0.12);
});
}
// Gentle faceted water motion.
for (let i = 0; i < waterPositions.count; i++) {
const x = waterBase[i * 2];
const z = waterBase[i * 2 + 1];
const y =
-0.18 +
Math.sin(x * 0.5 + clock.elapsedTime * 0.42) * 0.027 +
Math.cos(z * 0.62 - clock.elapsedTime * 0.35) * 0.023;
waterPositions.setY(i, y);
}
waterPositions.needsUpdate = true;
if (frame % 4 === 0) {
waterGeometry.computeVertexNormals();
}
plumeParticles.forEach((plume, index) => {
plume.position.y =
0.58 + Math.sin(clock.elapsedTime * 0.7 + index) * 0.09;
plume.position.x += Math.sin(clock.elapsedTime * 0.15 + index) * 0.001;
});
// Camera focus or current-follow behavior.
if (state.follow) {
desiredTarget.copy(currentMarker.position);
desiredTarget.y = 0;
cameraTarget.lerp(desiredTarget, 1 - Math.exp(-delta * 3.5));
spherical.radius = THREE.MathUtils.lerp(
spherical.radius,
17,
1 - Math.exp(-delta * 2)
);
} else if (focusing) {
cameraTarget.lerp(desiredTarget, 1 - Math.exp(-delta * 4));
spherical.radius = THREE.MathUtils.lerp(
spherical.radius,
desiredRadius,
1 - Math.exp(-delta * 4)
);
if (
cameraTarget.distanceTo(desiredTarget) < 0.04 &&
Math.abs(spherical.radius - desiredRadius) < 0.05
) {
focusing = false;
}
}
setCameraFromSpherical();
updateLabels();
renderer.render(scene, camera);
}
window.addEventListener("resize", () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
});
animate();
</script>
</body>
</html>
```