Prompt specification
Build or customize the Three.js 3D scene "Thoracic cavity and airway section" 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: Thoracic cavity and airway section
Category: Biology & Nature · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/thoracic-cavity-and-airway-section-x0961
Scene Description Prompt:
Build a self-contained HTML scene with Three.js from cdnjs featuring the thoracic cavity and airways. Model a transparent chest boundary, two simplified lungs, and a branching airway; distinguish pleural region from airway space and label proportions schematic. A 3D cutaway view.
Working Three.js Starter (thoracic-cavity-and-airway-section-x0961):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Thoracic Cavity — 3D Airway Cutaway</title>
<style>
:root {
--cyan: #55e6ff;
--cyan-soft: rgba(85, 230, 255, 0.18);
--violet: #bca5ff;
--violet-soft: rgba(188, 165, 255, 0.18);
--rose: #e88b98;
--amber: #f2bd6d;
--ink: #eaf7ff;
--muted: #99afbd;
--panel: rgba(7, 18, 28, 0.72);
--line: rgba(154, 212, 235, 0.18);
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background:
radial-gradient(circle at 52% 42%, #163243 0%, #0a1b27 38%, #050d15 78%),
#050d15;
color: var(--ink);
font-family:
Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
body::before {
content: "";
position: fixed;
inset: 0;
pointer-events: none;
background-image:
linear-gradient(rgba(106, 188, 220, 0.025) 1px, transparent 1px),
linear-gradient(90deg, rgba(106, 188, 220, 0.025) 1px, transparent 1px);
background-size: 42px 42px;
mask-image: radial-gradient(circle at center, black, transparent 80%);
}
#scene {
position: fixed;
inset: 0;
width: 100%;
height: 100%;
display: block;
outline: none;
touch-action: none;
cursor: grab;
}
#scene.dragging {
cursor: grabbing;
}
.ui {
position: fixed;
inset: 0;
z-index: 5;
pointer-events: none;
}
header {
position: absolute;
top: 26px;
left: 30px;
max-width: 440px;
}
.eyebrow {
display: flex;
align-items: center;
gap: 9px;
margin-bottom: 10px;
color: var(--cyan);
font-size: 10px;
font-weight: 750;
letter-spacing: 0.19em;
text-transform: uppercase;
}
.eyebrow::before {
content: "";
width: 27px;
height: 1px;
background: var(--cyan);
box-shadow: 0 0 9px var(--cyan);
}
h1 {
margin: 0;
font-size: clamp(24px, 3vw, 42px);
line-height: 1.03;
font-weight: 560;
letter-spacing: -0.035em;
text-shadow: 0 3px 30px rgba(0, 0, 0, 0.45);
}
header p {
margin: 11px 0 0;
max-width: 390px;
color: var(--muted);
font-size: 13px;
line-height: 1.55;
}
.schematic-badge {
display: inline-flex;
align-items: center;
gap: 8px;
margin-top: 14px;
padding: 7px 10px;
border: 1px solid rgba(242, 189, 109, 0.3);
border-radius: 999px;
color: #f5d19b;
background: rgba(91, 59, 18, 0.18);
font-size: 10px;
font-weight: 700;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.schematic-badge::before {
content: "";
width: 6px;
height: 6px;
border-radius: 50%;
background: var(--amber);
box-shadow: 0 0 10px rgba(242, 189, 109, 0.65);
}
.legend {
position: absolute;
top: 26px;
right: 28px;
width: 220px;
padding: 15px 16px;
border: 1px solid var(--line);
border-radius: 14px;
background: var(--panel);
box-shadow: 0 16px 45px rgba(0, 0, 0, 0.22);
backdrop-filter: blur(14px);
}
.legend-title {
margin-bottom: 11px;
color: #c8dce7;
font-size: 9px;
font-weight: 800;
letter-spacing: 0.16em;
text-transform: uppercase;
}
.legend-row {
display: flex;
align-items: center;
gap: 10px;
margin-top: 8px;
color: #c5d5df;
font-size: 11px;
}
.swatch {
width: 18px;
height: 5px;
flex: 0 0 auto;
border-radius: 999px;
background: var(--swatch);
box-shadow: 0 0 10px color-mix(in srgb, var(--swatch), transparent 45%);
}
.legend-note {
margin-top: 12px;
padding-top: 10px;
border-top: 1px solid var(--line);
color: #78909f;
font-size: 9px;
line-height: 1.45;
}
#leaders,
#labels {
position: fixed;
inset: 0;
z-index: 3;
width: 100%;
height: 100%;
overflow: visible;
pointer-events: none;
}
#labels {
z-index: 4;
}
.callout {
position: absolute;
width: max-content;
max-width: 205px;
padding: 9px 11px;
border: 1px solid rgba(151, 210, 232, 0.2);
border-radius: 9px;
background: rgba(6, 17, 26, 0.78);
box-shadow: 0 10px 28px rgba(0, 0, 0, 0.2);
backdrop-filter: blur(10px);
transition: opacity 160ms ease;
}
.callout strong {
display: block;
margin-bottom: 3px;
color: #edfaff;
font-size: 11px;
font-weight: 700;
letter-spacing: 0.015em;
}
.callout small {
display: block;
color: #8fa6b4;
font-size: 9px;
line-height: 1.35;
}
.callout::before {
content: "";
display: inline-block;
width: 5px;
height: 5px;
margin: 0 7px 1px 0;
border-radius: 50%;
background: var(--accent, var(--cyan));
box-shadow: 0 0 8px var(--accent, var(--cyan));
}
#trachea {
--accent: var(--cyan);
left: 3.2%;
top: 18%;
}
#bronchi {
--accent: var(--cyan);
right: 3.2%;
top: 29%;
}
#pleural {
--accent: var(--violet);
left: 3.2%;
top: 43%;
}
#lung-label {
--accent: var(--rose);
right: 3.2%;
top: 51%;
}
#lumen {
--accent: var(--cyan);
left: 3.2%;
top: 65%;
}
#diaphragm-label {
--accent: var(--amber);
right: 3.2%;
top: 78%;
}
body.labels-hidden #labels,
body.labels-hidden #leaders {
opacity: 0;
visibility: hidden;
}
.controls {
position: absolute;
left: 28px;
bottom: 24px;
display: flex;
align-items: center;
gap: 10px;
pointer-events: auto;
}
.hint {
padding: 9px 12px;
border: 1px solid var(--line);
border-radius: 9px;
color: #8da5b4;
background: rgba(7, 18, 28, 0.62);
font-size: 10px;
backdrop-filter: blur(10px);
}
button {
border: 1px solid rgba(85, 230, 255, 0.24);
border-radius: 9px;
padding: 9px 12px;
color: #dffaff;
background: rgba(8, 29, 40, 0.82);
font: inherit;
font-size: 10px;
font-weight: 650;
cursor: pointer;
transition:
border-color 150ms ease,
background 150ms ease,
transform 150ms ease;
}
button:hover {
border-color: rgba(85, 230, 255, 0.55);
background: rgba(14, 48, 63, 0.9);
transform: translateY(-1px);
}
button:focus-visible {
outline: 2px solid var(--cyan);
outline-offset: 2px;
}
.view-index {
position: absolute;
right: 28px;
bottom: 25px;
color: rgba(183, 211, 224, 0.5);
font-size: 9px;
letter-spacing: 0.12em;
text-transform: uppercase;
}
@media (max-width: 760px) {
header {
top: 17px;
left: 17px;
}
header p {
display: none;
}
.legend {
top: 17px;
right: 15px;
width: 160px;
padding: 11px;
}
.legend-row {
gap: 7px;
font-size: 9px;
}
.legend-note {
display: none;
}
.callout {
max-width: 130px;
padding: 6px 7px;
}
.callout strong {
margin: 0;
font-size: 9px;
}
.callout small {
display: none;
}
#trachea { left: 2%; top: 19%; }
#bronchi { right: 2%; top: 31%; }
#pleural { left: 2%; top: 46%; }
#lung-label { right: 2%; top: 55%; }
#lumen { left: 2%; top: 68%; }
#diaphragm-label { right: 2%; top: 80%; }
.controls {
left: 15px;
bottom: 15px;
}
.hint {
display: none;
}
.view-index {
display: none;
}
}
</style>
</head>
<body>
<canvas
id="scene"
tabindex="0"
aria-label="Interactive three-dimensional cutaway of the thoracic cavity and branching airways"
></canvas>
<svg id="leaders" aria-hidden="true"></svg>
<div id="labels" aria-hidden="true">
<div class="callout" id="trachea">
<strong>Trachea</strong>
<small>Superior conducting airway</small>
</div>
<div class="callout" id="bronchi">
<strong>Main bronchi</strong>
<small>Primary airway bifurcation</small>
</div>
<div class="callout" id="pleural">
<strong>Pleural region</strong>
<small>Thin serous interface, exaggerated</small>
</div>
<div class="callout" id="lung-label">
<strong>Lung parenchyma</strong>
<small>Posterior half retained in cutaway</small>
</div>
<div class="callout" id="lumen">
<strong>Airway lumen</strong>
<small>Cyan internal air space</small>
</div>
<div class="callout" id="diaphragm-label">
<strong>Diaphragm</strong>
<small>Inferior thoracic boundary</small>
</div>
</div>
<main class="ui">
<header>
<div class="eyebrow">Respiratory anatomy · interactive model</div>
<h1>Thoracic cavity<br />airway cutaway</h1>
<p>
A schematic three-dimensional view of the lungs, pleural interface,
diaphragm, and branching airway tree.
</p>
<div class="schematic-badge">Proportions schematic · not to scale</div>
</header>
<aside class="legend" aria-label="Color legend">
<div class="legend-title">Spatial key</div>
<div class="legend-row">
<span class="swatch" style="--swatch: #55e6ff"></span>
Airway lumen
</div>
<div class="legend-row">
<span class="swatch" style="--swatch: #bca5ff"></span>
Pleural region
</div>
<div class="legend-row">
<span class="swatch" style="--swatch: #e88b98"></span>
Lung parenchyma
</div>
<div class="legend-row">
<span class="swatch" style="--swatch: #f2bd6d"></span>
Diaphragm
</div>
<div class="legend-note">
Colors separate spaces conceptually; they are not literal tissue colors.
</div>
</aside>
<div class="controls">
<div class="hint">Drag to rotate · wheel to zoom · double-click to reset</div>
<button id="reset" type="button">Reset view</button>
<button id="toggle-labels" type="button">Hide labels</button>
</div>
<div class="view-index">Anterior-oblique cutaway / 01</div>
</main>
<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.querySelector("#scene");
const renderer = new THREE.WebGLRenderer({
canvas,
antialias: true,
alpha: true,
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.12;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.localClippingEnabled = true;
const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera(
38,
window.innerWidth / window.innerHeight,
0.1,
100
);
const model = new THREE.Group();
scene.add(model);
// Lighting
scene.add(new THREE.HemisphereLight(0xb9eaff, 0x18212e, 1.9));
const keyLight = new THREE.DirectionalLight(0xffffff, 3.2);
keyLight.position.set(4.5, 7, 7);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(1024, 1024);
keyLight.shadow.camera.left = -4;
keyLight.shadow.camera.right = 4;
keyLight.shadow.camera.top = 4;
keyLight.shadow.camera.bottom = -4;
scene.add(keyLight);
const cyanLight = new THREE.PointLight(0x36dfff, 22, 9, 2);
cyanLight.position.set(-3.5, 0.8, 3.5);
scene.add(cyanLight);
const violetLight = new THREE.PointLight(0x9b7cff, 18, 8, 2);
violetLight.position.set(3.8, -1, 1.4);
scene.add(violetLight);
const rimLight = new THREE.DirectionalLight(0x8eb8ff, 1.6);
rimLight.position.set(-4, 2, -5);
scene.add(rimLight);
// Shared materials
const lungMaterial = new THREE.MeshStandardMaterial({
color: 0xcd7180,
roughness: 0.64,
metalness: 0.02,
transparent: true,
opacity: 0.96,
side: THREE.DoubleSide
});
const lungFaceMaterial = new THREE.MeshStandardMaterial({
color: 0xe18a96,
roughness: 0.73,
metalness: 0,
side: THREE.DoubleSide
});
const fissureMaterial = new THREE.MeshStandardMaterial({
color: 0x9e4c60,
roughness: 0.78,
transparent: true,
opacity: 0.62
});
const pleuralMaterial = new THREE.MeshBasicMaterial({
color: 0xbca5ff,
transparent: true,
opacity: 0.105,
depthWrite: false,
side: THREE.DoubleSide,
blending: THREE.AdditiveBlending
});
const pleuralLineMaterial = new THREE.MeshBasicMaterial({
color: 0xc3afff,
transparent: true,
opacity: 0.82,
depthWrite: false
});
const airwayWallMaterial = new THREE.MeshStandardMaterial({
color: 0xf0dfee,
roughness: 0.32,
transparent: true,
opacity: 0.28,
depthWrite: false,
side: THREE.DoubleSide
});
const airwayLumenMaterial = new THREE.MeshStandardMaterial({
color: 0x49dfff,
emissive: 0x0bc4ec,
emissiveIntensity: 1.5,
roughness: 0.2,
metalness: 0.03
});
const cartilageMaterial = new THREE.MeshStandardMaterial({
color: 0xe6d8e8,
roughness: 0.44,
transparent: true,
opacity: 0.48,
depthWrite: false
});
// ------------------------------------------------------------
// Chest boundary: transparent, front-clipped ellipsoid.
// ------------------------------------------------------------
const chestCutPlane = new THREE.Plane(
new THREE.Vector3(0, 0, -1),
0
);
const chestMaterial = new THREE.MeshBasicMaterial({
color: 0x7bc9e8,
transparent: true,
opacity: 0.045,
depthWrite: false,
side: THREE.DoubleSide,
clippingPlanes: [chestCutPlane]
});
const chestShell = new THREE.Mesh(
new THREE.SphereGeometry(1, 64, 40),
chestMaterial
);
chestShell.scale.set(2.02, 2.48, 1.18);
chestShell.position.y = 0.08;
chestShell.renderOrder = 0;
model.add(chestShell);
// Cut-plane rim for the transparent thorax.
const chestRimPoints = [];
for (let i = 0; i < 128; i++) {
const a = (i / 128) * Math.PI * 2;
chestRimPoints.push(
new THREE.Vector3(
Math.cos(a) * 2.02,
0.08 + Math.sin(a) * 2.48,
0
)
);
}
const chestRim = new THREE.LineLoop(
new THREE.BufferGeometry().setFromPoints(chestRimPoints),
new THREE.LineBasicMaterial({
color: 0x7fcde8,
transparent: true,
opacity: 0.19,
depthWrite: false
})
);
model.add(chestRim);
// Posterior and lateral ribs, leaving the anterior wall open.
const ribMaterial = new THREE.MeshStandardMaterial({
color: 0xbdd9e5,
roughness: 0.48,
transparent: true,
opacity: 0.18,
depthWrite: false
});
for (const side of [-1, 1]) {
for (let i = 0; i < 8; i++) {
const y = -1.67 + i * 0.46;
const width = 1.72 - Math.max(0, y) * 0.29;
const depth = 0.84 - i * 0.035;
const points = [
new THREE.Vector3(side * 0.08, y, -depth),
new THREE.Vector3(side * width * 0.36, y + 0.02, -depth * 0.88),
new THREE.Vector3(side * width * 0.72, y + 0.03, -depth * 0.48),
new THREE.Vector3(side * width, y + 0.02, -0.02)
];
const curve = new THREE.CatmullRomCurve3(points);
const rib = new THREE.Mesh(
new THREE.TubeGeometry(curve, 30, 0.027, 8, false),
ribMaterial
);
rib.renderOrder = 1;
model.add(rib);
}
}
// Posterior vertebral column.
const vertebraMaterial = new THREE.MeshStandardMaterial({
color: 0x9eb9c8,
roughness: 0.7,
transparent: true,
opacity: 0.26
});
for (let i = 0; i < 13; i++) {
const vertebra = new THREE.Mesh(
new THREE.CylinderGeometry(0.08, 0.105, 0.145, 12),
vertebraMaterial
);
vertebra.position.set(0, -1.72 + i * 0.285, -0.91);
model.add(vertebra);
}
// ------------------------------------------------------------
// Lung geometry: posterior half-shell plus a flat cut face.
// ------------------------------------------------------------
function lungProfile(side, y, inflate = 1) {
const yy = THREE.MathUtils.clamp(y, -1.28, 1.28);
const theta = Math.acos(-yy / 1.28);
const u = theta / Math.PI;
const base =
Math.pow(Math.max(0, Math.sin(theta)), 0.72) *
(1.08 - 0.3 * u);
return {
center: side * (0.82 + 0.1 * (yy / 1.28)),
rx: 0.7 * base * inflate,
rz: 0.56 * base * inflate,
y: yy * inflate
};
}
function createLungSurface(side, inflate = 1) {
const positions = [];
const indices = [];
const rows = 38;
const columns = 48;
for (let j = 0; j <= rows; j++) {
const theta = (j / rows) * Math.PI;
const y = -1.28 * Math.cos(theta);
const p = lungProfile(side, y, inflate);
for (let i = 0; i <= columns; i++) {
const phi = Math.PI / 2 + (i / columns) * Math.PI;
let xOffset = side * p.rx * Math.sin(phi);
// Flatten the medial edge slightly.
if (xOffset * side < 0) {
xOffset *= 0.82;
}
positions.push(
p.center + xOffset,
p.y,
p.rz * Math.cos(phi) - 0.015
);
}
}
for (let j = 0; j < rows; j++) {
for (let i = 0; i < columns; i++) {
const a = j * (columns + 1) + i;
const b = a + 1;
const c = (j + 1) * (columns + 1) + i;
const d = c + 1;
indices.push(a, c, b, b, c, d);
}
}
const geometry = new THREE.BufferGeometry();
geometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(positions, 3)
);
geometry.setIndex(indices);
geometry.computeVertexNormals();
return geometry;
}
function createLungFace(side) {
const shape = new THREE.Shape();
const outer = [];
const inner = [];
const samples = 64;
for (let i = 0; i <= samples; i++) {
const y = 1.28 - (i / samples) * 2.56;
const p = lungProfile(side, y);
outer.push({
x: p.center + side * p.rx,
y: p.y
});
inner.push({
x: p.center - side * p.rx * 0.82,
y: p.y
});
}
shape.moveTo(outer[0].x, outer[0].y);
outer.forEach((p) => shape.lineTo(p.x, p.y));
for (let i = inner.length - 1; i >= 0; i--) {
shape.lineTo(inner[i].x, inner[i].y);
}
shape.closePath();
const geometry = new THREE.ShapeGeometry(shape, 32);
geometry.translate(0, 0, 0.008);
return geometry;
}
function createPleuralOutline(side) {
const points = [];
const samples = 64;
for (let i = 0; i <= samples; i++) {
const y = 1.28 - (i / samples) * 2.56;
const p = lungProfile(side, y, 1.035);
points.push(
new THREE.Vector3(
p.center + side * p.rx,
p.y,
0.026
)
);
}
for (let i = samples; i >= 0; i--) {
const y = 1.28 - (i / samples) * 2.56;
const p = lungProfile(side, y, 1.035);
points.push(
new THREE.Vector3(
p.center - side * p.rx * 0.82,
p.y,
0.026
)
);
}
const curve = new THREE.CatmullRomCurve3(
points,
true,
"centripetal"
);
return new THREE.Mesh(
new THREE.TubeGeometry(curve, 180, 0.017, 8, true),
pleuralLineMaterial
);
}
function edgePoint(side, y, outerAmount) {
const p = lungProfile(side, y);
const outerX = p.center + side * p.rx;
const innerX = p.center - side * p.rx * 0.82;
return new THREE.Vector3(
THREE.MathUtils.lerp(innerX, outerX, outerAmount),
y,
0.031
);
}
function createFissures(side) {
const curves = [];
if (side === -1) {
// Right lung: horizontal and oblique fissures.
curves.push(
new THREE.CatmullRomCurve3([
edgePoint(side, -0.08, 0),
edgePoint(side, -0.03, 0.45),
edgePoint(side, -0.09, 1)
])
);
}
curves.push(
new THREE.CatmullRomCurve3([
edgePoint(side, 0.57, 0.18),
edgePoint(side, 0.1, 0.48),
edgePoint(side, -0.42, 0.75),
edgePoint(side, -0.76, 0.88)
])
);
for (const curve of curves) {
const fissure = new THREE.Mesh(
new THREE.TubeGeometry(curve, 48, 0.012, 7, false),
fissureMaterial
);
model.add(fissure);
}
}
for (const side of [-1, 1]) {
const lung = new THREE.Mesh(
createLungSurface(side),
lungMaterial
);
lung.castShadow = true;
lung.receiveShadow = true;
model.add(lung);
const face = new THREE.Mesh(
createLungFace(side),
lungFaceMaterial
);
face.receiveShadow = true;
model.add(face);
const pleuralShell = new THREE.Mesh(
createLungSurface(side, 1.045),
pleuralMaterial
);
pleuralShell.renderOrder = 2;
model.add(pleuralShell);
const pleuralOutline = createPleuralOutline(side);
pleuralOutline.renderOrder = 5;
model.add(pleuralOutline);
createFissures(side);
}
// Small central mediastinal silhouette, behind the airway.
const heart = new THREE.Mesh(
new THREE.SphereGeometry(1, 32, 24),
new THREE.MeshStandardMaterial({
color: 0x8c3f58,
roughness: 0.65,
transparent: true,
opacity: 0.72
})
);
heart.position.set(0, -0.55, -0.2);
heart.scale.set(0.43, 0.61, 0.39);
heart.rotation.z = -0.12;
model.add(heart);
// ------------------------------------------------------------
// Airway tree: translucent wall surrounding a bright lumen.
// ------------------------------------------------------------
function addAirwaySegment(points, radius) {
const vectors = points.map(
(p) => new THREE.Vector3(p[0], p[1], p[2])
);
const curve = new THREE.CatmullRomCurve3(
vectors,
false,
"catmullrom",
0.25
);
const wall = new THREE.Mesh(
new THREE.TubeGeometry(
curve,
Math.max(16, points.length * 9),
radius,
14,
false
),
airwayWallMaterial
);
wall.renderOrder = 6;
model.add(wall);
const lumen = new THREE.Mesh(
new THREE.TubeGeometry(
curve,
Math.max(16, points.length * 9),
radius * 0.56,
14,
false
),
airwayLumenMaterial
);
lumen.renderOrder = 5;
model.add(lumen);
return { wall, lumen };
}
// Trachea
addAirwaySegment(
[
[0, 1.86, 0.25],
[0, 1.48, 0.24],
[0, 1.04, 0.22],
[0, 0.64, 0.2],
[0, 0.38, 0.17]
],
0.12
);
// Tracheal cartilage rings.
for (let i = 0; i < 10; i++) {
const t = i / 9;
const ring = new THREE.Mesh(
new THREE.TorusGeometry(0.12, 0.014, 8, 24),
cartilageMaterial
);
ring.rotation.x = Math.PI / 2;
ring.position.set(
0,
THREE.MathUtils.lerp(1.76, 0.53, t),
THREE.MathUtils.lerp(0.25, 0.19, t)
);
ring.renderOrder = 7;
model.add(ring);
}
// Main bronchi and intrapulmonary branches.
for (const side of [-1, 1]) {
const hilum = [side * 0.47, 0.29, 0.09];
addAirwaySegment(
[
[0, 0.39, 0.17],
[side * 0.23, 0.36, 0.14],
hilum
],
0.092
);
// Upper lobar branch.
addAirwaySegment(
[
hilum,
[side * 0.59, 0.57, 0.075],
[side * 0.47, 0.9, 0.055]
],
0.064
);
addAirwaySegment(
[
[side * 0.47, 0.9, 0.055],
[side * 0.28, 1.03, 0.045],
[side * 0.16, 1.15, 0.04]
],
0.035
);
addAirwaySegment(
[
[side * 0.47, 0.9, 0.055],
[side * 0.67, 1.02, 0.045],
[side * 0.78, 1.11, 0.04]
],
0.035
);
// Middle branch, present on the schematic right lung only.
if (side === -1) {
addAirwaySegment(
[
hilum,
[side * 0.61, 0.04, 0.07],
[side * 0.56, -0.21, 0.055]
],
0.058
);
addAirwaySegment(
[
[side * 0.56, -0.21, 0.055],
[side * 0.78, -0.31, 0.045],
[side * 0.83, -0.43, 0.04]
],
0.032
);
addAirwaySegment(
[
[side * 0.56, -0.21, 0.055],
[side * 0.4, -0.35, 0.045],
[side * 0.31, -0.47, 0.04]
],
0.032
);
}
// Lower lobar branch.
addAirwaySegment(
[
hilum,
[side * 0.66, -0.06, 0.065],
[side * 0.77, -0.53, 0.045],
[side * 0.61, -0.94, 0.035]
],
0.064
);
addAirwaySegment(
[
[side * 0.77, -0.53, 0.045],
[side * 0.98, -0.63, 0.04],
[side * 1.06, -0.77, 0.035]
],
0.034
);
addAirwaySegment(
[
[side * 0.77, -0.53, 0.045],
[side * 0.57, -0.7, 0.04],
[side * 0.48, -0.84, 0.035]
],
0.034
);
addAirwaySegment(
[
[side * 0.61, -0.94, 0.035],
[side * 0.7, -1.06, 0.03],
[side * 0.72, -1.17, 0.025]
],
0.029
);
}
// ------------------------------------------------------------
// Diaphragm: shallow elliptical dome.
// ------------------------------------------------------------
function createDiaphragm() {
const positions = [0, -1.1, -0.04];
const indices = [];
const rings = 8;
const segments = 64;
for (let ring = 1; ring <= rings; ring++) {
const r = ring / rings;
for (let i = 0; i < segments; i++) {
const a = (i / segments) * Math.PI * 2;
positions.push(
Math.cos(a) * 1.84 * r,
-1.48 + 0.38 * (1 - r * r),
Math.sin(a) * 0.83 * r - 0.06
);
}
}
for (let i = 0; i < segments; i++) {
indices.push(0, 1 + i, 1 + ((i + 1) % segments));
}
for (let ring = 1; ring < rings; ring++) {
const a0 = 1 + (ring - 1) * segments;
const b0 = 1 + ring * segments;
for (let i = 0; i < segments; i++) {
const next = (i + 1) % segments;
const a = a0 + i;
const b = a0 + next;
const c = b0 + i;
const d = b0 + next;
indices.push(a, c, b, b, c, d);
}
}
const geometry = new THREE.BufferGeometry();
geometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(positions, 3)
);
geometry.setIndex(indices);
geometry.computeVertexNormals();
const material = new THREE.MeshStandardMaterial({
color: 0xf2bd6d,
emissive: 0x5f3611,
emissiveIntensity: 0.28,
roughness: 0.72,
transparent: true,
opacity: 0.22,
depthWrite: false,
side: THREE.DoubleSide
});
const diaphragm = new THREE.Mesh(geometry, material);
diaphragm.renderOrder = 3;
model.add(diaphragm);
const rimPoints = [];
for (let i = 0; i < 96; i++) {
const a = (i / 96) * Math.PI * 2;
rimPoints.push(
new THREE.Vector3(
Math.cos(a) * 1.84,
-1.48,
Math.sin(a) * 0.83 - 0.06
)
);
}
const rimCurve = new THREE.CatmullRomCurve3(
rimPoints,
true,
"centripetal"
);
const rim = new THREE.Mesh(
new THREE.TubeGeometry(rimCurve, 150, 0.018, 7, true),
new THREE.MeshBasicMaterial({
color: 0xf2bd6d,
transparent: true,
opacity: 0.48,
depthWrite: false
})
);
rim.renderOrder = 4;
model.add(rim);
}
createDiaphragm();
// ------------------------------------------------------------
// Direct camera controls: orbit, zoom, reset.
// ------------------------------------------------------------
const defaultView = {
theta: 0.43,
phi: 1.34,
radius: 8.15
};
const target = new THREE.Vector3(0, -0.06, 0);
let theta = defaultView.theta;
let phi = defaultView.phi;
let radius = defaultView.radius;
let dragging = false;
let lastX = 0;
let lastY = 0;
function updateCamera() {
const sinPhi = Math.sin(phi);
camera.position.set(
target.x + radius * sinPhi * Math.sin(theta),
target.y + radius * Math.cos(phi),
target.z + radius * sinPhi * Math.cos(theta)
);
camera.lookAt(target);
}
function resetView() {
theta = defaultView.theta;
phi = defaultView.phi;
radius = defaultView.radius;
updateCamera();
}
canvas.addEventListener("pointerdown", (event) => {
dragging = true;
lastX = event.clientX;
lastY = event.clientY;
canvas.classList.add("dragging");
canvas.setPointerCapture(event.pointerId);
});
canvas.addEventListener("pointermove", (event) => {
if (!dragging) return;
const dx = event.clientX - lastX;
const dy = event.clientY - lastY;
lastX = event.clientX;
lastY = event.clientY;
theta -= dx * 0.0055;
phi = THREE.MathUtils.clamp(phi - dy * 0.0045, 0.58, 2.35);
updateCamera();
});
function endDrag(event) {
dragging = false;
canvas.classList.remove("dragging");
if (event.pointerId !== undefined && canvas.hasPointerCapture(event.pointerId)) {
canvas.releasePointerCapture(event.pointerId);
}
}
canvas.addEventListener("pointerup", endDrag);
canvas.addEventListener("pointercancel", endDrag);
canvas.addEventListener(
"wheel",
(event) => {
event.preventDefault();
radius = THREE.MathUtils.clamp(
radius + event.deltaY * 0.006,
5.4,
11.5
);
updateCamera();
},
{ passive: false }
);
canvas.addEventListener("dblclick", resetView);
document.querySelector("#reset").addEventListener("click", resetView);
const labelToggle = document.querySelector("#toggle-labels");
labelToggle.addEventListener("click", () => {
const hidden = document.body.classList.toggle("labels-hidden");
labelToggle.textContent = hidden ? "Show labels" : "Hide labels";
});
// ------------------------------------------------------------
// DOM callouts with projected SVG leader lines.
// ------------------------------------------------------------
const svg = document.querySelector("#leaders");
const svgNS = "http://www.w3.org/2000/svg";
const labelDefinitions = [
{
id: "trachea",
anchor: new THREE.Vector3(0, 1.26, 0.23),
side: "left"
},
{
id: "bronchi",
anchor: new THREE.Vector3(-0.27, 0.36, 0.12),
side: "right"
},
{
id: "pleural",
anchor: new THREE.Vector3(-1.48, -0.25, 0.025),
side: "left"
},
{
id: "lung-label",
anchor: new THREE.Vector3(0.94, -0.66, 0.03),
side: "right"
},
{
id: "lumen",
anchor: new THREE.Vector3(0.72, 0.13, 0.065),
side: "left"
},
{
id: "diaphragm-label",
anchor: new THREE.Vector3(0.15, -1.38, 0.25),
side: "right"
}
];
for (const definition of labelDefinitions) {
definition.element = document.getElementById(definition.id);
definition.line = document.createElementNS(svgNS, "line");
definition.line.setAttribute("stroke", "rgba(137, 210, 235, 0.46)");
definition.line.setAttribute("stroke-width", "1");
definition.line.setAttribute("vector-effect", "non-scaling-stroke");
definition.dot = document.createElementNS(svgNS, "circle");
definition.dot.setAttribute("r", "2.7");
definition.dot.setAttribute("fill", "#9ae8ff");
definition.dot.setAttribute("stroke", "rgba(255,255,255,0.45)");
definition.dot.setAttribute("stroke-width", "0.7");
svg.appendChild(definition.line);
svg.appendChild(definition.dot);
}
function updateCallouts() {
const width = window.innerWidth;
const height = window.innerHeight;
svg.setAttribute("viewBox", `0 0 ${width} ${height}`);
for (const definition of labelDefinitions) {
const projected = definition.anchor.clone().project(camera);
const visible = projected.z > -1 && projected.z < 1;
const x = (projected.x * 0.5 + 0.5) * width;
const y = (-projected.y * 0.5 + 0.5) * height;
const rect = definition.element.getBoundingClientRect();
const endX =
definition.side === "left"
? rect.right + 8
: rect.left - 8;
const endY = rect.top + rect.height / 2;
definition.line.setAttribute("x1", x);
definition.line.setAttribute("y1", y);
definition.line.setAttribute("x2", endX);
definition.line.setAttribute("y2", endY);
definition.line.style.opacity = visible ? "1" : "0";
definition.dot.setAttribute("cx", x);
definition.dot.setAttribute("cy", y);
definition.dot.style.opacity = visible ? "1" : "0";
}
}
// ------------------------------------------------------------
// Resize and render loop.
// ------------------------------------------------------------
function onResize() {
const width = window.innerWidth;
const height = window.innerHeight;
camera.aspect = width / height;
camera.updateProjectionMatrix();
renderer.setSize(width, height, false);
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
}
window.addEventListener("resize", onResize);
updateCamera();
function animate() {
requestAnimationFrame(animate);
renderer.render(scene, camera);
updateCallouts();
}
animate();
</script>
</body>
</html>
```