Prompt specification
Build or customize the Three.js 3D scene "Membrane transport intervention table" 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: Membrane transport intervention table
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/membrane-transport-intervention-table-x0856
Scene Description Prompt:
Build a self-contained HTML scene with Three.js from cdnjs featuring selective cell membranes. Allow toggling one channel type and changing particle concentration; show permitted crossings in a toy model while keeping passive movement distinct from explicitly energy-driven transport. Voxel style.
Working Three.js Starter (membrane-transport-intervention-table-x0856):
```html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Selective Membrane — Voxel Transport Lab</title>
<style>
:root {
--bg: #07131c;
--panel: rgba(7, 22, 30, 0.92);
--line: rgba(116, 220, 211, 0.24);
--text: #e7fbf7;
--muted: #86a8aa;
--mint: #57d7bd;
--orange: #ffb454;
--cyan: #55e6ff;
--red: #ff5470;
--yellow: #f8df65;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: var(--bg);
color: var(--text);
font-family: "SFMono-Regular", Consolas, "Liberation Mono", monospace;
}
body::after {
content: "";
position: fixed;
inset: 0;
z-index: 8;
pointer-events: none;
opacity: 0.12;
background:
repeating-linear-gradient(
to bottom,
transparent 0,
transparent 3px,
rgba(120, 255, 235, 0.08) 4px
);
}
#scene {
position: fixed;
inset: 0;
width: 100%;
height: 100%;
display: block;
cursor: grab;
touch-action: none;
}
#scene.dragging {
cursor: grabbing;
}
.panel {
position: fixed;
z-index: 10;
top: 20px;
left: 20px;
width: min(348px, calc(100vw - 40px));
padding: 18px;
border: 1px solid var(--line);
background: var(--panel);
box-shadow: 0 18px 60px rgba(0, 0, 0, 0.35);
backdrop-filter: blur(12px);
}
.eyebrow {
display: flex;
align-items: center;
gap: 9px;
margin-bottom: 11px;
color: var(--mint);
font-size: 10px;
letter-spacing: 0.18em;
text-transform: uppercase;
}
.eyebrow::before {
content: "";
width: 8px;
height: 8px;
background: var(--mint);
box-shadow: 0 0 16px var(--mint);
}
h1 {
margin: 0;
font-size: clamp(22px, 3vw, 29px);
line-height: 1.05;
letter-spacing: -0.04em;
text-transform: uppercase;
}
.intro {
margin: 11px 0 18px;
color: var(--muted);
font-size: 12px;
line-height: 1.6;
}
.section-title {
display: flex;
align-items: center;
justify-content: space-between;
margin: 17px 0 9px;
color: var(--muted);
font-size: 10px;
letter-spacing: 0.15em;
text-transform: uppercase;
}
.section-title::after {
content: "";
flex: 1;
height: 1px;
margin-left: 12px;
background: var(--line);
}
.channel {
display: grid;
grid-template-columns: 1fr auto;
align-items: center;
gap: 12px;
margin-bottom: 9px;
padding: 11px;
border: 1px solid rgba(130, 190, 190, 0.13);
background: rgba(255, 255, 255, 0.025);
}
.channel strong {
display: block;
margin-bottom: 4px;
font-size: 12px;
letter-spacing: 0.02em;
}
.channel small {
display: block;
color: var(--muted);
font-size: 10px;
line-height: 1.4;
}
.channel.passive strong {
color: var(--orange);
}
.channel.active strong {
color: var(--cyan);
}
.switch {
position: relative;
width: 46px;
height: 24px;
padding: 0;
border: 1px solid #31545a;
border-radius: 0;
background: #102a32;
cursor: pointer;
}
.switch span {
position: absolute;
top: 3px;
left: 3px;
width: 16px;
height: 16px;
background: #527076;
transition: transform 160ms ease, background 160ms ease;
}
.switch.is-on {
border-color: var(--mint);
background: rgba(87, 215, 189, 0.13);
}
.switch.is-on span {
transform: translateX(22px);
background: var(--mint);
box-shadow: 0 0 12px rgba(87, 215, 189, 0.65);
}
.switch:focus-visible,
button:focus-visible,
input:focus-visible {
outline: 2px solid var(--mint);
outline-offset: 3px;
}
.range-label {
display: flex;
justify-content: space-between;
margin-bottom: 9px;
font-size: 11px;
}
.range-label output {
color: var(--orange);
font-weight: bold;
}
input[type="range"] {
width: 100%;
height: 18px;
margin: 0;
accent-color: var(--orange);
cursor: pointer;
}
.range-scale {
display: flex;
justify-content: space-between;
margin-top: 2px;
color: #55787d;
font-size: 9px;
}
.stats {
display: grid;
grid-template-columns: repeat(2, 1fr);
gap: 7px;
margin-top: 15px;
}
.stat {
padding: 9px;
border-left: 2px solid #24474e;
background: rgba(255, 255, 255, 0.025);
}
.stat span {
display: block;
margin-bottom: 5px;
color: var(--muted);
font-size: 9px;
text-transform: uppercase;
}
.stat strong {
font-size: 17px;
}
.stat.solute strong {
color: var(--orange);
}
.stat.cargo strong {
color: var(--cyan);
}
.flux {
margin-top: 8px;
padding: 9px;
color: var(--muted);
border: 1px dashed rgba(116, 220, 211, 0.17);
font-size: 10px;
text-align: center;
}
.energy {
margin-top: 13px;
padding: 10px;
border: 1px solid rgba(85, 230, 255, 0.15);
background: rgba(85, 230, 255, 0.035);
}
.energy-head {
display: flex;
justify-content: space-between;
margin-bottom: 8px;
color: var(--cyan);
font-size: 10px;
letter-spacing: 0.08em;
}
.energy-track {
height: 8px;
padding: 2px;
border: 1px solid rgba(85, 230, 255, 0.25);
}
.energy-fill {
width: 100%;
height: 100%;
background: var(--cyan);
box-shadow: 0 0 12px rgba(85, 230, 255, 0.6);
transition: width 120ms linear, background 160ms ease;
}
.reset {
width: 100%;
margin-top: 14px;
padding: 10px;
border: 1px solid var(--mint);
border-radius: 0;
color: var(--mint);
background: rgba(87, 215, 189, 0.08);
font: inherit;
font-size: 10px;
letter-spacing: 0.13em;
text-transform: uppercase;
cursor: pointer;
}
.reset:hover {
color: #06131a;
background: var(--mint);
}
.legend {
position: fixed;
z-index: 10;
left: 50%;
bottom: 22px;
display: flex;
gap: 17px;
align-items: center;
transform: translateX(-50%);
padding: 10px 14px;
border: 1px solid var(--line);
background: rgba(7, 22, 30, 0.86);
color: var(--muted);
font-size: 10px;
white-space: nowrap;
}
.legend span {
display: flex;
align-items: center;
gap: 7px;
}
.cube {
width: 9px;
height: 9px;
display: inline-block;
box-shadow: 0 0 9px currentColor;
}
.cube.orange {
color: var(--orange);
background: var(--orange);
}
.cube.cyan {
color: var(--cyan);
background: var(--cyan);
}
.cube.red {
color: var(--red);
background: var(--red);
}
.hint {
position: fixed;
z-index: 10;
right: 20px;
bottom: 22px;
color: #729397;
font-size: 9px;
letter-spacing: 0.08em;
text-transform: uppercase;
}
@media (max-width: 760px) {
.panel {
top: 10px;
left: 10px;
width: calc(100vw - 20px);
max-height: 51vh;
overflow: auto;
padding: 14px;
}
.intro {
margin-bottom: 12px;
}
.legend {
left: 10px;
right: 10px;
bottom: 10px;
justify-content: center;
gap: 10px;
transform: none;
overflow: auto;
}
.hint {
display: none;
}
}
</style>
</head>
<body>
<canvas id="scene" aria-label="Interactive voxel model of a selective cell membrane"></canvas>
<aside class="panel">
<div class="eyebrow">Voxel Bio / Transport Lab</div>
<h1>Selective Membrane</h1>
<p class="intro">
Orange solute can diffuse through a passive channel.
Cyan cargo requires the explicitly energy-driven ATP pump.
</p>
<div class="section-title">Channel controls</div>
<div class="channel passive">
<div>
<strong>Passive diffusion channel</strong>
<small>Small solute only · no ATP · net down-gradient</small>
</div>
<button
id="passiveToggle"
class="switch is-on"
role="switch"
aria-checked="true"
aria-label="Toggle passive diffusion channel"
><span></span></button>
</div>
<div class="channel active">
<div>
<strong>Active ATP pump</strong>
<small>Cargo only · consumes ATP · against gradient</small>
</div>
<button
id="pumpToggle"
class="switch is-on"
role="switch"
aria-checked="true"
aria-label="Toggle active ATP pump"
><span></span></button>
</div>
<div class="section-title">Concentration</div>
<label class="range-label" for="concentration">
<span>Extracellular solute</span>
<output id="concentrationValue" for="concentration">64 /mL</output>
</label>
<input
id="concentration"
type="range"
min="12"
max="84"
step="1"
value="64"
>
<div class="range-scale">
<span>12 dilute</span>
<span>84 concentrated</span>
</div>
<div class="section-title">Live particle counts</div>
<div class="stats">
<div class="stat solute">
<span>Solute inside</span>
<strong id="soluteInside">12</strong>
</div>
<div class="stat solute">
<span>Solute outside</span>
<strong id="soluteOutside">64</strong>
</div>
<div class="stat cargo">
<span>Cargo inside</span>
<strong id="cargoInside">10</strong>
</div>
<div class="stat cargo">
<span>Cargo outside</span>
<strong id="cargoOutside">2</strong>
</div>
</div>
<div class="flux" id="fluxReadout">Passive net flux: 0 into cell</div>
<div class="energy">
<div class="energy-head">
<span>ATP RESERVE</span>
<span id="energyValue">100%</span>
</div>
<div class="energy-track">
<div class="energy-fill" id="energyFill"></div>
</div>
</div>
<button class="reset" id="reset">Reset particle field</button>
</aside>
<div class="legend">
<span><i class="cube orange"></i> Passive solute</span>
<span><i class="cube cyan"></i> ATP-driven cargo</span>
<span><i class="cube red"></i> Blocked crossing</span>
</div>
<div class="hint">Drag to orbit · wheel to zoom · double-click to reset view</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// -------------------------------------------------------------------------
// Scene setup
// -------------------------------------------------------------------------
const canvas = document.querySelector("#scene");
const renderer = new THREE.WebGLRenderer({
canvas,
antialias: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.15;
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x07131c);
scene.fog = new THREE.Fog(0x07131c, 20, 42);
const camera = new THREE.PerspectiveCamera(
43,
window.innerWidth / window.innerHeight,
0.1,
100
);
const hemi = new THREE.HemisphereLight(0x9af4e4, 0x07131c, 1.8);
scene.add(hemi);
const key = new THREE.DirectionalLight(0xffffff, 2.7);
key.position.set(7, 12, 9);
scene.add(key);
const cyanLight = new THREE.PointLight(0x36dfff, 18, 15, 2);
cyanLight.position.set(4, -1, 5);
scene.add(cyanLight);
const orangeLight = new THREE.PointLight(0xff9a3c, 12, 12, 2);
orangeLight.position.set(-3, 4, -3);
scene.add(orangeLight);
const world = new THREE.Group();
scene.add(world);
const particleGroup = new THREE.Group();
const effectGroup = new THREE.Group();
world.add(particleGroup, effectGroup);
const state = {
passive: true,
pump: true,
concentration: 64,
atp: 100
};
const CELL_LIMIT = 7.2;
const OUTSIDE_LIMIT = 10.2;
const CROSS_LIMIT = 0.16;
const GATE_RADIUS = 0.76;
const passiveGate = {
type: "passive",
y: 1.48,
z: -1.42
};
const activeGate = {
type: "pump",
y: -1.48,
z: 1.42
};
// -------------------------------------------------------------------------
// Small helpers
// -------------------------------------------------------------------------
function randomRange(min, max) {
return min + Math.random() * (max - min);
}
function makeBox(width, height, depth, material) {
return new THREE.BoxGeometry(width, height, depth);
}
function addBox(parent, size, position, material) {
const mesh = new THREE.Mesh(
new THREE.BoxGeometry(size[0], size[1], size[2]),
material
);
mesh.position.set(position[0], position[1], position[2]);
parent.add(mesh);
return mesh;
}
function createLabel(text, color, position, width = 4.4) {
const labelCanvas = document.createElement("canvas");
labelCanvas.width = 512;
labelCanvas.height = 112;
const ctx = labelCanvas.getContext("2d");
ctx.clearRect(0, 0, 512, 112);
ctx.fillStyle = "rgba(5, 18, 25, 0.88)";
ctx.fillRect(5, 5, 502, 102);
ctx.strokeStyle = color;
ctx.lineWidth = 5;
ctx.strokeRect(7, 7, 498, 98);
ctx.fillStyle = color;
ctx.font = "bold 30px monospace";
ctx.textAlign = "center";
ctx.textBaseline = "middle";
ctx.fillText(text, 256, 58);
const texture = new THREE.CanvasTexture(labelCanvas);
texture.colorSpace = THREE.SRGBColorSpace;
const material = new THREE.SpriteMaterial({
map: texture,
transparent: true,
depthTest: false,
depthWrite: false
});
const sprite = new THREE.Sprite(material);
sprite.position.set(...position);
sprite.scale.set(width, width * 112 / 512, 1);
sprite.renderOrder = 20;
world.add(sprite);
return sprite;
}
function addChamber(width, centerX, color, opacity) {
const height = 9.2;
const depth = 9.2;
const volume = new THREE.Mesh(
new THREE.BoxGeometry(width, height, depth),
new THREE.MeshBasicMaterial({
color,
transparent: true,
opacity,
depthWrite: false,
side: THREE.DoubleSide
})
);
volume.position.x = centerX;
world.add(volume);
const edges = new THREE.LineSegments(
new THREE.EdgesGeometry(volume.geometry),
new THREE.LineBasicMaterial({
color,
transparent: true,
opacity: 0.22
})
);
edges.position.copy(volume.position);
world.add(edges);
}
// -------------------------------------------------------------------------
// Voxel floor
// -------------------------------------------------------------------------
const floorGeometry = new THREE.BoxGeometry(0.82, 0.06, 0.82);
const floorMaterial = new THREE.MeshStandardMaterial({
color: 0x12313a,
roughness: 0.95,
metalness: 0
});
const floor = new THREE.InstancedMesh(
floorGeometry,
floorMaterial,
22 * 22
);
const dummy = new THREE.Object3D();
let floorIndex = 0;
for (let x = 0; x < 22; x++) {
for (let z = 0; z < 22; z++) {
dummy.position.set(
-9 + x * 0.95,
-5.05,
-10 + z * 0.95
);
dummy.updateMatrix();
floor.setMatrixAt(floorIndex++, dummy.matrix);
}
}
floor.instanceMatrix.needsUpdate = true;
world.add(floor);
// -------------------------------------------------------------------------
// Cell chambers and voxel membrane
// -------------------------------------------------------------------------
addChamber(CELL_LIMIT, -CELL_LIMIT / 2, 0x1bc6ae, 0.055);
addChamber(OUTSIDE_LIMIT, OUTSIDE_LIMIT / 2, 0x368dff, 0.035);
const membraneGroup = new THREE.Group();
world.add(membraneGroup);
const membraneGrid = 13;
const membraneSpacing = 0.72;
const membraneGeometry = new THREE.BoxGeometry(
0.52,
membraneSpacing * 0.91,
membraneSpacing * 0.91
);
const membraneMaterial = new THREE.MeshStandardMaterial({
color: 0xffffff,
roughness: 0.74,
metalness: 0.04,
transparent: true,
opacity: 0.72,
depthWrite: false
});
const membraneColors = [
new THREE.Color(0x176c67),
new THREE.Color(0x218278),
new THREE.Color(0x155b59),
new THREE.Color(0x2a9183)
];
const membraneTiles = [];
for (let iy = 0; iy < membraneGrid; iy++) {
for (let iz = 0; iz < membraneGrid; iz++) {
const y = (iy - (membraneGrid - 1) / 2) * membraneSpacing;
const z = (iz - (membraneGrid - 1) / 2) * membraneSpacing;
const inPassiveOpening =
Math.abs(y - passiveGate.y) < 0.91 &&
Math.abs(z - passiveGate.z) < 0.91;
const inActiveOpening =
Math.abs(y - activeGate.y) < 0.91 &&
Math.abs(z - activeGate.z) < 0.91;
if (!inPassiveOpening && !inActiveOpening) {
membraneTiles.push({ y, z });
}
}
}
const membrane = new THREE.InstancedMesh(
membraneGeometry,
membraneMaterial,
membraneTiles.length
);
membraneTiles.forEach((tile, index) => {
dummy.position.set(0, tile.y, tile.z);
dummy.rotation.set(0, 0, 0);
dummy.scale.set(1, 1, 1);
dummy.updateMatrix();
membrane.setMatrixAt(index, dummy.matrix);
membrane.setColorAt(
index,
membraneColors[index % membraneColors.length]
);
});
membrane.instanceMatrix.needsUpdate = true;
membrane.instanceColor.needsUpdate = true;
membraneGroup.add(membrane);
// -------------------------------------------------------------------------
// Channel / pump assemblies
// -------------------------------------------------------------------------
function buildGate(config) {
const group = new THREE.Group();
group.position.set(0, config.y, config.z);
world.add(group);
const accent = new THREE.MeshStandardMaterial({
color: config.color,
emissive: config.color,
emissiveIntensity: 0.7,
roughness: 0.5,
metalness: 0.12
});
const dark = new THREE.MeshStandardMaterial({
color: 0x102d36,
emissive: 0x06151c,
roughness: 0.8
});
// Square voxel frame around the opening.
addBox(group, [0.62, 0.22, 1.68], [0, 0.84, 0], accent);
addBox(group, [0.62, 0.22, 1.68], [0, -0.84, 0], accent);
addBox(group, [0.62, 1.46, 0.22], [0, 0, 0.84], accent);
addBox(group, [0.62, 1.46, 0.22], [0, 0, -0.84], accent);
const door = addBox(
group,
[0.38, 1.40, 1.40],
[0.03, 0, 0],
dark
);
const indicatorMaterial = new THREE.MeshBasicMaterial({
color: config.color,
transparent: true,
opacity: 0.95
});
const dots = [];
if (config.type === "passive") {
// Two sets imply bidirectional passive exchange.
for (let i = 0; i < 3; i++) {
const outsideDot = new THREE.Mesh(
new THREE.BoxGeometry(0.14, 0.14, 0.14),
indicatorMaterial
);
outsideDot.position.set(0.36 + i * 0.23, 0, 0);
group.add(outsideDot);
dots.push({ mesh: outsideDot, direction: 1, phase: i / 3 });
const insideDot = new THREE.Mesh(
new THREE.BoxGeometry(0.14, 0.14, 0.14),
indicatorMaterial
);
insideDot.position.set(-0.36 - i * 0.23, 0, 0);
group.add(insideDot);
dots.push({ mesh: insideDot, direction: -1, phase: i / 3 });
}
} else {
// The pump arrow points outward, from the cell into the exterior.
for (let i = 0; i < 4; i++) {
const dot = new THREE.Mesh(
new THREE.BoxGeometry(0.16, 0.16, 0.16),
indicatorMaterial
);
dot.position.set(0.3 + i * 0.25, 0, 0);
group.add(dot);
dots.push({ mesh: dot, direction: 1, phase: i / 4 });
}
addBox(
group,
[0.18, 0.20, 0.20],
[0.34, 0.48, 0],
new THREE.MeshStandardMaterial({
color: 0xf8df65,
emissive: 0xf8df65,
emissiveIntensity: 0.8
})
);
}
return {
...config,
group,
accent,
door,
dots,
indicatorMaterial,
flash: 0
};
}
const passiveAssembly = buildGate({
type: "passive",
y: passiveGate.y,
z: passiveGate.z,
color: 0xffb454
});
const activeAssembly = buildGate({
type: "pump",
y: activeGate.y,
z: activeGate.z,
color: 0x55e6ff
});
createLabel(
"PASSIVE / DIFFUSIVE",
"#ffb454",
[0.35, 3.12, -1.42],
4.5
);
createLabel(
"ACTIVE / ATP PUMP",
"#55e6ff",
[0.35, -3.18, 1.42],
4.5
);
createLabel(
"INTRACELLULAR",
"#65d9c2",
[-4.5, 5.05, 0],
4.4
);
createLabel(
"EXTRACELLULAR",
"#66a8ff",
[5.2, 5.05, 0],
4.4
);
// -------------------------------------------------------------------------
// Particles and crossing effects
// -------------------------------------------------------------------------
const particleGeometry = {
solute: new THREE.BoxGeometry(0.18, 0.18, 0.18),
cargo: new THREE.BoxGeometry(0.23, 0.23, 0.23)
};
const particleMaterial = {
solute: new THREE.MeshStandardMaterial({
color: 0xffb454,
emissive: 0x8a3c00,
emissiveIntensity: 0.65,
roughness: 0.42
}),
cargo: new THREE.MeshStandardMaterial({
color: 0x55e6ff,
emissive: 0x006a88,
emissiveIntensity: 0.85,
roughness: 0.32,
metalness: 0.12
})
};
const particles = [];
const effects = [];
const effectGeometry = new THREE.BoxGeometry(0.13, 0.13, 0.13);
let netPassiveFlux = 0;
let cargoPumped = 0;
function createParticle(type, side) {
const mesh = new THREE.Mesh(
particleGeometry[type],
particleMaterial[type]
);
const position = new THREE.Vector3(
side === "inside"
? randomRange(-CELL_LIMIT + 0.45, -0.55)
: randomRange(0.5, OUTSIDE_LIMIT - 0.5),
randomRange(-4.25, 4.25),
randomRange(-4.25, 4.25)
);
const velocity = new THREE.Vector3(
randomRange(-0.65, 0.65),
randomRange(-0.65, 0.65),
randomRange(-0.65, 0.65)
);
if (velocity.lengthSq() < 0.01) {
velocity.set(0.4, 0.2, -0.2);
}
velocity.normalize();
velocity.multiplyScalar(randomRange(0.48, 0.94));
if (type === "solute" && side === "outside") {
velocity.x -= 0.36;
}
if (type === "cargo" && side === "inside") {
velocity.x += 0.2;
}
mesh.position.copy(position);
particleGroup.add(mesh);
const particle = {
type,
side,
mesh,
position,
velocity,
blockedCooldown: 0
};
particles.push(particle);
return particle;
}
function clearEffects() {
for (const effect of effects) {
effectGroup.remove(effect.mesh);
effect.mesh.material.dispose();
}
effects.length = 0;
}
function buildParticles() {
for (const particle of particles) {
particleGroup.remove(particle.mesh);
}
particles.length = 0;
clearEffects();
netPassiveFlux = 0;
cargoPumped = 0;
// The initial cell-side solute concentration is deliberately lower.
for (let i = 0; i < 12; i++) {
createParticle("solute", "inside");
}
for (let i = 0; i < state.concentration; i++) {
createParticle("solute", "outside");
}
// Cargo begins concentrated inside, so outward pumping is against gradient.
for (let i = 0; i < 10; i++) {
createParticle("cargo", "inside");
}
for (let i = 0; i < 2; i++) {
createParticle("cargo", "outside");
}
updateStats();
}
function spawnBurst(position, color, count = 4, blocked = false) {
for (let i = 0; i < count; i++) {
const material = new THREE.MeshBasicMaterial({
color,
transparent: true,
opacity: 1
});
const mesh = new THREE.Mesh(effectGeometry, material);
mesh.position.copy(position);
mesh.position.x += blocked ? 0.26 : randomRange(-0.12, 0.12);
mesh.position.y += randomRange(-0.12, 0.12);
mesh.position.z += randomRange(-0.12, 0.12);
const velocity = new THREE.Vector3(
randomRange(-0.85, 0.85),
randomRange(-0.85, 0.85),
randomRange(-0.85, 0.85)
);
if (blocked) {
velocity.x = Math.abs(velocity.x) + 0.35;
}
effectGroup.add(mesh);
effects.push({
mesh,
velocity,
life: blocked ? 0.34 : 0.52,
maxLife: blocked ? 0.34 : 0.52
});
}
}
function isNearGate(particle, gate) {
return (
Math.abs(particle.position.y - gate.y) < GATE_RADIUS &&
Math.abs(particle.position.z - gate.z) < GATE_RADIUS
);
}
function blockCrossing(particle) {
if (particle.blockedCooldown <= 0) {
spawnBurst(
particle.position,
0xff5470,
particle.type === "cargo" ? 3 : 2,
true
);
particle.blockedCooldown = 0.55;
}
}
function allowCrossing(particle, newSide, gate) {
particle.side = newSide;
particle.position.x = newSide === "inside" ? -CROSS_LIMIT : CROSS_LIMIT;
if (particle.type === "solute") {
particle.velocity.x =
newSide === "inside"
? -Math.abs(particle.velocity.x)
: Math.abs(particle.velocity.x);
netPassiveFlux += newSide === "inside" ? 1 : -1;
} else {
particle.velocity.x = Math.abs(particle.velocity.x) + 0.22;
cargoPumped++;
state.atp = Math.max(0, state.atp - 4.5);
}
gate.flash = 1;
spawnBurst(
particle.position,
particle.type === "solute" ? 0xffb454 : 0x55e6ff,
particle.type === "solute" ? 4 : 6
);
}
function updateParticleCrossing(particle) {
const nearPassive = isNearGate(particle, passiveGate);
const nearActive = isNearGate(particle, activeGate);
if (particle.side === "inside") {
if (particle.position.x > -CROSS_LIMIT) {
if (nearPassive) {
if (particle.type === "solute" && state.passive) {
allowCrossing(particle, "outside", passiveAssembly);
} else {
particle.position.x = -CROSS_LIMIT;
particle.velocity.x = -Math.abs(particle.velocity.x);
blockCrossing(particle);
}
} else if (nearActive) {
if (
particle.type === "cargo" &&
state.pump &&
state.atp > 0
) {
allowCrossing(particle, "outside", activeAssembly);
} else {
particle.position.x = -CROSS_LIMIT;
particle.velocity.x = -Math.abs(particle.velocity.x);
blockCrossing(particle);
}
} else {
particle.position.x = -CROSS_LIMIT;
particle.velocity.x = -Math.abs(particle.velocity.x);
blockCrossing(particle);
}
}
} else {
if (particle.position.x < CROSS_LIMIT) {
if (nearPassive) {
if (particle.type === "solute" && state.passive) {
allowCrossing(particle, "inside", passiveAssembly);
} else {
particle.position.x = CROSS_LIMIT;
particle.velocity.x = Math.abs(particle.velocity.x);
blockCrossing(particle);
}
} else if (nearActive) {
// The ATP pump is intentionally one-way: inside to outside.
particle.position.x = CROSS_LIMIT;
particle.velocity.x = Math.abs(particle.velocity.x);
blockCrossing(particle);
} else {
particle.position.x = CROSS_LIMIT;
particle.velocity.x = Math.abs(particle.velocity.x);
blockCrossing(particle);
}
}
}
}
function updateParticles(dt) {
const outsideSolute = particles.filter(
p => p.type === "solute" && p.side === "outside"
).length;
const insideSolute = particles.filter(
p => p.type === "solute" && p.side === "inside"
).length;
const concentrationBias =
outsideSolute > insideSolute ? -0.28 : 0;
for (const particle of particles) {
particle.blockedCooldown -= dt;
// Brownian voxel jitter.
particle.velocity.x += randomRange(-0.65, 0.65) * dt;
particle.velocity.y += randomRange(-0.65, 0.65) * dt;
particle.velocity.z += randomRange(-0.65, 0.65) * dt;
if (particle.type === "solute") {
// Solutes are steered toward the passive opening, but retain
// random motion so exchange remains diffusion-like.
particle.velocity.y +=
(passiveGate.y - particle.position.y) * 0.85 * dt;
particle.velocity.z +=
(passiveGate.z - particle.position.z) * 0.85 * dt;
if (particle.side === "outside") {
particle.velocity.x += concentrationBias * dt;
} else {
particle.velocity.x += 0.035 * dt;
}
} else if (particle.side === "inside") {
// Cargo is routed toward the active pump.
particle.velocity.y +=
(activeGate.y - particle.position.y) * 1.18 * dt;
particle.velocity.z +=
(activeGate.z - particle.position.z) * 1.18 * dt;
particle.velocity.x += 0.48 * dt;
}
const damping = Math.pow(0.998, dt * 60);
particle.velocity.multiplyScalar(damping);
const speed = particle.velocity.length();
if (speed > 1.45) {
particle.velocity.multiplyScalar(1.45 / speed);
}
particle.position.addScaledVector(particle.velocity, dt);
// Keep particles within the illustrated slice.
if (particle.position.y < -4.55) {
particle.position.y = -4.55;
particle.velocity.y = Math.abs(particle.velocity.y);
} else if (particle.position.y > 4.55) {
particle.position.y = 4.55;
particle.velocity.y = -Math.abs(particle.velocity.y);
}
if (particle.position.z < -4.55) {
particle.position.z = -4.55;
particle.velocity.z = Math.abs(particle.velocity.z);
} else if (particle.position.z > 4.55) {
particle.position.z = 4.55;
particle.velocity.z = -Math.abs(particle.velocity.z);
}
if (particle.side === "inside") {
if (particle.position.x < -CELL_LIMIT + 0.2) {
particle.position.x = -CELL_LIMIT + 0.2;
particle.velocity.x = Math.abs(particle.velocity.x);
}
} else {
if (particle.position.x > OUTSIDE_LIMIT - 0.2) {
particle.position.x = OUTSIDE_LIMIT - 0.2;
particle.velocity.x = -Math.abs(particle.velocity.x);
}
}
updateParticleCrossing(particle);
particle.mesh.position.copy(particle.position);
particle.mesh.rotation.x += dt * 1.6;
particle.mesh.rotation.y += dt * 1.9;
}
}
function updateEffects(dt) {
for (let i = effects.length - 1; i >= 0; i--) {
const effect = effects[i];
effect.life -= dt;
if (effect.life <= 0) {
effectGroup.remove(effect.mesh);
effect.mesh.material.dispose();
effects.splice(i, 1);
continue;
}
effect.mesh.position.addScaledVector(effect.velocity, dt);
effect.velocity.multiplyScalar(Math.pow(0.96, dt * 60));
effect.mesh.rotation.x += dt * 4;
effect.mesh.rotation.y += dt * 3;
const scale = Math.max(0.05, effect.life / effect.maxLife);
effect.mesh.scale.setScalar(scale);
effect.mesh.material.opacity = scale;
}
}
// -------------------------------------------------------------------------
// Gate animation and ATP
// -------------------------------------------------------------------------
function updateGate(assembly, dt, time, operational) {
assembly.flash = Math.max(0, assembly.flash - dt * 2.8);
assembly.door.visible = !operational;
assembly.accent.emissiveIntensity =
0.25 +
(operational ? 0.55 : 0) +
assembly.flash * 2.2;
assembly.indicatorMaterial.opacity = operational ? 0.95 : 0.12;
for (const dot of assembly.dots) {
dot.mesh.visible = operational;
const travel = (time * 0.62 + dot.phase) % 1;
dot.mesh.position.x =
dot.direction > 0
? 0.28 + travel * 0.95
: -0.28 - travel * 0.95;
const scale = 0.75 + 0.25 * Math.sin(time * 5 + dot.phase * 6);
dot.mesh.scale.setScalar(scale);
}
}
function updateATP(dt) {
// A small recharge keeps the demonstration running, while each active
// transport event still has an explicit and visible ATP cost.
const recharge = state.pump ? 0.62 : 0.2;
state.atp = Math.min(100, state.atp + recharge * dt);
}
// -------------------------------------------------------------------------
// UI
// -------------------------------------------------------------------------
const passiveToggle = document.querySelector("#passiveToggle");
const pumpToggle = document.querySelector("#pumpToggle");
const concentration = document.querySelector("#concentration");
const concentrationValue = document.querySelector("#concentrationValue");
const resetButton = document.querySelector("#reset");
const soluteInsideEl = document.querySelector("#soluteInside");
const soluteOutsideEl = document.querySelector("#soluteOutside");
const cargoInsideEl = document.querySelector("#cargoInside");
const cargoOutsideEl = document.querySelector("#cargoOutside");
const fluxReadout = document.querySelector("#fluxReadout");
const energyValue = document.querySelector("#energyValue");
const energyFill = document.querySelector("#energyFill");
function syncToggle(button, enabled) {
button.classList.toggle("is-on", enabled);
button.setAttribute("aria-checked", String(enabled));
}
passiveToggle.addEventListener("click", () => {
state.passive = !state.passive;
syncToggle(passiveToggle, state.passive);
});
pumpToggle.addEventListener("click", () => {
state.pump = !state.pump;
syncToggle(pumpToggle, state.pump);
});
let concentrationTimer;
concentration.addEventListener("input", () => {
state.concentration = Number(concentration.value);
concentrationValue.textContent = `${state.concentration} /mL`;
clearTimeout(concentrationTimer);
concentrationTimer = setTimeout(buildParticles, 120);
});
resetButton.addEventListener("click", () => {
state.atp = 100;
buildParticles();
});
function updateStats() {
let soluteInside = 0;
let soluteOutside = 0;
let cargoInside = 0;
let cargoOutside = 0;
for (const particle of particles) {
if (particle.type === "solute") {
if (particle.side === "inside") soluteInside++;
else soluteOutside++;
} else {
if (particle.side === "inside") cargoInside++;
else cargoOutside++;
}
}
soluteInsideEl.textContent = soluteInside;
soluteOutsideEl.textContent = soluteOutside;
cargoInsideEl.textContent = cargoInside;
cargoOutsideEl.textContent = cargoOutside;
const direction = netPassiveFlux >= 0 ? "into cell" : "out of cell";
fluxReadout.textContent =
`Passive net flux: ${Math.abs(netPassiveFlux)} ${direction}` +
` · Cargo pumped: ${cargoPumped}`;
const roundedATP = Math.round(state.atp);
energyValue.textContent = `${roundedATP}%`;
energyFill.style.width = `${state.atp}%`;
energyFill.style.background =
state.atp < 20 ? "#ff5470" : "#55e6ff";
}
// -------------------------------------------------------------------------
// Direct camera controls: orbit, zoom, and reset
// -------------------------------------------------------------------------
const cameraTarget = new THREE.Vector3(0.7, 0, 0);
const view = {
theta: 0.90,
phi: 1.15,
radius: 19.5,
targetTheta: 0.90,
targetPhi: 1.15,
targetRadius: 19.5
};
function updateCamera(dt) {
const smoothing = 1 - Math.pow(0.0007, dt);
view.theta += (view.targetTheta - view.theta) * smoothing;
view.phi += (view.targetPhi - view.phi) * smoothing;
view.radius += (view.targetRadius - view.radius) * smoothing;
const sinPhi = Math.sin(view.phi);
camera.position.set(
cameraTarget.x + view.radius * sinPhi * Math.cos(view.theta),
cameraTarget.y + view.radius * Math.cos(view.phi),
cameraTarget.z + view.radius * sinPhi * Math.sin(view.theta)
);
camera.lookAt(cameraTarget);
}
function resetCamera() {
view.targetTheta = 0.90;
view.targetPhi = 1.15;
view.targetRadius = 19.5;
}
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 dx = event.clientX - pointerX;
const dy = event.clientY - pointerY;
pointerX = event.clientX;
pointerY = event.clientY;
view.targetTheta -= dx * 0.006;
view.targetPhi = THREE.MathUtils.clamp(
view.targetPhi + dy * 0.006,
0.45,
1.48
);
});
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("lostpointercapture", stopDragging);
canvas.addEventListener(
"wheel",
event => {
event.preventDefault();
view.targetRadius = THREE.MathUtils.clamp(
view.targetRadius * Math.exp(event.deltaY * 0.001),
10,
29
);
},
{ passive: false }
);
canvas.addEventListener("dblclick", resetCamera);
window.addEventListener("keydown", event => {
if (event.key === "ArrowLeft") view.targetTheta += 0.12;
if (event.key === "ArrowRight") view.targetTheta -= 0.12;
if (event.key === "ArrowUp") view.targetPhi -= 0.08;
if (event.key === "ArrowDown") view.targetPhi += 0.08;
if (event.key === "r" || event.key === "R") resetCamera();
});
window.addEventListener("resize", () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
});
// -------------------------------------------------------------------------
// Animation loop
// -------------------------------------------------------------------------
const clock = new THREE.Clock();
let elapsed = 0;
let statsTimer = 0;
buildParticles();
updateCamera(1);
function animate() {
requestAnimationFrame(animate);
const dt = Math.min(clock.getDelta(), 0.033);
elapsed += dt;
updateCamera(dt);
updateParticles(dt);
updateEffects(dt);
updateATP(dt);
updateGate(
passiveAssembly,
dt,
elapsed,
state.passive
);
updateGate(
activeAssembly,
dt,
elapsed,
state.pump && state.atp > 0
);
statsTimer += dt;
if (statsTimer > 0.15) {
statsTimer = 0;
updateStats();
}
renderer.render(scene, camera);
}
animate();
</script>
</body>
</html>
```