Prompt specification
Build or customize the Three.js 3D scene "Pressure-volume breathing explorer" inside the active workspace.
Engineering Constraints:
1. Runtime & Orbit: Use Three.js r160 with OrbitControls (damping enabled) and ACESFilmicToneMapping.
2. Frame Timing: Drive all motion strictly with clock.getDelta() so animation speed is FPS-independent.
3. Viewport Hygiene: Handle window resize cleanly, cap DPR at Math.min(window.devicePixelRatio, 2), and dispose geometries/materials/renderer on unmount.
Scene Metadata:
Scene: Pressure-volume breathing explorer
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/pressure-volume-breathing-explorer-x0964
Scene Description Prompt:
Please use Three.js from cdnjs for a voxel scene based on this idea: pressure-volume breathing explorer. Provide a toy thoracic-volume control and visualize the resulting pressure difference and airflow direction; label the model qualitative and avoid presenting respiratory measurements or diagnostic thresholds. Return one self-contained HTML file.
Working Three.js Starter (pressure-volume-breathing-explorer-x0964):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Pressure–Volume Breathing Explorer</title>
<style>
:root {
color-scheme: dark;
--bg: #07131d;
--panel: rgba(10, 27, 39, 0.82);
--panel-border: rgba(157, 220, 231, 0.17);
--text: #eaf8f7;
--muted: #91aeb9;
--cyan: #5ce7e0;
--cyan-soft: rgba(92, 231, 224, 0.15);
--amber: #ffb767;
--amber-soft: rgba(255, 183, 103, 0.15);
--neutral: #9bb9c5;
--shadow: 0 20px 50px rgba(0, 0, 0, 0.28);
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: var(--bg);
color: var(--text);
font-family:
Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
button,
input {
font: inherit;
}
#scene {
position: fixed;
inset: 0;
width: 100%;
height: 100%;
display: block;
outline: none;
cursor: grab;
touch-action: none;
}
#scene.dragging {
cursor: grabbing;
}
.vignette {
position: fixed;
inset: 0;
pointer-events: none;
background:
radial-gradient(circle at 52% 42%, transparent 25%, rgba(1, 8, 13, 0.14) 65%, rgba(1, 7, 11, 0.58) 100%),
linear-gradient(180deg, rgba(4, 15, 24, 0.12), rgba(4, 15, 24, 0.3));
}
.topbar {
position: fixed;
top: 24px;
left: 24px;
z-index: 10;
width: min(440px, calc(100vw - 48px));
pointer-events: none;
}
.eyebrow {
color: var(--cyan);
font-size: 0.68rem;
font-weight: 800;
letter-spacing: 0.16em;
text-transform: uppercase;
}
.qualitative-chip {
display: inline-flex;
align-items: center;
gap: 8px;
margin-bottom: 10px;
padding: 7px 10px;
border: 1px solid rgba(92, 231, 224, 0.35);
border-radius: 999px;
background: rgba(8, 30, 40, 0.76);
color: #c9fffb;
font-size: 0.68rem;
font-weight: 800;
letter-spacing: 0.12em;
text-transform: uppercase;
backdrop-filter: blur(12px);
}
.qualitative-chip::before {
width: 7px;
height: 7px;
border-radius: 50%;
background: var(--cyan);
box-shadow: 0 0 12px var(--cyan);
content: "";
}
h1 {
margin: 0;
max-width: 430px;
font-size: clamp(1.8rem, 3.2vw, 3.15rem);
font-weight: 720;
line-height: 0.98;
letter-spacing: -0.045em;
text-wrap: balance;
}
.subtitle {
max-width: 410px;
margin: 12px 0 0;
color: var(--muted);
font-size: 0.9rem;
line-height: 1.5;
}
.panel {
position: fixed;
z-index: 20;
border: 1px solid var(--panel-border);
border-radius: 18px;
background: var(--panel);
box-shadow: var(--shadow);
backdrop-filter: blur(18px) saturate(125%);
}
.controls {
top: 210px;
left: 24px;
width: 340px;
padding: 18px;
}
.panel-heading {
display: flex;
align-items: center;
justify-content: space-between;
gap: 12px;
margin-bottom: 16px;
}
.panel-heading h2 {
margin: 0;
font-size: 1rem;
font-weight: 680;
letter-spacing: -0.015em;
}
.panel-heading .mini-label {
color: var(--muted);
font-size: 0.67rem;
font-weight: 700;
letter-spacing: 0.09em;
text-transform: uppercase;
}
.range-wrap {
padding: 7px 3px 0;
}
input[type="range"] {
--position: 50%;
width: 100%;
height: 9px;
margin: 0;
appearance: none;
border-radius: 3px;
background:
linear-gradient(
90deg,
#3a8e91 0%,
#7de1d7 var(--position),
#263f4c var(--position),
#263f4c 100%
);
cursor: ew-resize;
}
input[type="range"]::-webkit-slider-thumb {
width: 24px;
height: 24px;
appearance: none;
border: 4px solid #14323e;
border-radius: 5px;
background: #eafffb;
box-shadow:
0 0 0 1px rgba(92, 231, 224, 0.7),
0 5px 18px rgba(0, 0, 0, 0.38);
}
input[type="range"]::-moz-range-thumb {
width: 18px;
height: 18px;
border: 4px solid #14323e;
border-radius: 5px;
background: #eafffb;
box-shadow:
0 0 0 1px rgba(92, 231, 224, 0.7),
0 5px 18px rgba(0, 0, 0, 0.38);
}
input[type="range"]:focus-visible {
outline: 2px solid var(--cyan);
outline-offset: 7px;
}
.range-labels {
display: flex;
justify-content: space-between;
margin-top: 11px;
color: var(--muted);
font-size: 0.68rem;
font-weight: 650;
letter-spacing: 0.03em;
}
.button-row {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 9px;
margin-top: 17px;
}
button {
min-height: 40px;
padding: 9px 12px;
border: 1px solid rgba(169, 224, 231, 0.2);
border-radius: 10px;
background: rgba(119, 186, 196, 0.08);
color: var(--text);
font-size: 0.76rem;
font-weight: 700;
cursor: pointer;
transition:
border-color 160ms ease,
background 160ms ease,
transform 160ms ease;
}
button:hover {
border-color: rgba(92, 231, 224, 0.45);
background: rgba(92, 231, 224, 0.12);
}
button:active {
transform: translateY(1px);
}
button:focus-visible {
outline: 2px solid var(--cyan);
outline-offset: 2px;
}
button.primary {
border-color: rgba(92, 231, 224, 0.38);
background: var(--cyan-soft);
color: #dffffc;
}
.relationship {
margin-top: 15px;
padding-top: 13px;
border-top: 1px solid rgba(157, 220, 231, 0.1);
color: var(--muted);
font-size: 0.72rem;
line-height: 1.5;
}
.relationship strong {
color: #d5ecef;
font-weight: 700;
}
.readout {
top: 24px;
right: 24px;
width: 330px;
padding: 18px;
}
.state-line {
display: flex;
align-items: center;
gap: 9px;
margin-top: 8px;
}
.state-dot {
width: 10px;
height: 10px;
border-radius: 50%;
background: var(--neutral);
box-shadow: 0 0 14px rgba(155, 185, 197, 0.65);
}
.state-name {
font-size: 1.65rem;
font-weight: 730;
letter-spacing: -0.035em;
}
.readout-block {
margin-top: 17px;
padding-top: 15px;
border-top: 1px solid rgba(157, 220, 231, 0.1);
}
.readout-label {
color: var(--muted);
font-size: 0.68rem;
font-weight: 750;
letter-spacing: 0.09em;
text-transform: uppercase;
}
.pressure-value {
display: block;
margin-top: 5px;
font-size: 1rem;
font-weight: 720;
}
.pressure-meter {
position: relative;
height: 10px;
margin: 13px 0 7px;
border: 1px solid rgba(255, 255, 255, 0.09);
border-radius: 999px;
background:
linear-gradient(
90deg,
rgba(92, 231, 224, 0.82) 0%,
rgba(92, 231, 224, 0.32) 42%,
rgba(155, 185, 197, 0.28) 49%,
rgba(155, 185, 197, 0.28) 51%,
rgba(255, 183, 103, 0.32) 58%,
rgba(255, 183, 103, 0.82) 100%
);
}
.pressure-meter::after {
position: absolute;
top: -4px;
bottom: -4px;
left: 50%;
width: 1px;
background: rgba(255, 255, 255, 0.55);
content: "";
}
#pressureNeedle {
position: absolute;
top: 50%;
left: 50%;
width: 16px;
height: 16px;
border: 3px solid #0d2530;
border-radius: 50%;
background: var(--neutral);
box-shadow: 0 0 12px currentColor;
transform: translate(-50%, -50%);
transition:
left 100ms ease,
background 150ms ease;
}
.meter-labels {
display: flex;
justify-content: space-between;
color: var(--muted);
font-size: 0.64rem;
}
.flow-card {
display: grid;
grid-template-columns: 38px 1fr;
align-items: center;
gap: 11px;
margin-top: 17px;
padding: 12px;
border: 1px solid rgba(157, 220, 231, 0.11);
border-radius: 12px;
background: rgba(255, 255, 255, 0.025);
}
.flow-symbol {
display: grid;
width: 36px;
height: 36px;
place-items: center;
border-radius: 9px;
background: rgba(155, 185, 197, 0.1);
color: var(--neutral);
font-size: 1.45rem;
font-weight: 800;
}
.flow-title {
margin-top: 3px;
font-size: 0.83rem;
font-weight: 720;
}
.flow-detail {
margin-top: 2px;
color: var(--muted);
font-size: 0.68rem;
line-height: 1.35;
}
.legend {
right: 24px;
bottom: 24px;
width: 330px;
padding: 15px 17px;
}
.legend-row {
display: flex;
align-items: flex-start;
gap: 10px;
margin: 8px 0;
color: #c8dde2;
font-size: 0.73rem;
line-height: 1.4;
}
.swatch {
flex: 0 0 auto;
width: 10px;
height: 10px;
margin-top: 3px;
border-radius: 2px;
box-shadow: 0 0 10px currentColor;
}
.swatch.cyan {
background: var(--cyan);
color: var(--cyan);
}
.swatch.amber {
background: var(--amber);
color: var(--amber);
}
.swatch.neutral {
background: var(--neutral);
color: var(--neutral);
}
.disclaimer {
margin: 12px 0 0;
padding-top: 11px;
border-top: 1px solid rgba(157, 220, 231, 0.1);
color: #829faa;
font-size: 0.67rem;
line-height: 1.5;
}
.scene-label {
position: fixed;
z-index: 8;
padding: 6px 8px;
border: 1px solid rgba(167, 222, 230, 0.18);
border-radius: 7px;
background: rgba(6, 19, 28, 0.72);
color: #b8d7de;
font-size: 0.62rem;
font-weight: 800;
letter-spacing: 0.1em;
text-transform: uppercase;
white-space: nowrap;
pointer-events: none;
transform: translate(-50%, -50%);
backdrop-filter: blur(8px);
}
.scene-label.pressure {
color: var(--neutral);
}
.hint {
position: fixed;
bottom: 24px;
left: 24px;
z-index: 9;
padding: 9px 12px;
border: 1px solid rgba(157, 220, 231, 0.12);
border-radius: 999px;
background: rgba(7, 22, 31, 0.65);
color: #829faa;
font-size: 0.68rem;
pointer-events: none;
backdrop-filter: blur(10px);
}
#loading {
position: fixed;
inset: 0;
z-index: 100;
display: grid;
place-items: center;
background: #07131d;
color: var(--cyan);
font-size: 0.78rem;
font-weight: 750;
letter-spacing: 0.12em;
text-transform: uppercase;
transition: opacity 300ms ease;
}
#loading.is-hidden {
opacity: 0;
pointer-events: none;
}
@media (max-width: 820px) {
.topbar {
top: 14px;
left: 14px;
width: calc(100vw - 28px);
}
h1 {
max-width: 360px;
font-size: clamp(1.6rem, 7vw, 2.25rem);
}
.subtitle {
display: none;
}
.qualitative-chip {
margin-bottom: 7px;
padding: 5px 8px;
}
.readout {
top: 104px;
right: 12px;
width: 225px;
padding: 13px;
border-radius: 14px;
}
.state-name {
font-size: 1.2rem;
}
.readout-block {
margin-top: 11px;
padding-top: 10px;
}
.flow-card {
grid-template-columns: 30px 1fr;
gap: 8px;
margin-top: 11px;
padding: 9px;
}
.flow-symbol {
width: 29px;
height: 29px;
font-size: 1.1rem;
}
.controls {
top: auto;
right: 12px;
bottom: 12px;
left: 12px;
width: auto;
padding: 13px;
border-radius: 14px;
}
.panel-heading {
margin-bottom: 9px;
}
.button-row {
margin-top: 11px;
}
.relationship,
.legend,
.hint,
.scene-label {
display: none;
}
}
@media (max-height: 680px) and (min-width: 821px) {
.controls {
top: 170px;
}
.relationship {
display: none;
}
.legend {
padding: 11px 14px;
}
.disclaimer {
margin-top: 8px;
padding-top: 8px;
}
}
</style>
</head>
<body>
<canvas
id="scene"
tabindex="0"
role="application"
aria-label="Interactive qualitative voxel breathing model. Drag to orbit, scroll to zoom, and use the thoracic volume control."
></canvas>
<div class="vignette"></div>
<header class="topbar">
<div class="qualitative-chip">Qualitative model</div>
<h1>Pressure–Volume Breathing Explorer</h1>
<p class="subtitle">
Explore how a change in thoracic volume creates a relative pressure
difference and a modeled direction of airflow.
</p>
</header>
<section class="panel controls" aria-label="Toy model controls">
<div class="panel-heading">
<h2>Toy thoracic volume</h2>
<span class="mini-label">Drag control</span>
</div>
<div class="range-wrap">
<input
id="volumeSlider"
type="range"
min="0"
max="100"
value="50"
aria-label="Toy thoracic volume"
aria-valuetext="Balanced toy thoracic volume"
/>
<div class="range-labels" aria-hidden="true">
<span>Smaller</span>
<span>Balanced</span>
<span>Larger</span>
</div>
</div>
<div class="button-row">
<button id="demoButton" class="primary" type="button">Play gentle cycle</button>
<button id="centerButton" type="button">Return to balance</button>
</div>
<div class="relationship">
<strong>Model relationship:</strong>
larger volume → lower inside pressure → inward air;
smaller volume → higher inside pressure → outward air.
</div>
</section>
<section class="panel readout" aria-live="polite" aria-label="Qualitative model state">
<div class="eyebrow">Relative model state</div>
<div class="state-line">
<span id="stateDot" class="state-dot"></span>
<span id="volumeState" class="state-name">Balanced</span>
</div>
<div class="readout-block">
<div class="readout-label">Inside relative to outside</div>
<strong id="pressureState" class="pressure-value">Near balance</strong>
<div class="pressure-meter" aria-hidden="true">
<span id="pressureNeedle"></span>
</div>
<div class="meter-labels" aria-hidden="true">
<span>Lower inside</span>
<span>Higher inside</span>
</div>
</div>
<div class="flow-card">
<div id="flowSymbol" class="flow-symbol" aria-hidden="true">○</div>
<div>
<div class="readout-label">Modeled airflow</div>
<div id="flowTitle" class="flow-title">No net direction</div>
<div id="flowDetail" class="flow-detail">The pressure difference is near balance.</div>
</div>
</div>
</section>
<aside class="panel legend" aria-label="Visualization legend">
<div class="eyebrow">Scene legend</div>
<div class="legend-row">
<span class="swatch cyan"></span>
<span><strong>Cyan:</strong> lower inside pressure and inward airflow.</span>
</div>
<div class="legend-row">
<span class="swatch amber"></span>
<span><strong>Amber:</strong> higher inside pressure and outward airflow.</span>
</div>
<div class="legend-row">
<span class="swatch neutral"></span>
<span><strong>Neutral:</strong> no strong modeled directional bias.</span>
</div>
<p class="disclaimer">
Illustrative relationships only. This toy does not report respiratory
measurements, diagnostic thresholds, or individual medical guidance.
</p>
</aside>
<div id="airwayLabel" class="scene-label">Airway</div>
<div id="lungLabel" class="scene-label">Voxel lungs</div>
<div id="pressureLabel" class="scene-label pressure">Near balance</div>
<div class="hint">Drag to orbit · Scroll to zoom · Double-click to reset</div>
<div id="loading">Building qualitative voxel model…</div>
<script type="module">
import * as THREE from "https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const canvas = document.getElementById("scene");
const loading = document.getElementById("loading");
const volumeSlider = document.getElementById("volumeSlider");
const demoButton = document.getElementById("demoButton");
const centerButton = document.getElementById("centerButton");
const volumeState = document.getElementById("volumeState");
const pressureState = document.getElementById("pressureState");
const pressureNeedle = document.getElementById("pressureNeedle");
const stateDot = document.getElementById("stateDot");
const flowSymbol = document.getElementById("flowSymbol");
const flowTitle = document.getElementById("flowTitle");
const flowDetail = document.getElementById("flowDetail");
const pressureLabel = document.getElementById("pressureLabel");
const airwayLabel = document.getElementById("airwayLabel");
const lungLabel = document.getElementById("lungLabel");
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, false);
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.08;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x07131d);
scene.fog = new THREE.Fog(0x07131d, 14, 31);
const camera = new THREE.PerspectiveCamera(
39,
window.innerWidth / window.innerHeight,
0.1,
80
);
const cameraTarget = new THREE.Vector3(0, 2.65, 0);
let cameraYaw = 0.53;
let cameraElevation = 0.13;
let cameraRadius = 11.8;
const hemisphere = new THREE.HemisphereLight(0xc9f6ff, 0x10202a, 2.15);
scene.add(hemisphere);
const keyLight = new THREE.DirectionalLight(0xf5ffff, 3.25);
keyLight.position.set(6, 10, 8);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(1024, 1024);
keyLight.shadow.camera.left = -8;
keyLight.shadow.camera.right = 8;
keyLight.shadow.camera.top = 10;
keyLight.shadow.camera.bottom = -5;
keyLight.shadow.camera.near = 0.5;
keyLight.shadow.camera.far = 30;
keyLight.shadow.bias = -0.00035;
keyLight.target.position.set(0, 2, 0);
scene.add(keyLight, keyLight.target);
const cyanLight = new THREE.PointLight(0x57e8ff, 24, 19, 2);
cyanLight.position.set(-5, 5, 4);
scene.add(cyanLight);
const amberLight = new THREE.PointLight(0xffaa5e, 17, 18, 2);
amberLight.position.set(5, 3.5, -2);
scene.add(amberLight);
const floor = new THREE.Mesh(
new THREE.PlaneGeometry(42, 42),
new THREE.MeshStandardMaterial({
color: 0x07131b,
roughness: 0.96,
metalness: 0
})
);
floor.rotation.x = -Math.PI / 2;
floor.position.y = -0.04;
floor.receiveShadow = true;
scene.add(floor);
const grid = new THREE.GridHelper(26, 26, 0x294b5b, 0x122b38);
grid.position.y = 0.005;
grid.material.transparent = true;
grid.material.opacity = 0.5;
scene.add(grid);
const modelRoot = new THREE.Group();
scene.add(modelRoot);
const lungRoot = new THREE.Group();
lungRoot.position.y = 2.43;
modelRoot.add(lungRoot);
const cageRoot = new THREE.Group();
cageRoot.position.y = 2.43;
modelRoot.add(cageRoot);
const diaphragmRoot = new THREE.Group();
diaphragmRoot.position.y = 0.5;
modelRoot.add(diaphragmRoot);
const airwayRoot = new THREE.Group();
modelRoot.add(airwayRoot);
const neutralLungColors = [
new THREE.Color(0x63c9c3),
new THREE.Color(0x86ddd5),
new THREE.Color(0x49aaa9)
];
const inhaleLungColors = [
new THREE.Color(0x54e3df),
new THREE.Color(0x8af4ea),
new THREE.Color(0x36c4c2)
];
const exhaleLungColors = [
new THREE.Color(0xd69b6d),
new THREE.Color(0xf0bb87),
new THREE.Color(0xbf7958)
];
const lungMaterials = neutralLungColors.map(
(color, index) =>
new THREE.MeshStandardMaterial({
color,
roughness: 0.67,
metalness: 0.02,
emissive: color.clone().multiplyScalar(0.12),
emissiveIntensity: 0.75
})
);
const cageMaterial = new THREE.MeshStandardMaterial({
color: 0xd6e1df,
roughness: 0.72,
metalness: 0.04
});
const airwayMaterial = new THREE.MeshStandardMaterial({
color: 0xb9e7eb,
roughness: 0.45,
metalness: 0.05,
emissive: 0x28525b,
emissiveIntensity: 0.55
});
const diaphragmMaterial = new THREE.MeshStandardMaterial({
color: 0xb86f65,
roughness: 0.78,
metalness: 0
});
const baseMaterial = new THREE.MeshStandardMaterial({
color: 0x17333f,
roughness: 0.86,
metalness: 0.08
});
const dummy = new THREE.Object3D();
function createVoxelBatch(parent, entries, size, materials) {
const buckets = materials.map(() => []);
for (const entry of entries) {
buckets[entry.material ?? 0].push(entry);
}
const geometry = new THREE.BoxGeometry(size, size, size);
buckets.forEach((bucket, materialIndex) => {
if (!bucket.length) return;
const mesh = new THREE.InstancedMesh(
geometry,
materials[materialIndex],
bucket.length
);
bucket.forEach((entry, index) => {
const position = entry.position;
const scale = entry.scale ?? [1, 1, 1];
dummy.position.set(position[0], position[1], position[2]);
dummy.rotation.set(0, 0, 0);
dummy.scale.set(scale[0], scale[1], scale[2]);
dummy.updateMatrix();
mesh.setMatrixAt(index, dummy.matrix);
});
mesh.instanceMatrix.needsUpdate = true;
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
});
}
// Voxel lungs with a central airway gap.
const lungEntries = [];
for (const side of [-1, 1]) {
for (let ix = 0; ix < 8; ix += 1) {
const xAbs = 0.48 + ix * 0.265;
for (let iy = 0; iy < 11; iy += 1) {
const y = 0.82 + iy * 0.3;
const normalizedY = (y - 2.25) / 1.65;
const width =
1.82 * Math.sqrt(Math.max(0.04, 1 - normalizedY * normalizedY));
const radial = (xAbs - 0.45) / width;
if (radial > 1) continue;
if (xAbs < 0.88 && y > 3.2) continue;
for (let iz = 0; iz < 3; iz += 1) {
const z = (iz - 1) * 0.42;
const depth = z / 0.58;
const shape =
Math.pow(radial, 1.8) +
Math.pow(Math.abs(normalizedY), 1.9) +
Math.pow(Math.abs(depth), 1.6);
if (shape > 1.08) continue;
lungEntries.push({
position: [side * xAbs, y - 2.43, z],
material: (ix + iy * 2 + iz + (side > 0 ? 1 : 0)) % 3
});
}
}
}
}
createVoxelBatch(lungRoot, lungEntries, 0.27, lungMaterials);
// A stylized rib cage made from discrete cubes.
const cageEntries = [];
for (let rib = 0; rib < 9; rib += 1) {
const baseY = -1.3 + rib * 0.34;
for (const side of [-1, 1]) {
for (let segment = 0; segment <= 18; segment += 1) {
const t = (segment / 18) * Math.PI;
const x = side * (0.24 + 2.0 * Math.sin(t));
const z = -0.93 * Math.cos(t);
const y = baseY + 0.1 * Math.sin(t * 2);
cageEntries.push({
position: [x, y, z],
scale: [1, 1, 1]
});
}
}
}
for (let y = -1.25; y <= 1.55; y += 0.22) {
cageEntries.push({ position: [0, y, 1.03] });
cageEntries.push({ position: [0, y, -1.03] });
}
for (let x = -2.05; x <= 2.05; x += 0.22) {
cageEntries.push({ position: [x, 1.58, 0.22] });
}
createVoxelBatch(cageRoot, cageEntries, 0.145, [cageMaterial]);
// Diaphragm-like voxel band.
const diaphragmEntries = [];
for (let x = -2.2; x <= 2.21; x += 0.42) {
diaphragmEntries.push({
position: [x, 0, -0.34],
scale: [1, 0.34, 1]
});
diaphragmEntries.push({
position: [x, 0, 0.34],
scale: [1, 0.34, 1]
});
}
createVoxelBatch(diaphragmRoot, diaphragmEntries, 0.29, [diaphragmMaterial]);
// Voxel base.
const baseEntries = [];
for (let x = -2; x <= 2; x += 1) {
for (let z = -1; z <= 1; z += 1) {
if (x * x + z * z * 1.4 > 5) continue;
baseEntries.push({
position: [x * 0.45, 0.1, z * 0.45],
scale: [1, 0.24, 1]
});
}
}
createVoxelBatch(scene, baseEntries, 0.43, [baseMaterial]);
// Square-section voxel airway and branching bronchi.
const airwayEntries = [];
for (let y = 4.22; y <= 6.32; y += 0.23) {
airwayEntries.push({ position: [-0.28, y, -0.28] });
airwayEntries.push({ position: [0.28, y, -0.28] });
airwayEntries.push({ position: [-0.28, y, 0.28] });
airwayEntries.push({ position: [0.28, y, 0.28] });
}
for (const side of [-1, 1]) {
for (let step = 0; step <= 10; step += 1) {
const t = step / 10;
airwayEntries.push({
position: [side * 1.65 * t, 4.27 - 0.98 * t, 0]
});
}
}
createVoxelBatch(airwayRoot, airwayEntries, 0.18, [airwayMaterial]);
// A translucent pressure field and wireframe boundary.
const pressureShellMaterial = new THREE.LineBasicMaterial({
color: 0x9bb9c5,
transparent: true,
opacity: 0.12,
depthWrite: false
});
const pressureShell = new THREE.LineSegments(
new THREE.EdgesGeometry(new THREE.BoxGeometry(5.4, 4.35, 2.6)),
pressureShellMaterial
);
pressureShell.position.y = 2.43;
pressureShell.renderOrder = 2;
modelRoot.add(pressureShell);
const pressureVolumeMaterial = new THREE.MeshBasicMaterial({
color: 0x9bb9c5,
transparent: true,
opacity: 0.025,
depthWrite: false,
side: THREE.BackSide
});
const pressureVolume = new THREE.Mesh(
new THREE.BoxGeometry(5.4, 4.35, 2.6),
pressureVolumeMaterial
);
pressureVolume.position.y = 2.43;
pressureVolume.renderOrder = 1;
modelRoot.add(pressureVolume);
// Blocky airflow arrows.
const flowArrowMaterial = new THREE.MeshStandardMaterial({
color: 0x5ce7e0,
emissive: 0x5ce7e0,
emissiveIntensity: 1.15,
roughness: 0.35,
transparent: true,
opacity: 0,
depthWrite: false
});
const arrowBox = new THREE.BoxGeometry(1, 1, 1);
function createVoxelArrow() {
const arrow = new THREE.Group();
const shaft = new THREE.Mesh(arrowBox, flowArrowMaterial);
shaft.scale.set(0.12, 0.65, 0.12);
shaft.position.y = 0.15;
arrow.add(shaft);
const headSizes = [0.3, 0.22, 0.14, 0.065];
headSizes.forEach((width, index) => {
const block = new THREE.Mesh(arrowBox, flowArrowMaterial);
block.scale.set(width, 0.13, width);
block.position.y = -0.22 - index * 0.13;
arrow.add(block);
});
arrow.visible = false;
arrow.renderOrder = 5;
modelRoot.add(arrow);
return arrow;
}
const flowArrows = [
{ object: createVoxelArrow(), kind: "central", y: 6.02 },
{ object: createVoxelArrow(), kind: "central", y: 5.22 }
];
for (const side of [-1, 1]) {
for (const t of [0.34, 0.72]) {
flowArrows.push({
object: createVoxelArrow(),
kind: "branch",
side,
t
});
}
}
// Flow particles are small moving voxels.
const particleCount = 40;
const flowParticleMaterial = new THREE.MeshStandardMaterial({
color: 0x5ce7e0,
emissive: 0x5ce7e0,
emissiveIntensity: 1.4,
roughness: 0.3,
transparent: true,
opacity: 0,
depthWrite: false
});
const flowParticles = new THREE.InstancedMesh(
new THREE.BoxGeometry(0.12, 0.12, 0.12),
flowParticleMaterial,
particleCount
);
flowParticles.frustumCulled = false;
flowParticles.visible = false;
flowParticles.renderOrder = 6;
modelRoot.add(flowParticles);
const particleData = Array.from({ length: particleCount }, (_, index) => ({
branch: index % 2 === 0 ? -1 : 1,
offset: (index * 0.173) % 1,
speed: 0.14 + (index % 5) * 0.008
}));
const cyan = new THREE.Color(0x5ce7e0);
const amber = new THREE.Color(0xffb767);
const neutral = new THREE.Color(0x9bb9c5);
let targetVolume = 0.5;
let displayedVolume = 0.5;
let demoActive = false;
let demoPhase = 0;
function updateReadout(volume) {
const delta = volume - 0.5;
const magnitude = Math.min(1, Math.abs(delta) * 2.8);
const direction = delta > 0.0001 ? "in" : delta < -0.0001 ? "out" : "neutral";
let volumeText = "Balanced";
if (direction === "in") volumeText = "Expanded";
if (direction === "out") volumeText = "Reduced";
volumeState.textContent = volumeText;
if (direction === "in") {
pressureState.textContent = "Lower inside";
pressureNeedle.style.left = `${50 - magnitude * 43}%`;
pressureNeedle.style.background = "#5ce7e0";
pressureNeedle.style.color = "#5ce7e0";
stateDot.style.background = "#5ce7e0";
stateDot.style.boxShadow = "0 0 14px #5ce7e0";
flowSymbol.textContent = "↓";
flowSymbol.style.color = "#5ce7e0";
flowSymbol.style.background = "rgba(92, 231, 224, 0.13)";
flowTitle.textContent = "Inward — toward the lungs";
flowDetail.textContent = "Lower inside pressure favors incoming air.";
pressureLabel.textContent = "Lower inside";
pressureLabel.style.color = "#5ce7e0";
pressureLabel.style.borderColor = "rgba(92, 231, 224, 0.35)";
} else if (direction === "out") {
pressureState.textContent = "Higher inside";
pressureNeedle.style.left = `${50 + magnitude * 43}%`;
pressureNeedle.style.background = "#ffb767";
pressureNeedle.style.color = "#ffb767";
stateDot.style.background = "#ffb767";
stateDot.style.boxShadow = "0 0 14px #ffb767";
flowSymbol.textContent = "↑";
flowSymbol.style.color = "#ffb767";
flowSymbol.style.background = "rgba(255, 183, 103, 0.13)";
flowTitle.textContent = "Outward — through the airway";
flowDetail.textContent = "Higher inside pressure favors outgoing air.";
pressureLabel.textContent = "Higher inside";
pressureLabel.style.color = "#ffb767";
pressureLabel.style.borderColor = "rgba(255, 183, 103, 0.35)";
} else {
pressureState.textContent = "Near balance";
pressureNeedle.style.left = "50%";
pressureNeedle.style.background = "#9bb9c5";
pressureNeedle.style.color = "#9bb9c5";
stateDot.style.background = "#9bb9c5";
stateDot.style.boxShadow = "0 0 14px rgba(155, 185, 197, 0.65)";
flowSymbol.textContent = "○";
flowSymbol.style.color = "#9bb9c5";
flowSymbol.style.background = "rgba(155, 185, 197, 0.1)";
flowTitle.textContent = "No modeled net direction";
flowDetail.textContent = "The relative pressure difference is near balance.";
pressureLabel.textContent = "Near balance";
pressureLabel.style.color = "#9bb9c5";
pressureLabel.style.borderColor = "rgba(155, 185, 197, 0.24)";
}
volumeSlider.setAttribute(
"aria-valuetext",
`${volumeText} toy thoracic volume`
);
}
function stopDemo() {
demoActive = false;
demoButton.textContent = "Play gentle cycle";
}
volumeSlider.addEventListener("input", () => {
stopDemo();
targetVolume = Number(volumeSlider.value) / 100;
});
centerButton.addEventListener("click", () => {
stopDemo();
targetVolume = 0.5;
volumeSlider.value = "50";
});
demoButton.addEventListener("click", () => {
demoActive = !demoActive;
if (demoActive) {
demoPhase = 0;
demoButton.textContent = "Pause cycle";
} else {
demoButton.textContent = "Resume cycle";
}
});
function flowPoint(u, branch, chestScale, target) {
if (u < 0.38) {
const t = u / 0.38;
target.set(0, 6.72 - t * 1.88, 0);
} else {
const t = (u - 0.38) / 0.62;
target.set(
branch * (0.18 + 1.58 * t) * chestScale,
4.84 - 1.7 * t,
0
);
}
return target;
}
const flowPointTemp = new THREE.Vector3();
const arrowDirection = new THREE.Vector3();
const downDirection = new THREE.Vector3(0, -1, 0);
function updateFlow(volume, elapsedTime, chestScale) {
const delta = volume - 0.5;
const magnitude = Math.min(1, Math.abs(delta) * 2.8);
const direction = delta > 0.0001 ? 1 : delta < -0.0001 ? -1 : 0;
const color = direction > 0 ? cyan : direction < 0 ? amber : neutral;
flowArrowMaterial.color.copy(color);
flowArrowMaterial.emissive.copy(color);
flowParticleMaterial.color.copy(color);
flowParticleMaterial.emissive.copy(color);
flowArrowMaterial.opacity = magnitude * 0.82;
flowParticleMaterial.opacity = magnitude * 0.9;
flowParticles.visible = direction !== 0 && magnitude > 0.015;
for (const arrow of flowArrows) {
arrow.object.visible = direction !== 0 && magnitude > 0.015;
if (arrow.kind === "central") {
arrow.object.position.set(0, arrow.y, 0);
arrowDirection.set(0, direction > 0 ? -1 : 1, 0);
} else {
const t = arrow.t;
const x = arrow.side * (0.18 + 1.58 * t) * chestScale;
const y = 4.84 - 1.7 * t;
arrow.object.position.set(x, y, 0);
arrowDirection.set(
arrow.side * 0.58 * direction,
-0.81 * direction,
0
);
}
arrow.object.quaternion.setFromUnitVectors(
downDirection,
arrowDirection.normalize()
);
}
if (flowParticles.visible) {
particleData.forEach((particle, index) => {
let u = (elapsedTime * particle.speed + particle.offset) % 1;
if (direction < 0) u = 1 - u;
flowPoint(u, particle.branch, chestScale, flowPointTemp);
dummy.position.copy(flowPointTemp);
dummy.rotation.set(0, 0, 0);
dummy.scale.setScalar(0.8 + (index % 3) * 0.12);
dummy.updateMatrix();
flowParticles.setMatrixAt(index, dummy.matrix);
});
flowParticles.instanceMatrix.needsUpdate = true;
}
}
function updateCamera() {
const horizontalRadius = Math.cos(cameraElevation) * cameraRadius;
camera.position.set(
cameraTarget.x + Math.sin(cameraYaw) * horizontalRadius,
cameraTarget.y + Math.sin(cameraElevation) * cameraRadius,
cameraTarget.z + Math.cos(cameraYaw) * horizontalRadius
);
camera.lookAt(cameraTarget);
camera.updateMatrixWorld();
}
function resetCamera() {
cameraYaw = 0.53;
cameraElevation = 0.13;
cameraRadius = window.innerWidth < 820 ? 13.7 : 11.8;
updateCamera();
}
// Direct camera controls implemented without external control modules.
let dragging = false;
let lastPointerX = 0;
let lastPointerY = 0;
const activePointers = new Map();
let previousPinchDistance = 0;
canvas.addEventListener("pointerdown", (event) => {
activePointers.set(event.pointerId, {
x: event.clientX,
y: event.clientY
});
if (activePointers.size === 1) {
dragging = true;
lastPointerX = event.clientX;
lastPointerY = event.clientY;
canvas.classList.add("dragging");
canvas.setPointerCapture(event.pointerId);
} else if (activePointers.size === 2) {
const points = [...activePointers.values()];
previousPinchDistance = Math.hypot(
points[0].x - points[1].x,
points[0].y - points[1].y
);
}
});
canvas.addEventListener("pointermove", (event) => {
if (!activePointers.has(event.pointerId)) return;
activePointers.set(event.pointerId, {
x: event.clientX,
y: event.clientY
});
if (activePointers.size === 1 && dragging) {
const dx = event.clientX - lastPointerX;
const dy = event.clientY - lastPointerY;
cameraYaw -= dx * 0.006;
cameraElevation = THREE.MathUtils.clamp(
cameraElevation + dy * 0.005,
-0.22,
0.72
);
lastPointerX = event.clientX;
lastPointerY = event.clientY;
updateCamera();
} else if (activePointers.size === 2) {
const points = [...activePointers.values()];
const distance = Math.hypot(
points[0].x - points[1].x,
points[0].y - points[1].y
);
if (previousPinchDistance > 0) {
cameraRadius = THREE.MathUtils.clamp(
cameraRadius * (previousPinchDistance / distance),
7.4,
18
);
updateCamera();
}
previousPinchDistance = distance;
}
});
function endPointer(event) {
activePointers.delete(event.pointerId);
if (activePointers.size === 0) {
dragging = false;
previousPinchDistance = 0;
canvas.classList.remove("dragging");
}
}
canvas.addEventListener("pointerup", endPointer);
canvas.addEventListener("pointercancel", endPointer);
canvas.addEventListener(
"wheel",
(event) => {
event.preventDefault();
cameraRadius = THREE.MathUtils.clamp(
cameraRadius * Math.exp(event.deltaY * 0.001),
7.4,
18
);
updateCamera();
},
{ passive: false }
);
canvas.addEventListener("dblclick", resetCamera);
canvas.addEventListener("keydown", (event) => {
let handled = true;
switch (event.key) {
case "ArrowLeft":
cameraYaw += 0.08;
break;
case "ArrowRight":
cameraYaw -= 0.08;
break;
case "ArrowUp":
cameraElevation = THREE.MathUtils.clamp(
cameraElevation + 0.06,
-0.22,
0.72
);
break;
case "ArrowDown":
cameraElevation = THREE.MathUtils.clamp(
cameraElevation - 0.06,
-0.22,
0.72
);
break;
case "+":
case "=":
cameraRadius = Math.max(7.4, cameraRadius - 0.7);
break;
case "-":
cameraRadius = Math.min(18, cameraRadius + 0.7);
break;
case "0":
resetCamera();
break;
default:
handled = false;
}
if (handled) {
event.preventDefault();
updateCamera();
}
});
const projected = new THREE.Vector3();
const labelPositions = {
airway: new THREE.Vector3(0, 6.65, 0),
lung: new THREE.Vector3(-2.2, 3.65, 0.2),
pressure: new THREE.Vector3(3.05, 4.78, 0)
};
function positionLabel(element, worldPosition) {
projected.copy(worldPosition).project(camera);
const visible =
projected.z > -1 &&
projected.z < 1 &&
Math.abs(projected.x) < 1.15 &&
Math.abs(projected.y) < 1.15;
element.style.opacity = visible ? "1" : "0";
element.style.left = `${(projected.x * 0.5 + 0.5) * window.innerWidth}px`;
element.style.top = `${(-projected.y * 0.5 + 0.5) * window.innerHeight}px`;
}
window.addEventListener("resize", () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight, false);
});
resetCamera();
const clock = new THREE.Clock();
let elapsedTime = 0;
let firstFrame = true;
renderer.setAnimationLoop(() => {
const deltaTime = Math.min(clock.getDelta(), 0.05);
elapsedTime += deltaTime;
if (demoActive) {
demoPhase += deltaTime * 1.22;
targetVolume = 0.5 + 0.34 * Math.sin(demoPhase);
volumeSlider.value = String(Math.round(targetVolume * 100));
}
const smoothing = 1 - Math.exp(-deltaTime * 7.5);
displayedVolume += (targetVolume - displayedVolume) * smoothing;
volumeSlider.style.setProperty(
"--position",
`${Math.max(0, Math.min(100, displayedVolume * 100))}%`
);
const chestScale = 0.82 + displayedVolume * 0.36;
const verticalScale = 0.96 + (displayedVolume - 0.5) * 0.08;
lungRoot.scale.set(chestScale, verticalScale, chestScale);
cageRoot.scale.set(chestScale, verticalScale, chestScale);
diaphragmRoot.position.y = 0.52 - (displayedVolume - 0.5) * 0.24;
diaphragmRoot.scale.set(chestScale, 1, chestScale);
pressureShell.scale.set(chestScale, verticalScale, chestScale);
pressureVolume.scale.copy(pressureShell.scale);
const delta = displayedVolume - 0.5;
const magnitude = Math.min(1, Math.abs(delta) * 2.8);
const direction = delta > 0.0001 ? 1 : delta < -0.0001 ? -1 : 0;
const pressureColor = direction > 0 ? cyan : direction < 0 ? amber : neutral;
pressureShellMaterial.color.copy(pressureColor);
pressureVolumeMaterial.color.copy(pressureColor);
pressureShellMaterial.opacity = 0.08 + magnitude * 0.3;
pressureVolumeMaterial.opacity = 0.018 + magnitude * 0.035;
const pulse = 1 + Math.sin(elapsedTime * 3.1) * magnitude * 0.006;
pressureShell.scale.multiplyScalar(pulse);
lungMaterials.forEach((material, index) => {
const directionalColors =
direction > 0 ? inhaleLungColors : exhaleLungColors;
const color = neutralLungColors[index]
.clone()
.lerp(directionalColors[index], magnitude * 0.82);
material.color.copy(color);
material.emissive.copy(color).multiplyScalar(0.13);
});
updateReadout(displayedVolume);
updateFlow(displayedVolume, elapsedTime, chestScale);
positionLabel(airwayLabel, labelPositions.airway);
positionLabel(lungLabel, labelPositions.lung);
positionLabel(pressureLabel, labelPositions.pressure);
renderer.render(scene, camera);
if (firstFrame) {
loading.classList.add("is-hidden");
firstFrame = false;
}
});
</script>
</body>
</html>
```