Prompt specification
Build or customize the Three.js 3D scene "Capillary bed branching model" 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: Capillary bed branching model
Category: Biology & Nature · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/capillary-bed-branching-model-x0965
Scene Description Prompt:
Create a single self-contained HTML file using Three.js from cdnjs that shows a capillary network between an arteriole and a venule. Model a small arteriole dividing into narrow vessels and joining a venule around tissue cells; show a continuous network and label vessel sizes exaggerated. A 3D cutaway view.
Working Three.js Starter (capillary-bed-branching-model-x0965):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>3D Capillary Network — Cutaway View</title>
<style>
:root {
color-scheme: dark;
--panel: rgba(8, 18, 31, 0.78);
--panel-border: rgba(174, 218, 255, 0.18);
--text: #eef7ff;
--muted: #9fb6c9;
--arterial: #ff5968;
--transition: #b25bd2;
--venous: #4c83ff;
--accent: #66d9cf;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background:
radial-gradient(circle at 48% 42%, #172d40 0%, #0a1827 42%, #050b13 100%);
font-family:
Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
#app,
#scene-container,
#labels {
position: absolute;
inset: 0;
}
#scene-container canvas {
display: block;
width: 100%;
height: 100%;
outline: none;
cursor: grab;
touch-action: none;
}
#scene-container canvas:active {
cursor: grabbing;
}
#labels {
pointer-events: none;
overflow: hidden;
transition: opacity 180ms ease;
}
#labels.hidden {
opacity: 0;
}
.intro {
position: absolute;
top: 24px;
left: 26px;
width: min(360px, calc(100vw - 52px));
padding: 17px 19px;
border: 1px solid var(--panel-border);
border-radius: 16px;
background: linear-gradient(
145deg,
rgba(13, 31, 48, 0.9),
rgba(7, 17, 29, 0.7)
);
box-shadow: 0 16px 50px rgba(0, 0, 0, 0.24);
backdrop-filter: blur(14px);
pointer-events: none;
}
.eyebrow {
margin-bottom: 7px;
color: var(--accent);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.19em;
text-transform: uppercase;
}
h1 {
margin: 0;
font-size: clamp(20px, 2.2vw, 29px);
font-weight: 680;
letter-spacing: -0.035em;
line-height: 1.05;
}
.intro p {
margin: 10px 0 0;
color: var(--muted);
font-size: 12px;
line-height: 1.55;
}
.toolbar {
position: absolute;
top: 24px;
right: 26px;
display: flex;
gap: 8px;
}
.toolbar button {
min-width: 42px;
height: 39px;
padding: 0 13px;
border: 1px solid var(--panel-border);
border-radius: 11px;
color: var(--text);
background: var(--panel);
font: inherit;
font-size: 11px;
font-weight: 650;
letter-spacing: 0.02em;
cursor: pointer;
backdrop-filter: blur(12px);
transition:
background 160ms ease,
border-color 160ms ease,
transform 160ms ease;
}
.toolbar button:hover {
border-color: rgba(102, 217, 207, 0.52);
background: rgba(20, 48, 64, 0.9);
transform: translateY(-1px);
}
.toolbar button.active {
border-color: rgba(102, 217, 207, 0.7);
color: #dffffb;
background: rgba(28, 86, 88, 0.72);
}
.scene-label {
position: absolute;
min-width: 116px;
padding: 9px 11px;
border: 1px solid rgba(180, 222, 255, 0.24);
border-radius: 11px;
color: var(--text);
background: rgba(6, 17, 29, 0.84);
box-shadow: 0 8px 24px rgba(0, 0, 0, 0.25);
backdrop-filter: blur(10px);
transform: translate(-50%, -50%);
white-space: nowrap;
}
.scene-label::after {
content: "";
position: absolute;
top: 100%;
left: 50%;
width: 1px;
height: 17px;
background: linear-gradient(
to bottom,
rgba(190, 231, 255, 0.8),
rgba(190, 231, 255, 0.05)
);
}
.scene-label .label-title {
display: flex;
align-items: center;
gap: 7px;
font-size: 11px;
font-weight: 760;
letter-spacing: 0.015em;
}
.scene-label .label-title::before {
content: "";
width: 7px;
height: 7px;
flex: 0 0 auto;
border-radius: 50%;
background: var(--dot, #fff);
box-shadow: 0 0 11px var(--dot, #fff);
}
.scene-label .label-detail {
margin-top: 4px;
color: var(--muted);
font-size: 9px;
letter-spacing: 0.025em;
}
.legend {
position: absolute;
bottom: 24px;
left: 26px;
width: min(350px, calc(100vw - 52px));
padding: 14px 16px;
border: 1px solid var(--panel-border);
border-radius: 14px;
background: var(--panel);
box-shadow: 0 14px 40px rgba(0, 0, 0, 0.22);
backdrop-filter: blur(14px);
pointer-events: none;
}
.legend-title {
display: flex;
justify-content: space-between;
align-items: center;
margin-bottom: 10px;
color: #dceaf5;
font-size: 10px;
font-weight: 800;
letter-spacing: 0.12em;
text-transform: uppercase;
}
.legend-title span:last-child {
color: var(--accent);
font-size: 9px;
letter-spacing: 0.05em;
}
.gradient {
height: 7px;
overflow: hidden;
border-radius: 99px;
background: linear-gradient(
90deg,
var(--arterial),
var(--transition),
var(--venous)
);
box-shadow: 0 0 14px rgba(178, 91, 210, 0.18);
}
.legend-scale {
display: flex;
justify-content: space-between;
margin-top: 7px;
color: var(--muted);
font-size: 9px;
}
.note {
margin-top: 10px;
color: #8299ac;
font-size: 9px;
line-height: 1.45;
}
.hint {
position: absolute;
right: 26px;
bottom: 26px;
padding: 9px 12px;
border: 1px solid rgba(174, 218, 255, 0.12);
border-radius: 10px;
color: #8fa8bb;
background: rgba(6, 16, 27, 0.58);
font-size: 9px;
letter-spacing: 0.025em;
backdrop-filter: blur(10px);
pointer-events: none;
}
#loading {
position: absolute;
inset: 0;
z-index: 20;
display: grid;
place-items: center;
color: #cce8f5;
background: #091522;
font-size: 12px;
letter-spacing: 0.08em;
transition: opacity 350ms ease;
}
#loading.done {
opacity: 0;
pointer-events: none;
}
@media (max-width: 700px) {
.intro {
top: 14px;
left: 14px;
width: calc(100vw - 112px);
padding: 13px 14px;
}
.intro p {
display: none;
}
h1 {
font-size: 18px;
}
.toolbar {
top: 14px;
right: 14px;
}
.toolbar button {
width: 38px;
padding: 0;
font-size: 0;
}
.toolbar button::first-letter {
font-size: 13px;
}
.legend {
bottom: 14px;
left: 14px;
width: calc(100vw - 28px);
}
.hint {
display: none;
}
.scene-label {
min-width: 100px;
padding: 7px 9px;
}
.scene-label .label-detail {
display: none;
}
}
</style>
</head>
<body>
<div id="app">
<div id="scene-container"></div>
<div id="labels"></div>
<section class="intro">
<div class="eyebrow">Microcirculation · 3D Cutaway</div>
<h1>Capillary Exchange Network</h1>
<p>
An arteriole branches into narrow capillary loops between tissue cells,
then the vessels reunite into a venule. Vessel diameters are
deliberately exaggerated for visibility.
</p>
</section>
<nav class="toolbar" aria-label="Scene controls">
<button id="reset-view" title="Reset camera view" aria-label="Reset camera view">
↺
</button>
<button id="toggle-labels" class="active" title="Toggle labels" aria-label="Toggle labels">
Labels
</button>
</nav>
<aside class="legend">
<div class="legend-title">
<span>Blood oxygenation</span>
<span>Flow direction →</span>
</div>
<div class="gradient"></div>
<div class="legend-scale">
<span>Oxygen-rich</span>
<span>Exchange</span>
<span>Oxygen-poor</span>
</div>
<div class="note">
Cutaway matrix and enlarged vessel diameters reveal the continuous
arterial–capillary–venous pathway.
</div>
</aside>
<div class="hint">Drag to orbit · Scroll to zoom · Double-click to reset</div>
<div id="loading">Building vascular microarchitecture…</div>
</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const container = document.getElementById("scene-container");
const labelLayer = document.getElementById("labels");
const loading = document.getElementById("loading");
// ---------------------------------------------------------------------
// Renderer, scene, camera, and lighting
// ---------------------------------------------------------------------
const renderer = new THREE.WebGLRenderer({
antialias: true,
alpha: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.15;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.setClearColor(0x000000, 0);
container.appendChild(renderer.domElement);
const scene = new THREE.Scene();
scene.fog = new THREE.FogExp2(0x091522, 0.022);
const camera = new THREE.PerspectiveCamera(
38,
window.innerWidth / window.innerHeight,
0.1,
100
);
const cameraState = {
target: new THREE.Vector3(0, 0, 0.45),
radius: 17,
theta: 0.55,
phi: 0.88,
targetRadius: 17,
targetTheta: 0.55,
targetPhi: 0.88
};
function updateCamera() {
const { radius, theta, phi, target } = cameraState;
const sinPhi = Math.sin(phi);
camera.position.set(
target.x + radius * sinPhi * Math.sin(theta),
target.y + radius * sinPhi * Math.cos(theta),
target.z + radius * Math.cos(phi)
);
camera.lookAt(target);
}
updateCamera();
scene.add(new THREE.HemisphereLight(0xc8eaff, 0x271b30, 2.0));
const keyLight = new THREE.DirectionalLight(0xffffff, 2.8);
keyLight.position.set(3, 7, 10);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(1024, 1024);
keyLight.shadow.camera.left = -11;
keyLight.shadow.camera.right = 11;
keyLight.shadow.camera.top = 9;
keyLight.shadow.camera.bottom = -9;
keyLight.shadow.camera.near = 1;
keyLight.shadow.camera.far = 30;
scene.add(keyLight);
const redLight = new THREE.PointLight(0xff4665, 18, 18, 2);
redLight.position.set(-6, 1.5, 5);
scene.add(redLight);
const blueLight = new THREE.PointLight(0x477aff, 19, 18, 2);
blueLight.position.set(6, 1.2, 5);
scene.add(blueLight);
const rimLight = new THREE.PointLight(0x7cebe0, 10, 15, 2);
rimLight.position.set(0, -4, 4);
scene.add(rimLight);
// ---------------------------------------------------------------------
// Cutaway tissue bed
// ---------------------------------------------------------------------
const tissueGroup = new THREE.Group();
scene.add(tissueGroup);
const tissueShape = new THREE.Shape();
tissueShape.moveTo(-8, -3.4);
tissueShape.lineTo(-1, -3.4);
tissueShape.lineTo(-1, -1.05);
tissueShape.lineTo(3, -1.05);
tissueShape.lineTo(3, -3.4);
tissueShape.lineTo(8, -3.4);
tissueShape.lineTo(8, 3.4);
tissueShape.lineTo(-8, 3.4);
tissueShape.closePath();
const tissueGeometry = new THREE.ExtrudeGeometry(tissueShape, {
depth: 0.88,
bevelEnabled: true,
bevelSegments: 3,
bevelSize: 0.11,
bevelThickness: 0.08,
curveSegments: 4,
steps: 1
});
const tissueTopMaterial = new THREE.MeshStandardMaterial({
color: 0xd6b99b,
roughness: 0.83,
metalness: 0,
transparent: true,
opacity: 0.34,
depthWrite: true
});
const tissueSideMaterial = new THREE.MeshStandardMaterial({
color: 0xa77d69,
roughness: 0.9,
transparent: true,
opacity: 0.54,
depthWrite: true
});
const tissue = new THREE.Mesh(tissueGeometry, [
tissueTopMaterial,
tissueSideMaterial
]);
tissue.position.z = -0.5;
tissue.receiveShadow = true;
tissueGroup.add(tissue);
// Highlight the exposed cut faces of the removed rectangular window.
const cutEdgePoints = [
new THREE.Vector3(-1, -3.35, 0.42),
new THREE.Vector3(-1, -1.05, 0.42),
new THREE.Vector3(3, -1.05, 0.42),
new THREE.Vector3(3, -3.35, 0.42)
];
const cutEdgeGeometry = new THREE.BufferGeometry().setFromPoints(cutEdgePoints);
const cutEdge = new THREE.Line(
cutEdgeGeometry,
new THREE.LineBasicMaterial({
color: 0xffd49b,
transparent: true,
opacity: 0.78
})
);
tissueGroup.add(cutEdge);
// Faint matrix fibers in the tissue bed.
const fiberMaterial = new THREE.LineBasicMaterial({
color: 0xf7d6b3,
transparent: true,
opacity: 0.16
});
for (let i = 0; i < 13; i++) {
const y = -3 + (i / 12) * 6;
const points = [];
for (let j = 0; j <= 28; j++) {
const x = -7.8 + (j / 28) * 15.6;
points.push(
new THREE.Vector3(
x,
y + Math.sin(j * 0.62 + i * 1.41) * 0.18,
0.43 + Math.cos(j * 0.4 + i) * 0.025
)
);
}
const fiber = new THREE.Line(
new THREE.BufferGeometry().setFromPoints(points),
fiberMaterial
);
tissueGroup.add(fiber);
}
// ---------------------------------------------------------------------
// Tissue cells
// ---------------------------------------------------------------------
const cellGroup = new THREE.Group();
scene.add(cellGroup);
const membraneMaterials = [
new THREE.MeshPhysicalMaterial({
color: 0xf5a89b,
roughness: 0.48,
clearcoat: 0.45,
clearcoatRoughness: 0.35,
transparent: true,
opacity: 0.43,
depthWrite: false
}),
new THREE.MeshPhysicalMaterial({
color: 0xf3c0a4,
roughness: 0.55,
clearcoat: 0.35,
transparent: true,
opacity: 0.36,
depthWrite: false
}),
new THREE.MeshPhysicalMaterial({
color: 0xdca4b5,
roughness: 0.5,
clearcoat: 0.4,
transparent: true,
opacity: 0.38,
depthWrite: false
})
];
const nucleusMaterial = new THREE.MeshStandardMaterial({
color: 0x713b65,
roughness: 0.52,
transparent: true,
opacity: 0.78
});
const cellPositions = [
[-6.2, 2.55, 0.66, 0.63],
[-4.7, 2.70, 0.58, 0.48],
[-3.0, 2.55, 0.54, 0.60],
[-1.2, 2.65, 0.64, 0.52],
[0.7, 2.55, 0.58, 0.59],
[2.6, 2.60, 0.62, 0.50],
[4.7, 2.60, 0.59, 0.60],
[6.5, 2.15, 0.63, 0.51],
[-5.2, -1.35, 0.58, 0.47],
[-3.4, -2.48, 0.64, 0.54],
[-1.7, -2.55, 0.59, 0.57],
[0.2, -2.60, 0.62, 0.52],
[2.0, -2.55, 0.58, 0.61],
[4.6, -2.30, 0.63, 0.52],
[6.1, -1.70, 0.57, 0.60],
[-2.3, 0.92, 0.37, 0.31],
[-0.25, 1.05, 0.38, 0.33],
[1.15, 0.92, 0.36, 0.31]
];
function createCell(position, scaleX, scaleY, index) {
const geometry = new THREE.IcosahedronGeometry(1, 2);
const positions = geometry.attributes.position;
const seed = index * 1.713;
for (let i = 0; i < positions.count; i++) {
const x = positions.getX(i);
const y = positions.getY(i);
const z = positions.getZ(i);
const noise =
1 +
0.11 * Math.sin(x * 7.3 + y * 4.1 + z * 5.7 + seed) +
0.06 * Math.cos(x * 3.8 - y * 8.2 + z * 2.4 + seed);
positions.setXYZ(i, x * noise, y * noise, z * noise);
}
geometry.computeVertexNormals();
const cell = new THREE.Mesh(
geometry,
membraneMaterials[index % membraneMaterials.length]
);
cell.position.set(position[0], position[1], position[2]);
cell.scale.set(scaleX, scaleY, scaleX * 0.64);
cell.rotation.set(
(index % 3) * 0.14,
(index % 5) * 0.17,
(index % 4) * 0.12
);
cellGroup.add(cell);
const nucleus = new THREE.Mesh(
new THREE.SphereGeometry(0.29, 20, 14),
nucleusMaterial
);
nucleus.scale.set(1, 0.82, 0.64);
nucleus.position.set(
cell.position.x + Math.sin(index) * 0.08,
cell.position.y + Math.cos(index * 0.7) * 0.07,
cell.position.z + 0.08
);
cellGroup.add(nucleus);
}
cellPositions.forEach(([x, y, scaleX, scaleY], index) => createCell([x, y, 0], scaleX, scaleY, index));
// ---------------------------------------------------------------------
// Vascular geometry
// ---------------------------------------------------------------------
const vesselGroup = new THREE.Group();
scene.add(vesselGroup);
const arterialJunction = new THREE.Vector3(-4.1, 0.25, 0.98);
const venousJunction = new THREE.Vector3(4.1, 0.25, 0.98);
const arterioleCurve = new THREE.CatmullRomCurve3([
new THREE.Vector3(-8.8, 1.85, 0.86),
new THREE.Vector3(-7.25, 1.65, 0.90),
new THREE.Vector3(-5.75, 0.83, 0.95),
arterialJunction.clone()
]);
const venuleCurve = new THREE.CatmullRomCurve3([
venousJunction.clone(),
new THREE.Vector3(5.25, 0.68, 0.98),
new THREE.Vector3(6.45, 1.24, 0.91),
new THREE.Vector3(8.8, 1.62, 0.85)
]);
const capillaryCurves = [
new THREE.CatmullRomCurve3([
arterialJunction.clone(),
new THREE.Vector3(-3.50, 1.18, 1.03),
new THREE.Vector3(-2.58, 2.12, 1.00),
new THREE.Vector3(-1.08, 1.76, 1.06),
new THREE.Vector3(0.28, 2.00, 1.00),
new THREE.Vector3(1.62, 1.20, 1.04),
new THREE.Vector3(2.72, 0.96, 1.00),
venousJunction.clone()
]),
new THREE.CatmullRomCurve3([
arterialJunction.clone(),
new THREE.Vector3(-3.12, 0.22, 1.10),
new THREE.Vector3(-1.78, 0.32, 1.02),
new THREE.Vector3(-0.48, -0.10, 1.10),
new THREE.Vector3(1.02, 0.26, 1.02),
new THREE.Vector3(2.42, 0.30, 1.00),
venousJunction.clone()
]),
new THREE.CatmullRomCurve3([
arterialJunction.clone(),
new THREE.Vector3(-3.42, -0.78, 1.03),
new THREE.Vector3(-2.22, -1.48, 1.00),
new THREE.Vector3(-0.52, -1.18, 1.07),
new THREE.Vector3(0.72, -1.58, 1.00),
new THREE.Vector3(2.10, -0.82, 1.02),
new THREE.Vector3(3.30, -0.20, 1.00),
venousJunction.clone()
])
];
const arterialMaterial = new THREE.MeshPhysicalMaterial({
color: 0xe84357,
emissive: 0x7d1025,
emissiveIntensity: 0.34,
roughness: 0.28,
metalness: 0.02,
clearcoat: 0.45,
clearcoatRoughness: 0.28
});
const venousMaterial = new THREE.MeshPhysicalMaterial({
color: 0x3e78ef,
emissive: 0x102c84,
emissiveIntensity: 0.38,
roughness: 0.3,
metalness: 0.02,
clearcoat: 0.45,
clearcoatRoughness: 0.3
});
const artery = new THREE.Mesh(
new THREE.TubeGeometry(arterioleCurve, 90, 0.27, 16, false),
arterialMaterial
);
artery.castShadow = true;
vesselGroup.add(artery);
const vein = new THREE.Mesh(
new THREE.TubeGeometry(venuleCurve, 90, 0.35, 16, false),
venousMaterial
);
vein.castShadow = true;
vesselGroup.add(vein);
const red = new THREE.Color(0xff5364);
const purple = new THREE.Color(0xb259cc);
const blue = new THREE.Color(0x4a82ff);
function capillaryColor(t) {
if (t < 0.53) {
return red.clone().lerp(purple, t / 0.53);
}
return purple.clone().lerp(blue, (t - 0.53) / 0.47);
}
function createGradientTube(curve, radius, segments = 150) {
const geometry = new THREE.TubeGeometry(
curve,
segments,
radius,
12,
false
);
const uv = geometry.attributes.uv;
const colors = new Float32Array(geometry.attributes.position.count * 3);
for (let i = 0; i < geometry.attributes.position.count; i++) {
const color = capillaryColor(uv.getX(i));
colors[i * 3] = color.r;
colors[i * 3 + 1] = color.g;
colors[i * 3 + 2] = color.b;
}
geometry.setAttribute(
"color",
new THREE.BufferAttribute(colors, 3)
);
const material = new THREE.MeshStandardMaterial({
color: 0xffffff,
vertexColors: true,
emissive: 0x32122f,
emissiveIntensity: 0.27,
roughness: 0.31,
metalness: 0.01
});
const mesh = new THREE.Mesh(geometry, material);
mesh.castShadow = true;
vesselGroup.add(mesh);
return mesh;
}
capillaryCurves.forEach((curve) => {
createGradientTube(curve, 0.105, 180);
});
// Cross-connections make the capillary region a continuous network
// rather than three visually isolated parallel tubes.
const connectorMaterial = new THREE.MeshStandardMaterial({
color: 0xa75bc4,
emissive: 0x35123f,
emissiveIntensity: 0.25,
roughness: 0.34
});
function addConnector(curveA, curveB, tA, tB, lift) {
const a = curveA.getPointAt(tA);
const b = curveB.getPointAt(tB);
const middle = a.clone().lerp(b, 0.5);
middle.z += lift;
middle.y += lift * 0.18;
const connector = new THREE.CatmullRomCurve3([
a,
middle,
b
]);
const mesh = new THREE.Mesh(
new THREE.TubeGeometry(connector, 48, 0.075, 10, false),
connectorMaterial
);
mesh.castShadow = true;
vesselGroup.add(mesh);
}
addConnector(capillaryCurves[0], capillaryCurves[1], 0.43, 0.49, 0.25);
addConnector(capillaryCurves[1], capillaryCurves[2], 0.54, 0.48, 0.22);
addConnector(capillaryCurves[0], capillaryCurves[2], 0.69, 0.63, 0.26);
// Junctions visually weld the arterial and venous tubes to the
// capillary branches.
const arterialNode = new THREE.Mesh(
new THREE.SphereGeometry(0.19, 24, 16),
arterialMaterial
);
arterialNode.position.copy(arterialJunction);
arterialNode.castShadow = true;
vesselGroup.add(arterialNode);
const venousNode = new THREE.Mesh(
new THREE.SphereGeometry(0.22, 24, 16),
venousMaterial
);
venousNode.position.copy(venousJunction);
venousNode.castShadow = true;
vesselGroup.add(venousNode);
// ---------------------------------------------------------------------
// Flowing blood cells
// ---------------------------------------------------------------------
const flowParticles = [];
function addFlow(curve, count, speed, radius, colorMode) {
for (let i = 0; i < count; i++) {
let color;
if (colorMode === "arterial") {
color = new THREE.Color(0xffa3a9);
} else if (colorMode === "venous") {
color = new THREE.Color(0x91b7ff);
} else {
color = capillaryColor(i / count);
}
const particle = new THREE.Mesh(
new THREE.SphereGeometry(radius, 10, 8),
new THREE.MeshBasicMaterial({ color })
);
vesselGroup.add(particle);
flowParticles.push({
mesh: particle,
curve,
offset: i / count,
speed
});
}
}
addFlow(arterioleCurve, 4, 0.11, 0.062, "arterial");
addFlow(venuleCurve, 4, 0.105, 0.072, "venous");
capillaryCurves.forEach((curve, index) => {
addFlow(curve, 5, 0.075 + index * 0.004, 0.041, "gradient");
});
// Small directional arrowheads.
function addFlowArrow(curve, t, color) {
const position = curve.getPointAt(t);
const tangent = curve.getTangentAt(t).normalize();
const arrow = new THREE.Mesh(
new THREE.ConeGeometry(0.105, 0.31, 16),
new THREE.MeshBasicMaterial({ color })
);
arrow.position.copy(position);
arrow.quaternion.setFromUnitVectors(
new THREE.Vector3(0, 1, 0),
tangent
);
vesselGroup.add(arrow);
}
addFlowArrow(arterioleCurve, 0.56, 0xffb0b8);
addFlowArrow(venuleCurve, 0.48, 0xa8c5ff);
// ---------------------------------------------------------------------
// Projected HTML labels
// ---------------------------------------------------------------------
const labels = [];
function createLabel(title, detail, anchor, color, offset) {
const element = document.createElement("div");
element.className = "scene-label";
element.style.setProperty("--dot", color);
element.innerHTML = `
<div class="label-title">${title}</div>
<div class="label-detail">${detail}</div>
`;
labelLayer.appendChild(element);
labels.push({
element,
anchor: new THREE.Vector3(...anchor),
offset
});
}
createLabel(
"Arteriole",
"≈ 10 μm · enlarged",
[-6.35, 1.18, 1.02],
"#ff5968",
[-44, -30]
);
createLabel(
"Capillary bed",
"≈ 5–8 μm · enlarged",
[-0.65, 1.65, 1.12],
"#c16ad7",
[0, -45]
);
createLabel(
"Venule",
"≈ 15–20 μm · enlarged",
[6.4, 1.08, 1.02],
"#4c83ff",
[50, -25]
);
createLabel(
"Tissue cells",
"nutrient and gas exchange",
[6.1, -1.7, 0.82],
"#f1a9a0",
[45, 25]
);
createLabel(
"Cutaway tissue",
"matrix removed for clarity",
[1.0, -1.05, 0.45],
"#ffd49b",
[0, 35]
);
const projected = new THREE.Vector3();
function updateLabels() {
const width = window.innerWidth;
const height = window.innerHeight;
labels.forEach((label) => {
projected.copy(label.anchor).project(camera);
const x = (projected.x * 0.5 + 0.5) * width + label.offset[0];
const y = (-projected.y * 0.5 + 0.5) * height + label.offset[1];
label.element.style.left = `${x}px`;
label.element.style.top = `${y}px`;
label.element.style.visibility =
projected.z > 1 ? "hidden" : "visible";
});
}
// ---------------------------------------------------------------------
// Custom camera controls — no external controls addon
// ---------------------------------------------------------------------
const pointers = new Map();
let previousPinchDistance = 0;
let lastPointer = { x: 0, y: 0 };
renderer.domElement.addEventListener("pointerdown", (event) => {
renderer.domElement.setPointerCapture(event.pointerId);
pointers.set(event.pointerId, {
x: event.clientX,
y: event.clientY
});
lastPointer = {
x: event.clientX,
y: event.clientY
};
if (pointers.size === 2) {
const [a, b] = [...pointers.values()];
previousPinchDistance = Math.hypot(a.x - b.x, a.y - b.y);
}
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (!pointers.has(event.pointerId)) return;
const old = pointers.get(event.pointerId);
pointers.set(event.pointerId, {
x: event.clientX,
y: event.clientY
});
if (pointers.size === 1) {
const dx = event.clientX - lastPointer.x;
const dy = event.clientY - lastPointer.y;
cameraState.targetTheta -= dx * 0.006;
cameraState.targetPhi = THREE.MathUtils.clamp(
cameraState.targetPhi - dy * 0.005,
0.32,
1.38
);
lastPointer = {
x: event.clientX,
y: event.clientY
};
} else if (pointers.size === 2) {
const [a, b] = [...pointers.values()];
const distance = Math.hypot(a.x - b.x, a.y - b.y);
if (previousPinchDistance > 0) {
const scale = previousPinchDistance / distance;
cameraState.targetRadius = THREE.MathUtils.clamp(
cameraState.targetRadius * scale,
9,
25
);
}
previousPinchDistance = distance;
}
});
function endPointer(event) {
pointers.delete(event.pointerId);
previousPinchDistance = 0;
}
renderer.domElement.addEventListener("pointerup", endPointer);
renderer.domElement.addEventListener("pointercancel", endPointer);
renderer.domElement.addEventListener(
"wheel",
(event) => {
event.preventDefault();
cameraState.targetRadius = THREE.MathUtils.clamp(
cameraState.targetRadius * Math.exp(event.deltaY * 0.001),
9,
25
);
},
{ passive: false }
);
function resetView() {
cameraState.targetRadius = 17;
cameraState.targetTheta = 0.55;
cameraState.targetPhi = 0.88;
cameraState.target.set(0, 0, 0.45);
}
renderer.domElement.addEventListener("dblclick", resetView);
document.getElementById("reset-view").addEventListener("click", resetView);
const toggleButton = document.getElementById("toggle-labels");
toggleButton.addEventListener("click", () => {
const hidden = labelLayer.classList.toggle("hidden");
toggleButton.classList.toggle("active", !hidden);
});
// ---------------------------------------------------------------------
// Resize and animation
// ---------------------------------------------------------------------
function resize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
}
window.addEventListener("resize", resize);
const clock = new THREE.Clock();
let elapsed = 0;
function animate() {
const delta = Math.min(clock.getDelta(), 0.05);
elapsed += delta;
const damping = 1 - Math.pow(0.001, delta);
cameraState.theta +=
(cameraState.targetTheta - cameraState.theta) * damping;
cameraState.phi +=
(cameraState.targetPhi - cameraState.phi) * damping;
cameraState.radius +=
(cameraState.targetRadius - cameraState.radius) * damping;
updateCamera();
updateLabels();
flowParticles.forEach((particle) => {
const t = (particle.offset + elapsed * particle.speed) % 1;
particle.mesh.position.copy(particle.curve.getPointAt(t));
const pulse = 1 + Math.sin(elapsed * 4.5 + particle.offset * 11) * 0.08;
particle.mesh.scale.setScalar(pulse);
});
const pulse = 0.27 + Math.sin(elapsed * 2.2) * 0.045;
capillaryCurves.forEach((curve) => {
// The meshes are stored consecutively after the artery and vein.
const index = vesselGroup.children.indexOf(
vesselGroup.children.find(
(child) => child.geometry?.type === "TubeGeometry"
)
);
});
vesselGroup.children.forEach((child) => {
if (
child.material?.vertexColors &&
child.material.emissiveIntensity !== undefined
) {
child.material.emissiveIntensity = pulse;
}
});
renderer.render(scene, camera);
requestAnimationFrame(animate);
}
animate();
requestAnimationFrame(() => {
loading.classList.add("done");
setTimeout(() => loading.remove(), 450);
});
</script>
</body>
</html>
```