Prompt specification
Build or customize the Three.js 3D scene "Exchange-distance teaching explorer" 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: Exchange-distance teaching explorer
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/exchange-distance-teaching-explorer-x0968
Scene Description Prompt:
Build a self-contained HTML scene with Three.js from cdnjs featuring capillary exchange. Allow changing cell-to-vessel spacing in a toy diffusion model; compare representative travel times under displayed assumptions and avoid claims about diagnosis or exercise performance. Voxel style.
Working Three.js Starter (exchange-distance-teaching-explorer-x0968):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Voxel Capillary Exchange</title>
<style>
:root {
color-scheme: dark;
--bg: #071118;
--panel: #0b1820;
--panel-2: #10242d;
--line: #203a45;
--text: #e7f3f4;
--muted: #8da8b1;
--cyan: #65e5df;
--amber: #ffd166;
--red: #f0445e;
--purple: #c48cff;
}
* {
box-sizing: border-box;
}
html,
body {
margin: 0;
min-height: 100%;
background: var(--bg);
color: var(--text);
font-family:
Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
button,
input {
font: inherit;
}
.app {
display: grid;
grid-template-columns: 350px minmax(0, 1fr);
min-height: 100vh;
}
.panel {
position: relative;
z-index: 5;
display: flex;
flex-direction: column;
gap: 22px;
min-height: 100vh;
padding: 26px 24px;
overflow-y: auto;
background:
linear-gradient(180deg, rgba(18, 43, 53, 0.94), rgba(7, 17, 24, 0.98)),
var(--panel);
border-right: 1px solid var(--line);
box-shadow: 14px 0 40px rgba(0, 0, 0, 0.24);
}
.eyebrow {
margin-bottom: 10px;
color: var(--cyan);
font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
font-size: 0.72rem;
font-weight: 800;
letter-spacing: 0.15em;
text-transform: uppercase;
}
h1 {
margin: 0;
font-size: clamp(2.1rem, 5vw, 3.2rem);
font-weight: 820;
letter-spacing: -0.055em;
line-height: 0.96;
}
.lede {
margin: 14px 0 0;
color: var(--muted);
font-size: 0.96rem;
line-height: 1.55;
}
.section {
padding-top: 20px;
border-top: 1px solid var(--line);
}
.section-heading {
display: flex;
align-items: center;
justify-content: space-between;
gap: 12px;
margin-bottom: 14px;
}
.section-heading h2 {
margin: 0;
font-size: 0.86rem;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.toy-badge {
padding: 4px 7px;
color: var(--amber);
background: rgba(255, 209, 102, 0.08);
border: 1px solid rgba(255, 209, 102, 0.28);
font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
font-size: 0.65rem;
font-weight: 800;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.control-head {
display: flex;
align-items: baseline;
justify-content: space-between;
gap: 12px;
}
.control-head label {
font-size: 0.95rem;
font-weight: 720;
}
#spacingOutput {
color: var(--amber);
font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
font-size: 1.15rem;
font-weight: 800;
}
#spacing {
width: 100%;
height: 6px;
margin: 20px 0 10px;
appearance: none;
border-radius: 0;
outline: none;
cursor: pointer;
}
#spacing::-webkit-slider-thumb {
width: 18px;
height: 18px;
appearance: none;
background: var(--amber);
border: 3px solid #162a32;
border-radius: 2px;
box-shadow: 0 0 0 1px var(--amber);
}
#spacing::-moz-range-thumb {
width: 14px;
height: 14px;
background: var(--amber);
border: 3px solid #162a32;
border-radius: 2px;
box-shadow: 0 0 0 1px var(--amber);
}
.range-scale,
.control-note {
color: var(--muted);
font-size: 0.75rem;
line-height: 1.45;
}
.range-scale {
display: flex;
justify-content: space-between;
font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
}
.control-note {
margin-top: 10px;
}
.metrics {
display: grid;
gap: 10px;
}
.metric-card {
position: relative;
padding: 14px;
overflow: hidden;
background: rgba(7, 18, 25, 0.72);
border: 1px solid var(--line);
border-left-width: 3px;
}
.metric-card.outbound {
border-left-color: var(--amber);
}
.metric-card.inbound {
border-left-color: var(--cyan);
}
.route {
display: flex;
align-items: center;
gap: 8px;
color: var(--muted);
font-size: 0.76rem;
font-weight: 700;
letter-spacing: 0.03em;
}
.swatch {
width: 9px;
height: 9px;
display: inline-block;
box-shadow: 0 0 10px currentColor;
}
.swatch.amber {
color: var(--amber);
background: var(--amber);
}
.swatch.cyan {
color: var(--cyan);
background: var(--cyan);
}
.metric-value {
display: flex;
align-items: baseline;
gap: 6px;
margin: 7px 0 9px;
}
.metric-value strong {
font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
font-size: 2rem;
letter-spacing: -0.06em;
}
.metric-value small {
color: var(--muted);
font-size: 0.85rem;
}
.compare-row {
display: flex;
justify-content: space-between;
gap: 10px;
padding-top: 8px;
border-top: 1px dashed #29434d;
color: var(--muted);
font-size: 0.72rem;
}
.compare-row strong {
color: var(--text);
font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
}
.ratio-box {
display: flex;
align-items: center;
justify-content: space-between;
gap: 10px;
padding: 11px 12px;
background: rgba(196, 140, 255, 0.08);
border: 1px solid rgba(196, 140, 255, 0.2);
color: #d9c2ff;
font-size: 0.75rem;
}
.ratio-box strong {
font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
font-size: 1rem;
}
.formula {
padding: 12px;
text-align: center;
color: var(--text);
background: #071219;
border: 1px solid var(--line);
font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
font-size: 1rem;
}
.assumptions {
margin: 12px 0 0;
padding-left: 18px;
color: var(--muted);
font-size: 0.78rem;
line-height: 1.55;
}
.assumptions li + li {
margin-top: 8px;
}
.assumptions b {
color: var(--text);
}
.disclaimer {
padding: 12px;
color: #b8cbd0;
background: rgba(240, 68, 94, 0.055);
border: 1px solid rgba(240, 68, 94, 0.18);
font-size: 0.73rem;
line-height: 1.5;
}
.button-row {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 8px;
margin-top: auto;
}
button {
min-height: 39px;
color: var(--text);
background: #102730;
border: 1px solid #2a4854;
border-radius: 2px;
font-size: 0.76rem;
font-weight: 800;
cursor: pointer;
}
button:hover {
background: #16343e;
border-color: #4a7280;
}
button:focus-visible,
input:focus-visible {
outline: 2px solid var(--cyan);
outline-offset: 3px;
}
.stage {
position: relative;
min-width: 0;
min-height: 100vh;
overflow: hidden;
isolation: isolate;
background:
radial-gradient(circle at 52% 42%, rgba(25, 74, 87, 0.23), transparent 44%),
linear-gradient(180deg, #0a1820, #061016);
}
.stage::before {
position: absolute;
inset: 0;
z-index: -1;
content: "";
opacity: 0.16;
background-image:
linear-gradient(rgba(119, 190, 202, 0.16) 1px, transparent 1px),
linear-gradient(90deg, rgba(119, 190, 202, 0.16) 1px, transparent 1px);
background-size: 32px 32px;
mask-image: linear-gradient(to bottom, black, transparent 82%);
}
.scene-canvas {
position: absolute;
inset: 0;
display: block;
width: 100%;
height: 100%;
cursor: grab;
touch-action: none;
}
.scene-canvas.dragging {
cursor: grabbing;
}
.hud-top {
position: absolute;
top: 22px;
left: 24px;
right: 24px;
z-index: 3;
display: flex;
align-items: flex-start;
justify-content: space-between;
gap: 16px;
pointer-events: none;
}
.hud-title {
max-width: 390px;
padding: 12px 14px;
background: rgba(5, 14, 20, 0.72);
border: 1px solid rgba(92, 142, 154, 0.25);
backdrop-filter: blur(8px);
}
.hud-title strong {
display: block;
margin-bottom: 4px;
font-size: 0.86rem;
letter-spacing: 0.04em;
text-transform: uppercase;
}
.hud-title span {
color: var(--muted);
font-size: 0.73rem;
line-height: 1.4;
}
.hud-tags {
display: flex;
flex-wrap: wrap;
justify-content: flex-end;
gap: 6px;
}
.hud-tags span {
padding: 6px 8px;
color: #b9d0d5;
background: rgba(5, 14, 20, 0.68);
border: 1px solid rgba(92, 142, 154, 0.24);
font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
font-size: 0.63rem;
letter-spacing: 0.06em;
text-transform: uppercase;
}
.legend {
position: absolute;
left: 24px;
bottom: 22px;
z-index: 3;
display: grid;
gap: 8px;
min-width: 215px;
padding: 13px 14px;
background: rgba(5, 14, 20, 0.78);
border: 1px solid rgba(92, 142, 154, 0.25);
backdrop-filter: blur(8px);
pointer-events: none;
}
.legend-row {
display: flex;
align-items: center;
gap: 9px;
color: #c4d7da;
font-size: 0.72rem;
}
.legend-mark {
width: 10px;
height: 10px;
flex: 0 0 auto;
}
.legend-mark.amber {
background: var(--amber);
box-shadow: 0 0 9px rgba(255, 209, 102, 0.8);
}
.legend-mark.cyan {
background: var(--cyan);
box-shadow: 0 0 9px rgba(101, 229, 223, 0.8);
}
.legend-mark.red {
background: var(--red);
box-shadow: 0 0 9px rgba(240, 68, 94, 0.8);
}
.hint {
position: absolute;
right: 24px;
bottom: 22px;
z-index: 3;
padding: 9px 11px;
color: var(--muted);
background: rgba(5, 14, 20, 0.7);
border: 1px solid rgba(92, 142, 154, 0.2);
font-size: 0.69rem;
pointer-events: none;
}
.pin {
position: absolute;
z-index: 4;
display: flex;
align-items: center;
gap: 6px;
padding: 5px 7px;
color: #d9e7e9;
background: rgba(4, 12, 17, 0.8);
border: 1px solid rgba(115, 168, 180, 0.32);
font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
font-size: 0.64rem;
font-weight: 800;
letter-spacing: 0.04em;
white-space: nowrap;
pointer-events: none;
transform: translate(-50%, -50%);
}
.pin::before {
width: 6px;
height: 6px;
content: "";
background: currentColor;
}
#vesselPin {
color: #ff8998;
}
#tissuePin {
color: var(--cyan);
}
#spacingPin {
color: var(--amber);
}
#loading {
position: absolute;
inset: 0;
z-index: 10;
display: grid;
place-items: center;
color: var(--muted);
background: #071118;
font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
font-size: 0.75rem;
letter-spacing: 0.08em;
text-transform: uppercase;
transition: opacity 0.3s ease;
}
#loading.hidden {
opacity: 0;
pointer-events: none;
}
@media (max-width: 850px) {
.app {
grid-template-columns: 1fr;
}
.panel {
min-height: auto;
border-right: 0;
border-bottom: 1px solid var(--line);
}
.stage {
min-height: 620px;
}
.hud-top {
top: 14px;
left: 14px;
right: 14px;
}
.hud-title {
max-width: 260px;
}
.hud-tags {
display: none;
}
.legend {
left: 14px;
bottom: 14px;
}
.hint {
right: 14px;
bottom: 14px;
}
}
@media (max-width: 520px) {
.panel {
padding: 22px 18px;
}
.stage {
min-height: 540px;
}
.hud-title span,
.hint {
display: none;
}
.legend {
right: 14px;
min-width: 0;
}
}
</style>
</head>
<body>
<div class="app">
<aside class="panel">
<div>
<div class="eyebrow">Interactive voxel model</div>
<h1>Capillary<br />exchange</h1>
<p class="lede">
Change the cell-to-vessel spacing and inspect a symmetric diffusion
proxy. Colored cubes show exchange in both directions.
</p>
</div>
<section class="section">
<div class="section-heading">
<h2>Spacing control</h2>
<span class="toy-badge">Toy estimate</span>
</div>
<div class="control-head">
<label for="spacing">Cell-to-vessel spacing</label>
<output id="spacingOutput" for="spacing" aria-live="polite">30 μm</output>
</div>
<input
id="spacing"
type="range"
min="10"
max="70"
step="1"
value="30"
aria-label="Cell-to-vessel spacing in micrometers"
/>
<div class="range-scale">
<span>10 μm</span>
<span>70 μm</span>
</div>
<div class="control-note">
Spacing changes both the voxel geometry and the displayed time scale.
</div>
</section>
<section class="section">
<div class="section-heading">
<h2>Representative time scales</h2>
<span class="toy-badge">Not physiological predictions</span>
</div>
<div class="metrics">
<article class="metric-card outbound">
<div class="route">
<span class="swatch amber"></span>
Vessel → tissue
</div>
<div class="metric-value">
<strong id="outCurrent">0.90</strong>
<small id="outUnit">s</small>
</div>
<div class="compare-row">
<span>25 μm reference</span>
<strong id="outReference">0.63 s</strong>
</div>
</article>
<article class="metric-card inbound">
<div class="route">
<span class="swatch cyan"></span>
Tissue → vessel
</div>
<div class="metric-value">
<strong id="inCurrent">0.90</strong>
<small id="inUnit">s</small>
</div>
<div class="compare-row">
<span>25 μm reference</span>
<strong id="inReference">0.63 s</strong>
</div>
</article>
<div class="ratio-box">
<span>Current ÷ 25 μm reference</span>
<strong id="ratioValue">1.44×</strong>
</div>
</div>
<div class="formula" style="margin-top: 10px">
t ≈ L² / 2D
</div>
<ul class="assumptions">
<li>
<b>Effective diffusivity:</b>
D = 5.0 × 10<sup>−10</sup> m²/s in both directions.
</li>
<li>
<b>Distance:</b> L is the nearest cell-edge to vessel-wall spacing.
</li>
<li>
<b>Interpretation:</b> t is a one-dimensional RMS-diffusion time
scale, not a mean arrival time.
</li>
<li>
<b>Symmetry:</b> equal spacing and diffusivity produce equal
directional estimates.
</li>
<li>
<b>Reference:</b> each direction is compared with a fixed 25 μm
spacing under the same assumptions.
</li>
</ul>
</section>
<div class="disclaimer">
Educational visualization only. The displayed values are toy-model
scaling proxies. They are not physiological predictions, diagnostic
estimates, or guidance about exercise performance.
</div>
<div class="button-row">
<button id="pauseButton" type="button" aria-pressed="false">
Pause motion
</button>
<button id="resetButton" type="button">Reset view</button>
</div>
</aside>
<main class="stage" id="stage" aria-label="Interactive voxel capillary scene">
<div id="loading">Building voxel scene…</div>
<div class="hud-top">
<div class="hud-title">
<strong>Exchange corridor</strong>
<span>
The red voxel chain is a toy capillary. Small moving cubes indicate
two exchange directions. Motion speed is not tied to the time values.
</span>
</div>
<div class="hud-tags">
<span>Voxel view</span>
<span>Symmetric model</span>
<span>Flow marker →</span>
</div>
</div>
<div class="pin" id="vesselPin">CAPILLARY</div>
<div class="pin" id="tissuePin">TISSUE CELL</div>
<div class="pin" id="spacingPin">L = 30 μm</div>
<div class="legend">
<div class="legend-row">
<span class="legend-mark amber"></span>
Vessel → tissue
</div>
<div class="legend-row">
<span class="legend-mark cyan"></span>
Tissue → vessel
</div>
<div class="legend-row">
<span class="legend-mark red"></span>
Toy capillary lumen
</div>
</div>
<div class="hint">Drag to orbit · Scroll to zoom · Double-click to reset</div>
</main>
</div>
<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 loading = document.getElementById("loading");
const renderer = new THREE.WebGLRenderer({
antialias: true,
alpha: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.1;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.domElement.className = "scene-canvas";
renderer.domElement.setAttribute("aria-label", "Voxel capillary exchange scene");
stage.prepend(renderer.domElement);
const scene = new THREE.Scene();
scene.fog = new THREE.Fog(0x071118, 18, 31);
const camera = new THREE.OrthographicCamera(-8, 8, 5, -5, 0.1, 80);
const target = new THREE.Vector3(0, 0.35, 0);
const cameraState = {
theta: 0.68,
phi: 1.02,
zoom: 1,
dragging: false,
lastX: 0,
lastY: 0
};
function updateCamera() {
const radius = 14;
const sinPhi = Math.sin(cameraState.phi);
camera.position.set(
target.x + radius * sinPhi * Math.sin(cameraState.theta),
target.y + radius * Math.cos(cameraState.phi),
target.z + radius * sinPhi * Math.cos(cameraState.theta)
);
camera.lookAt(target);
camera.zoom = cameraState.zoom;
camera.updateProjectionMatrix();
camera.updateMatrixWorld();
}
function resize() {
const width = Math.max(1, stage.clientWidth);
const height = Math.max(1, stage.clientHeight);
const aspect = width / height;
const viewHeight = 8.5;
camera.left = (-viewHeight * aspect) / 2;
camera.right = (viewHeight * aspect) / 2;
camera.top = viewHeight / 2;
camera.bottom = -viewHeight / 2;
camera.updateProjectionMatrix();
renderer.setSize(width, height, false);
}
new ResizeObserver(resize).observe(stage);
resize();
updateCamera();
// Direct camera controls: no external control modules are used.
const canvas = renderer.domElement;
canvas.addEventListener("pointerdown", (event) => {
cameraState.dragging = true;
cameraState.lastX = event.clientX;
cameraState.lastY = event.clientY;
canvas.setPointerCapture(event.pointerId);
canvas.classList.add("dragging");
});
canvas.addEventListener("pointermove", (event) => {
if (!cameraState.dragging) return;
const dx = event.clientX - cameraState.lastX;
const dy = event.clientY - cameraState.lastY;
cameraState.lastX = event.clientX;
cameraState.lastY = event.clientY;
cameraState.theta -= dx * 0.006;
cameraState.phi = THREE.MathUtils.clamp(
cameraState.phi - dy * 0.005,
0.48,
1.43
);
updateCamera();
});
function endDrag(event) {
cameraState.dragging = false;
canvas.classList.remove("dragging");
if (event.pointerId !== undefined && canvas.hasPointerCapture(event.pointerId)) {
canvas.releasePointerCapture(event.pointerId);
}
}
canvas.addEventListener("pointerup", endDrag);
canvas.addEventListener("pointercancel", endDrag);
canvas.addEventListener(
"wheel",
(event) => {
event.preventDefault();
cameraState.zoom = THREE.MathUtils.clamp(
cameraState.zoom * Math.exp(-event.deltaY * 0.001),
0.68,
1.85
);
updateCamera();
},
{ passive: false }
);
function resetCamera() {
cameraState.theta = 0.68;
cameraState.phi = 1.02;
cameraState.zoom = 1;
updateCamera();
}
canvas.addEventListener("dblclick", resetCamera);
document.getElementById("resetButton").addEventListener("click", resetCamera);
// Lighting.
scene.add(new THREE.HemisphereLight(0x8ed9ee, 0x10202a, 2.2));
const keyLight = new THREE.DirectionalLight(0xffd7b3, 3.1);
keyLight.position.set(5, 8, 7);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(1024, 1024);
keyLight.shadow.camera.left = -9;
keyLight.shadow.camera.right = 9;
keyLight.shadow.camera.top = 7;
keyLight.shadow.camera.bottom = -7;
keyLight.shadow.camera.near = 0.5;
keyLight.shadow.camera.far = 30;
scene.add(keyLight);
const fillLight = new THREE.DirectionalLight(0x5ecfff, 1.65);
fillLight.position.set(-6, 2, -4);
scene.add(fillLight);
// Ground and grid.
const floor = new THREE.Mesh(
new THREE.PlaneGeometry(32, 32),
new THREE.MeshStandardMaterial({
color: 0x07151d,
roughness: 0.98,
metalness: 0
})
);
floor.rotation.x = -Math.PI / 2;
floor.position.y = -3.05;
floor.receiveShadow = true;
scene.add(floor);
const grid = new THREE.GridHelper(24, 24, 0x315563, 0x18333e);
grid.position.y = -3.035;
grid.material.transparent = true;
grid.material.opacity = 0.22;
scene.add(grid);
const dummy = new THREE.Object3D();
function makeInstanced(geometry, material, count, options = {}) {
const mesh = new THREE.InstancedMesh(geometry, material, count);
mesh.instanceMatrix.setUsage(THREE.DynamicDrawUsage);
mesh.castShadow = options.castShadow ?? true;
mesh.receiveShadow = options.receiveShadow ?? true;
scene.add(mesh);
return mesh;
}
// Muted voxel tissue matrix in the background.
const backgroundMaterial = new THREE.MeshStandardMaterial({
color: 0xffffff,
roughness: 0.95,
transparent: true,
opacity: 0.2,
depthWrite: false
});
const backgroundMesh = makeInstanced(
new THREE.BoxGeometry(0.67, 0.67, 0.67),
backgroundMaterial,
7 * 8,
{ castShadow: false, receiveShadow: false }
);
let backgroundIndex = 0;
for (let row = 0; row < 8; row++) {
for (let col = 0; col < 7; col++) {
dummy.position.set(
-5.4 + col * 1.8,
-2.7 + row * 0.9,
-2.35 + (row % 2) * 0.16
);
dummy.rotation.set(0, 0, 0);
dummy.scale.set(1, 1, 1);
dummy.updateMatrix();
backgroundMesh.setMatrixAt(backgroundIndex, dummy.matrix);
backgroundMesh.setColorAt(
backgroundIndex,
new THREE.Color(row % 2 === 0 ? 0x17414c : 0x1c344c)
);
backgroundIndex++;
}
}
backgroundMesh.instanceMatrix.needsUpdate = true;
backgroundMesh.instanceColor.needsUpdate = true;
// Capillary assembled from rectangular voxels.
const vesselCenterY = 0.2;
const vesselTop = vesselCenterY + 0.32;
const vesselBottom = vesselCenterY - 0.32;
const segmentCount = 18;
const wallMaterial = new THREE.MeshStandardMaterial({
color: 0x8f2341,
roughness: 0.72,
metalness: 0.05
});
const lumenMaterial = new THREE.MeshStandardMaterial({
color: 0xef3f58,
emissive: 0x650719,
emissiveIntensity: 0.8,
roughness: 0.42
});
const wallMesh = makeInstanced(
new THREE.BoxGeometry(0.52, 0.14, 0.5),
wallMaterial,
segmentCount * 2
);
const lumenMesh = makeInstanced(
new THREE.BoxGeometry(0.5, 0.25, 0.28),
lumenMaterial,
segmentCount
);
let wallIndex = 0;
for (let i = 0; i < segmentCount; i++) {
const x = -4.6 + i * 0.54;
dummy.position.set(x, vesselCenterY + 0.25, 0);
dummy.updateMatrix();
wallMesh.setMatrixAt(wallIndex++, dummy.matrix);
dummy.position.set(x, vesselCenterY - 0.25, 0);
dummy.updateMatrix();
wallMesh.setMatrixAt(wallIndex++, dummy.matrix);
dummy.position.set(x, vesselCenterY, 0);
dummy.updateMatrix();
lumenMesh.setMatrixAt(i, dummy.matrix);
}
wallMesh.instanceMatrix.needsUpdate = true;
lumenMesh.instanceMatrix.needsUpdate = true;
const endCapGeometry = new THREE.BoxGeometry(0.18, 0.72, 0.72);
const endCapMaterial = new THREE.MeshStandardMaterial({
color: 0x6f1934,
roughness: 0.78
});
for (const x of [-5.03, 5.03]) {
const cap = new THREE.Mesh(endCapGeometry, endCapMaterial);
cap.position.set(x, vesselCenterY, 0);
cap.castShadow = true;
scene.add(cap);
}
// Moving lumen markers indicate the displayed flow direction.
const flowCount = 14;
const flowMaterial = new THREE.MeshStandardMaterial({
color: 0xffb1bb,
emissive: 0xff3454,
emissiveIntensity: 1.8,
roughness: 0.35
});
const flowMesh = makeInstanced(
new THREE.BoxGeometry(0.13, 0.13, 0.13),
flowMaterial,
flowCount,
{ receiveShadow: false }
);
// Tissue cells: each is a small voxel cluster.
const cellMaterial = new THREE.MeshStandardMaterial({
color: 0xffffff,
roughness: 0.62,
metalness: 0.03,
transparent: true,
opacity: 0.94
});
const nucleusMaterial = new THREE.MeshStandardMaterial({
color: 0xffb05e,
emissive: 0x5d2309,
emissiveIntensity: 0.35,
roughness: 0.48
});
const laneXs = [-3.75, -1.25, 1.25, 3.75];
const cellOffsetCount = 7;
const cellOffsets = [
[0, 0, 0],
[-0.23, 0, 0],
[0.23, 0, 0],
[0, -0.23, 0],
[0, 0.23, 0],
[0, 0, -0.23],
[0, 0, 0.23]
];
const cellMesh = makeInstanced(
new THREE.BoxGeometry(0.23, 0.23, 0.23),
cellMaterial,
laneXs.length * 2 * cellOffsetCount
);
const nucleusMesh = makeInstanced(
new THREE.BoxGeometry(0.2, 0.2, 0.2),
nucleusMaterial,
laneXs.length * 2
);
const lanes = [];
// Spacing bracket.
const bracketVertical = new THREE.Line(
new THREE.BufferGeometry().setFromPoints([
new THREE.Vector3(),
new THREE.Vector3()
]),
new THREE.LineBasicMaterial({
color: 0xffd166,
transparent: true,
opacity: 0.95
})
);
const bracketCaps = new THREE.LineSegments(
new THREE.BufferGeometry().setFromPoints([
new THREE.Vector3(),
new THREE.Vector3(),
new THREE.Vector3(),
new THREE.Vector3()
]),
new THREE.LineBasicMaterial({
color: 0xffd166,
transparent: true,
opacity: 0.95
})
);
bracketVertical.frustumCulled = false;
bracketCaps.frustumCulled = false;
scene.add(bracketVertical, bracketCaps);
// Exchange particles.
const particleCount = 40;
const outboundMaterial = new THREE.MeshStandardMaterial({
color: 0xffd166,
emissive: 0xa96500,
emissiveIntensity: 1.25,
roughness: 0.35
});
const inboundMaterial = new THREE.MeshStandardMaterial({
color: 0x65e5df,
emissive: 0x087f7c,
emissiveIntensity: 1.25,
roughness: 0.35
});
const outboundMesh = makeInstanced(
new THREE.BoxGeometry(0.105, 0.105, 0.105),
outboundMaterial,
particleCount,
{ receiveShadow: false }
);
const inboundMesh = makeInstanced(
new THREE.BoxGeometry(0.105, 0.105, 0.105),
inboundMaterial,
particleCount,
{ receiveShadow: false }
);
const particleData = Array.from({ length: particleCount }, (_, i) => ({
lane: i % (laneXs.length * 2),
phase: (i * 0.61803398875) % 1,
wobble: ((i % 7) - 3) * 0.018
}));
let spacingMicrons = 30;
function rebuildCells() {
const gapWorld = spacingMicrons * 0.022;
const cellHalfHeight = 0.36;
lanes.length = 0;
let bodyIndex = 0;
let nucleusIndex = 0;
for (const side of [1, -1]) {
for (let laneIndex = 0; laneIndex < laneXs.length; laneIndex++) {
const x = laneXs[laneIndex];
const z = side > 0 ? 0.05 : -0.05;
const y =
side > 0
? vesselTop + gapWorld + cellHalfHeight
: vesselBottom - gapWorld - cellHalfHeight;
lanes.push({ x, y, z, side });
const palette =
side > 0
? [0x55d8c4, 0x45bcca, 0x77dfb6]
: [0x4ec5bc, 0x62d5cf, 0x47b7d1];
const baseColor = new THREE.Color(palette[laneIndex]);
for (const [ox, oy, oz] of cellOffsets) {
dummy.position.set(x + ox, y + oy, z + oz);
dummy.rotation.set(0, 0, 0);
dummy.scale.set(1, 1, 1);
dummy.updateMatrix();
cellMesh.setMatrixAt(bodyIndex, dummy.matrix);
const tint = baseColor.clone();
tint.offsetHSL(0, 0, (ox + oy + oz) * 0.035);
cellMesh.setColorAt(bodyIndex, tint);
bodyIndex++;
}
dummy.position.set(x, y, z + 0.2);
dummy.updateMatrix();
nucleusMesh.setMatrixAt(nucleusIndex, dummy.matrix);
nucleusIndex++;
}
}
cellMesh.instanceMatrix.needsUpdate = true;
cellMesh.instanceColor.needsUpdate = true;
nucleusMesh.instanceMatrix.needsUpdate = true;
// Measurement bracket between vessel wall and nearest cell edge.
const bracketX = -5.12;
const bracketZ = 0.42;
const upperCellBottom = vesselTop + gapWorld;
const verticalPositions = bracketVertical.geometry.attributes.position;
verticalPositions.setXYZ(0, bracketX, vesselTop, bracketZ);
verticalPositions.setXYZ(1, bracketX, upperCellBottom, bracketZ);
verticalPositions.needsUpdate = true;
const capPositions = bracketCaps.geometry.attributes.position;
capPositions.setXYZ(0, bracketX - 0.13, vesselTop, bracketZ);
capPositions.setXYZ(1, bracketX + 0.13, vesselTop, bracketZ);
capPositions.setXYZ(2, bracketX - 0.13, upperCellBottom, bracketZ);
capPositions.setXYZ(3, bracketX + 0.13, upperCellBottom, bracketZ);
capPositions.needsUpdate = true;
bracketVertical.geometry.computeBoundingSphere();
bracketCaps.geometry.computeBoundingSphere();
}
// Displayed toy-model assumptions.
const effectiveDiffusivity = 5.0e-10; // m²/s
const referenceSpacing = 25; // μm
function timeScale(microns) {
const meters = microns * 1e-6;
return (meters * meters) / (2 * effectiveDiffusivity);
}
function durationParts(seconds) {
if (seconds < 1) {
return { value: seconds.toFixed(2), unit: "s" };
}
if (seconds < 60) {
return { value: seconds.toFixed(1), unit: "s" };
}
return { value: (seconds / 60).toFixed(1), unit: "min" };
}
function updateMetrics() {
const current = timeScale(spacingMicrons);
const reference = timeScale(referenceSpacing);
const currentParts = durationParts(current);
const ratio = current / reference;
document.getElementById("spacingOutput").textContent =
`${spacingMicrons} μm`;
document.getElementById("outCurrent").textContent = currentParts.value;
document.getElementById("outUnit").textContent = currentParts.unit;
document.getElementById("inCurrent").textContent = currentParts.value;
document.getElementById("inUnit").textContent = currentParts.unit;
const referenceText = durationParts(reference);
document.getElementById("outReference").textContent =
`${referenceText.value} ${referenceText.unit}`;
document.getElementById("inReference").textContent =
`${referenceText.value} ${referenceText.unit}`;
document.getElementById("ratioValue").textContent = `${ratio.toFixed(2)}×`;
document.getElementById("spacingPin").textContent = `L = ${spacingMicrons} μm`;
}
const slider = document.getElementById("spacing");
function updateSliderFill() {
const percentage = ((spacingMicrons - 10) / 60) * 100;
slider.style.background = `linear-gradient(
90deg,
#ffd166 0%,
#ffd166 ${percentage}%,
#1b3540 ${percentage}%,
#1b3540 100%
)`;
}
slider.addEventListener("input", () => {
spacingMicrons = Number(slider.value);
updateSliderFill();
rebuildCells();
updateMetrics();
});
updateSliderFill();
rebuildCells();
updateMetrics();
// Animation state.
let elapsed = 0;
let running = true;
const clock = new THREE.Clock();
const pauseButton = document.getElementById("pauseButton");
function toggleMotion() {
running = !running;
pauseButton.textContent = running ? "Pause motion" : "Resume motion";
pauseButton.setAttribute("aria-pressed", String(!running));
}
pauseButton.addEventListener("click", toggleMotion);
window.addEventListener("keydown", (event) => {
const activeTag = document.activeElement?.tagName;
const isFormControl =
activeTag === "INPUT" ||
activeTag === "BUTTON" ||
activeTag === "SELECT" ||
activeTag === "TEXTAREA";
if (event.code === "Space" && !isFormControl) {
event.preventDefault();
toggleMotion();
}
});
function updateParticles(time) {
for (let i = 0; i < particleCount; i++) {
const data = particleData[i];
const lane = lanes[data.lane % lanes.length];
if (!lane) continue;
const outboundProgress = (time * 0.11 + data.phase) % 1;
const inboundProgress = (time * 0.095 + data.phase + 0.38) % 1;
const vesselEdge = lane.side > 0 ? vesselTop : vesselBottom;
const wobbleX =
Math.sin(outboundProgress * Math.PI * 5 + i) * 0.065 +
data.wobble;
const wobbleZ =
Math.cos(outboundProgress * Math.PI * 4 + i * 0.7) * 0.075;
dummy.position.set(
lane.x + wobbleX,
THREE.MathUtils.lerp(vesselEdge, lane.y, outboundProgress),
lane.z + wobbleZ
);
dummy.rotation.set(0, 0, 0);
dummy.scale.setScalar(0.9 + 0.12 * Math.sin(i + time * 2));
dummy.updateMatrix();
outboundMesh.setMatrixAt(i, dummy.matrix);
dummy.position.set(
lane.x - wobbleX,
THREE.MathUtils.lerp(lane.y, vesselEdge, inboundProgress),
lane.z - wobbleZ
);
dummy.rotation.set(0, 0, 0);
dummy.scale.setScalar(0.9 + 0.12 * Math.cos(i + time * 2));
dummy.updateMatrix();
inboundMesh.setMatrixAt(i, dummy.matrix);
}
outboundMesh.instanceMatrix.needsUpdate = true;
inboundMesh.instanceMatrix.needsUpdate = true;
}
function updateFlow(time) {
for (let i = 0; i < flowCount; i++) {
const x = ((i / flowCount) * 9.5 + time * 0.72) % 9.5 - 4.75;
dummy.position.set(x, vesselCenterY, 0);
dummy.rotation.set(0, 0, 0);
dummy.scale.set(1, 1, 1);
dummy.updateMatrix();
flowMesh.setMatrixAt(i, dummy.matrix);
}
flowMesh.instanceMatrix.needsUpdate = true;
}
function placePin(element, worldPosition) {
const projected = worldPosition.clone().project(camera);
const visible = projected.z > -1 && projected.z < 1;
element.style.display = visible ? "flex" : "none";
if (!visible) return;
const x = (projected.x * 0.5 + 0.5) * stage.clientWidth;
const y = (-projected.y * 0.5 + 0.5) * stage.clientHeight;
element.style.left = `${x}px`;
element.style.top = `${y}px`;
}
const vesselPinPosition = new THREE.Vector3(-4.65, 0.82, 0);
const tissuePinPosition = new THREE.Vector3();
const spacingPinPosition = new THREE.Vector3();
function updatePins() {
const upperLane = lanes.find((lane) => lane.side > 0);
if (upperLane) {
tissuePinPosition.set(upperLane.x, upperLane.y + 0.54, upperLane.z);
}
const gapWorld = spacingMicrons * 0.022;
spacingPinPosition.set(
-5.12,
vesselTop + gapWorld * 0.5,
0.42
);
placePin(document.getElementById("vesselPin"), vesselPinPosition);
placePin(document.getElementById("tissuePin"), tissuePinPosition);
placePin(document.getElementById("spacingPin"), spacingPinPosition);
}
renderer.setAnimationLoop(() => {
const delta = Math.min(clock.getDelta(), 0.05);
if (running) {
elapsed += delta;
}
updateParticles(elapsed);
updateFlow(elapsed);
updatePins();
renderer.render(scene, camera);
loading.classList.add("hidden");
});
</script>
</body>
</html>
```