Prompt specification
Build or customize the Three.js 3D scene "Paper wick water-front 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: Paper wick water-front analogy
Category: Science & Physics · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/paper-wick-water-front-analogy-x1446
Scene Description Prompt:
Make one HTML file using Three.js from cdnjs for this stylized scene: Animate a colored front rising through a porous strip beside an enlarged capillary inset; label the porous-flow behavior illustrative rather than identical to one smooth tube.
Working Three.js Starter (paper-wick-water-front-analogy-x1446):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Rising Front Through a Porous Strip</title>
<style>
:root {
--ink: #20323a;
--muted: #63737a;
--paper: #f4f0e8;
--liquid: #2368f3;
--cyan: #22c7d9;
--coral: #ee765d;
--border: rgba(32, 50, 58, 0.15);
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background:
radial-gradient(circle at 50% 25%, #fffdf8 0%, var(--paper) 68%);
color: var(--ink);
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;
cursor: grab;
}
canvas.dragging {
cursor: grabbing;
}
.header {
position: fixed;
top: 24px;
left: 28px;
z-index: 5;
max-width: 520px;
pointer-events: none;
}
.eyebrow {
display: flex;
align-items: center;
gap: 9px;
margin-bottom: 8px;
color: var(--coral);
font-size: 11px;
font-weight: 800;
letter-spacing: 0.15em;
text-transform: uppercase;
}
.eyebrow::before {
width: 26px;
height: 3px;
border-radius: 4px;
background: var(--coral);
content: "";
}
h1 {
margin: 0;
font-size: clamp(25px, 3vw, 42px);
font-weight: 720;
line-height: 1.04;
letter-spacing: -0.045em;
}
.subtitle {
margin: 10px 0 0;
color: var(--muted);
font-size: 14px;
line-height: 1.5;
}
.controls {
position: fixed;
top: 24px;
right: 26px;
z-index: 6;
display: flex;
align-items: center;
gap: 8px;
}
button {
border: 1px solid var(--border);
border-radius: 999px;
padding: 9px 13px;
background: rgba(255, 255, 255, 0.78);
color: var(--ink);
font: inherit;
font-size: 12px;
font-weight: 650;
cursor: pointer;
backdrop-filter: blur(10px);
box-shadow: 0 5px 18px rgba(32, 50, 58, 0.06);
}
button:hover {
border-color: rgba(32, 50, 58, 0.3);
transform: translateY(-1px);
}
.camera-hint {
position: fixed;
right: 27px;
bottom: 23px;
z-index: 5;
color: rgba(32, 50, 58, 0.56);
font-size: 11px;
pointer-events: none;
}
.scene-label {
position: fixed;
z-index: 4;
width: 230px;
padding: 11px 13px;
border: 1px solid rgba(32, 50, 58, 0.12);
border-radius: 12px;
background: rgba(255, 255, 255, 0.78);
box-shadow: 0 8px 25px rgba(32, 50, 58, 0.07);
pointer-events: none;
transform: translate(-50%, -100%);
backdrop-filter: blur(9px);
}
.scene-label strong {
display: block;
margin-bottom: 4px;
color: var(--ink);
font-size: 11px;
font-weight: 850;
letter-spacing: 0.11em;
text-transform: uppercase;
}
.scene-label span {
display: block;
color: var(--muted);
font-size: 11px;
line-height: 1.4;
}
.scene-label::after {
position: absolute;
bottom: -7px;
left: 50%;
width: 12px;
height: 12px;
border-right: 1px solid rgba(32, 50, 58, 0.14);
border-bottom: 1px solid rgba(32, 50, 58, 0.14);
background: rgba(255, 255, 255, 0.78);
content: "";
transform: translateX(-50%) rotate(45deg);
}
#front-label {
position: fixed;
z-index: 4;
padding: 5px 8px;
border-radius: 7px;
background: var(--coral);
color: white;
font-size: 9px;
font-weight: 850;
letter-spacing: 0.08em;
pointer-events: none;
transform: translate(12px, -50%);
white-space: nowrap;
box-shadow: 0 5px 15px rgba(238, 118, 93, 0.2);
}
.legend {
position: fixed;
bottom: 25px;
left: 28px;
z-index: 5;
display: flex;
gap: 15px;
color: var(--muted);
font-size: 11px;
pointer-events: none;
}
.legend-item {
display: flex;
align-items: center;
gap: 7px;
}
.swatch {
width: 10px;
height: 10px;
border-radius: 3px;
}
.swatch.liquid {
background: linear-gradient(135deg, var(--liquid), var(--cyan));
}
.swatch.grain {
background: #b8aa96;
box-shadow: inset -2px -2px 0 rgba(73, 63, 51, 0.12);
}
.caveat {
position: fixed;
bottom: 22px;
left: 50%;
z-index: 6;
display: flex;
align-items: center;
gap: 10px;
width: min(610px, calc(100% - 40px));
padding: 11px 15px;
border: 1px solid rgba(238, 118, 93, 0.28);
border-radius: 12px;
background: rgba(255, 248, 244, 0.9);
color: #75483d;
font-size: 12px;
line-height: 1.35;
transform: translateX(-50%);
pointer-events: none;
backdrop-filter: blur(12px);
box-shadow: 0 10px 35px rgba(73, 44, 34, 0.08);
}
.caveat-icon {
display: grid;
flex: 0 0 22px;
width: 22px;
height: 22px;
place-items: center;
border-radius: 50%;
background: var(--coral);
color: white;
font-size: 12px;
font-weight: 850;
}
@media (max-width: 800px) {
.header {
top: 17px;
left: 18px;
max-width: calc(100% - 145px);
}
h1 {
font-size: 25px;
}
.subtitle {
display: none;
}
.controls {
top: 17px;
right: 17px;
}
button {
padding: 8px 11px;
}
.scene-label {
width: 165px;
padding: 8px 9px;
}
.scene-label span {
font-size: 9px;
}
.legend,
.camera-hint {
display: none;
}
.caveat {
bottom: 14px;
font-size: 11px;
}
}
</style>
</head>
<body>
<canvas id="scene" aria-label="Stylized 3D diagram of a colored liquid front rising through a porous strip"></canvas>
<header class="header">
<div class="eyebrow">Pore-scale schematic</div>
<h1>Rising through a porous strip</h1>
<p class="subtitle">
An irregular wetting front advances through a granular medium,
contrasted with a smooth enlarged capillary reference.
</p>
</header>
<div class="controls">
<button id="replay" type="button">Replay flow</button>
<button id="reset-view" type="button" title="Reset camera">Reset view</button>
</div>
<div id="porous-label" class="scene-label">
<strong>Porous strip</strong>
<span>Liquid occupies disconnected pores and advances at uneven local rates.</span>
</div>
<div id="capillary-label" class="scene-label">
<strong>Enlarged capillary inset</strong>
<span>A smooth single tube is shown as a visual reference only.</span>
</div>
<div id="front-label">IRREGULAR WETTING FRONT</div>
<div class="legend">
<div class="legend-item">
<span class="swatch liquid"></span>
<span>Colored liquid</span>
</div>
<div class="legend-item">
<span class="swatch grain"></span>
<span>Porous solid matrix</span>
</div>
</div>
<div class="caveat">
<span class="caveat-icon">i</span>
<span>
<strong>Illustrative porous-flow behavior</strong> — the rising front is
stylized and is not identical to flow in one smooth capillary tube.
</span>
</div>
<div class="camera-hint">Drag to orbit · scroll to zoom · double-click to reset</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const canvas = document.querySelector("#scene");
const renderer = new THREE.WebGLRenderer({
canvas,
antialias: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.08;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xf4f0e8);
scene.fog = new THREE.Fog(0xf4f0e8, 17, 29);
const camera = new THREE.OrthographicCamera(-6, 6, 4, -4, 0.1, 100);
const cameraTarget = new THREE.Vector3(0.25, 0, 0);
const cameraState = {
theta: 0.12,
phi: 1.37,
zoom: 1,
targetTheta: 0.12,
targetPhi: 1.37,
targetZoom: 1
};
// Lighting
scene.add(new THREE.HemisphereLight(0xffffff, 0xb4a792, 2.15));
const keyLight = new THREE.DirectionalLight(0xfff4df, 3.1);
keyLight.position.set(4.5, 8, 6);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(2048, 2048);
keyLight.shadow.camera.left = -8;
keyLight.shadow.camera.right = 8;
keyLight.shadow.camera.top = 8;
keyLight.shadow.camera.bottom = -8;
keyLight.shadow.bias = -0.0006;
scene.add(keyLight);
const fillLight = new THREE.DirectionalLight(0xccecff, 1.1);
fillLight.position.set(-5, 2, 4);
scene.add(fillLight);
// Ground
const ground = new THREE.Mesh(
new THREE.PlaneGeometry(30, 30),
new THREE.MeshStandardMaterial({
color: 0xeee8de,
roughness: 0.98,
metalness: 0
})
);
ground.rotation.x = -Math.PI / 2;
ground.position.y = -2.56;
ground.receiveShadow = true;
scene.add(ground);
const mainGroup = new THREE.Group();
mainGroup.position.x = -2.15;
scene.add(mainGroup);
const capillaryGroup = new THREE.Group();
capillaryGroup.position.x = 2.3;
scene.add(capillaryGroup);
const dummy = new THREE.Object3D();
const color = new THREE.Color();
const clamp = THREE.MathUtils.clamp;
const lerp = THREE.MathUtils.lerp;
function smoothstep(min, max, value) {
const x = clamp((value - min) / (max - min), 0, 1);
return x * x * (3 - 2 * x);
}
function randomBetween(min, max) {
return min + Math.random() * (max - min);
}
function roundedRectangle(width, height, radius) {
const shape = new THREE.Shape();
const x = -width / 2;
const y = -height / 2;
shape.moveTo(x + radius, y);
shape.lineTo(x + width - radius, y);
shape.quadraticCurveTo(x + width, y, x + width, y + radius);
shape.lineTo(x + width, y + height - radius);
shape.quadraticCurveTo(
x + width,
y + height,
x + width - radius,
y + height
);
shape.lineTo(x + radius, y + height);
shape.quadraticCurveTo(x, y + height, x, y + height - radius);
shape.lineTo(x, y + radius);
shape.quadraticCurveTo(x, y, x + radius, y);
return shape;
}
// Main porous strip frame
const slabGeometry = new THREE.BoxGeometry(1.66, 4.42, 0.8);
const slabMaterial = new THREE.MeshPhysicalMaterial({
color: 0xd9ece9,
transparent: true,
opacity: 0.13,
roughness: 0.16,
metalness: 0,
clearcoat: 0.8,
depthWrite: false,
side: THREE.DoubleSide
});
const slab = new THREE.Mesh(slabGeometry, slabMaterial);
slab.renderOrder = 5;
mainGroup.add(slab);
const slabEdges = new THREE.LineSegments(
new THREE.EdgesGeometry(slabGeometry),
new THREE.LineBasicMaterial({
color: 0x50676d,
transparent: true,
opacity: 0.48
})
);
mainGroup.add(slabEdges);
const backPanel = new THREE.Mesh(
new THREE.BoxGeometry(1.48, 4.18, 0.035),
new THREE.MeshStandardMaterial({
color: 0xffffff,
transparent: true,
opacity: 0.28,
roughness: 1,
depthWrite: false
})
);
backPanel.position.z = -0.34;
mainGroup.add(backPanel);
const stripBase = new THREE.Mesh(
new THREE.BoxGeometry(2.05, 0.19, 1.18),
new THREE.MeshStandardMaterial({
color: 0xc7bba9,
roughness: 0.82
})
);
stripBase.position.y = -2.35;
stripBase.castShadow = true;
stripBase.receiveShadow = true;
mainGroup.add(stripBase);
// Define pore locations first so grains can be placed around them.
const porePoints = [];
for (let i = 0; i < 96; i++) {
const x = randomBetween(-0.59, 0.59);
const y = randomBetween(-1.98, 1.98);
const z = randomBetween(-0.24, 0.25);
const normalizedHeight = (y + 1.98) / 3.96;
const threshold = clamp(
normalizedHeight +
randomBetween(-0.105, 0.105) +
0.035 * Math.sin(x * 8.0 + z * 4.0),
0.035,
0.985
);
porePoints.push({
position: new THREE.Vector3(x, y, z),
threshold,
size: randomBetween(0.72, 1.38),
phase: randomBetween(0, Math.PI * 2)
});
}
// Irregular solid grains surrounding the pore spaces
const grainCount = 126;
const grainGeometry = new THREE.IcosahedronGeometry(0.15, 1);
const grainMaterial = new THREE.MeshStandardMaterial({
color: 0xffffff,
vertexColors: true,
roughness: 0.88,
metalness: 0.02,
flatShading: true
});
const grains = new THREE.InstancedMesh(
grainGeometry,
grainMaterial,
grainCount
);
grains.castShadow = true;
grains.receiveShadow = true;
grains.frustumCulled = false;
const grainColors = [
new THREE.Color(0xb9aa95),
new THREE.Color(0xcbbfae),
new THREE.Color(0x9fa9a5),
new THREE.Color(0xa9967d),
new THREE.Color(0xd1c5b2)
];
let grainIndex = 0;
let attempts = 0;
while (grainIndex < grainCount && attempts < 5000) {
attempts++;
const x = randomBetween(-0.68, 0.68);
const y = randomBetween(-2.06, 2.06);
const z = randomBetween(-0.29, 0.3);
let tooClose = false;
for (const pore of porePoints) {
const dx = pore.position.x - x;
const dy = pore.position.y - y;
const dz = pore.position.z - z;
if (dx * dx + dy * dy + dz * dz < 0.052) {
tooClose = true;
break;
}
}
if (tooClose) continue;
dummy.position.set(x, y, z);
dummy.rotation.set(
randomBetween(0, Math.PI),
randomBetween(0, Math.PI),
randomBetween(0, Math.PI)
);
dummy.scale.set(
randomBetween(0.75, 1.3),
randomBetween(0.68, 1.25),
randomBetween(0.72, 1.2)
);
dummy.updateMatrix();
grains.setMatrixAt(grainIndex, dummy.matrix);
grains.setColorAt(
grainIndex,
grainColors[Math.floor(Math.random() * grainColors.length)]
);
grainIndex++;
}
mainGroup.add(grains);
// Colored liquid occupying the pore network
const liquidGeometry = new THREE.SphereGeometry(0.115, 14, 10);
const liquidMaterial = new THREE.MeshPhysicalMaterial({
color: 0xffffff,
vertexColors: true,
transparent: true,
opacity: 0.92,
roughness: 0.16,
metalness: 0.02,
clearcoat: 0.9,
clearcoatRoughness: 0.14,
depthWrite: false
});
const liquid = new THREE.InstancedMesh(
liquidGeometry,
liquidMaterial,
porePoints.length
);
liquid.instanceMatrix.setUsage(THREE.DynamicDrawUsage);
liquid.frustumCulled = false;
liquid.renderOrder = 3;
const deepBlue = new THREE.Color(0x2058dc);
const brightCyan = new THREE.Color(0x27d1dc);
porePoints.forEach((pore, index) => {
const normalizedHeight =
(pore.position.y + 1.98) / 3.96;
color.lerpColors(
deepBlue,
brightCyan,
clamp(normalizedHeight * 0.75 + randomBetween(-0.12, 0.12), 0, 1)
);
liquid.setColorAt(index, color);
});
liquid.instanceColor.needsUpdate = true;
mainGroup.add(liquid);
// A faint halo emphasizes the advancing edge.
const haloMaterial = new THREE.MeshBasicMaterial({
color: 0x28c9dd,
transparent: true,
opacity: 0.12,
depthWrite: false,
blending: THREE.AdditiveBlending
});
const halos = new THREE.InstancedMesh(
new THREE.SphereGeometry(0.14, 12, 8),
haloMaterial,
porePoints.length
);
halos.instanceMatrix.setUsage(THREE.DynamicDrawUsage);
halos.frustumCulled = false;
halos.renderOrder = 2;
mainGroup.add(halos);
// Approximate front marker
const frontMarker = new THREE.Group();
frontMarker.position.z = 0.38;
const markerLine = new THREE.Line(
new THREE.BufferGeometry().setFromPoints([
new THREE.Vector3(0, 0, 0),
new THREE.Vector3(0.28, 0, 0)
]),
new THREE.LineBasicMaterial({ color: 0xee765d })
);
frontMarker.add(markerLine);
const markerTip = new THREE.Mesh(
new THREE.ConeGeometry(0.07, 0.16, 14),
new THREE.MeshBasicMaterial({ color: 0xee765d })
);
markerTip.rotation.z = -Math.PI / 2;
markerTip.position.x = 0.34;
frontMarker.add(markerTip);
mainGroup.add(frontMarker);
// Capillary inset panel
const panelBorder = new THREE.Mesh(
new THREE.ShapeGeometry(roundedRectangle(2.95, 4.72, 0.24)),
new THREE.MeshStandardMaterial({
color: 0xe5d9c9,
roughness: 0.9
})
);
panelBorder.position.z = -0.61;
capillaryGroup.add(panelBorder);
const panel = new THREE.Mesh(
new THREE.ShapeGeometry(roundedRectangle(2.84, 4.61, 0.22)),
new THREE.MeshStandardMaterial({
color: 0xfffdf9,
roughness: 0.95,
transparent: true,
opacity: 0.92
})
);
panel.position.z = -0.58;
panel.receiveShadow = true;
capillaryGroup.add(panel);
const panelBase = new THREE.Mesh(
new THREE.BoxGeometry(3.06, 0.17, 1.12),
new THREE.MeshStandardMaterial({
color: 0xc7bba9,
roughness: 0.84
})
);
panelBase.position.set(0, -2.42, -0.1);
panelBase.castShadow = true;
panelBase.receiveShadow = true;
capillaryGroup.add(panelBase);
// Glass capillary
const tubeHeight = 3.75;
const tubeBottom = -1.67;
const tubeTop = tubeBottom + tubeHeight;
const glassMaterial = new THREE.MeshPhysicalMaterial({
color: 0xbce5eb,
transparent: true,
opacity: 0.2,
roughness: 0.08,
metalness: 0,
clearcoat: 1,
depthWrite: false,
side: THREE.DoubleSide
});
const glassTube = new THREE.Mesh(
new THREE.CylinderGeometry(0.56, 0.56, tubeHeight, 64, 1, true),
glassMaterial
);
glassTube.position.z = 0;
glassTube.renderOrder = 5;
capillaryGroup.add(glassTube);
const ringMaterial = new THREE.MeshStandardMaterial({
color: 0x77959b,
transparent: true,
opacity: 0.48,
roughness: 0.35,
metalness: 0.15
});
for (const y of [tubeBottom, tubeTop]) {
const ring = new THREE.Mesh(
new THREE.TorusGeometry(0.56, 0.035, 10, 64),
ringMaterial
);
ring.rotation.x = Math.PI / 2;
ring.position.y = y;
capillaryGroup.add(ring);
}
// Smooth liquid column in the capillary
const capillaryLiquidMaterial = new THREE.MeshPhysicalMaterial({
color: 0x2368f3,
transparent: true,
opacity: 0.88,
roughness: 0.18,
metalness: 0,
clearcoat: 0.9,
depthWrite: false
});
const capillaryLiquid = new THREE.Mesh(
new THREE.CylinderGeometry(0.43, 0.43, 1, 64),
capillaryLiquidMaterial
);
capillaryLiquid.renderOrder = 3;
capillaryGroup.add(capillaryLiquid);
const meniscusMaterial = new THREE.MeshPhysicalMaterial({
color: 0x27c9dc,
transparent: true,
opacity: 0.94,
roughness: 0.12,
clearcoat: 1,
depthWrite: false
});
const meniscus = new THREE.Mesh(
new THREE.SphereGeometry(0.43, 48, 20),
meniscusMaterial
);
meniscus.scale.set(1, 0.13, 1);
meniscus.renderOrder = 4;
capillaryGroup.add(meniscus);
// Glass highlights
const highlightMaterial = new THREE.MeshBasicMaterial({
color: 0xffffff,
transparent: true,
opacity: 0.36,
depthWrite: false
});
const highlight = new THREE.Mesh(
new THREE.PlaneGeometry(0.035, 2.85),
highlightMaterial
);
highlight.position.set(-0.27, 0.1, 0.54);
capillaryGroup.add(highlight);
// Dashed comparison connector
const connectorGeometry = new THREE.BufferGeometry();
connectorGeometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(new Float32Array(12), 3)
);
const connector = new THREE.Line(
connectorGeometry,
new THREE.LineDashedMaterial({
color: 0xee765d,
transparent: true,
opacity: 0.58,
dashSize: 0.08,
gapSize: 0.06,
depthWrite: false
})
);
connector.renderOrder = 1;
scene.add(connector);
// Labels are HTML so they remain crisp.
const porousLabel = document.querySelector("#porous-label");
const capillaryLabel = document.querySelector("#capillary-label");
const frontLabel = document.querySelector("#front-label");
const projected = new THREE.Vector3();
function placeLabel(element, worldPosition, offsetX = 0, offsetY = 0) {
projected.copy(worldPosition).project(camera);
const x = (projected.x * 0.5 + 0.5) * window.innerWidth;
const y = (-projected.y * 0.5 + 0.5) * window.innerHeight;
element.style.left = `${x + offsetX}px`;
element.style.top = `${y + offsetY}px`;
element.style.visibility =
projected.z > -1 && projected.z < 1 ? "visible" : "hidden";
}
function updateCamera() {
const easing = 0.1;
cameraState.theta = lerp(
cameraState.theta,
cameraState.targetTheta,
easing
);
cameraState.phi = lerp(
cameraState.phi,
cameraState.targetPhi,
easing
);
cameraState.zoom = lerp(
cameraState.zoom,
cameraState.targetZoom,
easing
);
const radius = 13;
const sinPhi = Math.sin(cameraState.phi);
camera.position.set(
cameraTarget.x + radius * sinPhi * Math.sin(cameraState.theta),
cameraTarget.y + radius * Math.cos(cameraState.phi),
cameraTarget.z + radius * sinPhi * Math.cos(cameraState.theta)
);
camera.lookAt(cameraTarget);
camera.zoom = cameraState.zoom;
camera.updateProjectionMatrix();
}
function resize() {
const width = window.innerWidth;
const height = window.innerHeight;
const aspect = width / height;
let viewWidth = 7.25 * aspect;
let viewHeight = 7.25;
if (viewWidth < 8.55) {
viewWidth = 8.55;
viewHeight = viewWidth / aspect;
}
camera.left = -viewWidth / 2;
camera.right = viewWidth / 2;
camera.top = viewHeight / 2;
camera.bottom = -viewHeight / 2;
camera.updateProjectionMatrix();
renderer.setSize(width, height, false);
}
window.addEventListener("resize", resize);
resize();
// Direct camera controls, implemented without addons.
let dragging = false;
let pointerX = 0;
let pointerY = 0;
canvas.addEventListener("pointerdown", (event) => {
dragging = true;
pointerX = event.clientX;
pointerY = event.clientY;
canvas.setPointerCapture(event.pointerId);
canvas.classList.add("dragging");
});
canvas.addEventListener("pointermove", (event) => {
if (!dragging) return;
const deltaX = event.clientX - pointerX;
const deltaY = event.clientY - pointerY;
cameraState.targetTheta -= deltaX * 0.006;
cameraState.targetPhi = clamp(
cameraState.targetPhi + deltaY * 0.005,
0.82,
1.53
);
pointerX = event.clientX;
pointerY = event.clientY;
});
function stopDragging(event) {
dragging = false;
canvas.classList.remove("dragging");
if (event.pointerId !== undefined && canvas.hasPointerCapture(event.pointerId)) {
canvas.releasePointerCapture(event.pointerId);
}
}
canvas.addEventListener("pointerup", stopDragging);
canvas.addEventListener("pointercancel", stopDragging);
canvas.addEventListener(
"wheel",
(event) => {
event.preventDefault();
cameraState.targetZoom = clamp(
cameraState.targetZoom * Math.exp(-event.deltaY * 0.001),
0.72,
1.55
);
},
{ passive: false }
);
function resetCamera() {
cameraState.targetTheta = 0.12;
cameraState.targetPhi = 1.37;
cameraState.targetZoom = 1;
}
canvas.addEventListener("dblclick", resetCamera);
document.querySelector("#reset-view").addEventListener("click", resetCamera);
let flowStart = performance.now();
document.querySelector("#replay").addEventListener("click", () => {
flowStart = performance.now();
});
const duration = 11.5;
function flowState(now) {
const elapsed = (now - flowStart) / 1000;
const phase = (elapsed % duration) / duration;
let rise;
let fade;
if (phase < 0.07) {
rise = 0;
fade = smoothstep(0, 0.07, phase);
} else if (phase < 0.84) {
rise = smoothstep(0.07, 0.84, phase);
fade = 1;
} else if (phase < 0.91) {
rise = 1;
fade = 1;
} else {
rise = 1;
fade = 1 - smoothstep(0.91, 1, phase);
}
return { rise, fade, phase };
}
const markerWorld = new THREE.Vector3();
function animate(now) {
updateCamera();
const { rise, fade } = flowState(now);
// Reveal pore liquid according to locally varied thresholds.
porePoints.forEach((pore, index) => {
const activation = smoothstep(
pore.threshold - 0.045,
pore.threshold + 0.055,
rise
);
const edgeBand = Math.max(
0,
1 - Math.abs(rise - pore.threshold) / 0.12
);
const pulse =
1 +
Math.sin(now * 0.004 + pore.phase) *
0.045 *
(0.25 + edgeBand);
const scale = activation * fade * pore.size * pulse;
dummy.position.copy(pore.position);
dummy.rotation.set(
pore.phase * 0.35,
pore.phase * 0.6,
pore.phase * 0.2
);
dummy.scale.set(
scale * 1.12,
scale * 0.9,
scale
);
dummy.updateMatrix();
liquid.setMatrixAt(index, dummy.matrix);
const haloScale = edgeBand * fade * pore.size * 1.5;
dummy.scale.setScalar(haloScale);
dummy.updateMatrix();
halos.setMatrixAt(index, dummy.matrix);
});
liquid.instanceMatrix.needsUpdate = true;
halos.instanceMatrix.needsUpdate = true;
// Approximate average height of the irregular front.
const frontY = lerp(-1.93, 1.93, rise);
frontMarker.position.y = frontY;
markerWorld.set(
mainGroup.position.x + 1.0,
frontY,
0.38
);
// Smooth capillary level for visual comparison.
const capillaryHeight = 3.24 * rise;
capillaryLiquid.scale.y = Math.max(capillaryHeight, 0.001);
capillaryLiquid.position.y = tubeBottom + capillaryHeight / 2;
meniscus.position.y =
tubeBottom +
capillaryHeight +
Math.sin(now * 0.0022) * 0.018 * fade;
capillaryLiquid.material.opacity = 0.88 * fade;
meniscus.material.opacity = 0.94 * fade;
meniscus.visible = fade > 0.01;
// Update the dashed connector between the porous front and inset.
const positions = connector.geometry.attributes.position.array;
positions[0] = markerWorld.x;
positions[1] = frontY;
positions[2] = 0.3;
positions[3] = -0.72;
positions[4] = frontY;
positions[5] = -0.08;
positions[6] = 0.48;
positions[7] = 0.52;
positions[8] = -0.22;
positions[9] = capillaryGroup.position.x - 1.02;
positions[10] = 0.52;
positions[11] = -0.3;
connector.geometry.attributes.position.needsUpdate = true;
connector.computeLineDistances();
placeLabel(
porousLabel,
new THREE.Vector3(mainGroup.position.x, 2.5, 0),
0,
-7
);
placeLabel(
capillaryLabel,
new THREE.Vector3(capillaryGroup.position.x, 2.5, -0.2),
0,
-7
);
placeLabel(frontLabel, markerWorld, 7, 0);
renderer.render(scene, camera);
}
renderer.setAnimationLoop(animate);
</script>
</body>
</html>
```