Prompt specification
Build or customize the Three.js 3D scene "Cell organelle and molecule scale ladder" 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 organelle and molecule scale ladder
Category: Biology & Nature · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/cell-organelle-and-molecule-scale-ladder-x0809
Scene Description Prompt:
Create a standalone low-poly scene in one HTML file using Three.js from cdnjs. Arrange schematic nested frames for tissue, cell, organelle, and molecule; use separate scale bars and explicitly mark that the models cannot share one visual magnification.
Working Three.js Starter (cell-organelle-and-molecule-scale-ladder-x0809):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" />
<title>Nested Schematic Frames — Tissue → Cell → Organelle → Molecule</title>
<style>
:root{
--bg:#070b16; --ink:#e9eefc; --dim:#93a4c4; --line:rgba(140,170,220,.18);
--tissue:#5fe3b0; --cell:#5aa9ff; --organelle:#ffb454; --molecule:#ff6ec7;
--warn:#ff6b6b;
}
*{box-sizing:border-box}
html,body{margin:0;height:100%;background:var(--bg);color:var(--ink);overflow:hidden;
font-family:ui-sans-serif,system-ui,-apple-system,"Segoe UI",Roboto,Helvetica,Arial,sans-serif;}
canvas{display:block;position:fixed;inset:0;touch-action:none}
.mono{font-family:ui-monospace,SFMono-Regular,Menlo,Consolas,monospace}
.panel{position:fixed;background:rgba(9,14,28,.74);border:1px solid var(--line);
border-radius:12px;backdrop-filter:blur(9px);box-shadow:0 10px 34px rgba(0,0,0,.5);}
/* ---------- top banner: the explicit statement ---------- */
#banner{top:14px;left:50%;transform:translateX(-50%);padding:9px 16px;display:flex;gap:10px;
align-items:center;border-color:rgba(255,107,107,.45);
background:linear-gradient(180deg,rgba(46,12,18,.86),rgba(9,14,28,.8));max-width:min(760px,92vw);}
#banner .dot{width:9px;height:9px;border-radius:50%;background:var(--warn);box-shadow:0 0 12px var(--warn);flex:0 0 auto}
#banner b{color:#ffd9d9;letter-spacing:.04em}
#banner span{color:#f0c9c9;font-size:12.5px;line-height:1.35}
/* ---------- ladder panel ---------- */
#ladder{top:14px;right:14px;width:274px;padding:12px 13px 11px}
#ladder h3{margin:0 0 2px;font-size:11px;letter-spacing:.16em;color:var(--dim);font-weight:700}
#ladder .sub{font-size:10.5px;color:#6f809f;margin-bottom:9px;letter-spacing:.05em}
.row{display:grid;grid-template-columns:9px 1fr auto;gap:8px;align-items:center;padding:6px 7px;
border-radius:8px;cursor:pointer;border:1px solid transparent;transition:.15s}
.row:hover{background:rgba(255,255,255,.05);border-color:var(--line)}
.row.on{background:rgba(255,255,255,.07);border-color:rgba(140,170,220,.3)}
.row .sw{width:9px;height:9px;border-radius:2px}
.row .nm{font-size:11.5px;letter-spacing:.1em;font-weight:700}
.row .bar{font-size:10.5px;color:var(--dim)}
.row .mag{font-size:11px;color:#cfe0ff}
.rule{height:1px;background:var(--line);margin:9px 0 8px}
#ladder .note{font-size:11px;line-height:1.45;color:#b9c8e2}
#ladder .note b{color:#ffd0d0}
.units{margin-top:8px;font-size:10px;color:#7f90af;line-height:1.5}
.units div{display:flex;justify-content:space-between;gap:8px}
/* ---------- bottom view bar ---------- */
#viewbar{left:50%;bottom:16px;transform:translateX(-50%);display:flex;gap:6px;padding:8px}
#viewbar button{background:transparent;border:1px solid var(--line);color:var(--dim);padding:7px 13px;
border-radius:8px;font-size:11.5px;letter-spacing:.09em;cursor:pointer;transition:.15s;font-weight:600}
#viewbar button:hover{color:var(--ink);border-color:rgba(140,170,220,.42)}
#viewbar button.on{color:#06101f;background:var(--ink);border-color:var(--ink)}
#status{left:14px;bottom:16px;padding:9px 12px;font-size:11.5px;color:#c3d2ea;max-width:44vw;line-height:1.45}
#status b{color:#fff}
#hint{right:14px;bottom:16px;padding:8px 11px;font-size:10.5px;color:#7183a3;letter-spacing:.05em}
/* ---------- projected 3D labels ---------- */
#tags{position:fixed;inset:0;pointer-events:none}
.tag{position:absolute;left:0;top:0;will-change:transform;white-space:nowrap;
background:rgba(6,11,22,.78);border:1px solid var(--line);border-left:3px solid var(--c,#888);
border-radius:7px;padding:5px 9px;box-shadow:0 6px 20px rgba(0,0,0,.45)}
.tag .t{font-size:12px;font-weight:800;letter-spacing:.16em;color:var(--c,#fff)}
.tag .s{font-size:10px;color:#9fb1cd;letter-spacing:.06em;margin-top:2px}
.tag.bar{padding:3px 7px;border-left-width:2px}
.tag.bar .b{font-size:11px;font-weight:700;color:var(--c,#fff)}
.tag.bar .bl{font-size:9px;color:#8a9ab8;letter-spacing:.08em}
.tag.x{border:1px solid rgba(140,170,220,.22);background:rgba(6,11,22,.7);border-radius:20px;padding:3px 10px;
font-size:10.5px;color:#cfe0ff;letter-spacing:.08em}
.fade{transition:opacity .25s}
</style>
</head>
<body>
<canvas id="scene"></canvas>
<div id="tags"></div>
<div id="banner" class="panel">
<span class="dot"></span>
<div>
<b>SCHEMATIC NESTING — NO SHARED MAGNIFICATION</b><br>
<span>Each frame is drawn at its own scale and carries its own scale bar.
The four models <b>cannot</b> be represented by one camera magnification; the bars are
<b>frame‑local and not interchangeable</b>.</span>
</div>
</div>
<div id="ladder" class="panel">
<h3>NESTING LADDER</h3>
<div class="sub">schematic ratios · not to scale</div>
<div id="rows"></div>
<div class="rule"></div>
<div class="note"><b>⚠</b> One camera position / one zoom cannot satisfy all four scale bars at once —
each level is a <b>separate magnification</b>.</div>
<div class="units" id="units"></div>
</div>
<div id="status" class="panel"></div>
<div id="viewbar" class="panel"></div>
<div id="hint" class="panel">drag · orbit | wheel · zoom | shift+drag · pan</div>
<script type="module">
import * as THREE from 'https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js';
/* ============================================================================
SCHEMATIC NESTING LADDER
Every level has: its own frame size, its own drawn scale bar, its own
magnification. The bars deliberately describe DIFFERENT real lengths for
DIFFERENT drawn lengths -> one world unit cannot mean one real distance
across the four models.
========================================================================== */
const DEG = Math.PI / 180;
const LEVELS = [
{ key:'tissue', name:'TISSUE', size:40.0, rot: 0*DEG, color:0x5fe3b0, css:'#5fe3b0',
bar:'1 mm', barUm:1000, mag:'×1', focus:78, zoomNote:'1 mm bar · frame-local' },
{ key:'cell', name:'CELL', size:13.0, rot: 9*DEG, color:0x5aa9ff, css:'#5aa9ff',
bar:'10 µm', barUm:10, mag:'×10³', focus:27, zoomNote:'10 µm bar · frame-local' },
{ key:'organelle', name:'ORGANELLE', size: 4.3, rot:-7*DEG, color:0xffb454, css:'#ffb454',
bar:'100 nm', barUm:0.1, mag:'×10⁶', focus:10.4, zoomNote:'100 nm bar · frame-local' },
{ key:'molecule', name:'MOLECULE', size: 1.4, rot:14*DEG, color:0xff6ec7, css:'#ff6ec7',
bar:'1 Å', barUm:0.0001, mag:'×10⁹', focus:3.9, zoomNote:'1 Å bar · frame-local' }
];
const BAR_FRAC = 0.22; // drawn bar length as a fraction of its frame
const BAR_H = { tissue:0.55, cell:0.24, organelle:0.11, molecule:0.055 };
/* ---------- renderer / scene ---------- */
const canvas = document.getElementById('scene');
const renderer = new THREE.WebGLRenderer({ canvas, antialias:true, powerPreference:'high-performance' });
renderer.setPixelRatio(Math.min(devicePixelRatio, 2));
renderer.setSize(innerWidth, innerHeight);
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.15;
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x070b16);
scene.fog = new THREE.Fog(0x070b16, 110, 430);
const camera = new THREE.PerspectiveCamera(45, innerWidth/innerHeight, 0.02, 1200);
camera.position.set(0, 0, 78);
/* ---------- lights ---------- */
scene.add(new THREE.AmbientLight(0x8ea6d0, 0.55));
const key = new THREE.DirectionalLight(0xffffff, 1.15); key.position.set(28, 36, 60); scene.add(key);
const rimA = new THREE.DirectionalLight(0x5fe3b0, 0.55); rimA.position.set(-40, 18, 25); scene.add(rimA);
const rimB = new THREE.DirectionalLight(0xff6ec7, 0.45); rimB.position.set(34, -26, 30); scene.add(rimB);
/* ---------- backdrop grid + dust ---------- */
const grid = new THREE.GridHelper(60, 30, 0x2b3c60, 0x18243c);
grid.rotation.x = Math.PI/2; grid.position.z = -9; grid.material.transparent = true; grid.material.opacity = 0.42;
scene.add(grid);
(function dust(){
const n = 520, pos = new Float32Array(n*3);
for(let i=0;i<n;i++){
const r = 25 + Math.random()*90, t = Math.random()*Math.PI*2, p = Math.acos(2*Math.random()-1);
pos[i*3] = r*Math.sin(p)*Math.cos(t); pos[i*3+1] = r*Math.sin(p)*Math.sin(t); pos[i*3+2] = r*Math.cos(p);
}
const g = new THREE.BufferGeometry(); g.setAttribute('position', new THREE.BufferAttribute(pos,3));
scene.add(new THREE.Points(g, new THREE.PointsMaterial({ color:0x3d5480, size:0.42, transparent:true, opacity:0.55 })));
})();
/* ---------- helpers ---------- */
const mat = (c,o={}) => new THREE.MeshStandardMaterial({ color:c, flatShading:true, roughness:0.82, metalness:0.06, ...o });
function mulberry32(a){return function(){a|=0;a=a+0x6D2B79F5|0;let t=Math.imul(a^a>>>15,1|a);t=t+Math.imul(t^t>>>7,61|t)^t;return ((t^t>>>14)>>>0)/4294967296;};}
/* ============================================================================
FRAME BUILDER — outline, corner brackets, ruler ticks, frame-local scale bar
========================================================================== */
function buildFrame(L){
const g = new THREE.Group();
g.rotation.z = L.rot;
const s = L.size, h = s/2;
const back = new THREE.Mesh(new THREE.PlaneGeometry(s*0.995,s*0.995),
new THREE.MeshBasicMaterial({ color:L.color, transparent:true, opacity:0.035, side:THREE.DoubleSide, depthWrite:false }));
back.position.z = -0.05; g.add(back);
const corners = [[-h,-h],[h,-h],[h,h],[-h,h]];
const seg = [];
for(let i=0;i<4;i++){ const a=corners[i], b=corners[(i+1)%4]; seg.push(...a,0, ...b,0); }
const outlineMat = new THREE.LineBasicMaterial({ color:L.color, transparent:true, opacity:0.95 });
g.add(new THREE.LineSegments(new THREE.BufferGeometry().setAttribute('position', new THREE.Float32BufferAttribute(seg,3)), outlineMat));
// corner brackets
const bl = [], t = s*0.13;
for(const [x,y] of corners){
const sx = x<0?1:-1, sy = y<0?1:-1;
bl.push(x,y,0, x+sx*t,y,0, x,y,0, x,y+sy*t,0);
}
const bracketMat = new THREE.LineBasicMaterial({ color:L.color, transparent:true, opacity:1 });
g.add(new THREE.LineSegments(new THREE.BufferGeometry().setAttribute('position', new THREE.Float32BufferAttribute(bl,3)), bracketMat));
// ruler ticks on top + bottom edges
const tk = [], N = 10, tl = s*0.035;
for(let i=1;i<N;i++){
const x = -h + (s*i/N), big = (i%5===0) ? tl*1.9 : tl;
tk.push(x, h, 0, x, h-big, 0, x, -h, 0, x, -h+big, 0);
}
g.add(new THREE.LineSegments(new THREE.BufferGeometry().setAttribute('position', new THREE.Float32BufferAttribute(tk,3)),
new THREE.LineBasicMaterial({ color:L.color, transparent:true, opacity:0.5 })));
/* ---- frame-local scale bar (bottom-left inside this frame) ---- */
const barLen = s*BAR_FRAC, barH = BAR_H[L.key], pad = s*0.055;
const barG = new THREE.Group();
const barMats = [];
const segs = 4, sl = barLen/segs;
for(let i=0;i<segs;i++){
const x0 = -h + pad + i*sl;
if(i%2===0){
const m = new THREE.MeshBasicMaterial({ color:L.color, transparent:true, opacity:0.95 });
const m2 = new THREE.Mesh(new THREE.BoxGeometry(sl*0.94, barH, 0.03), m);
m2.position.set(x0 + sl/2, -h + pad + barH/2, 0); barG.add(m2); barMats.push(m);
}else{
const m = new THREE.LineBasicMaterial({ color:L.color, transparent:true, opacity:0.9 });
const m2 = new THREE.LineSegments(new THREE.EdgesGeometry(new THREE.BoxGeometry(sl*0.94, barH, 0.03)), m);
m2.position.set(x0 + sl/2, -h + pad + barH/2, 0); barG.add(m2); barMats.push(m);
}
}
// end caps
const capMat = new THREE.LineBasicMaterial({ color:L.color, transparent:true, opacity:0.95 });
const caps = [];
const x0 = -h+pad, x1 = -h+pad+barLen, yb = -h+pad;
caps.push(x0,yb,0, x0,yb+barH*1.6,0, x1,yb,0, x1,yb+barH*1.6,0);
barG.add(new THREE.LineSegments(new THREE.BufferGeometry().setAttribute('position', new THREE.Float32BufferAttribute(caps,3)), capMat));
barMats.push(capMat);
g.add(barG);
g.position.z = 0.6;
scene.add(g);
L._g = g; L._outlineMat = outlineMat; L._bracketMat = bracketMat; L._barMats = barMats;
L._back = back;
L._barLocal = new THREE.Vector3(x1, yb + barH*0.6, 0.6);
L._tagLocal = new THREE.Vector3(-h, h + s*0.035, 0.6);
L._cornerTR = new THREE.Vector3(h, h, 0.62);
return g;
}
/* ============================================================================
LOW-POLY CONTENT — one model per level (each drawn at ITS OWN scale)
========================================================================== */
function buildTissue(){
const g = new THREE.Group(), rnd = mulberry32(7);
const bodyGeo = new THREE.IcosahedronGeometry(0.72, 0);
const nucGeo = new THREE.IcosahedronGeometry(0.28, 0);
const cols = [0x6fd6a8,0x8fe0c4,0x54b894,0xd7a878,0x9fb6cf,0x7fc9c0];
const mats = cols.map(c => mat(c));
const nucMat = mat(0x2c4a63);
for(let i=0;i<86;i++){
const a = rnd()*Math.PI*2, r = 10.4 + rnd()*8.6;
const cell = new THREE.Mesh(bodyGeo, mats[(rnd()*cols.length)|0]);
cell.position.set(Math.cos(a)*r, Math.sin(a)*r, (rnd()-0.5)*0.9);
cell.scale.set(0.85+rnd()*0.6, 0.7+rnd()*0.45, 0.8+rnd()*0.5);
cell.rotation.set(rnd()*3, rnd()*3, rnd()*3);
const n = new THREE.Mesh(nucGeo, nucMat);
n.position.set(0.14,0.08,0.2); n.scale.setScalar(0.9+rnd()*0.4); cell.add(n);
g.add(cell);
}
return g;
}
function buildCell(){
const g = new THREE.Group();
const memGeo = new THREE.IcosahedronGeometry(3.4, 1);
const membrane = new THREE.Mesh(memGeo, mat(0x2f6fb8,{ transparent:true, opacity:0.34, side:THREE.DoubleSide, depthWrite:false }));
g.add(membrane);
g.add(new THREE.LineSegments(new THREE.EdgesGeometry(memGeo, 18),
new THREE.LineBasicMaterial({ color:0x7fc4ff, transparent:true, opacity:0.85 })));
const nuc = new THREE.Mesh(new THREE.IcosahedronGeometry(1.05,1), mat(0x9dc7ff,{ roughness:0.6 }));
nuc.position.set(-2.15,1.5,0.25); nuc.scale.set(1,0.85,0.9); g.add(nuc);
const nucL = new THREE.LineSegments(new THREE.EdgesGeometry(new THREE.IcosahedronGeometry(1.05,1),15),
new THREE.LineBasicMaterial({color:0xd6e8ff, transparent:true, opacity:0.55}));
nucL.position.copy(nuc.position); nucL.scale.copy(nuc.scale); g.add(nucL);
const vg = new THREE.IcosahedronGeometry(0.34,0);
const vm = mat(0x8fd6ff);
[[2.1,-1.7],[1.4,2.2],[-1.2,-2.4],[2.6,0.6]].forEach(p=>{
const v = new THREE.Mesh(vg,vm); v.position.set(p[0],p[1],0.1); g.add(v);
});
return g;
}
function buildOrganelle(){
const g = new THREE.Group();
const ovoid = new THREE.IcosahedronGeometry(1.0,1);
const memb = new THREE.Mesh(ovoid, mat(0xffb454,{ transparent:true, opacity:0.42, side:THREE.DoubleSide, depthWrite:false }));
memb.scale.set(1.75,0.8,0.8); memb.rotation.z = 34*DEG; g.add(memb);
const edge = new THREE.LineSegments(new THREE.EdgesGeometry(ovoid,16), new THREE.LineBasicMaterial({color:0xffd08a, transparent:true, opacity:0.8}));
edge.scale.copy(memb.scale); edge.rotation.copy(memb.rotation); g.add(edge);
const cristae = new THREE.BoxGeometry(0.09,1.25,0.6);
const cm = mat(0xff7a45);
for(let i=0;i<4;i++){
const c = new THREE.Mesh(cristae,cm);
c.position.set(-0.95+i*0.63,0,0); c.rotation.z = 34*DEG; c.rotation.y = 0.25; g.add(c);
}
// highlight ring = the zoom window for the molecule frame
const pts=[]; for(let i=0;i<=48;i++){const a=i/48*Math.PI*2; pts.push(new THREE.Vector3(Math.cos(a)*0.62,Math.sin(a)*0.62,0.45));}
g.add(new THREE.Line(new THREE.BufferGeometry().setFromPoints(pts), new THREE.LineDashedMaterial({color:0xff6ec7, dashSize:0.08, gapSize:0.06, transparent:true, opacity:0.9})));
return g;
}
function buildMolecule(){
const g = new THREE.Group();
const A = [
{el:'P', p:[ 0.00, 0.00, 0.00], r:0.115, c:0xff9f43},
{el:'O', p:[ 0.25, 0.15, 0.00], r:0.088, c:0xff5c5c},
{el:'O', p:[-0.21, 0.21, 0.04], r:0.088, c:0xff5c5c},
{el:'O', p:[ 0.02,-0.25, 0.08], r:0.088, c:0xff5c5c},
{el:'C', p:[ 0.06, 0.41,-0.02], r:0.075, c:0xd6dee9},
{el:'N', p:[-0.27, 0.47, 0.06], r:0.075, c:0x6aa9ff},
{el:'C', p:[-0.49, 0.30,-0.02], r:0.075, c:0xd6dee9}
];
const B = [[0,1],[0,2],[0,3],[0,4],[4,5],[5,6],[6,2]];
const bondGeo = new THREE.CylinderGeometry(0.022,0.022,1,6,1);
const bondMat = mat(0x9fb2cc,{ roughness:0.5 });
const up = new THREE.Vector3(0,1,0), dir = new THREE.Vector3(), q = new THREE.Quaternion();
for(const [i,j] of B){
const a = new THREE.Vector3(...A[i].p), b = new THREE.Vector3(...A[j].p);
const m = new THREE.Mesh(bondGeo, bondMat);
m.position.copy(a).add(b).multiplyScalar(0.5);
dir.copy(b).sub(a); m.scale.y = dir.length(); q.setFromUnitVectors(up, dir.clone().normalize()); m.quaternion.copy(q);
g.add(m);
}
for(const a of A){
const m = new THREE.Mesh(new THREE.OctahedronGeometry(a.r,0), mat(a.c,{ roughness:0.45, metalness:0.15 }));
m.position.set(...a.p); g.add(m);
}
g.add(new THREE.Mesh(new THREE.IcosahedronGeometry(0.5,1),
new THREE.MeshBasicMaterial({ color:0xff6ec7, transparent:true, opacity:0.10, side:THREE.BackSide })));
return g;
}
/* ---------- build everything ---------- */
const contents = {
tissue: buildTissue(), cell: buildCell(), organelle: buildOrganelle(), molecule: buildMolecule()
};
LEVELS.forEach(L => { buildFrame(L); scene.add(contents[L.key]); });
/* ---------- zoom connectors (tissue → cell → organelle → molecule) ---------- */
const connTags = [];
for(let i=0;i<LEVELS.length-1;i++){
const a = LEVELS[i], b = LEVELS[i+1];
const pa = a._cornerTR.clone().applyAxisAngle(new THREE.Vector3(0,0,1), 0);
const pb = b._cornerTR.clone();
const geo = new THREE.BufferGeometry().setFromPoints([pa,pb]);
const line = new THREE.Line(geo, new THREE.LineDashedMaterial({ color:b.color, dashSize:0.9, gapSize:0.55, transparent:true, opacity:0.8 }));
line.computeLineDistances(); scene.add(line);
const cone = new THREE.Mesh(new THREE.ConeGeometry(0.28,0.85,6), mat(b.color,{ transparent:true, opacity:0.9 }));
cone.position.copy(pb); cone.position.z -= 0.55;
const d = pa.clone().sub(pb).normalize();
cone.quaternion.setFromUnitVectors(new THREE.Vector3(0,1,0), d);
scene.add(cone);
connTags.push({ mid: pa.clone().add(pb).multiplyScalar(0.5), text: b.mag });
}
/* ============================================================================
HTML TAGS
========================================================================== */
const tagHost = document.getElementById('tags');
const tags = [];
for(const L of LEVELS){
const el = document.createElement('div');
el.className = 'tag fade'; el.style.setProperty('--c', L.css);
el.innerHTML = `<div class="t">${L.name}</div><div class="s">${L.mag} · ${L.bar} bar (frame-local)</div>`;
tagHost.appendChild(el);
const bel = document.createElement('div');
bel.className = 'tag bar fade'; bel.style.setProperty('--c', L.css);
bel.innerHTML = `<span class="b">${L.bar}</span> <span class="bl">frame-local</span>`;
tagHost.appendChild(bel);
tags.push({ el, bel, L });
}
const xTags = connTags.map(c=>{
const el = document.createElement('div'); el.className='tag x fade';
el.textContent = c.text + ' (schematic)'; tagHost.appendChild(el);
return { el, mid:c.mid };
});
/* ---------- ladder panel rows ---------- */
const rowsEl = document.getElementById('rows');
const rowEls = LEVELS.map((L,i)=>{
const d = document.createElement('div'); d.className='row'; d.dataset.key = L.key;
d.innerHTML = `<span class="sw" style="background:${L.css}"></span>
<span><span class="nm" style="color:${L.css}">${L.name}</span><br><span class="bar">bar = ${L.bar}</span></span>
<span class="mag mono">${L.mag}</span>`;
d.addEventListener('click',()=>setFocus(L.key));
rowsEl.appendChild(d); return d;
});
const unitsEl = document.getElementById('units');
unitsEl.innerHTML = '<div style="margin-bottom:4px;color:#8a9ab8">What one drawn unit means at each level:</div>' +
LEVELS.map(L=>{
const drawn = L.size*BAR_FRAC, perUm = L.barUm/drawn;
let txt;
if(perUm >= 1) txt = perUm.toFixed(2)+' µm';
else if(perUm >= 0.001) txt = (perUm*1000).toFixed(1)+' nm';
else txt = (perUm*1e6).toFixed(0)+' pm';
return `<div><span style="color:${L.css}">${L.name.toLowerCase()}</span><span class="mono">1 unit = ${txt}</span></div>`;
}).join('');
/* ---------- view bar ---------- */
const viewbar = document.getElementById('viewbar');
const VIEWS = [{k:'all',t:'OVERVIEW'}].concat(LEVELS.map(L=>({k:L.key,t:L.name})));
const btnEls = VIEWS.map(v=>{
const b = document.createElement('button'); b.textContent = v.t;
b.addEventListener('click',()=>setFocus(v.k)); viewbar.appendChild(b); return b;
});
const statusEl = document.getElementById('status');
/* ============================================================================
FOCUS / MAGNIFICATION STATE (each level = its own magnification)
========================================================================== */
let focus = 'all';
function setFocus(key){
focus = key;
LEVELS.forEach(L=>{
contents[L.key].visible = (key==='all' || key===L.key);
const on = (key==='all' || key===L.key);
L._outlineMat.opacity = on ? 0.95 : 0.32;
L._bracketMat.opacity = on ? 1.0 : 0.35;
L._back.material.opacity = on ? 0.035 : 0.012;
L._barMats.forEach(m=>{ m.opacity = on ? (m.isLineBasicMaterial?0.95:0.9) : 0.3; });
});
rowEls.forEach((r,i)=>r.classList.toggle('on', LEVELS[i].key===key));
btnEls.forEach((b,i)=>b.classList.toggle('on', VIEWS[i].k===key));
const L = LEVELS.find(l=>l.key===key);
statusEl.innerHTML = key==='all'
? `<b>OVERVIEW</b> — four schematic frames, each with its own scale bar. Nothing here is to a common scale.`
: `<b>${L.name}</b> — magnification <b>${L.mag}</b>, ${L.zoomNote}. The other three frames are <b>not</b> at this magnification.`;
// camera move
const target = { r: L ? L.focus : 78, phi: 1.46, theta: 0 };
tweenTo(target);
}
/* ============================================================================
CUSTOM CAMERA CONTROLS (no addons)
========================================================================== */
const sph = { r:78, phi:1.46, theta:0 };
const tgt = new THREE.Vector3(0,0,0);
let tween = null;
function tweenTo(to){
tween = { from:{ r:sph.r, phi:sph.phi, theta:sph.theta, t:tgt.clone() },
to:{ r:to.r, phi:to.phi, theta:to.theta, t:new THREE.Vector3(0,0,0) }, t:0 };
}
function applyCamera(){
const sp = Math.sin(sph.phi);
camera.position.set(
tgt.x + sph.r*sp*Math.sin(sph.theta),
tgt.y + sph.r*Math.cos(sph.phi),
tgt.z + sph.r*sp*Math.cos(sph.theta)
);
camera.lookAt(tgt);
}
const pointers = new Map();
let dragging=false, panning=false, lx=0, ly=0, pinchDist=0;
canvas.addEventListener('contextmenu', e=>e.preventDefault());
canvas.addEventListener('pointerdown', e=>{
canvas.setPointerCapture(e.pointerId);
pointers.set(e.pointerId,{x:e.clientX,y:e.clientY});
if(pointers.size===1){ dragging=true; panning = e.shiftKey || e.button===2; lx=e.clientX; ly=e.clientY; tween=null; }
else if(pointers.size===2){
const [a,b]=[...pointers.values()]; pinchDist = Math.hypot(a.x-b.x,a.y-b.y); dragging=false;
}
});
canvas.addEventListener('pointermove', e=>{
if(!pointers.has(e.pointerId)) return;
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){ sph.r = THREE.MathUtils.clamp(sph.r * (pinchDist/d), 0.8, 260); }
pinchDist=d; applyCamera(); return;
}
if(!dragging) return;
const dx=e.clientX-lx, dy=e.clientY-ly; lx=e.clientX; ly=e.clientY;
if(panning){
const k = 2*sph.r*Math.tan(THREE.MathUtils.degToRad(camera.fov/2))/innerHeight;
const right=new THREE.Vector3().setFromMatrixColumn(camera.matrix,0);
const up=new THREE.Vector3().setFromMatrixColumn(camera.matrix,1);
tgt.addScaledVector(right,-dx*k).addScaledVector(up,dy*k);
}else{
sph.theta -= dx*0.005;
sph.phi = THREE.MathUtils.clamp(sph.phi - dy*0.005, 0.16, Math.PI-0.16);
}
applyCamera();
});
function endPointer(e){ pointers.delete(e.pointerId); if(pointers.size===0){dragging=false;panning=false;} }
canvas.addEventListener('pointerup', endPointer);
canvas.addEventListener('pointercancel', endPointer);
canvas.addEventListener('wheel', e=>{ e.preventDefault(); tween=null; sph.r = THREE.MathUtils.clamp(sph.r*Math.exp(e.deltaY*0.0012),0.8,260); applyCamera(); },{passive:false});
addEventListener('resize', ()=>{
camera.aspect = innerWidth/innerHeight; camera.updateProjectionMatrix();
renderer.setSize(innerWidth, innerHeight);
});
/* ============================================================================
TAG PROJECTION
========================================================================== */
const tmpV = new THREE.Vector3(), camDir = new THREE.Vector3();
function place(el, world){
tmpV.copy(world).project(camera);
camDir.copy(camera.position).sub(world).dot(camera.getWorldDirection(new THREE.Vector3()));
const vis = camDir > 0 && tmpV.z < 1 && Math.abs(tmpV.x)<1.25 && Math.abs(tmpV.y)<1.25;
const x = (tmpV.x*0.5+0.5)*innerWidth, y = (-tmpV.y*0.5+0.5)*innerHeight;
el.style.transform = `translate3d(${x}px,${y}px,0) translate(-50%,-100%)`;
el.style.opacity = vis ? 1 : 0;
}
function rotZ(v, L){ return new THREE.Vector3(v.x*Math.cos(L.rot)-v.y*Math.sin(L.rot), v.x*Math.sin(L.rot)+v.y*Math.cos(L.rot), v.z); }
/* ============================================================================
LOOP
========================================================================== */
const clock = new THREE.Clock();
function tick(){
const dt = Math.min(clock.getDelta(), 0.05);
if(tween){
tween.t = Math.min(1, tween.t + dt/0.9);
const e = tween.t<0.5 ? 4*tween.t**3 : 1-Math.pow(-2*tween.t+2,3)/2;
sph.r = THREE.MathUtils.lerp(tween.from.r, tween.to.r, e);
sph.phi = THREE.MathUtils.lerp(tween.from.phi, tween.to.phi, e);
sph.theta= THREE.MathUtils.lerp(tween.from.theta,tween.to.theta,e);
tgt.lerpVectors(tween.from.t, tween.to.t, e);
applyCamera();
if(tween.t>=1) tween=null;
}
for(const t of tags){ place(t.el, rotZ(t.L._tagLocal, t.L)); place(t.bel, rotZ(t.L._barLocal, t.L)); }
for(const t of xTags) place(t.el, t.mid);
renderer.render(scene, camera);
}
renderer.setAnimationLoop(tick);
setFocus('all');
applyCamera();
</script>
</body>
</html>
```