Prompt specification
Build or customize the Three.js 3D scene "Mixed garden root relationships" 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: Mixed garden root relationships
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/mixed-garden-root-relationships-x0743
Scene Description Prompt:
Build a self-contained HTML scene with Three.js from cdnjs featuring legume root nodules. Compose a small garden bed with legumes and neighboring crops above a root cutaway; highlight nodules and distinct resource pathways without promising automatic yield benefits. Voxel style.
Working Three.js Starter (mixed-garden-root-relationships-x0743):
```html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Legume Root Nodules — Voxel Cutaway</title>
<style>
:root {
color-scheme: dark;
--bg: #081313;
--panel: rgba(10, 28, 27, 0.86);
--panel-border: rgba(174, 255, 224, 0.16);
--text: #e7fff6;
--muted: #9bb8ad;
--mint: #58e2c3;
--gold: #ffca67;
--violet: #b69cff;
--pink: #ff7892;
}
* { box-sizing: border-box; }
html, body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: var(--bg);
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
color: var(--text);
}
button { font: inherit; }
#scene {
position: fixed;
inset: 0;
}
canvas {
display: block;
width: 100%;
height: 100%;
outline: none;
touch-action: none;
}
canvas:focus-visible {
outline: 2px solid var(--mint);
outline-offset: -3px;
}
#labels {
position: fixed;
inset: 0;
pointer-events: none;
z-index: 4;
overflow: hidden;
}
.scene-label {
position: absolute;
transform: translate(-50%, -50%);
white-space: nowrap;
padding: 6px 9px;
border: 1px solid rgba(184, 255, 229, 0.24);
border-radius: 6px;
background: rgba(7, 22, 21, 0.76);
box-shadow: 0 4px 18px rgba(0, 0, 0, 0.25);
color: #dff8ef;
font-size: 10px;
font-weight: 800;
letter-spacing: 0.12em;
backdrop-filter: blur(8px);
}
.scene-label::after {
content: "";
position: absolute;
left: 50%;
bottom: -10px;
width: 1px;
height: 9px;
background: rgba(184, 255, 229, 0.45);
}
.scene-label.nodule {
color: #ffd6df;
border-color: rgba(255, 120, 146, 0.72);
background: rgba(67, 20, 33, 0.82);
}
.scene-label.air {
color: #c9fff4;
border-color: rgba(88, 226, 195, 0.55);
}
.masthead {
position: fixed;
z-index: 10;
top: 24px;
left: 26px;
width: min(390px, calc(100vw - 390px));
pointer-events: none;
}
.eyebrow {
display: flex;
align-items: center;
gap: 9px;
margin-bottom: 9px;
color: var(--mint);
font-size: 10px;
font-weight: 900;
letter-spacing: 0.19em;
text-transform: uppercase;
}
.eyebrow::before {
content: "";
width: 24px;
height: 3px;
background: var(--mint);
box-shadow: 0 0 15px rgba(88, 226, 195, 0.55);
}
h1 {
margin: 0;
font-size: clamp(27px, 3.4vw, 49px);
line-height: 0.98;
letter-spacing: -0.045em;
text-wrap: balance;
}
.masthead p {
max-width: 350px;
margin: 13px 0 0;
color: var(--muted);
font-size: 13px;
line-height: 1.5;
}
.panel {
position: fixed;
z-index: 12;
top: 20px;
right: 20px;
width: 342px;
max-height: calc(100vh - 40px);
overflow: auto;
padding: 18px;
border: 1px solid var(--panel-border);
border-radius: 16px;
background: var(--panel);
box-shadow: 0 22px 70px rgba(0, 0, 0, 0.38);
backdrop-filter: blur(18px);
scrollbar-width: thin;
scrollbar-color: #315c50 transparent;
}
.panel-title {
display: flex;
align-items: center;
justify-content: space-between;
margin-bottom: 13px;
}
.panel-title strong {
font-size: 12px;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.model-tag {
padding: 5px 7px;
border: 1px solid rgba(182, 156, 255, 0.35);
border-radius: 5px;
color: #d9ceff;
background: rgba(182, 156, 255, 0.08);
font-size: 9px;
font-weight: 800;
letter-spacing: 0.09em;
text-transform: uppercase;
}
.path-list {
display: grid;
gap: 8px;
}
.path-button {
width: 100%;
display: grid;
grid-template-columns: 12px 1fr 26px;
align-items: center;
gap: 10px;
padding: 10px 11px;
border: 1px solid rgba(255, 255, 255, 0.075);
border-radius: 9px;
color: var(--text);
background: rgba(255, 255, 255, 0.035);
text-align: left;
cursor: pointer;
transition: opacity 160ms, border-color 160ms, background 160ms;
}
.path-button:hover {
background: rgba(255, 255, 255, 0.065);
border-color: rgba(255, 255, 255, 0.16);
}
.path-button.off {
opacity: 0.38;
}
.path-swatch {
width: 10px;
height: 10px;
border-radius: 2px;
background: var(--path-color);
box-shadow: 0 0 12px color-mix(in srgb, var(--path-color), transparent 45%);
}
.path-button.off .path-swatch {
background: transparent;
border: 1px solid var(--path-color);
box-shadow: none;
}
.path-copy strong {
display: block;
margin-bottom: 3px;
font-size: 11px;
letter-spacing: 0.075em;
text-transform: uppercase;
}
.path-copy span {
display: block;
color: var(--muted);
font-size: 10.5px;
line-height: 1.35;
}
.path-state {
color: var(--muted);
font-size: 17px;
text-align: center;
}
.detail {
margin-top: 15px;
padding: 13px;
border: 1px solid rgba(255, 120, 146, 0.23);
border-radius: 10px;
background: rgba(255, 120, 146, 0.055);
}
.detail-tag {
margin-bottom: 6px;
color: #ff9aae;
font-size: 9px;
font-weight: 900;
letter-spacing: 0.13em;
text-transform: uppercase;
}
.detail h2 {
margin: 0 0 7px;
font-size: 15px;
}
.detail p {
margin: 0;
color: #b9c9c2;
font-size: 11.5px;
line-height: 1.52;
}
.comparison {
margin-top: 12px;
padding: 12px 13px;
border-left: 3px solid var(--violet);
border-radius: 0 8px 8px 0;
background: rgba(182, 156, 255, 0.07);
color: #c3b8e9;
font-size: 11px;
line-height: 1.48;
}
.comparison strong {
color: #eee9ff;
}
.disclaimer {
margin-top: 12px;
padding-top: 12px;
border-top: 1px solid rgba(255, 255, 255, 0.09);
color: #9db1a8;
font-size: 10.5px;
line-height: 1.5;
}
.disclaimer strong {
color: #d2e5dc;
}
.panel-footer {
display: flex;
align-items: center;
justify-content: space-between;
gap: 12px;
margin-top: 13px;
}
.reset {
border: 1px solid rgba(88, 226, 195, 0.27);
border-radius: 7px;
padding: 8px 11px;
color: #bff7e7;
background: rgba(88, 226, 195, 0.07);
cursor: pointer;
font-size: 10px;
font-weight: 800;
letter-spacing: 0.07em;
text-transform: uppercase;
}
.reset:hover { background: rgba(88, 226, 195, 0.13); }
.not-scale {
color: #708c82;
font-size: 9px;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.camera-hint {
position: fixed;
z-index: 9;
left: 26px;
bottom: 23px;
display: flex;
align-items: center;
gap: 10px;
padding: 9px 12px;
border: 1px solid rgba(255, 255, 255, 0.08);
border-radius: 8px;
color: #8ca99e;
background: rgba(6, 19, 18, 0.64);
font-size: 10px;
pointer-events: none;
backdrop-filter: blur(8px);
}
.camera-hint b {
color: #cae7dd;
font-weight: 700;
}
.dot {
width: 6px;
height: 6px;
border-radius: 50%;
background: var(--mint);
box-shadow: 0 0 10px var(--mint);
}
#loading {
position: fixed;
z-index: 30;
inset: 0;
display: grid;
place-items: center;
color: var(--mint);
background: var(--bg);
font-size: 11px;
font-weight: 800;
letter-spacing: 0.16em;
text-transform: uppercase;
transition: opacity 300ms;
}
#loading.hidden {
opacity: 0;
pointer-events: none;
}
@media (max-width: 800px) {
.masthead {
top: 16px;
left: 16px;
width: calc(100vw - 32px);
}
.masthead p { display: none; }
h1 { font-size: 29px; }
.panel {
top: auto;
right: 10px;
bottom: 10px;
left: 10px;
width: auto;
max-height: 39vh;
padding: 13px;
border-radius: 13px;
}
.path-list {
grid-template-columns: repeat(3, minmax(0, 1fr));
}
.path-button {
grid-template-columns: 10px 1fr;
padding: 8px;
}
.path-state { display: none; }
.path-copy span { display: none; }
.path-copy strong { font-size: 9px; }
.comparison, .disclaimer { font-size: 9.5px; }
.camera-hint { display: none; }
.scene-label { font-size: 8px; padding: 5px 7px; }
}
@media (max-width: 480px) {
.panel {
max-height: 43vh;
}
.comparison { display: none; }
.path-list { gap: 5px; }
.path-button { gap: 6px; }
}
</style>
</head>
<body>
<main id="scene" aria-label="Interactive voxel garden bed showing legume root nodules"></main>
<div id="labels" aria-hidden="true">
<div class="scene-label air" id="label-air">AIR N₂</div>
<div class="scene-label" id="label-legume">LEGUME</div>
<div class="scene-label" id="label-crop">NEIGHBOR CROP</div>
<div class="scene-label nodule" id="label-nodule">ROOT NODULE</div>
<div class="scene-label" id="label-cutaway">OPEN ROOT CUTAWAY</div>
</div>
<header class="masthead">
<div class="eyebrow">Voxel botany / field cutaway</div>
<h1>Root nodules,<br />resource pathways.</h1>
<p>
Explore how legumes exchange resources with root-associated bacteria,
and how neighboring crops acquire nutrients from the soil.
</p>
</header>
<aside class="panel" aria-label="Scene information and pathway controls">
<div class="panel-title">
<strong>Resource pathways</strong>
<span class="model-tag">Schematic</span>
</div>
<div class="path-list">
<button
class="path-button"
data-path="nitrogen"
style="--path-color: var(--mint)"
aria-pressed="true"
>
<span class="path-swatch"></span>
<span class="path-copy">
<strong>Nitrogen fixation</strong>
<span>Air N₂ → nodule → legume</span>
</span>
<span class="path-state">−</span>
</button>
<button
class="path-button"
data-path="carbon"
style="--path-color: var(--gold)"
aria-pressed="true"
>
<span class="path-swatch"></span>
<span class="path-copy">
<strong>Carbon & energy</strong>
<span>Leaves → roots and nodule microbes</span>
</span>
<span class="path-state">−</span>
</button>
<button
class="path-button"
data-path="soil"
style="--path-color: var(--violet)"
aria-pressed="true"
>
<span class="path-swatch"></span>
<span class="path-copy">
<strong>Soil resources</strong>
<span>Water + minerals → plant roots</span>
</span>
<span class="path-state">−</span>
</button>
</div>
<section class="detail" aria-live="polite">
<div class="detail-tag" id="detail-tag">Interactive cutaway</div>
<h2 id="detail-title">Select a pink nodule</h2>
<p id="detail-text">
Pink voxel clusters identify root nodules. Select one to inspect the
plant–microbe relationship represented in this educational model.
</p>
</section>
<div class="comparison">
<strong>Neighbor-crop contrast:</strong> the maize and brassica are not
shown forming nitrogen-fixing nodules; their nitrogen supply is shown
coming from the soil and other farm inputs.
</div>
<div class="disclaimer">
<strong>Not a yield forecast.</strong>
Biological nitrogen fixation can contribute plant-available nitrogen
when the host, rhizobia, and field conditions are suitable. It does not
guarantee yield gains or replace soil testing, fertilization, or other
management.
</div>
<div class="panel-footer">
<button class="reset" id="reset-view">Reset view</button>
<span class="not-scale">Not to scale</span>
</div>
</aside>
<div class="camera-hint">
<span class="dot"></span>
<span><b>Drag</b> to orbit · <b>Scroll / pinch</b> to zoom · <b>Arrow keys</b> to rotate</span>
</div>
<div id="loading">Building voxel garden…</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// ------------------------------------------------------------
// Renderer, scene, and camera
// ------------------------------------------------------------
const sceneHost = document.querySelector("#scene");
const renderer = new THREE.WebGLRenderer({
antialias: true,
alpha: false,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.08;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.domElement.tabIndex = 0;
renderer.domElement.setAttribute(
"aria-label",
"3D voxel garden bed. Drag to orbit, scroll or pinch to zoom, and select pink root nodules."
);
sceneHost.appendChild(renderer.domElement);
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x081313);
scene.fog = new THREE.FogExp2(0x081313, 0.017);
const camera = new THREE.OrthographicCamera(-10, 10, 8, -8, 0.1, 100);
const view = {
theta: 0.66,
phi: 1.03,
zoom: 1,
target: new THREE.Vector3(-0.15, -0.35, 0),
targetGoal: new THREE.Vector3(-0.15, -0.35, 0)
};
const hemisphere = new THREE.HemisphereLight(0xb9fff0, 0x273126, 2.1);
scene.add(hemisphere);
const sun = new THREE.DirectionalLight(0xfff3d4, 3.3);
sun.position.set(-7, 12, 8);
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 = -9;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 35;
sun.shadow.normalBias = 0.025;
scene.add(sun);
const rim = new THREE.DirectionalLight(0x5be8cf, 1.15);
rim.position.set(9, 5, -8);
scene.add(rim);
const floor = new THREE.Mesh(
new THREE.PlaneGeometry(60, 60),
new THREE.MeshStandardMaterial({
color: 0x0c1c1b,
roughness: 1
})
);
floor.rotation.x = -Math.PI / 2;
floor.position.y = -3.18;
floor.receiveShadow = true;
scene.add(floor);
const grid = new THREE.GridHelper(34, 34, 0x315b51, 0x18332f);
grid.position.y = -3.165;
grid.material.transparent = true;
grid.material.opacity = 0.24;
scene.add(grid);
// ------------------------------------------------------------
// Shared voxel helpers
// ------------------------------------------------------------
const unitBox = new THREE.BoxGeometry(1, 1, 1);
const materialCache = new Map();
const up = new THREE.Vector3(0, 1, 0);
function material(color) {
if (!materialCache.has(color)) {
materialCache.set(
color,
new THREE.MeshStandardMaterial({
color,
roughness: 0.86,
metalness: 0.015
})
);
}
return materialCache.get(color);
}
function cube(
parent,
width,
height,
depth,
x,
y,
z,
color,
rotation = null
) {
const mesh = new THREE.Mesh(
unitBox,
typeof color === "number" ? material(color) : color
);
mesh.scale.set(width, height, depth);
mesh.position.set(x, y, z);
if (rotation) mesh.rotation.set(rotation.x, rotation.y, rotation.z);
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
function beam(parent, a, b, width, color, depth = width) {
const start = new THREE.Vector3(...a);
const end = new THREE.Vector3(...b);
const direction = end.clone().sub(start);
const length = direction.length();
const mesh = new THREE.Mesh(
unitBox,
typeof color === "number" ? material(color) : color
);
mesh.scale.set(width, length, depth);
mesh.position.copy(start).add(end).multiplyScalar(0.5);
mesh.quaternion.setFromUnitVectors(up, direction.normalize());
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
let randomSeed = 1907;
function random() {
randomSeed = (randomSeed * 1664525 + 1013904223) >>> 0;
return randomSeed / 4294967296;
}
function randomBetween(min, max) {
return min + (max - min) * random();
}
// ------------------------------------------------------------
// Raised bed and exposed soil section
// ------------------------------------------------------------
const garden = new THREE.Group();
scene.add(garden);
const soil = cube(
garden,
11.3,
3.6,
7,
0,
-1.15,
-0.5,
0x503729
);
soil.castShadow = true;
cube(garden, 11.45, 0.16, 7.12, 0, 0.65, -0.5, 0x3e4930);
cube(garden, 11.1, 0.055, 6.86, 0, 0.74, -0.5, 0x56613a);
// Front strata remain behind the roots.
cube(garden, 11.15, 0.55, 0.045, 0, -0.08, 3.018, 0x5a3e2c);
cube(garden, 11.15, 0.68, 0.045, 0, -0.78, 3.020, 0x493329);
cube(garden, 11.15, 0.82, 0.045, 0, -1.62, 3.022, 0x3c3028);
cube(garden, 11.15, 0.68, 0.045, 0, -2.37, 3.024, 0x342b26);
// Angular soil flecks on the cut face.
const soilColors = [0x5d4030, 0x694832, 0x49352c, 0x76513a];
for (let i = 0; i < 82; i++) {
const size = randomBetween(0.12, 0.31);
const fleck = cube(
garden,
size,
size * randomBetween(0.55, 1.1),
randomBetween(0.025, 0.055),
randomBetween(-5.35, 5.35),
randomBetween(-2.83, 0.48),
3.045,
soilColors[Math.floor(random() * soilColors.length)]
);
fleck.castShadow = false;
}
// Topsoil clumps.
for (let i = 0; i < 50; i++) {
const size = randomBetween(0.12, 0.32);
const clump = cube(
garden,
size,
randomBetween(0.04, 0.10),
size,
randomBetween(-5.25, 5.25),
0.79,
randomBetween(-3.65, 2.75),
soilColors[Math.floor(random() * soilColors.length)]
);
clump.castShadow = false;
}
const wood = 0x795039;
const woodDark = 0x4f3427;
// Side and back rails.
cube(garden, 0.42, 1.45, 7.65, -5.92, 0.48, -0.5, wood);
cube(garden, 0.42, 1.45, 7.65, 5.92, 0.48, -0.5, wood);
cube(garden, 12.25, 1.45, 0.42, 0, 0.48, -4.18, wood);
// The front is intentionally open; only a thin top lip remains.
cube(garden, 12.25, 0.28, 0.42, 0, 0.88, 3.72, wood);
cube(garden, 0.50, 1.95, 0.50, -5.92, 0.42, 3.72, woodDark);
cube(garden, 0.50, 1.95, 0.50, 5.92, 0.42, 3.72, woodDark);
// Small voxel fasteners.
for (const x of [-5.92, 5.92]) {
for (const z of [-4.18, 3.72]) {
cube(garden, 0.12, 0.12, 0.12, x, 0.72, z + 0.225, 0xc8a272);
}
}
// ------------------------------------------------------------
// Root systems and nodules
// ------------------------------------------------------------
const rootsGroup = new THREE.Group();
scene.add(rootsGroup);
const noduleClusters = [];
const nodulePickables = [];
function makeNodule(x, y, z, legumeLabel) {
const group = new THREE.Group();
group.position.set(x, y, z);
const noduleMaterial = new THREE.MeshStandardMaterial({
color: 0xff7892,
emissive: 0x6d1830,
emissiveIntensity: 0.48,
roughness: 0.56,
metalness: 0.02
});
const pieces = [
cube(group, 0.31, 0.25, 0.25, 0, 0, 0, noduleMaterial),
cube(group, 0.20, 0.22, 0.22, 0.23, 0.08, 0.025, noduleMaterial),
cube(group, 0.17, 0.18, 0.18, -0.19, -0.09, 0.035, noduleMaterial)
];
group.userData.material = noduleMaterial;
group.userData.legumeLabel = legumeLabel;
group.userData.baseScale = 1;
for (const piece of pieces) {
piece.userData.cluster = group;
nodulePickables.push(piece);
}
noduleClusters.push(group);
return group;
}
function makeRootSystem(x, withNodules, legumeLabel) {
const group = new THREE.Group();
rootsGroup.add(group);
const z = 3.075;
const rootColor = withNodules ? 0xd8b88a : 0xb8a080;
const trunk = [
[x, 0.59, z],
[x + 0.07, -0.18, z],
[x - 0.03, -1.02, z],
[x + 0.08, -2.55, z]
];
for (let i = 0; i < trunk.length - 1; i++) {
beam(group, trunk[i], trunk[i + 1], 0.13, rootColor);
}
const branches = [
[[x + 0.06, -0.22, z], [x - 0.48, -0.42, z], [x - 1.02, -0.84, z]],
[[x + 0.03, -0.55, z], [x + 0.44, -0.92, z], [x + 0.94, -1.28, z]],
[[x - 0.02, -1.08, z], [x - 0.43, -1.38, z], [x - 0.86, -1.83, z]],
[[x - 0.02, -1.48, z], [x + 0.35, -1.74, z], [x + 0.71, -2.13, z]],
[[x + 0.04, -1.98, z], [x - 0.28, -2.22, z], [x - 0.58, -2.48, z]]
];
for (const branch of branches) {
beam(group, branch[0], branch[1], 0.095, rootColor);
beam(group, branch[1], branch[2], 0.075, rootColor);
}
// Fine lateral roots.
beam(group, [x - 0.48, -0.42, z], [x - 0.67, -0.08, z], 0.055, rootColor);
beam(group, [x + 0.44, -0.92, z], [x + 0.66, -0.61, z], 0.05, rootColor);
beam(group, [x - 0.43, -1.38, z], [x - 0.67, -1.20, z], 0.05, rootColor);
beam(group, [x + 0.35, -1.74, z], [x + 0.57, -1.46, z], 0.045, rootColor);
if (withNodules) {
makeNodule(x - 0.65, -1.10, 3.17, legumeLabel);
makeNodule(x + 0.55, -1.55, 3.17, legumeLabel);
makeNodule(x - 0.25, -2.08, 3.17, legumeLabel);
makeNodule(x + 0.28, -0.78, 3.17, legumeLabel);
}
return group;
}
makeRootSystem(-2.7, true, "left legume");
makeRootSystem(-0.55, true, "right legume");
makeRootSystem(2.65, false, "neighbor crop");
makeRootSystem(4.15, false, "neighbor crop");
// ------------------------------------------------------------
// Above-ground voxel plants
// ------------------------------------------------------------
const plants = [];
function makeLegume(x, z, phase) {
const group = new THREE.Group();
group.position.set(x, 0.74, z);
scene.add(group);
const stem = 0x477a43;
const leafDark = 0x3f713a;
const leafLight = 0x70a84b;
const pod = 0xb7bd50;
beam(group, [0, 0, 0], [0.08, 1.95, 0], 0.13, stem);
beam(group, [-0.64, 0, 0.03], [-0.51, 2.25, 0.03], 0.08, 0x8d6843);
const levels = [0.42, 0.83, 1.22, 1.58];
levels.forEach((level, index) => {
const direction = index % 2 === 0 ? -1 : 1;
beam(
group,
[0.03, level, 0],
[direction * 0.62, level + 0.18, 0],
0.075,
stem
);
const leaf = cube(
group,
0.65,
0.17,
0.35,
direction * 0.59,
level + 0.19,
0,
index % 3 === 0 ? leafLight : leafDark
);
leaf.rotation.y = direction * 0.38;
leaf.rotation.z = direction * -0.15;
const leaf2 = cube(
group,
0.45,
0.15,
0.28,
direction * 0.33,
level + 0.37,
0.04,
leafDark
);
leaf2.rotation.y = direction * -0.48;
leaf2.rotation.z = direction * 0.16;
});
cube(group, 0.16, 0.64, 0.19, -0.48, 0.75, 0.02, pod);
cube(group, 0.14, 0.55, 0.17, 0.47, 1.13, -0.03, pod);
cube(group, 0.13, 0.13, 0.13, -0.11, 1.50, 0, 0xf2cf67);
group.userData.sway = true;
group.userData.phase = phase;
plants.push(group);
return group;
}
makeLegume(-2.7, 1.55, 0.2);
makeLegume(-0.55, 1.88, 1.7);
function makeMaize(x, z) {
const group = new THREE.Group();
group.position.set(x, 0.74, z);
scene.add(group);
const stalk = 0x628442;
const leaf = 0x6f9c40;
beam(group, [0, 0, 0], [0.08, 3.35, 0], 0.16, stalk);
const levels = [0.58, 1.02, 1.48, 1.95, 2.42];
levels.forEach((level, index) => {
const direction = index % 2 === 0 ? -1 : 1;
beam(
group,
[0.04, level, 0],
[direction * 0.82, level + 0.38, 0],
0.10,
leaf
);
const blade = cube(
group,
0.88,
0.13,
0.27,
direction * 0.62,
level + 0.31,
0,
index % 2 ? leaf : 0x82a94a
);
blade.rotation.z = direction * -0.34;
blade.rotation.y = direction * 0.22;
});
cube(group, 0.27, 0.66, 0.28, -0.19, 1.45, 0.05, 0xe0b347);
cube(group, 0.22, 0.57, 0.25, 0.21, 2.12, -0.03, 0xf0c351);
for (const xOffset of [-0.12, 0, 0.12]) {
cube(group, 0.09, 0.09, 0.09, xOffset, 1.25 + (xOffset + 0.12) * 0.8, 0.22, 0xc98e2e);
cube(group, 0.09, 0.09, 0.09, xOffset, 1.47 + (xOffset + 0.12) * 0.8, 0.22, 0xc98e2e);
}
beam(group, [0.08, 3.25, 0], [-0.18, 3.78, 0], 0.055, 0x9b733f);
beam(group, [0.08, 3.25, 0], [0.30, 3.78, 0], 0.055, 0x9b733f);
beam(group, [0.08, 3.25, 0], [0.08, 3.85, 0], 0.055, 0x9b733f);
group.userData.sway = true;
group.userData.phase = 3.1;
plants.push(group);
}
makeMaize(2.65, 1.15);
function makeBrassica(x, z) {
const group = new THREE.Group();
group.position.set(x, 0.74, z);
scene.add(group);
cube(group, 0.24, 0.34, 0.24, 0, 0.18, 0, 0x4d7339);
for (let i = 0; i < 8; i++) {
const angle = (i / 8) * Math.PI * 2;
const leaf = cube(
group,
0.70,
0.18,
0.34,
Math.cos(angle) * 0.36,
0.22 + (i % 2) * 0.08,
Math.sin(angle) * 0.36,
i % 2 ? 0x638c43 : 0x789e4c
);
leaf.rotation.y = -angle;
leaf.rotation.z = -0.18;
}
group.userData.sway = true;
group.userData.phase = 4.2;
plants.push(group);
}
makeBrassica(4.15, -1.0);
// Atmospheric N₂ cloud.
const cloud = new THREE.Group();
cloud.position.set(-4.08, 3.83, -0.22);
scene.add(cloud);
const cloudMaterial = new THREE.MeshStandardMaterial({
color: 0xdaf8f1,
emissive: 0x5faea0,
emissiveIntensity: 0.12,
transparent: true,
opacity: 0.84,
roughness: 1
});
const cloudBlocks = [
[-0.55, 0, 0, 0.9, 0.42, 0.55],
[0, 0.15, 0, 1.05, 0.58, 0.65],
[0.58, 0, 0, 0.85, 0.38, 0.5],
[-0.15, 0.34, 0.05, 0.72, 0.42, 0.5]
];
for (const block of cloudBlocks) {
const mesh = cube(cloud, ...block, cloudMaterial);
mesh.castShadow = false;
}
// ------------------------------------------------------------
// Resource pathways
// ------------------------------------------------------------
const pathMap = new Map();
const coneGeometry = new THREE.ConeGeometry(0.11, 0.31, 4);
const packetGeometry = new THREE.BoxGeometry(0.13, 0.13, 0.13);
function sampleRoute(route, t) {
const distance = t * route.total;
let segment = route.points.length - 2;
for (let i = 0; i < route.points.length - 1; i++) {
if (distance <= route.cumulative[i + 1]) {
segment = i;
break;
}
}
const a = route.points[segment];
const b = route.points[segment + 1];
const segmentLength = route.cumulative[segment + 1] - route.cumulative[segment];
const localT = segmentLength === 0 ? 0 : (distance - route.cumulative[segment]) / segmentLength;
return {
position: a.clone().lerp(b, localT),
tangent: b.clone().sub(a).normalize()
};
}
function createRoute(parent, points, color) {
const route = {
points: points.map((point) => new THREE.Vector3(...point)),
cumulative: [0],
total: 0,
packets: []
};
for (let i = 0; i < route.points.length - 1; i++) {
route.total += route.points[i].distanceTo(route.points[i + 1]);
route.cumulative.push(route.total);
}
const geometry = new THREE.BufferGeometry().setFromPoints(route.points);
const lineMaterial = new THREE.LineDashedMaterial({
color,
dashSize: 0.19,
gapSize: 0.12,
transparent: true,
opacity: 0.86,
depthTest: false,
depthWrite: false
});
const line = new THREE.Line(geometry, lineMaterial);
line.computeLineDistances();
line.renderOrder = 8;
parent.add(line);
const markerMaterial = new THREE.MeshBasicMaterial({
color,
transparent: true,
opacity: 0.95,
depthTest: false,
depthWrite: false
});
for (const t of [0.17, 0.37, 0.58, 0.78, 0.94]) {
const sample = sampleRoute(route, t);
const marker = new THREE.Mesh(coneGeometry, markerMaterial);
marker.position.copy(sample.position);
marker.quaternion.setFromUnitVectors(up, sample.tangent);
marker.renderOrder = 9;
parent.add(marker);
}
const packetMaterial = new THREE.MeshBasicMaterial({
color: 0xffffff,
depthTest: false,
depthWrite: false
});
for (let i = 0; i < 4; i++) {
const packet = new THREE.Mesh(packetGeometry, packetMaterial);
packet.material = new THREE.MeshBasicMaterial({
color,
depthTest: false,
depthWrite: false
});
packet.renderOrder = 10;
parent.add(packet);
route.packets.push(packet);
}
return route;
}
function createPath(name, color, routes) {
const group = new THREE.Group();
scene.add(group);
const path = {
group,
routes: routes.map((points) => createRoute(group, points, color))
};
pathMap.set(name, path);
return path;
}
createPath("nitrogen", 0x58e2c3, [
[
[-4.08, 4.22, -0.22],
[-4.08, 2.55, 0.35],
[-4.02, 0.65, 2.45],
[-3.35, -1.10, 3.18],
[-3.10, -0.42, 3.18],
[-2.70, 0.92, 1.55]
]
]);
createPath("carbon", 0xffca67, [
[
[-2.62, 2.15, 1.55],
[-2.62, 0.82, 2.42],
[-3.04, -0.50, 3.12],
[-3.35, -1.10, 3.18]
]
]);
createPath("soil", 0xb69cff, [
[
[-0.10, -2.68, 3.13],
[-0.42, -1.85, 3.13],
[-0.55, -0.88, 3.13],
[-0.55, 0.12, 3.12],
[-0.55, 0.86, 1.88]
],
[
[1.78, -2.68, 3.13],
[2.25, -1.83, 3.13],
[2.63, -0.86, 3.13],
[2.65, 0.12, 3.12],
[2.65, 0.87, 1.15]
]
]);
// ------------------------------------------------------------
// Labels
// ------------------------------------------------------------
const labelDefinitions = [
{
element: document.querySelector("#label-air"),
position: new THREE.Vector3(-4.08, 4.62, -0.22)
},
{
element: document.querySelector("#label-legume"),
position: new THREE.Vector3(-2.7, 2.92, 1.55)
},
{
element: document.querySelector("#label-crop"),
position: new THREE.Vector3(2.65, 4.43, 1.15)
},
{
element: document.querySelector("#label-nodule"),
position: new THREE.Vector3(-3.35, -1.10, 3.30)
},
{
element: document.querySelector("#label-cutaway"),
position: new THREE.Vector3(0.7, -2.75, 3.2)
}
];
function updateLabels() {
const width = window.innerWidth;
const height = window.innerHeight;
for (const label of labelDefinitions) {
const projected = label.position.clone().project(camera);
const visible = projected.z > -1 && projected.z < 1;
label.element.style.display = visible ? "block" : "none";
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 controls: orbit, zoom, keyboard, touch pinch
// ------------------------------------------------------------
function updateCamera() {
const aspect = window.innerWidth / window.innerHeight;
const baseHeight = Math.max(14.4, 13.5 / aspect);
const height = baseHeight / view.zoom;
camera.left = (-height * aspect) / 2;
camera.right = (height * aspect) / 2;
camera.top = height / 2;
camera.bottom = -height / 2;
camera.updateProjectionMatrix();
view.target.lerp(view.targetGoal, 0.09);
const radius = 28;
const sinPhi = Math.sin(view.phi);
camera.position.set(
view.target.x + radius * sinPhi * Math.sin(view.theta),
view.target.y + radius * Math.cos(view.phi),
view.target.z + radius * sinPhi * Math.cos(view.theta)
);
camera.lookAt(view.target);
}
function resize() {
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
updateCamera();
}
window.addEventListener("resize", resize);
const activePointers = new Map();
let pinchDistance = 0;
let pointerTravel = 0;
function getPinchDistance() {
const points = [...activePointers.values()];
if (points.length < 2) return 0;
return Math.hypot(points[0].x - points[1].x, points[0].y - points[1].y);
}
renderer.domElement.addEventListener("pointerdown", (event) => {
renderer.domElement.setPointerCapture(event.pointerId);
activePointers.set(event.pointerId, { x: event.clientX, y: event.clientY });
pointerTravel = 0;
if (activePointers.size === 2) {
pinchDistance = getPinchDistance();
}
});
renderer.domElement.addEventListener("pointermove", (event) => {
const previous = activePointers.get(event.pointerId);
if (!previous) return;
const dx = event.clientX - previous.x;
const dy = event.clientY - previous.y;
activePointers.set(event.pointerId, { x: event.clientX, y: event.clientY });
pointerTravel += Math.hypot(dx, dy);
if (activePointers.size === 1) {
view.theta -= dx * 0.006;
view.phi = THREE.MathUtils.clamp(view.phi + dy * 0.005, 0.50, 1.35);
} else if (activePointers.size === 2) {
const nextDistance = getPinchDistance();
if (pinchDistance > 0 && nextDistance > 0) {
view.zoom = THREE.MathUtils.clamp(
view.zoom * (pinchDistance / nextDistance),
0.68,
1.75
);
}
pinchDistance = nextDistance;
}
});
function releasePointer(event) {
activePointers.delete(event.pointerId);
pinchDistance = getPinchDistance();
if (activePointers.size === 0 && pointerTravel < 6) {
selectNoduleAt(event.clientX, event.clientY);
}
}
renderer.domElement.addEventListener("pointerup", releasePointer);
renderer.domElement.addEventListener("pointercancel", releasePointer);
renderer.domElement.addEventListener(
"wheel",
(event) => {
event.preventDefault();
view.zoom = THREE.MathUtils.clamp(
view.zoom * Math.exp(-event.deltaY * 0.001),
0.68,
1.75
);
},
{ passive: false }
);
renderer.domElement.addEventListener("keydown", (event) => {
let handled = true;
switch (event.key) {
case "ArrowLeft":
view.theta -= 0.09;
break;
case "ArrowRight":
view.theta += 0.09;
break;
case "ArrowUp":
view.phi = Math.max(0.5, view.phi - 0.07);
break;
case "ArrowDown":
view.phi = Math.min(1.35, view.phi + 0.07);
break;
case "+":
case "=":
view.zoom = Math.min(1.75, view.zoom + 0.08);
break;
case "-":
view.zoom = Math.max(0.68, view.zoom - 0.08);
break;
case "r":
case "R":
resetView();
break;
default:
handled = false;
}
if (handled) event.preventDefault();
});
function resetView() {
view.theta = 0.66;
view.phi = 1.03;
view.zoom = 1;
view.targetGoal.set(-0.15, -0.35, 0);
}
document.querySelector("#reset-view").addEventListener("click", resetView);
// ------------------------------------------------------------
// Path controls and nodule inspection
// ------------------------------------------------------------
document.querySelectorAll(".path-button").forEach((button) => {
button.addEventListener("click", () => {
const path = pathMap.get(button.dataset.path);
if (!path) return;
path.group.visible = !path.group.visible;
button.classList.toggle("off", !path.group.visible);
button.setAttribute("aria-pressed", String(path.group.visible));
button.querySelector(".path-state").textContent =
path.group.visible ? "−" : "+";
});
});
const raycaster = new THREE.Raycaster();
const pointer = new THREE.Vector2();
let selectedCluster = null;
const detailTag = document.querySelector("#detail-tag");
const detailTitle = document.querySelector("#detail-title");
const detailText = document.querySelector("#detail-text");
function selectNoduleAt(clientX, clientY) {
const rect = renderer.domElement.getBoundingClientRect();
pointer.x = ((clientX - rect.left) / rect.width) * 2 - 1;
pointer.y = -((clientY - rect.top) / rect.height) * 2 + 1;
raycaster.setFromCamera(pointer, camera);
const intersections = raycaster.intersectObjects(nodulePickables, false);
if (!intersections.length) {
selectedCluster = null;
detailTag.textContent = "Interactive cutaway";
detailTitle.textContent = "Select a pink nodule";
detailText.textContent =
"Pink voxel clusters identify root nodules. Select one to inspect the plant–microbe relationship represented in this educational model.";
return;
}
selectedCluster = intersections[0].object.userData.cluster;
detailTag.textContent = "Rhizobia × host plant";
detailTitle.textContent = `Nodule on ${selectedCluster.userData.legumeLabel}`;
detailText.textContent =
"Rhizobia living inside this root structure convert atmospheric N₂ into biologically available forms. The plant supplies carbon compounds and receives nitrogen in an exchange. Effectiveness depends on the partnership, plant, soil, moisture, and management.";
}
// ------------------------------------------------------------
// Animation
// ------------------------------------------------------------
const clock = new THREE.Clock();
function animate() {
const elapsed = clock.getElapsedTime();
// Gentle plant movement.
for (const plant of plants) {
plant.rotation.z = Math.sin(elapsed * 0.72 + plant.userData.phase) * 0.014;
plant.rotation.x = Math.cos(elapsed * 0.53 + plant.userData.phase) * 0.006;
}
// Atmospheric cloud drift.
cloud.position.x = -4.08 + Math.sin(elapsed * 0.35) * 0.10;
cloud.position.y = 3.83 + Math.sin(elapsed * 0.47) * 0.05;
// Moving pathway packets.
for (const path of pathMap.values()) {
for (const route of path.routes) {
route.packets.forEach((packet, index) => {
const t = (elapsed * 0.085 + index / route.packets.length) % 1;
const sample = sampleRoute(route, t);
packet.position.copy(sample.position);
packet.rotation.set(elapsed * 1.4, elapsed * 1.1, 0);
});
}
}
// Nodule selection pulse.
for (const cluster of noduleClusters) {
const isSelected = cluster === selectedCluster;
const targetScale = isSelected
? 1.12 + Math.sin(elapsed * 5.2) * 0.055
: 1;
cluster.scale.lerp(
new THREE.Vector3(targetScale, targetScale, targetScale),
0.16
);
cluster.userData.material.emissiveIntensity = isSelected ? 0.95 : 0.48;
}
updateCamera();
updateLabels();
renderer.render(scene, camera);
requestAnimationFrame(animate);
}
updateCamera();
animate();
document.querySelector("#loading").classList.add("hidden");
</script>
</body>
</html>
```