Prompt specification
Build or customize the Three.js 3D scene "Exchange-limitation classroom model" 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: Exchange-limitation classroom model
Category: Biology & Nature · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/exchange-limitation-classroom-model-x0760
Scene Description Prompt:
Make a low-poly toy plant–fungus exchange model with adjustable resources and explicit exchange rules. Use Three.js from cdnjs in standalone HTML.
Working Three.js Starter (exchange-limitation-classroom-model-x0760):
```html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Mycorrhiza Exchange Lab</title>
<style>
:root {
--ink: #20302a;
--muted: #69756f;
--paper: #f7f8f1;
--panel: #fffef9;
--line: #dfe4d9;
--green: #397a55;
--green-dark: #24543b;
--mint: #dcecdf;
--orange: #e99a37;
--teal: #27b8aa;
--shadow: 0 18px 50px rgba(38, 57, 47, 0.12);
}
* {
box-sizing: border-box;
}
html,
body {
margin: 0;
width: 100%;
height: 100%;
overflow: hidden;
background: var(--paper);
color: var(--ink);
font-family:
Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
button,
input {
font: inherit;
}
#app {
display: flex;
width: 100%;
height: 100%;
}
#controls {
position: relative;
z-index: 5;
width: 350px;
flex: 0 0 350px;
height: 100%;
overflow-y: auto;
padding: 24px 22px 30px;
background: var(--panel);
border-right: 1px solid var(--line);
box-shadow: 8px 0 30px rgba(45, 63, 54, 0.05);
}
.eyebrow {
margin: 0 0 8px;
color: var(--green);
font-size: 11px;
font-weight: 800;
letter-spacing: 0.16em;
text-transform: uppercase;
}
h1 {
margin: 0;
font-size: 27px;
line-height: 1.08;
letter-spacing: -0.035em;
}
.intro {
margin: 12px 0 22px;
color: var(--muted);
font-size: 13px;
line-height: 1.55;
}
.section {
margin-top: 20px;
padding-top: 19px;
border-top: 1px solid var(--line);
}
.section-title {
display: flex;
align-items: center;
justify-content: space-between;
margin: 0 0 14px;
font-size: 12px;
font-weight: 800;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.section-title span {
color: var(--muted);
font-size: 10px;
font-weight: 600;
letter-spacing: 0.03em;
text-transform: none;
}
.control {
margin: 0 0 16px;
}
.control-head {
display: flex;
align-items: baseline;
justify-content: space-between;
gap: 12px;
margin-bottom: 7px;
}
.control label {
font-size: 13px;
font-weight: 650;
}
.control output {
min-width: 43px;
color: var(--green-dark);
font-size: 12px;
font-weight: 800;
text-align: right;
}
.control small {
display: block;
margin-top: 5px;
color: var(--muted);
font-size: 10.5px;
line-height: 1.35;
}
input[type="range"] {
width: 100%;
height: 5px;
margin: 0;
border-radius: 999px;
outline: none;
background: #dfe7dd;
accent-color: var(--green);
cursor: pointer;
}
input[type="range"]::-webkit-slider-thumb {
width: 16px;
height: 16px;
border: 3px solid #fff;
border-radius: 50%;
background: var(--green);
box-shadow: 0 2px 7px rgba(32, 67, 47, 0.28);
appearance: none;
}
input[type="range"]::-moz-range-thumb {
width: 12px;
height: 12px;
border: 3px solid #fff;
border-radius: 50%;
background: var(--green);
box-shadow: 0 2px 7px rgba(32, 67, 47, 0.28);
}
.switch-row {
display: flex;
align-items: center;
justify-content: space-between;
gap: 16px;
margin: 12px 0;
padding: 12px;
border: 1px solid var(--line);
border-radius: 12px;
background: #fbfcf7;
}
.switch-copy strong {
display: block;
margin-bottom: 3px;
font-size: 12.5px;
}
.switch-copy small {
color: var(--muted);
font-size: 10.5px;
line-height: 1.35;
}
.switch {
position: relative;
width: 38px;
height: 22px;
flex: 0 0 auto;
}
.switch input {
position: absolute;
opacity: 0;
pointer-events: none;
}
.switch-track {
position: absolute;
inset: 0;
border-radius: 999px;
background: #cbd4ca;
transition: 160ms ease;
cursor: pointer;
}
.switch-track::after {
position: absolute;
top: 3px;
left: 3px;
width: 16px;
height: 16px;
border-radius: 50%;
background: #fff;
box-shadow: 0 2px 5px rgba(0, 0, 0, 0.15);
content: "";
transition: 160ms ease;
}
.switch input:checked + .switch-track {
background: var(--green);
}
.switch input:checked + .switch-track::after {
transform: translateX(16px);
}
.button-row {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 8px;
margin-top: 13px;
}
button {
min-height: 36px;
padding: 8px 10px;
border: 1px solid var(--line);
border-radius: 10px;
background: #fff;
color: var(--ink);
font-size: 11px;
font-weight: 750;
cursor: pointer;
transition:
transform 120ms ease,
border-color 120ms ease,
background 120ms ease;
}
button:hover {
border-color: #a9bdaa;
background: #f5faf3;
}
button:active {
transform: translateY(1px);
}
button.primary {
border-color: var(--green);
background: var(--green);
color: #fff;
}
button.primary:hover {
background: var(--green-dark);
}
.health-card {
padding: 14px;
border: 1px solid var(--line);
border-radius: 14px;
background: #fbfcf7;
}
.status {
display: flex;
align-items: center;
gap: 8px;
margin-bottom: 13px;
font-size: 12px;
font-weight: 800;
}
.status-dot {
width: 8px;
height: 8px;
border-radius: 50%;
background: var(--orange);
box-shadow: 0 0 0 4px rgba(233, 154, 55, 0.13);
}
.health-row {
margin: 10px 0;
}
.health-row > div:first-child {
display: flex;
justify-content: space-between;
margin-bottom: 5px;
font-size: 11px;
font-weight: 700;
}
.bar {
height: 6px;
overflow: hidden;
border-radius: 999px;
background: #e6eae1;
}
.bar > span {
display: block;
width: 0;
height: 100%;
border-radius: inherit;
background: var(--green);
transition: width 200ms ease;
}
.bar.fungi > span {
background: #9b70ad;
}
.metric-grid {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 8px;
margin-top: 11px;
}
.metric {
padding: 9px;
border: 1px solid var(--line);
border-radius: 10px;
background: #fff;
}
.metric span {
display: block;
margin-bottom: 3px;
color: var(--muted);
font-size: 9px;
font-weight: 700;
text-transform: uppercase;
}
.metric strong {
font-size: 13px;
}
.tiny-note {
margin: 16px 0 0;
color: var(--muted);
font-size: 10.5px;
line-height: 1.5;
}
#viewport {
position: relative;
min-width: 0;
height: 100%;
flex: 1;
overflow: hidden;
background:
radial-gradient(circle at 50% 30%, #fbfcf5 0, #edf1e7 58%, #e3e9de 100%);
}
#scene {
display: block;
width: 100%;
height: 100%;
touch-action: none;
cursor: grab;
}
#scene.dragging {
cursor: grabbing;
}
.scene-heading {
position: absolute;
top: 24px;
left: 26px;
z-index: 2;
pointer-events: none;
}
.scene-heading .eyebrow {
margin-bottom: 5px;
}
.scene-heading h2 {
margin: 0;
font-size: 18px;
letter-spacing: -0.02em;
}
.scene-heading p {
margin: 5px 0 0;
color: var(--muted);
font-size: 11px;
}
.scene-tools {
position: absolute;
top: 22px;
right: 24px;
z-index: 4;
display: flex;
gap: 8px;
}
.scene-tools button {
background: rgba(255, 254, 249, 0.88);
box-shadow: 0 5px 18px rgba(45, 63, 54, 0.08);
backdrop-filter: blur(10px);
}
.rules-panel {
position: absolute;
right: 24px;
bottom: 24px;
z-index: 3;
width: 315px;
padding: 16px 17px;
border: 1px solid rgba(209, 219, 207, 0.92);
border-radius: 16px;
background: rgba(255, 254, 249, 0.9);
box-shadow: var(--shadow);
backdrop-filter: blur(14px);
}
.rules-panel h3 {
display: flex;
align-items: center;
justify-content: space-between;
margin: 0 0 10px;
font-size: 12px;
letter-spacing: 0.04em;
text-transform: uppercase;
}
.rules-panel h3 span {
color: var(--green);
font-size: 9px;
letter-spacing: 0.08em;
}
.rule {
display: grid;
grid-template-columns: 22px 1fr;
gap: 8px;
margin: 8px 0;
color: #526059;
font-size: 10.5px;
line-height: 1.42;
}
.rule b {
display: grid;
width: 20px;
height: 20px;
place-items: center;
border-radius: 6px;
background: var(--mint);
color: var(--green-dark);
font-size: 9px;
}
.rule code {
color: var(--ink);
font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
font-size: 9.5px;
}
.legend {
position: absolute;
bottom: 24px;
left: 26px;
z-index: 3;
display: flex;
flex-wrap: wrap;
gap: 8px;
max-width: 330px;
padding: 9px 11px;
border: 1px solid rgba(209, 219, 207, 0.9);
border-radius: 12px;
background: rgba(255, 254, 249, 0.84);
color: var(--muted);
font-size: 10px;
backdrop-filter: blur(10px);
}
.legend-item {
display: flex;
align-items: center;
gap: 6px;
}
.legend-line {
width: 20px;
height: 3px;
border-radius: 99px;
}
.legend-line.mineral {
background: var(--orange);
}
.legend-line.sugar {
background: var(--teal);
}
.legend-hint {
width: 100%;
opacity: 0.75;
}
.scene-label {
position: absolute;
z-index: 2;
padding: 5px 8px;
border: 1px solid rgba(208, 218, 207, 0.8);
border-radius: 999px;
background: rgba(255, 254, 249, 0.82);
color: #4e5d54;
font-size: 9.5px;
font-weight: 750;
pointer-events: none;
transform: translate(-50%, -50%);
white-space: nowrap;
backdrop-filter: blur(8px);
}
@media (max-width: 1080px) {
.rules-panel {
width: 280px;
}
.scene-heading p {
display: none;
}
}
@media (max-width: 820px) {
html,
body {
height: auto;
min-height: 100%;
overflow: auto;
}
#app {
display: block;
}
#controls {
width: 100%;
height: auto;
max-height: none;
border-right: 0;
border-bottom: 1px solid var(--line);
}
#viewport {
height: 65vh;
min-height: 500px;
}
.rules-panel {
display: none;
}
.scene-heading {
top: 17px;
left: 18px;
}
.scene-tools {
top: 16px;
right: 16px;
}
.legend {
bottom: 16px;
left: 16px;
}
}
</style>
</head>
<body>
<div id="app">
<aside id="controls">
<p class="eyebrow">Ecology sandbox</p>
<h1>Mycorrhiza<br>Exchange Lab</h1>
<p class="intro">
A toy model of a plant trading photosynthetic sugars with a fungal
network for minerals gathered from soil.
</p>
<section class="section">
<h2 class="section-title">
Association
<span>trade policy</span>
</h2>
<div class="switch-row">
<div class="switch-copy">
<strong>Mycorrhizal link</strong>
<small>Enable fungal mineral delivery and sugar payment.</small>
</div>
<label class="switch">
<input id="association" type="checkbox" checked>
<span class="switch-track"></span>
</label>
</div>
<div class="switch-row">
<div class="switch-copy">
<strong>Reciprocal payment</strong>
<small id="reciprocalHelp">Fungus waits for carbon above the reserve.</small>
</div>
<label class="switch">
<input id="reciprocal" type="checkbox" checked>
<span class="switch-track"></span>
</label>
</div>
</section>
<section class="section">
<h2 class="section-title">
Resources
<span>availability</span>
</h2>
<div class="control">
<div class="control-head">
<label for="light">Sunlight</label>
<output id="lightOut">72%</output>
</div>
<input id="light" type="range" min="0" max="100" value="72">
<small>Drives the plant’s photosynthesis and sugar supply.</small>
</div>
<div class="control">
<div class="control-head">
<label for="water">Soil moisture</label>
<output id="waterOut">58%</output>
</div>
<input id="water" type="range" min="0" max="100" value="58">
<small>Supports photosynthesis, root uptake, and fungal mining.</small>
</div>
<div class="control">
<div class="control-head">
<label for="minerals">Soil minerals</label>
<output id="mineralsOut">48%</output>
</div>
<input id="minerals" type="range" min="0" max="100" value="48">
<small>The mineral pool available to roots and fungal hyphae.</small>
</div>
<div class="control">
<div class="control-head">
<label for="demand">Fungal carbon demand</label>
<output id="demandOut">40%</output>
</div>
<input id="demand" type="range" min="10" max="90" value="40">
<small>Higher demand raises the carbon payment per delivered mineral.</small>
</div>
</section>
<section class="section">
<h2 class="section-title">
Exchange settings
<span>rates & reserve</span>
</h2>
<div class="control">
<div class="control-head">
<label for="mineralEff">Mineral transfer efficiency</label>
<output id="mineralEffOut">78%</output>
</div>
<input id="mineralEff" type="range" min="30" max="100" value="78">
<small>How effectively fungal minerals fill the plant’s deficit.</small>
</div>
<div class="control">
<div class="control-head">
<label for="carbonEff">Carbon transfer efficiency</label>
<output id="carbonEffOut">70%</output>
</div>
<input id="carbonEff" type="range" min="20" max="100" value="70">
<small>How much of the requested payment reaches the fungus.</small>
</div>
<div class="control">
<div class="control-head">
<label for="reserve">Plant sugar reserve</label>
<output id="reserveOut">25%</output>
</div>
<input id="reserve" type="range" min="0" max="60" value="25">
<small>Sugar below this reserve is protected in reciprocal mode.</small>
</div>
<div class="button-row">
<button type="button" data-pulse="light">+ Light</button>
<button type="button" data-pulse="water">+ Water</button>
<button type="button" data-pulse="minerals">+ Minerals</button>
</div>
<div class="button-row">
<button id="pause" class="primary" type="button">Pause</button>
<button id="resetModel" type="button">Reset model</button>
</div>
</section>
<section class="section">
<h2 class="section-title">System state</h2>
<div class="health-card">
<div class="status">
<span id="statusDot" class="status-dot"></span>
<span id="statusText">Mutualism active</span>
</div>
<div class="health-row">
<div>
<span>Plant health</span>
<span id="plantHealthText">72%</span>
</div>
<div class="bar"><span id="plantHealthBar"></span></div>
</div>
<div class="health-row">
<div>
<span>Fungal network health</span>
<span id="fungusHealthText">62%</span>
</div>
<div class="bar fungi"><span id="fungusHealthBar"></span></div>
</div>
<div class="metric-grid">
<div class="metric">
<span>Plant sugar</span>
<strong id="sugarValue">58%</strong>
</div>
<div class="metric">
<span>Plant minerals</span>
<strong id="plantMineralValue">26%</strong>
</div>
<div class="metric">
<span>Mineral flow</span>
<strong id="mineralFlow">0.0/s</strong>
</div>
<div class="metric">
<span>Sugar flow</span>
<strong id="carbonFlow">0.0/s</strong>
</div>
</div>
</div>
<p class="tiny-note">
Values are illustrative, not calibrated biological measurements.
Drag the scene to orbit; scroll to zoom.
</p>
</section>
</aside>
<main id="viewport">
<canvas id="scene" aria-label="Interactive low-poly plant and fungus model"></canvas>
<div class="scene-heading">
<p class="eyebrow">Toy model 01</p>
<h2>Plant ↔ fungal exchange</h2>
<p>Low-poly cutaway · adjustable resources · explicit trade rules</p>
</div>
<div class="scene-tools">
<button id="resetView" type="button">Reset view</button>
</div>
<div id="plantLabel" class="scene-label">Plant</div>
<div id="fungusLabel" class="scene-label">Fungal network</div>
<div id="soilLabel" class="scene-label">Soil minerals</div>
<div class="legend">
<div class="legend-item">
<span class="legend-line mineral"></span>
Minerals → plant
</div>
<div class="legend-item">
<span class="legend-line sugar"></span>
Sugars → fungus
</div>
<div class="legend-hint">Drag to orbit · scroll or pinch to zoom</div>
</div>
<section class="rules-panel">
<h3>Explicit exchange rules <span>updated live</span></h3>
<div class="rule">
<b>1</b>
<div>
Plant makes sugar:
<code>Δsugar = light × moisture − respiration</code>
</div>
</div>
<div class="rule">
<b>2</b>
<div>
Fungi mine soil minerals and hold them in a fungal mineral stock.
</div>
</div>
<div class="rule">
<b>3</b>
<div>
Mineral delivery:
<code>min(fungal stock, plant deficit × efficiency)</code>
</div>
</div>
<div class="rule">
<b>4</b>
<div>
Carbon payment:
<code>mineral × demand × carbon efficiency</code>
</div>
</div>
<div class="rule">
<b>5</b>
<div id="ruleFive">
Reciprocal mode protects the plant’s sugar reserve. Fungus receives
payment as minerals are delivered.
</div>
</div>
</section>
</main>
</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const $ = (selector) => document.querySelector(selector);
const viewport = $("#viewport");
const canvas = $("#scene");
const clamp = (value, min, max) => Math.max(min, Math.min(max, value));
const lerp = (a, b, t) => a + (b - a) * t;
const params = {
light: 72,
water: 58,
minerals: 48,
demand: 40,
mineralEff: 78,
carbonEff: 70,
reserve: 25,
association: true,
reciprocal: true
};
const sim = {
plantSugar: 58,
plantMinerals: 26,
fungusCarbon: 38,
fungusMinerals: 32,
rates: {
mineral: 0,
carbon: 0
},
paymentPerUnit: 0
};
let running = true;
// ------------------------------------------------------------
// Renderer, scene, camera
// ------------------------------------------------------------
const renderer = new THREE.WebGLRenderer({
canvas,
antialias: true,
alpha: false,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.08;
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xeaf0e5);
scene.fog = new THREE.Fog(0xeaf0e5, 10, 22);
const camera = new THREE.PerspectiveCamera(
34,
1,
0.1,
60
);
const cameraState = {
yaw: 0.58,
pitch: 1.02,
radius: 8.8,
target: new THREE.Vector3(0, 0.35, 0)
};
const hemisphere = new THREE.HemisphereLight(
0xf7fff1,
0x604937,
2.25
);
scene.add(hemisphere);
const sunLight = new THREE.DirectionalLight(0xfff3cf, 3.1);
sunLight.position.set(-4, 8, 5);
sunLight.castShadow = true;
sunLight.shadow.mapSize.set(2048, 2048);
sunLight.shadow.camera.left = -6;
sunLight.shadow.camera.right = 6;
sunLight.shadow.camera.top = 6;
sunLight.shadow.camera.bottom = -6;
sunLight.shadow.camera.near = 1;
sunLight.shadow.camera.far = 20;
sunLight.shadow.bias = -0.0008;
scene.add(sunLight);
const fillLight = new THREE.DirectionalLight(0xb8e8df, 0.8);
fillLight.position.set(5, 3, -4);
scene.add(fillLight);
const world = new THREE.Group();
scene.add(world);
const leaves = [];
const mushroomCaps = [];
const mineralCrystals = [];
const exchangePaths = [];
const material = (color, extra = {}) =>
new THREE.MeshStandardMaterial({
color,
roughness: 0.82,
metalness: 0,
flatShading: true,
...extra
});
const stemMaterial = material(0x3d8b4e);
const leafMaterials = [
material(0x56a95d),
material(0x6fbb63),
material(0x438f52)
];
const rootMaterial = material(0xb98350);
const hyphaMaterial = material(0x9b70ad);
const mushroomStemMaterial = material(0xe9dcc1);
const mushroomCapMaterial = material(0xb66e91);
function mesh(geometry, mat, cast = true, receive = true) {
const object = new THREE.Mesh(geometry, mat);
object.castShadow = cast;
object.receiveShadow = receive;
return object;
}
function cylinderBetween(a, b, radius, mat, radialSegments = 6) {
const start = new THREE.Vector3(...a);
const end = new THREE.Vector3(...b);
const direction = end.clone().sub(start);
const length = direction.length();
const object = mesh(
new THREE.CylinderGeometry(
radius,
radius * 0.94,
length,
radialSegments
),
mat
);
object.position.copy(start).add(end).multiplyScalar(0.5);
object.quaternion.setFromUnitVectors(
new THREE.Vector3(0, 1, 0),
direction.normalize()
);
return object;
}
function tubeThrough(points, radius, mat, segments = 24) {
const curve = new THREE.CatmullRomCurve3(
points.map((point) => new THREE.Vector3(...point))
);
return mesh(
new THREE.TubeGeometry(curve, segments, radius, 4, false),
mat
);
}
function makeLeafGeometry(length = 0.95, width = 0.48) {
const geometry = new THREE.BufferGeometry();
geometry.setAttribute(
"position",
new THREE.Float32BufferAttribute([
0, 0, 0,
-width * 0.55, length * 0.44, 0,
0, length, 0,
width * 0.55, length * 0.44, 0,
0, length * 0.44, 0.12
], 3)
);
geometry.setIndex([
0, 1, 4,
1, 2, 4,
2, 3, 4,
3, 0, 4
]);
geometry.computeVertexNormals();
return geometry;
}
// ------------------------------------------------------------
// Diorama: soil, plant, roots, fungi, mushrooms
// ------------------------------------------------------------
function buildGround() {
const soilSide = mesh(
new THREE.CylinderGeometry(3.65, 3.38, 1.6, 12, 1, true),
material(0x80563b, {
transparent: true,
opacity: 0.68,
side: THREE.DoubleSide,
depthWrite: false
}),
false,
true
);
soilSide.position.y = -0.82;
world.add(soilSide);
const bottom = mesh(
new THREE.CylinderGeometry(3.38, 3.38, 0.18, 12),
material(0x65432f)
);
bottom.position.y = -1.7;
world.add(bottom);
const surface = mesh(
new THREE.CircleGeometry(3.66, 12),
material(0x8db875, {
transparent: true,
opacity: 0.2,
side: THREE.DoubleSide,
depthWrite: false
}),
false,
false
);
surface.rotation.x = -Math.PI / 2;
surface.position.y = -0.005;
world.add(surface);
const grassMaterial = material(0x6fa65d);
for (let i = 0; i < 22; i++) {
const angle = (i / 22) * Math.PI * 2 + 0.12;
const radius = 3.05 + (i % 3) * 0.16;
const tuft = mesh(
new THREE.ConeGeometry(0.07, 0.35 + (i % 2) * 0.1, 4),
grassMaterial
);
tuft.position.set(
Math.cos(angle) * radius,
0.11,
Math.sin(angle) * radius
);
tuft.rotation.z = (i % 2 ? 1 : -1) * 0.16;
world.add(tuft);
}
const pebbleMaterial = material(0x9a8068);
for (let i = 0; i < 13; i++) {
const angle = i * 2.37;
const radius = 1.1 + (i % 5) * 0.43;
const pebble = mesh(
new THREE.DodecahedronGeometry(0.1 + (i % 3) * 0.035, 0),
pebbleMaterial
);
pebble.position.set(
Math.cos(angle) * radius,
-0.9 - (i % 3) * 0.16,
Math.sin(angle) * radius * 0.75
);
pebble.rotation.set(i * 0.4, i * 0.7, i * 0.2);
world.add(pebble);
}
}
function buildPlant() {
const plantX = -1.18;
const stem = tubeThrough(
[
[plantX, 0, 0],
[plantX - 0.08, 0.65, 0.02],
[plantX + 0.05, 1.28, -0.02],
[plantX - 0.06, 1.92, 0.02],
[plantX + 0.03, 2.28, 0]
],
0.075,
stemMaterial,
28
);
world.add(stem);
const branchData = [
{
from: [plantX, 1.08, 0],
to: [plantX - 0.52, 1.47, 0.05],
leafRotation: [0.25, 1.08],
color: 0
},
{
from: [plantX, 1.42, 0],
to: [plantX + 0.55, 1.72, -0.03],
leafRotation: [-0.25, -1.02],
color: 1
},
{
from: [plantX, 1.76, 0],
to: [plantX - 0.36, 2.03, 0.08],
leafRotation: [0.5, 0.9],
color: 2
},
{
from: [plantX - 0.01, 2.15, 0],
to: [plantX + 0.04, 2.72, 0.02],
leafRotation: [0.2, 0.08],
color: 0
}
];
branchData.forEach((branch, index) => {
world.add(
cylinderBetween(
branch.from,
branch.to,
0.035,
stemMaterial,
5
)
);
const leaf = mesh(
makeLeafGeometry(0.82 + index * 0.035, 0.48),
leafMaterials[branch.color]
);
leaf.position.set(...branch.to);
leaf.rotation.set(
branch.leafRotation[0],
branch.leafRotation[1],
0
);
leaf.userData.baseZ = 0;
leaf.userData.phase = index * 1.3;
leaves.push(leaf);
world.add(leaf);
});
const rootMain = tubeThrough(
[
[plantX, 0.02, 0],
[plantX + 0.12, -0.38, 0.08],
[plantX + 0.35, -0.72, 0.16],
[plantX + 0.65, -1.02, 0.22]
],
0.055,
rootMaterial,
28
);
world.add(rootMain);
const roots = [
[
[plantX + 0.08, -0.24, 0.04],
[plantX - 0.32, -0.48, 0.17],
[plantX - 0.58, -0.93, 0.2]
],
[
[plantX + 0.18, -0.42, 0.1],
[plantX + 0.58, -0.66, -0.12],
[plantX + 0.76, -1.15, -0.2]
],
[
[plantX + 0.3, -0.65, 0.16],
[plantX + 0.16, -1.03, 0.3],
[plantX + 0.05, -1.43, 0.34]
]
];
roots.forEach((points) => {
world.add(tubeThrough(points, 0.027, rootMaterial, 18));
});
}
function buildFungi() {
const hub = [1.28, -0.62, 0.18];
const mainHypha = tubeThrough(
[
hub,
[0.75, -0.43, 0.25],
[0.18, -0.57, 0.3],
[-0.48, -0.7, 0.28],
[-0.88, -0.52, 0.34]
],
0.042,
hyphaMaterial,
34
);
world.add(mainHypha);
const branches = [
[
[0.82, -0.43, 0.24],
[0.95, -0.18, 0.48],
[1.18, -0.08, 0.66]
],
[
[0.4, -0.53, 0.29],
[0.34, -0.9, 0.5],
[0.53, -1.22, 0.56]
],
[
[0.02, -0.6, 0.3],
[-0.1, -0.33, 0.58],
[-0.35, -0.18, 0.76]
],
[
[-0.36, -0.67, 0.28],
[-0.45, -1.02, 0.08],
[-0.72, -1.28, -0.03]
],
[
[1.08, -0.55, 0.2],
[1.52, -0.27, -0.08],
[1.82, -0.05, -0.19]
]
];
branches.forEach((points) => {
world.add(tubeThrough(points, 0.022, hyphaMaterial, 20));
const end = points[points.length - 1];
const node = mesh(
new THREE.IcosahedronGeometry(0.065, 0),
hyphaMaterial
);
node.position.set(...end);
world.add(node);
});
const nodeMaterial = material(0xd5a6d4, {
emissive: 0x542653,
emissiveIntensity: 0.12
});
[
[0.74, -0.42, 0.25],
[0.19, -0.57, 0.3],
[-0.46, -0.7, 0.28],
[1.26, -0.62, 0.18]
].forEach((point) => {
const node = mesh(
new THREE.DodecahedronGeometry(0.085, 0),
nodeMaterial
);
node.position.set(...point);
world.add(node);
});
function addMushroom(x, z, scale, capMaterial) {
const group = new THREE.Group();
group.position.set(x, 0, z);
group.scale.setScalar(scale);
const stem = mesh(
new THREE.CylinderGeometry(0.09, 0.13, 0.65, 7),
mushroomStemMaterial
);
stem.position.y = 0.32;
group.add(stem);
const cap = mesh(
new THREE.SphereGeometry(
0.43,
7,
4,
0,
Math.PI * 2,
0,
Math.PI / 2
),
capMaterial
);
cap.position.y = 0.64;
cap.scale.y = 0.58;
cap.userData.baseY = cap.position.y;
cap.userData.phase = x * 3 + z;
mushroomCaps.push(cap);
group.add(cap);
const underside = mesh(
new THREE.CylinderGeometry(0.37, 0.37, 0.035, 7),
material(0xe7c9d3)
);
underside.position.y = 0.64;
group.add(underside);
world.add(group);
}
addMushroom(1.38, 0.05, 1.0, mushroomCapMaterial);
addMushroom(2.0, -0.2, 0.7, material(0xc985a3));
addMushroom(0.95, 0.65, 0.55, material(0xa7688f));
}
function buildResources() {
const sun = mesh(
new THREE.IcosahedronGeometry(0.34, 1),
material(0xf7c84d, {
emissive: 0xd99c19,
emissiveIntensity: 0.22
})
);
sun.position.set(-2.45, 3.65, -0.85);
sun.name = "sun";
world.add(sun);
const rayMaterial = material(0xf7ce65, {
emissive: 0xb68018,
emissiveIntensity: 0.15
});
for (let i = 0; i < 8; i++) {
const angle = (i / 8) * Math.PI * 2;
const ray = mesh(
new THREE.ConeGeometry(0.055, 0.19, 4),
rayMaterial
);
ray.position.set(
sun.position.x + Math.cos(angle) * 0.5,
sun.position.y + Math.sin(angle) * 0.5,
sun.position.z
);
ray.rotation.z = angle - Math.PI / 2;
world.add(ray);
}
const crystalMaterial = material(0xeaa64a, {
emissive: 0x6f3a08,
emissiveIntensity: 0.18
});
const crystalPositions = [
[2.2, -1.2, 0.75],
[1.8, -0.88, 1.1],
[2.55, -0.72, -0.15],
[0.75, -1.2, 1.0],
[0.15, -1.25, 0.7],
[-1.8, -1.15, 0.9],
[2.75, -1.25, 0.55],
[1.45, -1.34, -0.8],
[-0.35, -1.42, 0.9],
[2.1, -0.55, 1.35]
];
crystalPositions.forEach((point, index) => {
const crystal = mesh(
new THREE.OctahedronGeometry(0.12 + (index % 3) * 0.035),
crystalMaterial
);
crystal.position.set(...point);
crystal.rotation.set(index * 0.2, index * 0.45, index * 0.13);
world.add(crystal);
mineralCrystals.push(crystal);
});
const dropMaterial = material(0x65b9d0, {
emissive: 0x1d6676,
emissiveIntensity: 0.12
});
[
[2.85, 1.85, -0.7],
[3.12, 1.38, -0.45],
[2.62, 1.22, -1.1]
].forEach((point) => {
const drop = mesh(
new THREE.OctahedronGeometry(0.12, 0),
dropMaterial
);
drop.position.set(...point);
drop.scale.y = 1.35;
world.add(drop);
});
}
function makeExchangePath(start, control, end, color, name) {
const curve = new THREE.QuadraticBezierCurve3(
new THREE.Vector3(...start),
new THREE.Vector3(...control),
new THREE.Vector3(...end)
);
const pathMaterial = new THREE.MeshBasicMaterial({
color,
transparent: true,
opacity: 0.7,
depthWrite: false
});
const path = mesh(
new THREE.TubeGeometry(curve, 32, 0.025, 5, false),
pathMaterial,
false,
false
);
world.add(path);
const head = mesh(
new THREE.ConeGeometry(0.105, 0.27, 6),
new THREE.MeshBasicMaterial({
color,
transparent: true,
opacity: 0.95,
depthWrite: false
}),
false,
false
);
head.position.copy(curve.getPointAt(1));
head.quaternion.setFromUnitVectors(
new THREE.Vector3(0, 1, 0),
curve.getTangentAt(1).normalize()
);
world.add(head);
const particles = [];
for (let i = 0; i < 4; i++) {
const particle = mesh(
new THREE.IcosahedronGeometry(0.065, 0),
new THREE.MeshBasicMaterial({
color,
transparent: true,
opacity: 0.95,
depthWrite: false
}),
false,
false
);
particle.userData.phase = i / 4;
particles.push(particle);
world.add(particle);
}
exchangePaths.push({
name,
curve,
path,
head,
particles
});
}
function buildExchangePaths() {
makeExchangePath(
[1.22, -0.58, 0.46],
[0.15, -0.08, 0.92],
[-0.82, -0.57, 0.45],
0xefa33f,
"mineral"
);
makeExchangePath(
[-0.8, -0.48, 0.56],
[0.1, 0.08, 1.08],
[1.23, -0.64, 0.48],
0x25bbaa,
"carbon"
);
}
buildGround();
buildPlant();
buildFungi();
buildResources();
buildExchangePaths();
// ------------------------------------------------------------
// Simulation
// ------------------------------------------------------------
function resetSimulation() {
sim.plantSugar = 58;
sim.plantMinerals = 26;
sim.fungusCarbon = 38;
sim.fungusMinerals = 32;
sim.rates.mineral = 0;
sim.rates.carbon = 0;
sim.paymentPerUnit = 0;
}
function stepModel(dt) {
const waterFactor = 0.2 + 0.8 * (params.water / 100);
// Rule 1: photosynthesis minus plant respiration.
const photosynthesis =
(params.light / 100) *
(0.25 + 0.75 * (params.water / 100)) *
4.6;
const respiration = 0.95 + (1 - params.water / 100) * 1.35;
sim.plantSugar = clamp(
sim.plantSugar + (photosynthesis - respiration) * dt,
0,
100
);
// Direct root uptake continues alongside fungal exchange.
const nutrientTarget = (params.minerals / 100) * 72;
const directCapacity =
nutrientTarget *
(params.association ? 0.62 : 0.48) *
waterFactor;
const directUptake =
Math.max(0, directCapacity - sim.plantMinerals) * 0.12;
sim.plantMinerals = clamp(
sim.plantMinerals + (directUptake - 0.12) * dt,
0,
100
);
// Rule 2: fungi acquire minerals from soil.
const fungalActivity =
0.25 +
0.75 * clamp(sim.fungusCarbon / 45, 0, 1);
const miningRate =
(params.minerals / 100) *
1.5 *
(0.3 + 0.7 * (params.water / 100)) *
fungalActivity;
sim.fungusMinerals = clamp(
sim.fungusMinerals + miningRate * dt,
0,
85
);
let mineralDelivery = 0;
let carbonPayment = 0;
sim.paymentPerUnit =
(0.055 + (params.demand / 100) * 0.075) *
(params.carbonEff / 100);
if (params.association) {
const mineralDeficit = Math.max(
0,
nutrientTarget - sim.plantMinerals
);
// Rule 3: only the needed portion is delivered.
const potentialDelivery = Math.min(
sim.fungusMinerals,
mineralDeficit * (params.mineralEff / 100) * 0.18
);
if (params.reciprocal) {
// Rule 4: payment cannot consume the protected reserve.
const payableSugar = Math.max(
0,
sim.plantSugar - params.reserve
);
mineralDelivery = Math.min(
potentialDelivery,
payableSugar / Math.max(sim.paymentPerUnit, 0.0001)
);
carbonPayment = mineralDelivery * sim.paymentPerUnit;
} else {
// Credit mode: mineral is transferred first. Available sugar is paid.
mineralDelivery = potentialDelivery;
carbonPayment = Math.min(
sim.plantSugar,
mineralDelivery * sim.paymentPerUnit
);
}
sim.plantMinerals = clamp(
sim.plantMinerals + mineralDelivery,
0,
100
);
sim.fungusMinerals = clamp(
sim.fungusMinerals - mineralDelivery,
0,
85
);
sim.plantSugar = clamp(
sim.plantSugar - carbonPayment,
0,
100
);
sim.fungusCarbon = clamp(
sim.fungusCarbon + carbonPayment,
-10,
100
);
}
// Fungal carbon is spent on maintenance and mineral acquisition.
const fungalUpkeep = 0.015 + (params.demand / 100) * 0.06;
sim.fungusCarbon = clamp(
sim.fungusCarbon - fungalUpkeep * dt,
-10,
100
);
const smoothing = 1 - Math.exp(-dt * 5);
sim.rates.mineral = lerp(
sim.rates.mineral,
mineralDelivery / Math.max(dt, 0.0001),
smoothing
);
sim.rates.carbon = lerp(
sim.rates.carbon,
carbonPayment / Math.max(dt, 0.0001),
smoothing
);
}
// ------------------------------------------------------------
// UI
// ------------------------------------------------------------
const sliderIds = [
"light",
"water",
"minerals",
"demand",
"mineralEff",
"carbonEff",
"reserve"
];
function syncOutputs() {
sliderIds.forEach((id) => {
const value = Number($(`#${id}`).value);
params[id] = value;
$(`#${id}Out`).value = `${value}%`;
});
}
sliderIds.forEach((id) => {
$(`#${id}`).addEventListener("input", () => {
params[id] = Number($(`#${id}`).value);
$(`#${id}Out`).value = `${params[id]}%`;
});
});
$("#association").addEventListener("change", (event) => {
params.association = event.target.checked;
updateRuleText();
});
$("#reciprocal").addEventListener("change", (event) => {
params.reciprocal = event.target.checked;
updateRuleText();
});
function updateRuleText() {
if (!params.association) {
$("#ruleFive").textContent =
"Association is off. Direct root uptake continues, but no fungal mineral delivery or carbon payment occurs.";
} else if (!params.reciprocal) {
$("#ruleFive").textContent =
"Credit mode is active. The fungus transfers minerals first and the plant pays with available sugar afterward.";
} else {
$("#ruleFive").textContent =
"Reciprocal mode protects the plant’s sugar reserve. Fungus receives payment as minerals are delivered.";
}
}
document.querySelectorAll("[data-pulse]").forEach((button) => {
button.addEventListener("click", () => {
const resource = button.dataset.pulse;
params[resource] = clamp(params[resource] + 15, 0, 100);
$(`#${resource}`).value = params[resource];
$(`#${resource}Out`).value = `${params[resource]}%`;
});
});
$("#pause").addEventListener("click", () => {
running = !running;
$("#pause").textContent = running ? "Pause" : "Resume";
});
$("#resetModel").addEventListener("click", resetSimulation);
function healthValues() {
const plantHealth = clamp(
(
params.light * 0.22 +
params.water * 0.2 +
sim.plantMinerals * 0.38 +
sim.plantSugar * 0.2
) / 100,
0,
1
);
const fungusHealth = clamp(
(
Math.max(0, sim.fungusCarbon) * 0.55 +
sim.fungusMinerals * 0.25 +
params.minerals * 0.2
) / 100,
0,
1
);
return { plantHealth, fungusHealth };
}
function updateDashboard() {
const { plantHealth, fungusHealth } = healthValues();
$("#plantHealthBar").style.width = `${plantHealth * 100}%`;
$("#fungusHealthBar").style.width = `${fungusHealth * 100}%`;
$("#plantHealthText").textContent = `${Math.round(plantHealth * 100)}%`;
$("#fungusHealthText").textContent = `${Math.round(fungusHealth * 100)}%`;
$("#sugarValue").textContent = `${Math.round(sim.plantSugar)}%`;
$("#plantMineralValue").textContent = `${Math.round(sim.plantMinerals)}%`;
$("#mineralFlow").textContent = `${sim.rates.mineral.toFixed(2)}/s`;
$("#carbonFlow").textContent = `${sim.rates.carbon.toFixed(2)}/s`;
let status = "Mutualism active";
let color = "#e99a37";
if (!params.association) {
status = "Independent nutrition";
color = "#8d9891";
} else if (params.water < 25) {
status = "Water stress";
color = "#e0644f";
} else if (sim.plantSugar < 14) {
status = "Plant carbon stress";
color = "#e0644f";
} else if (sim.fungusCarbon < 10) {
status = "Fungus carbon stress";
color = "#a06aaa";
} else if (sim.rates.mineral < 0.03) {
status = "Association mostly idle";
color = "#809087";
}
$("#statusText").textContent = status;
$("#statusDot").style.background = color;
$("#statusDot").style.boxShadow = `0 0 0 4px ${color}22`;
}
// ------------------------------------------------------------
// Direct camera controls — no external controls addon
// ------------------------------------------------------------
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.classList.add("dragging");
});
canvas.addEventListener("pointermove", (event) => {
if (!dragging) return;
const dx = event.clientX - pointerX;
const dy = event.clientY - pointerY;
pointerX = event.clientX;
pointerY = event.clientY;
cameraState.yaw -= dx * 0.006;
cameraState.pitch = clamp(
cameraState.pitch + dy * 0.005,
0.48,
1.38
);
});
function endDrag(event) {
dragging = false;
canvas.classList.remove("dragging");
if (event.pointerId !== undefined) {
try {
canvas.releasePointerCapture(event.pointerId);
} catch (_) {}
}
}
canvas.addEventListener("pointerup", endDrag);
canvas.addEventListener("pointercancel", endDrag);
canvas.addEventListener(
"wheel",
(event) => {
event.preventDefault();
cameraState.radius = clamp(
cameraState.radius * Math.exp(event.deltaY * 0.001),
5.4,
14
);
},
{ passive: false }
);
function resetView() {
cameraState.yaw = 0.58;
cameraState.pitch = 1.02;
cameraState.radius = 8.8;
cameraState.target.set(0, 0.35, 0);
}
$("#resetView").addEventListener("click", resetView);
function updateCamera() {
const { yaw, pitch, radius, target } = cameraState;
camera.position.set(
target.x + radius * Math.sin(pitch) * Math.sin(yaw),
target.y + radius * Math.cos(pitch),
target.z + radius * Math.sin(pitch) * Math.cos(yaw)
);
camera.lookAt(target);
}
// ------------------------------------------------------------
// Labels and resize
// ------------------------------------------------------------
const labels = [
{
element: $("#plantLabel"),
position: new THREE.Vector3(-1.1, 2.85, 0)
},
{
element: $("#fungusLabel"),
position: new THREE.Vector3(1.7, 1.0, 0)
},
{
element: $("#soilLabel"),
position: new THREE.Vector3(2.65, -1.05, 0.8)
}
];
function updateLabels() {
const width = viewport.clientWidth;
const height = viewport.clientHeight;
labels.forEach(({ element, position }) => {
const projected = position.clone().project(camera);
if (
projected.z < -1 ||
projected.z > 1 ||
Math.abs(projected.x) > 1.15 ||
Math.abs(projected.y) > 1.15
) {
element.style.display = "none";
return;
}
element.style.display = "block";
element.style.left = `${(projected.x * 0.5 + 0.5) * width}px`;
element.style.top = `${(-projected.y * 0.5 + 0.5) * height}px`;
});
}
function resize() {
const width = viewport.clientWidth;
const height = viewport.clientHeight;
renderer.setSize(width, height, false);
camera.aspect = width / height;
camera.updateProjectionMatrix();
}
new ResizeObserver(resize).observe(viewport);
resize();
syncOutputs();
updateRuleText();
// ------------------------------------------------------------
// Animation
// ------------------------------------------------------------
const clock = new THREE.Clock();
let elapsed = 0;
let dashboardTimer = 0;
function updateVisuals(time, dt) {
const { plantHealth, fungusHealth } = healthValues();
leaves.forEach((leaf, index) => {
const sway =
Math.sin(time * 1.35 + leaf.userData.phase) *
0.018 *
(0.45 + params.water / 100);
leaf.rotation.z = sway;
leaf.rotation.x =
leaf.userData.baseZ +
Math.sin(time * 0.85 + index) * 0.012;
const healthyScale = 0.92 + plantHealth * 0.12;
leaf.scale.setScalar(healthyScale);
});
mushroomCaps.forEach((cap) => {
cap.position.y =
cap.userData.baseY +
Math.sin(time * 1.1 + cap.userData.phase) * 0.012;
cap.scale.set(
0.94 + fungusHealth * 0.12,
0.52 + fungusHealth * 0.09,
0.94 + fungusHealth * 0.12
);
});
const sun = world.getObjectByName("sun");
if (sun) {
const sunScale = 0.76 + (params.light / 100) * 0.34;
sun.scale.setScalar(sunScale);
sun.rotation.y += dt * 0.35;
}
mineralCrystals.forEach((crystal, index) => {
const visible = index < Math.ceil((params.minerals / 100) * 10);
crystal.visible = visible;
crystal.rotation.y += dt * (0.15 + index * 0.01);
crystal.material.emissiveIntensity =
0.1 + (params.minerals / 100) * 0.16;
});
exchangePaths.forEach((path) => {
const active =
path.name === "mineral"
? sim.rates.mineral > 0.025
: sim.rates.carbon > 0.006;
const targetOpacity = active ? 0.9 : 0.14;
path.path.material.opacity = lerp(
path.path.material.opacity,
targetOpacity,
1 - Math.exp(-dt * 5)
);
path.head.material.opacity = path.path.material.opacity;
path.particles.forEach((particle) => {
const phase = (time * (path.name === "mineral" ? 0.38 : 0.3)
+ particle.userData.phase) % 1;
particle.position.copy(path.curve.getPointAt(phase));
particle.material.opacity = active ? 0.95 : 0.16;
particle.rotation.x += dt * 1.2;
particle.rotation.y += dt * 1.6;
});
});
}
function animate() {
const dt = Math.min(clock.getDelta(), 0.05);
elapsed += dt;
if (running) {
stepModel(dt);
}
updateVisuals(elapsed, dt);
updateCamera();
updateLabels();
dashboardTimer += dt;
if (dashboardTimer > 0.12) {
updateDashboard();
dashboardTimer = 0;
}
renderer.render(scene, camera);
requestAnimationFrame(animate);
}
animate();
</script>
</body>
</html>
```