Prompt specification
Build or customize the Three.js 3D scene "Cilia coordination parameter exhibit" 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: Cilia coordination parameter exhibit
Category: Biology & Nature · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/cilia-coordination-parameter-exhibit-x0828
Scene Description Prompt:
Cilia coordination parameter exhibit. Provide controls for beat phase and active fraction in a toy surface transport model; compare particle displacement qualitatively and state that the flow is prescribed. Please build this with Three.js from cdnjs in one self-contained HTML file, using low-poly forms.
Working Three.js Starter (cilia-coordination-parameter-exhibit-x0828):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Cilia Coordination Parameter Exhibit</title>
<style>
:root {
color-scheme: dark;
--bg: #091316;
--panel: #102126;
--panel-2: #162b31;
--line: #294047;
--text: #edf7f5;
--muted: #9db2b4;
--teal: #3dd6c6;
--violet: #a594ff;
--orange: #ff805f;
--yellow: #ffd166;
}
* {
box-sizing: border-box;
}
body {
margin: 0;
min-height: 100vh;
background:
radial-gradient(circle at 15% 0%, #173138 0, transparent 36rem),
radial-gradient(circle at 90% 10%, #1b2735 0, transparent 32rem),
var(--bg);
color: var(--text);
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
button,
input {
font: inherit;
}
.shell {
width: min(1480px, calc(100% - 32px));
margin: 0 auto;
padding: 28px 0 36px;
}
header {
display: flex;
justify-content: space-between;
align-items: flex-start;
gap: 24px;
margin-bottom: 20px;
}
.eyebrow {
color: var(--teal);
font-size: 0.74rem;
font-weight: 800;
letter-spacing: 0.16em;
text-transform: uppercase;
margin-bottom: 8px;
}
h1 {
margin: 0;
max-width: 850px;
font-size: clamp(1.8rem, 4vw, 3.25rem);
line-height: 1.02;
letter-spacing: -0.045em;
}
.subtitle {
margin: 11px 0 0;
color: var(--muted);
max-width: 760px;
line-height: 1.55;
}
.flow-badge {
flex: 0 0 auto;
border: 1px solid #52706f;
background: rgba(61, 214, 198, 0.08);
color: #baf9f0;
border-radius: 999px;
padding: 10px 14px;
font-size: 0.72rem;
font-weight: 850;
letter-spacing: 0.12em;
white-space: nowrap;
}
.flow-badge::before {
content: "";
display: inline-block;
width: 8px;
height: 8px;
margin-right: 8px;
border-radius: 50%;
background: var(--teal);
box-shadow: 0 0 14px var(--teal);
}
.controls {
display: grid;
grid-template-columns: minmax(260px, 1fr) minmax(260px, 1fr) auto;
gap: 18px;
align-items: center;
padding: 17px;
margin-bottom: 16px;
border: 1px solid var(--line);
border-radius: 18px;
background: rgba(16, 33, 38, 0.88);
box-shadow: 0 18px 50px rgba(0, 0, 0, 0.18);
backdrop-filter: blur(14px);
}
.control-label {
display: flex;
align-items: baseline;
justify-content: space-between;
gap: 12px;
margin-bottom: 10px;
font-size: 0.86rem;
font-weight: 700;
}
output {
color: var(--teal);
font-variant-numeric: tabular-nums;
white-space: nowrap;
}
input[type="range"] {
width: 100%;
accent-color: var(--teal);
cursor: pointer;
}
.button-row {
display: flex;
gap: 8px;
}
button {
border: 1px solid #38545a;
border-radius: 10px;
padding: 10px 13px;
color: var(--text);
background: #193138;
cursor: pointer;
font-size: 0.82rem;
font-weight: 750;
}
button:hover {
background: #214149;
border-color: #52747a;
}
button.primary {
color: #08201d;
background: var(--teal);
border-color: var(--teal);
}
.exhibit {
display: grid;
grid-template-columns: minmax(0, 1fr) 330px;
gap: 16px;
align-items: stretch;
}
.stage-shell {
min-width: 0;
border: 1px solid var(--line);
border-radius: 20px;
overflow: hidden;
background: #dce8e5;
box-shadow: 0 24px 70px rgba(0, 0, 0, 0.27);
}
#stage {
position: relative;
width: 100%;
height: 590px;
isolation: isolate;
}
#stage canvas {
position: absolute;
inset: 0;
display: block;
width: 100%;
height: 100%;
touch-action: none;
cursor: grab;
}
#stage canvas:active {
cursor: grabbing;
}
.panel-label {
position: absolute;
z-index: 2;
max-width: calc(100% - 32px);
padding: 10px 12px;
border: 1px solid rgba(18, 52, 53, 0.15);
border-radius: 12px;
background: rgba(250, 255, 253, 0.84);
color: #173237;
box-shadow: 0 8px 24px rgba(20, 47, 47, 0.1);
pointer-events: none;
backdrop-filter: blur(8px);
}
.panel-label strong {
display: flex;
align-items: center;
gap: 8px;
font-size: 0.88rem;
}
.panel-label small {
display: block;
margin-top: 4px;
color: #527076;
font-size: 0.69rem;
line-height: 1.35;
}
.coord-chip {
display: inline-flex;
align-items: center;
border-radius: 999px;
padding: 3px 7px;
color: #0b4d47;
background: rgba(61, 214, 198, 0.23);
font-size: 0.67rem;
font-weight: 850;
}
.coord-chip.violet {
color: #44358a;
background: rgba(165, 148, 255, 0.23);
}
.camera-hint {
position: absolute;
z-index: 2;
left: 50%;
bottom: 12px;
transform: translateX(-50%);
padding: 7px 11px;
border-radius: 999px;
color: #28474b;
background: rgba(250, 255, 253, 0.8);
font-size: 0.68rem;
pointer-events: none;
white-space: nowrap;
}
.readout {
display: flex;
flex-direction: column;
gap: 14px;
padding: 18px;
border: 1px solid var(--line);
border-radius: 20px;
background: rgba(16, 33, 38, 0.9);
}
.readout h2 {
margin: 0;
font-size: 1.04rem;
letter-spacing: -0.02em;
}
.readout > p {
margin: 0;
color: var(--muted);
font-size: 0.77rem;
line-height: 1.5;
}
.metric {
padding: 12px;
border: 1px solid #253d43;
border-radius: 13px;
background: #102328;
}
.metric-title {
display: flex;
justify-content: space-between;
gap: 8px;
margin-bottom: 9px;
font-size: 0.76rem;
font-weight: 750;
}
.metric-title strong {
color: var(--orange);
font-variant-numeric: tabular-nums;
white-space: nowrap;
}
.track {
height: 7px;
overflow: hidden;
border-radius: 999px;
background: #263a40;
}
.track i {
display: block;
width: 0;
height: 100%;
border-radius: inherit;
background: var(--orange);
transition: width 120ms linear;
}
.metric.violet .track i {
background: var(--violet);
}
.metric-sub {
margin-top: 11px;
color: var(--muted);
font-size: 0.7rem;
}
.comparison {
padding: 12px;
border-left: 3px solid var(--teal);
border-radius: 4px 11px 11px 4px;
color: #c7dfdc;
background: rgba(61, 214, 198, 0.07);
font-size: 0.76rem;
line-height: 1.5;
}
.prescription {
margin-top: auto;
padding: 12px;
border: 1px solid #3a5658;
border-radius: 12px;
background: rgba(61, 214, 198, 0.06);
color: #c5d9d6;
font-size: 0.72rem;
line-height: 1.5;
}
.prescription strong {
color: var(--teal);
}
footer {
margin-top: 16px;
color: #81999c;
font-size: 0.71rem;
line-height: 1.55;
}
@media (max-width: 1050px) {
.exhibit {
grid-template-columns: 1fr;
}
.readout {
display: grid;
grid-template-columns: repeat(2, minmax(0, 1fr));
}
.readout h2,
.readout > p,
.comparison,
.prescription {
grid-column: 1 / -1;
}
.prescription {
margin-top: 0;
}
}
@media (max-width: 760px) {
.shell {
width: min(100% - 20px, 1480px);
padding-top: 18px;
}
header {
display: block;
}
.flow-badge {
display: inline-block;
margin-top: 15px;
}
.controls {
grid-template-columns: 1fr;
}
.button-row button {
flex: 1;
}
#stage {
height: 760px;
}
.readout {
display: flex;
}
}
</style>
</head>
<body>
<main class="shell">
<header>
<div>
<div class="eyebrow">Surface transport / coordination exhibit</div>
<h1>Cilia Coordination Parameter</h1>
<p class="subtitle">
Scrub the beat phase and choose the active ciliary population.
The phase-locked and phase-scrambled panels use the same surface,
particle population, and active fraction for a qualitative comparison.
</p>
</div>
<div class="flow-badge">PRESCRIBED FLOW</div>
</header>
<section class="controls" aria-label="Model controls">
<div>
<label class="control-label" for="phase">
<span>Beat phase offset</span>
<output id="phaseOutput" for="phase">35°</output>
</label>
<input id="phase" type="range" min="0" max="360" step="1" value="35">
</div>
<div>
<label class="control-label" for="fraction">
<span>Active fraction</span>
<output id="fractionOutput" for="fraction">65%</output>
</label>
<input id="fraction" type="range" min="0" max="100" step="1" value="65">
</div>
<div class="button-row">
<button id="pause" class="primary" type="button">Pause</button>
<button id="reset" type="button">Reset</button>
</div>
</section>
<section class="exhibit">
<div class="stage-shell">
<div id="stage" aria-label="Interactive three-dimensional cilia transport comparison">
<div id="coordLabel" class="panel-label">
<strong>
Phase-locked
<span class="coord-chip">C = 1.00</span>
</strong>
<small>
Active cilia share one beat phase.
<span id="coordLivePhase"></span>
</small>
</div>
<div id="scatterLabel" class="panel-label">
<strong>
Phase-scrambled
<span class="coord-chip violet">C ≈ 0.18</span>
</strong>
<small>
Active cilia retain fixed spatial phase offsets.
</small>
</div>
<div class="camera-hint">Drag to orbit · wheel/pinch to zoom · double-click to reset view</div>
</div>
</div>
<aside class="readout">
<h2>Particle displacement</h2>
<p>
Orange trails show recent motion. Both metrics use the same
illustrative model-unit scale.
</p>
<div class="metric">
<div class="metric-title">
<span>Locked · mean path displacement</span>
<strong id="coordPathValue">0.00 u</strong>
</div>
<div class="track"><i id="coordPathBar"></i></div>
</div>
<div class="metric violet">
<div class="metric-title">
<span>Scrambled · mean path displacement</span>
<strong id="scatterPathValue">0.00 u</strong>
</div>
<div class="track"><i id="scatterPathBar"></i></div>
</div>
<div class="metric">
<div class="metric-title">
<span>Locked · downstream drift</span>
<strong id="coordNetValue">0.00 u</strong>
</div>
<div class="track"><i id="coordNetBar"></i></div>
</div>
<div class="metric violet">
<div class="metric-title">
<span>Scrambled · downstream drift</span>
<strong id="scatterNetValue">0.00 u</strong>
</div>
<div class="track"><i id="scatterNetBar"></i></div>
</div>
<div id="comparison" class="comparison">
Phase-locked strokes reinforce into more coherent downstream transport.
</div>
<div class="prescription">
<strong>Flow is prescribed.</strong>
Particle velocities are sampled from an illustrative kinematic field
formed from local ciliary strokes. Particles do not feed back on the
flow, and no fluid momentum or Navier–Stokes equations are solved.
</div>
</aside>
</section>
<footer>
Toy model only. “C” denotes the conceptual coordination index, not a measured
biological quantity. Changing a control resets the particle comparison so
that both panels begin from identical initial conditions.
</footer>
</main>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const stage = document.getElementById("stage");
const phaseInput = document.getElementById("phase");
const fractionInput = document.getElementById("fraction");
const phaseOutput = document.getElementById("phaseOutput");
const fractionOutput = document.getElementById("fractionOutput");
const pauseButton = document.getElementById("pause");
const resetButton = document.getElementById("reset");
const coordLabel = document.getElementById("coordLabel");
const scatterLabel = document.getElementById("scatterLabel");
const coordLivePhase = document.getElementById("coordLivePhase");
const coordPathValue = document.getElementById("coordPathValue");
const scatterPathValue = document.getElementById("scatterPathValue");
const coordNetValue = document.getElementById("coordNetValue");
const scatterNetValue = document.getElementById("scatterNetValue");
const coordPathBar = document.getElementById("coordPathBar");
const scatterPathBar = document.getElementById("scatterPathBar");
const coordNetBar = document.getElementById("coordNetBar");
const scatterNetBar = document.getElementById("scatterNetBar");
const comparison = document.getElementById("comparison");
const CILIA_PER_ROW = 9;
const CILIA_COUNT = CILIA_PER_ROW * 2;
const PARTICLE_COUNT = 40;
const BEAT_SPEED = (Math.PI * 2) / 3.15;
const PARTICLE_RADIUS = 0.085;
let phaseOffset = THREE.MathUtils.degToRad(35);
let activeFraction = 0.65;
let activeSet = new Set();
let playing = true;
let simulationTime = 0;
let lastFrame = performance.now();
let statsTimer = 0;
let trailTimer = 0;
const renderer = new THREE.WebGLRenderer({
antialias: true,
alpha: false,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio || 1, 2));
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.08;
stage.prepend(renderer.domElement);
const shared = {
particleGeometry: new THREE.IcosahedronGeometry(PARTICLE_RADIUS, 0),
ciliumGeometry: new THREE.CylinderGeometry(0.035, 0.052, 0.72, 5, 1, false),
surfaceGeometry: new THREE.BoxGeometry(6.4, 0.26, 3.8)
};
shared.ciliumGeometry.translate(0, 0.36, 0);
const panels = [];
const cameraState = {
yaw: 0.57,
pitch: 0.76,
distance: 10.5,
target: new THREE.Vector3(0, 0.05, 0)
};
function hash01(n) {
const x = Math.sin(n * 127.1 + 311.7) * 43758.5453123;
return x - Math.floor(x);
}
function mulberry32(seed) {
return function random() {
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;
};
}
function clamp(value, min, max) {
return Math.max(min, Math.min(max, value));
}
function updateActiveSet() {
const order = Array.from(
{ length: CILIA_COUNT },
(_, i) => i
).sort((a, b) => hash01(a) - hash01(b));
const count = Math.round(activeFraction * CILIA_COUNT);
activeSet = new Set(order.slice(0, count));
for (const panel of panels) {
for (const cilium of panel.cilia) {
cilium.active = activeSet.has(cilium.index);
cilium.material.color.setHex(
cilium.active ? panel.activeColor : 0x91a7a4
);
}
}
}
function makeGrid() {
const positions = [];
for (let x = -3.2; x <= 3.201; x += 0.8) {
positions.push(x, 0.136, -1.9, x, 0.136, 1.9);
}
for (let z = -1.6; z <= 1.601; z += 0.8) {
positions.push(-3.2, 0.136, z, 3.2, 0.136, z);
}
const geometry = new THREE.BufferGeometry();
geometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(positions, 3)
);
const material = new THREE.LineBasicMaterial({
color: 0xb7cac6,
transparent: true,
opacity: 0.52
});
return new THREE.LineSegments(geometry, material);
}
function createPanel(coordinated) {
const background = coordinated ? 0xdfece8 : 0xe9e8e3;
const activeColor = coordinated ? 0x18a99e : 0x8270dc;
const scene = new THREE.Scene();
scene.background = new THREE.Color(background);
scene.fog = new THREE.Fog(background, 13, 27);
const camera = new THREE.PerspectiveCamera(39, 1, 0.1, 60);
scene.add(new THREE.HemisphereLight(0xffffff, 0x54706c, 2.0));
const sun = new THREE.DirectionalLight(0xffffff, 2.6);
sun.position.set(4.5, 8.5, 5.5);
sun.castShadow = true;
sun.shadow.mapSize.set(1024, 1024);
sun.shadow.camera.left = -7;
sun.shadow.camera.right = 7;
sun.shadow.camera.top = 7;
sun.shadow.camera.bottom = -7;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 22;
sun.shadow.bias = -0.0006;
scene.add(sun);
const floor = new THREE.Mesh(
new THREE.PlaneGeometry(30, 30),
new THREE.MeshStandardMaterial({
color: background,
roughness: 1,
metalness: 0
})
);
floor.rotation.x = -Math.PI / 2;
floor.position.y = -0.16;
floor.receiveShadow = true;
scene.add(floor);
const surface = new THREE.Mesh(
shared.surfaceGeometry,
new THREE.MeshStandardMaterial({
color: 0xf5faf8,
roughness: 0.86,
metalness: 0.02
})
);
surface.receiveShadow = true;
surface.castShadow = true;
scene.add(surface);
const edges = new THREE.LineSegments(
new THREE.EdgesGeometry(shared.surfaceGeometry),
new THREE.LineBasicMaterial({
color: 0x8caaa5,
transparent: true,
opacity: 0.68
})
);
scene.add(edges);
scene.add(makeGrid());
const downstream = new THREE.ArrowHelper(
new THREE.Vector3(1, 0, 0),
new THREE.Vector3(-1.9, 0.15, 0),
1.45,
coordinated ? 0x168f88 : 0x7564c8,
0.24,
0.15
);
downstream.line.material.transparent = true;
downstream.line.material.opacity = 0.55;
downstream.cone.material.transparent = true;
downstream.cone.material.opacity = 0.65;
scene.add(downstream);
const cilia = [];
let ciliumIndex = 0;
for (let row = 0; row < 2; row++) {
for (let i = 0; i < CILIA_PER_ROW; i++) {
const x = -2.7 + i * 0.675;
const z = row === 0 ? -1.47 : 1.47;
const material = new THREE.MeshStandardMaterial({
color: activeColor,
roughness: 0.72,
metalness: 0.03,
transparent: true,
opacity: 0.95
});
const mesh = new THREE.Mesh(shared.ciliumGeometry, material);
mesh.position.set(x, 0.14, z);
mesh.castShadow = true;
scene.add(mesh);
const phaseOffsetForCilium = coordinated
? 0
: (hash01(ciliumIndex + row * 41) * 2 - 1) * Math.PI * 0.88;
cilia.push({
index: ciliumIndex,
row,
x,
z,
mesh,
material,
active: true,
phaseOffset: phaseOffsetForCilium
});
ciliumIndex++;
}
}
const random = mulberry32(202503);
const particles = [];
const trails = [];
const trailLength = 24;
for (let i = 0; i < PARTICLE_COUNT; i++) {
const start = new THREE.Vector3(
-2.7 + random() * 4.5,
0.255,
-1.12 + random() * 2.24
);
const color = i % 3 === 0 ? 0xffc857 : 0xff7657;
const material = new THREE.MeshStandardMaterial({
color,
roughness: 0.62,
metalness: 0.04,
flatShading: true
});
const mesh = new THREE.Mesh(shared.particleGeometry, material);
mesh.position.copy(start);
mesh.castShadow = true;
scene.add(mesh);
particles.push({
index: i,
mesh,
start: start.clone(),
pathLength: 0
});
if (i % 4 === 0) {
const positions = new Float32Array(trailLength * 3);
const geometry = new THREE.BufferGeometry();
geometry.setAttribute(
"position",
new THREE.BufferAttribute(positions, 3).setUsage(THREE.DynamicDrawUsage)
);
geometry.setDrawRange(0, 0);
const line = new THREE.Line(
geometry,
new THREE.LineBasicMaterial({
color: 0xe9684d,
transparent: true,
opacity: 0.45
})
);
line.frustumCulled = false;
scene.add(line);
trails.push({
particleIndex: i,
positions,
count: 0,
line
});
}
}
return {
coordinated,
scene,
camera,
cilia,
particles,
trails,
trailLength,
activeColor,
stats: {
meanPath: 0,
meanNet: 0,
meanDisplacement: 0
}
};
}
panels.push(createPanel(true));
panels.push(createPanel(false));
updateActiveSet();
function currentBeatPhase() {
return phaseOffset + simulationTime * BEAT_SPEED;
}
/*
Prescribed kinematic flow model.
This is not a fluid simulation. Each active cilium contributes a local,
one-sided stroke to a sum field. The phase-locked panel includes a larger
coherent downstream pressure bias. Particles only sample this field;
they do not modify it.
*/
function sampleFlow(panel, x, z, phase) {
let vx = 0;
let vz = 0;
for (const cilium of panel.cilia) {
if (!cilium.active) continue;
const dx = x - cilium.x;
const dz = z - cilium.z;
const sigma = 0.78 + cilium.row * 0.06;
const gaussian = Math.exp(
-(dx * dx + dz * dz) / (2 * sigma * sigma)
);
const localPhase = phase + cilium.phaseOffset;
const sine = Math.sin(localPhase);
const effectiveStroke = Math.max(0, sine);
const recovery = Math.max(0, -sine);
const downstreamStroke = effectiveStroke * 0.55 - recovery * 0.06;
const crossStroke = effectiveStroke * 0.28 - recovery * 0.05;
const inward = cilium.z < 0 ? 1 : -1;
vx += gaussian * downstreamStroke;
vz += gaussian * crossStroke * inward;
}
vx *= 0.42;
vz *= 0.38;
const activeCoverage = activeSet.size / CILIA_COUNT;
const coherentPulse = Math.pow(
Math.max(0, Math.sin(phase)),
1.4
);
// Prescribed coherence-dependent downstream bias.
const coherentBias = panel.coordinated ? 0.14 : 0.028;
vx += activeCoverage * coherentBias * coherentPulse;
// Small deterministic transverse variation, also scaled by activity.
vz += activeCoverage * 0.014 *
Math.sin(phase * 1.3 + z * 1.7 + x * 0.4);
return { vx, vz };
}
function resetTrails(panel) {
for (const trail of panel.trails) {
trail.count = 0;
trail.positions.fill(0);
trail.line.geometry.attributes.position.needsUpdate = true;
trail.line.geometry.setDrawRange(0, 0);
}
}
function resetSimulation() {
simulationTime = 0;
statsTimer = 0;
trailTimer = 0;
for (const panel of panels) {
for (const particle of panel.particles) {
particle.mesh.position.copy(particle.start);
particle.pathLength = 0;
}
resetTrails(panel);
}
updateStats();
}
function updateParticles(dt) {
const phase = currentBeatPhase();
for (const panel of panels) {
for (const particle of panel.particles) {
const position = particle.mesh.position;
const velocity = sampleFlow(
panel,
position.x,
position.z,
phase
);
const oldX = position.x;
const oldZ = position.z;
position.x = clamp(position.x + velocity.vx * dt, -2.96, 2.96);
position.z = clamp(position.z + velocity.vz * dt, -1.58, 1.58);
const dx = position.x - oldX;
const dz = position.z - oldZ;
particle.pathLength += Math.hypot(dx, dz);
}
}
trailTimer += dt;
if (trailTimer >= 0.12) {
trailTimer = 0;
updateTrails();
}
}
function updateTrails() {
for (const panel of panels) {
for (const trail of panel.trails) {
const particle = panel.particles[trail.particleIndex];
const p = particle.mesh.position;
if (trail.count === panel.trailLength) {
trail.positions.copyWithin(0, 3);
trail.count--;
}
const index = trail.count * 3;
trail.positions[index] = p.x;
trail.positions[index + 1] = p.y;
trail.positions[index + 2] = p.z;
trail.count++;
trail.line.geometry.attributes.position.needsUpdate = true;
trail.line.geometry.setDrawRange(0, trail.count);
}
}
}
function updateCilia() {
const phase = currentBeatPhase();
for (const panel of panels) {
for (const cilium of panel.cilia) {
const localPhase = phase + cilium.phaseOffset;
const stroke = Math.pow(
Math.max(0, Math.sin(localPhase)),
1.25
);
const tilt = cilium.active
? 0.10 + stroke * 0.64
: 0.08;
// Cilia on opposite edges bend toward the center.
cilium.mesh.rotation.x =
(cilium.z < 0 ? 1 : -1) * tilt;
// A small downstream component along +X.
cilium.mesh.rotation.z = cilium.active
? -0.12 * stroke
: -0.01;
cilium.material.opacity = cilium.active ? 0.95 : 0.25;
}
}
}
function computePanelStats(panel) {
let path = 0;
let net = 0;
let displacement = 0;
for (const particle of panel.particles) {
const p = particle.mesh.position;
path += particle.pathLength;
net += p.x - particle.start.x;
displacement += p.distanceTo(particle.start);
}
const count = Math.max(1, panel.particles.length);
panel.stats.meanPath = path / count;
panel.stats.meanNet = net / count;
panel.stats.meanDisplacement = displacement / count;
}
function setBar(element, value, maximum) {
const percent = maximum > 0
? clamp((value / maximum) * 100, 0, 100)
: 0;
element.style.width = `${percent}%`;
}
function updateStats() {
computePanelStats(panels[0]);
computePanelStats(panels[1]);
const locked = panels[0].stats;
const scrambled = panels[1].stats;
coordPathValue.textContent = `${locked.meanPath.toFixed(2)} u`;
scatterPathValue.textContent = `${scrambled.meanPath.toFixed(2)} u`;
coordNetValue.textContent = `${locked.meanNet.toFixed(2)} u`;
scatterNetValue.textContent = `${scrambled.meanNet.toFixed(2)} u`;
const pathMaximum = Math.max(
locked.meanPath,
scrambled.meanPath,
0.001
);
const netMaximum = Math.max(
Math.abs(locked.meanNet),
Math.abs(scrambled.meanNet),
0.001
);
setBar(coordPathBar, locked.meanPath, pathMaximum);
setBar(scatterPathBar, scrambled.meanPath, pathMaximum);
setBar(coordNetBar, Math.abs(locked.meanNet), netMaximum);
setBar(scatterNetBar, Math.abs(scrambled.meanNet), netMaximum);
if (activeSet.size === 0) {
comparison.textContent =
"At 0% active fraction, no ciliary strokes contribute and both particle fields remain still.";
} else if (locked.meanPath > scrambled.meanPath * 1.08) {
comparison.textContent =
"At this setting, phase-locked beats produce the larger particle path displacement and a clearer downstream drift. Scrambled strokes are spatially fragmented.";
} else if (scrambled.meanPath > locked.meanPath * 1.08) {
comparison.textContent =
"At this instant, the scrambled field has accumulated slightly more path displacement. This is an oscillatory toy comparison, not a general performance guarantee.";
} else {
comparison.textContent =
"The two displacement measures are currently similar. Locked beats remain more spatially coherent, while scrambled beats distribute their motion over time.";
}
}
function updateCamera(camera) {
const { yaw, pitch, distance, target } = cameraState;
const horizontal = Math.sin(pitch) * distance;
camera.position.set(
target.x + horizontal * Math.sin(yaw),
target.y + Math.cos(pitch) * distance,
target.z + horizontal * Math.cos(yaw)
);
camera.lookAt(target);
}
function resetCamera() {
cameraState.yaw = 0.57;
cameraState.pitch = 0.76;
cameraState.distance = 10.5;
}
// Direct orbit / zoom controls; no Three.js examples or addons are used.
const pointers = new Map();
let singlePointerX = 0;
let singlePointerY = 0;
let pinchDistance = 0;
renderer.domElement.addEventListener("pointerdown", event => {
renderer.domElement.setPointerCapture(event.pointerId);
pointers.set(event.pointerId, {
x: event.clientX,
y: event.clientY
});
if (pointers.size === 1) {
singlePointerX = event.clientX;
singlePointerY = event.clientY;
} else if (pointers.size === 2) {
pinchDistance = getPinchDistance();
}
});
renderer.domElement.addEventListener("pointermove", event => {
if (!pointers.has(event.pointerId)) return;
pointers.set(event.pointerId, {
x: event.clientX,
y: event.clientY
});
if (pointers.size === 1) {
const dx = event.clientX - singlePointerX;
const dy = event.clientY - singlePointerY;
cameraState.yaw -= dx * 0.006;
cameraState.pitch = clamp(
cameraState.pitch + dy * 0.005,
0.34,
1.36
);
singlePointerX = event.clientX;
singlePointerY = event.clientY;
} else if (pointers.size === 2) {
const distance = getPinchDistance();
if (pinchDistance > 0 && distance > 0) {
cameraState.distance = clamp(
cameraState.distance * (pinchDistance / distance),
6.5,
17
);
}
pinchDistance = distance;
}
});
function endPointer(event) {
pointers.delete(event.pointerId);
if (pointers.size === 1) {
const remaining = [...pointers.values()][0];
singlePointerX = remaining.x;
singlePointerY = remaining.y;
} else {
pinchDistance = 0;
}
}
renderer.domElement.addEventListener("pointerup", endPointer);
renderer.domElement.addEventListener("pointercancel", endPointer);
renderer.domElement.addEventListener("pointerleave", event => {
if (!event.buttons) endPointer(event);
});
function getPinchDistance() {
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
);
}
renderer.domElement.addEventListener(
"wheel",
event => {
event.preventDefault();
cameraState.distance = clamp(
cameraState.distance * Math.exp(event.deltaY * 0.001),
6.5,
17
);
},
{ passive: false }
);
renderer.domElement.addEventListener("dblclick", resetCamera);
let stageWidth = 1;
let stageHeight = 1;
let rects = [];
function resize() {
stageWidth = Math.max(1, stage.clientWidth);
stageHeight = Math.max(1, stage.clientHeight);
renderer.setSize(stageWidth, stageHeight, false);
if (stageWidth < 760) {
const half = Math.floor(stageHeight / 2);
rects = [
// Top half in screen coordinates is the upper WebGL scissor.
{ x: 0, y: half, width: stageWidth, height: stageHeight - half },
{ x: 0, y: 0, width: stageWidth, height: half }
];
coordLabel.style.left = "14px";
coordLabel.style.top = "14px";
scatterLabel.style.left = "14px";
scatterLabel.style.top = `${half + 14}px`;
} else {
const half = Math.floor(stageWidth / 2);
rects = [
{ x: 0, y: 0, width: half, height: stageHeight },
{ x: half, y: 0, width: stageWidth - half, height: stageHeight }
];
coordLabel.style.left = "14px";
coordLabel.style.top = "14px";
scatterLabel.style.left = `${half + 14}px`;
scatterLabel.style.top = "14px";
}
}
new ResizeObserver(resize).observe(stage);
resize();
phaseInput.addEventListener("input", () => {
phaseOffset = THREE.MathUtils.degToRad(Number(phaseInput.value));
phaseOutput.textContent = `${phaseInput.value}°`;
resetSimulation();
});
fractionInput.addEventListener("input", () => {
activeFraction = Number(fractionInput.value) / 100;
updateActiveSet();
const count = activeSet.size;
fractionOutput.textContent =
`${fractionInput.value}% · ${count}/${CILIA_COUNT}`;
resetSimulation();
});
pauseButton.addEventListener("click", () => {
playing = !playing;
pauseButton.textContent = playing ? "Pause" : "Play";
pauseButton.setAttribute("aria-pressed", String(!playing));
});
resetButton.addEventListener("click", resetSimulation);
window.addEventListener("keydown", event => {
if (event.code !== "Space") return;
if (["INPUT", "BUTTON"].includes(document.activeElement.tagName)) return;
event.preventDefault();
pauseButton.click();
});
function render() {
renderer.setScissorTest(true);
for (let i = 0; i < panels.length; i++) {
const panel = panels[i];
const rect = rects[i];
panel.camera.aspect = rect.width / rect.height;
panel.camera.updateProjectionMatrix();
updateCamera(panel.camera);
renderer.setViewport(rect.x, rect.y, rect.width, rect.height);
renderer.setScissor(rect.x, rect.y, rect.width, rect.height);
renderer.render(panel.scene, panel.camera);
}
renderer.setScissorTest(false);
}
function animate(now) {
requestAnimationFrame(animate);
const dt = Math.min((now - lastFrame) / 1000, 0.04);
lastFrame = now;
if (playing) {
simulationTime += dt;
updateParticles(dt);
}
updateCilia();
const liveDegrees =
Math.round(THREE.MathUtils.radToDeg(currentBeatPhase()) % 360);
coordLivePhase.textContent = `Live carrier phase: ${liveDegrees}°.`;
statsTimer += dt;
if (statsTimer >= 0.1) {
statsTimer = 0;
updateStats();
}
render();
}
fractionOutput.textContent =
`${fractionInput.value}% · ${activeSet.size}/${CILIA_COUNT}`;
updateStats();
requestAnimationFrame(animate);
</script>
</body>
</html>
```