Prompt specification
Build or customize the Three.js 3D scene "Airway clearance educational cross-section" 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: Airway clearance educational cross-section
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/airway-clearance-educational-cross-section-x0827
Scene Description Prompt:
Animate voxel airway cilia transporting mucus along a simplified epithelial lining. Make a single HTML file using Three.js from cdnjs.
Working Three.js Starter (airway-clearance-educational-cross-section-x0827):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Voxel Airway — Ciliary Mucus Transport</title>
<style>
:root {
color-scheme: dark;
--panel: rgba(7, 17, 31, 0.72);
--border: rgba(151, 219, 255, 0.18);
--text: #e9f8ff;
--muted: #8da9b8;
--cyan: #68dcff;
--mint: #8fffd5;
--pink: #ff83ae;
--mucus: #b8ff58;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background:
radial-gradient(circle at 48% 36%, #15334a 0%, #0b1b2d 38%, #050b15 78%),
#050b15;
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
color: var(--text);
}
#scene {
position: fixed;
inset: 0;
width: 100%;
height: 100%;
display: block;
touch-action: none;
outline: none;
}
.hud {
position: fixed;
inset: 0;
z-index: 2;
pointer-events: none;
}
.panel {
position: absolute;
border: 1px solid var(--border);
background: var(--panel);
box-shadow: 0 16px 44px rgba(0, 0, 0, 0.28);
backdrop-filter: blur(14px);
-webkit-backdrop-filter: blur(14px);
}
.title-panel {
top: 22px;
left: 22px;
width: min(390px, calc(100vw - 44px));
padding: 17px 19px 16px;
border-radius: 14px;
}
.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: 50%;
background: var(--mint);
box-shadow: 0 0 13px var(--mint);
content: "";
}
h1 {
margin: 0;
font-size: clamp(19px, 2.2vw, 29px);
line-height: 1.08;
letter-spacing: -0.035em;
}
.subtitle {
margin: 9px 0 0;
color: var(--muted);
font-size: 12px;
line-height: 1.5;
}
.legend {
left: 22px;
bottom: 22px;
display: grid;
gap: 9px;
min-width: 190px;
padding: 13px 15px;
border-radius: 12px;
font-size: 11px;
}
.legend-row {
display: flex;
align-items: center;
gap: 10px;
color: #c8dbe5;
}
.swatch {
width: 11px;
height: 11px;
flex: 0 0 auto;
border-radius: 2px;
box-shadow: 0 0 12px currentColor;
}
.swatch.cilia {
color: var(--mint);
background: var(--mint);
}
.swatch.mucus {
color: var(--mucus);
background: var(--mucus);
}
.swatch.cells {
color: var(--pink);
background: var(--pink);
}
.hint {
margin-top: 2px;
padding-top: 9px;
border-top: 1px solid rgba(255, 255, 255, 0.08);
color: #728e9d;
font-size: 10px;
}
.controls {
right: 22px;
bottom: 22px;
width: 245px;
padding: 14px;
border-radius: 13px;
pointer-events: auto;
}
.control-header {
display: flex;
align-items: center;
justify-content: space-between;
margin-bottom: 10px;
}
.control-title {
color: #b9d1dc;
font-size: 10px;
font-weight: 800;
letter-spacing: 0.14em;
text-transform: uppercase;
}
.beat-state {
color: var(--mint);
font-size: 9px;
font-weight: 700;
letter-spacing: 0.08em;
}
.beat-track {
height: 4px;
overflow: hidden;
margin-bottom: 13px;
border-radius: 8px;
background: rgba(255, 255, 255, 0.08);
}
.beat-fill {
width: 0;
height: 100%;
border-radius: inherit;
background: linear-gradient(90deg, var(--cyan), var(--mint));
box-shadow: 0 0 12px rgba(104, 220, 255, 0.8);
}
.button-row {
display: flex;
gap: 8px;
}
button {
min-height: 34px;
border: 1px solid rgba(151, 219, 255, 0.2);
border-radius: 8px;
color: var(--text);
background: rgba(255, 255, 255, 0.055);
font: inherit;
font-size: 11px;
cursor: pointer;
transition: background 150ms ease, border-color 150ms ease, transform 150ms ease;
}
button:hover {
border-color: rgba(104, 220, 255, 0.55);
background: rgba(104, 220, 255, 0.12);
}
button:active {
transform: translateY(1px);
}
#pause {
flex: 1;
color: #07131e;
border-color: transparent;
background: var(--cyan);
font-weight: 800;
}
#reset {
padding: 0 12px;
}
.speed-row {
display: flex;
align-items: center;
gap: 10px;
margin-top: 12px;
color: var(--muted);
font-size: 10px;
}
input[type="range"] {
width: 100%;
height: 4px;
accent-color: var(--cyan);
cursor: pointer;
}
#speed-value {
width: 31px;
color: #cbe8f3;
text-align: right;
font-variant-numeric: tabular-nums;
}
#flow-label {
position: fixed;
z-index: 3;
display: none;
padding: 5px 8px;
transform: translate(-50%, -140%);
border: 1px solid rgba(104, 220, 255, 0.26);
border-radius: 6px;
color: var(--cyan);
background: rgba(5, 13, 24, 0.75);
font-size: 9px;
font-weight: 800;
letter-spacing: 0.12em;
white-space: nowrap;
pointer-events: none;
}
#loading {
position: fixed;
inset: 0;
z-index: 10;
display: grid;
place-items: center;
color: var(--cyan);
background: #07111f;
font-size: 11px;
font-weight: 800;
letter-spacing: 0.18em;
text-transform: uppercase;
transition: opacity 350ms ease;
}
#loading.hidden {
opacity: 0;
pointer-events: none;
}
@media (max-width: 700px) {
.title-panel {
top: 12px;
left: 12px;
width: calc(100vw - 24px);
}
.subtitle {
max-width: 280px;
}
.legend {
display: none;
}
.controls {
right: 12px;
bottom: 12px;
width: 220px;
}
}
</style>
</head>
<body>
<canvas id="scene" aria-label="Animated voxel airway epithelium with cilia transporting mucus"></canvas>
<div class="hud">
<section class="panel title-panel">
<div class="eyebrow">Respiratory Microenvironment</div>
<h1>Voxel Airway</h1>
<p class="subtitle">
Coordinated ciliary waves propel voxel mucus parcels across a simplified epithelial lining.
</p>
</section>
<section class="panel legend" aria-label="Scene legend">
<div class="legend-row">
<span class="swatch cells"></span>
<span>Epithelial cells</span>
</div>
<div class="legend-row">
<span class="swatch cilia"></span>
<span>Metachronal cilia</span>
</div>
<div class="legend-row">
<span class="swatch mucus"></span>
<span>Transported mucus</span>
</div>
<div class="hint">Drag to orbit · Scroll to zoom · Double-click to reset</div>
</section>
<section class="panel controls">
<div class="control-header">
<span class="control-title">Ciliary Beat</span>
<span id="beat-state" class="beat-state">POWER STROKE</span>
</div>
<div class="beat-track">
<div id="beat-fill" class="beat-fill"></div>
</div>
<div class="button-row">
<button id="pause" type="button">Pause</button>
<button id="reset" type="button">Reset view</button>
</div>
<label class="speed-row">
Speed
<input id="speed" type="range" min="0.25" max="2.25" step="0.05" value="1" />
<span id="speed-value">1.0×</span>
</label>
</section>
</div>
<div id="flow-label">MUCUS TRANSPORT +X</div>
<div id="loading">Building airway surface…</div>
<script type="module">
import * as THREE from "https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const canvas = document.querySelector("#scene");
const loading = document.querySelector("#loading");
const pauseButton = document.querySelector("#pause");
const resetButton = document.querySelector("#reset");
const speedInput = document.querySelector("#speed");
const speedValue = document.querySelector("#speed-value");
const beatFill = document.querySelector("#beat-fill");
const beatState = document.querySelector("#beat-state");
const flowLabel = document.querySelector("#flow-label");
// Deterministic randomness keeps the voxel arrangement stable between reloads.
function mulberry32(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 = mulberry32(74291);
const clamp = THREE.MathUtils.clamp;
const mod = (value, divisor) => ((value % divisor) + divisor) % divisor;
function smooth01(value) {
const t = clamp(value, 0, 1);
return t * t * (3 - 2 * t);
}
function smoothstep(edge0, edge1, value) {
return smooth01((value - edge0) / (edge1 - edge0));
}
const renderer = new THREE.WebGLRenderer({
canvas,
antialias: true,
alpha: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 1.75));
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setClearColor(0x07111f, 0);
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.08;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
const scene = new THREE.Scene();
scene.fog = new THREE.FogExp2(0x07111f, 0.017);
const camera = new THREE.PerspectiveCamera(
42,
window.innerWidth / window.innerHeight,
0.1,
100
);
const cameraState = {
theta: 0.58,
phi: 1.02,
radius: window.innerWidth < 700 ? 27 : 23,
target: new THREE.Vector3(0, 0.35, 0)
};
function updateCamera() {
const { theta, phi, radius, target } = cameraState;
const sinPhi = Math.sin(phi);
camera.position.set(
target.x + radius * sinPhi * Math.sin(theta),
target.y + radius * Math.cos(phi),
target.z + radius * sinPhi * Math.cos(theta)
);
camera.lookAt(target);
}
updateCamera();
// Lighting.
scene.add(new THREE.HemisphereLight(0xbceeff, 0x40162f, 1.65));
const keyLight = new THREE.DirectionalLight(0xffffff, 2.7);
keyLight.position.set(-5, 13, 8);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(1536, 1536);
keyLight.shadow.camera.left = -14;
keyLight.shadow.camera.right = 14;
keyLight.shadow.camera.top = 11;
keyLight.shadow.camera.bottom = -11;
keyLight.shadow.camera.near = 1;
keyLight.shadow.camera.far = 38;
keyLight.shadow.bias = -0.00025;
scene.add(keyLight);
const cyanLight = new THREE.PointLight(0x48caff, 15, 30, 2);
cyanLight.position.set(-8, 5, 7);
scene.add(cyanLight);
const pinkLight = new THREE.PointLight(0xff4f93, 11, 25, 2);
pinkLight.position.set(9, 3, -6);
scene.add(pinkLight);
// Airway surface parameters.
const CELL_COUNT_X = 17;
const CELL_COUNT_Z = 11;
const SPACING_X = 1.08;
const SPACING_Z = 1.03;
const CELL_HEIGHT = 0.58;
const CELL_COUNT = CELL_COUNT_X * CELL_COUNT_Z;
function surfaceAt(x, z) {
return (
-0.08 +
0.16 * Math.cos(x * 0.32) +
0.012 * z * z
);
}
const dummy = new THREE.Object3D();
// Dark supporting cell layer.
const supportGeometry = new THREE.BoxGeometry(1.11, 0.22, 1.06);
const supportMaterial = new THREE.MeshStandardMaterial({
color: 0xffffff,
roughness: 0.88,
metalness: 0.01
});
const supportMesh = new THREE.InstancedMesh(
supportGeometry,
supportMaterial,
CELL_COUNT
);
supportMesh.castShadow = true;
supportMesh.receiveShadow = true;
// Main epithelial blocks.
const cellGeometry = new THREE.BoxGeometry(0.98, CELL_HEIGHT, 0.94);
const cellMaterial = new THREE.MeshStandardMaterial({
color: 0xffffff,
roughness: 0.68,
metalness: 0.025
});
const cellMesh = new THREE.InstancedMesh(
cellGeometry,
cellMaterial,
CELL_COUNT
);
cellMesh.castShadow = true;
cellMesh.receiveShadow = true;
// Lighter apical membrane tiles.
const membraneGeometry = new THREE.BoxGeometry(0.78, 0.08, 0.78);
const membraneMaterial = new THREE.MeshStandardMaterial({
color: 0xffffff,
roughness: 0.58,
metalness: 0.03
});
const membraneMesh = new THREE.InstancedMesh(
membraneGeometry,
membraneMaterial,
CELL_COUNT
);
membraneMesh.castShadow = true;
membraneMesh.receiveShadow = true;
const ciliaRoots = [];
const cellColor = new THREE.Color();
const supportColor = new THREE.Color();
const membraneColor = new THREE.Color();
let cellIndex = 0;
for (let ix = 0; ix < CELL_COUNT_X; ix++) {
const x = (ix - (CELL_COUNT_X - 1) / 2) * SPACING_X;
for (let iz = 0; iz < CELL_COUNT_Z; iz++) {
const z = (iz - (CELL_COUNT_Z - 1) / 2) * SPACING_Z;
const surface = surfaceAt(x, z);
dummy.position.set(x, surface - 0.68, z);
dummy.rotation.set(0, 0, 0);
dummy.scale.set(1, 1, 1);
dummy.updateMatrix();
supportMesh.setMatrixAt(cellIndex, dummy.matrix);
supportColor.setHSL(
0.97 + (random() - 0.5) * 0.025,
0.34,
0.25 + random() * 0.055
);
supportMesh.setColorAt(cellIndex, supportColor);
dummy.position.set(x, surface - CELL_HEIGHT / 2, z);
dummy.updateMatrix();
cellMesh.setMatrixAt(cellIndex, dummy.matrix);
cellColor.setHSL(
0.955 + (random() - 0.5) * 0.035,
0.38 + random() * 0.1,
0.56 + random() * 0.1
);
cellMesh.setColorAt(cellIndex, cellColor);
dummy.position.set(x, surface + 0.015, z);
dummy.updateMatrix();
membraneMesh.setMatrixAt(cellIndex, dummy.matrix);
membraneColor.setHSL(
0.95 + (random() - 0.5) * 0.025,
0.34,
0.72 + random() * 0.07
);
membraneMesh.setColorAt(cellIndex, membraneColor);
ciliaRoots.push({
x,
y: surface + 0.055,
z,
variation: random() * Math.PI * 2
});
cellIndex++;
}
}
supportMesh.instanceMatrix.needsUpdate = true;
cellMesh.instanceMatrix.needsUpdate = true;
membraneMesh.instanceMatrix.needsUpdate = true;
if (supportMesh.instanceColor) supportMesh.instanceColor.needsUpdate = true;
if (cellMesh.instanceColor) cellMesh.instanceColor.needsUpdate = true;
if (membraneMesh.instanceColor) membraneMesh.instanceColor.needsUpdate = true;
scene.add(supportMesh, cellMesh, membraneMesh);
// Segmented voxel cilia.
const CILIA_SEGMENTS = 7;
const CILIA_LENGTH = 0.14;
const MAX_BEND = 1.18;
const ciliaGeometry = new THREE.BoxGeometry(
CILIA_LENGTH,
CILIA_LENGTH,
CILIA_LENGTH
);
const ciliaMaterial = new THREE.MeshStandardMaterial({
color: 0xffffff,
roughness: 0.4,
metalness: 0.04,
emissive: 0x092b2b,
emissiveIntensity: 0.22
});
const ciliaMesh = new THREE.InstancedMesh(
ciliaGeometry,
ciliaMaterial,
ciliaRoots.length * CILIA_SEGMENTS
);
ciliaMesh.instanceMatrix.setUsage(THREE.DynamicDrawUsage);
ciliaMesh.castShadow = true;
ciliaMesh.receiveShadow = true;
ciliaMesh.frustumCulled = false;
const ciliaColor = new THREE.Color();
let ciliaInstance = 0;
for (const root of ciliaRoots) {
for (let segment = 0; segment < CILIA_SEGMENTS; segment++) {
const f = segment / (CILIA_SEGMENTS - 1);
ciliaColor.setHSL(
0.47 + f * 0.065,
0.52 + (1 - f) * 0.12,
0.68 + f * 0.19
);
ciliaMesh.setColorAt(ciliaInstance++, ciliaColor);
}
}
if (ciliaMesh.instanceColor) ciliaMesh.instanceColor.needsUpdate = true;
scene.add(ciliaMesh);
// Mucus parcels are clusters of green voxels.
const MUCUS_COUNT = 19;
const MUCUS_LIMIT = 9.25;
const mucusParcels = [];
let mucusCubeCount = 0;
for (let i = 0; i < MUCUS_COUNT; i++) {
const cubeCount = 6 + Math.floor(random() * 5);
const cubes = [];
for (let cube = 0; cube < cubeCount; cube++) {
cubes.push({
offset: new THREE.Vector3(
(random() - 0.5) * 0.52,
(random() - 0.5) * 0.3,
(random() - 0.5) * 0.48
),
size: 0.78 + random() * 0.62,
color: new THREE.Color().setHSL(
0.27 + random() * 0.1,
0.64 + random() * 0.2,
0.47 + random() * 0.15
)
});
mucusCubeCount++;
}
const lane = -4.7 + random() * 9.4;
const initialX =
-MUCUS_LIMIT +
((i + 0.35) / MUCUS_COUNT) * MUCUS_LIMIT * 2 +
(random() - 0.5) * 0.65;
mucusParcels.push({
x: initialX,
z: lane,
seed: random() * Math.PI * 2,
cubes
});
}
const mucusGeometry = new THREE.BoxGeometry(0.25, 0.25, 0.25);
const mucusMaterial = new THREE.MeshStandardMaterial({
color: 0xffffff,
roughness: 0.31,
metalness: 0.02,
emissive: 0x123b12,
emissiveIntensity: 0.24
});
const mucusMesh = new THREE.InstancedMesh(
mucusGeometry,
mucusMaterial,
mucusCubeCount
);
mucusMesh.instanceMatrix.setUsage(THREE.DynamicDrawUsage);
mucusMesh.castShadow = true;
mucusMesh.receiveShadow = true;
mucusMesh.frustumCulled = false;
let mucusColorIndex = 0;
for (const parcel of mucusParcels) {
for (const cube of parcel.cubes) {
mucusMesh.setColorAt(mucusColorIndex++, cube.color);
}
}
if (mucusMesh.instanceColor) mucusMesh.instanceColor.needsUpdate = true;
scene.add(mucusMesh);
// Voxel direction indicator.
const flowGroup = new THREE.Group();
flowGroup.position.set(1.4, 1.35, -6.05);
const flowMaterial = new THREE.MeshBasicMaterial({
color: 0x68dcff,
transparent: true,
opacity: 0.52,
depthWrite: false
});
const flowShaft = new THREE.Mesh(
new THREE.BoxGeometry(1.8, 0.11, 0.11),
flowMaterial
);
flowShaft.position.x = -0.35;
flowGroup.add(flowShaft);
const flowHeadTop = new THREE.Mesh(
new THREE.BoxGeometry(0.72, 0.11, 0.11),
flowMaterial
);
flowHeadTop.position.set(0.79, 0.18, 0);
flowHeadTop.rotation.z = -0.62;
flowGroup.add(flowHeadTop);
const flowHeadBottom = flowHeadTop.clone();
flowHeadBottom.position.y = -0.18;
flowHeadBottom.rotation.z = 0.62;
flowGroup.add(flowHeadBottom);
const flowPulse = new THREE.Mesh(
new THREE.BoxGeometry(0.16, 0.16, 0.16),
new THREE.MeshBasicMaterial({
color: 0xb9f5ff,
transparent: true,
opacity: 0.9,
depthWrite: false
})
);
flowGroup.add(flowPulse);
scene.add(flowGroup);
const flowLabelPosition = new THREE.Vector3(
flowGroup.position.x,
flowGroup.position.y + 0.6,
flowGroup.position.z
);
function phaseAt(x, z, time) {
return mod(time * 0.72 - x * 0.17 + z * 0.028, 1);
}
function bendAt(phase) {
if (phase < 0.45) {
return MAX_BEND * smooth01(phase / 0.45);
}
if (phase < 0.78) {
return MAX_BEND;
}
return MAX_BEND * (1 - smooth01((phase - 0.78) / 0.22));
}
function ciliaTipRise(bend) {
let rise = 0;
for (let segment = 0; segment < CILIA_SEGMENTS; segment++) {
const f = segment / (CILIA_SEGMENTS - 1);
const angle = bend * (0.08 + 0.92 * f);
rise += Math.cos(angle) * CILIA_LENGTH;
}
return rise;
}
function updateCilia(time) {
let instance = 0;
for (const root of ciliaRoots) {
const phase = phaseAt(root.x, root.z, time);
const bend = bendAt(phase);
let x = root.x;
let y = root.y;
let z = root.z;
for (let segment = 0; segment < CILIA_SEGMENTS; segment++) {
const f = segment / (CILIA_SEGMENTS - 1);
const angle = bend * (0.08 + 0.92 * f);
const sideSweep =
Math.sin(time * 1.45 + root.x * 0.34 + root.z * 0.53 + root.variation) *
0.035 *
f;
const directionX = Math.sin(angle);
const directionY = Math.cos(angle);
const directionZ = sideSweep;
dummy.position.set(
x + directionX * CILIA_LENGTH * 0.5,
y + directionY * CILIA_LENGTH * 0.5,
z + directionZ * CILIA_LENGTH * 0.5
);
dummy.rotation.set(0, 0, -angle);
dummy.scale.setScalar(1 - f * 0.1);
dummy.updateMatrix();
ciliaMesh.setMatrixAt(instance++, dummy.matrix);
x += directionX * CILIA_LENGTH;
y += directionY * CILIA_LENGTH;
z += directionZ * CILIA_LENGTH;
}
}
ciliaMesh.instanceMatrix.needsUpdate = true;
}
const groupPosition = new THREE.Vector3();
const groupScale = new THREE.Vector3();
const groupQuaternion = new THREE.Quaternion();
const groupEuler = new THREE.Euler();
const identityQuaternion = new THREE.Quaternion();
const groupMatrix = new THREE.Matrix4();
const localMatrix = new THREE.Matrix4();
const finalMatrix = new THREE.Matrix4();
function updateMucus(time, delta) {
let instance = 0;
for (const parcel of mucusParcels) {
let phase = phaseAt(parcel.x, parcel.z, time);
const impulse =
smoothstep(0.08, 0.3, phase) *
(1 - smoothstep(0.42, 0.57, phase));
// The mucus receives forward impulses from the power stroke and
// does not reverse during the elastic recovery phase.
parcel.x += delta * (0.24 + impulse * 1.8);
if (parcel.x > MUCUS_LIMIT) {
parcel.x -= MUCUS_LIMIT * 2;
}
phase = phaseAt(parcel.x, parcel.z, time);
const bend = bendAt(phase);
const tipRise = ciliaTipRise(bend);
const edgeFade =
smoothstep(-MUCUS_LIMIT, -MUCUS_LIMIT + 1.35, parcel.x) *
(1 - smoothstep(MUCUS_LIMIT - 1.35, MUCUS_LIMIT, parcel.x));
groupPosition.set(
parcel.x,
surfaceAt(parcel.x, parcel.z) +
0.055 +
tipRise +
0.1 +
Math.sin(time * 2 + parcel.seed) * 0.035,
parcel.z
);
groupEuler.set(
Math.sin(time * 0.7 + parcel.seed) * 0.06,
time * 0.28 + parcel.seed,
Math.sin(time * 0.9 + parcel.seed) * 0.08
);
groupQuaternion.setFromEuler(groupEuler);
groupScale.setScalar(edgeFade);
groupMatrix.compose(groupPosition, groupQuaternion, groupScale);
for (const cube of parcel.cubes) {
localMatrix.compose(
cube.offset,
identityQuaternion,
new THREE.Vector3(cube.size, cube.size, cube.size)
);
finalMatrix.multiplyMatrices(groupMatrix, localMatrix);
mucusMesh.setMatrixAt(instance++, finalMatrix);
}
}
mucusMesh.instanceMatrix.needsUpdate = true;
}
// Direct camera controls: pointer orbit, wheel/pinch zoom, double-click reset.
const pointers = new Map();
let previousPinchDistance = 0;
function resetView() {
cameraState.theta = 0.58;
cameraState.phi = 1.02;
cameraState.radius = window.innerWidth < 700 ? 27 : 23;
updateCamera();
}
function currentPinchDistance() {
const values = [...pointers.values()];
if (values.length < 2) return 0;
return Math.hypot(
values[0].x - values[1].x,
values[0].y - values[1].y
);
}
canvas.addEventListener("pointerdown", (event) => {
canvas.setPointerCapture(event.pointerId);
pointers.set(event.pointerId, { x: event.clientX, y: event.clientY });
if (pointers.size === 2) {
previousPinchDistance = currentPinchDistance();
}
});
canvas.addEventListener("pointermove", (event) => {
const previous = pointers.get(event.pointerId);
if (!previous) return;
const deltaX = event.clientX - previous.x;
const deltaY = event.clientY - previous.y;
pointers.set(event.pointerId, {
x: event.clientX,
y: event.clientY
});
if (pointers.size === 1) {
cameraState.theta -= deltaX * 0.006;
cameraState.phi = clamp(cameraState.phi - deltaY * 0.005, 0.3, 1.38);
} else if (pointers.size === 2) {
const distance = currentPinchDistance();
if (previousPinchDistance > 0 && distance > 0) {
cameraState.radius = clamp(
cameraState.radius * (previousPinchDistance / distance),
12,
36
);
}
previousPinchDistance = distance;
}
});
function releasePointer(event) {
pointers.delete(event.pointerId);
previousPinchDistance = pointers.size === 2 ? currentPinchDistance() : 0;
}
canvas.addEventListener("pointerup", releasePointer);
canvas.addEventListener("pointercancel", releasePointer);
canvas.addEventListener(
"wheel",
(event) => {
event.preventDefault();
cameraState.radius = clamp(
cameraState.radius * Math.exp(event.deltaY * 0.001),
12,
36
);
},
{ passive: false }
);
canvas.addEventListener("dblclick", resetView);
resetButton.addEventListener("click", resetView);
let paused = false;
let speed = 1;
pauseButton.addEventListener("click", () => {
paused = !paused;
pauseButton.textContent = paused ? "Resume" : "Pause";
});
speedInput.addEventListener("input", () => {
speed = Number(speedInput.value);
speedValue.textContent = `${speed.toFixed(1)}×`;
});
function updateProjectedLabel() {
const projected = flowLabelPosition.clone().project(camera);
const visible = projected.z > -1 && projected.z < 1;
flowLabel.style.display = visible ? "block" : "none";
if (visible) {
flowLabel.style.left = `${(projected.x * 0.5 + 0.5) * window.innerWidth}px`;
flowLabel.style.top = `${(-projected.y * 0.5 + 0.5) * window.innerHeight}px`;
}
}
function resize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.fov = window.innerWidth < 700 ? 49 : 42;
camera.updateProjectionMatrix();
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 1.75));
renderer.setSize(window.innerWidth, window.innerHeight);
}
window.addEventListener("resize", resize);
resize();
const clock = new THREE.Clock();
let simulationTime = 0;
let firstFrame = true;
function animate() {
requestAnimationFrame(animate);
const frameDelta = Math.min(clock.getDelta(), 0.05);
const simulationDelta = paused ? 0 : frameDelta * speed;
simulationTime += simulationDelta;
updateCilia(simulationTime);
updateMucus(simulationTime, simulationDelta);
updateCamera();
flowPulse.position.x = -1.15 + mod(simulationTime * 0.72, 1) * 2.35;
flowPulse.material.opacity = 0.55 + Math.sin(simulationTime * 5) * 0.3;
flowMaterial.opacity = 0.42 + Math.sin(simulationTime * 1.8) * 0.09;
const centerPhase = phaseAt(0, 0, simulationTime);
const centerBend = bendAt(centerPhase);
beatFill.style.width = `${(centerBend / MAX_BEND) * 100}%`;
if (centerPhase < 0.45) {
beatState.textContent = "POWER STROKE";
} else if (centerPhase < 0.78) {
beatState.textContent = "CONTACT / HOLD";
} else {
beatState.textContent = "RECOVERY";
}
updateProjectedLabel();
renderer.render(scene, camera);
if (firstFrame) {
firstFrame = false;
loading.classList.add("hidden");
}
}
animate();
</script>
</body>
</html>
```