hud-7: live picture pan; fix the reset crash; tap-to-reset / hold-to-drag

Reported after hud-6:
  触发复位后直接崩溃了;我拖动后,我希望画面立刻就有反应,而不是我松手之后才改变。
  按住0.5s后拖动才是拖动,否则算复位

1. Why the picture only moved on release — a bad probe
The transform-write probe compared quaternion COMPONENTS. q and -q are the same
rotation and a runtime may hand back a renormalised quaternion, so a perfectly
capable runtime could be judged "read-only". When it was, the drag took the
rebuild branch, and that branch was throttled to "on release only" — hence the
picture sitting still until the finger came up. The probe now compares the
POSITION (exact, no sign ambiguity).

2. Two mechanisms, auto-selected and mutually exclusive
   'transform' -> layer.transform = q              (one write per change)
   'space'     -> layer.space = refSpace.getOffsetReferenceSpace(q)
                     (the recentering path every WebXR app uses; a layer pose is
                      recomputed from its space each frame)
   'rebuild'   -> recreate the media layer (manual escape hatch, "兼容" in the 2D panel)
createVideoLayer() only bakes the transform in for 'rebuild' — otherwise the
probe's own write would apply the aim twice. layer.transform is never written on
the 'space' path and vice versa. stats.videoRotMode / videoTransformWritable
report what was chosen.

3. The crash — create-before-destroy
The reset created the replacement layer BEFORE destroying the old one: two
XRMediaBinding equirect layers bound to one <video> element at once, then a
second rebuild microseconds later (release rebuilt, then reset rebuilt again),
took the browser down. A rebuild now runs in a setTimeout OUTSIDE the XR frame
callback, destroys FIRST, is throttled to 120 ms, and never queues more than one.
The transform/space paths never rebuild at all.

Two robustness holes found while testing:
  * videoSpaceFor() let a throwing getOffsetReferenceSpace() escape into the frame
    loop, where an exception kills the loop for good (rAF is only re-armed at the
    end of the callback) — a frozen session. Caught now, demoting to 'rebuild'.
  * the whole per-frame body is wrapped in try/catch for the same reason.

4. The gesture the owner asked for
   press the drag strip
     released before 500 ms, no movement -> RESET to dead ahead
     held past 500 ms, then moved        -> PAN
     held past 500 ms, never moved       -> RESET
The pan reference is captured when the hold threshold passes, not at the press,
so the hold itself cannot fling the picture; a ~1 deg dead zone absorbs jitter.
The strip now reads 点一下复位 · 按住 0.5s 再拖动 (the triple-tap hint is gone).

Verified offline against the real sources by three suites (build.py --check too):
  test-gesture  the state machine above, stubbed ray/clock: quick tap resets;
                hold-without-move resets; movement inside the hold window is
                ignored; after arming a +10/+5 deg ray move gives exactly
                +10/+5 deg; a pan does not reset; dead zone respected; pitch guard
  test-mech     position probe true/false for writable/ignored; transform
                strategy writes with no rebuild; space strategy assigns the offset
                space and leaves the transform alone; unchanged angle reuses the
                cached space; rebuild queues at most one and runs
                removeLayer -> createVideoLayer -> addLayer; throwing or absent
                getOffsetReferenceSpace is reported and falls back permanently
  test-ad8      quaternion aim/wrap/guard maths, panel bands on the 1440x520
                canvas, the strip's drawn readout, hold()
Deployed as mvp-5217a3 (hud-7); confirmed live on the Quest, errors empty.
This commit is contained in:
2026-10-11 21:47:55 +08:00
parent 263e270747
commit 7f751b7eee
7 changed files with 672 additions and 161 deletions
+26 -9
View File
@@ -1,8 +1,8 @@
# Jellyfin Web VR180 — 项目交接文档
> 给新对话用。读完这份就能接手,不需要重新调查环境。
> 最后更新:已部署版本 `2e32e6`(`NS.version = hud-6`;ADDENDUM 6 字幕深度/大小解耦
> + 7 唤出门限/前面板精简 + 7b 锁按钮与「锁定」面板模式 + 8 画面拖动搬迁)
> 最后更新:已部署版本 `5217a3`(`NS.version = hud-7`;ADDENDUM 6 字幕深度/大小解耦
> + 7 唤出门限/前面板精简 + 7b 锁按钮与「锁定」面板模式 + 8/8b 画面搬迁(实时拖动))
> 已在 Quest 浏览器里实机验证模块加载 + 锁定数学(`subLockAngle/subAngleDeg`,`errors: []`);
> **VR 会话内的观感还没看过**。
@@ -285,7 +285,7 @@ WebXR 规范:需要 *transient activation*。前面不能有 `await`,否则
| 引导线(十字双光束) | ✅ 任何时候都显示 |
| 面板唤出门限 | ✅ 只有指示线指向/靠近面板时才唤出;唤出那一下不算点击;500ms 内忽略 |
| 锁按钮(面板不启动) | ✅ 面板媒体行的第五个按钮:上锁后切到「只显示锁」的最小面板,并同样自动隐藏;每次进 VR 重置为未锁 |
| 画面搬迁(180°窗口) | ✅ 面板进度条上方的拖动条:按住拖动,画面绕眼点左右上下转;连点 3 次复位;每次进 VR 回正前方 |
| 画面搬迁(180°窗口) | ✅ 面板进度条上方的拖动条:按住 0.5s 后拖动 = 搬画面;点一下 = 复位;每次进 VR 回正前方 |
| 按压反馈 | ✅ 圆环放大 1.9× + 蓝填充 + 变白,300ms |
| 字幕抓拖 | ✅ 注视选中 + 手移动拖(与视线解耦,避免转头就飘) |
| 字幕深度/大小解耦 | ✅ 默认「锁定视角尺寸」:拉近/拉远只改深度,视角大小与画面位置不变 |
@@ -412,8 +412,8 @@ subMode / subCanvasSize / subCrop / subTex / subOpacity / subPlacement
subLockAngle / subAngleDeg / subTextDeg / subYOverZ ← ADDENDUM 6
ctlSubsOn / ctlSubDist / ctlSubAngle / ctlSubLock ← 控制条实际画出的值
ctlLock / ctlMode / ctlHold / pressAt / lastActivate ← ADDENDUM 7/7b 锁、面板保持与唤出判定
videoYaw / videoPitch / videoAngleLive / videoRotMode ← ADDENDUM 8 画面搬迁
videoRebuilds / videoReset / viewDrag / viewTaps
videoYaw / videoPitch / videoRotMode / videoTransformWritable ← ADDENDUM 8/8b 画面搬迁
videoSpace / videoRebuilds / videoReset / viewDrag / frameErr
ctlHidden / ctlVisible / ctlBeamOn / ctlDraws / ctlErr
panelPose / grab / grabValue / saved
errors[] 最近错误
@@ -470,7 +470,24 @@ errors[] 最近错误
拖动用**射线自身的方向差**而不是面板上的坐标:面板坐标挪几度就出界了,射线旋转无行程限制,
而且"光标下的那个点始终在光标下",才像"抓住画面拖"。
**唯一的未知**:`XREquirectLayer.transform` 规范上可写,但运行时可能忽略。所以每会话探测一次
(写一个已知旋转再读回):`live` = 每帧写 transform(零成本);`rebuild` = 只能重建媒体层,
那就只在**创建/松手/复位**时重建,拖动过程中画面在松手后追上。见 `stats.videoRotMode` /
`videoRebuilds`。**这条必须真机验证,离线测不出来。**
### 6.15 拖动为什么一开始"松手才动",以及复位为什么崩(ADDENDUM 8b)
两个都是我的 bug,记在下面免得再犯:
1. **探测用四元数分量比较 → 假阴性。** `q` 和 `-q` 是同一个旋转,运行时还可能返回归一化后的
值,所以"写进去再读回来比 y/w"会得出错误的"只读"结论。于是拖动走到重建分支,而那个分支
被限成"只在松手时执行" → 松手才动。**现在比 `position`**(精确、无符号歧义)。
2. **复位崩溃 = 先建后毁。** 同一 `<video>` 上同时存在两个 `XRMediaBinding` 等距柱状图层,
外加微秒级连着重建两次(松手一次 + 复位一次)→ 浏览器直接挂。现在重建一律:
在 `setTimeout`(**不在 XR 帧回调里**)执行、**先毁后建**、限流 120ms、最多排一个。
`transform`/`space` 两条路根本不重建,所以正常情况走不到这条。
3. 顺手补的两个健壮性漏洞:`getOffsetReferenceSpace()` 抛错会逃进帧循环——那里抛异常等于
**永久冻住会话**(rAF 只在回调末尾续订);现在包了 try/catch 并降级到 rebuild。整个每帧
函数体也都包了 try/catch。
**机制自动二选一**:`stats.videoRotMode` = `transform`(每帧写 transform)/ `space`
(`layer.space = refSpace.getOffsetReferenceSpace(q)`)/ `rebuild`(手动兼容档)。
两者**互斥**:创建图层时只有 rebuild 档会把 transform 烘进去,否则会叠加两次旋转。
**手势**(按你的要求):`点一下复位 · 按住 0.5s 再拖动`。拖动基准取"按住满 0.5s 那一刻"的射线,
不是按下那一刻——否则那半秒会把手抖放大成甩动。1° 死区防抖。
+127
View File
@@ -814,6 +814,10 @@ running.
## Runtime risk and how it is handled
> **SUPERSEDED by ADDENDUM 8b** — the probe below compared quaternion components,
> which is sign-sensitive and produced a false "read-only" verdict; and the gesture is
> now tap = reset / hold 0.5 s = drag. See the addendum at the end of this file.
`XRCompositionLayer.transform` / `XREquirectLayer.transform` are settable in the spec,
but a runtime may ignore the write. Since the whole interaction depends on it, the
module probes once per session:
@@ -849,3 +853,126 @@ probeVideoAngleMutable(layer) // write a known rotation, read it back
(`lastPressSrc` / `lastPressFrame`) captured in `pointerDown`.
- Stats: `videoYaw`, `videoPitch`, `videoAngleLive`, `videoRotMode`, `videoRebuilds`,
`videoReset`, `viewDrag`, `viewTaps`.
---
# ADDENDUM 8b — live picture pan: two mechanisms, the crash, and the real gesture
Owner feedback on ADDENDUM 8:
> 触发复位后直接崩溃了;另外,我拖动后,我希望画面立刻就有反应,而不是我松手之后才改变。
> 按住0.5s后拖动才是拖动,否则算复位
## 1. Why the picture only moved on release
ADDENDUM 8 decided whether it could write `layer.transform` with this probe:
```js
layer.transform = new XRRigidTransform(origin, q);
var o = layer.transform.orientation;
live = Math.abs(o.y - q.y) < 1e-4 && Math.abs(o.w - q.w) < 1e-4;
```
Comparing **quaternion components** is sign-sensitive — `q` and `−q` are the same
rotation — and a runtime is free to hand back a renormalised quaternion. So the probe
could report "read-only" for a runtime that was perfectly capable of the write. When it
did, the drag path took the rebuild branch, and because that branch was throttled to
"on release only", the picture sat still until the finger came up. That is exactly what
was observed.
The probe now compares the **position**, which is exact and has no sign ambiguity:
```js
layer.transform = new XRRigidTransform({x:0.123, y:-0.456, z:0.789}, identity);
writable = layer.transform.position matches that marker;
```
## 2. Two mechanisms, chosen automatically
```js
videoRotStrategy() -> 'transform' // preferred: one cheap write per change
| 'space' // layer.space = refSpace.getOffsetReferenceSpace(q)
| 'rebuild' // manual escape hatch ("兼容" in the 2D panel)
```
- `transform` — post-creation `layer.transform` writes are honoured.
- `space` — they are not, so the aim lives in an **offset reference space** instead. A
layer's pose is recomputed from its space every frame, and `getOffsetReferenceSpace()`
is the recentering path every WebXR app uses, so this is the more likely of the two to
be fully implemented.
- The two are **mutually exclusive**: `createVideoLayer()` must NOT bake the transform in
while the probe is about to write it, or the aim would be applied twice. Only the
`rebuild` strategy bakes it in at creation.
- `layer.transform` is never written on the `space` path, and vice versa.
Switching is automatic per session; `stats.videoRotMode` reports what was chosen and
`stats.videoTransformWritable` reports the raw probe result. The 2D panel has
「画面旋转: 自动 / 兼容」 to force the rebuild path without a redeploy.
## 3. The crash
The reset path rebuilt the video layer, and it created the replacement **before**
destroying the old one. Two `XRMediaBinding` equirect layers bound to the same `<video>`
element at the same time, plus a second rebuild microseconds later (the release path
also rebuilt, then the reset rebuilt again), took the browser down.
Every rebuild now:
1. runs in a `setTimeout` **outside the XR frame callback** (never inline in a frame),
2. **destroys the old layer first**, then creates the replacement,
3. is throttled to at most one per `REBUILD_MIN_MS` (120 ms),
4. never queues more than one pending rebuild.
The `space`/`transform` strategies rebuild **not at all**, so in the normal case the
crash path is not even reachable.
Two more robustness fixes found while testing this:
- `videoSpaceFor()` used to let a throwing `getOffsetReferenceSpace()` escape. It runs
inside the XR frame loop, where an exception would stop the loop and freeze the
session. It is caught now, and the strategy falls back to `rebuild`.
- The whole per-frame body (`updatePointer` / `pollGamepads` / `updatePanelPose` /
`updateGrabFrame` / `updateViewDrag` / frame hooks) is wrapped in a try/catch. A single
bad frame could previously kill the loop: `requestAnimationFrame` is only re-armed at
the end of the callback, so any throw froze the session permanently.
## 4. The real gesture
```
press the drag strip
├─ release before 500 ms, no movement -> RESET to dead ahead
├─ held past 500 ms, then moved -> PAN (picture follows the hand)
└─ held past 500 ms, never moved -> RESET
```
- The pan reference (`a0`) is captured **at the moment the hold threshold passes**, not
at the press. Otherwise the half second the user spends holding would fling the picture
the instant dragging armed.
- A ~1° dead zone keeps hand jitter from turning a reset tap into a tiny pan.
- The strip now reads `点一下复位 · 按住 0.5s 再拖动` instead of the retired triple-tap hint.
`stats.viewDrag` walks through it: `pressed (hold 500ms to pan)` →
`armed (drag to pan)` → `ended (moved)` / `reset (tap)`.
## 5. Verification
Three offline suites, all against the real sources (`build.py --check` passes too):
`test-ad8` — quaternion + panel geometry: aim for ±yaw/±pitch, unit norm, exact
azimuth/elevation recovery, yaw wrap, pitch guard, "aimed point stays under the cursor";
panel bands on the 1440×520 canvas (strip 0–0.20, inert readout 0.20–0.30, seek
0.30–0.52, media 0.52–0.74, subtitle 0.84–1.0, no dead column); the strip's drawn
readout; `hold()`.
`test-gesture` — the state machine above, driven with a stubbed ray: quick tap resets;
hold-without-movement resets; movement inside the hold window is ignored (no fling);
after arming a +10°/+5° ray move yields exactly +10°/+5°; a real pan does not reset; the
dead zone is respected; pitch cannot pass the guard.
`test-mech` — the mechanism: the position-based probe returns true for a writable
transform and false for an ignored one; the transform strategy writes and schedules no
rebuild; the space strategy assigns an offset space, leaves the transform alone (no
double rotation) and schedules no rebuild; unchanged angles reuse the cached space; the
rebuild strategy queues at most one, and its observed order is
`removeLayer -> createVideoLayer -> addLayer`; a throwing/absent
`getOffsetReferenceSpace()` is reported and permanently falls back to `rebuild`.
+101
View File
@@ -890,6 +890,10 @@ for the duration, otherwise the panel would auto-hide mid-drag.
## The one runtime unknown
> **SUPERSEDED by ADDENDUM 8b** — this probe compared quaternion components (sign- and
> normalisation-sensitive) and so could report "read-only" on a perfectly good runtime.
> The gesture also changed: tap = reset, hold 0.5 s then drag = pan.
`XREquirectLayer.transform` is settable per spec but a runtime may ignore the write. It
is probed once per session (write a known rotation, read it back):
@@ -934,3 +938,100 @@ x sweep 0.04..0.96 at y 0.05 -> no dead column
Live Quest 3 over CDP after deploy: `__JFVR.version = 'hud-6'`,
`state.videoYaw = state.videoPitch = 0`, `errors: []`.
---
# ADDENDUM 8b — live pan, the crash, and the real gesture
## Why the picture only moved on release
ADDENDUM 8 gated the transform write on a probe that compared **quaternion components**:
```js
live = Math.abs(readBack.orientation.y - q.y) < 1e-4 && Math.abs(readBack.orientation.w - q.w) < 1e-4;
```
`q` and `−q` are the same rotation, and a runtime may return a renormalised quaternion,
so this could answer "read-only" on a runtime that would have accepted the write. When it
did, the drag fell into the rebuild branch — which was throttled to release-only — so the
picture did not move until the finger came up.
The probe now compares the **position** (exact, no sign ambiguity):
```
layer.transform = XRRigidTransform({0.123, -0.456, 0.789}, identity)
writable <=> layer.transform.position equals that marker
```
## Two mechanisms, mutually exclusive
| `stats.videoRotMode` | Mechanism | Cost per change |
| --- | --- | --- |
| `transform` | `layer.transform = q` | one write |
| `space` | `layer.space = refSpace.getOffsetReferenceSpace(q)` | one space alloc |
| `rebuild` | recreate the media layer (manual escape hatch) | ~8/second max |
`space` exists because if the transform write is genuinely ignored,
`getOffsetReferenceSpace()` is the recentering path every WebXR app relies on, and a
layer's pose is recomputed from its space each frame. The two are never combined:
`createVideoLayer()` only bakes the transform in for the `rebuild` strategy, otherwise the
probe's write would double the aim. `stats.videoTransformWritable` shows the raw probe
result; the 2D panel's 「画面旋转: 自动 / 兼容」 can force the rebuild path.
## The crash, and every fix around it
The reset rebuilt the layer and created the replacement **before** destroying the old
one — two `XRMediaBinding` equirect layers on one `<video>` element at once, then a
second rebuild microseconds later (release rebuilt, then reset rebuilt again) took the
browser down.
A rebuild now: runs in a `setTimeout` **outside** the XR frame callback, **destroys
first**, is throttled to 120 ms, and never queues more than one. The `transform`/`space`
paths never rebuild at all, so the normal case cannot reach the crash.
Two robustness holes found while testing:
- `videoSpaceFor()` let a throwing `getOffsetReferenceSpace()` escape into the frame
loop, where an exception stops it for good (`requestAnimationFrame` is only re-armed at
the end of the callback) — a frozen session. It is caught, and the strategy demotes to
`rebuild`.
- The whole per-frame body is now wrapped in a try/catch for the same reason.
## The gesture
```
press the drag strip
├─ released before 500 ms with no movement -> RESET to dead ahead
├─ held past 500 ms, then moved -> PAN
└─ held past 500 ms, never moved -> RESET
```
The pan reference is taken **when the hold threshold passes**, not at the press, so the
hold itself cannot fling the picture; a ~1° dead zone absorbs hand jitter. The strip reads
`点一下复位 · 按住 0.5s 再拖动`.
## Verification
Everything below was asserted offline against the real sources with a stubbed ray, stubbed
WebXR layer objects and a stubbed clock (`build.py --check` passes as well):
```
test-gesture quick tap -> reset; hold-without-move -> reset; movement inside the hold
window ignored; after arming, +10°/+5° ray -> exactly +10°/+5°; a pan does
not reset; dead zone respected; pitch guard holds
test-mech probe true for a writable transform, false for an ignored one;
transform strategy: writes, no rebuild; space strategy: assigns the offset
space, leaves the transform alone, no rebuild; unchanged angle reuses the
cached space; rebuild strategy: one pending max, observed sequence
removeLayer -> createVideoLayer -> addLayer; throwing/absent
getOffsetReferenceSpace -> reported + permanent fallback
test-ad8 quaternion aim/wrap/guard maths, panel bands on the 1440x520 canvas,
the strip's drawn readout, hold()
```
Deployed as `mvp-5217a3` (hud-7) and confirmed live on the Quest.
## Still to confirm on device
Which of `transform` / `space` this runtime actually honours — `stats.videoRotMode` and
`stats.videoTransformWritable` say so once a session is running.
+209 -76
View File
@@ -19,7 +19,7 @@
var NS = window.__JFVR = window.__JFVR || {};
NS.version = 'hud-6'; // hud-6 = ADDENDUM 8 (drag the picture window: yaw/pitch about the eye)
NS.version = 'hud-7'; // hud-7 = ADDENDUM 8b (live picture pan: transform/space, hold-to-drag, tap-to-reset)
NS.__core = true;
/* ------------------------------------------------------------------ *
@@ -1911,7 +1911,7 @@
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('连点 3 次复位', textX, num(lay.viewHintDy) * h);
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));
@@ -4174,6 +4174,13 @@
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;
// 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,
@@ -4204,6 +4211,7 @@
subAlpha: S.subAlpha,
projection: S.projection,
stereo: S.stereo,
videoRotMode: S.videoRotMode,
controls: S.controls,
ctlAutoHideMs: S.ctlAutoHideMs
}));
@@ -4457,6 +4465,18 @@
'字幕的位置 / 距离 / 大小 / 不透明度都在 VR 里用控制条调(控制条最下面一行),' +
'调好会自动记住。VR 空间右侧有个锁图标:锁上后面板不会再被唤出。'));
// ADDENDUM 8: the picture-pan mechanism. Only touch this if panning does not
// move the picture live (see stats.videoRotMode).
body.appendChild(segRow('画面旋转', function () { return S.videoRotMode || 'auto'; },
function (v) {
S.videoRotMode = v;
if (S.session) {
try { videoSpaceCache = null; S.videoTransformLive = undefined; applyVideoAngle(true); } catch (e) {}
}
saveSettings();
},
[{ label: '自动', value: 'auto' }, { label: '兼容', value: 'rebuild' }]));
body.appendChild(NS.makeEl('div', 'jfvr-sec', '控制'));
body.appendChild(segRow('VR 内控制条', function () { return S.controls ? 'on' : 'off'; },
function (v) { S.controls = v === 'on'; saveSettings(); },
@@ -4673,8 +4693,11 @@
// then the transform is applied so probe + apply see the real layer.
S.videoYaw = 0;
S.videoPitch = 0;
probeVideoAngleMutable(vlayer);
applyVideoAngle(false); // undo the probe's test rotation
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'); }
@@ -4727,12 +4750,19 @@
frames = 0; lastT = time;
}
if (frame) {
updatePointer(frame);
pollGamepads(frame);
updatePanelPose(frame, dt);
updateGrabFrame(frame);
updateViewDrag(frame);
NS.runFrameHooks(frame, time, session);
// 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);
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);
};
@@ -4763,12 +4793,20 @@
upperVerticalAngle: Math.PI / 2,
lowerVerticalAngle: -Math.PI / 2
};
// ADDENDUM 8: the transform is the picture's aim. radius stays 0 (content at
// infinity) so this is a pure rotation about the eye point. The transform is
// what makes a rebuild-based runtime possible, and what makes the very first
// frame already point where the user left it (a mid-session rebuild).
var t = videoAngleTransform();
if (t) opts.transform = t;
// 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) }));
@@ -4794,9 +4832,10 @@
grab = null;
S.stats.grab = 'reset';
viewDrag = null;
viewTaps = [];
cancelVideoRebuild();
videoSpaceCache = null;
S.videoTransformLive = undefined;
S.videoLayer = null;
S.videoAngleLive = false;
S.videoYaw = 0;
S.videoPitch = 0;
if (NS.Controls && NS.Controls.hold) { try { NS.Controls.hold(false); } catch (e) {} }
@@ -5099,67 +5138,154 @@
function updateVideoAngleStats() {
S.stats.videoYaw = Math.round(S.videoYaw * 180 / Math.PI);
S.stats.videoPitch = Math.round(S.videoPitch * 180 / Math.PI);
S.stats.videoAngleLive = !!S.videoAngleLive;
S.stats.videoRotMode = videoRotStrategy();
}
// XREquirectLayer.transform is settable in the spec, but a runtime is free to
// ignore the write, so probe it once: write a known rotation and read it back.
function probeVideoAngleMutable(layer) {
S.videoAngleLive = false;
if (!layer || typeof XRRigidTransform === 'undefined') return;
/* ------------------------------------------------------------------
* 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 {
var q = videoQuat(0.05, 0.05);
layer.transform = new XRRigidTransform(videoOrigin(), q);
var o = layer.transform && layer.transform.orientation;
S.videoAngleLive = !!(o && Math.abs(o.y - q.y) < 1e-4 && Math.abs(o.w - q.w) < 1e-4);
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) {
S.videoAngleLive = false;
}
S.stats.videoRotMode = S.videoAngleLive ? 'live' : 'rebuild';
if (!S.videoAngleLive) {
log('video layer transform is read-only -> rebuilding the layer on changes');
return false;
}
}
// Push the current angles into the layer. On a live runtime this is just a
// transform write. On a runtime that ignores the write we must REBUILD the media
// layer — which is far too expensive to do once per drag frame, so rebuilds
// happen only when `force` is set (creation, release, reset).
function applyVideoAngle(force) {
var layer = S.videoLayer;
if (!layer || !S.session) return;
if (S.videoAngleLive) {
try {
layer.transform = videoAngleTransform();
if ('needsRedraw' in layer) layer.needsRedraw = true;
} catch (e) {
S.videoAngleLive = false;
S.stats.videoRotMode = 'rebuild';
}
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 (!S.videoAngleLive && force) rebuildVideoLayer();
updateVideoAngleStats();
if (!sp) return null;
videoSpaceCache = { space: sp, yaw: yaw, pitch: pitch };
return sp;
}
function rebuildVideoLayer() {
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;
var old = S.videoLayer;
S.videoLayer = fresh;
NS.addLayer(fresh);
if (old) {
NS.removeLayer(old);
try { if (typeof old.destroy === 'function') old.destroy(); } catch (e) {}
}
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;
@@ -5174,10 +5300,12 @@
* no range limit (a panel-space cursor would run off the panel's edge).
* -------------------------------------------------------------- */
var viewDrag = null;
var VIEW_TAP_MS = 1600; // three taps inside this window reset the view
var viewTaps = [];
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;
@@ -5194,8 +5322,11 @@
var ray = rayOf(frame, src);
var a = rayAngles(ray);
if (!a) { S.stats.viewDrag = 'no ray'; return; }
viewDrag = { src: src, a0: a, yaw0: S.videoYaw, pitch0: S.videoPitch, moved: false };
S.stats.viewDrag = 'started';
viewDrag = {
src: 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) {} }
}
@@ -5205,8 +5336,21 @@
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;
@@ -5216,31 +5360,20 @@
applyVideoAngle(false);
}
// Release. A press that never moved counts as a TAP: three taps inside
// VIEW_TAP_MS reset the view (the owner's "连点 3 次复位"). Counting taps on
// RELEASE rather than on press means a real drag cannot be mistaken for one.
// 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) {} }
if (!S.videoAngleLive) applyVideoAngle(true); // pay the rebuild once, on release
applyVideoAngle(true); // settle the final angle
if (moved) {
viewTaps = [];
S.stats.viewDrag = 'ended (moved)';
return;
}
var now = performance.now();
viewTaps = viewTaps.filter(function (t) { return now - t < VIEW_TAP_MS; });
viewTaps.push(now);
S.stats.viewTaps = viewTaps.length;
if (viewTaps.length >= 3) {
viewTaps = [];
resetVideoAngle('triple-tap');
S.stats.viewDrag = 'reset (triple tap)';
return;
}
S.stats.viewDrag = 'tap ' + viewTaps.length + '/3';
resetVideoAngle('tap');
S.stats.viewDrag = 'reset (tap)';
}
/* ================================================================== *
+1 -1
View File
@@ -11,7 +11,7 @@
var NS = window.__JFVR = window.__JFVR || {};
NS.version = 'hud-6'; // hud-6 = ADDENDUM 8 (drag the picture window: yaw/pitch about the eye)
NS.version = 'hud-7'; // hud-7 = ADDENDUM 8b (live picture pan: transform/space, hold-to-drag, tap-to-reset)
NS.__core = true;
/* ------------------------------------------------------------------ *
+1 -1
View File
@@ -518,7 +518,7 @@
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('连点 3 次复位', textX, num(lay.viewHintDy) * h);
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));
+207 -74
View File
@@ -124,6 +124,13 @@
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;
// 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,
@@ -154,6 +161,7 @@
subAlpha: S.subAlpha,
projection: S.projection,
stereo: S.stereo,
videoRotMode: S.videoRotMode,
controls: S.controls,
ctlAutoHideMs: S.ctlAutoHideMs
}));
@@ -407,6 +415,18 @@
'字幕的位置 / 距离 / 大小 / 不透明度都在 VR 里用控制条调(控制条最下面一行),' +
'调好会自动记住。VR 空间右侧有个锁图标:锁上后面板不会再被唤出。'));
// ADDENDUM 8: the picture-pan mechanism. Only touch this if panning does not
// move the picture live (see stats.videoRotMode).
body.appendChild(segRow('画面旋转', function () { return S.videoRotMode || 'auto'; },
function (v) {
S.videoRotMode = v;
if (S.session) {
try { videoSpaceCache = null; S.videoTransformLive = undefined; applyVideoAngle(true); } catch (e) {}
}
saveSettings();
},
[{ label: '自动', value: 'auto' }, { label: '兼容', value: 'rebuild' }]));
body.appendChild(NS.makeEl('div', 'jfvr-sec', '控制'));
body.appendChild(segRow('VR 内控制条', function () { return S.controls ? 'on' : 'off'; },
function (v) { S.controls = v === 'on'; saveSettings(); },
@@ -623,8 +643,11 @@
// then the transform is applied so probe + apply see the real layer.
S.videoYaw = 0;
S.videoPitch = 0;
probeVideoAngleMutable(vlayer);
applyVideoAngle(false); // undo the probe's test rotation
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'); }
@@ -677,12 +700,19 @@
frames = 0; lastT = time;
}
if (frame) {
updatePointer(frame);
pollGamepads(frame);
updatePanelPose(frame, dt);
updateGrabFrame(frame);
updateViewDrag(frame);
NS.runFrameHooks(frame, time, session);
// 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);
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);
};
@@ -713,12 +743,20 @@
upperVerticalAngle: Math.PI / 2,
lowerVerticalAngle: -Math.PI / 2
};
// ADDENDUM 8: the transform is the picture's aim. radius stays 0 (content at
// infinity) so this is a pure rotation about the eye point. The transform is
// what makes a rebuild-based runtime possible, and what makes the very first
// frame already point where the user left it (a mid-session rebuild).
var t = videoAngleTransform();
if (t) opts.transform = t;
// 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) }));
@@ -744,9 +782,10 @@
grab = null;
S.stats.grab = 'reset';
viewDrag = null;
viewTaps = [];
cancelVideoRebuild();
videoSpaceCache = null;
S.videoTransformLive = undefined;
S.videoLayer = null;
S.videoAngleLive = false;
S.videoYaw = 0;
S.videoPitch = 0;
if (NS.Controls && NS.Controls.hold) { try { NS.Controls.hold(false); } catch (e) {} }
@@ -1049,67 +1088,154 @@
function updateVideoAngleStats() {
S.stats.videoYaw = Math.round(S.videoYaw * 180 / Math.PI);
S.stats.videoPitch = Math.round(S.videoPitch * 180 / Math.PI);
S.stats.videoAngleLive = !!S.videoAngleLive;
S.stats.videoRotMode = videoRotStrategy();
}
// XREquirectLayer.transform is settable in the spec, but a runtime is free to
// ignore the write, so probe it once: write a known rotation and read it back.
function probeVideoAngleMutable(layer) {
S.videoAngleLive = false;
if (!layer || typeof XRRigidTransform === 'undefined') return;
/* ------------------------------------------------------------------
* 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 {
var q = videoQuat(0.05, 0.05);
layer.transform = new XRRigidTransform(videoOrigin(), q);
var o = layer.transform && layer.transform.orientation;
S.videoAngleLive = !!(o && Math.abs(o.y - q.y) < 1e-4 && Math.abs(o.w - q.w) < 1e-4);
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) {
S.videoAngleLive = false;
}
S.stats.videoRotMode = S.videoAngleLive ? 'live' : 'rebuild';
if (!S.videoAngleLive) {
log('video layer transform is read-only -> rebuilding the layer on changes');
return false;
}
}
// Push the current angles into the layer. On a live runtime this is just a
// transform write. On a runtime that ignores the write we must REBUILD the media
// layer — which is far too expensive to do once per drag frame, so rebuilds
// happen only when `force` is set (creation, release, reset).
function applyVideoAngle(force) {
var layer = S.videoLayer;
if (!layer || !S.session) return;
if (S.videoAngleLive) {
try {
layer.transform = videoAngleTransform();
if ('needsRedraw' in layer) layer.needsRedraw = true;
} catch (e) {
S.videoAngleLive = false;
S.stats.videoRotMode = 'rebuild';
}
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 (!S.videoAngleLive && force) rebuildVideoLayer();
updateVideoAngleStats();
if (!sp) return null;
videoSpaceCache = { space: sp, yaw: yaw, pitch: pitch };
return sp;
}
function rebuildVideoLayer() {
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;
var old = S.videoLayer;
S.videoLayer = fresh;
NS.addLayer(fresh);
if (old) {
NS.removeLayer(old);
try { if (typeof old.destroy === 'function') old.destroy(); } catch (e) {}
}
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;
@@ -1124,10 +1250,12 @@
* no range limit (a panel-space cursor would run off the panel's edge).
* -------------------------------------------------------------- */
var viewDrag = null;
var VIEW_TAP_MS = 1600; // three taps inside this window reset the view
var viewTaps = [];
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;
@@ -1144,8 +1272,11 @@
var ray = rayOf(frame, src);
var a = rayAngles(ray);
if (!a) { S.stats.viewDrag = 'no ray'; return; }
viewDrag = { src: src, a0: a, yaw0: S.videoYaw, pitch0: S.videoPitch, moved: false };
S.stats.viewDrag = 'started';
viewDrag = {
src: 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) {} }
}
@@ -1155,8 +1286,21 @@
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;
@@ -1166,31 +1310,20 @@
applyVideoAngle(false);
}
// Release. A press that never moved counts as a TAP: three taps inside
// VIEW_TAP_MS reset the view (the owner's "连点 3 次复位"). Counting taps on
// RELEASE rather than on press means a real drag cannot be mistaken for one.
// 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) {} }
if (!S.videoAngleLive) applyVideoAngle(true); // pay the rebuild once, on release
applyVideoAngle(true); // settle the final angle
if (moved) {
viewTaps = [];
S.stats.viewDrag = 'ended (moved)';
return;
}
var now = performance.now();
viewTaps = viewTaps.filter(function (t) { return now - t < VIEW_TAP_MS; });
viewTaps.push(now);
S.stats.viewTaps = viewTaps.length;
if (viewTaps.length >= 3) {
viewTaps = [];
resetVideoAngle('triple-tap');
S.stats.viewDrag = 'reset (triple tap)';
return;
}
S.stats.viewDrag = 'tap ' + viewTaps.length + '/3';
resetVideoAngle('tap');
S.stats.viewDrag = 'reset (tap)';
}
/* ================================================================== *