/*! * JFVR (Jellyfin Web VR) — built bundle. DO NOT EDIT. * Edit src/*.js and run: python3 build.py * Modules: 00-core.js, 10-subtitles.js, 20-controls.js, 30-hands.js, 90-main.js */ /* ===== 00-core.js ===== */ /*! * JFVR core — shared namespace, state, helpers, registries. * Loaded first. Everything else attaches to window.__JFVR. * * See INTERFACES.md for the binding contract. */ (function () { 'use strict'; if (window.__JFVR && window.__JFVR.__core) return; // idempotent var NS = window.__JFVR = window.__JFVR || {}; NS.version = 'hud-9'; // hud-9 = ADDENDUM 10 (native/TextTrack subtitles painted by the HUD; layout override per-session, ADDENDUM 9 rAF broker + auto dual/mono layout) NS.__core = true; /* ------------------------------------------------------------------ * * logging * ------------------------------------------------------------------ */ NS.log = function () { var a = Array.prototype.slice.call(arguments); try { console.log.apply(console, ['%c[JFVR ' + NS.version + ']', 'color:#4ea1ff;font-weight:bold'].concat(a)); } catch (e) { try { console.log('[JFVR]', a.join(' ')); } catch (e2) {} } }; /* ------------------------------------------------------------------ * * shared state (modules read/write; integrator owns lifecycle) * ------------------------------------------------------------------ */ var S = NS.state = { video: null, session: null, refSpace: null, viewerSpace: null, projection: '180', // '180' | '360' stereo: 'sbs', // 'mono' | 'sbs' | 'ou' // ADDENDUM 8 — where the 180/360 window points. The video layer is an // equirect sphere with radius 0 (content "at infinity"), so rotating its // transform rotates the CONTENT about the eye point without any parallax: // exactly "pan the picture so I do not have to crane my neck". Only the video // rotates — the panel (refSpace, fixed pose) and the subtitles (viewer space, // head-locked) stay in front of the user. Radians. NOT persisted: the owner // wants a fresh forward-facing view every time VR starts. videoYaw: 0, // + = the picture has been moved to the RIGHT videoPitch: 0, // + = the picture has been moved UP subtitles: true, ctlLock: false, // ADDENDUM 7: control-bar lock badge (per VR session) subMode: 'hud', // 'hud' | 'sphere' | 'off' subLayout: 'auto', // ADDENDUM 9: 'auto' | 'dual' | 'mono' subtitle layout subAlpha: 'premultiplied', // 'premultiplied' | 'straight' subOpacity: 0.85, // 0..1, applied to the subtitle layer live // y is the quad CENTRE; negative = below eye level. -0.42 keeps the strip // clearly under the visual centre rather than crowding it. subPlacement: { y: -0.42, z: -1.45, width: 2.0 }, // ADDENDUM 6 — depth / size decoupling. With subLockAngle ON, changing the // strip's DEPTH holds its angular size (subAngleDeg, the strip's full // angular width in degrees) and its angular height (the y/z ratio), so the // user can pull the subtitles to the depth of a nearby object without the // text blowing up or dropping out of the lower view. 69.2 deg is exactly // the pre-ADDENDUM-6 look: 2.0 m wide at 1.45 m. subLockAngle: true, subAngleDeg: 69.2, controls: true, ctlAutoHideMs: 4000, // in-VR control bar auto-hide // written every XR frame by 90-main.js, read by modules pointer: null, // { visible, source, origin:{x,y,z}, dir:{x,y,z}, hit:{nx,ny}|null } stats: {}, errors: [] }; // live handles (mirrors of state, convenient for modules) Object.defineProperty(NS, 'session', { get: function () { return S.session; }, set: function (v) { S.session = v; } }); Object.defineProperty(NS, 'video', { get: function () { return S.video; }, set: function (v) { S.video = v; } }); Object.defineProperty(NS, 'refSpace', { get: function () { return S.refSpace; }, set: function (v) { S.refSpace = v; } }); Object.defineProperty(NS, 'viewerSpace', { get: function () { return S.viewerSpace; }, set: function (v) { S.viewerSpace = v; } }); NS.recordError = function (where, e) { var msg = where + ': ' + ((e && e.message) || e); S.errors.push(msg); if (S.errors.length > 40) S.errors.shift(); return msg; }; /* ------------------------------------------------------------------ * * DOM helpers * ------------------------------------------------------------------ */ NS.makeEl = function (tag, className, text) { var el = document.createElement(tag); if (className) el.className = className; if (text != null) el.textContent = text; return el; }; NS.clamp = function (v, lo, hi) { return v < lo ? lo : (v > hi ? hi : v); }; /* ------------------------------------------------------------------ * * GL helpers * ------------------------------------------------------------------ */ NS.compileShader = function (gl, type, src) { var sh = gl.createShader(type); gl.shaderSource(sh, src); gl.compileShader(sh); if (!gl.getShaderParameter(sh, gl.COMPILE_STATUS)) { var info = gl.getShaderInfoLog(sh) || 'shader compile failed'; gl.deleteShader(sh); throw new Error(info); } return sh; }; // Fullscreen triangle in clip space. Fragment shader gets `in vec2 vUv` // with vUv.y already flipped so (0,0) is the top-left of the source image. NS.QUAD_VS = [ '#version 300 es', 'in vec2 aPos;', 'out vec2 vUv;', 'void main(){', ' vUv = vec2(aPos.x * 0.5 + 0.5, 0.5 - aPos.y * 0.5);', ' gl_Position = vec4(aPos, 0.0, 1.0);', '}' ].join('\n'); NS.bindFullscreenTriangle = function (gl, program) { var buf = gl.createBuffer(); gl.bindBuffer(gl.ARRAY_BUFFER, buf); gl.bufferData(gl.ARRAY_BUFFER, new Float32Array([-1, -1, 3, -1, -1, 3]), gl.STATIC_DRAW); var loc = gl.getAttribLocation(program, 'aPos'); gl.enableVertexAttribArray(loc); gl.vertexAttribPointer(loc, 2, gl.FLOAT, false, 0, 0); return loc; }; NS.linkProgram = function (gl, vsSrc, fsSrc) { var prog = gl.createProgram(); gl.attachShader(prog, NS.compileShader(gl, gl.VERTEX_SHADER, vsSrc)); gl.attachShader(prog, NS.compileShader(gl, gl.FRAGMENT_SHADER, fsSrc)); gl.linkProgram(prog); if (!gl.getProgramParameter(prog, gl.LINK_STATUS)) { var info = gl.getProgramInfoLog(prog) || 'program link failed'; throw new Error(info); } gl.useProgram(prog); return prog; }; NS.makeTexture = function (gl) { var t = gl.createTexture(); gl.bindTexture(gl.TEXTURE_2D, t); gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR); gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR); gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE); gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE); return t; }; NS.xrGl = function (alpha) { var c = document.createElement('canvas'); return c.getContext('webgl2', { xrCompatible: true, alpha: alpha !== false, premultipliedAlpha: true, antialias: false, depth: false, desynchronized: true }); }; // 4x4 matrix inverse (column-major, as used by WebXR). NS.invert4 = function (m) { var a00 = m[0], a01 = m[1], a02 = m[2], a03 = m[3], a10 = m[4], a11 = m[5], a12 = m[6], a13 = m[7], a20 = m[8], a21 = m[9], a22 = m[10], a23 = m[11], a30 = m[12], a31 = m[13], a32 = m[14], a33 = m[15], b00 = a00 * a11 - a01 * a10, b01 = a00 * a12 - a02 * a10, b02 = a00 * a13 - a03 * a10, b03 = a01 * a12 - a02 * a11, b04 = a01 * a13 - a03 * a11, b05 = a02 * a13 - a03 * a12, b06 = a20 * a31 - a21 * a30, b07 = a20 * a32 - a22 * a30, b08 = a20 * a33 - a23 * a30, b09 = a21 * a32 - a22 * a31, b10 = a21 * a33 - a23 * a31, b11 = a22 * a33 - a23 * a32, det = b00 * b11 - b01 * b10 + b02 * b09 + b03 * b08 - b04 * b07 + b05 * b06; if (!det) return new Float32Array(16); det = 1.0 / det; return new Float32Array([ (a11 * b11 - a12 * b10 + a13 * b09) * det, (a02 * b10 - a01 * b11 - a03 * b09) * det, (a31 * b05 - a32 * b04 + a33 * b03) * det, (a22 * b04 - a21 * b05 - a23 * b03) * det, (a12 * b08 - a10 * b11 - a13 * b07) * det, (a00 * b11 - a02 * b08 + a03 * b07) * det, (a32 * b02 - a30 * b05 - a33 * b01) * det, (a20 * b05 - a22 * b02 + a23 * b01) * det, (a10 * b10 - a11 * b08 + a13 * b06) * det, (a01 * b08 - a00 * b10 - a03 * b06) * det, (a30 * b04 - a31 * b02 + a33 * b00) * det, (a21 * b02 - a20 * b04 - a23 * b00) * det, (a11 * b07 - a10 * b09 - a12 * b06) * det, (a00 * b09 - a01 * b07 + a02 * b06) * det, (a31 * b01 - a30 * b03 - a32 * b00) * det, (a20 * b03 - a21 * b01 + a22 * b00) * det ]); }; /* ------------------------------------------------------------------ * * XR layer + frame-hook registry * * Layers are owned by modules; main.js owns pushing the render state. * ------------------------------------------------------------------ */ var layers = []; var hooks = []; var pushRenderState = null; // set by main.js once a session exists NS.setRenderStatePusher = function (fn) { pushRenderState = fn; }; NS.addLayer = function (layer) { if (!layer || layers.indexOf(layer) >= 0) return layer; layers.push(layer); NS.log('layer added (' + layers.length + ' total)'); if (pushRenderState) { try { pushRenderState(); } catch (e) { NS.recordError('addLayer', e); } } return layer; }; NS.removeLayer = function (layer) { var i = layers.indexOf(layer); if (i < 0) return; layers.splice(i, 1); NS.log('layer removed (' + layers.length + ' left)'); if (pushRenderState) { try { pushRenderState(); } catch (e) { NS.recordError('removeLayer', e); } } }; NS.getLayers = function () { return layers.slice(); }; NS.clearLayers = function () { layers.length = 0; if (pushRenderState) { try { pushRenderState(); } catch (e) {} } }; NS.addFrameHook = function (fn) { if (typeof fn !== 'function' || hooks.indexOf(fn) >= 0) return fn; hooks.push(fn); return fn; }; NS.removeFrameHook = function (fn) { var i = hooks.indexOf(fn); if (i >= 0) hooks.splice(i, 1); }; NS.runFrameHooks = function (frame, time, session) { for (var i = 0; i < hooks.length; i++) { try { hooks[i](frame, time, session); } catch (e) { var msg = NS.recordError('frameHook', e); if (!S.stats.hookErr) S.stats.hookErr = msg; } } }; NS.clearFrameHooks = function () { hooks.length = 0; }; /* ------------------------------------------------------------------ * * stereo / projection helpers shared by modules * ------------------------------------------------------------------ */ NS.stereoLayout = function () { return S.stereo === 'sbs' ? 'stereo-left-right' : S.stereo === 'ou' ? 'stereo-top-bottom' : 'mono'; }; NS.centralAngle = function () { return S.projection === '360' ? Math.PI * 2 : Math.PI; }; /* ------------------------------------------------------------------ * * capabilities probe (safe at load time: no user gesture needed) * ------------------------------------------------------------------ */ NS.caps = { ua: navigator.userAgent, secureContext: !!window.isSecureContext, hasXR: !!navigator.xr, immersiveVR: null, hasXRMediaBinding: typeof window.XRMediaBinding !== 'undefined', hasEquirectLayer: false, hasXRWebGLBinding: typeof window.XRWebGLBinding !== 'undefined', hasHandTracking: typeof window.XRHand !== 'undefined', hasXRJointSpace: typeof window.XRJointSpace !== 'undefined', hasGetJointPose: typeof window.XRFrame !== 'undefined' && typeof XRFrame.prototype.getJointPose === 'function', maxTextureSize: null, hevc: {}, decoding8k: null }; (function probe() { try { var mb = window.XRMediaBinding; if (mb && mb.prototype) NS.caps.hasEquirectLayer = typeof mb.prototype.createEquirectLayer === 'function'; var wg = window.XRWebGLBinding; if (wg && wg.prototype) NS.caps.hasQuadLayer = typeof wg.prototype.createQuadLayer === 'function'; } catch (e) {} try { var gl = document.createElement('canvas').getContext('webgl2') || document.createElement('canvas').getContext('webgl'); if (gl) NS.caps.maxTextureSize = gl.getParameter(gl.MAX_TEXTURE_SIZE); } catch (e) {} try { ['video/mp4; codecs="hev1.1.6.L183.B0"', 'video/mp4; codecs="hvc1.1.6.L183.B0"', 'video/mp4; codecs="hev1"', 'video/mp4; codecs="hvc1"'].forEach(function (cs) { NS.caps.hevc[cs] = (typeof MediaSource !== 'undefined' && MediaSource.isTypeSupported(cs)) || document.createElement('video').canPlayType(cs) || 'no'; }); } catch (e) {} if (navigator.xr && navigator.xr.isSessionSupported) { navigator.xr.isSessionSupported('immersive-vr') .then(function (v) { NS.caps.immersiveVR = v; }) .catch(function (e) { NS.caps.immersiveVR = 'err:' + e.message; }); } })(); NS.capsDiag = function () { var c = NS.caps; return [ 'origin : ' + location.origin, 'secureContext : ' + c.secureContext, 'immersive-vr : ' + c.immersiveVR, 'XRMediaBinding : ' + c.hasXRMediaBinding, 'XRWebGLBinding : ' + c.hasXRWebGLBinding, 'createQuadLayer : ' + c.hasQuadLayer, 'hand tracking API : ' + c.hasHandTracking + ' / jointSpace=' + c.hasXRJointSpace + ' / getJointPose=' + c.hasGetJointPose, 'MAX_TEXTURE_SIZE : ' + c.maxTextureSize, 'HEVC : ' + JSON.stringify(c.hevc) ].join('\n'); }; })(); /* ===== 10-subtitles.js ===== */ /*! * JFVR subtitles — Jellyfin's libass canvas -> a WebXR layer. * * Default 'hud' mode: a head-locked mono quad layer in __JFVR.viewerSpace, fed * from the LEFT half of the libass SBS canvas and auto-cropped to the ink * bounding box so the strip is tight and sharp. * * Legacy 'sphere' mode: an equirect layer on __JFVR.refSpace that renders the * whole SBS canvas (kept for the A/B toggle). * * 'off' mode: no layer. * * See INTERFACES.md § "10-subtitles.js" for the binding contract. This file * only defines functions; it never touches the DOM at load time. */ (function () { 'use strict'; var NS = window.__JFVR = window.__JFVR || {}; var S = NS.state || {}; function log() { try { if (typeof NS.log === 'function') NS.log.apply(null, arguments); else if (window.console) { console.log.apply(console, ['[JFVR sub]'].concat(Array.prototype.slice.call(arguments))); } } catch (e) {} } function setErr(msg) { try { if (!S || !S.stats) return; S.stats.subErr = (msg == null) ? null : String(msg); } catch (e) {} } function isNum(v) { return typeof v === 'number' && isFinite(v); } function clamp01(v) { return v < 0 ? 0 : (v > 1 ? 1 : v); } function clampInt(v, lo, hi) { v = Math.round(v); return v < lo ? lo : (v > hi ? hi : v); } /* ---------------- tunables ---------------- */ var SAMPLE_W = 320; // bbox + change detector scratch (never the full canvas) var SAMPLE_H = 160; var SAMPLE_MS = 250; // ~4 Hz bbox / change recompute var UPLOAD_MS = 1000 / 30; // hard cap on texture uploads (~30 Hz) var INK_ALPHA = 10; // alpha (0..255) counted as subtitle ink var PAD_PX = 12; // padding around the ink bbox, in source pixels var QUANT = 4; // quantise the crop rect (source px) to limit reallocation var SHRINK_TOL = 0.02; // hysteresis: an edge must shrink >2% before it moves var DIFF_SAMPLES = 32; // changed scratch samples needed to count as "content changed" var DIFF_SUM = 24; // per-sample channel delta that counts as a changed sample var HUD_TEX_W = 2048; // target texture width (≈30 px/deg for a 2 m strip @ 1.45 m) var HUD_TEX_ASPECT = 0.28; // texH = texW * this (room for multi-line subtitles) // ADDENDUM 9 — dual-eye vs normal (mono) subtitle layout, auto-detected. // The libass canvas holds EITHER a stereo left/right pair (two copies, one per // eye; the left half is the left eye) OR a single normal subtitle positioned // across the whole frame. // // IMPORTANT: dual-eye is NOT a pixel mirror. Both eyes carry the SAME text at // mirrored POSITIONS; the glyphs are not flipped, so comparing left[x] with // right[W-1-x] fails badly (measured mean delta ~258/1020, i.e. "not a mirror"). // What IS true is that the two halves' ink COLUMN profiles match under a small // horizontal shift, because they are the same line of text. Measured on device: // dual -> normalised copy distance 0.02, mono -> 1.00, seam ink present only for // mono. So the copy test is the discriminator. var CENTER_BAND_PX = 40; // source px around the eye seam that count as "centre ink" var COPY_TOL = 0.25; // column-profile copy distance for "two copies of one line" // (measured: dual 0.02-0.08, mono 1.00) // ADDENDUM 10 — Jellyfin only routes ssa/ass through SubtitlesOctopus (the // libass canvas). Every other text codec (subrip, vtt, ...) is drawn by the // BROWSER from a TextTrack (`video.addTextTrack` + mode 'showing'), so there is // no canvas at all to sample. The HUD then paints the active cue itself into a // private canvas and feeds it through the same crop/upload pipeline. var NATIVE_W = 1920, NATIVE_H = 1080; var NATIVE_FONT_H = 0.052; // cue font size as a fraction of the canvas height var NATIVE_LINE_H = 1.32; // line advance var NATIVE_MAX_W = 0.9; // max text width as a fraction of the canvas width var SPHERE_TEX_W = 4096; // legacy equirect layer size var SPHERE_TEX_H = 2048; var OPACITY_DEFAULT = 0.85; // dim the strip below full opacity by default var POS_STEP = 0.005; // placement quantisation (5 mm) so jitter does not thrash var WIDTH_STEP = 0.01; // width quantisation (10 mm) var MIN_WIDTH = 0.2; // never let the strip collapse (free mode) // Hard placement bounds. These live HERE and not only in 90-main.js so the // invariant holds no matter who calls setPlacement(). The near clamp is 0.35 m // (ADDENDUM 6): the owner's whole point is bringing the strip as close as the // object being watched, and objects in VR180 footage can be well under 0.5 m. var MIN_Y = -0.95, MAX_Y = -0.05; var MIN_Z = -2.6, MAX_Z = -0.35; var LEGACY_CORE_Y = -0.34; // pre-ADDENDUM-2 00-core.js default; treated as "unset" var DEFAULT_PLACEMENT = { y: -0.42, z: -1.45, width: 2.0 }; // resetPlacement() target /* ---------------- ADDENDUM 6: angular-size lock ---------------- * width = 2 * |z| * tan(theta/2) (theta = the strip's full angular width) * The quad's height is width * texH/texW, so holding theta also holds the * strip's angular height. Free mode keeps the old "pull closer = bigger". * MIN_WIDTH would defeat the lock at close range, so the lock uses a much * smaller floor: the angle itself is already clamped to ANGLE_MIN. */ var ANGLE_MIN = 20, ANGLE_MAX = 120; // locked strip angular width, degrees var MIN_WIDTH_LOCK = 0.05; function deg2rad(d) { return d * Math.PI / 180; } function rad2deg(r) { return r * 180 / Math.PI; } function widthForAngle(deg, z) { return 2 * Math.abs(z) * Math.tan(deg2rad(deg) / 2); } function angleForWidth(w, z) { return rad2deg(2 * Math.atan((w / 2) / Math.max(1e-6, Math.abs(z)))); } function clampAngle(d) { return d < ANGLE_MIN ? ANGLE_MIN : (d > ANGLE_MAX ? ANGLE_MAX : d); } var DEFAULT_ANGLE_DEG = clampAngle(angleForWidth(DEFAULT_PLACEMENT.width, DEFAULT_PLACEMENT.z)); /* ---------------- module state ---------------- */ var M = { mode: S.subMode || 'hud', alpha: S.subAlpha || 'premultiplied', // Quad CENTRE in viewer space. -0.42 keeps the strip clearly under the // visual centre (ADDENDUM 2). state.subPlacement is owned by 00-core.js; // this is the module fallback when core still carries the legacy -0.34. placement: { y: DEFAULT_PLACEMENT.y, z: DEFAULT_PLACEMENT.z, width: DEFAULT_PLACEMENT.width }, // ADDENDUM 6: locked angular width (degrees) + the y/z ratio it holds. lockAngle: (typeof S.subLockAngle === 'boolean') ? S.subLockAngle : true, angleDeg: isNum(S.subAngleDeg) ? clampAngle(S.subAngleDeg) : DEFAULT_ANGLE_DEG, k: DEFAULT_PLACEMENT.y / DEFAULT_PLACEMENT.z, opacity: isNum(S.subOpacity) ? clamp01(S.subOpacity) : OPACITY_DEFAULT, layerOpacitySupported: false, // runtime honours layer.opacity? quadMutable: false, // runtime allows in-place width/height/transform? prepared: false, styleBackup: null, session: null, canvas: null, gl: null, glCanvas: null, prog: null, tex: null, fbo: null, binding: null, layer: null, hook: null, sampleCanvas: null, sampleCtx: null, cropCanvas: null, cropCtx: null, srcW: 0, srcH: 0, halfW: 1, fullW: 1, eyeW: 1, texW: 0, texH: 0, // ADDENDUM 9: layout is the user setting ('auto' | 'dual' | 'mono'); dualEye // is the resolved result (auto-detected when layout === 'auto'). layout: (S.subLayout === 'dual' || S.subLayout === 'mono') ? S.subLayout : 'auto', dualEye: true, mismatch: 0, // consecutive samples disagreeing with dualEye (debounce) copyND: null, // diagnostics: normalised column-profile copy distance centerInk: null, // diagnostics: ink pixels in the seam band colL: null, colR: null, // reused column-profile scratch (half width each) // ADDENDUM 10: where the HUD's source pixels come from. srcKind: 'none', // 'libass' | 'native' | 'none' nativeCanvas: null, nativeCtx: null, nativeText: null, bbox: null, lastSample: null, hasInk: false, lastDrawnInk: false, dirty: false, needRebuild: false, lastSampleT: -1e9, lastUploadT: -1e9, uploads: 0, samples: 0 }; /* ================================================================ * DOM / diagnostics helpers * ============================================================== */ function getCanvas() { try { return document.querySelector('canvas.libassjs-canvas'); } catch (e) { return null; } } /* ================================================================ * ADDENDUM 10 — native (non-libass) subtitle source * Jellyfin asks the browser to render subrip/vtt cues from a TextTrack, so the * HUD has no canvas to read. Read the active cue, paint it into a private * canvas, and let the normal crop/upload path take over from there. * ============================================================== */ function ensureNativeCanvas() { if (!M.nativeCanvas) { try { M.nativeCanvas = document.createElement('canvas'); M.nativeCanvas.width = NATIVE_W; M.nativeCanvas.height = NATIVE_H; M.nativeCtx = M.nativeCanvas.getContext('2d'); // Kept in the DOM but hidden: Canvas2D works detached too, and this makes // the exact pixels the HUD uploads inspectable from DevTools. M.nativeCanvas.className = 'jfvr-native-subs'; M.nativeCanvas.style.display = 'none'; (document.body || document.documentElement).appendChild(M.nativeCanvas); } catch (e) { M.nativeCtx = null; } } return !!M.nativeCtx; } // Jellyfin renders non-ASS text subtitles one of two ways depending on the // build: into a TextTrack (video.addTextTrack + mode 'showing', the browser // shows it through ::cue) or into a DOM overlay (div.videoSubtitlesInner). // Both leave no canvas behind. function hasNativeTracks() { try { var v = S.video; if (v && v.textTracks) { for (var i = 0; i < v.textTracks.length; i++) { var tr = v.textTracks[i]; if (tr && tr.mode === 'showing' && tr.cues && tr.cues.length) return true; } } if (document.querySelector('.videoSubtitlesInner')) return true; } catch (e) {} return false; } function activeNativeText() { var out = []; try { var v = S.video; if (v && v.textTracks) { for (var i = 0; i < v.textTracks.length; i++) { var tr = v.textTracks[i]; if (!tr || tr.mode !== 'showing') continue; var cues = tr.activeCues; // maintained by the browser if (!cues) continue; for (var j = 0; j < cues.length; j++) { var s = cues[j] && cues[j].text; if (s) out.push(s); } } } if (!out.length) { var d = document.querySelector('.videoSubtitlesInner, .videoSubtitles'); if (d) { var t = d.innerText || ''; if (t) out.push(t); } } } catch (e) { return ''; } return out.join('\n'); } function cleanCueText(s) { return String(s == null ? '' : s) .replace(/\{[^}]*\}/g, '') // ASS override tags .replace(/<[^>]*>/g, '') // html-ish tags .replace(/ /g, ' ').replace(/&/g, '&') .replace(/</g, '<').replace(/>/g, '>') .replace(/[\u200e\u200f\u202a-\u202e]/g, '') // bidi control marks .replace(/\r/g, '') .replace(/[ \t]+\n/g, '\n') .replace(/\n{3,}/g, '\n\n') .trim(); } function wrapCueLines(ctx, text, maxW) { var out = []; var paras = text.split('\n'); for (var p = 0; p < paras.length; p++) { var para = paras[p]; if (!para) { out.push(''); continue; } var line = ''; for (var i = 0; i < para.length; i++) { var ch = para[i]; if (line && ctx.measureText(line + ch).width > maxW) { var sp = line.lastIndexOf(' '); if (sp > 0 && /[A-Za-z0-9]/.test(line.charAt(line.length - 1))) { out.push(line.slice(0, sp)); line = line.slice(sp + 1); } else { out.push(line); line = ''; } } line += ch; } out.push(line); } return out.length ? out : ['']; } function drawNativeCue(text) { var c = M.nativeCanvas, ctx = M.nativeCtx; if (!c || !ctx) return; var W = c.width, H = c.height; ctx.clearRect(0, 0, W, H); if (!text) return; var fs = Math.max(12, Math.round(H * NATIVE_FONT_H)); ctx.font = '600 ' + fs + 'px "Noto Sans SC","Noto Sans CJK SC","PingFang SC",' + '"Microsoft YaHei",system-ui,Arial,sans-serif'; ctx.textAlign = 'center'; ctx.textBaseline = 'middle'; ctx.lineJoin = 'round'; ctx.miterLimit = 2; var lines = wrapCueLines(ctx, text, W * NATIVE_MAX_W); var lh = fs * NATIVE_LINE_H; var y0 = H / 2 - (lines.length - 1) * lh / 2; ctx.lineWidth = Math.max(2, Math.round(fs * 0.1)); ctx.strokeStyle = 'rgba(0,0,0,0.95)'; ctx.fillStyle = '#ffffff'; for (var i = 0; i < lines.length; i++) { ctx.strokeText(lines[i], W / 2, y0 + i * lh); ctx.fillText(lines[i], W / 2, y0 + i * lh); } } // Decide, per frame, which canvas the HUD samples. libass wins when Jellyfin // created one; otherwise the native cue canvas. Returns false when there is no // subtitle source at all. function updateSource() { var lib = getCanvas(); if (lib && lib.width > 0 && lib.height > 0) { if (M.srcKind !== 'libass') { M.srcKind = 'libass'; M.canvas = lib; M.nativeText = null; M.lastSample = null; M.hasInk = false; M.bbox = null; computeTexDims(); M.needRebuild = true; } return true; } if (!hasNativeTracks()) { if (M.srcKind !== 'none') { M.srcKind = 'none'; M.hasInk = false; M.bbox = null; M.dirty = true; } return false; } if (!ensureNativeCanvas()) return false; var text = cleanCueText(activeNativeText()); if (text !== M.nativeText) { M.nativeText = text; drawNativeCue(text); M.dirty = true; } if (M.srcKind !== 'native') { M.srcKind = 'native'; M.canvas = M.nativeCanvas; M.dualEye = false; // our own canvas is always one centred line M.mismatch = 0; M.lastSample = null; M.hasInk = false; M.bbox = null; computeTexDims(); M.needRebuild = true; } return true; } function getHolder(video) { var holder = null; try { holder = document.querySelector('.videoPlayerContainer'); } catch (e) {} return holder || video || null; } function updateStats() { if (!S || !S.stats) return; var st = S.stats; st.subMode = M.mode; st.subAlpha = M.alpha; st.subLayer = !!M.layer; st.subPrepared = !!M.prepared; st.subCanvasSize = M.canvas ? (M.canvas.width + 'x' + M.canvas.height) : null; st.subSource = M.srcKind; st.subNativeText = M.nativeText ? M.nativeText.slice(0, 60) : null; st.subLayout = M.layout; st.subDual = M.dualEye; st.subEyeW = M.eyeW; st.subCopyND = (M.copyND == null) ? null : +M.copyND.toFixed(3); st.subCenterInk = M.centerInk; st.subTex = (M.texW && M.texH) ? (M.texW + 'x' + M.texH) : null; st.subInk = M.hasInk; st.subCrop = M.bbox ? [ clampInt(M.bbox.x0, 0, 1e9), clampInt(M.bbox.y0, 0, 1e9), clampInt(M.bbox.x1, 0, 1e9), clampInt(M.bbox.y1, 0, 1e9) ] : null; st.subUploads = M.uploads; st.subSamples = M.samples; st.subPlacement = [M.placement.y, M.placement.z, M.placement.width]; st.subAngleDeg = M.angleDeg; st.subLockAngle = M.lockAngle; st.subYOverZ = M.k; st.subOpacity = M.opacity; st.subLayerOpacity = M.layerOpacitySupported; } /* ================================================================ * prepare() / restore() — blow the player box up so libass re-renders * at a usable resolution while NOT in an immersive session. * ============================================================== */ function prepare() { if (M.prepared) return; // idempotent via a live flag, not a backup check // Only libass needs the box blown up so it re-renders bigger. The native cue // canvas is ours, so resizing the player would be pointless (and would move // the browser's own cue overlay around). if (!getCanvas()) { log('prepare skipped: native subtitle source'); return; } var video = S.video || null; var holder = getHolder(video); try { M.styleBackup = { holder: holder ? holder.style.cssText : null, video: video ? video.style.cssText : null }; var grow = ';width:7680px !important;height:3840px !important;' + 'max-width:none !important;max-height:none !important;'; if (holder) holder.style.cssText += grow; if (video && video !== holder) video.style.cssText += grow; M.prepared = true; if (S.stats) S.stats.subPrepared = true; window.dispatchEvent(new Event('resize')); } catch (e) { log('prepare failed', e); } } function restore() { if (!M.prepared) return; var video = S.video || null; var holder = getHolder(video); try { if (M.styleBackup) { if (holder && M.styleBackup.holder != null) holder.style.cssText = M.styleBackup.holder; if (video && video !== holder && M.styleBackup.video != null) video.style.cssText = M.styleBackup.video; } window.dispatchEvent(new Event('resize')); } catch (e) { log('restore failed', e); } M.prepared = false; M.styleBackup = null; if (S.stats) S.stats.subPrepared = false; } /* ================================================================ * introspection * ============================================================== */ function isReady() { var c = getCanvas(); return !!(c && c.width > 0 && c.height > 0); } function canvasSize() { var c = getCanvas(); if (!c || !c.width || !c.height) return null; return { w: c.width, h: c.height }; } // Live XR layer for main.js ray-casting / grab. M.layer is null before // create() and cleared by detachLayer() in destroy(), so this is always // safe to call and always returns the current layer, never a copy. function getLayer() { return M.layer; } /* ================================================================ * layer sizing * ============================================================== */ function maxTexSize() { return (NS.caps && NS.caps.maxTextureSize) || 4096; } function computeTexDims() { var c = M.canvas; if (!c) { M.texW = 0; M.texH = 0; return; } var cw = c.width || 0, ch = c.height || 0; var maxTex = maxTexSize(); M.srcW = cw; M.srcH = ch; M.fullW = Math.max(1, cw); M.halfW = Math.max(1, Math.floor(cw / 2)); // ADDENDUM 9: the HUD samples ONE eye's subtitle region. Dual-eye packs both // eyes into the canvas; a normal subtitle uses the whole frame. M.eyeW = M.dualEye ? M.halfW : M.fullW; if (M.mode === 'sphere') { M.texW = Math.max(2, Math.min(SPHERE_TEX_W, maxTex, cw || 2)); M.texH = Math.max(2, Math.min(SPHERE_TEX_H, maxTex, ch || 2)); } else { M.texW = Math.max(2, Math.min(HUD_TEX_W, maxTex, M.eyeW)); M.texH = Math.max(2, Math.min(maxTex, Math.round(M.texW * HUD_TEX_ASPECT))); } } function quadHeight() { return M.placement.width * (M.texH / M.texW); } function buildLayer(session) { if (M.mode === 'sphere') { if (!NS.refSpace) throw new Error('no refSpace for equirect layer'); var central = (typeof NS.centralAngle === 'function') ? NS.centralAngle() : (S.projection === '360' ? Math.PI * 2 : Math.PI); return M.binding.createEquirectLayer({ space: NS.refSpace, layout: 'stereo-left-right', centralHorizontalAngle: central, upperVerticalAngle: Math.PI / 2, lowerVerticalAngle: -Math.PI / 2, viewPixelWidth: M.texW, viewPixelHeight: M.texH, alpha: true }); } if (typeof XRRigidTransform === 'undefined') throw new Error('no XRRigidTransform'); if (!NS.viewerSpace) throw new Error('no viewerSpace (head-locked quad unavailable)'); return M.binding.createQuadLayer({ space: NS.viewerSpace, layout: 'mono', width: M.placement.width, height: quadHeight(), transform: new XRRigidTransform({ x: 0, y: M.placement.y, z: M.placement.z }), viewPixelWidth: M.texW, viewPixelHeight: M.texH, alpha: true }); } // XRQuadLayer.width/height/transform are readonly in the shipped WebXR Layers // spec, but some runtimes allow live mutation. Probe once so setPlacement() // can move the quad without a flicker when the runtime permits it. function markQuadMutable(l) { M.quadMutable = false; if (!l || M.mode !== 'hud') return; try { var w = l.width, h = l.height, t = l.transform; l.width = w; l.height = h; l.transform = t; M.quadMutable = true; } catch (e) { M.quadMutable = false; } } // Prefer layer.opacity when the runtime exposes a writable one; otherwise // drawHud()/drawSphere() scale the alpha in the fragment shader instead. function probeLayerOpacity(l) { M.layerOpacitySupported = false; if (!l) return; try { if (!('opacity' in l)) return; var prev = l.opacity; l.opacity = 0.5; M.layerOpacitySupported = (l.opacity === 0.5); l.opacity = prev; } catch (e) { M.layerOpacitySupported = false; } if (M.layerOpacitySupported) { try { l.opacity = M.opacity; } catch (e2) { M.layerOpacitySupported = false; } } } function applyOpacityLive() { var l = M.layer; if (!l) return; if (M.layerOpacitySupported) { try { l.opacity = M.opacity; return; } catch (e) { M.layerOpacitySupported = false; } } // No runtime opacity -> the shader uniform dims the strip instead. M.dirty = true; } // When the runtime honours layer.opacity the shader must stay neutral. function uniformOpacity() { return M.layerOpacitySupported ? 1 : M.opacity; } function detachLayer() { if (M.hook && typeof NS.removeFrameHook === 'function') NS.removeFrameHook(M.hook); M.hook = null; if (M.layer) { if (typeof NS.removeLayer === 'function') NS.removeLayer(M.layer); try { if (typeof M.layer.destroy === 'function') M.layer.destroy(); } catch (e) {} M.layer = null; } if (S.stats) S.stats.subLayer = false; } function attachLayer(session) { if (!session || !M.binding) return null; computeTexDims(); var l = buildLayer(session); if (!l) return null; M.layer = l; M.session = session; markQuadMutable(l); probeLayerOpacity(l); if (!M.hook) M.hook = function (frame, time, sess) { onFrame(frame, time, sess); }; if (typeof NS.addLayer === 'function') NS.addLayer(l); if (typeof NS.addFrameHook === 'function') NS.addFrameHook(M.hook); M.dirty = true; return l; } function rebuildLayer() { var session = NS.session || M.session; if (!session || !M.binding) return; try { var old = M.layer; var l = buildLayer(session); if (!l) { M.layer = old; return; } M.layer = l; M.session = session; markQuadMutable(l); probeLayerOpacity(l); if (typeof NS.addLayer === 'function') NS.addLayer(l); if (old) { if (typeof NS.removeLayer === 'function') NS.removeLayer(old); try { if (typeof old.destroy === 'function') old.destroy(); } catch (e) {} } M.dirty = true; log('subtitles layer rebuilt:', M.mode, M.texW + 'x' + M.texH); } catch (e) { setErr('rebuild: ' + ((e && e.message) || e)); log('rebuild failed', e); } updateStats(); } /* ================================================================ * create() / destroy() * ============================================================== */ function create(session) { try { destroy(); if (!S) return null; if (!session) { log('create: no session'); return null; } if (S.subMode === 'hud' || S.subMode === 'sphere' || S.subMode === 'off') M.mode = S.subMode; if (S.subAlpha === 'premultiplied' || S.subAlpha === 'straight') M.alpha = S.subAlpha; readLayoutSetting(); if (isNum(S.subOpacity)) M.opacity = clamp01(S.subOpacity); else S.subOpacity = M.opacity; if (typeof S.subLockAngle === 'boolean') M.lockAngle = S.subLockAngle; else S.subLockAngle = M.lockAngle; if (isNum(S.subAngleDeg)) M.angleDeg = clampAngle(S.subAngleDeg); else S.subAngleDeg = M.angleDeg; if (M.mode === 'off') { log('create: subtitle mode off'); return null; } if (typeof window.XRWebGLBinding !== 'function') { setErr('no XRWebGLBinding'); log('create: no XRWebGLBinding'); return null; } if (typeof NS.xrGl !== 'function' || typeof NS.linkProgram !== 'function' || typeof NS.makeTexture !== 'function' || typeof NS.bindFullscreenTriangle !== 'function') { setErr('core helpers missing'); log('create: __JFVR core helpers missing'); return null; } var gl = NS.xrGl(true); if (!gl) { setErr('no webgl2'); log('create: no webgl2 context'); return null; } M.gl = gl; M.glCanvas = gl.canvas; M.uploads = 0; M.samples = 0; // ADDENDUM 10: the source may be the libass canvas OR our own cue canvas. // Start on the native canvas so create() works even before Jellyfin has // created its renderer, then updateSource() upgrades to libass if it shows up. ensureNativeCanvas(); M.canvas = M.nativeCanvas; M.srcKind = 'none'; updateSource(); computeTexDims(); if (!M.texW || !M.texH) { setErr('bad canvas size'); log('create: bad canvas size'); destroy(); return null; } var FS = [ '#version 300 es', 'precision mediump float;', 'in vec2 vUv;', 'uniform sampler2D uTex;', 'uniform float uOpacity;', // 0..1 dimming 'uniform float uPremul;', // 1 = premultiplied texture, 0 = straight 'out vec4 outColor;', 'void main(){', ' vec4 c = texture(uTex, vUv);', ' c.rgb *= mix(1.0, uOpacity, uPremul);', ' c.a *= uOpacity;', ' outColor = c;', '}' ].join('\n'); M.prog = NS.linkProgram(gl, NS.QUAD_VS, FS); NS.bindFullscreenTriangle(gl, M.prog); gl.uniform1i(gl.getUniformLocation(M.prog, 'uTex'), 0); M.locOpacity = gl.getUniformLocation(M.prog, 'uOpacity'); M.locPremul = gl.getUniformLocation(M.prog, 'uPremul'); M.tex = NS.makeTexture(gl); M.fbo = gl.createFramebuffer(); gl.disable(gl.DEPTH_TEST); gl.disable(gl.BLEND); M.binding = new window.XRWebGLBinding(session, gl); if (!M.binding || typeof M.binding.getSubImage !== 'function') { throw new Error('XRWebGLBinding missing getSubImage'); } M.sampleCanvas = document.createElement('canvas'); M.sampleCanvas.width = SAMPLE_W; M.sampleCanvas.height = SAMPLE_H; M.sampleCtx = M.sampleCanvas.getContext('2d', { willReadFrequently: true }); M.cropCanvas = document.createElement('canvas'); M.cropCanvas.width = 2; M.cropCanvas.height = 2; M.cropCtx = M.cropCanvas.getContext('2d'); if (!M.sampleCtx || !M.cropCtx) throw new Error('2d scratch context unavailable'); var layer = attachLayer(session); if (!layer) throw new Error('layer creation returned null'); M.needRebuild = false; // the source was already resolved above sample(); // seed bbox / hasInk before the first XR frame M.dirty = true; setErr(null); updateStats(); log('subtitles layer created:', M.mode, M.texW + 'x' + M.texH, 'alpha=' + M.alpha, 'source=' + M.srcKind, 'canvas=' + (M.canvas ? M.canvas.width + 'x' + M.canvas.height : 'none')); return layer; } catch (e) { setErr((e && e.message) || e); log('create failed:', e); try { destroy(); } catch (e2) {} return null; } } function destroy() { try { detachLayer(); } catch (e) {} try { var gl = M.gl; if (gl) { try { if (M.tex) gl.deleteTexture(M.tex); } catch (e) {} try { if (M.fbo) gl.deleteFramebuffer(M.fbo); } catch (e) {} try { if (M.prog) gl.deleteProgram(M.prog); } catch (e) {} try { var lose = gl.getExtension('WEBGL_lose_context'); if (lose && typeof lose.loseContext === 'function') lose.loseContext(); } catch (e) {} } } catch (e) {} M.gl = null; M.glCanvas = null; M.prog = null; M.tex = null; M.fbo = null; M.binding = null; M.session = null; M.canvas = null; M.srcKind = 'none'; M.nativeText = null; M.sampleCanvas = null; M.sampleCtx = null; M.cropCanvas = null; M.cropCtx = null; M.srcW = 0; M.srcH = 0; M.halfW = 1; M.fullW = 1; M.eyeW = 1; M.texW = 0; M.texH = 0; M.copyND = null; M.centerInk = null; M.colL = null; M.colR = null; M.mismatch = 0; M.bbox = null; M.lastSample = null; M.hasInk = false; M.lastDrawnInk = false; M.dirty = false; M.needRebuild = false; M.lastSampleT = -1e9; M.lastUploadT = -1e9; updateStats(); } /* ================================================================ * bbox sampling (downsampled, ~4 Hz) * ============================================================== */ function quantise(b) { return { x0: Math.floor(b.x0 / QUANT) * QUANT, y0: Math.floor(b.y0 / QUANT) * QUANT, x1: Math.ceil(b.x1 / QUANT) * QUANT, y1: Math.ceil(b.y1 / QUANT) * QUANT }; } // Grow immediately (never clip new ink); shrink slowly past a dead band so the // strip does not jitter while text scrolls/changes. function updateBBox(raw) { var cur = M.bbox; if (!cur) { M.bbox = raw; return true; } if (raw.x0 < cur.x0 || raw.y0 < cur.y0 || raw.x1 > cur.x1 || raw.y1 > cur.y1) { M.bbox = { x0: Math.min(cur.x0, raw.x0), y0: Math.min(cur.y0, raw.y0), x1: Math.max(cur.x1, raw.x1), y1: Math.max(cur.y1, raw.y1) }; return true; } var tolX = Math.max(2, (cur.x1 - cur.x0) * SHRINK_TOL); var tolY = Math.max(2, (cur.y1 - cur.y0) * SHRINK_TOL); var next = { x0: cur.x0, y0: cur.y0, x1: cur.x1, y1: cur.y1 }; var changed = false; if (raw.x0 > cur.x0 + tolX) { next.x0 = cur.x0 + (raw.x0 - cur.x0) * 0.5; changed = true; } if (raw.y0 > cur.y0 + tolY) { next.y0 = cur.y0 + (raw.y0 - cur.y0) * 0.5; changed = true; } if (raw.x1 < cur.x1 - tolX) { next.x1 = cur.x1 + (raw.x1 - cur.x1) * 0.5; changed = true; } if (raw.y1 < cur.y1 - tolY) { next.y1 = cur.y1 + (raw.y1 - cur.y1) * 0.5; changed = true; } if (!changed) return false; M.bbox = quantise(next); return true; } // ADDENDUM 9: normalised distance between the two halves' ink column profiles at // their best horizontal alignment. ~0 for dual-eye (same line, two copies), ~1 // for a normal subtitle (the halves are different parts of one line). function columnCopyND(colL, colR, half) { var D = Math.round(half * 0.4), best = 1e9; for (var lag = -D; lag <= D; lag++) { var diff = 0, tot = 0; for (var i = 0; i < half; i++) { var j = i + lag; var r = (j >= 0 && j < half) ? colR[j] : 0; diff += Math.abs(colL[i] - r); tot += colL[i] + r; } if (tot > 0) { var nd = diff / tot; if (nd < best) best = nd; } } return (best === 1e9) ? 1 : best; } function sample() { var canvas = M.canvas; var ctx = M.sampleCtx; if (!canvas || !ctx) return; var cw = canvas.width, ch = canvas.height; if (!cw || !ch) return; if (cw !== M.srcW || ch !== M.srcH) { computeTexDims(); M.lastSample = null; M.needRebuild = true; // layer texture size no longer matches the source } try { ctx.clearRect(0, 0, SAMPLE_W, SAMPLE_H); ctx.drawImage(canvas, 0, 0, cw, ch, 0, 0, SAMPLE_W, SAMPLE_H); } catch (e) { setErr('downsample: ' + ((e && e.message) || e)); return; } var img; try { img = ctx.getImageData(0, 0, SAMPLE_W, SAMPLE_H); } catch (e) { setErr('getImageData: ' + ((e && e.message) || e)); return; } var d = img.data; var prev = M.lastSample; var hud = (M.mode === 'hud'); var half = SAMPLE_W >> 1; var diff = 0; var sx = cw / SAMPLE_W, sy = ch / SAMPLE_H; var padX = PAD_PX / sx, padY = PAD_PX / sy; var band = Math.max(1, Math.round(CENTER_BAND_PX / sx)); // Track the ink box of each REGION separately so the layout can be resolved // before cropping: l* = left half, r* = right half, centerInk = the seam. var lMinX = SAMPLE_W + 1, lMaxX = -1, lMinY = SAMPLE_H + 1, lMaxY = -1; var rMinX = SAMPLE_W + 1, rMaxX = -1, rMinY = SAMPLE_H + 1, rMaxY = -1; var centerInk = 0; if (!M.colL) { M.colL = new Int32Array(half); M.colR = new Int32Array(half); } var colL = M.colL, colR = M.colR; colL.fill(0); colR.fill(0); for (var y = 0; y < SAMPLE_H; y++) { var row = y * SAMPLE_W * 4; for (var x = 0; x < SAMPLE_W; x++) { var i = row + x * 4; if (prev && diff <= DIFF_SAMPLES) { var dd = Math.abs(d[i] - prev[i]) + Math.abs(d[i + 1] - prev[i + 1]) + Math.abs(d[i + 2] - prev[i + 2]) + Math.abs(d[i + 3] - prev[i + 3]); if (dd > DIFF_SUM) diff++; } if (!hud || d[i + 3] <= INK_ALPHA) continue; if (x < half) { if (x < lMinX) lMinX = x; if (x > lMaxX) lMaxX = x; if (y < lMinY) lMinY = y; if (y > lMaxY) lMaxY = y; if (x > half - band) centerInk++; colL[x]++; } else { if (x < rMinX) rMinX = x; if (x > rMaxX) rMaxX = x; if (y < rMinY) rMinY = y; if (y > rMaxY) rMaxY = y; if (x < half + band) centerInk++; colR[x - half]++; } } } M.samples++; M.lastSample = d; if (!prev || diff > DIFF_SAMPLES) M.dirty = true; // content changed -> re-upload if (!hud) { updateStats(); return; } // ---- ADDENDUM 9: resolve dual-eye vs mono ---------------------------- // dual-eye: two copies of the same line -> no ink at the seam AND the two // halves' column profiles match under a horizontal shift. mono: one line // across the whole frame -> ink crosses the seam and the halves do not match. var hasLeft = lMaxX >= 0, hasRight = rMaxX >= 0; var hasInk = hasLeft || hasRight || centerInk > 0; if (hasInk) { M.centerInk = centerInk; var dual = M.dualEye; if (M.srcKind === 'native') { M.copyND = null; dual = false; } // our own canvas is always mono else if (M.layout === 'dual') dual = true; else if (M.layout === 'mono') dual = false; else if (hasLeft && hasRight) { if (centerInk > 0) { M.copyND = null; dual = false; } else { M.copyND = columnCopyND(colL, colR, half); dual = (M.copyND < COPY_TOL); } } else dual = !!hasLeft; // ink in ONE half -> dual (left) else mono (right) if (dual !== M.dualEye) { // Debounce: a layout switch rebuilds the layer, so require two // consecutive samples before believing it (never observed to flicker on // real content, but cheap insurance against a rebuild every sample). M.mismatch = (M.mismatch || 0) + 1; if (M.mismatch >= 2) { M.mismatch = 0; M.dualEye = dual; computeTexDims(); M.needRebuild = true; // the texture width changed with the region } } else M.mismatch = 0; } // ---- crop to the ink box of the RESOLVED region ---------------------- var x0s, y0s, x1s, y1s; if (hasInk) { if (M.dualEye && hasLeft) { x0s = lMinX; y0s = lMinY; x1s = lMaxX; y1s = lMaxY; } else { x0s = hasLeft ? lMinX : rMinX; x1s = hasRight ? rMaxX : lMaxX; y0s = Math.min(hasLeft ? lMinY : Infinity, hasRight ? rMinY : Infinity); y1s = Math.max(hasLeft ? lMaxY : -1, hasRight ? rMaxY : -1); } } if (!hasInk) { if (M.hasInk) { M.hasInk = false; M.dirty = true; } // clear the strip once } else { var raw = quantise({ x0: Math.max(0, (x0s - padX) * sx), y0: Math.max(0, (y0s - padY) * sy), x1: Math.min(M.eyeW, (x1s + 1 + padX) * sx), y1: Math.min(ch, (y1s + 1 + padY) * sy) }); if (!M.hasInk) { M.hasInk = true; M.bbox = raw; M.dirty = true; } else if (updateBBox(raw)) M.dirty = true; } updateStats(); } /* ================================================================ * per-frame render * ============================================================== */ function drawHud(tw, th) { var gl = M.gl; var b = M.bbox; if (!gl || !b) return; var cw = Math.max(1, Math.min(M.eyeW, Math.round(b.x1 - b.x0))); var ch = Math.max(1, Math.round(b.y1 - b.y0)); var sx = Math.max(0, Math.min(Math.max(0, M.eyeW - cw), Math.round(b.x0))); var sy = Math.max(0, Math.min(Math.max(0, M.srcH - ch), Math.round(b.y0))); var crop = M.cropCanvas, cctx = M.cropCtx; if (crop.width !== cw || crop.height !== ch) { crop.width = cw; crop.height = ch; } cctx.clearRect(0, 0, cw, ch); cctx.drawImage(M.canvas, sx, sy, cw, ch, 0, 0, cw, ch); var scale = M.texW / M.eyeW; // texture px per source px var dw = cw * scale, dh = ch * scale; var fit = Math.min(1, tw / dw, th / dh); // never overflow the colorTexture dw *= fit; dh *= fit; var ox = Math.round((tw - dw) / 2); var oy = Math.round((th - dh) / 2); gl.activeTexture(gl.TEXTURE0); gl.bindTexture(gl.TEXTURE_2D, M.tex); gl.pixelStorei(gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, M.alpha === 'premultiplied'); gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, crop); gl.useProgram(M.prog); gl.uniform1f(M.locOpacity, uniformOpacity()); gl.uniform1f(M.locPremul, M.alpha === 'premultiplied' ? 1 : 0); gl.viewport(ox, oy, Math.max(1, Math.round(dw)), Math.max(1, Math.round(dh))); gl.drawArrays(gl.TRIANGLES, 0, 3); // Diagnostic: the ACTUAL angular height of the drawn subtitle ink. The // quad's height maps texH px onto width*texH/texW metres, so this is the // number to compare against the VR-reading literature (~1-2 deg preferred // for medium/long text; see HANDOFF.md 8). if (M.texH > 0 && M.texW > 0 && th > 0) { var inkM = (M.placement.width * (M.texH / M.texW)) * (dh / th); var zAbs = Math.max(1e-6, Math.abs(M.placement.z)); if (S && S.stats) S.stats.subTextDeg = rad2deg(2 * Math.atan((inkM / 2) / zAbs)); } M.uploads++; M.lastDrawnInk = true; } function drawSphere(tw, th) { var gl = M.gl; gl.activeTexture(gl.TEXTURE0); gl.bindTexture(gl.TEXTURE_2D, M.tex); gl.pixelStorei(gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, M.alpha === 'premultiplied'); gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, M.canvas); gl.useProgram(M.prog); gl.uniform1f(M.locOpacity, uniformOpacity()); gl.uniform1f(M.locPremul, M.alpha === 'premultiplied' ? 1 : 0); gl.viewport(0, 0, tw, th); gl.drawArrays(gl.TRIANGLES, 0, 3); M.uploads++; M.lastDrawnInk = true; } function redraw(frame) { var gl = M.gl; if (!gl) return; var sub; try { sub = M.binding.getSubImage(M.layer, frame); } // two args, per contract catch (e) { setErr('getSubImage: ' + ((e && e.message) || e)); return; } if (!sub || !sub.colorTexture) return; var tw = sub.colorTextureWidth || M.texW; var th = sub.colorTextureHeight || M.texH; try { gl.bindFramebuffer(gl.FRAMEBUFFER, M.fbo); gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, sub.colorTexture, 0); gl.viewport(0, 0, tw, th); gl.clearColor(0, 0, 0, 0); gl.clear(gl.COLOR_BUFFER_BIT); if (gl.checkFramebufferStatus(gl.FRAMEBUFFER) === gl.FRAMEBUFFER_COMPLETE) { if (M.mode === 'sphere') { drawSphere(tw, th); } else if (M.hasInk && M.bbox) { drawHud(tw, th); } else { M.lastDrawnInk = false; // cleared above -> strip is fully transparent } } } catch (e) { setErr('redraw: ' + ((e && e.message) || e)); } finally { try { gl.bindFramebuffer(gl.FRAMEBUFFER, null); } catch (e) {} try { gl.bindTexture(gl.TEXTURE_2D, null); } catch (e) {} } updateStats(); } function onFrame(frame, time, session) { try { if (!M.layer || !M.gl || !M.binding || !frame) return; if (M.session && session && session !== M.session) return; var t = isNum(time) ? time : (frame.predictedDisplayTime != null ? frame.predictedDisplayTime : (window.performance ? performance.now() : Date.now())); // Keep the source current every frame (native cue text changes on its own). var hasSrc = updateSource(); if (!hasSrc) { if (M.hasInk) { M.hasInk = false; M.bbox = null; M.dirty = true; } } else if (t - M.lastSampleT >= SAMPLE_MS || M.lastSampleT < 0) { M.lastSampleT = t; sample(); } if (M.needRebuild) { M.needRebuild = false; rebuildLayer(); return; } if (M.dirty && (t - M.lastUploadT) >= UPLOAD_MS) { M.lastUploadT = t; M.dirty = false; redraw(frame); } } catch (e) { setErr('frame: ' + ((e && e.message) || e)); } } /* ================================================================ * settings (safe before or during a session) * ============================================================== */ // ADDENDUM 9 — read state.subLayout into the module. A forced layout resolves // immediately; 'auto' keeps the current guess until sample() sees ink. function readLayoutSetting() { if (S.subLayout === 'dual' || S.subLayout === 'mono') M.layout = S.subLayout; else { M.layout = 'auto'; if (S.subLayout !== 'auto') S.subLayout = 'auto'; } if (M.layout !== 'auto') { M.dualEye = (M.layout === 'dual'); M.mismatch = 0; } } // Force home the dual-eye / mono decision, or go back to auto-detection. function setLayout(mode) { if (mode !== 'auto' && mode !== 'dual' && mode !== 'mono') return; M.layout = mode; S.subLayout = mode; if (!M.canvas) { updateStats(); return; } if (mode === 'auto') { M.bbox = null; M.hasInk = false; M.mismatch = 0; M.dirty = true; // re-detect from the next sample } else { var forced = (mode === 'dual'); if (forced !== M.dualEye) { M.dualEye = forced; M.bbox = null; M.hasInk = false; M.mismatch = 0; computeTexDims(); M.needRebuild = true; M.dirty = true; } } updateStats(); } function getLayout() { return M.layout; } function setMode(mode) { if (mode !== 'hud' && mode !== 'sphere' && mode !== 'off') return; var changed = (M.mode !== mode); M.mode = mode; S.subMode = mode; if (S.stats) S.stats.subMode = mode; if (!changed || !M.gl || !M.binding) { updateStats(); return; } try { detachLayer(); if (mode !== 'off') { var s = NS.session || M.session; if (s && attachLayer(s)) sample(); } } catch (e) { setErr('setMode: ' + ((e && e.message) || e)); log('setMode failed', e); } updateStats(); } function setAlphaMode(mode) { if (mode !== 'premultiplied' && mode !== 'straight') return; M.alpha = mode; S.subAlpha = mode; if (S.stats) S.stats.subAlpha = mode; M.dirty = true; // force a re-upload with the new UNPACK flag } // ADDENDUM 6 — the single low-level writer. Quantises, clamps and applies. // Callers pass an already-resolved (y, z, width) triple; setPlacement() and // setAngleDeg() decide what that triple should be for the current mode. function commitPlacement(y, z, width) { // Snap to a coarse grid so tiny slider jitter does not move/thrash the layer. y = Math.round(y / POS_STEP) * POS_STEP; z = Math.round(z / POS_STEP) * POS_STEP; width = Math.round(width / WIDTH_STEP) * WIDTH_STEP; // Enforce the bounds here too: callers other than 90-main.js must not be able // to place the strip outside the supported range. y = y < MIN_Y ? MIN_Y : (y > MAX_Y ? MAX_Y : y); z = z < MIN_Z ? MIN_Z : (z > MAX_Z ? MAX_Z : z); var minW = M.lockAngle ? MIN_WIDTH_LOCK : MIN_WIDTH; if (!(width > minW)) width = minW; var changed = Math.abs(y - M.placement.y) >= POS_STEP * 0.5 || Math.abs(z - M.placement.z) >= POS_STEP * 0.5 || Math.abs(width - M.placement.width) >= WIDTH_STEP * 0.5; M.placement.y = y; M.placement.z = z; M.placement.width = width; M.k = (z !== 0) ? (y / z) : M.k; // the angular height that the lock holds if (S) S.subPlacement = { y: y, z: z, width: width }; if (S && S.stats) { S.stats.subPlacement = [y, z, width]; S.stats.subAngleDeg = M.angleDeg; S.stats.subLockAngle = M.lockAngle; S.stats.subYOverZ = M.k; } if (!changed || !M.layer || M.mode !== 'hud') return; applyPlacementLive(); } // y / z / width in metres. With the angular lock ON: // * a depth change (p.z without p.y) also moves y to hold the y/z ratio, and // re-derives width from the locked angle -> DEPTH ONLY, same screen spot; // * a size change (p.width without p.z) re-derives the locked angle instead; // * a drag that carries both (grab & drag) keeps the caller's y and derives // only the width, so vertical hand motion still works. // With the lock OFF this is exactly the pre-ADDENDUM-6 free placement. function setPlacement(p) { if (!p || typeof p !== 'object') return; var y = isNum(p.y) ? p.y : M.placement.y; var z = isNum(p.z) ? p.z : M.placement.z; var width = (isNum(p.width) && p.width > 0) ? p.width : M.placement.width; var zGiven = isNum(p.z); var wGiven = isNum(p.width) && p.width > 0; if (M.lockAngle) { if (zGiven) { // Clamp the depth BEFORE deriving, otherwise a near/far request past the // bound would be resolved at the requested depth and then clamped into a // different angular size. z = z < MIN_Z ? MIN_Z : (z > MAX_Z ? MAX_Z : z); } if (wGiven && !zGiven) { // explicit size request: the caller wants a different angular size M.angleDeg = clampAngle(angleForWidth(width, M.placement.z)); width = widthForAngle(M.angleDeg, M.placement.z); if (S) S.subAngleDeg = M.angleDeg; } if (zGiven) { if (!isNum(p.y)) y = M.k * z; // hold the angular height width = widthForAngle(M.angleDeg, z); // hold the angular width } } commitPlacement(y, z, width); } // Restore the ADDENDUM-2 / ADDENDUM-6 defaults. Routed through commitPlacement() // so it uses the same live HUD path: in-place quad mutation when the runtime // allows it, otherwise a single-layer rebuild (frame hook and GL resources // untouched). Also refreshes state.subPlacement and is a no-op when no session // is live. The lock flag itself is left alone. function resetPlacement() { M.angleDeg = DEFAULT_ANGLE_DEG; M.k = DEFAULT_PLACEMENT.y / DEFAULT_PLACEMENT.z; if (S) { S.subAngleDeg = M.angleDeg; S.subLockAngle = M.lockAngle; } commitPlacement(DEFAULT_PLACEMENT.y, DEFAULT_PLACEMENT.z, DEFAULT_PLACEMENT.width); updateStats(); } // Turn the depth/size decoupling on or off. Turning it ON adopts the current // pair as THE angular size / height, so nothing moves at the moment of the // switch; turning it OFF keeps the current width and frees it again. function setLockAngle(on) { var v = !!on; if (v !== M.lockAngle) { M.lockAngle = v; if (v) { M.angleDeg = clampAngle(angleForWidth(M.placement.width, M.placement.z)); M.k = M.placement.y / M.placement.z; } } if (S) { S.subLockAngle = M.lockAngle; S.subAngleDeg = M.angleDeg; } updateStats(); } // Change the locked angular width (degrees) at the CURRENT depth. When the // lock is off this only remembers the value for the next time it is switched on. function setAngleDeg(deg) { if (!isNum(deg)) return; M.angleDeg = clampAngle(deg); if (S) S.subAngleDeg = M.angleDeg; if (M.lockAngle) { commitPlacement(M.placement.y, M.placement.z, widthForAngle(M.angleDeg, M.placement.z)); } updateStats(); } function getAngleDeg() { return M.angleDeg; } function getLockAngle() { return M.lockAngle; } function getPlacement() { return { y: M.placement.y, z: M.placement.z, width: M.placement.width }; } // Live HUD move: mutate the quad in place when the runtime allows it (no // flicker); otherwise rebuild just this layer — the frame hook and the GL // resources are untouched, so only the XR layer is recreated. function applyPlacementLive() { var l = M.layer; if (!l) return; if (M.quadMutable) { try { l.width = M.placement.width; l.height = quadHeight(); l.transform = new XRRigidTransform({ x: 0, y: M.placement.y, z: M.placement.z }); return; } catch (e) { M.quadMutable = false; // runtime refused in-place mutation } } M.needRebuild = true; } // 00-core.js owns state.subPlacement, but some builds still ship the legacy // pre-ADDENDUM-2 default y:-0.34. Treat that exact value as "unset" so the // module's own below-centre default (-0.42) still applies. function syncPlacementFromState() { var angleStored = !!(S && isNum(S.subAngleDeg)); if (S) { if (typeof S.subLockAngle === 'boolean') M.lockAngle = S.subLockAngle; if (angleStored) M.angleDeg = clampAngle(S.subAngleDeg); } var sp = S && S.subPlacement; var y = M.placement.y, z = M.placement.z, width = M.placement.width; if (sp && typeof sp === 'object') { if (isNum(sp.y) && sp.y !== LEGACY_CORE_Y) y = sp.y; if (isNum(sp.z)) z = sp.z; if (isNum(sp.width) && sp.width > 0) width = sp.width; } // No stored angle yet (first run, older localStorage): derive it from the // stored pair so the lock starts exactly at the current look. if (M.lockAngle && !angleStored) M.angleDeg = clampAngle(angleForWidth(width, z)); M.k = y / z; setPlacement({ y: y, z: z, width: width }); } function setOpacity(a) { if (!isNum(a)) return; var v = clamp01(a); M.opacity = v; if (S) S.subOpacity = v; if (S && S.stats) S.stats.subOpacity = v; applyOpacityLive(); M.dirty = true; // redraw the strip with the new alpha M.lastUploadT = -1e9; // apply on the very next frame, past the 30 Hz cap } function getOpacity() { return M.opacity; } function getMode() { return M.mode; } function init() { try { if (S.subMode === 'hud' || S.subMode === 'sphere' || S.subMode === 'off') M.mode = S.subMode; else if (S.subMode) S.subMode = M.mode; if (S.subAlpha === 'premultiplied' || S.subAlpha === 'straight') M.alpha = S.subAlpha; else if (S.subAlpha) S.subAlpha = M.alpha; readLayoutSetting(); if (isNum(S.subOpacity)) M.opacity = clamp01(S.subOpacity); else S.subOpacity = M.opacity; if (typeof S.subLockAngle === 'boolean') M.lockAngle = S.subLockAngle; else S.subLockAngle = M.lockAngle; if (isNum(S.subAngleDeg)) M.angleDeg = clampAngle(S.subAngleDeg); else S.subAngleDeg = M.angleDeg; syncPlacementFromState(); updateStats(); } catch (e) { log('init failed', e); } } /* ================================================================ * public API — exactly the names documented in INTERFACES.md * ============================================================== */ NS.Subtitles = { init: init, prepare: prepare, restore: restore, isReady: isReady, canvasSize: canvasSize, create: create, destroy: destroy, setMode: setMode, setLayout: setLayout, getLayout: getLayout, setAlphaMode: setAlphaMode, setPlacement: setPlacement, getPlacement: getPlacement, resetPlacement: resetPlacement, setLockAngle: setLockAngle, getLockAngle: getLockAngle, setAngleDeg: setAngleDeg, getAngleDeg: getAngleDeg, setOpacity: setOpacity, getOpacity: getOpacity, getLayer: getLayer, getMode: getMode, getSourceKind: function () { return M.srcKind; } }; })(); /* ===== 20-controls.js ===== */ /* 20-controls.js — __JFVR.Controls * * Head-locked VR control strip for video playback: progress bar + seek, * play/pause, skip +/-10 s, subtitle toggle, plus a subtitle row (position / * depth / size / opacity / reset — ADDENDUM 4, extended in ADDENDUM 6). Pure * 2D-canvas rendering plus a thin WebXR quad-layer wrapper. Hidden by default * (ADDENDUM 2): show() brings it up and it auto-hides; setPointer() drives the * aiming beam and the cursor. * * Everything the module exposes is documented in INTERFACES.md (§20-controls.js). * draw() and hitTest() are pure and do not touch WebXR, so they can be unit * tested in isolation. No document / navigator access happens at load time. */ (function () { 'use strict'; var NS = (typeof window !== 'undefined' && window.__JFVR) ? window.__JFVR : (typeof window !== 'undefined' ? (window.__JFVR = {}) : {}); function ns() { if (typeof window !== 'undefined' && window.__JFVR) return window.__JFVR; return NS; } function log() { var n = ns(); if (n && typeof n.log === 'function') { n.log.apply(null, arguments); return; } if (typeof console !== 'undefined' && console.log) { console.log.apply(console, ['[JFVR]'].concat(Array.prototype.slice.call(arguments))); } } function stats() { var n = ns(); if (!n) return null; if (!n.state) n.state = {}; if (!n.state.stats) n.state.stats = {}; return n.state.stats; } /* ------------------------------------------------------------------ * * Geometry (all normalized to the panel rect, origin top-left). * Pure data: shared by draw() and hitTest(). * ------------------------------------------------------------------ */ var FONT = 'system-ui, -apple-system, sans-serif'; var ACCENT = '#4ea1ff'; var TRACK_X0 = 0.03; var TRACK_X1 = 0.97; // Panel extents in metres (used for the XR quad layer). Headset feedback: // the ADDENDUM 4 panel (1.1 m x 0.30 m in front of the eye) filled far too // much of the view, so the whole strip is now 0.72 m x 0.21 m expanded and // 0.60 m x 0.05 m collapsed. var PANEL_M = { collapsed: { w: 0.60, h: 0.05 }, expanded: { w: 0.72, h: 0.26 }, // ADDENDUM 7b: the minimal panel shown while the controls are LOCKED. It // carries only the padlock, so nothing adjustable is reachable. locked: { w: 0.26, h: 0.10 } }; // Panel backing-store pixels at 2000 px/m, so the smaller panel stays as // crisp as the old 1000 px/m one. hitTest() infers the mode from the aspect // ratio of (w, h), so pass these (or any matching ratio). var PANEL_PX = { collapsed: { w: 1200, h: 100 }, expanded: { w: 1440, h: 520 }, locked: { w: 520, h: 200 } }; // Layer placement in viewer space (head-locked, centred, lower part of view). var PANEL_Y = -0.28; var PANEL_Z = 1.4; // hitTest mode threshold: collapsed ratio 12.0, expanded 2.77, locked 2.6. var COLLAPSED_RATIO = 8; // Pointer beam (ADDENDUM 2): a separate thin quad layer running from the // origin of the winning input ray to its hit. Shown only while the bar is. var BEAM_H = 0.011; // quad height = beam thickness, metres var BEAM_L_DEFAULT = 1.2; // length when the ray reaches no panel hit var BEAM_L_MIN = 0.05; var BEAM_L_MAX = 6; var BEAM_TEX = { w: 256, h: 8 }; // u = 0 is the origin end of the beam var CURSOR_R = 0.05; // cursor ring radius, fraction of panel height var MIN_UPLOAD_MS = 33; // ~30 Hz cap on canvas re-uploads var BUTTONS = ['back10', 'playpause', 'fwd10', 'subs', 'lock']; /* ---------------- ADDENDUM 7b: padlock ---------------- * The padlock is now a NORMAL PANEL BUTTON, drawn on the panel canvas. The * first attempt gave it its own quad layer parked to the right of the bar, * which failed on device: the only pointing device is the hand ray and the * cursor's acceptance window reaches the bar's own right edge, not a separate * object 0.4 m further out. As a panel button it travels the exact code path * every other button already uses. */ // Icon centred at (cx, cy) with size s. Closed + filled while locked. function drawLockIcon(ctx, cx, cy, s, locked) { var bodyW = s * 0.56, bodyH = s * 0.44; var bodyY = cy + s * 0.11; var lw = Math.max(1.5, s * 0.075); var col = locked ? ACCENT : '#ffffff'; ctx.strokeStyle = col; ctx.lineWidth = lw; ctx.lineCap = 'round'; var r = s * 0.185; ctx.beginPath(); if (locked) { ctx.arc(cx, bodyY - bodyH / 2, r, Math.PI, 0); // closed arch } else { // open: arch lifted and swung right, gap where the body would be ctx.arc(cx + s * 0.10, bodyY - bodyH / 2 - s * 0.07, r, Math.PI * 0.92, Math.PI * 1.88); } ctx.stroke(); ctx.beginPath(); rr(ctx, cx - bodyW / 2, bodyY - bodyH / 2, bodyW, bodyH, bodyH * 0.22); if (locked) { ctx.fillStyle = ACCENT; ctx.fill(); } else { ctx.strokeStyle = col; ctx.lineWidth = lw; ctx.stroke(); } ctx.fillStyle = locked ? 'rgba(10,16,26,0.82)' : col; ctx.beginPath(); ctx.arc(cx, bodyY - bodyH * 0.12, s * 0.058, 0, Math.PI * 2); ctx.fill(); ctx.beginPath(); ctx.moveTo(cx - s * 0.030, bodyY - bodyH * 0.02); ctx.lineTo(cx + s * 0.030, bodyY - bodyH * 0.02); ctx.lineTo(cx + s * 0.018, bodyY + bodyH * 0.30); ctx.lineTo(cx - s * 0.018, bodyY + bodyH * 0.30); ctx.closePath(); ctx.fill(); } // ADDENDUM 4 subtitle row, left to right. Each entry is the action type // hitTest() returns for that button. ADDENDUM 6 added the vertical position // pair (up/down) and the depth pair (near/far): with the angular lock on, the // depth pair changes the strip's DEPTH only, which is how the user brings the // subtitles to the depth of a nearby object without the text changing size. var SUB_BUTTONS = ['sub_up', 'sub_down', 'sub_near', 'sub_far', 'sub_smaller', 'sub_bigger', 'sub_opaque_down', 'sub_opaque_up', 'sub_reset']; // Values shown in the row's caption when the caller supplies no view.sub. var SUB_DEFAULT = { width: 2.0, opacity: 0.85, angleDeg: 69, lock: true, dist: 1.45 }; // Short Chinese label drawn under every button's icon. Headset feedback: the // icon-only row gave no clue what each button did. Keyed by the SAME action // types hitTest() returns, so the label cannot drift from the action. // playpause swaps 播放 / 暂停 with view.playing at draw time. var LABELS = { back10: '后退10s', playpause: '播放', fwd10: '前进10s', subs: '字幕', lock: '上锁', sub_up: '上', sub_down: '下', sub_near: '近', sub_far: '远', sub_smaller: '小', sub_bigger: '大', sub_opaque_down: '暗', sub_opaque_up: '亮', sub_reset: '复位' }; var LAYOUT = { collapsed: { trackCy: 0.50, trackVisH: 0.13, // ~6.5 mm of a 5 cm strip trackHitY0: 0.0, // full strip height is a generous seek band trackHitY1: 1.0 }, // Vertical bands of the 520 px (= 0.26 m) expanded panel, top to bottom: // the ADDENDUM 8 drag strip, time readout, progress track, media buttons, // subtitle caption, subtitle buttons. The hit bands are contiguous and never // overlap, so the seek band is untouched by the button rows (hitTest still // tests every button first). Every offset is a fraction of the panel, so // draw() and hitTest() scale with whatever w/h they are handed. expanded: { // ADDENDUM 8: the picture-pan drag strip sits ABOVE the progress bar (the // owner's requested place). It is the whole picture-pan affordance: pinch it // and drag, and the video content rotates with your hand. Three taps on it // within VIEW_TAP_MS reset the view to dead ahead. viewStripX0: 0.02, viewStripX1: 0.98, viewStripY0: 0.03, viewStripY1: 0.17, viewHitY0: 0.0, // 0 px band 0..104 px viewHitY1: 0.20, // 104 px viewLabelFont: 0.040, viewValueFont: 0.046, viewHintFont: 0.030, viewLabelDy: 0.075, // 39 px first line baseline viewHintDy: 0.155, // 81 px second (hint) baseline trackCy: 0.42, // 218 px trackVisH: 0.030, // 16 px, ~8 mm of a 26 cm panel trackHitY0: 0.30, // 156 px band 156..270 px, far larger than the bar trackHitY1: 0.52, // 270 px textBaseline: 0.275, // 143 px textFont: 0.075, // 39 px btnY: 0.62, // 322 px btnVisualW: 0.105, // 151 px btnVisualH: 0.125, // 65 px btnHitY0: 0.52, // 270 px band 270..385 px btnHitY1: 0.74, // 385 px // Five buttons - the padlock joined the row at the far right (centres // 0.18 / 0.36 / 0.54 / 0.72 / 0.90, pills 0.088..0.992). btnHitHalfW: 0.092, btnSpacing: 0.18, btnFirstCx: 0.18, // Icon over label inside each media button: the label says what the // button does, the icon keeps the quick visual cue. btnIconDy: -0.026, // -13.5 px, icon centre above the button centre btnIconSize: 0.065, // 34 px icon box btnLabelFont: 0.040, // 21 px label btnLabelDy: 0.041, // 21 px label baseline below the button centre // ADDENDUM 4 subtitle row (ADDENDUM 6: nine buttons), tiling the full // width (centres 0.10 .. 0.90, pitch 0.10, symmetric). The hit pills are // 0.104 wide on a 0.10 pitch, so adjacent hit regions overlap by a hair // instead of leaving a dead zone between. subLabelBaseline: 0.795, // 413 px caption baseline subCaptionFont: 0.040, // 21 px caption (distance + size + opacity readout) subBtnY: 0.925, // 481 px button centre subVisualW: 0.092, // 132 px subVisualH: 0.120, // 62 px subHitY0: 0.84, // 437 px band 437..520 px (below the caption) subHitY1: 1.0, // 520 px subHitHalfW: 0.052, subSpacing: 0.10, subFirstCx: 0.10, subIconDy: -0.020, // -10.4 px icon centre above the button centre subIconSize: 0.046, // 24 px icon box subLabelFont: 0.034, // 17.7 px label subLabelDy: 0.036 // 18.7 px label baseline below the button centre }, // ADDENDUM 7b: the locked panel. ONE target — the padlock — centred, with a // label that says what pressing it does. Nothing else is drawn or hit-tested, // so "locked" cannot leak any adjustable control. locked: { lockCx: 0.5, lockCy: 0.40, lockIconSize: 0.62, // fraction of h lockLabelFont: 0.155, lockLabelDy: 0.74, lockHitHalfW: 0.30, lockHitHalfH: 0.48 } }; /* ------------------------------------------------------------------ * * Small pure helpers. * ------------------------------------------------------------------ */ function num(v) { return (typeof v === 'number' && isFinite(v)) ? v : null; } function clamp(v, a, b) { return v < a ? a : (v > b ? b : v); } // mm:ss, or h:mm:ss past an hour. function fmtTime(t) { var v = num(t); if (v == null || v < 0) v = 0; v = Math.floor(v); var h = Math.floor(v / 3600); var m = Math.floor((v % 3600) / 60); var s = v % 60; var pad = function (n) { return (n < 10 ? '0' + n : '' + n); }; return h > 0 ? (h + ':' + pad(m) + ':' + pad(s)) : (pad(m) + ':' + pad(s)); } function modeFor(w, h) { return (w / h) >= COLLAPSED_RATIO ? 'collapsed' : 'expanded'; } // Rounded-rectangle path (no ctx.roundRect dependency; deterministic). function rr(ctx, x, y, w, h, r) { r = Math.min(r, Math.abs(w) * 0.5, Math.abs(h) * 0.5, h * 0.5); if (r < 0) r = 0; ctx.beginPath(); ctx.moveTo(x + r, y); ctx.lineTo(x + w - r, y); ctx.arcTo(x + w, y, x + w, y + r, r); ctx.lineTo(x + w, y + h - r); ctx.arcTo(x + w, y + h, x + w - r, y + h, r); ctx.lineTo(x + r, y + h); ctx.arcTo(x, y + h, x, y + h - r, r); ctx.lineTo(x, y + r); ctx.arcTo(x, y, x + r, y, r); ctx.closePath(); } // Per-character advance in em, used only to keep every label inside its // button. The required font stack is generic (no webfonts), so the real // metrics are unknown; this deliberately errs on the wide side instead of // measuring. function emAdvance(ch) { var c = ch.charCodeAt(0); if (c >= 0x2e80) return 1.0; // CJK / full-width glyph if (c >= 48 && c <= 57) return 0.56; // digits if (ch === '-') return 0.45; if (c >= 65 && c <= 90) return 0.66; // upper-case latin return 0.56; // lower-case latin / fallback } function textEm(s) { var em = 0; for (var i = 0; i < s.length; i++) em += emAdvance(s.charAt(i)); return em; } // Centred button label under the icon. `maxPx` comes from LAYOUT (thus from // h); it is only reduced when the estimated width would leave less than 12 % // of `availW` as padding, so no label can overflow its button. function drawLabel(ctx, text, cx, baselineY, maxPx, availW, color) { var px = maxPx; var em = textEm(text); if (em > 0) px = Math.min(px, (availW * 0.88) / em); px = Math.max(8, px); ctx.font = '600 ' + Math.round(px) + 'px ' + FONT; ctx.textAlign = 'center'; ctx.textBaseline = 'alphabetic'; ctx.fillStyle = color; ctx.fillText(text, cx, baselineY); } /* ------------------------------------------------------------------ * * draw() — pure 2D rendering, no WebXR, no timers, no side effects. * ------------------------------------------------------------------ */ function draw(ctx, w, h, view) { if (!ctx || !(w > 0) || !(h > 0)) return; view = view || {}; var mode = (view.mode === 'expanded' || view.mode === 'locked') ? view.mode : 'collapsed'; var lay = LAYOUT[mode]; ctx.clearRect(0, 0, w, h); // Translucent pill so the controls read over arbitrary video. ctx.fillStyle = 'rgba(0,0,0,0.55)'; rr(ctx, 0, 0, w, h, h * 0.5); ctx.fill(); ctx.strokeStyle = 'rgba(255,255,255,0.14)'; ctx.lineWidth = Math.max(1, h * 0.01); ctx.stroke(); // ADDENDUM 7b: the locked panel is JUST the padlock — no progress bar, no // buttons, nothing adjustable. That is the whole point of the lock. if (mode === 'locked') { var lk = view.lock !== false; // locked is the default for this mode var lcx = lay.lockCx * w, lcy = lay.lockCy * h; var lsz = lay.lockIconSize * h; if (view.hover === 'lock') { ctx.fillStyle = 'rgba(78,161,255,0.30)'; rr(ctx, lcx - w * 0.40, lcy - h * 0.42, w * 0.80, h * 0.84, h * 0.42); ctx.fill(); } drawLockIcon(ctx, lcx, lcy, lsz, lk); drawLabel(ctx, lk ? '解锁' : '上锁', lcx, lay.lockLabelDy * h, lay.lockLabelFont * h, w * 0.92, lk ? ACCENT : '#ffffff'); drawCursor(ctx, w, h, view.pointer); return; } var dur = num(view.duration); var cur = num(view.currentTime); var prog = (dur != null && dur > 0) ? clamp((cur == null ? 0 : cur) / dur, 0, 1) : 0; var bx0 = TRACK_X0 * w; var bx1 = TRACK_X1 * w; var bw = bx1 - bx0; var trackCy = lay.trackCy * h; var tvh = Math.max(2, lay.trackVisH * h); // Track background. ctx.fillStyle = 'rgba(255,255,255,0.22)'; rr(ctx, bx0, trackCy - tvh / 2, bw, tvh, tvh / 2); ctx.fill(); // Buffered-range hint (only when a 0..1 number is supplied). var buf = num(view.buffered); if (buf != null && buf > 0) { buf = clamp(buf, 0, 1); if (buf > prog) { ctx.fillStyle = 'rgba(255,255,255,0.34)'; rr(ctx, bx0, trackCy - tvh / 2, Math.max(tvh, bw * buf), tvh, tvh / 2); ctx.fill(); } } // Played portion. var pw = bw * prog; if (pw > 0.5) { ctx.fillStyle = ACCENT; rr(ctx, bx0, trackCy - tvh / 2, Math.max(tvh, pw), tvh, tvh / 2); ctx.fill(); } // Knob / scrub handle; the track hover state enlarges and tints it. var hoveringTrack = view.hover === 'track'; var knobR = tvh * (hoveringTrack ? 1.2 : 0.95); ctx.fillStyle = hoveringTrack ? ACCENT : '#ffffff'; ctx.beginPath(); ctx.arc(clamp(bx0 + pw, bx0, bx1), trackCy, knobR, 0, Math.PI * 2); ctx.closePath(); ctx.fill(); if (mode === 'expanded') { drawViewStrip(ctx, w, h, view, lay); drawExpanded(ctx, w, h, view, lay, dur, cur); } drawCursor(ctx, w, h, view.pointer); } // Where the input ray will land, in normalized panel coords (origin // top-left). `view.pointer` is optional and absent when nothing is aimed at // the panel, so this is a no-op then. Pure, like the rest of draw(). function drawCursor(ctx, w, h, pointer) { if (!pointer) return; var nx = num(pointer.nx); var ny = num(pointer.ny); if (nx == null || ny == null) return; if (nx < 0 || nx > 1 || ny < 0 || ny > 1) return; var x = nx * w; var y = ny * h; var pressed = !!pointer.pressed; // A press visibly flashes: the ring swells and fills. Without this the UI // gave no acknowledgement at all, which read as "clicking does nothing". var r = Math.max(4, h * CURSOR_R) * (pressed ? 1.9 : 1); if (pressed) { ctx.beginPath(); ctx.arc(x, y, r, 0, Math.PI * 2); ctx.closePath(); ctx.fillStyle = 'rgba(78,161,255,0.45)'; ctx.fill(); } // Dark backing ring keeps the reticle legible on white video frames. ctx.beginPath(); ctx.arc(x, y, r, 0, Math.PI * 2); ctx.closePath(); ctx.strokeStyle = 'rgba(0,0,0,0.75)'; ctx.lineWidth = Math.max(2, r * 0.5); ctx.stroke(); ctx.strokeStyle = pressed ? '#ffffff' : ACCENT; ctx.lineWidth = Math.max(1.5, r * (pressed ? 0.34 : 0.26)); ctx.stroke(); ctx.fillStyle = '#ffffff'; ctx.beginPath(); ctx.arc(x, y, Math.max(1.5, r * (pressed ? 0.34 : 0.2)), 0, Math.PI * 2); ctx.closePath(); ctx.fill(); } // ADDENDUM 8 — the picture-pan drag strip, ABOVE the progress bar. // It is one self-contained affordance: the grip says "grab me", the values say // where the picture currently points, and the hint says how to reset. Pure like // the rest of draw(): no state, no timers. function drawViewStrip(ctx, w, h, view, lay) { var v = view.view || {}; var yaw = num(v.yawDeg); var pitch = num(v.pitchDeg); if (yaw == null) yaw = 0; if (pitch == null) pitch = 0; var x0 = num(lay.viewStripX0) * w; var x1 = num(lay.viewStripX1) * w; var y0 = num(lay.viewStripY0) * h; var y1 = num(lay.viewStripY1) * h; var sw = x1 - x0, sh = y1 - y0; if (sw <= 0 || sh <= 0) return; var dragging = view.hover === 'view_drag'; ctx.fillStyle = dragging ? 'rgba(78,161,255,0.26)' : 'rgba(255,255,255,0.10)'; rr(ctx, x0, y0, sw, sh, sh * 0.30); ctx.fill(); ctx.strokeStyle = dragging ? ACCENT : 'rgba(255,255,255,0.26)'; ctx.lineWidth = Math.max(1.5, sh * 0.035); if (typeof ctx.setLineDash === 'function') ctx.setLineDash([sh * 0.22, sh * 0.16]); rr(ctx, x0, y0, sw, sh, sh * 0.30); ctx.stroke(); if (typeof ctx.setLineDash === 'function') ctx.setLineDash([]); // Grip: a 2x3 dot pad, the universal "this is draggable" mark. var gy = (y0 + y1) / 2; var gx = x0 + sh * 0.72; var dot = Math.max(1.5, sh * 0.075); var dx = sh * 0.30, dy = sh * 0.20; ctx.fillStyle = dragging ? '#ffffff' : 'rgba(255,255,255,0.75)'; for (var r = -1; r <= 1; r++) { for (var c = -1; c <= 1; c += 2) { ctx.beginPath(); ctx.arc(gx + c * dx * 0.5, gy + r * dy, dot, 0, Math.PI * 2); ctx.fill(); } } var textX = gx + sh * 0.60; ctx.textBaseline = 'alphabetic'; var f1 = Math.max(10, Math.round(num(lay.viewLabelFont) * h)); ctx.textAlign = 'left'; ctx.fillStyle = '#ffffff'; ctx.font = '600 ' + f1 + 'px ' + FONT; ctx.fillText('拖动调整画面', textX, num(lay.viewLabelDy) * h); ctx.font = Math.max(9, Math.round(num(lay.viewHintFont) * h)) + 'px ' + FONT; ctx.fillStyle = 'rgba(255,255,255,0.70)'; ctx.fillText('点一下复位 · 按住 0.5s 再拖动', textX, num(lay.viewHintDy) * h); // Current angles, right-aligned: the numbers to watch while dragging. var f2 = Math.max(10, Math.round(num(lay.viewValueFont) * h)); ctx.textAlign = 'right'; ctx.font = '700 ' + f2 + 'px ' + FONT; ctx.fillStyle = dragging ? ACCENT : '#ffffff'; ctx.fillText('偏航 ' + fmtSigned(yaw) + '\u00b0 俯仰 ' + fmtSigned(pitch) + '\u00b0', x1 - sh * 0.30, num(lay.viewLabelDy) * h); ctx.font = Math.max(9, Math.round(num(lay.viewHintFont) * h)) + 'px ' + FONT; ctx.fillStyle = 'rgba(255,255,255,0.60)'; ctx.fillText('0\u00b0 / 0\u00b0 = 正前方', x1 - sh * 0.30, num(lay.viewHintDy) * h); } // Signed degree readout for the drag strip: "+12" / "-8" / "0". function fmtSigned(deg) { var r = Math.round(deg); if (r > 0) return '+' + r; return String(r); } function drawExpanded(ctx, w, h, view, lay, dur, cur) { var fontSize = Math.max(10, Math.round(lay.textFont * h)); // Elapsed / duration readout above the scrubber. ctx.font = '600 ' + fontSize + 'px ' + FONT; ctx.textBaseline = 'alphabetic'; ctx.fillStyle = '#ffffff'; if (ctx.shadowColor !== undefined) { ctx.shadowColor = 'rgba(0,0,0,0.9)'; ctx.shadowBlur = fontSize * 0.18; } ctx.textAlign = 'left'; ctx.fillText(fmtTime(cur), TRACK_X0 * w, lay.textBaseline * h); ctx.textAlign = 'right'; ctx.fillText(fmtTime(dur), TRACK_X1 * w, lay.textBaseline * h); if (ctx.shadowColor !== undefined) { ctx.shadowColor = 'rgba(0,0,0,0)'; ctx.shadowBlur = 0; } // Buttons row: back10, play/pause, fwd10, subs. Each button carries its icon // plus a short Chinese label, both scaled from h (owner feedback: the // icon-only row was not self-explanatory). var size = lay.btnIconSize * h; var iconDy = lay.btnIconDy * h; var labelDy = lay.btnLabelDy * h; var labelPx = lay.btnLabelFont * h; for (var i = 0; i < BUTTONS.length; i++) { var id = BUTTONS[i]; var cx = (lay.btnFirstCx + i * lay.btnSpacing) * w; var cy = lay.btnY * h; var bw = lay.btnVisualW * w; var bh = lay.btnVisualH * h; var hovered = view.hover === id; if (hovered) { ctx.fillStyle = 'rgba(78,161,255,0.32)'; rr(ctx, cx - bw / 2, cy - bh / 2, bw, bh, bh * 0.35); ctx.fill(); } var color = '#ffffff'; var label = LABELS[id]; if (id === 'playpause') { if (view.playing) { iconPause(ctx, cx, cy + iconDy, size, color); label = '暂停'; } else { iconPlay(ctx, cx, cy + iconDy, size, color); label = '播放'; } } else if (id === 'back10') { iconSkip(ctx, cx, cy + iconDy, size, color, -1); } else if (id === 'fwd10') { iconSkip(ctx, cx, cy + iconDy, size, color, 1); } else if (id === 'subs') { // The toggle must SHOW its state: a filled accent pill while subtitles // are on, plus an accent icon/label. (Before, this read a module-local // flag that main.js never set, so the button always looked OFF.) if (view.subtitlesOn) { color = ACCENT; label = '字幕开'; if (!hovered) { ctx.fillStyle = 'rgba(78,161,255,0.22)'; rr(ctx, cx - bw / 2, cy - bh / 2, bw, bh, bh * 0.35); ctx.fill(); } } else { label = '字幕关'; } iconSubs(ctx, cx, cy + iconDy, size, color, !!view.subtitlesOn); } else if (id === 'lock') { // ADDENDUM 7b: the padlock. In the expanded panel it is always the // "lock" direction (locking swaps the panel to the minimal mode), but the // label follows view.lock so the same button can serve both directions. var lkOn = !!view.lock; if (lkOn) color = ACCENT; label = lkOn ? '解锁' : '上锁'; drawLockIcon(ctx, cx, cy + iconDy, size, lkOn); } drawLabel(ctx, label, cx, cy + labelDy, labelPx, bw, color); if (hovered) { ctx.strokeStyle = ACCENT; ctx.lineWidth = Math.max(1.5, bh * 0.06); rr(ctx, cx - bw / 2, cy - bh / 2, bw, bh, bh * 0.35); ctx.stroke(); } } drawSubRow(ctx, w, h, view, lay); } // ADDENDUM 4 subtitle row (ADDENDUM 6: seven buttons): a caption line with // the current values (angular width in degrees while locked, physical width in // metres while free; opacity as a percentage on the right) over the buttons. // Same hover treatment as the media row. Pure like the rest of draw(). function drawSubRow(ctx, w, h, view, lay) { var sub = view.sub || null; var width = sub ? num(sub.width) : null; var opacity = sub ? num(sub.opacity) : null; var angle = sub ? num(sub.angleDeg) : null; var dist = sub ? num(sub.dist) : null; var lock = sub && typeof sub.lock === 'boolean' ? sub.lock : SUB_DEFAULT.lock; if (width == null) width = SUB_DEFAULT.width; if (opacity == null) opacity = SUB_DEFAULT.opacity; if (angle == null) angle = SUB_DEFAULT.angleDeg; if (dist == null) dist = SUB_DEFAULT.dist; opacity = clamp(opacity, 0, 1); var fontSize = Math.max(10, Math.round((lay.subCaptionFont || lay.subLabelFont) * h)); ctx.font = '600 ' + fontSize + 'px ' + FONT; ctx.textBaseline = 'alphabetic'; ctx.fillStyle = '#ffffff'; if (ctx.shadowColor !== undefined) { ctx.shadowColor = 'rgba(0,0,0,0.9)'; ctx.shadowBlur = fontSize * 0.18; } ctx.textAlign = 'left'; // ADDENDUM 6: the caption always leads with the DEPTH in metres — that is the // number the user has to match against the object they are watching. The // second value is the active size control (locked angular width, or the // physical width when the lock is off). var sizeTxt = lock ? (Math.round(angle) + '\u00b0') : (width.toFixed(1) + 'm'); ctx.fillText('字幕 距' + dist.toFixed(2) + 'm · ' + sizeTxt, TRACK_X0 * w, lay.subLabelBaseline * h); ctx.textAlign = 'right'; ctx.fillText(Math.round(opacity * 100) + '%', TRACK_X1 * w, lay.subLabelBaseline * h); if (ctx.shadowColor !== undefined) { ctx.shadowColor = 'rgba(0,0,0,0)'; ctx.shadowBlur = 0; } var size = lay.subIconSize * h; var iconDy = lay.subIconDy * h; var labelDy = lay.subLabelDy * h; var labelPx = lay.subLabelFont * h; for (var i = 0; i < SUB_BUTTONS.length; i++) { var id = SUB_BUTTONS[i]; var cx = (lay.subFirstCx + i * lay.subSpacing) * w; var cy = lay.subBtnY * h; var bw = lay.subVisualW * w; var bh = lay.subVisualH * h; var hovered = view.hover === id; if (hovered) { ctx.fillStyle = 'rgba(78,161,255,0.32)'; rr(ctx, cx - bw / 2, cy - bh / 2, bw, bh, bh * 0.35); ctx.fill(); } if (id === 'sub_up') iconSubMove(ctx, cx, cy + iconDy, size, true); else if (id === 'sub_down') iconSubMove(ctx, cx, cy + iconDy, size, false); else if (id === 'sub_near') iconSubDepth(ctx, cx, cy + iconDy, size, false); else if (id === 'sub_far') iconSubDepth(ctx, cx, cy + iconDy, size, true); else if (id === 'sub_smaller') iconSubSize(ctx, cx, cy + iconDy, size, false); else if (id === 'sub_bigger') iconSubSize(ctx, cx, cy + iconDy, size, true); else if (id === 'sub_opaque_down') iconSubOpacity(ctx, cx, cy + iconDy, size, false, opacity); else if (id === 'sub_opaque_up') iconSubOpacity(ctx, cx, cy + iconDy, size, true, opacity); else if (id === 'sub_reset') iconSubReset(ctx, cx, cy + iconDy, size); drawLabel(ctx, LABELS[id], cx, cy + labelDy, labelPx, bw, '#ffffff'); if (hovered) { ctx.strokeStyle = ACCENT; ctx.lineWidth = Math.max(1.5, bh * 0.06); rr(ctx, cx - bw / 2, cy - bh / 2, bw, bh, bh * 0.35); ctx.stroke(); } } } // Vertical position pair (ADDENDUM 6): an arrow pointing up / down. Reads as // "move the subtitle strip up / down in the view". function iconSubMove(ctx, cx, cy, s, up) { ctx.save(); ctx.strokeStyle = '#ffffff'; ctx.lineCap = 'round'; ctx.lineJoin = 'round'; var d = up ? -1 : 1; // canvas +y is down ctx.lineWidth = Math.max(1.5, s * 0.09); ctx.beginPath(); ctx.moveTo(cx, cy + d * s * 0.30); ctx.lineTo(cx, cy - d * s * 0.26); ctx.stroke(); ctx.beginPath(); ctx.moveTo(cx - s * 0.18, cy - d * s * 0.06); ctx.lineTo(cx, cy - d * s * 0.28); ctx.lineTo(cx + s * 0.18, cy - d * s * 0.06); ctx.stroke(); ctx.restore(); } // Depth pair (ADDENDUM 6): chevrons pointing at the viewer (near) or away // (far), with a small plane marking the strip. Reads as "bring the subtitles // closer / push them away". function iconSubDepth(ctx, cx, cy, s, far) { ctx.save(); ctx.strokeStyle = '#ffffff'; ctx.lineCap = 'round'; ctx.lineJoin = 'round'; // the strip plane, offset toward the viewer end var px = cx + (far ? -s * 0.34 : s * 0.34); ctx.lineWidth = Math.max(1.5, s * 0.08); ctx.beginPath(); ctx.moveTo(px, cy - s * 0.24); ctx.lineTo(px, cy + s * 0.24); ctx.stroke(); // two chevrons: left = coming closer, right = going further ctx.lineWidth = Math.max(1.5, s * 0.075); var d = far ? 1 : -1; for (var k = 0; k < 2; k++) { var x = cx + d * (s * 0.02) + d * k * s * 0.22; ctx.beginPath(); ctx.moveTo(x - d * s * 0.11, cy - s * 0.18); ctx.lineTo(x + d * s * 0.05, cy); ctx.lineTo(x - d * s * 0.11, cy + s * 0.18); ctx.stroke(); } ctx.restore(); } // Size pair: the same 'A' at two sizes, plus a +/− mark. Reads as "make the // subtitle glyph bigger/smaller". function iconSubSize(ctx, cx, cy, s, bigger) { ctx.fillStyle = '#ffffff'; ctx.font = '700 ' + Math.max(8, Math.round(s * (bigger ? 0.6 : 0.38))) + 'px ' + FONT; ctx.textAlign = 'center'; ctx.textBaseline = 'middle'; ctx.fillText('A', cx - s * 0.1, cy); iconSign(ctx, cx + s * 0.3, cy, s, bigger); } // Opacity pair: a disc filled at the CURRENT opacity plus a +/− mark, so the // button doubles as a readout that brightens/dims with the value. function iconSubOpacity(ctx, cx, cy, s, up, opacity) { var r = s * 0.26; ctx.beginPath(); ctx.arc(cx - s * 0.1, cy, r, 0, Math.PI * 2); ctx.closePath(); ctx.fillStyle = 'rgba(255,255,255,' + (0.18 + 0.82 * clamp(opacity, 0, 1)).toFixed(3) + ')'; ctx.fill(); ctx.strokeStyle = '#ffffff'; ctx.lineWidth = Math.max(1.5, s * 0.06); ctx.stroke(); iconSign(ctx, cx + s * 0.3, cy, s, up); } function iconSign(ctx, cx, cy, s, plus) { ctx.save(); ctx.strokeStyle = '#ffffff'; ctx.lineWidth = Math.max(1.5, s * 0.075); ctx.lineCap = 'round'; var r = s * 0.13; ctx.beginPath(); ctx.moveTo(cx - r, cy); ctx.lineTo(cx + r, cy); ctx.stroke(); if (plus) { ctx.beginPath(); ctx.moveTo(cx, cy - r); ctx.lineTo(cx, cy + r); ctx.stroke(); } ctx.restore(); } // Reset: a circular arrow. function iconSubReset(ctx, cx, cy, s) { ctx.save(); ctx.strokeStyle = '#ffffff'; ctx.fillStyle = '#ffffff'; ctx.lineWidth = Math.max(1.5, s * 0.075); ctx.lineCap = 'round'; var r = s * 0.24; var a0 = Math.PI * 0.35; ctx.beginPath(); ctx.arc(cx, cy, r, a0, a0 + Math.PI * 1.5); ctx.stroke(); // Arrowhead at the arc's open end, pointing back along the tangent. var ax = cx + Math.cos(a0) * r; var ay = cy + Math.sin(a0) * r; var nx = Math.cos(a0); var ny = Math.sin(a0); var tx = Math.sin(a0); var ty = -Math.cos(a0); var hh = s * 0.14; ctx.beginPath(); ctx.moveTo(ax + tx * hh, ay + ty * hh); ctx.lineTo(ax - nx * hh * 0.6, ay - ny * hh * 0.6); ctx.lineTo(ax + nx * hh * 0.6, ay + ny * hh * 0.6); ctx.closePath(); ctx.fill(); ctx.restore(); } function iconPlay(ctx, cx, cy, s, color) { ctx.fillStyle = color; ctx.beginPath(); ctx.moveTo(cx - s * 0.26, cy - s * 0.34); ctx.lineTo(cx - s * 0.26, cy + s * 0.34); ctx.lineTo(cx + s * 0.34, cy); ctx.closePath(); ctx.fill(); } function iconPause(ctx, cx, cy, s, color) { ctx.fillStyle = color; var bw = s * 0.13; var bh = s * 0.72; var gap = s * 0.12; rr(ctx, cx - gap - bw, cy - bh / 2, bw, bh, bw * 0.4); ctx.fill(); rr(ctx, cx + gap, cy - bh / 2, bw, bh, bw * 0.4); ctx.fill(); } function iconSkip(ctx, cx, cy, s, color, dir) { ctx.save(); ctx.strokeStyle = color; ctx.fillStyle = color; ctx.lineWidth = Math.max(1.5, s * 0.09); ctx.lineCap = 'round'; var r = s * 0.30; var a0 = dir < 0 ? -Math.PI * 0.35 : -Math.PI * 0.15; var a1 = dir < 0 ? Math.PI * 1.15 : Math.PI * 1.35; ctx.beginPath(); ctx.arc(cx, cy, r, a0, a1, dir > 0); ctx.stroke(); // Arrowhead at the open end, pointing along the tangent. var ax = cx + Math.cos(a1) * r; var ay = cy + Math.sin(a1) * r; var nx = Math.cos(a1); var ny = Math.sin(a1); var tx = -ny; var ty = nx; if (dir > 0) { tx = -tx; ty = -ty; } var hh = s * 0.16; ctx.beginPath(); ctx.moveTo(ax + tx * hh, ay + ty * hh); ctx.lineTo(ax + nx * hh * 0.7, ay + ny * hh * 0.7); ctx.lineTo(ax - nx * hh * 0.7, ay - ny * hh * 0.7); ctx.closePath(); ctx.fill(); ctx.restore(); ctx.fillStyle = color; ctx.font = '700 ' + Math.max(8, Math.round(s * 0.42)) + 'px ' + FONT; ctx.textAlign = 'center'; ctx.textBaseline = 'middle'; ctx.fillText('10', cx, cy + s * 0.02); } function iconSubs(ctx, cx, cy, s, color, on) { var w = s * 0.86; var h = s * 0.6; ctx.lineWidth = Math.max(1.5, s * 0.075); ctx.strokeStyle = color; rr(ctx, cx - w / 2, cy - h / 2, w, h, h * 0.24); if (on) { ctx.fillStyle = 'rgba(78,161,255,0.25)'; ctx.fill(); } ctx.stroke(); ctx.fillStyle = color; ctx.font = '700 ' + Math.max(8, Math.round(s * 0.34)) + 'px ' + FONT; ctx.textAlign = 'center'; ctx.textBaseline = 'middle'; ctx.fillText('CC', cx, cy + s * 0.01); if (!on) { // OFF is not just "less bright": the CC glyph gets a slash, so the state // reads at a glance even with the accent tint lost against bright video. ctx.strokeStyle = color; ctx.lineWidth = Math.max(1.5, s * 0.08); ctx.beginPath(); ctx.moveTo(cx - w * 0.5, cy + h * 0.55); ctx.lineTo(cx + w * 0.5, cy - h * 0.55); ctx.stroke(); } } /* ------------------------------------------------------------------ * * Beam geometry — pure math + a texture generator. No WebXR types here so * the whole thing is unit-testable. * ------------------------------------------------------------------ */ function norm3(v) { if (!v) return null; var x = num(v.x), y = num(v.y), z = num(v.z); if (x == null || y == null || z == null) return null; var l = Math.sqrt(x * x + y * y + z * z); if (!(l > 1e-9)) return null; return { x: x / l, y: y / l, z: z / l }; } function cross3(a, b) { return { x: a.y * b.z - a.z * b.y, y: a.z * b.x - a.x * b.z, z: a.x * b.y - a.y * b.x }; } // Quaternion from an orthonormal basis given as its local axes, so the // resulting rotation maps local +X/+Y/+Z onto (ax, ay, az). function quatFromAxes(ax, ay, az) { var m00 = ax.x, m01 = ay.x, m02 = az.x; var m10 = ax.y, m11 = ay.y, m12 = az.y; var m20 = ax.z, m21 = ay.z, m22 = az.z; var q = { x: 0, y: 0, z: 0, w: 1 }; var tr = m00 + m11 + m22; var s; if (tr > 0) { s = Math.sqrt(tr + 1) * 2; q.w = 0.25 * s; q.x = (m21 - m12) / s; q.y = (m02 - m20) / s; q.z = (m10 - m01) / s; } else if (m00 > m11 && m00 > m22) { s = Math.sqrt(1 + m00 - m11 - m22) * 2; q.w = (m21 - m12) / s; q.x = 0.25 * s; q.y = (m01 + m10) / s; q.z = (m02 + m20) / s; } else if (m11 > m22) { s = Math.sqrt(1 + m11 - m00 - m22) * 2; q.w = (m02 - m20) / s; q.x = (m01 + m10) / s; q.y = 0.25 * s; q.z = (m12 + m21) / s; } else { s = Math.sqrt(1 + m22 - m00 - m11) * 2; q.w = (m10 - m01) / s; q.x = (m02 + m20) / s; q.y = (m12 + m21) / s; q.z = 0.25 * s; } return q; } var WORLD_UP = { x: 0, y: 1, z: 0 }; var WORLD_BACK = { x: 0, y: 0, z: 1 }; // Component of `v` perpendicular to the unit vector `u` (null when parallel). function perp(u, v) { var d = u.x * v.x + u.y * v.y + u.z * v.z; return norm3({ x: v.x - u.x * d, y: v.y - u.y * d, z: v.z - u.z * d }); } // Orientation for the beam quad. // // local +X is the quad's width/long axis and also the texture's u axis, and it // is mapped onto `dir`: the beam then lies ALONG the ray (a quad's local -Z is // its plane normal, so mapping -Z onto dir would lay the strip across the ray // and it could never read as a laser). // // local -Z is the face normal. It must be perpendicular to `dir` AND point at // the eye, otherwise the ribbon is seen edge-on and effectively disappears. // // A world-up bias is NOT good enough: measured on device, a hand-held ray gave // a near-vertical normal while the viewer was level with the beam, a 68.7 deg // angle between normal and view direction, and only ~2 mm of projected width // at 0.8 m - i.e. invisible. In viewer space the eye IS the origin, so the // direction from the beam to the eye is simply -pos; take the component of // that perpendicular to the beam axis. // // Texture consequence: u = 0 is the local -X end = `origin`, so the gradient // (bright at u = 0) runs origin -> hit. function beamQuat(dir, pos, crossed) { var x = norm3(dir); if (!x) return { x: 0, y: 0, z: 0, w: 1 }; var toEye = null; if (pos) { var pl = Math.sqrt(pos.x * pos.x + pos.y * pos.y + pos.z * pos.z); if (pl > 1e-4) toEye = { x: -pos.x / pl, y: -pos.y / pl, z: -pos.z / pl }; } var n = toEye ? perp(x, toEye) : null; // face the viewer when possible if (!n) n = perp(x, WORLD_UP); if (!n) n = perp(x, WORLD_BACK); if (!n) return { x: 0, y: 0, z: 0, w: 1 }; // The second ribbon of the crossed pair is rolled 90 deg about the beam axis. if (crossed) n = norm3(cross3(x, n)) || n; var z = { x: -n.x, y: -n.y, z: -n.z }; // local +Z; the front normal is -Z = n var y = cross3(z, x); // right-handed basis: Y = Z x X return quatFromAxes(x, y, z); } // Soft laser gradient, premultiplied RGBA: bright at u = 0 (the origin end), // gone at u = 1, feathered across v so the beam has no hard edge. function buildBeamTexture(w, h) { var data = new Uint8Array(w * h * 4); for (var y = 0; y < h; y++) { var v = (y + 0.5) / h; var edge = Math.sin(Math.PI * v); edge = edge * edge; for (var x = 0; x < w; x++) { var u = (x + 0.5) / w; var a = (0.15 + 0.85 * Math.pow(1 - u, 1.3)) * edge; if (a > 1) a = 1; else if (a < 0) a = 0; var i = (y * w + x) * 4; data[i] = Math.round(78 * a); // ACCENT, premultiplied by a data[i + 1] = Math.round(161 * a); data[i + 2] = Math.round(255 * a); data[i + 3] = Math.round(255 * a); } } return data; } /* ------------------------------------------------------------------ * * hitTest() — pure geometry of (nx, ny, w, h). * Mode is inferred from the panel aspect ratio. Every button (media row and * subtitle row) is tested before the track, so no button can ever be shadowed * by the seek band; the hit bands themselves do not overlap. * ------------------------------------------------------------------ */ function hitTest(nx, ny, w, h, modeHint) { if (!(w > 0) || !(h > 0)) return null; if (typeof nx !== 'number' || typeof ny !== 'number') return null; if (!(nx >= 0 && nx <= 1 && ny >= 0 && ny <= 1)) return null; var mode = (modeHint === 'expanded' || modeHint === 'locked') ? modeHint : modeFor(w, h); var lay = LAYOUT[mode]; // Locked panel: one target, the padlock, and it is a free hit over most of // the pill (the pill is small, so padding is not needed - and a bigger // window would make the summon gesture press it by accident). if (mode === 'locked') { if (Math.abs(nx - lay.lockCx) <= lay.lockHitHalfW && Math.abs(ny - lay.lockCy) <= lay.lockHitHalfH) { return { type: 'lock' }; } return null; } if (mode === 'expanded') { // ADDENDUM 8: the drag strip first. Its band (0..0.20) does not overlap the // seek band (0.30..0.52) or the button bands (0.52+), so order is cosmetic. if (ny >= lay.viewHitY0 && ny <= lay.viewHitY1 && nx >= lay.viewStripX0 && nx <= lay.viewStripX1) { return { type: 'view_drag' }; } var i, cx, y0 = lay.btnHitY0, y1 = lay.btnHitY1; for (i = 0; i < BUTTONS.length; i++) { cx = lay.btnFirstCx + i * lay.btnSpacing; if (ny >= y0 && ny <= y1 && nx >= cx - lay.btnHitHalfW && nx <= cx + lay.btnHitHalfW) { return { type: BUTTONS[i] }; } } var sy0 = lay.subHitY0, sy1 = lay.subHitY1; for (i = 0; i < SUB_BUTTONS.length; i++) { cx = lay.subFirstCx + i * lay.subSpacing; if (ny >= sy0 && ny <= sy1 && nx >= cx - lay.subHitHalfW && nx <= cx + lay.subHitHalfW) { return { type: SUB_BUTTONS[i] }; } } } if (ny >= lay.trackHitY0 && ny <= lay.trackHitY1) { var value = clamp((nx - TRACK_X0) / (TRACK_X1 - TRACK_X0), 0, 1); return { type: 'seek', value: value }; } return null; } /* ------------------------------------------------------------------ * * Module state (closures only — nothing leaks onto __JFVR except the * documented API). * ------------------------------------------------------------------ */ var S = { // 'collapsed' | 'expanded' | 'locked'. ADDENDUM 7b: "locked" is a minimal // panel carrying only the padlock. The mode is NOT reset on hide() any more — // the layer keeps the size it will be shown at, so the summon window is the // full panel rect and re-showing needs no resize. mode: 'expanded', shown: false, hover: null, subtitlesOn: false, video: null, getExtra: null, onActionCb: null, reticle: null, autoHideTimer: null, // visibility / opacity (ADDENDUM 2) opacity: 1, layerOpacityOk: false, // latest setPointer() payload, plus the beam quad it drives pointer: null, beamOn: false, beamSig: null, beamLayer: null, beamLayer2: null, beamTex: null, // padlock (ADDENDUM 7b) — panel state only, no own layer lockOn: false, hold: false, // ADDENDUM 8: drag in progress -> freeze auto-hide // XR resources canvas: null, ctx2d: null, gl: null, prog: null, attrLoc: -1, vbo: null, tex: null, fbo: null, binding: null, layer: null, hookFn: null, created: false, // redraw bookkeeping lastSig: null, lastPtrSig: null, lastDrawTime: -1e9, forceRedraw: true }; var FS = [ '#version 300 es', 'precision mediump float;', 'in vec2 vUv;', 'uniform sampler2D uTex;', 'out vec4 outColor;', 'void main(){ outColor = texture(uTex, vUv); }' ].join('\n'); function setErr(msg) { var s = stats(); if (s) s.ctlErr = msg; } // Single place that keeps the ctl* diagnostic keys in sync with the module. function updateVisibilityStats() { var s = stats(); if (!s) return; s.ctlVisible = S.shown; s.ctlHidden = !S.shown; s.ctlMode = S.mode; s.ctlLock = !!S.lockOn; s.ctlBeamOn = S.beamOn; s.ctlOpacity = S.opacity; s.ctlPointer = S.pointer ? ((S.pointer.visible ? S.pointer.source : 'gaze') + (S.pointer.hit ? ' @' + S.pointer.hit.nx.toFixed(2) + ',' + S.pointer.hit.ny.toFixed(2) : ' @-')) : '(none)'; } function init() { S.mode = 'expanded'; S.shown = false; S.hover = null; S.opacity = 1; S.pointer = null; S.beamOn = false; S.beamSig = null; var s = stats(); if (s) { s.ctlDraws = s.ctlDraws || 0; s.ctlLastAction = s.ctlLastAction || null; s.ctlHover = null; s.ctlErr = s.ctlErr || null; s.ctlLock = !!S.lockOn; s.ctlMode = S.lockOn ? 'locked' : 'expanded'; } updateVisibilityStats(); } /* ------------------------------------------------------------------ * * Media readers — injected via setVideo(), else read from the element. * ------------------------------------------------------------------ */ function reader(extra, name) { if (!extra) return null; var fn = extra[name]; if (typeof fn === 'function') { try { return fn(); } catch (e) { return null; } } return fn; } function readCurrentTime() { var v = reader(S.getExtra, 'currentTime'); if (v != null) return num(v); var el = S.video; return el ? num(el.currentTime) : null; } function readDuration() { var v = reader(S.getExtra, 'duration'); if (v != null) return num(v); var el = S.video; return el ? num(el.duration) : null; } function readBuffered() { var v = reader(S.getExtra, 'buffered'); if (v != null) return num(v); var el = S.video; if (!el || !el.buffered) return null; try { var len = el.buffered.length; if (!len) return null; var dur = readDuration(); if (!(dur > 0)) return null; return clamp(el.buffered.end(len - 1) / dur, 0, 1); } catch (e) { return null; } } function readPlaying() { var v = reader(S.getExtra, 'playing'); if (typeof v === 'boolean') return v; var el = S.video; return el ? (el.paused === false) : false; } function readSubtitles() { var v = reader(S.getExtra, 'subtitlesOn'); if (typeof v === 'boolean') return v; // main.js keeps the toggle in the SHARED state (state.subtitles), and only // ever passes a video to setVideo(); reading the module-local S.subtitlesOn // alone meant this always returned false, so the button never showed ON. var n = ns(); if (n && n.state && typeof n.state.subtitles === 'boolean') return n.state.subtitles; return !!S.subtitlesOn; } // What the cursor is currently over, so the button under it highlights. The // pointer position was already reduced to a hitTest() action by the draw path, // so this is just that lookup - and it means hover feedback needs no extra // plumbing from 90-main.js. // ADDENDUM 8: current video-window angles in DEGREES, read from the shared // state that 90-main.js owns (it owns the video layer's transform). function readViewAngles() { var n = ns(); var st = (n && n.state) || {}; var yaw = num(st.videoYaw); var pitch = num(st.videoPitch); return { yawDeg: yaw == null ? 0 : yaw * 180 / Math.PI, pitchDeg: pitch == null ? 0 : pitch * 180 / Math.PI }; } function readHover() { var extra = reader(S.getExtra, 'hover'); if (typeof extra === 'string') return extra; var p = S.pointer; if (!p || !p.hit) return null; var px = PANEL_PX[S.mode] || PANEL_PX.expanded; var a = hitTest(p.hit.nx, p.hit.ny, px.w, px.h, S.mode); if (!a) return null; return a.type === 'seek' ? 'track' : a.type; } // Current subtitle size/opacity/angle/depth for the ADDENDUM 4/6 row. main.js // owns the values (state.subPlacement / state.subOpacity / state.subAngleDeg, // persisted by main); this module only reads them so draw() can render the readout. function readSubValues() { var n = ns(); var st = (n && n.state) || {}; var p = st.subPlacement || {}; var width = num(p.width); var z = num(p.z); var opacity = num(st.subOpacity); var angle = num(st.subAngleDeg); return { width: width == null ? SUB_DEFAULT.width : width, opacity: opacity == null ? SUB_DEFAULT.opacity : clamp(opacity, 0, 1), angleDeg: angle == null ? SUB_DEFAULT.angleDeg : angle, lock: (typeof st.subLockAngle === 'boolean') ? st.subLockAngle : SUB_DEFAULT.lock, // Viewer space forward is -Z, so the DEPTH the user perceives is |z|. dist: z == null ? SUB_DEFAULT.dist : Math.abs(z) }; } // The cursor position draw() renders last. Absent (null) when the input ray // does not point at the panel, so there is nothing to highlight. function viewPointer() { var p = S.pointer; if (!p || !p.hit) return null; return { nx: p.hit.nx, ny: p.hit.ny, pressed: !!p.pressed }; } function buildView() { var v = { playing: readPlaying(), currentTime: readCurrentTime() || 0, duration: readDuration() || 0, buffered: readBuffered(), subtitlesOn: readSubtitles(), mode: S.mode, hover: readHover(), pointer: viewPointer(), lock: !!S.lockOn, // ADDENDUM 8: the video window's angles, in degrees, for the drag strip. view: readViewAngles(), sub: readSubValues() }; // Diagnostics: what the bar actually rendered, so the CC state can be checked // from CDP (stats.ctlSubsOn / ctlSubDist) without reading the canvas. var st = stats(); if (st) { st.ctlSubsOn = v.subtitlesOn; st.ctlSubDist = v.sub.dist; st.ctlSubAngle = v.sub.angleDeg; st.ctlSubLock = v.sub.lock; } return v; } // Media/canvas state only — the pointer is tracked separately (ptrSigOf) so a // moving ray can be redrawn at a capped rate without starving media changes. function sigOf(v) { return [ v.mode, v.playing ? 1 : 0, v.subtitlesOn ? 1 : 0, v.lock ? 1 : 0, v.hover || '', Math.floor(v.currentTime), Math.floor(v.duration), v.buffered == null ? '' : Math.round(v.buffered * 200), Math.round(v.sub.width * 100), Math.round(v.sub.opacity * 100), Math.round(v.sub.angleDeg), v.sub.lock ? 1 : 0, Math.round(v.sub.dist * 100), Math.round(v.view.yawDeg), Math.round(v.view.pitchDeg) ].join('|'); } function ptrSigOf(v) { if (!v.pointer) return '-'; return Math.round(v.pointer.nx * 400) + '/' + Math.round(v.pointer.ny * 400) + (v.pointer.pressed ? '!' : ''); } /* ------------------------------------------------------------------ * * XR lifecycle. * ------------------------------------------------------------------ */ function compile(gl, type, src) { var n = ns(); if (n && typeof n.compileShader === 'function') return n.compileShader(gl, type, src); var sh = gl.createShader(type); gl.shaderSource(sh, src); gl.compileShader(sh); if (!gl.getShaderParameter(sh, gl.COMPILE_STATUS)) { throw new Error(gl.getShaderInfoLog(sh) || 'shader compile failed'); } return sh; } function makeProgram(gl) { var n = ns(); var vsSrc = (n && n.QUAD_VS) ? n.QUAD_VS : null; if (!vsSrc) throw new Error('missing __JFVR.QUAD_VS'); var prog = gl.createProgram(); gl.attachShader(prog, compile(gl, gl.VERTEX_SHADER, vsSrc)); gl.attachShader(prog, compile(gl, gl.FRAGMENT_SHADER, FS)); gl.linkProgram(prog); if (!gl.getProgramParameter(prog, gl.LINK_STATUS)) { throw new Error(gl.getProgramInfoLog(prog) || 'controls program link failed'); } gl.useProgram(prog); return prog; } function bindGeometry(gl, prog) { var n = ns(); if (n && typeof n.bindFullscreenTriangle === 'function') { // Core helper returns the aPos attribute location; it owns the VBO. S.attrLoc = n.bindFullscreenTriangle(gl, prog); S.vbo = null; return; } var buf = gl.createBuffer(); gl.bindBuffer(gl.ARRAY_BUFFER, buf); gl.bufferData(gl.ARRAY_BUFFER, new Float32Array([-1, -1, 3, -1, -1, 3]), gl.STATIC_DRAW); var loc = gl.getAttribLocation(prog, 'aPos'); gl.enableVertexAttribArray(loc); gl.vertexAttribPointer(loc, 2, gl.FLOAT, false, 0, 0); S.attrLoc = loc; S.vbo = buf; } function applyModeToLayer() { var px = PANEL_PX[S.mode]; var m = PANEL_M[S.mode]; if (S.canvas) { S.canvas.width = px.w; S.canvas.height = px.h; S.ctx2d = S.canvas.getContext('2d'); } if (S.layer) { try { S.layer.width = m.w; S.layer.height = m.h; } catch (e) { /* read-only on some builds: layer keeps its size */ } } S.forceRedraw = true; updateVisibilityStats(); } // The bar layer is created once per session and never rebuilt: hiding sets // layer.opacity to 0 *and* drops it from the render state, so the bar cannot // stay on screen on a runtime that ignores layer.opacity. main.js still finds // it through getLayer() for its ray -> panel math while it is hidden. function applyBarVisible() { var n = ns(); var l = S.layer; if (!l) return; if (S.layerOpacityOk) { try { l.opacity = S.shown ? S.opacity : 0; } catch (e) { S.layerOpacityOk = false; } } if (S.shown) { if (n && typeof n.addLayer === 'function') n.addLayer(l); } else if (n && typeof n.removeLayer === 'function') { n.removeLayer(l); } } // Probe once whether the runtime honours layer.opacity; when it does not the // 2D context alpha dims the same pixels instead (see drawIntoCanvas). function probeLayerOpacity(l) { try { if (!l || !('opacity' in l)) return false; var prev = l.opacity; l.opacity = 0.5; var ok = (l.opacity === 0.5); l.opacity = prev; return ok; } catch (e) { return false; } } function uploadTexture() { if (!S.gl || !S.tex || !S.canvas) return; S.gl.bindTexture(S.gl.TEXTURE_2D, S.tex); S.gl.pixelStorei(S.gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, true); S.gl.texImage2D(S.gl.TEXTURE_2D, 0, S.gl.RGBA, S.gl.RGBA, S.gl.UNSIGNED_BYTE, S.canvas); } function drawIntoCanvas(view) { S.ctx2d.globalAlpha = S.layerOpacityOk ? 1 : S.opacity; draw(S.ctx2d, S.canvas.width, S.canvas.height, view); S.ctx2d.globalAlpha = 1; uploadTexture(); } function present(frame, layer, tex, fbW, fbH) { if (!S.gl || !S.binding || !layer || !tex) return; var sub = S.binding.getSubImage(layer, frame); if (!sub || !sub.colorTexture) return; var gl = S.gl; gl.bindFramebuffer(gl.FRAMEBUFFER, S.fbo); gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, sub.colorTexture, 0); gl.viewport(0, 0, sub.colorTextureWidth || fbW, sub.colorTextureHeight || fbH); gl.clearColor(0, 0, 0, 0); gl.clear(gl.COLOR_BUFFER_BIT); gl.useProgram(S.prog); gl.activeTexture(gl.TEXTURE0); gl.bindTexture(gl.TEXTURE_2D, tex); gl.drawArrays(gl.TRIANGLES, 0, 3); gl.bindFramebuffer(gl.FRAMEBUFFER, null); } function presentBar(frame) { if (!S.layer || !S.tex || !S.canvas) return; present(frame, S.layer, S.tex, S.canvas.width, S.canvas.height); } function presentBeam(frame) { if (!S.beamOn || !S.beamLayer || !S.beamTex) return; present(frame, S.beamLayer, S.beamTex, BEAM_TEX.w, BEAM_TEX.h); if (S.beamLayer2) present(frame, S.beamLayer2, S.beamTex, BEAM_TEX.w, BEAM_TEX.h); } /* ------------------------------------------------------------------ * * Beam: transform + visibility. Both origin/dir and the beam layer live in * VIEWER space (90-main.js computes the pointer there), so the ray * coordinates are used directly, with no per-frame space conversion. * ------------------------------------------------------------------ */ // Distance from the ray origin to the panel plane, used only when main does // not supply a hit distance. Same plane math main.js uses. function panelDistance(p) { var t = (S.layer && S.layer.transform) ? S.layer.transform.position : null; var cz = t ? num(t.z) : null; if (cz == null) cz = -PANEL_Z; if (!p.dir || Math.abs(p.dir.z) < 1e-6) return null; var d = (cz - p.origin.z) / p.dir.z; return d > 0 ? d : null; } function beamLength(p) { if (!p.hit) return BEAM_L_DEFAULT; var d = num(p.distance); if (d == null) d = panelDistance(p); if (d == null || !(d > BEAM_L_MIN)) return BEAM_L_DEFAULT; return clamp(d, BEAM_L_MIN, BEAM_L_MAX); } function beamSigOf(p, len) { return [ Math.round(p.origin.x * 1000), Math.round(p.origin.y * 1000), Math.round(p.origin.z * 1000), Math.round(p.dir.x * 1000), Math.round(p.dir.y * 1000), Math.round(p.dir.z * 1000), Math.round(len * 1000) ].join('|'); } function updateBeamTransform() { var p = S.pointer; if (!p || !p.origin || !p.dir || !S.beamLayer) return; var len = beamLength(p); var sig = beamSigOf(p, len); if (sig === S.beamSig) return; // 1 mm granularity: no churn S.beamSig = sig; var mid = { x: p.origin.x + p.dir.x * len * 0.5, y: p.origin.y + p.dir.y * len * 0.5, z: p.origin.z + p.dir.z * len * 0.5 }; try { S.beamLayer.transform = new XRRigidTransform(mid, beamQuat(p.dir, mid, false)); S.beamLayer.width = len; S.beamLayer.height = BEAM_H; if (S.beamLayer2) { S.beamLayer2.transform = new XRRigidTransform(mid, beamQuat(p.dir, mid, true)); S.beamLayer2.width = len; S.beamLayer2.height = BEAM_H; } } catch (e) { setErr('beam transform: ' + ((e && e.message) || e)); } } // The beam is an AIMING GUIDE: it must be visible whenever an input source is // pointing, independently of whether the bar is shown. Requiring S.shown here // was a bug - the bar only appears on activation, so the user could never aim // at it before pinching. function applyBeamVisible() { var n = ns(); var l = S.beamLayer; var on = !!(S.pointer && S.pointer.visible && S.pointer.beam !== false && l); if (on === S.beamOn) return; S.beamOn = on; var both = [S.beamLayer, S.beamLayer2]; for (var i = 0; i < both.length; i++) { var b = both[i]; if (!b) continue; try { b.opacity = on ? 1 : 0; } catch (e) { void e; } if (n) { if (on && typeof n.addLayer === 'function') n.addLayer(b); else if (!on && typeof n.removeLayer === 'function') n.removeLayer(b); } } updateVisibilityStats(); } function hook(frame, time, session) { try { if (!S.canvas || !S.ctx2d) return; // NOTE: this must NOT early-return on !S.shown. The beam is presented every // frame regardless of bar visibility; only the BAR work is gated below. if (S.shown) { var view = buildView(); var sig = sigOf(view); var psig = ptrSigOf(view); var need = (sig !== S.lastSig) || (psig !== S.lastPtrSig); if (!need && view.playing && (time - S.lastDrawTime) > 500) need = true; // The cursor tracks the ray every frame; cap pointer-only redraws at ~30 Hz. if (need && sig === S.lastSig && (time - S.lastDrawTime) < MIN_UPLOAD_MS) need = false; if (S.forceRedraw) need = true; if (need) { drawIntoCanvas(view); S.lastSig = sig; S.lastPtrSig = psig; S.lastDrawTime = time; S.forceRedraw = false; var s = stats(); if (s) { s.ctlDraws = (s.ctlDraws || 0) + 1; s.ctlHover = view.hover || ''; } updateVisibilityStats(); } } // Beam placement follows the pointer even while the bar is hidden. if (S.beamOn) updateBeamTransform(); presentBeam(frame); if (S.shown) presentBar(frame); } catch (e) { setErr('hook: ' + ((e && e.message) || e)); } void session; } function create(session, glContextOrNull) { try { if (S.created && S.layer) return S.layer; if (!session) { log('controls.create: no session'); return null; } if (typeof XRWebGLBinding === 'undefined') { log('controls.create: no XRWebGLBinding'); setErr('no XRWebGLBinding'); return null; } var n = ns(); // ADDENDUM 5: the panel lives in LOCAL space, not viewer space. Gaze // selection needs the panel to hold a stable world pose - if it were // head-locked the view centre would always map to its centre and no // individual button could ever be targeted. 90-main.js drives the lazy // follow by writing layer.transform every frame. var space = (n && n.refSpace) || (n && n.state && n.state.refSpace); if (!space) { log('controls.create: no refSpace'); setErr('no refSpace'); return null; } var canvas = document.createElement('canvas'); canvas.width = PANEL_PX.expanded.w; canvas.height = PANEL_PX.expanded.h; var ctx2d = canvas.getContext('2d'); if (!ctx2d) { log('controls.create: no 2d context'); setErr('no 2d context'); return null; } var gl = glContextOrNull; if (!gl) { gl = (n && typeof n.xrGl === 'function') ? n.xrGl(true) : document.createElement('canvas').getContext('webgl2', { xrCompatible: true, alpha: true, premultipliedAlpha: true, antialias: false, depth: false }); } if (!gl) { log('controls.create: no webgl2'); setErr('no webgl2'); return null; } var prog = makeProgram(gl); bindGeometry(gl, prog); var tex = gl.createTexture(); gl.bindTexture(gl.TEXTURE_2D, tex); gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR); gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR); gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE); gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE); gl.pixelStorei(gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, true); gl.disable(gl.DEPTH_TEST); gl.disable(gl.BLEND); gl.uniform1i(gl.getUniformLocation(prog, 'uTex'), 0); var fbo = gl.createFramebuffer(); var binding = new XRWebGLBinding(session, gl); var opts = { space: space, layout: 'mono', alpha: true, width: PANEL_M.expanded.w, height: PANEL_M.expanded.h, viewPixelWidth: canvas.width, viewPixelHeight: canvas.height }; if (typeof XRRigidTransform !== 'undefined') { opts.transform = new XRRigidTransform({ x: 0, y: PANEL_Y, z: -PANEL_Z }); } var layer = binding.createQuadLayer(opts); if (!layer) { log('controls.create: createQuadLayer returned null'); setErr('createQuadLayer null'); return null; } S.canvas = canvas; S.ctx2d = ctx2d; S.gl = gl; S.prog = prog; S.tex = tex; S.fbo = fbo; S.binding = binding; S.layer = layer; S.created = true; S.lastSig = null; S.lastPtrSig = null; S.lastDrawTime = -1e9; S.forceRedraw = true; S.layerOpacityOk = probeLayerOpacity(layer); // --- pointer beam: a second, independent quad layer --------------- // Built once and reused; only its transform changes per frame. A runtime // without XRRigidTransform (or without a spare layer) just means no beam // guide - the bar keeps working. if (typeof XRRigidTransform !== 'undefined') { try { S.beamLayer = binding.createQuadLayer({ space: space, layout: 'mono', alpha: true, width: BEAM_L_DEFAULT, height: BEAM_H, viewPixelWidth: BEAM_TEX.w, viewPixelHeight: BEAM_TEX.h, transform: new XRRigidTransform({ x: 0, y: 0, z: -BEAM_L_DEFAULT * 0.5 }) }) || null; // Second ribbon, rolled 90 deg about the beam axis (see beamQuat). // A single flat ribbon is seen edge-on whenever the beam points away // from the viewer; a crossed pair guarantees that at least 0.707 of // the width faces the eye from any angle. S.beamLayer2 = binding.createQuadLayer({ space: space, layout: 'mono', alpha: true, width: BEAM_L_DEFAULT, height: BEAM_H, viewPixelWidth: BEAM_TEX.w, viewPixelHeight: BEAM_TEX.h, transform: new XRRigidTransform({ x: 0, y: 0, z: -BEAM_L_DEFAULT * 0.5 }) }) || null; S.beamTex = gl.createTexture(); gl.bindTexture(gl.TEXTURE_2D, S.beamTex); gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR); gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR); gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE); gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE); // Raw pixels are already premultiplied; do not premultiply twice. gl.pixelStorei(gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, false); gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, BEAM_TEX.w, BEAM_TEX.h, 0, gl.RGBA, gl.UNSIGNED_BYTE, buildBeamTexture(BEAM_TEX.w, BEAM_TEX.h)); gl.pixelStorei(gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, true); // Starts hidden like the bar (see applyBeamVisible). try { S.beamLayer.opacity = 0; } catch (e) { void e; } try { if (S.beamLayer2) S.beamLayer2.opacity = 0; } catch (e) { void e; } } catch (e) { log('controls: beam layer unavailable:', (e && e.message) || e); S.beamLayer = null; S.beamLayer2 = null; } } else { log('controls: no XRRigidTransform, pointer beam disabled'); } // Sync the freshly created (expanded-sized) layer to the current mode. applyModeToLayer(); // ADDENDUM 2: created hidden. Both layers stay allocated for the session; // show()/hide() toggle opacity + render-state membership only. applyBarVisible(); applyBeamVisible(); var hk = hook; S.hookFn = hk; if (n && typeof n.addFrameHook === 'function') n.addFrameHook(hk); setErr(null); log('controls layer created (hidden, ' + S.mode + ', beam=' + !!S.beamLayer + ')'); return layer; } catch (e) { setErr('create: ' + ((e && e.message) || e)); log('controls.create failed:', (e && e.message) || e); try { teardown(); } catch (e2) { void e2; } return null; } } function teardown() { var n = ns(); if (S.hookFn && n && typeof n.removeFrameHook === 'function') { try { n.removeFrameHook(S.hookFn); } catch (e) { void e; } } ['layer', 'beamLayer', 'beamLayer2'].forEach(function (key) { if (S[key] && n && typeof n.removeLayer === 'function') { try { n.removeLayer(S[key]); } catch (e) { void e; } } }); var gl = S.gl; if (gl) { try { if (S.tex) gl.deleteTexture(S.tex); if (S.beamTex) gl.deleteTexture(S.beamTex); if (S.fbo) gl.deleteFramebuffer(S.fbo); if (S.vbo) gl.deleteBuffer(S.vbo); if (S.prog) gl.deleteProgram(S.prog); } catch (e) { void e; } } S.canvas = null; S.ctx2d = null; S.gl = null; S.prog = null; S.tex = null; S.beamTex = null; S.fbo = null; S.vbo = null; S.binding = null; S.layer = null; S.beamLayer = null; S.beamLayer2 = null; S.hookFn = null; S.created = false; S.pointer = null; S.beamOn = false; S.beamSig = null; S.layerOpacityOk = false; // The session is gone: a later create() must start hidden again, at the // expanded size (the summon window is the full panel rect), and UNLOCKED - // 90-main.js also clears it, but the module must not depend on that. S.shown = false; S.mode = 'expanded'; S.lockOn = false; updateVisibilityStats(); } function destroy() { try { if (S.autoHideTimer != null) { clearTimeout(S.autoHideTimer); S.autoHideTimer = null; } } catch (e) { void e; } try { teardown(); } catch (e) { void e; } S.forceRedraw = true; S.lastSig = null; S.lastPtrSig = null; } /* ------------------------------------------------------------------ * * Public API: the INTERFACES.md surface, plus setReticle which is kept for * the pre-ADDENDUM-2 gaze-only input path (setPointer supersedes it). * ------------------------------------------------------------------ */ function setVideo(videoEl, getExtra) { S.video = videoEl || null; S.getExtra = getExtra || null; } function onAction(cb) { S.onActionCb = (typeof cb === 'function') ? cb : null; } // Visible + expanded, then auto-hide. ADDENDUM 2: this is the only way the // bar becomes visible - create() leaves it hidden. function show(autoHideMs) { // ADDENDUM 7b: which panel comes up is decided by the lock state. S.mode = S.lockOn ? 'locked' : 'expanded'; S.shown = true; applyModeToLayer(); applyBarVisible(); S.beamSig = null; // re-place the beam for this show applyBeamVisible(); if (S.beamOn) updateBeamTransform(); restartAutoHide(autoHideMs); updateVisibilityStats(); } function restartAutoHide(autoHideMs) { if (S.autoHideTimer != null) { clearTimeout(S.autoHideTimer); S.autoHideTimer = null; } if (S.hold) return; // a drag is in progress: stay up var n = ns(); var def = (n && n.state && typeof n.state.ctlAutoHideMs === 'number') ? n.state.ctlAutoHideMs : 4000; var ms = (typeof autoHideMs === 'number' && isFinite(autoHideMs) && autoHideMs > 0) ? autoHideMs : def; S.autoHideTimer = setTimeout(function () { S.autoHideTimer = null; hide(); }, ms); } // ADDENDUM 8: pause the auto-hide while a hold-style interaction (the picture // drag) is running. Without this the panel would disappear mid-drag, because // the timer that show() armed keeps running. function hold(on) { var v = !!on; if (v === S.hold) return; S.hold = v; if (v) { if (S.autoHideTimer != null) { clearTimeout(S.autoHideTimer); S.autoHideTimer = null; } } else if (S.shown) { restartAutoHide(); } var s = stats(); if (s) s.ctlHold = v; } // Fully hidden: no bar, no beam, no cursor, and no further drawing/uploading // until the next show(). The MODE is kept so the layer stays at the size it will // be shown at, which keeps the summon window (main.js ray-casts the layer) at // the full panel rect rather than a 5 cm strip. function hide() { if (S.autoHideTimer != null) { clearTimeout(S.autoHideTimer); S.autoHideTimer = null; } S.shown = false; S.hold = false; var hs = stats(); if (hs) hs.ctlHold = false; applyBarVisible(); applyBeamVisible(); updateVisibilityStats(); } function isVisible() { return S.shown; } // 0..1 dimmer for the bar, applied live to the XR layer (or to the canvas // alpha when the runtime has no layer.opacity). Never rebuilds the layer. function setOpacity(a) { var v = num(a); S.opacity = (v == null) ? 1 : clamp(v, 0, 1); if (S.layer && S.layerOpacityOk) { try { S.layer.opacity = S.shown ? S.opacity : 0; } catch (e) { S.layerOpacityOk = false; } } S.forceRedraw = true; updateVisibilityStats(); } function getOpacity() { return S.opacity; } /* ------------------------------------------------------------------ * * ADDENDUM 7b padlock state. * * Locking does NOT hide the panel here: 90-main.js decides that by calling * show() again, which re-picks the mode from S.lockOn and restarts the * auto-hide timer. So "lock, then the lock icon auto-hides like the panel" * falls out of the existing lifecycle instead of a second one. * ------------------------------------------------------------------ */ function setLock(on) { var v = !!on; if (v === S.lockOn) return; S.lockOn = v; if (S.shown) { S.mode = v ? 'locked' : 'expanded'; // swap the panel in place applyModeToLayer(); } var st = stats(); if (st) st.ctlLock = v; } function getLock() { return !!S.lockOn; } // Per-frame pointer from 90-main.js. Coordinates are in VIEWER space - the // same space as the head-locked bar and the beam quad - and are copied here // so the module never holds on to main's mutable object. function setPointer(p) { if (!p || typeof p !== 'object') { S.pointer = null; applyBeamVisible(); updateVisibilityStats(); return; } var origin = norm3(p.origin) ? { x: num(p.origin.x), y: num(p.origin.y), z: num(p.origin.z) } : null; var dir = norm3(p.dir); var hit = (p.hit && num(p.hit.nx) != null && num(p.hit.ny) != null) ? { nx: clamp(p.hit.nx, 0, 1), ny: clamp(p.hit.ny, 0, 1) } : null; S.pointer = { visible: !!p.visible && !!origin && !!dir, // `beam` MUST be carried through: the beam is only shown for an accurate // controller ray. Dropping the field made `beam !== false` always true, so // gaze aiming drew a degenerate beam from the eye itself. beam: p.beam !== false, source: (typeof p.source === 'string') ? p.source : '', origin: origin, dir: dir, hit: hit, distance: num(p.distance), pressed: !!p.pressed }; applyBeamVisible(); if (S.beamOn) updateBeamTransform(); updateVisibilityStats(); } function setReticle(nx, ny) { if (typeof nx === 'number' && typeof ny === 'number' && isFinite(nx) && isFinite(ny)) { S.reticle = { nx: clamp(nx, 0, 1), ny: clamp(ny, 0, 1) }; } else { S.reticle = null; } } function handleSelect(inputSource, frame) { // Unified pointer (ADDENDUM 2) wins when main set one; setReticle() stays as // the fallback for the older gaze-only path. With NEITHER there is nothing to // press: the old "assume the panel centre" fallback silently pressed whatever // button happened to live at (0.5, 0.5), which is a seek/skip in disguise. var pt = (S.pointer && S.pointer.hit) || S.reticle; if (!pt) { updateVisibilityStats(); return null; } var px = PANEL_PX[S.mode] || PANEL_PX.expanded; // The mode MUST be passed: the runtime does not tell us which panel we are // showing, and "collapsed" (a thin strip) shares no safe invariant with the // locked panel. var action = hitTest(pt.nx, pt.ny, px.w, px.h, S.mode); var s = stats(); if (s) s.ctlHover = (action ? (action.type === 'seek' ? 'track' : action.type) : ''); if (action) { if (s) s.ctlLastAction = action.type; if (S.onActionCb) { try { S.onActionCb(action); } catch (e) { setErr('onAction: ' + ((e && e.message) || e)); } } } updateVisibilityStats(); void inputSource; void frame; return action; } var Controls = { // pure / testable draw: draw, hitTest: hitTest, // lifecycle init: init, create: create, destroy: destroy, // state / input setVideo: setVideo, onAction: onAction, show: show, hide: hide, isVisible: isVisible, hold: hold, setOpacity: setOpacity, getOpacity: getOpacity, setLock: setLock, getLock: getLock, handleSelect: handleSelect, // input model (ADDENDUM 2): main pushes the winning ray every XR frame; // setReticle stays for the older gaze-only path. setPointer: setPointer, setReticle: setReticle, // accessor 90-main.js probes for its controller-ray -> panel math getLayer: function () { return S.layer; }, get layer() { return S.layer; }, // geometry constants, so main can pass the matching w/h to hitTest() PANEL_M: PANEL_M, PANEL_PX: PANEL_PX }; var n2 = ns(); n2.Controls = Controls; })(); /* ===== 30-hands.js ===== */ /* 30-hands.js — __JFVR.Hands * * Draws the official Meta generic-hand models (the @webxr-input-profiles GLBs) * so the user sees their real hands in VR, posed from the 25 WebXR joint poses * the runtime reports. This replaces the hand-rolled laser beam / cursor. * * Three.js draws the models, but it does NOT own the XR session. This page * already composes several layers (an XRMediaBinding equirect video layer plus * quad layers for subtitles and the control panel); calling * renderer.xr.setSession() would install a baseLayer, which is illegal next to * `layers`. So instead: * - we build our own xrCompatible WebGL2 context, * - create a projection layer with XRWebGLBinding and RETURN it, so * 90-main.js can put it in updateRenderState({ layers }), * - drive renderer.render() by hand from a __JFVR frame hook, binding the * layer's swapchain framebuffer and each view's viewport ourselves. * * Interface (INTERFACES.md): init() / create(session) / destroy() / isReady() / * getLayer(). Nothing here reads document, the network or WebXR at load time; * only init()/create() do. */ (function () { 'use strict'; var NS = (typeof window !== 'undefined' && window.__JFVR) ? window.__JFVR : null; if (!NS || NS.Hands) return; // core missing, or already installed /* ------------------------------------------------------------------ * * constants * ------------------------------------------------------------------ */ var CDN = 'https://cdn.jsdelivr.net/npm/'; var THREE_VERSION = 'three@0.160.0'; var THREE_URL = CDN + THREE_VERSION + '/build/three.module.js'; var ADDONS_URL = CDN + THREE_VERSION + '/examples/jsm/'; var IMPORT_MAP = { imports: { 'three': THREE_URL, 'three/addons/': ADDONS_URL } }; var HAND_URL = { left: CDN + '@webxr-input-profiles/assets@1.0/dist/profiles/generic-hand/left.glb', right: CDN + '@webxr-input-profiles/assets@1.0/dist/profiles/generic-hand/right.glb' }; // The 25 WebXR hand joints. The generic-hand GLB names its joint nodes after // these exact strings, so a normalised name lookup maps bone -> joint. var JOINT_NAMES = [ 'wrist', 'thumb-metacarpal', 'thumb-phalanx-proximal', 'thumb-phalanx-distal', 'thumb-tip', 'index-finger-metacarpal', 'index-finger-phalanx-proximal', 'index-finger-phalanx-intermediate', 'index-finger-phalanx-distal', 'index-finger-tip', 'middle-finger-metacarpal', 'middle-finger-phalanx-proximal', 'middle-finger-phalanx-intermediate', 'middle-finger-phalanx-distal', 'middle-finger-tip', 'ring-finger-metacarpal', 'ring-finger-phalanx-proximal', 'ring-finger-phalanx-intermediate', 'ring-finger-phalanx-distal', 'ring-finger-tip', 'pinky-finger-metacarpal', 'pinky-finger-phalanx-proximal', 'pinky-finger-phalanx-intermediate', 'pinky-finger-phalanx-distal', 'pinky-finger-tip' ]; /* ------------------------------------------------------------------ * * state * ------------------------------------------------------------------ */ var S = { // asset state: survives sessions, so a re-entry reuses the loaded GLBs inited: false, ready: false, THREE: null, models: { left: null, right: null }, // session state: torn down by destroy() session: null, gl: null, binding: null, projectionLayer: null, fbo: null, renderer: null, scene: null, camera: null, hookFn: null, frames: 0 }; /* ------------------------------------------------------------------ * * helpers * ------------------------------------------------------------------ */ function log() { try { if (NS && typeof NS.log === 'function') { NS.log.apply(NS, arguments); return; } } catch (e) { /* fall through */ } try { console.log.apply(console, ['[JFVR hands]'].concat(Array.prototype.slice.call(arguments))); } catch (e2) {} } function msg(e) { return (e && e.message) ? e.message : String(e); } function stats() { if (!NS.state) NS.state = {}; if (!NS.state.stats) NS.state.stats = {}; return NS.state.stats; } function setStat(k, v) { try { stats()[k] = v; } catch (e) {} } function setErr(text) { setStat('handsErr', text); } function refSpaceOf() { var proj = S.projectionLayer; if (proj && proj.space) return proj.space; // layers with a space if (NS.refSpace) return NS.refSpace; return (NS.state && NS.state.refSpace) || null; } function normalizeName(n) { return String(n == null ? '' : n).toLowerCase().replace(/_/g, '-'); } /* ------------------------------------------------------------------ * * asset loading (async, kicked off by init()) * ------------------------------------------------------------------ */ // 90-main.js has no import map, so Three's modules cannot resolve their bare // `three` import until we install one. Must happen before the first dynamic // import of a bare specifier, which is why init() does it up front. function ensureImportMap() { if (!document || !document.head) return; if (document.querySelector('script[type="importmap"]')) return; var im = document.createElement('script'); im.type = 'importmap'; im.textContent = JSON.stringify(IMPORT_MAP); document.head.appendChild(im); } function buildModel(gltf, handedness) { var root = gltf && (gltf.scene || (gltf.scenes && gltf.scenes[0])); if (!root) { log('hands[' + handedness + ']: GLB has no scene'); return null; } // The mesh is skinned: its bind-pose bounds are wrong once the joints move, // so frustum culling would make the hand pop in and out. Never cull it. root.traverse(function (o) { if (o.isMesh || o.isSkinnedMesh) o.frustumCulled = false; }); var byName = {}; root.traverse(function (o) { if (o.name) byName[normalizeName(o.name)] = o; }); var bones = {}; var missing = []; for (var i = 0; i < JOINT_NAMES.length; i++) { var joint = JOINT_NAMES[i]; var bone = byName[normalizeName(joint)]; if (bone) bones[joint] = bone; else missing.push(joint); } // Tolerate a renamed/partial skeleton: report once, pose what we can. if (missing.length) { log('hands[' + handedness + ']: no GLB bone for ' + missing.join(', ') + ' (joint skipped)'); } return { root: root, bones: bones, missing: missing }; } function loadModels(THREE, GLTFLoader) { var loader = new GLTFLoader(); function one(handedness) { return new Promise(function (resolve, reject) { loader.load(HAND_URL[handedness], function (gltf) { var model = buildModel(gltf, handedness); if (!model) { reject(new Error(handedness + ': empty scene')); return; } S.models[handedness] = model; if (S.scene) S.scene.add(model.root); // layer may already be rendering resolve(); }, undefined, function (e) { reject(new Error(handedness + ': ' + msg(e))); }); }); } return Promise.all([one('left'), one('right')]); } function load() { return import('three').then(function (THREE) { S.THREE = THREE; return import('three/addons/loaders/GLTFLoader.js').then(function (mod) { return loadModels(THREE, mod.GLTFLoader); }); }).then(function () { S.ready = true; setStat('handsReady', true); setErr(null); ensureRenderer(); // if create() already ran, start drawing the models log('hand models ready'); }).catch(function (e) { setErr('load: ' + msg(e)); log('hand model load failed:', msg(e)); }); } function init() { if (S.inited) return; S.inited = true; setStat('handsReady', false); setStat('handsErr', null); setStat('handsSources', 'none'); setStat('handsFrames', 0); try { ensureImportMap(); var p = load(); if (p && typeof p.catch === 'function') { p.catch(function (e) { setErr('init: ' + msg(e)); }); // belt and braces } } catch (e) { // Never propagate: a missing import map / document must not kill boot. setErr('init: ' + msg(e)); log('hands init failed:', msg(e)); } } /* ------------------------------------------------------------------ * * three.js + scene (built lazily, once THREE and a session are available) * ------------------------------------------------------------------ */ function makeGl() { if (!document || !document.createElement) return null; var canvas = document.createElement('canvas'); // A dedicated context: the other modules keep their own for the quad // layers, and the projection layer must render into its own swapchain. return canvas.getContext('webgl2', { xrCompatible: true, alpha: true, premultipliedAlpha: true, antialias: true, depth: true, stencil: false }) || null; } function ensureRenderer() { if (S.renderer || !S.gl || !S.THREE) return S.renderer; try { var THREE = S.THREE; var renderer = new THREE.WebGLRenderer({ canvas: S.gl.canvas, context: S.gl, alpha: true, antialias: true }); // NEVER renderer.xr.setSession(): that sets a baseLayer and would clash // with the page's `layers` array. We bind the projection framebuffer and // render manually from the frame hook instead. renderer.autoClear = true; renderer.setClearColor(0x000000, 0); // transparent: the video shows through var scene = new THREE.Scene(); // The generic-hand GLBs use PBR materials; without a light they render // black, so give them a hemisphere fill plus a key light. scene.add(new THREE.HemisphereLight(0xffffff, 0x445566, 2.5)); var key = new THREE.DirectionalLight(0xffffff, 1.4); key.position.set(0.3, 0.8, 0.5); scene.add(key); var camera = new THREE.PerspectiveCamera(); // The hook writes this camera's matrices straight from the XR view, so // stop three from recomputing matrixWorld from position/quaternion. camera.matrixAutoUpdate = false; camera.matrixWorldAutoUpdate = false; S.renderer = renderer; S.scene = scene; S.camera = camera; ['left', 'right'].forEach(function (h) { if (S.models[h]) scene.add(S.models[h].root); }); log('three renderer ready for hands'); } catch (e) { setErr('renderer: ' + msg(e)); log('renderer setup failed:', msg(e)); S.renderer = null; S.scene = null; S.camera = null; } return S.renderer; } /* ------------------------------------------------------------------ * * XR projection layer + manual render * ------------------------------------------------------------------ */ // Resolve the framebuffer + viewport to render `view` into. // // Two shapes are accepted, in order: // 1. a projection layer that exposes `framebuffer` + `getViewport(view)`; // 2. the WebXR Layers spec path, XRWebGLBinding.getViewSubImage(layer, view), // which returns this view's viewport plus the swapchain colour/depth // textures; we attach those to a framebuffer of our own. (The spec gives // XRProjectionLayer no framebuffer and no getViewport(), so on a real // runtime branch 2 is the one that runs.) function bindViewTarget(view) { var gl = S.gl; var proj = S.projectionLayer; var binding = S.binding; if (!gl || !proj) return null; if (proj.framebuffer && typeof proj.getViewport === 'function') { var direct = null; try { direct = proj.getViewport(view); } catch (e) { direct = null; } if (direct) { gl.bindFramebuffer(gl.FRAMEBUFFER, proj.framebuffer); return { framebuffer: proj.framebuffer, viewport: direct }; } } if (binding && typeof binding.getViewSubImage === 'function') { var sub = null; try { sub = binding.getViewSubImage(proj, view); } catch (e) { sub = null; } if (sub && sub.colorTexture) { if (!S.fbo) S.fbo = gl.createFramebuffer(); gl.bindFramebuffer(gl.FRAMEBUFFER, S.fbo); gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, sub.colorTexture, 0); if (sub.depthStencilTexture) { gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.DEPTH_ATTACHMENT, gl.TEXTURE_2D, sub.depthStencilTexture, 0); } if (sub.viewport) return { framebuffer: S.fbo, viewport: sub.viewport }; } } return null; } // Blank the layer while Three has not finished loading, so the compositor // does not show stale content. function clearLayer(pose) { var gl = S.gl; var target = bindViewTarget(pose.views[0]); if (!target) return; var vp = target.viewport; gl.viewport(Math.round(vp.x), Math.round(vp.y), Math.round(vp.width), Math.round(vp.height)); gl.clearColor(0, 0, 0, 0); gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT); } function renderFrame(frame) { var space = refSpaceOf(); if (!space) return; var pose = null; try { pose = frame.getViewerPose(space); } catch (e) { pose = null; } if (!pose || !pose.views || !pose.views.length) return; var renderer = S.renderer || ensureRenderer(); if (!renderer) { clearLayer(pose); return; } var gl = S.gl; var camera = S.camera; // The layer colour texture holds BOTH eyes in one framebuffer, so a bare // gl.clear() would erase the eye already drawn. autoClear stays on (it // clears to the transparent clear colour above) and the scissor box keeps // each clear + draw inside that eye's viewport. gl.enable(gl.SCISSOR_TEST); for (var i = 0; i < pose.views.length; i++) { var view = pose.views[i]; var target = bindViewTarget(view); if (!target) break; var vp = target.viewport; var x = Math.round(vp.x), y = Math.round(vp.y); var w = Math.round(vp.width), h = Math.round(vp.height); if (w <= 0 || h <= 0) continue; gl.viewport(x, y, w, h); gl.scissor(x, y, w, h); camera.projectionMatrix.fromArray(view.projectionMatrix); camera.projectionMatrixInverse.copy(camera.projectionMatrix).invert(); camera.matrixWorldInverse.fromArray(view.transform.inverse.matrix); camera.matrixWorld.copy(camera.matrixWorldInverse).invert(); renderer.render(S.scene, camera); } gl.disable(gl.SCISSOR_TEST); } /* ------------------------------------------------------------------ * * joint posing * ------------------------------------------------------------------ */ function poseHand(frame, handedness, hand, space) { var model = S.models[handedness]; if (!model || !model.bones || !space) return; for (var i = 0; i < JOINT_NAMES.length; i++) { var joint = JOINT_NAMES[i]; var bone = model.bones[joint]; if (!bone) continue; // unmatched bone: logged once at load var jointSpace = null; try { jointSpace = hand.get(joint); } catch (e) { jointSpace = null; } if (!jointSpace) continue; var jp = null; try { jp = frame.getJointPose(jointSpace, space); } catch (e) { jp = null; } if (!jp || !jp.transform) continue; // The generic-hand skeleton is FLAT: every joint node is a direct child // of the armature root, which sits at identity. A joint's pose in the // reference space is therefore also its local transform, so feeding the // matrix to the bone's TRS and letting three propagate is exact. The // model is metre-scaled (hand span ~0.2 m), so jp.radius is NOT applied: // the mesh already matches the joint scale. bone.matrix.fromArray(jp.transform.matrix); bone.matrix.decompose(bone.position, bone.quaternion, bone.scale); } } function poseHands(frame, session) { var left = false, right = false; var sources = (session && session.inputSources) || null; var space = refSpaceOf(); if (sources) { for (var i = 0; i < sources.length; i++) { var src = sources[i]; var hand = src && src.hand; if (!hand || hand.size !== 25) continue; // XRHand: exactly 25 joints var handedness = src.handedness; if (handedness !== 'left' && handedness !== 'right') continue; if (handedness === 'left') left = true; else right = true; poseHand(frame, handedness, hand, space); } } // Show a hand only while its input source is present. if (S.models.left && S.models.left.root) S.models.left.root.visible = left; if (S.models.right && S.models.right.root) S.models.right.root.visible = right; setStat('handsSources', (left || right) ? (left ? 'left' : '') + (left && right ? ',' : '') + (right ? 'right' : '') : 'none'); } /* ------------------------------------------------------------------ * * frame hook * ------------------------------------------------------------------ */ function hook(frame, time, session) { if (!S.projectionLayer || !frame) return; S.frames++; setStat('handsFrames', S.frames); poseHands(frame, session); renderFrame(frame); void time; } function registerHook() { if (S.hookFn) return; S.hookFn = hook; if (typeof NS.addFrameHook === 'function') NS.addFrameHook(hook); } /* ------------------------------------------------------------------ * * lifecycle * ------------------------------------------------------------------ */ function create(session) { try { if (S.projectionLayer && S.session === session) return S.projectionLayer; // idempotent if (!session) { log('hands.create: no session'); return null; } destroy(); // drop anything left from a previous session if (typeof XRWebGLBinding === 'undefined') { setErr('no XRWebGLBinding'); log('hands.create: XRWebGLBinding unavailable'); return null; } var gl = makeGl(); if (!gl) { setErr('no webgl2 context'); log('hands.create: no webgl2 context'); return null; } var binding = new XRWebGLBinding(session, gl); var proj = binding.createProjectionLayer({ textureType: 'texture', colorFormat: gl.RGBA8, depthFormat: gl.DEPTH_COMPONENT24, scaleFactor: 1, alpha: true }); if (!proj) { setErr('createProjectionLayer returned null'); log('hands.create: no projection layer'); return null; } S.session = session; S.gl = gl; S.binding = binding; S.projectionLayer = proj; S.fbo = null; S.frames = 0; ensureRenderer(); // no-op until THREE has loaded; the hook retries registerHook(); setErr(null); log('hands projection layer created (models ' + (S.ready ? 'ready' : 'loading') + ')'); return proj; } catch (e) { setErr('create: ' + msg(e)); log('hands.create failed:', msg(e)); try { destroy(); } catch (e2) { void e2; } return null; } } function destroy() { try { if (S.hookFn && typeof NS.removeFrameHook === 'function') NS.removeFrameHook(S.hookFn); } catch (e) { void e; } S.hookFn = null; try { if (S.projectionLayer && typeof NS.removeLayer === 'function') NS.removeLayer(S.projectionLayer); } catch (e) { void e; } try { if (S.gl && S.fbo) S.gl.deleteFramebuffer(S.fbo); } catch (e) { void e; } S.fbo = null; var renderer = S.renderer; S.renderer = null; if (renderer) { // Drop the context with the session: browsers cap live WebGL contexts, // and the next create() builds a bright new one. Only our own resources // are touched - the loaded models are kept for reuse. try { if (typeof renderer.forceContextLoss === 'function') renderer.forceContextLoss(); } catch (e) { void e; } try { renderer.dispose(); } catch (e) { void e; } } S.scene = null; S.camera = null; S.binding = null; S.projectionLayer = null; S.gl = null; S.session = null; S.frames = 0; setStat('handsFrames', 0); setStat('handsSources', 'none'); } function isReady() { // Models loaded AND a live projection layer + renderer: genuinely drawing. return !!(S.ready && S.renderer && S.projectionLayer); } function getLayer() { return S.projectionLayer || null; } /* ------------------------------------------------------------------ * * public API * ------------------------------------------------------------------ */ NS.Hands = { init: init, create: create, destroy: destroy, isReady: isReady, getLayer: getLayer }; })(); /* ===== 90-main.js ===== */ /*! * JFVR main — boot, DOM scan, in-page settings panel, XR session lifecycle, * input dispatch, wiring. * * Loaded last. Owns everything the modules do not. * See INTERFACES.md. */ (function () { 'use strict'; var NS = window.__JFVR; if (!NS) { console.error('[JFVR] core missing'); return; } if (NS.__main) return; NS.__main = true; var S = NS.state; var log = NS.log; /* ================================================================== * * rAF broker * ------------------------------------------------------------------ * While an immersive XR session runs, window.requestAnimationFrame is * COMPLETELY stopped (measured: 0 callbacks in 6s). Jellyfin renders libass * subtitles from an rAF loop, so that froze the subtitle canvas. * * The FIRST fix was to queue callbacks in XR and pump the queue from * video.requestVideoFrameCallback. That is not enough on Jellyfin 12.x: a * callback that is ALREADY PENDING in the NATIVE scheduler when the session * starts is destroyed with the native scheduler and never runs again, and * SubtitlesOctopus starts ONE long-lived rAF loop when the video loads whose * id is never reset. Verified on device: instance.rafId stuck on a native id * (1150), oneshotState.eventStart frozen ~7 min behind the playhead, zero * oneshot-render requests for the whole session -> the whole prerender chain * stalls and the HUD shows the last line drawn before VR started. * * So the queue is used ALWAYS, even outside XR, and drained by a native rAF * pump. Nothing is ever left pending natively, so nothing can be lost when the * session starts; startRafPump just swaps the driver. In XR the native pump * stops and video.requestVideoFrameCallback drives the queue (it still fires, * in step with the video); a slow timer also drives it when the video is not * presenting frames (paused / stalled / ended), so a paused seek or a track * switch still redraws instead of leaving a stale line on screen. * ================================================================== */ var RAFQ = { inXR: false, native: (window.requestAnimationFrame || function () {}).bind(window), nativeCancel: (window.cancelAnimationFrame || function () {}).bind(window), queue: new Map(), nextId: 1000000000, pumped: 0, nativeId: null, lastPump: 0, err: null }; function drainRaf(t) { if (!RAFQ.queue.size) return; var cbs = []; RAFQ.queue.forEach(function (cb) { cbs.push(cb); }); RAFQ.queue.clear(); RAFQ.pumped++; for (var i = 0; i < cbs.length; i++) { try { cbs[i](t); } catch (e) { if (!RAFQ.err) RAFQ.err = NS.recordError('raf cb', e); } } } // Native driver (outside XR). Exactly ONE native callback is kept in flight so // that a rAF which re-schedules itself cannot multiply the driver (an earlier // version nulled nativeId before draining, re-armed on every self-rescheduling // callback AND again at the end -> exponential native callbacks -> frozen page). function nativeRafStep(t) { if (RAFQ.inXR) { RAFQ.nativeId = null; return; } // the XR driver owns the queue RAFQ.nativeId = RAFQ.native(nativeRafStep); // re-arm BEFORE draining drainRaf(t); if (!RAFQ.queue.size && !RAFQ.inXR) { // nothing left -> stop try { RAFQ.nativeCancel(RAFQ.nativeId); } catch (e) {} RAFQ.nativeId = null; } } function ensureNativeRaf() { if (RAFQ.inXR || RAFQ.nativeId != null) return; RAFQ.nativeId = RAFQ.native(nativeRafStep); } try { window.requestAnimationFrame = function (cb) { if (typeof cb !== 'function') return 0; var id = RAFQ.nextId++; RAFQ.queue.set(id, cb); if (!RAFQ.inXR) ensureNativeRaf(); return id; }; window.cancelAnimationFrame = function (id) { if (RAFQ.queue.has(id)) { RAFQ.queue.delete(id); return; } return RAFQ.nativeCancel(id); }; } catch (e) { log('rAF broker install failed', e); } function startRafPump(video) { // NOTE: do NOT clear the queue. The callback that a naive broker loses at // session start is sitting in it, and it is the one that keeps Jellyfin's // subtitle loop alive. The pending NATIVE driver id is dropped: that callback // is lost with the native scheduler anyway, and clearing the id lets the // driver restart cleanly after the session ends. RAFQ.nativeId = null; RAFQ.inXR = true; RAFQ.pumped = 0; RAFQ.lastPump = 0; RAFQ.err = null; var stopped = false; S.stats.rafPump = video && video.requestVideoFrameCallback ? 'videoFrameCallback' : 'interval'; if (video && video.requestVideoFrameCallback) { var step = function (now) { if (stopped) return; RAFQ.lastPump = performance.now(); drainRaf(now); video.requestVideoFrameCallback(step); }; video.requestVideoFrameCallback(step); } // Idle fallback: rVFC stops while the video is paused / stalled / ended. var id = setInterval(function () { if (stopped || !RAFQ.inXR) return; if (performance.now() - RAFQ.lastPump > 60) drainRaf(performance.now()); }, 50); RAFQ.stop = function () { stopped = true; clearInterval(id); }; } function stopRafPump() { try { if (RAFQ.stop) RAFQ.stop(); } catch (e) {} RAFQ.stop = null; RAFQ.inXR = false; RAFQ.lastPump = 0; // hand the queue back to the native driver so nothing is stranded ensureNativeRaf(); } /* ================================================================== * * settings persistence (localStorage, per browser) * ================================================================== */ var STORE_KEY = 'jfvr.settings.v1'; var saveTimer = null; function num(v, lo, hi, dflt) { if (typeof v !== 'number' || !isFinite(v)) return dflt; return NS.clamp(v, lo, hi); } function loadSettings() { var raw; try { raw = localStorage.getItem(STORE_KEY); } catch (e) { return; } if (!raw) return; var o; try { o = JSON.parse(raw); } catch (e) { return; } // corrupt -> defaults if (!o || typeof o !== 'object') return; try { if (o.subPlacement && typeof o.subPlacement === 'object') { S.subPlacement = { y: num(o.subPlacement.y, -0.95, -0.05, S.subPlacement.y), z: num(o.subPlacement.z, -2.6, -0.35, S.subPlacement.z), width: num(o.subPlacement.width, 0.2, 4.5, S.subPlacement.width) }; } // ADDENDUM 6: depth/size decoupling. When locked, the stored width is // re-derived from subAngleDeg + z by 10-subtitles.js, so this pair is what // actually restores the user's look. if (typeof o.subLockAngle === 'boolean') S.subLockAngle = o.subLockAngle; if (typeof o.subAngleDeg === 'number') S.subAngleDeg = num(o.subAngleDeg, 20, 120, S.subAngleDeg); S.subOpacity = num(o.subOpacity, 0.1, 1.0, S.subOpacity); S.ctlAutoHideMs = num(o.ctlAutoHideMs, 1000, 10000, S.ctlAutoHideMs); if (o.subMode === 'hud' || o.subMode === 'sphere') S.subMode = o.subMode; // subLayout is deliberately NOT restored: a persisted manual override // silently ruined the default dual-eye content once already (forced 'mono' // made the HUD show both eyes side by side). Layout always starts on 'auto'. // ADDENDUM 8: how the picture aim is applied. 'space' is the live path; the // 'rebuild' escape hatch exists because mutating a media layer's transform is // ignored by this runtime, so if an offset reference space turns out not to be // honoured either, this is how we get movement back without a redeploy. if (o.videoRotMode === 'auto' || o.videoRotMode === 'space' || o.videoRotMode === 'rebuild') { S.videoRotMode = o.videoRotMode; } // 'off' is deliberately NOT restored. Persisting it had the same effect as // persisting subtitles:false - the subtitle layer was never created on the // next session, which looked like a loading failure. Same for subAlpha, // which is harmless but pointless to restore when subtitles start ON. if (o.subAlpha === 'premultiplied' || o.subAlpha === 'straight') S.subAlpha = o.subAlpha; if (o.projection === '180' || o.projection === '360') S.projection = o.projection; if (o.stereo === 'mono' || o.stereo === 'sbs' || o.stereo === 'ou') S.stereo = o.stereo; if (typeof o.controls === 'boolean') S.controls = o.controls; // NOTE: `subtitles` is deliberately NOT restored. Persisting it meant a // session where the user toggled subtitles off came back with them off, // the subtitle layer never being created - which looked like a loading // failure. Subtitles always start ON; the toggle is per-session only. log('settings restored', JSON.stringify(S.subPlacement), 'opacity', S.subOpacity); } catch (e) { /* keep defaults */ } } function saveSettings() { if (saveTimer != null) clearTimeout(saveTimer); saveTimer = setTimeout(function () { saveTimer = null; try { localStorage.setItem(STORE_KEY, JSON.stringify({ subPlacement: S.subPlacement, subLockAngle: S.subLockAngle, subAngleDeg: S.subAngleDeg, subOpacity: S.subOpacity, subMode: S.subMode, subAlpha: S.subAlpha, projection: S.projection, stereo: S.stereo, videoRotMode: S.videoRotMode, controls: S.controls, ctlAutoHideMs: S.ctlAutoHideMs })); S.stats.saved = 'ok'; } catch (e) { S.stats.saved = 'err:' + ((e && e.message) || e); } }, 400); } /* ================================================================== * * video discovery * ================================================================== */ function findVideo() { var list = Array.prototype.slice.call(document.querySelectorAll('video')); var playing = null, any = null; for (var i = 0; i < list.length; i++) { var v = list[i]; if (!v.videoWidth) continue; if (!any) any = v; if (!v.paused && v.readyState >= 2 && !playing) playing = v; } return playing || any || null; } /* ================================================================== * * format detection * ================================================================== */ function detectFromName(name) { if (!name) return null; var n = name.toLowerCase(); var is360 = /(^|[^0-9])360([^0-9]|$)/.test(n) || /\bvr360\b/.test(n); var is180 = /(^|[^0-9])180([^0-9]|$)/.test(n) || /\bvr180\b/.test(n); var sbs = /(^|[^a-z])(sbs|lr|3dh)([^a-z]|$)/.test(n); var ou = /(^|[^a-z])(ou|tb|3dv)([^a-z]|$)/.test(n); if (!is180 && !is360 && !sbs && !ou) return null; return { projection: is360 ? '360' : '180', stereo: ou ? 'ou' : (sbs || is180 || is360) ? 'sbs' : 'mono' }; } function detectFromShape(w, h) { if (!w || !h) return null; var r = w / h; if (r >= 3.6) return { projection: '360', stereo: 'sbs' }; if (r >= 1.8 && r <= 2.2) return { projection: '180', stereo: 'sbs' }; if (r >= 1.15 && r <= 1.45) return { projection: '180', stereo: 'mono' }; if (r <= 0.6) return { projection: '180', stereo: 'ou' }; return { projection: '180', stereo: 'sbs' }; } function currentItemId() { var m = /[?&]id=([a-f0-9]{32})/i.exec(location.hash || ''); return m ? m[1] : null; } function tryFetchFileName() { var id = currentItemId(); if (!id || typeof ApiClient === 'undefined' || !ApiClient.getItem) return Promise.resolve(''); var uid; try { uid = ApiClient.getCurrentUserId(); } catch (e) { uid = null; } if (!uid) return Promise.resolve(''); return ApiClient.getItem(uid, id) .then(function (item) { return (item && (item.Path || item.Name)) || ''; }) .catch(function () { return ''; }); } function applyAutoDetect() { return tryFetchFileName().then(function (name) { if (name) { S.fileName = name.split(/[\\/]/).pop(); var d = detectFromName(S.fileName); if (d) { S.projection = d.projection; S.stereo = d.stereo; S.detected = 'filename: ' + S.fileName; return; } } var v = S.video; var d2 = v && v.videoWidth ? detectFromShape(v.videoWidth, v.videoHeight) : null; if (d2) { S.projection = d2.projection; S.stereo = d2.stereo; S.detected = 'resolution: ' + v.videoWidth + 'x' + v.videoHeight; } }).catch(function () {}); } /* ================================================================== * * in-page UI * ================================================================== */ var CSS = [ // Sits ABOVE Jellyfin's own OSD bar: at the viewport's bottom-right it // covered the player's fullscreen button (owner report). 120 px clears the // OSD bottom bar; the settings panel stacks above it. '.jfvr-btn{position:fixed;right:16px;bottom:120px;z-index:2147483000;display:flex;align-items:center;gap:8px;', 'padding:10px 16px;border:0;border-radius:24px;background:linear-gradient(135deg,#4ea1ff,#7b5cff);color:#fff;', 'font-size:15px;font-weight:600;cursor:pointer;box-shadow:0 6px 22px rgba(0,0,0,.45);font-family:inherit}', '.jfvr-btn:active{transform:scale(.96)}', // the panel must always fit the viewport AND keep the CTA reachable, so the // body scrolls and the button lives in a sticky footer '.jfvr-panel{position:fixed;right:16px;bottom:180px;z-index:2147483001;width:290px;background:rgba(20,20,24,.96);', 'color:#eaeaea;border:1px solid rgba(255,255,255,.14);border-radius:14px;box-shadow:0 12px 40px rgba(0,0,0,.6);', 'font-family:inherit;font-size:14px;backdrop-filter:blur(10px);display:flex;flex-direction:column;', 'max-height:calc(100vh - 200px);overflow:hidden}', '.jfvr-head{display:flex;justify-content:space-between;align-items:center;padding:10px 12px;background:rgba(255,255,255,.06);font-weight:600;flex:0 0 auto}', '.jfvr-x{background:none;border:0;color:#aaa;font-size:20px;line-height:1;cursor:pointer;padding:0 4px}', '.jfvr-body{padding:12px;overflow-y:auto;flex:1 1 auto;min-height:0}', '.jfvr-foot{flex:0 0 auto;padding:10px 12px 12px;background:rgba(16,16,20,.98);border-top:1px solid rgba(255,255,255,.1)}', '.jfvr-row{display:flex;align-items:center;justify-content:space-between;margin-bottom:9px;gap:8px}', '.jfvr-lbl{color:#9aa0aa;font-size:13px;white-space:nowrap}', '.jfvr-seg{display:flex;border:1px solid rgba(255,255,255,.16);border-radius:8px;overflow:hidden;flex-shrink:0}', '.jfvr-seg button{background:none;border:0;color:#bbb;padding:6px 9px;font-size:12px;cursor:pointer;font-family:inherit}', '.jfvr-seg button+button{border-left:1px solid rgba(255,255,255,.16)}', '.jfvr-seg button.on{background:#4ea1ff;color:#fff;font-weight:600}', '.jfvr-seg button:disabled{color:#555;cursor:default}', '.jfvr-hint{font-size:12px;color:#8b93a1;margin:2px 0 10px;line-height:1.5;word-break:break-all}', '.jfvr-go{width:100%;padding:10px;border:0;border-radius:9px;background:#4ea1ff;color:#fff;font-size:15px;', 'font-weight:600;cursor:pointer;font-family:inherit}', '.jfvr-go:disabled{background:#3a3f47;color:#888;cursor:default}', '.jfvr-diag{margin-top:10px;font-size:11px;color:#8b93a1}', '.jfvr-diag summary{cursor:pointer;user-select:none}', '.jfvr-diag pre{white-space:pre-wrap;word-break:break-all;margin:8px 0 0;font-size:10px;line-height:1.45;', 'max-height:230px;overflow:auto;color:#9fb0c4;background:rgba(0,0,0,.35);padding:8px;border-radius:7px}', '.jfvr-msg{margin-top:8px;font-size:12px;color:#ffb3b3;line-height:1.55;white-space:pre-line;word-break:break-all}', '.jfvr-sec{margin:12px 0 6px;font-size:11px;letter-spacing:.08em;text-transform:uppercase;color:#6d7684;', 'border-top:1px solid rgba(255,255,255,.09);padding-top:9px}', '.jfvr-sl{margin-bottom:7px}', '.jfvr-sl .jfvr-lbl{display:flex;justify-content:space-between;font-size:12px;margin-bottom:2px}', '.jfvr-sl input[type=range]{width:100%;accent-color:#4ea1ff;height:18px;cursor:pointer}' ].join(''); function injectCss() { if (document.getElementById('jfvr-css')) return; var s = NS.makeEl('style'); s.id = 'jfvr-css'; s.textContent = CSS; document.head.appendChild(s); } var btn = null, panel = null, diagTimer = null; // Every sliderRow registers a re-read here, so a value changed from anywhere // (VR control bar, grab & drag, the lock toggle) is reflected in the open panel. var panelSync = null; var panelSyncs = []; function ensureButton() { if (btn && document.body.contains(btn)) return; injectCss(); var b = NS.makeEl('button', 'jfvr-btn'); b.type = 'button'; b.innerHTML = '🎥VR 播放'; b.addEventListener('click', function (ev) { ev.stopPropagation(); togglePanel(); }); document.body.appendChild(b); btn = b; } function removeButton() { if (btn) { btn.remove(); btn = null; } hidePanel(); } function segRow(label, getValue, setValue, options, disabledFn) { var row = NS.makeEl('div', 'jfvr-row'); row.appendChild(NS.makeEl('span', 'jfvr-lbl', label)); var seg = NS.makeEl('div', 'jfvr-seg'); options.forEach(function (o) { var b = NS.makeEl('button', null, o.label); b.type = 'button'; if (getValue() === o.value) b.classList.add('on'); if (disabledFn && disabledFn(o.value)) { b.disabled = true; } b.addEventListener('click', function (ev) { ev.stopPropagation(); setValue(o.value); Array.prototype.forEach.call(seg.children, function (c) { c.classList.remove('on'); }); b.classList.add('on'); refreshHint(); }); seg.appendChild(b); }); row.appendChild(seg); return row; } function sliderRow(label, min, max, step, fmt, getValue, setValue) { var wrap = NS.makeEl('div', 'jfvr-sl'); var lab = NS.makeEl('div', 'jfvr-lbl'); var name = NS.makeEl('span', null, label); var val = NS.makeEl('span', null, fmt(getValue())); lab.appendChild(name); lab.appendChild(val); wrap.appendChild(lab); var inp = NS.makeEl('input'); inp.type = 'range'; inp.min = min; inp.max = max; inp.step = step; inp.value = getValue(); inp.addEventListener('input', function (ev) { ev.stopPropagation(); var v = parseFloat(inp.value); setValue(v); val.textContent = fmt(v); }); wrap.appendChild(inp); panelSyncs.push(function () { var v = getValue(); if (document.activeElement !== inp) inp.value = v; val.textContent = fmt(v); }); return wrap; } function togglePanel() { if (panel) { hidePanel(); return; } injectCss(); panelSyncs = []; // Subtitles must be prepared BEFORE entering VR: libass does not recompute // its backing store while an immersive session is running. if (S.subtitles && NS.Subtitles) { try { NS.Subtitles.prepare(); } catch (e) { NS.recordError('prepare', e); } } panel = NS.makeEl('div', 'jfvr-panel'); panel.addEventListener('click', function (ev) { ev.stopPropagation(); }); var head = NS.makeEl('div', 'jfvr-head'); head.appendChild(NS.makeEl('span', null, 'VR 播放')); var x = NS.makeEl('button', 'jfvr-x', '\u00d7'); x.type = 'button'; x.addEventListener('click', function (ev) { ev.stopPropagation(); hidePanel(); }); head.appendChild(x); panel.appendChild(head); var body = NS.makeEl('div', 'jfvr-body'); if (!window.isSecureContext) { body.appendChild(NS.makeEl('div', 'jfvr-msg', '\u26a0 当前不是安全上下文(' + location.origin + '),点了也进不去 VR。\n' + '先在 Quest 浏览器 chrome://flags 里启用 unsafely-treat-insecure-origin-as-secure,' + '填入 http://192.168.123.199:8096,Relaunch 后重开本页。')); } body.appendChild(NS.makeEl('div', 'jfvr-sec', '画面')); body.appendChild(segRow('投影', function () { return S.projection; }, function (v) { S.projection = v; S.detected = 'manual'; saveSettings(); }, [{ label: '180\u00b0', value: '180' }, { label: '360\u00b0', value: '360' }])); body.appendChild(segRow('立体', function () { return S.stereo; }, function (v) { S.stereo = v; S.detected = 'manual'; saveSettings(); }, [{ label: '单目', value: 'mono' }, { label: '左右', value: 'sbs' }, { label: '上下', value: 'ou' }])); // ADDENDUM 7: the ONLY subtitle setting left before entering VR. Everything // else (mode / alpha / opacity / position / depth / size / lock) is set IN // VR, where the user can actually judge it - slider values chosen on a flat // screen do not transfer to the headset. 视角宽度 keeps its default; the // in-VR "小/大" buttons and the subtitle-row caption are the controls. body.appendChild(NS.makeEl('div', 'jfvr-sec', '字幕')); body.appendChild(segRow('显示', function () { return S.subtitles ? 'on' : 'off'; }, function (v) { S.subtitles = v === 'on'; saveSettings(); }, [{ label: '开', value: 'on' }, { label: '关', value: 'off' }])); // ADDENDUM 9: dual-eye ASS vs a normal (mono) subtitle. 'auto' detects from // the libass canvas; force it only if the detection gets it wrong. body.appendChild(segRow('字幕布局', function () { return S.subLayout || 'auto'; }, function (v) { S.subLayout = v; if (NS.Subtitles && NS.Subtitles.setLayout) { try { NS.Subtitles.setLayout(v); } catch (e) {} } saveSettings(); }, [{ label: '自动', value: 'auto' }, { label: '双目', value: 'dual' }, { label: '普通', value: 'mono' }])); body.appendChild(NS.makeEl('div', 'jfvr-hint', '字幕的位置 / 距离 / 大小 / 不透明度都在 VR 里用控制条调(控制条最下面一行),' + '调好会自动记住。VR 空间右侧有个锁图标:锁上后面板不会再被唤出。')); // ADDENDUM 8: the picture-pan mechanism. Only touch this if panning does not // move the picture live (see stats.videoRotMode). body.appendChild(segRow('画面旋转', function () { return S.videoRotMode || 'auto'; }, function (v) { S.videoRotMode = v; if (S.session) { try { videoSpaceCache = null; S.videoTransformLive = undefined; applyVideoAngle(true); } catch (e) {} } saveSettings(); }, [{ label: '自动', value: 'auto' }, { label: '兼容', value: 'rebuild' }])); body.appendChild(NS.makeEl('div', 'jfvr-sec', '控制')); body.appendChild(segRow('VR 内控制条', function () { return S.controls ? 'on' : 'off'; }, function (v) { S.controls = v === 'on'; saveSettings(); }, [{ label: '开', value: 'on' }, { label: '关', value: 'off' }])); body.appendChild(sliderRow('自动收起', 1000, 10000, 500, function (v) { return (v / 1000).toFixed(1) + ' s'; }, function () { return S.ctlAutoHideMs; }, function (v) { S.ctlAutoHideMs = v; saveSettings(); })); var hint = NS.makeEl('div', 'jfvr-hint'); hint.id = 'jfvr-hint'; body.appendChild(hint); var det = document.createElement('details'); det.className = 'jfvr-diag'; var sum = document.createElement('summary'); sum.textContent = '诊断信息(可复制发我)'; det.appendChild(sum); var pre = document.createElement('pre'); pre.id = 'jfvr-diag'; det.appendChild(pre); body.appendChild(det); panel.appendChild(body); // The status line and the CTA live in a sticky footer so neither can scroll // off-screen - the CTA did before, once the settings grew past a short viewport. var foot = NS.makeEl('div', 'jfvr-foot'); var msg = NS.makeEl('div', 'jfvr-msg'); msg.id = 'jfvr-msg'; foot.appendChild(msg); var go = NS.makeEl('button', 'jfvr-go', '进入 VR'); go.type = 'button'; go.id = 'jfvr-go'; go.addEventListener('click', function (ev) { ev.stopPropagation(); if (S.subtitles && NS.Subtitles) { try { NS.Subtitles.prepare(); } catch (e) {} } go.disabled = true; go.textContent = '进入中\u2026'; enterVR().catch(function (e) { log('enterVR threw', e); }).then(function () { go.disabled = false; go.textContent = '进入 VR'; }); }); foot.appendChild(go); panel.appendChild(foot); document.body.appendChild(panel); panelSync = function () { for (var i = 0; i < panelSyncs.length; i++) { try { panelSyncs[i](); } catch (e) {} } }; refreshHint(); updateDiag(); if (!diagTimer) { diagTimer = setInterval(function () { if (panel) updateDiag(); else { clearInterval(diagTimer); diagTimer = null; } }, 1000); } } function hidePanel() { if (panel) { panel.remove(); panel = null; } panelSync = null; panelSyncs = []; // Only shrink the subtitle canvas back when we are not in / about to be in VR. if (!S.session && NS.Subtitles) { try { NS.Subtitles.restore(); } catch (e) {} } } function refreshHint() { var h = document.getElementById('jfvr-hint'); if (!h) return; var v = S.video; var shape = v && v.videoWidth ? (v.videoWidth + '\u00d7' + v.videoHeight) : '未知'; var txt = '画面 ' + shape + ' · ' + (S.detected || '自动'); var proj = S.projection === '360' ? 'VR360' : 'VR180'; var st = S.stereo === 'sbs' ? '左右' : S.stereo === 'ou' ? '上下' : '单目'; txt += ' → ' + proj + ' ' + st; var cs = NS.Subtitles && NS.Subtitles.canvasSize ? NS.Subtitles.canvasSize() : null; if (cs) txt += ' · 字幕画布 ' + cs.w + '\u00d7' + cs.h; h.textContent = txt; } function updateDiag() { var pre = document.getElementById('jfvr-diag'); if (!pre) return; var v = S.video; var video = v ? [ 'size : ' + v.videoWidth + ' x ' + v.videoHeight, 'readyState : ' + v.readyState + ' paused: ' + v.paused, 't / dur : ' + v.currentTime.toFixed(1) + ' / ' + (isFinite(v.duration) ? v.duration.toFixed(1) : 'n/a'), 'src : ' + (v.currentSrc || '').slice(0, 100) ].join('\n') : 'no