Prompt specification
Build or customize the Three.js 3D scene "Capillary tube width comparison" 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: Capillary tube width comparison
Category: Science & Physics · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/capillary-tube-width-comparison-x1445
Scene Description Prompt:
Use Three.js from cdnjs to place several transparent tubes of different radii in one reservoir; show ideal capillary heights under a stated wetting relation and use exaggerated dimensions for visibility. Deliver one self-contained HTML file with low-poly forms.
Working Three.js Starter (capillary-tube-width-comparison-x1445):
```html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="color-scheme" content="dark">
<title>Ideal Capillary Rise in a Reservoir</title>
<style>
:root {
--panel: rgba(10, 25, 42, 0.82);
--panel-border: rgba(145, 214, 255, 0.18);
--text: #edf8ff;
--muted: #9db7c9;
--cyan: #52d8ff;
--green: #7ef0c6;
--gold: #ffd27d;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background:
radial-gradient(circle at 50% 22%, #173851 0%, #0b1d30 43%, #050c16 100%);
color: var(--text);
font-family:
Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
#app,
#stage {
position: absolute;
inset: 0;
}
#stage canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
outline: none;
}
.panel {
position: absolute;
z-index: 10;
padding: 18px 19px;
border: 1px solid var(--panel-border);
border-radius: 16px;
background: var(--panel);
box-shadow:
0 18px 60px rgba(0, 0, 0, 0.28),
inset 0 1px 0 rgba(255, 255, 255, 0.04);
backdrop-filter: blur(14px);
-webkit-backdrop-filter: blur(14px);
}
.model-panel {
top: 22px;
left: 22px;
width: 342px;
}
.eyebrow {
margin: 0 0 7px;
color: var(--cyan);
font-size: 11px;
font-weight: 800;
letter-spacing: 0.19em;
text-transform: uppercase;
}
h1 {
margin: 0;
font-size: clamp(23px, 2.3vw, 34px);
font-weight: 760;
line-height: 1.04;
letter-spacing: -0.035em;
}
.intro {
margin: 11px 0 15px;
color: var(--muted);
font-size: 13px;
line-height: 1.5;
}
.formula {
display: flex;
align-items: center;
justify-content: center;
min-height: 67px;
margin: 13px 0;
border: 1px solid rgba(82, 216, 255, 0.2);
border-radius: 12px;
background:
linear-gradient(135deg, rgba(82, 216, 255, 0.1), rgba(82, 216, 255, 0.025));
color: #f5fcff;
font-family: Georgia, "Times New Roman", serif;
font-size: 23px;
text-align: center;
}
.formula sub,
.formula sup {
font-size: 0.56em;
}
.formula .equals {
margin: 0 11px;
color: var(--cyan);
font-family: inherit;
font-size: 20px;
}
.chip-row {
display: flex;
flex-wrap: wrap;
gap: 7px;
margin-bottom: 13px;
}
.chip {
padding: 6px 9px;
border: 1px solid rgba(157, 183, 201, 0.17);
border-radius: 999px;
background: rgba(255, 255, 255, 0.035);
color: #c7d9e6;
font-size: 11px;
}
.chip strong {
color: #fff;
}
.assumption {
margin: 0;
padding-top: 12px;
border-top: 1px solid rgba(255, 255, 255, 0.08);
color: var(--muted);
font-size: 11px;
line-height: 1.55;
}
.readout-panel {
top: 22px;
right: 22px;
width: 288px;
}
.panel-title {
margin: 0 0 12px;
font-size: 12px;
font-weight: 800;
letter-spacing: 0.12em;
text-transform: uppercase;
}
.readout-row {
display: grid;
grid-template-columns: 11px 1fr auto;
align-items: center;
gap: 9px;
padding: 10px 0;
border-top: 1px solid rgba(255, 255, 255, 0.075);
}
.readout-row:first-of-type {
border-top: 0;
}
.swatch {
width: 8px;
height: 8px;
border-radius: 50%;
box-shadow: 0 0 14px currentColor;
}
.readout-row span {
color: var(--muted);
font-size: 12px;
}
.readout-row strong {
font-size: 15px;
font-variant-numeric: tabular-nums;
}
.scale-note {
margin: 12px 0 0;
padding-top: 12px;
border-top: 1px solid rgba(255, 255, 255, 0.08);
color: var(--muted);
font-size: 11px;
line-height: 1.55;
}
.scale-note b {
color: #dceefa;
}
#labels {
position: absolute;
inset: 0;
z-index: 5;
overflow: hidden;
pointer-events: none;
}
.tag {
position: absolute;
min-width: 118px;
padding: 9px 11px;
transform: translate(-50%, -100%);
border: 1px solid rgba(142, 215, 255, 0.22);
border-radius: 10px;
background: rgba(7, 20, 34, 0.88);
box-shadow: 0 9px 24px rgba(0, 0, 0, 0.28);
color: var(--text);
text-align: left;
pointer-events: auto;
cursor: pointer;
transition:
opacity 160ms ease,
border-color 160ms ease,
transform 160ms ease;
}
.tag::after {
position: absolute;
bottom: -14px;
left: 50%;
width: 1px;
height: 14px;
background: linear-gradient(var(--tag-color), transparent);
content: "";
opacity: 0.7;
}
.tag:hover,
.tag.active {
border-color: var(--tag-color);
transform: translate(-50%, -100%) translateY(-2px);
}
.tag .name {
display: block;
margin-bottom: 3px;
color: var(--tag-color);
font-size: 9px;
font-weight: 850;
letter-spacing: 0.15em;
}
.tag strong {
display: block;
font-size: 17px;
font-variant-numeric: tabular-nums;
}
.tag small {
display: block;
margin-top: 2px;
color: var(--muted);
font-size: 10px;
}
.surface-label {
position: absolute;
transform: translate(-50%, -50%);
color: rgba(190, 232, 248, 0.78);
font-size: 9px;
font-weight: 750;
letter-spacing: 0.11em;
text-transform: uppercase;
white-space: nowrap;
pointer-events: none;
}
.labels-off .tag,
.labels-off .surface-label {
opacity: 0;
pointer-events: none;
}
.controls {
position: absolute;
z-index: 10;
bottom: 22px;
left: 50%;
display: flex;
align-items: center;
gap: 8px;
padding: 8px;
transform: translateX(-50%);
border: 1px solid var(--panel-border);
border-radius: 14px;
background: rgba(8, 21, 35, 0.88);
box-shadow: 0 14px 45px rgba(0, 0, 0, 0.34);
backdrop-filter: blur(12px);
-webkit-backdrop-filter: blur(12px);
}
button {
border: 0;
font: inherit;
}
.control-button {
padding: 9px 13px;
border: 1px solid rgba(144, 210, 247, 0.15);
border-radius: 9px;
background: rgba(255, 255, 255, 0.045);
color: #d9edf8;
font-size: 11px;
font-weight: 700;
white-space: nowrap;
cursor: pointer;
transition: background 150ms ease, color 150ms ease;
}
.control-button:hover {
background: rgba(82, 216, 255, 0.14);
color: #fff;
}
.control-button.primary {
background: rgba(82, 216, 255, 0.16);
color: #bdefff;
}
.hint {
padding: 0 8px;
color: #7793a7;
font-size: 10px;
white-space: nowrap;
}
#loading {
position: absolute;
inset: 0;
z-index: 30;
display: grid;
place-items: center;
background: #071321;
color: #8edfff;
font-size: 12px;
letter-spacing: 0.14em;
text-transform: uppercase;
transition: opacity 400ms ease;
}
#loading.ready {
opacity: 0;
pointer-events: none;
}
@media (max-width: 900px) {
.readout-panel {
display: none;
}
.model-panel {
width: min(330px, calc(100vw - 28px));
top: 14px;
left: 14px;
padding: 15px;
}
.intro {
margin-bottom: 10px;
}
.formula {
min-height: 58px;
margin: 10px 0;
font-size: 20px;
}
.controls {
bottom: 14px;
}
.hint {
display: none;
}
}
@media (max-width: 560px) {
.model-panel {
width: calc(100vw - 28px);
}
.chip-row,
.assumption {
display: none;
}
.formula {
margin-bottom: 0;
}
.tag {
min-width: 102px;
padding: 7px 8px;
}
.tag strong {
font-size: 14px;
}
.controls {
width: calc(100vw - 28px);
justify-content: center;
}
.control-button {
flex: 1;
padding-inline: 8px;
}
}
</style>
</head>
<body>
<div id="app">
<div id="stage" aria-label="Interactive three-dimensional capillary-rise demonstration"></div>
<div id="labels">
<div id="surface-label" class="surface-label">Reservoir free surface · h = 0</div>
</div>
<section class="panel model-panel">
<p class="eyebrow">Surface tension study</p>
<h1>Ideal capillary rise</h1>
<p class="intro">
Three open glass tubes stand in one large reservoir. A narrower tube
draws the liquid higher under the complete-wetting condition.
</p>
<div class="formula" aria-label="Jurin's law">
h =
<span>
2σ cos θ
<span class="equals">/</span>
ρgr
</span>
</div>
<div class="chip-row">
<span class="chip"><strong>θ = 0°</strong> · complete wetting</span>
<span class="chip"><strong>σ = 72.8</strong> mN/m</span>
<span class="chip"><strong>ρ = 998</strong> kg/m³</span>
<span class="chip"><strong>g = 9.81</strong> m/s²</span>
</div>
<p class="assumption">
Large-reservoir approximation, ideal circular tubes, negligible contact-angle
hysteresis, and <strong>cos θ = 1</strong>. The menisci are concave for
complete wetting.
</p>
</section>
<aside class="panel readout-panel">
<h2 class="panel-title">Predicted equilibrium</h2>
<div id="readout-rows"></div>
<p class="scale-note">
<b>Visibility scale:</b> tube radii are drawn at 520 scene units per metre
and capillary rises at 600 scene units per metre. Reservoir and tube
proportions are diagrammatic; all displayed heights retain their true
millimetre values.
</p>
</aside>
<nav class="controls" aria-label="Visualization controls">
<button id="replay" class="control-button primary" type="button">Replay rise</button>
<button id="reset" class="control-button" type="button">Reset view</button>
<button id="toggle-labels" class="control-button" type="button">Hide labels</button>
<span class="hint">Drag to orbit · Wheel to zoom · Click a tube</span>
</nav>
<div id="loading">Loading capillary model…</div>
</div>
<noscript>This demonstration requires JavaScript and WebGL.</noscript>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// ---------------------------------------------------------------------
// Physical model: ideal Jurin's law for a circular capillary.
// ---------------------------------------------------------------------
const GAMMA = 0.0728; // N/m, water at approximately 20 °C
const DENSITY = 998; // kg/m^3
const GRAVITY = 9.81; // m/s^2
const CONTACT_ANGLE = 0; // degrees; complete wetting
const RADIAL_SCALE = 520; // scene units per physical metre
const VERTICAL_SCALE = 600; // scene units per physical metre
const SURFACE_Y = 0;
const TUBE_BOTTOM = -1.05;
const TUBE_TOP = 10.6;
const RESERVOIR_WIDTH = 15.6;
const RESERVOIR_DEPTH = 5.6;
const tubes = [
{ name: "TUBE 01", radius: 0.001, x: -4.4, color: 0x52d8ff, offset: [45, -12] },
{ name: "TUBE 02", radius: 0.002, x: 0.0, color: 0x7ef0c6, offset: [ 0, -12] },
{ name: "TUBE 03", radius: 0.003, x: 4.4, color: 0xffd27d, offset: [55, -12] }
];
for (const tube of tubes) {
tube.physicalHeight =
(2 * GAMMA * Math.cos(THREE.MathUtils.degToRad(CONTACT_ANGLE))) /
(DENSITY * GRAVITY * tube.radius);
tube.displayRadius = tube.radius * RADIAL_SCALE;
tube.displayHeight = tube.physicalHeight * VERTICAL_SCALE;
}
// ---------------------------------------------------------------------
// Renderer, scene and camera.
// ---------------------------------------------------------------------
const stage = document.querySelector("#stage");
const loading = document.querySelector("#loading");
const scene = new THREE.Scene();
scene.fog = new THREE.Fog(0x071522, 19, 43);
const camera = new THREE.PerspectiveCamera(
42,
window.innerWidth / window.innerHeight,
0.1,
100
);
const renderer = new THREE.WebGLRenderer({
antialias: true,
alpha: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setClearColor(0x000000, 0);
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.18;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.domElement.tabIndex = 0;
stage.appendChild(renderer.domElement);
const cameraTarget = new THREE.Vector3(0, 3.4, 0);
const spherical = new THREE.Spherical(22, 1.34, 0.48);
function updateCamera() {
const offset = new THREE.Vector3().setFromSpherical(spherical);
camera.position.copy(cameraTarget).add(offset);
camera.lookAt(cameraTarget);
}
function resetView() {
const narrow = window.innerWidth / window.innerHeight < 0.85;
spherical.set(narrow ? 29 : 22, 1.34, 0.48);
updateCamera();
}
updateCamera();
const hemisphere = new THREE.HemisphereLight(0xc9efff, 0x102338, 2.2);
scene.add(hemisphere);
const keyLight = new THREE.DirectionalLight(0xffffff, 3.3);
keyLight.position.set(7, 13, 9);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(1024, 1024);
keyLight.shadow.camera.left = -12;
keyLight.shadow.camera.right = 12;
keyLight.shadow.camera.top = 15;
keyLight.shadow.camera.bottom = -8;
scene.add(keyLight);
const rimLight = new THREE.DirectionalLight(0x4ac9ff, 1.8);
rimLight.position.set(-10, 5, -8);
scene.add(rimLight);
const warmLight = new THREE.PointLight(0xffd18a, 16, 18, 2);
warmLight.position.set(7, 2, 4);
scene.add(warmLight);
// ---------------------------------------------------------------------
// Low-poly helpers.
// ---------------------------------------------------------------------
function addBoxEdges(mesh, color = 0x78cce8, opacity = 0.28) {
const edges = new THREE.EdgesGeometry(mesh.geometry);
const lines = new THREE.LineSegments(
edges,
new THREE.LineBasicMaterial({
color,
transparent: true,
opacity,
depthWrite: false
})
);
mesh.add(lines);
return lines;
}
function makeMeniscus(radius, depth, segments = 12, rings = 4) {
const positions = [0, -depth, 0];
const indices = [];
for (let ring = 1; ring <= rings; ring++) {
const rho = radius * ring / rings;
const y = -depth * (1 - Math.pow(rho / radius, 2));
for (let i = 0; i < segments; i++) {
const angle = i / segments * Math.PI * 2;
positions.push(
Math.cos(angle) * rho,
y,
Math.sin(angle) * rho
);
}
}
const at = (ring, segment) =>
1 + (ring - 1) * segments + (segment % segments);
for (let i = 0; i < segments; i++) {
indices.push(0, at(1, i + 1), at(1, i));
}
for (let ring = 1; ring < rings; ring++) {
for (let i = 0; i < segments; i++) {
const a = at(ring, i);
const b = at(ring, i + 1);
const c = at(ring + 1, i + 1);
const d = at(ring + 1, i);
indices.push(a, c, d, a, b, c);
}
}
const geometry = new THREE.BufferGeometry();
geometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(positions, 3)
);
geometry.setIndex(indices);
geometry.computeVertexNormals();
return geometry;
}
// ---------------------------------------------------------------------
// Reservoir: open low-poly tank, transparent walls and shallow water.
// ---------------------------------------------------------------------
const reservoir = new THREE.Group();
scene.add(reservoir);
const floorMaterial = new THREE.MeshStandardMaterial({
color: 0x142c42,
roughness: 0.82,
metalness: 0.08,
flatShading: true
});
const floor = new THREE.Mesh(
new THREE.BoxGeometry(RESERVOIR_WIDTH, 0.24, RESERVOIR_DEPTH),
floorMaterial
);
floor.position.y = -1.25;
floor.receiveShadow = true;
reservoir.add(floor);
addBoxEdges(floor, 0x73c9e8, 0.36);
const glassMaterial = new THREE.MeshPhysicalMaterial({
color: 0xbcecff,
transparent: true,
opacity: 0.10,
roughness: 0.16,
metalness: 0,
clearcoat: 1,
clearcoatRoughness: 0.12,
side: THREE.DoubleSide,
depthWrite: false
});
const wallHeight = 1.58;
const wallY = -0.76;
const walls = [
[0.11, wallHeight, RESERVOIR_DEPTH, 0, wallY, -RESERVOIR_DEPTH / 2],
[0.11, wallHeight, RESERVOIR_DEPTH, 0, wallY, RESERVOIR_DEPTH / 2],
[RESERVOIR_WIDTH, wallHeight, 0.11, -RESERVOIR_WIDTH / 2, wallY, 0],
[RESERVOIR_WIDTH, wallHeight, 0.11, RESERVOIR_WIDTH / 2, wallY, 0]
];
for (const [w, h, d, x, y, z] of walls) {
const wall = new THREE.Mesh(
new THREE.BoxGeometry(w, h, d),
glassMaterial
);
wall.position.set(x, y, z);
wall.renderOrder = 8;
reservoir.add(wall);
addBoxEdges(wall, 0x8edfff, 0.32);
}
const waterVolume = new THREE.Mesh(
new THREE.BoxGeometry(
RESERVOIR_WIDTH - 0.22,
1.02,
RESERVOIR_DEPTH - 0.22
),
new THREE.MeshPhysicalMaterial({
color: 0x1688b3,
transparent: true,
opacity: 0.15,
roughness: 0.2,
depthWrite: false,
side: THREE.DoubleSide
})
);
waterVolume.position.y = -0.52;
waterVolume.renderOrder = 1;
reservoir.add(waterVolume);
const waterSurface = new THREE.Mesh(
new THREE.PlaneGeometry(
RESERVOIR_WIDTH - 0.22,
RESERVOIR_DEPTH - 0.22,
8,
4
),
new THREE.MeshPhysicalMaterial({
color: 0x43c9e7,
transparent: true,
opacity: 0.33,
roughness: 0.24,
metalness: 0.02,
side: THREE.DoubleSide,
depthWrite: false,
flatShading: true
})
);
waterSurface.rotation.x = -Math.PI / 2;
waterSurface.position.y = 0.008;
waterSurface.renderOrder = 2;
reservoir.add(waterSurface);
const surfaceLine = new THREE.Line(
new THREE.BufferGeometry().setFromPoints([
new THREE.Vector3(-RESERVOIR_WIDTH / 2 + 0.12, 0.025, RESERVOIR_DEPTH / 2 - 0.08),
new THREE.Vector3( RESERVOIR_WIDTH / 2 - 0.12, 0.025, RESERVOIR_DEPTH / 2 - 0.08)
]),
new THREE.LineBasicMaterial({
color: 0x8ce9ff,
transparent: true,
opacity: 0.65
})
);
surfaceLine.renderOrder = 9;
reservoir.add(surfaceLine);
// ---------------------------------------------------------------------
// Capillary tubes, liquids, concave menisci, guides and labels.
// ---------------------------------------------------------------------
const tubeObjects = [];
const hitMeshes = [];
const labelContainer = document.querySelector("#labels");
const surfaceLabel = document.querySelector("#surface-label");
for (const data of tubes) {
const tubeGroup = new THREE.Group();
tubeGroup.position.x = data.x;
scene.add(tubeGroup);
const innerRadius = data.displayRadius;
const wallThickness = 0.055 + innerRadius * 0.035;
const outerRadius = innerRadius + wallThickness;
const tubeGlass = glassMaterial.clone();
tubeGlass.opacity = 0.18;
const wall = new THREE.Mesh(
new THREE.CylinderGeometry(
outerRadius,
outerRadius,
TUBE_TOP - TUBE_BOTTOM,
12,
1,
true
),
tubeGlass
);
wall.position.y = (TUBE_TOP + TUBE_BOTTOM) / 2;
wall.renderOrder = 7;
wall.userData.tubeIndex = tubeObjects.length;
tubeGroup.add(wall);
hitMeshes.push(wall);
const rimMaterial = new THREE.MeshStandardMaterial({
color: 0xbfefff,
emissive: data.color,
emissiveIntensity: 0.12,
roughness: 0.22,
metalness: 0.08,
flatShading: true
});
const topRim = new THREE.Mesh(
new THREE.TorusGeometry(
innerRadius + wallThickness / 2,
wallThickness / 2,
4,
16
),
rimMaterial
);
topRim.rotation.x = Math.PI / 2;
topRim.position.y = TUBE_TOP;
topRim.renderOrder = 9;
tubeGroup.add(topRim);
const bottomRim = new THREE.Mesh(
new THREE.TorusGeometry(
innerRadius + wallThickness / 2,
wallThickness / 2,
4,
16
),
rimMaterial
);
bottomRim.rotation.x = Math.PI / 2;
bottomRim.position.y = TUBE_BOTTOM;
bottomRim.renderOrder = 6;
tubeGroup.add(bottomRim);
const liquidRadius = innerRadius * 0.92;
const lowerLiquid = new THREE.Mesh(
new THREE.CylinderGeometry(liquidRadius, liquidRadius, 1, 12),
new THREE.MeshPhysicalMaterial({
color: 0x087ea4,
emissive: 0x063d55,
emissiveIntensity: 0.22,
transparent: true,
opacity: 0.28,
roughness: 0.24,
depthWrite: false,
flatShading: true
})
);
lowerLiquid.scale.y = SURFACE_Y - TUBE_BOTTOM;
lowerLiquid.position.y = (SURFACE_Y + TUBE_BOTTOM) / 2;
lowerLiquid.renderOrder = 3;
tubeGroup.add(lowerLiquid);
const upperLiquid = new THREE.Mesh(
new THREE.CylinderGeometry(liquidRadius, liquidRadius, 1, 12),
new THREE.MeshPhysicalMaterial({
color: data.color,
emissive: data.color,
emissiveIntensity: 0.24,
transparent: true,
opacity: 0.58,
roughness: 0.2,
depthWrite: false,
flatShading: true
})
);
upperLiquid.renderOrder = 4;
tubeGroup.add(upperLiquid);
const meniscusMaterial = new THREE.MeshStandardMaterial({
color: data.color,
emissive: data.color,
emissiveIntensity: 0.3,
roughness: 0.22,
metalness: 0.05,
side: THREE.DoubleSide,
flatShading: true
});
const meniscus = new THREE.Mesh(
makeMeniscus(liquidRadius, Math.min(0.25, Math.max(0.11, innerRadius * 0.22))),
meniscusMaterial
);
meniscus.renderOrder = 5;
tubeGroup.add(meniscus);
// A low-poly surface ripple around each immersed tube.
for (let ringIndex = 0; ringIndex < 2; ringIndex++) {
const ripple = new THREE.Mesh(
new THREE.RingGeometry(
outerRadius + 0.08 + ringIndex * 0.18,
outerRadius + 0.105 + ringIndex * 0.18,
20
),
new THREE.MeshBasicMaterial({
color: 0xa5efff,
transparent: true,
opacity: 0.22 - ringIndex * 0.08,
side: THREE.DoubleSide,
depthWrite: false
})
);
ripple.rotation.x = -Math.PI / 2;
ripple.position.y = 0.021 + ringIndex * 0.002;
ripple.renderOrder = 3;
tubeGroup.add(ripple);
}
// Dashed ideal-height guide just outside the tube.
const guideX = outerRadius + 0.34;
const guide = new THREE.Line(
new THREE.BufferGeometry().setFromPoints([
new THREE.Vector3(guideX, 0, 0),
new THREE.Vector3(guideX, data.displayHeight, 0)
]),
new THREE.LineDashedMaterial({
color: data.color,
dashSize: 0.15,
gapSize: 0.10,
transparent: true,
opacity: 0.55,
depthWrite: false
})
);
guide.computeLineDistances();
guide.renderOrder = 9;
tubeGroup.add(guide);
const ticks = new THREE.LineSegments(
new THREE.BufferGeometry().setFromPoints([
new THREE.Vector3(guideX - 0.17, 0, 0),
new THREE.Vector3(guideX + 0.17, 0, 0),
new THREE.Vector3(guideX - 0.17, data.displayHeight, 0),
new THREE.Vector3(guideX + 0.17, data.displayHeight, 0),
new THREE.Vector3(outerRadius, data.displayHeight, 0),
new THREE.Vector3(guideX, data.displayHeight, 0)
]),
new THREE.LineBasicMaterial({
color: data.color,
transparent: true,
opacity: 0.66,
depthWrite: false
})
);
ticks.renderOrder = 9;
tubeGroup.add(ticks);
const endpoint = new THREE.Mesh(
new THREE.SphereGeometry(0.065, 8, 5),
new THREE.MeshBasicMaterial({ color: data.color })
);
endpoint.position.set(guideX, data.displayHeight, 0);
endpoint.renderOrder = 10;
tubeGroup.add(endpoint);
const tag = document.createElement("button");
tag.type = "button";
tag.className = "tag";
tag.style.setProperty("--tag-color", `#${data.color.toString(16).padStart(6, "0")}`);
tag.innerHTML = `
<span class="name">${data.name}</span>
<strong>${(data.physicalHeight * 1000).toFixed(1)} mm</strong>
<small>r = ${(data.radius * 1000).toFixed(1)} mm · θ = 0°</small>
`;
tag.setAttribute(
"aria-label",
`${data.name}, radius ${data.radius * 1000} millimetres, ideal rise ${data.physicalHeight * 1000} millimetres`
);
labelContainer.appendChild(tag);
tag.addEventListener("click", (event) => {
event.stopPropagation();
selectTube(tubeObjects.length);
});
tubeObjects.push({
data,
group: tubeGroup,
wall,
tubeGlass,
rimMaterial,
upperLiquid,
meniscus,
guide,
ticks,
endpoint,
tag,
anchor: new THREE.Vector3(
data.x + guideX,
data.displayHeight + 0.2,
0
)
});
}
function selectTube(index) {
tubeObjects.forEach((tube, i) => {
const selected = i === index;
tube.rimMaterial.emissiveIntensity = selected ? 0.7 : 0.12;
tube.tubeGlass.opacity = selected ? 0.30 : 0.18;
tube.tag.classList.toggle("active", selected);
});
}
selectTube(1);
// Numeric readout.
document.querySelector("#readout-rows").innerHTML = tubes.map(data => `
<div class="readout-row">
<i class="swatch" style="
color:#${data.color.toString(16).padStart(6, "0")};
background:#${data.color.toString(16).padStart(6, "0")}
"></i>
<span>r = ${(data.radius * 1000).toFixed(1)} mm</span>
<strong>${(data.physicalHeight * 1000).toFixed(1)} mm</strong>
</div>
`).join("");
// ---------------------------------------------------------------------
// Rise animation.
// ---------------------------------------------------------------------
const reduceMotion = window.matchMedia(
"(prefers-reduced-motion: reduce)"
).matches;
let progress = reduceMotion ? 1 : 0;
let animationStart = performance.now();
let animationActive = !reduceMotion;
const animationDuration = 2200;
function replayRise() {
progress = 0;
animationStart = performance.now();
animationActive = true;
}
function updateTubeVisuals() {
for (const tube of tubeObjects) {
const risenHeight = tube.data.displayHeight * progress;
const visibleHeight = Math.max(0.001, risenHeight);
tube.upperLiquid.scale.y = visibleHeight;
tube.upperLiquid.position.y = SURFACE_Y + visibleHeight / 2;
tube.meniscus.position.y = SURFACE_Y + risenHeight;
const guideOpacity = 0.22 + 0.43 * progress;
tube.guide.material.opacity = guideOpacity;
tube.ticks.material.opacity = guideOpacity;
tube.endpoint.scale.setScalar(0.75 + 0.25 * progress);
}
}
document.querySelector("#replay").addEventListener("click", replayRise);
// ---------------------------------------------------------------------
// Camera controls implemented directly without Three.js addons.
// ---------------------------------------------------------------------
let dragging = false;
let pointerMoved = false;
let lastX = 0;
let lastY = 0;
const raycaster = new THREE.Raycaster();
const pointer = new THREE.Vector2();
renderer.domElement.addEventListener("pointerdown", event => {
dragging = true;
pointerMoved = false;
lastX = event.clientX;
lastY = event.clientY;
renderer.domElement.setPointerCapture(event.pointerId);
});
renderer.domElement.addEventListener("pointermove", event => {
if (!dragging) return;
const dx = event.clientX - lastX;
const dy = event.clientY - lastY;
if (Math.abs(dx) + Math.abs(dy) > 2) {
pointerMoved = true;
}
lastX = event.clientX;
lastY = event.clientY;
spherical.theta -= dx * 0.006;
spherical.phi = THREE.MathUtils.clamp(
spherical.phi - dy * 0.005,
0.48,
1.48
);
updateCamera();
});
renderer.domElement.addEventListener("pointerup", event => {
dragging = false;
if (!pointerMoved) {
pointer.x = event.clientX / window.innerWidth * 2 - 1;
pointer.y = -(event.clientY / window.innerHeight) * 2 + 1;
raycaster.setFromCamera(pointer, camera);
const hits = raycaster.intersectObjects(hitMeshes, false);
if (hits.length) {
selectTube(hits[0].object.userData.tubeIndex);
}
}
if (renderer.domElement.hasPointerCapture(event.pointerId)) {
renderer.domElement.releasePointerCapture(event.pointerId);
}
});
renderer.domElement.addEventListener("pointercancel", () => {
dragging = false;
});
renderer.domElement.addEventListener(
"wheel",
event => {
event.preventDefault();
spherical.radius = THREE.MathUtils.clamp(
spherical.radius * Math.exp(event.deltaY * 0.001),
12,
36
);
updateCamera();
},
{ passive: false }
);
renderer.domElement.addEventListener("dblclick", resetView);
renderer.domElement.addEventListener("contextmenu", event => event.preventDefault());
document.querySelector("#reset").addEventListener("click", resetView);
const toggleLabelsButton = document.querySelector("#toggle-labels");
toggleLabelsButton.addEventListener("click", () => {
const hidden = document.body.classList.toggle("labels-off");
toggleLabelsButton.textContent = hidden ? "Show labels" : "Hide labels";
});
// ---------------------------------------------------------------------
// Project DOM annotations into the Three.js scene.
// ---------------------------------------------------------------------
function projectLabel(element, worldPosition, offsetX = 0, offsetY = 0) {
const projected = worldPosition.clone().project(camera);
const visible = projected.z > -1 && projected.z < 1;
element.style.display = visible ? "" : "none";
if (!visible) return;
const x = (projected.x * 0.5 + 0.5) * window.innerWidth + offsetX;
const y = (-projected.y * 0.5 + 0.5) * window.innerHeight + offsetY;
element.style.left = `${x}px`;
element.style.top = `${y}px`;
}
function updateAnnotations() {
for (const tube of tubeObjects) {
const anchor = tube.anchor.clone();
anchor.y = SURFACE_Y + tube.data.displayHeight * progress + 0.12;
projectLabel(
tube.tag,
anchor,
tube.data.offset[0],
tube.data.offset[1]
);
}
projectLabel(
surfaceLabel,
new THREE.Vector3(-6.45, 0.06, 2.4),
-18,
12
);
}
// ---------------------------------------------------------------------
// Resize and render loop.
// ---------------------------------------------------------------------
function onResize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
}
window.addEventListener("resize", onResize);
let firstFrame = true;
function animate(now) {
requestAnimationFrame(animate);
if (animationActive) {
const linear = THREE.MathUtils.clamp(
(now - animationStart) / animationDuration,
0,
1
);
const eased = 1 - Math.pow(1 - linear, 3);
progress = eased;
if (linear >= 1) {
animationActive = false;
progress = 1;
}
}
updateTubeVisuals();
renderer.render(scene, camera);
updateAnnotations();
if (firstFrame) {
firstFrame = false;
loading.classList.add("ready");
}
}
requestAnimationFrame(animate);
</script>
</body>
</html>
```