Prompt specification
Build or customize the Three.js 3D scene "Closing gap in a cell sheet" 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: Closing gap in a cell sheet
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/closing-gap-in-a-cell-sheet-x0819
Scene Description Prompt:
Build a self-contained HTML scene with Three.js from cdnjs featuring cell migration. Compose two edges of a schematic epithelial sheet around a small gap; animate collective migration with visible neighbors and label the scene an illustrative tissue process. Voxel style.
Working Three.js Starter (closing-gap-in-a-cell-sheet-x0819):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Collective Epithelial Migration</title>
<style>
:root {
--ink: #eef8f5;
--muted: #9eb7b6;
--teal: #58d8c3;
--amber: #ffb45c;
--panel: rgba(8, 24, 31, 0.78);
--line: rgba(174, 221, 216, 0.17);
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background:
radial-gradient(circle at 50% 43%, #173640 0%, #0a1d25 42%, #061117 100%);
color: var(--ink);
font-family:
Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
#scene {
position: fixed;
inset: 0;
width: 100%;
height: 100%;
display: block;
cursor: grab;
touch-action: none;
outline: none;
}
#scene.grabbing {
cursor: grabbing;
}
.vignette {
position: fixed;
inset: 0;
pointer-events: none;
background:
linear-gradient(180deg, rgba(3, 12, 17, 0.32), transparent 30%),
radial-gradient(circle at center, transparent 48%, rgba(2, 9, 14, 0.48));
}
.overlay {
position: fixed;
inset: 0;
pointer-events: none;
}
.copy {
position: absolute;
top: clamp(22px, 4vw, 52px);
left: clamp(22px, 4vw, 58px);
width: min(540px, calc(100vw - 44px));
text-shadow: 0 2px 22px rgba(0, 0, 0, 0.35);
}
.eyebrow {
display: inline-flex;
align-items: center;
gap: 9px;
padding: 7px 10px;
border: 1px solid rgba(255, 180, 92, 0.38);
border-radius: 4px;
color: #ffd19c;
background: rgba(44, 29, 17, 0.48);
font-size: 11px;
font-weight: 800;
letter-spacing: 0.16em;
text-transform: uppercase;
}
.eyebrow::before {
width: 6px;
height: 6px;
content: "";
background: var(--amber);
box-shadow: 0 0 12px rgba(255, 180, 92, 0.75);
}
h1 {
max-width: 540px;
margin: 17px 0 10px;
font-size: clamp(32px, 4.5vw, 60px);
font-weight: 760;
line-height: 0.98;
letter-spacing: -0.055em;
}
.copy p {
max-width: 475px;
margin: 0;
color: #b7ccca;
font-size: clamp(13px, 1.2vw, 16px);
line-height: 1.55;
}
.copy strong {
color: #e8f4f1;
font-weight: 650;
}
.panel {
border: 1px solid var(--line);
border-radius: 10px;
background: var(--panel);
box-shadow: 0 18px 50px rgba(0, 0, 0, 0.22);
backdrop-filter: blur(14px);
}
.process-card {
position: absolute;
top: clamp(22px, 4vw, 48px);
right: clamp(22px, 3vw, 42px);
width: min(330px, calc(100vw - 44px));
padding: 19px;
}
.process-card .micro {
color: var(--muted);
font-size: 10px;
font-weight: 750;
letter-spacing: 0.13em;
text-transform: uppercase;
}
.stage {
display: grid;
grid-template-columns: 42px 1fr;
gap: 12px;
margin-top: 15px;
align-items: start;
}
.stage-number {
color: var(--teal);
font-size: 31px;
font-weight: 760;
line-height: 1;
letter-spacing: -0.06em;
}
.stage-title {
display: block;
margin-bottom: 5px;
color: #edf8f6;
font-size: 14px;
font-weight: 720;
}
.stage-copy {
display: block;
color: #91aaa9;
font-size: 12px;
line-height: 1.45;
}
.steps {
display: grid;
grid-template-columns: repeat(4, 1fr);
gap: 5px;
margin-top: 18px;
}
.steps i {
height: 3px;
border-radius: 3px;
background: rgba(145, 185, 182, 0.16);
transition: background 180ms ease;
}
.steps i.active {
background: var(--teal);
box-shadow: 0 0 9px rgba(88, 216, 195, 0.28);
}
.legend {
position: absolute;
right: clamp(22px, 3vw, 42px);
bottom: 112px;
width: 222px;
padding: 15px 17px;
}
.legend-title {
margin-bottom: 11px;
color: #d6e6e3;
font-size: 10px;
font-weight: 800;
letter-spacing: 0.13em;
text-transform: uppercase;
}
.legend-row {
display: flex;
align-items: center;
gap: 10px;
margin-top: 9px;
color: #a9bfbd;
font-size: 11px;
}
.swatch {
width: 13px;
height: 13px;
flex: 0 0 auto;
border: 1px solid rgba(255, 255, 255, 0.18);
}
.swatch.cell {
background: #3bb4a8;
box-shadow: inset -3px -3px 0 rgba(0, 0, 0, 0.13);
}
.swatch.front {
background: var(--amber);
}
.swatch.substrate {
background: #15343d;
border-color: rgba(107, 184, 190, 0.24);
}
.controls {
position: absolute;
left: 50%;
bottom: 24px;
display: grid;
grid-template-columns: auto auto minmax(220px, 1fr);
gap: 13px;
align-items: center;
width: min(790px, calc(100vw - 34px));
padding: 12px;
transform: translateX(-50%);
pointer-events: auto;
}
button {
min-height: 39px;
border: 1px solid rgba(161, 210, 204, 0.19);
border-radius: 6px;
color: #dcebe8;
background: rgba(28, 61, 67, 0.72);
font: inherit;
font-size: 12px;
font-weight: 700;
cursor: pointer;
transition:
background 150ms ease,
border-color 150ms ease,
transform 150ms ease;
}
button:hover {
border-color: rgba(88, 216, 195, 0.55);
background: rgba(38, 83, 85, 0.85);
}
button:active {
transform: translateY(1px);
}
#play {
min-width: 84px;
color: #092124;
border-color: transparent;
background: var(--teal);
}
#play:hover {
background: #76ead6;
}
#replay {
padding: 0 14px;
}
.timeline {
min-width: 0;
padding: 0 5px;
}
.timeline-top {
display: flex;
justify-content: space-between;
gap: 10px;
margin-bottom: 7px;
color: #91aaa9;
font-size: 10px;
font-weight: 650;
letter-spacing: 0.06em;
text-transform: uppercase;
}
#time {
color: #bcd0cd;
font-variant-numeric: tabular-nums;
}
input[type="range"] {
width: 100%;
height: 4px;
margin: 0;
border-radius: 10px;
appearance: none;
background:
linear-gradient(
90deg,
var(--teal) 0%,
var(--teal) var(--progress, 0%),
rgba(140, 184, 181, 0.2) var(--progress, 0%),
rgba(140, 184, 181, 0.2) 100%
);
cursor: pointer;
}
input[type="range"]::-webkit-slider-thumb {
width: 14px;
height: 14px;
border: 3px solid #0b242b;
border-radius: 50%;
appearance: none;
background: #78ead6;
box-shadow: 0 0 0 1px rgba(120, 234, 214, 0.6);
}
input[type="range"]::-moz-range-thumb {
width: 10px;
height: 10px;
border: 3px solid #0b242b;
border-radius: 50%;
background: #78ead6;
box-shadow: 0 0 0 1px rgba(120, 234, 214, 0.6);
}
.hint {
position: absolute;
left: clamp(22px, 4vw, 58px);
bottom: 28px;
color: rgba(186, 209, 206, 0.66);
font-size: 10px;
font-weight: 650;
letter-spacing: 0.055em;
text-transform: uppercase;
}
.world-label {
position: absolute;
top: 0;
left: 0;
padding: 7px 9px;
border: 1px solid rgba(165, 214, 208, 0.19);
border-radius: 4px;
color: #b9cecb;
background: rgba(7, 22, 29, 0.74);
box-shadow: 0 8px 20px rgba(0, 0, 0, 0.18);
font-size: 9px;
font-weight: 800;
letter-spacing: 0.11em;
text-transform: uppercase;
white-space: nowrap;
will-change: transform;
}
.world-label.gap {
border-color: rgba(255, 180, 92, 0.32);
color: #ffd19c;
text-align: center;
}
.world-label.gap strong {
display: block;
margin-top: 3px;
color: #fff1db;
font-size: 11px;
letter-spacing: 0.04em;
}
.world-label.edge {
color: #9fe4d8;
}
@media (max-width: 860px) {
.copy {
top: 20px;
}
.copy p {
max-width: 360px;
}
.process-card {
top: auto;
right: 16px;
bottom: 101px;
width: 285px;
}
.legend {
display: none;
}
.hint {
display: none;
}
}
@media (max-width: 620px) {
h1 {
max-width: 330px;
font-size: 34px;
}
.copy p {
max-width: 330px;
font-size: 12px;
}
.process-card {
display: none;
}
.controls {
bottom: 13px;
grid-template-columns: auto auto 1fr;
gap: 7px;
padding: 9px;
}
#play {
min-width: 67px;
}
#replay {
padding: 0 10px;
}
.world-label.edge {
display: none;
}
}
</style>
</head>
<body>
<canvas id="scene" aria-label="Interactive voxel schematic of collective epithelial migration"></canvas>
<div class="vignette"></div>
<div class="overlay">
<section class="copy">
<div class="eyebrow">Illustrative tissue process</div>
<h1>Collective cell migration</h1>
<p>
Two epithelial sheet edges advance into a small gap.
<strong>Neighbor-linked rows</strong> move together, gradually closing the space.
</p>
</section>
<aside class="process-card panel" aria-live="polite">
<div class="micro">Process state</div>
<div class="stage">
<div class="stage-number" id="stage-number">01</div>
<div>
<strong class="stage-title" id="stage-title">Two edges flank a gap</strong>
<span class="stage-copy" id="stage-copy">
The epithelial fronts remain separated by a narrow space.
</span>
</div>
</div>
<div class="steps" aria-hidden="true">
<i class="active"></i>
<i></i>
<i></i>
<i></i>
</div>
</aside>
<aside class="legend panel">
<div class="legend-title">Schematic key</div>
<div class="legend-row">
<span class="swatch cell"></span>
<span>Epithelial voxel</span>
</div>
<div class="legend-row">
<span class="swatch front"></span>
<span>Leading-edge extension</span>
</div>
<div class="legend-row">
<span class="swatch substrate"></span>
<span>Basal substrate</span>
</div>
</aside>
<div class="controls panel">
<button id="play" type="button" aria-label="Pause animation">Pause</button>
<button id="replay" type="button" aria-label="Replay animation">Replay</button>
<div class="timeline">
<div class="timeline-top">
<span>Migration timeline</span>
<span id="time">0.0 / 13.0 s</span>
</div>
<input
id="scrubber"
type="range"
min="0"
max="1000"
step="1"
value="0"
aria-label="Migration animation timeline"
>
</div>
</div>
<div class="hint">Drag to orbit · Shift-drag to pan · Scroll to zoom</div>
<div id="label-gap" class="world-label gap">
Migration gap
<strong id="gap-value">1.64 u</strong>
</div>
<div id="label-left" class="world-label edge">Epithelial edge A</div>
<div id="label-right" class="world-label edge">Epithelial edge B</div>
</div>
<script type="module">
import * as THREE from "https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// -------------------------------------------------------------------------
// Renderer, scene, camera
// -------------------------------------------------------------------------
const canvas = document.querySelector("#scene");
const renderer = new THREE.WebGLRenderer({
canvas,
antialias: true,
alpha: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight, false);
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.13;
renderer.setClearColor(0x07151d, 0);
const scene = new THREE.Scene();
scene.fog = new THREE.Fog(0x081820, 21, 39);
const camera = new THREE.OrthographicCamera(-8, 8, 5, -5, 0.1, 100);
const orbit = {
theta: 0.62,
phi: 1.02,
userZoom: 1,
target: new THREE.Vector3(0, 0.05, 0)
};
const VIEW_HEIGHT = 9.2;
const desiredWorldWidth = 15.3;
let fitZoom = 1;
function updateCamera() {
const radius = 18;
const sinPhi = Math.sin(orbit.phi);
camera.position.set(
orbit.target.x + radius * sinPhi * Math.sin(orbit.theta),
orbit.target.y + radius * Math.cos(orbit.phi),
orbit.target.z + radius * sinPhi * Math.cos(orbit.theta)
);
camera.lookAt(orbit.target);
camera.zoom = fitZoom * orbit.userZoom;
camera.updateProjectionMatrix();
}
function resize() {
const width = window.innerWidth;
const height = window.innerHeight;
const aspect = width / height;
renderer.setSize(width, height, false);
camera.left = (-VIEW_HEIGHT * aspect) / 2;
camera.right = (VIEW_HEIGHT * aspect) / 2;
camera.top = VIEW_HEIGHT / 2;
camera.bottom = -VIEW_HEIGHT / 2;
fitZoom = Math.min(
1,
(VIEW_HEIGHT * aspect) / desiredWorldWidth
);
updateCamera();
}
window.addEventListener("resize", resize);
resize();
// -------------------------------------------------------------------------
// Direct camera controls: orbit, pan, and zoom
// -------------------------------------------------------------------------
let dragging = false;
let panning = false;
let pointerX = 0;
let pointerY = 0;
canvas.addEventListener("pointerdown", (event) => {
dragging = true;
panning = event.shiftKey || event.button === 2;
pointerX = event.clientX;
pointerY = event.clientY;
canvas.setPointerCapture(event.pointerId);
canvas.classList.add("grabbing");
});
canvas.addEventListener("pointermove", (event) => {
if (!dragging) return;
const dx = event.clientX - pointerX;
const dy = event.clientY - pointerY;
pointerX = event.clientX;
pointerY = event.clientY;
if (panning) {
const scale = VIEW_HEIGHT / (window.innerHeight * camera.zoom);
const rightX = Math.cos(orbit.theta);
const rightZ = -Math.sin(orbit.theta);
const forwardX = Math.sin(orbit.theta);
const forwardZ = Math.cos(orbit.theta);
orbit.target.x -= dx * scale * rightX;
orbit.target.z -= dx * scale * rightZ;
orbit.target.x += dy * scale * forwardX;
orbit.target.z += dy * scale * forwardZ;
orbit.target.x = THREE.MathUtils.clamp(orbit.target.x, -3.5, 3.5);
orbit.target.z = THREE.MathUtils.clamp(orbit.target.z, -3, 3);
} else {
orbit.theta -= dx * 0.006;
orbit.phi = THREE.MathUtils.clamp(
orbit.phi - dy * 0.005,
0.47,
1.38
);
}
updateCamera();
});
function endDrag(event) {
dragging = false;
canvas.classList.remove("grabbing");
if (event.pointerId !== undefined && canvas.hasPointerCapture(event.pointerId)) {
canvas.releasePointerCapture(event.pointerId);
}
}
canvas.addEventListener("pointerup", endDrag);
canvas.addEventListener("pointercancel", endDrag);
canvas.addEventListener("contextmenu", (event) => event.preventDefault());
canvas.addEventListener("wheel", (event) => {
event.preventDefault();
orbit.userZoom = THREE.MathUtils.clamp(
orbit.userZoom * Math.exp(-event.deltaY * 0.001),
0.62,
1.65
);
updateCamera();
}, { passive: false });
canvas.addEventListener("dblclick", () => {
orbit.theta = 0.62;
orbit.phi = 1.02;
orbit.userZoom = 1;
orbit.target.set(0, 0.05, 0);
updateCamera();
});
// -------------------------------------------------------------------------
// Lighting
// -------------------------------------------------------------------------
const hemisphere = new THREE.HemisphereLight(0xcff7ff, 0x163038, 2.05);
scene.add(hemisphere);
const keyLight = new THREE.DirectionalLight(0xffefd6, 3.25);
keyLight.position.set(-4.5, 9, 6.5);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(2048, 2048);
keyLight.shadow.camera.left = -8;
keyLight.shadow.camera.right = 8;
keyLight.shadow.camera.top = 6;
keyLight.shadow.camera.bottom = -6;
keyLight.shadow.camera.near = 1;
keyLight.shadow.camera.far = 25;
keyLight.shadow.bias = -0.00035;
scene.add(keyLight);
const rimLight = new THREE.DirectionalLight(0x65dfff, 1.45);
rimLight.position.set(7, 5, -6);
scene.add(rimLight);
const warmFill = new THREE.PointLight(0xffa956, 8, 15, 2);
warmFill.position.set(0, 2.8, 4);
scene.add(warmFill);
// -------------------------------------------------------------------------
// Voxel substrate
// -------------------------------------------------------------------------
const floorBase = new THREE.Mesh(
new THREE.BoxGeometry(11.8, 0.18, 7.25),
new THREE.MeshStandardMaterial({
color: 0x102830,
roughness: 0.94,
metalness: 0.02
})
);
floorBase.position.y = -0.25;
floorBase.receiveShadow = true;
scene.add(floorBase);
const tileGeometry = new THREE.BoxGeometry(0.475, 0.055, 0.475);
const tileMaterial = new THREE.MeshStandardMaterial({
color: 0xffffff,
roughness: 0.9,
metalness: 0.01
});
const tileCountX = 23;
const tileCountZ = 14;
const tiles = new THREE.InstancedMesh(
tileGeometry,
tileMaterial,
tileCountX * tileCountZ
);
const dummy = new THREE.Object3D();
const tileColor = new THREE.Color();
let tileIndex = 0;
for (let x = 0; x < tileCountX; x++) {
for (let z = 0; z < tileCountZ; z++) {
dummy.position.set(
(x - (tileCountX - 1) / 2) * 0.505,
-0.135,
(z - (tileCountZ - 1) / 2) * 0.505
);
dummy.updateMatrix();
tiles.setMatrixAt(tileIndex, dummy.matrix);
const variation = ((x * 7 + z * 11) % 5) * 0.012;
tileColor.setHSL(0.53, 0.29, 0.145 + variation);
tiles.setColorAt(tileIndex, tileColor);
tileIndex++;
}
}
tiles.instanceMatrix.needsUpdate = true;
if (tiles.instanceColor) tiles.instanceColor.needsUpdate = true;
tiles.receiveShadow = true;
scene.add(tiles);
// A flat amber region visually marks the initial space between the sheets.
const gapMaterial = new THREE.MeshStandardMaterial({
color: 0xffa85c,
emissive: 0x4b2109,
emissiveIntensity: 0.35,
transparent: true,
opacity: 0.22,
depthWrite: false,
roughness: 0.85
});
const gapMarker = new THREE.Mesh(
new THREE.BoxGeometry(1, 0.018, 5.72),
gapMaterial
);
gapMarker.position.set(0, -0.096, 0);
scene.add(gapMarker);
// -------------------------------------------------------------------------
// Voxel epithelial cells
// -------------------------------------------------------------------------
const cellGeometry = new THREE.BoxGeometry(0.66, 0.44, 0.66);
const nucleusGeometry = new THREE.BoxGeometry(0.21, 0.105, 0.21);
const bodyMaterials = [
new THREE.MeshStandardMaterial({
color: 0x5bd8c4,
roughness: 0.68,
metalness: 0.02,
flatShading: true
}),
new THREE.MeshStandardMaterial({
color: 0x3bb4a8,
roughness: 0.72,
metalness: 0.02,
flatShading: true
}),
new THREE.MeshStandardMaterial({
color: 0x278f87,
roughness: 0.75,
metalness: 0.02,
flatShading: true
}),
new THREE.MeshStandardMaterial({
color: 0x207873,
roughness: 0.78,
metalness: 0.02,
flatShading: true
}),
new THREE.MeshStandardMaterial({
color: 0x1b6664,
roughness: 0.8,
metalness: 0.02,
flatShading: true
})
];
const nucleusMaterial = new THREE.MeshStandardMaterial({
color: 0x465474,
roughness: 0.55,
metalness: 0.05,
flatShading: true
});
const extensionMaterial = new THREE.MeshStandardMaterial({
color: 0xffb45c,
emissive: 0x5a2605,
emissiveIntensity: 0.32,
roughness: 0.62,
flatShading: true
});
const extensionGeometryLeft = new THREE.BoxGeometry(0.20, 0.13, 0.38);
extensionGeometryLeft.translate(0.10, 0, 0);
const extensionGeometryRight = new THREE.BoxGeometry(0.20, 0.13, 0.38);
extensionGeometryRight.translate(-0.10, 0, 0);
const cells = [];
const rowCount = 8;
const depthCount = 5;
const xSpacing = 0.74;
const zSpacing = 0.72;
const frontX = 1.15;
const migrationDistance = 0.72;
const frontExtension = 0.018;
function createCell(side, row, depth) {
const direction = side === "left" ? 1 : -1;
const baseX = side === "left"
? -frontX - depth * xSpacing
: frontX + depth * xSpacing;
const baseZ = (row - (rowCount - 1) / 2) * zSpacing;
const group = new THREE.Group();
group.position.set(baseX, 0, baseZ);
const body = new THREE.Mesh(
cellGeometry,
bodyMaterials[Math.min(depth, bodyMaterials.length - 1)]
);
body.position.y = 0.13;
body.castShadow = true;
body.receiveShadow = true;
group.add(body);
const nucleus = new THREE.Mesh(nucleusGeometry, nucleusMaterial);
nucleus.position.y = 0.414;
nucleus.castShadow = true;
group.add(nucleus);
let extension = null;
if (depth === 0) {
extension = new THREE.Mesh(
direction === 1
? extensionGeometryLeft
: extensionGeometryRight,
extensionMaterial
);
extension.position.set(direction * 0.33, 0.12, 0);
extension.scale.x = 0.001;
extension.castShadow = true;
group.add(extension);
}
scene.add(group);
cells.push({
group,
extension,
baseX,
baseZ,
row,
depth,
direction,
side
});
}
for (const side of ["left", "right"]) {
for (let row = 0; row < rowCount; row++) {
for (let depth = 0; depth < depthCount; depth++) {
createCell(side, row, depth);
}
}
}
// -------------------------------------------------------------------------
// Animation timeline
// -------------------------------------------------------------------------
const DURATION = 13;
const MIGRATION_START = 1;
const MIGRATION_END = 9.7;
const playButton = document.querySelector("#play");
const replayButton = document.querySelector("#replay");
const scrubber = document.querySelector("#scrubber");
const timeReadout = document.querySelector("#time");
const gapValue = document.querySelector("#gap-value");
const stageNumber = document.querySelector("#stage-number");
const stageTitle = document.querySelector("#stage-title");
const stageCopy = document.querySelector("#stage-copy");
const stageBars = [...document.querySelectorAll(".steps i")];
let elapsed = 0;
let playing = true;
let lastFrame = performance.now();
let currentStage = -1;
function clamp01(value) {
return Math.min(1, Math.max(0, value));
}
function smoothstep(value) {
const t = clamp01(value);
return t * t * (3 - 2 * t);
}
function migrationProgress(time) {
if (time <= MIGRATION_START) return 0;
if (time >= MIGRATION_END) return 1;
return smoothstep(
(time - MIGRATION_START) / (MIGRATION_END - MIGRATION_START)
);
}
function updatePlayButton() {
playButton.textContent = playing ? "Pause" : "Play";
playButton.setAttribute(
"aria-label",
playing ? "Pause animation" : "Play animation"
);
}
function updateStage(time) {
let stage;
if (time < 1) {
stage = {
number: "01",
title: "Two edges flank a gap",
copy: "The epithelial fronts remain separated by a narrow space."
};
} else if (time < 3.1) {
stage = {
number: "02",
title: "Leading cells polarize",
copy: "Small amber extensions emerge from the front-row cells."
};
} else if (time < 8.8) {
stage = {
number: "03",
title: "Neighbors advance together",
copy: "Each visible row migrates as a connected epithelial front."
};
} else {
stage = {
number: "04",
title: "The gap closes",
copy: "Opposing fronts approach contact while retaining their neighbors."
};
}
if (stage.number !== String(currentStage)) {
currentStage = Number(stage.number);
stageNumber.textContent = stage.number;
stageTitle.textContent = stage.title;
stageCopy.textContent = stage.copy;
stageBars.forEach((bar, index) => {
bar.classList.toggle("active", index < currentStage);
});
}
}
function applyMigration(progress) {
let leftInnerEdge = Infinity;
let rightInnerEdge = -Infinity;
for (const cell of cells) {
// A slight row-to-row wave gives the collective front a visible rhythm.
const rowDelay = cell.row * 0.015;
const rowProgress = smoothstep(
(progress - 0.02 - rowDelay) / (0.88 - cell.row * 0.01)
);
// Every cell in a row moves by the same amount, preserving visible
// neighbor-to-neighbor contact. The leading cell gains only a tiny
// extra offset that remains smaller than the inter-cell clearance.
const leadingOffset = cell.depth === 0
? frontExtension * rowProgress
: 0;
const shift = migrationDistance * rowProgress + leadingOffset;
cell.group.position.x =
cell.baseX + cell.direction * shift;
if (cell.extension) {
const emergence = smoothstep(
(rowProgress - 0.025) / 0.24
);
cell.extension.scale.x =
0.001 + emergence * (0.55 + rowProgress * 0.20);
}
if (cell.depth === 0) {
const cellEdge =
cell.direction === 1
? cell.group.position.x + 0.33
: cell.group.position.x - 0.33;
if (cell.side === "left") {
leftInnerEdge = Math.min(leftInnerEdge, cellEdge);
} else {
rightInnerEdge = Math.max(rightInnerEdge, cellEdge);
}
}
}
const gap = Math.max(0.16, rightInnerEdge - leftInnerEdge);
gapMarker.scale.x = gap;
gapMaterial.opacity = 0.06 + 0.18 * clamp01(gap / 1.64);
if (gap <= 0.22) {
gapValue.textContent = "FRONTS IN CONTACT";
} else {
gapValue.textContent = `${gap.toFixed(2)} u`;
}
}
function updateTimelineUI() {
const fraction = elapsed / DURATION;
scrubber.value = Math.round(fraction * 1000);
scrubber.style.setProperty("--progress", `${fraction * 100}%`);
timeReadout.textContent = `${elapsed.toFixed(1)} / ${DURATION.toFixed(1)} s`;
}
playButton.addEventListener("click", () => {
if (!playing && elapsed >= DURATION) {
elapsed = 0;
currentStage = -1;
}
playing = !playing;
updatePlayButton();
});
replayButton.addEventListener("click", () => {
elapsed = 0;
playing = true;
currentStage = -1;
updatePlayButton();
});
scrubber.addEventListener("input", () => {
elapsed = (Number(scrubber.value) / 1000) * DURATION;
playing = false;
currentStage = -1;
updatePlayButton();
});
window.addEventListener("keydown", (event) => {
if (
event.code === "Space" &&
event.target === document.body
) {
event.preventDefault();
playButton.click();
}
if (
event.key.toLowerCase() === "r" &&
event.target === document.body
) {
replayButton.click();
}
});
// -------------------------------------------------------------------------
// Screen-space labels attached to world positions
// -------------------------------------------------------------------------
const labelGap = document.querySelector("#label-gap");
const labelLeft = document.querySelector("#label-left");
const labelRight = document.querySelector("#label-right");
const projected = new THREE.Vector3();
function placeLabel(element, worldPosition, offsetX = 0, offsetY = 0) {
projected.copy(worldPosition).project(camera);
const visible =
projected.z >= -1 &&
projected.z <= 1 &&
projected.x > -1.25 &&
projected.x < 1.25 &&
projected.y > -1.25 &&
projected.y < 1.25;
element.style.display = visible ? "block" : "none";
if (!visible) return;
const x = (projected.x * 0.5 + 0.5) * window.innerWidth + offsetX;
const y = (-projected.y * 0.5 + 0.5) * window.innerHeight + offsetY;
element.style.transform =
`translate(-50%, -50%) translate(${x}px, ${y}px)`;
}
function updateLabels() {
placeLabel(labelGap, new THREE.Vector3(0, 1.15, 0), 0, -8);
placeLabel(labelLeft, new THREE.Vector3(-2.55, 0.65, 2.55), -22, 0);
placeLabel(labelRight, new THREE.Vector3(2.55, 0.65, -2.55), 22, 0);
}
// -------------------------------------------------------------------------
// Render loop
// -------------------------------------------------------------------------
function animate(now) {
requestAnimationFrame(animate);
const delta = Math.min((now - lastFrame) / 1000, 0.05);
lastFrame = now;
if (playing) {
elapsed = Math.min(DURATION, elapsed + delta);
if (elapsed >= DURATION) {
playing = false;
updatePlayButton();
}
}
const progress = migrationProgress(elapsed);
applyMigration(progress);
updateStage(elapsed);
updateTimelineUI();
updateLabels();
renderer.render(scene, camera);
}
updatePlayButton();
requestAnimationFrame(animate);
</script>
</body>
</html>
```