The canPlayType override is now OFF by default: Jellyfin builds its own device profile and the plugin no longer influences the direct-play / transcode decision (opt back in with localStorage['jfvr-cpt']='on'). The decodingInfo probe still runs for the diagnostics panel only. Keep the bitrate prompt, and make it actually reachable: the old "has picture" check (videoWidth > 0 && readyState >= 2) is already true for a decoder that parsed the header and then stalled, so a frozen 8K HEVC Main 10 playback stood the watchdog down forever. Track currentTime instead and prompt after STALL_MS (20s) when the playhead stops moving.
7224 lines
281 KiB
JavaScript
7224 lines
281 KiB
JavaScript
/*!
|
||
* 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, 40-watchdog.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-11'; // hud-11 = ADDENDUM 12 (single pointer owner: dominant hand by default, press takes over, hold exclusion) + 11/10/9
|
||
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,
|
||
handPref: 'right', // ADDENDUM 12: dominant hand = the pointer's default owner
|
||
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,
|
||
|
||
/* MediaCapabilities verdicts, keyed '<profile_idc>:<level>'. See below. */
|
||
codec: {},
|
||
codecBlocked: [],
|
||
codecKept: [],
|
||
cptPatched: false,
|
||
cptMode: null
|
||
};
|
||
|
||
/* ------------------------------------------------------------------ *
|
||
* codec capability: what this browser will ACTUALLY decode
|
||
* ------------------------------------------------------------------
|
||
* Two layers.
|
||
*
|
||
* 1. NS.caps.hevc -- raw canPlayType() answers, for the diagnostics panel.
|
||
* This used to only ask about Main profile (8-bit), which is why the
|
||
* project's notes said "8K HEVC: supported" right up until a Main 10
|
||
* (10-bit) file showed up and could not be decoded at all. The Main 10
|
||
* strings are the ones that actually matter for 8K VR here.
|
||
*
|
||
* 2. NS.caps.codec + the canPlayType override -- makes Jellyfin Web build
|
||
* an ACCURATE device profile.
|
||
*
|
||
* Why the override: Jellyfin Web derives its entire HEVC claim from
|
||
* canPlayType (src/scripts/browserDeviceProfile.js):
|
||
*
|
||
* let maxHevcLevel = 120, hevcProfiles = 'main';
|
||
* ...
|
||
* if (canPlayType('video/mp4; codecs="hvc1.2.4.L183"')) {
|
||
* maxHevcLevel = 183; // level 6.1 = 8K
|
||
* hevcProfiles = 'main|main 10';
|
||
* }
|
||
*
|
||
* canPlayType answers "does this system have an HEVC decoder", not "can it
|
||
* do 8192x4096 Main 10 at 60 fps". So a desktop browser that merely ships an
|
||
* HEVC decoder claims 8K Main 10, the server picks DirectPlay, and playback
|
||
* dies on a black screen with no player UI (the OSD is only built after
|
||
* 'playbackstart'). decodingInfo asks the actual decoder, so feeding its
|
||
* verdict back through canPlayType lets Jellyfin's own logic land on the
|
||
* right profile: the desktop drops to whatever it can really do and the
|
||
* server transcodes, while a Quest that genuinely decodes 8K Main 10 keeps
|
||
* DirectPlay. Meta's browser-video docs recommend exactly this API over UA
|
||
* sniffing.
|
||
*
|
||
* A tier is blocked when:
|
||
*
|
||
* supported === false the decoder cannot handle the format at all
|
||
* smooth === false it can, but not in real time at that resolution and
|
||
* frame rate -- the "decoder exists, 8K does not work"
|
||
* case
|
||
*
|
||
* AND, for a Main 10 tier, only when the SAME LEVEL IS ALSO UNUSABLE IN MAIN
|
||
* (8-bit). That second condition is not cosmetic; it is forced by a real
|
||
* limitation in Jellyfin's device profile.
|
||
*
|
||
* browserDeviceProfile.js derives the level ceiling and the profile list from
|
||
* ONE chain of Main 10 probes -- the only Main (8-bit) probes are L4.0 and
|
||
* L4.1. So the reachable states are only:
|
||
*
|
||
* (120, main) (123, main) (123, main|main 10) (153, main|main 10)
|
||
* (183, main|main 10) (186, main|main 10)
|
||
*
|
||
* "Main up to 8K, Main 10 only up to 4K" is NOT expressible. So blocking
|
||
* 2:183 on a device that decodes 8-bit 8K but not 10-bit 8K does not just
|
||
* spare the 10-bit file -- it drags every 8-bit 8K file down to level 153 and
|
||
* transcodes them too. On a server that cannot transcode 8K in real time
|
||
* (measured: 8192x4096 -> 1280x640), that trades one broken file for sixty.
|
||
*
|
||
* Verified on the machine this was written for: NVIDIA VAAPI decodes
|
||
* 8192x4096 Main 8-bit fine and fails to create a decoder for 8192x4096
|
||
* Main 10 (nvidia-vaapi-driver passes the surface bit depth straight to
|
||
* cuvidCreateDecoder). Its whole library is 8-bit 8K, so lowering the ceiling
|
||
* would have broken all of it.
|
||
*
|
||
* The cost of this rule: a device that decodes 8-bit at a level but not
|
||
* 10-bit will still claim 10-bit at that level and can black-screen on such a
|
||
* file. 40-watchdog.js covers exactly that, which is why it stays.
|
||
*
|
||
* THE HOOK IS OFF BY DEFAULT. The plugin no longer influences Jellyfin's
|
||
* direct-play / transcode decision at all -- Jellyfin uses its native device
|
||
* profile, and when playback cannot keep up the 40-watchdog.js prompt lets the
|
||
* user pick a bitrate. The override is kept only as an opt-in experiment:
|
||
* localStorage['jfvr-cpt'] = 'on' enable the canPlayType hook
|
||
* localStorage['jfvr-cpt-keep'] = '2:183' never downgrade these tiers
|
||
*/
|
||
|
||
// decodingInfo needs real dimensions; a level string does not carry them, so
|
||
// each level is probed at the resolution and bitrate it exists for. Main
|
||
// (8-bit) is probed at the same levels as Main 10 because it is the control
|
||
// that decides whether a level is really out of reach.
|
||
//
|
||
// NOTE on the codec strings: they MUST carry a constraint byte. Chrome's
|
||
// MediaCapabilities.decodingInfo does not recognise the bare 'hvc1.1.L120'
|
||
// spelling and answers supported:false for it even on a machine that decodes
|
||
// HEVC perfectly, while 'hvc1.1.0.L120' is answered correctly. That one
|
||
// character used to matter far more than it looks: browserDeviceProfile.js
|
||
// gates the WHOLE hevc entry on supportsHevc(), which is exactly
|
||
//
|
||
// canPlayType('...codecs="hvc1.1.L120"') ||
|
||
// canPlayType('...codecs="hev1.1.L120"') ||
|
||
// canPlayType('...codecs="hvc1.1.0.L120"') || ...
|
||
//
|
||
// so a spurious block of tier 1:120 (all spellings map to it) removes hevc
|
||
// from every DirectPlayProfile and transcode every HEVC file in the library.
|
||
// 1:120 is NOT part of Jellyfin's level chain; it is only that gate.
|
||
var CODEC_PROBES = [
|
||
{ key: '1:120', name: 'Main L4.0', w: 1920, h: 1080, fps: 30, mbps: 8, codec: 'hvc1.1.0.L120' },
|
||
{ key: '1:123', name: 'Main L4.1', w: 1920, h: 1080, fps: 60, mbps: 8, codec: 'hvc1.1.4.L123' },
|
||
{ key: '1:153', name: 'Main L5.1 (4K)', w: 3840, h: 2160, fps: 60, mbps: 20, codec: 'hvc1.1.5.L153.B0' },
|
||
{ key: '1:183', name: 'Main L6.1 (8K)', w: 8192, h: 4096, fps: 60, mbps: 30, codec: 'hvc1.1.6.L183.B0' },
|
||
{ key: '1:186', name: 'Main L6.2 (8K60)', w: 8192, h: 4096, fps: 60, mbps: 30, codec: 'hvc1.1.6.L186.B0' },
|
||
{ key: '2:123', name: 'Main10 L4.1', w: 1920, h: 1080, fps: 60, mbps: 8, codec: 'hvc1.2.4.L123' },
|
||
{ key: '2:153', name: 'Main10 L5.1 (4K)', w: 3840, h: 2160, fps: 60, mbps: 20, codec: 'hvc1.2.4.L153' },
|
||
{ key: '2:183', name: 'Main10 L6.1 (8K)', w: 8192, h: 4096, fps: 60, mbps: 30, codec: 'hvc1.2.4.L183' },
|
||
{ key: '2:186', name: 'Main10 L6.2 (8K60)', w: 8192, h: 4096, fps: 60, mbps: 30, codec: 'hvc1.2.4.L186' }
|
||
];
|
||
|
||
/**
|
||
* 'video/mp4; codecs="hvc1.2.4.L183.B0"' -> '2:183' (profile_idc : level).
|
||
* Both the hvc1 and hev1 spellings map to the same key: they differ in the
|
||
* sample entry, not in what has to be decoded.
|
||
*/
|
||
function hevcKey(type) {
|
||
var m = /(?:hvc1|hev1)\.([0-9]+)(?:\.[0-9]+)?\.l([0-9]+)/i.exec(String(type || ''));
|
||
return m ? m[1] + ':' + m[2] : null;
|
||
}
|
||
NS.caps.hevcKey = hevcKey;
|
||
|
||
(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 {
|
||
var ve = document.createElement('video');
|
||
['video/mp4; codecs="hev1.1.6.L183.B0"', // Main 8-bit 8K (the files that work)
|
||
'video/mp4; codecs="hvc1.1.6.L183.B0"',
|
||
'video/mp4; codecs="hev1.2.4.L183.B0"', // Main 10 8K (the files that do not)
|
||
'video/mp4; codecs="hvc1.2.4.L183.B0"',
|
||
'video/mp4; codecs="hev1.2.4.L153.B0"', // Main 10 4K
|
||
'video/mp4; codecs="hvc1.2.4.L153.B0"',
|
||
'video/mp4; codecs="hev1"',
|
||
'video/mp4; codecs="hvc1"'].forEach(function (cs) {
|
||
NS.caps.hevc[cs] = (typeof MediaSource !== 'undefined' && MediaSource.isTypeSupported(cs)) ||
|
||
ve.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; });
|
||
}
|
||
})();
|
||
|
||
/* ------------------------------------------------------------------ *
|
||
* MediaCapabilities probe + canPlayType override
|
||
* ------------------------------------------------------------------ */
|
||
|
||
/** Tiers the user has pinned as "keep claiming this", '<key>,<key>' in localStorage. */
|
||
function readKeepList() {
|
||
try {
|
||
var raw = localStorage.getItem('jfvr-cpt-keep') || '';
|
||
return raw.split(',').map(function (s) { return s.trim(); }).filter(Boolean);
|
||
} catch (e) { return []; }
|
||
}
|
||
|
||
/** Why a verdict means "do not claim this tier", or null if it is fine. */
|
||
function blockReason(v, keep) {
|
||
if (!v || v.error) return null; // unknown -> leave it to the browser
|
||
if (keep.indexOf(v.key) >= 0) return null; // user pinned it
|
||
if (v.supported === false) return 'unsupported';
|
||
if (v.smooth === false) return 'not-smooth';
|
||
return null;
|
||
}
|
||
|
||
/** True when this tier is not usable on its own terms. */
|
||
function tierUnusable(key) {
|
||
var v = NS.caps.codec[key];
|
||
return !!v && !v.error && (v.supported === false || v.smooth === false);
|
||
}
|
||
|
||
/**
|
||
* The decision the canPlayType hook uses.
|
||
*
|
||
* A Main 10 tier is only blocked when the SAME LEVEL is also unusable in Main
|
||
* (8-bit). See the long comment above CODEC_PROBES: Jellyfin's profile cannot
|
||
* express "Main up to 8K, Main 10 up to 4K", so blocking 2:183 on a device
|
||
* that handles 8-bit 8K would drag all of its 8-bit 8K files into transcoding.
|
||
*/
|
||
function shouldBlock(key, keep) {
|
||
var v = NS.caps.codec[key];
|
||
if (!v || v.error) return null;
|
||
if (keep.indexOf(key) >= 0) return null;
|
||
var own = blockReason(v, keep);
|
||
if (!own) return null;
|
||
var parts = key.split(':');
|
||
|
||
// Base tier. 1:120 is not in Jellyfin's level chain -- it is only the
|
||
// coarse "does this client have HEVC at all" gate (supportsHevc() in
|
||
// browserDeviceProfile.js, which probes exactly this level). Blocking it
|
||
// drops hevc from every DirectPlayProfile, i.e. transcodes the entire
|
||
// HEVC library. Never act on it unless the device is unusable at EVERY
|
||
// Main tier, so one flaky/early probe cannot do that.
|
||
if (key === '1:120') {
|
||
var anyMainUsable = CODEC_PROBES.some(function (p) {
|
||
if (p.key === '1:120' || p.key.charAt(0) !== '1') return false;
|
||
var m = NS.caps.codec[p.key];
|
||
return !!m && !m.error && m.supported !== false && m.smooth !== false;
|
||
});
|
||
if (anyMainUsable) return null;
|
||
}
|
||
|
||
if (parts[0] === '2') {
|
||
// Control: the same level in Main (8-bit).
|
||
if (!tierUnusable('1:' + parts[1])) return null;
|
||
}
|
||
return own;
|
||
}
|
||
|
||
function applyVerdicts() {
|
||
var keep = NS.caps.__keep || [];
|
||
NS.caps.codecKept = [];
|
||
NS.caps.codecBlocked = [];
|
||
CODEC_PROBES.forEach(function (p) {
|
||
var v = NS.caps.codec[p.key];
|
||
if (!v) return;
|
||
var why = shouldBlock(p.key, keep);
|
||
if (why) NS.caps.codecBlocked.push(p.name + ' (' + why + ')');
|
||
else if (keep.indexOf(p.key) >= 0) NS.caps.codecKept.push(p.name);
|
||
});
|
||
}
|
||
|
||
function installCanPlayTypeOverride() {
|
||
if (NS.caps.cptMode === 'off') return;
|
||
var proto = window.HTMLVideoElement && window.HTMLVideoElement.prototype;
|
||
if (!proto || typeof proto.canPlayType !== 'function') return;
|
||
|
||
var orig = proto.canPlayType;
|
||
proto.canPlayType = function (type) {
|
||
try {
|
||
var key = hevcKey(type);
|
||
// Only a definite "no" is acted on. Anything else is the browser's call.
|
||
if (key && shouldBlock(key, NS.caps.__keep || [])) return '';
|
||
} catch (e) { /* never break the native answer */ }
|
||
return orig.apply(this, arguments);
|
||
};
|
||
NS.caps.cptPatched = true;
|
||
NS.caps.cptMode = 'on';
|
||
}
|
||
|
||
(function probeCodecs() {
|
||
// Native behavior is the default: do NOT install the canPlayType override,
|
||
// so Jellyfin builds its own device profile exactly as it would without this
|
||
// plugin. Opt back in with localStorage['jfvr-cpt'] = 'on' when diagnosing.
|
||
// The decodingInfo probe still runs so the diagnostics panel can report what
|
||
// the device really supports -- it just no longer changes any decision.
|
||
var hookOn = false;
|
||
try { hookOn = localStorage.getItem('jfvr-cpt') === 'on'; } catch (e) {}
|
||
NS.caps.cptMode = hookOn ? null : 'off';
|
||
|
||
var mc = navigator.mediaCapabilities;
|
||
if (!mc || typeof mc.decodingInfo !== 'function') { NS.caps.cptMode = 'no-api'; return; }
|
||
|
||
// Read the pin list once, up front: canPlayType is synchronous and must not
|
||
// touch localStorage on every call.
|
||
NS.caps.__keep = readKeepList();
|
||
|
||
// Install FIRST, synchronously, so the hook is in place before anything can
|
||
// read canPlayType. Until a verdict lands the hook just defers to native,
|
||
// which is the safe default.
|
||
if (hookOn) installCanPlayTypeOverride();
|
||
|
||
var pending = CODEC_PROBES.length;
|
||
CODEC_PROBES.forEach(function (p) {
|
||
var cfg = {
|
||
type: 'file',
|
||
video: {
|
||
contentType: 'video/mp4; codecs="' + p.codec + '"',
|
||
width: p.w, height: p.h, bitrate: p.mbps * 1000000, framerate: p.fps
|
||
}
|
||
};
|
||
mc.decodingInfo(cfg).then(function (info) {
|
||
NS.caps.codec[p.key] = {
|
||
key: p.key, name: p.name, workload: p.w + 'x' + p.h + '@' + p.fps + ' ' + p.mbps + 'Mbps',
|
||
supported: info.supported, smooth: info.smooth, powerEfficient: info.powerEfficient
|
||
};
|
||
}).catch(function (e) {
|
||
NS.caps.codec[p.key] = { key: p.key, name: p.name, error: String(e && e.message || e) };
|
||
}).then(function () {
|
||
if (--pending === 0) {
|
||
applyVerdicts();
|
||
NS.log('codec probe done (hook ' + NS.caps.cptMode + '); would block = ' +
|
||
(NS.caps.codecBlocked.join(', ') || 'none') +
|
||
(NS.caps.codecKept.length ? '; pinned = ' + NS.caps.codecKept.join(', ') : ''));
|
||
}
|
||
});
|
||
});
|
||
})();
|
||
|
||
NS.capsDiag = function () {
|
||
var c = NS.caps;
|
||
var keep = readKeepList();
|
||
var real = CODEC_PROBES.map(function (p) {
|
||
var v = c.codec[p.key];
|
||
if (!v) return ' ' + p.name.padEnd(20) + ' (pending)';
|
||
if (v.error) return ' ' + p.name.padEnd(20) + ' error: ' + v.error;
|
||
var why = shouldBlock(p.key, keep);
|
||
var own = blockReason(v, keep);
|
||
var note = why ? ' <-- BLOCKED (' + why + ')' :
|
||
(own ? ' (unusable, but kept: 8-bit at this level works)' :
|
||
(keep.indexOf(p.key) >= 0 ? ' <-- pinned' : ''));
|
||
return ' ' + p.name.padEnd(20) + ' supported=' + v.supported +
|
||
' smooth=' + v.smooth + ' powerEfficient=' + v.powerEfficient +
|
||
' [' + v.workload + ']' + note;
|
||
}).join('\n');
|
||
|
||
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 canPlayType : ' + JSON.stringify(c.hevc),
|
||
'canPlayType hook : ' + (c.cptMode || 'pending') +
|
||
(c.cptMode === 'on' ? ' (blocked: ' + (c.codecBlocked.join(', ') || 'nothing yet') + ')' : ''),
|
||
'HEVC real (decodingInfo):',
|
||
real
|
||
].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;
|
||
}
|
||
|
||
// ADDENDUM 11b: the beam reaches the panel plane whenever the ray meets it,
|
||
// even when the intersection is off the panel (there is simply no cursor then).
|
||
// Previously it fell back to a fixed length without a hit, so an edge-aimed
|
||
// beam stopped short of the plane while the cursor was clamped to the border.
|
||
function beamLength(p) {
|
||
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
|
||
};
|
||
})();
|
||
|
||
|
||
|
||
/* ===== 40-watchdog.js ===== */
|
||
|
||
/*!
|
||
* JFVR watchdog — "this video never started" detection.
|
||
*
|
||
* WHY THIS EXISTS
|
||
* ---------------
|
||
* Jellyfin Web builds the whole player UI (including the quality menu) only
|
||
* after the 'playbackstart' event, which the media element fires once it has
|
||
* actually started. The load order is:
|
||
*
|
||
* HtmlVideoPlayer.play() htmlVideoPlayer/plugin.js:502
|
||
* createMediaElement() :1802 -> loading.show() spinner
|
||
* -> <div.videoPlayerContainer><video>
|
||
* -> inserted into document.body
|
||
* (no src yet!)
|
||
* setCurrentSrc() :593 -> elem.src = url, only now
|
||
* -> loadedmetadata -> canplay -> play
|
||
* -> 'playbackstart'
|
||
* -> OSD controller (apps/legacy/controllers/playback/video/index.js:591)
|
||
*
|
||
* So when the browser cannot use the stream, the OSD never appears at all: the
|
||
* page is a spinner over an empty container with a <video> whose videoWidth is
|
||
* still 0. There is no quality menu to click, because it was never created.
|
||
*
|
||
* That is the case this module covers. It watches for exactly that state and
|
||
* offers the only thing that can help: tell Jellyfin to transcode.
|
||
*
|
||
* WHY localStorage AND NOT THE PLAYBACK MANAGER
|
||
* --------------------------------------------
|
||
* The bitrate is normally set through playbackManager.setMaxStreamingBitrate(),
|
||
* which does a live changeStream(). That manager is an ES module export and is
|
||
* NOT on window (verified: window.playbackManager is undefined; window.Emby only
|
||
* has Page). So the watchdog writes the same keys appSettings would have written
|
||
* (src/scripts/settings/appSettings.js) and reloads, which makes Jellyfin request
|
||
* PlaybackInfo again with the new cap and pick Transcode.
|
||
*
|
||
* NOTE the key name really does say "bitrate" twice — that is upstream's typo,
|
||
* reproduced here deliberately.
|
||
*/
|
||
(function () {
|
||
'use strict';
|
||
|
||
var NS = window.__JFVR;
|
||
if (!NS) { console.error('[JFVR] core missing'); return; }
|
||
if (NS.__watchdog) return;
|
||
NS.__watchdog = true;
|
||
|
||
var S = NS.state;
|
||
var log = NS.log;
|
||
|
||
/* ------------------------------------------------------------------ *
|
||
* tunables
|
||
* ------------------------------------------------------------------ */
|
||
var POLL_MS = 500;
|
||
|
||
// Direct play on a LAN should have metadata almost immediately; the file is
|
||
// 11 GB but its moov atom is at the front, so there is nothing to wait for.
|
||
// A transcode is different: the server has to decode the first GOP and produce
|
||
// segments before the browser sees any metadata, and this NAS is doing 8K.
|
||
var DEAD_MS = 15000;
|
||
var DEAD_TRANSCODE_MS = 45000;
|
||
|
||
// Metadata parsed but the playhead is not moving. This is the silent stall: a
|
||
// decoder that reads the header and then cannot keep up (8K HEVC Main 10 on a
|
||
// broken VAAPI driver is the motivating case). The old "has picture" check
|
||
// only looked at videoWidth/readyState, which are both already set here, so
|
||
// playback that never advances stood the watchdog down forever and the user
|
||
// got a frozen picture with no prompt. Give it a little longer than a clean
|
||
// start, since a heavy 8K stream legitimately buffers early.
|
||
var STALL_MS = 20000;
|
||
|
||
// After the user answers (either way) stay quiet, so a slow-but-working
|
||
// playback does not get nagged at.
|
||
var QUIET_MS = 120000;
|
||
|
||
var QUIET_KEY = 'jfvr-watchdog-quiet-until';
|
||
|
||
// Values from Jellyfin's own menu (src/components/qualityOptions.js).
|
||
var CHOICES = [
|
||
{ label: '20 Mbps', bitrate: 20000000, note: '≈4K,最接近原始画质' },
|
||
{ label: '8 Mbps', bitrate: 8000000, note: '1080p 档,稳' },
|
||
{ label: '3 Mbps', bitrate: 3000000, note: '720p 档,最省算力' }
|
||
];
|
||
|
||
var W = {
|
||
seenAt: 0, // when the current stalled <video> first showed up
|
||
shown: false,
|
||
quietUntil: 0,
|
||
lastReason: '',
|
||
applied: null,
|
||
codec: null, // filled in best-effort from the Jellyfin API
|
||
panel: null,
|
||
lastTime: -1, // playhead seen on the previous poll
|
||
progressAt: 0 // when the playhead last moved
|
||
};
|
||
NS.Watchdog = W;
|
||
|
||
/* ------------------------------------------------------------------ *
|
||
* helpers
|
||
* ------------------------------------------------------------------ */
|
||
|
||
function now() { return Date.now(); }
|
||
|
||
/**
|
||
* The element Jellyfin creates in createMediaElement(). It exists from the
|
||
* very start of playback — including the failure case, which is the whole
|
||
* point: findVideo() in 90-main.js requires videoWidth and would return null.
|
||
*/
|
||
function playerVideo() {
|
||
var c = document.querySelector('.videoPlayerContainer');
|
||
if (!c) return null;
|
||
return c.querySelector('video') || null;
|
||
}
|
||
|
||
function isTranscode(v) {
|
||
var src = v.currentSrc || v.src || '';
|
||
return src.indexOf('/hls') >= 0 || src.indexOf('.m3u8') >= 0;
|
||
}
|
||
|
||
/** Metadata parsed AND a frame available. False means "no picture at all". */
|
||
function hasPicture(v) {
|
||
return v.videoWidth > 0 && v.readyState >= 2;
|
||
}
|
||
|
||
function readQuiet() {
|
||
try {
|
||
var raw = window.sessionStorage && sessionStorage.getItem(QUIET_KEY);
|
||
return raw ? parseInt(raw, 10) || 0 : 0;
|
||
} catch (e) { return 0; }
|
||
}
|
||
|
||
function setQuiet(ms) {
|
||
W.quietUntil = now() + ms;
|
||
try { sessionStorage.setItem(QUIET_KEY, String(W.quietUntil)); } catch (e) {}
|
||
}
|
||
|
||
/**
|
||
* Write the same keys Jellyfin's own quality menu writes, for BOTH in-network
|
||
* and out-of-network, because the client is the only side that knows which one
|
||
* applies and we cannot ask it (appSettings is not on window).
|
||
*/
|
||
function applyBitrate(bitrate) {
|
||
var media = 'Video';
|
||
['true', 'false'].forEach(function (inNet) {
|
||
try {
|
||
localStorage.setItem('enableautobitratebitrate-' + media + '-' + inNet, 'false');
|
||
localStorage.setItem('maxbitrate-' + media + '-' + inNet, String(bitrate));
|
||
} catch (e) {
|
||
log('watchdog: could not persist bitrate', e && e.message);
|
||
}
|
||
});
|
||
W.applied = bitrate;
|
||
log('watchdog: capped bitrate at', bitrate, 'and reloading');
|
||
setQuiet(QUIET_MS);
|
||
window.location.reload();
|
||
}
|
||
|
||
/* ------------------------------------------------------------------ *
|
||
* best-effort: name the actual format so the message is actionable
|
||
* ------------------------------------------------------------------ */
|
||
|
||
function itemIdFrom(v) {
|
||
var s = (v && (v.currentSrc || v.src)) || '';
|
||
var m = s.match(/\/Videos\/([0-9a-f]{32})\//i);
|
||
if (m) return m[1];
|
||
m = (window.location.hash || '').match(/[?&]id=([0-9a-f]{32})/i);
|
||
return m ? m[1] : null;
|
||
}
|
||
|
||
function describeCodec(v) {
|
||
if (W.codec) return;
|
||
var id = itemIdFrom(v);
|
||
if (!id || !window.ApiClient) return;
|
||
var url;
|
||
try { url = window.ApiClient.getUrl('Items/' + id); } catch (e) { return; }
|
||
window.ApiClient.ajax({ type: 'GET', url: url, dataType: 'json' }).then(function (item) {
|
||
var ms = (item && item.MediaSources && item.MediaSources[0] && item.MediaSources[0].MediaStreams) || [];
|
||
for (var i = 0; i < ms.length; i++) {
|
||
var st = ms[i];
|
||
if (st.Type !== 'Video') continue;
|
||
W.codec = [
|
||
st.Width + '×' + st.Height,
|
||
(st.Codec || '').toUpperCase(),
|
||
st.Profile || '',
|
||
st.BitDepth ? st.BitDepth + '-bit' : ''
|
||
].filter(Boolean).join(' ');
|
||
break;
|
||
}
|
||
if (W.shown) render(); // update the message in place
|
||
}).catch(function () { /* the prompt works fine without this */ });
|
||
}
|
||
|
||
/* ------------------------------------------------------------------ *
|
||
* the prompt
|
||
* ------------------------------------------------------------------ */
|
||
|
||
var WD_CSS = [
|
||
'.jfvr-wd{position:fixed;left:50%;top:50%;transform:translate(-50%,-50%);z-index:2147483002;width:420px;max-width:calc(100vw - 32px);',
|
||
'background:rgba(18,18,22,.97);color:#eaeaea;border:1px solid rgba(255,255,255,.16);border-radius:14px;',
|
||
'box-shadow:0 18px 60px rgba(0,0,0,.7);font-family:inherit;font-size:14px;padding:18px 18px 14px;backdrop-filter:blur(10px)}',
|
||
'.jfvr-wd h3{margin:0 0 6px;font-size:16px;font-weight:600}',
|
||
'.jfvr-wd p{margin:0 0 10px;font-size:13px;line-height:1.6;color:#c3c9d4}',
|
||
'.jfvr-wd .jfvr-wd-fmt{color:#ffd479;font-weight:600}',
|
||
'.jfvr-wd .jfvr-wd-sub{font-size:12px;color:#8b93a1;line-height:1.55;margin-bottom:12px}',
|
||
'.jfvr-wd-btns{display:flex;flex-direction:column;gap:8px;margin-bottom:10px}',
|
||
'.jfvr-wd-btns button{display:flex;justify-content:space-between;align-items:baseline;gap:10px;width:100%;padding:10px 12px;',
|
||
'border:1px solid rgba(255,255,255,.16);border-radius:9px;background:rgba(255,255,255,.05);color:#eaeaea;',
|
||
'font-size:14px;font-weight:600;cursor:pointer;font-family:inherit;text-align:left}',
|
||
'.jfvr-wd-btns button:hover{background:rgba(78,161,255,.18);border-color:#4ea1ff}',
|
||
'.jfvr-wd-btns button span.note{font-size:11px;font-weight:400;color:#8b93a1}',
|
||
'.jfvr-wd-quiet{width:100%;padding:9px;border:0;border-radius:9px;background:transparent;color:#8b93a1;',
|
||
'font-size:13px;cursor:pointer;font-family:inherit}',
|
||
'.jfvr-wd-quiet:hover{color:#eaeaea}',
|
||
'.jfvr-wd-diag{margin-top:10px;font-size:11px;color:#8b93a1}',
|
||
'.jfvr-wd-diag summary{cursor:pointer;user-select:none}',
|
||
'.jfvr-wd-diag pre{white-space:pre-wrap;word-break:break-all;margin:8px 0 0;font-size:10px;line-height:1.5;',
|
||
'max-height:150px;overflow:auto;color:#9fb0c4;background:rgba(0,0,0,.35);padding:8px;border-radius:7px}'
|
||
].join('');
|
||
|
||
function injectWdCss() {
|
||
if (document.getElementById('jfvr-wd-css')) return;
|
||
var s = NS.makeEl('style');
|
||
s.id = 'jfvr-wd-css';
|
||
s.textContent = WD_CSS;
|
||
document.head.appendChild(s);
|
||
}
|
||
|
||
function diagText(v) {
|
||
var e = v.error;
|
||
return [
|
||
'readyState : ' + v.readyState + ' (0=HAVE_NOTHING, 1=HAVE_METADATA, 2=HAVE_CURRENT_DATA)',
|
||
'networkState: ' + v.networkState,
|
||
'videoWidth : ' + v.videoWidth + '×' + v.videoHeight,
|
||
'paused : ' + v.paused,
|
||
'currentTime : ' + v.currentTime,
|
||
'error : ' + (e ? e.code + ' ' + (e.message || '') : 'none'),
|
||
'src : ' + String(v.currentSrc || v.src || '').slice(0, 160),
|
||
'格式 : ' + (W.codec || '(未知)')
|
||
].join('\n');
|
||
}
|
||
|
||
function render() {
|
||
if (!W.panel) return;
|
||
var body = W.panel.querySelector('.jfvr-wd-body');
|
||
var v = playerVideo();
|
||
if (body && v) {
|
||
var fmt = body.querySelector('.jfvr-wd-fmt');
|
||
if (fmt) fmt.textContent = W.codec || '这个格式';
|
||
var pre = body.querySelector('pre');
|
||
if (pre) pre.textContent = diagText(v);
|
||
}
|
||
}
|
||
|
||
function show(v, reason) {
|
||
if (W.shown) { render(); return; }
|
||
W.shown = true;
|
||
W.lastReason = reason;
|
||
injectWdCss();
|
||
|
||
var p = NS.makeEl('div', 'jfvr-wd');
|
||
p.id = 'jfvr-wd';
|
||
p.appendChild(NS.makeEl('h3', null,
|
||
reason === 'stalled' ? '视频播放卡住了' : '视频没能开始播放'));
|
||
|
||
var body = NS.makeEl('div', 'jfvr-wd-body');
|
||
|
||
var msg = NS.makeEl('p');
|
||
var waited = Math.round(timeoutMs(v) / 1000);
|
||
var intro;
|
||
if (reason === 'error') {
|
||
intro = '播放器报错了,一个画面都没解出来。';
|
||
} else if (reason === 'stalled') {
|
||
intro = '画面已经出来了,但播放进度停住不动了(<code>currentTime</code> 不再前进)—— ' +
|
||
'解码器跟不上这个文件。';
|
||
} else {
|
||
intro = '等了 ' + waited + ' 秒都没拿到任何画面(<code>videoWidth = 0</code>),' +
|
||
'所以播放器界面也没有被创建出来。';
|
||
}
|
||
msg.innerHTML =
|
||
intro +
|
||
'最常见的原因是<strong>这台设备解不动这个格式</strong>:' +
|
||
'<span class="jfvr-wd-fmt">' + (W.codec || '该格式') + '</span>。<br>' +
|
||
'让服务器转码成 H.264 再播就能看到画面,代价是画质下降(这台 NAS 的转码上限约 4K)。';
|
||
body.appendChild(msg);
|
||
|
||
body.appendChild(NS.makeEl('div', 'jfvr-wd-sub',
|
||
'选一档码率后会写入播放设置并重新加载页面。'));
|
||
|
||
var btns = NS.makeEl('div', 'jfvr-wd-btns');
|
||
CHOICES.forEach(function (c) {
|
||
var b = NS.makeEl('button');
|
||
b.type = 'button';
|
||
b.appendChild(NS.makeEl('span', null, c.label));
|
||
b.appendChild(NS.makeEl('span', 'note', c.note));
|
||
b.addEventListener('click', function (ev) {
|
||
ev.stopPropagation();
|
||
applyBitrate(c.bitrate);
|
||
});
|
||
btns.appendChild(b);
|
||
});
|
||
body.appendChild(btns);
|
||
|
||
var quiet = NS.makeEl('button', 'jfvr-wd-quiet', '继续等待,先别管我');
|
||
quiet.type = 'button';
|
||
quiet.addEventListener('click', function (ev) {
|
||
ev.stopPropagation();
|
||
setQuiet(QUIET_MS);
|
||
hide();
|
||
});
|
||
body.appendChild(quiet);
|
||
|
||
var diag = NS.makeEl('details', 'jfvr-wd-diag');
|
||
diag.appendChild(NS.makeEl('summary', null, '诊断信息'));
|
||
diag.appendChild(NS.makeEl('pre', null, diagText(v)));
|
||
body.appendChild(diag);
|
||
|
||
p.appendChild(body);
|
||
document.body.appendChild(p);
|
||
W.panel = p;
|
||
|
||
NS.recordError('watchdog', new Error('playback never started: ' + reason));
|
||
log('watchdog: prompt shown, reason =', reason, 'codec =', W.codec);
|
||
}
|
||
|
||
function hide() {
|
||
W.shown = false;
|
||
if (W.panel) { W.panel.remove(); W.panel = null; }
|
||
}
|
||
|
||
function reset() {
|
||
W.seenAt = 0;
|
||
W.lastTime = -1;
|
||
W.progressAt = 0;
|
||
W.codec = null;
|
||
if (W.shown) hide();
|
||
}
|
||
|
||
function timeoutMs(v) {
|
||
return isTranscode(v) ? DEAD_TRANSCODE_MS : DEAD_MS;
|
||
}
|
||
|
||
/* ------------------------------------------------------------------ *
|
||
* poll
|
||
* ------------------------------------------------------------------ */
|
||
|
||
function poll() {
|
||
// Never interfere with an active VR session: the session owns the flow and
|
||
// a modal in the middle of it would be a disaster.
|
||
if (S.session) { if (W.shown) hide(); return; }
|
||
|
||
var v = playerVideo();
|
||
if (!v) { reset(); return; }
|
||
|
||
// A media error is unambiguous — no need to wait out the timer.
|
||
if (v.error) {
|
||
describeCodec(v);
|
||
if (W.quietUntil && now() < W.quietUntil) return;
|
||
show(v, 'error');
|
||
return;
|
||
}
|
||
|
||
// Metadata is parsed and a frame exists. That is NOT enough to call it
|
||
// healthy: a decoder that read the header and then cannot keep up leaves
|
||
// videoWidth/readyState set while currentTime never moves. Only a moving
|
||
// playhead counts as playback.
|
||
if (hasPicture(v)) {
|
||
if (!W.progressAt) W.progressAt = now();
|
||
|
||
// Paused / seeking is the user's doing, not a stall.
|
||
if (v.paused || v.seeking) { W.progressAt = now(); W.lastTime = v.currentTime; return; }
|
||
|
||
if (v.currentTime !== W.lastTime) {
|
||
W.lastTime = v.currentTime;
|
||
W.progressAt = now();
|
||
if (W.shown) hide(); // it recovered (e.g. after a transcode switch)
|
||
return;
|
||
}
|
||
|
||
if (now() - W.progressAt < STALL_MS) return;
|
||
if (W.quietUntil && now() < W.quietUntil) return;
|
||
describeCodec(v);
|
||
show(v, 'stalled');
|
||
return;
|
||
}
|
||
|
||
// No picture at all yet: the original "never started" case.
|
||
if (!W.seenAt) W.seenAt = now();
|
||
if (now() - W.seenAt < timeoutMs(v)) return;
|
||
|
||
if (W.quietUntil && now() < W.quietUntil) return;
|
||
|
||
describeCodec(v);
|
||
show(v, 'timeout');
|
||
}
|
||
|
||
NS.Watchdog.init = function () {
|
||
W.quietUntil = readQuiet();
|
||
setInterval(poll, POLL_MS);
|
||
log('watchdog armed (dead=' + DEAD_MS + 'ms, transcode=' + DEAD_TRANSCODE_MS +
|
||
'ms, stall=' + STALL_MS + 'ms)');
|
||
};
|
||
})();
|
||
|
||
|
||
|
||
/* ===== 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 (o.handPref === 'right' || o.handPref === 'left') S.handPref = o.handPref;
|
||
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,
|
||
handPref: S.handPref,
|
||
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.
|
||
// left/top are set from JS (see placeButton) so the button can be dragged
|
||
// anywhere. touch-action:none is what makes a drag work on a touch screen
|
||
// instead of scrolling the page underneath.
|
||
'.jfvr-btn{position:fixed;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:grab;box-shadow:0 6px 22px rgba(0,0,0,.45);font-family:inherit;',
|
||
'touch-action:none;user-select:none;-webkit-user-select:none;-webkit-tap-highlight-color:transparent}',
|
||
'.jfvr-btn:active{transform:scale(.96)}',
|
||
'.jfvr-btn.jfvr-drag{cursor:grabbing;transform:none;box-shadow:0 10px 32px rgba(0,0,0,.65)}',
|
||
// the panel must always fit the viewport AND keep the CTA reachable, so the
|
||
// body scrolls and the button lives in a sticky footer
|
||
// Positioned from JS (positionPanel) so it follows the button once the
|
||
// button has been dragged; it no longer lives in the viewport corner.
|
||
'.jfvr-panel{position:fixed;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 - 32px);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 = '<span>🎥</span><span>VR 播放</span>';
|
||
b.title = '点击打开设置;按住拖动可以换位置;双击回到默认位置';
|
||
b.addEventListener('click', function (ev) {
|
||
ev.stopPropagation();
|
||
// A drag still produces a click on most browsers. Swallow that one so
|
||
// dragging the button never opens the panel by accident.
|
||
if (dragMoved) { dragMoved = false; return; }
|
||
togglePanel();
|
||
});
|
||
// Escape hatch: a button dragged half off a small window is annoying to
|
||
// recover by hand.
|
||
b.addEventListener('dblclick', function (ev) {
|
||
ev.stopPropagation();
|
||
resetButtonPos();
|
||
});
|
||
installDrag(b);
|
||
document.body.appendChild(b);
|
||
btn = b;
|
||
btnPos = readBtnPos();
|
||
placeButton(b, btnPos);
|
||
}
|
||
|
||
/* ------------------------------------------------------------------ *
|
||
* button placement / dragging
|
||
* ------------------------------------------------------------------
|
||
* The button used to be pinned to the bottom-right corner. On a short window
|
||
* that put it right on top of the bitrate choices the playback watchdog
|
||
* shows (that panel is centred), and there was no way to move it. It is
|
||
* draggable now and the spot is remembered per browser.
|
||
*
|
||
* The settings panel follows the button rather than the viewport corner, so
|
||
* the two cannot drift apart.
|
||
* ------------------------------------------------------------------ */
|
||
var BTN_POS_KEY = 'jfvr-btn-pos';
|
||
var BTN_MARGIN = 8;
|
||
var DRAG_SLOP = 4; // px of travel before it counts as a drag, not a tap
|
||
|
||
var btnPos = null; // {left, top} once placed; persisted on drop
|
||
var dragging = false;
|
||
var dragMoved = false;
|
||
var dragStart = null;
|
||
|
||
function readBtnPos() {
|
||
try {
|
||
var o = JSON.parse(localStorage.getItem(BTN_POS_KEY) || 'null');
|
||
if (o && typeof o.left === 'number' && typeof o.top === 'number') return o;
|
||
} catch (e) {}
|
||
return null;
|
||
}
|
||
|
||
function writeBtnPos(pos) {
|
||
try { localStorage.setItem(BTN_POS_KEY, JSON.stringify(pos)); } catch (e) {}
|
||
}
|
||
|
||
function clampPos(left, top, w, h) {
|
||
var maxLeft = Math.max(BTN_MARGIN, window.innerWidth - w - BTN_MARGIN);
|
||
var maxTop = Math.max(BTN_MARGIN, window.innerHeight - h - BTN_MARGIN);
|
||
return {
|
||
left: Math.round(Math.min(Math.max(left, BTN_MARGIN), maxLeft)),
|
||
top: Math.round(Math.min(Math.max(top, BTN_MARGIN), maxTop))
|
||
};
|
||
}
|
||
|
||
/** The default spot: the old fixed corner, expressed in left/top. */
|
||
function defaultPos(b) {
|
||
return { left: window.innerWidth - b.offsetWidth - 16,
|
||
top: window.innerHeight - b.offsetHeight - 120 };
|
||
}
|
||
|
||
function placeButton(b, pos) {
|
||
var p = pos || defaultPos(b);
|
||
var c = clampPos(p.left, p.top, b.offsetWidth, b.offsetHeight);
|
||
b.style.left = c.left + 'px';
|
||
b.style.top = c.top + 'px';
|
||
b.style.right = 'auto';
|
||
b.style.bottom = 'auto';
|
||
return c;
|
||
}
|
||
|
||
function resetButtonPos() {
|
||
if (!btn) return;
|
||
try { localStorage.removeItem(BTN_POS_KEY); } catch (e) {}
|
||
btnPos = placeButton(btn, null);
|
||
positionPanel();
|
||
}
|
||
|
||
function installDrag(b) {
|
||
b.addEventListener('pointerdown', function (ev) {
|
||
if (ev.button != null && ev.button !== 0) return; // primary button / touch only
|
||
var r = b.getBoundingClientRect();
|
||
dragging = true;
|
||
dragMoved = false;
|
||
dragStart = { x: ev.clientX, y: ev.clientY, left: r.left, top: r.top };
|
||
b.classList.add('jfvr-drag');
|
||
try { b.setPointerCapture(ev.pointerId); } catch (e) {}
|
||
});
|
||
|
||
b.addEventListener('pointermove', function (ev) {
|
||
if (!dragging || !dragStart) return;
|
||
var dx = ev.clientX - dragStart.x;
|
||
var dy = ev.clientY - dragStart.y;
|
||
if (!dragMoved) {
|
||
if (Math.abs(dx) < DRAG_SLOP && Math.abs(dy) < DRAG_SLOP) return;
|
||
dragMoved = true;
|
||
if (panel) hidePanel(); // do not drag the panel around with it
|
||
}
|
||
btnPos = clampPos(dragStart.left + dx, dragStart.top + dy, b.offsetWidth, b.offsetHeight);
|
||
b.style.left = btnPos.left + 'px';
|
||
b.style.top = btnPos.top + 'px';
|
||
});
|
||
|
||
function endDrag(ev) {
|
||
if (!dragging) return;
|
||
dragging = false;
|
||
dragStart = null;
|
||
b.classList.remove('jfvr-drag');
|
||
try { b.releasePointerCapture(ev.pointerId); } catch (e) {}
|
||
if (dragMoved) writeBtnPos(btnPos);
|
||
}
|
||
b.addEventListener('pointerup', endDrag);
|
||
b.addEventListener('pointercancel', endDrag);
|
||
}
|
||
|
||
/** Put the panel next to the button: above it, or below when there is no room. */
|
||
function positionPanel() {
|
||
if (!panel || !btn) return;
|
||
var b = btn.getBoundingClientRect();
|
||
var w = panel.offsetWidth;
|
||
var h = panel.offsetHeight;
|
||
var gap = 8;
|
||
|
||
var left = b.right - w; // right edges aligned
|
||
var top = b.top - h - gap;
|
||
if (top < BTN_MARGIN) top = b.bottom + gap; // no room above -> below
|
||
|
||
var maxLeft = Math.max(BTN_MARGIN, window.innerWidth - w - BTN_MARGIN);
|
||
var maxTop = Math.max(BTN_MARGIN, window.innerHeight - h - BTN_MARGIN);
|
||
panel.style.left = Math.round(Math.min(Math.max(left, BTN_MARGIN), maxLeft)) + 'px';
|
||
panel.style.top = Math.round(Math.min(Math.max(top, BTN_MARGIN), maxTop)) + 'px';
|
||
panel.style.right = 'auto';
|
||
panel.style.bottom = 'auto';
|
||
}
|
||
|
||
function onViewportResize() {
|
||
if (btn && document.body.contains(btn)) btnPos = placeButton(btn, btnPos);
|
||
positionPanel();
|
||
}
|
||
|
||
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 空间右侧有个锁图标:锁上后面板不会再被唤出。'));
|
||
|
||
body.appendChild(NS.makeEl('div', 'jfvr-sec', '操作'));
|
||
body.appendChild(segRow('主用手', function () { return S.handPref || 'right'; },
|
||
function (v) { S.handPref = v; saveSettings(); },
|
||
[{ label: '右手', value: 'right' }, { label: '左手', value: 'left' }]));
|
||
body.appendChild(NS.makeEl('div', 'jfvr-hint',
|
||
'引导线/光标默认在主用手上;另一只手按下会立刻接管那一次操作。'));
|
||
|
||
// 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);
|
||
positionPanel();
|
||
|
||
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 <video>';
|
||
|
||
var keys = Object.keys(S.stats).sort();
|
||
var runtime = keys.map(function (k) {
|
||
var val = S.stats[k];
|
||
return ' ' + k + ' = ' + (typeof val === 'object' ? JSON.stringify(val) : val);
|
||
}).join('\n');
|
||
|
||
pre.textContent =
|
||
'--- 能力 ---\n' + NS.capsDiag() +
|
||
'\n\n--- 视频 ---\n' + video +
|
||
'\n\n--- 运行 ---\n' +
|
||
' rafPumped = ' + RAFQ.pumped + ' pending=' + RAFQ.queue.size + (RAFQ.err ? ' err=' + RAFQ.err : '') + '\n' +
|
||
runtime +
|
||
(S.errors.length ? '\n\n--- 错误 ---\n' + S.errors.slice(-6).map(function (e) { return ' ' + e; }).join('\n') : '');
|
||
}
|
||
|
||
function setMsg(t) {
|
||
var m = document.getElementById('jfvr-msg');
|
||
if (m) m.textContent = t || '';
|
||
}
|
||
|
||
/* ================================================================== *
|
||
* XR session lifecycle
|
||
* ================================================================== */
|
||
function pushRenderState() {
|
||
var session = S.session;
|
||
if (!session || S.ending) return;
|
||
var layers = NS.getLayers();
|
||
if (!layers.length) return;
|
||
try {
|
||
session.updateRenderState({ layers: layers });
|
||
} catch (e) {
|
||
// A teardown race: modules drop their layers while the session is already
|
||
// ending. Harmless, and not worth surfacing as an error.
|
||
S.stats.renderStateErr = String((e && e.message) || e);
|
||
}
|
||
}
|
||
NS.setRenderStatePusher(pushRenderState);
|
||
|
||
function enterVR() {
|
||
var video = S.video;
|
||
if (!video) { setMsg('未找到视频元素。'); return Promise.resolve(); }
|
||
if (!window.isSecureContext) {
|
||
setMsg('当前页不是安全上下文,WebXR 不可用。\n\nQuest 浏览器:chrome://flags → 搜 insecure → ' +
|
||
'启用 unsafely-treat-insecure-origin-as-secure → 填 ' + location.origin +
|
||
' → Relaunch。');
|
||
return Promise.resolve();
|
||
}
|
||
if (!navigator.xr) { setMsg('此浏览器没有暴露 WebXR。'); return Promise.resolve(); }
|
||
|
||
setMsg('');
|
||
|
||
// requestSession() must be called synchronously inside the click's
|
||
// transient-activation task. Never await before it.
|
||
var sessionPromise;
|
||
try {
|
||
log('requesting immersive-vr \u2026');
|
||
sessionPromise = navigator.xr.requestSession('immersive-vr', {
|
||
optionalFeatures: ['local-floor', 'bounded-floor', 'layers', 'hand-tracking']
|
||
});
|
||
} catch (e) {
|
||
setMsg('进入 VR 失败:' + ((e && e.message) || e));
|
||
return Promise.resolve();
|
||
}
|
||
|
||
try { video.play().catch(function () {}); } catch (e) {}
|
||
var pre = Promise.resolve();
|
||
try { if (document.pictureInPictureElement) pre = document.exitPictureInPicture().catch(function () {}); } catch (e) {}
|
||
try { if (document.fullscreenElement) pre = pre.then(function () { return document.exitFullscreen().catch(function () {}); }); } catch (e) {}
|
||
|
||
return pre.then(function () { return sessionPromise; }).then(function (session) {
|
||
S.session = session;
|
||
log('session started', session);
|
||
|
||
// --- frame-rate fix -------------------------------------------------
|
||
// 59.94 fps video on a 90 Hz session shows each frame 1 or 2 refreshes
|
||
// alternately (90/59.94 = 1.5) => 3:2 pulldown judder. 120 Hz is an exact
|
||
// multiple (2.002), so every frame gets the same number of refreshes.
|
||
try {
|
||
var rates = session.supportedFrameRates ? Array.prototype.slice.call(session.supportedFrameRates) : [];
|
||
var want = rates.indexOf(120) >= 0 ? 120 : (rates.length ? rates[rates.length - 1] : 0);
|
||
S.stats.supportedFrameRates = rates;
|
||
if (want && typeof session.updateTargetFrameRate === 'function') {
|
||
session.updateTargetFrameRate(want);
|
||
S.stats.targetFrameRate = want;
|
||
log('updateTargetFrameRate ->', want, 'Hz (supported: ' + rates.join('/') + ')');
|
||
}
|
||
} catch (e) { log('updateTargetFrameRate failed', e); }
|
||
|
||
session.addEventListener('end', onSessionEnd, { once: true });
|
||
session.addEventListener('select', onSelect);
|
||
try { session.addEventListener('selectend', onSelectEnd); } catch (e) {}
|
||
|
||
// --- reference spaces ------------------------------------------------
|
||
var p = session.requestReferenceSpace('local').then(function (rs) { S.refSpace = rs; NS.refSpace = rs; })
|
||
.catch(function (e) { NS.recordError('local refSpace', e); });
|
||
p = p.then(function () {
|
||
return session.requestReferenceSpace('viewer')
|
||
.then(function (vs) { S.viewerSpace = vs; NS.viewerSpace = vs; })
|
||
.catch(function (e) { NS.recordError('viewer refSpace', e); });
|
||
});
|
||
|
||
// --- rAF broker, layers, frame loop ---------------------------------
|
||
return p.then(function () {
|
||
startRafPump(video);
|
||
|
||
var made = [];
|
||
|
||
// ORDER MATTERS: layers composite in array order, later = on top.
|
||
// video (bottom) -> hands -> subtitles -> controls (top)
|
||
try {
|
||
var vlayer = createVideoLayer(session);
|
||
if (vlayer) {
|
||
NS.addLayer(vlayer);
|
||
S.videoLayer = vlayer;
|
||
made.push('video');
|
||
// ADDENDUM 8: angles start dead ahead every session (never restored),
|
||
// then the transform is applied so probe + apply see the real layer.
|
||
S.videoYaw = 0;
|
||
S.videoPitch = 0;
|
||
videoSpaceCache = null;
|
||
S.videoTransformLive = probeVideoTransformWritable(vlayer);
|
||
S.stats.videoTransformWritable = !!S.videoTransformLive;
|
||
applyVideoAngle(true); // restores the probe marker, sets the aim
|
||
log('video aim: ' + videoRotStrategy() + ' (transform writable: ' + !!S.videoTransformLive + ')');
|
||
updateVideoAngleStats();
|
||
}
|
||
} catch (e) { NS.recordError('video layer', e); made.push('video:err'); }
|
||
|
||
// System hand models (WebXR Input Profiles 'generic-hand'), rendered into
|
||
// their own projection layer. Placed above the video but below the panel
|
||
// so the panel stays readable when a hand is behind it.
|
||
try {
|
||
if (NS.Hands && NS.Hands.create) {
|
||
var hl = NS.Hands.create(session);
|
||
if (hl) { NS.addLayer(hl); made.push('hands'); }
|
||
else made.push('hands:null');
|
||
}
|
||
} catch (e) { NS.recordError('hands.create', e); made.push('hands:err'); }
|
||
|
||
try {
|
||
if (S.subtitles && NS.Subtitles && S.subMode !== 'off') {
|
||
NS.Subtitles.setMode(S.subMode);
|
||
var sl = NS.Subtitles.create(session);
|
||
if (sl) made.push('subtitles'); else made.push('subtitles:null');
|
||
}
|
||
} catch (e) { NS.recordError('subtitles.create', e); made.push('subtitles:err'); }
|
||
|
||
try {
|
||
if (S.controls && NS.Controls) {
|
||
NS.Controls.setVideo(video, controlsExtra());
|
||
NS.Controls.onAction(handleControlAction);
|
||
// ADDENDUM 7: the panel lock is per VR session - a new session always
|
||
// starts unlocked so it can never be left in a state where the bar
|
||
// refuses to appear and the user does not know why.
|
||
S.ctlLock = false;
|
||
barShownAt = -1e9;
|
||
if (NS.Controls.setLock) { try { NS.Controls.setLock(false); } catch (e) {} }
|
||
var cl = NS.Controls.create(session);
|
||
if (cl) made.push('controls'); else made.push('controls:null');
|
||
}
|
||
} catch (e) { NS.recordError('controls.create', e); made.push('controls:err'); }
|
||
|
||
S.stats.layersBuilt = made.join(',');
|
||
|
||
var frames = 0, lastT = 0, lastFrameT = 0;
|
||
var loop = function (time, frame) {
|
||
if (S.session !== session) return;
|
||
frames++;
|
||
if (!lastT) lastT = time;
|
||
var dt = lastFrameT ? Math.min(0.1, (time - lastFrameT) / 1000) : 0.016;
|
||
lastFrameT = time;
|
||
if (time - lastT >= 1000) {
|
||
S.stats.fps = +(frames * 1000 / (time - lastT)).toFixed(1);
|
||
frames = 0; lastT = time;
|
||
}
|
||
if (frame) {
|
||
// One bad frame must never stop the loop: if this throws, rAF is never
|
||
// requested again and the whole session freezes.
|
||
try {
|
||
updatePointer(frame);
|
||
pollGamepads(frame);
|
||
dropStaleHolds();
|
||
updatePanelPose(frame, dt);
|
||
updateGrabFrame(frame);
|
||
updateViewDrag(frame);
|
||
NS.runFrameHooks(frame, time, session);
|
||
} catch (e) {
|
||
NS.recordError('frame', e);
|
||
S.stats.frameErr = (e && e.message) || String(e);
|
||
}
|
||
}
|
||
session.requestAnimationFrame(loop);
|
||
};
|
||
session.requestAnimationFrame(loop);
|
||
|
||
log('layers:', S.stats.layersBuilt, '| freespace ok');
|
||
});
|
||
}).then(function () {
|
||
try { video.play().catch(function () {}); } catch (e) {}
|
||
setMsg('已进入 VR。');
|
||
refreshHint();
|
||
}).catch(function (e) {
|
||
NS.recordError('enterVR', e);
|
||
setMsg('进入 VR 失败:' + ((e && e.message) || e));
|
||
});
|
||
}
|
||
|
||
// video equirect layer (equirect via XRMediaBinding, or WebGL fallback)
|
||
function createVideoLayer(session) {
|
||
var video = S.video;
|
||
if (!video) return null;
|
||
var useMedia = NS.caps.hasEquirectLayer;
|
||
if (!useMedia) { log('XRMediaBinding.createEquirectLayer unavailable'); return null; }
|
||
var opts = {
|
||
space: S.refSpace,
|
||
layout: NS.stereoLayout(),
|
||
centralHorizontalAngle: NS.centralAngle(),
|
||
upperVerticalAngle: Math.PI / 2,
|
||
lowerVerticalAngle: -Math.PI / 2
|
||
};
|
||
// ADDENDUM 8: the picture's aim. radius stays 0 (content at infinity), so this
|
||
// is a pure rotation about the eye point.
|
||
// 'space' -> the rotation lives in an offset reference space, which a layer
|
||
// pose is recomputed from every frame (live updates).
|
||
// 'rebuild' -> the rotation is supplied as the creation transform, which is
|
||
// the only form this runtime honours.
|
||
// The transform must NOT be baked in for the transform strategy: the probe
|
||
// writes to it right after creation, and baking it in as well would apply the
|
||
// aim twice. Only the rebuild strategy bakes it in, because a fresh layer is the
|
||
// only surviving form of the write it can rely on.
|
||
if (videoRotStrategy() === 'rebuild') {
|
||
var t = videoAngleTransform();
|
||
if (t) opts.transform = t;
|
||
}
|
||
var layer = new XRMediaBinding(session).createEquirectLayer(video, opts);
|
||
log('video layer', JSON.stringify({ layout: opts.layout, central: opts.centralHorizontalAngle,
|
||
yaw: Math.round(S.videoYaw * 180 / Math.PI), pitch: Math.round(S.videoPitch * 180 / Math.PI) }));
|
||
return layer;
|
||
}
|
||
|
||
// frame reference kept for select handling; re-installed after every
|
||
// session teardown because clearFrameHooks() would otherwise remove it.
|
||
var frameTracker = function (frame) { S.lastFrame = frame; };
|
||
function installFrameTracker() { NS.addFrameHook(frameTracker); }
|
||
|
||
function onSessionEnd() {
|
||
log('session ended', S.stats);
|
||
S.ending = true; // stop pushRenderState from racing the teardown
|
||
stopRafPump();
|
||
try { if (NS.Subtitles) NS.Subtitles.destroy(); } catch (e) {}
|
||
try { if (NS.Controls) NS.Controls.destroy(); } catch (e) {}
|
||
try { if (NS.Hands) NS.Hands.destroy(); } catch (e) {}
|
||
NS.clearLayers();
|
||
NS.clearFrameHooks();
|
||
installFrameTracker();
|
||
S.lastFrame = null;
|
||
grab = null;
|
||
S.stats.grab = 'reset';
|
||
viewDrag = null;
|
||
cancelVideoRebuild();
|
||
videoSpaceCache = null;
|
||
S.videoTransformLive = undefined;
|
||
S.videoLayer = null;
|
||
S.videoYaw = 0;
|
||
S.videoPitch = 0;
|
||
if (NS.Controls && NS.Controls.hold) { try { NS.Controls.hold(false); } catch (e) {} }
|
||
S.ctlLock = false;
|
||
panelPose = null;
|
||
pointer = { visible: false, beam: false, source: 'none', origin: { x: 0, y: 0, z: 0 }, dir: { x: 0, y: 0, z: -1 }, hit: null };
|
||
activeSrcKey = null; // ADDENDUM 11: fresh hand choice every session
|
||
lastActivateByKey = {};
|
||
S.pointer = null;
|
||
S.session = null;
|
||
S.refSpace = null;
|
||
S.viewerSpace = null;
|
||
NS.session = null; NS.refSpace = null; NS.viewerSpace = null;
|
||
S.ending = false;
|
||
if (!panel && NS.Subtitles) { try { NS.Subtitles.restore(); } catch (e) {} }
|
||
setMsg('');
|
||
try { if (S.video) S.video.play().catch(function () {}); } catch (e) {}
|
||
}
|
||
|
||
/* ================================================================== *
|
||
* input
|
||
* ================================================================== */
|
||
// Unified pointer: hands and controllers both expose targetRaySpace, so one
|
||
// code path serves both. Everything is computed in VIEWER space, which is also
|
||
// where the head-locked control quad lives, so the beam quad can use the ray
|
||
// coordinates directly.
|
||
var pointer = {
|
||
visible: false,
|
||
source: 'gaze',
|
||
origin: { x: 0, y: 0, z: 0 },
|
||
dir: { x: 0, y: 0, z: -1 },
|
||
hit: { nx: 0.5, ny: 0.5 }
|
||
};
|
||
|
||
// Quads expose width/height; equirect layers do not. Find the control quad
|
||
// without depending on a specific property name on the Controls module.
|
||
function controlsLayer() {
|
||
if (!NS.Controls) return null;
|
||
if (NS.Controls.layer) return NS.Controls.layer;
|
||
if (typeof NS.Controls.getLayer === 'function') {
|
||
try { var l = NS.Controls.getLayer(); if (l) return l; } catch (e) {}
|
||
}
|
||
var all = NS.getLayers();
|
||
for (var i = all.length - 1; i >= 0; i--) {
|
||
if (typeof all[i].width === 'number' && typeof all[i].height === 'number') return all[i];
|
||
}
|
||
return null;
|
||
}
|
||
|
||
// Intersect an input source's ray with the head-locked control quad.
|
||
// Returns { origin, dir, hit:{nx,ny}, distance } all in viewer space, or null.
|
||
// Raw pointer ray for an input source, in LOCAL space. The control panel now
|
||
// lives in local space too (ADDENDUM 5), so the ray and the panel share one
|
||
// coordinate system and no per-frame conversion is needed.
|
||
function rayOf(frame, src) {
|
||
if (!NS.refSpace || !src || !src.targetRaySpace) return null;
|
||
var pose;
|
||
try { pose = frame.getPose(src.targetRaySpace, NS.refSpace); } catch (e) { return null; }
|
||
if (!pose) return null;
|
||
|
||
var o = pose.transform.position;
|
||
var q = pose.transform.orientation;
|
||
// forward = -Z rotated by q
|
||
var x = q.x, y = q.y, z = q.z, w = q.w;
|
||
var dx = -(2 * (x * z + w * y));
|
||
var dy = -(2 * (y * z - w * x));
|
||
var dz = -(1 - 2 * (x * x + y * y));
|
||
var len = Math.sqrt(dx * dx + dy * dy + dz * dz) || 1;
|
||
return {
|
||
origin: { x: o.x, y: o.y, z: o.z },
|
||
dir: { x: dx / len, y: dy / len, z: dz / len }
|
||
};
|
||
}
|
||
|
||
// The gaze ray: the head's forward direction in LOCAL space.
|
||
//
|
||
// Hands use this instead of their own targetRaySpace. Meta documents the hand
|
||
// ray as an EMULATED ray, and on device it measured (-0.874, 0.461, -0.154) -
|
||
// 87 % sideways while the panel was straight ahead - so aiming with it lands
|
||
// somewhere the user is not looking. Gaze + pinch is Meta's documented
|
||
// recommendation for hands and needs no ray accuracy at all.
|
||
function gazeRay(frame) {
|
||
if (!NS.refSpace || !NS.viewerSpace) return null;
|
||
var pose;
|
||
try { pose = frame.getPose(NS.viewerSpace, NS.refSpace); } catch (e) { return null; }
|
||
if (!pose) return null;
|
||
var o = pose.transform.position;
|
||
var q = pose.transform.orientation;
|
||
var x = q.x, y = q.y, z = q.z, w = q.w;
|
||
var dx = -(2 * (x * z + w * y));
|
||
var dy = -(2 * (y * z - w * x));
|
||
var dz = -(1 - 2 * (x * x + y * y));
|
||
var len = Math.sqrt(dx * dx + dy * dy + dz * dz) || 1;
|
||
return {
|
||
origin: { x: o.x, y: o.y, z: o.z },
|
||
dir: { x: dx / len, y: dy / len, z: dz / len }
|
||
};
|
||
}
|
||
|
||
/* ---------------- pose helpers (ADDENDUM 5) ---------------- *
|
||
* The panel can carry a yaw now (lazy follow) and the subtitle strip still
|
||
* lives in VIEWER space while the pointer rays are computed in LOCAL space, so
|
||
* the hit test can no longer assume an axis-aligned plane in the ray's own
|
||
* frame. Everything below transforms the ray into the LAYER's own space.
|
||
*/
|
||
function qMul(a, b) {
|
||
return {
|
||
x: a.w * b.x + a.x * b.w + a.y * b.z - a.z * b.y,
|
||
y: a.w * b.y - a.x * b.z + a.y * b.w + a.z * b.x,
|
||
z: a.w * b.z + a.x * b.y - a.y * b.x + a.z * b.w,
|
||
w: a.w * b.w - a.x * b.x - a.y * b.y - a.z * b.z
|
||
};
|
||
}
|
||
|
||
function qRot(q, v) {
|
||
var ux = q.x, uy = q.y, uz = q.z, s = q.w;
|
||
var tx = 2 * (uy * v.z - uz * v.y);
|
||
var ty = 2 * (uz * v.x - ux * v.z);
|
||
var tz = 2 * (ux * v.y - uy * v.x);
|
||
return {
|
||
x: v.x + s * tx + (uy * tz - uz * ty),
|
||
y: v.y + s * ty + (uz * tx - ux * tz),
|
||
z: v.z + s * tz + (ux * ty - uy * tx)
|
||
};
|
||
}
|
||
|
||
// Pose of a layer expressed in NS.refSpace, composing the layer space's pose
|
||
// with the layer's own transform.
|
||
function layerPoseInLocal(frame, layer) {
|
||
var t = layer.transform || null;
|
||
var tPos = t ? t.position : { x: 0, y: 0, z: 0 };
|
||
var tQuat = t ? t.orientation : { x: 0, y: 0, z: 0, w: 1 };
|
||
if (!layer.space || layer.space === NS.refSpace) {
|
||
return { p: { x: tPos.x, y: tPos.y, z: tPos.z }, q: tQuat };
|
||
}
|
||
var lp;
|
||
try { lp = frame.getPose(layer.space, NS.refSpace); } catch (e) { return null; }
|
||
if (!lp) return null;
|
||
var off = qRot(lp.transform.orientation, tPos);
|
||
return {
|
||
p: {
|
||
x: lp.transform.position.x + off.x,
|
||
y: lp.transform.position.y + off.y,
|
||
z: lp.transform.position.z + off.z
|
||
},
|
||
q: qMul(lp.transform.orientation, tQuat)
|
||
};
|
||
}
|
||
|
||
// Widen the acceptance window so a near-miss still counts as a press: the
|
||
// finger ray is good but not surgical, and a missed press looks like a
|
||
// completely dead UI.
|
||
function panelHitWide(frame, ray, layer) {
|
||
return panelHit(frame, ray, layer, 0.25, 0.6);
|
||
}
|
||
|
||
// Intersect a LOCAL-space ray with a layer's plane and return panel-space
|
||
// normalised coordinates. `padN`/`padY` widen the acceptance window beyond the
|
||
// quad (0..1), which is needed for the thin subtitle strip.
|
||
function panelHit(frame, ray, layer, padN, padY) {
|
||
if (!ray || !layer || !NS.refSpace) return null;
|
||
var pose = layerPoseInLocal(frame, layer);
|
||
if (!pose) return null;
|
||
|
||
// ray -> panel space (translate by -p, then rotate by q^-1)
|
||
var o = qRot({ x: -pose.q.x, y: -pose.q.y, z: -pose.q.z, w: pose.q.w },
|
||
{ x: ray.origin.x - pose.p.x, y: ray.origin.y - pose.p.y, z: ray.origin.z - pose.p.z });
|
||
var d = qRot({ x: -pose.q.x, y: -pose.q.y, z: -pose.q.z, w: pose.q.w }, ray.dir);
|
||
|
||
if (Math.abs(d.z) < 1e-6) return null;
|
||
var tt = -o.z / d.z; // the layer plane is z = 0 in panel space
|
||
if (tt <= 0) return null;
|
||
|
||
var px = o.x + d.x * tt;
|
||
var py = o.y + d.y * tt;
|
||
var hw = (layer.width || 1) / 2;
|
||
var hh = (layer.height || 0.2) / 2;
|
||
var nx = (px + hw) / (hw * 2);
|
||
var ny = (hh - py) / (hh * 2);
|
||
var pn = (padN == null) ? 0.1 : padN;
|
||
var pv = (padY == null) ? 0.4 : padY;
|
||
if (nx < -pn || nx > 1 + pn || ny < -pv || ny > 1 + pv) return null;
|
||
return {
|
||
nx: NS.clamp(nx, 0, 1),
|
||
ny: NS.clamp(ny, 0, 1),
|
||
distance: tt,
|
||
inBounds: (nx >= 0 && nx <= 1 && ny >= 0 && ny <= 1)
|
||
};
|
||
}
|
||
|
||
// ADDENDUM 7: the "index finger ray" option was removed - the system ray is
|
||
// the only mode (an index-finger ray carries fingertip jitter straight into
|
||
// the cursor, and nobody wants to point with one finger).
|
||
|
||
// Identity for an input source that survives the runtime re-listing it.
|
||
function srcKey(src) {
|
||
if (!src) return null;
|
||
return (src.hand ? 'hand' : 'ctrl') + ':' + (src.handedness || '?');
|
||
}
|
||
|
||
function candByKey(cands, key) {
|
||
for (var i = 0; i < cands.length; i++) if (cands[i].key === key) return cands[i];
|
||
return null;
|
||
}
|
||
|
||
function candByHand(cands, hand) {
|
||
for (var i = 0; i < cands.length; i++) if (cands[i].hand === hand) return cands[i];
|
||
return null;
|
||
}
|
||
|
||
// ADDENDUM 12 — pure: choose which input source owns the pointer.
|
||
// cands [{ key, hand, onPanel, ... }] in runtime order
|
||
// preferKey the source that is acting right now (select / gamepad press)
|
||
// ownerKey the source that owns the pointer (last accepted press)
|
||
// prefHand the dominant hand ('right' | 'left') — the default owner
|
||
//
|
||
// The acting source OWNS the shot: if it cannot be resolved the pick is null and
|
||
// the press MISSES (never another hand's position). Otherwise:
|
||
// owner aiming -> preferred hand aiming -> any hand aiming -> owner ->
|
||
// preferred hand -> first.
|
||
// A mouse-style single cursor: exactly ONE hand ever drives the guide line.
|
||
function pickPointerCand(cands, preferKey, ownerKey, prefHand) {
|
||
if (preferKey) return candByKey(cands, preferKey);
|
||
if (!cands || !cands.length) return null;
|
||
var owner = ownerKey ? candByKey(cands, ownerKey) : null;
|
||
var pref = prefHand ? candByHand(cands, prefHand) : null;
|
||
if (owner && owner.onPanel) return owner;
|
||
if (pref && pref.onPanel) return pref;
|
||
for (var i = 0; i < cands.length; i++) if (cands[i].onPanel) return cands[i];
|
||
if (owner) return owner;
|
||
if (pref) return pref;
|
||
return cands[0];
|
||
}
|
||
|
||
// When the source that started a hold disappears (hand tracking lost), the hold
|
||
// would otherwise stick forever because no release event ever arrives.
|
||
function dropStaleHolds() {
|
||
var srcs = (S.session && S.session.inputSources) ? S.session.inputSources : [];
|
||
var i;
|
||
function present(src) {
|
||
for (i = 0; i < srcs.length; i++) if (srcs[i] === src) return true;
|
||
return false;
|
||
}
|
||
if (grab && !present(grab.src)) endGrab();
|
||
if (viewDrag && !present(viewDrag.src)) endViewDrag();
|
||
}
|
||
|
||
// ADDENDUM 11: which hand owns the pointer.
|
||
var activeSrcKey = null;
|
||
|
||
function updatePointer(frame, preferSrc) {
|
||
if (!S.session) return;
|
||
var next = {
|
||
visible: false, beam: false, source: 'none',
|
||
origin: { x: 0, y: 0, z: 0 }, dir: { x: 0, y: 0, z: -1 },
|
||
hit: null, distance: null
|
||
};
|
||
var srcs = S.session.inputSources ? Array.prototype.slice.call(S.session.inputSources) : [];
|
||
var layer = controlsLayer();
|
||
|
||
// Use the SYSTEM ray for everything - including hands.
|
||
//
|
||
// Meta's WebXR Hands doc: "the targetRaySpace for hands is populated with an
|
||
// emulated ray that matches the behaviors of the pointing ray of hands
|
||
// present in Meta Horizon Home". That emulated ray is STABILISED by the
|
||
// platform; an index-finger ray computed from joint poses carries the
|
||
// per-frame fingertip jitter straight into the cursor, so its landing point
|
||
// is visibly less steady. Meta's own samples use targetRaySpace too.
|
||
//
|
||
// ADDENDUM 11: with two hands BOTH report a tracked-pointer ray, and the
|
||
// runtime's order is not "the hand the user is using". Taking the first one
|
||
// pinned the guide line to one hand AND made the OTHER hand's select land
|
||
// wherever the pinned hand pointed. The pointer now follows, in order:
|
||
// 1. the source acting right now (select / gamepad press),
|
||
// 2. the last acting source, while it is aiming at the panel,
|
||
// 3. any source aiming at the panel,
|
||
// 4. the last acting source, then the first one.
|
||
var cands = [];
|
||
for (var i = 0; i < srcs.length; i++) {
|
||
var src = srcs[i];
|
||
if (src.targetRayMode !== 'tracked-pointer') continue;
|
||
var ray = rayOf(frame, src);
|
||
if (!ray) continue;
|
||
var hit = null;
|
||
try { hit = panelHit(frame, ray, layer, 0.25, 0.6); } catch (e) { hit = null; }
|
||
cands.push({
|
||
src: src, ray: ray, hit: hit, key: srcKey(src), hand: src.handedness,
|
||
onPanel: !!(hit && hit.inBounds)
|
||
});
|
||
}
|
||
|
||
var pick = pickPointerCand(cands, preferSrc ? srcKey(preferSrc) : null, activeSrcKey, S.handPref);
|
||
|
||
if (pick) {
|
||
activeSrcKey = pick.key;
|
||
next = {
|
||
visible: true,
|
||
beam: true,
|
||
source: (pick.src.hand ? 'hand' : 'controller') + ':' + pick.src.handedness,
|
||
origin: pick.ray.origin,
|
||
dir: pick.ray.dir,
|
||
// ADDENDUM 11b: only a hit that is really ON the panel becomes the
|
||
// cursor. panelHit() clamps an off-panel intersection to the border, and
|
||
// drawing the cursor at that clamped point while the beam ended at the
|
||
// true intersection made the two drift apart - the further off the panel
|
||
// the ray pointed, the worse the gap. `distance` is still published so the
|
||
// beam still reaches the panel plane.
|
||
hit: (pick.hit && pick.hit.inBounds) ? { nx: pick.hit.nx, ny: pick.hit.ny } : null,
|
||
distance: pick.hit ? pick.hit.distance : null,
|
||
pressed: performance.now() < pressUntil
|
||
};
|
||
}
|
||
|
||
pointer = next;
|
||
S.pointer = next;
|
||
|
||
if (NS.Controls && NS.Controls.setPointer) {
|
||
try { NS.Controls.setPointer(next); } catch (e) {}
|
||
}
|
||
S.stats.activeSrc = next.visible ? (activeSrcKey || '(none)') : '(none)';
|
||
S.stats.handPref = S.handPref || 'right';
|
||
S.stats.pointer = next.source +
|
||
(next.hit ? ' HIT ' + next.hit.nx.toFixed(2) + ',' + next.hit.ny.toFixed(2) : ' (no hit)');
|
||
S.stats.inputSources = srcs.map(function (s) {
|
||
var gp = s.gamepad;
|
||
var padInfo = '';
|
||
if (gp && gp.buttons) {
|
||
var down = [];
|
||
for (var b = 0; b < gp.buttons.length; b++) {
|
||
if (gp.buttons[b] && (gp.buttons[b].pressed || gp.buttons[b].value > 0.5)) down.push(b);
|
||
}
|
||
padInfo = ':pad(' + gp.buttons.length + ')' + (down.length ? '@' + down.join(',') : '');
|
||
}
|
||
return (s.hand ? 'hand' : 'ctrl') + ':' + s.handedness + padInfo;
|
||
}).join(' ') || '(none)';
|
||
}
|
||
|
||
/* ================================================================== *
|
||
* ADDENDUM 8 — panning the video window
|
||
*
|
||
* The video layer is an equirect sphere with radius 0, i.e. the content sits at
|
||
* infinity. That has an important consequence: only the sphere's ORIENTATION
|
||
* decides which direction you see, and translating it does nothing. So panning
|
||
* the picture is purely `layer.transform`'s rotation, which rotates the content
|
||
* about the eye point — no parallax, no swimming when the head moves, and no
|
||
* decode/upload cost (8K HEVC DirectPlay is untouched).
|
||
*
|
||
* Only the video rotates. The control panel lives at a fixed pose in refSpace
|
||
* and the subtitles are head-locked in viewer space, so the UI stays in front of
|
||
* the user while the picture moves.
|
||
* ================================================================== */
|
||
var VIDEO_PITCH_LIMIT = Math.PI / 2 - 0.02; // "unlimited", minus a gimbal guard
|
||
|
||
function wrapYaw(a) {
|
||
while (a > Math.PI) a -= Math.PI * 2;
|
||
while (a < -Math.PI) a += Math.PI * 2;
|
||
return a;
|
||
}
|
||
|
||
function clampPitch(p) { return NS.clamp(p, -VIDEO_PITCH_LIMIT, VIDEO_PITCH_LIMIT); }
|
||
|
||
// q = qy(-yaw) * qx(pitch): pitch about the content's own X first, then yaw about
|
||
// the world Y — the standard look-around composition. The yaw sign is flipped so
|
||
// the STATE reads the way the user thinks about it: +yaw = the picture has been
|
||
// moved to the RIGHT, +pitch = UP (so a scene that used to need a physical
|
||
// head-down is brought up to eye level).
|
||
//
|
||
// The window's aim direction (the layer's local -Z) then works out to
|
||
// (cos p · sin yaw, sin p, -cos p · cos yaw)
|
||
// and the drag adds the ray's own yaw/pitch deltas directly, so the content point
|
||
// under the cursor stays under the cursor.
|
||
function videoQuat(yaw, pitch) {
|
||
var qy = { x: 0, y: Math.sin(-yaw / 2), z: 0, w: Math.cos(-yaw / 2) };
|
||
var qx = { x: Math.sin(pitch / 2), y: 0, z: 0, w: Math.cos(pitch / 2) };
|
||
return qMul(qy, qx);
|
||
}
|
||
|
||
function videoOrigin() { return { x: 0, y: 0, z: 0 }; }
|
||
|
||
function videoAngleTransform() {
|
||
if (typeof XRRigidTransform === 'undefined') return undefined;
|
||
return new XRRigidTransform(videoOrigin(), videoQuat(S.videoYaw, S.videoPitch));
|
||
}
|
||
|
||
function updateVideoAngleStats() {
|
||
S.stats.videoYaw = Math.round(S.videoYaw * 180 / Math.PI);
|
||
S.stats.videoPitch = Math.round(S.videoPitch * 180 / Math.PI);
|
||
S.stats.videoRotMode = videoRotStrategy();
|
||
}
|
||
|
||
/* ------------------------------------------------------------------
|
||
* HOW the aim is applied. Measured on the Quest 3 browser:
|
||
*
|
||
* * A transform passed to createEquirectLayer IS honoured -> rebuilding the
|
||
* media layer visibly moves the picture. That is the only reason the first
|
||
* version appeared to work "on release".
|
||
* * Writing layer.transform AFTER creation is ignored: the drag updated the
|
||
* angle every frame but nothing moved until the layer was rebuilt.
|
||
*
|
||
* So the primary strategy changes the layer's SPACE instead. getOffsetReference-
|
||
* Space is the recentering path every WebXR app uses for teleport/roomscale, and
|
||
* a layer's pose is recomputed from its space each frame, so assigning a rotated
|
||
* offset space rotates the picture live:
|
||
*
|
||
* layer.space = refSpace.getOffsetReferenceSpace(XRRigidTransform(0, q))
|
||
*
|
||
* The fallback ('rebuild') recreates the media layer with the transform supplied
|
||
* at creation. It is throttled, done OUTSIDE the XR frame callback, and destroys
|
||
* the old layer BEFORE creating the replacement - doing it inline with two
|
||
* XRMediaBinding layers alive on the same <video> crashed the browser.
|
||
* ------------------------------------------------------------------ */
|
||
function videoRotStrategy() {
|
||
if (S.videoRotMode === 'rebuild') return 'rebuild'; // manual escape hatch
|
||
if (S.videoRotMode === 'space') return 'space'; // manual
|
||
return S.videoTransformLive === false ? 'space' : 'transform';
|
||
}
|
||
|
||
// Is a post-creation `layer.transform` write honoured?
|
||
//
|
||
// Compare the POSITION, not the orientation. The first version compared
|
||
// orientation components and was sign/normalisation sensitive, so a perfectly
|
||
// good runtime could look read-only - and that false negative is what made the
|
||
// drag fall back to rebuilding the layer. Positions are exact.
|
||
function probeVideoTransformWritable(layer) {
|
||
if (!layer || typeof XRRigidTransform === 'undefined') return false;
|
||
var marker = { x: 0.123, y: -0.456, z: 0.789 };
|
||
try {
|
||
layer.transform = new XRRigidTransform(marker, { x: 0, y: 0, z: 0, w: 1 });
|
||
var p = layer.transform && layer.transform.position;
|
||
var okPos = !!p && Math.abs(p.x - marker.x) < 1e-6 &&
|
||
Math.abs(p.y - marker.y) < 1e-6 && Math.abs(p.z - marker.z) < 1e-6;
|
||
return okPos;
|
||
} catch (e) {
|
||
return false;
|
||
}
|
||
}
|
||
|
||
var videoSpaceCache = null;
|
||
|
||
function videoSpaceFor(yaw, pitch, force) {
|
||
if (typeof XRRigidTransform === 'undefined' || !S.refSpace) return null;
|
||
if (typeof S.refSpace.getOffsetReferenceSpace !== 'function') return null;
|
||
var c = videoSpaceCache;
|
||
if (!force && c && Math.abs(c.yaw - yaw) < 1e-4 && Math.abs(c.pitch - pitch) < 1e-4) return c.space;
|
||
// A throwing getOffsetReferenceSpace must not escape: this runs inside the XR
|
||
// frame loop, where an exception would stop the loop and freeze the session.
|
||
var sp;
|
||
try {
|
||
sp = S.refSpace.getOffsetReferenceSpace(
|
||
new XRRigidTransform(videoOrigin(), videoQuat(yaw, pitch)));
|
||
} catch (e) {
|
||
S.stats.videoSpace = 'throw: ' + ((e && e.message) || e);
|
||
return null;
|
||
}
|
||
if (!sp) return null;
|
||
videoSpaceCache = { space: sp, yaw: yaw, pitch: pitch };
|
||
return sp;
|
||
}
|
||
|
||
var REBUILD_MIN_MS = 120; // rebuild strategy: at most ~8 recreations/second
|
||
var rebuildTimer = null;
|
||
var lastRebuildAt = 0;
|
||
|
||
function scheduleVideoRebuild() {
|
||
if (rebuildTimer != null) return; // one pending is enough
|
||
var wait = Math.max(0, REBUILD_MIN_MS - (performance.now() - lastRebuildAt));
|
||
rebuildTimer = setTimeout(function () {
|
||
rebuildTimer = null;
|
||
lastRebuildAt = performance.now();
|
||
rebuildVideoLayerNow();
|
||
}, wait);
|
||
}
|
||
|
||
function cancelVideoRebuild() {
|
||
if (rebuildTimer != null) { clearTimeout(rebuildTimer); rebuildTimer = null; }
|
||
}
|
||
|
||
function rebuildVideoLayerNow() {
|
||
var session = S.session;
|
||
if (!session || !S.video) return;
|
||
// Destroy FIRST: two XRMediaBinding layers bound to one <video> element at the
|
||
// same time is what took the browser down.
|
||
var old = S.videoLayer;
|
||
S.videoLayer = null;
|
||
if (old) {
|
||
try { NS.removeLayer(old); } catch (e) {}
|
||
try { if (typeof old.destroy === 'function') old.destroy(); } catch (e) {}
|
||
}
|
||
try {
|
||
var fresh = createVideoLayer(session);
|
||
if (!fresh) return;
|
||
S.videoLayer = fresh;
|
||
NS.addLayer(fresh);
|
||
S.stats.videoRebuilds = (S.stats.videoRebuilds || 0) + 1;
|
||
} catch (e) {
|
||
NS.recordError('videoRebuild', e);
|
||
}
|
||
}
|
||
|
||
// Called for every angle change (and every drag frame). In 'space' mode this is a
|
||
// single space assignment; in 'rebuild' mode it schedules a throttled rebuild.
|
||
function applyVideoAngle(force) {
|
||
var layer = S.videoLayer;
|
||
if (!layer || !S.session) return;
|
||
if (videoRotStrategy() === 'transform') {
|
||
try {
|
||
layer.transform = videoAngleTransform();
|
||
if ('needsRedraw' in layer) layer.needsRedraw = true;
|
||
S.stats.videoRotMode = 'transform';
|
||
} catch (e) {
|
||
// Demote to the space path for the rest of the session.
|
||
S.videoTransformLive = false;
|
||
S.stats.videoTransformErr = (e && e.message) || String(e);
|
||
}
|
||
}
|
||
if (videoRotStrategy() === 'space') {
|
||
var sp = videoSpaceFor(S.videoYaw, S.videoPitch, !!force);
|
||
if (sp) {
|
||
try {
|
||
layer.space = sp;
|
||
S.stats.videoSpace = 'assigned';
|
||
} catch (e) {
|
||
S.stats.videoSpace = 'err: ' + ((e && e.message) || e);
|
||
S.videoRotMode = 'rebuild'; // fall back for good
|
||
}
|
||
} else {
|
||
S.stats.videoSpace = 'unavailable';
|
||
S.videoRotMode = 'rebuild';
|
||
}
|
||
}
|
||
if (videoRotStrategy() === 'rebuild') scheduleVideoRebuild();
|
||
updateVideoAngleStats();
|
||
void force;
|
||
}
|
||
|
||
function resetVideoAngle(why) {
|
||
S.videoYaw = 0;
|
||
S.videoPitch = 0;
|
||
if (S.session) { try { applyVideoAngle(true); } catch (e) {} }
|
||
S.stats.videoReset = why || 'reset';
|
||
updateVideoAngleStats();
|
||
}
|
||
|
||
/* ---------------- drag the picture (ADDENDUM 8) ----------------
|
||
* The pointer ray's own direction is the drag handle: rotating the aiming ray by
|
||
* δ rotates the picture by the same δ, so the content follows the hand 1:1 with
|
||
* no range limit (a panel-space cursor would run off the panel's edge).
|
||
* -------------------------------------------------------------- */
|
||
var viewDrag = null;
|
||
var lastPressSrc = null; // source / frame of the last accepted press, for
|
||
var lastPressFrame = null; // hold-style actions (the picture drag)
|
||
// Owner's gesture: HOLD the strip for half a second and move -> pan the picture.
|
||
// Anything else (a plain tap, or a hold with no movement) resets to dead ahead.
|
||
var VIEW_HOLD_MS = 500;
|
||
var VIEW_DEADZONE = 0.017; // ~1 deg of ray movement before a drag counts
|
||
|
||
function rayAngles(ray) {
|
||
if (!ray || !ray.dir) return null;
|
||
var d = ray.dir;
|
||
var len = Math.sqrt(d.x * d.x + d.y * d.y + d.z * d.z) || 1;
|
||
var dx = d.x / len, dy = d.y / len, dz = d.z / len;
|
||
return {
|
||
yaw: Math.atan2(dx, -dz),
|
||
pitch: Math.asin(NS.clamp(dy, -1, 1))
|
||
};
|
||
}
|
||
|
||
function beginViewDrag(src, frame) {
|
||
var ray = rayOf(frame, src);
|
||
var a = rayAngles(ray);
|
||
if (!a) { S.stats.viewDrag = 'no ray'; return; }
|
||
viewDrag = {
|
||
src: src, key: srcKey(src), t0: performance.now(), armed: false,
|
||
a0: null, yaw0: S.videoYaw, pitch0: S.videoPitch, moved: false
|
||
};
|
||
S.stats.viewDrag = 'pressed (hold ' + VIEW_HOLD_MS + 'ms to pan)';
|
||
if (NS.Controls && NS.Controls.hold) { try { NS.Controls.hold(true); } catch (e) {} }
|
||
}
|
||
|
||
function updateViewDrag(frame) {
|
||
if (!viewDrag || !frame) return;
|
||
var ray = rayOf(frame, viewDrag.src);
|
||
if (!ray) return;
|
||
var a = rayAngles(ray);
|
||
if (!a) return;
|
||
|
||
if (!viewDrag.armed) {
|
||
// Still inside the hold window: this may yet turn out to be a reset tap.
|
||
if (performance.now() - viewDrag.t0 < VIEW_HOLD_MS) return;
|
||
// Armed. Measure deltas from HERE, not from the press, so the half second the
|
||
// user spent holding does not fling the picture.
|
||
viewDrag.armed = true;
|
||
viewDrag.a0 = a;
|
||
S.stats.viewDrag = 'armed (drag to pan)';
|
||
return;
|
||
}
|
||
|
||
var dyaw = wrapYaw(a.yaw - viewDrag.a0.yaw);
|
||
var dpitch = a.pitch - viewDrag.a0.pitch;
|
||
if (Math.abs(dyaw) < VIEW_DEADZONE && Math.abs(dpitch) < VIEW_DEADZONE) return;
|
||
var ny = wrapYaw(viewDrag.yaw0 + dyaw);
|
||
var np = clampPitch(viewDrag.pitch0 + dpitch);
|
||
if (Math.abs(ny - S.videoYaw) < 1e-4 && Math.abs(np - S.videoPitch) < 1e-4) return;
|
||
viewDrag.moved = true;
|
||
S.videoYaw = ny;
|
||
S.videoPitch = np;
|
||
applyVideoAngle(false);
|
||
}
|
||
|
||
// Release. A press that never panned is a reset (owner: "按住 0.5s 后拖动才是
|
||
// 拖动,否则算复位"), which also means a hold with no movement resets.
|
||
function endViewDrag() {
|
||
if (!viewDrag) return;
|
||
var moved = viewDrag.moved;
|
||
viewDrag = null;
|
||
if (NS.Controls && NS.Controls.hold) { try { NS.Controls.hold(false); } catch (e) {} }
|
||
applyVideoAngle(true); // settle the final angle
|
||
if (moved) {
|
||
S.stats.viewDrag = 'ended (moved)';
|
||
return;
|
||
}
|
||
resetVideoAngle('tap');
|
||
S.stats.viewDrag = 'reset (tap)';
|
||
}
|
||
|
||
/* ================================================================== *
|
||
* Fixed panel pose
|
||
*
|
||
* The panel simply floats at a fixed spot in front of the session origin:
|
||
* lower middle of the view. It does NOT follow the head at all - no lazy
|
||
* follow, no gaze tracking, no re-centring. Identity rotation, matching the
|
||
* orientation that was verified working when the panel lived in viewer space.
|
||
* ================================================================== */
|
||
var PANEL_Y = -0.34;
|
||
var PANEL_Z = -1.45;
|
||
var panelPose = null;
|
||
|
||
function updatePanelPose(frame) {
|
||
if (panelPose) return; // set once; never moves again
|
||
var layer = controlsLayer();
|
||
if (!layer) return;
|
||
panelPose = { x: 0, y: PANEL_Y, z: PANEL_Z };
|
||
try {
|
||
layer.transform = new XRRigidTransform({ x: 0, y: PANEL_Y, z: PANEL_Z });
|
||
S.stats.panelPose = '0,' + PANEL_Y + ',' + PANEL_Z + ' (fixed)';
|
||
} catch (e) {
|
||
if (!S.stats.panelPoseErr) S.stats.panelPoseErr = String((e && e.message) || e);
|
||
}
|
||
}
|
||
|
||
// ADDENDUM 7: selectstart used to show() the bar unconditionally — the second,
|
||
// independent reason the bar appeared on every pinch. Summoning is now decided
|
||
// in pointerDown() from the actual aim, so this listener is gone.
|
||
|
||
/* ------------------------------------------------------------------
|
||
* Activation.
|
||
*
|
||
* The platform already does gesture recognition for us, so we do NOT compute
|
||
* thumb/index joint distances ourselves:
|
||
* - controllers and (on some builds) hands fire selectstart/select/selectend
|
||
* - Meta documents hand pinch as "implemented in the emulated Gamepad
|
||
* attribute for the hand as a button press", which does NOT always raise a
|
||
* select event - so the gamepad buttons must be polled as well.
|
||
* Palm pinch is reserved by the system (left hand = menu, leaves the session),
|
||
* so only the ordinary pinch/trigger reaches us.
|
||
* ------------------------------------------------------------------ */
|
||
var ACTIVATE_COOLDOWN_MS = 250;
|
||
var lastActivate = 0;
|
||
// ADDENDUM 11b: the cooldown is per INPUT SOURCE, not global. With hands, a
|
||
// stray gamepad edge from one hand used to swallow the other hand's `select`
|
||
// within 250 ms, so the click silently used the first hand's position.
|
||
var lastActivateByKey = {};
|
||
var padPrev = new WeakMap();
|
||
|
||
/* ---------------- grab & drag the subtitle strip ---------------- *
|
||
* The strip is a head-locked quad in viewer space, so the ray and the strip
|
||
* share one coordinate system - no world-space math is needed.
|
||
* y follows the drag on a FIXED reference plane
|
||
* z follows the hand moving toward/away (pulling a real object closer)
|
||
* ---------------------------------------------------------------- */
|
||
var grab = null;
|
||
|
||
function subtitleStripLayer() {
|
||
if (!NS.Subtitles || typeof NS.Subtitles.getLayer !== 'function') return null;
|
||
try { return NS.Subtitles.getLayer(); } catch (e) { return null; }
|
||
}
|
||
|
||
// Returns a hit on the subtitle strip, or null.
|
||
//
|
||
// Two deliberate restrictions, both needed because the strip occupies the
|
||
// lower centre of the view by default:
|
||
// 1. only a REAL quad hit counts (no padding)
|
||
// 2. only the central 40% counts - the visible text is centred in the quad
|
||
// (the crop is letterboxed into the texture), so this is the text itself.
|
||
// Without them the strip would swallow every aim in the lower half of the view
|
||
// and shadow the control bar, which overlaps it in y.
|
||
function subtitleStripHit(frame, src) {
|
||
if (!S.subtitles || !NS.Subtitles) return null;
|
||
var layer = subtitleStripLayer();
|
||
if (!layer) return null;
|
||
var ray = rayOf(frame, src);
|
||
if (!ray) return null;
|
||
var h = panelHit(frame, ray, layer, 0.35, 2.0);
|
||
if (!h || !h.inBounds) return null;
|
||
if (h.nx < 0.30 || h.nx > 0.70 || h.ny < 0.30 || h.ny > 0.70) return null;
|
||
return { ray: ray, hit: h, layer: layer };
|
||
}
|
||
|
||
function beginGrab(src, s, frame) {
|
||
var p0 = sourcePointInViewer(frame, src);
|
||
if (!p0) { S.stats.grab = 'no source point'; return; }
|
||
grab = {
|
||
src: src,
|
||
key: srcKey(src),
|
||
p0: p0,
|
||
place0: { y: S.subPlacement.y, z: S.subPlacement.z, width: S.subPlacement.width },
|
||
moved: false
|
||
};
|
||
S.stats.grab = 'started';
|
||
S.stats.grabValue = 'y=' + S.subPlacement.y.toFixed(2) + ' z=' + S.subPlacement.z.toFixed(2);
|
||
log('grab start', S.stats.grabValue);
|
||
}
|
||
|
||
// The hand/controller position in VIEWER space. The subtitle strip is
|
||
// head-locked, so moving the strip with a hand delta in the SAME head-relative
|
||
// frame is what keeps the drag stable while the head turns.
|
||
//
|
||
// Note: with hands the aiming is gaze-based, so the drag must NOT follow the
|
||
// gaze (that would swing the strip every time the head moves). The hand does
|
||
// the dragging; the gaze only decides WHICH thing is grabbed.
|
||
function sourcePointInViewer(frame, src) {
|
||
if (!frame || !src || !NS.viewerSpace) return null;
|
||
try {
|
||
if (src.hand) {
|
||
var joint = src.hand.get('wrist') || src.hand.get('index-finger-tip');
|
||
var jp = joint ? frame.getJointPose(joint, NS.viewerSpace) : null;
|
||
if (jp) return { x: jp.transform.position.x, y: jp.transform.position.y, z: jp.transform.position.z };
|
||
return null;
|
||
}
|
||
var sp = src.gripSpace || src.targetRaySpace;
|
||
var pp = sp ? frame.getPose(sp, NS.viewerSpace) : null;
|
||
if (pp) return { x: pp.transform.position.x, y: pp.transform.position.y, z: pp.transform.position.z };
|
||
} catch (e) {}
|
||
return null;
|
||
}
|
||
|
||
function updateGrabFrame(frame) {
|
||
if (!grab || !frame) return;
|
||
var p = sourcePointInViewer(frame, grab.src);
|
||
if (!p) return;
|
||
|
||
var ny = NS.clamp(grab.place0.y + (p.y - grab.p0.y), -0.95, -0.05);
|
||
// viewer space forward is -Z, so pulling the hand back increases p.z and the
|
||
// strip comes closer - matching how pulling a real object behaves
|
||
var nz = NS.clamp(grab.place0.z + (p.z - grab.p0.z), -2.6, -0.35);
|
||
// ADDENDUM 6: with the angular lock on, pulling the strip closer keeps its
|
||
// screen position, so the hand's depth motion must also carry y by the
|
||
// locked y/z ratio of the grab start. Vertical hand motion still adds on top.
|
||
if (S.subLockAngle && grab.place0.z) {
|
||
var k0 = grab.place0.y / grab.place0.z;
|
||
ny = NS.clamp(k0 * nz + (p.y - grab.p0.y), -0.95, -0.05);
|
||
}
|
||
|
||
if (Math.abs(ny - S.subPlacement.y) < 0.001 && Math.abs(nz - S.subPlacement.z) < 0.001) return;
|
||
grab.moved = true;
|
||
S.subPlacement.y = ny;
|
||
S.subPlacement.z = nz;
|
||
try { NS.Subtitles.setPlacement({ y: ny, z: nz, width: S.subPlacement.width }); } catch (e) {}
|
||
if (NS.Subtitles && NS.Subtitles.getPlacement) { try { S.subPlacement = NS.Subtitles.getPlacement(); } catch (e) {} }
|
||
S.stats.grabValue = 'y=' + ny.toFixed(2) + ' z=' + nz.toFixed(2);
|
||
}
|
||
|
||
function endGrab() {
|
||
if (!grab) return;
|
||
var moved = grab.moved;
|
||
grab = null;
|
||
S.stats.grab = moved ? 'ended (moved)' : 'ended';
|
||
if (moved) saveSettings();
|
||
}
|
||
|
||
/* ---------------- activation ---------------- */
|
||
|
||
// ADDENDUM 7 — how the control bar is summoned:
|
||
// * pressing/aiming now only SUMMONS the bar when the ray actually lands on
|
||
// (or near) the bar. It used to appear on ANY pinch anywhere in the view,
|
||
// which made it pop up constantly.
|
||
// * the summoning press never also clicks a button, and for PANEL_ARM_MS
|
||
// after the bar appears all activations are ignored — otherwise the hand
|
||
// travelling onto the bar right after the summon triggers a random button.
|
||
// * while LOCKED, summoning shows only the padlock panel (see the padlock
|
||
// section below) — no progress bar, nothing adjustable.
|
||
var PANEL_ARM_MS = 500;
|
||
var barShownAt = -1e9;
|
||
|
||
/* ---------------- ADDENDUM 7b: the padlock ----------------
|
||
* The padlock is an ordinary button on the control panel (the `lock` action),
|
||
* so it uses the same cursor, hit test and activation path as every other
|
||
* button. Locking swaps the panel to the minimal "locked" mode: only the
|
||
* padlock is drawn and only the padlock is hit-tested. The panel then auto-hides
|
||
* like it always does; the next summon brings up the padlock alone.
|
||
*/
|
||
function toggleControlsLock() {
|
||
S.ctlLock = !S.ctlLock;
|
||
S.stats.ctlLock = S.ctlLock;
|
||
if (NS.Controls && NS.Controls.setLock) { try { NS.Controls.setLock(S.ctlLock); } catch (e) {} }
|
||
// show() re-picks the mode from the new lock state and restarts the auto-hide
|
||
// timer, so locking -> padlock-only -> auto-hide all fall out of one lifecycle.
|
||
if (NS.Controls && NS.Controls.show) { try { NS.Controls.show(); } catch (e) {} }
|
||
// Arm the panel again: the press that UNLOCKS must not also land on whatever
|
||
// button is now under the cursor.
|
||
barShownAt = performance.now();
|
||
S.stats.pressAt = (S.ctlLock ? 'LOCKED' : 'UNLOCKED') + ' (armed ' + PANEL_ARM_MS + 'ms)';
|
||
}
|
||
|
||
function pointerDown(src, frame, via) {
|
||
if (!S.session) return;
|
||
var now = performance.now();
|
||
var actKey = srcKey(src) || (via || '?');
|
||
if (now - (lastActivateByKey[actKey] || 0) < ACTIVATE_COOLDOWN_MS) return;
|
||
lastActivateByKey[actKey] = now;
|
||
lastActivate = now;
|
||
S.stats.lastActivate = (via || '?') + (src && src.handedness ? ':' + src.handedness : '');
|
||
S.stats.pressSrc = actKey;
|
||
|
||
// Captured BEFORE show(): when the control bar is already up, a pinch belongs
|
||
// to the bar, so it must not also yank the subtitles.
|
||
var barWasShown = NS.Controls && NS.Controls.isVisible ? !!NS.Controls.isVisible() : false;
|
||
|
||
// ADDENDUM 11b: attribute the shot to the acting source. If its ray cannot be
|
||
// resolved, MISS - never fall back to whatever hand the per-frame pointer was
|
||
// on, which is exactly how a right-hand pinch ended up landing on the left
|
||
// hand's position.
|
||
frame = frame || S.lastFrame;
|
||
if (src) {
|
||
var excludeKey = srcKey(src);
|
||
if (excludeKey &&
|
||
((grab && grab.key && grab.key !== excludeKey) ||
|
||
(viewDrag && viewDrag.key && viewDrag.key !== excludeKey))) {
|
||
S.stats.pressAt = 'IGNORED (other hand is holding)';
|
||
return;
|
||
}
|
||
if (frame) { try { updatePointer(frame, src); } catch (e) {} }
|
||
if (!pointer || !pointer.visible) {
|
||
S.stats.pressAt = 'MISS (acting source has no ray)';
|
||
S.stats.pressPtr = '(no ray)';
|
||
return;
|
||
}
|
||
S.stats.pressPtr = pointer.source + (pointer.hit ? ' HIT' : ' nohit');
|
||
} else if (frame) {
|
||
try { updatePointer(frame); } catch (e) {}
|
||
}
|
||
|
||
// 1) grabbing the subtitle strip wins, but only while the bar is down
|
||
if (frame && !barWasShown) {
|
||
try {
|
||
var s = subtitleStripHit(frame, src);
|
||
if (s) {
|
||
if (NS.Controls && NS.Controls.show) { try { NS.Controls.show(); } catch (e) {} }
|
||
barShownAt = now;
|
||
beginGrab(src, s, frame);
|
||
return;
|
||
}
|
||
} catch (e) { NS.recordError('grabStart', e); }
|
||
}
|
||
|
||
var onPanel = !!(pointer && pointer.visible && pointer.hit);
|
||
|
||
// Summoning is deliberately MORE forgiving than button hitting: the ray only
|
||
// has to pass near the bar to bring it up (the arming delay is what prevents
|
||
// a mis-click), while the on-bar cursor stays exact. Without this, a ray that
|
||
// grazed the bar's edge could never summon it at all.
|
||
var summonable = onPanel;
|
||
if (!summonable && frame && pointer && pointer.visible) {
|
||
try {
|
||
summonable = !!panelHit(frame, { origin: pointer.origin, dir: pointer.dir },
|
||
controlsLayer(), 0.5, 0.9);
|
||
} catch (e) { summonable = false; }
|
||
}
|
||
|
||
// 2) bar is down -> normal action, once it is armed
|
||
if (barWasShown) {
|
||
if (now - barShownAt < PANEL_ARM_MS) {
|
||
S.stats.pressAt = 'IGNORED (arming)'; // the summon is still settling
|
||
return;
|
||
}
|
||
if (!onPanel) { S.stats.pressAt = 'MISS (no hit on the panel)'; return; }
|
||
if (NS.Controls && NS.Controls.show) { try { NS.Controls.show(); } catch (e) {} } // extend auto-hide
|
||
pressUntil = now + 300;
|
||
S.stats.pressAt = pointer.hit.nx.toFixed(2) + ',' + pointer.hit.ny.toFixed(2);
|
||
// Remembered for hold-style actions (the ADDENDUM 8 picture drag), which need
|
||
// the input source and frame that started them.
|
||
lastPressSrc = src;
|
||
lastPressFrame = frame;
|
||
try { NS.Controls.handleSelect(src, frame); }
|
||
catch (e) { NS.recordError('handleSelect', e); }
|
||
return;
|
||
}
|
||
|
||
// 3) bar is up? no. Only a real aim summons it (and only when unlocked).
|
||
if (S.ctlLock) { S.stats.pressAt = 'MISS (panel locked)'; return; }
|
||
if (!summonable) { S.stats.pressAt = 'MISS (not aiming at the panel)'; return; }
|
||
if (NS.Controls && NS.Controls.show) { try { NS.Controls.show(); } catch (e) {} }
|
||
barShownAt = now;
|
||
S.stats.pressAt = 'SUMMON (armed ' + PANEL_ARM_MS + 'ms)';
|
||
}
|
||
|
||
var pressUntil = 0;
|
||
|
||
// ADDENDUM 12: only the source that STARTED a hold ends it. A release from the
|
||
// other hand used to cancel the first hand's drag (subtitle grab / picture pan).
|
||
function pointerUp(src) {
|
||
var key = srcKey(src);
|
||
if (key) {
|
||
if (grab && grab.key && grab.key !== key) return;
|
||
if (viewDrag && viewDrag.key && viewDrag.key !== key) return;
|
||
}
|
||
endGrab();
|
||
endViewDrag();
|
||
}
|
||
|
||
function pollGamepads(frame) {
|
||
if (!S.session) return;
|
||
var srcs = S.session.inputSources ? Array.prototype.slice.call(S.session.inputSources) : [];
|
||
for (var i = 0; i < srcs.length; i++) {
|
||
var src = srcs[i];
|
||
var gp = src.gamepad;
|
||
if (!gp || !gp.buttons) continue;
|
||
var pressed = false;
|
||
for (var b = 0; b < gp.buttons.length; b++) {
|
||
var bt = gp.buttons[b];
|
||
if (bt && (bt.pressed || bt.value > 0.5)) { pressed = true; break; }
|
||
}
|
||
var prev = padPrev.get(src) || false;
|
||
padPrev.set(src, pressed);
|
||
if (pressed && !prev) pointerDown(src, frame, 'gamepad');
|
||
else if (!pressed && prev) pointerUp(src);
|
||
}
|
||
}
|
||
|
||
function onSelect(ev) {
|
||
pointerDown(ev && ev.inputSource, S.lastFrame, 'select');
|
||
}
|
||
|
||
function onSelectEnd(ev) {
|
||
pointerUp(ev && ev.inputSource);
|
||
}
|
||
/* ---------------- live subtitle setters (single source of truth) ---------------- */
|
||
// state.subPlacement / subAngleDeg are re-read from the module after every
|
||
// call: with the angular lock on the module DERIVES width (and, for a depth
|
||
// change, y), which is what gets persisted.
|
||
function setSubPlacementLive(p) {
|
||
if (NS.Subtitles && NS.Subtitles.setPlacement) {
|
||
try { NS.Subtitles.setPlacement(p); } catch (e) { NS.recordError('setPlacement', e); }
|
||
if (NS.Subtitles.getPlacement) { try { S.subPlacement = NS.Subtitles.getPlacement(); } catch (e) {} }
|
||
if (NS.Subtitles.getAngleDeg) { try { S.subAngleDeg = NS.Subtitles.getAngleDeg(); } catch (e) {} }
|
||
} else {
|
||
if (typeof p.y === 'number') S.subPlacement.y = NS.clamp(p.y, -0.95, -0.05);
|
||
if (typeof p.z === 'number') S.subPlacement.z = NS.clamp(p.z, -2.6, -0.35);
|
||
if (typeof p.width === 'number') S.subPlacement.width = NS.clamp(p.width, 0.2, 4.0);
|
||
}
|
||
if (panelSync) panelSync();
|
||
saveSettings();
|
||
}
|
||
|
||
// Locked angular width, in degrees. With the lock on this is the size control.
|
||
function setSubAngleLive(deg) {
|
||
S.subAngleDeg = NS.clamp(deg, 20, 120);
|
||
if (NS.Subtitles && NS.Subtitles.setAngleDeg) {
|
||
try { NS.Subtitles.setAngleDeg(S.subAngleDeg); } catch (e) { NS.recordError('setAngleDeg', e); }
|
||
}
|
||
if (NS.Subtitles && NS.Subtitles.getPlacement) { try { S.subPlacement = NS.Subtitles.getPlacement(); } catch (e) {} }
|
||
if (NS.Subtitles && NS.Subtitles.getAngleDeg) { try { S.subAngleDeg = NS.Subtitles.getAngleDeg(); } catch (e) {} }
|
||
if (panelSync) panelSync();
|
||
saveSettings();
|
||
}
|
||
|
||
// ADDENDUM 6 switch: depth-only moves (locked) vs the old free placement.
|
||
// Extra live readers handed to the control bar. The bar is a separate module
|
||
// with its own private state, so anything it needs to DISPLAY (rather than
|
||
// mutate) has to be readable through here or through __JFVR.state.
|
||
function controlsExtra() {
|
||
return { subtitlesOn: function () { return !!S.subtitles; } };
|
||
}
|
||
|
||
function setSubOpacityLive(a) {
|
||
S.subOpacity = NS.clamp(a, 0.1, 1.0);
|
||
if (NS.Subtitles && NS.Subtitles.setOpacity) {
|
||
try { NS.Subtitles.setOpacity(S.subOpacity); } catch (e) {}
|
||
}
|
||
if (panelSync) panelSync();
|
||
saveSettings();
|
||
}
|
||
|
||
function handleControlAction(action) {
|
||
var v = S.video;
|
||
if (!action) return;
|
||
S.stats.ctlLastAction = JSON.stringify(action);
|
||
log('control action', action);
|
||
// The padlock and the subtitle actions do not need a <video>.
|
||
switch (action.type) {
|
||
case 'lock': toggleControlsLock(); break;
|
||
case 'view_drag':
|
||
if (lastPressSrc && lastPressFrame) beginViewDrag(lastPressSrc, lastPressFrame);
|
||
else S.stats.viewDrag = 'no press context';
|
||
break;
|
||
case 'sub_up': setSubPlacementLive({ y: S.subPlacement.y + 0.04 }); break;
|
||
case 'sub_down': setSubPlacementLive({ y: S.subPlacement.y - 0.04 }); break;
|
||
case 'sub_near': setSubPlacementLive({ z: S.subPlacement.z + 0.15 }); break;
|
||
case 'sub_far': setSubPlacementLive({ z: S.subPlacement.z - 0.15 }); break;
|
||
case 'sub_smaller':
|
||
if (S.subLockAngle) setSubAngleLive(S.subAngleDeg - 5);
|
||
else setSubPlacementLive({ width: S.subPlacement.width - 0.15 });
|
||
break;
|
||
case 'sub_bigger':
|
||
if (S.subLockAngle) setSubAngleLive(S.subAngleDeg + 5);
|
||
else setSubPlacementLive({ width: S.subPlacement.width + 0.15 });
|
||
break;
|
||
case 'sub_opaque_down': setSubOpacityLive(S.subOpacity - 0.05); break;
|
||
case 'sub_opaque_up': setSubOpacityLive(S.subOpacity + 0.05); break;
|
||
case 'sub_reset':
|
||
if (NS.Subtitles && NS.Subtitles.resetPlacement) { try { NS.Subtitles.resetPlacement(); } catch (e) {} }
|
||
if (NS.Subtitles && NS.Subtitles.getPlacement) { try { S.subPlacement = NS.Subtitles.getPlacement(); } catch (e) {} }
|
||
if (NS.Subtitles && NS.Subtitles.getAngleDeg) { try { S.subAngleDeg = NS.Subtitles.getAngleDeg(); } catch (e) {} }
|
||
setSubOpacityLive(0.85);
|
||
if (panelSync) panelSync();
|
||
S.stats.subResetAt = Date.now();
|
||
break;
|
||
}
|
||
if (!v) { updateDiag(); return; }
|
||
switch (action.type) {
|
||
case 'playpause':
|
||
if (v.paused) v.play().catch(function () {}); else v.pause();
|
||
break;
|
||
case 'back10':
|
||
v.currentTime = Math.max(0, v.currentTime - 10);
|
||
break;
|
||
case 'fwd10':
|
||
v.currentTime = Math.min(isFinite(v.duration) ? v.duration : v.currentTime + 10, v.currentTime + 10);
|
||
break;
|
||
case 'seek':
|
||
if (isFinite(v.duration)) v.currentTime = NS.clamp(action.value, 0, 1) * v.duration;
|
||
break;
|
||
case 'subs':
|
||
S.subtitles = !S.subtitles;
|
||
if (NS.Subtitles) {
|
||
NS.Subtitles.setMode(S.subtitles ? (S.subMode === 'off' ? 'hud' : S.subMode) : 'off');
|
||
if (S.subtitles) { try { NS.Subtitles.prepare(); } catch (e) {} }
|
||
}
|
||
saveSettings();
|
||
break;
|
||
}
|
||
updateDiag();
|
||
}
|
||
|
||
/* ================================================================== *
|
||
* boot
|
||
* ================================================================== */
|
||
var lastVideo = null;
|
||
|
||
function scan() {
|
||
var v = findVideo();
|
||
if (v === lastVideo) return;
|
||
lastVideo = v;
|
||
S.video = v;
|
||
NS.video = v;
|
||
if (v) {
|
||
log('video found', v.videoWidth + 'x' + v.videoHeight, (v.currentSrc || '').slice(0, 80));
|
||
ensureButton();
|
||
S.detected = '';
|
||
S.stats = { fps: null, frames: 0 };
|
||
applyAutoDetect().then(function () { refreshHint(); updateDiag(); });
|
||
if (NS.Controls && NS.Controls.setVideo) { try { NS.Controls.setVideo(v, controlsExtra()); } catch (e) {} }
|
||
} else {
|
||
removeButton();
|
||
}
|
||
}
|
||
|
||
function boot() {
|
||
log('boot v' + NS.version + ' | secure=' + window.isSecureContext +
|
||
' xr=' + !!navigator.xr + ' hand=' + NS.caps.hasHandTracking);
|
||
loadSettings();
|
||
// Subtitles are per-session: never restored as OFF, always ON at boot. Both
|
||
// `subtitles` and `subMode` could otherwise leave the layer uncreated.
|
||
S.subtitles = true;
|
||
if (S.subMode === 'off') S.subMode = 'hud';
|
||
S.subLayout = 'auto'; // ADDENDUM 10: the override is per-session only
|
||
try { if (NS.Subtitles && NS.Subtitles.init) NS.Subtitles.init(); } catch (e) { NS.recordError('Subtitles.init', e); }
|
||
try { if (NS.Controls && NS.Controls.init) NS.Controls.init(); } catch (e) { NS.recordError('Controls.init', e); }
|
||
try { if (NS.Hands && NS.Hands.init) NS.Hands.init(); } catch (e) { NS.recordError('Hands.init', e); }
|
||
// Watches for "the player never produced a picture" and offers the only fix
|
||
// that helps (let the server transcode). See 40-watchdog.js.
|
||
try { if (NS.Watchdog && NS.Watchdog.init) NS.Watchdog.init(); } catch (e) { NS.recordError('Watchdog.init', e); }
|
||
|
||
scan();
|
||
new MutationObserver(scan).observe(document.documentElement, { childList: true, subtree: true });
|
||
setInterval(scan, 1500);
|
||
window.addEventListener('beforeunload', function () {
|
||
if (S.session) { try { S.session.end(); } catch (e) {} }
|
||
});
|
||
// Keep the draggable button (and the panel that follows it) on screen when
|
||
// the window changes size.
|
||
window.addEventListener('resize', onViewportResize);
|
||
installFrameTracker();
|
||
|
||
window.__JFVR_STATE__ = S;
|
||
window.__JFVR_API = NS;
|
||
log('ready');
|
||
}
|
||
|
||
if (document.readyState === 'loading') {
|
||
document.addEventListener('DOMContentLoaded', boot, { once: true });
|
||
} else {
|
||
boot();
|
||
}
|
||
})();
|
||
|