Prompt specification
Build or customize the Three.js 3D scene "Long-cell transport corridor" 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: Long-cell transport corridor
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/long-cell-transport-corridor-x0835
Scene Description Prompt:
Make a standalone HTML scene with Three.js from cdnjs that depicts cytoskeletal cargo transport. Compose a narrow elongated cell with branching tracks and cargo destinations; keep vesicles attached to their tracks until clearly marked release points. Voxel style.
Working Three.js Starter (long-cell-transport-corridor-x0835):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1" />
<title>Cytoskeletal Cargo Transport — Voxel</title>
<style>
:root{
--bg:#05070f; --ink:#dbe7ff; --dim:#8ea3c8;
--pink:#ff3d8b; --green:#5ef2a0; --amber:#ffb020;
}
*{box-sizing:border-box}
html,body{margin:0;height:100%;background:var(--bg);overflow:hidden;
font-family:ui-monospace,SFMono-Regular,Menlo,Consolas,monospace;color:var(--ink)}
#stage{position:fixed;inset:0}
#stage canvas{display:block}
#labels{position:fixed;inset:0;pointer-events:none;overflow:hidden}
.lbl{position:absolute;transform:translate(-50%,-50%);white-space:nowrap;
font-size:9.5px;letter-spacing:.11em;font-weight:700;padding:3px 6px;border-radius:3px;
background:rgba(7,11,22,.78);border:1px solid rgba(255,255,255,.22);
text-shadow:0 1px 3px #000;transition:opacity .2s}
.lbl.rel{border-color:var(--pink);color:#ffd2e5;background:rgba(60,8,32,.8);
box-shadow:0 0 12px rgba(255,61,139,.45)}
.lbl.dest{border-color:currentColor}
.lbl.lock{color:var(--green);border-color:rgba(94,242,160,.55);background:rgba(6,32,20,.78)}
.lbl.free{color:var(--amber);border-color:rgba(255,176,32,.6);background:rgba(44,28,4,.8)}
.lbl.done{color:var(--pink);border-color:rgba(255,61,139,.6)}
.lbl.mtoc{color:#ffe08a;border-color:rgba(255,224,138,.5)}
.panel{position:fixed;background:rgba(8,12,24,.74);border:1px solid rgba(120,160,220,.22);
border-radius:10px;backdrop-filter:blur(7px);padding:12px 14px;
box-shadow:0 10px 40px rgba(0,0,0,.55)}
#hud{top:14px;left:14px;max-width:270px}
#hud h1{margin:0 0 3px;font-size:13px;letter-spacing:.14em;color:#eaf2ff}
#hud p{margin:0 0 9px;font-size:10px;color:var(--dim);line-height:1.5}
#stats{font-size:10px;color:#bcd0f2;border-top:1px solid rgba(120,160,220,.18);
padding-top:8px;margin-top:2px;letter-spacing:.06em}
.key{display:inline-block;width:9px;height:9px;border-radius:2px;margin-right:6px;
vertical-align:-1px;box-shadow:0 0 8px currentColor}
#controls{bottom:14px;right:14px;display:flex;flex-direction:column;gap:7px;align-items:stretch}
.row{display:flex;gap:6px;flex-wrap:wrap;justify-content:flex-end}
button{font:inherit;font-size:10px;letter-spacing:.09em;color:#cfe0ff;cursor:pointer;
background:rgba(30,42,72,.72);border:1px solid rgba(130,170,235,.3);border-radius:6px;
padding:6px 10px;transition:.15s}
button:hover{background:rgba(52,72,120,.85);border-color:rgba(150,190,255,.6)}
button.on{background:rgba(255,61,139,.22);border-color:var(--pink);color:#ffd2e5}
#hint{bottom:14px;left:14px;font-size:10px;color:var(--dim);letter-spacing:.05em}
#hint b{color:#cfe0ff;font-weight:600}
</style>
</head>
<body>
<div id="stage"></div>
<div id="labels"></div>
<div id="hud" class="panel">
<h1>CYTOSKELETAL CARGO TRANSPORT</h1>
<p>Voxel microtubule network inside a narrow elongated cell. Vesicles stay
<b style="color:var(--green)">track-locked</b> until they reach a marked
<b style="color:var(--pink)">release point</b>, then detach to a depot.</p>
<div id="stats">loading…</div>
</div>
<div id="hint" class="panel">
<b>drag</b> orbit · <b>wheel</b> zoom · <b>right-drag / shift-drag</b> pan
</div>
<div id="controls" class="panel">
<div class="row">
<button id="btnPause">⏸ PAUSE</button>
<button id="btnSpeed">SPEED ×1</button>
<button id="btnOrbit" class="on">↻ AUTO-ORBIT</button>
</div>
<div class="row">
<button id="btnGates" class="on">◈ RELEASE POINTS</button>
<button id="btnTrails" class="on">⋯ TRAILS</button>
<button id="btnView">⟲ 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';
/* ───────────────────────── setup ───────────────────────── */
const stage = document.getElementById('stage');
const labelRoot = document.getElementById('labels');
const renderer = new THREE.WebGLRenderer({ antialias:true, powerPreference:'high-performance' });
renderer.setPixelRatio(Math.min(devicePixelRatio, 2));
renderer.setSize(innerWidth, innerHeight);
stage.appendChild(renderer.domElement);
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x05070f);
scene.fog = new THREE.FogExp2(0x05070f, 0.0135);
const camera = new THREE.PerspectiveCamera(42, innerWidth/innerHeight, 0.1, 400);
scene.add(new THREE.HemisphereLight(0x8fbcff, 0x0a0c18, 0.62));
const key = new THREE.DirectionalLight(0xffffff, 0.85); key.position.set(9, 14, 8); scene.add(key);
const rimA = new THREE.PointLight(0x39d8ff, 26, 34, 2); rimA.position.set(-3, 3, -17); scene.add(rimA);
const rimB = new THREE.PointLight(0xff4f9a, 24, 34, 2); rimB.position.set(3, -2, 15); scene.add(rimB);
const fill = new THREE.PointLight(0x7c5cff, 16, 26, 2); fill.position.set(0, 6, 2); scene.add(fill);
const UNIT = new THREE.BoxGeometry(1,1,1);
const dummy = new THREE.Object3D();
const tmpCol = new THREE.Color();
const V = (x,y,z)=>new THREE.Vector3(x,y,z);
// deterministic RNG
let _s = 987654321;
const rnd = () => (_s = (_s*1664525 + 1013904223) >>> 0) / 4294967296;
function instanced(voxels, material){
const m = new THREE.InstancedMesh(UNIT, material, voxels.length);
voxels.forEach((v,i)=>{
dummy.position.copy(v.p);
dummy.scale.set(v.s, v.s, v.s);
dummy.rotation.set(0,0,0);
dummy.updateMatrix();
m.setMatrixAt(i, dummy.matrix);
m.setColorAt(i, tmpCol.set(v.c));
});
m.instanceMatrix.needsUpdate = true;
if (m.instanceColor) m.instanceColor.needsUpdate = true;
return m;
}
function setVoxels(mesh, list){
list.forEach((v,i)=>{
dummy.position.copy(v.p);
dummy.scale.set(v.s, v.s, v.s);
dummy.rotation.set(0,0,0);
dummy.updateMatrix();
mesh.setMatrixAt(i, dummy.matrix);
});
mesh.instanceMatrix.needsUpdate = true;
}
/* ───────────────────────── cell shell (narrow + elongated) ───────────────────────── */
function cellRadius(z){
const t = Math.abs(z)/21;
const taper = Math.max(0, (t - 0.74)/0.26);
return Math.max(0.28, 1.72 * (1 - taper*taper*0.92));
}
const memVox = [];
for (let z=-21; z<=21; z+=1){
const r = cellRadius(z), n = 16;
for (let i=0;i<n;i++){
if (rnd() < 0.13) continue; // porous membrane lattice
const a = i/n*Math.PI*2 + (rnd()-0.5)*0.06;
const c = new THREE.Color().setHSL(0.50+rnd()*0.05, 0.55, 0.42+rnd()*0.13);
memVox.push({p:V(Math.cos(a)*r, Math.sin(a)*r, z+(rnd()-0.5)*0.12), s:1.02, c});
}
}
const membrane = instanced(memVox, new THREE.MeshLambertMaterial({
color:0xffffff, transparent:true, opacity:0.20, depthWrite:false, side:THREE.DoubleSide
}));
membrane.renderOrder = 4;
scene.add(membrane);
// faint organelle clutter inside the cell
const orgVox = [];
for (let k=0;k<9;k++){
const z = -17 + rnd()*34, a = rnd()*Math.PI*2, rr = 0.5+rnd()*0.8;
const base = V(Math.cos(a)*rr, Math.sin(a)*rr, z);
const hue = rnd()<0.5 ? 0.34 : 0.62;
for (let j=0;j<7;j++){
orgVox.push({p:base.clone().add(V((rnd()-0.5)*0.9,(rnd()-0.5)*0.7,(rnd()-0.5)*1.5)),
s:0.20+rnd()*0.12, c:new THREE.Color().setHSL(hue,0.5,0.32+rnd()*0.12)});
}
}
for (let k=0;k<120;k++){
const z=-20+rnd()*40, a=rnd()*Math.PI*2, rr=rnd()*1.5;
orgVox.push({p:V(Math.cos(a)*rr, Math.sin(a)*rr, z), s:0.09,
c:new THREE.Color().setHSL(0.6,0.3,0.5+rnd()*0.2)});
}
scene.add(instanced(orgVox, new THREE.MeshLambertMaterial({color:0xffffff})));
// distant voxel starfield
const stars = [];
for (let i=0;i<260;i++){
const a=rnd()*Math.PI*2, b=Math.acos(rnd()*2-1), R=90+rnd()*70;
stars.push({p:V(Math.sin(b)*Math.cos(a)*R, Math.cos(b)*R, Math.sin(b)*Math.sin(a)*R),
s:0.6+rnd()*0.9, c:new THREE.Color().setHSL(0.6,0.25,0.25+rnd()*0.25)});
}
scene.add(instanced(stars, new THREE.MeshBasicMaterial({color:0xffffff})));
/* ───────────────────────── microtubule tracks ───────────────────────── */
const TRACKS = [
{ id:'trunk', color:0x39c6ff,
pts:[[0,0,-18],[0,0,-12],[0.15,0.10,-6],[-0.15,0.15,0],[0.10,-0.10,6],[0,0,12]],
release:{t:0.60, name:'RP-1'},
dest:{name:'AXON TIP · SYNAPSE', pos:[0.35,0.30,19.6], color:0xff9f43, style:'cluster'},
cargo:[{c:0xff6b6b},{c:0xa0e7e5}] },
{ id:'endoA', color:0x4fe0c0,
pts:[[0,0,-12],[0.75,0.30,-11],[1.15,0.40,-9.6],[1.35,0.30,-8.4]],
release:{t:0.55, name:'RP-2'},
dest:{name:'EARLY ENDOSOME', pos:[1.35,0.30,-8.4], color:0xb06bff, style:'cluster'},
cargo:[{c:0xf9f871}] },
{ id:'golgi', color:0x9d7bff,
pts:[[0,0,-6],[-0.70,0.50,-5.2],[-1.15,0.60,-4.2],[-1.35,0.50,-3.2]],
release:{t:0.50, name:'RP-3'},
dest:{name:'GOLGI STACK', pos:[-1.35,0.50,-3.2], color:0xffd166, style:'stack'},
cargo:[{c:0xc8a2ff}] },
{ id:'lyso', color:0xff8fab,
pts:[[0,0,0],[0.60,0.50,1.2],[1.05,0.70,2.6],[1.30,0.60,4.2]],
release:{t:0.58, name:'RP-4'},
dest:{name:'LYSOSOME', pos:[1.30,0.60,4.2], color:0x7cf5c8, style:'cluster'},
cargo:[{c:0xff8fab},{c:0xffd9a0}] },
{ id:'rec', color:0xffc857,
pts:[[0,0,6],[-0.80,-0.40,7.2],[-1.20,-0.50,8.8],[-1.35,-0.40,10.6]],
release:{t:0.45, name:'RP-5'},
dest:{name:'RECYCLING ENDOSOME', pos:[-1.35,-0.40,10.6], color:0x6ec7ff, style:'cluster'},
cargo:[{c:0x9be7ff}] },
{ id:'tip', color:0x7ee787,
pts:[[0,0,12],[0.15,0.20,15],[0.30,0.25,18],[0.35,0.30,20]],
release:{t:0.72, name:'RP-6'},
dest:{name:'GROWTH CONE · PM', pos:[0.35,0.30,20.0], color:0xff6bd6, style:'cluster'},
cargo:[{c:0x7ee787}] },
];
const trackVox = [], gateVox = [], destVox = [];
const labels = [];
function addLabel(text, cls, pos, color){
const el = document.createElement('div');
el.className = 'lbl ' + cls;
el.textContent = text;
if (color) el.style.color = '#' + new THREE.Color(color).getHexString();
labelRoot.appendChild(el);
const L = {el, pos:pos.clone(), shown:true};
labels.push(L);
return L;
}
TRACKS.forEach(tr=>{
const curve = new THREE.CatmullRomCurve3(tr.pts.map(p=>V(...p)), false, 'centripetal', 0.5);
tr.curve = curve;
tr.len = curve.getLength();
tr.releasePos = curve.getPointAt(tr.release.t);
tr.releaseTan = curve.getTangentAt(tr.release.t);
tr.destPos = V(...tr.dest.pos);
// voxel rail: 5-voxel cross-section along the path
const steps = Math.max(10, Math.floor(tr.len/0.40));
const base = new THREE.Color(tr.color);
for (let i=0;i<=steps;i++){
const u = i/steps;
const p = curve.getPointAt(u), tan = curve.getTangentAt(u);
let n1 = new THREE.Vector3().crossVectors(tan, V(0,1,0));
if (n1.lengthSq() < 1e-6) n1.set(1,0,0);
n1.normalize();
const n2 = new THREE.Vector3().crossVectors(tan, n1).normalize();
trackVox.push({p:p.clone(), s:0.30, c:base.clone().offsetHSL(0,0,0.10)});
const shade = (i%2===0) ? 0.02 : -0.10;
for (const sgn of [[1,0],[-1,0],[0,1],[0,-1]]){
const q = p.clone().add(n1.clone().multiplyScalar(sgn[0]*0.27)).add(n2.clone().multiplyScalar(sgn[1]*0.27));
trackVox.push({p:q, s:0.24, c:base.clone().offsetHSL(0,0,shade)});
}
}
// ── release gate: bright pink ring + posts ──
const rp = tr.releasePos, rt = tr.releaseTan;
let g1 = new THREE.Vector3().crossVectors(rt, V(0,1,0));
if (g1.lengthSq() < 1e-6) g1.set(1,0,0);
g1.normalize();
const g2 = new THREE.Vector3().crossVectors(rt, g1).normalize();
for (let i=0;i<12;i++){
const a = i/12*Math.PI*2;
const q = rp.clone().add(g1.clone().multiplyScalar(Math.cos(a)*0.62)).add(g2.clone().multiplyScalar(Math.sin(a)*0.62));
gateVox.push({p:q, s:0.28, c:new THREE.Color(0xff3d8b)});
}
for (const s of [1,-1]) for (const o of [0.95]){
gateVox.push({p:rp.clone().add(g2.clone().multiplyScalar(s*o)), s:0.30, c:new THREE.Color(0xff3d8b)});
gateVox.push({p:rp.clone().add(g2.clone().multiplyScalar(s*o)).add(g1.clone().multiplyScalar(s*0.34)), s:0.22, c:new THREE.Color(0xffd9e6)});
}
gateVox.push({p:rp.clone(), s:0.24, c:new THREE.Color(0xffffff)});
tr.gateLabel = addLabel('RELEASE · ' + tr.release.name, 'rel', rp.clone().add(V(0,0.85,0)));
// ── destination depot ──
const dp = tr.destPos, dc = new THREE.Color(tr.dest.color);
if (tr.dest.style === 'stack'){
for (let r=0;r<4;r++) for (let i=0;i<7;i++){
const a=i/7*Math.PI*2;
destVox.push({p:dp.clone().add(V(Math.cos(a)*0.44, r*0.24-0.36, Math.sin(a)*0.44)), s:0.26,
c:dc.clone().offsetHSL(0,0,(i%3-1)*0.07)});
}
} else {
destVox.push({p:dp.clone(), s:0.46, c:dc.clone()});
for (let i=0;i<14;i++){
const a=i/14*Math.PI*2, b=(i%3-1)*0.34;
destVox.push({p:dp.clone().add(V(Math.cos(a)*0.44, Math.sin(a)*0.36, b)), s:0.30,
c:dc.clone().offsetHSL(0,0,(i%3-1)*0.08)});
}
for (let i=0;i<8;i++){
const a=i/8*Math.PI*2;
destVox.push({p:dp.clone().add(V(Math.cos(a)*0.80, Math.sin(a)*0.70, 0)), s:0.16,
c:dc.clone().offsetHSL(0,0,-0.18)});
}
}
tr.destLabel = addLabel(tr.dest.name, 'dest', dp.clone().add(V(0,1.0,0)), tr.dest.color);
});
scene.add(instanced(trackVox, new THREE.MeshLambertMaterial({color:0xffffff})));
const gateMesh = instanced(gateVox, new THREE.MeshBasicMaterial({color:0xffffff}));
scene.add(gateMesh);
scene.add(instanced(destVox, new THREE.MeshLambertMaterial({color:0xffffff})));
// MTOC (microtubule organizing centre) at the minus end
const mtoc = [];
for (let i=0;i<16;i++){
const a=i/16*Math.PI*2;
mtoc.push({p:V(Math.cos(a)*0.55, Math.sin(a)*0.45, -19.2+((i%4)-1.5)*0.28), s:0.30,
c:new THREE.Color(0xffe08a).offsetHSL(0,0,(i%3-1)*0.08)});
}
mtoc.push({p:V(0,0,-19.2), s:0.42, c:new THREE.Color(0xffe08a)});
scene.add(instanced(mtoc, new THREE.MeshLambertMaterial({color:0xffffff})));
addLabel('MTOC · MINUS END', 'mtoc', V(0,0.9,-19.2));
/* ───────────────────────── vesicles ───────────────────────── */
const vesicles = [];
const trailMat = new THREE.MeshLambertMaterial({color:0xffffff});
const burstMat = new THREE.MeshBasicMaterial({color:0xffffff});
TRACKS.forEach((tr, ti)=>{
tr.cargo.forEach((cv, ci)=>{
const cargo = new THREE.Color(cv.c);
const body = new THREE.InstancedMesh(UNIT, new THREE.MeshLambertMaterial({color:0xffffff}), 9);
// local layout: 1 core + 6 shell + 1 tether
const shellOff = [[0.30,0,0],[-0.30,0,0],[0,0.30,0],[0,-0.30,0],[0,0,0.30],[0,0,-0.30]];
const layout = [{p:V(0,0,0), s:0.44, c:cargo.clone()}];
shellOff.forEach((o,i)=>layout.push({
p:V(...o), s:0.24, c:cargo.clone().offsetHSL(0,0, i%2?0.16:-0.05)}));
layout.push({p:V(0,0,0), s:0.20, c:new THREE.Color(0xdfe9ff)}); // tether block
scene.add(body);
const trail = new THREE.InstancedMesh(UNIT, trailMat, 16);
trail.frustumCulled = false;
for (let i=0;i<16;i++){
dummy.position.set(0,0,0); dummy.scale.set(0.001,0.001,0.001); dummy.updateMatrix();
trail.setMatrixAt(i, dummy.matrix);
trail.setColorAt(i, tmpCol.copy(cargo).offsetHSL(0,0,0.18 - i*0.012));
}
trail.instanceMatrix.needsUpdate = true; trail.instanceColor.needsUpdate = true;
scene.add(trail);
const burst = new THREE.InstancedMesh(UNIT, burstMat, 12);
burst.frustumCulled = false; burst.visible = false;
scene.add(burst);
const kin = new THREE.Mesh(UNIT, new THREE.MeshBasicMaterial({color:0xffd84d}));
kin.scale.setScalar(0.17); scene.add(kin);
const dyn = new THREE.Mesh(UNIT, new THREE.MeshBasicMaterial({color:0x5ad2ff}));
dyn.scale.setScalar(0.17); scene.add(dyn);
const v = {
tr, body, trail, burst, kin, dyn, cargo,
state:'wait', timer: 0.6 + ti*0.9 + ci*1.7 + rnd()*1.4,
t:0, rate:0.055 + rnd()*0.012, flight:0, burstT:0,
trailPts:[], trailAcc:0,
label: addLabel('TRACK-LOCKED', 'lock', V(0,0,0)),
bodyPos:V(0,0,0), particles:null,
};
vesicles.push(v);
});
});
/* ───────────────────────── labels / HUD ───────────────────────── */
const statsEl = document.getElementById('stats');
let statAcc = 0;
/* ───────────────────────── camera controls (no addons) ───────────────────────── */
const cam = {
theta:2.35, phi:1.16, radius:31,
target:V(0,0,-1),
dTheta:2.35, dPhi:1.16, dRadius:31, dTarget:V(0,0,-1)
};
let dragging=false, panning=false, lastX=0, lastY=0, pointers=new Map(), pinchDist=0;
renderer.domElement.addEventListener('contextmenu', e=>e.preventDefault());
renderer.domElement.addEventListener('pointerdown', e=>{
pointers.set(e.pointerId, {x:e.clientX, y:e.clientY});
if (pointers.size===2){
const [a,b] = [...pointers.values()];
pinchDist = Math.hypot(a.x-b.x, a.y-b.y);
}
dragging = true;
panning = e.button===2 || e.shiftKey;
lastX = e.clientX; lastY = e.clientY;
renderer.domElement.setPointerCapture(e.pointerId);
});
renderer.domElement.addEventListener('pointermove', e=>{
if (pointers.has(e.pointerId)) pointers.set(e.pointerId, {x:e.clientX, y:e.clientY});
if (pointers.size===2){
const [a,b] = [...pointers.values()];
const d = Math.hypot(a.x-b.x, a.y-b.y);
if (pinchDist>0) cam.dRadius = THREE.MathUtils.clamp(cam.dRadius * (pinchDist/d), 9, 90);
pinchDist = d;
return;
}
if (!dragging) return;
const dx = e.clientX-lastX, dy = e.clientY-lastY;
lastX = e.clientX; lastY = e.clientY;
if (panning){
const right = new THREE.Vector3().setFromMatrixColumn(camera.matrix, 0);
const up = new THREE.Vector3().setFromMatrixColumn(camera.matrix, 1);
cam.dTarget.addScaledVector(right, -dx*cam.radius*0.0016)
.addScaledVector(up, dy*cam.radius*0.0016);
} else {
cam.dTheta -= dx*0.006;
cam.dPhi = THREE.MathUtils.clamp(cam.dPhi - dy*0.006, 0.12, Math.PI-0.12);
}
});
['pointerup','pointercancel','pointerleave'].forEach(ev=>
renderer.domElement.addEventListener(ev, e=>{ pointers.delete(e.pointerId); if(!pointers.size) dragging=false; }));
renderer.domElement.addEventListener('wheel', e=>{
e.preventDefault();
cam.dRadius = THREE.MathUtils.clamp(cam.dRadius * Math.exp(e.deltaY*0.0011), 9, 90);
}, {passive:false});
const settings = { paused:false, speed:1, autoOrbit:true, gates:true, trails:true };
document.getElementById('btnPause').onclick = e=>{
settings.paused = !settings.paused;
e.target.textContent = settings.paused ? '▶ RESUME' : '⏸ PAUSE';
e.target.classList.toggle('on', settings.paused);
};
const speedBtn = document.getElementById('btnSpeed');
const speeds = [1,2,0.5];
let si = 0;
speedBtn.onclick = ()=>{ si = (si+1)%speeds.length; settings.speed = speeds[si]; speedBtn.textContent = 'SPEED ×'+speeds[si]; };
const orbitBtn = document.getElementById('btnOrbit');
orbitBtn.onclick = ()=>{ settings.autoOrbit = !settings.autoOrbit; orbitBtn.classList.toggle('on', settings.autoOrbit); };
const gateBtn = document.getElementById('btnGates');
gateBtn.onclick = ()=>{
settings.gates = !settings.gates; gateMesh.visible = settings.gates;
gateBtn.classList.toggle('on', settings.gates);
TRACKS.forEach(t=>t.gateLabel.el.style.display = settings.gates ? '' : 'none');
};
const trailBtn = document.getElementById('btnTrails');
trailBtn.onclick = ()=>{
settings.trails = !settings.trails; trailBtn.classList.toggle('on', settings.trails);
};
document.getElementById('btnView').onclick = ()=>{
cam.dTheta = 2.35; cam.dPhi = 1.16; cam.dRadius = 31; cam.dTarget.set(0,0,-1);
};
/* ───────────────────────── simulation ───────────────────────── */
const clock = new THREE.Clock();
function updateVesicle(v, dt){
const tr = v.tr;
if (v.state === 'wait'){
v.timer -= dt;
if (v.timer <= 0) v.state = 'attached';
} else if (v.state === 'attached'){
v.t += dt * v.rate;
if (v.t >= tr.release.t){
v.t = tr.release.t;
v.state = 'released';
v.flight = 0;
v.startPos = tr.releasePos.clone();
// outward perpendicular so cargo visibly leaves the rail
let n1 = new THREE.Vector3().crossVectors(tr.releaseTan, V(0,1,0));
if (n1.lengthSq()<1e-6) n1.set(1,0,0);
n1.normalize();
const n2 = new THREE.Vector3().crossVectors(tr.releaseTan, n1).normalize();
v.ctrl = tr.releasePos.clone()
.add(tr.releaseTan.clone().multiplyScalar(1.1))
.add(n2.clone().multiplyScalar(0.85));
}
} else if (v.state === 'released'){
v.flight += dt / 2.7;
if (v.flight >= 1){
v.flight = 1;
v.state = 'delivered';
v.burstT = 0;
v.particles = [];
for (let i=0;i<12;i++){
const a = rnd()*Math.PI*2, b = Math.acos(rnd()*2-1), sp = 0.9+rnd()*2.1;
v.particles.push({
p0:tr.destPos.clone(),
vel:V(Math.sin(b)*Math.cos(a)*sp, Math.sin(b)*Math.sin(a)*sp, Math.cos(b)*sp)
});
}
}
} else if (v.state === 'delivered'){
v.burstT += dt;
if (v.burstT > 1.0){
v.state = 'wait'; v.timer = 1.2 + rnd()*2.4; v.t = 0;
v.trailPts.length = 0;
v.burst.visible = false;
}
}
const u = THREE.MathUtils.clamp(v.t, 0, 1);
const tp = tr.curve.getPointAt(u);
const tan = tr.curve.getTangentAt(u);
let n1 = new THREE.Vector3().crossVectors(tan, V(0,1,0));
if (n1.lengthSq()<1e-6) n1.set(1,0,0);
n1.normalize();
const n2 = new THREE.Vector3().crossVectors(tan, n1).normalize();
let bodyPos, bodyScale = 1, tether = null;
if (v.state === 'attached' || v.state === 'wait'){
const off = n2.clone().multiplyScalar(0.56);
bodyPos = tp.clone().add(off);
tether = off.clone().multiplyScalar(0.55);
// trail sample
v.trailAcc += dt;
if (v.trailAcc > 0.055){
v.trailAcc = 0;
v.trailPts.unshift(bodyPos.clone());
if (v.trailPts.length > 16) v.trailPts.pop();
}
v.kin.position.copy(tr.curve.getPointAt(Math.min(1,u+0.028))).add(n2.clone().multiplyScalar(0.30));
v.dyn.position.copy(tr.curve.getPointAt(Math.max(0,u-0.028))).add(n2.clone().multiplyScalar(0.30));
v.kin.visible = v.dyn.visible = true;
} else if (v.state === 'released'){
const f = THREE.MathUtils.clamp(v.flight,0,1);
const e = f*f*(3-2*f);
bodyPos = new THREE.Vector3()
.add(v.startPos.clone().multiplyScalar((1-e)*(1-e)))
.add(v.ctrl.clone().multiplyScalar(2*(1-e)*e))
.add(tr.destPos.clone().multiplyScalar(e*e));
v.kin.visible = v.dyn.visible = false;
} else { // delivered
bodyScale = Math.max(0, 1 - v.burstT/0.5);
bodyPos = tr.destPos.clone();
v.kin.visible = v.dyn.visible = false;
}
v.bodyPos.copy(bodyPos);
// rebuild vesicle voxels (core + shell + tether block)
const shellOff = [[0.30,0,0],[-0.30,0,0],[0,0.30,0],[0,-0.30,0],[0,0,0.30],[0,0,-0.30]];
const vox = [];
vox.push({p:bodyPos, s:0.44*bodyScale, c:v.cargo.clone()});
shellOff.forEach((o,i)=>vox.push({
p:bodyPos.clone().add(V(...o).multiplyScalar(bodyScale)),
s:0.24*bodyScale,
c:v.cargo.clone().offsetHSL(0,0, i%2?0.16:-0.05)}));
if (tether && bodyScale>0.2){
vox.push({p:tp.clone().add(tether), s:0.20*bodyScale, c:new THREE.Color(0xdfe9ff)});
} else {
vox.push({p:bodyPos, s:0.001, c:new THREE.Color(0xffffff)});
}
setVoxels(v.body, vox);
// trail voxels
const tv = [];
for (let i=0;i<16;i++){
if (i < v.trailPts.length){
const s = 0.20 * (1 - i/16);
tv.push({p:v.trailPts[i], s:Math.max(0.005,s), c:v.cargo});
} else tv.push({p:V(0,0,0), s:0.001, c:v.cargo});
}
setVoxels(v.trail, tv);
v.trail.visible = settings.trails && (v.state==='attached'||v.state==='wait');
// delivery burst
if (v.state === 'delivered' && v.particles){
v.burst.visible = true;
const bv = [];
const life = Math.min(1, v.burstT/0.85);
v.particles.forEach(p=>{
const pos = p.p0.clone().add(p.vel.clone().multiplyScalar(v.burstT*0.55));
bv.push({p:pos, s:Math.max(0.01, 0.24*(1-life)), c:new THREE.Color(0xffffff)});
});
setVoxels(v.burst, bv);
} else {
v.burst.visible = false;
}
// status chip
const lab = v.label;
lab.pos.copy(bodyPos).add(V(0,0.62,0));
if (v.state==='attached'||v.state==='wait'){
lab.el.textContent = 'TRACK-LOCKED'; lab.el.className = 'lbl lock';
} else if (v.state==='released'){
lab.el.textContent = 'RELEASED → DEPOT'; lab.el.className = 'lbl free';
} else {
lab.el.textContent = 'DELIVERED'; lab.el.className = 'lbl done';
}
}
/* ───────────────────────── render loop ───────────────────────── */
function updateLabels(){
const w = innerWidth, h = innerHeight;
for (const L of labels){
const p = L.pos.clone().project(camera);
if (p.z > 1 || p.z < -1){ L.el.style.display='none'; continue; }
L.el.style.display = 'block';
const x = (p.x*0.5+0.5)*w, y = (-p.y*0.5+0.5)*h;
L.el.style.transform = `translate(-50%,-50%) translate(${x.toFixed(1)}px,${y.toFixed(1)}px)`;
}
}
function animate(){
requestAnimationFrame(animate);
const raw = Math.min(0.05, clock.getDelta());
const dt = settings.paused ? 0 : raw * settings.speed;
if (settings.autoOrbit && !dragging) cam.dTheta += raw*0.045;
const k = 1 - Math.exp(-raw*7);
cam.theta += (cam.dTheta - cam.theta)*k;
cam.phi += (cam.dPhi - cam.phi)*k;
cam.radius+= (cam.dRadius - cam.radius)*k;
cam.target.lerp(cam.dTarget, k);
const sp = Math.sin(cam.phi), cp = Math.cos(cam.phi);
camera.position.set(
cam.target.x + cam.radius*sp*Math.cos(cam.theta),
cam.target.y + cam.radius*cp,
cam.target.z + cam.radius*sp*Math.sin(cam.theta));
camera.lookAt(cam.target);
for (const v of vesicles) updateVesicle(v, dt);
// gate pulse
const pulse = 0.24 + 0.10*Math.sin(performance.now()*0.006);
gateMesh.material.color.setScalar(1);
gateMesh.scale.setScalar(1 + 0.06*Math.sin(performance.now()*0.005));
statAcc += raw;
if (statAcc > 0.25){
statAcc = 0;
const locked = vesicles.filter(v=>v.state==='attached'||v.state==='wait').length;
const flight = vesicles.filter(v=>v.state==='released').length;
const done = vesicles.filter(v=>v.state==='delivered').length;
statsEl.innerHTML =
`<span class="key" style="color:#5ef2a0"></span>TRACK-LOCKED ${locked} `+
`<span class="key" style="color:#ffb020"></span>IN FLIGHT ${flight} `+
`<span class="key" style="color:#ff3d8b"></span>RELEASE GATES ${TRACKS.length}`;
}
updateLabels();
renderer.render(scene, camera);
}
animate();
addEventListener('resize', ()=>{
camera.aspect = innerWidth/innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(innerWidth, innerHeight);
});
</script>
</body>
</html>
```