Prompt specification
Build or customize the Three.js 3D scene "Breathing museum bell-jar analogy" 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: Breathing museum bell-jar analogy
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/breathing-museum-bell-jar-analogy-x0963
Scene Description Prompt:
Build a self-contained HTML scene with Three.js from cdnjs featuring breathing pressure and airflow. Compose a bell-jar model beside a schematic chest with labels explaining matching roles and limitations; show that the analogy omits ribs, pleural details, and active expiration. Voxel style.
Working Three.js Starter (breathing-museum-bell-jar-analogy-x0963):
```html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>The Breathing Bell — Voxel Analogy</title>
<style>
:root {
color-scheme: dark;
--bg: #071018;
--panel: rgba(10, 24, 34, 0.84);
--panel-strong: rgba(10, 27, 39, 0.95);
--line: rgba(144, 210, 236, 0.22);
--text: #eef8ff;
--muted: #91aabd;
--cyan: #59dcff;
--cyan-soft: #b8f2ff;
--amber: #ffb65e;
--pink: #ff7897;
--red: #ff697b;
--green: #65f0ba;
}
* {
box-sizing: border-box;
}
html,
body {
margin: 0;
width: 100%;
min-height: 100%;
background: var(--bg);
color: var(--text);
font-family: Inter, ui-sans-serif, system-ui, -apple-system,
BlinkMacSystemFont, "Segoe UI", sans-serif;
}
body {
overflow: hidden;
}
button,
canvas {
-webkit-tap-highlight-color: transparent;
}
#app {
position: relative;
width: 100vw;
height: 100vh;
min-height: 620px;
overflow: hidden;
background:
radial-gradient(circle at 24% 28%, rgba(54, 150, 191, 0.13), transparent 32%),
radial-gradient(circle at 77% 31%, rgba(218, 67, 111, 0.11), transparent 31%),
linear-gradient(180deg, #08131c 0%, #050c12 100%);
}
#viewport {
position: absolute;
inset: 0;
overflow: hidden;
}
#scene {
display: block;
width: 100%;
height: 100%;
outline: none;
cursor: grab;
touch-action: none;
}
#scene:active {
cursor: grabbing;
}
#leaders {
position: absolute;
inset: 0;
width: 100%;
height: 100%;
pointer-events: none;
overflow: visible;
}
#leaders polyline {
fill: none;
stroke: rgba(125, 205, 234, 0.58);
stroke-width: 1.25;
stroke-dasharray: 4 5;
}
#leaders circle {
fill: var(--cyan);
stroke: rgba(255, 255, 255, 0.8);
stroke-width: 1;
filter: drop-shadow(0 0 5px rgba(89, 220, 255, 0.75));
}
#topbar {
position: absolute;
z-index: 5;
top: 22px;
left: 26px;
max-width: 610px;
pointer-events: none;
}
.eyebrow {
display: flex;
align-items: center;
gap: 9px;
margin-bottom: 7px;
color: var(--cyan);
font: 700 10px/1.2 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
letter-spacing: 0.18em;
text-transform: uppercase;
}
.eyebrow::before {
content: "";
width: 22px;
height: 2px;
background: currentColor;
box-shadow: 0 0 10px currentColor;
}
h1 {
margin: 0;
font-size: clamp(27px, 3.1vw, 46px);
line-height: 0.98;
letter-spacing: -0.055em;
font-weight: 800;
}
#topbar p {
max-width: 520px;
margin: 10px 0 0;
color: var(--muted);
font-size: 13px;
line-height: 1.45;
}
#phaseCard {
position: absolute;
z-index: 7;
top: 24px;
right: 25px;
width: 238px;
padding: 13px 15px 12px;
border: 1px solid rgba(89, 220, 255, 0.34);
border-left: 3px solid var(--cyan);
background: var(--panel-strong);
box-shadow: 0 16px 50px rgba(0, 0, 0, 0.25);
backdrop-filter: blur(12px);
transition: border-color 180ms ease, border-left-color 180ms ease;
}
#phaseCard.exhale {
border-color: rgba(255, 182, 94, 0.38);
border-left-color: var(--amber);
}
.phase-top {
display: flex;
align-items: center;
justify-content: space-between;
gap: 12px;
}
.phase-label {
color: var(--muted);
font: 700 9px/1 ui-monospace, SFMono-Regular, Menlo, monospace;
letter-spacing: 0.16em;
}
#phaseName {
color: var(--cyan);
font: 800 20px/1 ui-monospace, SFMono-Regular, Menlo, monospace;
letter-spacing: 0.07em;
}
#phaseCard.exhale #phaseName {
color: var(--amber);
}
.live-dot {
width: 7px;
height: 7px;
border-radius: 50%;
background: currentColor;
box-shadow: 0 0 12px currentColor;
animation: pulse 1.2s ease-in-out infinite;
}
@keyframes pulse {
50% {
opacity: 0.35;
transform: scale(0.72);
}
}
.phase-metrics {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 6px;
margin-top: 11px;
}
.metric {
padding: 7px 5px 6px;
border: 1px solid rgba(255, 255, 255, 0.08);
background: rgba(255, 255, 255, 0.035);
text-align: center;
}
.metric span {
display: block;
color: var(--muted);
font: 700 8px/1.2 ui-monospace, SFMono-Regular, Menlo, monospace;
letter-spacing: 0.08em;
}
.metric strong {
display: block;
margin-top: 4px;
color: var(--text);
font: 800 13px/1 ui-monospace, SFMono-Regular, Menlo, monospace;
}
#equation {
position: absolute;
z-index: 6;
left: 50%;
bottom: 22px;
transform: translateX(-50%);
display: flex;
align-items: center;
gap: 10px;
min-width: 550px;
padding: 11px 16px;
border: 1px solid var(--line);
background: var(--panel);
box-shadow: 0 16px 45px rgba(0, 0, 0, 0.28);
backdrop-filter: blur(12px);
pointer-events: none;
font: 700 11px/1 ui-monospace, SFMono-Regular, Menlo, monospace;
letter-spacing: 0.04em;
}
#equation .arrow {
color: var(--muted);
font-size: 15px;
}
#equation .term {
white-space: nowrap;
}
#eqVolume,
#eqPressure {
color: var(--cyan);
}
#eqFlow {
color: var(--cyan-soft);
}
#controlHint {
position: absolute;
z-index: 5;
left: 26px;
bottom: 27px;
color: rgba(170, 204, 220, 0.65);
pointer-events: none;
font: 600 9px/1.4 ui-monospace, SFMono-Regular, Menlo, monospace;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.model-tag {
position: absolute;
z-index: 4;
transform: translate(-50%, -100%);
min-width: 174px;
padding: 8px 11px;
border: 1px solid rgba(89, 220, 255, 0.35);
background: rgba(6, 19, 28, 0.82);
text-align: center;
pointer-events: none;
backdrop-filter: blur(8px);
}
.model-tag.chest {
border-color: rgba(255, 120, 151, 0.38);
}
.model-tag strong {
display: block;
font: 800 11px/1.2 ui-monospace, SFMono-Regular, Menlo, monospace;
letter-spacing: 0.1em;
}
.model-tag small {
display: block;
margin-top: 3px;
color: var(--muted);
font-size: 9px;
letter-spacing: 0.04em;
}
.omission-chips {
display: flex;
justify-content: center;
gap: 4px;
margin-top: 7px;
}
.omission-chips span {
padding: 3px 4px;
border: 1px solid rgba(255, 105, 123, 0.28);
color: #ff9aac;
background: rgba(255, 105, 123, 0.06);
font: 800 7px/1 ui-monospace, SFMono-Regular, Menlo, monospace;
letter-spacing: 0.04em;
}
.rails {
position: absolute;
z-index: 8;
inset: 118px 22px 86px;
display: flex;
align-items: flex-start;
justify-content: space-between;
gap: 18px;
pointer-events: none;
}
.rail {
display: grid;
gap: 12px;
width: 252px;
pointer-events: auto;
}
.rail-left {
justify-items: start;
}
.rail-right {
justify-items: end;
}
.callout {
position: relative;
width: 100%;
padding: 14px 15px 13px;
border: 1px solid var(--line);
border-top: 2px solid var(--cyan);
background:
linear-gradient(135deg, rgba(89, 220, 255, 0.055), transparent 55%),
var(--panel);
box-shadow: 0 15px 42px rgba(0, 0, 0, 0.2);
backdrop-filter: blur(12px);
}
.callout::after {
content: "";
position: absolute;
right: -1px;
bottom: -1px;
width: 13px;
height: 13px;
border-right: 1px solid rgba(89, 220, 255, 0.55);
border-bottom: 1px solid rgba(89, 220, 255, 0.55);
}
.callout.pink {
border-top-color: var(--pink);
background:
linear-gradient(135deg, rgba(255, 120, 151, 0.055), transparent 55%),
var(--panel);
}
.callout.pink::after {
border-color: rgba(255, 120, 151, 0.55);
}
.callout.limit {
border-top-color: var(--amber);
background:
linear-gradient(135deg, rgba(255, 182, 94, 0.055), transparent 55%),
var(--panel);
}
.callout.limit::after {
border-color: rgba(255, 182, 94, 0.55);
}
.callout-label {
color: var(--cyan);
font: 800 9px/1.2 ui-monospace, SFMono-Regular, Menlo, monospace;
letter-spacing: 0.13em;
text-transform: uppercase;
}
.pink .callout-label {
color: var(--pink);
}
.limit .callout-label {
color: var(--amber);
}
.callout h2 {
margin: 7px 0 7px;
font-size: 16px;
line-height: 1.15;
letter-spacing: -0.025em;
}
.callout p,
.callout li {
color: #a9c0ce;
font-size: 11px;
line-height: 1.48;
}
.callout p {
margin: 0;
}
.callout ul {
margin: 8px 0 0;
padding-left: 15px;
}
.callout li {
margin: 4px 0;
}
.callout li::marker {
color: var(--amber);
}
.role-match {
display: inline-block;
margin-top: 9px;
padding: 4px 6px;
border: 1px solid rgba(101, 240, 186, 0.25);
color: var(--green);
background: rgba(101, 240, 186, 0.05);
font: 800 8px/1 ui-monospace, SFMono-Regular, Menlo, monospace;
letter-spacing: 0.08em;
text-transform: uppercase;
}
@media (max-width: 960px) {
body {
overflow: auto;
}
#app {
height: auto;
min-height: 100vh;
overflow: visible;
}
#viewport {
position: relative;
height: 64vh;
min-height: 500px;
}
#leaders {
display: none;
}
#topbar {
top: 15px;
left: 15px;
max-width: calc(100% - 30px);
}
#topbar p {
max-width: 70%;
font-size: 11px;
}
#phaseCard {
top: auto;
right: 14px;
bottom: 72px;
width: 205px;
padding: 10px 11px;
}
#phaseName {
font-size: 16px;
}
.phase-metrics {
margin-top: 8px;
}
#equation {
left: 14px;
right: 14px;
bottom: 14px;
transform: none;
min-width: 0;
justify-content: center;
gap: 6px;
padding: 10px 8px;
font-size: 9px;
}
#controlHint {
display: none;
}
.rails {
position: relative;
inset: auto;
display: grid;
grid-template-columns: 1fr 1fr;
gap: 12px;
padding: 0 14px 24px;
}
.rail {
width: 100%;
}
.model-tag {
min-width: 142px;
padding: 6px 8px;
}
.model-tag strong {
font-size: 9px;
}
.model-tag small {
font-size: 8px;
}
.omission-chips span {
font-size: 6px;
}
}
@media (max-width: 620px) {
#viewport {
height: 61vh;
min-height: 455px;
}
#topbar p {
display: none;
}
h1 {
max-width: 250px;
font-size: 27px;
}
#phaseCard {
width: 174px;
bottom: 62px;
}
.phase-label {
display: none;
}
.metric {
padding: 5px 3px;
}
.metric span {
font-size: 7px;
}
.metric strong {
font-size: 11px;
}
#equation {
gap: 4px;
font-size: 7px;
}
#equation .arrow {
font-size: 11px;
}
.rails {
grid-template-columns: 1fr;
}
.rail {
gap: 10px;
}
.callout {
padding: 12px 13px;
}
.callout h2 {
font-size: 15px;
}
.omission-chips {
flex-wrap: wrap;
}
}
</style>
</head>
<body>
<main id="app">
<section id="viewport" aria-label="Interactive Three.js breathing analogy">
<canvas
id="scene"
tabindex="0"
aria-label="Voxel bell jar beside a schematic chest. Drag to orbit and scroll to zoom."
></canvas>
<svg id="leaders" aria-hidden="true"></svg>
<header id="topbar">
<div class="eyebrow">Voxel physiology / model 01</div>
<h1>The Breathing Bell</h1>
<p>
A bell jar explains pressure and airflow. The chest beside it shows
why the analogy is useful—and where the real mechanism diverges.
</p>
</header>
<section id="phaseCard" class="inhale" aria-live="polite">
<div class="phase-top">
<span class="phase-label">LIVE PRESSURE CYCLE</span>
<strong id="phaseName">INHALE</strong>
<i class="live-dot" aria-hidden="true"></i>
</div>
<div class="phase-metrics">
<div class="metric">
<span>VOLUME</span>
<strong id="metricVolume">↑</strong>
</div>
<div class="metric">
<span>PRESSURE</span>
<strong id="metricPressure">↓</strong>
</div>
<div class="metric">
<span>FLOW</span>
<strong id="metricFlow">IN</strong>
</div>
</div>
</section>
<div id="jarTag" class="model-tag">
<strong>01 / BELL JAR</strong>
<small>pressure-volume analogue</small>
<div class="omission-chips" aria-label="Features omitted by the bell jar analogy">
<span>NO RIBS</span>
<span>NO PLEURA</span>
<span>NO ACTIVE EXHALE</span>
</div>
</div>
<div id="chestTag" class="model-tag chest">
<strong>02 / SCHEMATIC CHEST</strong>
<small>coupled respiratory system</small>
</div>
<div id="equation" aria-label="Breathing pressure and airflow relationship">
<span class="term">SEALED VOLUME <b id="eqVolume">↑</b></span>
<span class="arrow">→</span>
<span class="term">PRESSURE <b id="eqPressure">↓</b></span>
<span class="arrow">→</span>
<span class="term"><b id="eqFlow">AIR IN</b></span>
</div>
<div id="controlHint">drag to orbit · scroll / pinch to zoom · R resets</div>
</section>
<section class="rails" aria-label="Roles and limitations of the analogy">
<aside class="rail rail-left">
<article class="callout" id="callout-volume">
<div class="callout-label">Shared role / change volume</div>
<h2>The membrane changes the chamber.</h2>
<p>
Pulling the flexible floor downward enlarges the sealed jar.
In the chest, respiratory muscles change the volume of the thorax.
</p>
<span class="role-match">volume → pressure</span>
</article>
<article class="callout" id="callout-pressure">
<div class="callout-label">Shared role / pressure difference</div>
<h2>The gradient moves the air.</h2>
<p>
Lower internal pressure draws air through the tube; higher internal
pressure pushes air out. The jar makes this causal chain visible.
</p>
</article>
</aside>
<aside class="rail rail-right">
<article class="callout pink" id="callout-coupling">
<div class="callout-label">Real chest / coupled system</div>
<h2>Lungs follow the chest wall.</h2>
<p>
The diaphragm and ribs alter thoracic volume. Pleural coupling
transmits that motion to the lungs while the airway remains open.
</p>
<span class="role-match">same pressure principle</span>
</article>
<article class="callout limit" id="callout-limits">
<div class="callout-label">Limitation / not modeled</div>
<h2>Three important omissions.</h2>
<ul>
<li>Ribs and chest-wall geometry</li>
<li>Pleural space and membrane coupling</li>
<li>Active expiration by abdominal and intercostal muscles</li>
</ul>
</article>
</aside>
</section>
</main>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const viewport = document.querySelector("#viewport");
const canvas = document.querySelector("#scene");
const phaseCard = document.querySelector("#phaseCard");
const phaseName = document.querySelector("#phaseName");
const metricVolume = document.querySelector("#metricVolume");
const metricPressure = document.querySelector("#metricPressure");
const metricFlow = document.querySelector("#metricFlow");
const eqVolume = document.querySelector("#eqVolume");
const eqPressure = document.querySelector("#eqPressure");
const eqFlow = document.querySelector("#eqFlow");
const jarTag = document.querySelector("#jarTag");
const chestTag = document.querySelector("#chestTag");
const leaderSvg = document.querySelector("#leaders");
const renderer = new THREE.WebGLRenderer({
canvas,
antialias: true,
alpha: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
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.fog = new THREE.FogExp2(0x071018, 0.023);
const camera = new THREE.OrthographicCamera(-8, 8, 5, -5, 0.1, 100);
const cameraTarget = new THREE.Vector3(0, 2.65, 0);
const orbit = {
theta: -0.08,
phi: 1.28,
zoom: 1
};
const orbitHome = { ...orbit };
const ambient = new THREE.AmbientLight(0xb9dded, 0.68);
scene.add(ambient);
const hemisphere = new THREE.HemisphereLight(0xc7ecff, 0x15202a, 1.15);
scene.add(hemisphere);
const key = new THREE.DirectionalLight(0xffffff, 2.25);
key.position.set(-4, 10, 7);
key.castShadow = true;
key.shadow.mapSize.set(2048, 2048);
key.shadow.camera.left = -11;
key.shadow.camera.right = 11;
key.shadow.camera.top = 9;
key.shadow.camera.bottom = -7;
key.shadow.camera.near = 1;
key.shadow.camera.far = 30;
scene.add(key);
const cyanLight = new THREE.PointLight(0x38c8ff, 24, 13, 2);
cyanLight.position.set(-5, 5.5, 4);
scene.add(cyanLight);
const pinkLight = new THREE.PointLight(0xff547c, 26, 13, 2);
pinkLight.position.set(5, 5.5, 3.5);
scene.add(pinkLight);
const unitBox = new THREE.BoxGeometry(1, 1, 1);
const edgeGeometry = new THREE.EdgesGeometry(unitBox);
const materials = {
platform: new THREE.MeshStandardMaterial({
color: 0x102532,
roughness: 0.72,
metalness: 0.16
}),
platformTop: new THREE.MeshStandardMaterial({
color: 0x183746,
roughness: 0.62,
metalness: 0.12
}),
dark: new THREE.MeshStandardMaterial({
color: 0x07131c,
roughness: 0.75,
metalness: 0.05
}),
cyan: new THREE.MeshStandardMaterial({
color: 0x59dcff,
roughness: 0.42,
metalness: 0.08,
emissive: 0x0b7899,
emissiveIntensity: 0.35
}),
cyanSoft: new THREE.MeshStandardMaterial({
color: 0xb8f2ff,
roughness: 0.45,
emissive: 0x267f9e,
emissiveIntensity: 0.22
}),
amber: new THREE.MeshStandardMaterial({
color: 0xffb65e,
roughness: 0.42,
metalness: 0.06,
emissive: 0x9b4c0c,
emissiveIntensity: 0.34
}),
pink: new THREE.MeshStandardMaterial({
color: 0xff7897,
roughness: 0.66,
metalness: 0.02,
emissive: 0x70162f,
emissiveIntensity: 0.18
}),
pinkDark: new THREE.MeshStandardMaterial({
color: 0xc54d70,
roughness: 0.7
}),
ivory: new THREE.MeshStandardMaterial({
color: 0xd7e4e5,
roughness: 0.58,
metalness: 0.06
}),
red: new THREE.MeshStandardMaterial({
color: 0xff697b,
roughness: 0.6,
emissive: 0x7d1523,
emissiveIntensity: 0.22
}),
glass: new THREE.MeshStandardMaterial({
color: 0x73d9ff,
roughness: 0.15,
metalness: 0.04,
transparent: true,
opacity: 0.105,
depthWrite: false,
side: THREE.DoubleSide
}),
glassEdge: new THREE.MeshStandardMaterial({
color: 0x80dfff,
roughness: 0.28,
transparent: true,
opacity: 0.32,
depthWrite: false
}),
pipe: new THREE.MeshStandardMaterial({
color: 0x17495e,
roughness: 0.44,
metalness: 0.22,
transparent: true,
opacity: 0.72
}),
pleural: new THREE.MeshStandardMaterial({
color: 0x53e8ff,
roughness: 0.35,
transparent: true,
opacity: 0.78,
emissive: 0x0a6a82,
emissiveIntensity: 0.4
}),
torso: new THREE.MeshStandardMaterial({
color: 0x6aa2bd,
roughness: 0.9,
transparent: true,
opacity: 0.055,
depthWrite: false,
side: THREE.DoubleSide
})
};
const edgeMaterial = new THREE.LineBasicMaterial({
color: 0x6db5cf,
transparent: true,
opacity: 0.26
});
function cube(parent, w, h, d, x, y, z, material, edges = false) {
const mesh = new THREE.Mesh(unitBox, material);
mesh.scale.set(w, h, d);
mesh.position.set(x, y, z);
mesh.castShadow = !material.transparent;
mesh.receiveShadow = !material.transparent;
parent.add(mesh);
if (edges) {
const outline = new THREE.LineSegments(edgeGeometry, edgeMaterial);
mesh.add(outline);
}
return mesh;
}
function makePedestal(parent, accentMaterial) {
cube(parent, 4.5, 0.22, 3.55, 0, 0.11, 0, materials.platform, true);
cube(parent, 4.28, 0.08, 3.32, 0, 0.26, 0, materials.platformTop, true);
cube(parent, 0.08, 0.05, 3.05, -2.02, 0.33, 0, accentMaterial);
cube(parent, 0.08, 0.05, 3.05, 2.02, 0.33, 0, accentMaterial);
for (const x of [-1.88, 1.88]) {
for (const z of [-1.42, 1.42]) {
cube(parent, 0.14, 0.06, 0.14, x, 0.34, z, accentMaterial);
}
}
}
function makeVoxelArrow(parent, material) {
const arrow = new THREE.Group();
cube(arrow, 0.15, 0.72, 0.15, 0, -0.05, 0, material);
// Stepped voxel arrowhead, pointing toward local +Y.
cube(arrow, 0.46, 0.14, 0.16, 0, 0.36, 0, material);
cube(arrow, 0.32, 0.14, 0.16, 0, 0.51, 0, material);
cube(arrow, 0.17, 0.14, 0.16, 0, 0.66, 0, material);
parent.add(arrow);
return arrow;
}
function makePressureGauge(parent, x, y, z) {
const gauge = new THREE.Group();
gauge.position.set(x, y, z);
cube(gauge, 0.19, 1.65, 0.13, 0, 0, 0, materials.dark);
cube(gauge, 0.33, 0.035, 0.17, 0, 0, 0.095, materials.cyanSoft);
for (let i = -3; i <= 3; i++) {
cube(
gauge,
i === 0 ? 0.34 : 0.22,
0.035,
0.15,
0.11,
i * 0.21,
0.075,
materials.cyanSoft
);
}
const markerMaterial = materials.cyan.clone();
const marker = cube(
gauge,
0.46,
0.15,
0.24,
0,
0,
0.12,
markerMaterial
);
parent.add(gauge);
return { gauge, marker, markerMaterial };
}
// Ground and grid.
const ground = new THREE.Mesh(
new THREE.PlaneGeometry(50, 50),
new THREE.MeshStandardMaterial({
color: 0x061018,
roughness: 0.94,
metalness: 0.03
})
);
ground.rotation.x = -Math.PI / 2;
ground.position.y = -0.01;
ground.receiveShadow = true;
scene.add(ground);
const grid = new THREE.GridHelper(40, 40, 0x234858, 0x142b38);
grid.position.y = 0.005;
grid.material.transparent = true;
grid.material.opacity = 0.28;
scene.add(grid);
// ---------------------------------------------------------------------
// LEFT: bell-jar pressure model
// ---------------------------------------------------------------------
const jarRoot = new THREE.Group();
jarRoot.position.x = -3.55;
scene.add(jarRoot);
makePedestal(jarRoot, materials.cyan);
// Gasket around the flexible floor.
cube(jarRoot, 3.35, 0.13, 0.18, 0, 0.39, -1.52, materials.dark);
cube(jarRoot, 3.35, 0.13, 0.18, 0, 0.39, 1.52, materials.dark);
cube(jarRoot, 0.18, 0.13, 3.08, -1.58, 0.39, 0, materials.dark);
cube(jarRoot, 0.18, 0.13, 3.08, 1.58, 0.39, 0, materials.dark);
const diaphragm = new THREE.InstancedMesh(
unitBox,
materials.cyanSoft,
81
);
diaphragm.instanceMatrix.setUsage(THREE.DynamicDrawUsage);
diaphragm.castShadow = true;
diaphragm.receiveShadow = true;
jarRoot.add(diaphragm);
const diaphragmDummy = new THREE.Object3D();
// A stepped, block-built glass bell.
const jarWidths = [
3.15, 3.15, 3.15,
2.95, 2.95,
2.75, 2.75,
2.45, 2.45,
2.15,
1.85,
1.55
];
jarWidths.forEach((width, row) => {
const y = 0.78 + row * 0.29;
const panelHeight = 0.305;
cube(
jarRoot,
width,
panelHeight,
0.065,
0,
y,
width / 2,
materials.glass
);
cube(
jarRoot,
width,
panelHeight,
0.065,
0,
y,
-width / 2,
materials.glass
);
cube(
jarRoot,
0.065,
panelHeight,
width - 0.08,
width / 2,
y,
0,
materials.glass
);
cube(
jarRoot,
0.065,
panelHeight,
width - 0.08,
-width / 2,
y,
0,
materials.glass
);
for (const x of [-width / 2, width / 2]) {
for (const z of [-width / 2, width / 2]) {
cube(
jarRoot,
0.13,
0.31,
0.13,
x,
y,
z,
materials.glassEdge
);
}
}
});
// Voxel neck and cap.
cube(jarRoot, 1.18, 0.08, 1.18, 0, 4.28, 0, materials.glassEdge);
cube(jarRoot, 0.9, 0.48, 0.07, 0, 4.52, 0.45, materials.glass);
cube(jarRoot, 0.9, 0.48, 0.07, 0, 4.52, -0.45, materials.glass);
cube(jarRoot, 0.07, 0.48, 0.9, 0.45, 4.52, 0, materials.glass);
cube(jarRoot, 0.07, 0.48, 0.9, -0.45, 4.52, 0, materials.glass);
// Hollow voxel airway through the top.
for (let y = 4.82; y <= 5.76; y += 0.29) {
cube(jarRoot, 0.48, 0.08, 0.10, 0, y, 0.23, materials.pipe);
cube(jarRoot, 0.48, 0.08, 0.10, 0, y, -0.23, materials.pipe);
cube(jarRoot, 0.10, 0.08, 0.38, 0.23, y, 0, materials.pipe);
cube(jarRoot, 0.10, 0.08, 0.38, -0.23, y, 0, materials.pipe);
}
// Sparse air-field voxels reveal the chamber's changing volume.
let randomSeed = 714;
function random() {
randomSeed = (randomSeed * 16807) % 2147483647;
return (randomSeed - 1) / 2147483646;
}
const airFieldBase = [];
for (let i = 0; i < 38; i++) {
airFieldBase.push({
x: (random() - 0.5) * 2.65,
y: 1.1 + random() * 2.65,
z: (random() - 0.5) * 2.55
});
}
const airFieldMaterial = new THREE.MeshStandardMaterial({
color: 0x76e6ff,
roughness: 0.4,
transparent: true,
opacity: 0.48,
emissive: 0x166d83,
emissiveIntensity: 0.45
});
const airField = new THREE.InstancedMesh(
unitBox,
airFieldMaterial,
airFieldBase.length
);
airField.instanceMatrix.setUsage(THREE.DynamicDrawUsage);
jarRoot.add(airField);
const fieldDummy = new THREE.Object3D();
const jarGauge = makePressureGauge(jarRoot, 1.82, 2.25, 1.22);
const jarArrowMaterial = materials.cyan.clone();
jarArrowMaterial.transparent = true;
const jarArrow = makeVoxelArrow(jarRoot, jarArrowMaterial);
jarArrow.position.set(-1.66, 4.92, 0.9);
function createVerticalFlow(parent, count, outsideY, insideY, z = 0) {
const inhaleGroup = new THREE.Group();
const exhaleGroup = new THREE.Group();
const inhaleMaterial = new THREE.MeshStandardMaterial({
color: 0x64e3ff,
emissive: 0x0b7899,
emissiveIntensity: 0.9,
roughness: 0.35,
transparent: true,
opacity: 0.9
});
const exhaleMaterial = new THREE.MeshStandardMaterial({
color: 0xffbd68,
emissive: 0x9b4c0c,
emissiveIntensity: 0.9,
roughness: 0.35,
transparent: true,
opacity: 0.9
});
const inhaleParticles = [];
const exhaleParticles = [];
for (let i = 0; i < count; i++) {
const p1 = cube(inhaleGroup, 0.13, 0.13, 0.13, 0, outsideY, z, inhaleMaterial);
const p2 = cube(exhaleGroup, 0.13, 0.13, 0.13, 0, outsideY, z, exhaleMaterial);
inhaleParticles.push(p1);
exhaleParticles.push(p2);
}
parent.add(inhaleGroup, exhaleGroup);
return {
inhaleGroup,
exhaleGroup,
inhaleParticles,
exhaleParticles,
inhaleMaterial,
exhaleMaterial,
outsideY,
insideY,
z
};
}
const jarFlow = createVerticalFlow(jarRoot, 13, 5.88, 4.05, 0);
// ---------------------------------------------------------------------
// RIGHT: schematic chest
// ---------------------------------------------------------------------
const chestRoot = new THREE.Group();
chestRoot.position.x = 3.55;
scene.add(chestRoot);
makePedestal(chestRoot, materials.pink);
// A faint torso envelope.
cube(chestRoot, 2.95, 3.85, 1.45, 0, 2.42, 0, materials.torso);
// Spine.
for (let i = 0; i < 9; i++) {
cube(
chestRoot,
0.3,
0.31,
0.3,
0,
0.72 + i * 0.43,
-0.69,
materials.ivory,
true
);
}
// Sternum.
for (let i = 0; i < 6; i++) {
cube(
chestRoot,
0.25,
0.34,
0.18,
0,
1.62 + i * 0.43,
0.72,
materials.ivory,
true
);
}
// Voxel lungs.
const lungGroups = [];
for (const side of [-1, 1]) {
const lung = new THREE.Group();
chestRoot.add(lung);
lungGroups.push(lung);
for (let row = 0; row < 6; row++) {
for (let zIndex = 0; zIndex < 4; zIndex++) {
for (let xIndex = 0; xIndex < 2; xIndex++) {
const taper = row / 5;
const x = side * (0.47 + xIndex * 0.31 + taper * 0.06);
const y = 1.2 + row * 0.41;
const z = (zIndex - 1.5) * 0.32;
cube(
lung,
0.29,
0.37,
0.29,
x,
y,
z,
(row + zIndex + xIndex) % 4 === 0
? materials.pinkDark
: materials.pink
);
}
}
}
}
// Rib cage: block segments arranged as half-ellipses.
const ribGroup = new THREE.Group();
const pleuraGroup = new THREE.Group();
chestRoot.add(ribGroup, pleuraGroup);
function buildArc(group, scale, size, material) {
for (let rib = 0; rib < 6; rib++) {
const y = 1.47 + rib * 0.49;
for (const side of [-1, 1]) {
for (let i = 0; i <= 9; i++) {
const t = (i / 9) * Math.PI;
const x = side * 1.58 * Math.sin(t) * scale;
const z = -0.66 * Math.cos(t) * scale;
cube(
group,
size,
size * 0.82,
size,
x,
y,
z,
material
);
}
}
}
}
buildArc(ribGroup, 1, 0.17, materials.ivory);
buildArc(pleuraGroup, 0.91, 0.105, materials.pleural);
// Trachea and bronchial branches.
for (let i = 0; i < 6; i++) {
cube(
chestRoot,
0.25,
0.27,
0.25,
0,
3.62 + i * 0.27,
0.05,
materials.cyan
);
}
for (const side of [-1, 1]) {
for (let i = 0; i < 5; i++) {
cube(
chestRoot,
0.23,
0.23,
0.23,
side * (0.1 + i * 0.13),
3.55 - i * 0.19,
0.04,
materials.cyan
);
}
}
// Voxel diaphragm under the lungs.
const chestDiaphragm = new THREE.Group();
chestRoot.add(chestDiaphragm);
for (let i = -6; i <= 6; i++) {
const normalized = Math.abs(i) / 6;
const y = 0.92 + normalized * normalized * 0.38;
cube(
chestDiaphragm,
0.27,
0.18,
1.23,
i * 0.24,
y,
0.02,
materials.cyan
);
cube(
chestDiaphragm,
0.24,
0.15,
0.85,
i * 0.24,
y + 0.1,
-0.08,
materials.cyanSoft
);
}
// Abdominal wall and active-expiration arrows.
const abdominalGroup = new THREE.Group();
chestRoot.add(abdominalGroup);
for (let i = -4; i <= 4; i++) {
cube(
abdominalGroup,
0.23,
0.22,
0.22,
i * 0.23,
0.68,
0.89,
materials.red
);
}
const leftExpirationArrowMaterial = materials.red.clone();
leftExpirationArrowMaterial.transparent = true;
const rightExpirationArrowMaterial = materials.red.clone();
rightExpirationArrowMaterial.transparent = true;
const leftExpirationArrow = makeVoxelArrow(
chestRoot,
leftExpirationArrowMaterial
);
leftExpirationArrow.position.set(-1.43, 0.92, 1.18);
leftExpirationArrow.rotation.z = -Math.PI / 2;
const rightExpirationArrow = makeVoxelArrow(
chestRoot,
rightExpirationArrowMaterial
);
rightExpirationArrow.position.set(1.43, 0.92, 1.18);
rightExpirationArrow.rotation.z = Math.PI / 2;
const chestGauge = makePressureGauge(chestRoot, 1.83, 2.45, 1.12);
const chestArrowMaterial = materials.cyan.clone();
chestArrowMaterial.transparent = true;
const chestArrow = makeVoxelArrow(chestRoot, chestArrowMaterial);
chestArrow.position.set(1.27, 4.83, 0.68);
const chestFlow = createVerticalFlow(chestRoot, 14, 5.08, 3.23, 0.08);
// ---------------------------------------------------------------------
// Label leader lines
// ---------------------------------------------------------------------
const calloutSpecs = [
{
element: document.querySelector("#callout-volume"),
object: diaphragm,
offset: new THREE.Vector3(-0.55, 0.5, 0.35),
side: "left"
},
{
element: document.querySelector("#callout-pressure"),
object: jarGauge.marker,
offset: new THREE.Vector3(0, 0, 0),
side: "left"
},
{
element: document.querySelector("#callout-coupling"),
object: pleuraGroup,
offset: new THREE.Vector3(1.25, 2.7, 0.45),
side: "right"
},
{
element: document.querySelector("#callout-limits"),
object: abdominalGroup,
offset: new THREE.Vector3(0.2, 0.8, 1),
side: "right"
}
];
for (const spec of calloutSpecs) {
const polyline = document.createElementNS(
"http://www.w3.org/2000/svg",
"polyline"
);
const circle = document.createElementNS(
"http://www.w3.org/2000/svg",
"circle"
);
circle.setAttribute("r", "3");
leaderSvg.append(polyline, circle);
spec.polyline = polyline;
spec.circle = circle;
}
const projected = new THREE.Vector3();
function projectLocal(object, localOffset) {
object.updateWorldMatrix(true, false);
projected.copy(localOffset);
object.localToWorld(projected);
projected.project(camera);
return {
x: (projected.x * 0.5 + 0.5) * viewport.clientWidth,
y: (-projected.y * 0.5 + 0.5) * viewport.clientHeight
};
}
function positionTag(element, object, localOffset) {
const p = projectLocal(object, localOffset);
element.style.left = `${p.x}px`;
element.style.top = `${p.y}px`;
}
function updateLeaders() {
if (window.innerWidth <= 960) return;
const viewportRect = viewport.getBoundingClientRect();
for (const spec of calloutSpecs) {
const rect = spec.element.getBoundingClientRect();
const p = projectLocal(spec.object, spec.offset);
const fromX =
spec.side === "left"
? rect.right - viewportRect.left
: rect.left - viewportRect.left;
const fromY = rect.top + rect.height * 0.5 - viewportRect.top;
const elbowX = fromX + (spec.side === "left" ? 24 : -24);
spec.polyline.setAttribute(
"points",
`${fromX},${fromY} ${elbowX},${fromY} ${p.x},${p.y}`
);
spec.circle.setAttribute("cx", p.x);
spec.circle.setAttribute("cy", p.y);
}
}
// ---------------------------------------------------------------------
// Direct camera controls: no addons or examples modules
// ---------------------------------------------------------------------
const pointers = new Map();
let previousPinchDistance = 0;
function clampOrbit() {
orbit.phi = THREE.MathUtils.clamp(orbit.phi, 0.83, 1.48);
orbit.theta = THREE.MathUtils.clamp(orbit.theta, -0.85, 0.85);
orbit.zoom = THREE.MathUtils.clamp(orbit.zoom, 0.72, 1.62);
}
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
);
}
canvas.addEventListener("pointerdown", (event) => {
canvas.setPointerCapture(event.pointerId);
pointers.set(event.pointerId, { x: event.clientX, y: event.clientY });
if (pointers.size === 2) {
previousPinchDistance = pointerDistance();
}
});
canvas.addEventListener("pointermove", (event) => {
if (!pointers.has(event.pointerId)) return;
const previous = pointers.get(event.pointerId);
const dx = event.clientX - previous.x;
const dy = event.clientY - previous.y;
pointers.set(event.pointerId, { x: event.clientX, y: event.clientY });
if (pointers.size === 1) {
orbit.theta -= dx * 0.005;
orbit.phi -= dy * 0.004;
clampOrbit();
} else if (pointers.size === 2) {
const distance = pointerDistance();
if (previousPinchDistance > 0 && distance > 0) {
orbit.zoom *= distance / previousPinchDistance;
clampOrbit();
}
previousPinchDistance = distance;
}
});
function releasePointer(event) {
pointers.delete(event.pointerId);
previousPinchDistance = pointers.size === 2 ? pointerDistance() : 0;
}
canvas.addEventListener("pointerup", releasePointer);
canvas.addEventListener("pointercancel", releasePointer);
canvas.addEventListener("lostpointercapture", releasePointer);
canvas.addEventListener(
"wheel",
(event) => {
event.preventDefault();
orbit.zoom *= Math.exp(-event.deltaY * 0.001);
clampOrbit();
},
{ passive: false }
);
window.addEventListener("keydown", (event) => {
if (event.key === "ArrowLeft") orbit.theta -= 0.08;
if (event.key === "ArrowRight") orbit.theta += 0.08;
if (event.key === "ArrowUp") orbit.phi -= 0.05;
if (event.key === "ArrowDown") orbit.phi += 0.05;
if (event.key === "+" || event.key === "=") orbit.zoom += 0.08;
if (event.key === "-" || event.key === "_") orbit.zoom -= 0.08;
if (event.key.toLowerCase() === "r") {
orbit.theta = orbitHome.theta;
orbit.phi = orbitHome.phi;
orbit.zoom = orbitHome.zoom;
}
clampOrbit();
});
function resize() {
const width = Math.max(1, viewport.clientWidth);
const height = Math.max(1, viewport.clientHeight);
const aspect = width / height;
renderer.setSize(width, height, false);
// Preserve enough horizontal room for both models on narrow canvases.
const viewHeight = Math.max(8.95, 15.9 / aspect);
camera.left = (-viewHeight * aspect) / 2;
camera.right = (viewHeight * aspect) / 2;
camera.top = viewHeight / 2;
camera.bottom = -viewHeight / 2;
camera.updateProjectionMatrix();
}
window.addEventListener("resize", resize);
resize();
// ---------------------------------------------------------------------
// Animation
// ---------------------------------------------------------------------
const clock = new THREE.Clock();
const flowColorIn = new THREE.Color(0x59dcff);
const flowColorOut = new THREE.Color(0xffb65e);
const temporaryColor = new THREE.Color();
function updateFlow(flow, volume, inhaling, strength) {
const count = flow.inhaleParticles.length;
flow.inhaleGroup.visible = inhaling && strength > 0.015;
flow.exhaleGroup.visible = !inhaling && strength > 0.015;
flow.inhaleMaterial.opacity = 0.2 + strength * 0.8;
flow.exhaleMaterial.opacity = 0.2 + strength * 0.8;
for (let i = 0; i < count; i++) {
const offset = i / count;
const inhaleU = (volume + offset) % 1;
flow.inhaleParticles[i].position.y = THREE.MathUtils.lerp(
flow.outsideY,
flow.insideY,
inhaleU
);
const exhaleU = (1 - volume + offset) % 1;
flow.exhaleParticles[i].position.y = THREE.MathUtils.lerp(
flow.insideY,
flow.outsideY,
exhaleU
);
}
}
function updateArrow(arrow, material, inhaling, strength) {
arrow.visible = strength > 0.018;
arrow.rotation.z = inhaling ? Math.PI : 0;
material.opacity = 0.18 + strength * 0.82;
material.color.copy(inhaling ? flowColorIn : flowColorOut);
material.emissive.copy(inhaling ? flowColorIn : flowColorOut);
}
function updateGauge(gauge, pressure, inhaling) {
gauge.marker.position.y = pressure * 0.62;
temporaryColor.copy(inhaling ? flowColorIn : flowColorOut);
gauge.markerMaterial.color.copy(temporaryColor);
gauge.markerMaterial.emissive.copy(temporaryColor);
gauge.markerMaterial.emissiveIntensity = 0.38;
}
function animate() {
const elapsed = clock.getElapsedTime();
const cycle = (elapsed / 6.8) * Math.PI * 2;
// 0 = minimum volume, 1 = maximum volume.
const volume = 0.5 - 0.5 * Math.cos(cycle);
const derivative = Math.sin(cycle);
const pressure = -derivative;
const strength = Math.abs(derivative);
const inhaling = derivative >= 0;
// Bell-jar membrane: down on inhalation, up on expiration.
for (let zIndex = 0; zIndex < 9; zIndex++) {
for (let xIndex = 0; xIndex < 9; xIndex++) {
const x = (xIndex - 4) * 0.33;
const z = (zIndex - 4) * 0.33;
const edge = Math.max(Math.abs(xIndex - 4), Math.abs(zIndex - 4)) / 4;
const y = 0.62 - (volume - 0.5) * 0.46 * (1 - edge * 0.28);
diaphragmDummy.position.set(x, y, z);
diaphragmDummy.scale.set(0.305, 0.115, 0.305);
diaphragmDummy.updateMatrix();
diaphragm.setMatrixAt(zIndex * 9 + xIndex, diaphragmDummy.matrix);
}
}
diaphragm.instanceMatrix.needsUpdate = true;
// Air particles spread out as chamber volume increases.
for (let i = 0; i < airFieldBase.length; i++) {
const base = airFieldBase[i];
const spread = 0.74 + volume * 0.26;
fieldDummy.position.set(
base.x * spread,
base.y,
base.z * spread
);
fieldDummy.scale.setScalar(0.085 + strength * 0.025);
fieldDummy.updateMatrix();
airField.setMatrixAt(i, fieldDummy.matrix);
}
airField.instanceMatrix.needsUpdate = true;
// Real chest: thoracic expansion, lung expansion, and pleural motion.
ribGroup.scale.set(
1 + volume * 0.065,
1,
1 + volume * 0.05
);
pleuraGroup.scale.copy(ribGroup.scale);
for (const lung of lungGroups) {
lung.scale.set(
1 + volume * 0.055,
1 + volume * 0.045,
1 + volume * 0.04
);
}
chestDiaphragm.position.y = -(volume - 0.5) * 0.42;
// Active expiration is shown only during the expiratory half.
const activeExpiration = derivative < 0 ? strength : 0;
abdominalGroup.scale.x = 1 - activeExpiration * 0.11;
leftExpirationArrow.visible = activeExpiration > 0.04;
rightExpirationArrow.visible = activeExpiration > 0.04;
leftExpirationArrowMaterial.opacity = 0.2 + activeExpiration * 0.8;
rightExpirationArrowMaterial.opacity = 0.2 + activeExpiration * 0.8;
updateFlow(jarFlow, volume, inhaling, strength);
updateFlow(chestFlow, volume, inhaling, strength);
updateArrow(jarArrow, jarArrowMaterial, inhaling, strength);
updateArrow(chestArrow, chestArrowMaterial, inhaling, strength);
updateGauge(jarGauge, pressure, inhaling);
updateGauge(chestGauge, pressure, inhaling);
// Interface state.
phaseCard.classList.toggle("exhale", !inhaling);
phaseName.textContent = inhaling ? "INHALE" : "EXHALE";
metricVolume.textContent = inhaling ? "↑" : "↓";
metricPressure.textContent = inhaling ? "↓" : "↑";
metricFlow.textContent = inhaling ? "IN" : "OUT";
eqVolume.textContent = inhaling ? "↑" : "↓";
eqPressure.textContent = inhaling ? "↓" : "↑";
eqFlow.textContent = inhaling ? "AIR IN" : "AIR OUT";
eqFlow.style.color = inhaling ? "var(--cyan-soft)" : "var(--amber)";
// Camera update.
const radius = 18;
camera.position.set(
cameraTarget.x + radius * Math.sin(orbit.phi) * Math.sin(orbit.theta),
cameraTarget.y + radius * Math.cos(orbit.phi),
cameraTarget.z + radius * Math.sin(orbit.phi) * Math.cos(orbit.theta)
);
camera.lookAt(cameraTarget);
camera.zoom = orbit.zoom;
camera.updateProjectionMatrix();
camera.updateMatrixWorld();
positionTag(jarTag, jarRoot, new THREE.Vector3(0, 5.22, 0));
positionTag(chestTag, chestRoot, new THREE.Vector3(0, 5.18, 0));
updateLeaders();
renderer.render(scene, camera);
}
renderer.setAnimationLoop(animate);
</script>
</body>
</html>
```