Prompt specification
Build or customize the Three.js 3D scene "Random diffusion versus directed transport" 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: Random diffusion versus directed transport
Category: Biology & Nature · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/random-diffusion-versus-directed-transport-x0834
Scene Description Prompt:
Compare stylized random diffusion with track-guided cargo transport in adjacent transparent chambers, using illustrative animation speeds. One HTML file with Three.js from cdnjs.
Working Three.js Starter (random-diffusion-versus-directed-transport-x0834):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Diffusion vs. Guided Transport</title>
<style>
:root {
color-scheme: dark;
--cyan: #60dcff;
--cyan-soft: rgba(96, 220, 255, 0.15);
--amber: #ffb454;
--amber-soft: rgba(255, 180, 84, 0.15);
--text: #eaf5ff;
--muted: #91a7bb;
--panel: rgba(7, 16, 28, 0.72);
--border: rgba(158, 206, 235, 0.17);
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background:
radial-gradient(circle at 50% 15%, #11243a 0%, #08111d 43%, #050a12 100%);
color: var(--text);
font-family: Inter, ui-sans-serif, system-ui, -apple-system,
BlinkMacSystemFont, "Segoe UI", sans-serif;
}
#scene {
position: fixed;
inset: 0;
}
#scene canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
cursor: grab;
}
#scene canvas:active {
cursor: grabbing;
}
.ui {
position: fixed;
inset: 0;
z-index: 2;
pointer-events: none;
}
.topbar {
position: absolute;
top: 22px;
left: 26px;
right: 26px;
display: flex;
align-items: flex-start;
justify-content: space-between;
gap: 24px;
}
h1 {
margin: 0 0 6px;
font-size: clamp(23px, 2.5vw, 38px);
line-height: 1.05;
letter-spacing: -0.04em;
font-weight: 750;
}
.subtitle {
margin: 0;
max-width: 620px;
color: var(--muted);
font-size: 13px;
line-height: 1.55;
}
.live {
display: inline-flex;
align-items: center;
gap: 8px;
flex: none;
padding: 8px 11px;
border: 1px solid var(--border);
border-radius: 999px;
background: rgba(6, 14, 24, 0.66);
color: #bad1e1;
font-size: 10px;
font-weight: 750;
letter-spacing: 0.13em;
text-transform: uppercase;
backdrop-filter: blur(12px);
}
.live-dot {
width: 7px;
height: 7px;
border-radius: 50%;
background: #62f6ae;
box-shadow: 0 0 12px #62f6ae;
animation: pulse 1.8s ease-in-out infinite;
}
@keyframes pulse {
50% {
opacity: 0.4;
transform: scale(0.75);
}
}
.chamber-cards {
position: absolute;
top: 104px;
left: 50%;
width: min(850px, calc(100% - 52px));
transform: translateX(-50%);
display: grid;
grid-template-columns: 1fr 1fr;
gap: 14px;
}
.chamber-card {
position: relative;
overflow: hidden;
padding: 13px 16px 12px;
border: 1px solid var(--border);
border-radius: 13px;
background: linear-gradient(145deg, rgba(11, 24, 39, 0.83), rgba(6, 14, 24, 0.6));
backdrop-filter: blur(12px);
box-shadow: 0 12px 40px rgba(0, 0, 0, 0.16);
}
.chamber-card::before {
content: "";
position: absolute;
inset: 0 auto 0 0;
width: 3px;
background: var(--accent);
box-shadow: 0 0 18px var(--accent);
}
.diffusion {
--accent: var(--cyan);
--accent-soft: var(--cyan-soft);
}
.guided {
--accent: var(--amber);
--accent-soft: var(--amber-soft);
}
.eyebrow {
display: flex;
align-items: center;
justify-content: space-between;
gap: 12px;
margin-bottom: 5px;
color: var(--accent);
font-size: 9px;
font-weight: 800;
letter-spacing: 0.14em;
text-transform: uppercase;
}
.speed-badge {
flex: none;
padding: 3px 6px;
border: 1px solid color-mix(in srgb, var(--accent) 36%, transparent);
border-radius: 5px;
background: var(--accent-soft);
letter-spacing: 0.06em;
white-space: nowrap;
}
.chamber-card h2 {
margin: 0 0 4px;
font-size: 17px;
letter-spacing: -0.02em;
}
.chamber-card p {
margin: 0;
color: var(--muted);
font-size: 11px;
line-height: 1.5;
}
.bottom-left {
position: absolute;
left: 26px;
bottom: 24px;
width: min(410px, calc(100% - 52px));
padding: 13px 15px;
border: 1px solid var(--border);
border-radius: 12px;
background: var(--panel);
backdrop-filter: blur(14px);
}
.comparison {
display: grid;
grid-template-columns: 1fr auto 1fr;
align-items: center;
gap: 12px;
}
.comparison-item {
min-width: 0;
}
.comparison-item strong {
display: block;
margin-bottom: 3px;
font-size: 11px;
}
.comparison-item span {
display: block;
color: var(--muted);
font-size: 10px;
line-height: 1.35;
}
.comparison-arrow {
color: #557086;
font-size: 20px;
}
.fine-print {
margin-top: 10px;
padding-top: 9px;
border-top: 1px solid rgba(158, 206, 235, 0.1);
color: #6f879a;
font-size: 9px;
line-height: 1.45;
}
.controls {
position: absolute;
right: 26px;
bottom: 24px;
display: flex;
align-items: center;
gap: 8px;
padding: 9px;
border: 1px solid var(--border);
border-radius: 12px;
background: var(--panel);
pointer-events: auto;
backdrop-filter: blur(14px);
}
button {
height: 34px;
padding: 0 12px;
border: 1px solid rgba(158, 206, 235, 0.18);
border-radius: 8px;
background: rgba(23, 42, 59, 0.82);
color: #d9eaf6;
font: inherit;
font-size: 11px;
font-weight: 650;
cursor: pointer;
transition:
background 150ms ease,
border-color 150ms ease,
transform 150ms ease;
}
button:hover {
background: rgba(37, 64, 86, 0.95);
border-color: rgba(158, 206, 235, 0.34);
}
button:active {
transform: translateY(1px);
}
.speed-control {
display: flex;
align-items: center;
gap: 8px;
padding: 0 6px;
color: var(--muted);
font-size: 10px;
}
input[type="range"] {
width: 92px;
accent-color: #67d9f7;
cursor: pointer;
}
#speedValue {
width: 32px;
color: #dcecf6;
font-variant-numeric: tabular-nums;
text-align: right;
}
.camera-hint {
position: absolute;
right: 27px;
bottom: 78px;
color: rgba(161, 184, 200, 0.62);
font-size: 9px;
letter-spacing: 0.04em;
pointer-events: none;
}
@media (max-width: 900px) {
.topbar {
top: 16px;
left: 18px;
right: 18px;
}
.subtitle {
max-width: 460px;
}
.chamber-cards {
top: 94px;
width: calc(100% - 36px);
gap: 9px;
}
.chamber-card {
padding: 10px 12px;
}
.chamber-card p {
display: none;
}
.bottom-left {
left: 18px;
bottom: 18px;
}
.controls {
right: 18px;
bottom: 18px;
}
.camera-hint {
right: 19px;
bottom: 70px;
}
}
@media (max-width: 650px) {
.live,
.comparison-arrow,
.comparison-item span,
.camera-hint {
display: none;
}
.chamber-cards {
grid-template-columns: 1fr 1fr;
}
.eyebrow {
font-size: 8px;
}
.speed-badge {
display: none;
}
.chamber-card h2 {
font-size: 14px;
}
.bottom-left {
width: calc(100% - 36px);
bottom: 76px;
}
.controls {
left: 18px;
right: 18px;
justify-content: center;
}
}
</style>
</head>
<body>
<div id="scene"></div>
<div class="ui">
<header class="topbar">
<div>
<h1>Diffusion vs. guided transport</h1>
<p class="subtitle">
Random motion on the left. Track-constrained cargo on the right.
Drag to orbit and scroll to zoom.
</p>
</div>
<div class="live">
<span class="live-dot"></span>
Live simulation
</div>
</header>
<section class="chamber-cards" aria-label="Simulation comparison">
<article class="chamber-card diffusion">
<div class="eyebrow">
<span>A · Stochastic diffusion</span>
<span class="speed-badge">≈ 0.6 u/s</span>
</div>
<h2>Random walk</h2>
<p>
Independent particles change direction constantly, producing broad,
indirect movement through the chamber.
</p>
</article>
<article class="chamber-card guided">
<div class="eyebrow">
<span>B · Track-guided cargo</span>
<span class="speed-badge">≈ 1.2 u/s</span>
</div>
<h2>Fixed route</h2>
<p>
Every crate follows the same guide rail, moving predictably toward
the delivery gate.
</p>
</article>
</section>
<aside class="bottom-left">
<div class="comparison">
<div class="comparison-item">
<strong style="color: var(--cyan)">Scatter & wander</strong>
<span>Many possible paths</span>
</div>
<div class="comparison-arrow">→</div>
<div class="comparison-item">
<strong style="color: var(--amber)">Route & deliver</strong>
<span>One constrained path</span>
</div>
</div>
<div class="fine-print">
Animation speeds and distances are illustrative, not a physical
benchmark. The chamber dimensions are also scaled for legibility.
</div>
</aside>
<div class="camera-hint">Drag to orbit · Scroll to zoom · Double-click to reset view</div>
<div class="controls" aria-label="Simulation controls">
<button id="pauseButton" type="button">Pause</button>
<label class="speed-control" for="speedSlider">
Speed
<input
id="speedSlider"
type="range"
min="0.5"
max="1.8"
step="0.1"
value="1"
/>
<span id="speedValue">1.0×</span>
</label>
<button id="resetButton" type="button">Reset</button>
<button id="viewButton" type="button">Reset view</button>
</div>
</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// -------------------------------------------------------------------------
// Renderer and scene
// -------------------------------------------------------------------------
const sceneHost = document.querySelector("#scene");
const renderer = new THREE.WebGLRenderer({
antialias: true,
alpha: 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.12;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.domElement.setAttribute(
"aria-label",
"Two transparent chambers comparing random diffusion with guided cargo transport"
);
sceneHost.appendChild(renderer.domElement);
const scene = new THREE.Scene();
scene.fog = new THREE.Fog(0x07111d, 23, 43);
const camera = new THREE.PerspectiveCamera(
42,
window.innerWidth / window.innerHeight,
0.1,
80
);
const hemisphere = new THREE.HemisphereLight(0xb9eaff, 0x101521, 2.1);
scene.add(hemisphere);
const keyLight = new THREE.DirectionalLight(0xffffff, 2.7);
keyLight.position.set(4, 11, 8);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(1024, 1024);
keyLight.shadow.camera.left = -11;
keyLight.shadow.camera.right = 11;
keyLight.shadow.camera.top = 8;
keyLight.shadow.camera.bottom = -8;
keyLight.shadow.bias = -0.0008;
scene.add(keyLight);
const cyanLight = new THREE.PointLight(0x39cfff, 24, 18, 2);
cyanLight.position.set(-8, 4, 5);
scene.add(cyanLight);
const amberLight = new THREE.PointLight(0xff9f35, 26, 18, 2);
amberLight.position.set(8, 4, 5);
scene.add(amberLight);
// -------------------------------------------------------------------------
// Shared dimensions and helpers
// -------------------------------------------------------------------------
const CHAMBER_WIDTH = 7.2;
const CHAMBER_DEPTH = 5.4;
const CHAMBER_HEIGHT = 4.4;
const LEFT_CENTER = -3.85;
const RIGHT_CENTER = 3.85;
const CYAN = 0x60dcff;
const AMBER = 0xffb454;
function createGrid(width, depth, color) {
const positions = [];
const segmentsX = 12;
const segmentsZ = 9;
for (let i = 0; i <= segmentsX; i++) {
const x = -width / 2 + (i / segmentsX) * width;
positions.push(x, 0.012, -depth / 2, x, 0.012, depth / 2);
}
for (let i = 0; i <= segmentsZ; i++) {
const z = -depth / 2 + (i / segmentsZ) * depth;
positions.push(-width / 2, 0.012, z, width / 2, 0.012, z);
}
const geometry = new THREE.BufferGeometry();
geometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(positions, 3)
);
const material = new THREE.LineBasicMaterial({
color,
transparent: true,
opacity: 0.13,
depthWrite: false
});
return new THREE.LineSegments(geometry, material);
}
function roundedRect(ctx, x, y, width, height, radius) {
ctx.beginPath();
ctx.moveTo(x + radius, y);
ctx.lineTo(x + width - radius, y);
ctx.quadraticCurveTo(x + width, y, x + width, y + radius);
ctx.lineTo(x + width, y + height - radius);
ctx.quadraticCurveTo(
x + width,
y + height,
x + width - radius,
y + height
);
ctx.lineTo(x + radius, y + height);
ctx.quadraticCurveTo(x, y + height, x, y + height - radius);
ctx.lineTo(x, y + radius);
ctx.quadraticCurveTo(x, y, x + radius, y);
ctx.closePath();
}
function createLabelSprite(text, color) {
const canvas = document.createElement("canvas");
canvas.width = 512;
canvas.height = 112;
const ctx = canvas.getContext("2d");
ctx.clearRect(0, 0, canvas.width, canvas.height);
roundedRect(ctx, 8, 8, 496, 96, 18);
ctx.fillStyle = "rgba(4, 12, 22, 0.86)";
ctx.fill();
ctx.strokeStyle = color;
ctx.lineWidth = 3;
ctx.stroke();
ctx.fillStyle = color;
ctx.font = "700 31px system-ui, sans-serif";
ctx.textAlign = "center";
ctx.textBaseline = "middle";
ctx.fillText(text, 256, 57);
const texture = new THREE.CanvasTexture(canvas);
texture.colorSpace = THREE.SRGBColorSpace;
const material = new THREE.SpriteMaterial({
map: texture,
transparent: true,
depthTest: false,
depthWrite: false
});
const sprite = new THREE.Sprite(material);
sprite.scale.set(2.45, 0.54, 1);
sprite.renderOrder = 20;
return sprite;
}
function createChamber(centerX, accent) {
const group = new THREE.Group();
group.position.x = centerX;
scene.add(group);
const floorMaterial = new THREE.MeshStandardMaterial({
color: 0x0a1725,
roughness: 0.76,
metalness: 0.18,
transparent: true,
opacity: 0.94
});
const floor = new THREE.Mesh(
new THREE.BoxGeometry(
CHAMBER_WIDTH,
0.14,
CHAMBER_DEPTH
),
floorMaterial
);
floor.position.y = -0.07;
floor.receiveShadow = true;
group.add(floor);
group.add(createGrid(CHAMBER_WIDTH, CHAMBER_DEPTH, accent));
const glassMaterial = new THREE.MeshPhysicalMaterial({
color: accent,
roughness: 0.18,
metalness: 0.02,
transparent: true,
opacity: 0.055,
side: THREE.DoubleSide,
depthWrite: false
});
const glass = new THREE.Mesh(
new THREE.BoxGeometry(
CHAMBER_WIDTH,
CHAMBER_HEIGHT,
CHAMBER_DEPTH
),
glassMaterial
);
glass.position.y = CHAMBER_HEIGHT / 2;
glass.renderOrder = 5;
group.add(glass);
const edgeMaterial = new THREE.LineBasicMaterial({
color: accent,
transparent: true,
opacity: 0.48
});
const edges = new THREE.LineSegments(
new THREE.EdgesGeometry(
new THREE.BoxGeometry(
CHAMBER_WIDTH,
CHAMBER_HEIGHT,
CHAMBER_DEPTH
)
),
edgeMaterial
);
edges.position.y = CHAMBER_HEIGHT / 2;
group.add(edges);
const postMaterial = new THREE.MeshStandardMaterial({
color: 0x172b3d,
roughness: 0.42,
metalness: 0.7,
emissive: accent,
emissiveIntensity: 0.32
});
const postGeometry = new THREE.BoxGeometry(0.055, CHAMBER_HEIGHT, 0.055);
for (const x of [-CHAMBER_WIDTH / 2, CHAMBER_WIDTH / 2]) {
for (const z of [-CHAMBER_DEPTH / 2, CHAMBER_DEPTH / 2]) {
const post = new THREE.Mesh(postGeometry, postMaterial);
post.position.set(x, CHAMBER_HEIGHT / 2, z);
group.add(post);
}
}
const stripMaterial = new THREE.MeshBasicMaterial({
color: accent,
transparent: true,
opacity: 0.7
});
for (const z of [-CHAMBER_DEPTH / 2, CHAMBER_DEPTH / 2]) {
const strip = new THREE.Mesh(
new THREE.BoxGeometry(CHAMBER_WIDTH, 0.025, 0.025),
stripMaterial
);
strip.position.set(0, 0.025, z);
group.add(strip);
}
return group;
}
const diffusionChamber = createChamber(LEFT_CENTER, CYAN);
const guidedChamber = createChamber(RIGHT_CENTER, AMBER);
const diffusionLabel = createLabelSprite("RANDOM WALK", "#60dcff");
diffusionLabel.position.set(0, 3.95, -2.55);
diffusionChamber.add(diffusionLabel);
const guidedLabel = createLabelSprite("GUIDED TRACK", "#ffb454");
guidedLabel.position.set(0, 3.95, -2.55);
guidedChamber.add(guidedLabel);
// -------------------------------------------------------------------------
// Left chamber: stochastic random walkers
// -------------------------------------------------------------------------
const PARTICLE_COUNT = 105;
const DIFFUSION_SPEED = 0.6;
const particleGeometry = new THREE.IcosahedronGeometry(0.085, 0);
const particleMaterial = new THREE.MeshStandardMaterial({
color: 0xffffff,
roughness: 0.42,
metalness: 0.22,
emissive: 0x16465a,
emissiveIntensity: 0.4,
vertexColors: true
});
const particleMesh = new THREE.InstancedMesh(
particleGeometry,
particleMaterial,
PARTICLE_COUNT
);
particleMesh.instanceMatrix.setUsage(THREE.DynamicDrawUsage);
particleMesh.frustumCulled = false;
particleMesh.castShadow = true;
diffusionChamber.add(particleMesh);
const particleGlowPositions = new Float32Array(PARTICLE_COUNT * 3);
const particleGlowGeometry = new THREE.BufferGeometry();
particleGlowGeometry.setAttribute(
"position",
new THREE.BufferAttribute(particleGlowPositions, 3)
);
const particleGlow = new THREE.Points(
particleGlowGeometry,
new THREE.PointsMaterial({
color: 0x8be8ff,
size: 0.19,
transparent: true,
opacity: 0.2,
depthWrite: false,
blending: THREE.AdditiveBlending
})
);
particleGlow.frustumCulled = false;
diffusionChamber.add(particleGlow);
const particles = [];
function randomParticleState(particle) {
particle.position.set(
THREE.MathUtils.randFloatSpread(CHAMBER_WIDTH - 0.65),
THREE.MathUtils.randFloat(0.28, CHAMBER_HEIGHT - 0.35),
THREE.MathUtils.randFloatSpread(CHAMBER_DEPTH - 0.65)
);
particle.velocity.set(
THREE.MathUtils.randFloatSpread(0.8),
THREE.MathUtils.randFloatSpread(0.55),
THREE.MathUtils.randFloatSpread(0.8)
);
if (particle.velocity.lengthSq() < 0.03) {
particle.velocity.set(0.3, 0.1, -0.2);
}
}
for (let i = 0; i < PARTICLE_COUNT; i++) {
const particle = {
position: new THREE.Vector3(),
velocity: new THREE.Vector3(),
rotationSpeed: THREE.MathUtils.randFloatSpread(2.5)
};
randomParticleState(particle);
particles.push(particle);
const hue = THREE.MathUtils.lerp(0.48, 0.58, Math.random());
const color = new THREE.Color().setHSL(hue, 0.9, 0.66);
particleMesh.setColorAt(i, color);
}
particleMesh.instanceColor.needsUpdate = true;
const particleDummy = new THREE.Object3D();
function updateParticles(delta) {
const xLimit = CHAMBER_WIDTH / 2 - 0.22;
const yMin = 0.22;
const yMax = CHAMBER_HEIGHT - 0.22;
const zLimit = CHAMBER_DEPTH / 2 - 0.22;
for (let i = 0; i < PARTICLE_COUNT; i++) {
const particle = particles[i];
// Random acceleration with damping creates a stylized Brownian walk.
particle.velocity.x += (Math.random() - 0.5) * 5.2 * delta;
particle.velocity.y += (Math.random() - 0.5) * 3.4 * delta;
particle.velocity.z += (Math.random() - 0.5) * 5.2 * delta;
particle.velocity.multiplyScalar(Math.exp(-1.2 * delta));
const speed = particle.velocity.length();
if (speed > DIFFUSION_SPEED) {
particle.velocity.multiplyScalar(DIFFUSION_SPEED / speed);
} else if (speed < 0.16) {
particle.velocity.set(
THREE.MathUtils.randFloatSpread(0.4),
THREE.MathUtils.randFloatSpread(0.25),
THREE.MathUtils.randFloatSpread(0.4)
);
}
particle.position.addScaledVector(particle.velocity, delta);
if (particle.position.x < -xLimit) {
particle.position.x = -xLimit;
particle.velocity.x = Math.abs(particle.velocity.x);
} else if (particle.position.x > xLimit) {
particle.position.x = xLimit;
particle.velocity.x = -Math.abs(particle.velocity.x);
}
if (particle.position.y < yMin) {
particle.position.y = yMin;
particle.velocity.y = Math.abs(particle.velocity.y);
} else if (particle.position.y > yMax) {
particle.position.y = yMax;
particle.velocity.y = -Math.abs(particle.velocity.y);
}
if (particle.position.z < -zLimit) {
particle.position.z = -zLimit;
particle.velocity.z = Math.abs(particle.velocity.z);
} else if (particle.position.z > zLimit) {
particle.position.z = zLimit;
particle.velocity.z = -Math.abs(particle.velocity.z);
}
particleDummy.position.copy(particle.position);
particleDummy.rotation.set(
elapsed * particle.rotationSpeed,
elapsed * particle.rotationSpeed * 0.7,
elapsed * particle.rotationSpeed * 0.4
);
particleDummy.scale.setScalar(0.8 + Math.random() * 0.35);
particleDummy.updateMatrix();
particleMesh.setMatrixAt(i, particleDummy.matrix);
const offset = i * 3;
particleGlowPositions[offset] = particle.position.x;
particleGlowPositions[offset + 1] = particle.position.y;
particleGlowPositions[offset + 2] = particle.position.z;
}
particleMesh.instanceMatrix.needsUpdate = true;
particleGlowGeometry.attributes.position.needsUpdate = true;
}
// -------------------------------------------------------------------------
// Right chamber: guide track and cargo
// -------------------------------------------------------------------------
const TRACK_SPEED = 1.2;
const trackPoints = [
new THREE.Vector3(-2.75, 0.18, -1.65),
new THREE.Vector3(-1.35, 0.18, -1.75),
new THREE.Vector3(-0.75, 0.18, -0.75),
new THREE.Vector3(-1.35, 0.18, 0.45),
new THREE.Vector3(-2.25, 0.18, 1.55),
new THREE.Vector3(0.0, 0.18, 1.75),
new THREE.Vector3(2.25, 0.18, 1.1),
new THREE.Vector3(2.75, 0.18, -0.25),
new THREE.Vector3(2.05, 0.18, -1.6),
new THREE.Vector3(0.35, 0.18, -1.25),
new THREE.Vector3(0.95, 0.18, -0.25)
];
const trackCurve = new THREE.CatmullRomCurve3(
trackPoints,
true,
"centripetal"
);
const trackLength = trackCurve.getLength();
const trackBase = new THREE.Mesh(
new THREE.TubeGeometry(trackCurve, 260, 0.105, 8, true),
new THREE.MeshStandardMaterial({
color: 0x152a3c,
roughness: 0.36,
metalness: 0.78
})
);
guidedChamber.add(trackBase);
const trackCore = new THREE.Mesh(
new THREE.TubeGeometry(trackCurve, 260, 0.043, 8, true),
new THREE.MeshStandardMaterial({
color: AMBER,
emissive: 0xff8b21,
emissiveIntensity: 1.7,
roughness: 0.28,
metalness: 0.42
})
);
guidedChamber.add(trackCore);
const tieMaterial = new THREE.MeshStandardMaterial({
color: 0x4c6171,
roughness: 0.5,
metalness: 0.6
});
const tieGeometry = new THREE.BoxGeometry(0.12, 0.035, 0.46);
for (let i = 0; i < 76; i++) {
const t = i / 76;
const point = trackCurve.getPointAt(t);
const tangent = trackCurve.getTangentAt(t);
const tie = new THREE.Mesh(tieGeometry, tieMaterial);
tie.position.copy(point);
tie.position.y = 0.105;
tie.rotation.y = Math.atan2(-tangent.z, tangent.x);
guidedChamber.add(tie);
}
const flowArrows = [];
const arrowGeometry = new THREE.ConeGeometry(0.095, 0.28, 6);
const arrowMaterial = new THREE.MeshStandardMaterial({
color: 0xffd19a,
emissive: 0xff9f35,
emissiveIntensity: 1.2,
roughness: 0.35
});
const upVector = new THREE.Vector3(0, 1, 0);
for (let i = 0; i < 13; i++) {
const arrow = new THREE.Mesh(arrowGeometry, arrowMaterial);
guidedChamber.add(arrow);
flowArrows.push(arrow);
}
const cargoMaterial = new THREE.MeshStandardMaterial({
color: 0xf39a35,
roughness: 0.43,
metalness: 0.12,
emissive: 0x5b2604,
emissiveIntensity: 0.32
});
const cargoBaseMaterial = new THREE.MeshStandardMaterial({
color: 0x233746,
roughness: 0.36,
metalness: 0.72
});
const strapMaterial = new THREE.MeshStandardMaterial({
color: 0xffe2a8,
roughness: 0.4,
metalness: 0.3,
emissive: 0x8f4a08,
emissiveIntensity: 0.25
});
const ledMaterial = new THREE.MeshBasicMaterial({
color: 0x9dff9c
});
function createCargo() {
const cargo = new THREE.Group();
const base = new THREE.Mesh(
new THREE.BoxGeometry(0.76, 0.12, 0.56),
cargoBaseMaterial
);
base.position.y = 0.13;
base.castShadow = true;
cargo.add(base);
const box = new THREE.Mesh(
new THREE.BoxGeometry(0.57, 0.42, 0.45),
cargoMaterial
);
box.position.y = 0.4;
box.castShadow = true;
cargo.add(box);
const strapA = new THREE.Mesh(
new THREE.BoxGeometry(0.08, 0.435, 0.47),
strapMaterial
);
strapA.position.y = 0.405;
cargo.add(strapA);
const strapB = new THREE.Mesh(
new THREE.BoxGeometry(0.59, 0.435, 0.065),
strapMaterial
);
strapB.position.y = 0.405;
cargo.add(strapB);
const led = new THREE.Mesh(
new THREE.SphereGeometry(0.035, 8, 8),
ledMaterial
);
led.position.set(0.24, 0.63, 0.19);
cargo.add(led);
return cargo;
}
const cargoCount = 7;
const cargoUnits = [];
for (let i = 0; i < cargoCount; i++) {
const cargo = createCargo();
guidedChamber.add(cargo);
cargoUnits.push(cargo);
}
// Delivery gate.
const deliveryT = 0.58;
const deliveryPoint = trackCurve.getPointAt(deliveryT);
const deliveryTangent = trackCurve.getTangentAt(deliveryT);
const deliveryGate = new THREE.Group();
deliveryGate.position.copy(deliveryPoint);
deliveryGate.rotation.y = Math.atan2(-deliveryTangent.z, deliveryTangent.x);
guidedChamber.add(deliveryGate);
const gateMaterial = new THREE.MeshStandardMaterial({
color: 0x1b4037,
roughness: 0.38,
metalness: 0.62,
emissive: 0x2bc789,
emissiveIntensity: 0.7
});
for (const z of [-0.55, 0.55]) {
const post = new THREE.Mesh(
new THREE.BoxGeometry(0.12, 1.45, 0.12),
gateMaterial
);
post.position.set(0, 0.72, z);
deliveryGate.add(post);
}
const gateTop = new THREE.Mesh(
new THREE.BoxGeometry(0.12, 0.12, 1.22),
gateMaterial
);
gateTop.position.set(0, 1.43, 0);
deliveryGate.add(gateTop);
const deliveryRingMaterial = new THREE.MeshBasicMaterial({
color: 0x65ffb1,
transparent: true,
opacity: 0.78,
depthWrite: false
});
const deliveryRing = new THREE.Mesh(
new THREE.TorusGeometry(0.5, 0.025, 8, 48),
deliveryRingMaterial
);
deliveryRing.position.set(0, 0.48, 0);
deliveryRing.rotation.y = Math.PI / 2;
deliveryGate.add(deliveryRing);
const deliveryLabel = createLabelSprite("DELIVERY", "#7dffb7");
deliveryLabel.position.set(0, 1.85, 0);
deliveryLabel.scale.set(1.55, 0.34, 1);
deliveryGate.add(deliveryLabel);
// Dispatch pad.
const dispatchPoint = trackCurve.getPointAt(0);
const dispatchPad = new THREE.Mesh(
new THREE.TorusGeometry(0.42, 0.025, 8, 36),
new THREE.MeshBasicMaterial({
color: 0xffc16f,
transparent: true,
opacity: 0.65
})
);
dispatchPad.position.copy(dispatchPoint);
dispatchPad.position.y = 0.035;
dispatchPad.rotation.x = Math.PI / 2;
guidedChamber.add(dispatchPad);
let trackPhase = 0;
function updateTrack(delta) {
trackPhase =
(trackPhase + (delta * TRACK_SPEED) / trackLength) % 1;
for (let i = 0; i < cargoUnits.length; i++) {
const cargo = cargoUnits[i];
const t = (trackPhase + i / cargoUnits.length) % 1;
const point = trackCurve.getPointAt(t);
const tangent = trackCurve.getTangentAt(t);
cargo.position.copy(point);
cargo.position.y += 0.025;
cargo.rotation.y = Math.atan2(-tangent.z, tangent.x);
}
for (let i = 0; i < flowArrows.length; i++) {
const arrow = flowArrows[i];
const t =
(i / flowArrows.length + elapsed * 0.055) % 1;
const point = trackCurve.getPointAt(t);
const tangent = trackCurve.getTangentAt(t);
arrow.position.copy(point);
arrow.position.y += 0.095;
arrow.quaternion.setFromUnitVectors(upVector, tangent.clone().normalize());
}
const pulse = 1 + Math.sin(elapsed * 3.2) * 0.035;
deliveryRing.scale.setScalar(pulse);
deliveryRingMaterial.opacity = 0.68 + Math.sin(elapsed * 3.2) * 0.12;
}
// -------------------------------------------------------------------------
// Ambient background dust
// -------------------------------------------------------------------------
const dustCount = 190;
const dustPositions = new Float32Array(dustCount * 3);
for (let i = 0; i < dustCount; i++) {
dustPositions[i * 3] = THREE.MathUtils.randFloatSpread(25);
dustPositions[i * 3 + 1] = THREE.MathUtils.randFloat(0.5, 11);
dustPositions[i * 3 + 2] = THREE.MathUtils.randFloat(-9, 4);
}
const dustGeometry = new THREE.BufferGeometry();
dustGeometry.setAttribute(
"position",
new THREE.BufferAttribute(dustPositions, 3)
);
const dust = new THREE.Points(
dustGeometry,
new THREE.PointsMaterial({
color: 0x8db6ce,
size: 0.025,
transparent: true,
opacity: 0.3,
depthWrite: false
})
);
scene.add(dust);
// -------------------------------------------------------------------------
// Camera controls implemented directly, without camera-control addons
// -------------------------------------------------------------------------
const cameraState = {
theta: 0,
phi: 1.02,
radius: 19.2,
target: new THREE.Vector3(0, 1.45, 0)
};
const defaultCamera = {
theta: 0,
phi: 1.02,
radius: 19.2
};
function applyCamera() {
const sinPhi = Math.sin(cameraState.phi);
const x =
cameraState.target.x +
cameraState.radius * sinPhi * Math.sin(cameraState.theta);
const y =
cameraState.target.y +
cameraState.radius * Math.cos(cameraState.phi);
const z =
cameraState.target.z +
cameraState.radius * sinPhi * Math.cos(cameraState.theta);
camera.position.set(x, y, z);
camera.lookAt(cameraState.target);
}
let dragging = false;
let pointerX = 0;
let pointerY = 0;
renderer.domElement.addEventListener("pointerdown", (event) => {
dragging = true;
pointerX = event.clientX;
pointerY = event.clientY;
renderer.domElement.setPointerCapture(event.pointerId);
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (!dragging) return;
const dx = event.clientX - pointerX;
const dy = event.clientY - pointerY;
pointerX = event.clientX;
pointerY = event.clientY;
cameraState.theta -= dx * 0.006;
cameraState.phi = THREE.MathUtils.clamp(
cameraState.phi - dy * 0.005,
0.48,
1.36
);
applyCamera();
});
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(
"wheel",
(event) => {
event.preventDefault();
cameraState.radius = THREE.MathUtils.clamp(
cameraState.radius + event.deltaY * 0.012,
12.5,
28
);
applyCamera();
},
{ passive: false }
);
function resetCamera() {
cameraState.theta = defaultCamera.theta;
cameraState.phi = defaultCamera.phi;
cameraState.radius = defaultCamera.radius;
applyCamera();
}
renderer.domElement.addEventListener("dblclick", resetCamera);
applyCamera();
// -------------------------------------------------------------------------
// UI controls
// -------------------------------------------------------------------------
const pauseButton = document.querySelector("#pauseButton");
const resetButton = document.querySelector("#resetButton");
const viewButton = document.querySelector("#viewButton");
const speedSlider = document.querySelector("#speedSlider");
const speedValue = document.querySelector("#speedValue");
let paused = false;
let simulationSpeed = 1;
let elapsed = 0;
pauseButton.addEventListener("click", () => {
paused = !paused;
pauseButton.textContent = paused ? "Resume" : "Pause";
pauseButton.setAttribute("aria-pressed", String(paused));
});
speedSlider.addEventListener("input", () => {
simulationSpeed = Number(speedSlider.value);
speedValue.textContent = `${simulationSpeed.toFixed(1)}×`;
});
function resetSimulation() {
elapsed = 0;
trackPhase = 0;
for (const particle of particles) {
randomParticleState(particle);
}
}
resetButton.addEventListener("click", resetSimulation);
viewButton.addEventListener("click", resetCamera);
window.addEventListener("keydown", (event) => {
if (event.target instanceof HTMLInputElement) return;
if (event.code === "Space") {
event.preventDefault();
pauseButton.click();
} else if (event.key.toLowerCase() === "r") {
resetSimulation();
}
});
// -------------------------------------------------------------------------
// Animation
// -------------------------------------------------------------------------
const clock = new THREE.Clock();
function animate() {
requestAnimationFrame(animate);
const realDelta = Math.min(clock.getDelta(), 0.04);
const delta = paused ? 0 : realDelta * simulationSpeed;
elapsed += delta;
updateParticles(delta);
updateTrack(delta);
dust.rotation.y += realDelta * 0.006;
renderer.render(scene, camera);
}
animate();
window.addEventListener("resize", () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
});
</script>
</body>
</html>
```