Prompt specification
Build or customize the Three.js 3D scene "Flower availability foraging sandbox" 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: Flower availability foraging sandbox
Category: Biology & Nature · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/flower-availability-foraging-sandbox-x0656
Scene Description Prompt:
Build a self-contained HTML scene with Three.js from cdnjs featuring bee foraging and pollination. Create a small orchard model with adjustable flowering patches; a stated simplified visitation rule shows how removing or adding resources changes travel distances and observed visit counts. Low-poly.
Working Three.js Starter (flower-availability-foraging-sandbox-x0656):
```html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Pollinator Commons — Bee Foraging Orchard</title>
<style>
:root {
color-scheme: light;
--ink: #18352d;
--muted: #567269;
--panel: rgba(250, 252, 244, 0.92);
--line: rgba(31, 80, 66, 0.16);
--green: #28705b;
--green-dark: #185143;
--yellow: #f3bf45;
--pink: #e77f9c;
--shadow: 0 16px 45px rgba(34, 66, 54, 0.18);
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #dceee3;
color: var(--ink);
font-family:
Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
button,
input,
select {
font: inherit;
}
button,
select {
color: var(--ink);
}
#scene {
position: fixed;
inset: 0;
display: block;
width: 100%;
height: 100%;
touch-action: none;
cursor: grab;
}
#scene.dragging {
cursor: grabbing;
}
.hud {
position: fixed;
z-index: 10;
pointer-events: none;
}
.interactive {
pointer-events: auto;
}
header {
top: 22px;
left: 22px;
max-width: 430px;
}
.eyebrow {
display: inline-flex;
align-items: center;
gap: 8px;
margin-bottom: 9px;
padding: 7px 11px;
border: 1px solid rgba(24, 81, 67, 0.16);
border-radius: 999px;
background: rgba(250, 252, 244, 0.76);
color: var(--green-dark);
font-size: 11px;
font-weight: 800;
letter-spacing: 0.12em;
text-transform: uppercase;
backdrop-filter: blur(12px);
}
.eyebrow::before {
width: 7px;
height: 7px;
border-radius: 50%;
background: #3d9b78;
content: "";
box-shadow: 0 0 0 4px rgba(61, 155, 120, 0.13);
}
h1 {
margin: 0;
color: #173e32;
font-size: clamp(27px, 3vw, 42px);
font-weight: 800;
letter-spacing: -0.045em;
line-height: 1;
text-shadow: 0 2px 18px rgba(255, 255, 255, 0.7);
}
header p {
max-width: 390px;
margin: 10px 0 0;
color: #44675b;
font-size: 13px;
line-height: 1.5;
}
#metrics {
top: 22px;
right: 22px;
display: grid;
grid-template-columns: repeat(2, minmax(145px, 1fr));
gap: 9px;
width: min(350px, calc(100vw - 44px));
}
.metric {
min-height: 93px;
padding: 14px 15px;
border: 1px solid var(--line);
border-radius: 17px;
background: var(--panel);
box-shadow: var(--shadow);
backdrop-filter: blur(16px);
}
.metric-label {
display: block;
margin-bottom: 7px;
color: var(--muted);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.09em;
text-transform: uppercase;
}
.metric-value {
display: block;
color: var(--green-dark);
font-size: 25px;
font-weight: 800;
letter-spacing: -0.04em;
line-height: 1;
}
.metric-value small {
margin-left: 3px;
color: var(--muted);
font-size: 12px;
font-weight: 700;
letter-spacing: 0;
}
.metric-note {
display: block;
margin-top: 7px;
color: #698078;
font-size: 10px;
line-height: 1.25;
}
#controls {
bottom: 22px;
left: 22px;
width: min(390px, calc(100vw - 44px));
max-height: calc(100vh - 44px);
overflow: auto;
padding: 18px;
border: 1px solid var(--line);
border-radius: 22px;
background: var(--panel);
box-shadow: var(--shadow);
backdrop-filter: blur(18px);
scrollbar-width: thin;
}
.panel-heading {
display: flex;
align-items: center;
justify-content: space-between;
gap: 12px;
margin-bottom: 15px;
}
.panel-heading h2 {
margin: 0;
font-size: 16px;
letter-spacing: -0.02em;
}
.run-state {
display: inline-flex;
align-items: center;
gap: 6px;
color: var(--muted);
font-size: 10px;
font-weight: 700;
white-space: nowrap;
}
.run-state::before {
width: 7px;
height: 7px;
border-radius: 50%;
background: #46a87f;
content: "";
}
.run-state.paused::before {
background: #d1903e;
}
.control-row {
margin: 0 0 14px;
}
.control-label {
display: flex;
align-items: baseline;
justify-content: space-between;
gap: 10px;
margin-bottom: 7px;
font-size: 12px;
font-weight: 750;
}
.control-label output {
color: var(--green);
font-size: 11px;
font-weight: 800;
}
.sub-label {
display: block;
margin-bottom: 7px;
color: var(--muted);
font-size: 10px;
line-height: 1.35;
}
input[type="range"] {
width: 100%;
height: 5px;
margin: 3px 0;
border-radius: 999px;
outline: none;
background:
linear-gradient(
90deg,
var(--green) 0 var(--fill, 50%),
#d9e2d7 var(--fill, 50%) 100%
);
appearance: none;
}
input[type="range"]::-webkit-slider-thumb {
width: 17px;
height: 17px;
border: 3px solid #f8faef;
border-radius: 50%;
background: var(--green);
box-shadow: 0 2px 7px rgba(23, 62, 50, 0.26);
cursor: pointer;
appearance: none;
}
input[type="range"]::-moz-range-thumb {
width: 12px;
height: 12px;
border: 3px solid #f8faef;
border-radius: 50%;
background: var(--green);
box-shadow: 0 2px 7px rgba(23, 62, 50, 0.26);
cursor: pointer;
}
.patch-buttons {
display: flex;
gap: 7px;
margin-top: 8px;
}
.small-button,
.primary-button {
border: 1px solid rgba(40, 112, 91, 0.2);
border-radius: 10px;
background: #edf3e8;
color: var(--green-dark);
cursor: pointer;
font-size: 11px;
font-weight: 800;
transition:
transform 120ms ease,
background 120ms ease;
}
.small-button {
padding: 7px 10px;
}
.small-button:hover,
.primary-button:hover {
background: #e2eddc;
transform: translateY(-1px);
}
.small-button:disabled {
cursor: default;
opacity: 0.38;
transform: none;
}
.primary-button {
padding: 9px 12px;
background: var(--green);
color: #fff;
}
.primary-button:hover {
background: var(--green-dark);
}
.button-row {
display: flex;
flex-wrap: wrap;
gap: 7px;
margin: 12px 0 15px;
}
.divider {
height: 1px;
margin: 15px 0;
background: var(--line);
}
.rule {
padding: 12px;
border: 1px solid rgba(40, 112, 91, 0.12);
border-radius: 13px;
background: rgba(226, 237, 221, 0.62);
}
.rule strong {
display: block;
margin-bottom: 6px;
font-size: 11px;
}
.rule p {
margin: 0;
color: #4f6c61;
font-size: 10.5px;
line-height: 1.55;
}
.rule code {
padding: 1px 4px;
border-radius: 4px;
background: rgba(255, 255, 255, 0.75);
color: var(--green-dark);
font-size: 10px;
font-weight: 800;
}
#legend {
right: 22px;
bottom: 22px;
padding: 12px 14px;
border: 1px solid var(--line);
border-radius: 15px;
background: rgba(250, 252, 244, 0.84);
box-shadow: var(--shadow);
backdrop-filter: blur(14px);
}
.legend-row {
display: flex;
align-items: center;
gap: 8px;
margin: 5px 0;
color: var(--muted);
font-size: 10px;
}
.swatch {
width: 9px;
height: 9px;
border-radius: 50%;
background: var(--yellow);
}
.swatch.flower {
background: var(--pink);
}
.swatch.selected {
border: 2px solid var(--green);
background: transparent;
}
.camera-buttons {
display: flex;
gap: 5px;
margin-top: 9px;
}
#hint {
bottom: 25px;
left: 50%;
transform: translateX(-50%);
padding: 8px 13px;
border-radius: 999px;
background: rgba(250, 252, 244, 0.74);
color: #537166;
font-size: 10px;
font-weight: 700;
white-space: nowrap;
backdrop-filter: blur(10px);
}
@media (max-width: 850px) {
header {
top: 14px;
left: 14px;
max-width: 48vw;
}
header p {
display: none;
}
h1 {
font-size: 27px;
}
#metrics {
top: 14px;
right: 14px;
grid-template-columns: repeat(2, 125px);
width: 254px;
}
.metric {
min-height: 78px;
padding: 11px;
}
.metric-value {
font-size: 21px;
}
#controls {
bottom: 14px;
left: 14px;
width: min(365px, calc(100vw - 28px));
max-height: 48vh;
}
#legend,
#hint {
display: none;
}
}
@media (max-width: 560px) {
header {
max-width: 46vw;
}
.eyebrow {
padding: 6px 8px;
font-size: 8px;
}
h1 {
font-size: 22px;
}
#metrics {
grid-template-columns: repeat(2, 104px);
width: 216px;
}
.metric {
min-height: 70px;
padding: 9px;
}
.metric-label {
font-size: 8px;
}
.metric-value {
font-size: 18px;
}
.metric-note {
font-size: 8px;
}
}
</style>
</head>
<body>
<canvas id="scene" aria-label="Interactive low-poly orchard with foraging bees"></canvas>
<header class="hud">
<div class="eyebrow">Live field model · simplified</div>
<h1>Pollinator Commons</h1>
<p>
Adjust the orchard’s flower patches, then watch how bees redistribute
flights and arrival counts under a transparent foraging rule.
</p>
</header>
<section id="metrics" class="hud interactive" aria-live="polite">
<div class="metric">
<span class="metric-label">Observed visits</span>
<span class="metric-value" id="visitsValue">0</span>
<span class="metric-note" id="visitsNote">baseline 0</span>
</div>
<div class="metric">
<span class="metric-label">Bee travel</span>
<span class="metric-value">
<span id="distanceValue">0.0</span><small>model m</small>
</span>
<span class="metric-note" id="distanceNote">baseline 0.0 m</span>
</div>
<div class="metric">
<span class="metric-label">Mean flight</span>
<span class="metric-value">
<span id="flightValue">0.0</span><small>model m</small>
</span>
<span class="metric-note" id="flightNote">baseline 0.0 m</span>
</div>
<div class="metric">
<span class="metric-label">Pollination</span>
<span class="metric-value" id="pollinationValue">0%</span>
<span class="metric-note">synthetic activity index</span>
</div>
</section>
<section id="controls" class="hud interactive">
<div class="panel-heading">
<h2>Flowering resources</h2>
<span class="run-state" id="runState">Running</span>
</div>
<div class="control-row">
<label class="control-label" for="countInput">
<span>Active flower patches</span>
<output id="countValue">6 of 12</output>
</label>
<input id="countInput" type="range" min="1" max="12" step="1" value="6" />
<div class="patch-buttons">
<button class="small-button" id="removePatch" type="button">− Remove patch</button>
<button class="small-button" id="addPatch" type="button">+ Add patch</button>
</div>
</div>
<div class="control-row">
<label class="control-label" for="bloomInput">
<span>Overall bloom richness</span>
<output id="bloomValue">75%</output>
</label>
<input id="bloomInput" type="range" min="0.15" max="1" step="0.05" value="0.75" />
</div>
<div class="divider"></div>
<div class="control-row">
<label class="control-label" for="selectedBloomInput">
<span id="selectedPatchLabel">Selected patch richness</span>
<output id="selectedBloomValue">75%</output>
</label>
<span class="sub-label">
Click a patch in the orchard. This value overrides overall bloom for that patch.
</span>
<input
id="selectedBloomInput"
type="range"
min="0.15"
max="1"
step="0.05"
value="0.75"
/>
</div>
<div class="button-row">
<button class="primary-button" id="uniformButton" type="button">Make all uniform</button>
<button class="small-button" id="pauseButton" type="button">Pause</button>
<button class="small-button" id="resetButton" type="button">Restart run</button>
<label class="small-button" for="speedSelect">
Speed
<select
id="speedSelect"
style="border:0; background:transparent; font-weight:800"
>
<option value="1">1×</option>
<option value="2" selected>2×</option>
<option value="4">4×</option>
<option value="8">8×</option>
</select>
</label>
</div>
<div class="rule">
<strong>Simplified visitation rule</strong>
<p>
From patch <em>i</em>, a bee chooses the active patch <em>j</em> with the
greatest <code>Rj / (1 + d(i,j) / 6)</code>, where <code>R</code> is bloom
richness and <code>d</code> is straight-line patch distance. Ties use the
lower patch ID. Flight speed is 2.4 model m/s. After arrival, the bee
stays for <code>2.5 + 7(1 − R)</code> seconds, then chooses again.
Each arrival counts as one visit.
</p>
<p style="margin-top:7px">
Seven bees are used in both runs. The comparison run always has six
patches at 75% bloom. Resource changes restart both runs at the same time.
</p>
</div>
</section>
<aside id="legend" class="hud interactive">
<div class="legend-row"><span class="swatch"></span> Foraging bee</div>
<div class="legend-row"><span class="swatch flower"></span> Flower patch</div>
<div class="legend-row"><span class="swatch selected"></span> Selected patch</div>
<div class="camera-buttons">
<button class="small-button" id="zoomIn" type="button" aria-label="Zoom in">+</button>
<button class="small-button" id="zoomOut" type="button" aria-label="Zoom out">−</button>
<button class="small-button" id="cameraReset" type="button">Reset view</button>
</div>
</aside>
<div id="hint" class="hud">
Drag to orbit · scroll to zoom · click a flower patch to inspect it
</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// ---------------------------------------------------------------------
// Core setup
// ---------------------------------------------------------------------
const canvas = document.querySelector("#scene");
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xdceee3);
scene.fog = new THREE.Fog(0xdceee3, 28, 54);
const renderer = new THREE.WebGLRenderer({
canvas,
antialias: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.08;
const camera = new THREE.PerspectiveCamera(
42,
window.innerWidth / window.innerHeight,
0.1,
100
);
const hemi = new THREE.HemisphereLight(0xf6fff4, 0x718260, 2.15);
scene.add(hemi);
const sun = new THREE.DirectionalLight(0xfff1ca, 3.2);
sun.position.set(-9, 17, 11);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -18;
sun.shadow.camera.right = 18;
sun.shadow.camera.top = 18;
sun.shadow.camera.bottom = -18;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 45;
sun.shadow.normalBias = 0.035;
scene.add(sun);
const fill = new THREE.DirectionalLight(0xaed9d2, 0.75);
fill.position.set(12, 7, -10);
scene.add(fill);
const MODEL = Object.freeze({
beeCount: 7,
flightSpeed: 2.4,
scoreDistance: 6,
dwellBase: 2.5,
dwellRichnessFactor: 7
});
const state = {
patchCount: 6,
globalBloom: 0.75,
speed: 2,
paused: false,
selectedPatch: 0
};
const patches = [];
const patchHitMeshes = [];
const trees = [];
// Distribution order keeps small patch counts spread across the orchard.
const patchOrder = [0, 3, 4, 7, 8, 11, 1, 2, 5, 6, 9, 10];
function activeIndices(count = state.patchCount) {
return patchOrder.slice(0, count).map((index) => index);
}
function baselineIndices() {
return activeIndices(6);
}
function groundHeight(x, z) {
return (
0.075 * Math.sin(x * 0.57) +
0.055 * Math.cos(z * 0.69) +
0.025 * Math.sin((x + z) * 0.33)
);
}
function randomGenerator(seed) {
return function () {
seed |= 0;
seed = (seed + 0x6d2b79f5) | 0;
let t = Math.imul(seed ^ (seed >>> 15), 1 | seed);
t = (t + Math.imul(t ^ (t >>> 7), 61 | t)) ^ t;
return ((t ^ (t >>> 14)) >>> 0) / 4294967296;
};
}
const random = randomGenerator(82371);
// ---------------------------------------------------------------------
// Low-poly terrain and paths
// ---------------------------------------------------------------------
const terrainGeometry = new THREE.PlaneGeometry(31, 25, 16, 12)
.toNonIndexed();
terrainGeometry.rotateX(-Math.PI / 2);
const terrainPositions = terrainGeometry.attributes.position;
const terrainColors = new Float32Array(terrainPositions.count * 3);
const color = new THREE.Color();
for (let i = 0; i < terrainPositions.count; i++) {
const x = terrainPositions.getX(i);
const z = terrainPositions.getZ(i);
terrainPositions.setY(i, groundHeight(x, z));
}
for (let i = 0; i < terrainPositions.count; i += 3) {
const hue = 0.245 + random() * 0.025;
const saturation = 0.27 + random() * 0.08;
const lightness = 0.49 + random() * 0.065;
color.setHSL(hue, saturation, lightness);
for (let k = 0; k < 3; k++) {
const p = (i + k) * 3;
terrainColors[p] = color.r;
terrainColors[p + 1] = color.g;
terrainColors[p + 2] = color.b;
}
}
terrainGeometry.setAttribute(
"color",
new THREE.BufferAttribute(terrainColors, 3)
);
terrainGeometry.computeVertexNormals();
const terrain = new THREE.Mesh(
terrainGeometry,
new THREE.MeshStandardMaterial({
vertexColors: true,
roughness: 1,
flatShading: true
})
);
terrain.receiveShadow = true;
scene.add(terrain);
const pathMaterial = new THREE.MeshStandardMaterial({
color: 0xd8c59d,
roughness: 1,
flatShading: true
});
function addPathSegment(ax, az, bx, bz, width = 1.25) {
const dx = bx - ax;
const dz = bz - az;
const length = Math.hypot(dx, dz);
const middleX = (ax + bx) * 0.5;
const middleZ = (az + bz) * 0.5;
const segment = new THREE.Mesh(
new THREE.BoxGeometry(length, 0.055, width),
pathMaterial
);
segment.position.set(
middleX,
groundHeight(middleX, middleZ) + 0.018,
middleZ
);
segment.rotation.y = -Math.atan2(dz, dx);
segment.receiveShadow = true;
scene.add(segment);
}
addPathSegment(0, 9.2, 0, 6.7, 1.5);
addPathSegment(0, 6.7, -2.5, 5.7, 1.25);
addPathSegment(0, 6.7, 2.5, 5.7, 1.25);
addPathSegment(-2.5, 5.7, -2.5, 0.8, 1.1);
addPathSegment(2.5, 5.7, 2.5, 0.8, 1.1);
addPathSegment(-2.5, 0.8, 0, -0.2, 1.05);
addPathSegment(2.5, 0.8, 0, -0.2, 1.05);
// ---------------------------------------------------------------------
// Orchard trees
// ---------------------------------------------------------------------
const trunkGeometry = new THREE.CylinderGeometry(0.17, 0.24, 1.55, 6);
const trunkMaterial = new THREE.MeshStandardMaterial({
color: 0x77543d,
roughness: 1,
flatShading: true
});
const canopyGeometry = new THREE.IcosahedronGeometry(1, 0);
const canopyMaterials = [
new THREE.MeshStandardMaterial({
color: 0x5d9a54,
roughness: 0.95,
flatShading: true
}),
new THREE.MeshStandardMaterial({
color: 0x6fa85b,
roughness: 0.95,
flatShading: true
}),
new THREE.MeshStandardMaterial({
color: 0x4f8c50,
roughness: 0.95,
flatShading: true
})
];
const fruitMaterial = new THREE.MeshStandardMaterial({
color: 0xe99670,
roughness: 0.8,
flatShading: true
});
function createTree(x, z, treeIndex) {
const group = new THREE.Group();
group.position.set(x, groundHeight(x, z), z);
group.rotation.y = random() * Math.PI * 2;
const scale = 0.9 + random() * 0.16;
group.scale.setScalar(scale);
const trunk = new THREE.Mesh(trunkGeometry, trunkMaterial);
trunk.position.y = 0.76;
trunk.castShadow = true;
trunk.receiveShadow = true;
group.add(trunk);
const blobs = [
{ x: 0, y: 2.05, z: 0, sx: 1.08, sy: 1.05, sz: 1.0 },
{ x: -0.62, y: 1.78, z: 0.1, sx: 0.78, sy: 0.82, sz: 0.8 },
{ x: 0.64, y: 1.9, z: -0.08, sx: 0.82, sy: 0.9, sz: 0.82 },
{ x: 0.03, y: 2.55, z: -0.12, sx: 0.7, sy: 0.74, sz: 0.72 }
];
blobs.forEach((blob, i) => {
const leaf = new THREE.Mesh(
canopyGeometry,
canopyMaterials[(treeIndex + i) % canopyMaterials.length]
);
leaf.position.set(blob.x, blob.y, blob.z);
leaf.scale.set(blob.sx, blob.sy, blob.sz);
leaf.castShadow = true;
leaf.receiveShadow = true;
group.add(leaf);
});
for (let i = 0; i < 3; i++) {
const fruit = new THREE.Mesh(
new THREE.IcosahedronGeometry(0.105, 0),
fruitMaterial
);
const angle = random() * Math.PI * 2;
const radius = 0.58 + random() * 0.3;
fruit.position.set(
Math.cos(angle) * radius,
1.65 + random() * 0.75,
Math.sin(angle) * radius
);
fruit.castShadow = true;
group.add(fruit);
}
scene.add(group);
trees.push(group);
}
const treeXs = [-7.5, -2.5, 2.5, 7.5];
const treeZs = [-5, 0, 5];
let treeIndex = 0;
for (const z of treeZs) {
for (const x of treeXs) {
createTree(x, z, treeIndex++);
}
}
// ---------------------------------------------------------------------
// Flower patches
// ---------------------------------------------------------------------
const stemGeometry = new THREE.CylinderGeometry(0.013, 0.019, 0.27, 5);
const stemMaterial = new THREE.MeshStandardMaterial({
color: 0x477d48,
roughness: 1,
flatShading: true
});
const blossomGeometry = new THREE.IcosahedronGeometry(0.115, 0);
const centerGeometry = new THREE.IcosahedronGeometry(0.045, 0);
const centerMaterial = new THREE.MeshStandardMaterial({
color: 0xf5d45d,
roughness: 0.8,
flatShading: true
});
const hitMaterial = new THREE.MeshBasicMaterial({
transparent: true,
opacity: 0,
depthWrite: false,
colorWrite: false
});
const selectionMaterial = new THREE.MeshBasicMaterial({
color: 0xf5c84c,
transparent: true,
opacity: 0.95
});
const palePink = new THREE.Color(0xf5b6c8);
const richPink = new THREE.Color(0xe96f96);
const pollinatedOrange = new THREE.Color(0xf38a5b);
const soilLow = new THREE.Color(0x8a8d60);
const soilHigh = new THREE.Color(0xa9aa72);
let patchIndex = 0;
for (const treeZ of treeZs) {
for (const treeX of treeXs) {
const x = treeX + 0.85;
const z = treeZ + 0.55;
const group = new THREE.Group();
group.position.set(x, groundHeight(x, z) + 0.055, z);
const baseMaterial = new THREE.MeshStandardMaterial({
color: soilLow,
roughness: 1,
flatShading: true
});
const base = new THREE.Mesh(
new THREE.CylinderGeometry(0.73, 0.81, 0.075, 12),
baseMaterial
);
base.receiveShadow = true;
group.add(base);
const flowerMaterial = new THREE.MeshStandardMaterial({
color: palePink,
roughness: 0.9,
flatShading: true
});
const heads = [];
for (let j = 0; j < 10; j++) {
const angle = j * 2.39996 + patchIndex * 0.43;
const radius = 0.12 + 0.57 * Math.sqrt((j + 0.35) / 10);
const flower = new THREE.Group();
flower.position.set(
Math.cos(angle) * radius,
0,
Math.sin(angle) * radius
);
const stem = new THREE.Mesh(stemGeometry, stemMaterial);
stem.position.y = 0.16;
stem.castShadow = true;
flower.add(stem);
const head = new THREE.Mesh(blossomGeometry, flowerMaterial);
head.position.y = 0.32;
head.rotation.set(random(), random(), random());
head.castShadow = true;
flower.add(head);
heads.push(head);
const center = new THREE.Mesh(centerGeometry, centerMaterial);
center.position.y = 0.385;
flower.add(center);
group.add(flower);
}
const ring = new THREE.Mesh(
new THREE.TorusGeometry(0.91, 0.025, 5, 28),
selectionMaterial
);
ring.rotation.x = Math.PI / 2;
ring.position.y = 0.09;
group.add(ring);
const hit = new THREE.Mesh(
new THREE.CylinderGeometry(0.95, 0.95, 0.8, 10),
hitMaterial
);
hit.position.y = 0.35;
hit.userData.patchIndex = patchIndex;
group.add(hit);
patchHitMeshes.push(hit);
scene.add(group);
patches.push({
index: patchIndex,
x,
z,
group,
baseMaterial,
flowerMaterial,
heads,
ring,
hit,
override: null,
richness: state.globalBloom,
visits: 0,
pollination: 0
});
patchIndex++;
}
}
function patchRichness(patch) {
return patch.override ?? state.globalBloom;
}
function updatePatchAppearance() {
const active = new Set(activeIndices());
for (const patch of patches) {
const richness = patchRichness(patch);
patch.richness = richness;
patch.group.visible = active.has(patch.index);
patch.ring.visible =
active.has(patch.index) && state.selectedPatch === patch.index;
const flowerScale = 0.42 + richness * 0.95;
for (const head of patch.heads) {
head.scale.setScalar(flowerScale);
}
patch.baseMaterial.color
.copy(soilLow)
.lerp(soilHigh, richness * 0.8);
patch.flowerMaterial.color
.copy(palePink)
.lerp(richPink, richness * 0.7)
.lerp(pollinatedOrange, patch.pollination * 0.52);
}
}
// ---------------------------------------------------------------------
// Hive and fence
// ---------------------------------------------------------------------
const hive = new THREE.Group();
hive.position.set(0, groundHeight(0, 8.8), 8.8);
const hiveWood = new THREE.MeshStandardMaterial({
color: 0xd89d48,
roughness: 0.9,
flatShading: true
});
for (let i = 0; i < 3; i++) {
const box = new THREE.Mesh(
new THREE.BoxGeometry(1.25, 0.42, 1.0),
hiveWood
);
box.position.y = 0.22 + i * 0.4;
box.castShadow = true;
box.receiveShadow = true;
hive.add(box);
}
const roof = new THREE.Mesh(
new THREE.ConeGeometry(0.93, 0.55, 4),
new THREE.MeshStandardMaterial({
color: 0x855a3f,
roughness: 1,
flatShading: true
})
);
roof.position.y = 1.57;
roof.rotation.y = Math.PI / 4;
roof.castShadow = true;
hive.add(roof);
const entrance = new THREE.Mesh(
new THREE.BoxGeometry(0.48, 0.12, 0.05),
new THREE.MeshBasicMaterial({ color: 0x3d2a20 })
);
entrance.position.set(0, 0.25, -0.515);
hive.add(entrance);
scene.add(hive);
const fenceMaterial = new THREE.MeshStandardMaterial({
color: 0xb59b72,
roughness: 1,
flatShading: true
});
function addFenceRail(x, z, length, rotate = false) {
const rail = new THREE.Mesh(
new THREE.BoxGeometry(length, 0.11, 0.11),
fenceMaterial
);
rail.position.set(x, groundHeight(x, z) + 0.62, z);
rail.rotation.y = rotate ? Math.PI / 2 : 0;
rail.castShadow = true;
scene.add(rail);
}
for (let x = -9; x <= 9; x += 2) {
const post = new THREE.Mesh(
new THREE.BoxGeometry(0.15, 1.05, 0.15),
fenceMaterial
);
post.position.set(x, groundHeight(x, -7.7) + 0.48, -7.7);
post.castShadow = true;
scene.add(post);
}
addFenceRail(0, -7.7, 18);
addFenceRail(0, -7.7, 18);
addFenceRail(-9.3, 0, 15.5, true);
addFenceRail(9.3, 0, 15.5, true);
// ---------------------------------------------------------------------
// Clouds and pollen motes
// ---------------------------------------------------------------------
const cloudMaterial = new THREE.MeshStandardMaterial({
color: 0xf6fbf3,
roughness: 1,
flatShading: true
});
const cloudGroups = [];
for (let i = 0; i < 6; i++) {
const cloud = new THREE.Group();
cloud.position.set(
-18 + random() * 36,
11 + random() * 5,
-15 + random() * 25
);
for (let j = 0; j < 4; j++) {
const puff = new THREE.Mesh(
new THREE.IcosahedronGeometry(1, 0),
cloudMaterial
);
puff.position.set(j * 0.78 - 1.17, random() * 0.38, random() * 0.4);
puff.scale.set(1.25, 0.67 + random() * 0.25, 0.78);
cloud.add(puff);
}
scene.add(cloud);
cloudGroups.push(cloud);
}
const motePositions = new Float32Array(90 * 3);
for (let i = 0; i < 90; i++) {
motePositions[i * 3] = (random() - 0.5) * 22;
motePositions[i * 3 + 1] = 0.6 + random() * 3.2;
motePositions[i * 3 + 2] = (random() - 0.5) * 17;
}
const moteGeometry = new THREE.BufferGeometry();
moteGeometry.setAttribute(
"position",
new THREE.BufferAttribute(motePositions, 3)
);
const motes = new THREE.Points(
moteGeometry,
new THREE.PointsMaterial({
color: 0xffefaa,
size: 0.065,
transparent: true,
opacity: 0.62,
depthWrite: false
})
);
scene.add(motes);
// ---------------------------------------------------------------------
// Bees
// ---------------------------------------------------------------------
const beeBodyMaterial = new THREE.MeshStandardMaterial({
color: 0xf2bd36,
roughness: 0.72,
flatShading: true
});
const beeBlackMaterial = new THREE.MeshStandardMaterial({
color: 0x2b2925,
roughness: 0.82,
flatShading: true
});
const wingMaterial = new THREE.MeshBasicMaterial({
color: 0xf5ffff,
transparent: true,
opacity: 0.58,
side: THREE.DoubleSide,
depthWrite: false
});
const beeBodyGeometry = new THREE.SphereGeometry(1, 8, 6);
const wingGeometry = new THREE.CircleGeometry(0.145, 6);
const beeShadowGeometry = new THREE.CircleGeometry(0.22, 10);
const beeShadowMaterial = new THREE.MeshBasicMaterial({
color: 0x263a30,
transparent: true,
opacity: 0.16,
depthWrite: false
});
function createBeeVisual() {
const group = new THREE.Group();
const abdomen = new THREE.Mesh(beeBodyGeometry, beeBodyMaterial);
abdomen.scale.set(0.18, 0.145, 0.29);
abdomen.castShadow = true;
group.add(abdomen);
const stripeA = new THREE.Mesh(beeBodyGeometry, beeBlackMaterial);
stripeA.position.z = -0.09;
stripeA.scale.set(0.184, 0.15, 0.065);
group.add(stripeA);
const stripeB = new THREE.Mesh(beeBodyGeometry, beeBlackMaterial);
stripeB.position.z = 0.06;
stripeB.scale.set(0.187, 0.153, 0.055);
group.add(stripeB);
const head = new THREE.Mesh(beeBodyGeometry, beeBlackMaterial);
head.position.set(0, 0.015, 0.29);
head.scale.set(0.145, 0.135, 0.135);
head.castShadow = true;
group.add(head);
const wingPivots = [];
for (const side of [-1, 1]) {
const pivot = new THREE.Group();
pivot.position.set(side * 0.13, 0.14, -0.03);
const wing = new THREE.Mesh(wingGeometry, wingMaterial);
wing.rotation.x = -Math.PI / 2;
wing.position.x = side * 0.04;
pivot.add(wing);
group.add(pivot);
wingPivots.push({ pivot, side });
}
for (const side of [-1, 1]) {
const antenna = new THREE.Mesh(
new THREE.CylinderGeometry(0.009, 0.009, 0.19, 4),
beeBlackMaterial
);
antenna.position.set(side * 0.075, 0.09, 0.4);
antenna.rotation.x = -0.7;
group.add(antenna);
}
const shadow = new THREE.Mesh(
beeShadowGeometry,
beeShadowMaterial
);
shadow.rotation.x = -Math.PI / 2;
scene.add(group, shadow);
return { group, shadow, wingPivots };
}
// ---------------------------------------------------------------------
// Pollination sparks
// ---------------------------------------------------------------------
const sparkGeometry = new THREE.IcosahedronGeometry(0.045, 0);
const sparkMaterial = new THREE.MeshBasicMaterial({
color: 0xffdb62
});
const sparks = [];
for (let i = 0; i < 30; i++) {
const mesh = new THREE.Mesh(sparkGeometry, sparkMaterial);
mesh.visible = false;
scene.add(mesh);
sparks.push({
mesh,
life: 0,
velocity: new THREE.Vector3()
});
}
function spawnSparks(patch) {
let spawned = 0;
for (const spark of sparks) {
if (spark.life > 0) continue;
spark.mesh.visible = true;
spark.mesh.position.set(
patch.group.position.x + (random() - 0.5) * 0.7,
patch.group.position.y + 0.35 + random() * 0.25,
patch.group.position.z + (random() - 0.5) * 0.7
);
spark.velocity.set(
(random() - 0.5) * 0.7,
0.5 + random() * 0.65,
(random() - 0.5) * 0.7
);
spark.life = 0.65 + random() * 0.35;
if (++spawned >= 3) break;
}
}
function updateSparks(dt) {
for (const spark of sparks) {
if (spark.life <= 0) continue;
spark.life -= dt;
spark.velocity.y -= 0.8 * dt;
spark.mesh.position.addScaledVector(spark.velocity, dt);
spark.mesh.scale.setScalar(Math.max(0.1, spark.life));
if (spark.life <= 0) {
spark.mesh.visible = false;
}
}
}
// ---------------------------------------------------------------------
// Agent simulation
// ---------------------------------------------------------------------
const currentStats = {
visits: 0,
distance: 0,
flights: 0
};
const baselineStats = {
visits: 0,
distance: 0,
flights: 0
};
function createAgent(serial) {
return {
serial,
patchIndex: 0,
targetIndex: 0,
mode: "forage",
timer: 0,
angle: 0,
travelElapsed: 0,
travelDuration: 1,
from: new THREE.Vector3(),
to: new THREE.Vector3(),
position: new THREE.Vector3(),
yaw: 0
};
}
const currentAgents = [];
const baselineAgents = [];
for (let i = 0; i < MODEL.beeCount; i++) {
currentAgents.push({
agent: createAgent(i),
visual: createBeeVisual()
});
baselineAgents.push(createAgent(i));
}
function resetAgent(agent, active, serial) {
const patch = patches[active[serial % active.length]];
agent.patchIndex = patch.index;
agent.targetIndex = patch.index;
agent.mode = "forage";
agent.timer = 0.9 + serial * 0.31;
agent.angle = serial * 1.77;
agent.travelElapsed = 0;
agent.yaw = -agent.angle;
agent.position.set(
patch.group.position.x + Math.cos(agent.angle) * 0.35,
patch.group.position.y + 0.88,
patch.group.position.z + Math.sin(agent.angle) * 0.35
);
agent.from.copy(agent.position);
agent.to.copy(agent.position);
}
function selectTarget(agent, active, richnessFor) {
const originIsActive = active.includes(agent.patchIndex);
const origin = originIsActive ? agent.patchIndex : null;
let bestIndex = active[0];
let bestScore = -Infinity;
for (const index of active) {
const target = patches[index];
const distance = origin === null
? 0
: Math.hypot(
patches[origin].group.position.x - target.group.position.x,
patches[origin].group.position.z - target.group.position.z
);
const score =
richnessFor(target) /
(1 + distance / MODEL.scoreDistance);
const tied = Math.abs(score - bestScore) < 1e-9;
if (score > bestScore + 1e-9 || (tied && index < bestIndex)) {
bestScore = score;
bestIndex = index;
}
}
return bestIndex;
}
function beginTravel(agent, active, richnessFor, record, isCurrent) {
const targetIndex = selectTarget(agent, active, richnessFor);
const target = patches[targetIndex];
const originIsActive = active.includes(agent.patchIndex);
const origin = originIsActive ? patches[agent.patchIndex] : null;
const distance = origin
? Math.hypot(
origin.group.position.x - target.group.position.x,
origin.group.position.z - target.group.position.z
)
: 0;
agent.targetIndex = targetIndex;
agent.mode = "travel";
agent.travelElapsed = 0;
agent.travelDuration = Math.max(0.05, distance / MODEL.flightSpeed);
if (origin) {
agent.from.set(
origin.group.position.x,
origin.group.position.y + 0.88,
origin.group.position.z
);
} else {
agent.from.copy(agent.position);
}
agent.to.set(
target.group.position.x,
target.group.position.y + 0.88,
target.group.position.z
);
agent.yaw = Math.atan2(
agent.to.x - agent.from.x,
agent.to.z - agent.from.z
);
}
function updateAgent(
agent,
dt,
active,
richnessFor,
record,
isCurrent
) {
if (!active.length) return;
if (agent.mode === "forage") {
const patch = patches[agent.patchIndex];
const richness = richnessFor(patch);
agent.timer -= dt;
agent.angle += dt * 1.45;
agent.position.set(
patch.group.position.x + Math.cos(agent.angle) * 0.36,
patch.group.position.y +
0.88 +
Math.sin(agent.serial * 2.1 + agent.angle * 2.7) * 0.09,
patch.group.position.z + Math.sin(agent.angle) * 0.36
);
agent.yaw = -agent.angle;
if (isCurrent) {
patch.pollination = Math.min(
1,
patch.pollination + dt * 0.0023 * (0.35 + richness)
);
}
if (agent.timer <= 0) {
beginTravel(agent, active, richnessFor, record, isCurrent);
}
} else {
agent.travelElapsed += dt;
const progress = Math.min(
1,
agent.travelElapsed / agent.travelDuration
);
agent.position.lerpVectors(agent.from, agent.to, progress);
agent.position.y += Math.sin(progress * Math.PI) * 0.85;
if (progress >= 1) {
const target = patches[agent.targetIndex];
const richness = richnessFor(target);
const origin = patches[agent.patchIndex];
const distance = Math.hypot(
origin.group.position.x - target.group.position.x,
origin.group.position.z - target.group.position.z
);
agent.patchIndex = agent.targetIndex;
agent.mode = "forage";
agent.timer =
MODEL.dwellBase +
MODEL.dwellRichnessFactor * (1 - richness);
agent.angle = agent.serial * 0.9 + record.visits * 0.37;
agent.position.copy(agent.to);
record.visits += 1;
record.distance += distance;
if (distance > 0.05) {
record.flights += 1;
}
if (isCurrent) {
target.visits += 1;
target.pollination = Math.min(
1,
target.pollination + 0.025 + richness * 0.04
);
spawnSparks(target);
}
}
}
}
function currentRichness(patch) {
return patchRichness(patch);
}
function baselineRichness() {
return 0.75;
}
function resetRun() {
currentStats.visits = 0;
currentStats.distance = 0;
currentStats.flights = 0;
baselineStats.visits = 0;
baselineStats.distance = 0;
baselineStats.flights = 0;
for (const patch of patches) {
patch.visits = 0;
patch.pollination = 0;
}
const currentActive = activeIndices();
const baseActive = baselineIndices();
currentAgents.forEach((entry, i) => {
resetAgent(entry.agent, currentActive, i);
});
baselineAgents.forEach((agent, i) => {
resetAgent(agent, baseActive, i);
});
updatePatchAppearance();
updateMetrics();
}
let resetTimer = null;
function scheduleReset() {
window.clearTimeout(resetTimer);
resetTimer = window.setTimeout(resetRun, 140);
}
// ---------------------------------------------------------------------
// Camera controls implemented directly
// ---------------------------------------------------------------------
const cameraState = {
azimuth: 0.63,
polar: 1.02,
radius: 25.5,
target: new THREE.Vector3(0, 0.9, 0.3)
};
function updateCamera() {
const spherical = new THREE.Spherical(
cameraState.radius,
cameraState.polar,
cameraState.azimuth
);
camera.position.setFromSpherical(spherical);
camera.position.add(cameraState.target);
camera.lookAt(cameraState.target);
}
function resetCamera() {
cameraState.azimuth = 0.63;
cameraState.polar = 1.02;
cameraState.radius = 25.5;
cameraState.target.set(0, 0.9, 0.3);
updateCamera();
}
const pointer = {
down: false,
moved: false,
x: 0,
y: 0,
startX: 0,
startY: 0
};
const raycaster = new THREE.Raycaster();
const mouse = new THREE.Vector2();
canvas.addEventListener("pointerdown", (event) => {
if (event.button !== 0) return;
pointer.down = true;
pointer.moved = false;
pointer.x = event.clientX;
pointer.y = event.clientY;
pointer.startX = event.clientX;
pointer.startY = event.clientY;
canvas.setPointerCapture(event.pointerId);
canvas.classList.add("dragging");
});
canvas.addEventListener("pointermove", (event) => {
if (!pointer.down) return;
const dx = event.clientX - pointer.x;
const dy = event.clientY - pointer.y;
if (
Math.hypot(
event.clientX - pointer.startX,
event.clientY - pointer.startY
) > 5
) {
pointer.moved = true;
}
cameraState.azimuth -= dx * 0.006;
cameraState.polar = THREE.MathUtils.clamp(
cameraState.polar - dy * 0.005,
0.35,
1.38
);
pointer.x = event.clientX;
pointer.y = event.clientY;
updateCamera();
});
canvas.addEventListener("pointerup", (event) => {
pointer.down = false;
canvas.classList.remove("dragging");
if (pointer.moved) return;
const rect = canvas.getBoundingClientRect();
mouse.x = ((event.clientX - rect.left) / rect.width) * 2 - 1;
mouse.y = -((event.clientY - rect.top) / rect.height) * 2 + 1;
raycaster.setFromCamera(mouse, camera);
const active = new Set(activeIndices());
const hits = raycaster.intersectObjects(
patchHitMeshes.filter(
(mesh) => mesh.parent.visible && active.has(mesh.userData.patchIndex)
),
false
);
if (!hits.length) return;
selectPatch(hits[0].object.userData.patchIndex);
});
canvas.addEventListener("pointercancel", () => {
pointer.down = false;
canvas.classList.remove("dragging");
});
canvas.addEventListener(
"wheel",
(event) => {
event.preventDefault();
cameraState.radius = THREE.MathUtils.clamp(
cameraState.radius + event.deltaY * 0.018,
11,
41
);
updateCamera();
},
{ passive: false }
);
// ---------------------------------------------------------------------
// Interface
// ---------------------------------------------------------------------
const countInput = document.querySelector("#countInput");
const countValue = document.querySelector("#countValue");
const bloomInput = document.querySelector("#bloomInput");
const bloomValue = document.querySelector("#bloomValue");
const selectedBloomInput = document.querySelector("#selectedBloomInput");
const selectedBloomValue = document.querySelector("#selectedBloomValue");
const selectedPatchLabel = document.querySelector("#selectedPatchLabel");
const removePatchButton = document.querySelector("#removePatch");
const addPatchButton = document.querySelector("#addPatch");
const uniformButton = document.querySelector("#uniformButton");
const pauseButton = document.querySelector("#pauseButton");
const resetButton = document.querySelector("#resetButton");
const speedSelect = document.querySelector("#speedSelect");
const runState = document.querySelector("#runState");
function updateRangeFill(input) {
const min = Number(input.min);
const max = Number(input.max);
const value = Number(input.value);
const percent = ((value - min) / (max - min)) * 100;
input.style.setProperty("--fill", `${percent}%`);
}
function syncSelectedControl() {
const patch = patches[state.selectedPatch];
const value = patchRichness(patch);
selectedBloomInput.value = value;
selectedBloomValue.textContent = `${Math.round(value * 100)}%`;
selectedPatchLabel.textContent =
`Patch ${String(patch.index + 1).padStart(2, "0")} richness`;
updateRangeFill(selectedBloomInput);
}
function selectPatch(index) {
state.selectedPatch = index;
syncSelectedControl();
updatePatchAppearance();
}
function setPatchCount(value) {
state.patchCount = THREE.MathUtils.clamp(value, 1, 12);
countInput.value = state.patchCount;
countValue.textContent = `${state.patchCount} of 12`;
const active = activeIndices();
if (!active.includes(state.selectedPatch)) {
state.selectedPatch = active[0];
}
removePatchButton.disabled = state.patchCount <= 1;
addPatchButton.disabled = state.patchCount >= 12;
syncSelectedControl();
updatePatchAppearance();
scheduleReset();
}
countInput.addEventListener("input", () => {
setPatchCount(Number(countInput.value));
});
bloomInput.addEventListener("input", () => {
state.globalBloom = Number(bloomInput.value);
bloomValue.textContent = `${Math.round(state.globalBloom * 100)}%`;
const selected = patches[state.selectedPatch];
if (selected.override === null) {
syncSelectedControl();
}
updatePatchAppearance();
scheduleReset();
});
selectedBloomInput.addEventListener("input", () => {
const patch = patches[state.selectedPatch];
patch.override = Number(selectedBloomInput.value);
selectedBloomValue.textContent = `${Math.round(patch.override * 100)}%`;
updatePatchAppearance();
scheduleReset();
});
removePatchButton.addEventListener("click", () => {
setPatchCount(state.patchCount - 1);
});
addPatchButton.addEventListener("click", () => {
setPatchCount(state.patchCount + 1);
});
uniformButton.addEventListener("click", () => {
for (const patch of patches) {
patch.override = null;
}
syncSelectedControl();
updatePatchAppearance();
scheduleReset();
});
pauseButton.addEventListener("click", () => {
state.paused = !state.paused;
pauseButton.textContent = state.paused ? "Resume" : "Pause";
runState.textContent = state.paused ? "Paused" : "Running";
runState.classList.toggle("paused", state.paused);
});
resetButton.addEventListener("click", resetRun);
speedSelect.addEventListener("change", () => {
state.speed = Number(speedSelect.value);
});
document.querySelector("#zoomIn").addEventListener("click", () => {
cameraState.radius = Math.max(11, cameraState.radius - 2);
updateCamera();
});
document.querySelector("#zoomOut").addEventListener("click", () => {
cameraState.radius = Math.min(41, cameraState.radius + 2);
updateCamera();
});
document.querySelector("#cameraReset").addEventListener(
"click",
resetCamera
);
window.addEventListener("keydown", (event) => {
if (
event.target instanceof HTMLInputElement ||
event.target instanceof HTMLSelectElement
) {
return;
}
if (event.code === "Space") {
event.preventDefault();
pauseButton.click();
}
if (event.key.toLowerCase() === "r") {
resetRun();
}
});
function updateMetrics() {
document.querySelector("#visitsValue").textContent =
currentStats.visits.toLocaleString();
document.querySelector("#distanceValue").textContent =
currentStats.distance.toFixed(1);
const currentMean =
currentStats.distance / Math.max(1, currentStats.flights);
const baselineMean =
baselineStats.distance / Math.max(1, baselineStats.flights);
document.querySelector("#flightValue").textContent =
currentMean.toFixed(1);
const visitDelta = currentStats.visits - baselineStats.visits;
const distanceDelta = currentStats.distance - baselineStats.distance;
const meanDelta = currentMean - baselineMean;
document.querySelector("#visitsNote").textContent =
`${visitDelta >= 0 ? "+" : ""}${visitDelta} vs 6-patch baseline`;
document.querySelector("#distanceNote").textContent =
`${distanceDelta >= 0 ? "+" : ""}${distanceDelta.toFixed(1)} m vs baseline`;
document.querySelector("#flightNote").textContent =
`${meanDelta >= 0 ? "+" : ""}${meanDelta.toFixed(1)} m vs baseline`;
const active = activeIndices();
const pollination =
active.reduce(
(sum, index) => sum + patches[index].pollination,
0
) / active.length;
document.querySelector("#pollinationValue").textContent =
`${Math.round(pollination * 100)}%`;
updatePatchAppearance();
}
window.addEventListener("resize", () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
});
// ---------------------------------------------------------------------
// Main loop
// ---------------------------------------------------------------------
let lastTime = performance.now();
let metricTimer = 0;
let elapsed = 0;
function animate(now) {
requestAnimationFrame(animate);
const realDt = Math.min((now - lastTime) / 1000, 0.05);
lastTime = now;
const dt = realDt * state.speed;
if (!state.paused) {
elapsed += dt;
const currentActive = activeIndices();
const baseActive = baselineIndices();
for (const entry of currentAgents) {
updateAgent(
entry.agent,
dt,
currentActive,
currentRichness,
currentStats,
true
);
}
for (const agent of baselineAgents) {
updateAgent(
agent,
dt,
baseActive,
baselineRichness,
baselineStats,
false
);
}
updateSparks(dt);
}
for (const entry of currentAgents) {
const { agent, visual } = entry;
visual.group.position.copy(agent.position);
visual.group.rotation.y = agent.yaw;
visual.group.rotation.z =
Math.sin(now * 0.011 + agent.serial) * 0.055;
visual.shadow.position.set(
agent.position.x,
groundHeight(agent.position.x, agent.position.z) + 0.035,
agent.position.z
);
visual.shadow.scale.setScalar(
1 - Math.min(0.25, (agent.position.y - 0.8) * 0.08)
);
for (const { pivot, side } of visual.wingPivots) {
pivot.rotation.z =
side * (0.2 + Math.sin(now * 0.045 + agent.serial) * 0.58);
}
}
cloudGroups.forEach((cloud, i) => {
cloud.position.x += realDt * (0.055 + i * 0.008);
if (cloud.position.x > 22) cloud.position.x = -22;
});
motes.rotation.y += realDt * 0.018;
metricTimer += realDt;
if (metricTimer > 0.15) {
metricTimer = 0;
updateMetrics();
}
renderer.render(scene, camera);
}
updateRangeFill(countInput);
updateRangeFill(bloomInput);
updateRangeFill(selectedBloomInput);
state.selectedPatch = activeIndices()[0];
syncSelectedControl();
removePatchButton.disabled = false;
addPatchButton.disabled = false;
updateCamera();
resetRun();
requestAnimationFrame(animate);
</script>
</body>
</html>
```