Spaces:
Running
Running
Download static/index.html from AK51/4DGussianView: direct link, hf CLI and curl.
- Browser
- Download file 29 kB
-
https://huggingface.co/spaces/AK51/4DGussianView/resolve/main/static/index.html
- Command line
-
hf download hf://spaces/AK51/4DGussianView/static/index.html
-
curl -L -o index.html https://huggingface.co/spaces/AK51/4DGussianView/resolve/main/static/index.html
29 kB
| <html lang="en"> | |
| <head> | |
| <meta charset="UTF-8" /> | |
| <meta name="viewport" content="width=device-width, initial-scale=1.0" /> | |
| <title>3D Gaussian 4D — Video Frame Stack Visualizer</title> | |
| <style> | |
| :root { | |
| --panel-bg: rgba(18, 20, 28, 0.92); | |
| --accent: #4ea1ff; | |
| --accent-2: #ff6ec7; | |
| --text: #e8ecf4; | |
| --muted: #9aa4b8; | |
| --border: rgba(255, 255, 255, 0.08); | |
| } | |
| * { box-sizing: border-box; } | |
| html, body { | |
| margin: 0; height: 100%; overflow: hidden; | |
| font-family: "Segoe UI", system-ui, -apple-system, sans-serif; | |
| background: #05060a; color: var(--text); | |
| } | |
| #canvas-holder { position: fixed; inset: 0; } | |
| canvas { display: block; } | |
| /* Control panel */ | |
| #panel { | |
| position: fixed; top: 16px; left: 16px; width: 320px; | |
| max-height: calc(100vh - 32px); overflow-y: auto; | |
| background: var(--panel-bg); backdrop-filter: blur(10px); | |
| border: 1px solid var(--border); border-radius: 14px; | |
| padding: 18px; z-index: 10; | |
| box-shadow: 0 12px 40px rgba(0,0,0,0.5); | |
| } | |
| #panel h1 { | |
| font-size: 16px; margin: 0 0 2px; letter-spacing: 0.3px; | |
| background: linear-gradient(90deg, var(--accent), var(--accent-2)); | |
| -webkit-background-clip: text; background-clip: text; color: transparent; | |
| } | |
| #panel .sub { font-size: 11px; color: var(--muted); margin-bottom: 14px; } | |
| #panelHeader { | |
| display: flex; align-items: flex-start; justify-content: space-between; gap: 10px; | |
| margin-bottom: 6px; | |
| } | |
| #panelHeader h1 { margin: 0; } | |
| #panelHeader #panelSub { margin-bottom: 0; } | |
| #panelToggle { | |
| width: 28px; height: 28px; min-width: 28px; padding: 0; flex: 0 0 auto; | |
| font-size: 18px; line-height: 1; font-weight: 700; color: var(--text); | |
| background: #1a1f2b; border: 1px solid var(--border); border-radius: 8px; | |
| } | |
| #panelToggle:hover { transform: none; background: #232a3a; } | |
| /* Collapsed state: hide the body, shrink the panel to just its header. */ | |
| #panel.collapsed { width: auto; } | |
| #panel.collapsed #panelBody { display: none; } | |
| #panel.collapsed #panelSub { display: none; } | |
| #panel.collapsed #panelHeader { margin-bottom: 0; } | |
| #panelBody { transition: opacity 0.15s ease; } | |
| .group { margin-bottom: 14px; padding-bottom: 14px; border-bottom: 1px solid var(--border); } | |
| .group:last-child { border-bottom: none; } | |
| label { display: block; font-size: 12px; color: var(--muted); margin-bottom: 6px; } | |
| .row { display: flex; align-items: center; justify-content: space-between; gap: 8px; margin-bottom: 8px; } | |
| .val { font-size: 12px; color: var(--accent); font-variant-numeric: tabular-nums; } | |
| input[type="range"] { width: 100%; accent-color: var(--accent); } | |
| input[type="number"], select { | |
| width: 100%; padding: 7px 9px; border-radius: 8px; | |
| border: 1px solid var(--border); background: #0d1018; color: var(--text); | |
| font-size: 13px; | |
| } | |
| .file-row { display: flex; gap: 8px; align-items: center; } | |
| input[type="file"] { font-size: 11px; color: var(--muted); width: 100%; } | |
| button { | |
| width: 100%; padding: 10px; border: none; border-radius: 10px; cursor: pointer; | |
| font-size: 13px; font-weight: 600; color: #05060a; | |
| background: linear-gradient(90deg, var(--accent), var(--accent-2)); | |
| transition: transform 0.08s ease, opacity 0.2s ease; | |
| } | |
| button:hover { transform: translateY(-1px); } | |
| button:disabled { opacity: 0.5; cursor: not-allowed; transform: none; } | |
| button.secondary { | |
| background: #1a1f2b; color: var(--text); border: 1px solid var(--border); | |
| font-weight: 500; | |
| } | |
| .checkbox-row { display: flex; align-items: center; gap: 8px; margin-bottom: 8px; } | |
| .checkbox-row input { accent-color: var(--accent); } | |
| .checkbox-row label { margin: 0; color: var(--text); font-size: 12px; } | |
| #status { | |
| font-size: 12px; color: var(--muted); margin-top: 8px; min-height: 16px; | |
| line-height: 1.4; | |
| } | |
| #status.error { color: #ff7a7a; } | |
| #status.ok { color: #7affa0; } | |
| .hint { font-size: 10.5px; color: var(--muted); margin-top: 4px; line-height: 1.4; } | |
| .credit { | |
| margin-top: 14px; padding-top: 12px; border-top: 1px solid var(--border); | |
| text-align: center; font-size: 11px; color: var(--muted); letter-spacing: 0.4px; | |
| } | |
| .credit::before { content: "✦ "; color: var(--accent); } | |
| /* Loading bar */ | |
| #bar-wrap { height: 4px; background: rgba(255,255,255,0.08); border-radius: 3px; overflow: hidden; margin-top: 8px; display: none; } | |
| #bar { height: 100%; width: 0%; background: linear-gradient(90deg, var(--accent), var(--accent-2)); transition: width 0.15s ease; } | |
| /* Overlay legend */ | |
| #legend { | |
| position: fixed; bottom: 16px; left: 16px; z-index: 10; | |
| background: var(--panel-bg); border: 1px solid var(--border); border-radius: 10px; | |
| padding: 10px 14px; font-size: 11px; color: var(--muted); line-height: 1.6; | |
| } | |
| #legend b { color: var(--text); } | |
| kbd { | |
| background: #1a1f2b; border: 1px solid var(--border); border-radius: 4px; | |
| padding: 1px 5px; font-size: 10px; color: var(--text); | |
| } | |
| /* Live preview of the focused frame (upper right), user-resizable */ | |
| #preview { | |
| position: fixed; top: 16px; right: 16px; z-index: 10; | |
| width: 240px; min-width: 140px; max-width: 90vw; | |
| background: var(--panel-bg); backdrop-filter: blur(10px); | |
| border: 1px solid var(--border); border-radius: 12px; | |
| padding: 10px; box-shadow: 0 12px 40px rgba(0,0,0,0.5); | |
| display: none; | |
| /* native resize handle (bottom-left so it doesn't run off-screen at top-right) */ | |
| resize: horizontal; overflow: hidden; direction: rtl; | |
| } | |
| #preview > * { direction: ltr; } /* keep content left-to-right */ | |
| #preview .title { | |
| font-size: 11px; color: var(--muted); margin-bottom: 8px; | |
| display: flex; justify-content: space-between; align-items: center; | |
| } | |
| #preview .title b { color: var(--accent); font-variant-numeric: tabular-nums; } | |
| #preview .frame-wrap { | |
| position: relative; width: 100%; border-radius: 8px; overflow: hidden; | |
| background: #000; border: 2px solid var(--accent); | |
| display: flex; align-items: center; justify-content: center; | |
| min-height: 60px; | |
| } | |
| #preview img { width: 100%; display: block; } | |
| #preview .time { font-size: 10.5px; color: var(--muted); margin-top: 6px; text-align: center; } | |
| /* Explicit size slider row */ | |
| #preview .size-row { display: flex; align-items: center; gap: 8px; margin-bottom: 8px; } | |
| #preview .size-row label { margin: 0; font-size: 10.5px; color: var(--muted); white-space: nowrap; } | |
| #preview .size-row input[type="range"] { flex: 1; accent-color: var(--accent); } | |
| </style> | |
| </head> | |
| <body> | |
| <div id="canvas-holder"></div> | |
| <div id="panel"> | |
| <div id="panelHeader"> | |
| <div> | |
| <h1>3D Gaussian · 4D Stack</h1> | |
| <div class="sub" id="panelSub">Stack video frames along Z to view time as a volume.</div> | |
| </div> | |
| <button id="panelToggle" title="Minimize panel" aria-label="Minimize panel">–</button> | |
| </div> | |
| <div id="panelBody"> | |
| <div class="group"> | |
| <label>Video source</label> | |
| <div class="checkbox-row"> | |
| <input type="checkbox" id="useSample" checked /> | |
| <label for="useSample">Use bundled sample (Lunar.mp4)</label> | |
| </div> | |
| <div class="file-row" id="fileRow" style="display:none;"> | |
| <input type="file" id="videoFile" accept="video/*" /> | |
| </div> | |
| <div class="hint">Upload an MP4/MOV/WebM, or use the sample.</div> | |
| </div> | |
| <div class="group"> | |
| <div class="row"> | |
| <label for="numFrames" style="margin:0;">Number of frames</label> | |
| <span class="val" id="numFramesVal">200</span> | |
| </div> | |
| <input type="range" id="numFrames" min="2" max="500" step="1" value="200" /> | |
| <div class="hint">Frames are sampled evenly across the video's timeline.</div> | |
| <button id="loadBtn" style="margin-top:10px;">Extract & Visualize</button> | |
| <div id="bar-wrap"><div id="bar"></div></div> | |
| <div id="status"></div> | |
| </div> | |
| <div class="group"> | |
| <div class="row"> | |
| <label for="opacity" style="margin:0;">Global transparency</label> | |
| <span class="val" id="opacityVal">0.55</span> | |
| </div> | |
| <input type="range" id="opacity" min="0.02" max="1" step="0.01" value="0.55" /> | |
| <div class="row" style="margin-top:10px;"> | |
| <label for="spacing" style="margin:0;">Z spacing</label> | |
| <span class="val" id="spacingVal">1.0</span> | |
| </div> | |
| <input type="range" id="spacing" min="0.2" max="6" step="0.1" value="1.0" /> | |
| </div> | |
| <div class="group"> | |
| <div class="checkbox-row"> | |
| <input type="checkbox" id="cursorFx" checked /> | |
| <label for="cursorFx">Cursor-along-Z transparency effect</label> | |
| </div> | |
| <div class="row"> | |
| <label for="focusWidth" style="margin:0;">Focus band width</label> | |
| <span class="val" id="focusWidthVal">6</span> | |
| </div> | |
| <input type="range" id="focusWidth" min="1" max="40" step="1" value="6" /> | |
| <div class="hint">Move the mouse up/down over the scene to sweep a bright focus band along the Z stack.</div> | |
| </div> | |
| <div class="group"> | |
| <button class="secondary" id="resetView">Reset camera</button> | |
| </div> | |
| <div class="credit">Created by Andy Kong</div> | |
| </div><!-- /panelBody --> | |
| </div> | |
| <div id="legend"> | |
| <div><b>Drag</b> to orbit · <b>Scroll</b> to zoom · <b>Right-drag</b> to pan</div> | |
| <div>Move mouse <b>up/down</b> to sweep the focus band along <kbd>Z</kbd></div> | |
| <div id="frameInfo"></div> | |
| </div> | |
| <div id="preview"> | |
| <div class="title"><span>Focused frame</span><b id="previewIdx">—</b></div> | |
| <div class="size-row"> | |
| <label for="previewSize">Size</label> | |
| <input type="range" id="previewSize" min="140" max="640" step="10" value="240" /> | |
| </div> | |
| <div class="frame-wrap"><img id="previewImg" alt="focused frame preview" /></div> | |
| <div class="time" id="previewTime"></div> | |
| </div> | |
| <script type="importmap"> | |
| { | |
| "imports": { | |
| "three": "https://unpkg.com/three@0.160.0/build/three.module.js", | |
| "three/addons/": "https://unpkg.com/three@0.160.0/examples/jsm/" | |
| } | |
| } | |
| </script> | |
| <script type="module"> | |
| import * as THREE from 'three'; | |
| import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; | |
| // ---------------------------------------------------------------- scene setup | |
| const holder = document.getElementById('canvas-holder'); | |
| const scene = new THREE.Scene(); | |
| scene.background = new THREE.Color(0x05060a); | |
| const camera = new THREE.PerspectiveCamera(50, window.innerWidth / window.innerHeight, 0.1, 5000); | |
| const DEFAULT_CAM = new THREE.Vector3(90, 55, 120); | |
| camera.position.copy(DEFAULT_CAM); | |
| const renderer = new THREE.WebGLRenderer({ antialias: true }); | |
| renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2)); | |
| renderer.setSize(window.innerWidth, window.innerHeight); | |
| holder.appendChild(renderer.domElement); | |
| const controls = new OrbitControls(camera, renderer.domElement); | |
| controls.enableDamping = true; | |
| controls.dampingFactor = 0.08; | |
| scene.add(new THREE.AmbientLight(0xffffff, 1.0)); | |
| // group that holds all the frame planes; we recenter it so orbiting feels natural | |
| const stackGroup = new THREE.Group(); | |
| scene.add(stackGroup); | |
| // axes + grid helpers | |
| let helpers = new THREE.Group(); | |
| scene.add(helpers); | |
| // ---------------------------------------------------------------- state | |
| let frameMeshes = []; // { mesh, material, z } | |
| let stackDepth = 0; // total z span | |
| let planeW = 0, planeH = 0; // world size of each plane | |
| const texLoader = new THREE.TextureLoader(); | |
| const params = { | |
| opacity: 0.55, | |
| spacing: 1.0, | |
| cursorFx: true, | |
| focusWidth: 6, | |
| }; | |
| // normalized cursor position over the canvas (0..1 vertical), drives focus band | |
| // (used as a fallback when the cursor isn't directly over a frame) | |
| let cursorZNorm = 0.5; | |
| let hasCursor = false; | |
| // Raycasting: find which frame the cursor is actually pointing at, so the | |
| // highlight and focus band align with the image under the cursor at ANY camera angle. | |
| const raycaster = new THREE.Raycaster(); | |
| const pointerNDC = new THREE.Vector2(); // normalized device coords (-1..1) | |
| const _tmpV = new THREE.Vector3(); // scratch vector reused each frame | |
| let pointerInside = false; | |
| let focusIdx = 0; // current focused frame index (float, smoothed) | |
| let focusIdxTarget = 0; // where the focus wants to be this frame | |
| let hasEverPicked = false;// has the cursor landed on a frame at least once? | |
| let lastPreviewIdx = -1; // avoid reloading the preview image every frame | |
| // White highlight border drawn around the focused frame. | |
| let highlightBox = null; | |
| // ---------------------------------------------------------------- UI wiring | |
| const $ = (id) => document.getElementById(id); | |
| const statusEl = $('status'); | |
| const barWrap = $('bar-wrap'); | |
| const bar = $('bar'); | |
| function setStatus(msg, kind) { | |
| statusEl.textContent = msg || ''; | |
| statusEl.className = kind || ''; | |
| } | |
| $('useSample').addEventListener('change', (e) => { | |
| $('fileRow').style.display = e.target.checked ? 'none' : 'flex'; | |
| }); | |
| $('numFrames').addEventListener('input', (e) => { $('numFramesVal').textContent = e.target.value; }); | |
| $('opacity').addEventListener('input', (e) => { | |
| params.opacity = parseFloat(e.target.value); | |
| $('opacityVal').textContent = params.opacity.toFixed(2); | |
| }); | |
| $('spacing').addEventListener('input', (e) => { | |
| params.spacing = parseFloat(e.target.value); | |
| $('spacingVal').textContent = params.spacing.toFixed(1); | |
| layoutStack(); | |
| }); | |
| $('cursorFx').addEventListener('change', (e) => { params.cursorFx = e.target.checked; }); | |
| $('focusWidth').addEventListener('input', (e) => { | |
| params.focusWidth = parseInt(e.target.value, 10); | |
| $('focusWidthVal').textContent = params.focusWidth; | |
| }); | |
| $('resetView').addEventListener('click', () => { | |
| camera.position.copy(DEFAULT_CAM); | |
| controls.target.set(0, 0, 0); | |
| controls.update(); | |
| }); | |
| // Minimize / restore the left control panel. When minimized, shift the whole | |
| // 3D view toward the lower-left of the screen (into the space the menu freed), | |
| // so the origin/axes sit near the lower-left corner. | |
| let menuMinimized = false; | |
| $('panelToggle').addEventListener('click', () => { | |
| const panel = $('panel'); | |
| menuMinimized = panel.classList.toggle('collapsed'); | |
| const btn = $('panelToggle'); | |
| btn.textContent = menuMinimized ? '+' : '–'; | |
| btn.title = menuMinimized ? 'Expand panel' : 'Minimize panel'; | |
| applyViewShift(); | |
| }); | |
| // Uses camera.setViewOffset to pan the rendered scene without touching the | |
| // camera transform or OrbitControls target. | |
| function applyViewShift() { | |
| const w = window.innerWidth, h = window.innerHeight; | |
| if (menuMinimized) { | |
| // +offsetX moves content left; -offsetY moves content down. | |
| const dx = Math.min(w * 0.28, 320); // shift left toward freed menu space | |
| const dy = -h * 0.22; // shift down toward the bottom | |
| camera.setViewOffset(w, h, dx, dy, w, h); | |
| } else { | |
| camera.clearViewOffset(); | |
| } | |
| } | |
| // Resize the preview window via slider (sets its width; height follows aspect). | |
| $('previewSize').addEventListener('input', (e) => { | |
| $('preview').style.width = e.target.value + 'px'; | |
| }); | |
| $('loadBtn').addEventListener('click', loadAndBuild); | |
| // True when the pointer is hovering over a UI panel (left menu or preview), | |
| // in which case we must NOT drive the Gaussian focus — otherwise it jitters. | |
| let pointerOverUI = false; | |
| function isOverUI(e) { | |
| const el = document.elementFromPoint(e.clientX, e.clientY); | |
| return !!(el && el.closest && el.closest('#panel, #preview, #legend')); | |
| } | |
| // Track cursor in normalized device coords for raycasting, plus a vertical | |
| // fallback value used only when the ray misses every frame. | |
| renderer.domElement.addEventListener('mousemove', (e) => { | |
| pointerOverUI = isOverUI(e); | |
| if (pointerOverUI) { | |
| // Freeze the effect while over UI: keep last focus, mark cursor inactive. | |
| pointerInside = false; | |
| return; | |
| } | |
| const rect = renderer.domElement.getBoundingClientRect(); | |
| pointerNDC.x = ((e.clientX - rect.left) / rect.width) * 2 - 1; | |
| pointerNDC.y = -((e.clientY - rect.top) / rect.height) * 2 + 1; | |
| pointerInside = true; | |
| cursorZNorm = 1 - (e.clientY - rect.top) / rect.height; // fallback mapping | |
| cursorZNorm = Math.min(1, Math.max(0, cursorZNorm)); | |
| hasCursor = true; | |
| }); | |
| // Because the panels sit above the canvas, moving onto them may not fire the | |
| // canvas 'mousemove'. Listen at the window level too, to catch UI hover. | |
| window.addEventListener('mousemove', (e) => { | |
| const over = isOverUI(e); | |
| if (over) { pointerOverUI = true; pointerInside = false; } | |
| }); | |
| renderer.domElement.addEventListener('mouseleave', () => { | |
| hasCursor = false; pointerInside = false; hasEverPicked = false; | |
| }); | |
| // ---------------------------------------------------------------- build stack | |
| async function loadAndBuild() { | |
| const btn = $('loadBtn'); | |
| btn.disabled = true; | |
| setStatus('Extracting frames on server…'); | |
| barWrap.style.display = 'block'; | |
| bar.style.width = '8%'; | |
| const fd = new FormData(); | |
| fd.append('numFrames', $('numFrames').value); | |
| const useSample = $('useSample').checked; | |
| fd.append('useSample', useSample ? 'true' : 'false'); | |
| if (!useSample) { | |
| const f = $('videoFile').files[0]; | |
| if (!f) { setStatus('Please choose a video file first.', 'error'); btn.disabled = false; barWrap.style.display='none'; return; } | |
| fd.append('video', f); | |
| } | |
| let manifest; | |
| try { | |
| const res = await fetch('/api/extract', { method: 'POST', body: fd }); | |
| if (!res.ok) { | |
| const err = await res.json().catch(() => ({})); | |
| throw new Error(err.error || `Server error ${res.status}`); | |
| } | |
| manifest = await res.json(); | |
| } catch (err) { | |
| setStatus('Extraction failed: ' + err.message, 'error'); | |
| btn.disabled = false; barWrap.style.display = 'none'; | |
| return; | |
| } | |
| setStatus(`Got ${manifest.count} frames. Loading textures…`); | |
| await buildStack(manifest, (p) => { bar.style.width = (10 + p * 88).toFixed(0) + '%'; }); | |
| bar.style.width = '100%'; | |
| setStatus(`Done — ${manifest.count} frames stacked (${manifest.width}×${manifest.height}px each).`, 'ok'); | |
| $('frameInfo').innerHTML = `<b>${manifest.count}</b> frames · source total <b>${manifest.totalFrames}</b>`; | |
| setTimeout(() => { barWrap.style.display = 'none'; bar.style.width = '0%'; }, 600); | |
| btn.disabled = false; | |
| } | |
| function disposeStack() { | |
| lastPreviewIdx = -1; | |
| hasEverPicked = false; | |
| const preview = $('preview'); | |
| if (preview) preview.style.display = 'none'; | |
| for (const f of frameMeshes) { | |
| stackGroup.remove(f.mesh); | |
| f.mesh.geometry.dispose(); | |
| f.material.map && f.material.map.dispose(); | |
| f.material.dispose(); | |
| } | |
| frameMeshes = []; | |
| } | |
| function loadTexture(url) { | |
| return new Promise((resolve, reject) => { | |
| texLoader.load(url, (tex) => { | |
| tex.colorSpace = THREE.SRGBColorSpace; | |
| tex.minFilter = THREE.LinearFilter; | |
| tex.magFilter = THREE.LinearFilter; | |
| resolve(tex); | |
| }, undefined, reject); | |
| }); | |
| } | |
| async function buildStack(manifest, onProgress) { | |
| disposeStack(); | |
| // World size of each plane: normalize width to a fixed extent, keep aspect. | |
| const aspect = manifest.aspect || (manifest.width / manifest.height) || 1; | |
| planeW = 60; | |
| planeH = planeW / aspect; | |
| const geo = new THREE.PlaneGeometry(planeW, planeH); | |
| // Shift geometry so the plane's LOWER-LEFT corner sits at the local origin, | |
| // and the image lies flat on the X-Y plane (requirement #4). | |
| geo.translate(planeW / 2, planeH / 2, 0); | |
| const total = manifest.frames.length; | |
| let done = 0; | |
| // Load textures with limited concurrency to keep the browser responsive. | |
| const CONCURRENCY = 8; | |
| let cursor = 0; | |
| async function worker() { | |
| while (cursor < total) { | |
| const i = cursor++; | |
| const fr = manifest.frames[i]; | |
| let tex; | |
| try { tex = await loadTexture(fr.url); } | |
| catch { done++; if (onProgress) onProgress(done / total); continue; } | |
| const mat = new THREE.MeshBasicMaterial({ | |
| map: tex, | |
| transparent: true, | |
| opacity: params.opacity, | |
| side: THREE.DoubleSide, | |
| depthWrite: false, // proper blending for stacked transparency | |
| }); | |
| const mesh = new THREE.Mesh(geo, mat); | |
| mesh.renderOrder = i; | |
| stackGroup.add(mesh); | |
| frameMeshes[i] = { mesh, material: mat, z: 0, url: fr.url, time: fr.time }; | |
| done++; | |
| if (onProgress) onProgress(done / total); | |
| } | |
| } | |
| await Promise.all(Array.from({ length: CONCURRENCY }, worker)); | |
| // Compact any holes from failed loads. | |
| frameMeshes = frameMeshes.filter(Boolean); | |
| buildHighlight(); | |
| layoutStack(); | |
| frameStackHelpers(); | |
| } | |
| // A thick white rectangular border that we snap onto the focused frame. | |
| // Built from four boxes (top/bottom/left/right) so the border has real thickness. | |
| function buildHighlight() { | |
| if (highlightBox) { | |
| stackGroup.remove(highlightBox); | |
| highlightBox.traverse((o) => { o.geometry && o.geometry.dispose(); o.material && o.material.dispose(); }); | |
| highlightBox = null; | |
| } | |
| const t = Math.max(0.6, planeW * 0.012); // border thickness | |
| const mat = new THREE.MeshBasicMaterial({ | |
| color: 0xffffff, | |
| transparent: true, | |
| opacity: 1.0, | |
| depthTest: false, // always visible, even behind other frames | |
| }); | |
| const grp = new THREE.Group(); | |
| const mkBar = (w, h) => new THREE.Mesh(new THREE.BoxGeometry(w, h, t), mat); | |
| // Border sits around a plane whose lower-left is at local (0,0). Center is (planeW/2, planeH/2). | |
| const cx = planeW / 2, cy = planeH / 2; | |
| const top = mkBar(planeW + t * 2, t); top.position.set(cx, planeH + t / 2, 0); | |
| const bot = mkBar(planeW + t * 2, t); bot.position.set(cx, -t / 2, 0); | |
| const left = mkBar(t, planeH + t * 2); left.position.set(-t / 2, cy, 0); | |
| const right = mkBar(t, planeH + t * 2); right.position.set(planeW + t / 2, cy, 0); | |
| grp.add(top, bot, left, right); | |
| grp.renderOrder = 9999; | |
| grp.visible = false; | |
| highlightBox = grp; | |
| stackGroup.add(highlightBox); | |
| } | |
| // Position each frame along Z using the current spacing, then recenter the | |
| // group so the middle of the stack is at the world origin for easy orbiting. | |
| function layoutStack() { | |
| if (!frameMeshes.length) return; | |
| const n = frameMeshes.length; | |
| for (let i = 0; i < n; i++) { | |
| const z = i * params.spacing; // frame 0 at z=0, frame k at z=k*spacing | |
| frameMeshes[i].z = z; | |
| frameMeshes[i].mesh.position.set(0, 0, z); | |
| } | |
| stackDepth = (n - 1) * params.spacing; | |
| // Recenter group: lower-left of first frame is at local (0,0,0); we offset the | |
| // whole group so the visual centroid is near the world origin. | |
| stackGroup.position.set(-planeW / 2, -planeH / 2, -stackDepth / 2); | |
| // keep the true (0,0,0) origin discoverable: helpers live in world space | |
| frameStackHelpers(); | |
| } | |
| // axes + grid, sized to the current stack | |
| function frameStackHelpers() { | |
| scene.remove(helpers); | |
| helpers = new THREE.Group(); | |
| const span = Math.max(planeW, planeH, stackDepth, 20); | |
| // Axis lines drawn from the TRUE origin (0,0,0) = lower-left of first frame, | |
| // shown in the group's local frame so they line up with the images. | |
| const originGroup = new THREE.Group(); | |
| originGroup.position.copy(stackGroup.position); | |
| const axisLen = span * 1.15; | |
| const mkAxis = (dir, color) => { | |
| const g = new THREE.BufferGeometry().setFromPoints([ | |
| new THREE.Vector3(0, 0, 0), | |
| dir.clone().multiplyScalar(axisLen), | |
| ]); | |
| return new THREE.Line(g, new THREE.LineBasicMaterial({ color })); | |
| }; | |
| originGroup.add(mkAxis(new THREE.Vector3(1, 0, 0), 0xff5555)); // X red | |
| originGroup.add(mkAxis(new THREE.Vector3(0, 1, 0), 0x55ff77)); // Y green | |
| originGroup.add(mkAxis(new THREE.Vector3(0, 0, 1), 0x5599ff)); // Z blue (time) | |
| // small marker cube at the true origin | |
| const originCube = new THREE.Mesh( | |
| new THREE.BoxGeometry(1.5, 1.5, 1.5), | |
| new THREE.MeshBasicMaterial({ color: 0xffffff }) | |
| ); | |
| originCube.position.set(0, 0, 0); | |
| originGroup.add(originCube); | |
| helpers.add(originGroup); | |
| scene.add(helpers); | |
| } | |
| // ---------------------------------------------------------------- render loop | |
| // Figure out which frame the cursor is pointing at using a raycast. Returns a | |
| // frame index (int) or -1 if the ray missed all frames. | |
| function pickFocusedFrame() { | |
| if (!frameMeshes.length || !pointerInside) return -1; | |
| raycaster.setFromCamera(pointerNDC, camera); | |
| const meshes = frameMeshes.map((f) => f.mesh); | |
| const hits = raycaster.intersectObjects(meshes, false); | |
| if (!hits.length) return -1; | |
| // Nearest hit = the frame the user is visually pointing at. | |
| const hitMesh = hits[0].object; | |
| return frameMeshes.findIndex((f) => f.mesh === hitMesh); | |
| } | |
| function updateFocusAndHighlight() { | |
| const n = frameMeshes.length; | |
| if (!n) return; | |
| // The effect is "active" when enabled and the cursor is in play. While the | |
| // cursor is over a UI panel we FREEZE (hold the last focus) instead of | |
| // recomputing, which previously made the Gaussian band jitter wildly. | |
| const effectOn = params.cursorFx && hasCursor; | |
| const frozen = pointerOverUI; | |
| // Determine target focus index (skipped entirely while frozen). | |
| if (effectOn && !frozen) { | |
| const picked = pickFocusedFrame(); | |
| if (picked >= 0) { | |
| // Cursor is over a frame: that frame is the target. | |
| focusIdxTarget = picked; | |
| hasEverPicked = true; | |
| } else if (!hasEverPicked) { | |
| // Cursor has never touched a frame yet: use vertical mapping as a seed. | |
| focusIdxTarget = cursorZNorm * (n - 1); | |
| } | |
| // else: cursor drifted just off a frame edge -> KEEP the last target so the | |
| // focused frame doesn't jump. It stays put until the cursor lands on another frame. | |
| // Smooth the motion so it eases toward the target instead of snapping. | |
| focusIdx += (focusIdxTarget - focusIdx) * 0.2; | |
| } | |
| // Opacity: Gaussian band centered on the focused frame. Frames that sit | |
| // BETWEEN the camera and the focused frame ("in front") are hidden so the | |
| // focused image isn't blocked. Front/back is decided by the camera's position | |
| // relative to the focused frame along the stack's Z axis (works at any orbit). | |
| if (effectOn) { | |
| const w = Math.max(0.5, params.focusWidth); | |
| const fi = Math.min(n - 1, Math.max(0, focusIdx)); | |
| // Is the camera on the +Z side of the focused frame? (higher indices = larger Z) | |
| const focusWorldZ = frameMeshes[Math.round(fi)].mesh.getWorldPosition(_tmpV).z; | |
| const cameraOnPlusZ = camera.position.z >= focusWorldZ; | |
| for (let i = 0; i < n; i++) { | |
| // A frame is "in front" of the focused one if it lies on the camera's side. | |
| const inFront = cameraOnPlusZ ? (i > fi + 0.5) : (i < fi - 0.5); | |
| if (inFront) { | |
| frameMeshes[i].material.opacity = 0; // hidden so it can't block | |
| frameMeshes[i].mesh.visible = false; | |
| continue; | |
| } | |
| frameMeshes[i].mesh.visible = true; | |
| const d = (i - fi) / w; | |
| const g = Math.exp(-0.5 * d * d); | |
| const o = params.opacity * (0.15 + 0.85 * g) + g * 0.15; | |
| frameMeshes[i].material.opacity = Math.min(1, o); | |
| } | |
| } else { | |
| for (let i = 0; i < n; i++) { | |
| frameMeshes[i].mesh.visible = true; | |
| frameMeshes[i].material.opacity = params.opacity; | |
| } | |
| } | |
| // Highlight border + live preview: snap onto the nearest focused frame. | |
| const preview = $('preview'); | |
| if (highlightBox) { | |
| if (effectOn) { | |
| const idx = Math.round(Math.min(n - 1, Math.max(0, focusIdx))); | |
| highlightBox.visible = true; | |
| highlightBox.position.z = frameMeshes[idx].z; | |
| // Report which frame is highlighted. | |
| const info = $('frameInfo'); | |
| if (info) { | |
| info.innerHTML = `Pointing at frame <b>#${idx + 1}</b> / ${n} · z = <b>${frameMeshes[idx].z.toFixed(1)}</b>`; | |
| } | |
| // Live preview (upper right) — update only when the focused frame changes. | |
| if (preview && idx !== lastPreviewIdx) { | |
| lastPreviewIdx = idx; | |
| const fr = frameMeshes[idx]; | |
| preview.style.display = 'block'; | |
| $('previewImg').src = fr.url; | |
| $('previewIdx').textContent = `#${idx + 1} / ${n}`; | |
| const t = fr.time; | |
| $('previewTime').textContent = (t != null) ? `t = ${t.toFixed(2)} s` : ''; | |
| } | |
| } else { | |
| highlightBox.visible = false; | |
| if (preview) preview.style.display = 'none'; | |
| lastPreviewIdx = -1; | |
| } | |
| } | |
| } | |
| function animate() { | |
| requestAnimationFrame(animate); | |
| controls.update(); | |
| updateFocusAndHighlight(); | |
| renderer.render(scene, camera); | |
| } | |
| animate(); | |
| window.addEventListener('resize', () => { | |
| camera.aspect = window.innerWidth / window.innerHeight; | |
| camera.updateProjectionMatrix(); | |
| renderer.setSize(window.innerWidth, window.innerHeight); | |
| applyViewShift(); // re-apply the shift with new viewport dimensions | |
| }); | |
| // Auto-run on startup with the bundled sample so the user sees the | |
| // visualization immediately, without clicking "Extract & Visualize". | |
| window.addEventListener('load', () => { | |
| setStatus('Loading sample video…'); | |
| $('useSample').checked = true; | |
| $('fileRow').style.display = 'none'; | |
| loadAndBuild(); | |
| }); | |
| </script> | |
| </body> | |
| </html> | |