Prompt specification
Build or customize the Three.js 3D scene "One-way valve sequence 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: One-way valve sequence model
Category: Biology & Nature · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/one-way-valve-sequence-model-x0994
Scene Description Prompt:
Build a self-contained HTML scene with Three.js from cdnjs featuring cardiovascular circulation. Animate schematic valves opening and closing along a pumping cycle; arrows must follow the intended chamber sequence and never pass through closed valves. A 3D cutaway view.
Working Three.js Starter (one-way-valve-sequence-model-x0994):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Cardiovascular Circulation — 3D Cutaway</title>
<style>
:root {
color-scheme: dark;
--bg: #06101c;
--panel: rgba(9, 22, 38, 0.82);
--line: rgba(154, 202, 230, 0.16);
--text: #e8f4fb;
--muted: #89a8bd;
--blue: #3aa0ff;
--red: #ff536b;
--green: #62e6ad;
--closed: #df7180;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: var(--bg);
font-family: Inter, ui-sans-serif, system-ui, -apple-system,
BlinkMacSystemFont, "Segoe UI", sans-serif;
}
button,
input {
font: inherit;
}
#scene {
position: fixed;
inset: 0;
width: 100%;
height: 100%;
display: block;
touch-action: none;
cursor: grab;
}
#scene:active {
cursor: grabbing;
}
.glass {
background: var(--panel);
border: 1px solid var(--line);
box-shadow: 0 18px 55px rgba(0, 0, 0, 0.28);
backdrop-filter: blur(16px);
-webkit-backdrop-filter: blur(16px);
}
#title {
position: fixed;
top: 24px;
left: 26px;
z-index: 5;
pointer-events: none;
}
.eyebrow {
display: flex;
align-items: center;
gap: 9px;
color: #91d8ff;
font-size: 10px;
font-weight: 800;
letter-spacing: 0.19em;
text-transform: uppercase;
}
.heart-mark {
width: 10px;
height: 10px;
border-radius: 50% 50% 50% 0;
background: var(--red);
transform: rotate(-45deg);
box-shadow: 0 0 18px rgba(255, 83, 107, 0.7);
animation: heartbeat 1.7s ease-in-out infinite;
}
@keyframes heartbeat {
0%, 100% { transform: rotate(-45deg) scale(0.9); }
45% { transform: rotate(-45deg) scale(1.15); }
60% { transform: rotate(-45deg) scale(0.96); }
}
#title h1 {
margin: 8px 0 5px;
max-width: 440px;
font-size: clamp(24px, 3vw, 42px);
line-height: 1.02;
letter-spacing: -0.045em;
font-weight: 760;
}
#title p {
margin: 0;
color: var(--muted);
font-size: 12px;
letter-spacing: 0.04em;
}
#panel {
position: fixed;
z-index: 6;
top: 24px;
right: 24px;
width: 326px;
max-height: calc(100vh - 48px);
overflow: auto;
padding: 18px;
border-radius: 18px;
scrollbar-width: thin;
scrollbar-color: #2b4a60 transparent;
}
.panel-heading {
display: flex;
align-items: center;
justify-content: space-between;
gap: 12px;
margin-bottom: 16px;
}
.panel-heading strong {
font-size: 11px;
letter-spacing: 0.14em;
text-transform: uppercase;
}
.cutaway-badge {
padding: 5px 8px;
border: 1px solid rgba(98, 230, 173, 0.3);
border-radius: 999px;
color: var(--green);
background: rgba(98, 230, 173, 0.07);
font-size: 9px;
font-weight: 800;
letter-spacing: 0.12em;
}
#phaseName {
margin: 0 0 6px;
font-size: 21px;
letter-spacing: -0.025em;
}
#phaseDescription {
min-height: 36px;
margin: 0;
color: var(--muted);
font-size: 12px;
line-height: 1.5;
}
.progress-track {
height: 3px;
margin: 14px 0 17px;
overflow: hidden;
border-radius: 999px;
background: rgba(255, 255, 255, 0.08);
}
#progressFill {
width: 0%;
height: 100%;
border-radius: inherit;
background: linear-gradient(90deg, var(--blue), #a68cff, var(--red));
}
.section-label {
margin: 16px 0 8px;
color: #6f91a8;
font-size: 9px;
font-weight: 800;
letter-spacing: 0.17em;
text-transform: uppercase;
}
.valve-grid {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 7px;
}
.valve-row {
display: flex;
align-items: center;
justify-content: space-between;
gap: 7px;
min-height: 37px;
padding: 8px 9px;
border: 1px solid rgba(255, 255, 255, 0.07);
border-radius: 9px;
background: rgba(255, 255, 255, 0.025);
transition: border-color 180ms, background 180ms;
}
.valve-row > span:first-child {
color: #abc2d1;
font-size: 9px;
font-weight: 750;
letter-spacing: 0.06em;
}
.status {
display: flex;
align-items: center;
gap: 5px;
color: var(--closed);
font-size: 8px;
font-weight: 850;
letter-spacing: 0.07em;
}
.status::before {
content: "";
width: 6px;
height: 6px;
border-radius: 50%;
background: currentColor;
box-shadow: 0 0 8px currentColor;
}
.valve-row.open {
border-color: rgba(98, 230, 173, 0.22);
background: rgba(98, 230, 173, 0.055);
}
.valve-row.open .status {
color: var(--green);
}
.sequence {
display: flex;
flex-wrap: wrap;
align-items: center;
gap: 5px;
}
.flow-chip {
padding: 5px 7px;
border: 1px solid rgba(255, 255, 255, 0.08);
border-radius: 7px;
color: #7f9eb1;
background: rgba(255, 255, 255, 0.025);
font-size: 8px;
font-weight: 800;
letter-spacing: 0.045em;
transition: 180ms;
}
.flow-chip.blue {
color: #69b9ff;
}
.flow-chip.red {
color: #ff7a8c;
}
.flow-chip.active {
border-color: currentColor;
box-shadow: 0 0 17px color-mix(in srgb, currentColor 15%, transparent);
background: color-mix(in srgb, currentColor 8%, transparent);
}
.sequence-arrow {
color: #476477;
font-size: 10px;
}
.safety-note {
margin: 12px 0 0;
padding-top: 11px;
border-top: 1px solid rgba(255, 255, 255, 0.06);
color: #7292a7;
font-size: 9px;
line-height: 1.45;
}
.opacity-control {
display: grid;
grid-template-columns: auto 1fr auto;
align-items: center;
gap: 9px;
color: #91adbf;
font-size: 9px;
font-weight: 750;
letter-spacing: 0.05em;
}
#wallOpacity {
width: 100%;
accent-color: #65c8ff;
cursor: pointer;
}
#wallValue {
min-width: 34px;
color: #bdd3e1;
text-align: right;
}
#controls {
position: fixed;
z-index: 8;
left: 26px;
bottom: 25px;
display: flex;
align-items: center;
gap: 7px;
padding: 7px;
border-radius: 13px;
}
.control-button {
height: 34px;
min-width: 38px;
padding: 0 11px;
border: 1px solid rgba(255, 255, 255, 0.09);
border-radius: 8px;
color: #bad0de;
background: rgba(255, 255, 255, 0.035);
cursor: pointer;
font-size: 10px;
font-weight: 750;
transition: 150ms;
}
.control-button:hover {
color: white;
border-color: rgba(130, 202, 235, 0.35);
background: rgba(100, 190, 235, 0.08);
}
.control-button.primary {
min-width: 76px;
color: #071522;
background: #89d9ff;
border-color: transparent;
}
.control-button.selected {
color: #dff5ff;
border-color: rgba(116, 208, 255, 0.5);
background: rgba(86, 184, 232, 0.13);
}
.divider {
width: 1px;
height: 23px;
margin: 0 2px;
background: rgba(255, 255, 255, 0.08);
}
#legend {
position: fixed;
z-index: 6;
left: 50%;
bottom: 30px;
display: flex;
gap: 18px;
transform: translateX(-50%);
pointer-events: none;
color: #8eaabc;
font-size: 9px;
font-weight: 750;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.legend-item {
display: flex;
align-items: center;
gap: 7px;
}
.legend-dot {
width: 8px;
height: 8px;
border-radius: 50%;
box-shadow: 0 0 10px currentColor;
}
#hint {
position: fixed;
right: 25px;
bottom: 23px;
z-index: 5;
color: #58778c;
font-size: 9px;
letter-spacing: 0.04em;
pointer-events: none;
}
#labels {
position: fixed;
inset: 0;
z-index: 3;
overflow: hidden;
pointer-events: none;
}
.world-label {
position: absolute;
display: flex;
align-items: center;
gap: 7px;
min-width: 90px;
transform: translate(-50%, -50%);
filter: drop-shadow(0 4px 10px rgba(0, 0, 0, 0.5));
transition: opacity 120ms;
}
.world-label .dot {
width: 7px;
height: 7px;
flex: 0 0 auto;
border: 1px solid rgba(255, 255, 255, 0.65);
border-radius: 50%;
background: currentColor;
box-shadow: 0 0 10px currentColor;
}
.world-label .label-copy {
padding: 5px 7px;
border-left: 1px solid rgba(255, 255, 255, 0.15);
background: rgba(3, 12, 22, 0.55);
backdrop-filter: blur(5px);
}
.world-label b {
display: block;
color: #dbeaf3;
font-size: 9px;
line-height: 1.1;
letter-spacing: 0.07em;
}
.world-label small {
display: block;
margin-top: 3px;
color: currentColor;
font-size: 7px;
font-weight: 800;
letter-spacing: 0.1em;
}
.world-label.blue { color: var(--blue); }
.world-label.red { color: var(--red); }
.world-label.minor .label-copy { padding: 3px 6px; }
.world-label.minor b { font-size: 8px; }
@media (max-width: 900px) {
#panel {
top: auto;
right: 12px;
bottom: 76px;
left: 12px;
width: auto;
max-height: 38vh;
padding: 14px;
}
#title {
top: 14px;
left: 15px;
}
#title h1 {
font-size: 25px;
}
#controls {
left: 12px;
bottom: 12px;
}
#legend,
#hint {
display: none;
}
.world-label.minor {
display: none;
}
}
@media (max-width: 520px) {
#title p {
display: none;
}
#title h1 {
max-width: 280px;
font-size: 22px;
}
.control-button {
padding: 0 8px;
}
#resetView {
display: none;
}
}
</style>
</head>
<body>
<canvas id="scene" aria-label="Interactive 3D cardiovascular circulation cutaway"></canvas>
<div id="labels"></div>
<header id="title">
<div class="eyebrow">
<span class="heart-mark"></span>
Interactive hemodynamics
</div>
<h1>Cardiovascular<br />circulation</h1>
<p>Three-dimensional cutaway · animated chamber and valve cycle</p>
</header>
<aside id="panel" class="glass">
<div class="panel-heading">
<strong>Pumping cycle</strong>
<span class="cutaway-badge">3D CUTAWAY</span>
</div>
<h2 id="phaseName">Atrial systole</h2>
<p id="phaseDescription" aria-live="polite"></p>
<div class="progress-track">
<div id="progressFill"></div>
</div>
<div class="section-label">Valve states</div>
<div class="valve-grid">
<div class="valve-row" id="valve-tricuspid">
<span>TRICUSPID</span><span class="status">CLOSED</span>
</div>
<div class="valve-row" id="valve-mitral">
<span>MITRAL</span><span class="status">CLOSED</span>
</div>
<div class="valve-row" id="valve-pulmonary">
<span>PULMONARY</span><span class="status">CLOSED</span>
</div>
<div class="valve-row" id="valve-aortic">
<span>AORTIC</span><span class="status">CLOSED</span>
</div>
</div>
<div class="section-label">Chamber sequence</div>
<div class="sequence">
<span class="flow-chip blue" id="flow-body-ra">BODY</span>
<span class="sequence-arrow">→</span>
<span class="flow-chip blue" id="flow-ra-rv">RA → RV</span>
<span class="sequence-arrow">→</span>
<span class="flow-chip blue" id="flow-rv-lungs">LUNGS</span>
<span class="sequence-arrow">→</span>
<span class="flow-chip red" id="flow-lungs-la">LA</span>
<span class="sequence-arrow">→</span>
<span class="flow-chip red" id="flow-la-lv">LV</span>
<span class="sequence-arrow">→</span>
<span class="flow-chip red" id="flow-lv-body">BODY</span>
</div>
<p class="safety-note">
Flow arrows are hard-gated by valve state: an arrow is hidden before its
valve can close, so no arrowhead crosses a shut valve.
</p>
<div class="section-label">Wall opacity</div>
<label class="opacity-control">
<span>MYO</span>
<input id="wallOpacity" type="range" min="45" max="100" value="94" />
<span id="wallValue">94%</span>
</label>
</aside>
<div id="controls" class="glass">
<button id="playPause" class="control-button primary" type="button">Pause</button>
<div class="divider"></div>
<button class="control-button speed selected" data-speed="0.5" type="button">0.5×</button>
<button class="control-button speed selected" data-speed="1" type="button">1×</button>
<button class="control-button speed" data-speed="1.5" type="button">1.5×</button>
<div class="divider"></div>
<button id="resetView" class="control-button" type="button">Reset view</button>
</div>
<div id="legend">
<span class="legend-item">
<span class="legend-dot" style="color:#3aa0ff;background:#3aa0ff"></span>
Deoxygenated
</span>
<span class="legend-item">
<span class="legend-dot" style="style="color:#ff536b;background:#ff536b"></span>
Oxygenated
</span>
</div>
<div id="hint">Drag to orbit · Scroll to zoom · Double-click to reset</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// ------------------------------------------------------------
// Core setup
// ------------------------------------------------------------
const canvas = document.querySelector("#scene");
const renderer = new THREE.WebGLRenderer({
canvas,
antialias: true,
alpha: false,
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;
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x06101c);
scene.fog = new THREE.FogExp2(0x06101c, 0.035);
const camera = new THREE.PerspectiveCamera(
39,
window.innerWidth / window.innerHeight,
0.1,
100
);
const target = new THREE.Vector3(-0.38, 0.02, 0);
let yaw = 0;
let pitch = 0.08;
let cameraRadius = 10.4;
function updateCamera() {
const horizontal = cameraRadius * Math.cos(pitch);
camera.position.set(
target.x + Math.sin(yaw) * horizontal,
target.y + Math.sin(pitch) * cameraRadius,
target.z + Math.cos(yaw) * horizontal
);
camera.lookAt(target);
camera.updateMatrixWorld();
}
updateCamera();
const hemi = new THREE.HemisphereLight(0xa9dcff, 0x30151d, 1.9);
scene.add(hemi);
const key = new THREE.DirectionalLight(0xffffff, 3.1);
key.position.set(-4, 7, 8);
key.castShadow = true;
key.shadow.mapSize.set(1024, 1024);
key.shadow.camera.left = -5;
key.shadow.camera.right = 5;
key.shadow.camera.top = 5;
key.shadow.camera.bottom = -5;
scene.add(key);
const rimBlue = new THREE.PointLight(0x228dff, 28, 12, 2);
rimBlue.position.set(-5, 1.5, 3);
scene.add(rimBlue);
const rimRed = new THREE.PointLight(0xff365d, 22, 10, 2);
rimRed.position.set(4.5, 0.5, 2);
scene.add(rimRed);
const cardiac = new THREE.Group();
scene.add(cardiac);
const DEOXY = 0x3297ff;
const OXY = 0xff536b;
const DEOXY_LIGHT = 0x78c4ff;
const OXY_LIGHT = 0xff8797;
// ------------------------------------------------------------
// Platform and atmosphere
// ------------------------------------------------------------
const platform = new THREE.Mesh(
new THREE.CylinderGeometry(3.15, 3.42, 0.18, 64),
new THREE.MeshStandardMaterial({
color: 0x0c1c2b,
roughness: 0.72,
metalness: 0.15
})
);
platform.position.set(0, -2.77, 0);
platform.receiveShadow = true;
scene.add(platform);
const platformRing = new THREE.Mesh(
new THREE.TorusGeometry(2.86, 0.012, 6, 96),
new THREE.MeshBasicMaterial({
color: 0x4bb8e9,
transparent: true,
opacity: 0.22
})
);
platformRing.rotation.x = Math.PI / 2;
platformRing.position.set(0, -2.665, 0);
scene.add(platformRing);
const particlePositions = [];
for (let i = 0; i < 170; i++) {
particlePositions.push(
(Math.random() - 0.5) * 15,
(Math.random() - 0.5) * 9,
-2.5 + Math.random() * 4
);
}
const particleGeometry = new THREE.BufferGeometry();
particleGeometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(particlePositions, 3)
);
const particles = new THREE.Points(
particleGeometry,
new THREE.PointsMaterial({
color: 0x79bce1,
size: 0.018,
transparent: true,
opacity: 0.34,
depthWrite: false
})
);
scene.add(particles);
// ------------------------------------------------------------
// Extruded myocardial shell with four real cutaway openings
// ------------------------------------------------------------
const heartShape = new THREE.Shape();
heartShape.moveTo(-0.15, 2.25);
heartShape.bezierCurveTo(-0.9, 2.2, -1.85, 1.9, -2.25, 1.25);
heartShape.bezierCurveTo(-2.65, 0.6, -2.4, -0.45, -2.05, -1.2);
heartShape.bezierCurveTo(-1.55, -2.15, -0.2, -2.35, 0.48, -2.58);
heartShape.bezierCurveTo(1.15, -2.42, 2.1, -1.72, 2.35, -0.72);
heartShape.bezierCurveTo(2.58, 0.2, 2.35, 1.28, 1.78, 1.9);
heartShape.bezierCurveTo(1.23, 2.36, 0.4, 2.35, -0.15, 2.25);
heartShape.closePath();
function addChamberHole(cx, cy, rx, ry) {
const hole = new THREE.Path();
hole.absellipse(cx, cy, rx, ry, 0, Math.PI * 2, true);
heartShape.holes.push(hole);
}
addChamberHole(-1.12, 0.83, 0.73, 0.72); // Right atrium
addChamberHole(-1.02, -0.86, 0.82, 0.92); // Right ventricle
addChamberHole(0.95, 0.85, 0.63, 0.65); // Left atrium
addChamberHole(1.02, -0.88, 0.73, 0.99); // Left ventricle
const heartGeometry = new THREE.ExtrudeGeometry(heartShape, {
depth: 1.35,
bevelEnabled: true,
bevelSegments: 3,
steps: 1,
bevelSize: 0.07,
bevelThickness: 0.07,
curveSegments: 32
});
heartGeometry.translate(0, 0, -0.675);
heartGeometry.computeVertexNormals();
const tissueCap = new THREE.MeshStandardMaterial({
color: 0x9f3d56,
roughness: 0.46,
metalness: 0.06,
transparent: true,
opacity: 0.94,
side: THREE.DoubleSide
});
const tissueSide = tissueCap.clone();
tissueSide.color.setHex(0x682239);
tissueSide.roughness = 0.56;
const heartMesh = new THREE.Mesh(
heartGeometry,
[tissueCap, tissueSide]
);
heartMesh.castShadow = true;
heartMesh.receiveShadow = true;
cardiac.add(heartMesh);
const contourPoints = heartShape.getPoints(100).map(
point => new THREE.Vector3(point.x, point.y, 0.705)
);
const contour = new THREE.LineLoop(
new THREE.BufferGeometry().setFromPoints(contourPoints),
new THREE.LineBasicMaterial({
color: 0xe47a8c,
transparent: true,
opacity: 0.38
})
);
cardiac.add(contour);
// ------------------------------------------------------------
// Chamber interiors
// ------------------------------------------------------------
function addChamber(cx, cy, rx, ry, oxygenated) {
const baseColor = oxygenated ? 0x651c31 : 0x123b68;
const glowColor = oxygenated ? OXY : DEOXY;
const back = new THREE.Mesh(
new THREE.CircleGeometry(1, 64),
new THREE.MeshStandardMaterial({
color: baseColor,
roughness: 0.74,
metalness: 0.02,
side: THREE.DoubleSide
})
);
back.position.set(cx, cy, -0.57);
back.scale.set(rx * 0.98, ry * 0.98, 1);
back.receiveShadow = true;
cardiac.add(back);
const volume = new THREE.Mesh(
new THREE.SphereGeometry(1, 32, 20),
new THREE.MeshPhysicalMaterial({
color: glowColor,
transparent: true,
opacity: 0.13,
roughness: 0.22,
metalness: 0,
depthWrite: false,
side: THREE.DoubleSide
})
);
volume.position.set(cx, cy, -0.17);
volume.scale.set(rx * 0.9, ry * 0.9, 0.55);
cardiac.add(volume);
const ellipse = new THREE.EllipseCurve(
cx, cy, rx, ry, 0, Math.PI * 2, false, 0
);
const borderPoints = ellipse.getPoints(72).map(
point => new THREE.Vector3(point.x, point.y, 0.695)
);
const border = new THREE.LineLoop(
new THREE.BufferGeometry().setFromPoints(borderPoints),
new THREE.LineBasicMaterial({
color: glowColor,
transparent: true,
opacity: 0.62
})
);
cardiac.add(border);
}
addChamber(-1.12, 0.83, 0.73, 0.72, false);
addChamber(-1.02, -0.86, 0.82, 0.92, false);
addChamber(0.95, 0.85, 0.63, 0.65, true);
addChamber(1.02, -0.88, 0.73, 0.99, true);
// ------------------------------------------------------------
// Large vessels
// ------------------------------------------------------------
function addVessel(points, color, radius = 0.17) {
const curve = new THREE.CatmullRomCurve3(
points.map(point => new THREE.Vector3(...point)),
false,
"catmullrom",
0.35
);
const wall = new THREE.Mesh(
new THREE.TubeGeometry(curve, 72, radius, 12, false),
new THREE.MeshStandardMaterial({
color,
roughness: 0.34,
metalness: 0.04,
transparent: true,
opacity: 0.76
})
);
wall.castShadow = true;
cardiac.add(wall);
const core = new THREE.Mesh(
new THREE.TubeGeometry(curve, 72, radius * 0.34, 8, false),
new THREE.MeshBasicMaterial({
color,
transparent: true,
opacity: 0.26,
depthWrite: false
})
);
cardiac.add(core);
}
// Systemic venous return: body -> right atrium.
addVessel([
[-3.0, 2.62, 0.0],
[-2.82, 1.65, 0.0],
[-2.32, 1.02, -0.04],
[-1.62, 0.82, -0.12]
], DEOXY, 0.19);
// Pulmonary artery: right ventricle -> lungs.
addVessel([
[-0.92, -0.72, -0.08],
[-0.58, -0.08, -0.10],
[-0.35, 0.43, -0.12],
[0.02, 1.32, -0.10],
[0.82, 2.16, -0.04],
[1.85, 2.62, 0.0]
], DEOXY, 0.17);
// Pulmonary veins: lungs -> left atrium.
addVessel([
[3.22, 1.88, 0.0],
[2.45, 1.57, -0.05],
[1.73, 1.18, -0.08],
[1.22, 0.91, -0.12]
], OXY, 0.15);
// Aorta: left ventricle -> body.
addVessel([
[1.12, -0.78, -0.10],
[1.26, -0.18, -0.10],
[1.25, 0.46, -0.12],
[1.49, 1.38, -0.10],
[2.12, 2.17, 0.0],
[3.08, 2.68, 0.0]
], OXY, 0.18);
// ------------------------------------------------------------
// Valve construction
// ------------------------------------------------------------
const valves = {};
function makeLeafGeometry(radius) {
const shape = new THREE.Shape();
shape.moveTo(0, -radius * 0.74);
shape.quadraticCurveTo(
radius * 0.52, -radius * 0.65,
radius * 0.98, 0
);
shape.quadraticCurveTo(
radius * 0.52, radius * 0.65,
0, radius * 0.74
);
shape.quadraticCurveTo(-radius * 0.08, 0, 0, -radius * 0.74);
shape.closePath();
return new THREE.ShapeGeometry(shape, 18);
}
function makeCuspGeometry(radius) {
const length = radius * 0.86;
const width = radius * 0.55;
const shape = new THREE.Shape();
shape.moveTo(0, -width * 0.5);
shape.quadraticCurveTo(
-length * 0.45, -width * 0.48,
-length, 0
);
shape.quadraticCurveTo(
-length * 0.45, width * 0.48,
0, width * 0.5
);
shape.closePath();
return new THREE.ShapeGeometry(shape, 16);
}
function createValve(id, x, y, radius, kind) {
const group = new THREE.Group();
group.position.set(x, y, 0.755);
cardiac.add(group);
const ringMaterial = new THREE.MeshStandardMaterial({
color: 0xf3d2d4,
roughness: 0.3,
metalness: 0.05,
emissive: 0x4a101a,
emissiveIntensity: 0.2
});
const ring = new THREE.Mesh(
new THREE.TorusGeometry(radius, 0.045, 9, 48),
ringMaterial
);
group.add(ring);
const halo = new THREE.Mesh(
new THREE.TorusGeometry(radius * 1.18, 0.012, 6, 48),
new THREE.MeshBasicMaterial({
color: kind === "AV" ? 0xff9cae : 0x9bdcff,
transparent: true,
opacity: 0.22
})
);
group.add(halo);
const aperture = new THREE.Mesh(
new THREE.CircleGeometry(radius * 0.91, 40),
new THREE.MeshBasicMaterial({
color: 0x120b13,
transparent: true,
opacity: 0.24,
side: THREE.DoubleSide,
depthWrite: false
})
);
aperture.position.z = -0.055;
group.add(aperture);
const leafMaterial = new THREE.MeshStandardMaterial({
color: 0xf2afb8,
roughness: 0.36,
metalness: 0.03,
emissive: 0x30070d,
emissiveIntensity: 0.12,
side: THREE.DoubleSide
});
const valve = {
id,
kind,
group,
ring,
ringMaterial,
halo,
aperture,
open: 0,
pivots: []
};
if (kind === "AV") {
const geometry = makeLeafGeometry(radius);
const leftPivot = new THREE.Group();
leftPivot.position.set(-radius * 0.94, 0, 0.015);
const leftLeaf = new THREE.Mesh(geometry, leafMaterial);
leftPivot.add(leftLeaf);
group.add(leftPivot);
const rightPivot = new THREE.Group();
rightPivot.position.set(radius * 0.94, 0, 0.015);
const rightLeaf = new THREE.Mesh(geometry, leafMaterial);
rightLeaf.scale.x = -1;
rightPivot.add(rightLeaf);
group.add(rightPivot);
valve.pivots.push(
{ pivot: leftPivot, sign: -1 },
{ pivot: rightPivot, sign: 1 }
);
} else {
const geometry = makeCuspGeometry(radius);
for (let i = 0; i < 3; i++) {
const angle = (i / 3) * Math.PI * 2;
const pivot = new THREE.Group();
pivot.position.set(
Math.cos(angle) * radius * 0.91,
Math.sin(angle) * radius * 0.91,
0.012
);
pivot.rotation.z = angle;
const cusp = new THREE.Mesh(geometry, leafMaterial);
pivot.add(cusp);
group.add(pivot);
valve.pivots.push({
pivot,
cusp,
baseAngle: angle,
sign: i % 2 === 0 ? 1 : -1
});
}
}
valves[id] = valve;
return valve;
}
createValve("tricuspid", -1.12, 0.09, 0.31, "AV");
createValve("mitral", 0.98, 0.12, 0.285, "AV");
createValve("pulmonary", -0.35, 0.43, 0.245, "SL");
createValve("aortic", 1.25, 0.46, 0.235, "SL");
function setValveOpen(valve, amount) {
valve.open = amount;
if (valve.kind === "AV") {
for (const item of valve.pivots) {
item.pivot.rotation.z = item.sign * amount * 1.28;
}
} else {
for (const item of valve.pivots) {
item.cusp.rotation.y = amount * 1.18;
item.pivot.rotation.z =
item.baseAngle + amount * item.sign * 0.12;
}
}
valve.ringMaterial.emissiveIntensity = 0.18 + amount * 1.25;
valve.halo.material.opacity = 0.16 + amount * 0.42;
valve.aperture.material.opacity = 0.25 - amount * 0.16;
}
// ------------------------------------------------------------
// Directional flow paths
//
// Gated paths contain no visible arrows or flow line unless the
// corresponding valve is sufficiently open. This is a hard
// visibility gate rather than an opacity fade.
// ------------------------------------------------------------
const flows = [];
function createFlow(id, points, color, gated = false, rate = 0.25, arrowCount = 3) {
const curve = new THREE.CatmullRomCurve3(
points.map(point => new THREE.Vector3(...point)),
false,
"catmullrom",
0.35
);
const lineMaterial = new THREE.MeshBasicMaterial({
color,
transparent: true,
opacity: gated ? 0 : 0.3,
depthWrite: false,
toneMapped: false
});
const line = new THREE.Mesh(
new THREE.TubeGeometry(curve, 80, 0.021, 6, false),
lineMaterial
);
line.renderOrder = 4;
cardiac.add(line);
const arrowGeometry = new THREE.ConeGeometry(0.09, 0.24, 12);
const arrows = [];
for (let i = 0; i < arrowCount; i++) {
const arrow = new THREE.Mesh(
arrowGeometry,
new THREE.MeshBasicMaterial({
color,
transparent: true,
opacity: 0.97,
depthTest: false,
depthWrite: false,
toneMapped: false
})
);
arrow.renderOrder = 10;
arrow.visible = false;
cardiac.add(arrow);
arrows.push(arrow);
}
const flow = {
id,
curve,
line,
lineMaterial,
arrows,
gated,
rate,
arrowCount,
localTime: 0,
active: false
};
flows.push(flow);
return flow;
}
// Body -> right atrium: no valve on this segment.
createFlow("body-ra", [
[-3.03, 2.58, 0.28],
[-2.8, 1.66, 0.28],
[-2.28, 1.0, 0.28],
[-1.58, 0.82, 0.58],
[-1.12, 0.82, 0.76]
], DEOXY_LIGHT, false, 0.13, 3);
// Right atrium -> tricuspid -> right ventricle.
createFlow("ra-rv", [
[-1.12, 0.82, 0.78],
[-1.12, 0.54, 0.79],
[-1.12, 0.09, 0.80],
[-1.05, -0.36, 0.79],
[-1.02, -0.86, 0.77]
], DEOXY_LIGHT, true, 0.34, 2);
// Right ventricle -> pulmonary valve -> lungs.
createFlow("rv-lungs", [
[-1.02, -0.86, 0.78],
[-0.74, -0.26, 0.79],
[-0.35, 0.43, 0.81],
[-0.06, 1.18, 0.55],
[0.76, 2.1, 0.30],
[1.84, 2.61, 0.28]
], DEOXY_LIGHT, true, 0.14, 4);
// Lungs -> left atrium: no valve on this segment.
createFlow("lungs-la", [
[3.21, 1.88, 0.27],
[2.45, 1.57, 0.28],
[1.76, 1.18, 0.29],
[1.25, 0.92, 0.58],
[0.95, 0.84, 0.76]
], OXY_LIGHT, false, 0.13, 3);
// Left atrium -> mitral -> left ventricle.
createFlow("la-lv", [
[0.95, 0.84, 0.78],
[0.97, 0.51, 0.79],
[0.98, 0.12, 0.80],
[1.0, -0.38, 0.79],
[1.02, -0.88, 0.77]
], OXY_LIGHT, true, 0.34, 2);
// Left ventricle -> aortic valve -> body.
createFlow("lv-body", [
[1.02, -0.88, 0.78],
[1.16, -0.3, 0.79],
[1.25, 0.46, 0.81],
[1.47, 1.28, 0.54],
[2.08, 2.12, 0.30],
[3.07, 2.67, 0.28]
], OXY_LIGHT, true, 0.14, 4);
const flowById = Object.fromEntries(flows.map(flow => [flow.id, flow]));
const yAxis = new THREE.Vector3(0, 1, 0);
function updateFlow(flow, active, simulationDelta) {
const wasActive = flow.active;
flow.active = active;
if (active) {
if (!wasActive) flow.localTime = 0;
flow.localTime += simulationDelta;
flow.line.visible = true;
flow.lineMaterial.opacity = flow.gated ? 0.53 : 0.30;
for (let i = 0; i < flow.arrows.length; i++) {
const progress =
(flow.localTime * flow.rate + i / flow.arrowCount) % 1;
const point = flow.curve.getPointAt(progress);
const tangent = flow.curve.getTangentAt(progress).normalize();
const arrow = flow.arrows[i];
arrow.visible = true;
arrow.position.copy(point);
arrow.quaternion.setFromUnitVectors(yAxis, tangent);
}
} else {
// A gated path is completely removed from the scene when its
// valve is not safely open. No arrow can survive a closure.
flow.localTime = 0;
flow.line.visible = !flow.gated;
flow.lineMaterial.opacity = flow.gated ? 0 : 0.30;
for (const arrow of flow.arrows) {
arrow.visible = false;
}
}
}
// ------------------------------------------------------------
// Projected HTML labels
// ------------------------------------------------------------
const labelDefinitions = [
{
title: "RIGHT ATRIUM",
sub: "VENOUS IN",
position: [-1.93, 1.23, 0.8],
color: "blue"
},
{
title: "RIGHT VENTRICLE",
sub: "PUMPS TO LUNGS",
position: [-1.94, -0.73, 0.8],
color: "blue"
},
{
title: "LEFT ATRIUM",
sub: "PULMONARY IN",
position: [1.65, 1.27, 0.8],
color: "red"
},
{
title: "LEFT VENTRICLE",
sub: "PUMPS TO BODY",
position: [1.79, -0.77, 0.8],
color: "red"
},
{
title: "LUNGS",
sub: "PULMONARY",
position: [1.87, 2.73, 0.25],
color: "blue",
minor: true
},
{
title: "BODY",
sub: "SYSTEMIC",
position: [3.17, 2.79, 0.25],
color: "red",
minor: true
}
];
const labelLayer = document.querySelector("#labels");
const projectedLabels = [];
for (const definition of labelDefinitions) {
const element = document.createElement("div");
element.className =
`world-label ${definition.color}${definition.minor ? " minor" : ""}`;
element.innerHTML = `
<span class="dot"></span>
<span class="label-copy">
<b>${definition.title}</b>
<small>${definition.sub}</small>
</span>
`;
labelLayer.appendChild(element);
projectedLabels.push({
element,
position: new THREE.Vector3(...definition.position)
});
}
function updateLabels() {
const width = renderer.domElement.clientWidth;
const height = renderer.domElement.clientHeight;
for (const label of projectedLabels) {
const projected = label.position.clone().project(camera);
const visible =
projected.z > -1 &&
projected.z < 1 &&
projected.x > -1.15 &&
projected.x < 1.15 &&
projected.y > -1.15 &&
projected.y < 1.15;
label.element.style.opacity = visible ? "1" : "0";
if (!visible) continue;
label.element.style.left =
`${(projected.x * 0.5 + 0.5) * width}px`;
label.element.style.top =
`${(-projected.y * 0.5 + 0.5) * height}px`;
}
}
// ------------------------------------------------------------
// UI state
// ------------------------------------------------------------
const phaseName = document.querySelector("#phaseName");
const phaseDescription = document.querySelector("#phaseDescription");
const progressFill = document.querySelector("#progressFill");
const playPause = document.querySelector("#playPause");
const valveRows = {
tricuspid: document.querySelector("#valve-tricuspid"),
mitral: document.querySelector("#valve-mitral"),
pulmonary: document.querySelector("#valve-pulmonary"),
aortic: document.querySelector("#valve-aortic")
};
const flowChips = {
"body-ra": document.querySelector("#flow-body-ra"),
"ra-rv": document.querySelector("#flow-ra-rv"),
"rv-lungs": document.querySelector("#flow-rv-lungs"),
"lungs-la": document.querySelector("#flow-lungs-la"),
"la-lv": document.querySelector("#flow-la-lv"),
"lv-body": document.querySelector("#flow-lv-body")
};
function updateValveUI(id, amount) {
const row = valveRows[id];
const isOpen = amount > 0.5;
row.classList.toggle("open", isOpen);
row.querySelector(".status").textContent = isOpen ? "OPEN" : "CLOSED";
}
function setChip(id, active) {
flowChips[id].classList.toggle("active", active);
}
function getPhase(progress) {
if (progress < 0.18) {
return {
name: "Atrial systole",
description: "The atria contract and fill the ventricles through the open AV valves."
};
}
if (progress < 0.24) {
return {
name: "Isovolumetric contraction",
description: "All four valves close while ventricular pressure rises."
};
}
if (progress < 0.52) {
return {
name: "Ventricular systole",
description: "The ventricles eject blood through the open pulmonary and aortic valves."
};
}
if (progress < 0.59) {
return {
name: "Isovolumetric relaxation",
description: "The ventricular outlets close before the chambers refill."
};
}
if (progress < 0.88) {
return {
name: "Ventricular filling",
description: "The AV valves open for passive filling as the ventricles relax."
};
}
return {
name: "Diastasis",
description: "The heart briefly rests before the next atrial contraction."
};
}
// ------------------------------------------------------------
// Pumping-cycle timing
// ------------------------------------------------------------
const cycleDuration = 5.4;
let elapsed = 0;
let speed = 1;
let playing = true;
const clamp01 = value => Math.max(0, Math.min(1, value));
function smoothstep(edge0, edge1, value) {
const t = clamp01((value - edge0) / (edge1 - edge0));
return t * t * (3 - 2 * t);
}
function avValveOpen(progress) {
// Atrial systole: active AV flow.
if (progress < 0.18) {
return (
smoothstep(0.0, 0.025, progress) *
(1 - smoothstep(0.145, 0.18, progress))
);
}
// Early ventricular diastole: passive AV filling.
if (progress >= 0.59 && progress <= 0.88) {
return (
smoothstep(0.59, 0.625, progress) *
(1 - smoothstep(0.845, 0.88, progress))
);
}
return 0;
}
function semilunarValveOpen(progress) {
if (progress < 0.24 || progress > 0.52) return 0;
return (
smoothstep(0.24, 0.27, progress) *
(1 - smoothstep(0.485, 0.52, progress))
);
}
function updateCycleUI(progress) {
const phase = getPhase(progress);
phaseName.textContent = phase.name;
phaseDescription.textContent = phase.description;
progressFill.style.width = `${progress * 100}%`;
}
// ------------------------------------------------------------
// Direct camera controls — no external controls addon
// ------------------------------------------------------------
let dragging = false;
let pointerX = 0;
let pointerY = 0;
canvas.addEventListener("pointerdown", event => {
dragging = true;
pointerX = event.clientX;
pointerY = event.clientY;
canvas.setPointerCapture(event.pointerId);
});
canvas.addEventListener("pointermove", event => {
if (!dragging) return;
const deltaX = event.clientX - pointerX;
const deltaY = event.clientY - pointerY;
yaw -= deltaX * 0.006;
pitch = THREE.MathUtils.clamp(
pitch + deltaY * 0.005,
-0.48,
0.55
);
pointerX = event.clientX;
pointerY = event.clientY;
updateCamera();
});
function stopDragging(event) {
dragging = false;
if (event.pointerId !== undefined && canvas.hasPointerCapture(event.pointerId)) {
canvas.releasePointerCapture(event.pointerId);
}
}
canvas.addEventListener("pointerup", stopDragging);
canvas.addEventListener("pointercancel", stopDragging);
canvas.addEventListener(
"wheel",
event => {
event.preventDefault();
cameraRadius = THREE.MathUtils.clamp(
cameraRadius + event.deltaY * 0.008,
7.3,
15.5
);
updateCamera();
},
{ passive: false }
);
function resetView() {
yaw = 0;
pitch = 0.08;
cameraRadius = 10.4;
target.set(-0.38, 0.02, 0);
updateCamera();
}
canvas.addEventListener("dblclick", resetView);
document.querySelector("#resetView").addEventListener("click", resetView);
playPause.addEventListener("click", () => {
playing = !playing;
playPause.textContent = playing ? "Pause" : "Resume";
playPause.setAttribute("aria-pressed", String(!playing));
});
document.querySelectorAll(".speed").forEach(button => {
button.addEventListener("click", () => {
speed = Number(button.dataset.speed);
document.querySelectorAll(".speed").forEach(other => {
other.classList.toggle("selected", other === button);
});
});
});
const wallOpacity = document.querySelector("#wallOpacity");
const wallValue = document.querySelector("#wallValue");
wallOpacity.addEventListener("input", () => {
const value = Number(wallOpacity.value);
wallValue.textContent = `${value}%`;
for (const material of [tissueCap, tissueSide]) {
material.opacity = value / 100;
material.transparent = value < 100;
material.depthWrite = value > 88;
material.needsUpdate = true;
}
});
window.addEventListener("resize", () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
});
// ------------------------------------------------------------
// Render loop
// ------------------------------------------------------------
const clock = new THREE.Clock();
renderer.setAnimationLoop(() => {
const realDelta = Math.min(clock.getDelta(), 0.05);
const simulationDelta = playing ? realDelta * speed : 0;
elapsed = (elapsed + simulationDelta) % cycleDuration;
const progress = elapsed / cycleDuration;
const avOpen = avValveOpen(progress);
const semilunarOpen = semilunarValveOpen(progress);
setValveOpen(valves.tricuspid, avOpen);
setValveOpen(valves.mitral, avOpen);
setValveOpen(valves.pulmonary, semilunarOpen);
setValveOpen(valves.aortic, semilunarOpen);
// The visibility threshold keeps arrows away from a valve
// throughout its opening and closing transition.
const avFlowActive = avOpen > 0.9;
const semilunarFlowActive = semilunarOpen > 0.9;
updateFlow(flowById["body-ra"], true, simulationDelta);
updateFlow(flowById["ra-rv"], avFlowActive, simulationDelta);
updateFlow(flowById["rv-lungs"], semilunarFlowActive, simulationDelta);
updateFlow(flowById["lungs-la"], true, simulationDelta);
updateFlow(flowById["la-lv"], avFlowActive, simulationDelta);
updateFlow(flowById["lv-body"], semilunarFlowActive, simulationDelta);
updateValveUI("tricuspid", avOpen);
updateValveUI("mitral", avOpen);
updateValveUI("pulmonary", semilunarOpen);
updateValveUI("aortic", semilunarOpen);
setChip("body-ra", true);
setChip("ra-rv", avFlowActive);
setChip("rv-lungs", semilunarFlowActive);
setChip("lungs-la", true);
setChip("la-lv", avFlowActive);
setChip("lv-body", semilunarFlowActive);
updateCycleUI(progress);
// Small mechanical contraction of the myocardium.
let contraction = 0;
if (progress < 0.52) {
const q = progress / 0.52;
contraction = Math.exp(-Math.pow((q - 0.30) / 0.12, 2));
} else {
const q = (progress - 0.52) / 0.48;
contraction = 0.13 * Math.exp(-Math.pow((q - 0.25) / 0.16, 2));
}
cardiac.scale.setScalar(1 + contraction * 0.009);
particles.rotation.y += realDelta * 0.006;
updateLabels();
renderer.render(scene, camera);
});
</script>
</body>
</html>
```