Files
jellyfin_vr_web_plugin/src/00-core.js
T
cat-shark 0713f35513 hud-11: one pointer owner (dominant hand, press takes over) + hand isolation
ADDENDUM 12. The pointer was symmetric: whoever acted last owned it, and when
neither had acted the first source in runtime order won - the LEFT hand on this
runtime, so a right-handed user's guide line sat on the non-dominant hand.

- state.handPref ('right'|'left', default 'right') is the default owner. Set from the
  pre-VR panel (操作 -> 主用手), persisted. pickPointerCand(cands, preferKey,
  ownerKey, prefHand): acting source owns the shot (or MISSes), else owner aiming ->
  preferred hand aiming -> any hand aiming -> owner -> preferred hand -> first.
- A press takes the pointer over and the owner sticks while it is aiming.
- Hold exclusion: while a grab/viewDrag started by one source is active, a press from
  the other source is ignored (pressAt "IGNORED (other hand is holding)").
- Source-matched release: pointerUp(src) only ends the hold src started. It used to be
  argument-less, so either hand's release cancelled the other hand's subtitle grab or
  picture pan. grab/viewDrag now carry a key; onSelectEnd(ev) and pollGamepads pass
  their source.
- dropStaleHolds() runs each frame and ends a hold whose source left
  session.inputSources (tracking lost), so nothing sticks.

New diagnostics: stats.handPref, stats.owner.

Verified with injected synthetic input sources:
  default (right), both on panel     pointer hand:right HIT 0.78,0.50  (was hand:left)
  handPref=left, fresh session       pointer hand:left  HIT 0.22,0.50
  left press, release                owner hand:left, stays left
  right holds picture-pan, left press    "IGNORED (other hand is holding)"
  right holds picture-pan, LEFT release  viewDrag stays armed (drag survives)
  right holds picture-pan, RIGHT release viewDrag reset (tap) (drag ends)
2026-10-12 21:36:03 +08:00

356 lines
14 KiB
JavaScript

/*!
* 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
};
(function probe() {
try {
var mb = window.XRMediaBinding;
if (mb && mb.prototype) NS.caps.hasEquirectLayer = typeof mb.prototype.createEquirectLayer === 'function';
var wg = window.XRWebGLBinding;
if (wg && wg.prototype) NS.caps.hasQuadLayer = typeof wg.prototype.createQuadLayer === 'function';
} catch (e) {}
try {
var gl = document.createElement('canvas').getContext('webgl2') ||
document.createElement('canvas').getContext('webgl');
if (gl) NS.caps.maxTextureSize = gl.getParameter(gl.MAX_TEXTURE_SIZE);
} catch (e) {}
try {
['video/mp4; codecs="hev1.1.6.L183.B0"',
'video/mp4; codecs="hvc1.1.6.L183.B0"',
'video/mp4; codecs="hev1"',
'video/mp4; codecs="hvc1"'].forEach(function (cs) {
NS.caps.hevc[cs] = (typeof MediaSource !== 'undefined' && MediaSource.isTypeSupported(cs)) ||
document.createElement('video').canPlayType(cs) || 'no';
});
} catch (e) {}
if (navigator.xr && navigator.xr.isSessionSupported) {
navigator.xr.isSessionSupported('immersive-vr')
.then(function (v) { NS.caps.immersiveVR = v; })
.catch(function (e) { NS.caps.immersiveVR = 'err:' + e.message; });
}
})();
NS.capsDiag = function () {
var c = NS.caps;
return [
'origin : ' + location.origin,
'secureContext : ' + c.secureContext,
'immersive-vr : ' + c.immersiveVR,
'XRMediaBinding : ' + c.hasXRMediaBinding,
'XRWebGLBinding : ' + c.hasXRWebGLBinding,
'createQuadLayer : ' + c.hasQuadLayer,
'hand tracking API : ' + c.hasHandTracking + ' / jointSpace=' + c.hasXRJointSpace + ' / getJointPose=' + c.hasGetJointPose,
'MAX_TEXTURE_SIZE : ' + c.maxTextureSize,
'HEVC : ' + JSON.stringify(c.hevc)
].join('\n');
};
})();