Prompt specification
Build or customize the Three.js 3D scene "Microbial food-web toy dynamics" 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: Microbial food-web toy dynamics
Category: Biology & Nature · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/microbial-food-web-toy-dynamics-x0860
Scene Description Prompt:
Make a standalone HTML scene with Three.js from cdnjs that depicts microbial food webs. Let users adjust initial populations in a deliberately simplified resource-consumer model; show bounded trajectories and state that parameter choices do not predict real pond ecology. Low-poly.
Working Three.js Starter (microbial-food-web-toy-dynamics-x0860):
```html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Microbial Food Web — Interactive Pond Model</title>
<style>
:root {
color-scheme: dark;
--bg: #061311;
--panel: #0b1a18;
--panel-2: #102320;
--line: rgba(181, 229, 211, 0.14);
--text: #e7f5ee;
--muted: #91aaa0;
--nutrient: #f6c85f;
--algae: #69dc83;
--bacteria: #e779c4;
--protozoa: #65c9ff;
--accent: #72e0bc;
}
* {
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;
}
#app {
display: grid;
grid-template-columns: minmax(0, 1fr) 390px;
width: 100%;
height: 100vh;
}
#viewport {
position: relative;
min-width: 0;
min-height: 0;
overflow: hidden;
background: var(--bg);
}
#threeCanvas {
display: block;
width: 100%;
height: 100%;
cursor: grab;
touch-action: none;
outline: none;
}
#threeCanvas:active {
cursor: grabbing;
}
.vignette {
position: absolute;
inset: 0;
pointer-events: none;
background:
radial-gradient(circle at 50% 48%, transparent 38%, rgba(1, 8, 7, 0.32) 100%),
linear-gradient(to bottom, rgba(3, 12, 10, 0.18), transparent 35%);
}
.hero {
position: absolute;
top: 0;
left: 0;
z-index: 3;
width: min(570px, calc(100% - 190px));
padding: 30px 34px 34px;
pointer-events: none;
}
.eyebrow {
margin-bottom: 10px;
color: var(--accent);
font-size: 0.68rem;
font-weight: 800;
letter-spacing: 0.19em;
text-transform: uppercase;
}
h1 {
max-width: 520px;
margin: 0;
font-size: clamp(2rem, 4.2vw, 4.4rem);
font-weight: 620;
letter-spacing: -0.055em;
line-height: 0.96;
text-wrap: balance;
}
.hero p {
max-width: 470px;
margin: 17px 0 0;
color: #a9c1b8;
font-size: 0.93rem;
line-height: 1.55;
}
.scene-actions {
position: absolute;
z-index: 5;
top: 24px;
right: 24px;
display: flex;
align-items: center;
gap: 8px;
}
.status-pill,
.icon-button {
border: 1px solid var(--line);
background: rgba(7, 22, 19, 0.78);
backdrop-filter: blur(12px);
box-shadow: 0 10px 30px rgba(0, 0, 0, 0.16);
}
.status-pill {
display: flex;
align-items: center;
gap: 8px;
padding: 9px 12px;
border-radius: 999px;
color: #bcd1c8;
font-size: 0.7rem;
font-weight: 750;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.status-pill i {
width: 7px;
height: 7px;
border-radius: 50%;
background: var(--accent);
box-shadow: 0 0 12px var(--accent);
}
.status-pill.paused i {
background: var(--nutrient);
box-shadow: 0 0 12px var(--nutrient);
}
.icon-button {
height: 35px;
padding: 0 11px;
border-radius: 10px;
cursor: pointer;
color: #bcd1c8;
font-size: 0.72rem;
font-weight: 700;
}
.icon-button:hover {
border-color: rgba(114, 224, 188, 0.5);
color: white;
}
.view-hint {
position: absolute;
z-index: 3;
bottom: 22px;
left: 28px;
display: flex;
align-items: center;
gap: 9px;
padding: 9px 12px;
border: 1px solid var(--line);
border-radius: 10px;
background: rgba(5, 17, 14, 0.68);
color: #91aaa0;
font-size: 0.72rem;
backdrop-filter: blur(10px);
pointer-events: none;
}
.view-hint span {
color: var(--accent);
font-size: 1rem;
}
#worldLabels {
position: absolute;
inset: 0;
z-index: 2;
pointer-events: none;
}
.world-label {
position: absolute;
display: flex;
align-items: center;
gap: 7px;
padding: 6px 9px;
border: 1px solid rgba(210, 240, 228, 0.16);
border-radius: 999px;
background: rgba(5, 18, 15, 0.73);
color: #e7f5ee;
font-size: 0.68rem;
font-weight: 750;
letter-spacing: 0.035em;
white-space: nowrap;
transform: translate(-50%, -120%);
backdrop-filter: blur(8px);
opacity: 0;
transition: opacity 160ms ease;
}
.world-label i {
width: 7px;
height: 7px;
border-radius: 50%;
}
.world-label.nutrient i { background: var(--nutrient); }
.world-label.algae i { background: var(--algae); }
.world-label.bacteria i { background: var(--bacteria); }
.world-label.protozoa i { background: var(--protozoa); }
#panel {
position: relative;
z-index: 10;
overflow-y: auto;
padding: 27px 25px 24px;
border-left: 1px solid var(--line);
background:
radial-gradient(circle at 80% 0%, rgba(64, 151, 122, 0.11), transparent 30%),
var(--panel);
scrollbar-width: thin;
scrollbar-color: #29473e transparent;
}
.panel-heading {
display: flex;
align-items: flex-start;
justify-content: space-between;
gap: 18px;
margin-bottom: 22px;
}
.panel-heading h2 {
margin: 0;
font-size: 1.17rem;
font-weight: 720;
letter-spacing: -0.025em;
}
.panel-heading p {
margin: 6px 0 0;
color: var(--muted);
font-size: 0.76rem;
line-height: 1.45;
}
.bounded-badge {
flex: 0 0 auto;
padding: 6px 8px;
border: 1px solid rgba(114, 224, 188, 0.25);
border-radius: 8px;
color: var(--accent);
font-size: 0.58rem;
font-weight: 800;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.panel-section {
padding: 20px 0;
border-top: 1px solid var(--line);
}
.panel-section:first-of-type {
border-top: 0;
padding-top: 0;
}
.section-title {
display: flex;
align-items: center;
justify-content: space-between;
margin-bottom: 15px;
color: #c9ded5;
font-size: 0.67rem;
font-weight: 800;
letter-spacing: 0.13em;
text-transform: uppercase;
}
.section-title small {
color: #6e8e82;
font-size: 0.62rem;
font-weight: 650;
letter-spacing: 0.04em;
text-transform: none;
}
.control {
display: block;
margin-bottom: 17px;
}
.control:last-child {
margin-bottom: 0;
}
.control > span:first-child {
display: flex;
align-items: center;
gap: 8px;
margin-bottom: 9px;
color: #dcece5;
font-size: 0.78rem;
font-weight: 650;
}
.control output {
margin-left: auto;
min-width: 34px;
padding: 3px 7px;
border-radius: 7px;
background: rgba(255, 255, 255, 0.055);
color: white;
font-variant-numeric: tabular-nums;
text-align: center;
}
.dot {
width: 8px;
height: 8px;
border-radius: 50%;
box-shadow: 0 0 10px currentColor;
}
.dot.r { color: var(--nutrient); background: var(--nutrient); }
.dot.a { color: var(--algae); background: var(--algae); }
.dot.b { color: var(--bacteria); background: var(--bacteria); }
.dot.p { color: var(--protozoa); background: var(--protozoa); }
input[type="range"] {
width: 100%;
height: 4px;
margin: 0;
border-radius: 99px;
appearance: none;
background: #233b34;
cursor: pointer;
accent-color: var(--accent);
}
input[type="range"]::-webkit-slider-thumb {
width: 15px;
height: 15px;
border: 3px solid #0c1d19;
border-radius: 50%;
appearance: none;
background: var(--accent);
box-shadow: 0 0 0 1px rgba(114, 224, 188, 0.45);
}
input[type="range"]::-moz-range-thumb {
width: 10px;
height: 10px;
border: 3px solid #0c1d19;
border-radius: 50%;
background: var(--accent);
}
.range-note {
display: flex;
justify-content: space-between;
margin-top: 5px;
color: #617d73;
font-size: 0.59rem;
}
.button-row {
display: grid;
grid-template-columns: 1fr 0.72fr;
gap: 9px;
margin-top: 20px;
}
button {
border: 1px solid var(--line);
border-radius: 10px;
cursor: pointer;
transition:
border-color 150ms ease,
background 150ms ease,
transform 150ms ease;
}
button:active {
transform: translateY(1px);
}
.primary-button {
padding: 11px 12px;
border-color: transparent;
background: var(--accent);
color: #082018;
font-size: 0.76rem;
font-weight: 820;
}
.primary-button:hover {
background: #91edcd;
}
.secondary-button {
padding: 10px 12px;
background: rgba(255, 255, 255, 0.035);
color: #c3d8cf;
font-size: 0.74rem;
font-weight: 720;
}
.secondary-button:hover {
border-color: rgba(114, 224, 188, 0.35);
background: rgba(114, 224, 188, 0.07);
}
.utility-row {
display: flex;
align-items: center;
justify-content: space-between;
gap: 12px;
margin-top: 10px;
}
.text-button {
padding: 6px 0;
border: 0;
background: transparent;
color: #809f94;
font-size: 0.68rem;
}
.text-button:hover {
color: var(--accent);
}
.speed-control {
display: flex;
align-items: center;
gap: 4px;
padding: 3px;
border: 1px solid var(--line);
border-radius: 9px;
}
.speed-control span {
padding-left: 5px;
color: #6f8e83;
font-size: 0.61rem;
text-transform: uppercase;
}
.speed-button {
min-width: 31px;
padding: 5px 7px;
border: 0;
border-radius: 6px;
background: transparent;
color: #78968b;
font-size: 0.65rem;
font-weight: 760;
}
.speed-button.active {
background: rgba(114, 224, 188, 0.13);
color: var(--accent);
}
.reading-grid {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 8px;
}
.reading {
padding: 12px;
border: 1px solid var(--line);
border-radius: 11px;
background: rgba(255, 255, 255, 0.025);
}
.reading-label {
display: flex;
align-items: center;
gap: 7px;
color: #9bb4aa;
font-size: 0.66rem;
font-weight: 650;
}
.reading-value {
margin-top: 6px;
color: white;
font-size: 1.25rem;
font-weight: 660;
font-variant-numeric: tabular-nums;
}
.reading-value small {
color: #668279;
font-size: 0.62rem;
font-weight: 550;
}
#chart {
display: block;
width: 100%;
height: 220px;
border: 1px solid var(--line);
border-radius: 12px;
background:
linear-gradient(rgba(255, 255, 255, 0.018), rgba(255, 255, 255, 0.008));
}
.chart-key {
display: flex;
flex-wrap: wrap;
gap: 12px;
margin-top: 10px;
color: #829e94;
font-size: 0.62rem;
}
.chart-key span {
display: flex;
align-items: center;
gap: 5px;
}
.chart-key i {
width: 14px;
height: 2px;
border-radius: 99px;
}
.toggle-row {
display: flex;
align-items: center;
justify-content: space-between;
gap: 15px;
padding: 10px 12px;
border: 1px solid var(--line);
border-radius: 10px;
background: rgba(255, 255, 255, 0.02);
color: #b9cec5;
font-size: 0.73rem;
}
.toggle-row input {
width: 17px;
height: 17px;
accent-color: var(--accent);
}
.model-copy {
margin: 12px 0 0;
color: #829e94;
font-size: 0.71rem;
line-height: 1.55;
}
.model-copy strong {
color: #bed3ca;
font-weight: 700;
}
.disclaimer {
margin-top: 14px;
padding: 13px 14px;
border: 1px solid rgba(246, 200, 95, 0.2);
border-radius: 11px;
background: rgba(246, 200, 95, 0.055);
color: #c7b988;
font-size: 0.69rem;
line-height: 1.5;
}
.disclaimer strong {
display: block;
margin-bottom: 3px;
color: var(--nutrient);
font-size: 0.61rem;
letter-spacing: 0.09em;
text-transform: uppercase;
}
@media (max-width: 900px) {
html,
body {
height: auto;
overflow: auto;
}
#app {
display: block;
height: auto;
}
#viewport {
height: 62vh;
min-height: 440px;
}
#panel {
overflow: visible;
border-top: 1px solid var(--line);
border-left: 0;
padding: 25px 22px 32px;
}
.hero {
width: calc(100% - 130px);
padding: 24px 23px;
}
.hero p {
max-width: 410px;
}
}
@media (max-width: 560px) {
#viewport {
height: 58vh;
min-height: 400px;
}
.hero {
width: calc(100% - 24px);
padding: 21px 18px;
}
.hero h1 {
max-width: 310px;
font-size: 2.25rem;
}
.hero p {
max-width: 300px;
font-size: 0.78rem;
}
.scene-actions {
top: 13px;
right: 13px;
}
.status-pill {
padding: 7px 9px;
font-size: 0.59rem;
}
.icon-button {
display: none;
}
.view-hint {
left: 13px;
bottom: 13px;
}
}
</style>
</head>
<body>
<main id="app">
<section id="viewport" aria-label="Interactive three-dimensional pond food web">
<canvas
id="threeCanvas"
tabindex="0"
aria-label="Low-poly microbial pond. Drag to orbit and use the mouse wheel to zoom."
></canvas>
<div class="vignette"></div>
<header class="hero">
<div class="eyebrow">Pond ecology / interactive study 01</div>
<h1>Microbial<br />food web</h1>
<p>
Explore a stylized pond microbiome. Adjust the starting abundances,
then watch how a renewable resource and three consumers settle into
bounded, shifting trajectories.
</p>
</header>
<div class="scene-actions">
<div id="runStatus" class="status-pill"><i></i><span>Running</span></div>
<button id="resetCamera" class="icon-button" type="button">
Reset view
</button>
</div>
<div id="worldLabels" aria-hidden="true">
<div class="world-label nutrient" data-label="nutrient">
<i></i>Nutrients
</div>
<div class="world-label algae" data-label="algae">
<i></i>Algae
</div>
<div class="world-label bacteria" data-label="bacteria">
<i></i>Bacteria
</div>
<div class="world-label protozoa" data-label="protozoa">
<i></i>Protozoa
</div>
</div>
<div class="view-hint">
<span>↔</span> Drag to orbit · wheel to zoom
</div>
</section>
<aside id="panel" aria-label="Simulation controls and model information">
<div class="panel-heading">
<div>
<h2>Initial conditions</h2>
<p>Set the relative abundance at model time zero.</p>
</div>
<div class="bounded-badge">Bounded 0–100</div>
</div>
<section class="panel-section">
<div class="section-title">
<span>Starting abundance</span>
<small>relative units</small>
</div>
<label class="control" for="r0">
<span>
<i class="dot r"></i>
Nutrients
<output id="r0Value" for="r0">65</output>
</span>
<input id="r0" type="range" min="0" max="100" step="1" value="65" />
<span class="range-note"><span>0</span><span>100</span></span>
</label>
<label class="control" for="a0">
<span>
<i class="dot a"></i>
Algae
<output id="a0Value" for="a0">24</output>
</span>
<input id="a0" type="range" min="0" max="100" step="1" value="24" />
<span class="range-note"><span>0</span><span>100</span></span>
</label>
<label class="control" for="b0">
<span>
<i class="dot b"></i>
Bacteria
<output id="b0Value" for="b0">30</output>
</span>
<input id="b0" type="range" min="0" max="100" step="1" value="30" />
<span class="range-note"><span>0</span><span>100</span></span>
</label>
<label class="control" for="p0">
<span>
<i class="dot p"></i>
Protozoa
<output id="p0Value" for="p0">12</output>
</span>
<input id="p0" type="range" min="0" max="100" step="1" value="12" />
<span class="range-note"><span>0</span><span>100</span></span>
</label>
<div class="button-row">
<button id="apply" class="primary-button" type="button">
Apply & restart
</button>
<button id="pause" class="secondary-button" type="button">
Pause
</button>
</div>
<div class="utility-row">
<button id="defaults" class="text-button" type="button">
Restore defaults
</button>
<div class="speed-control" aria-label="Simulation speed">
<span>Speed</span>
<button class="speed-button" data-speed="0.5" type="button">½×</button>
<button class="speed-button active" data-speed="1" type="button">1×</button>
<button class="speed-button" data-speed="2" type="button">2×</button>
</div>
</div>
</section>
<section class="panel-section">
<div class="section-title">
<span>Current abundance</span>
<small>model time <span id="modelTime">0.0</span> min</small>
</div>
<div class="reading-grid">
<div class="reading">
<div class="reading-label"><i class="dot r"></i>Nutrients</div>
<div class="reading-value"><span id="rValue">65.0</span> <small>units</small></div>
</div>
<div class="reading">
<div class="reading-label"><i class="dot a"></i>Algae</div>
<div class="reading-value"><span id="aValue">24.0</span> <small>units</small></div>
</div>
<div class="reading">
<div class="reading-label"><i class="dot b"></i>Bacteria</div>
<div class="reading-value"><span id="bValue">30.0</span> <small>units</small></div>
</div>
<div class="reading">
<div class="reading-label"><i class="dot p"></i>Protozoa</div>
<div class="reading-value"><span id="pValue">12.0</span> <small>units</small></div>
</div>
</div>
</section>
<section class="panel-section">
<div class="section-title">
<span>Population trajectories</span>
<small>last 120 model minutes</small>
</div>
<canvas id="chart" aria-label="Chart of bounded population trajectories"></canvas>
<div class="chart-key">
<span><i style="background: var(--nutrient)"></i>Nutrients</span>
<span><i style="background: var(--algae)"></i>Algae</span>
<span><i style="background: var(--bacteria)"></i>Bacteria</span>
<span><i style="background: var(--protozoa)"></i>Protozoa</span>
</div>
</section>
<section class="panel-section">
<div class="section-title"><span>Model notes</span></div>
<label class="toggle-row">
<span>Show food-web links</span>
<input id="toggleLinks" type="checkbox" checked />
</label>
<p class="model-copy">
<strong>One generic renewable resource</strong> supports algae and
bacteria. Protozoa consume both. Density-limited growth and fixed
coefficients create bounded, coupled trajectories. A numerical guard
keeps every state in the displayed 0–100 range.
</p>
<p class="model-copy">
The 3D icons show relative abundance symbolically; they are not
one-to-one organism counts. The chart is drawn with the native Canvas
API.
</p>
<div class="disclaimer">
<strong>Ecological disclaimer</strong>
Parameter choices are illustrative and do not predict real pond
ecology. This is a teaching toy, not a calibrated ecosystem model.
</div>
</section>
</aside>
</main>
<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.querySelector("#viewport");
const canvas = document.querySelector("#threeCanvas");
const chartCanvas = document.querySelector("#chart");
const COLORS = {
R: 0xf6c85f,
A: 0x69dc83,
B: 0xe779c4,
P: 0x65c9ff
};
const CSS_COLORS = {
R: "#f6c85f",
A: "#69dc83",
B: "#e779c4",
P: "#65c9ff"
};
const DEFAULTS = {
R: 65,
A: 24,
B: 30,
P: 12
};
const sliders = {
R: document.querySelector("#r0"),
A: document.querySelector("#a0"),
B: document.querySelector("#b0"),
P: document.querySelector("#p0")
};
const outputs = {
R: document.querySelector("#r0Value"),
A: document.querySelector("#a0Value"),
B: document.querySelector("#b0Value"),
P: document.querySelector("#p0Value")
};
const valueElements = {
R: document.querySelector("#rValue"),
A: document.querySelector("#aValue"),
B: document.querySelector("#bValue"),
P: document.querySelector("#pValue")
};
const modelTimeElement = document.querySelector("#modelTime");
const runStatusElement = document.querySelector("#runStatus");
const pauseButton = document.querySelector("#pause");
const toggleLinks = document.querySelector("#toggleLinks");
let state = { ...DEFAULTS };
let running = true;
let simulationSpeed = 1;
let modelTime = 0;
let accumulator = 0;
let sampleClock = 0;
let history = [{ t: 0, ...state }];
const MODEL_DT = 0.025;
function clamp(value, min, max) {
return Math.min(max, Math.max(min, value));
}
/*
Deliberately simplified resource-consumer model.
R: one renewable, chemostat-like resource
A: algae, with resource-supported density limitation
B: bacteria, with resource-supported density limitation
P: protozoa, with prey-supported density limitation
All coefficients are fixed and illustrative, not ecological estimates.
*/
function derivative(s) {
const R = Math.max(0, s.R);
const A = Math.max(0, s.A);
const B = Math.max(0, s.B);
const P = Math.max(0, s.P);
const resourceForAlgae = 8 + 0.65 * R;
const resourceForBacteria = 5 + 0.55 * R;
const resourceForProtozoa = 4 + 0.24 * A + 0.16 * B;
return {
R:
0.16 * (80 - R)
- 0.045 * A
- 0.035 * B,
A:
0.11 * A
* (1 - (A + 0.55 * P) / resourceForAlgae)
- 0.008 * A,
B:
0.09 * B
* (1 - (B + 0.40 * P) / resourceForBacteria)
- 0.009 * B,
P:
0.075 * P
* (1 - P / resourceForProtozoa)
- 0.012 * P
};
}
function integrate(dt) {
const d = derivative(state);
state.R = clamp(state.R + d.R * dt, 0, 100);
state.A = clamp(state.A + d.A * dt, 0, 100);
state.B = clamp(state.B + d.B * dt, 0, 100);
state.P = clamp(state.P + d.P * dt, 0, 100);
}
function recordSample() {
history.push({ t: modelTime, ...state });
// Retain enough samples for a long-running session.
if (history.length > 3000) {
history.splice(0, history.length - 3000);
}
}
function updateSliderLabels() {
for (const key of Object.keys(sliders)) {
outputs[key].value = sliders[key].value;
outputs[key].textContent = sliders[key].value;
}
}
function updateRunStatus() {
runStatusElement.classList.toggle("paused", !running);
runStatusElement.querySelector("span").textContent =
running ? "Running" : "Paused";
pauseButton.textContent = running ? "Pause" : "Resume";
}
function restartSimulation() {
state = {
R: Number(sliders.R.value),
A: Number(sliders.A.value),
B: Number(sliders.B.value),
P: Number(sliders.P.value)
};
modelTime = 0;
accumulator = 0;
sampleClock = 0;
history = [{ t: 0, ...state }];
running = true;
updateRunStatus();
updateReadings();
drawChart();
}
function updateReadings() {
valueElements.R.textContent = state.R.toFixed(1);
valueElements.A.textContent = state.A.toFixed(1);
valueElements.B.textContent = state.B.toFixed(1);
valueElements.P.textContent = state.P.toFixed(1);
modelTimeElement.textContent = modelTime.toFixed(1);
}
for (const key of Object.keys(sliders)) {
sliders[key].addEventListener("input", updateSliderLabels);
}
document.querySelector("#apply").addEventListener("click", restartSimulation);
pauseButton.addEventListener("click", () => {
running = !running;
updateRunStatus();
});
document.querySelector("#defaults").addEventListener("click", () => {
for (const key of Object.keys(DEFAULTS)) {
sliders[key].value = DEFAULTS[key];
}
updateSliderLabels();
restartSimulation();
});
document.querySelectorAll(".speed-button").forEach((button) => {
button.addEventListener("click", () => {
simulationSpeed = Number(button.dataset.speed);
document
.querySelectorAll(".speed-button")
.forEach((b) => b.classList.toggle("active", b === button));
});
});
updateSliderLabels();
// ---------------------------------------------------------------------
// Three.js scene
// ---------------------------------------------------------------------
const renderer = new THREE.WebGLRenderer({
canvas,
antialias: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.12;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x061311);
scene.fog = new THREE.FogExp2(0x061311, 0.026);
const camera = new THREE.PerspectiveCamera(
43,
viewport.clientWidth / viewport.clientHeight,
0.1,
100
);
const cameraState = {
theta: 0.72,
phi: 0.88,
radius: 16.4,
targetTheta: 0.72,
targetPhi: 0.88,
targetRadius: 16.4,
target: new THREE.Vector3(0, 0.05, 0)
};
function updateCamera() {
cameraState.theta +=
(cameraState.targetTheta - cameraState.theta) * 0.09;
cameraState.phi +=
(cameraState.targetPhi - cameraState.phi) * 0.09;
cameraState.radius +=
(cameraState.targetRadius - cameraState.radius) * 0.09;
const sinPhi = Math.sin(cameraState.phi);
camera.position.set(
cameraState.target.x
+ cameraState.radius * sinPhi * Math.sin(cameraState.theta),
cameraState.target.y
+ cameraState.radius * Math.cos(cameraState.phi),
cameraState.target.z
+ cameraState.radius * sinPhi * Math.cos(cameraState.theta)
);
camera.lookAt(cameraState.target);
}
updateCamera();
const hemisphere = new THREE.HemisphereLight(
0xc4fff1,
0x06100d,
1.65
);
scene.add(hemisphere);
const sun = new THREE.DirectionalLight(0xffe8bd, 3.1);
sun.position.set(5.5, 11, 7);
sun.castShadow = true;
sun.shadow.mapSize.set(1024, 1024);
sun.shadow.camera.left = -11;
sun.shadow.camera.right = 11;
sun.shadow.camera.top = 11;
sun.shadow.camera.bottom = -11;
sun.shadow.bias = -0.0008;
scene.add(sun);
const cyanFill = new THREE.PointLight(0x42bca6, 18, 22, 2);
cyanFill.position.set(-5, 4, -4);
scene.add(cyanFill);
const ground = new THREE.Mesh(
new THREE.PlaneGeometry(70, 70),
new THREE.MeshStandardMaterial({
color: 0x071512,
roughness: 1,
flatShading: true
})
);
ground.rotation.x = -Math.PI / 2;
ground.position.y = -0.93;
ground.receiveShadow = true;
scene.add(ground);
const basin = new THREE.Mesh(
new THREE.CylinderGeometry(8.1, 8.8, 0.72, 32, 1, false),
new THREE.MeshStandardMaterial({
color: 0x18342e,
roughness: 0.94,
flatShading: true
})
);
basin.position.y = -0.55;
basin.receiveShadow = true;
basin.castShadow = true;
scene.add(basin);
const water = new THREE.Mesh(
new THREE.CircleGeometry(7.72, 40),
new THREE.MeshStandardMaterial({
color: 0x2b9c89,
roughness: 0.55,
metalness: 0.05,
transparent: true,
opacity: 0.27,
side: THREE.DoubleSide,
depthWrite: false
})
);
water.rotation.x = -Math.PI / 2;
water.position.y = -0.175;
water.receiveShadow = true;
scene.add(water);
const rim = new THREE.Mesh(
new THREE.TorusGeometry(7.92, 0.13, 6, 40),
new THREE.MeshStandardMaterial({
color: 0x527466,
roughness: 0.92,
flatShading: true
})
);
rim.rotation.x = Math.PI / 2;
rim.position.y = -0.12;
rim.castShadow = true;
scene.add(rim);
let seed = 73129;
function random() {
seed = (seed * 1664525 + 1013904223) >>> 0;
return seed / 4294967296;
}
const rockGeometry = new THREE.DodecahedronGeometry(0.42, 0);
const rockMaterial = new THREE.MeshStandardMaterial({
color: 0x415d52,
roughness: 0.95,
flatShading: true
});
for (let i = 0; i < 25; i++) {
const angle = (i / 25) * Math.PI * 2 + random() * 0.16;
const radius = 7.9 + random() * 0.8;
const rock = new THREE.Mesh(rockGeometry, rockMaterial);
rock.position.set(
Math.cos(angle) * radius,
-0.12 + random() * 0.1,
Math.sin(angle) * radius
);
rock.scale.set(
0.65 + random() * 1.2,
0.5 + random() * 0.75,
0.65 + random() * 1.1
);
rock.rotation.set(random() * 2, random() * 2, random() * 2);
rock.castShadow = true;
rock.receiveShadow = true;
scene.add(rock);
}
function makeOrganismMaterial(color) {
const base = new THREE.Color(color);
return new THREE.MeshStandardMaterial({
color: base,
emissive: base.clone().multiplyScalar(0.12),
emissiveIntensity: 0.8,
roughness: 0.62,
metalness: 0,
flatShading: true
});
}
const organismDefinitions = {
R: {
count: 24,
geometry: new THREE.OctahedronGeometry(0.09, 0),
material: makeOrganismMaterial(COLORS.R),
max: 70
},
A: {
count: 28,
geometry: new THREE.IcosahedronGeometry(0.16, 0),
material: makeOrganismMaterial(COLORS.A),
max: 50
},
B: {
count: 34,
geometry: new THREE.CylinderGeometry(0.048, 0.058, 0.29, 6),
material: makeOrganismMaterial(COLORS.B),
max: 45
},
P: {
count: 15,
geometry: new THREE.IcosahedronGeometry(0.22, 0),
material: makeOrganismMaterial(COLORS.P),
max: 25
}
};
const organisms = [];
for (const [key, definition] of Object.entries(organismDefinitions)) {
for (let i = 0; i < definition.count; i++) {
const mesh = new THREE.Mesh(definition.geometry, definition.material);
const angle = random() * Math.PI * 2;
const radius = Math.sqrt(random()) * 6.25;
const base = new THREE.Vector3(
Math.cos(angle) * radius,
-0.02 + random() * 0.52,
Math.sin(angle) * radius
);
mesh.position.copy(base);
mesh.castShadow = true;
mesh.receiveShadow = true;
mesh.userData = {
key,
base,
phase: random() * Math.PI * 2,
speed: 0.18 + random() * 0.35,
orbit: 0.06 + random() * 0.16,
currentScale: 0.8,
targetScale: 0.8
};
scene.add(mesh);
organisms.push(mesh);
}
}
// Food-web arrows and travelling energy pulses.
const linksGroup = new THREE.Group();
scene.add(linksGroup);
const linkDefinitions = [
{
from: new THREE.Vector3(-3.8, 0.9, 2.4),
to: new THREE.Vector3(-0.7, 0.85, -2.8),
color: COLORS.A
},
{
from: new THREE.Vector3(-3.8, 0.9, 2.4),
to: new THREE.Vector3(3.4, 0.85, -1.9),
color: COLORS.B
},
{
from: new THREE.Vector3(-0.7, 0.85, -2.8),
to: new THREE.Vector3(2.8, 0.95, 2.4),
color: COLORS.P
},
{
from: new THREE.Vector3(3.4, 0.85, -1.9),
to: new THREE.Vector3(2.8, 0.95, 2.4),
color: COLORS.P
}
];
const links = [];
for (const definition of linkDefinitions) {
const midpoint = definition.from
.clone()
.lerp(definition.to, 0.5);
midpoint.y += 1.15 + definition.from.distanceTo(definition.to) * 0.035;
const curve = new THREE.QuadraticBezierCurve3(
definition.from,
midpoint,
definition.to
);
const tube = new THREE.Mesh(
new THREE.TubeGeometry(curve, 24, 0.024, 5, false),
new THREE.MeshBasicMaterial({
color: definition.color,
transparent: true,
opacity: 0.48,
depthWrite: false
})
);
linksGroup.add(tube);
const arrowHead = new THREE.Mesh(
new THREE.ConeGeometry(0.105, 0.29, 6),
new THREE.MeshBasicMaterial({
color: definition.color,
transparent: true,
opacity: 0.82,
depthWrite: false
})
);
const headPosition = curve.getPointAt(0.78);
const tangent = curve.getTangentAt(0.78).normalize();
arrowHead.position.copy(headPosition);
arrowHead.quaternion.setFromUnitVectors(
new THREE.Vector3(0, 1, 0),
tangent
);
linksGroup.add(arrowHead);
const pulse = new THREE.Mesh(
new THREE.OctahedronGeometry(0.075, 0),
new THREE.MeshBasicMaterial({
color: definition.color,
transparent: true,
opacity: 0.9,
depthWrite: false
})
);
linksGroup.add(pulse);
links.push({ curve, pulse });
}
toggleLinks.addEventListener("change", () => {
linksGroup.visible = toggleLinks.checked;
});
const labelAnchors = {
nutrient: new THREE.Vector3(-3.8, 1.05, 2.4),
algae: new THREE.Vector3(-0.7, 1.0, -2.8),
bacteria: new THREE.Vector3(3.4, 1.0, -1.9),
protozoa: new THREE.Vector3(2.8, 1.1, 2.4)
};
const worldLabels = [];
document.querySelectorAll(".world-label").forEach((element) => {
worldLabels.push({
element,
anchor: labelAnchors[element.dataset.label]
});
});
const projected = new THREE.Vector3();
function updateWorldLabels() {
const width = viewport.clientWidth;
const height = viewport.clientHeight;
for (const label of worldLabels) {
projected.copy(label.anchor).project(camera);
const visible =
projected.z > -1 &&
projected.z < 1 &&
projected.x > -1.1 &&
projected.x < 1.1 &&
projected.y > -1.1 &&
projected.y < 1.1;
label.element.style.opacity = visible ? "1" : "0";
if (visible) {
label.element.style.left =
`${(projected.x * 0.5 + 0.5) * width}px`;
label.element.style.top =
`${(-projected.y * 0.5 + 0.5) * height}px`;
}
}
}
// ---------------------------------------------------------------------
// Direct camera interaction: drag to orbit, wheel/pinch to zoom.
// ---------------------------------------------------------------------
let dragging = false;
let pointerX = 0;
let pointerY = 0;
canvas.addEventListener("pointerdown", (event) => {
if (event.button !== 0) return;
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;
cameraState.targetTheta -= dx * 0.006;
cameraState.targetPhi = clamp(
cameraState.targetPhi + dy * 0.005,
0.34,
1.34
);
});
function endDrag(event) {
dragging = false;
if (event.pointerId !== undefined) {
if (canvas.hasPointerCapture(event.pointerId)) {
canvas.releasePointerCapture(event.pointerId);
}
}
}
canvas.addEventListener("pointerup", endDrag);
canvas.addEventListener("pointercancel", endDrag);
canvas.addEventListener("lostpointercapture", () => {
dragging = false;
});
canvas.addEventListener(
"wheel",
(event) => {
event.preventDefault();
cameraState.targetRadius = clamp(
cameraState.targetRadius * Math.exp(event.deltaY * 0.001),
10.5,
24
);
},
{ passive: false }
);
document.querySelector("#resetCamera").addEventListener("click", () => {
cameraState.targetTheta = 0.72;
cameraState.targetPhi = 0.88;
cameraState.targetRadius = 16.4;
});
// ---------------------------------------------------------------------
// Native Canvas trajectory chart
// ---------------------------------------------------------------------
function drawChart() {
const width = Math.max(1, chartCanvas.clientWidth);
const height = Math.max(1, chartCanvas.clientHeight);
const dpr = Math.min(window.devicePixelRatio, 2);
const targetWidth = Math.round(width * dpr);
const targetHeight = Math.round(height * dpr);
if (chartCanvas.width !== targetWidth) {
chartCanvas.width = targetWidth;
}
if (chartCanvas.height !== targetHeight) {
chartCanvas.height = targetHeight;
}
const ctx = chartCanvas.getContext("2d");
ctx.setTransform(dpr, 0, 0, dpr, 0, 0);
ctx.clearRect(0, 0, width, height);
const plot = {
left: 33,
right: 10,
top: 14,
bottom: 26
};
const plotWidth = width - plot.left - plot.right;
const plotHeight = height - plot.top - plot.bottom;
const startTime = Math.max(0, modelTime - 120);
const endTime = Math.max(30, modelTime);
const timeRange = Math.max(0.001, endTime - startTime);
function xForTime(t) {
return plot.left + ((t - startTime) / timeRange) * plotWidth;
}
function yForValue(v) {
return plot.top + (1 - clamp(v, 0, 100) / 100) * plotHeight;
}
ctx.font = "9px system-ui, sans-serif";
ctx.textAlign = "right";
ctx.textBaseline = "middle";
for (const value of [0, 25, 50, 75, 100]) {
const y = yForValue(value);
ctx.beginPath();
ctx.moveTo(plot.left, y);
ctx.lineTo(width - plot.right, y);
ctx.strokeStyle = "rgba(181, 229, 211, 0.09)";
ctx.lineWidth = 1;
ctx.stroke();
ctx.fillStyle = "#617d73";
ctx.fillText(String(value), plot.left - 7, y);
}
ctx.textAlign = "center";
ctx.textBaseline = "top";
for (let i = 0; i <= 4; i++) {
const t = startTime + (timeRange * i) / 4;
const x = xForTime(t);
ctx.fillStyle = "#617d73";
ctx.fillText(`${t.toFixed(0)}m`, x, height - plot.bottom + 8);
}
ctx.save();
ctx.beginPath();
ctx.rect(
plot.left,
plot.top,
plotWidth,
plotHeight
);
ctx.clip();
for (const key of ["R", "A", "B", "P"]) {
ctx.beginPath();
let started = false;
for (const sample of history) {
if (sample.t < startTime - 0.5) continue;
const x = xForTime(sample.t);
const y = yForValue(sample[key]);
if (!started) {
ctx.moveTo(x, y);
started = true;
} else {
ctx.lineTo(x, y);
}
}
ctx.strokeStyle = CSS_COLORS[key];
ctx.lineWidth = key === "R" ? 1.7 : 2;
ctx.lineJoin = "round";
ctx.lineCap = "round";
ctx.stroke();
const last = history[history.length - 1];
ctx.beginPath();
ctx.arc(
xForTime(last.t),
yForValue(last[key]),
2.7,
0,
Math.PI * 2
);
ctx.fillStyle = CSS_COLORS[key];
ctx.fill();
}
ctx.restore();
}
// ---------------------------------------------------------------------
// Resize and animation
// ---------------------------------------------------------------------
function resizeThree() {
const width = Math.max(1, viewport.clientWidth);
const height = Math.max(1, viewport.clientHeight);
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(width, height, false);
camera.aspect = width / height;
camera.updateProjectionMatrix();
}
const resizeObserver = new ResizeObserver(() => {
resizeThree();
drawChart();
});
resizeObserver.observe(viewport);
resizeThree();
drawChart();
window.addEventListener("resize", drawChart);
function updateOrganisms(elapsed) {
for (const mesh of organisms) {
const { key, base, phase, speed, orbit } = mesh.userData;
const population = state[key];
const maxPopulation = organismDefinitions[key].max;
const targetScale =
population <= 0.05
? 0
: 0.32 + 1.03 * Math.sqrt(
clamp(population / maxPopulation, 0, 1)
);
mesh.userData.currentScale = THREE.MathUtils.lerp(
mesh.userData.currentScale,
targetScale,
0.09
);
mesh.visible = population > 0.05;
mesh.scale.setScalar(mesh.userData.currentScale);
mesh.position.x =
base.x + Math.sin(elapsed * speed + phase) * orbit;
mesh.position.z =
base.z + Math.cos(elapsed * speed * 0.87 + phase) * orbit;
mesh.position.y =
base.y + Math.sin(elapsed * 0.55 + phase) * 0.055;
if (key === "B") {
mesh.rotation.set(
0,
phase + elapsed * 0.38,
Math.PI / 2
);
} else {
mesh.rotation.set(
phase * 0.4 + elapsed * 0.13,
phase + elapsed * 0.19,
phase * 0.2
);
}
}
}
function updateLinks(elapsed) {
links.forEach((link, index) => {
const t = (elapsed * 0.22 + index * 0.27) % 1;
const point = link.curve.getPointAt(t);
link.pulse.position.copy(point);
const pulse = 0.72 + Math.sin(t * Math.PI) * 0.85;
link.pulse.scale.setScalar(pulse);
});
}
const clock = new THREE.Clock();
let lastChartTime = 0;
function animate() {
requestAnimationFrame(animate);
const realDelta = Math.min(clock.getDelta(), 0.05);
const elapsed = clock.elapsedTime;
if (running) {
accumulator += realDelta * simulationSpeed;
let steps = 0;
while (accumulator >= MODEL_DT && steps < 24) {
integrate(MODEL_DT);
modelTime += MODEL_DT;
sampleClock += MODEL_DT;
if (sampleClock >= 0.25 - 1e-8) {
sampleClock -= 0.25;
recordSample();
}
accumulator -= MODEL_DT;
steps++;
}
}
updateCamera();
updateOrganisms(elapsed);
updateLinks(elapsed);
updateWorldLabels();
if (elapsed - lastChartTime > 0.1) {
updateReadings();
drawChart();
lastChartTime = elapsed;
}
renderer.render(scene, camera);
}
updateReadings();
updateRunStatus();
animate();
</script>
</body>
</html>
```