Prompt specification
Build or customize the Three.js 3D scene "Coastal nutrient upwelling exhibit" 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: Coastal nutrient upwelling exhibit
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/coastal-nutrient-upwelling-exhibit-x1231
Scene Description Prompt:
Coastal nutrient upwelling exhibit. Compose a coast, offshore surface arrows, and rising cool-water ribbons in cutaway; label the process schematic and separate water motion from decorative fish movement. Please build this with Three.js from cdnjs in one self-contained HTML file, using visible voxel blocks.
Working Three.js Starter (coastal-nutrient-upwelling-exhibit-x1231):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Coastal Nutrient Upwelling — Voxel Process Schematic</title>
<style>
:root {
--ink: #17343b;
--muted: #58747a;
--water: #087f9a;
--water-bright: #16b7ce;
--fish: #ee8747;
--nutrient: #78bd43;
--paper: rgba(255, 255, 255, 0.91);
--line: rgba(23, 52, 59, 0.16);
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #eaf4f1;
color: var(--ink);
font-family: Inter, ui-sans-serif, system-ui, -apple-system,
BlinkMacSystemFont, "Segoe UI", sans-serif;
}
#viewport {
position: fixed;
inset: 0;
}
canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
cursor: grab;
}
canvas:active {
cursor: grabbing;
}
.hud {
position: fixed;
inset: 0;
pointer-events: none;
z-index: 10;
}
.title-block {
position: absolute;
top: 22px;
left: 24px;
max-width: 430px;
padding: 15px 18px 16px;
border: 1px solid var(--line);
border-radius: 14px;
background: var(--paper);
box-shadow: 0 12px 35px rgba(30, 75, 80, 0.08);
backdrop-filter: blur(10px);
}
.eyebrow {
margin-bottom: 5px;
color: var(--water);
font-size: 11px;
font-weight: 800;
letter-spacing: 0.15em;
text-transform: uppercase;
}
h1 {
margin: 0;
font-size: clamp(23px, 2.6vw, 34px);
line-height: 1.05;
letter-spacing: -0.035em;
}
.subtitle {
margin-top: 8px;
color: var(--muted);
font-size: 13px;
line-height: 1.45;
}
.controls {
position: absolute;
top: 22px;
right: 24px;
width: 224px;
padding: 13px 15px;
border: 1px solid var(--line);
border-radius: 14px;
background: var(--paper);
box-shadow: 0 12px 35px rgba(30, 75, 80, 0.08);
backdrop-filter: blur(10px);
pointer-events: auto;
}
.controls-title {
margin-bottom: 9px;
color: var(--muted);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.13em;
text-transform: uppercase;
}
.control-row {
display: flex;
align-items: center;
gap: 8px;
min-height: 29px;
font-size: 12px;
font-weight: 650;
cursor: pointer;
}
.control-row input {
width: 15px;
height: 15px;
margin: 0;
accent-color: var(--water);
}
.control-divider {
height: 1px;
margin: 9px 0;
background: var(--line);
}
button {
width: 100%;
padding: 8px 10px;
border: 1px solid rgba(8, 127, 154, 0.25);
border-radius: 8px;
background: rgba(8, 127, 154, 0.08);
color: var(--ink);
font: inherit;
font-size: 12px;
font-weight: 750;
cursor: pointer;
}
button:hover {
background: rgba(8, 127, 154, 0.15);
}
.hint {
margin-top: 8px;
color: var(--muted);
font-size: 10px;
line-height: 1.35;
text-align: center;
}
.legend {
position: absolute;
left: 24px;
bottom: 22px;
width: min(440px, calc(100vw - 48px));
padding: 13px 16px;
border: 1px solid var(--line);
border-radius: 14px;
background: var(--paper);
box-shadow: 0 12px 35px rgba(30, 75, 80, 0.08);
backdrop-filter: blur(10px);
}
.legend-grid {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 14px;
}
.legend-item {
display: grid;
grid-template-columns: 24px 1fr;
gap: 8px;
align-items: start;
}
.swatch {
position: relative;
display: block;
width: 24px;
height: 10px;
margin-top: 3px;
}
.swatch.water {
background: linear-gradient(
90deg,
transparent 0 8%,
var(--water-bright) 8% 88%,
transparent 88%
);
}
.swatch.water::after {
position: absolute;
top: -4px;
left: 8px;
width: 0;
height: 0;
border-top: 5px solid transparent;
border-right: 7px solid var(--water-bright);
border-bottom: 5px solid transparent;
content: "";
}
.swatch.fish {
background: var(--fish);
clip-path: polygon(
0 50%, 24% 18%, 78% 18%, 100% 50%,
78% 82%, 24% 82%
);
}
.legend strong {
display: block;
margin-bottom: 2px;
font-size: 12px;
}
.legend p {
margin: 0;
color: var(--muted);
font-size: 10.5px;
line-height: 1.35;
}
.scale-note {
margin-top: 10px;
padding-top: 9px;
border-top: 1px solid var(--line);
color: var(--muted);
font-size: 10px;
line-height: 1.35;
}
#leaders {
position: fixed;
inset: 0;
width: 100%;
height: 100%;
pointer-events: none;
z-index: 4;
}
#leaders line {
stroke: rgba(23, 52, 59, 0.46);
stroke-width: 1.25;
stroke-dasharray: 3 3;
}
.annotation {
position: fixed;
z-index: 5;
display: flex;
align-items: center;
gap: 7px;
max-width: 205px;
padding: 7px 9px;
border: 1px solid rgba(8, 127, 154, 0.25);
border-radius: 8px;
background: rgba(255, 255, 255, 0.94);
box-shadow: 0 7px 20px rgba(30, 75, 80, 0.09);
color: var(--ink);
font-size: 10.5px;
font-weight: 750;
line-height: 1.2;
letter-spacing: 0.02em;
transform: translate(-50%, -50%);
white-space: nowrap;
pointer-events: none;
}
.annotation b {
display: inline-grid;
width: 20px;
height: 20px;
place-items: center;
flex: 0 0 auto;
border-radius: 5px;
background: var(--water);
color: white;
font-size: 9px;
}
.annotation.deco {
border-color: rgba(238, 135, 71, 0.45);
color: #7a4023;
}
.annotation.deco b {
background: var(--fish);
}
.annotation.coast {
border-color: rgba(84, 112, 76, 0.35);
}
.annotation.coast b {
background: #718d59;
}
#loading {
position: fixed;
inset: 0;
z-index: 100;
display: grid;
place-items: center;
background: #eaf4f1;
color: var(--water);
font-size: 13px;
font-weight: 750;
letter-spacing: 0.08em;
text-transform: uppercase;
}
#loading.hidden {
display: none;
}
@media (max-width: 760px) {
.title-block {
top: 12px;
left: 12px;
right: 12px;
max-width: none;
padding: 11px 13px;
}
.subtitle {
display: none;
}
.controls {
top: 107px;
right: 12px;
width: 190px;
padding: 10px 12px;
}
.legend {
left: 12px;
bottom: 12px;
width: calc(100vw - 24px);
padding: 10px 12px;
}
.legend-grid {
gap: 8px;
}
.scale-note {
display: none;
}
.annotation {
max-width: 170px;
padding: 6px 7px;
font-size: 9px;
}
}
</style>
</head>
<body>
<div id="viewport"></div>
<svg id="leaders" aria-hidden="true"></svg>
<div class="hud">
<section class="title-block">
<div class="eyebrow">Process schematic · Voxel cutaway</div>
<h1>Coastal nutrient upwelling</h1>
<div class="subtitle">
Offshore surface transport pulls cool, nutrient-rich water toward the
coast, where it rises and supplies the coastal ecosystem.
</div>
</section>
<section class="controls" aria-label="Display controls">
<div class="controls-title">Display & camera</div>
<label class="control-row">
<input id="water-toggle" type="checkbox" checked>
Water motion
</label>
<label class="control-row">
<input id="fish-toggle" type="checkbox" checked>
Decorative fish
</label>
<label class="control-row">
<input id="rotate-toggle" type="checkbox">
Slow orbit
</label>
<div class="control-divider"></div>
<button id="reset-view" type="button">Reset camera</button>
<div class="hint">Drag to orbit · Wheel to zoom<br>Double-click to reset</div>
</section>
<section class="legend" aria-label="Motion legend">
<div class="legend-grid">
<div class="legend-item">
<span class="swatch water"></span>
<div>
<strong>Water motion</strong>
<p>Blue arrows and rising cyan ribbons encode the process.</p>
</div>
</div>
<div class="legend-item">
<span class="swatch fish"></span>
<div>
<strong>Decorative fish</strong>
<p>Orange fish move independently and are not a process cue.</p>
</div>
</div>
</div>
<div class="scale-note">
Green voxels represent dissolved nutrients carried upward with the
cool-water plume. The diagram is schematic and not to scale.
</div>
</section>
<div id="label-surface" class="annotation">
<b>01</b><span>Offshore surface flow</span>
</div>
<div id="label-rise" class="annotation">
<b>02</b><span>Cool water rises along the coast</span>
</div>
<div id="label-nutrients" class="annotation">
<b>03</b><span>Nutrients reach coastal water</span>
</div>
<div id="label-coast" class="annotation coast">
<b>▰</b><span>Coast and cliff</span>
</div>
<div id="label-fish" class="annotation deco">
<b>~</b><span>Decorative fish · horizontal only</span>
</div>
</div>
<div id="loading">Building voxel cutaway…</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const viewport = document.getElementById("viewport");
const loading = document.getElementById("loading");
let renderer;
try {
renderer = new THREE.WebGLRenderer({
antialias: true,
alpha: false,
powerPreference: "high-performance"
});
} catch (error) {
loading.textContent = "WebGL could not be initialized.";
throw error;
}
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.05;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.domElement.setAttribute(
"aria-label",
"Interactive three-dimensional coastal upwelling process diagram"
);
viewport.appendChild(renderer.domElement);
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xeaf4f1);
scene.fog = new THREE.Fog(0xeaf4f1, 48, 88);
const camera = new THREE.PerspectiveCamera(
42,
window.innerWidth / window.innerHeight,
0.1,
150
);
const hemisphere = new THREE.HemisphereLight(0xf4ffff, 0x8a8268, 2.25);
scene.add(hemisphere);
const sun = new THREE.DirectionalLight(0xfff4dc, 3.15);
sun.position.set(-14, 25, 17);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -28;
sun.shadow.camera.right = 28;
sun.shadow.camera.top = 25;
sun.shadow.camera.bottom = -25;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 65;
sun.shadow.bias = -0.0004;
scene.add(sun);
const rimLight = new THREE.DirectionalLight(0x84dce6, 1.15);
rimLight.position.set(18, 8, -18);
scene.add(rimLight);
const unitBox = new THREE.BoxGeometry(1, 1, 1);
const dummy = new THREE.Object3D();
function standardMaterial(options = {}) {
return new THREE.MeshStandardMaterial({
roughness: 0.78,
metalness: 0,
flatShading: true,
...options
});
}
const materials = {
sand: standardMaterial({ color: 0xd8b779 }),
sandLight: standardMaterial({ color: 0xe4c990 }),
rock: standardMaterial({ color: 0x9a8067 }),
rockLight: standardMaterial({ color: 0xb39678 }),
grass: standardMaterial({ color: 0x789b59 }),
grassLight: standardMaterial({ color: 0x91ad64 }),
waterSurface: standardMaterial({
color: 0xffffff,
transparent: true,
opacity: 0.56,
roughness: 0.25,
depthWrite: false
}),
waterWall: standardMaterial({
color: 0x51bfd2,
transparent: true,
opacity: 0.23,
roughness: 0.32,
depthWrite: false
}),
foam: standardMaterial({ color: 0xeafcff }),
arrow: standardMaterial({
color: 0x087f9a,
emissive: 0x064c61,
emissiveIntensity: 0.28
}),
ribbon: standardMaterial({
color: 0xffffff,
transparent: true,
opacity: 0.58,
roughness: 0.35,
depthWrite: false
}),
pulse: standardMaterial({
color: 0x6de9f5,
emissive: 0x22bfd4,
emissiveIntensity: 1.05,
roughness: 0.25
}),
nutrient: standardMaterial({
color: 0x8bd44f,
emissive: 0x397c19,
emissiveIntensity: 0.6
}),
fish: standardMaterial({ color: 0xee8747 }),
fishLight: standardMaterial({ color: 0xf6a05f }),
eye: standardMaterial({ color: 0x23383b })
};
/**
* Creates a voxel batch from simple transform descriptions.
* Instancing keeps the large cutaway lightweight while retaining
* individually visible cube gaps and edges.
*/
function createBatch(positions, material, parent = scene) {
if (!positions.length) return null;
const mesh = new THREE.InstancedMesh(unitBox, material, positions.length);
mesh.castShadow = !material.transparent;
mesh.receiveShadow = true;
mesh.instanceMatrix.setUsage(THREE.StaticDrawUsage);
positions.forEach((p, index) => {
dummy.position.set(p.x, p.y, p.z);
dummy.rotation.set(p.rx || 0, p.ry || 0, p.rz || 0);
dummy.scale.set(p.sx ?? 1, p.sy ?? 1, p.sz ?? 1);
dummy.updateMatrix();
mesh.setMatrixAt(index, dummy.matrix);
if (p.color) {
mesh.setColorAt(index, new THREE.Color(p.color));
}
});
mesh.instanceMatrix.needsUpdate = true;
if (mesh.instanceColor) mesh.instanceColor.needsUpdate = true;
parent.add(mesh);
return mesh;
}
function hash2(x, z) {
const value = Math.sin(x * 127.1 + z * 311.7) * 43758.5453123;
return value - Math.floor(value);
}
// Ground and seabed.
const ground = new THREE.Mesh(
new THREE.PlaneGeometry(160, 160),
standardMaterial({ color: 0xdde8df, roughness: 1 })
);
ground.rotation.x = -Math.PI / 2;
ground.position.y = -7.85;
ground.receiveShadow = true;
scene.add(ground);
const seabed = [];
for (let x = -15; x <= 7; x++) {
for (let z = -7; z <= 7; z++) {
seabed.push({
x,
y: -7.42,
z,
sx: 0.93,
sy: 0.34,
sz: 0.93,
color: hash2(x, z) > 0.76 ? 0xc5a773 : 0xd5b77e
});
}
}
createBatch(seabed, standardMaterial({ color: 0xffffff }));
// Transparent voxel water: rear and left walls plus a tiled surface.
// The front remains open to create the cutaway.
const rearWall = [];
const leftWall = [];
for (let x = -15; x <= 7; x++) {
for (let y = -6; y <= -1; y++) {
rearWall.push({
x,
y,
z: -7,
sx: 0.93,
sy: 0.93,
sz: 0.93
});
}
}
for (let z = -6; z <= 6; z++) {
for (let y = -6; y <= -1; y++) {
leftWall.push({
x: -15,
y,
z,
sx: 0.93,
sy: 0.93,
sz: 0.93
});
}
}
createBatch(rearWall, materials.waterWall);
createBatch(leftWall, materials.waterWall);
const surfaceTiles = [];
for (let x = -15; x <= 7; x++) {
for (let z = -7; z <= 7; z++) {
const variation = hash2(x * 0.7, z * 1.3);
surfaceTiles.push({
x,
y: 0,
z,
sx: 0.93,
sy: 0.18,
sz: 0.93,
color: new THREE.Color().setHSL(
0.515 + variation * 0.012,
0.67,
0.51 + variation * 0.065
)
});
}
}
createBatch(surfaceTiles, materials.waterSurface);
// Voxel coastline, rising from a low beach to a high coastal cliff.
const beachBlocks = [];
const rockBlocks = [];
const grassBlocks = [];
for (let x = 8; x <= 16; x++) {
for (let z = -7; z <= 7; z++) {
const baseHeight = 7 + (x - 8);
const edgeLift =
(Math.abs(z) > 6 && hash2(x, z) > 0.55) ? 1 : 0;
const height = baseHeight + edgeLift;
for (let layer = 0; layer < height; layer++) {
const y = -7 + layer;
const isTop = layer === height - 1;
const block = {
x,
y,
z,
sx: 0.96,
sy: 0.96,
sz: 0.96
};
if (isTop && y >= 0) {
grassBlocks.push({
...block,
color:
hash2(x + 4, z) > 0.55
? 0x789b59
: 0x91ad64
});
} else if (y <= 1) {
beachBlocks.push({
...block,
color:
hash2(x, z + 9) > 0.52
? 0xd8b779
: 0xe4c990
});
} else {
rockBlocks.push({
...block,
color:
hash2(x * 1.3, z * 1.7) > 0.58
? 0x9a8067
: 0xb39678
});
}
}
}
}
createBatch(beachBlocks, standardMaterial({ color: 0xffffff }));
createBatch(rockBlocks, standardMaterial({ color: 0xffffff }));
createBatch(grassBlocks, standardMaterial({ color: 0xffffff }));
// Sparse blocks of shoreline foam.
const foam = [];
for (let z = -7; z <= 7; z += 2) {
foam.push({
x: 7.55,
y: 0.18,
z,
sx: 0.38,
sy: 0.15,
sz: 0.74
});
if (hash2(z, 3) > 0.42) {
foam.push({
x: 7.1,
y: 0.16,
z: z + 0.5,
sx: 0.55,
sy: 0.12,
sz: 0.42
});
}
}
createBatch(foam, materials.foam);
// Three rising cool-water ribbon paths.
const ribbonCurves = [-3.5, 0, 3.5].map((z, ribbonIndex) => {
const shift = ribbonIndex * 0.12;
return new THREE.CatmullRomCurve3(
[
new THREE.Vector3(6.8, -6.65, z),
new THREE.Vector3(6.55, -5.45, z - 0.1),
new THREE.Vector3(6.1, -4.05, z - 0.2 + shift),
new THREE.Vector3(5.25, -2.65, z + shift),
new THREE.Vector3(3.85, -1.45, z + 0.12),
new THREE.Vector3(1.8, -0.48, z + 0.18),
new THREE.Vector3(-1.0, -0.1, z + 0.12),
new THREE.Vector3(-4.4, 0.12, z)
],
false,
"catmullrom",
0.48
);
});
const ribbonBlocks = [];
ribbonCurves.forEach((curve, ribbonIndex) => {
for (let i = 0; i <= 72; i++) {
const t = i / 72;
const point = curve.getPointAt(t);
for (let width = -1; width <= 1; width++) {
const lightness = 0.42 + width * 0.025 + (i % 4) * 0.012;
ribbonBlocks.push({
x: point.x,
y: point.y,
z: point.z + width * 0.245,
sx: 0.31,
sy: 0.31,
sz: 0.31,
color: new THREE.Color().setHSL(
0.515 + ribbonIndex * 0.006,
0.76,
lightness
)
});
}
}
});
createBatch(ribbonBlocks, materials.ribbon);
// Moving process elements are kept separate from decorative fish.
const waterMotionGroup = new THREE.Group();
scene.add(waterMotionGroup);
function createArrow(colorMaterial, scale = 1) {
const arrow = new THREE.Group();
const blocks = [];
// Shaft points toward negative X.
for (let i = 0; i < 7; i++) {
blocks.push({
x: 0.35 + i * 0.42,
y: 0,
z: 0,
sx: 0.38,
sy: 0.28,
sz: 0.38
});
}
// Stepped voxel arrowhead.
blocks.push(
{ x: -0.48, y: 0, z: 0, sx: 0.38, sy: 0.28, sz: 0.38 },
{ x: -0.06, y: 0, z: -0.42, sx: 0.38, sy: 0.28, sz: 0.38 },
{ x: -0.06, y: 0, z: 0.42, sx: 0.38, sy: 0.28, sz: 0.38 },
{ x: 0.36, y: 0, z: -0.84, sx: 0.38, sy: 0.28, sz: 0.38 },
{ x: 0.36, y: 0, z: 0, sx: 0.38, sy: 0.28, sz: 0.38 },
{ x: 0.36, y: 0, z: 0.84, sx: 0.38, sy: 0.28, sz: 0.38 }
);
createBatch(blocks, colorMaterial, arrow);
arrow.scale.setScalar(scale);
return arrow;
}
const surfaceArrows = [];
[-4.4, 0, 4.4].forEach((z, index) => {
const arrow = createArrow(materials.arrow);
arrow.position.set(4.5, 0.68, z);
arrow.userData.phase = index * 6.7;
arrow.userData.speed = 1.35 + index * 0.13;
waterMotionGroup.add(arrow);
surfaceArrows.push(arrow);
});
// Pale wind streaks above the surface, also moving offshore.
const windStreaks = [];
[-5.5, -3.7].forEach((z, index) => {
const streak = createArrow(
standardMaterial({
color: 0xd9f4f3,
emissive: 0xa5dfe0,
emissiveIntensity: 0.15,
transparent: true,
opacity: 0.85
}),
0.64
);
streak.position.set(2, 2.75 + index * 0.25, z);
streak.userData.phase = index * 9;
streak.userData.speed = 2.1;
waterMotionGroup.add(streak);
windStreaks.push(streak);
});
// Bright moving pulses show the direction of flow along each ribbon.
const pulseMesh = new THREE.InstancedMesh(
unitBox,
materials.pulse,
ribbonCurves.length * 3
);
pulseMesh.instanceMatrix.setUsage(THREE.DynamicDrawUsage);
pulseMesh.frustumCulled = false;
waterMotionGroup.add(pulseMesh);
// Nutrient voxels rise with the cool-water plume.
const nutrientMesh = new THREE.InstancedMesh(
unitBox,
materials.nutrient,
30
);
nutrientMesh.instanceMatrix.setUsage(THREE.DynamicDrawUsage);
nutrientMesh.frustumCulled = false;
waterMotionGroup.add(nutrientMesh);
// Decorative fish: independent, horizontal, voxel-built creatures.
const fishGroup = new THREE.Group();
scene.add(fishGroup);
function createFish(phase, depth, lane) {
const fish = new THREE.Group();
const bodyBlocks = [
{ x: 0, y: 0, z: 0, sx: 0.72, sy: 0.36, sz: 0.30 },
{ x: 0.43, y: 0.01, z: 0, sx: 0.34, sy: 0.32, sz: 0.29 },
{ x: -0.48, y: 0, z: 0, sx: 0.28, sy: 0.28, sz: 0.13 },
{ x: -0.69, y: 0, z: 0, sx: 0.19, sy: 0.48, sz: 0.11 },
{ x: -0.08, y: 0.25, z: 0, sx: 0.27, sy: 0.19, sz: 0.10 }
];
createBatch(bodyBlocks, materials.fish, fish);
createBatch(
[{ x: 0.54, y: 0.08, z: 0.15, sx: 0.07, sy: 0.07, sz: 0.06 }],
materials.eye,
fish
);
fish.userData.phase = phase;
fish.userData.depth = depth;
fish.userData.lane = lane;
fishGroup.add(fish);
return fish;
}
const fishes = [];
for (let i = 0; i < 7; i++) {
fishes.push(
createFish(
i * 0.92,
-1.2 - (i % 3) * 0.55,
5.2 + (i % 2) * 0.55
)
);
}
// ------------------------------------------------------------------
// Camera controls implemented directly in this HTML.
// ------------------------------------------------------------------
const cameraTarget = new THREE.Vector3(-0.6, -1.6, 0);
const initialCamera = {
theta: 0.61,
phi: 1.17,
radius: 34
};
let desiredTheta = initialCamera.theta;
let desiredPhi = initialCamera.phi;
let desiredRadius = initialCamera.radius;
let currentTheta = desiredTheta;
let currentPhi = desiredPhi;
let currentRadius = desiredRadius;
function applyCamera() {
const sinPhi = Math.sin(currentPhi);
camera.position.set(
cameraTarget.x + currentRadius * sinPhi * Math.sin(currentTheta),
cameraTarget.y + currentRadius * Math.cos(currentPhi),
cameraTarget.z + currentRadius * sinPhi * Math.cos(currentTheta)
);
camera.lookAt(cameraTarget);
}
function resetCamera() {
desiredTheta = initialCamera.theta;
desiredPhi = initialCamera.phi;
desiredRadius = initialCamera.radius;
cameraTarget.set(-0.6, -1.6, 0);
}
const canvas = renderer.domElement;
let dragging = false;
let pointerX = 0;
let pointerY = 0;
canvas.addEventListener("pointerdown", (event) => {
dragging = true;
pointerX = event.clientX;
pointerY = event.clientY;
canvas.setPointerCapture(event.pointerId);
});
canvas.addEventListener("pointermove", (event) => {
if (!dragging) return;
const dx = event.clientX - pointerX;
const dy = event.clientY - pointerY;
pointerX = event.clientX;
pointerY = event.clientY;
desiredTheta -= dx * 0.006;
desiredPhi = THREE.MathUtils.clamp(
desiredPhi + dy * 0.005,
0.38,
1.43
);
});
function endDrag(event) {
dragging = false;
if (event.pointerId !== undefined && canvas.hasPointerCapture(event.pointerId)) {
canvas.releasePointerCapture(event.pointerId);
}
}
canvas.addEventListener("pointerup", endDrag);
canvas.addEventListener("pointercancel", endDrag);
canvas.addEventListener(
"wheel",
(event) => {
event.preventDefault();
desiredRadius = THREE.MathUtils.clamp(
desiredRadius * Math.exp(event.deltaY * 0.001),
18,
53
);
},
{ passive: false }
);
canvas.addEventListener("dblclick", resetCamera);
document.getElementById("reset-view").addEventListener("click", resetCamera);
// ------------------------------------------------------------------
// Projected process annotations.
// ------------------------------------------------------------------
const leaderSvg = document.getElementById("leaders");
const svgNamespace = "http://www.w3.org/2000/svg";
const annotations = [
{
element: document.getElementById("label-surface"),
anchor: new THREE.Vector3(-5.3, 0.8, 0),
offset: [-142, -72]
},
{
element: document.getElementById("label-rise"),
anchor: new THREE.Vector3(5.8, -3.0, 0),
offset: [-175, 10]
},
{
element: document.getElementById("label-nutrients"),
anchor: new THREE.Vector3(2.6, -0.4, 0),
offset: [-125, 72]
},
{
element: document.getElementById("label-coast"),
anchor: new THREE.Vector3(12.2, 4.4, 0),
offset: [72, -50]
},
{
element: document.getElementById("label-fish"),
anchor: new THREE.Vector3(-6.5, -1.4, 5.6),
offset: [-60, 68]
}
];
annotations.forEach((annotation) => {
annotation.line = document.createElementNS(svgNamespace, "line");
leaderSvg.appendChild(annotation.line);
});
function updateAnnotations() {
const width = window.innerWidth;
const height = window.innerHeight;
const offsetScale = Math.min(1, width / 1050);
annotations.forEach((annotation) => {
const projected = annotation.anchor.clone().project(camera);
const visible = projected.z > -1 && projected.z < 1;
annotation.element.style.display = visible ? "flex" : "none";
annotation.line.style.display = visible ? "block" : "none";
if (!visible) return;
const anchorX = (projected.x * 0.5 + 0.5) * width;
const anchorY = (-projected.y * 0.5 + 0.5) * height;
const labelX = THREE.MathUtils.clamp(
anchorX + annotation.offset[0] * offsetScale,
92,
width - 92
);
const labelY = THREE.MathUtils.clamp(
anchorY + annotation.offset[1] * offsetScale,
80,
height - 155
);
annotation.element.style.left = `${labelX}px`;
annotation.element.style.top = `${labelY}px`;
annotation.line.setAttribute("x1", anchorX);
annotation.line.setAttribute("y1", anchorY);
annotation.line.setAttribute("x2", labelX);
annotation.line.setAttribute("y2", labelY);
});
}
// ------------------------------------------------------------------
// Controls and animation.
// ------------------------------------------------------------------
const waterToggle = document.getElementById("water-toggle");
const fishToggle = document.getElementById("fish-toggle");
const rotateToggle = document.getElementById("rotate-toggle");
let waterMotionEnabled = true;
let fishMotionEnabled = true;
let autoRotateEnabled = false;
waterToggle.addEventListener("change", () => {
waterMotionEnabled = waterToggle.checked;
waterMotionGroup.visible = waterMotionEnabled;
});
fishToggle.addEventListener("change", () => {
fishMotionEnabled = fishToggle.checked;
fishGroup.visible = fishMotionEnabled;
});
rotateToggle.addEventListener("change", () => {
autoRotateEnabled = rotateToggle.checked;
});
const clock = new THREE.Clock();
let waterTime = 0;
let fishTime = 0;
function updateProcessMotion(delta) {
if (!waterMotionEnabled) return;
waterTime += delta;
surfaceArrows.forEach((arrow) => {
const travel = 23;
arrow.position.x =
6.5 - ((waterTime * arrow.userData.speed + arrow.userData.phase) % travel);
});
windStreaks.forEach((streak) => {
const travel = 28;
streak.position.x =
7 - ((waterTime * streak.userData.speed + streak.userData.phase) % travel);
});
let pulseIndex = 0;
ribbonCurves.forEach((curve, ribbonIndex) => {
for (let lane = 0; lane < 3; lane++) {
const t =
(waterTime * 0.11 + ribbonIndex * 0.08 + lane / 3) % 1;
const point = curve.getPointAt(t);
dummy.position.copy(point);
dummy.position.z += (lane - 1) * 0.24;
dummy.rotation.set(0, 0, 0);
dummy.scale.setScalar(0.51);
dummy.updateMatrix();
pulseMesh.setMatrixAt(pulseIndex++, dummy.matrix);
}
});
pulseMesh.instanceMatrix.needsUpdate = true;
for (let i = 0; i < 30; i++) {
const ribbonIndex = i % ribbonCurves.length;
const curve = ribbonCurves[ribbonIndex];
const particleIndex = Math.floor(i / ribbonCurves.length);
const t =
(waterTime * 0.082 + particleIndex / 10 + ribbonIndex * 0.017) % 1;
const point = curve.getPointAt(t);
dummy.position.copy(point);
dummy.position.y += Math.sin(waterTime * 2.4 + i * 1.7) * 0.12;
dummy.position.z +=
Math.sin(waterTime * 1.8 + i * 0.9) * 0.22 +
(i % 3 - 1) * 0.08;
dummy.rotation.set(
waterTime * 0.8 + i,
waterTime * 0.5,
waterTime * 0.7
);
dummy.scale.setScalar(0.12 + (i % 3) * 0.018);
dummy.updateMatrix();
nutrientMesh.setMatrixAt(i, dummy.matrix);
}
nutrientMesh.instanceMatrix.needsUpdate = true;
}
function updateFishMotion(delta) {
if (!fishMotionEnabled) return;
fishTime += delta;
fishes.forEach((fish) => {
const angle = fishTime * 0.22 + fish.userData.phase;
const x = -6.4 + Math.cos(angle) * 4.3;
const z = fish.userData.lane + Math.sin(angle) * 0.95;
fish.position.set(x, fish.userData.depth, z);
// Keep the fish in a horizontal plane and point it along its path.
const dx = -Math.sin(angle);
const dz = Math.cos(angle);
fish.rotation.y = Math.atan2(-dz, dx);
});
}
function resize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
}
window.addEventListener("resize", resize);
function animate() {
requestAnimationFrame(animate);
const delta = Math.min(clock.getDelta(), 0.05);
if (autoRotateEnabled && !dragging) {
desiredTheta += delta * 0.085;
}
currentTheta += (desiredTheta - currentTheta) * 0.11;
currentPhi += (desiredPhi - currentPhi) * 0.11;
currentRadius += (desiredRadius - currentRadius) * 0.11;
applyCamera();
updateProcessMotion(delta);
updateFishMotion(delta);
updateAnnotations();
renderer.render(scene, camera);
}
applyCamera();
updateAnnotations();
loading.classList.add("hidden");
animate();
</script>
</body>
</html>
```