@displayxr/inline3d 1.4.0 → 1.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +52 -0
- package/index.d.ts +47 -0
- package/js/inline3d-mode-switch.js +246 -0
- package/js/inline3d.js +389 -18
- package/package.json +2 -1
package/CHANGELOG.md
CHANGED
|
@@ -5,6 +5,58 @@ entry points (`.`, `./three`) are frozen for 1.x, while the **scene subpaths** (
|
|
|
5
5
|
`./splat`, `./model`) are a preview tier whose options may change in any release. Entries below say
|
|
6
6
|
which tier they touch, because that is what tells you whether an upgrade can move your pixels.
|
|
7
7
|
|
|
8
|
+
## 1.5.0 — 2026-09-06
|
|
9
|
+
|
|
10
|
+
### Added
|
|
11
|
+
|
|
12
|
+
- **The 2D↔3D switch is EASED, and every page gets it for free.** 1.4.0 made the panel's mode a
|
|
13
|
+
page-facing control and collapsed the stereo rig the moment a 1-view mode went active — correct,
|
|
14
|
+
and a snap. The transition now ramps: a **page-initiated** switch walks every window's
|
|
15
|
+
`ipdFactor`/`parallaxFactor` between 0 and what the page asked for over **180 ms**, **smoothstep**
|
|
16
|
+
(Hermite `3t^2 - 2t^3`) — the defaults the native DisplayXR apps configure, because this is a port
|
|
17
|
+
of the sequencer they already use (`dxr::ModeSwitch`, displayxr-common) rather than a second
|
|
18
|
+
design. New dependency-free module `js/inline3d-mode-switch.js` holds the state machine;
|
|
19
|
+
`test/mode-switch.test.mjs` mirrors the C++ smoke test case for case. *(core tier — additive)*
|
|
20
|
+
|
|
21
|
+
- **The ORDER is the feature, and it is asymmetric.** Going flat (a `viewCount === 1` target) ramps
|
|
22
|
+
the disparity **out first** and forwards the mode request only when it lands, so the panel flips
|
|
23
|
+
on already-flat content instead of snapping a stereo image flat. Coming back (a 2-view target)
|
|
24
|
+
forwards the request **first** and eases the disparity in **only once the panel REPORTS 3D** —
|
|
25
|
+
ramping up any earlier would put stereo on a still-flat panel, which is the blurry double image
|
|
26
|
+
the whole mode API exists to make unreachable. A hand-rolled tween gets exactly this wrong.
|
|
27
|
+
|
|
28
|
+
- **`createInline3D({ modeSwitch: { durationMs, easing, enabled } })`** — `durationMs` default
|
|
29
|
+
`180` (`0` keeps the ordering and lands in one frame), `easing` default `'smoothstep'` (also
|
|
30
|
+
`'linear'`, `'easeoutcubic'`; an unknown name warns once and falls back), `enabled: false`
|
|
31
|
+
restores 1.4.0's snap exactly. **`wall.modeSwitch`** is the read-only live state
|
|
32
|
+
`{active, factor}` for a page that wants to move its own 2D chrome alongside the panel. The SDK
|
|
33
|
+
adds **no UI** — which key or button toggles the display stays the page's call.
|
|
34
|
+
*(core tier — additive)*
|
|
35
|
+
|
|
36
|
+
- **What a page can feel, spelled out.** The restore is to the **configured** steady factors (each
|
|
37
|
+
window's own rig, never a hardcoded 1) and the ramp is a **copy** on the way to the layer, so the
|
|
38
|
+
1.4.0 guarantees hold unchanged: a per-frame `setViewRig` loop cannot walk the page out of 2D,
|
|
39
|
+
and a lazy tile that rebuilds mid-transition comes back at the current factor. The ramp is driven
|
|
40
|
+
by **wall-clock dt** from the session's frame loop (never frame counts, so it lasts the same at
|
|
41
|
+
30 fps and 144 fps), with a timer fallback so a held request still lands when frames stop.
|
|
42
|
+
Reversing mid-flight retargets from the disparity **in force** — the first press never snaps —
|
|
43
|
+
and a reversed going-flat switch **never fires**: nothing is asked of the display at all.
|
|
44
|
+
|
|
45
|
+
### Changed
|
|
46
|
+
|
|
47
|
+
- **`requestRenderingMode(i)` / `setStereoEnabled(false)` for a 1-view target now resolve when the
|
|
48
|
+
request has been FORWARDED**, i.e. after the ramp (~`durationMs`), not on the call. They reject
|
|
49
|
+
as before if the browser refuses — and a refusal ramps the disparity back **up**, because a
|
|
50
|
+
refused switch must leave the page in 3D rather than flat. One new failure: a request dropped by
|
|
51
|
+
a reversal before it ever fired rejects with an `Error` named **`superseded`**. Nothing changes
|
|
52
|
+
for a page that only awaits the promise it already awaited; `{ enabled: false }` restores the old
|
|
53
|
+
timing. *(core tier — behaviour change, opt-out)*
|
|
54
|
+
|
|
55
|
+
- A mode change the page did **not** request (another tab, the shell, a page that opens with the
|
|
56
|
+
panel already flat) still **snaps** — there is nothing to ramp from, and the reported state stays
|
|
57
|
+
the sole authority for the rig. `wall.stereoCollapsed` continues to mean what the display last
|
|
58
|
+
**reported**, never what is mid-ramp.
|
|
59
|
+
|
|
8
60
|
## 1.4.0 — 2026-09-06
|
|
9
61
|
|
|
10
62
|
### Added
|
package/index.d.ts
CHANGED
|
@@ -250,6 +250,11 @@ export interface TileHandle {
|
|
|
250
250
|
* by the session's `renderingmodechange` event, not by this promise.
|
|
251
251
|
*
|
|
252
252
|
* A `viewCount === 1` mode is requestable and is how a page goes flat.
|
|
253
|
+
*
|
|
254
|
+
* With the eased transition on (the default — see {@link ModeSwitchOptions}) a going-flat
|
|
255
|
+
* request is HELD while the disparity ramps out, so the promise resolves when the request has
|
|
256
|
+
* been forwarded rather than on the call; a request dropped by a reversal in that window
|
|
257
|
+
* rejects with an `Error` named `superseded`.
|
|
253
258
|
*/
|
|
254
259
|
requestRenderingMode(modeIndex: number): Promise<void>;
|
|
255
260
|
/**
|
|
@@ -325,6 +330,13 @@ export interface Inline3D {
|
|
|
325
330
|
readonly activeMode: { modeIndex: number; viewCount: number };
|
|
326
331
|
/** True while the SDK is holding every window's rig flat because a 1-view mode is active. */
|
|
327
332
|
readonly stereoCollapsed: boolean;
|
|
333
|
+
/**
|
|
334
|
+
* The eased 2D<->3D transition, live. `factor` is what every window's
|
|
335
|
+
* `ipdFactor`/`parallaxFactor` is being multiplied by on the way to the layer (`1` in 3D, `0`
|
|
336
|
+
* flat, in between mid-ramp); `active` is true while a page-initiated switch is in any of its
|
|
337
|
+
* phases. Read-only and purely informational — the SDK adds no UI of its own for this.
|
|
338
|
+
*/
|
|
339
|
+
readonly modeSwitch: { active: boolean; factor: number };
|
|
328
340
|
|
|
329
341
|
/**
|
|
330
342
|
* What this build can lift into the floating native viewer, or `null` on a browser with no
|
|
@@ -411,6 +423,41 @@ export interface CreateInline3DOptions {
|
|
|
411
423
|
* SDK never touches your DOM's `will-change`, because the chrome already occludes the tiles.
|
|
412
424
|
*/
|
|
413
425
|
autoChrome?: boolean;
|
|
426
|
+
/** The eased 2D<->3D transition. On by default; see {@link ModeSwitchOptions}. */
|
|
427
|
+
modeSwitch?: ModeSwitchOptions;
|
|
428
|
+
}
|
|
429
|
+
|
|
430
|
+
/**
|
|
431
|
+
* The eased 2D<->3D transition — on by default, and the same sequencer (and the same defaults)
|
|
432
|
+
* the native DisplayXR apps use.
|
|
433
|
+
*
|
|
434
|
+
* Instead of snapping the stereo rig the moment the panel's mode changes, a **page-initiated**
|
|
435
|
+
* switch ramps every window's `ipdFactor`/`parallaxFactor` between 0 and what the page asked for,
|
|
436
|
+
* in the order that looks right:
|
|
437
|
+
*
|
|
438
|
+
* - **going flat** (a `viewCount === 1` target): the disparity ramps OUT first, and the mode
|
|
439
|
+
* request is forwarded only when it lands — so the panel flips on already-flat content. That is
|
|
440
|
+
* why `requestRenderingMode()` / `setStereoEnabled(false)` resolve a ramp later than they used
|
|
441
|
+
* to: they resolve when the request has actually been forwarded.
|
|
442
|
+
* - **coming back** (a 2-view target): the request goes out at once, and the disparity eases in
|
|
443
|
+
* only once the panel REPORTS 3D — disparity on a still-flat panel is the double image the
|
|
444
|
+
* whole mode API exists to prevent.
|
|
445
|
+
*
|
|
446
|
+
* Interruptible: pressing the toggle again mid-ramp retargets from the disparity in force, and
|
|
447
|
+
* reversing a going-flat switch that has not fired yet simply ramps back up without ever asking
|
|
448
|
+
* the panel for anything (the dropped request rejects with an `Error` named `superseded`).
|
|
449
|
+
*
|
|
450
|
+
* A mode change the page did **not** request (another tab, the shell, a panel that opens flat)
|
|
451
|
+
* always snaps — there is nothing to ramp from. This is aesthetic policy only; correctness is the
|
|
452
|
+
* runtime's either way.
|
|
453
|
+
*/
|
|
454
|
+
export interface ModeSwitchOptions {
|
|
455
|
+
/** Ramp duration in ms (default `180`, matching the native default of 0.18 s). `0` = instant. */
|
|
456
|
+
durationMs?: number;
|
|
457
|
+
/** Easing curve (default `'smoothstep'`, Hermite `3t^2 - 2t^3`). */
|
|
458
|
+
easing?: 'smoothstep' | 'linear' | 'easeoutcubic';
|
|
459
|
+
/** `false` restores the plain snap of 1.4.0 (default `true`). */
|
|
460
|
+
enabled?: boolean;
|
|
414
461
|
}
|
|
415
462
|
|
|
416
463
|
/** The return of {@link startInline3D}. */
|
|
@@ -0,0 +1,246 @@
|
|
|
1
|
+
// inline3d-mode-switch.js — the eased 2D<->3D rendering-mode transition, as a state machine.
|
|
2
|
+
//
|
|
3
|
+
// A dependency-free port of `dxr::ModeSwitch` (displayxr-common `common/mode_switch.h/.cpp`), the
|
|
4
|
+
// sequencer the native DisplayXR apps already use. It owns nothing but a scalar ramp and one
|
|
5
|
+
// decision, and that decision is the whole point — the SEQUENCING ASYMMETRY that hand-rolled
|
|
6
|
+
// versions get wrong:
|
|
7
|
+
//
|
|
8
|
+
// 3D -> 2D : ramp the disparity to 0 FIRST, and only then issue the mode request, so the panel
|
|
9
|
+
// flips on already-flat content.
|
|
10
|
+
// 2D -> 3D : issue the mode request FIRST (the first 3D frame is flat), then ease the disparity
|
|
11
|
+
// up to the app's steady value.
|
|
12
|
+
//
|
|
13
|
+
// It is aesthetic policy, never correctness: the runtime keeps the eye set coherent whatever the
|
|
14
|
+
// page does, and a browser or a page that skips this sees exactly the old snap.
|
|
15
|
+
//
|
|
16
|
+
// Driven by WALL-CLOCK dt, not frame counts, so the ramp takes the same time at 30 fps and 144 fps.
|
|
17
|
+
// Interruptible: calling request() mid-flight retargets seamlessly from the value in force right
|
|
18
|
+
// now, and a not-yet-fired ->2D that gets reversed simply ramps back up and NEVER fires.
|
|
19
|
+
//
|
|
20
|
+
// This module knows nothing about WebXR, the SDK, or the DOM — it is a pure state machine so it
|
|
21
|
+
// can be unit-tested on its own (`test/mode-switch.test.mjs`, mirroring the C++ smoke test).
|
|
22
|
+
|
|
23
|
+
/** The C++ default ramp duration (`XrSessionUpdateModeSwitch` configures 0.18 s). */
|
|
24
|
+
export const MODE_SWITCH_DEFAULT_DURATION_MS = 180;
|
|
25
|
+
|
|
26
|
+
/** The C++ default curve (`ModeSwitchEasing::SmoothStep`). */
|
|
27
|
+
export const MODE_SWITCH_DEFAULT_EASING = 'smoothstep';
|
|
28
|
+
|
|
29
|
+
/** Every easing this understands — the three of `dxr::ModeSwitchEasing`. */
|
|
30
|
+
export const MODE_SWITCH_EASINGS = ['linear', 'smoothstep', 'easeoutcubic'];
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Normalise an easing name to one of {@link MODE_SWITCH_EASINGS}, or `null` when it is not one of
|
|
34
|
+
* them. Case- and separator-insensitive (`'ease-out-cubic'`, `'easeOutCubic'`), so a caller may
|
|
35
|
+
* spell it the way its own config does. Returning null rather than a default is what lets the
|
|
36
|
+
* caller decide whether an unknown name is worth a warning.
|
|
37
|
+
*
|
|
38
|
+
* @param {string} [easing]
|
|
39
|
+
* @returns {string|null}
|
|
40
|
+
*/
|
|
41
|
+
export function normaliseModeSwitchEasing(easing) {
|
|
42
|
+
if (typeof easing !== 'string') return null;
|
|
43
|
+
const key = easing.toLowerCase().replace(/[-_\s]/g, '');
|
|
44
|
+
return MODE_SWITCH_EASINGS.includes(key) ? key : null;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/** The curves themselves. `t` is already clamped to [0,1] by the caller. */
|
|
48
|
+
function ease(easing, t) {
|
|
49
|
+
if (t <= 0) return 0;
|
|
50
|
+
if (t >= 1) return 1;
|
|
51
|
+
if (easing === 'linear') return t;
|
|
52
|
+
if (easing === 'easeoutcubic') {
|
|
53
|
+
const u = 1 - t;
|
|
54
|
+
return 1 - u * u * u;
|
|
55
|
+
}
|
|
56
|
+
return t * t * (3 - 2 * t); // smoothstep — Hermite 3t^2 - 2t^3
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
const IDLE = 'idle';
|
|
60
|
+
const RAMP_DOWN_THEN_FIRE = 'rampDownThenFire';
|
|
61
|
+
const FIRE_THEN_RAMP_UP = 'fireThenRampUp';
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* The 2D<->3D mode-switch sequencer. One instance per session.
|
|
65
|
+
*
|
|
66
|
+
* The values it ramps are DIMENSIONLESS here: the SDK drives it with `steady: 1`, so `factor` is
|
|
67
|
+
* the fraction of each window's OWN configured `ipdFactor`/`parallaxFactor` to send this frame
|
|
68
|
+
* (0 = flat, 1 = exactly what the page asked for). The C++ original ramps an absolute ipdFactor
|
|
69
|
+
* instead; the state machine is identical either way, which is why `steady` is a parameter rather
|
|
70
|
+
* than a constant.
|
|
71
|
+
*/
|
|
72
|
+
export class ModeSwitch {
|
|
73
|
+
constructor(durationS, easing) {
|
|
74
|
+
this._phase = IDLE;
|
|
75
|
+
this._targetMode = null;
|
|
76
|
+
this._firePending = false; // fireThenRampUp: emit `fire` on the next update()
|
|
77
|
+
this._fireAtEnd = false; // rampDownThenFire: emit `fire` when the ramp lands
|
|
78
|
+
this._from = 0;
|
|
79
|
+
this._to = 0;
|
|
80
|
+
this._cur = 0; // last evaluated factor
|
|
81
|
+
this._t = 1; // normalised progress; 1 = landed/idle
|
|
82
|
+
this._dur = MODE_SWITCH_DEFAULT_DURATION_MS / 1000;
|
|
83
|
+
this._easing = MODE_SWITCH_DEFAULT_EASING;
|
|
84
|
+
if (durationS !== undefined || easing !== undefined) this.configure(durationS, easing);
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* Ramp duration in SECONDS and easing curve. `durationS <= 0` means instant — the switch fires
|
|
89
|
+
* and the value reaches its endpoint on the first `update()`, which is the honest way to say
|
|
90
|
+
* "no transition" without a second code path. An unknown easing name falls back to the default
|
|
91
|
+
* silently (the caller is the right place to warn about its own option).
|
|
92
|
+
*
|
|
93
|
+
* @param {number} [durationS] default 0.18
|
|
94
|
+
* @param {string} [easing] `'smoothstep'` (default) | `'linear'` | `'easeoutcubic'`
|
|
95
|
+
*/
|
|
96
|
+
configure(durationS, easing) {
|
|
97
|
+
if (durationS !== undefined) {
|
|
98
|
+
const d = Number(durationS);
|
|
99
|
+
this._dur = Number.isFinite(d) && d > 0 ? d : 0;
|
|
100
|
+
}
|
|
101
|
+
if (easing !== undefined) {
|
|
102
|
+
this._easing = normaliseModeSwitchEasing(easing) || MODE_SWITCH_DEFAULT_EASING;
|
|
103
|
+
}
|
|
104
|
+
return this;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/** The ramp duration in seconds (0 = instant). */
|
|
108
|
+
get durationS() {
|
|
109
|
+
return this._dur;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/** The easing name in force. */
|
|
113
|
+
get easing() {
|
|
114
|
+
return this._easing;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* Begin — or, mid-flight, seamlessly REDIRECT — a transition to `targetMode`.
|
|
119
|
+
*
|
|
120
|
+
* Safe to call on every toggle: a mid-ramp call fully resets the phase from the value in force
|
|
121
|
+
* right now, so a pending (un-fired) ->2D that the user reverses is simply dropped.
|
|
122
|
+
*
|
|
123
|
+
* @param {object} req
|
|
124
|
+
* @param {number|null} [req.targetMode] the rendering-mode index wanted (reported back by `update`)
|
|
125
|
+
* @param {number} req.targetViewCount that mode's view count (1 = 2D/mono, >1 = 3D)
|
|
126
|
+
* @param {number|null} [req.currentMode] the mode index active right now
|
|
127
|
+
* @param {number} req.currentViewCount its view count
|
|
128
|
+
* @param {number} [req.current] the value in force RIGHT NOW — the last `update()` factor while a
|
|
129
|
+
* ramp runs, and the STEADY value when idle. Passing an internal 0 while idle is the
|
|
130
|
+
* classic first-press snap: there is nothing to ramp down from.
|
|
131
|
+
* @param {number} [req.steady] the value to restore to (the page's own configured factors; the
|
|
132
|
+
* SDK passes 1 because it scales each window's own numbers by the result)
|
|
133
|
+
*/
|
|
134
|
+
request({
|
|
135
|
+
targetMode = null,
|
|
136
|
+
targetViewCount,
|
|
137
|
+
currentMode = null,
|
|
138
|
+
currentViewCount,
|
|
139
|
+
current,
|
|
140
|
+
steady = 1,
|
|
141
|
+
} = {}) {
|
|
142
|
+
const toMono = !(targetViewCount > 1);
|
|
143
|
+
const fromMono = !(currentViewCount > 1);
|
|
144
|
+
const steadyValue = Number.isFinite(steady) ? steady : 1;
|
|
145
|
+
const currentValue = Number.isFinite(current) ? current : steadyValue;
|
|
146
|
+
|
|
147
|
+
this._targetMode = targetMode;
|
|
148
|
+
this._from = currentValue;
|
|
149
|
+
|
|
150
|
+
if (toMono && !fromMono) {
|
|
151
|
+
// 3D -> 2D: flatten first, switch on landing, so 2D engages on already-mono content. The
|
|
152
|
+
// mode request is HELD until the ramp completes.
|
|
153
|
+
this._phase = RAMP_DOWN_THEN_FIRE;
|
|
154
|
+
this._to = 0;
|
|
155
|
+
this._fireAtEnd = true;
|
|
156
|
+
this._firePending = false;
|
|
157
|
+
} else if (!toMono && fromMono) {
|
|
158
|
+
// 2D -> 3D: switch now so the first 3D frame is flat (`from` is forced to 0 regardless of
|
|
159
|
+
// any stale value), then ease up to steady.
|
|
160
|
+
this._phase = FIRE_THEN_RAMP_UP;
|
|
161
|
+
this._from = 0;
|
|
162
|
+
this._to = steadyValue;
|
|
163
|
+
this._firePending = true;
|
|
164
|
+
this._fireAtEnd = false;
|
|
165
|
+
} else {
|
|
166
|
+
// Same dimensionality: 2D->2D, 3D->3D, or the REVERSAL of a not-yet-fired ->2D (nothing
|
|
167
|
+
// fired, so the display is still 3D and `currentViewCount` is still > 1). No flatten: switch
|
|
168
|
+
// now — skipping the fire when the target is already the current mode, which is exactly what
|
|
169
|
+
// makes a reversal never issue a stale request — and restore steady disparity for a 3D
|
|
170
|
+
// target. For a 2D target the value is irrelevant (mono), so leave it where it is.
|
|
171
|
+
this._phase = FIRE_THEN_RAMP_UP;
|
|
172
|
+
this._to = toMono ? currentValue : steadyValue;
|
|
173
|
+
this._firePending = !(targetMode !== null && targetMode === currentMode);
|
|
174
|
+
this._fireAtEnd = false;
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
this._t = this._dur > 0 ? 0 : 1;
|
|
178
|
+
this._cur = this._from;
|
|
179
|
+
return this;
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
/**
|
|
183
|
+
* Advance the ramp by `dtS` seconds and report this frame's outputs.
|
|
184
|
+
*
|
|
185
|
+
* @param {number} dtS wall-clock seconds since the last call
|
|
186
|
+
* @returns {{factor:number, fire:boolean, mode:(number|null)}} `factor` is the value to submit
|
|
187
|
+
* this frame; `fire` is true on EXACTLY ONE update — the frame on which the caller should
|
|
188
|
+
* issue the real mode request for `mode`.
|
|
189
|
+
*/
|
|
190
|
+
update(dtS) {
|
|
191
|
+
let fire = false;
|
|
192
|
+
|
|
193
|
+
if (this._phase !== IDLE) {
|
|
194
|
+
const dt = Number.isFinite(dtS) && dtS > 0 ? dtS : 0;
|
|
195
|
+
if (this._t < 1 && this._dur > 0) {
|
|
196
|
+
this._t += dt / this._dur;
|
|
197
|
+
if (this._t > 1) this._t = 1;
|
|
198
|
+
} else {
|
|
199
|
+
this._t = 1;
|
|
200
|
+
}
|
|
201
|
+
this._cur = this._from + (this._to - this._from) * ease(this._easing, this._t);
|
|
202
|
+
|
|
203
|
+
if (this._phase === FIRE_THEN_RAMP_UP) {
|
|
204
|
+
if (this._firePending) {
|
|
205
|
+
this._firePending = false;
|
|
206
|
+
fire = true;
|
|
207
|
+
}
|
|
208
|
+
if (this._t >= 1) this._phase = IDLE;
|
|
209
|
+
} else {
|
|
210
|
+
if (this._t >= 1) {
|
|
211
|
+
if (this._fireAtEnd) {
|
|
212
|
+
this._fireAtEnd = false;
|
|
213
|
+
fire = true;
|
|
214
|
+
}
|
|
215
|
+
this._phase = IDLE;
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
return { factor: this._cur, fire, mode: this._targetMode };
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
/** True while a ramp is in flight or a held mode request has not fired yet. */
|
|
224
|
+
active() {
|
|
225
|
+
return this._phase !== IDLE;
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
/** The current factor, without advancing the clock. */
|
|
229
|
+
value() {
|
|
230
|
+
return this._cur;
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
/** True while a mode request is being HELD until the ramp lands (a ->2D that has not fired). */
|
|
234
|
+
firePending() {
|
|
235
|
+
return this._phase === RAMP_DOWN_THEN_FIRE ? this._fireAtEnd : this._firePending;
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
/** Drop everything in flight. The factor is left where it is — the caller owns what to do next. */
|
|
239
|
+
cancel() {
|
|
240
|
+
this._phase = IDLE;
|
|
241
|
+
this._firePending = false;
|
|
242
|
+
this._fireAtEnd = false;
|
|
243
|
+
this._t = 1;
|
|
244
|
+
return this;
|
|
245
|
+
}
|
|
246
|
+
}
|
package/js/inline3d.js
CHANGED
|
@@ -32,6 +32,17 @@
|
|
|
32
32
|
import { undock, undockAvailable, undockUrl, tileScreenRect, setUndockLayerResolver } from './inline3d-undock.js';
|
|
33
33
|
export { undock, undockAvailable, undockUrl, tileScreenRect };
|
|
34
34
|
|
|
35
|
+
// The eased 2D<->3D transition. A pure state machine (no DOM, no WebXR) ported from the native
|
|
36
|
+
// `dxr::ModeSwitch`, so the browser eases the disparity around a mode switch the same way — and in
|
|
37
|
+
// the same ORDER — as the native apps and the demos. See _requestRenderingModeEased.
|
|
38
|
+
import {
|
|
39
|
+
ModeSwitch,
|
|
40
|
+
MODE_SWITCH_DEFAULT_DURATION_MS,
|
|
41
|
+
MODE_SWITCH_DEFAULT_EASING,
|
|
42
|
+
MODE_SWITCH_EASINGS,
|
|
43
|
+
normaliseModeSwitchEasing,
|
|
44
|
+
} from './inline3d-mode-switch.js';
|
|
45
|
+
|
|
35
46
|
// The document's single live manager. The browser's per-frame element-rect report is a
|
|
36
47
|
// WHOLE-WIDGET setter — each live session pushes the complete list of rects to weave — so two
|
|
37
48
|
// managers in one document overwrite each other frame by frame and neither one's tiles hold
|
|
@@ -329,14 +340,56 @@ function defaultDisplayRig(win) {
|
|
|
329
340
|
};
|
|
330
341
|
}
|
|
331
342
|
|
|
332
|
-
// The page's rig with the stereo dialled
|
|
333
|
-
// both eyes
|
|
343
|
+
// The page's rig with the stereo dialled DOWN by `factor`: eye separation and head-tracking
|
|
344
|
+
// response scaled together, so `factor` 0 renders both eyes from the SAME place (the woven atlas
|
|
345
|
+
// carries one image twice) and `factor` 1 is exactly what the page asked for. Everything between
|
|
346
|
+
// is the eased 2D<->3D transition (see ModeSwitch) — which is why this is a scale and not a
|
|
347
|
+
// boolean: a flat panel and a full-disparity one are the two ENDS of one continuum.
|
|
334
348
|
//
|
|
335
349
|
// A COPY, never a mutation. A page driving a rig per frame reuses one descriptor object
|
|
336
|
-
// (cameraRigFromCamera's `out`), so
|
|
350
|
+
// (cameraRigFromCamera's `out`), so scaling the factors in place would write the flattening into
|
|
337
351
|
// the page's own state and it would never come back — the restore would restore 0.
|
|
338
|
-
|
|
339
|
-
|
|
352
|
+
//
|
|
353
|
+
// An unset factor is the runtime's default of 1, so it scales like an explicit 1 rather than
|
|
354
|
+
// staying absent: a rig that says nothing about disparity still goes flat.
|
|
355
|
+
function scaledRig(rig, factor) {
|
|
356
|
+
const ipd = Number.isFinite(rig.ipdFactor) ? rig.ipdFactor : 1;
|
|
357
|
+
const parallax = Number.isFinite(rig.parallaxFactor) ? rig.parallaxFactor : 1;
|
|
358
|
+
return { ...rig, ipdFactor: ipd * factor, parallaxFactor: parallax * factor };
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
// How often the fallback tick advances a transition when session frames are NOT arriving (a
|
|
362
|
+
// background tab, every tile scrolled away). Roughly one 60 Hz frame — the ramp is time-based, so
|
|
363
|
+
// this is a floor on smoothness, never on duration.
|
|
364
|
+
const MODE_SWITCH_TICK_MS = 16;
|
|
365
|
+
|
|
366
|
+
// Wall clock for the transition ramp, in ms. Read through the global on every call (never
|
|
367
|
+
// captured) so a test can install its own clock, and so a page that runs before `performance`
|
|
368
|
+
// exists still gets a monotonic-enough source. Frame COUNTS are deliberately not used: the ramp
|
|
369
|
+
// has to take the same time at 30 fps and at 144 fps.
|
|
370
|
+
function nowMs() {
|
|
371
|
+
return typeof performance !== 'undefined' && performance && typeof performance.now === 'function'
|
|
372
|
+
? performance.now()
|
|
373
|
+
: Date.now();
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
// The easing option, validated here rather than in the state machine: the sequencer falls back
|
|
377
|
+
// silently (it has no opinion about a caller's config), but a typo in `createInline3D` is worth
|
|
378
|
+
// exactly one warning — a page that asked for 'ease-in-out' and got smoothstep should know.
|
|
379
|
+
let notedModeSwitchEasing = false;
|
|
380
|
+
function resolveModeSwitchEasing(easing) {
|
|
381
|
+
if (easing === undefined || easing === null) return MODE_SWITCH_DEFAULT_EASING;
|
|
382
|
+
const known = normaliseModeSwitchEasing(easing);
|
|
383
|
+
if (known) return known;
|
|
384
|
+
if (!notedModeSwitchEasing) {
|
|
385
|
+
notedModeSwitchEasing = true;
|
|
386
|
+
console.warn(
|
|
387
|
+
`[inline3d] createInline3D({ modeSwitch: { easing: ${JSON.stringify(easing)} } }) is not a ` +
|
|
388
|
+
`curve this SDK knows (${MODE_SWITCH_EASINGS.join(' / ')}); using ` +
|
|
389
|
+
`'${MODE_SWITCH_DEFAULT_EASING}'.`
|
|
390
|
+
);
|
|
391
|
+
}
|
|
392
|
+
return MODE_SWITCH_DEFAULT_EASING;
|
|
340
393
|
}
|
|
341
394
|
|
|
342
395
|
// Why the SDK collapses the rig behind the page's back, said once, where the code is.
|
|
@@ -439,13 +492,30 @@ function noteRigWinsOverHeight() {
|
|
|
439
492
|
* exclusively via addGlobalOverlay()/data-inline3d-overlay. Ignored (nothing is
|
|
440
493
|
* scanned, no `will-change` is set on your DOM) on a browser with draw-order
|
|
441
494
|
* occlusion, where chrome occludes tiles by itself.
|
|
495
|
+
* @param {object} [opts.modeSwitch] The EASED 2D<->3D transition, on by default.
|
|
496
|
+
* `{ durationMs=180, easing='smoothstep'|'linear'|'easeoutcubic', enabled=true }` — the
|
|
497
|
+
* same defaults the native DisplayXR apps configure. Instead of snapping the stereo rig
|
|
498
|
+
* the moment the panel's mode changes, a page-initiated switch ramps every window's
|
|
499
|
+
* `ipdFactor`/`parallaxFactor` between 0 and what the page asked for, in the order that
|
|
500
|
+
* looks right: going FLAT ramps the disparity out first and only then asks the panel to
|
|
501
|
+
* switch, and coming BACK asks first and eases the disparity in once the panel reports
|
|
502
|
+
* 3D. `enabled:false` restores the plain snap. It is aesthetic policy only — the runtime
|
|
503
|
+
* keeps the eye set coherent either way — and a mode change the page did NOT request
|
|
504
|
+
* (another tab, the shell, a panel opening flat) always snaps, because there is nothing
|
|
505
|
+
* to ramp from. Read the live state on `wall.modeSwitch`.
|
|
442
506
|
* @returns {Promise<Inline3D | {supported:false, error?:Error}>} the manager, which also carries
|
|
443
507
|
* the display API (`getDisplayInfo` / `getRenderingModes` / `requestRenderingMode` /
|
|
444
508
|
* `setStereoEnabled`, `on`/`off`) and `undock` — `{model, splat}` on a browser with
|
|
445
509
|
* `XRDisplayLayer.undock`, `null` on one without.
|
|
446
510
|
*/
|
|
447
511
|
export async function createInline3D(opts = {}) {
|
|
448
|
-
const {
|
|
512
|
+
const {
|
|
513
|
+
referenceSpace = 'viewer',
|
|
514
|
+
lazy = true,
|
|
515
|
+
rootMargin = '50% 0px',
|
|
516
|
+
autoChrome = true,
|
|
517
|
+
modeSwitch = null,
|
|
518
|
+
} = opts;
|
|
449
519
|
if (!inline3DAvailable()) return { supported: false };
|
|
450
520
|
let session;
|
|
451
521
|
try {
|
|
@@ -461,7 +531,7 @@ export async function createInline3D(opts = {}) {
|
|
|
461
531
|
} catch {
|
|
462
532
|
/* rAF still fires without a ref space; views are just null (fine for image/video). */
|
|
463
533
|
}
|
|
464
|
-
return new Inline3D(session, refSpace, { lazy, rootMargin, autoChrome });
|
|
534
|
+
return new Inline3D(session, refSpace, { lazy, rootMargin, autoChrome, modeSwitch });
|
|
465
535
|
}
|
|
466
536
|
|
|
467
537
|
/**
|
|
@@ -513,7 +583,7 @@ function chromeTextPlates(root) {
|
|
|
513
583
|
}
|
|
514
584
|
|
|
515
585
|
class Inline3D {
|
|
516
|
-
constructor(session, refSpace, { lazy, rootMargin, autoChrome = true }) {
|
|
586
|
+
constructor(session, refSpace, { lazy, rootMargin, autoChrome = true, modeSwitch = null }) {
|
|
517
587
|
this.supported = true;
|
|
518
588
|
this.session = session;
|
|
519
589
|
this.refSpace = refSpace;
|
|
@@ -553,6 +623,23 @@ class Inline3D {
|
|
|
553
623
|
this._stereoCollapsed = false;
|
|
554
624
|
this._displayListeners = new Map(); // event type -> Set(callback), for on()/off()
|
|
555
625
|
this._primedDisplayState = false;
|
|
626
|
+
// ── the eased 2D<->3D transition (opts.modeSwitch) ───────────────────────────────
|
|
627
|
+
// The collapse above is a LATCH; what actually reaches each layer is that latch turned into a
|
|
628
|
+
// SCALE — `_stereoFactor`, 0 (flat) to 1 (exactly the rig the page set). With the sequencer
|
|
629
|
+
// off, or for a mode change the page did not request, the scale is only ever 0 or 1 and
|
|
630
|
+
// nothing looks different. With it on, a page-initiated switch walks the scale across that
|
|
631
|
+
// range over `durationMs` and holds the mode request until the right end of the ramp.
|
|
632
|
+
const msOpts = modeSwitch && typeof modeSwitch === 'object' ? modeSwitch : {};
|
|
633
|
+
this._msEnabled = msOpts.enabled !== false;
|
|
634
|
+
this._modeSwitch = new ModeSwitch(
|
|
635
|
+
(Number.isFinite(msOpts.durationMs) ? Math.max(0, msOpts.durationMs) : MODE_SWITCH_DEFAULT_DURATION_MS) / 1000,
|
|
636
|
+
resolveModeSwitchEasing(msOpts.easing)
|
|
637
|
+
);
|
|
638
|
+
this._stereoFactor = 1; // what every window's ipd/parallax is multiplied by on the way out
|
|
639
|
+
this._msFire = null; // a ->2D request HELD until the ramp-down lands
|
|
640
|
+
this._msArmedUp = false; // a ->3D request went out; the up-ramp waits for the panel to say 3D
|
|
641
|
+
this._msLastMs = null; // wall clock of the previous advance (null = the ramp has not ticked)
|
|
642
|
+
this._msTick = null; // the frames-stopped fallback timer; see _armModeSwitchTick
|
|
556
643
|
// Undock capabilities, refreshed off the first live layer (see _refreshUndock). Null is the
|
|
557
644
|
// load-bearing value: it means this browser has no XRDisplayLayer.undock at all.
|
|
558
645
|
this.undock = hasUndock() ? { model: false, splat: false } : null;
|
|
@@ -833,7 +920,8 @@ class Inline3D {
|
|
|
833
920
|
* pushed FLAT (ipd/parallax 0) — the flattening is a latch on the way out, not a value
|
|
834
921
|
* written into your descriptor, so a page driving a rig every frame cannot undo the 2D
|
|
835
922
|
* state by simply carrying on, and `setStereoEnabled(true)` restores exactly what you last
|
|
836
|
-
* asked for.
|
|
923
|
+
* asked for. During the eased 2D<->3D transition the same applies with a FRACTION in place
|
|
924
|
+
* of the 0: what leaves for the layer is your rig scaled by `wall.modeSwitch.factor`.
|
|
837
925
|
*/
|
|
838
926
|
setViewRig: (rig) => {
|
|
839
927
|
win.viewRig = rig || null;
|
|
@@ -884,10 +972,18 @@ class Inline3D {
|
|
|
884
972
|
* On success the session fires `renderingmodechange` — see {@link on}. That event, not
|
|
885
973
|
* this promise, is when the new mode is in effect.
|
|
886
974
|
*
|
|
975
|
+
* EASED BY DEFAULT (`createInline3D({modeSwitch})`). A GOING-FLAT request (`viewCount === 1`)
|
|
976
|
+
* is HELD while the disparity ramps out, and forwarded only when it lands — so this promise
|
|
977
|
+
* resolves when the browser actually got the request, roughly `durationMs` later, and the
|
|
978
|
+
* panel flips on already-flat content. A request that a reversal drops in that window
|
|
979
|
+
* rejects with an `Error` named `superseded`; nothing was ever asked of the display. Coming
|
|
980
|
+
* BACK is unchanged in timing: the request goes out at once and the disparity eases in when
|
|
981
|
+
* the panel reports 3D.
|
|
982
|
+
*
|
|
887
983
|
* @param {number} modeIndex
|
|
888
984
|
* @returns {Promise<void>}
|
|
889
985
|
*/
|
|
890
|
-
requestRenderingMode: (modeIndex) => this.
|
|
986
|
+
requestRenderingMode: (modeIndex) => this._requestRenderingModeEased(modeIndex, win),
|
|
891
987
|
/**
|
|
892
988
|
* SUGAR over {@link requestRenderingMode}, and nothing more. `false` requests the first
|
|
893
989
|
* mode with `viewCount === 1 && isRequestable`; `true` requests the first with
|
|
@@ -910,6 +1006,11 @@ class Inline3D {
|
|
|
910
1006
|
* otherwise exactly as `requestRenderingMode` does. Resolves to the boolean asked for —
|
|
911
1007
|
* the request was accepted; the mode is in force when the event says so.
|
|
912
1008
|
*
|
|
1009
|
+
* Eased by default, exactly as {@link requestRenderingMode} is: `false` ramps the disparity
|
|
1010
|
+
* out before the request goes anywhere, `true` requests first and eases the disparity back
|
|
1011
|
+
* in once the panel reports 3D, and pressing the pair in quick succession reverses cleanly
|
|
1012
|
+
* rather than firing a stale switch.
|
|
1013
|
+
*
|
|
913
1014
|
* @param {boolean} enabled
|
|
914
1015
|
* @returns {Promise<boolean>}
|
|
915
1016
|
*/
|
|
@@ -968,7 +1069,7 @@ class Inline3D {
|
|
|
968
1069
|
|
|
969
1070
|
/** Ask the runtime to switch the display to `modeIndex`. Pass-through; see the handle's doc. */
|
|
970
1071
|
requestRenderingMode(modeIndex) {
|
|
971
|
-
return this.
|
|
1072
|
+
return this._requestRenderingModeEased(modeIndex, null);
|
|
972
1073
|
}
|
|
973
1074
|
|
|
974
1075
|
/** Sugar over {@link requestRenderingMode}: false -> a 1-view mode, true -> the 2-view mode. */
|
|
@@ -994,6 +1095,22 @@ class Inline3D {
|
|
|
994
1095
|
return this._stereoCollapsed;
|
|
995
1096
|
}
|
|
996
1097
|
|
|
1098
|
+
/**
|
|
1099
|
+
* The eased 2D<->3D transition, live: `{active, factor}`.
|
|
1100
|
+
*
|
|
1101
|
+
* `factor` is what every window's `ipdFactor`/`parallaxFactor` is being multiplied by on the way
|
|
1102
|
+
* to the layer — `1` in 3D, `0` flat, in between mid-ramp. `active` is true while a
|
|
1103
|
+
* page-initiated switch is in any of its phases: ramping the disparity out, holding the ->2D
|
|
1104
|
+
* request until it lands, waiting for the panel to report 3D, or easing back in.
|
|
1105
|
+
*
|
|
1106
|
+
* Read-only and purely informational — a page that wants to grey a button or cross-fade some 2D
|
|
1107
|
+
* chrome alongside the panel can, and one that does not care never has to look. The SDK adds no
|
|
1108
|
+
* UI of its own for this, and never will: which key or button toggles the display is the page's.
|
|
1109
|
+
*/
|
|
1110
|
+
get modeSwitch() {
|
|
1111
|
+
return { active: this._msTransitionActive(), factor: this._stereoFactor };
|
|
1112
|
+
}
|
|
1113
|
+
|
|
997
1114
|
close() {
|
|
998
1115
|
try {
|
|
999
1116
|
this.session.end();
|
|
@@ -1009,14 +1126,15 @@ class Inline3D {
|
|
|
1009
1126
|
|
|
1010
1127
|
/**
|
|
1011
1128
|
* The rig this window's layer should actually be holding right now: what the page asked for,
|
|
1012
|
-
*
|
|
1013
|
-
*
|
|
1129
|
+
* scaled by the manager's current stereo factor (0 while a 1-view mode is active, 1 in 3D, and
|
|
1130
|
+
* everything between during an eased transition). Null means "say nothing" — leave the browser
|
|
1131
|
+
* on the `virtualDisplayHeight` shorthand it was built with.
|
|
1014
1132
|
*
|
|
1015
1133
|
* Used in BOTH directions (push at a live layer, build a new one), which is the point: a tile
|
|
1016
1134
|
* that scrolls away and rebuilds while stereo is off must not come back in 3D.
|
|
1017
1135
|
*/
|
|
1018
1136
|
_effectiveViewRig(win) {
|
|
1019
|
-
if (
|
|
1137
|
+
if (this._stereoFactor >= 1) {
|
|
1020
1138
|
// `stereoSynthRig`: this window never had a rig of its own, so going flat had to SEND one
|
|
1021
1139
|
// (there is no way to say "the default, but flat" as a scalar). Coming back therefore has
|
|
1022
1140
|
// to send the un-flat version explicitly too — returning null here would leave the layer
|
|
@@ -1030,7 +1148,7 @@ class Inline3D {
|
|
|
1030
1148
|
// hold the flattened rig forever. Recorded here rather than in the collapse itself because a
|
|
1031
1149
|
// window CREATED while the panel is already flat goes down this path on its first activate.
|
|
1032
1150
|
if (!win.viewRig) win.stereoSynthRig = true;
|
|
1033
|
-
return
|
|
1151
|
+
return scaledRig(win.viewRig || defaultDisplayRig(win), this._stereoFactor);
|
|
1034
1152
|
}
|
|
1035
1153
|
|
|
1036
1154
|
/**
|
|
@@ -1039,12 +1157,25 @@ class Inline3D {
|
|
|
1039
1157
|
* Driven ONLY by what the display reports — the first `getRenderingModes()` read and every
|
|
1040
1158
|
* `renderingmodechange` — never by a request. That is what makes a refused request a no-op in
|
|
1041
1159
|
* both directions: nothing here runs unless the mode actually changed.
|
|
1160
|
+
*
|
|
1161
|
+
* THE REPORT OWNS THE FACTOR ONLY WHEN THE SEQUENCER DOES NOT. A mode change the page did not
|
|
1162
|
+
* ask for (another tab, the shell, a panel that opened flat) snaps, because there is nothing to
|
|
1163
|
+
* ramp FROM — the transition is a page-initiated aesthetic, not a correctness step. The two
|
|
1164
|
+
* exceptions are the two halves of a page-initiated switch: while a ramp is in flight it owns
|
|
1165
|
+
* the factor outright, and a report of 3D that a `->3D` request armed starts the up-ramp here
|
|
1166
|
+
* rather than snapping (the whole reason that request fires first and eases second).
|
|
1042
1167
|
*/
|
|
1043
1168
|
_setStereoCollapsed(collapsed) {
|
|
1044
1169
|
const next = !!collapsed;
|
|
1045
1170
|
if (this._stereoCollapsed === next) return;
|
|
1046
1171
|
this._stereoCollapsed = next;
|
|
1047
1172
|
if (next) noteAutoCollapse();
|
|
1173
|
+
if (!next && this._msArmedUp) {
|
|
1174
|
+
this._msArmedUp = false;
|
|
1175
|
+
this._startUpRamp(); // the panel is in 3D at last — ease the disparity back in
|
|
1176
|
+
} else if (!this._modeSwitch.active()) {
|
|
1177
|
+
this._stereoFactor = next ? 0 : 1;
|
|
1178
|
+
}
|
|
1048
1179
|
for (const win of this._windows.values()) {
|
|
1049
1180
|
// Diagnostics only: the factors that were in force when the panel went flat. The restore
|
|
1050
1181
|
// itself just re-pushes `win.viewRig`, which was never mutated.
|
|
@@ -1161,9 +1292,236 @@ class Inline3D {
|
|
|
1161
1292
|
);
|
|
1162
1293
|
}
|
|
1163
1294
|
|
|
1295
|
+
// ── the eased 2D<->3D transition ──────────────────────────────────────────────────────
|
|
1296
|
+
//
|
|
1297
|
+
// Every PAGE-INITIATED mode request goes through here; a mode change reported from elsewhere
|
|
1298
|
+
// does not (see _setStereoCollapsed). The asymmetry below is the whole helper, and it is the
|
|
1299
|
+
// native `dxr::ModeSwitch` contract, unchanged:
|
|
1300
|
+
//
|
|
1301
|
+
// -> 2D : ramp the disparity out FIRST, and fire the request only when it lands, so the panel
|
|
1302
|
+
// flips on already-flat content instead of snapping a stereo image flat.
|
|
1303
|
+
// -> 3D : fire the request FIRST and ease the disparity in afterwards — and in the browser,
|
|
1304
|
+
// only once the panel REPORTS 3D, because until then the disparity would be going up
|
|
1305
|
+
// on a flat panel, which is the double-image the whole mode API exists to prevent.
|
|
1306
|
+
//
|
|
1307
|
+
// Everything else is fall-through: the sequencer disabled, a browser with no `setViewRig` (there
|
|
1308
|
+
// is nothing to ramp), an unknown target or current view count, a `viewCount > 2` mode the
|
|
1309
|
+
// browser will refuse anyway, and a same-dimensionality change (2D->2D, 3D->3D) which needs no
|
|
1310
|
+
// flatten at all.
|
|
1311
|
+
|
|
1312
|
+
/** True while a page-initiated transition is in flight in any of its phases. */
|
|
1313
|
+
_msTransitionActive() {
|
|
1314
|
+
return this._modeSwitch.active() || this._msArmedUp || this._msFire !== null;
|
|
1315
|
+
}
|
|
1316
|
+
|
|
1317
|
+
/**
|
|
1318
|
+
* `requestRenderingMode()` with the transition applied. Resolves when the request has actually
|
|
1319
|
+
* been FORWARDED to the browser (so, for a ->2D switch, after the ramp) and rejects exactly as
|
|
1320
|
+
* the pass-through does — plus one new failure: an `Error` named `superseded` when a second
|
|
1321
|
+
* request replaced this one before it ever fired.
|
|
1322
|
+
*/
|
|
1323
|
+
async _requestRenderingModeEased(modeIndex, win) {
|
|
1324
|
+
if (!this._msEnabled || !hasViewRig()) return this._requestRenderingMode(modeIndex, win);
|
|
1325
|
+
// The mode table is what says whether this index is 2D or 3D. It is normally already cached
|
|
1326
|
+
// (the first activation primes it), and a read that fails just means the sequencer has no
|
|
1327
|
+
// opinion — the request still goes out.
|
|
1328
|
+
let modes = this._modes;
|
|
1329
|
+
if (!Array.isArray(modes) || modes.length === 0) {
|
|
1330
|
+
try {
|
|
1331
|
+
modes = await this._getRenderingModes(win);
|
|
1332
|
+
} catch {
|
|
1333
|
+
modes = null;
|
|
1334
|
+
}
|
|
1335
|
+
}
|
|
1336
|
+
const target = (Array.isArray(modes) ? modes : []).find((m) => m && m.modeIndex === modeIndex);
|
|
1337
|
+
const targetViews = target && Number.isFinite(target.viewCount) ? target.viewCount : 0;
|
|
1338
|
+
const currentViews = this._activeViewCount;
|
|
1339
|
+
if (targetViews < 1 || targetViews > 2 || currentViews < 1) {
|
|
1340
|
+
return this._requestRenderingMode(modeIndex, win);
|
|
1341
|
+
}
|
|
1342
|
+
if (targetViews === 1) {
|
|
1343
|
+
if (currentViews === 1) return this._requestRenderingMode(modeIndex, win); // 2D -> 2D
|
|
1344
|
+
return this._rampDownThenRequest(modeIndex, win);
|
|
1345
|
+
}
|
|
1346
|
+
return this._requestThenRampUp(modeIndex, win);
|
|
1347
|
+
}
|
|
1348
|
+
|
|
1349
|
+
/**
|
|
1350
|
+
* 3D -> 2D. Ramp the disparity to 0, THEN forward the request (see _advanceModeSwitch, which is
|
|
1351
|
+
* what actually fires it). The returned promise is the page's, and it settles on the forwarded
|
|
1352
|
+
* request — so `await wall.setStereoEnabled(false)` still means "the browser has it".
|
|
1353
|
+
*
|
|
1354
|
+
* A second ->2D request for the SAME mode mid-ramp is idempotent: the page gets the promise
|
|
1355
|
+
* already in flight rather than a superseded rejection, because mashing one button twice is not
|
|
1356
|
+
* an error. A different target retargets from the CURRENT disparity, seamlessly.
|
|
1357
|
+
*/
|
|
1358
|
+
_rampDownThenRequest(modeIndex, win) {
|
|
1359
|
+
if (this._msFire && this._msFire.modeIndex === modeIndex) return this._msFire.promise;
|
|
1360
|
+
this._settlePendingDown('superseded', `a request for mode ${modeIndex} replaced it`);
|
|
1361
|
+
const pending = { modeIndex, win, resolve: null, reject: null, promise: null };
|
|
1362
|
+
pending.promise = new Promise((resolve, reject) => {
|
|
1363
|
+
pending.resolve = resolve;
|
|
1364
|
+
pending.reject = reject;
|
|
1365
|
+
});
|
|
1366
|
+
this._msFire = pending;
|
|
1367
|
+
this._modeSwitch.request({
|
|
1368
|
+
targetMode: modeIndex,
|
|
1369
|
+
targetViewCount: 1,
|
|
1370
|
+
currentMode: this._activeModeIndex,
|
|
1371
|
+
currentViewCount: this._activeViewCount,
|
|
1372
|
+
// The value ON SCREEN right now: the ramp's own output mid-flight, and the page's steady
|
|
1373
|
+
// rig (factor 1) when idle. Passing the sequencer's internal 0 while idle is the classic
|
|
1374
|
+
// first-press snap — there would be nothing to ramp down from.
|
|
1375
|
+
current: this._stereoFactor,
|
|
1376
|
+
steady: 1,
|
|
1377
|
+
});
|
|
1378
|
+
this._msLastMs = nowMs();
|
|
1379
|
+
this._armModeSwitchTick();
|
|
1380
|
+
return pending.promise;
|
|
1381
|
+
}
|
|
1382
|
+
|
|
1383
|
+
/**
|
|
1384
|
+
* -> 3D. Forward the request NOW (the browser needs the panel moving before the disparity can
|
|
1385
|
+
* mean anything), then ease the disparity in — starting only when the panel REPORTS 3D, which
|
|
1386
|
+
* is `_setStereoCollapsed(false)` releasing the latch.
|
|
1387
|
+
*
|
|
1388
|
+
* The one case that does not wait: a REVERSAL of a ramp-down that never fired. The panel never
|
|
1389
|
+
* left 3D, so there is no report coming; the disparity just walks back up from wherever the
|
|
1390
|
+
* ramp got to, and the stale 2D request is dropped rather than fired.
|
|
1391
|
+
*/
|
|
1392
|
+
_requestThenRampUp(modeIndex, win) {
|
|
1393
|
+
const reversal = this._msFire !== null;
|
|
1394
|
+
const noopReversal = reversal && modeIndex === this._activeModeIndex;
|
|
1395
|
+
this._settlePendingDown('superseded', `a request for mode ${modeIndex} reversed it`);
|
|
1396
|
+
// A reversal back to the mode that is STILL active asks the browser for nothing: the runtime
|
|
1397
|
+
// never changed mode, so the only thing owed is the disparity.
|
|
1398
|
+
const forwarded = noopReversal
|
|
1399
|
+
? Promise.resolve(undefined)
|
|
1400
|
+
: this._requestRenderingMode(modeIndex, win);
|
|
1401
|
+
if (this._stereoFactor < 1) {
|
|
1402
|
+
if (this._stereoCollapsed) {
|
|
1403
|
+
// The panel is really flat: hold at 0 and wait for it to say otherwise.
|
|
1404
|
+
this._msArmedUp = true;
|
|
1405
|
+
this._modeSwitch.cancel();
|
|
1406
|
+
this._stereoFactor = 0;
|
|
1407
|
+
} else {
|
|
1408
|
+
this._startUpRamp();
|
|
1409
|
+
}
|
|
1410
|
+
}
|
|
1411
|
+
return forwarded.catch((err) => {
|
|
1412
|
+
// Refused. Nothing about the panel moved, so neither may the disparity: drop the armed
|
|
1413
|
+
// up-ramp and settle back on whatever the display last REPORTED.
|
|
1414
|
+
if (this._msArmedUp) {
|
|
1415
|
+
this._msArmedUp = false;
|
|
1416
|
+
if (!this._modeSwitch.active()) this._stereoFactor = this._stereoCollapsed ? 0 : 1;
|
|
1417
|
+
}
|
|
1418
|
+
throw err;
|
|
1419
|
+
});
|
|
1420
|
+
}
|
|
1421
|
+
|
|
1422
|
+
/**
|
|
1423
|
+
* Start (or restart) the up-ramp from the current disparity to the page's steady rig. Used both
|
|
1424
|
+
* when the panel reports 3D after a `->3D` request and when a ->2D request was REFUSED — a
|
|
1425
|
+
* refusal must leave the page in 3D, not flat.
|
|
1426
|
+
*
|
|
1427
|
+
* No request is ever fired from here: whatever there was to send went out before the ramp
|
|
1428
|
+
* started, which is why `_msFire` is empty by construction.
|
|
1429
|
+
*/
|
|
1430
|
+
_startUpRamp() {
|
|
1431
|
+
this._settlePendingDown('superseded', 'the display returned to 3D');
|
|
1432
|
+
this._modeSwitch.request({
|
|
1433
|
+
targetMode: this._activeModeIndex,
|
|
1434
|
+
targetViewCount: 2,
|
|
1435
|
+
currentMode: this._activeModeIndex, // equal ⇒ the sequencer fires nothing
|
|
1436
|
+
currentViewCount: 2,
|
|
1437
|
+
current: this._stereoFactor,
|
|
1438
|
+
steady: 1,
|
|
1439
|
+
});
|
|
1440
|
+
this._msLastMs = nowMs();
|
|
1441
|
+
this._armModeSwitchTick();
|
|
1442
|
+
}
|
|
1443
|
+
|
|
1444
|
+
/** Settle a held ->2D request that will now never fire. Never throws into the caller. */
|
|
1445
|
+
_settlePendingDown(name, why) {
|
|
1446
|
+
const pending = this._msFire;
|
|
1447
|
+
if (!pending) return;
|
|
1448
|
+
this._msFire = null;
|
|
1449
|
+
const err = new Error(
|
|
1450
|
+
`[inline3d] the request for rendering mode ${pending.modeIndex} was never forwarded: ${why}. ` +
|
|
1451
|
+
'A ->2D switch is held until the disparity has ramped out, so a request that is reversed ' +
|
|
1452
|
+
'or replaced in that window is dropped rather than fired late.'
|
|
1453
|
+
);
|
|
1454
|
+
err.name = name;
|
|
1455
|
+
pending.reject(err);
|
|
1456
|
+
}
|
|
1457
|
+
|
|
1458
|
+
/**
|
|
1459
|
+
* Advance the transition by WALL-CLOCK dt and act on what it says. Called from the session's
|
|
1460
|
+
* frame loop and from the fallback tick; both are safe because the ramp is time-based, so a
|
|
1461
|
+
* double advance in one frame moves it by dt = 0.
|
|
1462
|
+
*
|
|
1463
|
+
* Rigs are pushed only when the factor actually MOVED — an idle manager must not re-push every
|
|
1464
|
+
* frame, and a landed ramp pushes its last value once.
|
|
1465
|
+
*/
|
|
1466
|
+
_advanceModeSwitch() {
|
|
1467
|
+
if (!this._modeSwitch.active()) {
|
|
1468
|
+
this._msLastMs = null;
|
|
1469
|
+
this._disarmModeSwitchTick();
|
|
1470
|
+
return;
|
|
1471
|
+
}
|
|
1472
|
+
const now = nowMs();
|
|
1473
|
+
const dt = typeof this._msLastMs === 'number' ? Math.max(0, (now - this._msLastMs) / 1000) : 0;
|
|
1474
|
+
this._msLastMs = now;
|
|
1475
|
+
const out = this._modeSwitch.update(dt);
|
|
1476
|
+
if (out.factor !== this._stereoFactor) {
|
|
1477
|
+
this._stereoFactor = out.factor;
|
|
1478
|
+
for (const win of this._windows.values()) this._pushViewRig(win);
|
|
1479
|
+
}
|
|
1480
|
+
if (out.fire && this._msFire) {
|
|
1481
|
+
const pending = this._msFire;
|
|
1482
|
+
this._msFire = null;
|
|
1483
|
+
this._requestRenderingMode(pending.modeIndex, pending.win).then(
|
|
1484
|
+
(v) => pending.resolve(v),
|
|
1485
|
+
(err) => {
|
|
1486
|
+
// The panel refused to go flat, so the page must not be left flat either — ease the
|
|
1487
|
+
// disparity back to steady before handing the rejection on.
|
|
1488
|
+
this._startUpRamp();
|
|
1489
|
+
pending.reject(err);
|
|
1490
|
+
}
|
|
1491
|
+
);
|
|
1492
|
+
}
|
|
1493
|
+
if (!this._modeSwitch.active()) {
|
|
1494
|
+
this._msLastMs = null;
|
|
1495
|
+
this._disarmModeSwitchTick();
|
|
1496
|
+
}
|
|
1497
|
+
}
|
|
1498
|
+
|
|
1499
|
+
/**
|
|
1500
|
+
* A timer that advances the ramp when SESSION FRAMES are not arriving. The frame loop is the
|
|
1501
|
+
* normal driver, but a held ->2D request must not sit forever because every tile scrolled away,
|
|
1502
|
+
* the tab went background, or the page simply has no live layer — the page awaited a promise
|
|
1503
|
+
* and the browser is owed a request.
|
|
1504
|
+
*/
|
|
1505
|
+
_armModeSwitchTick() {
|
|
1506
|
+
if (this._msTick !== null || typeof setTimeout !== 'function') return;
|
|
1507
|
+
this._msTick = setTimeout(() => {
|
|
1508
|
+
this._msTick = null;
|
|
1509
|
+
if (!this._running) return;
|
|
1510
|
+
this._advanceModeSwitch();
|
|
1511
|
+
if (this._modeSwitch.active()) this._armModeSwitchTick();
|
|
1512
|
+
}, MODE_SWITCH_TICK_MS);
|
|
1513
|
+
}
|
|
1514
|
+
|
|
1515
|
+
_disarmModeSwitchTick() {
|
|
1516
|
+
if (this._msTick === null) return;
|
|
1517
|
+
if (typeof clearTimeout === 'function') clearTimeout(this._msTick);
|
|
1518
|
+
this._msTick = null;
|
|
1519
|
+
}
|
|
1520
|
+
|
|
1164
1521
|
/**
|
|
1165
1522
|
* The sugar behind `setStereoEnabled` — pick a mode by view count and request it. Nothing
|
|
1166
|
-
* else: the rig follows the resulting `renderingmodechange
|
|
1523
|
+
* else: the rig follows the resulting `renderingmodechange` (eased, when a transition is
|
|
1524
|
+
* configured), not this call.
|
|
1167
1525
|
*/
|
|
1168
1526
|
async _setStereoEnabled(enabled, win) {
|
|
1169
1527
|
const want = enabled ? 2 : 1;
|
|
@@ -1177,8 +1535,11 @@ class Inline3D {
|
|
|
1177
1535
|
'and cannot invent one — read getRenderingModes() and drive the list yourself.'
|
|
1178
1536
|
);
|
|
1179
1537
|
}
|
|
1180
|
-
|
|
1181
|
-
|
|
1538
|
+
// Already there ⇒ the request would be a no-op... UNLESS a transition is in flight, in which
|
|
1539
|
+
// case this is the user reversing the toggle and the disparity still has to walk back. Taking
|
|
1540
|
+
// the early-out there would leave a page that pressed 2D then 3D stuck part-way flat.
|
|
1541
|
+
if (mode.isActive && !this._msTransitionActive()) return !!enabled;
|
|
1542
|
+
await this._requestRenderingModeEased(mode.modeIndex, win);
|
|
1182
1543
|
return !!enabled;
|
|
1183
1544
|
}
|
|
1184
1545
|
|
|
@@ -1897,6 +2258,10 @@ class Inline3D {
|
|
|
1897
2258
|
_frame(t, f) {
|
|
1898
2259
|
if (!this._running) return;
|
|
1899
2260
|
this._requestFrame();
|
|
2261
|
+
// The 2D<->3D ramp, on WALL-CLOCK dt (never a frame count, so it lasts the same wall time at
|
|
2262
|
+
// 30 fps and 144 fps). Before the windows, so the rig this frame's views are located against
|
|
2263
|
+
// is the ramped one. No-op — and pushes nothing — when no transition is in flight.
|
|
2264
|
+
this._advanceModeSwitch();
|
|
1900
2265
|
const pose = this.refSpace ? f.getViewerPose(this.refSpace) : null;
|
|
1901
2266
|
const views = pose ? pose.views : null;
|
|
1902
2267
|
for (const win of this._windows.values()) {
|
|
@@ -2045,6 +2410,12 @@ class Inline3D {
|
|
|
2045
2410
|
if (liveManager === this) liveManager = null;
|
|
2046
2411
|
this._unbindLifecycle();
|
|
2047
2412
|
this._disarmDprWatch();
|
|
2413
|
+
// A transition in flight dies with the session: nothing will drive the ramp, and the held
|
|
2414
|
+
// request has nowhere to go — so the page's promise is settled rather than left pending.
|
|
2415
|
+
this._disarmModeSwitchTick();
|
|
2416
|
+
this._modeSwitch.cancel();
|
|
2417
|
+
this._msArmedUp = false;
|
|
2418
|
+
this._settlePendingDown('closed', 'the inline-3D session closed first');
|
|
2048
2419
|
if (this._observer) this._observer.disconnect();
|
|
2049
2420
|
for (const win of this._windows.values()) {
|
|
2050
2421
|
this._stopOverlayScan(win);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@displayxr/inline3d",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.5.0",
|
|
4
4
|
"description": "Turn any HTML <canvas> into a glasses-free-3D window on a DisplayXR display, inside an ordinary web page. Dependency-free; progressive enhancement (falls back to plain 2D everywhere else).",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"types": "./index.d.ts",
|
|
@@ -36,6 +36,7 @@
|
|
|
36
36
|
"files": [
|
|
37
37
|
"js/inline3d.js",
|
|
38
38
|
"js/inline3d-undock.js",
|
|
39
|
+
"js/inline3d-mode-switch.js",
|
|
39
40
|
"js/inline3d-three.js",
|
|
40
41
|
"js/inline3d-viewer.js",
|
|
41
42
|
"js/inline3d-splat.js",
|