Prompt specification
Build or customize the Three.js 3D scene "Nephron route and vessel model" inside the active workspace.
Engineering Constraints:
1. Runtime & Orbit: Use Three.js r160 with OrbitControls (damping enabled) and ACESFilmicToneMapping.
2. Frame Timing: Drive all motion strictly with clock.getDelta() so animation speed is FPS-independent.
3. Viewport Hygiene: Handle window resize cleanly, cap DPR at Math.min(window.devicePixelRatio, 2), and dispose geometries/materials/renderer on unmount.
Scene Metadata:
Scene: Nephron route and vessel model
Category: Biology & Nature · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/nephron-route-and-vessel-model-x1001
Scene Description Prompt:
Nephron route and vessel model. Create one simplified nephron with glomerular region, tubular path, and nearby vessel route; distinguish filtrate inside the tubule from blood remaining in vessels. Please build this with Three.js from cdnjs in one self-contained HTML file, using a 3D cutaway.
Working Three.js Starter (nephron-route-and-vessel-model-x1001):
```html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>3D Nephron Cutaway</title>
<style>
:root {
color-scheme: dark;
--cyan: #52d9ff;
--cyan-soft: #b9f4ff;
--red: #ff5364;
--red-soft: #ffc0c7;
--wall: #f3d7bd;
--ink: #eaf7ff;
--muted: #91a9b8;
--panel: rgba(8, 20, 31, 0.78);
--line: rgba(125, 205, 235, 0.32);
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background:
radial-gradient(circle at 50% 42%, #12283a 0%, #08131e 48%, #040a11 100%);
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
outline: none;
}
#label-layer {
position: fixed;
inset: 0;
z-index: 3;
pointer-events: none;
}
#leader-lines {
position: absolute;
inset: 0;
width: 100%;
height: 100%;
overflow: visible;
}
.model-label {
position: absolute;
min-width: 112px;
max-width: 190px;
padding: 7px 10px;
border: 1px solid rgba(132, 213, 239, 0.25);
border-radius: 8px;
background: rgba(6, 17, 27, 0.82);
box-shadow: 0 6px 20px rgba(0, 0, 0, 0.24);
backdrop-filter: blur(10px);
transform: translate(-50%, -50%);
white-space: nowrap;
color: var(--ink);
font-size: 12px;
font-weight: 650;
letter-spacing: 0.01em;
}
.model-label small {
display: block;
margin-top: 2px;
color: var(--muted);
font-size: 9px;
font-weight: 500;
letter-spacing: 0.035em;
text-transform: uppercase;
}
.model-label.blood {
border-color: rgba(255, 83, 100, 0.35);
}
.model-label.filtrate {
border-color: rgba(82, 217, 255, 0.4);
}
.ui {
position: fixed;
z-index: 5;
color: var(--ink);
}
#title {
top: 24px;
left: 28px;
pointer-events: none;
}
#title .eyebrow {
color: var(--cyan);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.22em;
text-transform: uppercase;
}
#title h1 {
margin: 5px 0 4px;
font-size: clamp(24px, 3vw, 38px);
font-weight: 700;
letter-spacing: -0.045em;
}
#title p {
margin: 0;
color: var(--muted);
font-size: 12px;
}
#controls {
top: 22px;
right: 24px;
display: flex;
gap: 7px;
padding: 7px;
border: 1px solid var(--line);
border-radius: 12px;
background: var(--panel);
backdrop-filter: blur(14px);
box-shadow: 0 10px 30px rgba(0, 0, 0, 0.2);
}
button {
border: 1px solid rgba(129, 202, 226, 0.2);
border-radius: 8px;
padding: 9px 12px;
color: var(--ink);
background: rgba(39, 76, 94, 0.32);
font: inherit;
font-size: 11px;
font-weight: 700;
cursor: pointer;
transition: background 140ms ease, border-color 140ms ease, transform 140ms ease;
}
button:hover {
background: rgba(58, 119, 143, 0.48);
border-color: rgba(129, 202, 226, 0.5);
}
button:active {
transform: translateY(1px);
}
#legend {
left: 24px;
bottom: 24px;
width: 224px;
padding: 14px 15px;
border: 1px solid var(--line);
border-radius: 13px;
background: var(--panel);
backdrop-filter: blur(14px);
box-shadow: 0 10px 30px rgba(0, 0, 0, 0.2);
}
#legend h2 {
margin: 0 0 10px;
font-size: 10px;
letter-spacing: 0.16em;
text-transform: uppercase;
color: var(--muted);
}
.legend-row {
display: flex;
align-items: center;
gap: 10px;
margin-top: 8px;
color: #dcecf4;
font-size: 11px;
}
.swatch {
width: 22px;
height: 7px;
flex: 0 0 auto;
border-radius: 8px;
box-shadow: 0 0 12px currentColor;
}
.swatch.filtrate {
color: var(--cyan);
background: var(--cyan);
}
.swatch.blood {
color: var(--red);
background: var(--red);
}
.swatch.wall {
color: var(--wall);
background: var(--wall);
}
.swatch.kidney {
color: #a95f82;
background: #a95f82;
}
#hint {
right: 24px;
bottom: 24px;
max-width: 260px;
padding: 10px 13px;
border: 1px solid var(--line);
border-radius: 11px;
color: var(--muted);
background: rgba(8, 20, 31, 0.66);
backdrop-filter: blur(10px);
font-size: 10px;
line-height: 1.55;
text-align: right;
}
#status {
position: fixed;
z-index: 5;
left: 50%;
bottom: 24px;
transform: translateX(-50%);
color: rgba(215, 239, 249, 0.64);
font-size: 10px;
letter-spacing: 0.09em;
text-transform: uppercase;
pointer-events: none;
}
@media (max-width: 760px) {
#title {
top: 17px;
left: 17px;
}
#title h1 {
font-size: 25px;
}
#title p {
max-width: 210px;
}
#controls {
top: auto;
right: 14px;
bottom: 15px;
}
button {
padding: 8px 9px;
font-size: 10px;
}
#legend {
left: 14px;
bottom: 72px;
width: 180px;
padding: 11px 12px;
}
#hint,
#status,
.minor-label {
display: none !important;
}
.model-label {
min-width: 92px;
padding: 5px 7px;
font-size: 10px;
}
.model-label small {
display: none;
}
}
</style>
</head>
<body>
<div id="label-layer">
<svg id="leader-lines" aria-hidden="true"></svg>
</div>
<header id="title" class="ui">
<div class="eyebrow">Renal physiology · 3D cutaway</div>
<h1>Nephron Route</h1>
<p>Filtrate inside the tubule versus blood inside the vessels.</p>
</header>
<nav id="controls" class="ui" aria-label="Model controls">
<button id="flow-button" type="button">Pause flow</button>
<button id="cutaway-button" type="button">Cutaway: on</button>
<button id="reset-button" type="button">Reset view</button>
</nav>
<aside id="legend" class="ui">
<h2>Model key</h2>
<div class="legend-row">
<span class="swatch filtrate"></span>
<span>Filtrate inside tubule</span>
</div>
<div class="legend-row">
<span class="swatch blood"></span>
<span>Blood inside vessels</span>
</div>
<div class="legend-row">
<span class="swatch wall"></span>
<span>Tubule / vessel wall</span>
</div>
<div class="legend-row">
<span class="swatch kidney"></span>
<span>Kidney section</span>
</div>
</aside>
<div id="hint" class="ui">
Drag to orbit · Shift-drag or right-drag to pan<br>
Scroll to zoom · R to reset · Arrow keys to orbit
</div>
<div id="status" class="ui">Cyan particles = filtrate · Red particles = blood</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// -------------------------------------------------------------------------
// Renderer, scene, camera, and lights
// -------------------------------------------------------------------------
const renderer = new THREE.WebGLRenderer({
antialias: true,
alpha: false,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setClearColor(0x071019, 1);
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.12;
renderer.localClippingEnabled = true;
renderer.domElement.setAttribute(
"aria-label",
"Interactive 3D cutaway model of a nephron, filtrate route, and blood vessels"
);
renderer.domElement.tabIndex = 0;
document.body.prepend(renderer.domElement);
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x071019);
scene.fog = new THREE.FogExp2(0x071019, 0.028);
const camera = new THREE.PerspectiveCamera(
42,
window.innerWidth / window.innerHeight,
0.1,
100
);
const target = new THREE.Vector3(0, -0.05, 0.9);
const view = {
theta: 0.42,
phi: 1.18,
radius: 8.7
};
const hemisphere = new THREE.HemisphereLight(0xb9eaff, 0x24101f, 2.1);
scene.add(hemisphere);
const keyLight = new THREE.DirectionalLight(0xffffff, 3.0);
keyLight.position.set(4, 7, 8);
scene.add(keyLight);
const rimLight = new THREE.DirectionalLight(0x55d7ff, 2.1);
rimLight.position.set(-6, 2, 4);
scene.add(rimLight);
const warmLight = new THREE.PointLight(0xff6c83, 18, 12, 2);
warmLight.position.set(4, -2, 4);
scene.add(warmLight);
// A camera-facing clipping plane removes the near half of tubular and
// vascular walls, exposing the lumens while preserving the full fluid cores.
const wallCutPlane = new THREE.Plane(new THREE.Vector3(0, 0, -1), 1.0);
const kidneyCutPlane = new THREE.Plane(new THREE.Vector3(0, 0, -1), 0.22);
const cutMaterials = {
walls: [],
kidney: []
};
let cutawayEnabled = true;
let flowing = true;
let elapsed = 0;
// -------------------------------------------------------------------------
// Kidney cutaway
// -------------------------------------------------------------------------
const kidneyGeometry = new THREE.SphereGeometry(1, 64, 48);
const kidneyPositions = kidneyGeometry.attributes.position;
for (let i = 0; i < kidneyPositions.count; i++) {
const x = kidneyPositions.getX(i);
const y = kidneyPositions.getY(i);
const z = kidneyPositions.getZ(i);
let newX = x * 1.45;
if (x > 0) {
newX -= (
0.95 *
Math.exp(-(y * y) / 1.4) *
Math.pow(x, 0.85)
);
}
kidneyPositions.setXYZ(i, newX, y * 2.35, z * 0.76);
}
kidneyGeometry.computeVertexNormals();
const kidneyMaterial = new THREE.MeshStandardMaterial({
color: 0x8c4165,
emissive: 0x2a0c1d,
emissiveIntensity: 0.35,
roughness: 0.66,
metalness: 0.02,
transparent: true,
opacity: 0.17,
side: THREE.DoubleSide,
depthWrite: false,
clippingPlanes: [kidneyCutPlane]
});
const kidney = new THREE.Mesh(kidneyGeometry, kidneyMaterial);
kidney.renderOrder = 0;
scene.add(kidney);
cutMaterials.kidney.push(kidneyMaterial);
const kidneyWireMaterial = new THREE.MeshBasicMaterial({
color: 0xc07b9d,
wireframe: true,
transparent: true,
opacity: 0.027,
depthWrite: false,
clippingPlanes: [kidneyCutPlane]
});
const kidneyWire = new THREE.Mesh(kidneyGeometry, kidneyWireMaterial);
kidneyWire.renderOrder = 1;
scene.add(kidneyWire);
cutMaterials.kidney.push(kidneyWireMaterial);
function shapeFromPoints(points) {
const shape = new THREE.Shape();
shape.moveTo(points[0][0], points[0][1]);
for (let i = 1; i < points.length; i++) {
shape.lineTo(points[i][0], points[i][1]);
}
shape.closePath();
return shape;
}
const sectionOutlinePoints = [
[-1.48, -2.18],
[-1.57, -1.28],
[-1.50, -0.10],
[-1.34, 1.20],
[-0.87, 2.03],
[-0.20, 2.31],
[0.45, 2.14],
[0.96, 1.54],
[1.21, 0.73],
[0.96, 0.27],
[0.53, 0.02],
[0.62, -0.39],
[1.08, -1.00],
[1.30, -1.66],
[0.83, -2.16],
[-0.08, -2.38]
];
const sectionShape = shapeFromPoints(sectionOutlinePoints);
const sectionFillMaterial = new THREE.MeshBasicMaterial({
color: 0x8d3b61,
transparent: true,
opacity: 0.13,
side: THREE.DoubleSide,
depthWrite: false
});
const sectionFill = new THREE.Mesh(
new THREE.ShapeGeometry(sectionShape),
sectionFillMaterial
);
sectionFill.position.z = 0.225;
sectionFill.renderOrder = 0;
scene.add(sectionFill);
const medullaShape = shapeFromPoints([
[-0.58, -1.94],
[0.49, -1.84],
[0.69, 0.10],
[0.12, 0.86],
[-0.38, 0.56],
[-0.67, -0.28]
]);
const medulla = new THREE.Mesh(
new THREE.ShapeGeometry(medullaShape),
new THREE.MeshBasicMaterial({
color: 0x4a1936,
transparent: true,
opacity: 0.28,
side: THREE.DoubleSide,
depthWrite: false
})
);
medulla.position.z = 0.23;
scene.add(medulla);
const sectionOutline = new THREE.LineLoop(
new THREE.BufferGeometry().setFromPoints(
sectionOutlinePoints.map(
(point) => new THREE.Vector3(point[0], point[1], 0.24)
)
),
new THREE.LineBasicMaterial({
color: 0xd084a8,
transparent: true,
opacity: 0.64
})
);
scene.add(sectionOutline);
// -------------------------------------------------------------------------
// Route and tube helpers
// -------------------------------------------------------------------------
function curveFrom(points) {
return new THREE.CatmullRomCurve3(
points.map((point) => new THREE.Vector3(...point)),
false,
"centripetal",
0.5
);
}
function addTubePair({
curve,
outerRadius,
innerRadius,
wallColor,
wallOpacity = 0.30,
fluidColor,
fluidOpacity = 1,
segments = 110,
radialSegments = 14,
emissiveIntensity = 0.35
}) {
const wallGeometry = new THREE.TubeGeometry(
curve,
segments,
outerRadius,
radialSegments,
false
);
const wallMaterial = new THREE.MeshStandardMaterial({
color: wallColor,
emissive: wallColor,
emissiveIntensity: emissiveIntensity * 0.25,
roughness: 0.38,
metalness: 0.02,
transparent: true,
opacity: wallOpacity,
side: THREE.DoubleSide,
depthWrite: false,
clippingPlanes: [wallCutPlane]
});
const wall = new THREE.Mesh(wallGeometry, wallMaterial);
wall.renderOrder = 3;
scene.add(wall);
cutMaterials.walls.push(wallMaterial);
const fluidGeometry = new THREE.TubeGeometry(
curve,
segments,
innerRadius,
radialSegments,
false
);
const fluidMaterial = new THREE.MeshStandardMaterial({
color: fluidColor,
emissive: fluidColor,
emissiveIntensity,
roughness: 0.26,
metalness: 0.03,
transparent: fluidOpacity < 1,
opacity: fluidOpacity
});
const fluid = new THREE.Mesh(fluidGeometry, fluidMaterial);
fluid.renderOrder = 2;
scene.add(fluid);
return { wall, fluid, wallMaterial, fluidMaterial };
}
const filtrateColor = 0x49d8ff;
const bloodColor = 0xff4054;
// Filtered fluid path, divided into named nephron segments.
const proximalCurve = curveFrom([
[-0.72, 1.42, 0.98],
[-1.08, 1.18, 1.02],
[-1.43, 0.78, 1.04],
[-1.24, 0.38, 0.96],
[-0.82, 0.43, 0.94]
]);
const loopCurve = curveFrom([
[-0.82, 0.43, 0.94],
[-0.57, 0.02, 0.96],
[-0.39, -0.43, 1.00],
[-0.25, -0.85, 0.97],
[0.00, -1.04, 0.95],
[0.28, -0.93, 1.01],
[0.35, -0.50, 1.06],
[0.27, -0.05, 1.08]
]);
const distalCurve = curveFrom([
[0.27, -0.05, 1.08],
[0.50, 0.48, 1.05],
[0.88, 0.61, 0.96],
[1.05, 0.96, 0.94],
[0.78, 1.21, 1.00],
[0.40, 1.33, 1.04],
[0.05, 1.17, 1.03]
]);
const collectingCurve = curveFrom([
[0.05, 1.17, 1.03],
[0.04, 0.76, 1.07],
[0.01, 0.20, 1.10],
[-0.04, -0.44, 1.14],
[-0.09, -1.12, 1.18]
]);
addTubePair({
curve: proximalCurve,
outerRadius: 0.145,
innerRadius: 0.088,
wallColor: 0xf1d4b8,
fluidColor: filtrateColor,
emissiveIntensity: 0.55
});
addTubePair({
curve: loopCurve,
outerRadius: 0.118,
innerRadius: 0.070,
wallColor: 0xf1d4b8,
fluidColor: filtrateColor,
emissiveIntensity: 0.55
});
addTubePair({
curve: distalCurve,
outerRadius: 0.132,
innerRadius: 0.080,
wallColor: 0xf1d4b8,
fluidColor: filtrateColor,
emissiveIntensity: 0.55
});
addTubePair({
curve: collectingCurve,
outerRadius: 0.182,
innerRadius: 0.116,
wallColor: 0xb8e3df,
wallOpacity: 0.25,
fluidColor: 0x65e8ff,
emissiveIntensity: 0.68,
segments: 120
});
// Blood remains inside a separate vascular network.
const arteryCurve = curveFrom([
[2.75, 1.45, 1.28],
[1.80, 1.52, 1.28],
[0.80, 1.47, 1.18],
[-0.68, 1.42, 1.00]
]);
const peritubularCurve = curveFrom([
[-0.68, 1.42, 1.00],
[-0.10, 1.74, 1.18],
[0.55, 1.70, 1.30],
[1.12, 1.40, 1.32],
[0.90, 0.88, 1.30],
[0.35, 0.48, 1.28],
[-0.22, 0.24, 1.28],
[-0.53, -0.28, 1.29],
[-0.28, -0.83, 1.29],
[0.12, -0.92, 1.34],
[0.48, -0.68, 1.33],
[0.62, -0.20, 1.31],
[0.92, 0.28, 1.31],
[1.20, 0.62, 1.29],
[1.24, 1.02, 1.28],
[1.52, 0.78, 1.28],
[1.78, 0.16, 1.27],
[2.45, -0.30, 1.27],
[2.72, -1.42, 1.27]
]);
addTubePair({
curve: arteryCurve,
outerRadius: 0.105,
innerRadius: 0.066,
wallColor: 0x7e1d2c,
wallOpacity: 0.42,
fluidColor: bloodColor,
emissiveIntensity: 0.52,
segments: 100
});
addTubePair({
curve: peritubularCurve,
outerRadius: 0.090,
innerRadius: 0.055,
wallColor: 0x7e1d2c,
wallOpacity: 0.36,
fluidColor: bloodColor,
emissiveIntensity: 0.52,
segments: 180
});
// -------------------------------------------------------------------------
// Glomerular region and Bowman's capsule
// -------------------------------------------------------------------------
const glomerulusPosition = new THREE.Vector3(-0.72, 1.42, 1.0);
const capsuleMaterial = new THREE.MeshPhysicalMaterial({
color: 0xb9f1ff,
emissive: 0x23566c,
emissiveIntensity: 0.22,
roughness: 0.18,
metalness: 0,
transparent: true,
opacity: 0.18,
transmission: 0.15,
side: THREE.DoubleSide,
depthWrite: false,
clippingPlanes: [wallCutPlane]
});
const capsule = new THREE.Mesh(
new THREE.SphereGeometry(0.48, 40, 28),
capsuleMaterial
);
capsule.position.copy(glomerulusPosition);
capsule.scale.set(1.08, 0.93, 0.83);
capsule.renderOrder = 4;
scene.add(capsule);
cutMaterials.walls.push(capsuleMaterial);
const capsuleHalo = new THREE.Mesh(
new THREE.TorusGeometry(0.47, 0.012, 8, 80),
new THREE.MeshBasicMaterial({
color: 0x82eaff,
transparent: true,
opacity: 0.38,
depthWrite: false
})
);
capsuleHalo.position.copy(glomerulusPosition);
capsuleHalo.scale.set(1.08, 0.93, 1);
scene.add(capsuleHalo);
const tuftGroup = new THREE.Group();
tuftGroup.position.copy(glomerulusPosition);
scene.add(tuftGroup);
const tuftMaterial = new THREE.MeshStandardMaterial({
color: bloodColor,
emissive: bloodColor,
emissiveIntensity: 0.45,
roughness: 0.35,
metalness: 0.02
});
for (let i = 0; i < 7; i++) {
const loop = new THREE.Mesh(
new THREE.TorusGeometry(
0.135 + i * 0.012,
0.026,
9,
44,
Math.PI * (1.25 + (i % 3) * 0.14)
),
tuftMaterial
);
loop.rotation.set(
0.35 + i * 0.48,
-0.55 + i * 0.39,
i * 0.27
);
loop.position.set(
Math.sin(i * 1.7) * 0.055,
Math.cos(i * 1.3) * 0.06,
Math.sin(i * 2.1) * 0.035
);
tuftGroup.add(loop);
}
const tuftCore = new THREE.Mesh(
new THREE.SphereGeometry(0.10, 18, 14),
tuftMaterial
);
tuftGroup.add(tuftCore);
// -------------------------------------------------------------------------
// Animated flow particles
// -------------------------------------------------------------------------
const flows = [];
const up = new THREE.Vector3(0, 1, 0);
const dummy = new THREE.Object3D();
function addFlow(curve, {
count = 8,
color = filtrateColor,
speed = 0.08,
radius = 0.033,
kind = "filtrate",
phase = 0
} = {}) {
const geometry = new THREE.SphereGeometry(radius, 10, 8);
const material = new THREE.MeshStandardMaterial({
color,
emissive: color,
emissiveIntensity: 1.1,
roughness: 0.24,
metalness: 0.02
});
const mesh = new THREE.InstancedMesh(geometry, material, count);
mesh.instanceMatrix.setUsage(THREE.DynamicDrawUsage);
mesh.frustumCulled = false;
mesh.renderOrder = 5;
scene.add(mesh);
flows.push({ curve, count, speed, kind, phase, mesh });
}
function addArrow(curve, u, color, scale = 1) {
const arrow = new THREE.Mesh(
new THREE.ConeGeometry(0.055 * scale, 0.16 * scale, 12),
new THREE.MeshBasicMaterial({
color,
transparent: true,
opacity: 0.82,
depthWrite: false
})
);
arrow.position.copy(curve.getPointAt(u));
arrow.quaternion.setFromUnitVectors(up, curve.getTangentAt(u).normalize());
arrow.renderOrder = 6;
scene.add(arrow);
}
addFlow(proximalCurve, { count: 8, speed: 0.075, phase: 0.00 });
addFlow(loopCurve, { count: 12, speed: 0.065, phase: 0.15 });
addFlow(distalCurve, { count: 8, speed: 0.080, phase: 0.32 });
addFlow(collectingCurve, { count: 10, speed: 0.095, phase: 0.46 });
addFlow(arteryCurve, {
count: 10,
color: 0xff5364,
speed: 0.12,
radius: 0.043,
kind: "blood",
phase: 0.05
});
addFlow(peritubularCurve, {
count: 22,
color: 0xff5364,
speed: 0.105,
radius: 0.037,
kind: "blood",
phase: 0.24
});
addArrow(proximalCurve, 0.60, 0xb8f6ff, 0.85);
addArrow(loopCurve, 0.50, 0xb8f6ff, 0.80);
addArrow(distalCurve, 0.53, 0xb8f6ff, 0.85);
addArrow(collectingCurve, 0.56, 0xd8fbff, 1.0);
addArrow(arteryCurve, 0.55, 0xffb3bc, 0.85);
addArrow(peritubularCurve, 0.30, 0xffb3bc, 0.80);
addArrow(peritubularCurve, 0.76, 0xffb3bc, 0.80);
// -------------------------------------------------------------------------
// Projected HTML labels
// -------------------------------------------------------------------------
const labelLayer = document.querySelector("#label-layer");
const leaderSVG = document.querySelector("#leader-lines");
const labels = [];
function addLabel({
title,
subtitle = "",
anchor,
offset = [0, 0],
className = "",
minor = false
}) {
const element = document.createElement("div");
element.className = `model-label ${className} ${minor ? "minor-label" : ""}`;
element.innerHTML = `${title}${subtitle ? `<small>${subtitle}</small>` : ""}`;
labelLayer.appendChild(element);
const line = document.createElementNS(
"http://www.w3.org/2000/svg",
"line"
);
line.setAttribute("stroke", "rgba(133, 207, 231, 0.48)");
line.setAttribute("stroke-width", "1");
line.setAttribute("stroke-dasharray", "3 4");
leaderSVG.appendChild(line);
labels.push({
element,
line,
anchor: anchor.clone(),
offset
});
}
addLabel({
title: "Glomerulus",
subtitle: "Renal corpuscle",
anchor: glomerulusPosition,
offset: [-115, -60]
});
addLabel({
title: "Proximal tubule",
subtitle: "Filtrate inside",
anchor: proximalCurve.getPointAt(0.54),
offset: [-125, 12],
className: "filtrate"
});
addLabel({
title: "Loop of Henle",
subtitle: "Descending · ascending",
anchor: loopCurve.getPointAt(0.50),
offset: [-92, 76],
className: "filtrate"
});
addLabel({
title: "Distal tubule",
subtitle: "Filtrate inside",
anchor: distalCurve.getPointAt(0.48),
offset: [95, -44],
className: "filtrate"
});
addLabel({
title: "Collecting duct",
subtitle: "Filtrate continues",
anchor: collectingCurve.getPointAt(0.58),
offset: [98, 80],
className: "filtrate"
});
addLabel({
title: "Renal artery",
subtitle: "Blood toward glomerulus",
anchor: arteryCurve.getPointAt(0.73),
offset: [82, -64],
className: "blood"
});
addLabel({
title: "Peritubular vessels",
subtitle: "Blood remains in vessels",
anchor: peritubularCurve.getPointAt(0.39),
offset: [115, -12],
className: "blood"
});
addLabel({
title: "Renal vein",
subtitle: "Blood returns to circulation",
anchor: peritubularCurve.getPointAt(0.97),
offset: [70, 53],
className: "blood"
});
addLabel({
title: "Cortex",
anchor: new THREE.Vector3(-1.05, 1.45, 0.26),
offset: [-92, -62],
minor: true
});
addLabel({
title: "Medulla",
anchor: new THREE.Vector3(0.20, -0.72, 0.26),
offset: [-78, 66],
minor: true
});
function updateLabels() {
const width = window.innerWidth;
const height = window.innerHeight;
const offsetScale = width < 760 ? 0.62 : 1;
for (const item of labels) {
const projected = item.anchor.clone().project(camera);
if (projected.z < -1 || projected.z > 1) {
item.element.style.display = "none";
item.line.style.display = "none";
continue;
}
const x = (projected.x * 0.5 + 0.5) * width;
const y = (-projected.y * 0.5 + 0.5) * height;
const labelX = THREE.MathUtils.clamp(
x + item.offset[0] * offsetScale,
75,
width - 75
);
const labelY = THREE.MathUtils.clamp(
y + item.offset[1] * offsetScale,
48,
height - 48
);
item.element.style.display = "block";
item.element.style.left = `${labelX}px`;
item.element.style.top = `${labelY}px`;
item.line.style.display = "block";
item.line.setAttribute("x1", x);
item.line.setAttribute("y1", y);
item.line.setAttribute("x2", labelX);
item.line.setAttribute("y2", labelY);
}
}
// -------------------------------------------------------------------------
// Direct camera controls: orbit, zoom, pan, touch pinch, keyboard
// -------------------------------------------------------------------------
const canvas = renderer.domElement;
const pointers = new Map();
let previousPinchDistance = 0;
let panButton = false;
function updateCamera() {
const fit = Math.max(1, 0.86 / camera.aspect);
const radius = view.radius * fit;
camera.position.set(
target.x + radius * Math.sin(view.phi) * Math.sin(view.theta),
target.y + radius * Math.cos(view.phi),
target.z + radius * Math.sin(view.phi) * Math.cos(view.theta)
);
camera.lookAt(target);
camera.updateMatrixWorld();
// The wall cut faces the camera as the model is orbited.
const cutNormal = target.clone().sub(camera.position).normalize();
wallCutPlane.setFromNormalAndCoplanarPoint(
cutNormal,
new THREE.Vector3(0, 0, 1)
);
}
function resetView() {
view.theta = 0.42;
view.phi = 1.18;
view.radius = 8.7;
target.set(0, -0.05, 0.9);
updateCamera();
}
canvas.addEventListener("contextmenu", (event) => event.preventDefault());
canvas.addEventListener("pointerdown", (event) => {
canvas.setPointerCapture(event.pointerId);
pointers.set(event.pointerId, { x: event.clientX, y: event.clientY });
panButton = event.button === 2 || event.shiftKey;
if (pointers.size === 2) {
const [a, b] = [...pointers.values()];
previousPinchDistance = Math.hypot(a.x - b.x, a.y - b.y);
}
});
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 >= 2) {
const [a, b] = [...pointers.values()];
const distance = Math.hypot(a.x - b.x, a.y - b.y);
if (previousPinchDistance > 0) {
view.radius *= previousPinchDistance / distance;
view.radius = THREE.MathUtils.clamp(view.radius, 5.0, 15.0);
}
previousPinchDistance = distance;
updateCamera();
return;
}
if (panButton || event.shiftKey) {
const right = new THREE.Vector3().setFromMatrixColumn(
camera.matrix,
0
);
const upVector = new THREE.Vector3().setFromMatrixColumn(
camera.matrix,
1
);
const scale = view.radius * 0.0011;
target.addScaledVector(right, -dx * scale);
target.addScaledVector(upVector, dy * scale);
} else {
view.theta -= dx * 0.006;
view.phi -= dy * 0.006;
}
view.phi = THREE.MathUtils.clamp(view.phi, 0.38, 2.25);
updateCamera();
});
function endPointer(event) {
pointers.delete(event.pointerId);
if (pointers.size < 2) previousPinchDistance = 0;
}
canvas.addEventListener("pointerup", endPointer);
canvas.addEventListener("pointercancel", endPointer);
canvas.addEventListener(
"wheel",
(event) => {
event.preventDefault();
view.radius *= Math.exp(event.deltaY * 0.001);
view.radius = THREE.MathUtils.clamp(view.radius, 5.0, 15.0);
updateCamera();
},
{ passive: false }
);
window.addEventListener("keydown", (event) => {
if (event.key.toLowerCase() === "r") {
resetView();
}
if (event.key === "ArrowLeft") view.theta -= 0.08;
if (event.key === "ArrowRight") view.theta += 0.08;
if (event.key === "ArrowUp") view.phi -= 0.06;
if (event.key === "ArrowDown") view.phi += 0.06;
if (event.key === "+" || event.key === "=") {
view.radius = Math.max(5, view.radius - 0.4);
}
if (event.key === "-" || event.key === "_") {
view.radius = Math.min(15, view.radius + 0.4);
}
view.phi = THREE.MathUtils.clamp(view.phi, 0.38, 2.25);
updateCamera();
});
// -------------------------------------------------------------------------
// UI controls
// -------------------------------------------------------------------------
const flowButton = document.querySelector("#flow-button");
const cutawayButton = document.querySelector("#cutaway-button");
const resetButton = document.querySelector("#reset-button");
flowButton.addEventListener("click", () => {
flowing = !flowing;
flowButton.textContent = flowing ? "Pause flow" : "Resume flow";
});
function applyCutaway() {
for (const material of cutMaterials.walls) {
material.clippingPlanes = cutawayEnabled ? [wallCutPlane] : [];
material.needsUpdate = true;
}
for (const material of cutMaterials.kidney) {
material.clippingPlanes = cutawayEnabled ? [kidneyCutPlane] : [];
material.needsUpdate = true;
}
sectionFill.visible = cutawayEnabled;
medulla.visible = cutawayEnabled;
sectionOutline.visible = cutawayEnabled;
cutawayButton.textContent = `Cutaway: ${cutawayEnabled ? "on" : "off"}`;
}
cutawayButton.addEventListener("click", () => {
cutawayEnabled = !cutawayEnabled;
applyCutaway();
});
resetButton.addEventListener("click", resetView);
// -------------------------------------------------------------------------
// Resize and animation
// -------------------------------------------------------------------------
function resize() {
const width = window.innerWidth;
const height = window.innerHeight;
camera.aspect = width / height;
camera.updateProjectionMatrix();
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(width, height);
leaderSVG.setAttribute("viewBox", `0 0 ${width} ${height}`);
updateCamera();
}
window.addEventListener("resize", resize);
function updateFlows() {
for (const flow of flows) {
for (let i = 0; i < flow.count; i++) {
const u = (
elapsed * flow.speed +
flow.phase +
i / flow.count
) % 1;
const position = flow.curve.getPointAt(u);
const tangent = flow.curve.getTangentAt(u).normalize();
dummy.position.copy(position);
dummy.quaternion.setFromUnitVectors(up, tangent);
const pulse = 0.88 + Math.sin(elapsed * 4 + i) * 0.12;
const elongation = flow.kind === "blood" ? 1.55 : 1.0;
dummy.scale.set(pulse, pulse * elongation, pulse);
dummy.updateMatrix();
flow.mesh.setMatrixAt(i, dummy.matrix);
}
flow.mesh.instanceMatrix.needsUpdate = true;
}
}
resetView();
applyCutaway();
resize();
const clock = new THREE.Clock();
renderer.setAnimationLoop(() => {
const delta = Math.min(clock.getDelta(), 0.05);
if (flowing) {
elapsed += delta;
}
updateFlows();
const pulse = 1 + Math.sin(elapsed * 2.1) * 0.025;
capsuleHalo.scale.set(1.08 * pulse, 0.93 * pulse, 1);
updateLabels();
renderer.render(scene, camera);
});
</script>
</body>
</html>
```