Prompt specification
Build or customize the Three.js 3D scene "Predator-prey microchamber" 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: Predator-prey microchamber
Category: Biology & Nature · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/predator-prey-microchamber-x0858
Scene Description Prompt:
Stylized protists engulf generic prey in a schematic microchamber; consumed objects disappear consistently. One HTML file using Three.js from cdnjs.
Working Three.js Starter (predator-prey-microchamber-x0858):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Protist Feeding in a Microchamber</title>
<style>
:root {
color-scheme: dark;
--ink: #ecfbff;
--muted: #91afb9;
--glass: rgba(8, 28, 40, 0.64);
--line: rgba(153, 224, 238, 0.18);
--cyan: #62e6d2;
--coral: #ff8d7d;
--gold: #ffd36b;
}
* { box-sizing: border-box; }
html, body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background:
radial-gradient(circle at 50% 42%, #123b4a 0%, #081d2b 43%, #040d17 100%);
color: var(--ink);
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}
canvas {
position: fixed;
inset: 0;
display: block;
width: 100%;
height: 100%;
touch-action: none;
cursor: grab;
outline: none;
}
canvas:active { cursor: grabbing; }
.panel {
position: fixed;
z-index: 10;
pointer-events: none;
}
.info {
top: 22px;
left: 22px;
width: min(340px, calc(100vw - 44px));
padding: 18px 18px 16px;
border: 1px solid var(--line);
border-radius: 18px;
background: linear-gradient(145deg, rgba(12, 42, 55, 0.78), var(--glass));
box-shadow: 0 18px 60px rgba(0, 0, 0, 0.25);
backdrop-filter: blur(16px);
}
.eyebrow {
display: flex;
align-items: center;
gap: 8px;
margin-bottom: 8px;
color: var(--cyan);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.19em;
text-transform: uppercase;
}
.live-dot {
width: 7px;
height: 7px;
border-radius: 50%;
background: var(--cyan);
box-shadow: 0 0 13px var(--cyan);
animation: pulse 1.8s ease-in-out infinite;
}
@keyframes pulse {
50% { opacity: 0.42; transform: scale(0.8); }
}
h1 {
margin: 0;
font-size: clamp(22px, 3vw, 30px);
font-weight: 720;
line-height: 1.08;
letter-spacing: -0.035em;
}
.subtitle {
margin: 8px 0 17px;
color: var(--muted);
font-size: 13px;
line-height: 1.45;
}
.stats {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 8px;
}
.stat {
padding: 10px 11px;
border: 1px solid rgba(159, 225, 235, 0.11);
border-radius: 11px;
background: rgba(1, 14, 23, 0.26);
}
.stat span {
display: block;
color: var(--muted);
font-size: 9px;
font-weight: 750;
letter-spacing: 0.13em;
text-transform: uppercase;
}
.stat strong {
display: block;
margin-top: 3px;
font-size: 20px;
font-variant-numeric: tabular-nums;
}
.progress-track {
height: 4px;
margin-top: 14px;
overflow: hidden;
border-radius: 99px;
background: rgba(255, 255, 255, 0.08);
}
#progress {
width: 0;
height: 100%;
border-radius: inherit;
background: linear-gradient(90deg, var(--cyan), var(--gold), var(--coral));
transition: width 250ms ease;
}
.legend {
display: flex;
flex-wrap: wrap;
gap: 12px;
margin-top: 13px;
color: var(--muted);
font-size: 10px;
}
.legend span {
display: inline-flex;
align-items: center;
gap: 5px;
}
.swatch {
width: 7px;
height: 7px;
border-radius: 50%;
background: var(--cyan);
box-shadow: 0 0 8px currentColor;
}
.swatch.prey { background: var(--gold); }
.swatch.consumed { background: var(--coral); }
.controls {
left: 50%;
bottom: 19px;
display: flex;
align-items: center;
gap: 12px;
padding: 9px 12px;
border: 1px solid var(--line);
border-radius: 999px;
background: rgba(5, 20, 29, 0.64);
color: var(--muted);
font-size: 11px;
white-space: nowrap;
transform: translateX(-50%);
backdrop-filter: blur(12px);
}
.controls b { color: var(--ink); font-weight: 650; }
#reset {
pointer-events: auto;
padding: 5px 9px;
border: 1px solid rgba(140, 222, 235, 0.18);
border-radius: 999px;
background: rgba(81, 193, 190, 0.09);
color: var(--ink);
font: inherit;
font-size: 10px;
cursor: pointer;
transition: background 160ms ease, border-color 160ms ease;
}
#reset:hover {
border-color: rgba(140, 222, 235, 0.4);
background: rgba(81, 193, 190, 0.17);
}
.corner-label {
right: 22px;
bottom: 22px;
color: rgba(173, 218, 228, 0.47);
font-size: 9px;
letter-spacing: 0.15em;
text-transform: uppercase;
writing-mode: vertical-rl;
}
@media (max-width: 600px) {
.info {
top: 12px;
left: 12px;
width: calc(100vw - 24px);
padding: 14px;
}
.subtitle { margin-bottom: 12px; }
.legend, .corner-label { display: none; }
.controls { bottom: 12px; }
}
</style>
</head>
<body>
<section class="panel info" aria-label="Simulation information">
<div class="eyebrow"><span class="live-dot"></span> Live microchamber</div>
<h1>Protist feeding study</h1>
<p class="subtitle">Stylized protists locate, engulf, and consume generic geometric prey.</p>
<div class="stats">
<div class="stat">
<span>Prey remaining</span>
<strong id="remaining">24</strong>
</div>
<div class="stat">
<span>Consumed</span>
<strong id="consumed">0</strong>
</div>
</div>
<div class="progress-track"><div id="progress"></div></div>
<div class="legend">
<span><i class="swatch"></i> Protists</span>
<span><i class="swatch prey"></i> Prey</span>
<span><i class="swatch consumed"></i> Engulfed</span>
</div>
</section>
<div class="panel controls">
<span><b>Drag</b> orbit</span>
<span><b>Scroll</b> zoom</span>
<span><b>Arrow keys</b> rotate</span>
<button id="reset" type="button">Reset view</button>
</div>
<div class="panel corner-label">Schematic chamber · 6.4 mm</div>
<script type="module">
import * as THREE from "https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// ---------- Renderer ----------
const renderer = new THREE.WebGLRenderer({ antialias: true, alpha: true });
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.12;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.domElement.tabIndex = 0;
renderer.domElement.setAttribute(
"aria-label",
"Interactive schematic of protists consuming prey in a microchamber"
);
document.body.prepend(renderer.domElement);
const scene = new THREE.Scene();
scene.fog = new THREE.FogExp2(0x071a27, 0.035);
const camera = new THREE.PerspectiveCamera(
42,
window.innerWidth / window.innerHeight,
0.1,
80
);
const clock = new THREE.Clock();
const chamberRadius = 6.4;
const floorY = -0.28;
// ---------- Lighting ----------
scene.add(new THREE.HemisphereLight(0xb9f4ff, 0x08202d, 2.25));
const key = new THREE.DirectionalLight(0xffffff, 3.1);
key.position.set(4, 8, 5);
key.castShadow = true;
key.shadow.mapSize.set(1024, 1024);
key.shadow.camera.left = -8;
key.shadow.camera.right = 8;
key.shadow.camera.top = 8;
key.shadow.camera.bottom = -8;
key.shadow.camera.near = 0.5;
key.shadow.camera.far = 25;
key.shadow.bias = -0.0006;
scene.add(key);
const cyanLight = new THREE.PointLight(0x43dfd2, 22, 20, 2);
cyanLight.position.set(-5, 3.5, -2);
scene.add(cyanLight);
const warmLight = new THREE.PointLight(0xffa46b, 17, 18, 2);
warmLight.position.set(4, 2.2, 4);
scene.add(warmLight);
// ---------- Chamber ----------
const chamber = new THREE.Group();
scene.add(chamber);
const floorMaterial = new THREE.MeshStandardMaterial({
color: 0x174353,
roughness: 0.76,
metalness: 0.04,
transparent: true,
opacity: 0.9
});
const floor = new THREE.Mesh(
new THREE.CylinderGeometry(chamberRadius, chamberRadius, 0.18, 96),
floorMaterial
);
floor.position.y = floorY;
floor.receiveShadow = true;
chamber.add(floor);
const wallMaterial = new THREE.MeshPhysicalMaterial({
color: 0x8ddbe8,
roughness: 0.15,
metalness: 0,
transparent: true,
opacity: 0.105,
side: THREE.DoubleSide,
depthWrite: false,
clearcoat: 1,
clearcoatRoughness: 0.12
});
const wall = new THREE.Mesh(
new THREE.CylinderGeometry(chamberRadius, chamberRadius, 3.25, 96, 1, true),
wallMaterial
);
wall.position.y = 1.33;
wall.renderOrder = 8;
chamber.add(wall);
const rimMaterial = new THREE.MeshStandardMaterial({
color: 0x8cd8e4,
emissive: 0x174d59,
emissiveIntensity: 0.65,
roughness: 0.28,
metalness: 0.2,
transparent: true,
opacity: 0.72
});
for (const y of [-0.16, 2.94]) {
const rim = new THREE.Mesh(
new THREE.TorusGeometry(chamberRadius, 0.055, 10, 128),
rimMaterial
);
rim.rotation.x = Math.PI / 2;
rim.position.y = y;
chamber.add(rim);
}
const gridMaterial = new THREE.LineBasicMaterial({
color: 0x78c6d3,
transparent: true,
opacity: 0.12,
depthWrite: false
});
for (const radius of [1.2, 2.4, 3.6, 4.8, 6.0]) {
const points = [];
for (let i = 0; i < 128; i++) {
const a = (i / 128) * Math.PI * 2;
points.push(new THREE.Vector3(Math.cos(a) * radius, floorY + 0.1, Math.sin(a) * radius));
}
const line = new THREE.LineLoop(
new THREE.BufferGeometry().setFromPoints(points),
gridMaterial
);
chamber.add(line);
}
const radialPoints = [];
for (let i = 0; i < 24; i++) {
const a = (i / 24) * Math.PI * 2;
radialPoints.push(
new THREE.Vector3(0, floorY + 0.102, 0),
new THREE.Vector3(Math.cos(a) * 6.1, floorY + 0.102, Math.sin(a) * 6.1)
);
}
chamber.add(new THREE.LineSegments(
new THREE.BufferGeometry().setFromPoints(radialPoints),
gridMaterial
));
const centerMark = new THREE.Mesh(
new THREE.RingGeometry(0.13, 0.17, 48),
new THREE.MeshBasicMaterial({
color: 0x9de8e7,
transparent: true,
opacity: 0.3,
side: THREE.DoubleSide
})
);
centerMark.rotation.x = -Math.PI / 2;
centerMark.position.y = floorY + 0.105;
chamber.add(centerMark);
// Suspended micro-particles.
const particleCount = 180;
const particlePositions = new Float32Array(particleCount * 3);
let randomSeed = 73421;
function random() {
randomSeed = (randomSeed * 1664525 + 1013904223) >>> 0;
return randomSeed / 4294967296;
}
for (let i = 0; i < particleCount; i++) {
const angle = random() * Math.PI * 2;
const radius = Math.sqrt(random()) * 5.95;
particlePositions[i * 3] = Math.cos(angle) * radius;
particlePositions[i * 3 + 1] = -0.1 + random() * 2.7;
particlePositions[i * 3 + 2] = Math.sin(angle) * radius;
}
const particleGeometry = new THREE.BufferGeometry();
particleGeometry.setAttribute(
"position",
new THREE.BufferAttribute(particlePositions, 3)
);
const suspendedParticles = new THREE.Points(
particleGeometry,
new THREE.PointsMaterial({
color: 0xc1f5f7,
size: 0.035,
transparent: true,
opacity: 0.35,
depthWrite: false
})
);
scene.add(suspendedParticles);
// ---------- Helpers ----------
const tempA = new THREE.Vector3();
const tempB = new THREE.Vector3();
const tempC = new THREE.Vector3();
const up = new THREE.Vector3(0, 1, 0);
function makeBodyGeometry() {
const geometry = new THREE.IcosahedronGeometry(0.82, 3);
const positions = geometry.attributes.position;
for (let i = 0; i < positions.count; i++) {
const x = positions.getX(i);
const y = positions.getY(i);
const z = positions.getZ(i);
const length = Math.hypot(x, y, z);
const nx = x / length;
const ny = y / length;
const nz = z / length;
const ripple =
1 +
0.045 * Math.sin(nx * 7.1 + ny * 3.7) +
0.025 * Math.cos(nz * 9.2 - nx * 4.4);
positions.setXYZ(i, x * ripple, y * ripple, z * ripple);
}
geometry.computeVertexNormals();
return geometry;
}
const ciliaGeometry = new THREE.CylinderGeometry(0.014, 0.025, 0.46, 6);
ciliaGeometry.translate(0, 0.23, 0);
const protists = [];
function createProtist(color, base, phase, speed) {
const root = new THREE.Group();
root.position.copy(base);
scene.add(root);
const visual = new THREE.Group();
root.add(visual);
const membraneMaterial = new THREE.MeshPhysicalMaterial({
color,
roughness: 0.27,
metalness: 0.02,
transparent: true,
opacity: 0.72,
depthWrite: false,
clearcoat: 0.9,
clearcoatRoughness: 0.16,
side: THREE.DoubleSide
});
const bodyGeometry = makeBodyGeometry();
const body = new THREE.Mesh(bodyGeometry, membraneMaterial);
body.castShadow = true;
body.renderOrder = 3;
visual.add(body);
const edges = new THREE.LineSegments(
new THREE.EdgesGeometry(bodyGeometry, 34),
new THREE.LineBasicMaterial({
color: 0xd8ffff,
transparent: true,
opacity: 0.15,
depthWrite: false
})
);
edges.renderOrder = 4;
visual.add(edges);
const nucleus = new THREE.Mesh(
new THREE.SphereGeometry(0.25, 28, 20),
new THREE.MeshStandardMaterial({
color: 0x213d68,
emissive: 0x102346,
emissiveIntensity: 0.6,
roughness: 0.4
})
);
nucleus.position.set(-0.2, 0.08, -0.18);
nucleus.scale.set(1, 0.86, 1);
visual.add(nucleus);
const nucleolus = new THREE.Mesh(
new THREE.SphereGeometry(0.09, 18, 12),
new THREE.MeshBasicMaterial({ color: 0x829fff })
);
nucleolus.position.set(-0.24, 0.1, -0.08);
visual.add(nucleolus);
const vacuoleMaterial = new THREE.MeshPhysicalMaterial({
color: 0xbdf7f4,
roughness: 0.08,
transparent: true,
opacity: 0.42,
depthWrite: false
});
for (let i = 0; i < 4; i++) {
const vacuole = new THREE.Mesh(
new THREE.SphereGeometry(0.07 + i * 0.012, 14, 10),
vacuoleMaterial
);
vacuole.position.set(
0.28 + Math.cos(i * 2.3) * 0.18,
-0.22 + i * 0.14,
-0.1 + Math.sin(i * 2.3) * 0.15
);
visual.add(vacuole);
}
const mouthAnchor = new THREE.Object3D();
mouthAnchor.position.set(0, 0, 0.79);
visual.add(mouthAnchor);
const cavity = new THREE.Mesh(
new THREE.SphereGeometry(0.24, 24, 16),
new THREE.MeshBasicMaterial({ color: 0x07141f })
);
cavity.scale.set(1, 0.68, 0.26);
mouthAnchor.add(cavity);
const mouthRing = new THREE.Mesh(
new THREE.TorusGeometry(0.265, 0.045, 10, 36),
new THREE.MeshStandardMaterial({
color: new THREE.Color(color).lerp(new THREE.Color(0xffffff), 0.22),
emissive: new THREE.Color(color).multiplyScalar(0.2),
roughness: 0.45
})
);
mouthRing.scale.y = 0.7;
mouthAnchor.add(mouthRing);
const ciliaCount = 30;
const ciliaMaterial = new THREE.MeshStandardMaterial({
color: new THREE.Color(color).lerp(new THREE.Color(0xeaffff), 0.5),
roughness: 0.5,
transparent: true,
opacity: 0.72
});
const cilia = new THREE.InstancedMesh(ciliaGeometry, ciliaMaterial, ciliaCount);
cilia.instanceMatrix.setUsage(THREE.DynamicDrawUsage);
cilia.frustumCulled = false;
visual.add(cilia);
const ciliaData = [];
const goldenAngle = Math.PI * (3 - Math.sqrt(5));
for (let i = 0; i < ciliaCount; i++) {
const y = 1 - (i / (ciliaCount - 1)) * 2;
const radial = Math.sqrt(Math.max(0, 1 - y * y));
const theta = i * goldenAngle;
const normal = new THREE.Vector3(
Math.cos(theta) * radial,
y * 0.55,
Math.sin(theta) * radial
).normalize();
const tangent = new THREE.Vector3().crossVectors(normal, up);
if (tangent.lengthSq() < 0.01) tangent.set(1, 0, 0);
tangent.normalize();
ciliaData.push({
normal,
tangent,
phase: random() * Math.PI * 2
});
}
const protist = {
root,
visual,
body,
nucleus,
mouthAnchor,
mouthRing,
cilia,
ciliaData,
base: base.clone(),
phase,
speed,
color,
target: null,
currentYaw: phase * 0.3,
mouthOpen: 0,
lastCapture: -10
};
protists.push(protist);
return protist;
}
createProtist(
0x54d8c5,
new THREE.Vector3(-2.1, 0.62, -1.45),
0.4,
0.82
);
createProtist(
0xff8879,
new THREE.Vector3(2.0, 0.65, -0.2),
2.2,
0.74
);
createProtist(
0xe8bd55,
new THREE.Vector3(0.25, 0.59, 2.15),
4.4,
0.9
);
// ---------- Generic prey ----------
const prey = [];
const preyColors = [0xffd36b, 0xff8f78, 0x83c9ff, 0xc79bff, 0xaee66f];
const preyGeometries = [
new THREE.IcosahedronGeometry(0.17, 1),
new THREE.OctahedronGeometry(0.18, 0),
new THREE.TetrahedronGeometry(0.2, 0)
];
const totalPrey = 24;
for (let i = 0; i < totalPrey; i++) {
let x, z;
do {
const angle = random() * Math.PI * 2;
const radius = 1.1 + Math.sqrt(random()) * 4.1;
x = Math.cos(angle) * radius;
z = Math.sin(angle) * radius;
} while (
protists.some((p) => Math.hypot(x - p.base.x, z - p.base.z) < 1.55)
);
const group = new THREE.Group();
group.position.set(x, -0.03 + random() * 0.48, z);
scene.add(group);
const color = preyColors[i % preyColors.length];
const geometry = preyGeometries[i % preyGeometries.length];
const mesh = new THREE.Mesh(
geometry,
new THREE.MeshStandardMaterial({
color,
emissive: color,
emissiveIntensity: 0.14,
roughness: 0.38,
metalness: 0.08
})
);
mesh.castShadow = true;
group.add(mesh);
const halo = new THREE.Mesh(
new THREE.TorusGeometry(0.235, 0.012, 6, 30),
new THREE.MeshBasicMaterial({
color,
transparent: true,
opacity: 0.45,
depthWrite: false
})
);
halo.rotation.set(random() * Math.PI, random() * Math.PI, random() * Math.PI);
group.add(halo);
const baseScale = 0.78 + random() * 0.36;
group.scale.setScalar(baseScale);
prey.push({
group,
mesh,
halo,
color,
baseScale,
state: "free",
target: null,
captureStart: 0,
captureFrom: new THREE.Vector3(),
spin: random() * Math.PI * 2
});
}
// ---------- Capture effects ----------
const pulses = [];
let consumedCount = 0;
const remainingLabel = document.getElementById("remaining");
const consumedLabel = document.getElementById("consumed");
const progressLabel = document.getElementById("progress");
function updateStats() {
remainingLabel.textContent = String(totalPrey - consumedCount);
consumedLabel.textContent = String(consumedCount);
progressLabel.style.width = `${(consumedCount / totalPrey) * 100}%`;
}
function makePulse(protist) {
const material = new THREE.MeshBasicMaterial({
color: protist.color,
transparent: true,
opacity: 0.8,
depthWrite: false
});
const mesh = new THREE.Mesh(
new THREE.TorusGeometry(0.18, 0.022, 8, 32),
material
);
protist.mouthAnchor.getWorldPosition(mesh.position);
mesh.quaternion.copy(protist.root.quaternion);
scene.add(mesh);
pulses.push({ mesh, age: 0, duration: 0.7 });
}
function chooseTarget(protist) {
let nearest = null;
let nearestDistance = Infinity;
for (const item of prey) {
if (item.state !== "free") continue;
const distance = protist.root.position.distanceToSquared(item.group.position);
if (distance < nearestDistance) {
nearestDistance = distance;
nearest = item;
}
}
if (nearest) {
nearest.state = "claimed";
nearest.target = protist;
protist.target = nearest;
}
}
function beginCapture(protist, item, time) {
item.state = "captured";
item.captureStart = time;
item.captureFrom.copy(item.group.position);
protist.lastCapture = time;
}
// ---------- Camera controls implemented directly ----------
const cameraTarget = new THREE.Vector3(0, 0.48, 0);
let cameraYaw = -0.12;
let cameraPitch = 0.55;
let cameraDistance = 11.8;
let desiredYaw = cameraYaw;
let desiredPitch = cameraPitch;
let desiredDistance = cameraDistance;
let dragging = false;
let previousX = 0;
let previousY = 0;
function updateCamera(dt) {
const damping = 1 - Math.exp(-dt * 8);
cameraYaw += (desiredYaw - cameraYaw) * damping;
cameraPitch += (desiredPitch - cameraPitch) * damping;
cameraDistance += (desiredDistance - cameraDistance) * damping;
const horizontal = Math.cos(cameraPitch) * cameraDistance;
camera.position.set(
cameraTarget.x + Math.sin(cameraYaw) * horizontal,
cameraTarget.y + Math.sin(cameraPitch) * cameraDistance,
cameraTarget.z + Math.cos(cameraYaw) * horizontal
);
camera.lookAt(cameraTarget);
}
renderer.domElement.addEventListener("pointerdown", (event) => {
dragging = true;
previousX = event.clientX;
previousY = event.clientY;
renderer.domElement.setPointerCapture(event.pointerId);
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (!dragging) return;
const deltaX = event.clientX - previousX;
const deltaY = event.clientY - previousY;
previousX = event.clientX;
previousY = event.clientY;
desiredYaw -= deltaX * 0.005;
desiredPitch = THREE.MathUtils.clamp(desiredPitch + deltaY * 0.004, 0.25, 1.12);
});
function endDrag(event) {
dragging = false;
if (renderer.domElement.hasPointerCapture(event.pointerId)) {
renderer.domElement.releasePointerCapture(event.pointerId);
}
}
renderer.domElement.addEventListener("pointerup", endDrag);
renderer.domElement.addEventListener("pointercancel", endDrag);
renderer.domElement.addEventListener(
"wheel",
(event) => {
event.preventDefault();
desiredDistance = THREE.MathUtils.clamp(
desiredDistance + event.deltaY * 0.008,
7.4,
18
);
},
{ passive: false }
);
window.addEventListener("keydown", (event) => {
if (event.key === "ArrowLeft") desiredYaw += 0.12;
if (event.key === "ArrowRight") desiredYaw -= 0.12;
if (event.key === "ArrowUp") desiredPitch = Math.min(1.12, desiredPitch + 0.08);
if (event.key === "ArrowDown") desiredPitch = Math.max(0.25, desiredPitch - 0.08);
if (event.key === "+" || event.key === "=") {
desiredDistance = Math.max(7.4, desiredDistance - 0.5);
}
if (event.key === "-") {
desiredDistance = Math.min(18, desiredDistance + 0.5);
}
});
document.getElementById("reset").addEventListener("click", () => {
desiredYaw = -0.12;
desiredPitch = 0.55;
desiredDistance = 11.8;
});
// ---------- Animation ----------
function updateCilia(protist, time) {
const matrix = new THREE.Matrix4();
const quaternion = new THREE.Quaternion();
for (let i = 0; i < protist.ciliaData.length; i++) {
const data = protist.ciliaData[i];
const wave = Math.sin(time * 4.2 + data.phase) * 0.2;
const direction = data.normal
.clone()
.multiplyScalar(Math.cos(wave))
.addScaledVector(data.tangent, Math.sin(wave))
.normalize();
quaternion.setFromUnitVectors(up, direction);
tempA.copy(direction).multiplyScalar(0.74);
matrix.compose(tempA, quaternion, tempB.set(1, 1, 1));
protist.cilia.setMatrixAt(i, matrix);
}
protist.cilia.instanceMatrix.needsUpdate = true;
}
function updateProtist(protist, time, dt) {
const target = protist.target;
let desiredYaw = protist.currentYaw + 0.05;
if (target && (target.state === "claimed" || target.state === "captured")) {
tempA.copy(target.group.position).sub(protist.root.position);
tempA.y = 0;
const distance = tempA.length();
if (distance > 0.001) {
desiredYaw = Math.atan2(tempA.x, tempA.z);
if (target.state === "claimed") {
const step = Math.min(distance, dt * protist.speed * (0.8 + distance * 0.12));
protist.root.position.addScaledVector(tempA.normalize(), step);
}
}
const opening = target.state === "captured"
? 1
: THREE.MathUtils.clamp(1 - distance / 2.1, 0, 0.82);
protist.mouthOpen += (opening - protist.mouthOpen) * (1 - Math.exp(-dt * 9));
if (
target.state === "claimed" &&
distance < 1.33 &&
time - protist.lastCapture > 0.45
) {
beginCapture(protist, target, time);
}
} else {
protist.target = null;
const patrolAngle = time * 0.18 * protist.speed + protist.phase;
tempA.set(
protist.base.x + Math.cos(patrolAngle) * 1.45,
protist.base.y,
protist.base.z + Math.sin(patrolAngle * 0.83) * 1.35
);
protist.root.position.lerp(tempA, 1 - Math.exp(-dt * 0.65));
desiredYaw = Math.atan2(
tempA.x - protist.root.position.x,
tempA.z - protist.root.position.z
);
protist.mouthOpen += (0 - protist.mouthOpen) * (1 - Math.exp(-dt * 5));
}
const radialDistance = Math.hypot(protist.root.position.x, protist.root.position.z);
if (radialDistance > 5.05) {
protist.root.position.x *= 5.05 / radialDistance;
protist.root.position.z *= 5.05 / radialDistance;
}
const yawDifference = Math.atan2(
Math.sin(desiredYaw - protist.currentYaw),
Math.cos(desiredYaw - protist.currentYaw)
);
protist.currentYaw += yawDifference * (1 - Math.exp(-dt * 5));
protist.root.rotation.y = protist.currentYaw;
protist.root.position.y =
protist.base.y + Math.sin(time * 1.15 + protist.phase) * 0.055;
const pulse = 1 + Math.sin(time * 2.1 + protist.phase) * 0.018;
protist.visual.scale.set(pulse, 1 / pulse, pulse);
protist.visual.rotation.z = Math.sin(time * 0.65 + protist.phase) * 0.045;
protist.nucleus.scale.set(
1 + Math.sin(time * 1.7 + protist.phase) * 0.035,
0.86 - Math.sin(time * 1.7 + protist.phase) * 0.025,
1 + Math.sin(time * 1.7 + protist.phase) * 0.035
);
protist.mouthAnchor.scale.set(
1 + protist.mouthOpen * 0.2,
0.7 + protist.mouthOpen * 0.42,
1
);
protist.mouthAnchor.position.z = 0.79 + protist.mouthOpen * 0.055;
updateCilia(protist, time);
}
function updatePrey(time, dt) {
for (const item of prey) {
if (item.state === "free" || item.state === "claimed") {
item.group.rotation.y += dt * (item.state === "claimed" ? 1.35 : 0.52);
item.group.rotation.x += dt * 0.23;
item.halo.rotation.z += dt * 0.7;
item.halo.material.opacity =
item.state === "claimed" ? 0.72 + Math.sin(time * 8) * 0.18 : 0.4;
continue;
}
if (item.state !== "captured") continue;
const progress = THREE.MathUtils.clamp(
(time - item.captureStart) / 1.08,
0,
1
);
const eased = progress * progress * (3 - 2 * progress);
item.target.mouthAnchor.getWorldPosition(tempA);
item.group.position.lerpVectors(item.captureFrom, tempA, eased);
const dx = tempA.x - item.captureFrom.x;
const dz = tempA.z - item.captureFrom.z;
const sideLength = Math.hypot(dx, dz) || 1;
const swirl = Math.sin(progress * Math.PI) * 0.22;
item.group.position.x += (-dz / sideLength) * swirl;
item.group.position.z += (dx / sideLength) * swirl;
item.group.rotation.y += dt * 7;
item.group.rotation.z += dt * 5;
item.group.scale.setScalar(item.baseScale * Math.max(0, 1 - eased));
if (progress >= 1) {
item.state = "consumed";
item.group.visible = false;
item.target = null;
consumedCount += 1;
updateStats();
makePulse(item.target || protists[0]);
}
}
}
function updatePulses(dt) {
for (let i = pulses.length - 1; i >= 0; i--) {
const pulse = pulses[i];
pulse.age += dt;
const progress = pulse.age / pulse.duration;
pulse.mesh.scale.setScalar(0.5 + progress * 1.8);
pulse.mesh.material.opacity = Math.max(0, 0.8 * (1 - progress));
if (progress >= 1) {
scene.remove(pulse.mesh);
pulse.mesh.geometry.dispose();
pulse.mesh.material.dispose();
pulses.splice(i, 1);
}
}
}
function animate() {
const dt = Math.min(clock.getDelta(), 0.05);
const time = clock.elapsedTime;
for (const protist of protists) {
if (!protist.target) chooseTarget(protist);
updateProtist(protist, time, dt);
}
updatePrey(time, dt);
updatePulses(dt);
updateCamera(dt);
suspendedParticles.rotation.y = time * 0.012;
suspendedParticles.position.y = Math.sin(time * 0.25) * 0.035;
renderer.render(scene, camera);
}
window.addEventListener("resize", () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
});
updateStats();
renderer.setAnimationLoop(animate);
</script>
</body>
</html>
```