Prompt specification
Build or customize the Three.js 3D scene "Circulation route tracing puzzle" 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: Circulation route tracing puzzle
Category: Biology & Nature · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/circulation-route-tracing-puzzle-x0996
Scene Description Prompt:
Let viewers follow a tagged particle from a body vein through heart, lungs, and body artery; verify chamber order and clearly state that the visualization is schematic. Build this with Three.js from cdnjs as a low-poly scene in one HTML file.
Working Three.js Starter (circulation-route-tracing-puzzle-x0996):
```html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Cardiopulmonary Particle Route</title>
<style>
:root {
--ink: #17332d;
--muted: #627771;
--panel: rgba(251, 253, 251, 0.9);
--line: rgba(28, 67, 57, 0.14);
--blue: #3478c8;
--red: #d85255;
--yellow: #ffd45a;
--active: #fff3bf;
}
* { box-sizing: border-box; }
html, body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #edf4f0;
color: var(--ink);
font-family:
Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
canvas {
display: block;
touch-action: none;
}
#scene, #labels {
position: fixed;
inset: 0;
}
#labels {
pointer-events: none;
overflow: hidden;
}
#labels.hidden {
display: none;
}
.panel {
background: var(--panel);
border: 1px solid rgba(255, 255, 255, 0.85);
box-shadow: 0 18px 50px rgba(30, 61, 52, 0.13);
backdrop-filter: blur(14px);
-webkit-backdrop-filter: blur(14px);
}
#title-card {
position: fixed;
top: 18px;
left: 18px;
width: min(400px, calc(100vw - 36px));
padding: 18px 19px 16px;
border-radius: 18px;
z-index: 5;
}
.eyebrow {
margin-bottom: 7px;
color: #4e766a;
font-size: 11px;
font-weight: 800;
letter-spacing: 0.14em;
text-transform: uppercase;
}
h1 {
margin: 0;
font-size: clamp(23px, 3vw, 31px);
line-height: 1.05;
letter-spacing: -0.035em;
}
.subtitle {
margin: 9px 0 13px;
color: var(--muted);
font-size: 13px;
line-height: 1.45;
}
.schematic-badge {
display: inline-flex;
align-items: center;
gap: 7px;
padding: 7px 10px;
border: 1px solid #e0b84d;
border-radius: 999px;
background: #fff5cc;
color: #725400;
font-size: 11px;
font-weight: 850;
letter-spacing: 0.08em;
}
.schematic-badge::before {
width: 7px;
height: 7px;
border-radius: 50%;
background: #e2ae16;
content: "";
box-shadow: 0 0 0 4px rgba(226, 174, 22, 0.14);
}
#controls {
position: fixed;
top: 18px;
right: 18px;
z-index: 6;
display: flex;
flex-wrap: wrap;
justify-content: flex-end;
gap: 7px;
max-width: 430px;
}
button {
appearance: none;
border: 1px solid var(--line);
border-radius: 11px;
padding: 9px 12px;
background: rgba(251, 253, 251, 0.93);
color: var(--ink);
font: inherit;
font-size: 12px;
font-weight: 750;
cursor: pointer;
box-shadow: 0 7px 22px rgba(30, 61, 52, 0.09);
}
button:hover {
background: #fff;
transform: translateY(-1px);
}
button:focus-visible {
outline: 3px solid rgba(52, 120, 200, 0.3);
outline-offset: 2px;
}
#sequence-panel {
position: fixed;
left: 18px;
bottom: 18px;
z-index: 5;
width: 330px;
max-height: calc(100vh - 245px);
overflow: auto;
padding: 15px;
border-radius: 18px;
}
.panel-heading {
display: flex;
align-items: center;
justify-content: space-between;
gap: 10px;
margin-bottom: 10px;
}
.panel-heading h2 {
margin: 0;
font-size: 14px;
letter-spacing: -0.01em;
}
.check {
color: #28745d;
font-size: 12px;
font-weight: 850;
white-space: nowrap;
}
.order-check {
padding: 11px 12px;
border: 1px solid rgba(45, 120, 94, 0.18);
border-radius: 12px;
background: rgba(223, 243, 232, 0.72);
}
.order-check .label {
color: #527168;
font-size: 10px;
font-weight: 800;
letter-spacing: 0.09em;
text-transform: uppercase;
}
.order-check strong {
display: block;
margin: 4px 0 3px;
font-size: 17px;
letter-spacing: 0.02em;
}
.order-check small {
color: #58756c;
font-size: 11px;
}
#route-list {
list-style: none;
margin: 10px 0 0;
padding: 0;
}
#route-list li {
position: relative;
display: grid;
grid-template-columns: 29px 1fr;
gap: 8px;
align-items: start;
margin: 2px 0;
padding: 7px 8px;
border: 1px solid transparent;
border-radius: 10px;
transition: background 160ms ease, border-color 160ms ease;
}
#route-list li::after {
position: absolute;
top: 31px;
bottom: -3px;
left: 22px;
width: 1px;
background: #d7e1dd;
content: "";
}
#route-list li:last-child::after {
display: none;
}
#route-list li.active {
border-color: rgba(221, 172, 27, 0.45);
background: var(--active);
}
.step-number {
display: grid;
width: 27px;
height: 27px;
place-items: center;
border-radius: 50%;
background: #e8efec;
color: #60756e;
font-size: 10px;
font-weight: 850;
}
#route-list li.active .step-number {
background: #e9b72d;
color: #fff;
}
.step-name {
display: block;
font-size: 12px;
font-weight: 800;
}
.step-detail {
display: block;
margin-top: 2px;
color: var(--muted);
font-size: 10px;
line-height: 1.35;
}
.fine-print {
margin: 12px 2px 1px;
padding-top: 11px;
border-top: 1px solid var(--line);
color: #657870;
font-size: 10px;
line-height: 1.45;
}
.fine-print strong {
color: #475f57;
}
#status {
position: fixed;
right: 18px;
bottom: 18px;
z-index: 5;
width: min(365px, calc(100vw - 36px));
overflow: hidden;
padding: 13px 15px 15px;
border-radius: 16px;
}
.status-top {
display: flex;
align-items: center;
gap: 9px;
}
#status-index {
color: #8b6c11;
font-size: 11px;
font-weight: 900;
letter-spacing: 0.08em;
}
#status-name {
font-size: 14px;
font-weight: 850;
}
#status-detail {
margin-top: 5px;
color: var(--muted);
font-size: 11px;
line-height: 1.4;
}
.progress-track {
height: 4px;
margin-top: 11px;
overflow: hidden;
border-radius: 999px;
background: #e1e9e5;
}
#progress {
width: 0;
height: 100%;
border-radius: inherit;
background: linear-gradient(90deg, #3478c8, #d85255 54%, #ffd45a);
}
.world-label {
position: absolute;
z-index: 3;
padding: 5px 8px;
transform: translate(-50%, -50%);
border: 1px solid rgba(255, 255, 255, 0.9);
border-radius: 8px;
background: rgba(251, 253, 251, 0.86);
box-shadow: 0 5px 15px rgba(22, 48, 41, 0.11);
color: #29483f;
font-size: 10px;
font-weight: 850;
letter-spacing: 0.03em;
white-space: nowrap;
}
.world-label.blue {
border-color: rgba(52, 120, 200, 0.38);
color: #245c9d;
}
.world-label.red {
border-color: rgba(216, 82, 85, 0.38);
color: #a43d42;
}
#particle-tag {
position: absolute;
z-index: 4;
transform: translate(13px, -32px);
padding: 6px 9px;
border: 2px solid #ffe17b;
border-radius: 8px;
background: #263d36;
box-shadow: 0 6px 20px rgba(22, 42, 36, 0.25);
color: #fff4b9;
font-size: 9px;
font-weight: 900;
letter-spacing: 0.09em;
white-space: nowrap;
}
#particle-tag::before {
position: absolute;
top: 50%;
left: -8px;
width: 6px;
height: 6px;
transform: translateY(-50%) rotate(45deg);
border-bottom: 2px solid #ffe17b;
border-left: 2px solid #ffe17b;
background: #263d36;
content: "";
}
.legend {
position: fixed;
right: 20px;
top: 80px;
z-index: 4;
display: flex;
gap: 12px;
padding: 8px 10px;
border-radius: 999px;
background: rgba(251, 253, 251, 0.78);
color: #587068;
font-size: 10px;
font-weight: 700;
}
.legend span {
display: flex;
align-items: center;
gap: 5px;
}
.dot {
width: 8px;
height: 8px;
border-radius: 50%;
}
.dot.blue { background: var(--blue); }
.dot.red { background: var(--red); }
.dot.gold { background: var(--yellow); }
@media (max-height: 760px) and (min-width: 700px) {
#sequence-panel {
max-height: calc(100vh - 190px);
padding: 12px;
}
#route-list li {
padding-top: 5px;
padding-bottom: 5px;
}
.step-detail {
display: none;
}
.fine-print {
font-size: 9px;
}
}
@media (max-width: 700px) {
#title-card {
top: 10px;
left: 10px;
width: calc(100vw - 20px);
padding: 13px 14px;
}
.subtitle {
margin: 7px 0 10px;
}
#controls {
top: 132px;
right: 10px;
max-width: calc(100vw - 20px);
}
button {
padding: 8px 10px;
font-size: 11px;
}
.legend {
display: none;
}
#sequence-panel {
left: 10px;
bottom: 92px;
width: min(310px, calc(100vw - 20px));
max-height: 38vh;
padding: 11px;
}
.order-check {
padding: 8px 10px;
}
.order-check strong {
font-size: 14px;
}
.fine-print {
display: none;
}
#status {
right: 10px;
bottom: 10px;
width: calc(100vw - 20px);
padding: 10px 12px 12px;
}
#status-detail {
display: none;
}
.world-label {
padding: 4px 6px;
font-size: 8px;
}
}
</style>
</head>
<body>
<div id="scene"></div>
<div id="labels">
<div id="particle-tag">TAGGED PARTICLE</div>
</div>
<header id="title-card" class="panel">
<div class="eyebrow">Low-poly circulation study</div>
<h1>One particle.<br>One complete circuit.</h1>
<p class="subtitle">
Follow a tagged blood particle through the venous system, both sides of
the heart, the pulmonary circuit, and the systemic circulation.
</p>
<div class="schematic-badge">SCHEMATIC — NOT TO SCALE</div>
</header>
<nav id="controls" aria-label="Animation controls">
<button id="play-button" type="button">Pause</button>
<button id="restart-button" type="button">Restart particle</button>
<button id="labels-button" type="button" aria-pressed="true">Hide labels</button>
<button id="view-button" type="button">Reset view</button>
</nav>
<div class="legend" aria-hidden="true">
<span><i class="dot blue"></i>Deoxygenated</span>
<span><i class="dot red"></i>Oxygenated</span>
<span><i class="dot gold"></i>Tracked particle</span>
</div>
<aside id="sequence-panel" class="panel">
<div class="panel-heading">
<h2>Verified flow route</h2>
<span class="check">✓ Order checked</span>
</div>
<div class="order-check">
<span class="label">Chamber order</span>
<strong>RA → RV → LA → LV</strong>
<small>Right heart → lungs → left heart</small>
</div>
<ol id="route-list"></ol>
<p class="fine-print">
<strong>Schematic only.</strong> Sizes, spacing, colors, and branching are
simplified. The particle follows one representative route through the
right lung; real pulmonary vessels branch to both lungs. In the default
view, the patient’s right is on the viewer’s left.
</p>
</aside>
<section id="status" class="panel" aria-live="polite">
<div class="status-top">
<span id="status-index">01 / 10</span>
<span id="status-name">Body vein</span>
</div>
<div id="status-detail">Venous blood returns toward the right atrium.</div>
<div class="progress-track"><div id="progress"></div></div>
</section>
<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, and lighting
// ------------------------------------------------------------
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xedf4f0);
scene.fog = new THREE.Fog(0xedf4f0, 16, 31);
const renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.08;
document.getElementById("scene").appendChild(renderer.domElement);
const camera = new THREE.PerspectiveCamera(
42,
window.innerWidth / window.innerHeight,
0.1,
100
);
const ambient = new THREE.HemisphereLight(0xffffff, 0x71877c, 2.2);
scene.add(ambient);
const keyLight = new THREE.DirectionalLight(0xfff6e8, 3.1);
keyLight.position.set(5, 8, 8);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(2048, 2048);
keyLight.shadow.camera.left = -7;
keyLight.shadow.camera.right = 7;
keyLight.shadow.camera.top = 7;
keyLight.shadow.camera.bottom = -7;
keyLight.shadow.bias = -0.0005;
scene.add(keyLight);
const rimLight = new THREE.DirectionalLight(0x9dc7ff, 1.4);
rimLight.position.set(-6, 3, -5);
scene.add(rimLight);
const ground = new THREE.Mesh(
new THREE.CircleGeometry(7, 32),
new THREE.MeshStandardMaterial({
color: 0xdce9e2,
roughness: 1,
metalness: 0
})
);
ground.rotation.x = -Math.PI / 2;
ground.position.y = -3.32;
ground.receiveShadow = true;
scene.add(ground);
// ------------------------------------------------------------
// Color constants and helper functions
// ------------------------------------------------------------
const BLUE = 0x3478c8;
const RED = 0xd85255;
const GOLD = 0xffd45a;
const clamp = THREE.MathUtils.clamp;
const smooth = (t) => t * t * (3 - 2 * t);
function makeCurve(points, tension = 0.25) {
return new THREE.CatmullRomCurve3(
points.map((p) => new THREE.Vector3(...p)),
false,
"catmullrom",
tension
);
}
function addEdges(mesh, opacity = 0.24, color = 0x294b42) {
const edges = new THREE.LineSegments(
new THREE.EdgesGeometry(mesh.geometry, 25),
new THREE.LineBasicMaterial({
color,
transparent: true,
opacity
})
);
mesh.add(edges);
return edges;
}
function stageColor(stage, u) {
const from = new THREE.Color(stage.startColor);
const to = new THREE.Color(stage.endColor);
const a = stage.transitionStart ?? 0;
const b = stage.transitionEnd ?? 1;
const k = smooth(clamp((u - a) / Math.max(0.0001, b - a), 0, 1));
return from.lerp(to, k);
}
function createGradientTube(
curve,
startColor,
endColor,
radius = 0.05,
tubularSegments = 64,
transitionStart = 0,
transitionEnd = 1
) {
const radialSegments = 6;
const geometry = new THREE.TubeGeometry(
curve,
tubularSegments,
radius,
radialSegments,
false
);
const count = geometry.attributes.position.count;
const colors = new Float32Array(count * 3);
const from = new THREE.Color(startColor);
const to = new THREE.Color(endColor);
for (let j = 0; j <= tubularSegments; j++) {
const t = j / tubularSegments;
const k = smooth(
clamp(
(t - transitionStart) /
Math.max(0.0001, transitionEnd - transitionStart),
0,
1
)
);
const color = from.clone().lerp(to, k);
for (let i = 0; i <= radialSegments; i++) {
const index = j * (radialSegments + 1) + i;
colors[index * 3] = color.r;
colors[index * 3 + 1] = color.g;
colors[index * 3 + 2] = color.b;
}
}
geometry.setAttribute(
"color",
new THREE.BufferAttribute(colors, 3)
);
const material = new THREE.MeshStandardMaterial({
vertexColors: true,
roughness: 0.58,
metalness: 0.02,
flatShading: true
});
const mesh = new THREE.Mesh(geometry, material);
mesh.castShadow = true;
mesh.receiveShadow = true;
scene.add(mesh);
return mesh;
}
function addArrow(curve, color, t = 0.54) {
const arrow = new THREE.Mesh(
new THREE.ConeGeometry(0.075, 0.22, 5),
new THREE.MeshStandardMaterial({
color,
emissive: color,
emissiveIntensity: 0.13,
roughness: 0.5
})
);
arrow.position.copy(curve.getPointAt(t));
const tangent = curve.getTangentAt(t).normalize();
arrow.quaternion.setFromUnitVectors(
new THREE.Vector3(0, 1, 0),
tangent
);
arrow.castShadow = true;
scene.add(arrow);
}
// ------------------------------------------------------------
// The verified route
// ------------------------------------------------------------
const rawStages = [
{
name: "Body vein",
detail: "Venous blood returns toward the right atrium.",
duration: 4.5,
startColor: BLUE,
endColor: BLUE,
radius: 0.075,
points: [
[-3.45, 2.60, -0.20],
[-3.25, 1.70, -0.05],
[-2.55, 0.85, 0.12],
[-1.30, 0.86, 0.45],
[-0.62, 0.64, 0.63]
]
},
{
name: "Right atrium",
detail: "The right atrium receives venous blood from the body.",
duration: 2.1,
startColor: BLUE,
endColor: BLUE,
radius: 0.052,
points: [
[-0.62, 0.64, 0.63],
[-0.63, 0.49, 0.67],
[-0.38, 0.36, 0.67]
]
},
{
name: "Right ventricle",
detail: "The right ventricle pumps blood into the pulmonary artery.",
duration: 2.4,
startColor: BLUE,
endColor: BLUE,
radius: 0.052,
points: [
[-0.38, 0.36, 0.67],
[-0.18, 0.04, 0.72],
[0.12, 0.50, 0.92]
]
},
{
name: "Pulmonary artery",
detail: "The pulmonary artery carries deoxygenated blood toward the lungs.",
duration: 3.2,
startColor: BLUE,
endColor: BLUE,
radius: 0.059,
points: [
[0.12, 0.50, 0.92],
[0.10, 1.00, 1.00],
[-0.60, 1.32, 0.73],
[-1.35, 1.35, 0.35]
]
},
{
name: "Lung capillaries",
detail: "Gas exchange: CO₂ leaves the blood and O₂ enters it.",
duration: 3.8,
startColor: BLUE,
endColor: RED,
radius: 0.034,
transitionStart: 0.10,
transitionEnd: 0.80,
points: [
[-1.35, 1.35, 0.35],
[-1.85, 1.75, 0.12],
[-2.45, 1.20, 0.12],
[-2.40, 0.55, 0.18],
[-1.95, 0.35, 0.25],
[-1.25, 0.75, 0.38]
]
},
{
name: "Pulmonary vein",
detail: "The pulmonary vein returns oxygenated blood to the left atrium.",
duration: 3.0,
startColor: RED,
endColor: RED,
radius: 0.052,
points: [
[-1.25, 0.75, 0.38],
[-0.72, 1.03, 0.50],
[0.00, 0.92, 0.52],
[0.52, 0.70, 0.42]
]
},
{
name: "Left atrium",
detail: "The left atrium receives oxygenated blood from the lungs.",
duration: 2.1,
startColor: RED,
endColor: RED,
radius: 0.048,
points: [
[0.52, 0.70, 0.42],
[0.63, 0.50, 0.50],
[0.28, 0.10, 0.52]
]
},
{
name: "Left ventricle",
detail: "The left ventricle pumps oxygenated blood into the aorta.",
duration: 2.4,
startColor: RED,
endColor: RED,
radius: 0.052,
points: [
[0.28, 0.10, 0.52],
[0.28, -0.03, 0.50],
[0.55, 0.42, 0.86]
]
},
{
name: "Body artery",
detail: "The aorta carries oxygenated blood from the left ventricle to body tissues.",
duration: 4.5,
startColor: RED,
endColor: RED,
radius: 0.075,
points: [
[0.55, 0.42, 0.86],
[0.82, 1.08, 1.00],
[0.80, 1.76, 0.84],
[1.42, 2.05, 0.60],
[2.40, 2.20, 0.30],
[3.25, 2.45, 0.05]
]
},
{
name: "Body tissues",
detail: "Body capillaries release O₂ and collect CO₂; venous blood returns to the body vein.",
duration: 5.2,
startColor: RED,
endColor: BLUE,
radius: 0.040,
transitionStart: 0.08,
transitionEnd: 0.35,
points: [
[3.25, 2.45, 0.05],
[3.56, 2.66, -0.10],
[3.43, 2.15, -0.46],
[2.10, 2.84, -0.75],
[0.00, 2.90, -0.82],
[-2.10, 2.84, -0.75],
[-3.45, 2.60, -0.20]
]
}
];
const stages = rawStages.map((stage) => ({
...stage,
curve: makeCurve(stage.points),
startColor: new THREE.Color(stage.startColor),
endColor: new THREE.Color(stage.endColor)
}));
const totalDuration = stages.reduce(
(sum, stage) => sum + stage.duration,
0
);
for (const stage of stages) {
createGradientTube(
stage.curve,
stage.startColor,
stage.endColor,
stage.radius,
72,
stage.transitionStart ?? 0,
stage.transitionEnd ?? 1
);
}
[
[0, 0.48],
[1, 0.56],
[2, 0.58],
[3, 0.52],
[4, 0.56],
[5, 0.50],
[6, 0.55],
[7, 0.62],
[8, 0.66],
[9, 0.56]
].forEach(([index, t]) => {
const stage = stages[index];
const color =
index === 4
? BLUE
: index === 9
? RED
: stage.startColor.getHex();
addArrow(stage.curve, color, t);
});
// ------------------------------------------------------------
// Schematic body volume and body tissues
// ------------------------------------------------------------
const torsoGeometry = new THREE.IcosahedronGeometry(1, 2);
const torso = new THREE.Mesh(
torsoGeometry,
new THREE.MeshStandardMaterial({
color: 0xb8d0c5,
transparent: true,
opacity: 0.055,
roughness: 1,
depthWrite: false,
side: THREE.DoubleSide
})
);
torso.position.set(0, -0.02, -0.65);
torso.scale.set(3.58, 3.42, 1.28);
scene.add(torso);
const torsoWire = new THREE.Mesh(
torsoGeometry,
new THREE.MeshBasicMaterial({
color: 0x66887a,
wireframe: true,
transparent: true,
opacity: 0.075,
depthWrite: false
})
);
torsoWire.position.copy(torso.position);
torsoWire.scale.copy(torso.scale);
scene.add(torsoWire);
function addTissueCluster(centerX, centerY, centerZ) {
const group = new THREE.Group();
const material = new THREE.MeshStandardMaterial({
color: 0xd99b88,
roughness: 0.86,
flatShading: true,
transparent: true,
opacity: 0.66
});
const positions = [
[0, 0, 0],
[0.30, 0.12, 0.02],
[-0.25, 0.16, -0.03],
[0.08, -0.25, 0.05]
];
for (const [x, y, z] of positions) {
const mesh = new THREE.Mesh(
new THREE.DodecahedronGeometry(0.28, 0),
material
);
mesh.position.set(x, y, z);
mesh.scale.set(1.15, 0.78, 0.72);
mesh.castShadow = true;
group.add(mesh);
}
group.position.set(centerX, centerY, centerZ);
scene.add(group);
}
addTissueCluster(3.42, 2.42, 0.02);
addTissueCluster(-3.43, 2.52, -0.18);
// ------------------------------------------------------------
// Low-poly lungs
// ------------------------------------------------------------
const lungGroups = [];
function createLung(side) {
const group = new THREE.Group();
const x = side * 1.95;
const lung = new THREE.Mesh(
new THREE.IcosahedronGeometry(1, 1),
new THREE.MeshStandardMaterial({
color: 0xdba6a0,
roughness: 0.88,
transparent: true,
opacity: 0.72,
flatShading: true
})
);
lung.scale.set(0.88, 1.45, 0.68);
lung.rotation.z = side * -0.08;
lung.castShadow = true;
lung.receiveShadow = true;
addEdges(lung, 0.18, 0x8f5d5a);
group.add(lung);
group.position.set(x, 0.95, -0.16);
scene.add(group);
lungGroups.push(group);
return group;
}
createLung(-1);
createLung(1);
// Pulmonary artery branches to both lungs.
for (const side of [-1, 1]) {
const artery = makeCurve([
[0.10, 1.00, 1.00],
[side * 0.72, 1.31, 0.75],
[side * 1.28, 1.35, 0.34],
[side * 1.86, 1.63, 0.08]
]);
createGradientTube(artery, BLUE, BLUE, 0.036, 42);
const vein = makeCurve([
[side * 1.86, 0.66, 0.18],
[side * 1.28, 0.94, 0.39],
[0.52, 0.70, 0.42]
]);
createGradientTube(vein, RED, RED, 0.035, 42);
}
// Fine intrapulmonary branches.
for (const side of [-1, 1]) {
const branchSets = [
[
[side * 1.28, 1.35, 0.32],
[side * 1.72, 1.92, 0.05],
[side * 2.03, 2.18, 0.02]
],
[
[side * 1.28, 1.35, 0.32],
[side * 2.12, 1.38, 0.08],
[side * 2.48, 1.40, 0.02]
],
[
[side * 1.28, 1.35, 0.32],
[side * 1.78, 0.82, 0.12],
[side * 2.05, 0.45, 0.08]
],
[
[side * 1.28, 1.35, 0.32],
[side * 1.48, 1.05, 0.20],
[side * 1.56, 0.72, 0.18]
]
];
branchSets.forEach((points, index) => {
const branch = makeCurve(points);
const start = index < 2 ? BLUE : RED;
const end = index < 2 ? BLUE : RED;
createGradientTube(branch, start, end, 0.014, 28);
});
}
// ------------------------------------------------------------
// Four-chamber low-poly heart
// ------------------------------------------------------------
const heartGroup = new THREE.Group();
scene.add(heartGroup);
const heartShell = new THREE.Mesh(
new THREE.IcosahedronGeometry(1, 1),
new THREE.MeshStandardMaterial({
color: 0xc66d72,
transparent: true,
opacity: 0.12,
roughness: 0.8,
depthWrite: false,
flatShading: true
})
);
heartShell.position.set(-0.03, 0.32, 0.38);
heartShell.scale.set(0.94, 1.20, 0.72);
heartShell.rotation.z = -0.14;
heartShell.castShadow = true;
heartGroup.add(heartShell);
function addChamber(position, scale, color) {
const chamber = new THREE.Mesh(
new THREE.IcosahedronGeometry(1, 1),
new THREE.MeshStandardMaterial({
color,
roughness: 0.58,
transparent: true,
opacity: 0.83,
flatShading: true
})
);
chamber.position.set(...position);
chamber.scale.set(...scale);
chamber.castShadow = true;
addEdges(chamber, 0.28, color);
heartGroup.add(chamber);
return chamber;
}
addChamber([-0.62, 0.64, 0.63], [0.50, 0.64, 0.47], BLUE);
addChamber([-0.18, 0.04, 0.72], [0.60, 0.69, 0.54], 0x2f68b5);
addChamber([0.52, 0.70, 0.42], [0.45, 0.52, 0.41], RED);
addChamber([0.28, -0.03, 0.50], [0.60, 0.74, 0.52], 0xc74449);
// ------------------------------------------------------------
// Tracked particle and secondary flow markers
// ------------------------------------------------------------
const particle = new THREE.Group();
const particleCore = new THREE.Mesh(
new THREE.IcosahedronGeometry(0.11, 1),
new THREE.MeshBasicMaterial({ color: BLUE })
);
particleCore.castShadow = true;
particle.add(particleCore);
const particleHalo = new THREE.Mesh(
new THREE.IcosahedronGeometry(0.24, 1),
new THREE.MeshBasicMaterial({
color: GOLD,
transparent: true,
opacity: 0.28,
depthWrite: false
})
);
particle.add(particleHalo);
const particleLight = new THREE.PointLight(GOLD, 0.9, 1.7);
particle.add(particleLight);
scene.add(particle);
const flowBeads = [];
stages.forEach((stage, stageIndex) => {
for (let i = 0; i < 2; i++) {
const bead = new THREE.Mesh(
new THREE.IcosahedronGeometry(0.032, 0),
new THREE.MeshBasicMaterial({
color: stage.startColor,
transparent: true,
opacity: 0.42,
depthWrite: false
})
);
scene.add(bead);
flowBeads.push({
mesh: bead,
stageIndex,
offset: (i / 2 + stageIndex * 0.137) % 1
});
}
});
// ------------------------------------------------------------
// Projected HTML labels
// ------------------------------------------------------------
const labelLayer = document.getElementById("labels");
const particleTag = document.getElementById("particle-tag");
const worldLabels = [];
function addWorldLabel(text, position, className = "") {
const element = document.createElement("div");
element.className = `world-label ${className}`;
element.textContent = text;
labelLayer.appendChild(element);
worldLabels.push({
element,
position: new THREE.Vector3(...position)
});
}
addWorldLabel("RIGHT LUNG", [-2.02, 1.10, -0.10]);
addWorldLabel("LEFT LUNG", [2.02, 1.10, -0.10]);
addWorldLabel("BODY VEIN", [-2.80, 1.28, 0.02], "blue");
addWorldLabel("BODY ARTERY", [2.62, 2.30, 0.18], "red");
addWorldLabel("RIGHT ATRIUM", [-1.02, 1.16, 1.02], "blue");
addWorldLabel("RIGHT VENTRICLE", [-0.95, -0.30, 1.08], "blue");
addWorldLabel("LEFT ATRIUM", [1.13, 1.13, 0.90], "red");
addWorldLabel("LEFT VENTRICLE", [1.03, -0.57, 0.95], "red");
const projected = new THREE.Vector3();
function projectElement(element, position) {
projected.copy(position).project(camera);
const visible =
projected.z > -1 &&
projected.z < 1 &&
Math.abs(projected.x) < 1.12 &&
Math.abs(projected.y) < 1.12;
element.style.display = visible ? "block" : "none";
if (visible) {
const x = (projected.x * 0.5 + 0.5) * window.innerWidth;
const y = (-projected.y * 0.5 + 0.5) * window.innerHeight;
element.style.left = `${x}px`;
element.style.top = `${y}px`;
}
}
// ------------------------------------------------------------
// Route list and status UI
// ------------------------------------------------------------
const routeList = document.getElementById("route-list");
const routeRows = stages.map((stage, index) => {
const row = document.createElement("li");
const number = document.createElement("span");
number.className = "step-number";
number.textContent = String(index + 1).padStart(2, "0");
const copy = document.createElement("div");
const name = document.createElement("span");
name.className = "step-name";
name.textContent = stage.name;
const detail = document.createElement("span");
detail.className = "step-detail";
detail.textContent = stage.detail;
copy.append(name, detail);
row.append(number, copy);
routeList.appendChild(row);
return row;
});
const statusIndex = document.getElementById("status-index");
const statusName = document.getElementById("status-name");
const statusDetail = document.getElementById("status-detail");
const progress = document.getElementById("progress");
let activeStage = -1;
function setActiveStage(index) {
if (index === activeStage) return;
activeStage = index;
routeRows.forEach((row, i) => {
row.classList.toggle("active", i === index);
});
const stage = stages[index];
statusIndex.textContent =
`${String(index + 1).padStart(2, "0")} / ${String(stages.length).padStart(2, "0")}`;
statusName.textContent = stage.name;
statusDetail.textContent = stage.detail;
}
// ------------------------------------------------------------
// Custom camera controls: orbit, wheel/pinch zoom, keyboard
// ------------------------------------------------------------
const orbit = {
target: new THREE.Vector3(0, 0.05, 0),
theta: 0.57,
phi: 1.09,
radius: 11.7,
desiredTheta: 0.57,
desiredPhi: 1.09,
desiredRadius: 11.7
};
const pointers = new Map();
let lastX = 0;
let lastY = 0;
let pinchDistance = 0;
function pointerDistance() {
const values = [...pointers.values()];
if (values.length < 2) return 0;
return Math.hypot(
values[0].x - values[1].x,
values[0].y - values[1].y
);
}
renderer.domElement.addEventListener("pointerdown", (event) => {
renderer.domElement.setPointerCapture(event.pointerId);
pointers.set(event.pointerId, { x: event.clientX, y: event.clientY });
if (pointers.size === 1) {
lastX = event.clientX;
lastY = event.clientY;
} else if (pointers.size === 2) {
pinchDistance = pointerDistance();
}
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (!pointers.has(event.pointerId)) return;
pointers.set(event.pointerId, { x: event.clientX, y: event.clientY });
if (pointers.size === 1) {
const dx = event.clientX - lastX;
const dy = event.clientY - lastY;
lastX = event.clientX;
lastY = event.clientY;
orbit.desiredTheta -= dx * 0.006;
orbit.desiredPhi = clamp(orbit.desiredPhi - dy * 0.005, 0.34, 2.62);
} else if (pointers.size === 2) {
const distance = pointerDistance();
if (distance > 0 && pinchDistance > 0) {
orbit.desiredRadius = clamp(
orbit.desiredRadius * (pinchDistance / distance),
7.0,
18.0
);
}
pinchDistance = distance;
}
});
function releasePointer(event) {
pointers.delete(event.pointerId);
if (pointers.size === 1) {
const remaining = [...pointers.entries()][0];
lastX = remaining[1].x;
lastY = remaining[1].y;
}
}
renderer.domElement.addEventListener("pointerup", releasePointer);
renderer.domElement.addEventListener("pointercancel", releasePointer);
renderer.domElement.addEventListener(
"wheel",
(event) => {
event.preventDefault();
orbit.desiredRadius = clamp(
orbit.desiredRadius * Math.exp(event.deltaY * 0.001),
7.0,
18.0
);
},
{ passive: false }
);
renderer.domElement.addEventListener("contextmenu", (event) => {
event.preventDefault();
});
window.addEventListener("keydown", (event) => {
if (event.key === "ArrowLeft") orbit.desiredTheta += 0.10;
if (event.key === "ArrowRight") orbit.desiredTheta -= 0.10;
if (event.key === "ArrowUp") orbit.desiredPhi -= 0.08;
if (event.key === "ArrowDown") orbit.desiredPhi += 0.08;
if (event.key === "+" || event.key === "=") {
orbit.desiredRadius = clamp(orbit.desiredRadius - 0.6, 7, 18);
}
if (event.key === "-" || event.key === "_") {
orbit.desiredRadius = clamp(orbit.desiredRadius + 0.6, 7, 18);
}
});
function updateCamera(dt) {
const damping = 1 - Math.exp(-dt * 9);
orbit.theta += (orbit.desiredTheta - orbit.theta) * damping;
orbit.phi += (orbit.desiredPhi - orbit.phi) * damping;
orbit.radius += (orbit.desiredRadius - orbit.radius) * damping;
const sinPhi = Math.sin(orbit.phi);
const cosPhi = Math.cos(orbit.phi);
camera.position.set(
orbit.target.x + orbit.radius * sinPhi * Math.sin(orbit.theta),
orbit.target.y + orbit.radius * cosPhi,
orbit.target.z + orbit.radius * sinPhi * Math.cos(orbit.theta)
);
camera.lookAt(orbit.target);
camera.updateMatrixWorld();
}
// ------------------------------------------------------------
// Buttons
// ------------------------------------------------------------
let playing = true;
let globalTime = 0;
const playButton = document.getElementById("play-button");
playButton.addEventListener("click", () => {
playing = !playing;
playButton.textContent = playing ? "Pause" : "Resume";
});
document.getElementById("restart-button").addEventListener("click", () => {
globalTime = 0;
activeStage = -1;
});
document.getElementById("labels-button").addEventListener("click", (event) => {
const hidden = labelLayer.classList.toggle("hidden");
event.currentTarget.textContent = hidden ? "Show labels" : "Hide labels";
event.currentTarget.setAttribute("aria-pressed", String(!hidden));
});
document.getElementById("view-button").addEventListener("click", () => {
orbit.desiredTheta = 0.57;
orbit.desiredPhi = 1.09;
orbit.desiredRadius = 11.7;
});
// ------------------------------------------------------------
// Animation
// ------------------------------------------------------------
const clock = new THREE.Clock();
function findStageAt(time) {
let elapsed = 0;
for (let i = 0; i < stages.length; i++) {
elapsed += stages[i].duration;
if (time < elapsed) {
return {
index: i,
local: clamp((time - (elapsed - stages[i].duration)) /
stages[i].duration, 0, 1)
};
}
}
return {
index: stages.length - 1,
local: 1
};
}
function animate() {
const dt = Math.min(clock.getDelta(), 0.05);
if (playing) globalTime += dt;
updateCamera(dt);
const cycleTime = globalTime % totalDuration;
const { index, local } = findStageAt(cycleTime);
const stage = stages[index];
setActiveStage(index);
const position = stage.curve.getPointAt(local);
particle.position.copy(position);
const color = stageColor(stage, local);
particleCore.material.color.copy(color);
particleLight.color.set(GOLD);
const pulse = 1 + Math.sin(globalTime * 7) * 0.07;
particleHalo.scale.setScalar(pulse);
// Secondary markers show the direction of flow.
for (const bead of flowBeads) {
const beadStage = stages[bead.stageIndex];
const localBead = (
globalTime / beadStage.duration +
bead.offset
) % 1;
bead.mesh.position.copy(
beadStage.curve.getPointAt(localBead)
);
bead.mesh.material.color.copy(
stageColor(beadStage, localBead)
);
}
// A restrained heartbeat and breathing motion.
const heartbeat = Math.pow(
Math.max(0, Math.sin(globalTime * Math.PI * 2.15)),
9
);
heartShell.scale.set(
0.94 * (1 + heartbeat * 0.025),
1.20 * (1 + heartbeat * 0.025),
0.72 * (1 + heartbeat * 0.025)
);
const breathing = 1 + Math.sin(globalTime * 1.4) * 0.012;
for (const lung of lungGroups) {
lung.scale.y = breathing;
}
progress.style.width = `${(cycleTime / totalDuration) * 100}%`;
for (const label of worldLabels) {
projectElement(label.element, label.position);
}
const particleWorld = particle.getWorldPosition(new THREE.Vector3());
projectElement(particleTag, particleWorld);
renderer.render(scene, camera);
}
renderer.setAnimationLoop(animate);
window.addEventListener("resize", () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
});
</script>
</body>
</html>
```