Prompt specification
Build or customize the Three.js 3D scene "Water-balance particle explorer" 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: Water-balance particle explorer
Category: Biology & Nature · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/water-balance-particle-explorer-x0864
Scene Description Prompt:
Build a self-contained HTML scene with Three.js from cdnjs featuring osmosis. Provide internal and external impermeant-solute controls in a bounded toy cell model; animate net water movement qualitatively and explain that equilibrium depends on pressure as well as concentration. Low-poly.
Working Three.js Starter (water-balance-particle-explorer-x0864):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Osmosis — Concentration and Pressure</title>
<style>
:root {
color-scheme: dark;
--cyan: #68ddff;
--mint: #70efad;
--amber: #ffb068;
--pink: #ff78bd;
--panel: rgba(8, 23, 32, 0.78);
--border: rgba(145, 225, 255, 0.22);
--muted: #9bb7c3;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #06131b;
font-family:
Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
color: #e8f8ff;
}
#scene {
position: fixed;
inset: 0;
}
canvas {
display: block;
touch-action: none;
}
.glass {
background: var(--panel);
border: 1px solid var(--border);
box-shadow: 0 16px 40px rgba(0, 0, 0, 0.28);
backdrop-filter: blur(14px);
-webkit-backdrop-filter: blur(14px);
}
#title {
position: fixed;
top: 18px;
left: 18px;
width: min(370px, calc(100vw - 36px));
padding: 17px 19px;
border-radius: 18px;
pointer-events: none;
}
.eyebrow {
margin-bottom: 6px;
color: var(--cyan);
font-size: 0.7rem;
font-weight: 800;
letter-spacing: 0.18em;
text-transform: uppercase;
}
h1 {
margin: 0;
font-size: clamp(1.45rem, 3vw, 2.15rem);
line-height: 1.04;
letter-spacing: -0.04em;
}
#title p {
margin: 9px 0 0;
color: var(--muted);
font-size: 0.86rem;
line-height: 1.45;
}
#readout {
position: fixed;
top: 18px;
right: 18px;
width: 292px;
padding: 16px;
border-radius: 18px;
pointer-events: none;
}
#status {
display: inline-flex;
align-items: center;
gap: 8px;
padding: 7px 10px;
border: 1px solid currentColor;
border-radius: 999px;
color: var(--cyan);
font-size: 0.72rem;
font-weight: 900;
letter-spacing: 0.09em;
}
#status::before {
width: 7px;
height: 7px;
border-radius: 50%;
background: currentColor;
box-shadow: 0 0 14px currentColor;
content: "";
}
#mechanism {
min-height: 42px;
margin: 12px 0;
color: #d8edf5;
font-size: 0.84rem;
line-height: 1.4;
}
.readout-row {
display: flex;
justify-content: space-between;
gap: 16px;
padding: 6px 0;
border-top: 1px solid rgba(255, 255, 255, 0.07);
color: var(--muted);
font-size: 0.74rem;
}
.readout-row strong {
color: #f0fbff;
font-variant-numeric: tabular-nums;
text-align: right;
}
#formula {
margin-top: 10px;
padding: 10px;
border-radius: 10px;
background: rgba(0, 0, 0, 0.22);
color: var(--cyan);
font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
font-size: 0.76rem;
text-align: center;
}
#controls {
position: fixed;
bottom: 18px;
left: 18px;
width: 315px;
max-height: calc(100vh - 36px);
padding: 16px;
overflow: auto;
border-radius: 18px;
}
.section-title {
display: flex;
align-items: center;
justify-content: space-between;
margin-bottom: 12px;
font-size: 0.75rem;
font-weight: 800;
letter-spacing: 0.09em;
text-transform: uppercase;
}
.section-title span {
color: var(--muted);
font-size: 0.64rem;
font-weight: 600;
letter-spacing: 0;
text-transform: none;
}
.control {
margin: 13px 0;
}
.control label {
display: flex;
align-items: baseline;
justify-content: space-between;
gap: 10px;
margin-bottom: 7px;
color: #d8edf5;
font-size: 0.78rem;
}
.control output {
min-width: 64px;
color: var(--cyan);
font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
font-size: 0.75rem;
text-align: right;
}
input[type="range"] {
width: 100%;
height: 5px;
margin: 0;
border-radius: 10px;
accent-color: var(--cyan);
cursor: pointer;
}
.divider {
height: 1px;
margin: 15px 0;
background: rgba(255, 255, 255, 0.09);
}
.presets {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 7px;
}
button {
padding: 9px 8px;
border: 1px solid rgba(104, 221, 255, 0.2);
border-radius: 10px;
background: rgba(104, 221, 255, 0.07);
color: #dff8ff;
font: inherit;
font-size: 0.7rem;
line-height: 1.2;
cursor: pointer;
transition:
background 150ms ease,
transform 150ms ease;
}
button:hover {
background: rgba(104, 221, 255, 0.15);
transform: translateY(-1px);
}
button:active {
transform: translateY(0);
}
#explain {
position: fixed;
right: 18px;
bottom: 18px;
width: 350px;
padding: 16px 17px;
border-radius: 18px;
pointer-events: none;
}
#explain h2 {
margin: 0 0 7px;
color: var(--cyan);
font-size: 0.86rem;
letter-spacing: 0.04em;
}
#explain p {
margin: 0;
color: #c4dbe4;
font-size: 0.78rem;
line-height: 1.48;
}
#explain .equation {
margin: 9px 0;
padding: 8px 10px;
border-left: 2px solid var(--cyan);
background: rgba(104, 221, 255, 0.07);
color: #e8fbff;
font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
}
#explain small {
display: block;
margin-top: 9px;
color: #819da9;
font-size: 0.68rem;
line-height: 1.4;
}
.legend {
display: flex;
flex-wrap: wrap;
gap: 11px;
margin-top: 12px;
color: var(--muted);
font-size: 0.67rem;
}
.legend i {
display: inline-block;
width: 8px;
height: 8px;
margin-right: 5px;
border-radius: 50%;
}
.world-label {
position: fixed;
z-index: 2;
padding: 5px 8px;
border: 1px solid rgba(144, 226, 255, 0.2);
border-radius: 8px;
background: rgba(5, 17, 24, 0.66);
color: #c9efff;
font-size: 0.62rem;
font-weight: 800;
letter-spacing: 0.1em;
pointer-events: none;
text-transform: uppercase;
transform: translate(-50%, -50%);
white-space: nowrap;
}
#cameraHint {
position: fixed;
left: 50%;
bottom: 10px;
color: rgba(191, 222, 232, 0.58);
font-size: 0.65rem;
pointer-events: none;
transform: translateX(-50%);
white-space: nowrap;
}
@media (max-width: 820px) {
#title {
width: calc(100vw - 36px);
}
#readout {
top: 145px;
width: 245px;
}
#explain {
width: 290px;
}
#controls {
width: 285px;
}
}
@media (max-width: 620px) {
#title {
padding: 13px 15px;
}
#title p {
display: none;
}
#readout {
top: 106px;
right: 10px;
width: 210px;
padding: 12px;
}
#mechanism {
min-height: 0;
margin: 8px 0;
}
.readout-row {
font-size: 0.67rem;
}
#controls {
bottom: 10px;
left: 10px;
width: calc(100vw - 20px);
max-height: 43vh;
padding: 12px;
}
#explain {
display: none;
}
#cameraHint {
display: none;
}
}
</style>
</head>
<body>
<div id="scene"></div>
<section id="title" class="glass">
<div class="eyebrow">Toy cell / osmosis</div>
<h1>Water follows a combined chemical drive.</h1>
<p>
Change impermeant solute and pressure. The membrane passes water but
blocks the solute.
</p>
</section>
<section id="readout" class="glass" aria-live="polite">
<div id="status">WATER ENTERS</div>
<div id="mechanism"></div>
<div class="readout-row">
<span>Solute contribution</span>
<strong id="osmoticValue"></strong>
</div>
<div class="readout-row">
<span>Pressure contribution</span>
<strong id="pressureValue"></strong>
</div>
<div class="readout-row">
<span>Illustrative flux</span>
<strong id="fluxValue"></strong>
</div>
<div id="formula"></div>
</section>
<section id="controls" class="glass">
<div class="section-title">
Impermeant solute
<span>particles cannot cross</span>
</div>
<div class="control">
<label for="insideSolute">
Internal concentration
<output id="insideSoluteValue"></output>
</label>
<input
id="insideSolute"
type="range"
min="0"
max="300"
step="5"
value="220"
>
</div>
<div class="control">
<label for="outsideSolute">
External concentration
<output id="outsideSoluteValue"></output>
</label>
<input
id="outsideSolute"
type="range"
min="0"
max="300"
step="5"
value="80"
>
</div>
<div class="divider"></div>
<div class="section-title">
Hydrostatic pressure
<span>pressure offsets solute drive</span>
</div>
<div class="control">
<label for="insidePressure">
Internal pressure
<output id="insidePressureValue"></output>
</label>
<input
id="insidePressure"
type="range"
min="0"
max="6"
step="0.1"
value="1"
>
</div>
<div class="control">
<label for="outsidePressure">
External pressure
<output id="outsidePressureValue"></output>
</label>
<input
id="outsidePressure"
type="range"
min="0"
max="6"
step="0.1"
value="1"
>
</div>
<div class="divider"></div>
<div class="section-title">Try a balance</div>
<div class="presets">
<button data-preset="inward">Solute pulls inward</button>
<button data-preset="balanced">Pressure balances solute</button>
<button data-preset="reversed">Pressure reverses flow</button>
<button data-preset="equal">Equal conditions</button>
</div>
<div class="legend">
<span><i style="background:#69ddff"></i>water</span>
<span><i style="background:#ffb15e"></i>internal solute</span>
<span><i style="background:#ff76bd"></i>external solute</span>
</div>
</section>
<section id="explain" class="glass">
<h2>Why concentration alone is not enough</h2>
<p>
Water responds to water chemical potential. More impermeant solute lowers
the water chemical potential on that side, while higher pressure on a
side pushes water away.
</p>
<div class="equation">
At equilibrium:<br>
RT(C<sub>in</sub> − C<sub>out</sub>) ≈ P<sub>in</sub> − P<sub>out</sub>
</div>
<p>
Thus concentrations need not match at equilibrium: a pressure difference
can exactly offset the osmotic difference.
</p>
<small>
A dilute-solution approximation is used at 310 K. Tracer motion is
qualitative; the sliders manually set the compartment conditions.
</small>
</section>
<div id="insideLabel" class="world-label">Cell interior</div>
<div id="outsideLabel" class="world-label">External bath</div>
<div id="cameraHint">Drag to orbit · scroll to zoom · press R 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 $ = (selector) => document.querySelector(selector);
const inputs = {
insideSolute: $("#insideSolute"),
outsideSolute: $("#outsideSolute"),
insidePressure: $("#insidePressure"),
outsidePressure: $("#outsidePressure")
};
const outputs = {
insideSolute: $("#insideSoluteValue"),
outsideSolute: $("#outsideSoluteValue"),
insidePressure: $("#insidePressureValue"),
outsidePressure: $("#outsidePressureValue")
};
// 310 K, R = 0.082057 L atm / (mol K).
const RT = 25.43767;
const CENTER = new THREE.Vector3(0, 0.35, 0);
const COLORS = {
equilibrium: new THREE.Color(0x70efad),
inward: new THREE.Color(0x68ddff),
outward: new THREE.Color(0xffb068)
};
function updateInputLabels() {
outputs.insideSolute.value = `${inputs.insideSolute.value} mM`;
outputs.outsideSolute.value = `${inputs.outsideSolute.value} mM`;
outputs.insidePressure.value = `${Number(inputs.insidePressure.value).toFixed(1)} atm`;
outputs.outsidePressure.value = `${Number(inputs.outsidePressure.value).toFixed(1)} atm`;
}
Object.values(inputs).forEach((input) => {
input.addEventListener("input", updateInputLabels);
});
updateInputLabels();
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x06131b);
scene.fog = new THREE.Fog(0x06131b, 15, 30);
const camera = new THREE.PerspectiveCamera(
45,
window.innerWidth / window.innerHeight,
0.1,
100
);
const renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
$("#scene").appendChild(renderer.domElement);
const hemisphere = new THREE.HemisphereLight(0xb7efff, 0x18313b, 2.15);
scene.add(hemisphere);
const keyLight = new THREE.DirectionalLight(0xffffff, 3.2);
keyLight.position.set(6, 9, 5);
keyLight.castShadow = true;
scene.add(keyLight);
const rimLight = new THREE.PointLight(0x3fdcff, 24, 18, 2);
rimLight.position.set(-4, 2, -4);
scene.add(rimLight);
const warmLight = new THREE.PointLight(0xff8d54, 15, 14, 2);
warmLight.position.set(4, -1, 3);
scene.add(warmLight);
// Bounded external bath / platform.
const bath = new THREE.Mesh(
new THREE.CylinderGeometry(7.5, 7.8, 0.5, 24),
new THREE.MeshStandardMaterial({
color: 0x102d39,
roughness: 0.92,
metalness: 0.04,
flatShading: true
})
);
bath.position.y = -2.5;
bath.receiveShadow = true;
scene.add(bath);
const bathRim = new THREE.Mesh(
new THREE.TorusGeometry(7.48, 0.055, 4, 48),
new THREE.MeshBasicMaterial({
color: 0x3c91a7,
transparent: true,
opacity: 0.52
})
);
bathRim.rotation.x = Math.PI / 2;
bathRim.position.y = -2.24;
scene.add(bathRim);
const grid = new THREE.GridHelper(14, 14, 0x2c7788, 0x194553);
grid.position.y = -2.235;
grid.material.transparent = true;
grid.material.opacity = 0.24;
scene.add(grid);
// Low-poly cell membrane.
const cellGroup = new THREE.Group();
cellGroup.position.copy(CENTER);
scene.add(cellGroup);
const membraneGeometry = new THREE.IcosahedronGeometry(2.05, 1);
const membraneMaterial = new THREE.MeshStandardMaterial({
color: 0x5bcde8,
emissive: 0x0b4050,
emissiveIntensity: 0.6,
transparent: true,
opacity: 0.18,
roughness: 0.32,
metalness: 0.05,
side: THREE.DoubleSide,
depthWrite: false,
flatShading: true
});
const membrane = new THREE.Mesh(membraneGeometry, membraneMaterial);
membrane.castShadow = true;
cellGroup.add(membrane);
const membraneEdges = new THREE.LineSegments(
new THREE.EdgesGeometry(membraneGeometry, 8),
new THREE.LineBasicMaterial({
color: 0x8be9ff,
transparent: true,
opacity: 0.52
})
);
cellGroup.add(membraneEdges);
const membraneHalo = new THREE.Mesh(
new THREE.IcosahedronGeometry(2.13, 1),
new THREE.MeshBasicMaterial({
color: 0x68ddff,
transparent: true,
opacity: 0.055,
side: THREE.BackSide,
depthWrite: false
})
);
cellGroup.add(membraneHalo);
const statusRing = new THREE.Mesh(
new THREE.TorusGeometry(2.3, 0.035, 4, 48),
new THREE.MeshBasicMaterial({
color: 0x68ddff,
transparent: true,
opacity: 0.72
})
);
statusRing.rotation.x = Math.PI / 2;
cellGroup.add(statusRing);
// Water-permeable pores embedded in the membrane.
const poreDirections = [
new THREE.Vector3(1, 0.12, 0.08).normalize(),
new THREE.Vector3(-0.52, 0.58, 0.18).normalize(),
new THREE.Vector3(0.15, -0.48, 0.86).normalize(),
new THREE.Vector3(-0.42, 0.08, -0.9).normalize(),
new THREE.Vector3(0.48, 0.55, -0.48).normalize()
];
const poreMaterial = new THREE.MeshStandardMaterial({
color: 0x68ddff,
emissive: 0x68ddff,
emissiveIntensity: 1.2,
roughness: 0.35,
flatShading: true
});
const pores = poreDirections.map((direction) => {
const pore = new THREE.Mesh(
new THREE.TorusGeometry(0.16, 0.035, 4, 10),
poreMaterial
);
pore.position.copy(direction).multiplyScalar(2.055);
pore.quaternion.setFromUnitVectors(
new THREE.Vector3(0, 0, 1),
direction
);
cellGroup.add(pore);
return pore;
});
// Deterministic pseudo-random particle placement.
let randomSeed = 931827;
function random() {
randomSeed =
(randomSeed * 1664525 + 1013904223) >>> 0;
return randomSeed / 4294967296;
}
function randomInsidePosition() {
const direction = new THREE.Vector3();
do {
direction.set(
random() * 2 - 1,
random() * 2 - 1,
random() * 2 - 1
);
} while (direction.lengthSq() < 0.02);
direction.normalize().multiplyScalar(
1.67 * Math.cbrt(random())
);
return direction;
}
function randomOutsidePosition() {
const angle = random() * Math.PI * 2;
const radius = Math.sqrt(
3.1 * 3.1 + random() * (6.7 * 6.7 - 3.1 * 3.1)
);
return new THREE.Vector3(
Math.cos(angle) * radius,
-1.85 + random() * 4.9,
Math.sin(angle) * radius
);
}
const waterGeometry = new THREE.IcosahedronGeometry(0.065, 0);
const waterMaterial = new THREE.MeshStandardMaterial({
color: 0x68ddff,
emissive: 0x0b5972,
emissiveIntensity: 1.1,
roughness: 0.3,
flatShading: true
});
const fluxMaterial = new THREE.MeshStandardMaterial({
color: 0xb5f2ff,
emissive: 0x42cfff,
emissiveIntensity: 2,
roughness: 0.2,
flatShading: true
});
const insideWaterCount = 44;
const outsideWaterCount = 58;
const fluxCount = 25;
const insideWater = new THREE.InstancedMesh(
waterGeometry,
waterMaterial,
insideWaterCount
);
const outsideWater = new THREE.InstancedMesh(
waterGeometry,
waterMaterial,
outsideWaterCount
);
const fluxWater = new THREE.InstancedMesh(
waterGeometry,
fluxMaterial,
fluxCount
);
for (const mesh of [insideWater, outsideWater, fluxWater]) {
mesh.instanceMatrix.setUsage(THREE.DynamicDrawUsage);
mesh.frustumCulled = false;
scene.add(mesh);
}
const insideWaterBases = Array.from(
{ length: insideWaterCount },
randomInsidePosition
);
const outsideWaterBases = Array.from(
{ length: outsideWaterCount },
randomOutsidePosition
);
const fluxPhases = Array.from({ length: fluxCount }, () => random());
const soluteGeometry = new THREE.DodecahedronGeometry(0.105, 0);
const insideSoluteMaterial = new THREE.MeshStandardMaterial({
color: 0xffb15e,
emissive: 0x7a2e00,
emissiveIntensity: 0.6,
roughness: 0.58,
flatShading: true
});
const outsideSoluteMaterial = new THREE.MeshStandardMaterial({
color: 0xff76bd,
emissive: 0x6d124b,
emissiveIntensity: 0.6,
roughness: 0.58,
flatShading: true
});
const maxSoluteParticles = 38;
const insideSolute = new THREE.InstancedMesh(
soluteGeometry,
insideSoluteMaterial,
maxSoluteParticles
);
const outsideSolute = new THREE.InstancedMesh(
soluteGeometry,
outsideSoluteMaterial,
maxSoluteParticles
);
for (const mesh of [insideSolute, outsideSolute]) {
mesh.instanceMatrix.setUsage(THREE.DynamicDrawUsage);
mesh.frustumCulled = false;
scene.add(mesh);
}
const insideSoluteBases = Array.from(
{ length: maxSoluteParticles },
randomInsidePosition
);
const outsideSoluteBases = Array.from(
{ length: maxSoluteParticles },
randomOutsidePosition
);
// Directional arrows reinforce the net direction.
const flowArrows = poreDirections.map(() => {
const arrow = new THREE.ArrowHelper(
new THREE.Vector3(1, 0, 0),
new THREE.Vector3(),
1.75,
0x68ddff,
0.3,
0.17
);
arrow.line.material.transparent = true;
arrow.line.material.opacity = 0.75;
arrow.cone.material.transparent = true;
arrow.cone.material.opacity = 0.88;
arrow.renderOrder = 4;
scene.add(arrow);
return arrow;
});
const dummy = new THREE.Object3D();
const tempPosition = new THREE.Vector3();
const tempDirection = new THREE.Vector3();
const tempOffset = new THREE.Vector3();
let currentDrive = 0;
let currentStrength = 0;
let currentDirection = 1;
let flowPhase = 0;
function getConditions() {
const insideConcentration =
Number(inputs.insideSolute.value) / 1000;
const outsideConcentration =
Number(inputs.outsideSolute.value) / 1000;
const insidePressure = Number(inputs.insidePressure.value);
const outsidePressure = Number(inputs.outsidePressure.value);
// Positive means the inward direction.
// Solute contribution: RT(Cin - Cout).
// Pressure contribution: Pin - Pout, which opposes inward flow.
const osmoticDrive =
RT * (insideConcentration - outsideConcentration);
const pressureDrive = insidePressure - outsidePressure;
const drive = osmoticDrive - pressureDrive;
return {
osmoticDrive,
pressureDrive,
drive
};
}
function updateReadout() {
const conditions = getConditions();
currentDrive = conditions.drive;
const equilibriumThreshold = 0.12;
const isEquilibrium =
Math.abs(currentDrive) < equilibriumThreshold;
currentStrength = THREE.MathUtils.clamp(
Math.abs(currentDrive) / 4,
0,
1
);
currentDirection = currentDrive >= 0 ? 1 : -1;
const status = $("#status");
const mechanism = $("#mechanism");
const formula = $("#formula");
if (isEquilibrium) {
status.textContent = "EQUILIBRIUM";
status.style.color = "#70efad";
mechanism.textContent =
"The solute and pressure effects cancel, so there is no net water movement.";
} else if (currentDrive > 0) {
status.textContent = "WATER ENTERS";
status.style.color = "#68ddff";
mechanism.textContent =
"The combined drive lowers the water chemical potential inside, drawing water into the cell.";
} else {
status.textContent = "WATER LEAVES";
status.style.color = "#ffb068";
mechanism.textContent =
"The combined drive favors water moving out of the cell into the external bath.";
}
$("#osmoticValue").textContent =
`${conditions.osmoticDrive >= 0 ? "+" : ""}` +
`${conditions.osmoticDrive.toFixed(2)} atm inward`;
$("#pressureValue").textContent =
`${conditions.pressureDrive >= 0 ? "+" : ""}` +
`${conditions.pressureDrive.toFixed(2)} atm outward`;
let fluxText = "still";
if (!isEquilibrium) {
if (currentStrength < 0.2) fluxText = "weak";
else if (currentStrength < 0.55) fluxText = "moderate";
else fluxText = "strong";
}
$("#fluxValue").textContent = fluxText;
formula.textContent =
`${conditions.osmoticDrive.toFixed(2)} − ` +
`${conditions.pressureDrive.toFixed(2)} = ` +
`${conditions.drive.toFixed(2)} atm`;
const activeColor = isEquilibrium
? COLORS.equilibrium
: currentDrive > 0
? COLORS.inward
: COLORS.outward;
statusRing.material.color.copy(activeColor);
membraneHalo.material.color.copy(activeColor);
poreMaterial.color.copy(activeColor);
poreMaterial.emissive.copy(activeColor);
for (const arrow of flowArrows) {
arrow.setColor(activeColor);
}
fluxWater.visible = !isEquilibrium && currentStrength > 0.025;
}
function updateSoluteCounts() {
const insideCount = Math.round(
(Number(inputs.insideSolute.value) / 300) *
maxSoluteParticles
);
const outsideCount = Math.round(
(Number(inputs.outsideSolute.value) / 300) *
maxSoluteParticles
);
insideSolute.count = insideCount;
outsideSolute.count = outsideCount;
}
Object.values(inputs).forEach((input) => {
input.addEventListener("input", () => {
updateSoluteCounts();
updateReadout();
});
});
updateSoluteCounts();
updateReadout();
const presets = {
inward: {
insideSolute: 220,
outsideSolute: 80,
insidePressure: 1,
outsidePressure: 1
},
balanced: {
insideSolute: 220,
outsideSolute: 80,
insidePressure: 4.6,
outsidePressure: 1
},
reversed: {
insideSolute: 220,
outsideSolute: 80,
insidePressure: 6,
outsidePressure: 1
},
equal: {
insideSolute: 120,
outsideSolute: 120,
insidePressure: 1,
outsidePressure: 1
}
};
document.querySelectorAll("[data-preset]").forEach((button) => {
button.addEventListener("click", () => {
const preset = presets[button.dataset.preset];
for (const [key, value] of Object.entries(preset)) {
inputs[key].value = value;
}
updateInputLabels();
updateSoluteCounts();
updateReadout();
});
});
// Camera controls are implemented here rather than using an addon.
const cameraTarget = new THREE.Vector3(0, 0.25, 0);
let cameraYaw = 0.72;
let cameraPitch = 0.32;
let cameraRadius = 11.7;
let dragging = false;
let previousX = 0;
let previousY = 0;
function updateCamera() {
const horizontalRadius =
cameraRadius * Math.cos(cameraPitch);
camera.position.set(
cameraTarget.x + Math.sin(cameraYaw) * horizontalRadius,
cameraTarget.y + Math.sin(cameraPitch) * cameraRadius,
cameraTarget.z + Math.cos(cameraYaw) * horizontalRadius
);
camera.lookAt(cameraTarget);
}
function resetCamera() {
cameraYaw = 0.72;
cameraPitch = 0.32;
cameraRadius = 11.7;
updateCamera();
}
renderer.domElement.addEventListener("pointerdown", (event) => {
dragging = true;
previousX = event.clientX;
previousY = event.clientY;
renderer.domElement.setPointerCapture(event.pointerId);
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (!dragging) return;
const deltaX = event.clientX - previousX;
const deltaY = event.clientY - previousY;
previousX = event.clientX;
previousY = event.clientY;
cameraYaw -= deltaX * 0.006;
cameraPitch = THREE.MathUtils.clamp(
cameraPitch + deltaY * 0.005,
-0.12,
1.2
);
updateCamera();
});
function stopDragging(event) {
dragging = false;
if (
event.pointerId !== undefined &&
renderer.domElement.hasPointerCapture(event.pointerId)
) {
renderer.domElement.releasePointerCapture(event.pointerId);
}
}
renderer.domElement.addEventListener("pointerup", stopDragging);
renderer.domElement.addEventListener("pointercancel", stopDragging);
renderer.domElement.addEventListener(
"pointerleave",
(event) => {
if (dragging && event.buttons === 0) stopDragging(event);
}
);
renderer.domElement.addEventListener(
"wheel",
(event) => {
event.preventDefault();
cameraRadius = THREE.MathUtils.clamp(
cameraRadius + event.deltaY * 0.009,
6.2,
19
);
updateCamera();
},
{ passive: false }
);
window.addEventListener("keydown", (event) => {
if (event.key.toLowerCase() === "r") resetCamera();
});
updateCamera();
function projectLabel(element, worldPosition) {
tempPosition.copy(worldPosition).project(camera);
const visible = tempPosition.z < 1;
element.style.display = visible ? "block" : "none";
element.style.left =
`${(tempPosition.x * 0.5 + 0.5) * window.innerWidth}px`;
element.style.top =
`${(-tempPosition.y * 0.5 + 0.5) * window.innerHeight}px`;
}
function updateParticles(time, delta) {
// Interior water.
for (let i = 0; i < insideWaterCount; i++) {
const base = insideWaterBases[i];
tempOffset.set(
Math.sin(time * 0.7 + i * 1.7) * 0.035,
Math.cos(time * 0.8 + i * 1.1) * 0.035,
Math.sin(time * 0.6 + i * 2.3) * 0.035
);
dummy.position.copy(CENTER).add(base).add(tempOffset);
dummy.rotation.set(time * 0.3 + i, time * 0.45, i * 0.7);
dummy.scale.setScalar(1);
dummy.updateMatrix();
insideWater.setMatrixAt(i, dummy.matrix);
}
insideWater.instanceMatrix.needsUpdate = true;
// Exterior water.
for (let i = 0; i < outsideWaterCount; i++) {
const base = outsideWaterBases[i];
tempOffset.set(
Math.sin(time * 0.35 + i * 0.8) * 0.08,
Math.cos(time * 0.45 + i * 1.4) * 0.08,
Math.sin(time * 0.4 + i * 2.1) * 0.08
);
dummy.position.copy(base).add(tempOffset);
dummy.rotation.set(i + time * 0.2, time * 0.3, i * 0.45);
dummy.scale.setScalar(1);
dummy.updateMatrix();
outsideWater.setMatrixAt(i, dummy.matrix);
}
outsideWater.instanceMatrix.needsUpdate = true;
// Impermeant solute particles remain in their original compartment.
for (let i = 0; i < insideSolute.count; i++) {
const base = insideSoluteBases[i];
tempOffset.set(
Math.sin(time * 0.45 + i) * 0.02,
Math.cos(time * 0.5 + i * 1.3) * 0.02,
Math.sin(time * 0.4 + i * 2.4) * 0.02
);
dummy.position.copy(CENTER).add(base).add(tempOffset);
dummy.rotation.set(time * 0.18 + i, time * 0.23, i * 0.6);
dummy.scale.setScalar(1);
dummy.updateMatrix();
insideSolute.setMatrixAt(i, dummy.matrix);
}
insideSolute.instanceMatrix.needsUpdate = true;
for (let i = 0; i < outsideSolute.count; i++) {
const base = outsideSoluteBases[i];
tempOffset.set(
Math.sin(time * 0.4 + i * 1.2) * 0.025,
Math.cos(time * 0.45 + i) * 0.025,
Math.sin(time * 0.35 + i * 1.7) * 0.025
);
dummy.position.copy(base).add(tempOffset);
dummy.rotation.set(time * 0.16 + i, time * 0.2, i * 0.4);
dummy.scale.setScalar(1);
dummy.updateMatrix();
outsideSolute.setMatrixAt(i, dummy.matrix);
}
outsideSolute.instanceMatrix.needsUpdate = true;
// Tracers crossing the membrane visualize net water movement.
if (!fluxWater.visible) {
flowArrows.forEach((arrow) => {
arrow.visible = false;
});
} else {
flowPhase =
(flowPhase + delta * (0.12 + currentStrength * 0.65)) % 1;
for (let i = 0; i < fluxCount; i++) {
const poreDirection = poreDirections[i % poreDirections.length];
const phase = (fluxPhases[i] + flowPhase) % 1;
let radius;
if (currentDirection > 0) {
radius = THREE.MathUtils.lerp(3.25, 1.25, phase);
} else {
radius = THREE.MathUtils.lerp(1.25, 3.25, phase);
}
dummy.position
.copy(CENTER)
.addScaledVector(poreDirection, radius);
dummy.rotation.set(
time * 0.9 + i,
time * 0.7 + phase * Math.PI,
i * 0.8
);
dummy.scale.setScalar(1.3);
dummy.updateMatrix();
fluxWater.setMatrixAt(i, dummy.matrix);
}
fluxWater.instanceMatrix.needsUpdate = true;
flowArrows.forEach((arrow, i) => {
const direction = poreDirections[i];
if (currentDirection > 0) {
arrow.visible = true;
arrow.position
.copy(CENTER)
.addScaledVector(direction, 3.3);
tempDirection.copy(direction).multiplyScalar(-1);
} else {
arrow.visible = true;
arrow.position
.copy(CENTER)
.addScaledVector(direction, 1.2);
tempDirection.copy(direction);
}
arrow.setDirection(tempDirection);
arrow.setLength(
1.85,
0.28 + currentStrength * 0.08,
0.16 + currentStrength * 0.05
);
});
}
statusRing.material.opacity =
0.58 + Math.sin(time * 2.2) * 0.16;
poreMaterial.emissiveIntensity =
fluxWater.visible ? 1.1 + currentStrength * 1.5 : 0.45;
membraneHalo.material.opacity =
fluxWater.visible ? 0.045 + currentStrength * 0.05 : 0.035;
}
const clock = new THREE.Clock();
renderer.setAnimationLoop(() => {
const delta = Math.min(clock.getDelta(), 0.05);
const time = clock.elapsedTime;
updateParticles(time, delta);
updateReadout();
projectLabel(
$("#insideLabel"),
CENTER.clone().add(new THREE.Vector3(-0.7, 0.25, 0))
);
projectLabel(
$("#outsideLabel"),
new THREE.Vector3(4.15, 1.15, 0.35)
);
renderer.render(scene, camera);
});
function resize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
}
window.addEventListener("resize", resize);
</script>
</body>
</html>
```