Prompt specification
Build or customize the Three.js 3D scene "Tissue cleanup microenvironment" 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: Tissue cleanup microenvironment
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/tissue-cleanup-microenvironment-x0867
Scene Description Prompt:
Animate a voxel phagocytic cell completely enclosing debris among generic tissue cells. Build one HTML file using Three.js from cdnjs.
Working Three.js Starter (tissue-cleanup-microenvironment-x0867):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Voxel Phagocytosis</title>
<style>
:root {
color-scheme: dark;
--panel: rgba(8, 13, 31, 0.76);
--border: rgba(146, 180, 255, 0.2);
--text: #eaf2ff;
--muted: #91a4c7;
--pink: #f472d0;
--cyan: #59e1dc;
--gold: #ffc857;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #060a18;
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
#app,
canvas {
position: fixed;
inset: 0;
width: 100%;
height: 100%;
}
canvas {
display: block;
touch-action: none;
cursor: grab;
}
canvas:active {
cursor: grabbing;
}
.glass {
background: var(--panel);
border: 1px solid var(--border);
box-shadow: 0 18px 60px rgba(0, 0, 0, 0.28);
backdrop-filter: blur(14px);
-webkit-backdrop-filter: blur(14px);
}
#title-card {
position: fixed;
top: 22px;
left: 22px;
z-index: 5;
width: min(360px, calc(100vw - 44px));
padding: 17px 19px;
border-radius: 18px;
pointer-events: none;
}
.eyebrow {
display: flex;
align-items: center;
gap: 9px;
margin-bottom: 8px;
color: var(--cyan);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.19em;
text-transform: uppercase;
}
.eyebrow::before {
width: 7px;
height: 7px;
border-radius: 2px;
background: var(--cyan);
box-shadow: 0 0 16px var(--cyan);
content: "";
}
h1 {
margin: 0;
font-size: clamp(21px, 3vw, 30px);
line-height: 1.05;
letter-spacing: -0.04em;
}
#title-card p {
margin: 10px 0 0;
color: var(--muted);
font-size: 12px;
line-height: 1.55;
}
#legend {
position: fixed;
top: 22px;
right: 22px;
z-index: 5;
display: flex;
gap: 13px;
padding: 11px 14px;
border-radius: 14px;
pointer-events: none;
}
.legend-item {
display: flex;
align-items: center;
gap: 7px;
color: #b9c8e2;
font-size: 10px;
font-weight: 700;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.swatch {
width: 9px;
height: 9px;
border-radius: 2px;
box-shadow: 0 0 10px currentColor;
}
.swatch.cell {
color: var(--pink);
background: var(--pink);
}
.swatch.tissue {
color: #60a5fa;
background: #60a5fa;
}
.swatch.debris {
color: var(--gold);
background: var(--gold);
}
#tag {
position: fixed;
z-index: 4;
padding: 5px 8px;
border: 1px solid currentColor;
border-radius: 7px;
background: rgba(5, 9, 23, 0.8);
box-shadow: 0 0 18px color-mix(in srgb, currentColor 30%, transparent);
color: var(--gold);
font-size: 9px;
font-weight: 850;
letter-spacing: 0.12em;
text-transform: uppercase;
transform: translate(-50%, -100%);
pointer-events: none;
white-space: nowrap;
}
#cell-tag {
color: var(--pink);
}
#bottom {
position: fixed;
right: 22px;
bottom: 22px;
left: 22px;
z-index: 5;
display: grid;
grid-template-columns: minmax(0, 1fr) auto;
align-items: center;
gap: 18px;
padding: 15px 18px;
border-radius: 18px;
pointer-events: none;
}
#timeline-wrap {
min-width: 0;
}
#status-row {
display: flex;
align-items: center;
justify-content: space-between;
gap: 15px;
margin-bottom: 10px;
}
#status {
overflow: hidden;
color: #dce8ff;
font-size: 12px;
font-weight: 700;
text-overflow: ellipsis;
white-space: nowrap;
}
#time {
color: var(--muted);
font-size: 10px;
font-variant-numeric: tabular-nums;
letter-spacing: 0.08em;
}
#progress-track {
position: relative;
height: 4px;
overflow: hidden;
border-radius: 10px;
background: rgba(133, 158, 204, 0.16);
}
#progress {
width: 100%;
height: 100%;
transform: scaleX(0);
transform-origin: left center;
border-radius: inherit;
background: linear-gradient(90deg, var(--pink), var(--cyan), var(--gold));
box-shadow: 0 0 15px rgba(89, 225, 220, 0.5);
}
#stages {
display: grid;
grid-template-columns: repeat(6, 1fr);
gap: 8px;
margin-top: 9px;
}
.stage {
color: #617296;
font-size: 8px;
font-weight: 800;
letter-spacing: 0.09em;
text-align: center;
text-transform: uppercase;
transition: color 180ms ease;
}
.stage.active {
color: #f4f8ff;
}
.stage.done {
color: #7ddbd7;
}
#controls {
display: flex;
gap: 8px;
pointer-events: auto;
}
button {
min-width: 76px;
padding: 10px 13px;
border: 1px solid rgba(146, 180, 255, 0.2);
border-radius: 10px;
background: rgba(85, 104, 149, 0.14);
color: #dce8ff;
font: inherit;
font-size: 10px;
font-weight: 800;
letter-spacing: 0.07em;
text-transform: uppercase;
cursor: pointer;
transition:
background 160ms ease,
border-color 160ms ease,
transform 160ms ease;
}
button:hover {
border-color: rgba(146, 180, 255, 0.45);
background: rgba(99, 126, 181, 0.24);
transform: translateY(-1px);
}
button:active {
transform: translateY(0);
}
#hint {
position: fixed;
right: 24px;
bottom: 111px;
z-index: 4;
color: rgba(166, 187, 220, 0.62);
font-size: 9px;
font-weight: 700;
letter-spacing: 0.08em;
pointer-events: none;
text-transform: uppercase;
}
@media (max-width: 760px) {
#title-card {
top: 12px;
left: 12px;
width: calc(100vw - 24px);
}
#legend {
display: none;
}
#bottom {
right: 12px;
bottom: 12px;
left: 12px;
grid-template-columns: 1fr;
gap: 12px;
}
#controls {
justify-content: stretch;
}
button {
flex: 1;
min-width: 0;
}
#stages {
gap: 3px;
}
.stage {
font-size: 7px;
letter-spacing: 0.02em;
}
#hint {
display: none;
}
}
</style>
</head>
<body>
<div id="app"></div>
<section id="title-card" class="glass">
<div class="eyebrow">Voxel microscopy / live specimen</div>
<h1>Phagocytic Encapsulation</h1>
<p>
A macrophage extends voxel pseudopods, seals a phagosome around the
golden debris, internalizes it, and begins fragmentation.
</p>
</section>
<section id="legend" class="glass">
<div class="legend-item">
<span class="swatch cell"></span>
Phagocyte
</div>
<div class="legend-item">
<span class="swatch tissue"></span>
Tissue
</div>
<div class="legend-item">
<span class="swatch debris"></span>
Debris
</div>
</section>
<div id="tag">Debris</div>
<div id="cell-tag">Phagocyte</div>
<section id="bottom" class="glass">
<div id="timeline-wrap">
<div id="status-row">
<div id="status">Contact — phagocyte recognizes nearby debris</div>
<div id="time">00.0 / 14.0 s</div>
</div>
<div id="progress-track">
<div id="progress"></div>
</div>
<div id="stages">
<div class="stage active">Contact</div>
<div class="stage">Pseudopods</div>
<div class="stage">Engulf</div>
<div class="stage">Seal</div>
<div class="stage">Internalize</div>
<div class="stage">Digest</div>
</div>
</div>
<div id="controls">
<button id="replay" type="button">Replay</button>
<button id="pause" type="button">Pause</button>
<button id="xray" type="button">X-ray on</button>
</div>
</section>
<div id="hint">Drag to orbit · Scroll to zoom</div>
<script type="module">
import * as THREE from "https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// -------------------------------------------------------------------------
// Setup
// -------------------------------------------------------------------------
const app = document.getElementById("app");
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x060a18);
scene.fog = new THREE.FogExp2(0x060a18, 0.023);
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.15;
app.appendChild(renderer.domElement);
const camera = new THREE.OrthographicCamera(-10, 10, 10, -10, 0.1, 100);
const cameraTarget = new THREE.Vector3(0.45, 0.05, 0);
let cameraYaw = 0.68;
let cameraPitch = 0.46;
let cameraZoom = 1;
let dragging = false;
let lastPointerX = 0;
let lastPointerY = 0;
function updateCamera() {
const distance = 24;
const horizontal = Math.cos(cameraPitch) * distance;
camera.position.set(
cameraTarget.x + Math.sin(cameraYaw) * horizontal,
cameraTarget.y + Math.sin(cameraPitch) * distance,
cameraTarget.z + Math.cos(cameraYaw) * horizontal
);
camera.lookAt(cameraTarget);
const aspect = window.innerWidth / window.innerHeight;
const halfHeight = (window.innerWidth < 760 ? 10.6 : 9.15) / cameraZoom;
camera.left = -halfHeight * aspect;
camera.right = halfHeight * aspect;
camera.top = halfHeight;
camera.bottom = -halfHeight;
camera.updateProjectionMatrix();
camera.updateMatrixWorld();
}
renderer.domElement.addEventListener("pointerdown", (event) => {
dragging = true;
lastPointerX = event.clientX;
lastPointerY = event.clientY;
renderer.domElement.setPointerCapture(event.pointerId);
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (!dragging) return;
const dx = event.clientX - lastPointerX;
const dy = event.clientY - lastPointerY;
lastPointerX = event.clientX;
lastPointerY = event.clientY;
cameraYaw -= dx * 0.006;
cameraPitch = THREE.MathUtils.clamp(
cameraPitch + dy * 0.005,
0.08,
1.28
);
});
function stopDragging(event) {
dragging = false;
if (event?.pointerId !== undefined) {
try {
renderer.domElement.releasePointerCapture(event.pointerId);
} catch (_) {}
}
}
renderer.domElement.addEventListener("pointerup", stopDragging);
renderer.domElement.addEventListener("pointercancel", stopDragging);
renderer.domElement.addEventListener(
"wheel",
(event) => {
event.preventDefault();
cameraZoom = THREE.MathUtils.clamp(
cameraZoom * Math.exp(-event.deltaY * 0.001),
0.7,
1.65
);
updateCamera();
},
{ passive: false }
);
window.addEventListener("resize", () => {
renderer.setSize(window.innerWidth, window.innerHeight);
updateCamera();
});
// -------------------------------------------------------------------------
// Lighting
// -------------------------------------------------------------------------
scene.add(new THREE.HemisphereLight(0xc7e8ff, 0x170b2d, 2.2));
const keyLight = new THREE.DirectionalLight(0xffffff, 3.2);
keyLight.position.set(7, 11, 8);
scene.add(keyLight);
const magentaLight = new THREE.PointLight(0xe94cff, 22, 19, 2);
magentaLight.position.set(-3, 2, 5);
scene.add(magentaLight);
const cyanLight = new THREE.PointLight(0x42e8ff, 18, 18, 2);
cyanLight.position.set(5, -2, 2);
scene.add(cyanLight);
const warmLight = new THREE.PointLight(0xffa62b, 12, 10, 2);
warmLight.position.set(6, 2, 0);
scene.add(warmLight);
// -------------------------------------------------------------------------
// Utilities
// -------------------------------------------------------------------------
const dummy = new THREE.Object3D();
const tempColor = new THREE.Color();
const tempVector = new THREE.Vector3();
const tempVectorB = new THREE.Vector3();
function smooth(a, b, value) {
const x = THREE.MathUtils.clamp((value - a) / (b - a), 0, 1);
return x * x * (3 - 2 * x);
}
function mulberry32(seed) {
return function () {
let t = (seed += 0x6d2b79f5);
t = Math.imul(t ^ (t >>> 15), t | 1);
t ^= t + Math.imul(t ^ (t >>> 7), t | 61);
return ((t ^ (t >>> 14)) >>> 0) / 4294967296;
};
}
const random = mulberry32(91357);
function fibonacciDirections(count) {
const directions = [];
const goldenAngle = Math.PI * (3 - Math.sqrt(5));
for (let i = 0; i < count; i++) {
const y = 1 - (i / (count - 1)) * 2;
const radius = Math.sqrt(Math.max(0, 1 - y * y));
const angle = goldenAngle * i;
directions.push(
new THREE.Vector3(
Math.cos(angle) * radius,
y,
Math.sin(angle) * radius
)
);
}
return directions;
}
function makeInstancedMesh(count, material, parent = scene) {
const mesh = new THREE.InstancedMesh(
new THREE.BoxGeometry(1, 1, 1),
material,
count
);
mesh.instanceMatrix.setUsage(THREE.DynamicDrawUsage);
mesh.frustumCulled = false;
parent.add(mesh);
return mesh;
}
function setInstance(mesh, index, position, scale, rotation = null) {
dummy.position.copy(position);
dummy.scale.setScalar(scale);
if (rotation) {
dummy.rotation.set(rotation.x, rotation.y, rotation.z);
} else {
dummy.rotation.set(0, 0, 0);
}
dummy.updateMatrix();
mesh.setMatrixAt(index, dummy.matrix);
}
// -------------------------------------------------------------------------
// Generic voxel tissue cells
// -------------------------------------------------------------------------
const tissuePalette = [
0x38c9c1,
0x5c9df2,
0x8d7cf3,
0x48b8dc,
0x5fc7a6,
0x727fe8
];
const tissueSpecs = [
{ center: [-7.8, 0.4, 3.6], radius: 1.15 },
{ center: [-6.4, 3.8, -5.0], radius: 1.32 },
{ center: [-1.0, 4.5, -7.7], radius: 1.10 },
{ center: [5.6, 4.3, -5.7], radius: 1.38 },
{ center: [8.0, 1.2, -1.4], radius: 1.05 },
{ center: [7.2, -3.6, 3.5], radius: 1.25 },
{ center: [1.0, -6.0, 3.8], radius: 1.12 },
{ center: [-5.8, -4.2, -2.5], radius: 1.28 },
{ center: [-8.7, -1.0, -0.5], radius: 0.95 },
{ center: [3.0, 0.5, 8.4], radius: 1.18 },
{ center: [9.0, 3.8, 0.2], radius: 0.94 },
{ center: [2.8, 6.2, -5.2], radius: 1.08 },
{ center: [-2.4, 1.3, 8.8], radius: 1.05 },
{ center: [4.0, -5.0, -5.0], radius: 1.16 }
];
const tissueShellMaterial = new THREE.MeshStandardMaterial({
color: 0xffffff,
transparent: true,
opacity: 0.68,
roughness: 0.68,
metalness: 0.02,
depthWrite: false
});
const tissueNucleusMaterial = new THREE.MeshStandardMaterial({
color: 0x254b9e,
roughness: 0.45,
metalness: 0.08,
emissive: 0x0a1742,
emissiveIntensity: 0.45
});
const tissueCells = [];
tissueSpecs.forEach((spec, cellIndex) => {
const group = new THREE.Group();
group.position.fromArray(spec.center);
scene.add(group);
const directions = fibonacciDirections(88);
const shell = makeInstancedMesh(
directions.length,
tissueShellMaterial.clone(),
group
);
const baseColor = new THREE.Color(
tissuePalette[cellIndex % tissuePalette.length]
);
directions.forEach((direction, index) => {
const local = direction.clone().multiplyScalar(spec.radius);
setInstance(shell, index, local, 0.25 * spec.radius);
tempColor.copy(baseColor);
tempColor.offsetHSL(
(random() - 0.5) * 0.035,
(random() - 0.5) * 0.08,
(random() - 0.5) * 0.12
);
shell.setColorAt(index, tempColor);
});
shell.instanceColor.needsUpdate = true;
const nucleusCount = 15;
const nucleus = makeInstancedMesh(
nucleusCount,
tissueNucleusMaterial.clone(),
group
);
for (let i = 0; i < nucleusCount; i++) {
const local = new THREE.Vector3(
(random() - 0.5) * 0.55 * spec.radius,
(random() - 0.5) * 0.45 * spec.radius,
(random() - 0.5) * 0.55 * spec.radius
);
setInstance(nucleus, i, local, 0.21 * spec.radius);
}
tissueCells.push({
group,
base: new THREE.Vector3().fromArray(spec.center),
phase: random() * Math.PI * 2
});
});
// -------------------------------------------------------------------------
// Large voxel phagocyte
// -------------------------------------------------------------------------
const cellMaterial = new THREE.MeshStandardMaterial({
color: 0xffffff,
transparent: true,
opacity: 0.48,
roughness: 0.38,
metalness: 0.12,
emissive: 0x220524,
emissiveIntensity: 0.32,
depthWrite: false
});
const cellDirections = fibonacciDirections(2200);
const cellMesh = makeInstancedMesh(cellDirections.length, cellMaterial);
const pinkA = new THREE.Color(0xf34fc8);
const pinkB = new THREE.Color(0x8847ee);
const pinkC = new THREE.Color(0xff6f9d);
cellDirections.forEach((direction, index) => {
const mix = 0.5 + 0.5 * Math.sin(direction.y * 5.2 + direction.x * 2.4);
tempColor.lerpColors(pinkB, pinkA, mix);
if (direction.x > 0.45) {
tempColor.lerp(pinkC, 0.38);
}
tempColor.offsetHSL(
(random() - 0.5) * 0.025,
(random() - 0.5) * 0.08,
(random() - 0.5) * 0.09
);
cellMesh.setColorAt(index, tempColor);
});
cellMesh.instanceColor.needsUpdate = true;
// Sparse internal cytoplasm voxels.
const cytoplasmGroup = new THREE.Group();
scene.add(cytoplasmGroup);
const cytoplasmMaterial = new THREE.MeshStandardMaterial({
color: 0xa557ef,
transparent: true,
opacity: 0.13,
roughness: 0.8,
depthWrite: false
});
const cytoplasm = makeInstancedMesh(330, cytoplasmMaterial, cytoplasmGroup);
for (let i = 0; i < 330; i++) {
const direction = new THREE.Vector3(
random() * 2 - 1,
random() * 2 - 1,
random() * 2 - 1
);
if (direction.lengthSq() < 0.001) direction.set(0, 1, 0);
direction.normalize().multiplyScalar(Math.cbrt(random()) * 3.8);
setInstance(cytoplasm, i, direction, 0.11 + random() * 0.11);
}
// -------------------------------------------------------------------------
// Voxel pseudopods
// -------------------------------------------------------------------------
const pseudopodMaterial = new THREE.MeshStandardMaterial({
color: 0xf15bc7,
roughness: 0.36,
metalness: 0.08,
emissive: 0x54113f,
emissiveIntensity: 0.28,
transparent: true,
opacity: 0.9
});
const armCurves = [
new THREE.CatmullRomCurve3([
new THREE.Vector3(1.9, 2.65, 2.0),
new THREE.Vector3(3.45, 2.75, 1.75),
new THREE.Vector3(5.0, 2.0, 1.05),
new THREE.Vector3(5.65, 0.85, 0.35),
new THREE.Vector3(5.15, -0.45, -0.15),
new THREE.Vector3(4.55, -0.75, -0.35)
]),
new THREE.CatmullRomCurve3([
new THREE.Vector3(2.0, -2.55, 1.95),
new THREE.Vector3(3.5, -2.45, 1.7),
new THREE.Vector3(5.05, -1.75, 0.95),
new THREE.Vector3(5.55, -0.65, 0.25),
new THREE.Vector3(5.15, 0.35, -0.18),
new THREE.Vector3(4.6, 0.55, -0.4)
])
];
const pseudopodCount = armCurves.length * 48;
const pseudopods = makeInstancedMesh(pseudopodCount, pseudopodMaterial);
const pseudopodColors = [
new THREE.Color(0xff62c9),
new THREE.Color(0xb957ef),
new THREE.Color(0xff7ba9)
];
for (let i = 0; i < pseudopodCount; i++) {
pseudopods.setColorAt(
i,
pseudopodColors[i % pseudopodColors.length]
);
}
pseudopods.instanceColor.needsUpdate = true;
// -------------------------------------------------------------------------
// Debris cluster
// -------------------------------------------------------------------------
const debrisGroup = new THREE.Group();
scene.add(debrisGroup);
const debrisMaterial = new THREE.MeshStandardMaterial({
color: 0xffffff,
roughness: 0.36,
metalness: 0.08,
emissive: 0x7a3100,
emissiveIntensity: 0.48
});
const debrisCount = 54;
const debrisMesh = makeInstancedMesh(
debrisCount,
debrisMaterial,
debrisGroup
);
const debrisData = [];
for (let i = 0; i < debrisCount; i++) {
const direction = new THREE.Vector3(
random() * 2 - 1,
random() * 2 - 1,
random() * 2 - 1
);
if (direction.lengthSq() < 0.001) direction.set(1, 0, 0);
direction.normalize();
const local = direction.multiplyScalar(Math.cbrt(random()) * 0.76);
const scale = 0.18 + random() * 0.15;
const drift = new THREE.Vector3(
(random() - 0.5) * 0.85,
(random() - 0.5) * 0.85,
(random() - 0.5) * 0.85
);
debrisData.push({ local, drift, scale });
const gold = new THREE.Color(
random() > 0.42 ? 0xffbd3e : 0xf47a24
);
debrisMesh.setColorAt(i, gold);
}
debrisMesh.instanceColor.needsUpdate = true;
// -------------------------------------------------------------------------
// Phagosome and sealing membrane
// -------------------------------------------------------------------------
const phagosomeGroup = new THREE.Group();
scene.add(phagosomeGroup);
const phagosomeMaterial = new THREE.MeshStandardMaterial({
color: 0x61e8e1,
transparent: true,
opacity: 0.42,
roughness: 0.3,
metalness: 0.05,
emissive: 0x0b575b,
emissiveIntensity: 0.55,
depthWrite: false
});
const phagosomeDirections = fibonacciDirections(150);
const phagosome = makeInstancedMesh(
phagosomeDirections.length,
phagosomeMaterial,
phagosomeGroup
);
phagosomeDirections.forEach((direction, index) => {
setInstance(
phagosome,
index,
direction.clone().multiplyScalar(1.06),
0.18
);
tempColor.setHSL(
0.49 + (random() - 0.5) * 0.04,
0.82,
0.57 + (random() - 0.5) * 0.12
);
phagosome.setColorAt(index, tempColor);
});
phagosome.instanceColor.needsUpdate = true;
const sealGroup = new THREE.Group();
scene.add(sealGroup);
const sealMaterial = new THREE.MeshStandardMaterial({
color: 0x9bfff4,
emissive: 0x2a9c9a,
emissiveIntensity: 0.8,
roughness: 0.25,
transparent: true,
opacity: 0.9
});
const sealCount = 76;
const sealMesh = makeInstancedMesh(
sealCount,
sealMaterial,
sealGroup
);
for (let i = 0; i < sealCount; i++) {
const angle = (i / sealCount) * Math.PI * 2;
const position = new THREE.Vector3(
0,
Math.cos(angle) * 1.5,
Math.sin(angle) * 1.5
);
setInstance(sealMesh, i, position, 0.19);
tempColor.setHSL(0.48, 0.9, 0.64 + random() * 0.12);
sealMesh.setColorAt(i, tempColor);
}
sealMesh.instanceColor.needsUpdate = true;
// -------------------------------------------------------------------------
// Digestion motes
// -------------------------------------------------------------------------
const moteMaterial = new THREE.MeshStandardMaterial({
color: 0xffd36a,
emissive: 0xff8a22,
emissiveIntensity: 1.2,
roughness: 0.3
});
const moteCount = 72;
const motes = makeInstancedMesh(moteCount, moteMaterial);
const moteData = [];
for (let i = 0; i < moteCount; i++) {
moteData.push({
angle: random() * Math.PI * 2,
speed: 0.35 + random() * 1.2,
radius: 0.4 + random() * 2.5,
height: (random() - 0.5) * 2.8,
size: 0.045 + random() * 0.085
});
}
// -------------------------------------------------------------------------
// Background voxel particles
// -------------------------------------------------------------------------
const dustMaterial = new THREE.MeshStandardMaterial({
color: 0x7e9bd4,
transparent: true,
opacity: 0.28,
roughness: 0.9,
depthWrite: false
});
const dust = makeInstancedMesh(180, dustMaterial);
for (let i = 0; i < 180; i++) {
const position = new THREE.Vector3(
(random() - 0.5) * 28,
(random() - 0.5) * 19,
-9 - random() * 12
);
setInstance(dust, i, position, 0.035 + random() * 0.055);
}
// -------------------------------------------------------------------------
// Animation
// -------------------------------------------------------------------------
const duration = 14;
let elapsed = 0;
let paused = false;
let xray = true;
const clock = new THREE.Clock();
const debrisTag = document.getElementById("tag");
const cellTag = document.getElementById("cell-tag");
const statusElement = document.getElementById("status");
const timeElement = document.getElementById("time");
const progressElement = document.getElementById("progress");
const stageElements = [...document.querySelectorAll(".stage")];
const statusMessages = [
"Contact — phagocyte recognizes nearby debris",
"Pseudopods — two membrane arms extend toward the particle",
"Engulfment — the cell membrane wraps around the debris",
"Sealing — a closed phagosome forms around the particle",
"Internalization — the enclosed particle moves into the cytoplasm",
"Digestion — voxel fragments disperse inside the phagosome"
];
const stageTimes = [0, 1.2, 2.8, 5.2, 6.5, 9.0];
function sphereExit(direction, center, radius) {
const b = direction.dot(center);
const c = center.lengthSq() - radius * radius;
const discriminant = b * b - c;
if (discriminant < 0) return -Infinity;
const exit = b + Math.sqrt(discriminant);
return exit > 0 ? exit : -Infinity;
}
function updateCellShell(time) {
const growth = smooth(2.45, 6.05, time);
const retract = smooth(8.25, 9.55, time);
const lobeCenter = tempVector.set(4.05, 0.34, 0).lerp(
new THREE.Vector3(4.75, 0.3, 0),
growth
);
lobeCenter.x = THREE.MathUtils.lerp(lobeCenter.x, 3.75, retract);
lobeCenter.y = THREE.MathUtils.lerp(lobeCenter.y, 0.1, retract);
let lobeRadius = THREE.MathUtils.lerp(0.58, 1.45, growth);
lobeRadius = THREE.MathUtils.lerp(lobeRadius, 0.54, retract);
const baseRadius =
4.14 +
Math.sin(time * 1.35) * 0.055 +
Math.sin(time * 2.1 + 0.7) * 0.025;
for (let i = 0; i < cellDirections.length; i++) {
const direction = cellDirections[i];
const wobble =
1 +
Math.sin(direction.x * 7.1 + time * 1.4) * 0.016 +
Math.sin(direction.y * 8.7 - time * 1.1) * 0.012 +
Math.sin(direction.z * 6.3 + direction.x * 3.0) * 0.009;
const bodyRadius = baseRadius * wobble;
const lobeSurface = sphereExit(
direction,
lobeCenter,
lobeRadius
);
const radius = Math.max(bodyRadius, lobeSurface);
tempVectorB.copy(direction).multiplyScalar(radius);
const voxelScale = 0.345 + (i % 5) * 0.006;
setInstance(cellMesh, i, tempVectorB, voxelScale);
}
cellMesh.instanceMatrix.needsUpdate = true;
}
function updatePseudopods(time) {
const extension = smooth(1.15, 4.55, time);
const merge = 1 - smooth(5.65, 6.45, time);
let index = 0;
armCurves.forEach((curve, armIndex) => {
for (let i = 0; i < 48; i++) {
const u = i / 47;
const point = curve.getPointAt(u, tempVectorB);
const leadingEdge = 1 - smooth(extension - 0.075, extension, u);
const tipTaper = THREE.MathUtils.lerp(1, 0.65, smooth(0.82, 1, u));
const visible =
smooth(0, 0.025, extension) * leadingEdge * merge;
const scale = 0.35 * tipTaper * visible;
tempVector.copy(point);
tempVector.y += Math.sin(time * 2 + armIndex + i * 0.13) * 0.025;
setInstance(pseudopods, index, tempVector, scale);
index++;
}
});
pseudopods.instanceMatrix.needsUpdate = true;
}
function updateDebris(time) {
const approach = smooth(0, 1.45, time);
const internalize = smooth(6.45, 9.15, time);
const digest = smooth(9.05, 11.7, time);
const start = new THREE.Vector3(5.85, 0.75, 0.35);
const contact = new THREE.Vector3(4.75, 0.3, 0);
const internal = new THREE.Vector3(0.75, 0.1, 0.18);
debrisGroup.position.lerpVectors(start, contact, approach);
debrisGroup.position.lerp(internal, internalize);
debrisGroup.rotation.set(
time * 0.19,
time * 0.27,
Math.sin(time * 0.42) * 0.18
);
for (let i = 0; i < debrisCount; i++) {
const item = debrisData[i];
tempVector.copy(item.local);
tempVector.addScaledVector(item.drift, digest * 0.62);
tempVector.y += Math.sin(time * 2.4 + i) * digest * 0.12;
const scale = item.scale * (1 - digest * 0.9);
setInstance(debrisMesh, i, tempVector, scale);
}
debrisMesh.instanceMatrix.needsUpdate = true;
}
function updatePhagosome(time) {
const seal = smooth(5.15, 6.35, time);
const digest = smooth(9.05, 11.8, time);
phagosomeGroup.position.copy(debrisGroup.position);
phagosomeGroup.rotation.copy(debrisGroup.rotation);
const size =
seal *
(1 - digest * 0.28) *
(1 + Math.sin(time * 2.6) * 0.025);
phagosomeGroup.scale.setScalar(Math.max(0.001, size));
phagosomeGroup.visible = seal > 0.005;
const ringLife = smooth(4.95, 5.55, time);
const ringRetract = 1 - smooth(6.25, 6.95, time);
const ringVisibility = ringLife * ringRetract;
sealGroup.position.copy(debrisGroup.position);
sealGroup.visible = ringVisibility > 0.005;
const ringRadius = THREE.MathUtils.lerp(1.52, 1.02, smooth(5.1, 6.5, time));
for (let i = 0; i < sealCount; i++) {
const angle = (i / sealCount) * Math.PI * 2;
tempVector.set(
Math.sin(time * 2.2 + angle * 3) * 0.045,
Math.cos(angle) * ringRadius,
Math.sin(angle) * ringRadius
);
const scale = 0.2 * ringVisibility;
setInstance(sealMesh, i, tempVector, scale);
}
sealMesh.instanceMatrix.needsUpdate = true;
sealMaterial.opacity = 0.9 * ringVisibility;
}
function updateMotes(time) {
const visibility = smooth(8.9, 10.0, time);
const fade = 1 - smooth(11.65, 13.7, time);
for (let i = 0; i < moteCount; i++) {
const item = moteData[i];
const angle = item.angle + time * item.speed;
tempVector.set(
Math.cos(angle) * item.radius,
item.height + Math.sin(angle * 1.7) * 0.45,
Math.sin(angle) * item.radius
);
const scale = item.size * visibility * fade;
setInstance(motes, i, tempVector, scale);
}
motes.instanceMatrix.needsUpdate = true;
}
function updateTissue(time) {
tissueCells.forEach((cell, index) => {
cell.group.position.copy(cell.base);
cell.group.position.x += Math.sin(time * 0.42 + cell.phase) * 0.15;
cell.group.position.y += Math.cos(time * 0.37 + cell.phase) * 0.13;
cell.group.position.z += Math.sin(time * 0.29 + cell.phase * 1.4) * 0.1;
cell.group.rotation.y = Math.sin(time * 0.23 + cell.phase) * 0.12;
cell.group.rotation.z = Math.cos(time * 0.21 + index) * 0.05;
const pulse = 1 + Math.sin(time * 1.2 + cell.phase) * 0.018;
cell.group.scale.setScalar(pulse);
});
}
function placeLabel(element, worldPosition, verticalOffset = 0) {
tempVector.copy(worldPosition);
tempVector.y += verticalOffset;
tempVector.project(camera);
const visible = tempVector.z > -1 && tempVector.z < 1;
element.style.display = visible ? "block" : "none";
const x = (tempVector.x * 0.5 + 0.5) * window.innerWidth;
const y = (-tempVector.y * 0.5 + 0.5) * window.innerHeight;
element.style.left = `${x}px`;
element.style.top = `${y}px`;
}
function updateInterface(time) {
progressElement.style.transform = `scaleX(${time / duration})`;
timeElement.textContent = `${time.toFixed(1).padStart(4, "0")} / ${duration.toFixed(1)} s`;
let activeStage = 0;
for (let i = 0; i < stageTimes.length; i++) {
if (time >= stageTimes[i]) activeStage = i;
}
stageElements.forEach((element, index) => {
element.classList.toggle("active", index === activeStage);
element.classList.toggle("done", index < activeStage);
});
statusElement.textContent = statusMessages[activeStage];
if (time >= 9.0) {
debrisTag.textContent = "Phagosome";
} else if (time >= 5.2) {
debrisTag.textContent = "Sealed debris";
} else {
debrisTag.textContent = "Debris";
}
placeLabel(debrisTag, debrisGroup.position, 1.15);
placeLabel(
cellTag,
new THREE.Vector3(-1.7, 3.7, 0.2),
0.2
);
}
// -------------------------------------------------------------------------
// Controls
// -------------------------------------------------------------------------
const replayButton = document.getElementById("replay");
const pauseButton = document.getElementById("pause");
const xrayButton = document.getElementById("xray");
replayButton.addEventListener("click", () => {
elapsed = 0;
});
pauseButton.addEventListener("click", () => {
paused = !paused;
pauseButton.textContent = paused ? "Resume" : "Pause";
});
xrayButton.addEventListener("click", () => {
xray = !xray;
cellMaterial.opacity = xray ? 0.48 : 0.94;
cellMaterial.depthWrite = !xray;
cytoplasmMaterial.opacity = xray ? 0.13 : 0.04;
xrayButton.textContent = xray ? "X-ray on" : "X-ray off";
});
// -------------------------------------------------------------------------
// Render loop
// -------------------------------------------------------------------------
function animate() {
requestAnimationFrame(animate);
const delta = Math.min(clock.getDelta(), 0.05);
if (!paused) elapsed += delta;
const time = elapsed % duration;
if (!dragging) cameraYaw += delta * 0.025;
updateCamera();
updateCellShell(time);
updatePseudopods(time);
updateDebris(time);
updatePhagosome(time);
updateMotes(time);
updateTissue(time);
cytoplasmGroup.rotation.y = time * 0.035;
cytoplasmGroup.rotation.z = Math.sin(time * 0.17) * 0.045;
updateInterface(time);
renderer.render(scene, camera);
}
updateCamera();
animate();
</script>
</body>
</html>
```