Prompt specification
Build or customize the Three.js 3D scene "Delayed Consequences of One Intervention" 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: Delayed Consequences of One Intervention
Category: Games & Play · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/delayed-consequences-of-one-intervention-x3536
Scene Description Prompt:
Compare delayed intervention effects in low-poly toy ecosystems starting identically, with explicit model assumptions. One HTML file using Three.js from cdnjs.
Working Three.js Starter (delayed-consequences-of-one-intervention-x3536):
```html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1">
<title>Intervention Lab — Low-Poly Ecosystems</title>
<style>
:root {
color-scheme: dark;
--bg: #09111f;
--panel: #111c2e;
--panel-2: #17243a;
--line: #2a3a54;
--text: #eef4ff;
--muted: #9eb0c9;
--control: #9aabc2;
--immediate: #ffb15f;
--delayed: #63e6cf;
--danger: #ff7f88;
}
* { box-sizing: border-box; }
body {
margin: 0;
min-height: 100vh;
color: var(--text);
background:
radial-gradient(circle at 15% 0%, #1b3150 0, transparent 35%),
radial-gradient(circle at 90% 10%, #173d3e 0, transparent 30%),
var(--bg);
font: 14px/1.45 Inter, ui-sans-serif, system-ui, -apple-system,
BlinkMacSystemFont, "Segoe UI", sans-serif;
}
button, input, select { font: inherit; }
button, select {
color: var(--text);
background: #1b2940;
border: 1px solid #344863;
border-radius: 9px;
}
button {
cursor: pointer;
padding: 9px 13px;
transition: background .15s, border-color .15s, transform .15s;
}
button:hover {
background: #263751;
border-color: #56708f;
}
button:active { transform: translateY(1px); }
button.primary {
color: #07131d;
background: var(--delayed);
border-color: var(--delayed);
font-weight: 750;
}
button.primary:hover { background: #8af2df; }
button.small {
padding: 6px 10px;
font-size: 12px;
}
.app {
width: min(1540px, calc(100% - 28px));
margin: 0 auto;
padding: 26px 0 48px;
}
header.page {
display: flex;
justify-content: space-between;
align-items: end;
gap: 24px;
margin-bottom: 20px;
}
.eyebrow {
margin: 0 0 7px;
color: var(--delayed);
font-size: 11px;
font-weight: 800;
letter-spacing: .16em;
text-transform: uppercase;
}
h1 {
margin: 0;
font-size: clamp(27px, 4vw, 45px);
line-height: 1.06;
letter-spacing: -.035em;
}
.subtitle {
max-width: 760px;
margin: 10px 0 0;
color: var(--muted);
font-size: 14px;
}
.clock {
flex: 0 0 auto;
min-width: 125px;
padding: 10px 15px;
text-align: right;
border: 1px solid var(--line);
border-radius: 12px;
background: rgba(17, 28, 46, .78);
}
.clock strong {
display: block;
font-size: 25px;
font-variant-numeric: tabular-nums;
}
.clock span {
color: var(--muted);
font-size: 11px;
text-transform: uppercase;
letter-spacing: .1em;
}
.toolbar {
display: flex;
flex-wrap: wrap;
align-items: center;
gap: 12px;
padding: 12px;
margin-bottom: 16px;
border: 1px solid var(--line);
border-radius: 14px;
background: rgba(17, 28, 46, .88);
backdrop-filter: blur(10px);
}
.control-group {
display: flex;
align-items: center;
gap: 9px;
padding-right: 12px;
border-right: 1px solid var(--line);
}
.control-group:last-child {
padding-right: 0;
border-right: 0;
}
.control-group label {
color: var(--muted);
white-space: nowrap;
}
input[type="range"] {
width: 135px;
accent-color: var(--delayed);
}
.value {
min-width: 56px;
color: var(--text);
font-weight: 750;
font-variant-numeric: tabular-nums;
}
select { padding: 8px 10px; }
.hint {
margin-left: auto;
color: var(--muted);
font-size: 12px;
}
.cards {
display: grid;
grid-template-columns: repeat(3, minmax(0, 1fr));
gap: 14px;
}
.card {
overflow: hidden;
border: 1px solid var(--line);
border-radius: 16px;
background: rgba(17, 28, 46, .95);
box-shadow: 0 16px 38px rgba(0, 0, 0, .15);
}
.card.immediate { border-top: 3px solid var(--immediate); }
.card.delayed { border-top: 3px solid var(--delayed); }
.card.control { border-top: 3px solid var(--control); }
.card-head {
display: flex;
justify-content: space-between;
align-items: center;
gap: 12px;
padding: 13px 14px 11px;
}
.card-title {
display: flex;
align-items: center;
gap: 9px;
font-weight: 800;
}
.dot {
width: 9px;
height: 9px;
border-radius: 50%;
background: var(--control);
box-shadow: 0 0 0 4px rgba(154, 171, 194, .10);
}
.immediate .dot { background: var(--immediate); }
.delayed .dot { background: var(--delayed); }
.badge {
max-width: 180px;
padding: 4px 8px;
border-radius: 999px;
color: var(--muted);
background: #0c1626;
font-size: 11px;
text-align: center;
}
.badge.armed { color: var(--delayed); }
.badge.applied { color: #a6f3df; }
.viewport {
position: relative;
height: 300px;
overflow: hidden;
background: #122036;
}
.viewport canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
cursor: grab;
}
.viewport canvas:active { cursor: grabbing; }
.camera-hint {
position: absolute;
left: 10px;
bottom: 8px;
padding: 4px 7px;
color: rgba(239, 246, 255, .66);
background: rgba(4, 9, 16, .48);
border-radius: 6px;
font-size: 10px;
pointer-events: none;
}
.metrics {
display: grid;
grid-template-columns: repeat(3, 1fr);
border-top: 1px solid var(--line);
}
.metric {
padding: 10px 13px;
border-right: 1px solid var(--line);
}
.metric:last-child { border-right: 0; }
.metric span {
display: block;
margin-bottom: 3px;
color: var(--muted);
font-size: 10px;
font-weight: 750;
letter-spacing: .09em;
text-transform: uppercase;
}
.metric strong {
font-size: 19px;
font-variant-numeric: tabular-nums;
}
.panel {
margin-top: 16px;
padding: 16px;
border: 1px solid var(--line);
border-radius: 14px;
background: rgba(17, 28, 46, .92);
}
.panel-head {
display: flex;
justify-content: space-between;
align-items: center;
gap: 12px;
margin-bottom: 11px;
}
.panel h2 {
margin: 0;
font-size: 16px;
}
.tabs {
display: flex;
gap: 5px;
}
.tabs button.active {
color: #07131d;
background: var(--delayed);
border-color: var(--delayed);
}
.chart-wrap {
position: relative;
height: 255px;
}
#chart {
display: block;
width: 100%;
height: 100%;
}
.legend {
display: flex;
flex-wrap: wrap;
gap: 17px;
margin-top: 8px;
color: var(--muted);
font-size: 12px;
}
.legend span {
display: inline-flex;
align-items: center;
gap: 6px;
}
.legend i {
width: 18px;
height: 3px;
border-radius: 2px;
background: currentColor;
}
.legend .l-control { color: var(--control); }
.legend .l-immediate { color: var(--immediate); }
.legend .l-delayed { color: var(--delayed); }
.outcomes {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 11px;
margin-top: 13px;
}
.outcome {
padding: 12px;
border: 1px solid var(--line);
border-radius: 10px;
background: #0d1829;
}
.outcome span {
display: block;
color: var(--muted);
font-size: 11px;
text-transform: uppercase;
letter-spacing: .07em;
}
.outcome strong {
display: block;
margin: 4px 0;
font-size: 19px;
font-variant-numeric: tabular-nums;
}
.outcome small {
color: var(--muted);
}
details {
margin-top: 16px;
border: 1px solid var(--line);
border-radius: 14px;
background: rgba(17, 28, 46, .92);
}
summary {
cursor: pointer;
padding: 15px 17px;
font-weight: 800;
}
.assumptions {
display: grid;
grid-template-columns: 1.25fr .75fr;
gap: 18px;
padding: 0 17px 17px;
color: var(--muted);
}
.equations {
padding: 13px;
border: 1px solid var(--line);
border-radius: 10px;
background: #0d1829;
color: var(--text);
font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
font-size: 13px;
}
.equations div { margin: 5px 0; }
.assumptions ul {
margin: 0;
padding-left: 19px;
}
.assumptions li { margin: 6px 0; }
.parameter-table {
width: 100%;
border-collapse: collapse;
margin-top: 13px;
color: var(--muted);
font-size: 12px;
}
.parameter-table td {
padding: 6px 8px;
border-top: 1px solid var(--line);
}
.parameter-table td:last-child {
text-align: right;
color: var(--text);
font-variant-numeric: tabular-nums;
}
footer {
margin-top: 16px;
color: var(--muted);
font-size: 12px;
text-align: center;
}
@media (max-width: 980px) {
.cards { grid-template-columns: 1fr; }
.viewport { height: 340px; }
.assumptions { grid-template-columns: 1fr; }
}
@media (max-width: 680px) {
header.page { align-items: start; }
.clock { display: none; }
.toolbar { align-items: stretch; }
.control-group {
width: 100%;
padding: 0 0 9px;
border-right: 0;
border-bottom: 1px solid var(--line);
}
.control-group:last-child { border-bottom: 0; }
input[type="range"] { flex: 1; }
.hint { display: none; }
.outcomes { grid-template-columns: 1fr; }
.panel-head { align-items: flex-start; flex-direction: column; }
.viewport { height: 290px; }
}
</style>
</head>
<body>
<main class="app">
<header class="page">
<div>
<p class="eyebrow">Deterministic predator–prey laboratory</p>
<h1>Does intervention timing matter?</h1>
<p class="subtitle">
Three low-poly ecosystems begin with exactly the same pre-intervention
state. Compare no action, an immediate fox cull, and the same cull
applied after a configurable delay.
</p>
</div>
<div class="clock">
<strong id="clock">0.00 yr</strong>
<span>simulation time</span>
</div>
</header>
<section class="toolbar" aria-label="Simulation controls">
<div class="control-group">
<button id="play" class="primary">Pause</button>
<button id="reset">Reset</button>
</div>
<div class="control-group">
<label for="delay">Delayed action</label>
<input id="delay" type="range" min="2" max="20" step="1" value="8">
<span id="delayValue" class="value">8 yr</span>
</div>
<div class="control-group">
<label for="cull">Foxes removed</label>
<input id="cull" type="range" min="25" max="75" step="5" value="55">
<span id="cullValue" class="value">55%</span>
</div>
<div class="control-group">
<label for="speed">Speed</label>
<select id="speed">
<option value="0.5">0.5×</option>
<option value="1" selected>1×</option>
<option value="2">2×</option>
<option value="4">4×</option>
</select>
</div>
<div class="hint">Drag a scene to orbit · Scroll to zoom</div>
</section>
<section class="cards">
<article class="card control">
<div class="card-head">
<div class="card-title"><i class="dot"></i>Control</div>
<div id="badge-control" class="badge">No intervention</div>
</div>
<div id="view-control" class="viewport">
<div class="camera-hint">drag to orbit · wheel to zoom</div>
</div>
<div class="metrics">
<div class="metric"><span>Grass</span><strong id="control-G">100.0</strong></div>
<div class="metric"><span>Rabbits</span><strong id="control-R">30.0</strong></div>
<div class="metric"><span>Foxes</span><strong id="control-F">8.0</strong></div>
</div>
</article>
<article class="card immediate">
<div class="card-head">
<div class="card-title"><i class="dot"></i>Immediate</div>
<div id="badge-immediate" class="badge">Cull at 0.0 yr</div>
</div>
<div id="view-immediate" class="viewport">
<div class="camera-hint">drag to orbit · wheel to zoom</div>
</div>
<div class="metrics">
<div class="metric"><span>Grass</span><strong id="immediate-G">100.0</strong></div>
<div class="metric"><span>Rabbits</span><strong id="immediate-R">30.0</strong></div>
<div class="metric"><span>Foxes</span><strong id="immediate-F">3.6</strong></div>
</div>
</article>
<article class="card delayed">
<div class="card-head">
<div class="card-title"><i class="dot"></i>Delayed</div>
<div id="badge-delayed" class="badge armed">Cull at 8.0 yr · armed</div>
</div>
<div id="view-delayed" class="viewport">
<div class="camera-hint">drag to orbit · wheel to zoom</div>
</div>
<div class="metrics">
<div class="metric"><span>Grass</span><strong id="delayed-G">100.0</strong></div>
<div class="metric"><span>Rabbits</span><strong id="delayed-R">30.0</strong></div>
<div class="metric"><span>Foxes</span><strong id="delayed-F">8.0</strong></div>
</div>
</article>
</section>
<section class="panel">
<div class="panel-head">
<h2>Population trajectories</h2>
<div class="tabs" aria-label="Chart population">
<button class="small active" data-chart="R">Rabbits</button>
<button class="small" data-chart="F">Foxes</button>
<button class="small" data-chart="G">Grass</button>
</div>
</div>
<div class="chart-wrap">
<canvas id="chart"></canvas>
</div>
<div class="legend">
<span class="l-control"><i></i>Control</span>
<span class="l-immediate"><i></i>Immediate cull</span>
<span class="l-delayed"><i></i>Delayed cull</span>
</div>
<div class="outcomes">
<div class="outcome">
<span>Rabbit gap now</span>
<strong id="rabbitGap">Immediate +0.0 · Delayed +0.0</strong>
<small>Compared with the no-intervention world.</small>
</div>
<div class="outcome">
<span>At delayed action + 5 years</span>
<strong id="fiveYearGap">Pending</strong>
<small id="fiveYearSub">Available after year 13.</small>
</div>
<div class="outcome">
<span>Delayed versus immediate</span>
<strong id="timingGap">Pending</strong>
<small id="timingSub">Compared five years after the delayed action.</small>
</div>
</div>
</section>
<details open>
<summary>Model assumptions, intervention rule, and limitations</summary>
<div class="assumptions">
<div>
<div class="equations">
<div>dG/dt = rG·G·(1 − G/K) − a·G·R</div>
<div>dR/dt = b·G·R − dR·R − c·R·F</div>
<div>dF/dt = e·c·R·F − m·F</div>
</div>
<table class="parameter-table">
<tr><td>Initial grass G</td><td>100</td></tr>
<tr><td>Initial rabbits R</td><td>30</td></tr>
<tr><td>Initial foxes F</td><td>8</td></tr>
<tr><td>Grass carrying capacity K</td><td>120</td></tr>
<tr><td>Grass growth rG</td><td>0.18</td></tr>
<tr><td>Grazing coefficient a</td><td>0.0016</td></tr>
<tr><td>Rabbit birth coefficient b</td><td>0.0016</td></tr>
<tr><td>Rabbit mortality dR</td><td>0.14</td></tr>
<tr><td>Fox predation coefficient c</td><td>0.002</td></tr>
<tr><td>Predation conversion e</td><td>1</td></tr>
<tr><td>Fox mortality m</td><td>0.07</td></tr>
<tr><td>Numerical step</td><td>0.02 yr</td></tr>
<tr><td>Integrator</td><td>Fixed-step RK4</td></tr>
</table>
</div>
<ul>
<li>
All worlds use the same deterministic equations, parameters,
time step, and pre-intervention initial conditions.
</li>
<li>
The intervention removes a fixed percentage of foxes. It does not
add food, alter parameters, relocate foxes, or trigger compensatory
reproduction.
</li>
<li>
The immediate world applies the cull at year 0. The delayed world
applies the identical cull when the selected year is reached.
Before that time, the delayed and control trajectories are identical.
</li>
<li>
There is no randomness, immigration, emigration, seasonality,
habitat spatial structure, age structure, disease, or human demand.
</li>
<li>
Grass has logistic regrowth; rabbit and fox populations are
continuous compartment values. The visible animal counts are
rounded for display, not a separate agent-based population.
</li>
<li>
Coefficients are intentionally dimensionless “toy” values. The
result demonstrates phase-dependent intervention effects, not a
calibrated prediction for a real ecosystem.
</li>
</ul>
</div>
</details>
<footer>
The dashed chart line marks the delayed intervention. Values after an
intervention depend on where the predator–prey cycle happens to be at
that moment.
</footer>
</main>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
/* -------------------------------------------------------------------------- */
/* Model */
/* -------------------------------------------------------------------------- */
const P = Object.freeze({
K: 120,
rG: 0.18,
a: 0.0016,
b: 0.0016,
dR: 0.14,
c: 0.002,
e: 1,
m: 0.07,
dt: 0.02
});
const INITIAL = Object.freeze({ G: 100, R: 30, F: 8 });
let delayYears = 8;
let cullFraction = 0.55;
let simulationSpeed = 1;
let running = true;
let chartMode = "R";
function derivative(s) {
const { G, R, F } = s;
return {
G: P.rG * G * (1 - G / P.K) - P.a * G * R,
R: P.b * G * R - P.dR * R - P.c * R * F,
F: P.e * P.c * R * F - P.m * F
};
}
function addState(s, d, h) {
return {
G: s.G + d.G * h,
R: s.R + d.R * h,
F: s.F + d.F * h
};
}
class EcosystemModel {
constructor(kind) {
this.kind = kind;
this.time = 0;
this.state = { ...INITIAL };
this.history = [{
t: 0,
G: this.state.G,
R: this.state.R,
F: this.state.F
}];
this.nextSample = 0.1;
if (kind === "immediate") {
this.applyCull();
}
}
applyCull() {
if (this.applied) return;
this.state.F *= 1 - cullFraction;
this.applied = true;
}
step(dt) {
if (
this.kind === "delayed" &&
!this.applied &&
this.time + 1e-9 >= delayYears
) {
this.applyCull();
}
const y0 = { ...this.state };
const k1 = derivative(y0);
const k2 = derivative(addState(y0, k1, dt / 2));
const k3 = derivative(addState(y0, k2, dt / 2));
const k4 = derivative(addState(y0, k3, dt));
this.state = {
G: y0.G + dt * (k1.G + 2 * k2.G + 2 * k3.G + k4.G) / 6,
R: y0.R + dt * (k1.R + 2 * k2.R + 2 * k3.R + k4.R) / 6,
F: y0.F + dt * (k1.F + 2 * k2.F + 2 * k3.F + k4.F) / 6
};
this.state.G = Math.max(0, this.state.G);
this.state.R = Math.max(0, this.state.R);
this.state.F = Math.max(0, this.state.F);
this.time += dt;
if (this.time + 1e-9 >= this.nextSample) {
this.history.push({
t: this.time,
G: this.state.G,
R: this.state.R,
F: this.state.F
});
this.nextSample += 0.1;
}
}
sampleAt(t) {
const h = this.history;
if (t <= h[0].t) return { ...h[0] };
if (t >= h[h.length - 1].t) return { ...h[h.length - 1] };
let lo = 0;
let hi = h.length - 1;
while (hi - lo > 1) {
const mid = (lo + hi) >> 1;
if (h[mid].t <= t) lo = mid;
else hi = mid;
}
const a = h[lo];
const b = h[hi];
const f = (t - a.t) / Math.max(1e-9, b.t - a.t);
return {
t,
G: a.G + (b.G - a.G) * f,
R: a.R + (b.R - a.R) * f,
F: a.F + (b.F - a.F) * f
};
}
}
let models = [];
function makeModels() {
models = [
new EcosystemModel("control"),
new EcosystemModel("immediate"),
new EcosystemModel("delayed")
];
for (const world of worlds) {
world.model = models.find(m => m.kind === world.kind);
}
}
/* -------------------------------------------------------------------------- */
/* Direct camera controls — no Three.js examples/addons */
/* -------------------------------------------------------------------------- */
class CameraController {
constructor(camera, canvas) {
this.camera = camera;
this.canvas = canvas;
this.target = new THREE.Vector3(0, 0.45, 0);
this.azimuth = 0.68;
this.polar = 0.82;
this.radius = 10.7;
this.minRadius = 7;
this.maxRadius = 17;
this.minPolar = 0.28;
this.maxPolar = 1.38;
this.dragging = false;
this.pointerX = 0;
this.pointerY = 0;
this.onDown = e => {
this.dragging = true;
this.pointerX = e.clientX;
this.pointerY = e.clientY;
canvas.setPointerCapture(e.pointerId);
};
this.onMove = e => {
if (!this.dragging) return;
const dx = e.clientX - this.pointerX;
const dy = e.clientY - this.pointerY;
this.pointerX = e.clientX;
this.pointerY = e.clientY;
this.azimuth -= dx * 0.006;
this.polar = THREE.MathUtils.clamp(
this.polar - dy * 0.005,
this.minPolar,
this.maxPolar
);
this.updateCamera();
};
this.onUp = e => {
this.dragging = false;
if (canvas.hasPointerCapture(e.pointerId)) {
canvas.releasePointerCapture(e.pointerId);
}
};
this.onWheel = e => {
e.preventDefault();
this.radius = THREE.MathUtils.clamp(
this.radius * Math.exp(e.deltaY * 0.001),
this.minRadius,
this.maxRadius
);
this.updateCamera();
};
this.onDouble = () => {
this.azimuth = 0.68;
this.polar = 0.82;
this.radius = 10.7;
this.updateCamera();
};
canvas.addEventListener("pointerdown", this.onDown);
canvas.addEventListener("pointermove", this.onMove);
canvas.addEventListener("pointerup", this.onUp);
canvas.addEventListener("pointercancel", this.onUp);
canvas.addEventListener("wheel", this.onWheel, { passive: false });
canvas.addEventListener("dblclick", this.onDouble);
this.updateCamera();
}
updateCamera() {
const sinPolar = Math.sin(this.polar);
this.camera.position.set(
this.target.x + this.radius * sinPolar * Math.sin(this.azimuth),
this.target.y + this.radius * Math.cos(this.polar),
this.target.z + this.radius * sinPolar * Math.cos(this.azimuth)
);
this.camera.lookAt(this.target);
}
}
/* -------------------------------------------------------------------------- */
/* Low-poly world */
/* -------------------------------------------------------------------------- */
const MAX_PLANTS = 90;
const MAX_RABBITS = 85;
const MAX_FOXES = 32;
function seededRandom(seed) {
let value = seed >>> 0;
return () => {
value = (1664525 * value + 1013904223) >>> 0;
return value / 4294967296;
};
}
function standardMaterial(color, extra = {}) {
return new THREE.MeshStandardMaterial({
color,
roughness: 0.88,
metalness: 0,
flatShading: true,
...extra
});
}
function makeInstanced(scene, geometry, material, count) {
const mesh = new THREE.InstancedMesh(geometry, material, count);
mesh.instanceMatrix.setUsage(THREE.DynamicDrawUsage);
mesh.castShadow = true;
mesh.receiveShadow = true;
mesh.frustumCulled = false;
scene.add(mesh);
return mesh;
}
class ToyWorld {
constructor(kind, container) {
this.kind = kind;
this.container = container;
this.scene = new THREE.Scene();
this.scene.background = new THREE.Color(0x122036);
this.scene.fog = new THREE.Fog(0x122036, 11, 20);
this.renderer = new THREE.WebGLRenderer({
antialias: true,
alpha: false,
powerPreference: "high-performance"
});
this.renderer.setPixelRatio(Math.min(window.devicePixelRatio, 1.8));
this.renderer.outputColorSpace = THREE.SRGBColorSpace;
this.renderer.toneMapping = THREE.ACESFilmicToneMapping;
this.renderer.toneMappingExposure = 1.18;
this.renderer.shadowMap.enabled = true;
this.renderer.shadowMap.type = THREE.PCFSoftShadowMap;
container.prepend(this.renderer.domElement);
this.camera = new THREE.PerspectiveCamera(38, 1, 0.1, 50);
this.controls = new CameraController(this.camera, this.renderer.domElement);
this.dummy = new THREE.Object3D();
this.up = new THREE.Vector3(0, 1, 0);
this.tailDirection = new THREE.Vector3();
this.buildScene();
this.buildCreatures();
this.resizeObserver = new ResizeObserver(() => this.resize());
this.resizeObserver.observe(container);
this.resize();
}
buildScene() {
const scene = this.scene;
scene.add(new THREE.HemisphereLight(0xcce8ff, 0x30452e, 2.1));
const sun = new THREE.DirectionalLight(0xfff0d4, 3.0);
sun.position.set(-4, 9, 5);
sun.castShadow = true;
sun.shadow.mapSize.set(1024, 1024);
sun.shadow.camera.left = -7;
sun.shadow.camera.right = 7;
sun.shadow.camera.top = 7;
sun.shadow.camera.bottom = -7;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 22;
sun.shadow.bias = -0.0007;
scene.add(sun);
const rim = new THREE.DirectionalLight(0x73d7ff, 1.1);
rim.position.set(6, 4, -6);
scene.add(rim);
const base = new THREE.Mesh(
new THREE.CylinderGeometry(4.85, 5.2, 0.58, 12),
standardMaterial(0x4d5c47)
);
base.position.y = -0.31;
base.receiveShadow = true;
base.castShadow = true;
scene.add(base);
const top = new THREE.Mesh(
new THREE.CylinderGeometry(4.82, 4.82, 0.12, 32),
standardMaterial(0x78966d)
);
top.position.y = 0.01;
top.receiveShadow = true;
scene.add(top);
const lowerSoil = new THREE.Mesh(
new THREE.CylinderGeometry(4.87, 4.87, 0.18, 32),
standardMaterial(0x6b4f38)
);
lowerSoil.position.y = -0.12;
lowerSoil.receiveShadow = true;
scene.add(lowerSoil);
// A shallow, non-ecological decorative pond.
const pond = new THREE.Mesh(
new THREE.CircleGeometry(0.78, 18),
standardMaterial(0x4c9caf, { roughness: 0.42 })
);
pond.rotation.x = -Math.PI / 2;
pond.position.set(1.9, 0.075, -1.7);
pond.scale.y = 0.62;
pond.receiveShadow = true;
scene.add(pond);
const rand = seededRandom(1217);
const rockGeometry = new THREE.DodecahedronGeometry(0.16, 0);
const rockMaterial = standardMaterial(0x9aa39d);
for (let i = 0; i < 10; i++) {
const angle = rand() * Math.PI * 2;
const radius = 3.3 + rand() * 1.1;
const rock = new THREE.Mesh(rockGeometry, rockMaterial);
rock.position.set(
Math.cos(angle) * radius,
0.15,
Math.sin(angle) * radius
);
rock.scale.set(
0.65 + rand() * 0.9,
0.45 + rand() * 0.65,
0.65 + rand() * 0.9
);
rock.rotation.set(rand(), rand() * Math.PI, rand());
rock.castShadow = true;
rock.receiveShadow = true;
scene.add(rock);
}
this.plantMesh = makeInstanced(
scene,
new THREE.ConeGeometry(0.22, 0.82, 5),
standardMaterial(0xffffff),
MAX_PLANTS
);
this.plantData = [];
const plantRand = seededRandom(441);
for (let i = 0; i < MAX_PLANTS; i++) {
const angle = plantRand() * Math.PI * 2;
const radius = Math.sqrt(plantRand()) * 3.9;
const x = Math.cos(angle) * radius;
const z = Math.sin(angle) * radius;
// Keep the decorative pond mostly clear.
const nearPond = (x - 1.9) ** 2 + ((z + 1.7) / 0.65) ** 2 < 0.7;
if (nearPond) {
this.plantData.push({ x: -3.6, z: -3.6, scale: 0.1, tint: 0.4 });
continue;
}
this.plantData.push({
x,
z,
scale: 0.72 + plantRand() * 0.62,
tint: plantRand()
});
const color = new THREE.Color().setHSL(
0.25 + plantRand() * 0.055,
0.38 + plantRand() * 0.20,
0.36 + plantRand() * 0.16
);
this.plantMesh.setColorAt(i, color);
}
if (this.plantMesh.instanceColor) {
this.plantMesh.instanceColor.needsUpdate = true;
}
}
buildCreatures() {
const scene = this.scene;
this.rabbitBody = makeInstanced(
scene,
new THREE.IcosahedronGeometry(0.28, 0),
standardMaterial(0xe9d9c5),
MAX_RABBITS
);
this.rabbitHead = makeInstanced(
scene,
new THREE.IcosahedronGeometry(0.19, 0),
standardMaterial(0xf1e5d5),
MAX_RABBITS
);
this.rabbitEars = makeInstanced(
scene,
new THREE.ConeGeometry(0.065, 0.29, 4),
standardMaterial(0xdcaeaa),
MAX_RABBITS * 2
);
this.foxBody = makeInstanced(
scene,
new THREE.IcosahedronGeometry(0.34, 0),
standardMaterial(0xd96732),
MAX_FOXES
);
this.foxHead = makeInstanced(
scene,
new THREE.IcosahedronGeometry(0.21, 0),
standardMaterial(0xe27b3c),
MAX_FOXES
);
this.foxTail = makeInstanced(
scene,
new THREE.ConeGeometry(0.15, 0.52, 5),
standardMaterial(0x7f3827),
MAX_FOXES
);
this.foxEars = makeInstanced(
scene,
new THREE.ConeGeometry(0.085, 0.22, 4),
standardMaterial(0x9a432a),
MAX_FOXES * 2
);
const rand = seededRandom(9001);
this.rabbitData = Array.from({ length: MAX_RABBITS }, () => {
const angle = rand() * Math.PI * 2;
const radius = 0.4 + Math.sqrt(rand()) * 3.25;
return {
x: Math.cos(angle) * radius,
z: Math.sin(angle) * radius,
angle,
speed: (0.22 + rand() * 0.22) * (rand() > 0.5 ? 1 : -1),
phase: rand() * Math.PI * 2,
scale: 0.82 + rand() * 0.25
};
});
this.foxData = Array.from({ length: MAX_FOXES }, () => {
const angle = rand() * Math.PI * 2;
const radius = 0.7 + Math.sqrt(rand()) * 2.8;
return {
x: Math.cos(angle) * radius,
z: Math.sin(angle) * radius,
angle,
speed: (0.16 + rand() * 0.16) * (rand() > 0.5 ? 1 : -1),
phase: rand() * Math.PI * 2,
scale: 0.86 + rand() * 0.22
};
});
const rabbitColor = new THREE.Color();
for (let i = 0; i < MAX_RABBITS; i++) {
rabbitColor.setHSL(
0.08,
0.28,
0.78 + (i % 4) * 0.025
);
this.rabbitBody.setColorAt(i, rabbitColor);
this.rabbitHead.setColorAt(i, rabbitColor);
}
const foxColor = new THREE.Color();
for (let i = 0; i < MAX_FOXES; i++) {
foxColor.setHSL(
0.055 + (i % 3) * 0.008,
0.68,
0.49 + (i % 2) * 0.035
);
this.foxBody.setColorAt(i, foxColor);
this.foxHead.setColorAt(i, foxColor);
}
if (this.rabbitBody.instanceColor) {
this.rabbitBody.instanceColor.needsUpdate = true;
this.rabbitHead.instanceColor.needsUpdate = true;
}
if (this.foxBody.instanceColor) {
this.foxBody.instanceColor.needsUpdate = true;
this.foxHead.instanceColor.needsUpdate = true;
}
}
resize() {
const width = Math.max(1, this.container.clientWidth);
const height = Math.max(1, this.container.clientHeight);
this.renderer.setSize(width, height, false);
this.camera.aspect = width / height;
this.camera.updateProjectionMatrix();
}
setInstance(mesh, index, position, rotation, scale) {
this.dummy.position.set(position.x, position.y, position.z);
this.dummy.rotation.set(rotation.x, rotation.y, rotation.z);
this.dummy.scale.set(scale.x, scale.y, scale.z);
this.dummy.updateMatrix();
mesh.setMatrixAt(index, this.dummy.matrix);
}
update() {
const s = this.model.state;
const t = this.model.time;
const plantCount = Math.min(
MAX_PLANTS,
Math.round(MAX_PLANTS * Math.max(0, Math.min(1, s.G / P.K)))
);
const biomassScale = 0.58 + 0.60 * Math.max(0, Math.min(1.2, s.G / P.K));
for (let i = 0; i < MAX_PLANTS; i++) {
const p = this.plantData[i];
const visible = i < plantCount;
this.setInstance(
this.plantMesh,
i,
{ x: p.x, y: visible ? 0.41 * biomassScale * p.scale : -5, z: p.z },
{ x: 0, y: p.tint * Math.PI * 2, z: 0 },
{
x: visible ? p.scale : 0.001,
y: visible ? biomassScale : 0.001,
z: visible ? p.scale : 0.001
}
);
}
this.plantMesh.count = MAX_PLANTS;
this.plantMesh.instanceMatrix.needsUpdate = true;
const rabbitCount = Math.min(MAX_RABBITS, Math.max(0, Math.round(s.R)));
for (let i = 0; i < rabbitCount; i++) {
const r = this.rabbitData[i];
const a = r.angle + t * r.speed;
const dx = Math.cos(a);
const dz = Math.sin(a);
const px = Math.cos(a) * 0.25;
const pz = Math.sin(a) * 0.25;
const hop = Math.abs(Math.sin(t * 4.0 + r.phase)) * 0.11;
const scale = r.scale;
this.setInstance(
this.rabbitBody,
i,
{ x: r.x, y: 0.31 + hop, z: r.z },
{ x: 0, y: -a, z: 0 },
{ x: 1.18 * scale, y: 0.82 * scale, z: 0.88 * scale }
);
this.setInstance(
this.rabbitHead,
i,
{ x: r.x + dx * 0.23, y: 0.46 + hop, z: r.z + dz * 0.23 },
{ x: 0, y: -a, z: 0 },
{ x: scale, y: scale, z: scale }
);
for (let ear = 0; ear < 2; ear++) {
const side = ear === 0 ? -1 : 1;
this.setInstance(
this.rabbitEars,
i * 2 + ear,
{
x: r.x + dx * 0.23 - dz * side * 0.075,
y: 0.69 + hop,
z: r.z + dz * 0.23 + dx * side * 0.075
},
{ x: 0, y: -a, z: side * 0.16 },
{ x: scale, y: scale, z: scale }
);
}
}
this.rabbitBody.count = rabbitCount;
this.rabbitHead.count = rabbitCount;
this.rabbitEars.count = rabbitCount * 2;
this.rabbitBody.instanceMatrix.needsUpdate = true;
this.rabbitHead.instanceMatrix.needsUpdate = true;
this.rabbitEars.instanceMatrix.needsUpdate = true;
const foxCount = Math.min(MAX_FOXES, Math.max(0, Math.round(s.F)));
for (let i = 0; i < foxCount; i++) {
const f = this.foxData[i];
const a = f.angle + t * f.speed;
const dx = Math.cos(a);
const dz = Math.sin(a);
const gait = Math.abs(Math.sin(t * 2.8 + f.phase)) * 0.035;
const scale = f.scale;
this.setInstance(
this.foxBody,
i,
{ x: f.x, y: 0.34 + gait, z: f.z },
{ x: 0, y: -a, z: 0 },
{ x: 1.34 * scale, y: 0.73 * scale, z: 0.82 * scale }
);
this.setInstance(
this.foxHead,
i,
{ x: f.x + dx * 0.28, y: 0.49 + gait, z: f.z + dz * 0.28 },
{ x: 0, y: -a, z: 0 },
{ x: scale, y: scale, z: scale }
);
this.tailDirection.set(-dx, 0, -dz).normalize();
this.dummy.position.set(
f.x - dx * 0.40,
0.37 + gait,
f.z - dz * 0.40
);
this.dummy.quaternion.setFromUnitVectors(this.up, this.tailDirection);
this.dummy.scale.set(scale, scale, scale);
this.dummy.updateMatrix();
this.foxTail.setMatrixAt(i, this.dummy.matrix);
for (let ear = 0; ear < 2; ear++) {
const side = ear === 0 ? -1 : 1;
this.setInstance(
this.foxEars,
i * 2 + ear,
{
x: f.x + dx * 0.28 - dz * side * 0.10,
y: 0.68 + gait,
z: f.z + dz * 0.28 + dx * side * 0.10
},
{ x: 0, y: -a, z: side * 0.12 },
{ x: scale, y: scale, z: scale }
);
}
}
this.foxBody.count = foxCount;
this.foxHead.count = foxCount;
this.foxTail.count = foxCount;
this.foxEars.count = foxCount * 2;
this.foxBody.instanceMatrix.needsUpdate = true;
this.foxHead.instanceMatrix.needsUpdate = true;
this.foxTail.instanceMatrix.needsUpdate = true;
this.foxEars.instanceMatrix.needsUpdate = true;
}
render() {
this.renderer.render(this.scene, this.camera);
}
}
const worlds = [
new ToyWorld("control", document.getElementById("view-control")),
new ToyWorld("immediate", document.getElementById("view-immediate")),
new ToyWorld("delayed", document.getElementById("view-delayed"))
];
/* -------------------------------------------------------------------------- */
/* Chart */
/* -------------------------------------------------------------------------- */
const chartCanvas = document.getElementById("chart");
const chartContext = chartCanvas.getContext("2d");
const chartColors = {
control: "#9aabc2",
immediate: "#ffb15f",
delayed: "#63e6cf"
};
function formatSigned(value, digits = 1) {
if (!Number.isFinite(value)) return "—";
const sign = value > 0.005 ? "+" : "";
return `${sign}${value.toFixed(digits)}`;
}
function drawChart() {
const rect = chartCanvas.getBoundingClientRect();
const dpr = Math.min(window.devicePixelRatio || 1, 2);
const width = Math.max(1, rect.width);
const height = Math.max(1, rect.height);
const pixelWidth = Math.round(width * dpr);
const pixelHeight = Math.round(height * dpr);
if (
chartCanvas.width !== pixelWidth ||
chartCanvas.height !== pixelHeight
) {
chartCanvas.width = pixelWidth;
chartCanvas.height = pixelHeight;
}
const ctx = chartContext;
ctx.setTransform(dpr, 0, 0, dpr, 0, 0);
ctx.clearRect(0, 0, width, height);
const margin = { left: 47, right: 18, top: 20, bottom: 31 };
const plotW = Math.max(1, width - margin.left - margin.right);
const plotH = Math.max(1, height - margin.top - margin.bottom);
const currentTime = Math.max(...models.map(m => m.time));
const maxX = Math.max(30, Math.ceil(currentTime / 5) * 5);
let maxY = 1;
for (const model of models) {
for (const sample of model.history) {
maxY = Math.max(maxY, sample[chartMode]);
}
}
maxY *= 1.08;
const x = t => margin.left + (t / maxX) * plotW;
const y = v => margin.top + plotH - (v / maxY) * plotH;
ctx.font = "11px system-ui, sans-serif";
ctx.lineWidth = 1;
for (let i = 0; i <= 5; i++) {
const yy = margin.top + (plotH * i) / 5;
const value = maxY * (1 - i / 5);
ctx.strokeStyle = "rgba(158,176,201,.13)";
ctx.beginPath();
ctx.moveTo(margin.left, yy);
ctx.lineTo(margin.left + plotW, yy);
ctx.stroke();
ctx.fillStyle = "#8396b1";
ctx.textAlign = "right";
ctx.textBaseline = "middle";
ctx.fillText(value.toFixed(value < 10 ? 1 : 0), margin.left - 8, yy);
}
const xTicks = Math.min(10, Math.max(6, Math.floor(maxX / 5)));
for (let i = 0; i <= xTicks; i++) {
const t = (maxX * i) / xTicks;
const xx = x(t);
ctx.strokeStyle = "rgba(158,176,201,.08)";
ctx.beginPath();
ctx.moveTo(xx, margin.top);
ctx.lineTo(xx, margin.top + plotH);
ctx.stroke();
ctx.fillStyle = "#8396b1";
ctx.textAlign = "center";
ctx.textBaseline = "top";
ctx.fillText(`${t.toFixed(0)} yr`, xx, margin.top + plotH + 9);
}
// Delayed intervention marker.
const delayedX = x(delayYears);
ctx.save();
ctx.setLineDash([5, 5]);
ctx.strokeStyle = "rgba(99,230,207,.72)";
ctx.lineWidth = 1.5;
ctx.beginPath();
ctx.moveTo(delayedX, margin.top);
ctx.lineTo(delayedX, margin.top + plotH);
ctx.stroke();
ctx.restore();
ctx.fillStyle = "#63e6cf";
ctx.textAlign = delayedX > margin.left + plotW * 0.78 ? "right" : "left";
ctx.textBaseline = "top";
ctx.fillText(
"delayed cull",
delayedX + (delayedX > margin.left + plotW * 0.78 ? -5 : 5),
margin.top + 3
);
for (const model of models) {
ctx.strokeStyle = chartColors[model.kind];
ctx.lineWidth = model.kind === "delayed" ? 2.5 : 1.9;
ctx.lineJoin = "round";
ctx.lineCap = "round";
ctx.beginPath();
model.history.forEach((sample, i) => {
const xx = x(sample.t);
const yy = y(sample[chartMode]);
if (i === 0) ctx.moveTo(xx, yy);
else ctx.lineTo(xx, yy);
});
ctx.stroke();
}
ctx.strokeStyle = "rgba(158,176,201,.28)";
ctx.lineWidth = 1;
ctx.strokeRect(margin.left, margin.top, plotW, plotH);
}
/* -------------------------------------------------------------------------- */
/* UI and outcomes */
/* -------------------------------------------------------------------------- */
const ids = ["control", "immediate", "delayed"];
function updateReadouts() {
if (!models.length) return;
const control = models[0];
const immediate = models[1];
const delayed = models[2];
document.getElementById("clock").textContent =
`${control.time.toFixed(2)} yr`;
for (const model of models) {
document.getElementById(`${model.kind}-G`).textContent =
model.state.G.toFixed(1);
document.getElementById(`${model.kind}-R`).textContent =
model.state.R.toFixed(1);
document.getElementById(`${model.kind}-F`).textContent =
model.state.F.toFixed(1);
}
const delayedBadge = document.getElementById("badge-delayed");
if (delayed.applied) {
delayedBadge.textContent = `Cull applied at ${delayYears.toFixed(1)} yr`;
delayedBadge.className = "badge applied";
} else {
delayedBadge.textContent = `Cull at ${delayYears.toFixed(1)} yr · armed`;
delayedBadge.className = "badge armed";
}
const immediateGap = immediate.state.R - control.state.R;
const delayedGap = delayed.state.R - control.state.R;
document.getElementById("rabbitGap").textContent =
`Immediate ${formatSigned(immediateGap)} · Delayed ${formatSigned(delayedGap)}`;
const target = delayYears + 5;
if (control.time + 1e-9 >= target) {
const delayedAtTarget = delayed.sampleAt(target);
const controlAtTarget = control.sampleAt(target);
const immediateAtTarget = immediate.sampleAt(target);
const rabbitGap =
delayedAtTarget.R - controlAtTarget.R;
const foxGap =
delayedAtTarget.F - controlAtTarget.F;
document.getElementById("fiveYearGap").textContent =
`Rabbit ${formatSigned(rabbitGap)} · Fox ${formatSigned(foxGap)}`;
document.getElementById("fiveYearSub").textContent =
`At year ${target.toFixed(1)}, relative to control.`;
const timingRabbitGap =
delayedAtTarget.R - immediateAtTarget.R;
const timingFoxGap =
delayedAtTarget.F - immediateAtTarget.F;
document.getElementById("timingGap").textContent =
`Rabbit ${formatSigned(timingRabbitGap)} · Fox ${formatSigned(timingFoxGap)}`;
document.getElementById("timingSub").textContent =
`Delayed versus immediate at year ${target.toFixed(1)}.`;
} else {
document.getElementById("fiveYearGap").textContent = "Pending";
document.getElementById("fiveYearSub").textContent =
`Available after year ${target.toFixed(1)}.`;
document.getElementById("timingGap").textContent = "Pending";
document.getElementById("timingSub").textContent =
`Compared five years after the delayed action.`;
}
}
function resetSimulation() {
makeModels();
accumulator = 0;
updateReadouts();
drawChart();
}
const delayInput = document.getElementById("delay");
const cullInput = document.getElementById("cull");
const speedInput = document.getElementById("speed");
const playButton = document.getElementById("play");
delayInput.addEventListener("input", () => {
document.getElementById("delayValue").textContent =
`${delayInput.value} yr`;
});
delayInput.addEventListener("change", () => {
delayYears = Number(delayInput.value);
resetSimulation();
});
cullInput.addEventListener("input", () => {
document.getElementById("cullValue").textContent =
`${cullInput.value}%`;
});
cullInput.addEventListener("change", () => {
cullFraction = Number(cullInput.value) / 100;
resetSimulation();
});
speedInput.addEventListener("change", () => {
simulationSpeed = Number(speedInput.value);
});
playButton.addEventListener("click", () => {
running = !running;
playButton.textContent = running ? "Pause" : "Resume";
});
document.getElementById("reset").addEventListener("click", resetSimulation);
document.querySelectorAll("[data-chart]").forEach(button => {
button.addEventListener("click", () => {
document.querySelectorAll("[data-chart]").forEach(b => {
b.classList.toggle("active", b === button);
});
chartMode = button.dataset.chart;
drawChart();
});
});
window.addEventListener("keydown", event => {
if (event.code === "Space" && event.target === document.body) {
event.preventDefault();
playButton.click();
}
});
/* -------------------------------------------------------------------------- */
/* Main loop */
/* -------------------------------------------------------------------------- */
makeModels();
updateReadouts();
drawChart();
let previousTime = performance.now();
let accumulator = 0;
let lastUiUpdate = 0;
function animate(now) {
const realDelta = Math.min((now - previousTime) / 1000, 0.1);
previousTime = now;
if (running) {
accumulator += realDelta * simulationSpeed;
let steps = 0;
while (accumulator >= P.dt && steps < 100) {
for (const model of models) model.step(P.dt);
accumulator -= P.dt;
steps++;
}
}
for (const world of worlds) {
world.update();
world.render();
}
if (now - lastUiUpdate > 90) {
updateReadouts();
drawChart();
lastUiUpdate = now;
}
requestAnimationFrame(animate);
}
requestAnimationFrame(animate);
</script>
</body>
</html>
```