Prompt specification
Build or customize the Three.js 3D scene "Cell interior diffusion maze" 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: Cell interior diffusion maze
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/cell-interior-diffusion-maze-x0839
Scene Description Prompt:
Make a voxel Three.js scene in a self-contained HTML file using cdnjs: cell interior diffusion maze. Compose a schematic cytoplasm with organelle obstacles and a marked release site; particles move randomly around barriers while the scene explicitly omits real molecular-scale fluid physics.
Working Three.js Starter (cell-interior-diffusion-maze-x0839):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Voxel Cytosol Diffusion Maze</title>
<style>
:root {
color-scheme: dark;
--bg: #071019;
--panel: rgba(9, 19, 31, 0.82);
--border: rgba(147, 211, 255, 0.18);
--text: #e8f5ff;
--muted: #8da6b9;
--cyan: #64d8ff;
--green: #73ffc0;
--amber: #ffac55;
--purple: #db83ff;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: var(--bg);
color: var(--text);
font-family:
Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
#scene {
display: block;
width: 100%;
height: 100%;
touch-action: none;
outline: none;
}
.header {
position: fixed;
z-index: 5;
top: 26px;
left: 30px;
width: min(470px, calc(100vw - 60px));
pointer-events: none;
text-shadow: 0 2px 20px rgba(0, 0, 0, 0.65);
}
.eyebrow {
display: flex;
align-items: center;
gap: 9px;
margin-bottom: 10px;
color: var(--green);
font-size: 11px;
font-weight: 750;
letter-spacing: 0.2em;
text-transform: uppercase;
}
.eyebrow::before {
width: 24px;
height: 1px;
content: "";
background: currentColor;
box-shadow: 0 0 10px currentColor;
}
h1 {
margin: 0;
font-size: clamp(29px, 4vw, 52px);
font-weight: 720;
line-height: 0.98;
letter-spacing: -0.045em;
}
.subtitle {
max-width: 430px;
margin: 14px 0 0;
color: #a9bfd0;
font-size: 14px;
line-height: 1.55;
}
.panel {
position: fixed;
z-index: 6;
top: 24px;
right: 24px;
width: 292px;
padding: 18px;
border: 1px solid var(--border);
border-radius: 18px;
background: var(--panel);
box-shadow:
0 22px 70px rgba(0, 0, 0, 0.4),
inset 0 1px rgba(255, 255, 255, 0.04);
backdrop-filter: blur(18px);
}
.panel-heading {
display: flex;
align-items: center;
justify-content: space-between;
margin-bottom: 15px;
}
.panel-heading strong {
font-size: 12px;
letter-spacing: 0.12em;
text-transform: uppercase;
}
.status {
display: flex;
align-items: center;
gap: 7px;
color: var(--green);
font-size: 10px;
font-weight: 700;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.status::before {
width: 7px;
height: 7px;
border-radius: 50%;
content: "";
background: currentColor;
box-shadow: 0 0 10px currentColor;
}
.control {
margin-top: 15px;
}
.control-label {
display: flex;
justify-content: space-between;
margin-bottom: 8px;
color: #b6cad9;
font-size: 11px;
letter-spacing: 0.04em;
}
output {
color: var(--text);
font-variant-numeric: tabular-nums;
}
input[type="range"] {
width: 100%;
height: 4px;
margin: 0;
border-radius: 10px;
appearance: none;
background: #203344;
accent-color: var(--cyan);
cursor: pointer;
}
input[type="range"]::-webkit-slider-thumb {
width: 14px;
height: 14px;
border: 2px solid #c8f2ff;
border-radius: 50%;
appearance: none;
background: #1e91b7;
box-shadow: 0 0 10px rgba(100, 216, 255, 0.45);
}
.buttons {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 8px;
margin-top: 17px;
}
button {
min-height: 38px;
border: 1px solid rgba(142, 204, 241, 0.22);
border-radius: 10px;
background: rgba(36, 60, 78, 0.72);
color: var(--text);
font: inherit;
font-size: 11px;
font-weight: 700;
letter-spacing: 0.04em;
cursor: pointer;
transition:
border-color 150ms ease,
background 150ms ease,
transform 150ms ease;
}
button:hover {
border-color: rgba(115, 255, 192, 0.55);
background: rgba(42, 77, 83, 0.82);
}
button:active {
transform: translateY(1px);
}
#toggle {
grid-column: 1 / -1;
border-color: rgba(115, 255, 192, 0.35);
background: rgba(37, 101, 88, 0.38);
color: #baffdf;
}
.legend {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 9px 12px;
margin-top: 17px;
padding-top: 15px;
border-top: 1px solid rgba(151, 205, 239, 0.12);
}
.legend-item {
display: flex;
align-items: center;
gap: 7px;
color: #9fb5c7;
font-size: 10px;
}
.swatch {
width: 9px;
height: 9px;
flex: 0 0 auto;
border-radius: 2px;
background: var(--color);
box-shadow: 0 0 8px color-mix(in srgb, var(--color) 65%, transparent);
}
.model-note {
margin-top: 16px;
padding: 11px 12px;
border: 1px solid rgba(255, 172, 85, 0.18);
border-radius: 10px;
background: rgba(99, 59, 18, 0.12);
color: #cbb79c;
font-size: 10px;
line-height: 1.55;
}
.model-note strong {
display: block;
margin-bottom: 3px;
color: #ffd3a0;
font-size: 9px;
letter-spacing: 0.12em;
text-transform: uppercase;
}
.world-label {
position: fixed;
z-index: 4;
top: 0;
left: 0;
display: none;
padding: 5px 8px;
border: 1px solid color-mix(in srgb, var(--label) 35%, transparent);
border-radius: 5px;
background: rgba(5, 12, 19, 0.76);
color: var(--label);
font-size: 9px;
font-weight: 750;
letter-spacing: 0.1em;
line-height: 1;
text-transform: uppercase;
white-space: nowrap;
pointer-events: none;
transform: translate(-50%, -50%);
box-shadow: 0 5px 18px rgba(0, 0, 0, 0.3);
}
.world-label::after {
position: absolute;
top: 100%;
left: 50%;
width: 1px;
height: 12px;
content: "";
background: linear-gradient(var(--label), transparent);
opacity: 0.55;
}
.footer {
position: fixed;
z-index: 4;
right: 27px;
bottom: 20px;
color: #728b9e;
font-size: 10px;
letter-spacing: 0.08em;
pointer-events: none;
}
.scale-note {
position: fixed;
z-index: 4;
bottom: 22px;
left: 30px;
color: #6f899c;
font-size: 9px;
font-weight: 700;
letter-spacing: 0.16em;
text-transform: uppercase;
pointer-events: none;
}
@media (max-width: 720px) {
.header {
top: 18px;
left: 18px;
width: calc(100vw - 36px);
}
.subtitle {
display: none;
}
.panel {
top: auto;
right: 14px;
bottom: 14px;
width: min(292px, calc(100vw - 28px));
padding: 14px;
}
.legend,
.model-note {
display: none;
}
.footer,
.scale-note {
display: none;
}
}
</style>
</head>
<body>
<canvas id="scene" aria-label="Interactive voxel cytosol diffusion maze"></canvas>
<header class="header">
<div class="eyebrow">Cell interior / schematic model</div>
<h1>Voxel cytosol<br>diffusion maze</h1>
<p class="subtitle">
Tracers wander through a voxelized cytoplasm, avoiding solid organelle
barriers before dispersing from a marked release site.
</p>
</header>
<aside class="panel">
<div class="panel-heading">
<strong>Field controls</strong>
<span class="status" id="status">Running</span>
</div>
<div class="control">
<label class="control-label" for="speed">
Random-walk speed
<output id="speedValue">1.0×</output>
</label>
<input id="speed" type="range" min="0.25" max="2.5" step="0.05" value="1">
</div>
<div class="control">
<label class="control-label" for="count">
Tracer count
<output id="countValue">180</output>
</label>
<input id="count" type="range" min="30" max="280" step="10" value="180">
</div>
<div class="buttons">
<button id="toggle" type="button">Pause tracers</button>
<button id="reset" type="button">Reset field</button>
<button id="resetView" type="button">Reset view</button>
<button id="release" type="button">Re-release</button>
</div>
<div class="legend">
<div class="legend-item">
<span class="swatch" style="--color:#78dcff"></span>
Tracers
</div>
<div class="legend-item">
<span class="swatch" style="--color:#73ffc0"></span>
Release site
</div>
<div class="legend-item">
<span class="swatch" style="--color:#ffac55"></span>
Nucleus
</div>
<div class="legend-item">
<span class="swatch" style="--color:#db83ff"></span>
Mitochondria
</div>
<div class="legend-item">
<span class="swatch" style="--color:#50dfc2"></span>
ER barrier
</div>
<div class="legend-item">
<span class="swatch" style="--color:#f3d37a"></span>
Microtubules
</div>
</div>
<div class="model-note">
<strong>Schematic model — not to scale</strong>
Random 3D walks and hard voxel exclusions only. This scene intentionally
omits real molecular-scale fluid physics, solvent drag, hydrodynamics,
diffusion equations, and molecular interactions.
</div>
</aside>
<div
class="world-label"
id="labelRelease"
style="--label:#73ffc0"
>Release site</div>
<div
class="world-label"
id="labelNucleus"
style="--label:#ffac55"
>Nucleus barrier</div>
<div
class="world-label"
id="labelMito"
style="--label:#db83ff"
>Mitochondria</div>
<div
class="world-label"
id="labelER"
style="--label:#50dfc2"
>ER barrier</div>
<div class="scale-note">Schematic / not to scale</div>
<div class="footer">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";
// -------------------------------------------------------------------------
// Renderer, scene, camera
// -------------------------------------------------------------------------
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, false);
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.08;
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x071019);
scene.fog = new THREE.FogExp2(0x071019, 0.022);
const camera = new THREE.PerspectiveCamera(
47,
window.innerWidth / window.innerHeight,
0.1,
100
);
const cameraTarget = new THREE.Vector3(0, -0.25, 0);
const cameraState = {
yaw: 0.62,
pitch: 0.48,
distance: 25
};
function updateCamera() {
const cp = Math.cos(cameraState.pitch);
const sp = Math.sin(cameraState.pitch);
camera.position.set(
cameraTarget.x + cameraState.distance * cp * Math.sin(cameraState.yaw),
cameraTarget.y + cameraState.distance * sp,
cameraTarget.z + cameraState.distance * cp * Math.cos(cameraState.yaw)
);
camera.lookAt(cameraTarget);
camera.updateMatrixWorld();
}
function resetView() {
cameraState.yaw = 0.62;
cameraState.pitch = 0.48;
cameraState.distance = 25;
cameraTarget.set(0, -0.25, 0);
updateCamera();
}
updateCamera();
// -------------------------------------------------------------------------
// Lighting
// -------------------------------------------------------------------------
scene.add(new THREE.HemisphereLight(0xc5efff, 0x071019, 2.15));
const keyLight = new THREE.DirectionalLight(0xffffff, 3.1);
keyLight.position.set(7, 13, 9);
scene.add(keyLight);
const rimLight = new THREE.DirectionalLight(0x5f8cff, 1.6);
rimLight.position.set(-9, 5, -10);
scene.add(rimLight);
const releaseLight = new THREE.PointLight(0x62ffc0, 18, 8, 2);
releaseLight.position.set(-7.2, -4.0, -3.8);
scene.add(releaseLight);
const purpleLight = new THREE.PointLight(0xce70ff, 12, 11, 2);
purpleLight.position.set(5, 3, -1.4);
scene.add(purpleLight);
// -------------------------------------------------------------------------
// Cytoplasm volume and schematic grid
// -------------------------------------------------------------------------
const chamber = {
minX: -9,
maxX: 9,
minY: -5.5,
maxY: 5.5,
minZ: -6,
maxZ: 6
};
const cytoplasmShell = new THREE.Mesh(
new THREE.BoxGeometry(18, 11, 12),
new THREE.MeshBasicMaterial({
color: 0x3199d8,
transparent: true,
opacity: 0.035,
side: THREE.BackSide,
depthWrite: false
})
);
scene.add(cytoplasmShell);
const chamberEdges = new THREE.LineSegments(
new THREE.EdgesGeometry(new THREE.BoxGeometry(18, 11, 12)),
new THREE.LineBasicMaterial({
color: 0x70cfff,
transparent: true,
opacity: 0.23
})
);
scene.add(chamberEdges);
const floor = new THREE.Mesh(
new THREE.PlaneGeometry(18, 12),
new THREE.MeshStandardMaterial({
color: 0x0b2430,
roughness: 0.94,
metalness: 0,
transparent: true,
opacity: 0.45,
side: THREE.DoubleSide
})
);
floor.rotation.x = -Math.PI / 2;
floor.position.y = -5.48;
scene.add(floor);
function createGrid(step, opacity) {
const positions = [];
for (let x = chamber.minX; x <= chamber.maxX; x += step) {
positions.push(
x, -5.44, chamber.minZ,
x, -5.44, chamber.maxZ
);
}
for (let z = chamber.minZ; z <= chamber.maxZ; z += step) {
positions.push(
chamber.minX, -5.44, z,
chamber.maxX, -5.44, z
);
}
const geometry = new THREE.BufferGeometry();
geometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(positions, 3)
);
return new THREE.LineSegments(
geometry,
new THREE.LineBasicMaterial({
color: 0x62bddd,
transparent: true,
opacity
})
);
}
scene.add(createGrid(0.5, 0.055));
scene.add(createGrid(1, 0.105));
// -------------------------------------------------------------------------
// Voxel obstacles and collider list
// -------------------------------------------------------------------------
const voxelLists = {
nucleus: [],
mitochondria: [],
er: [],
vesicle: [],
microtubule: []
};
const colliders = [];
function addBlock(type, x, y, z, sx, sy, sz) {
const block = {
x, y, z,
sx, sy, sz,
minX: x - sx * 0.5,
maxX: x + sx * 0.5,
minY: y - sy * 0.5,
maxY: y + sy * 0.5,
minZ: z - sz * 0.5,
maxZ: z + sz * 0.5
};
voxelLists[type].push(block);
colliders.push(block);
}
// Large solid voxel nucleus.
{
const cell = 0.58;
const center = new THREE.Vector3(-1.25, -0.55, -1.15);
const radii = new THREE.Vector3(2.75, 2.05, 2.2);
for (
let ix = -Math.ceil(radii.x / cell);
ix <= Math.ceil(radii.x / cell);
ix++
) {
for (
let iy = -Math.ceil(radii.y / cell);
iy <= Math.ceil(radii.y / cell);
iy++
) {
for (
let iz = -Math.ceil(radii.z / cell);
iz <= Math.ceil(radii.z / cell);
iz++
) {
const x = center.x + ix * cell;
const y = center.y + iy * cell;
const z = center.z + iz * cell;
const q =
((x - center.x) / radii.x) ** 2 +
((y - center.y) / radii.y) ** 2 +
((z - center.z) / radii.z) ** 2;
if (q <= 1.02) {
addBlock(
"nucleus",
x, y, z,
cell * 0.96,
cell * 0.96,
cell * 0.96
);
}
}
}
}
}
// Hollow voxel mitochondria.
function addMitochondrion(center, radii, cell) {
const ixMax = Math.ceil(radii.x / cell);
const iyMax = Math.ceil(radii.y / cell);
const izMax = Math.ceil(radii.z / cell);
for (let ix = -ixMax; ix <= ixMax; ix++) {
for (let iy = -iyMax; iy <= iyMax; iy++) {
for (let iz = -izMax; iz <= izMax; iz++) {
const x = center.x + ix * cell;
const y = center.y + iy * cell;
const z = center.z + iz * cell;
const outer =
((x - center.x) / radii.x) ** 2 +
((y - center.y) / radii.y) ** 2 +
((z - center.z) / radii.z) ** 2;
const inner =
((x - center.x) / (radii.x * 0.54)) ** 2 +
((y - center.y) / (radii.y * 0.54)) ** 2 +
((z - center.z) / (radii.z * 0.54)) ** 2;
if (outer <= 1 && inner >= 1) {
addBlock(
"mitochondria",
x, y, z,
cell * 0.95,
cell * 0.95,
cell * 0.95
);
}
}
}
}
}
addMitochondrion(
new THREE.Vector3(5.0, 1.55, -1.4),
new THREE.Vector3(2.3, 0.98, 1.18),
0.5
);
addMitochondrion(
new THREE.Vector3(-5.35, 2.75, 2.8),
new THREE.Vector3(1.75, 0.82, 1.02),
0.46
);
// ER-like barrier with a small opening.
{
const cell = 0.52;
for (let iz = -9; iz <= 3; iz++) {
for (let iy = -4; iy <= 4; iy++) {
const opening =
(iz === -3 || iz === -2) &&
(iy === 1 || iy === 2);
if (!opening) {
addBlock(
"er",
2.05,
iy * cell,
iz * cell,
cell * 0.96,
cell * 0.96,
cell * 0.96
);
}
}
}
// Second partial barrier.
for (let ix = 0; ix <= 5; ix++) {
for (let iy = -4; iy <= 1; iy++) {
if (ix === 2 && iy === -1) continue;
addBlock(
"er",
4.4 + ix * cell,
iy * cell,
3.55,
cell * 0.96,
cell * 0.96,
cell * 0.96
);
}
}
}
// Small vesicle clusters.
function addVesicleCluster(center, radius, count) {
for (let i = 0; i < count; i++) {
const angle = (i / count) * Math.PI * 2;
addBlock(
"vesicle",
center.x + Math.cos(angle) * radius,
center.y + Math.sin(angle * 2) * 0.22,
center.z + Math.sin(angle) * radius,
0.29,
0.29,
0.29
);
}
}
addVesicleCluster(new THREE.Vector3(6.8, -3.1, 1.8), 0.62, 8);
addVesicleCluster(new THREE.Vector3(-6.6, -2.9, 4.3), 0.54, 7);
addVesicleCluster(new THREE.Vector3(0.8, 3.6, 4.0), 0.48, 6);
// Schematic segmented microtubules.
for (let i = 0; i < 13; i++) {
addBlock(
"microtubule",
-7.2 + i * 0.52,
3.85,
-4.25,
0.38,
0.38,
0.38
);
}
for (let i = 0; i < 9; i++) {
addBlock(
"microtubule",
4.8,
-3.7,
-4.1 + i * 0.52,
0.38,
0.38,
0.38
);
}
const voxelGeometry = new THREE.BoxGeometry(1, 1, 1);
const palettes = {
nucleus: {
color: 0xffa34f,
emissive: 0x5a2205,
roughness: 0.72
},
mitochondria: {
color: 0xd879ff,
emissive: 0x39094c,
roughness: 0.62
},
er: {
color: 0x4de0bf,
emissive: 0x073d35,
roughness: 0.67
},
vesicle: {
color: 0x7fbcff,
emissive: 0x102b55,
roughness: 0.58
},
microtubule: {
color: 0xf1cf73,
emissive: 0x3a2c08,
roughness: 0.76
}
};
const dummy = new THREE.Object3D();
const color = new THREE.Color();
for (const [type, blocks] of Object.entries(voxelLists)) {
const palette = palettes[type];
const material = new THREE.MeshStandardMaterial({
color: 0xffffff,
emissive: palette.emissive,
emissiveIntensity: 0.48,
roughness: palette.roughness,
metalness: 0.04
});
const mesh = new THREE.InstancedMesh(
voxelGeometry,
material,
blocks.length
);
for (let i = 0; i < blocks.length; i++) {
const b = blocks[i];
dummy.position.set(b.x, b.y, b.z);
dummy.rotation.set(0, 0, 0);
dummy.scale.set(b.sx, b.sy, b.sz);
dummy.updateMatrix();
mesh.setMatrixAt(i, dummy.matrix);
color.setHex(palette.color);
color.offsetHSL(
(Math.random() - 0.5) * 0.025,
(Math.random() - 0.5) * 0.08,
(Math.random() - 0.5) * 0.11
);
mesh.setColorAt(i, color);
}
mesh.instanceMatrix.needsUpdate = true;
mesh.instanceColor.needsUpdate = true;
scene.add(mesh);
}
// -------------------------------------------------------------------------
// Marked release site
// -------------------------------------------------------------------------
const releasePosition = new THREE.Vector3(-7.2, -4.28, -3.8);
const releaseFloor = new THREE.Vector3(-7.2, -5.22, -3.8);
const markerMaterial = new THREE.MeshBasicMaterial({
color: 0x73ffc0,
transparent: true,
opacity: 0.86
});
const markerRingPositions = [];
const markerRingCount = 20;
for (let i = 0; i < markerRingCount; i++) {
const angle = (i / markerRingCount) * Math.PI * 2;
markerRingPositions.push(
new THREE.Vector3(
releaseFloor.x + Math.cos(angle) * 0.63,
releaseFloor.y,
releaseFloor.z + Math.sin(angle) * 0.63
)
);
}
const markerRing = new THREE.InstancedMesh(
voxelGeometry,
markerMaterial,
markerRingCount
);
markerRing.instanceMatrix.setUsage(THREE.DynamicDrawUsage);
scene.add(markerRing);
const beaconPositions = [];
const beaconCount = 9;
for (let i = 0; i < beaconCount; i++) {
beaconPositions.push(
new THREE.Vector3(
releaseFloor.x,
releaseFloor.y + 0.16 + i * 0.18,
releaseFloor.z
)
);
}
const releaseBeacon = new THREE.InstancedMesh(
voxelGeometry,
markerMaterial.clone(),
beaconCount
);
releaseBeacon.instanceMatrix.setUsage(THREE.DynamicDrawUsage);
scene.add(releaseBeacon);
function updateMarker(time) {
const pulse = 0.5 + 0.5 * Math.sin(time * 3.2);
for (let i = 0; i < markerRingCount; i++) {
const p = markerRingPositions[i];
const scale = 0.14 + pulse * 0.035;
dummy.position.copy(p);
dummy.rotation.set(0, time * 0.25, 0);
dummy.scale.set(scale, 0.075, scale);
dummy.updateMatrix();
markerRing.setMatrixAt(i, dummy.matrix);
}
markerRing.instanceMatrix.needsUpdate = true;
for (let i = 0; i < beaconCount; i++) {
const p = beaconPositions[i];
const taper = 1 - i / (beaconCount * 1.6);
const scale = (0.10 + pulse * 0.055) * taper;
dummy.position.copy(p);
dummy.rotation.set(0, time * 0.5, 0);
dummy.scale.setScalar(Math.max(scale, 0.035));
dummy.updateMatrix();
releaseBeacon.setMatrixAt(i, dummy.matrix);
}
releaseBeacon.instanceMatrix.needsUpdate = true;
releaseBeacon.material.opacity = 0.52 + pulse * 0.36;
releaseLight.intensity = 13 + pulse * 10;
}
// -------------------------------------------------------------------------
// Tracer particles
// -------------------------------------------------------------------------
const MAX_TRACERS = 280;
const particleGeometry = new THREE.BoxGeometry(1, 1, 1);
const particleMaterial = new THREE.MeshStandardMaterial({
color: 0xffffff,
emissive: 0x1c7997,
emissiveIntensity: 1.8,
roughness: 0.38,
metalness: 0.02
});
const particleMesh = new THREE.InstancedMesh(
particleGeometry,
particleMaterial,
MAX_TRACERS
);
particleMesh.instanceMatrix.setUsage(THREE.DynamicDrawUsage);
particleMesh.frustumCulled = false;
scene.add(particleMesh);
const particles = [];
for (let i = 0; i < MAX_TRACERS; i++) {
const p = {
position: releasePosition.clone(),
velocity: new THREE.Vector3(),
size: 0.12 + Math.random() * 0.07,
nextTurn: Math.random() * 0.5,
rotation: new THREE.Euler(
Math.random() * Math.PI,
Math.random() * Math.PI,
Math.random() * Math.PI
)
};
particles.push(p);
color.setHSL(
0.48 + Math.random() * 0.16,
0.82,
0.62 + Math.random() * 0.12
);
particleMesh.setColorAt(i, color);
}
particleMesh.instanceColor.needsUpdate = true;
// A brighter trajectory is retained for the first tracer.
const TRAIL_LENGTH = 90;
const trailPositions = new Float32Array(TRAIL_LENGTH * 3);
const trailHistory = [];
const trailGeometry = new THREE.BufferGeometry();
trailGeometry.setAttribute(
"position",
new THREE.BufferAttribute(trailPositions, 3)
);
trailGeometry.setDrawRange(0, 0);
const trail = new THREE.Line(
trailGeometry,
new THREE.LineBasicMaterial({
color: 0xa6f3ff,
transparent: true,
opacity: 0.5
})
);
trail.frustumCulled = false;
scene.add(trail);
let particleCount = 180;
let speedMultiplier = 1;
let running = true;
let simulationTime = 0;
function blocked(position, radius = 0.075) {
for (let i = 0; i < colliders.length; i++) {
const b = colliders[i];
if (
position.x + radius > b.minX &&
position.x - radius < b.maxX &&
position.y + radius > b.minY &&
position.y - radius < b.maxY &&
position.z + radius > b.minZ &&
position.z - radius < b.maxZ
) {
return true;
}
}
return false;
}
function randomDirection() {
const z = Math.random() * 2 - 1;
const angle = Math.random() * Math.PI * 2;
const radius = Math.sqrt(Math.max(0, 1 - z * z));
return new THREE.Vector3(
Math.cos(angle) * radius,
z,
Math.sin(angle) * radius
);
}
function randomSafePosition() {
for (let attempt = 0; attempt < 150; attempt++) {
const p = new THREE.Vector3(
chamber.minX + 0.3 + Math.random() * (chamber.maxX - chamber.minX - 0.6),
chamber.minY + 0.3 + Math.random() * (chamber.maxY - chamber.minY - 0.6),
chamber.minZ + 0.3 + Math.random() * (chamber.maxZ - chamber.minZ - 0.6)
);
if (!blocked(p, 0.12)) return p;
}
return releasePosition.clone();
}
function resetParticles() {
for (let i = 0; i < particleCount; i++) {
const p = particles[i];
p.position.copy(releasePosition).add(
new THREE.Vector3(
(Math.random() - 0.5) * 0.9,
(Math.random() - 0.5) * 0.55,
(Math.random() - 0.5) * 0.9
)
);
if (blocked(p.position, 0.1)) {
p.position.copy(randomSafePosition());
}
p.velocity.copy(randomDirection()).multiplyScalar(
0.4 + Math.random() * 0.8
);
p.nextTurn = Math.random() * 0.4;
}
trailHistory.length = 0;
trailHistory.push(particles[0].position.clone());
updateTrailGeometry();
particleMesh.count = particleCount;
}
function moveAxis(particle, axis, amount) {
const radius = particle.size * 0.42;
const before = particle.position[axis];
let after = before + amount;
if (axis === "x") {
if (after < chamber.minX + radius) {
after = chamber.minX + radius;
particle.velocity.x = Math.abs(particle.velocity.x) * 0.45;
} else if (after > chamber.maxX - radius) {
after = chamber.maxX - radius;
particle.velocity.x = -Math.abs(particle.velocity.x) * 0.45;
}
} else if (axis === "y") {
if (after < chamber.minY + radius) {
after = chamber.minY + radius;
particle.velocity.y = Math.abs(particle.velocity.y) * 0.45;
} else if (after > chamber.maxY - radius) {
after = chamber.maxY - radius;
particle.velocity.y = -Math.abs(particle.velocity.y) * 0.45;
}
} else if (axis === "z") {
if (after < chamber.minZ + radius) {
after = chamber.minZ + radius;
particle.velocity.z = Math.abs(particle.velocity.z) * 0.45;
} else if (after > chamber.maxZ - radius) {
after = chamber.maxZ - radius;
particle.velocity.z = -Math.abs(particle.velocity.z) * 0.45;
}
}
particle.position[axis] = after;
if (blocked(particle.position, radius)) {
particle.position[axis] = before;
particle.velocity[axis] *= -0.36;
particle.nextTurn = simulationTime + Math.random() * 0.08;
}
}
function updateParticles(dt) {
const damping = Math.exp(-1.55 * dt);
const maxSpeed = 3.1 * speedMultiplier;
for (let i = 0; i < particleCount; i++) {
const p = particles[i];
if (simulationTime >= p.nextTurn) {
const impulse = randomDirection().multiplyScalar(
speedMultiplier * (0.75 + Math.random() * 1.15)
);
p.velocity.multiplyScalar(0.38).add(impulse);
p.nextTurn = simulationTime + 0.12 + Math.random() * 0.38;
}
p.velocity.multiplyScalar(damping);
p.velocity.x += (Math.random() - 0.5) * 0.34 * dt * speedMultiplier;
p.velocity.y += (Math.random() - 0.5) * 0.34 * dt * speedMultiplier;
p.velocity.z += (Math.random() - 0.5) * 0.34 * dt * speedMultiplier;
const speed = p.velocity.length();
if (speed > maxSpeed) {
p.velocity.multiplyScalar(maxSpeed / speed);
}
moveAxis(p, "x", p.velocity.x * dt);
moveAxis(p, "z", p.velocity.z * dt);
moveAxis(p, "y", p.velocity.y * dt);
p.rotation.x += p.velocity.z * dt * 0.9;
p.rotation.y += p.velocity.x * dt * 0.9;
dummy.position.copy(p.position);
dummy.rotation.copy(p.rotation);
dummy.scale.setScalar(p.size);
dummy.updateMatrix();
particleMesh.setMatrixAt(i, dummy.matrix);
}
particleMesh.instanceMatrix.needsUpdate = true;
}
function updateTrailGeometry() {
for (let i = 0; i < trailHistory.length; i++) {
const p = trailHistory[i];
trailPositions[i * 3] = p.x;
trailPositions[i * 3 + 1] = p.y;
trailPositions[i * 3 + 2] = p.z;
}
trailGeometry.attributes.position.needsUpdate = true;
trailGeometry.setDrawRange(0, trailHistory.length);
}
function updateTrail() {
if (!particles.length) return;
trailHistory.unshift(particles[0].position.clone());
if (trailHistory.length > TRAIL_LENGTH) {
trailHistory.length = TRAIL_LENGTH;
}
updateTrailGeometry();
}
resetParticles();
// -------------------------------------------------------------------------
// Projected world labels
// -------------------------------------------------------------------------
const labels = [
{
element: document.querySelector("#labelRelease"),
position: new THREE.Vector3(-7.2, -2.75, -3.8)
},
{
element: document.querySelector("#labelNucleus"),
position: new THREE.Vector3(-1.25, 2.05, -1.15)
},
{
element: document.querySelector("#labelMito"),
position: new THREE.Vector3(5.0, 2.9, -1.4)
},
{
element: document.querySelector("#labelER"),
position: new THREE.Vector3(2.05, 2.75, -2.0)
}
];
const projected = new THREE.Vector3();
function updateLabels() {
const width = window.innerWidth;
const height = window.innerHeight;
for (const label of labels) {
projected.copy(label.position).project(camera);
const visible =
projected.z > -1 &&
projected.z < 1 &&
projected.x > -1.15 &&
projected.x < 1.15 &&
projected.y > -1.15 &&
projected.y < 1.15;
label.element.style.display = visible ? "block" : "none";
if (visible) {
label.element.style.left =
`${(projected.x * 0.5 + 0.5) * width}px`;
label.element.style.top =
`${(-projected.y * 0.5 + 0.5) * height}px`;
}
}
}
// -------------------------------------------------------------------------
// Controls
// -------------------------------------------------------------------------
const speedInput = document.querySelector("#speed");
const countInput = document.querySelector("#count");
const speedValue = document.querySelector("#speedValue");
const countValue = document.querySelector("#countValue");
const toggleButton = document.querySelector("#toggle");
const status = document.querySelector("#status");
speedInput.addEventListener("input", () => {
speedMultiplier = Number(speedInput.value);
speedValue.textContent = `${speedMultiplier.toFixed(2)}×`;
});
countInput.addEventListener("input", () => {
particleCount = Number(countInput.value);
countValue.textContent = String(particleCount);
resetParticles();
});
function setRunning(value) {
running = value;
toggleButton.textContent = running ? "Pause tracers" : "Resume tracers";
status.textContent = running ? "Running" : "Paused";
status.style.color = running ? "#73ffc0" : "#ffca73";
}
toggleButton.addEventListener("click", () => {
setRunning(!running);
});
document.querySelector("#reset").addEventListener("click", resetParticles);
document.querySelector("#release").addEventListener("click", resetParticles);
document.querySelector("#resetView").addEventListener("click", resetView);
// Direct camera controls; no examples/addons modules are used.
let dragging = false;
let lastPointerX = 0;
let lastPointerY = 0;
canvas.addEventListener("pointerdown", (event) => {
dragging = true;
lastPointerX = event.clientX;
lastPointerY = event.clientY;
canvas.setPointerCapture(event.pointerId);
});
canvas.addEventListener("pointermove", (event) => {
if (!dragging) return;
const dx = event.clientX - lastPointerX;
const dy = event.clientY - lastPointerY;
cameraState.yaw -= dx * 0.006;
cameraState.pitch = THREE.MathUtils.clamp(
cameraState.pitch + dy * 0.005,
-0.18,
1.18
);
lastPointerX = event.clientX;
lastPointerY = event.clientY;
updateCamera();
});
function stopDragging(event) {
dragging = false;
if (event.pointerId !== undefined && canvas.hasPointerCapture(event.pointerId)) {
canvas.releasePointerCapture(event.pointerId);
}
}
canvas.addEventListener("pointerup", stopDragging);
canvas.addEventListener("pointercancel", stopDragging);
canvas.addEventListener("contextmenu", (event) => event.preventDefault());
canvas.addEventListener(
"wheel",
(event) => {
event.preventDefault();
cameraState.distance = THREE.MathUtils.clamp(
cameraState.distance + event.deltaY * 0.012,
13,
38
);
updateCamera();
},
{ passive: false }
);
canvas.addEventListener("dblclick", resetView);
window.addEventListener("resize", () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight, false);
});
// -------------------------------------------------------------------------
// Animation loop
// -------------------------------------------------------------------------
const clock = new THREE.Clock();
let frame = 0;
function animate() {
requestAnimationFrame(animate);
const dt = Math.min(clock.getDelta(), 0.033);
const visualTime = clock.elapsedTime;
if (running) {
simulationTime += dt;
updateParticles(dt);
if (frame % 2 === 0) {
updateTrail();
}
}
updateMarker(visualTime);
updateLabels();
frame++;
renderer.render(scene, camera);
}
animate();
</script>
</body>
</html>
```