Prompt specification
Build or customize the Three.js 3D scene "Four-chamber flow-route 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: Four-chamber flow-route model
Category: Biology & Nature · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/four-chamber-flow-route-model-x0993
Scene Description Prompt:
Make a cutaway Three.js scene in a self-contained HTML file using cdnjs: four-chamber flow-route model. Create a simplified four-chamber heart with directional connections to lung and body loops; label oxygenation convention clearly and avoid implying blue blood inside the body.
Working Three.js Starter (four-chamber-flow-route-model-x0993):
```html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Four-Chamber Heart Flow — Cutaway Model</title>
<style>
:root {
color-scheme: dark;
--background: #0d1717;
--panel: rgba(17, 29, 29, 0.88);
--border: rgba(220, 237, 232, 0.16);
--text: #edf5f1;
--muted: #aebeb9;
--oxygen-poor: #f0b44d;
--oxygen-rich: #ff6669;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: var(--background);
color: var(--text);
font-family:
Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
#viewport,
#leaders,
#labels {
position: fixed;
inset: 0;
}
#viewport {
z-index: 0;
}
#viewport canvas {
display: block;
width: 100%;
height: 100%;
outline: none;
touch-action: none;
cursor: grab;
}
#viewport canvas:active {
cursor: grabbing;
}
#leaders {
z-index: 1;
width: 100%;
height: 100%;
pointer-events: none;
}
#labels {
z-index: 2;
pointer-events: none;
}
.panel {
position: fixed;
z-index: 4;
border: 1px solid var(--border);
border-radius: 14px;
background: var(--panel);
box-shadow: 0 16px 40px rgba(0, 0, 0, 0.22);
backdrop-filter: blur(12px);
}
header {
top: 22px;
left: 22px;
width: min(360px, calc(100vw - 44px));
padding: 18px 20px;
pointer-events: none;
}
.eyebrow {
margin-bottom: 6px;
color: #8ec8b5;
font-size: 11px;
font-weight: 800;
letter-spacing: 0.13em;
text-transform: uppercase;
}
h1 {
margin: 0;
font-size: clamp(22px, 2.4vw, 31px);
line-height: 1.08;
letter-spacing: -0.025em;
}
header p {
margin: 9px 0 0;
color: var(--muted);
font-size: 13px;
line-height: 1.45;
}
#legend {
bottom: 22px;
left: 22px;
width: min(390px, calc(100vw - 44px));
padding: 15px 17px;
pointer-events: none;
}
.legend-title {
margin-bottom: 10px;
color: #8ec8b5;
font-size: 10px;
font-weight: 800;
letter-spacing: 0.13em;
text-transform: uppercase;
}
.legend-row {
display: flex;
align-items: flex-start;
gap: 9px;
margin: 7px 0;
color: #d8e3df;
font-size: 12px;
line-height: 1.35;
}
.swatch {
flex: 0 0 auto;
width: 17px;
height: 8px;
margin-top: 3px;
border-radius: 999px;
box-shadow: 0 0 12px currentColor;
}
.swatch.poor {
color: var(--oxygen-poor);
background: var(--oxygen-poor);
}
.swatch.rich {
color: var(--oxygen-rich);
background: var(--oxygen-rich);
}
.note {
margin-top: 10px;
padding-top: 9px;
border-top: 1px solid var(--border);
color: var(--muted);
font-size: 11px;
line-height: 1.45;
}
.route-summary {
margin-top: 9px;
color: #edf5f1;
font-size: 11px;
font-weight: 700;
line-height: 1.45;
}
.route-summary span {
color: #8ec8b5;
}
#controls {
position: fixed;
right: 22px;
bottom: 22px;
z-index: 5;
display: flex;
align-items: center;
gap: 12px;
}
#hint {
padding: 9px 12px;
border: 1px solid var(--border);
border-radius: 10px;
background: rgba(17, 29, 29, 0.82);
color: var(--muted);
font-size: 11px;
backdrop-filter: blur(10px);
pointer-events: none;
}
button {
border: 1px solid rgba(142, 200, 181, 0.38);
border-radius: 10px;
padding: 10px 13px;
background: #18312e;
color: #e7f4ef;
font: inherit;
font-size: 12px;
font-weight: 700;
cursor: pointer;
}
button:hover {
background: #21413c;
border-color: rgba(142, 200, 181, 0.72);
}
button:focus-visible,
canvas:focus-visible {
outline: 2px solid #8ec8b5;
outline-offset: 3px;
}
.flow-label {
--accent: #d9e5e0;
position: absolute;
min-width: 116px;
padding: 7px 9px;
transform: translate(-50%, -50%);
border: 1px solid color-mix(in srgb, var(--accent) 48%, transparent);
border-radius: 8px;
background: rgba(12, 23, 23, 0.86);
box-shadow: 0 6px 18px rgba(0, 0, 0, 0.22);
color: #edf5f1;
text-align: center;
backdrop-filter: blur(7px);
white-space: nowrap;
}
.flow-label.chamber {
min-width: 104px;
padding: 6px 8px;
}
.flow-label.organ {
min-width: 156px;
white-space: normal;
}
.flow-label .name {
display: block;
font-size: 9px;
font-weight: 850;
letter-spacing: 0.055em;
line-height: 1.25;
text-transform: uppercase;
}
.flow-label .detail {
display: block;
margin-top: 3px;
color: #b7c5c1;
font-size: 9px;
line-height: 1.3;
}
.flow-label .oxygen {
display: flex;
align-items: center;
justify-content: center;
gap: 5px;
margin-top: 4px;
color: var(--accent);
font-size: 9px;
font-weight: 750;
}
.flow-label .dot {
display: inline-block;
width: 6px;
height: 6px;
border-radius: 50%;
background: var(--accent);
box-shadow: 0 0 8px var(--accent);
}
@media (max-width: 700px) {
header {
top: 10px;
left: 10px;
width: calc(100vw - 20px);
padding: 13px 15px;
}
header p {
font-size: 11px;
}
#legend {
left: 10px;
bottom: 52px;
width: calc(100vw - 20px);
padding: 11px 13px;
}
.legend-title,
.route-summary,
.note {
font-size: 10px;
}
.legend-row {
margin: 4px 0;
font-size: 10px;
}
#controls {
right: 10px;
bottom: 10px;
}
#hint {
display: none;
}
.flow-label {
min-width: 96px;
padding: 5px 6px;
}
.flow-label.chamber {
min-width: 86px;
}
.flow-label.organ {
min-width: 126px;
}
.flow-label .name {
font-size: 8px;
}
.flow-label .detail,
.flow-label .oxygen {
font-size: 8px;
}
}
</style>
</head>
<body>
<div id="viewport"></div>
<svg id="leaders" aria-hidden="true"></svg>
<div id="labels" aria-hidden="true"></div>
<header class="panel">
<div class="eyebrow">Interactive anatomical schematic</div>
<h1>Four-chamber flow route</h1>
<p>
The front heart wall is removed for a cutaway view.
Arrows and moving markers show the direction of blood flow.
</p>
</header>
<section id="legend" class="panel" aria-label="Oxygenation legend">
<div class="legend-title">Oxygenation convention</div>
<div class="legend-row">
<span class="swatch poor"></span>
<span><strong>Oxygen-poor</strong> — systemic venous blood and pulmonary arterial blood</span>
</div>
<div class="legend-row">
<span class="swatch rich"></span>
<span><strong>Oxygen-rich</strong> — pulmonary venous blood and systemic arterial blood</span>
</div>
<div class="note">
These are schematic oxygenation colors, not literal blood color.
Blue is intentionally not used for blood. Organ tissue is shown neutrally.
Arteries carry blood away from the heart; veins carry blood toward it.
</div>
<div class="route-summary">
Body → RA → RV → <span>Lungs</span> → LA → LV → Body
</div>
</section>
<div id="controls">
<div id="hint">Drag to orbit · Scroll to zoom · Double-click to reset</div>
<button id="reset" type="button">Reset view</button>
</div>
<script type="module">
import * as THREE from "https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const viewport = document.getElementById("viewport");
const labelLayer = document.getElementById("labels");
const leaderLayer = document.getElementById("leaders");
const OXYGEN_POOR = 0xf0b44d;
const OXYGEN_RICH = 0xff6669;
const TISSUE = 0xf0d0c9;
const ORGAN_TISSUE = 0xa99b94;
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x0d1717);
const renderer = new THREE.WebGLRenderer({
antialias: true,
alpha: false,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.12;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
viewport.appendChild(renderer.domElement);
renderer.domElement.tabIndex = 0;
renderer.domElement.setAttribute(
"aria-label",
"Interactive three-dimensional cutaway of the four-chamber heart"
);
const camera = new THREE.OrthographicCamera(-7, 7, 4, -4, 0.1, 100);
const target = new THREE.Vector3(0, 0, 0);
let yaw = 0.025;
let pitch = 0.035;
let cameraDistance = 16;
scene.add(new THREE.HemisphereLight(0xf7fff9, 0x26302e, 2.1));
const keyLight = new THREE.DirectionalLight(0xfff8ef, 3.1);
keyLight.position.set(4, 7, 9);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(1024, 1024);
keyLight.shadow.camera.left = -8;
keyLight.shadow.camera.right = 8;
keyLight.shadow.camera.top = 6;
keyLight.shadow.camera.bottom = -6;
scene.add(keyLight);
const fillLight = new THREE.DirectionalLight(0xbfe9dc, 1.25);
fillLight.position.set(-6, -2, 5);
scene.add(fillLight);
const rimLight = new THREE.DirectionalLight(0xffd1bd, 1.8);
rimLight.position.set(2, 4, -5);
scene.add(rimLight);
const model = new THREE.Group();
scene.add(model);
// A restrained halo helps separate the cutaway from the background.
const halo = new THREE.Mesh(
new THREE.CircleGeometry(3.15, 96),
new THREE.MeshBasicMaterial({
color: 0x17302e,
transparent: true,
opacity: 0.45,
depthWrite: false
})
);
halo.position.set(0, 0.05, -1.75);
model.add(halo);
const haloRing = new THREE.Mesh(
new THREE.TorusGeometry(3.05, 0.018, 8, 128),
new THREE.MeshBasicMaterial({
color: 0x416c64,
transparent: true,
opacity: 0.42,
depthWrite: false
})
);
haloRing.position.copy(halo.position);
model.add(haloRing);
const shellMaterial = new THREE.MeshPhysicalMaterial({
color: 0xf5d8d1,
roughness: 0.3,
metalness: 0,
clearcoat: 0.25,
transparent: true,
opacity: 0.42,
side: THREE.DoubleSide,
depthWrite: false
});
const rimMaterial = new THREE.MeshStandardMaterial({
color: 0xffeadf,
roughness: 0.34,
metalness: 0,
emissive: 0x4a2822,
emissiveIntensity: 0.12
});
const septumMaterial = new THREE.MeshStandardMaterial({
color: 0xe8beb6,
roughness: 0.55,
transparent: true,
opacity: 0.74,
depthWrite: false
});
const poorChamberMaterial = new THREE.MeshStandardMaterial({
color: 0xc98b2f,
roughness: 0.48,
metalness: 0,
emissive: 0x6d3d05,
emissiveIntensity: 0.16,
transparent: true,
opacity: 0.78,
side: THREE.BackSide,
depthWrite: false
});
const richChamberMaterial = new THREE.MeshStandardMaterial({
color: 0xd65357,
roughness: 0.48,
metalness: 0,
emissive: 0x721b20,
emissiveIntensity: 0.16,
transparent: true,
opacity: 0.78,
side: THREE.BackSide,
depthWrite: false
});
/*
A partial sphere retains the rear half of each chamber.
The omitted front half creates the cutaway, while the torus marks
the cut boundary.
*/
const chamberGeometry = new THREE.SphereGeometry(
1,
48,
32,
Math.PI,
Math.PI,
0,
Math.PI
);
function makeChamber(name, position, scale, oxygenation) {
const group = new THREE.Group();
group.position.set(...position);
const cavity = new THREE.Mesh(
chamberGeometry,
oxygenation === "poor" ? poorChamberMaterial : richChamberMaterial
);
cavity.scale.set(scale[0] * 0.87, scale[1] * 0.87, scale[2] * 0.87);
cavity.renderOrder = 1;
group.add(cavity);
const wall = new THREE.Mesh(chamberGeometry, shellMaterial);
wall.scale.set(...scale);
wall.renderOrder = 3;
group.add(wall);
const cutRim = new THREE.Mesh(
new THREE.TorusGeometry(1, 0.028, 10, 80),
rimMaterial
);
cutRim.scale.set(scale[0], scale[1], 1);
cutRim.position.z = 0.035;
cutRim.renderOrder = 4;
group.add(cutRim);
const innerRim = new THREE.Mesh(
new THREE.TorusGeometry(1, 0.017, 8, 72),
new THREE.MeshStandardMaterial({
color: oxygenation === "poor" ? 0xffd27a : 0xff9295,
roughness: 0.4,
emissive: oxygenation === "poor" ? 0x6d4308 : 0x741f22,
emissiveIntensity: 0.25
})
);
innerRim.scale.set(scale[0] * 0.87, scale[1] * 0.87, 1);
innerRim.position.z = 0.045;
innerRim.renderOrder = 4;
group.add(innerRim);
model.add(group);
return group;
}
// Anatomical right chambers appear on the viewer's left.
makeChamber("right atrium", [-1.15, 1.25, 0], [0.92, 0.82, 0.78], "poor");
makeChamber("right ventricle", [-1.08, -1.05, 0], [1.03, 1.08, 0.82], "poor");
// Anatomical left chambers appear on the viewer's right.
makeChamber("left atrium", [1.12, 1.25, 0], [0.82, 0.75, 0.75], "rich");
makeChamber("left ventricle", [1.08, -1.0, 0], [1.1, 1.12, 0.86], "rich");
const septum = new THREE.Mesh(
new THREE.SphereGeometry(1, 32, 24),
septumMaterial
);
septum.scale.set(0.12, 2.3, 0.46);
septum.position.set(0, 0.05, -0.25);
septum.renderOrder = 2;
model.add(septum);
const avGroove = new THREE.Mesh(
new THREE.SphereGeometry(1, 36, 20),
septumMaterial
);
avGroove.scale.set(1.72, 0.1, 0.58);
avGroove.position.set(0, 0.35, -0.25);
avGroove.renderOrder = 2;
model.add(avGroove);
function makeValve(x, y) {
const valve = new THREE.Mesh(
new THREE.TorusGeometry(0.29, 0.035, 8, 36),
rimMaterial
);
valve.position.set(x, y, 0.29);
valve.scale.set(1, 0.55, 1);
valve.renderOrder = 5;
model.add(valve);
for (const side of [-1, 1]) {
const leaflet = new THREE.Mesh(
new THREE.SphereGeometry(1, 18, 10),
rimMaterial
);
leaflet.scale.set(0.19, 0.055, 0.035);
leaflet.position.set(x + side * 0.13, y, 0.29);
leaflet.rotation.z = side * 0.3;
leaflet.renderOrder = 5;
model.add(leaflet);
}
}
makeValve(-1.16, 0.36);
makeValve(1.11, 0.36);
const organMaterial = new THREE.MeshStandardMaterial({
color: ORGAN_TISSUE,
roughness: 0.7,
metalness: 0
});
const organAccent = new THREE.MeshStandardMaterial({
color: 0xc1b3aa,
roughness: 0.62,
metalness: 0
});
function cylinderBetween(a, b, radius, material, parent) {
const start = new THREE.Vector3(...a);
const end = new THREE.Vector3(...b);
const direction = end.clone().sub(start);
const midpoint = start.clone().add(end).multiplyScalar(0.5);
const mesh = new THREE.Mesh(
new THREE.CylinderGeometry(radius, radius, direction.length(), 14),
material
);
mesh.position.copy(midpoint);
mesh.quaternion.setFromUnitVectors(
new THREE.Vector3(0, 1, 0),
direction.normalize()
);
parent.add(mesh);
return mesh;
}
// Neutral body/torso icon.
const bodyGroup = new THREE.Group();
bodyGroup.position.set(-4.65, -0.05, -0.08);
model.add(bodyGroup);
const torso = new THREE.Mesh(
new THREE.SphereGeometry(1, 32, 24),
organMaterial
);
torso.scale.set(0.67, 1.08, 0.37);
torso.castShadow = true;
bodyGroup.add(torso);
const head = new THREE.Mesh(
new THREE.SphereGeometry(1, 28, 20),
organAccent
);
head.scale.set(0.39, 0.43, 0.37);
head.position.set(0, 1.63, 0);
head.castShadow = true;
bodyGroup.add(head);
cylinderBetween([0, 1.05, 0], [0, 1.3, 0], 0.18, organAccent, bodyGroup);
cylinderBetween([-0.48, 0.48, 0], [-0.8, -0.42, 0], 0.12, organMaterial, bodyGroup);
cylinderBetween([0.48, 0.48, 0], [0.8, -0.42, 0], 0.12, organMaterial, bodyGroup);
const pelvis = new THREE.Mesh(
new THREE.SphereGeometry(1, 24, 16),
organMaterial
);
pelvis.scale.set(0.45, 0.32, 0.34);
pelvis.position.set(0, -1.0, 0);
bodyGroup.add(pelvis);
cylinderBetween([-0.24, -0.95, 0], [-0.32, -1.6, 0], 0.12, organMaterial, bodyGroup);
cylinderBetween([0.24, -0.95, 0], [0.32, -1.6, 0], 0.12, organMaterial, bodyGroup);
// A small neutral capillary motif inside the torso.
const capillaryMaterial = new THREE.MeshBasicMaterial({
color: 0xd3c8bf,
transparent: true,
opacity: 0.65
});
const capillaryPoints = [
[-0.38, 0.52, 0.36], [-0.12, 0.25, 0.39],
[0.18, 0.38, 0.39], [0.4, 0.08, 0.37],
[-0.3, -0.1, 0.39], [0.03, -0.43, 0.38],
[0.34, -0.62, 0.35]
];
for (let i = 0; i < capillaryPoints.length - 1; i++) {
cylinderBetween(
capillaryPoints[i],
capillaryPoints[i + 1],
0.018,
capillaryMaterial,
bodyGroup
);
}
// Neutral lung icon.
const lungGroup = new THREE.Group();
lungGroup.position.set(4.65, 0.12, -0.08);
model.add(lungGroup);
const leftLobe = new THREE.Mesh(
new THREE.SphereGeometry(1, 32, 24),
organMaterial
);
leftLobe.scale.set(0.56, 1.05, 0.38);
leftLobe.position.set(-0.5, 0.12, 0);
leftLobe.castShadow = true;
lungGroup.add(leftLobe);
const rightLobe = new THREE.Mesh(
new THREE.SphereGeometry(1, 32, 24),
organMaterial
);
rightLobe.scale.set(0.56, 1.05, 0.38);
rightLobe.position.set(0.5, 0.12, 0);
rightLobe.castShadow = true;
lungGroup.add(rightLobe);
cylinderBetween([0, 1.55, 0], [0, 0.52, 0], 0.13, organAccent, lungGroup);
cylinderBetween([0, 0.65, 0], [-0.52, 0.15, 0], 0.09, organAccent, lungGroup);
cylinderBetween([0, 0.65, 0], [0.52, 0.15, 0], 0.09, organAccent, lungGroup);
const routes = [];
const particles = [];
const routeOuterMaterial = new THREE.MeshStandardMaterial({
color: 0xf2ded7,
roughness: 0.48,
transparent: true,
opacity: 0.27,
depthWrite: false
});
function makeRoute(points, color, options = {}) {
const {
outer = true,
arrowTimes = [0.5],
particleCount = 2,
speed = 0.12
} = options;
const vectors = points.map((point) => new THREE.Vector3(...point));
const curve = new THREE.CatmullRomCurve3(
vectors,
false,
"catmullrom",
0.45
);
if (outer) {
const halo = new THREE.Mesh(
new THREE.TubeGeometry(curve, 100, 0.145, 10, false),
routeOuterMaterial
);
halo.renderOrder = 2;
model.add(halo);
}
const colorMaterial = new THREE.MeshStandardMaterial({
color,
roughness: 0.35,
metalness: 0,
emissive: color,
emissiveIntensity: 0.16
});
const vessel = new THREE.Mesh(
new THREE.TubeGeometry(curve, 100, 0.076, 12, false),
colorMaterial
);
vessel.renderOrder = 3;
model.add(vessel);
const arrowGeometry = new THREE.ConeGeometry(0.13, 0.33, 18);
const up = new THREE.Vector3(0, 1, 0);
for (const time of arrowTimes) {
const arrow = new THREE.Mesh(arrowGeometry, colorMaterial);
arrow.position.copy(curve.getPointAt(time));
arrow.quaternion.setFromUnitVectors(
up,
curve.getTangentAt(time).normalize()
);
arrow.renderOrder = 4;
model.add(arrow);
}
const particleGeometry = new THREE.SphereGeometry(0.085, 14, 10);
const particleMaterial = new THREE.MeshStandardMaterial({
color,
emissive: color,
emissiveIntensity: 1.1,
roughness: 0.25
});
for (let i = 0; i < particleCount; i++) {
const particle = new THREE.Mesh(
particleGeometry,
particleMaterial
);
particle.renderOrder = 5;
model.add(particle);
particles.push({
mesh: particle,
curve,
offset: i / particleCount + 0.06 * i
});
}
routes.push({ curve, speed });
return curve;
}
// Systemic venous return: body → right atrium.
makeRoute(
[
[-3.88, 0.25, 0.4],
[-3.15, 0.5, 0.4],
[-2.45, 0.88, 0.4],
[-1.63, 1.25, 0.4]
],
OXYGEN_POOR,
{ arrowTimes: [0.4, 0.78], speed: 0.13 }
);
// Right atrium → right ventricle.
makeRoute(
[
[-1.16, 0.78, 0.68],
[-1.16, 0.38, 0.68],
[-1.15, -0.1, 0.68],
[-1.08, -0.54, 0.68]
],
OXYGEN_POOR,
{ outer: false, arrowTimes: [0.55], particleCount: 1, speed: 0.16 }
);
// Pulmonary artery: right ventricle → lungs.
makeRoute(
[
[-1.25, -1.62, 0.32],
[-1.55, -2.3, 0.32],
[-0.45, -2.78, 0.32],
[1.55, -2.65, 0.32],
[3.05, -1.75, 0.32],
[3.95, -0.55, 0.32]
],
OXYGEN_POOR,
{ arrowTimes: [0.25, 0.58, 0.86], speed: 0.105 }
);
// Pulmonary veins: lungs → left atrium.
makeRoute(
[
[4.0, 0.78, 0.42],
[3.1, 1.18, 0.42],
[2.2, 1.38, 0.42],
[1.62, 1.3, 0.42]
],
OXYGEN_RICH,
{ arrowTimes: [0.42, 0.76], speed: 0.13 }
);
// Left atrium → left ventricle.
makeRoute(
[
[1.12, 0.78, 0.68],
[1.12, 0.38, 0.68],
[1.1, -0.1, 0.68],
[1.08, -0.54, 0.68]
],
OXYGEN_RICH,
{ outer: false, arrowTimes: [0.55], particleCount: 1, speed: 0.16 }
);
// Aorta / systemic arteries: left ventricle → body.
makeRoute(
[
[1.2, -1.55, 0.3],
[1.7, -0.55, 0.3],
[2.05, 0.25, 0.3],
[2.4, 1.75, 0.3],
[1.45, 2.55, 0.3],
[-1.7, 2.5, 0.3],
[-3.25, 1.65, 0.3],
[-3.85, 0.72, 0.3]
],
OXYGEN_RICH,
{ arrowTimes: [0.22, 0.53, 0.82], speed: 0.095 }
);
// Labels are HTML projected from world-space anchors.
const labels = [];
function addLabel({
name,
detail = "",
oxygen = "",
anchor,
offset = [0, 0],
kind = "",
accent = "#d9e5e0",
leader = false
}) {
const element = document.createElement("div");
element.className = `flow-label ${kind}`;
element.style.setProperty("--accent", accent);
const nameElement = document.createElement("span");
nameElement.className = "name";
nameElement.textContent = name;
element.appendChild(nameElement);
if (detail) {
const detailElement = document.createElement("span");
detailElement.className = "detail";
detailElement.textContent = detail;
element.appendChild(detailElement);
}
if (oxygen) {
const oxygenElement = document.createElement("span");
oxygenElement.className = "oxygen";
const dot = document.createElement("span");
dot.className = "dot";
oxygenElement.appendChild(dot);
oxygenElement.appendChild(
document.createTextNode(oxygen)
);
element.appendChild(oxygenElement);
}
labelLayer.appendChild(element);
let line = null;
if (leader) {
line = document.createElementNS(
"http://www.w3.org/2000/svg",
"line"
);
line.setAttribute("stroke", accent);
line.setAttribute("stroke-width", "1");
line.setAttribute("stroke-opacity", "0.62");
line.setAttribute("stroke-dasharray", "3 4");
leaderLayer.appendChild(line);
}
labels.push({
element,
line,
anchor: new THREE.Vector3(...anchor),
offset
});
}
addLabel({
name: "Right atrium",
oxygen: "O₂-poor",
anchor: [-1.15, 1.3, 0.8],
kind: "chamber",
accent: "#f0b44d"
});
addLabel({
name: "Right ventricle",
oxygen: "O₂-poor",
anchor: [-1.08, -1.05, 0.8],
kind: "chamber",
accent: "#f0b44d"
});
addLabel({
name: "Left atrium",
oxygen: "O₂-rich",
anchor: [1.12, 1.3, 0.8],
kind: "chamber",
accent: "#ff6669"
});
addLabel({
name: "Left ventricle",
oxygen: "O₂-rich",
anchor: [1.08, -1.0, 0.8],
kind: "chamber",
accent: "#ff6669"
});
addLabel({
name: "Body · systemic capillaries",
detail: "O₂ delivered here; O₂-poor blood returns",
anchor: [-4.65, 0.55, 0.4],
offset: [-12, -94],
kind: "organ",
accent: "#d9e5e0",
leader: true
});
addLabel({
name: "Lungs · pulmonary capillaries",
detail: "O₂ enters blood here; O₂-rich blood returns",
anchor: [4.65, 0.65, 0.4],
offset: [18, -96],
kind: "organ",
accent: "#d9e5e0",
leader: true
});
addLabel({
name: "Vena cava",
detail: "O₂-poor · toward heart",
anchor: [-2.55, 0.67, 0.5],
offset: [-22, -45],
accent: "#f0b44d",
leader: true
});
addLabel({
name: "Pulmonary artery",
detail: "O₂-poor · to lungs",
anchor: [-0.2, -2.73, 0.5],
offset: [0, 35],
accent: "#f0b44d",
leader: true
});
addLabel({
name: "Pulmonary veins",
detail: "O₂-rich · to heart",
anchor: [2.82, 1.27, 0.5],
offset: [36, -53],
accent: "#ff6669",
leader: true
});
addLabel({
name: "Aorta · systemic arteries",
detail: "O₂-rich · to body",
anchor: [0.8, 2.54, 0.5],
offset: [0, -32],
accent: "#ff6669",
leader: true
});
function updateLabels() {
const width = window.innerWidth;
const height = window.innerHeight;
for (const label of labels) {
const projected = label.anchor.clone().project(camera);
const anchorX = (projected.x * 0.5 + 0.5) * width;
const anchorY = (-projected.y * 0.5 + 0.5) * height;
const halfWidth = label.element.offsetWidth / 2;
const halfHeight = label.element.offsetHeight / 2;
let labelX = anchorX + label.offset[0];
let labelY = anchorY + label.offset[1];
labelX = THREE.MathUtils.clamp(
labelX,
halfWidth + 8,
width - halfWidth - 8
);
labelY = THREE.MathUtils.clamp(
labelY,
halfHeight + 8,
height - halfHeight - 8
);
label.element.style.left = `${labelX}px`;
label.element.style.top = `${labelY}px`;
if (label.line) {
label.line.setAttribute("x1", anchorX);
label.line.setAttribute("y1", anchorY);
label.line.setAttribute("x2", labelX);
label.line.setAttribute("y2", labelY);
}
}
}
function updateCamera() {
const horizontal = Math.cos(pitch) * cameraDistance;
camera.position.set(
Math.sin(yaw) * horizontal,
Math.sin(pitch) * cameraDistance + 0.15,
Math.cos(yaw) * horizontal
);
camera.lookAt(target);
camera.updateMatrixWorld();
}
function resize() {
const width = window.innerWidth;
const height = window.innerHeight;
const aspect = width / height;
const baseHeight = 8.2;
const minimumWidth = 12.8;
const viewWidth = Math.max(minimumWidth, baseHeight * aspect);
const viewHeight = viewWidth / aspect;
camera.left = -viewWidth / 2;
camera.right = viewWidth / 2;
camera.top = viewHeight / 2;
camera.bottom = -viewHeight / 2;
camera.updateProjectionMatrix();
renderer.setSize(width, height);
}
function resetView() {
yaw = 0.025;
pitch = 0.035;
camera.zoom = 1;
camera.updateProjectionMatrix();
updateCamera();
}
let pointerActive = false;
let pointerX = 0;
let pointerY = 0;
renderer.domElement.addEventListener("pointerdown", (event) => {
pointerActive = true;
pointerX = event.clientX;
pointerY = event.clientY;
renderer.domElement.setPointerCapture(event.pointerId);
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (!pointerActive) return;
const deltaX = event.clientX - pointerX;
const deltaY = event.clientY - pointerY;
pointerX = event.clientX;
pointerY = event.clientY;
yaw = THREE.MathUtils.clamp(
yaw - deltaX * 0.0045,
-0.48,
0.48
);
pitch = THREE.MathUtils.clamp(
pitch + deltaY * 0.003,
-0.2,
0.28
);
updateCamera();
});
function endPointer(event) {
pointerActive = false;
if (
renderer.domElement.hasPointerCapture &&
renderer.domElement.hasPointerCapture(event.pointerId)
) {
renderer.domElement.releasePointerCapture(event.pointerId);
}
}
renderer.domElement.addEventListener("pointerup", endPointer);
renderer.domElement.addEventListener("pointercancel", endPointer);
renderer.domElement.addEventListener(
"wheel",
(event) => {
event.preventDefault();
camera.zoom = THREE.MathUtils.clamp(
camera.zoom * Math.exp(-event.deltaY * 0.001),
0.72,
1.65
);
camera.updateProjectionMatrix();
},
{ passive: false }
);
renderer.domElement.addEventListener("dblclick", resetView);
renderer.domElement.addEventListener("keydown", (event) => {
const step = 0.045;
if (event.key === "ArrowLeft") {
yaw = THREE.MathUtils.clamp(yaw - step, -0.48, 0.48);
} else if (event.key === "ArrowRight") {
yaw = THREE.MathUtils.clamp(yaw + step, -0.48, 0.48);
} else if (event.key === "ArrowUp") {
pitch = THREE.MathUtils.clamp(pitch + step, -0.2, 0.28);
} else if (event.key === "ArrowDown") {
pitch = THREE.MathUtils.clamp(pitch - step, -0.2, 0.28);
} else if (event.key === "Home") {
resetView();
} else {
return;
}
event.preventDefault();
updateCamera();
});
document.getElementById("reset").addEventListener("click", resetView);
window.addEventListener("resize", resize);
const reducedMotion = window.matchMedia(
"(prefers-reduced-motion: reduce)"
).matches;
resize();
updateCamera();
const clock = new THREE.Clock();
function animate() {
requestAnimationFrame(animate);
const elapsed = clock.getElapsedTime();
if (!reducedMotion) {
for (const particle of particles) {
const t = (elapsed * (particle.curve.userData?.speed ?? 1)) % 1;
// Each particle uses the speed stored on its route.
// The fallback expression above keeps the animation valid if a
// route does not provide explicit speed metadata.
particle.mesh.position.copy(particle.curve.getPointAt(t));
}
// Apply each route's own speed to its particles.
let routeIndex = 0;
for (const route of routes) {
const routeParticles = particles.filter(
(particle) => particle.curve === route.curve
);
for (const particle of routeParticles) {
const t =
(elapsed * route.speed + particle.offset) % 1;
particle.mesh.position.copy(route.curve.getPointAt(t));
}
routeIndex++;
}
}
updateLabels();
renderer.render(scene, camera);
}
animate();
</script>
</body>
</html>
```