Prompt specification
Build or customize the Three.js 3D scene "Osmotic volume comparison cells" 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: Osmotic volume comparison cells
Category: Biology & Nature · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/osmotic-volume-comparison-cells-x0862
Scene Description Prompt:
Build a self-contained HTML scene with Three.js from cdnjs featuring osmosis. Place simplified cells in three labeled external solutions; show illustrative swelling, steady size, or shrinking under stated membrane assumptions, without representing all real cells identically. Stylized solid forms.
Working Three.js Starter (osmotic-volume-comparison-cells-x0862):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Osmosis — Three.js Interactive Scene</title>
<style>
:root {
color-scheme: light;
font-family:
Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
--ink: #172235;
--muted: #617087;
--panel: rgba(255, 255, 255, 0.88);
--line: rgba(33, 55, 83, 0.12);
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #eaf0f5;
color: var(--ink);
}
body {
min-height: 520px;
}
#scene {
position: fixed;
inset: 0;
display: block;
width: 100%;
height: 100%;
touch-action: none;
cursor: grab;
}
#scene:active {
cursor: grabbing;
}
.hud {
position: fixed;
z-index: 5;
top: 26px;
left: 28px;
right: 28px;
display: flex;
align-items: flex-start;
justify-content: space-between;
gap: 24px;
pointer-events: none;
}
.brand {
max-width: 470px;
}
.eyebrow {
margin-bottom: 7px;
color: #697a91;
font-size: 11px;
font-weight: 800;
letter-spacing: 0.18em;
text-transform: uppercase;
}
h1 {
margin: 0;
font-size: clamp(34px, 4.4vw, 58px);
line-height: 0.98;
letter-spacing: -0.055em;
}
.subtitle {
max-width: 430px;
margin: 10px 0 0;
color: var(--muted);
font-size: 14px;
line-height: 1.5;
}
.hud-right {
display: grid;
justify-items: end;
gap: 9px;
padding-top: 3px;
}
.legend-card,
.gesture-note {
border: 1px solid rgba(33, 55, 83, 0.1);
border-radius: 999px;
background: rgba(255, 255, 255, 0.7);
box-shadow: 0 8px 30px rgba(38, 54, 76, 0.06);
color: #52627a;
font-size: 11px;
font-weight: 650;
backdrop-filter: blur(12px);
}
.legend-card {
display: flex;
align-items: center;
gap: 8px;
padding: 9px 13px;
}
.legend-dot {
width: 8px;
height: 8px;
border-radius: 50%;
background: #238ca8;
box-shadow: 0 0 0 4px rgba(35, 140, 168, 0.12);
}
.gesture-note {
padding: 8px 12px;
opacity: 0.82;
}
#labels {
position: fixed;
z-index: 3;
inset: 0;
pointer-events: none;
}
.station-label {
position: absolute;
width: 208px;
padding: 12px 14px 13px;
overflow: hidden;
border: 1px solid rgba(33, 55, 83, 0.11);
border-top: 4px solid var(--tone);
border-radius: 15px;
background: rgba(255, 255, 255, 0.88);
box-shadow:
0 15px 38px rgba(31, 49, 73, 0.11),
0 2px 7px rgba(31, 49, 73, 0.05);
text-align: center;
transform: translate(-50%, -50%);
backdrop-filter: blur(13px);
transition: opacity 120ms ease;
}
.station-label .kicker {
display: block;
margin-bottom: 3px;
color: #7a889a;
font-size: 9px;
font-weight: 800;
letter-spacing: 0.15em;
text-transform: uppercase;
}
.station-label h2 {
margin: 0;
font-size: 18px;
line-height: 1.1;
letter-spacing: -0.025em;
}
.concentration {
margin-top: 5px;
color: #596980;
font-size: 12px;
font-variant-numeric: tabular-nums;
}
.concentration strong {
color: var(--tone);
font-size: 16px;
}
.outcome {
display: inline-flex;
align-items: center;
gap: 6px;
margin-top: 8px;
padding: 5px 8px;
border-radius: 999px;
background: color-mix(in srgb, var(--tone) 10%, white);
color: #435169;
font-size: 10px;
font-weight: 750;
}
.outcome::before {
width: 6px;
height: 6px;
flex: 0 0 auto;
border-radius: 50%;
background: var(--tone);
content: "";
}
.model-card {
position: fixed;
z-index: 6;
bottom: 22px;
left: 50%;
width: calc(100% - 44px);
max-width: 1060px;
overflow: hidden;
border: 1px solid rgba(33, 55, 83, 0.12);
border-radius: 20px;
background: var(--panel);
box-shadow: 0 20px 55px rgba(31, 49, 73, 0.13);
transform: translateX(-50%);
backdrop-filter: blur(18px);
}
.model-top {
display: flex;
align-items: center;
justify-content: space-between;
gap: 18px;
padding: 14px 16px 12px;
}
.model-title {
min-width: 0;
}
.model-title strong {
display: block;
margin-bottom: 3px;
font-size: 13px;
}
.model-title span {
display: block;
color: var(--muted);
font-size: 11px;
line-height: 1.4;
}
.controls {
display: flex;
flex: 0 0 auto;
align-items: center;
gap: 8px;
}
button {
min-width: 78px;
padding: 9px 13px;
border: 1px solid rgba(33, 55, 83, 0.13);
border-radius: 10px;
background: #fff;
color: var(--ink);
font: inherit;
font-size: 11px;
font-weight: 800;
cursor: pointer;
transition:
transform 120ms ease,
background 120ms ease,
box-shadow 120ms ease;
}
button:hover {
background: #f3f7fb;
box-shadow: 0 5px 14px rgba(31, 49, 73, 0.08);
transform: translateY(-1px);
}
button:focus-visible {
outline: 3px solid rgba(35, 140, 168, 0.24);
outline-offset: 2px;
}
button.primary {
border-color: #1e5269;
background: #1e5269;
color: #fff;
}
button.primary:hover {
background: #173f51;
}
.progress-track {
position: absolute;
right: 0;
bottom: 0;
left: 0;
height: 3px;
background: rgba(33, 55, 83, 0.07);
}
#progress {
width: 0;
height: 100%;
background: linear-gradient(90deg, #7b5ac7, #238ca8, #e5a72d);
transition: width 80ms linear;
}
.assumptions {
display: grid;
grid-template-columns: repeat(3, 1fr);
border-top: 1px solid var(--line);
}
.assumption {
padding: 12px 16px 15px;
border-right: 1px solid var(--line);
}
.assumption:last-child {
border-right: 0;
}
.assumption b {
display: block;
margin-bottom: 4px;
color: #35465e;
font-size: 10px;
letter-spacing: 0.035em;
text-transform: uppercase;
}
.assumption p {
margin: 0;
color: var(--muted);
font-size: 11px;
line-height: 1.45;
}
@media (max-width: 760px) {
.hud {
top: 16px;
left: 16px;
right: 16px;
}
h1 {
font-size: 36px;
}
.subtitle {
max-width: 300px;
font-size: 12px;
}
.hud-right {
display: none;
}
.station-label {
width: 116px;
padding: 8px 7px 9px;
border-radius: 11px;
border-top-width: 3px;
}
.station-label .kicker {
font-size: 7px;
}
.station-label h2 {
font-size: 13px;
}
.concentration {
font-size: 9px;
}
.concentration strong {
font-size: 12px;
}
.outcome {
gap: 4px;
padding: 4px 6px;
font-size: 8px;
}
.model-card {
bottom: 10px;
width: calc(100% - 20px);
border-radius: 15px;
}
.model-top {
padding: 10px;
}
.model-title span {
max-width: 230px;
}
.controls {
gap: 5px;
}
button {
min-width: 61px;
padding: 8px 9px;
font-size: 10px;
}
.assumptions {
grid-template-columns: 1fr 1fr;
}
.assumption {
padding: 9px 10px 11px;
}
.assumption:nth-child(2) {
border-right: 0;
}
.assumption:nth-child(3) {
grid-column: 1 / -1;
border-top: 1px solid var(--line);
border-right: 0;
}
.assumption b {
font-size: 8px;
}
.assumption p {
font-size: 9px;
}
}
@media (max-height: 650px) and (min-width: 761px) {
.hud {
top: 16px;
}
.subtitle {
display: none;
}
.model-card {
bottom: 12px;
}
}
</style>
</head>
<body>
<canvas
id="scene"
role="img"
aria-label="Interactive three-dimensional demonstration of osmosis in hypertonic, isotonic, and hypotonic solutions."
></canvas>
<header class="hud">
<div class="brand">
<div class="eyebrow">Biology Lab · Water Transport</div>
<h1>Osmosis</h1>
<p class="subtitle">
Water crosses a selectively permeable membrane, moving toward the side
with the higher concentration of non-penetrating solute.
</p>
</div>
<div class="hud-right">
<div class="legend-card">
<span class="legend-dot"></span>
Arrows show net water movement
</div>
<div class="gesture-note">
Drag to orbit · Scroll to zoom · Double-click to reset
</div>
</div>
</header>
<div id="labels">
<section class="station-label" id="label-hyper" style="--tone: #7957c7">
<span class="kicker">External solution</span>
<h2>Hypertonic</h2>
<div class="concentration"><strong>0.60</strong> Osm</div>
<div class="outcome" id="status-hyper">Water moves out · shrinking</div>
</section>
<section class="station-label" id="label-iso" style="--tone: #238c83">
<span class="kicker">External solution</span>
<h2>Isotonic</h2>
<div class="concentration"><strong>0.30</strong> Osm</div>
<div class="outcome" id="status-iso">No net flow · steady</div>
</section>
<section class="station-label" id="label-hypo" style="--tone: #d99b24">
<span class="kicker">External solution</span>
<h2>Hypotonic</h2>
<div class="concentration"><strong>0.15</strong> Osm</div>
<div class="outcome" id="status-hypo">Water moves in · swelling</div>
</section>
</div>
<aside class="model-card" aria-label="Model assumptions and animation controls">
<div class="model-top">
<div class="model-title">
<strong>Illustrative membrane model</strong>
<span>
The membrane is freely permeable to water but impermeable to sucrose.
Cell interior: 0.30 Osm non-penetrating solute.
</span>
</div>
<div class="controls">
<button id="play" class="primary" type="button">Pause</button>
<button id="replay" type="button">Replay</button>
</div>
</div>
<div class="assumptions">
<div class="assumption">
<b>Direction of net flow</b>
<p>Water moves from lower osmolarity toward higher osmolarity.</p>
</div>
<div class="assumption">
<b>Simplified geometry</b>
<p>Solid spheres represent representative cells, not real cell anatomy.</p>
</div>
<div class="assumption">
<b>Scope and limitations</b>
<p>
Volume response only. Active transport, membrane regulation, pressure,
and differences among real cells are not represented.
</p>
</div>
</div>
<div class="progress-track" aria-hidden="true">
<div id="progress"></div>
</div>
</aside>
<noscript>This interactive scene requires JavaScript.</noscript>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// ---------------------------------------------------------------------
// Renderer, camera, lighting
// ---------------------------------------------------------------------
const canvas = document.querySelector("#scene");
const renderer = new THREE.WebGLRenderer({
canvas,
antialias: true,
alpha: false,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.08;
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xeaf0f5);
scene.fog = new THREE.Fog(0xeaf0f5, 38, 80);
const camera = new THREE.PerspectiveCamera(
42,
window.innerWidth / window.innerHeight,
0.1,
120
);
const hemisphere = new THREE.HemisphereLight(0xffffff, 0xa9b6c7, 2.0);
scene.add(hemisphere);
const keyLight = new THREE.DirectionalLight(0xffffff, 3.2);
keyLight.position.set(5, 10, 8);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(2048, 2048);
keyLight.shadow.camera.left = -11;
keyLight.shadow.camera.right = 11;
keyLight.shadow.camera.top = 10;
keyLight.shadow.camera.bottom = -10;
keyLight.shadow.camera.near = 1;
keyLight.shadow.camera.far = 35;
keyLight.shadow.bias = -0.0004;
scene.add(keyLight);
const fillLight = new THREE.DirectionalLight(0xcfe5ff, 1.25);
fillLight.position.set(-7, 5, -5);
scene.add(fillLight);
const groundMaterial = new THREE.MeshStandardMaterial({
color: 0xe2e9f0,
roughness: 0.95,
metalness: 0
});
const ground = new THREE.Mesh(
new THREE.PlaneGeometry(70, 70),
groundMaterial
);
ground.rotation.x = -Math.PI / 2;
ground.position.y = -0.13;
ground.receiveShadow = true;
scene.add(ground);
// ---------------------------------------------------------------------
// Materials
// ---------------------------------------------------------------------
const platformMaterial = new THREE.MeshStandardMaterial({
color: 0xf8fafc,
roughness: 0.82,
metalness: 0.02
});
const vesselMaterial = new THREE.MeshStandardMaterial({
color: 0xffffff,
transparent: true,
opacity: 0.17,
roughness: 0.18,
metalness: 0.02,
side: THREE.DoubleSide,
depthWrite: false
});
const rimMaterial = new THREE.MeshStandardMaterial({
color: 0xd9e3ed,
roughness: 0.45,
metalness: 0.08
});
const cellMaterial = new THREE.MeshStandardMaterial({
color: 0xff756c,
roughness: 0.56,
metalness: 0.02
});
const nucleusMaterial = new THREE.MeshStandardMaterial({
color: 0x643b62,
roughness: 0.72
});
const organelleMaterial = new THREE.MeshStandardMaterial({
color: 0xb94b61,
roughness: 0.8
});
const highlightMaterial = new THREE.MeshStandardMaterial({
color: 0xffb5a9,
roughness: 0.75
});
// ---------------------------------------------------------------------
// Stylized solid cells
// ---------------------------------------------------------------------
function createCell() {
const group = new THREE.Group();
// Slightly larger back-facing shell creates a clean membrane outline.
const membraneOutline = new THREE.Mesh(
new THREE.SphereGeometry(0.585, 32, 24),
new THREE.MeshBasicMaterial({
color: 0x8d3f53,
side: THREE.BackSide
})
);
membraneOutline.castShadow = true;
group.add(membraneOutline);
const body = new THREE.Mesh(
new THREE.SphereGeometry(0.56, 32, 24),
cellMaterial
);
body.castShadow = true;
body.receiveShadow = true;
group.add(body);
const nucleus = new THREE.Mesh(
new THREE.SphereGeometry(0.17, 24, 18),
nucleusMaterial
);
nucleus.position.set(0.07, 0.05, 0.49);
nucleus.castShadow = true;
group.add(nucleus);
const organelle1 = new THREE.Mesh(
new THREE.SphereGeometry(0.055, 14, 10),
organelleMaterial
);
organelle1.position.set(-0.22, -0.15, 0.49);
group.add(organelle1);
const organelle2 = new THREE.Mesh(
new THREE.SphereGeometry(0.043, 14, 10),
organelleMaterial
);
organelle2.position.set(0.25, 0.22, 0.43);
group.add(organelle2);
const highlight = new THREE.Mesh(
new THREE.SphereGeometry(0.065, 14, 10),
highlightMaterial
);
highlight.position.set(-0.21, 0.24, 0.43);
group.add(highlight);
return group;
}
// ---------------------------------------------------------------------
// Directional water arrows and moving water markers
// ---------------------------------------------------------------------
const upAxis = new THREE.Vector3(0, 1, 0);
function createArrow(origin, direction, length, color, opacity = 1) {
const arrow = new THREE.Group();
const material = new THREE.MeshBasicMaterial({
color,
transparent: true,
opacity,
depthTest: false,
depthWrite: false
});
const shaftLength = Math.max(0.08, length - 0.22);
const shaft = new THREE.Mesh(
new THREE.CylinderGeometry(0.028, 0.028, shaftLength, 12),
material
);
shaft.position.y = shaftLength / 2;
shaft.renderOrder = 20;
arrow.add(shaft);
const head = new THREE.Mesh(
new THREE.ConeGeometry(0.105, 0.22, 18),
material
);
head.position.y = length - 0.11;
head.renderOrder = 20;
arrow.add(head);
arrow.position.copy(origin);
arrow.quaternion.setFromUnitVectors(
upAxis,
direction.clone().normalize()
);
return arrow;
}
function addWaterPath(group, from, to, material) {
for (let i = 0; i < 3; i++) {
const marker = new THREE.Mesh(
new THREE.SphereGeometry(0.058, 12, 10),
material
);
marker.renderOrder = 22;
group.add(marker);
group.userData.waterPaths.push({
marker,
from: from.clone(),
to: to.clone(),
offset: i / 3
});
}
}
// ---------------------------------------------------------------------
// Solution stations
// ---------------------------------------------------------------------
const stations = [
{
id: "hyper",
x: -4.45,
accent: 0x7957c7,
targetScale: 0.76,
direction: "inward"
},
{
id: "iso",
x: 0,
accent: 0x238c83,
targetScale: 1.0,
direction: "balanced"
},
{
id: "hypo",
x: 4.45,
accent: 0xd99b24,
targetScale: 1.29,
direction: "outward"
}
];
function createStation(config) {
const group = new THREE.Group();
group.position.x = config.x;
group.userData.waterPaths = [];
group.userData.cells = [];
const accentColor = new THREE.Color(config.accent);
const fluidColor = accentColor.clone().lerp(
new THREE.Color(0xffffff),
0.7
);
const platform = new THREE.Mesh(
new THREE.CylinderGeometry(1.95, 2.05, 0.18, 64),
platformMaterial
);
platform.position.y = 0.01;
platform.receiveShadow = true;
platform.castShadow = true;
group.add(platform);
const accentRing = new THREE.Mesh(
new THREE.TorusGeometry(1.84, 0.035, 12, 64),
new THREE.MeshStandardMaterial({
color: config.accent,
roughness: 0.55,
metalness: 0.04
})
);
accentRing.rotation.x = Math.PI / 2;
accentRing.position.y = 0.115;
group.add(accentRing);
const vesselBottom = new THREE.Mesh(
new THREE.CylinderGeometry(1.73, 1.73, 0.12, 64),
rimMaterial
);
vesselBottom.position.y = 0.18;
vesselBottom.receiveShadow = true;
group.add(vesselBottom);
const fluidMaterial = new THREE.MeshStandardMaterial({
color: fluidColor,
transparent: true,
opacity: 0.29,
roughness: 0.4,
metalness: 0,
side: THREE.DoubleSide,
depthWrite: false
});
const fluid = new THREE.Mesh(
new THREE.CylinderGeometry(1.61, 1.61, 2.55, 64),
fluidMaterial
);
fluid.position.y = 1.49;
fluid.renderOrder = 1;
group.add(fluid);
const surface = new THREE.Mesh(
new THREE.CylinderGeometry(1.61, 1.61, 0.035, 64),
new THREE.MeshStandardMaterial({
color: fluidColor,
transparent: true,
opacity: 0.56,
roughness: 0.3,
depthWrite: false
})
);
surface.position.y = 2.765;
surface.renderOrder = 2;
group.add(surface);
const vesselWall = new THREE.Mesh(
new THREE.CylinderGeometry(1.73, 1.73, 3.18, 64, 1, true),
vesselMaterial
);
vesselWall.position.y = 1.79;
vesselWall.renderOrder = 4;
group.add(vesselWall);
const topRim = new THREE.Mesh(
new THREE.TorusGeometry(1.73, 0.045, 12, 64),
rimMaterial
);
topRim.rotation.x = Math.PI / 2;
topRim.position.y = 3.38;
topRim.castShadow = true;
group.add(topRim);
// Four representative cells at slightly different sizes.
const cellPositions = [
[-0.72, 1.18, -0.12, 0.84],
[0.66, 1.12, -0.28, 1.00],
[-0.35, 1.95, -0.43, 0.91],
[0.40, 1.84, 0.24, 0.78]
];
for (let i = 0; i < cellPositions.length; i++) {
const [x, y, z, baseScale] = cellPositions[i];
const cell = createCell();
cell.position.set(x, y, z);
cell.scale.setScalar(baseScale);
cell.userData.basePosition = cell.position.clone();
cell.userData.baseScale = baseScale;
cell.userData.phase = i * 1.7 + config.x;
group.add(cell);
group.userData.cells.push(cell);
}
const arrowColor = 0x238ca8;
const arrowY = 3.08;
const arrowZ = 0.72;
if (config.direction === "inward") {
group.add(
createArrow(
new THREE.Vector3(1.15, arrowY, arrowZ),
new THREE.Vector3(-1, 0, 0),
1.02,
arrowColor
)
);
group.add(
createArrow(
new THREE.Vector3(-1.15, arrowY, arrowZ),
new THREE.Vector3(1, 0, 0),
1.02,
arrowColor
)
);
} else if (config.direction === "outward") {
group.add(
createArrow(
new THREE.Vector3(-1.05, arrowY, arrowZ),
new THREE.Vector3(-1, 0, 0),
1.02,
arrowColor
)
);
group.add(
createArrow(
new THREE.Vector3(1.05, arrowY, arrowZ),
new THREE.Vector3(1, 0, 0),
1.02,
arrowColor
)
);
} else {
group.add(
createArrow(
new THREE.Vector3(-0.92, arrowY, arrowZ),
new THREE.Vector3(1, 0, 0),
0.55,
arrowColor,
0.46
)
);
group.add(
createArrow(
new THREE.Vector3(0.92, arrowY, arrowZ),
new THREE.Vector3(-1, 0, 0),
0.55,
arrowColor,
0.46
)
);
}
const waterMaterial = new THREE.MeshBasicMaterial({
color: 0x42b8d0,
transparent: true,
opacity: 0.82,
depthTest: false,
depthWrite: false
});
if (config.direction === "inward") {
addWaterPath(
group,
new THREE.Vector3(1.25, arrowY, arrowZ + 0.06),
new THREE.Vector3(0.16, arrowY, arrowZ + 0.06),
waterMaterial
);
addWaterPath(
group,
new THREE.Vector3(-1.25, arrowY, arrowZ + 0.06),
new THREE.Vector3(-0.16, arrowY, arrowZ + 0.06),
waterMaterial
);
} else if (config.direction === "outward") {
addWaterPath(
group,
new THREE.Vector3(-0.16, arrowY, arrowZ + 0.06),
new THREE.Vector3(-1.25, arrowY, arrowZ + 0.06),
waterMaterial
);
addWaterPath(
group,
new THREE.Vector3(0.16, arrowY, arrowZ + 0.06),
new THREE.Vector3(1.25, arrowY, arrowZ + 0.06),
waterMaterial
);
} else {
const balancedMaterial = waterMaterial.clone();
balancedMaterial.opacity = 0.36;
addWaterPath(
group,
new THREE.Vector3(-0.18, arrowY, arrowZ + 0.06),
new THREE.Vector3(0.18, arrowY, arrowZ + 0.06),
balancedMaterial
);
addWaterPath(
group,
new THREE.Vector3(0.18, arrowY, arrowZ + 0.06),
new THREE.Vector3(-0.18, arrowY, arrowZ + 0.06),
balancedMaterial
);
}
scene.add(group);
return group;
}
const stationGroups = stations.map(createStation);
// ---------------------------------------------------------------------
// Projected HTML labels
// ---------------------------------------------------------------------
const labelDefinitions = [
{
element: document.querySelector("#label-hyper"),
anchor: new THREE.Vector3(stations[0].x, 4.02, 0)
},
{
element: document.querySelector("#label-iso"),
anchor: new THREE.Vector3(stations[1].x, 4.02, 0)
},
{
element: document.querySelector("#label-hypo"),
anchor: new THREE.Vector3(stations[2].x, 4.02, 0)
}
];
function updateLabels() {
const width = window.innerWidth;
const height = window.innerHeight;
for (const label of labelDefinitions) {
const projected = label.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;
label.element.style.left = `${x}px`;
label.element.style.top = `${y}px`;
label.element.style.opacity = visible ? "1" : "0";
}
}
// ---------------------------------------------------------------------
// Custom camera controls — no examples/addons used
// ---------------------------------------------------------------------
const cameraTarget = new THREE.Vector3(0, 1.28, 0);
const spherical = new THREE.Spherical(15.2, 1.03, 0);
let fitRadius = 15.2;
let userMovedCamera = false;
let dragging = false;
let previousPointerX = 0;
let previousPointerY = 0;
function calculateFitRadius() {
const halfFovRadians = THREE.MathUtils.degToRad(camera.fov * 0.5);
const required = 7.05 / (Math.tan(halfFovRadians) * camera.aspect);
return Math.max(14.7, required);
}
function updateCamera() {
spherical.phi = THREE.MathUtils.clamp(
spherical.phi,
0.62,
1.43
);
spherical.radius = THREE.MathUtils.clamp(
spherical.radius,
9.5,
44
);
const sinPhi = Math.sin(spherical.phi);
camera.position.set(
cameraTarget.x + spherical.radius * sinPhi * Math.sin(spherical.theta),
cameraTarget.y + spherical.radius * Math.cos(spherical.phi),
cameraTarget.z + spherical.radius * sinPhi * Math.cos(spherical.theta)
);
camera.lookAt(cameraTarget);
}
canvas.addEventListener("pointerdown", (event) => {
dragging = true;
previousPointerX = event.clientX;
previousPointerY = event.clientY;
canvas.setPointerCapture(event.pointerId);
});
canvas.addEventListener("pointermove", (event) => {
if (!dragging) return;
const deltaX = event.clientX - previousPointerX;
const deltaY = event.clientY - previousPointerY;
previousPointerX = event.clientX;
previousPointerY = event.clientY;
spherical.theta -= deltaX * 0.006;
spherical.phi -= deltaY * 0.005;
userMovedCamera = true;
});
function endDrag(event) {
dragging = false;
if (canvas.hasPointerCapture(event.pointerId)) {
canvas.releasePointerCapture(event.pointerId);
}
}
canvas.addEventListener("pointerup", endDrag);
canvas.addEventListener("pointercancel", endDrag);
canvas.addEventListener(
"wheel",
(event) => {
event.preventDefault();
spherical.radius *= Math.exp(event.deltaY * 0.001);
spherical.radius = THREE.MathUtils.clamp(
spherical.radius,
fitRadius * 0.65,
44
);
userMovedCamera = true;
},
{ passive: false }
);
function resetCamera() {
spherical.theta = 0;
spherical.phi = 1.03;
spherical.radius = fitRadius;
userMovedCamera = false;
updateCamera();
}
canvas.addEventListener("dblclick", resetCamera);
function handleResize() {
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);
fitRadius = calculateFitRadius();
if (!userMovedCamera) {
spherical.radius = fitRadius;
} else {
spherical.radius = Math.max(spherical.radius, fitRadius * 0.85);
}
updateCamera();
}
window.addEventListener("resize", handleResize);
handleResize();
// ---------------------------------------------------------------------
// Animation
// ---------------------------------------------------------------------
const duration = 8.0;
const reducedMotion = window.matchMedia(
"(prefers-reduced-motion: reduce)"
).matches;
let elapsed = reducedMotion ? duration : 0;
let playing = !reducedMotion;
let finalStateApplied = reducedMotion;
const playButton = document.querySelector("#play");
const replayButton = document.querySelector("#replay");
const progress = document.querySelector("#progress");
const statusElements = {
hyper: document.querySelector("#status-hyper"),
iso: document.querySelector("#status-iso"),
hypo: document.querySelector("#status-hypo")
};
function updatePlayButton() {
playButton.textContent = playing ? "Pause" : "Play";
}
function applyFinalStatus() {
statusElements.hyper.textContent = "Water lost · cell shrank";
statusElements.iso.textContent = "Equal exchange · cell steady";
statusElements.hypo.textContent = "Water gained · cell swollen";
}
if (reducedMotion) {
applyFinalStatus();
updatePlayButton();
}
playButton.addEventListener("click", () => {
if (elapsed >= duration) {
elapsed = 0;
finalStateApplied = false;
statusElements.hyper.textContent = "Water moves out · shrinking";
statusElements.iso.textContent = "No net flow · steady";
statusElements.hypo.textContent = "Water moves in · swelling";
}
playing = !playing;
updatePlayButton();
});
replayButton.addEventListener("click", () => {
elapsed = 0;
playing = true;
finalStateApplied = false;
statusElements.hyper.textContent = "Water moves out · shrinking";
statusElements.iso.textContent = "No net flow · steady";
statusElements.hypo.textContent = "Water moves in · swelling";
updatePlayButton();
});
const clock = new THREE.Clock();
function smoothStep(value) {
return value * value * (3 - 2 * value);
}
function animate() {
requestAnimationFrame(animate);
const delta = Math.min(clock.getDelta(), 0.05);
if (playing) {
elapsed = Math.min(duration, elapsed + delta);
if (elapsed >= duration) {
playing = false;
updatePlayButton();
}
}
const normalizedTime = THREE.MathUtils.clamp(
elapsed / duration,
0,
1
);
const easedTime = smoothStep(normalizedTime);
for (let i = 0; i < stationGroups.length; i++) {
const group = stationGroups[i];
const station = stations[i];
for (const cell of group.userData.cells) {
const volumeFactor = THREE.MathUtils.lerp(
1,
station.targetScale,
easedTime
);
cell.scale.setScalar(
cell.userData.baseScale * volumeFactor
);
cell.position.y =
cell.userData.basePosition.y +
Math.sin(elapsed * 0.8 + cell.userData.phase) * 0.018;
}
for (const path of group.userData.waterPaths) {
const movement =
(elapsed * 0.38 + path.offset) % 1;
path.marker.position.lerpVectors(
path.from,
path.to,
movement
);
}
}
if (normalizedTime >= 1 && !finalStateApplied) {
finalStateApplied = true;
applyFinalStatus();
}
progress.style.width = `${normalizedTime * 100}%`;
updateCamera();
updateLabels();
renderer.render(scene, camera);
}
animate();
</script>
</body>
</html>
```