Prompt specification
Build or customize the Three.js 3D scene "Pond drop food-web diorama" 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: Pond drop food-web diorama
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/pond-drop-food-web-diorama-x0859
Scene Description Prompt:
Make a standalone HTML scene with Three.js from cdnjs that depicts microbial food webs. Compose algae-like cells, bacteria-like dots, protists, and organic particles in an enlarged water droplet; use directed feeding arrows separate from physical swimming paths. Voxel style.
Working Three.js Starter (pond-drop-food-web-diorama-x0859):
```html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Microbial Food Web — Voxel Water Droplet</title>
<style>
:root {
--bg: #06151d;
--panel: rgba(7, 24, 33, 0.78);
--border: rgba(142, 225, 244, 0.18);
--text: #e6f8fb;
--muted: #89aeb8;
--cyan: #65d8ee;
--amber: #ffbd59;
--algae: #72ed83;
--bacteria: #ff6f93;
--protist: #7ba8ff;
--detritus: #ca8b4d;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background:
radial-gradient(circle at 50% 42%, #123744 0%, #071d27 42%, #030b10 100%);
color: var(--text);
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
button {
font: inherit;
}
#scene {
position: fixed;
inset: 0;
}
#scene canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
outline: none;
}
.ui {
position: fixed;
inset: 0;
pointer-events: none;
z-index: 10;
}
.panel {
border: 1px solid var(--border);
background: var(--panel);
box-shadow:
0 18px 50px rgba(0, 0, 0, 0.28),
inset 0 1px 0 rgba(255, 255, 255, 0.04);
backdrop-filter: blur(14px);
-webkit-backdrop-filter: blur(14px);
}
.topbar {
position: absolute;
top: 22px;
left: 22px;
right: 22px;
display: flex;
align-items: flex-start;
justify-content: space-between;
gap: 18px;
}
.brand {
max-width: 390px;
padding: 17px 19px 16px;
border-radius: 16px;
}
.eyebrow {
display: flex;
align-items: center;
gap: 9px;
margin-bottom: 8px;
color: var(--cyan);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.2em;
text-transform: uppercase;
}
.eyebrow::before {
content: "";
width: 8px;
height: 8px;
border: 2px solid var(--cyan);
border-radius: 2px;
box-shadow: 0 0 12px rgba(101, 216, 238, 0.8);
}
h1 {
margin: 0;
font-size: clamp(22px, 3vw, 34px);
font-weight: 760;
letter-spacing: -0.045em;
line-height: 1.02;
}
.subtitle {
margin: 9px 0 0;
color: var(--muted);
font-size: 12px;
line-height: 1.55;
}
.scale-badge {
display: inline-flex;
align-items: center;
gap: 7px;
margin-top: 12px;
padding: 6px 8px;
border: 1px solid rgba(101, 216, 238, 0.2);
border-radius: 6px;
color: #b9eaf2;
background: rgba(101, 216, 238, 0.06);
font-size: 10px;
font-weight: 700;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.scale-badge::before {
content: "";
width: 17px;
height: 5px;
border-bottom: 2px solid var(--cyan);
border-left: 2px solid var(--cyan);
border-right: 2px solid var(--cyan);
}
.legend {
width: 252px;
padding: 15px 16px;
border-radius: 15px;
}
.legend-title {
margin-bottom: 11px;
color: #d8f6fb;
font-size: 10px;
font-weight: 800;
letter-spacing: 0.16em;
text-transform: uppercase;
}
.legend-row {
display: flex;
align-items: center;
gap: 10px;
min-height: 28px;
color: #bad1d8;
font-size: 11px;
}
.legend-row strong {
margin-left: auto;
color: #edfaff;
font-weight: 700;
}
.organism-icon {
width: 15px;
height: 15px;
border-radius: 3px;
background: currentColor;
box-shadow:
5px 5px 0 -2px currentColor,
-4px 5px 0 -3px currentColor;
}
.icon-algae { color: var(--algae); }
.icon-bacteria { color: var(--bacteria); }
.icon-protist { color: var(--protist); }
.icon-detritus { color: var(--detritus); }
.legend-divider {
height: 1px;
margin: 11px 0;
background: rgba(255, 255, 255, 0.08);
}
.arrow-sample {
position: relative;
width: 43px;
height: 2px;
background: var(--amber);
box-shadow: 0 0 8px rgba(255, 189, 89, 0.35);
}
.arrow-sample::after {
content: "";
position: absolute;
right: -1px;
top: -4px;
width: 0;
height: 0;
border-top: 5px solid transparent;
border-bottom: 5px solid transparent;
border-left: 7px solid var(--amber);
}
.path-sample {
width: 43px;
height: 0;
border-top: 2px dashed var(--cyan);
opacity: 0.9;
}
.legend-note {
margin-top: 11px;
color: #72939d;
font-size: 10px;
line-height: 1.45;
}
#labels {
position: fixed;
inset: 0;
z-index: 8;
pointer-events: none;
overflow: hidden;
}
.world-label {
position: absolute;
left: 0;
top: 0;
padding: 5px 8px;
border: 1px solid currentColor;
border-radius: 5px;
background: rgba(4, 18, 25, 0.82);
box-shadow: 0 4px 16px rgba(0, 0, 0, 0.22);
color: #c8f5fb;
font-size: 9px;
font-weight: 800;
letter-spacing: 0.13em;
white-space: nowrap;
transition: opacity 120ms ease;
}
.world-label.algae { color: var(--algae); }
.world-label.bacteria { color: var(--bacteria); }
.world-label.protist { color: var(--protist); }
.world-label.detritus { color: var(--detritus); }
.bottombar {
position: absolute;
left: 22px;
right: 22px;
bottom: 20px;
display: flex;
align-items: flex-end;
justify-content: space-between;
gap: 16px;
}
.bottom-left {
display: flex;
align-items: flex-end;
gap: 10px;
}
.counts {
display: flex;
gap: 3px;
padding: 8px;
border-radius: 12px;
}
.count {
min-width: 63px;
padding: 7px 9px;
border-radius: 8px;
background: rgba(255, 255, 255, 0.025);
}
.count b {
display: block;
color: #f1fdff;
font-size: 15px;
line-height: 1;
}
.count span {
display: block;
margin-top: 4px;
color: var(--muted);
font-size: 9px;
font-weight: 700;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.controls {
display: flex;
gap: 7px;
padding: 8px;
border-radius: 12px;
pointer-events: auto;
}
.controls button {
min-height: 34px;
padding: 0 12px;
border: 1px solid rgba(138, 220, 237, 0.15);
border-radius: 8px;
background: rgba(125, 215, 235, 0.06);
color: #c5e9ef;
cursor: pointer;
font-size: 11px;
font-weight: 700;
transition:
background 150ms ease,
border-color 150ms ease,
transform 150ms ease;
}
.controls button:hover {
border-color: rgba(138, 220, 237, 0.4);
background: rgba(125, 215, 235, 0.13);
transform: translateY(-1px);
}
.controls button:focus-visible {
outline: 2px solid var(--cyan);
outline-offset: 2px;
}
.hint {
margin-top: 7px;
color: rgba(176, 209, 216, 0.62);
font-size: 9px;
letter-spacing: 0.04em;
text-align: right;
}
.inspector {
position: absolute;
left: 22px;
top: 190px;
width: 250px;
padding: 13px 15px;
border-radius: 12px;
pointer-events: none;
}
.inspect-kicker {
color: #6f919b;
font-size: 8px;
font-weight: 800;
letter-spacing: 0.16em;
text-transform: uppercase;
}
.inspector strong {
display: block;
margin-top: 5px;
color: #e9fbff;
font-size: 13px;
}
.inspector p {
margin: 6px 0 0;
color: #8eacb5;
font-size: 10px;
line-height: 1.45;
}
@media (max-width: 760px) {
.topbar {
top: 12px;
left: 12px;
right: 12px;
}
.brand {
max-width: calc(100vw - 24px);
padding: 13px 15px;
}
.subtitle,
.scale-badge,
.legend,
.inspector,
.hint {
display: none;
}
.bottombar {
left: 12px;
right: 12px;
bottom: 12px;
}
.counts {
display: none;
}
.controls {
width: 100%;
}
.controls button {
flex: 1;
padding: 0 8px;
}
}
</style>
</head>
<body>
<div id="scene"></div>
<div class="ui">
<div class="topbar">
<section class="brand panel">
<div class="eyebrow">Enlarged water droplet</div>
<h1>Microbial Food Web</h1>
<p class="subtitle">
A voxel-scale ecosystem where producers, decomposers, grazers,
and predators exchange energy.
</p>
<div class="scale-badge">500 μm field · voxel reconstruction</div>
</section>
<aside class="legend panel">
<div class="legend-title">Visual key</div>
<div class="legend-row">
<i class="organism-icon icon-algae"></i>
Algae <strong>producer</strong>
</div>
<div class="legend-row">
<i class="organism-icon icon-bacteria"></i>
Bacteria <strong>decomposer</strong>
</div>
<div class="legend-row">
<i class="organism-icon icon-protist"></i>
Protists <strong>consumer</strong>
</div>
<div class="legend-row">
<i class="organism-icon icon-detritus"></i>
Organic matter <strong>detritus</strong>
</div>
<div class="legend-divider"></div>
<div class="legend-row">
<i class="arrow-sample"></i>
Feeding arrow
</div>
<div class="legend-row">
<i class="path-sample"></i>
Swimming path
</div>
<div class="legend-note">
Solid arrows point from food source to consumer. Dashed cyan loops
show physical movement through the droplet.
</div>
</aside>
</div>
<section class="inspector panel">
<div class="inspect-kicker">Selected organism</div>
<strong id="selectedName">Click a voxel cell</strong>
<p id="selectedRole">
Inspect an organism to reveal its role in the miniature food web.
</p>
</section>
<div class="bottombar">
<div class="bottom-left">
<div class="counts panel">
<div class="count"><b>06</b><span>Algae</span></div>
<div class="count"><b>14</b><span>Bacteria</span></div>
<div class="count"><b>05</b><span>Protists</span></div>
<div class="count"><b>04</b><span>Detritus</span></div>
</div>
</div>
<div>
<div class="controls panel">
<button id="pauseButton" type="button">Pause motion</button>
<button id="labelButton" type="button">Labels: On</button>
<button id="resetButton" type="button">Reset view</button>
</div>
<div class="hint">
Drag to orbit · wheel to zoom · Shift/right-drag to pan · click to inspect
</div>
</div>
</div>
</div>
<div id="labels"></div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// -------------------------------------------------------------------------
// Renderer and scene
// -------------------------------------------------------------------------
const container = document.getElementById("scene");
const labelsContainer = document.getElementById("labels");
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x06151d);
scene.fog = new THREE.FogExp2(0x06151d, 0.018);
const renderer = new THREE.WebGLRenderer({
antialias: true,
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.domElement.tabIndex = 0;
renderer.domElement.setAttribute(
"aria-label",
"Interactive three-dimensional microbial food web"
);
container.appendChild(renderer.domElement);
const camera = new THREE.PerspectiveCamera(
43,
window.innerWidth / window.innerHeight,
0.1,
100
);
scene.add(new THREE.HemisphereLight(0xc5f5ff, 0x10242c, 1.65));
const keyLight = new THREE.DirectionalLight(0xf4ffff, 2.4);
keyLight.position.set(5, 8, 7);
scene.add(keyLight);
const cyanLight = new THREE.PointLight(0x36d8ff, 28, 24, 2);
cyanLight.position.set(-4, 2, 5);
scene.add(cyanLight);
const coralLight = new THREE.PointLight(0xff6f91, 18, 22, 2);
coralLight.position.set(4, -3, 2);
scene.add(coralLight);
const cubeGeometry = new THREE.BoxGeometry(1, 1, 1);
const materials = {
algae: new THREE.MeshStandardMaterial({
color: 0x37d96c,
emissive: 0x0d5127,
emissiveIntensity: 0.52,
roughness: 0.72,
metalness: 0.02
}),
algaeBright: new THREE.MeshStandardMaterial({
color: 0xa4ff75,
emissive: 0x2e7b23,
emissiveIntensity: 0.68,
roughness: 0.65
}),
bacteria: new THREE.MeshStandardMaterial({
color: 0xff5f88,
emissive: 0x611229,
emissiveIntensity: 0.55,
roughness: 0.62
}),
bacteriaAccent: new THREE.MeshStandardMaterial({
color: 0xffb267,
emissive: 0x74320c,
emissiveIntensity: 0.48,
roughness: 0.65
}),
protist: new THREE.MeshStandardMaterial({
color: 0x639fff,
emissive: 0x163d78,
emissiveIntensity: 0.62,
roughness: 0.56
}),
protistAccent: new THREE.MeshStandardMaterial({
color: 0x9e79ff,
emissive: 0x3e216f,
emissiveIntensity: 0.54,
roughness: 0.58
}),
nucleus: new THREE.MeshStandardMaterial({
color: 0x172447,
emissive: 0x090d21,
emissiveIntensity: 0.8,
roughness: 0.7
}),
detritus: new THREE.MeshStandardMaterial({
color: 0xc48746,
emissive: 0x4d2d13,
emissiveIntensity: 0.35,
roughness: 0.9
}),
detritusDark: new THREE.MeshStandardMaterial({
color: 0x6e4934,
emissive: 0x24150d,
emissiveIntensity: 0.25,
roughness: 0.95
})
};
// -------------------------------------------------------------------------
// Deterministic helpers
// -------------------------------------------------------------------------
let seed = 782341;
function random() {
seed = (seed * 1664525 + 1013904223) >>> 0;
return seed / 4294967296;
}
function randomBetween(min, max) {
return min + (max - min) * random();
}
function randomPointInSphere(maxRadius) {
const azimuth = random() * Math.PI * 2;
const cosine = random() * 2 - 1;
const sine = Math.sqrt(1 - cosine * cosine);
const radius = maxRadius * Math.cbrt(random());
return new THREE.Vector3(
radius * sine * Math.cos(azimuth),
radius * cosine,
radius * sine * Math.sin(azimuth)
);
}
function addVoxel(parent, size, x, y, z, material, rotation = 0) {
const mesh = new THREE.Mesh(cubeGeometry, material);
mesh.scale.setScalar(size);
mesh.position.set(x, y, z);
mesh.rotation.set(
rotation * 0.35 + randomBetween(-0.08, 0.08),
rotation * 0.25 + randomBetween(-0.12, 0.12),
randomBetween(-0.1, 0.1)
);
parent.add(mesh);
return mesh;
}
// -------------------------------------------------------------------------
// Enlarged water droplet: translucent membrane plus a voxelized surface
// -------------------------------------------------------------------------
const droplet = new THREE.Mesh(
new THREE.SphereGeometry(6.05, 48, 32),
new THREE.MeshPhysicalMaterial({
color: 0x61d8f4,
transparent: true,
opacity: 0.055,
roughness: 0.2,
metalness: 0,
side: THREE.DoubleSide,
depthWrite: false
})
);
droplet.scale.y = 1.08;
droplet.renderOrder = 4;
scene.add(droplet);
const shellCount = 560;
const shellMaterial = new THREE.MeshBasicMaterial({
color: 0xffffff,
transparent: true,
opacity: 0.28,
depthWrite: false,
side: THREE.DoubleSide
});
const shellMesh = new THREE.InstancedMesh(
cubeGeometry,
shellMaterial,
shellCount
);
const dummy = new THREE.Object3D();
const goldenAngle = Math.PI * (3 - Math.sqrt(5));
const instanceColor = new THREE.Color();
for (let i = 0; i < shellCount; i++) {
const y = 1 - ((i + 0.5) / shellCount) * 2;
const radius = Math.sqrt(Math.max(0, 1 - y * y));
const angle = goldenAngle * i;
const size = randomBetween(0.085, 0.17);
dummy.position.set(
Math.cos(angle) * radius * 6.05,
y * 6.05 * 1.08,
Math.sin(angle) * radius * 6.05
);
dummy.scale.setScalar(size);
dummy.rotation.set(
random() * Math.PI,
random() * Math.PI,
random() * Math.PI
);
dummy.updateMatrix();
shellMesh.setMatrixAt(i, dummy.matrix);
instanceColor.setHSL(
randomBetween(0.49, 0.55),
randomBetween(0.55, 0.9),
randomBetween(0.48, 0.72)
);
shellMesh.setColorAt(i, instanceColor);
}
shellMesh.instanceMatrix.needsUpdate = true;
shellMesh.instanceColor.needsUpdate = true;
shellMesh.renderOrder = 5;
scene.add(shellMesh);
// Suspended microscopic motes inside the droplet.
const moteCount = 210;
const moteMaterial = new THREE.MeshBasicMaterial({
color: 0xa9efff,
transparent: true,
opacity: 0.26,
depthWrite: false
});
const waterMotes = new THREE.InstancedMesh(
cubeGeometry,
moteMaterial,
moteCount
);
for (let i = 0; i < moteCount; i++) {
const point = randomPointInSphere(5.65);
const size = randomBetween(0.025, 0.075);
dummy.position.copy(point);
dummy.scale.setScalar(size);
dummy.rotation.set(random() * Math.PI, random() * Math.PI, random() * Math.PI);
dummy.updateMatrix();
waterMotes.setMatrixAt(i, dummy.matrix);
}
waterMotes.instanceMatrix.needsUpdate = true;
waterMotes.renderOrder = 1;
scene.add(waterMotes);
// Sparse background voxels give the scene depth without competing with the drop.
const backgroundCount = 130;
const backgroundMaterial = new THREE.MeshBasicMaterial({
color: 0x64c7dc,
transparent: true,
opacity: 0.16,
depthWrite: false
});
const backgroundVoxels = new THREE.InstancedMesh(
cubeGeometry,
backgroundMaterial,
backgroundCount
);
for (let i = 0; i < backgroundCount; i++) {
const point = randomPointInSphere(24);
if (point.length() < 8) point.setLength(8);
dummy.position.copy(point);
dummy.scale.setScalar(randomBetween(0.025, 0.09));
dummy.rotation.set(random() * Math.PI, random() * Math.PI, random() * Math.PI);
dummy.updateMatrix();
backgroundVoxels.setMatrixAt(i, dummy.matrix);
}
backgroundVoxels.instanceMatrix.needsUpdate = true;
scene.add(backgroundVoxels);
// -------------------------------------------------------------------------
// Physical swimming paths: closed, dashed cyan curves
// -------------------------------------------------------------------------
function createLoop(center, radii, phase) {
const points = [];
for (let i = 0; i < 8; i++) {
const angle = phase + (i / 8) * Math.PI * 2;
const wobble = randomBetween(0.84, 1.08);
points.push(
new THREE.Vector3(
center.x + Math.cos(angle) * radii.x * wobble,
center.y + Math.sin(angle * 2 + phase) * radii.y * 0.58,
center.z + Math.sin(angle) * radii.z * wobble
)
);
}
return new THREE.CatmullRomCurve3(
points,
true,
"catmullrom",
0.48
);
}
const routeDefinitions = [
{
center: new THREE.Vector3(-2.0, 1.0, 0.0),
radii: new THREE.Vector3(1.8, 0.9, 1.4),
phase: 0.2
},
{
center: new THREE.Vector3(2.0, -1.0, 0.5),
radii: new THREE.Vector3(1.6, 0.9, 1.6),
phase: 1.1
},
{
center: new THREE.Vector3(-1.8, -2.0, 1.4),
radii: new THREE.Vector3(1.4, 0.6, 1.0),
phase: 0.6
},
{
center: new THREE.Vector3(2.3, 1.6, -1.1),
radii: new THREE.Vector3(1.3, 0.8, 1.2),
phase: 1.8
},
{
center: new THREE.Vector3(0.2, 0.0, 2.6),
radii: new THREE.Vector3(1.6, 0.8, 1.0),
phase: 2.3
},
{
center: new THREE.Vector3(-2.7, 0.1, -1.2),
radii: new THREE.Vector3(1.5, 0.8, 1.2),
phase: 2.8
},
{
center: new THREE.Vector3(2.7, -0.3, -0.7),
radii: new THREE.Vector3(1.7, 0.9, 1.4),
phase: 3.7
},
{
center: new THREE.Vector3(0.0, -2.0, -1.5),
radii: new THREE.Vector3(2.0, 0.7, 1.2),
phase: 4.3
}
];
const routes = routeDefinitions.map((definition) => {
const route = createLoop(
definition.center,
definition.radii,
definition.phase
);
const geometry = new THREE.BufferGeometry().setFromPoints(
route.getPoints(170)
);
const material = new THREE.LineDashedMaterial({
color: 0x53cbdf,
dashSize: 0.11,
gapSize: 0.14,
transparent: true,
opacity: 0.34,
depthTest: false,
depthWrite: false
});
const line = new THREE.Line(geometry, material);
line.computeLineDistances();
line.renderOrder = 2;
scene.add(line);
return route;
});
// -------------------------------------------------------------------------
// Voxel organism factories
// -------------------------------------------------------------------------
function createAlgae(scale) {
const group = new THREE.Group();
addVoxel(group, 0.78 * scale, 0, 0, 0, materials.algae);
addVoxel(group, 0.49 * scale, 0.56 * scale, 0, 0, materials.algae);
addVoxel(group, 0.49 * scale, -0.56 * scale, 0, 0, materials.algae);
addVoxel(group, 0.49 * scale, 0, 0.56 * scale, 0, materials.algae);
addVoxel(group, 0.49 * scale, 0, -0.56 * scale, 0, materials.algae);
addVoxel(group, 0.46 * scale, 0, 0, 0.56 * scale, materials.algae);
addVoxel(group, 0.46 * scale, 0, 0, -0.56 * scale, materials.algae);
addVoxel(
group,
0.22 * scale,
0.25 * scale,
0.25 * scale,
0.39 * scale,
materials.algaeBright
);
addVoxel(
group,
0.2 * scale,
-0.29 * scale,
-0.17 * scale,
0.35 * scale,
materials.algaeBright
);
addVoxel(
group,
0.19 * scale,
0.15 * scale,
0.4 * scale,
-0.31 * scale,
materials.algaeBright
);
return group;
}
function createBacterium(scale) {
const group = new THREE.Group();
for (let i = 0; i < 4; i++) {
addVoxel(
group,
0.23 * scale,
(i - 1.5) * 0.23 * scale,
Math.sin(i * 1.8) * 0.07 * scale,
Math.cos(i * 1.4) * 0.09 * scale,
i % 2 ? materials.bacteria : materials.bacteriaAccent
);
}
addVoxel(
group,
0.12 * scale,
-0.69 * scale,
0.08 * scale,
0.08 * scale,
materials.bacteria
);
addVoxel(
group,
0.1 * scale,
-0.83 * scale,
0.16 * scale,
0.02 * scale,
materials.bacteriaAccent
);
return group;
}
function createProtist(scale) {
const group = new THREE.Group();
const cubeSize = 0.48 * scale;
for (let x = -1; x <= 1; x++) {
for (let y = -1; y <= 1; y++) {
for (let z = -1; z <= 1; z++) {
const corner = Math.abs(x) + Math.abs(y) + Math.abs(z) > 2;
const cutout = x === 1 && y === 1;
if (!corner && !cutout) {
const material =
(x + y + z) % 3 === 0
? materials.protistAccent
: materials.protist;
addVoxel(
group,
cubeSize,
x * cubeSize,
y * cubeSize,
z * cubeSize,
material
);
}
}
}
}
addVoxel(
group,
0.42 * scale,
0,
0,
0.48 * scale,
materials.nucleus
);
addVoxel(
group,
0.14 * scale,
1.02 * scale,
0,
0,
materials.protistAccent
);
addVoxel(
group,
0.14 * scale,
-1.02 * scale,
0,
0,
materials.protistAccent
);
addVoxel(
group,
0.14 * scale,
0,
1.02 * scale,
0,
materials.protistAccent
);
addVoxel(
group,
0.14 * scale,
0,
-1.02 * scale,
0,
materials.protistAccent
);
return group;
}
function createDetritus(scale) {
const group = new THREE.Group();
addVoxel(group, 0.48 * scale, 0, 0, 0, materials.detritus);
addVoxel(group, 0.34 * scale, 0.35 * scale, 0.08 * scale, 0, materials.detritusDark);
addVoxel(group, 0.32 * scale, -0.28 * scale, 0.18 * scale, 0.08 * scale, materials.detritus);
addVoxel(group, 0.3 * scale, 0.05 * scale, -0.3 * scale, 0.04 * scale, materials.detritusDark);
addVoxel(group, 0.26 * scale, 0.04 * scale, 0.03 * scale, 0.35 * scale, materials.detritus);
addVoxel(group, 0.19 * scale, -0.16 * scale, -0.13 * scale, 0.31 * scale, materials.detritusDark);
return group;
}
// -------------------------------------------------------------------------
// Populate the droplet
// -------------------------------------------------------------------------
const algae = [];
const bacteria = [];
const protists = [];
const detritus = [];
const organismGroups = [];
function registerOrganism(group, type, title, role) {
group.userData.organism = true;
group.userData.type = type;
group.userData.title = title;
group.userData.role = role;
organismGroups.push(group);
scene.add(group);
return group;
}
for (let i = 0; i < 6; i++) {
const group = createAlgae(randomBetween(0.76, 1.0));
registerOrganism(
group,
"algae",
"Algal cell",
"Primary producer: captures light and releases dissolved nutrients."
);
group.userData.route = routes[i % 2];
group.userData.speed = randomBetween(0.007, 0.012);
group.userData.offset = i * 0.13;
group.userData.phase = random() * Math.PI * 2;
algae.push(group);
}
for (let i = 0; i < 14; i++) {
const group = createBacterium(randomBetween(0.72, 1.12));
registerOrganism(
group,
"bacteria",
"Bacterial cell",
"Decomposer and microbial grazer: consumes detritus and small algae."
);
group.userData.route = routes[2 + (i % 3)];
group.userData.speed = randomBetween(0.027, 0.043);
group.userData.offset = i * 0.071;
group.userData.phase = random() * Math.PI * 2;
bacteria.push(group);
}
for (let i = 0; i < 5; i++) {
const scale = i === 4 ? 1.35 : randomBetween(0.82, 1.12);
const group = createProtist(scale);
registerOrganism(
group,
"protist",
i === 4 ? "Predatory protist" : "Protist",
i === 4
? "Larger predator: consumes smaller protists and bacteria."
: "Grazing consumer: feeds on bacteria and algal cells."
);
group.userData.route = routes[5 + (i % 2)];
group.userData.speed = randomBetween(0.012, 0.019);
group.userData.offset = i * 0.17;
group.userData.phase = random() * Math.PI * 2;
protists.push(group);
}
for (let i = 0; i < 4; i++) {
const group = createDetritus(randomBetween(0.85, 1.25));
registerOrganism(
group,
"detritus",
"Organic particles",
"Detrital matter: the carbon and nutrient source used by bacteria."
);
group.userData.route = routes[7];
group.userData.speed = randomBetween(0.006, 0.01);
group.userData.offset = i * 0.23;
group.userData.phase = random() * Math.PI * 2;
detritus.push(group);
}
// -------------------------------------------------------------------------
// Directed feeding arrows: solid warm lines, pyramid heads, moving pulses
// -------------------------------------------------------------------------
const feedingArrows = [];
const arrowSegments = 22;
const upAxis = new THREE.Vector3(0, 1, 0);
const xAxis = new THREE.Vector3(1, 0, 0);
function createFeedingArrow(source, target, index) {
const positions = new Float32Array((arrowSegments + 1) * 3);
const geometry = new THREE.BufferGeometry();
geometry.setAttribute(
"position",
new THREE.BufferAttribute(positions, 3).setUsage(THREE.DynamicDrawUsage)
);
const material = new THREE.LineBasicMaterial({
color: 0xffbd59,
transparent: true,
opacity: 0.88,
depthTest: false,
depthWrite: false
});
const line = new THREE.Line(geometry, material);
line.frustumCulled = false;
line.renderOrder = 10;
scene.add(line);
const head = new THREE.Mesh(
new THREE.ConeGeometry(0.15, 0.38, 4),
new THREE.MeshBasicMaterial({
color: 0xffc467,
depthTest: false,
depthWrite: false
})
);
head.renderOrder = 11;
scene.add(head);
const pulse = new THREE.Mesh(
cubeGeometry,
new THREE.MeshBasicMaterial({
color: 0xffe3a1,
depthTest: false,
depthWrite: false
})
);
pulse.scale.setScalar(0.095);
pulse.renderOrder = 12;
scene.add(pulse);
feedingArrows.push({
source,
target,
line,
positions,
head,
pulse,
index,
phase: index * 0.137
});
}
// Resource -> consumer relationships.
createFeedingArrow(detritus[0], bacteria[0], 0);
createFeedingArrow(detritus[1], bacteria[5], 1);
createFeedingArrow(detritus[2], bacteria[9], 2);
createFeedingArrow(algae[0], bacteria[1], 3);
createFeedingArrow(algae[2], bacteria[7], 4);
createFeedingArrow(algae[4], bacteria[12], 5);
createFeedingArrow(algae[0], protists[0], 6);
createFeedingArrow(algae[3], protists[2], 7);
createFeedingArrow(bacteria[0], protists[0], 8);
createFeedingArrow(bacteria[3], protists[1], 9);
createFeedingArrow(bacteria[8], protists[3], 10);
createFeedingArrow(protists[0], protists[4], 11);
// -------------------------------------------------------------------------
// Movement animation
// -------------------------------------------------------------------------
const tangent = new THREE.Vector3();
function updateOrganisms(time) {
for (const group of organismGroups) {
const data = group.userData;
const u = (time * data.speed + data.offset) % 1;
const point = data.route.getPointAt(u);
data.route.getTangentAt(u, tangent);
group.position.copy(point);
group.position.y += Math.sin(time * 0.82 + data.phase) * 0.09;
group.rotation.y = Math.atan2(tangent.x, tangent.z);
group.rotation.x = Math.cos(time * 0.37 + data.phase) * 0.055;
group.rotation.z = Math.sin(time * 0.44 + data.phase) * 0.07;
const pulse = 1 + Math.sin(time * 1.6 + data.phase) * 0.018;
group.scale.setScalar(pulse);
}
}
const curve = new THREE.QuadraticBezierCurve3();
const start = new THREE.Vector3();
const end = new THREE.Vector3();
const midpoint = new THREE.Vector3();
const direction = new THREE.Vector3();
const normal = new THREE.Vector3();
const arrowTangent = new THREE.Vector3();
function updateFeedingArrows(time) {
for (const arrow of feedingArrows) {
start.copy(arrow.source.position);
end.copy(arrow.target.position);
direction.subVectors(end, start);
const distance = direction.length();
midpoint.addVectors(start, end).multiplyScalar(0.5);
normal.crossVectors(direction, upAxis);
if (normal.lengthSq() < 0.0001) {
normal.crossVectors(direction, xAxis);
}
normal.normalize();
const side = arrow.index % 2 === 0 ? 1 : -1;
const bend = THREE.MathUtils.clamp(distance * 0.13, 0.22, 0.78) * side;
midpoint.addScaledVector(normal, bend);
curve.v0.copy(start);
curve.v1.copy(midpoint);
curve.v2.copy(end);
const points = curve.getPoints(arrowSegments);
for (let i = 0; i <= arrowSegments; i++) {
const offset = i * 3;
arrow.positions[offset] = points[i].x;
arrow.positions[offset + 1] = points[i].y;
arrow.positions[offset + 2] = points[i].z;
}
arrow.line.geometry.attributes.position.needsUpdate = true;
curve.getTangent(1, arrowTangent).normalize();
arrow.head.position.copy(curve.getPoint(1));
arrow.head.quaternion.setFromUnitVectors(upAxis, arrowTangent);
const pulsePosition = (time * 0.19 + arrow.phase) % 1;
arrow.pulse.position.copy(curve.getPoint(pulsePosition));
const pulseScale = 0.075 + Math.sin(pulsePosition * Math.PI) * 0.045;
arrow.pulse.scale.setScalar(pulseScale);
}
}
// -------------------------------------------------------------------------
// Labels projected from world space
// -------------------------------------------------------------------------
const labelDefinitions = [
{ object: algae[1], type: "algae", text: "ALGA", offset: 0.85 },
{ object: bacteria[2], type: "bacteria", text: "BACTERIA", offset: 0.45 },
{ object: protists[2], type: "protist", text: "PROTIST", offset: 1.25 },
{ object: detritus[1], type: "detritus", text: "ORGANIC MATTER", offset: 0.7 }
];
const worldLabels = labelDefinitions.map((definition) => {
const element = document.createElement("div");
element.className = `world-label ${definition.type}`;
element.textContent = definition.text;
labelsContainer.appendChild(element);
return { ...definition, element };
});
const projected = new THREE.Vector3();
let labelsVisible = true;
function updateLabels() {
if (!labelsVisible) return;
for (const label of worldLabels) {
projected.copy(label.object.position);
projected.y += label.offset;
projected.project(camera);
const visible =
projected.z > -1 &&
projected.z < 1 &&
Math.abs(projected.x) < 1.15 &&
Math.abs(projected.y) < 1.15;
label.element.style.opacity = visible ? "1" : "0";
if (visible) {
const x = (projected.x * 0.5 + 0.5) * window.innerWidth;
const y = (-projected.y * 0.5 + 0.5) * window.innerHeight;
label.element.style.transform =
`translate(${x}px, ${y}px) translate(-50%, -100%)`;
}
}
}
// -------------------------------------------------------------------------
// Direct camera controls, implemented without addons
// -------------------------------------------------------------------------
const cameraState = {
azimuth: 0.52,
polar: 1.27,
distance: 17.4,
target: new THREE.Vector3(0, 0, 0),
goalAzimuth: 0.52,
goalPolar: 1.27,
goalDistance: 17.4,
goalTarget: new THREE.Vector3(0, 0, 0)
};
let dragging = false;
let dragButton = 0;
let previousPointerX = 0;
let previousPointerY = 0;
let dragDistance = 0;
const canvas = renderer.domElement;
canvas.addEventListener("pointerdown", (event) => {
if (event.button !== 0 && event.button !== 2) return;
dragging = true;
dragButton = event.button;
previousPointerX = event.clientX;
previousPointerY = event.clientY;
dragDistance = 0;
canvas.setPointerCapture(event.pointerId);
canvas.style.cursor = "grabbing";
});
canvas.addEventListener("pointermove", (event) => {
if (!dragging) return;
const dx = event.clientX - previousPointerX;
const dy = event.clientY - previousPointerY;
previousPointerX = event.clientX;
previousPointerY = event.clientY;
dragDistance += Math.abs(dx) + Math.abs(dy);
if (event.shiftKey || dragButton === 2) {
const right = new THREE.Vector3().setFromMatrixColumn(
camera.matrixWorld,
0
);
const up = new THREE.Vector3().setFromMatrixColumn(
camera.matrixWorld,
1
);
const panScale = cameraState.goalDistance * 0.0014;
cameraState.goalTarget.addScaledVector(right, -dx * panScale);
cameraState.goalTarget.addScaledVector(up, dy * panScale);
cameraState.goalTarget.x = THREE.MathUtils.clamp(
cameraState.goalTarget.x,
-4,
4
);
cameraState.goalTarget.y = THREE.MathUtils.clamp(
cameraState.goalTarget.y,
-4,
4
);
cameraState.goalTarget.z = THREE.MathUtils.clamp(
cameraState.goalTarget.z,
-4,
4
);
} else {
cameraState.goalAzimuth -= dx * 0.006;
cameraState.goalPolar = THREE.MathUtils.clamp(
cameraState.goalPolar - dy * 0.006,
0.35,
2.72
);
}
});
canvas.addEventListener("pointerup", (event) => {
if (!dragging) return;
dragging = false;
canvas.style.cursor = "default";
if (dragDistance < 6) {
inspectAtPointer(event);
}
});
canvas.addEventListener("pointercancel", () => {
dragging = false;
canvas.style.cursor = "default";
});
canvas.addEventListener("contextmenu", (event) => {
event.preventDefault();
});
canvas.addEventListener(
"wheel",
(event) => {
event.preventDefault();
cameraState.goalDistance = THREE.MathUtils.clamp(
cameraState.goalDistance * Math.exp(event.deltaY * 0.001),
9,
28
);
},
{ passive: false }
);
function updateCamera(delta) {
const smoothing = 1 - Math.exp(-8 * delta);
cameraState.azimuth = THREE.MathUtils.lerp(
cameraState.azimuth,
cameraState.goalAzimuth,
smoothing
);
cameraState.polar = THREE.MathUtils.lerp(
cameraState.polar,
cameraState.goalPolar,
smoothing
);
cameraState.distance = THREE.MathUtils.lerp(
cameraState.distance,
cameraState.goalDistance,
smoothing
);
cameraState.target.lerp(cameraState.goalTarget, smoothing);
const horizontal = Math.sin(cameraState.polar) * cameraState.distance;
camera.position.set(
cameraState.target.x + horizontal * Math.sin(cameraState.azimuth),
cameraState.target.y + Math.cos(cameraState.polar) * cameraState.distance,
cameraState.target.z + horizontal * Math.cos(cameraState.azimuth)
);
camera.lookAt(cameraState.target);
camera.updateMatrixWorld();
}
function resetCamera() {
cameraState.goalAzimuth = 0.52;
cameraState.goalPolar = 1.27;
cameraState.goalDistance = 17.4;
cameraState.goalTarget.set(0, 0, 0);
}
// -------------------------------------------------------------------------
// Selection and inspection
// -------------------------------------------------------------------------
const raycaster = new THREE.Raycaster();
const pointer = new THREE.Vector2();
const selectedName = document.getElementById("selectedName");
const selectedRole = document.getElementById("selectedRole");
const selectionBox = new THREE.Box3Helper(new THREE.Box3(), 0xffffff);
selectionBox.visible = false;
selectionBox.material.transparent = true;
selectionBox.material.opacity = 0.72;
selectionBox.material.depthTest = false;
selectionBox.renderOrder = 20;
scene.add(selectionBox);
let selectedOrganism = null;
function findOrganism(object) {
let current = object;
while (current) {
if (current.userData && current.userData.organism) {
return current;
}
current = current.parent;
}
return null;
}
function inspectAtPointer(event) {
const bounds = canvas.getBoundingClientRect();
pointer.x = ((event.clientX - bounds.left) / bounds.width) * 2 - 1;
pointer.y = -((event.clientY - bounds.top) / bounds.height) * 2 + 1;
raycaster.setFromCamera(pointer, camera);
const hits = raycaster.intersectObjects(organismGroups, true);
if (hits.length) {
selectedOrganism = findOrganism(hits[0].object);
selectedName.textContent = selectedOrganism.userData.title;
selectedRole.textContent = selectedOrganism.userData.role;
selectionBox.visible = true;
} else {
selectedOrganism = null;
selectedName.textContent = "Click a voxel cell";
selectedRole.textContent =
"Inspect an organism to reveal its role in the miniature food web.";
selectionBox.visible = false;
}
}
function updateSelectionBox() {
if (!selectedOrganism || !selectionBox.visible) return;
const bounds = new THREE.Box3().setFromObject(selectedOrganism);
bounds.expandByScalar(0.12);
selectionBox.box.copy(bounds);
selectionBox.updateMatrixWorld(true);
}
// -------------------------------------------------------------------------
// UI controls
// -------------------------------------------------------------------------
let running = true;
let simulationTime = 0;
const pauseButton = document.getElementById("pauseButton");
const labelButton = document.getElementById("labelButton");
const resetButton = document.getElementById("resetButton");
pauseButton.addEventListener("click", () => {
running = !running;
pauseButton.textContent = running ? "Pause motion" : "Resume motion";
});
labelButton.addEventListener("click", () => {
labelsVisible = !labelsVisible;
labelsContainer.style.display = labelsVisible ? "block" : "none";
labelButton.textContent = labelsVisible ? "Labels: On" : "Labels: Off";
});
resetButton.addEventListener("click", resetCamera);
canvas.addEventListener("dblclick", resetCamera);
window.addEventListener("keydown", (event) => {
if (event.target instanceof HTMLButtonElement) return;
if (event.code === "Space") {
event.preventDefault();
pauseButton.click();
} else if (event.key.toLowerCase() === "r") {
resetCamera();
} else if (event.key.toLowerCase() === "l") {
labelButton.click();
}
});
window.addEventListener("resize", () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
});
// -------------------------------------------------------------------------
// Main loop
// -------------------------------------------------------------------------
let previousTime = performance.now();
function animate(now) {
const delta = THREE.MathUtils.clamp((now - previousTime) / 1000, 0, 0.05);
previousTime = now;
if (running) {
simulationTime += delta;
}
updateCamera(delta);
updateOrganisms(simulationTime);
updateFeedingArrows(simulationTime);
waterMotes.rotation.y = simulationTime * 0.012;
waterMotes.rotation.x = Math.sin(simulationTime * 0.08) * 0.025;
backgroundVoxels.rotation.y = simulationTime * 0.0025;
updateSelectionBox();
updateLabels();
renderer.render(scene, camera);
}
updateOrganisms(0);
updateFeedingArrows(0);
renderer.setAnimationLoop(animate);
</script>
</body>
</html>
```