File size: 19,016 Bytes
9bcd283
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Robot Ego Patrol Map</title>
<style>
:root{--bg:#0b0e12;--panel:rgba(20,24,30,.86);--ink:#e3e7ec;--muted:#929ba7;--line:#2c333d;--accent:#7d9cf0;--auto:#4fd1c5;--idle:#8a95a5;--stall:#f2a541;--goal:#ff6b8a}
*{box-sizing:border-box}
html,body{margin:0;height:100%;background:var(--bg);color:var(--ink);font:13px/1.45 -apple-system,"Helvetica Neue",sans-serif;overflow:hidden}
#view{position:fixed;inset:0}
#view canvas{display:block;width:100%;height:100%;touch-action:none}
.panel{position:fixed;background:var(--panel);border:1px solid var(--line);border-radius:10px;padding:10px 12px;backdrop-filter:blur(6px)}
#info{top:12px;left:12px;max-width:360px}
#info h1{font-size:15px;margin:0 0 4px}
#info .stat{color:var(--muted)}
#info .badges{display:flex;flex-wrap:wrap;gap:4px;margin:6px 0}
#info .badges span{background:#222831;border-radius:5px;padding:1px 6px;font-size:11.5px}
.row{display:flex;gap:6px;flex-wrap:wrap;margin-top:4px;align-items:center}
.lbl{margin-top:8px}
button{background:#222831;color:var(--ink);border:1px solid var(--line);border-radius:6px;padding:4px 9px;font:inherit;cursor:pointer}
button.on{border-color:var(--accent);color:var(--accent)}
button:focus-visible,input:focus-visible{outline:2px solid var(--accent);outline-offset:1px}
input[type=range]{accent-color:var(--accent)}
#clip{width:120px}
#state{margin-top:8px;display:grid;grid-template-columns:auto 1fr;gap:1px 10px;font-size:12px}
#state dt{color:var(--muted)}#state dd{margin:0;text-align:right;font-variant-numeric:tabular-nums}
.pill{display:inline-block;padding:0 7px;border-radius:9px;font-size:11.5px}
.pill.auto{background:rgba(79,209,197,.18);color:var(--auto)}.pill.idle{background:rgba(138,149,165,.22)}.pill.stall{background:rgba(242,165,65,.2);color:var(--stall)}
#pip{top:12px;right:12px;width:min(36vw,420px);padding:6px}
#pip video{width:100%;display:block;border-radius:6px;background:#000;aspect-ratio:16/9}
#pip .cap{color:var(--muted);font-size:11.5px;margin-top:3px}
#bar{left:12px;right:12px;bottom:12px;display:grid;grid-template-columns:auto auto 1fr auto;gap:10px;align-items:center}
#bar input[type=range]{width:100%}
#chart{grid-column:1 / -1;width:100%;height:56px;display:block}
#time{font-variant-numeric:tabular-nums;color:var(--muted);min-width:110px;text-align:right}
.legend{display:flex;flex-wrap:wrap;gap:4px 12px;color:var(--muted);font-size:11.5px;margin-top:6px}
.legend i{display:inline-block;width:10px;height:3px;vertical-align:middle;margin-right:4px}
#msg{position:fixed;left:50%;top:50%;transform:translate(-50%,-50%);color:var(--muted)}
@media (max-width:720px){#pip{width:46vw}#info{max-width:50vw;font-size:12px}#state{display:none}}
</style>
<script src="../ss.js"></script><script>addEventListener("DOMContentLoaded",()=>SS.hydrate())</script>
<script type="importmap">
{"imports":{"three":"https://cdn.jsdelivr.net/npm/three@0.160.0/build/three.module.js",
"three/addons/":"https://cdn.jsdelivr.net/npm/three@0.160.0/examples/jsm/"}}
</script>
</head>
<body>
<div id="view"></div>
<div id="msg">Loading LiDAR map…</div>
<div class="panel" id="info">
  <h1>Robot egocentric · office patrol → 3D LiDAR map + motion</h1>
  <div class="stat" id="stats"></div>
  <div class="badges"><span>360° LiDAR</span><span>LiDAR-inertial odometry</span><span>per-point deskew</span><span>moving people removed</span><span>nav goals + modes</span></div>
  <div class="lbl">Colour</div>
  <div class="row" id="colors"><button data-c="h" class="on">Height</button><button data-c="i">Reflectivity</button></div>
  <div class="lbl">Display</div>
  <div class="row"><label for="clip">Cut above</label><input id="clip" type="range" min="0.3" max="3.8" step="0.05" value="2.2"><span id="clipv" style="color:var(--muted)"></span></div>
  <div class="row"><button id="t-floor" class="on">Floor</button><button id="t-edl" class="on">Depth shading</button></div>
  <div class="lbl">View</div>
  <div class="row" id="views"><button data-view="orbit">Overview</button><button data-view="top">Top</button><button data-view="follow">Follow robot</button></div>
  <div class="legend"><span><i style="background:var(--auto)"></i>Path, autonomous</span><span><i style="background:var(--idle)"></i>Path, idle</span><span><i style="background:var(--stall)"></i>Stalled</span><span><i style="background:var(--goal)"></i>Nav goal</span></div>
  <dl id="state">
    <dt>Mode</dt><dd id="s-mode"></dd>
    <dt>Speed / command</dt><dd id="s-v"></dd>
    <dt>Distance so far</dt><dd id="s-d"></dd>
  </dl>
</div>
<div class="panel" id="pip">
  <video id="vid" data-ss-src="b3/data/front_camera.mp4" muted playsinline preload="auto"></video>
  <div class="cap">Front camera (faces blurred), synced with the 3D timeline</div>
</div>
<div class="panel" id="bar">
  <button id="play">▶ Play</button>
  <button id="speed" title="Playback speed">4×</button>
  <input id="slider" type="range" min="0" max="8905" value="0" step="1" aria-label="Frame">
  <span id="time">0.0 s</span>
  <canvas id="chart"></canvas>
</div>
<script type="module">
import * as THREE from 'three';
import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
THREE.ColorManagement.enabled = false;
const $ = id => document.getElementById(id);
const V = '';
const BG = 0x0b0e12, FPS = 30;

const meta = await (await fetch('data/trajectory.json' + V)).json();
const T = meta.traj, N = T.t.length, DUR = meta.duration, FLOOR = meta.floor, CX = meta.center[0], CY = meta.center[1];
const NF = Math.round(DUR * FPS) + 1; $('slider').max = NF - 1;

/* ---------- renderer + eye-dome lighting ---------- */
const view = $('view');
const renderer = new THREE.WebGLRenderer({ antialias: false });
renderer.outputColorSpace = THREE.LinearSRGBColorSpace;
const PR = Math.min(devicePixelRatio, 2); renderer.setPixelRatio(PR);
view.appendChild(renderer.domElement);
const scene = new THREE.Scene(); scene.fog = new THREE.Fog(BG, 28, 75);
const camera = new THREE.PerspectiveCamera(45, 1, 0.1, 300);
const controls = new OrbitControls(camera, renderer.domElement);
controls.enableDamping = true; controls.dampingFactor = 0.12; controls.maxPolarAngle = Math.PI * 0.495; controls.minDistance = 1.5; controls.maxDistance = 90;
const P = (x, y, z) => new THREE.Vector3(x, z, -y);

let rt = null;
const edl = new THREE.ShaderMaterial({
  uniforms: { tColor: { value: null }, tDepth: { value: null }, px: { value: new THREE.Vector2() }, near: { value: camera.near }, far: { value: camera.far }, bg: { value: new THREE.Color(BG) }, on: { value: 1 } },
  vertexShader: 'varying vec2 vUv;void main(){vUv=uv;gl_Position=vec4(position.xy,0.,1.);}',
  fragmentShader: `varying vec2 vUv;uniform sampler2D tColor,tDepth;uniform vec2 px;uniform float near,far,on;uniform vec3 bg;
  float lz(vec2 uv){float d=texture2D(tDepth,uv).x;if(d>=1.0)return log2(far);float n=d*2.-1.;return log2((2.*near*far)/(far+near-n*(far-near)));}
  void main(){vec4 c=texture2D(tColor,vUv);if(c.a<0.5){gl_FragColor=vec4(bg,1.);return;}
    if(on<0.5){gl_FragColor=vec4(c.rgb,1.);return;}
    float z0=lz(vUv),s=0.;
    s+=max(0.,z0-lz(vUv+vec2(1.5,0)*px));s+=max(0.,z0-lz(vUv+vec2(-1.5,0)*px));s+=max(0.,z0-lz(vUv+vec2(0,1.5)*px));s+=max(0.,z0-lz(vUv+vec2(0,-1.5)*px));
    s+=max(0.,z0-lz(vUv+vec2(1.05,1.05)*px));s+=max(0.,z0-lz(vUv+vec2(-1.05,1.05)*px));s+=max(0.,z0-lz(vUv+vec2(1.05,-1.05)*px));s+=max(0.,z0-lz(vUv+vec2(-1.05,-1.05)*px));
    gl_FragColor=vec4(c.rgb*exp(-s/8.*260.),1.);}`,
  depthTest: false, depthWrite: false });
const quadScene = new THREE.Scene(), quadCam = new THREE.OrthographicCamera(-1, 1, 1, -1, 0, 1);
quadScene.add(new THREE.Mesh(new THREE.PlaneGeometry(2, 2), edl));
function resize() {
  const w = innerWidth, h = innerHeight; renderer.setSize(w, h); camera.aspect = w / h; camera.updateProjectionMatrix();
  if (rt) { rt.depthTexture.dispose(); rt.dispose(); }
  const W = Math.max(1, Math.floor(w * PR)), H = Math.max(1, Math.floor(h * PR));
  const dt = new THREE.DepthTexture(W, H); dt.type = THREE.UnsignedIntType;
  rt = new THREE.WebGLRenderTarget(W, H, { minFilter: THREE.NearestFilter, magFilter: THREE.NearestFilter, depthTexture: dt });
  edl.uniforms.tColor.value = rt.texture; edl.uniforms.tDepth.value = dt; edl.uniforms.px.value.set(1 / W, 1 / H);
  drawChart();
}
addEventListener('resize', resize);

/* ---------- point cloud (binary PLY: float x,y,z, uchar intensity, uchar floor; sorted by z) ---------- */
const buf = await (await fetch('data/scene_lidar.ply' + V)).arrayBuffer();
const head = new TextDecoder().decode(new Uint8Array(buf, 0, 600));
const hEnd = head.indexOf('end_header\n') + 11, NP = +head.match(/element vertex (\d+)/)[1];
const dv = new DataView(buf, hEnd);
const pos = new Float32Array(NP * 3), colH = new Float32Array(NP * 3), colI = new Float32Array(NP * 3), zs = new Float32Array(NP);
const lin = h => [(h >> 16 & 255) / 255, (h >> 8 & 255) / 255, (h & 255) / 255].map(v => Math.pow(v, 2.2));
const stops = [[0, lin(0x2a5d7a)], [0.25, lin(0x2f8fa3)], [0.5, lin(0x67c9bf)], [0.7, lin(0xcfe6dc)], [0.85, lin(0xf0c27a)], [1, lin(0xe0805c)]];
function ramp(t) { t = Math.max(0, Math.min(1, t)); for (let k = 1; k < stops.length; k++) if (t <= stops[k][0]) { const a = stops[k - 1], b = stops[k], f = (t - a[0]) / (b[0] - a[0]); return [0, 1, 2].map(j => a[1][j] + (b[1][j] - a[1][j]) * f); } return stops.at(-1)[1]; }
const tmp = new THREE.Color();
let floorEnd = 0;
for (let n = 0; n < NP; n++) {
  const o = n * 14, x = dv.getFloat32(o, true), y = dv.getFloat32(o + 4, true), z = dv.getFloat32(o + 8, true), it = dv.getUint8(o + 12), fl = dv.getUint8(o + 13);
  pos[n * 3] = x; pos[n * 3 + 1] = z; pos[n * 3 + 2] = -y; zs[n] = z; if (fl) floorEnd = n + 1;
  const g = Math.pow(Math.min(1, it / 110), 0.6);
  let c; if (fl) { const v = 0.035 + 0.05 * g; c = [v, v * 1.08, v * 1.25]; } else c = ramp((z - FLOOR - 0.05) / 2.6);
  colH.set(c.map(v => Math.pow(v, 1 / 2.2)), n * 3);
  tmp.setHSL(0.55 - 0.47 * g, 0.55, 0.16 + 0.55 * g); colI[n * 3] = tmp.r; colI[n * 3 + 1] = tmp.g; colI[n * 3 + 2] = tmp.b;
}
const geo = new THREE.BufferGeometry();
geo.setAttribute('position', new THREE.BufferAttribute(pos, 3));
const colAttr = new THREE.BufferAttribute(colH.slice(), 3); geo.setAttribute('color', colAttr);
const pmat = new THREE.PointsMaterial({ size: 0.05, sizeAttenuation: true, vertexColors: true });
const cloud = new THREE.Points(geo, pmat); cloud.frustumCulled = false; scene.add(cloud);
$('msg').remove();
$('stats').textContent = `${(NP / 1e6).toFixed(2)} M points · 4 cm cells · ${DUR.toFixed(0)} s · ${meta.goals.length} nav goals`;

let showFloor = true;
function upper(v) { let lo = 0, hi = NP; while (lo < hi) { const m = (lo + hi) >> 1; if (zs[m] <= v) lo = m + 1; else hi = m; } return lo; }
function applyRange() { const top = upper(FLOOR + +$('clip').value), st = showFloor ? 0 : floorEnd; geo.setDrawRange(st, Math.max(0, top - st)); $('clipv').textContent = (+$('clip').value).toFixed(2) + ' m'; }
$('clip').oninput = applyRange; applyRange();
$('t-floor').onclick = e => { showFloor = !showFloor; e.target.classList.toggle('on', showFloor); applyRange(); };
$('t-edl').onclick = e => { const on = !edl.uniforms.on.value; edl.uniforms.on.value = on ? 1 : 0; e.target.classList.toggle('on', on); };
document.querySelectorAll('#colors button').forEach(b => b.onclick = () => {
  colAttr.array.set(b.dataset.c === 'h' ? colH : colI); colAttr.needsUpdate = true;
  document.querySelectorAll('#colors button').forEach(x => x.classList.toggle('on', x === b)); });

/* ---------- path, goals, robot ---------- */
const stalls = meta.segs.filter(s => s.label === 'stalled');
const inStall = t => stalls.some(s => t >= s.s && t <= s.e);
const cum = new Float32Array(N); for (let k = 1; k < N; k++) cum[k] = cum[k - 1] + Math.hypot(T.x[k] - T.x[k - 1], T.y[k] - T.y[k - 1]);
const stateAt = k => inStall(T.t[k]) ? 2 : (T.mode[k] === 1 ? 1 : 0);
const COL = [0x8a95a5, 0x4fd1c5, 0xf2a541];
for (let k = 0, s = 0; k <= N; k++) {
  if (k === N || stateAt(k) !== stateAt(s)) {
    const e = Math.min(k, N - 1), pts = [];
    for (let j = s; j <= e; j += 2) pts.push(P(T.x[j], T.y[j], FLOOR + 0.08));
    pts.push(P(T.x[e], T.y[e], FLOOR + 0.08));
    if (pts.length >= 2 && cum[e] - cum[s] > 0.05)
      scene.add(new THREE.Mesh(new THREE.TubeGeometry(new THREE.CatmullRomCurve3(pts), Math.max(4, pts.length * 2), 0.045, 6, false), new THREE.MeshBasicMaterial({ color: COL[stateAt(s)], fog: false })));
    s = k;
  }
}
const fmt = s => { const m = Math.floor(s / 60); return m + ':' + (s - 60 * m).toFixed(1).padStart(4, '0'); };
meta.goals.forEach((g, i) => {
  const mat = new THREE.MeshBasicMaterial({ color: 0xff6b8a, fog: false, side: THREE.DoubleSide });
  const pole = new THREE.Mesh(new THREE.CylinderGeometry(0.03, 0.03, 1.6, 8), mat); pole.position.copy(P(g.x, g.y, FLOOR + 0.8)); scene.add(pole);
  const ring = new THREE.Mesh(new THREE.RingGeometry(0.35, 0.45, 40), mat); ring.rotation.x = -Math.PI / 2; ring.position.copy(P(g.x, g.y, FLOOR + 0.03)); scene.add(ring);
  const cv = document.createElement('canvas'); cv.width = 320; cv.height = 64; const cx = cv.getContext('2d');
  cx.font = '500 30px ui-monospace, Menlo, monospace'; cx.fillStyle = '#ff6b8a'; cx.fillText('goal ' + (i + 1) + ' · set ' + fmt(g.t), 6, 42);
  const sp = new THREE.Sprite(new THREE.SpriteMaterial({ map: new THREE.CanvasTexture(cv), depthTest: false, fog: false })); sp.scale.set(2.5, 0.5, 1); sp.position.copy(P(g.x, g.y, FLOOR + 1.9 + i * 0.45)); scene.add(sp);
});
const robot = new THREE.Group();
robot.add(new THREE.Mesh(new THREE.BoxGeometry(0.7, 0.3, 0.35), new THREE.MeshBasicMaterial({ color: 0xffffff, fog: false })));
const nose = new THREE.Mesh(new THREE.ConeGeometry(0.14, 0.35, 16), new THREE.MeshBasicMaterial({ color: 0x4fd1c5, fog: false })); nose.rotation.z = -Math.PI / 2; nose.position.x = 0.52; robot.add(nose);
const halo = new THREE.Mesh(new THREE.RingGeometry(0.6, 0.66, 48), new THREE.MeshBasicMaterial({ color: 0xffffff, transparent: true, opacity: 0.6, side: THREE.DoubleSide, fog: false })); halo.rotation.x = -Math.PI / 2; halo.position.y = -0.15; robot.add(halo);
scene.add(robot);

/* ---------- views ---------- */
let follow = false;
function setView(v) {
  follow = v === 'follow';
  if (v === 'orbit') { camera.position.copy(P(CX - 9, CY - 15, 12)); controls.target.copy(P(CX + 1, CY, FLOOR + 0.3)); }
  if (v === 'top') { camera.position.copy(P(CX, CY + 0.01, 42)); controls.target.copy(P(CX, CY, FLOOR)); }
  if (v === 'follow') { const k = cur, yw = T.yaw[k]; camera.position.copy(P(T.x[k] - 3.5 * Math.cos(yw), T.y[k] - 3.5 * Math.sin(yw), T.z[k] + 1.8)); controls.target.copy(P(T.x[k], T.y[k], T.z[k])); }
  document.querySelectorAll('#views button').forEach(b => b.classList.toggle('on', b.dataset.view === v));
}
document.querySelectorAll('#views button').forEach(b => b.onclick = () => setView(b.dataset.view));

/* ---------- timeline chart: speed vs command, mode bands ---------- */
const chart = $('chart');
function drawChart() {
  const r = chart.getBoundingClientRect(), W = Math.max(10, r.width * PR), H = 56 * PR; chart.width = W; chart.height = H;
  const c = chart.getContext('2d'), X = t => t / DUR * W, vmax = 0.6, Y = v => H - 4 * PR - Math.max(0, Math.min(vmax, v)) / vmax * (H - 10 * PR);
  c.fillStyle = 'rgba(79,209,197,.10)';
  for (let k = 0, s = 0; k <= N; k++) if (k === N || T.mode[k] !== T.mode[s]) { if (T.mode[s] === 1) c.fillRect(X(T.t[s]), 0, X(T.t[Math.min(k, N - 1)]) - X(T.t[s]), H); s = k; }
  c.fillStyle = 'rgba(242,165,65,.75)'; stalls.forEach(s => c.fillRect(X(s.s), H - 3 * PR, X(s.e) - X(s.s), 3 * PR));
  c.fillStyle = '#ff6b8a'; meta.goals.forEach(g => c.fillRect(X(g.t) - PR, 0, 2 * PR, H));
  const line = (arr, col, dash) => { c.strokeStyle = col; c.lineWidth = 1.3 * PR; c.setLineDash(dash); c.beginPath(); for (let k = 0; k < N; k++) { const x = X(T.t[k]), y = Y(arr[k]); k ? c.lineTo(x, y) : c.moveTo(x, y); } c.stroke(); c.setLineDash([]); };
  line(T.cmd, 'rgba(146,155,167,.9)', [4 * PR, 3 * PR]); line(T.v, '#4fd1c5', []);
  c.fillStyle = '#929ba7'; c.font = `${10 * PR}px -apple-system, sans-serif`; c.fillText('forward speed (solid) vs command (dashed), 0–0.6 m/s · amber = stalled · pink = goal set', 6 * PR, 11 * PR);
  c.fillStyle = '#fff'; c.fillRect(X(T.t[cur]) - PR / 2, 0, PR, H);
}

/* ---------- playback: the video is the clock while playing ---------- */
const vid = $('vid'), slider = $('slider');
let cur = 0, frame = 0, playing = false;
const SPEEDS = [1, 2, 4, 8]; let spd = 4;
function idxAt(t) { let lo = 0, hi = N - 1; while (lo < hi) { const m = (lo + hi + 1) >> 1; if (T.t[m] <= t) lo = m; else hi = m - 1; } return lo; }
function setFrame(f, seekVideo) {
  frame = Math.max(0, Math.min(NF - 1, f)); const t = frame / FPS; cur = idxAt(t); const k = cur;
  const prev = robot.position.clone();
  robot.position.copy(P(T.x[k], T.y[k], T.z[k])); robot.rotation.y = T.yaw[k];
  if (follow) { const d = robot.position.clone().sub(prev); controls.target.add(d); camera.position.add(d); }
  slider.value = frame; $('time').textContent = `${t.toFixed(1)} s / ${DUR.toFixed(0)} s`;
  const st = inStall(t);
  $('s-mode').innerHTML = st ? '<span class="pill stall">auto · stalled</span>' : T.mode[k] === 1 ? '<span class="pill auto">autonomous</span>' : '<span class="pill idle">idle</span>';
  $('s-v').textContent = `${T.v[k].toFixed(2)} / ${T.cmd[k].toFixed(2)} m/s`;
  $('s-d').textContent = cum[k].toFixed(1) + ' m';
  if (seekVideo && vid.readyState >= 1) vid.currentTime = Math.min(vid.duration || DUR, t + 0.5 / FPS);
  drawChart();
}
slider.oninput = () => { setFrame(+slider.value, true); };
$('play').onclick = () => {
  playing = !playing; $('play').textContent = playing ? '❚❚ Pause' : '▶ Play';
  if (playing) { if (frame >= NF - 1) setFrame(0, true); vid.playbackRate = spd; vid.play().catch(() => {}); } else vid.pause();
};
$('speed').onclick = () => { spd = SPEEDS[(SPEEDS.indexOf(spd) + 1) % SPEEDS.length]; $('speed').textContent = spd + '×'; vid.playbackRate = spd; };
vid.addEventListener('ended', () => { playing = false; $('play').textContent = '▶ Play'; });
vid.addEventListener('loadedmetadata', () => setFrame(frame, true));

resize(); setView('orbit'); setFrame(Math.round(150 * FPS), true);
let last = performance.now();
renderer.setAnimationLoop(now => {
  const dt = (now - last) / 1000; last = now;
  if (playing) {
    if (vid.readyState >= 2 && !vid.paused) setFrame(Math.round(vid.currentTime * FPS), false);
    else if (vid.readyState < 2) setFrame(frame + Math.round(dt * FPS * spd) || frame + 1, false);
  }
  halo.scale.setScalar(1 + 0.15 * Math.sin(now / 300));
  controls.update();
  renderer.setRenderTarget(rt); renderer.setClearColor(BG, 0); renderer.clear(); renderer.render(scene, camera);
  renderer.setRenderTarget(null); renderer.render(quadScene, quadCam);
});
</script>
</body>
</html>