@displayxr/inline3d-tools 0.0.1 → 0.1.1
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/AGENTS.md +351 -0
- package/LICENSE +190 -0
- package/README.md +204 -1
- package/bin/inline3d-tools.mjs +40 -0
- package/lib/assets/asset-check.mjs +262 -0
- package/lib/assets/blender/prep.py +74 -0
- package/lib/assets/prep.mjs +50 -0
- package/lib/check/analyze.mjs +244 -0
- package/lib/check/cdp.mjs +198 -0
- package/lib/check/cli.mjs +66 -0
- package/lib/check/fake-env.js +435 -0
- package/lib/check/png.mjs +75 -0
- package/lib/check/probe.js +245 -0
- package/lib/check/run.mjs +811 -0
- package/lib/check/server.mjs +33 -0
- package/lib/gen.mjs +172 -0
- package/lib/mcp.mjs +168 -0
- package/lib/scaffold.mjs +62 -0
- package/package.json +50 -4
- package/templates/app.js +153 -0
- package/templates/esm/index.html +29 -0
- package/templates/style.css +17 -0
- package/templates/vite/index.html +20 -0
- package/templates/vite/package.template.json +18 -0
|
@@ -0,0 +1,435 @@
|
|
|
1
|
+
// inline3d-check — a FAKE DisplayXR inline-3D environment, injected at document start (before any
|
|
2
|
+
// page script) so @displayxr/inline3d believes it runs in the DisplayXR Browser.
|
|
3
|
+
//
|
|
4
|
+
// What it provides (the surface js/inline3d.js probes and calls):
|
|
5
|
+
// navigator.xr.requestSession('inline-3d') -> a session whose requestAnimationFrame rides the
|
|
6
|
+
// window's, with renderState, requestReferenceSpace, trackingState 'tracking', end().
|
|
7
|
+
// navigator.xr.isSessionSupported -> true for 'inline-3d' (and the call is RECORDED: the
|
|
8
|
+
// docs say never to gate on it).
|
|
9
|
+
// window.XRDisplayLayer(session, canvas, init) with setViewRig, getViewport, getDisplayInfo,
|
|
10
|
+
// getRenderingModes, requestRenderingMode, setViewerIndependent, close, and the static
|
|
11
|
+
// occlusionByDrawOrder = true (a current, draw-order-occlusion browser).
|
|
12
|
+
//
|
|
13
|
+
// The views are GEOMETRIC, not a test pattern: two eyes 63 mm apart at the nominal viewer distance
|
|
14
|
+
// (0.65 m) in front of a 0.3456 m wide panel, Kooima off-axis frusta through the canvas's own rect,
|
|
15
|
+
// scaled by the layer's rig exactly as the runtime does (display rig: m2v = virtualDisplayHeight /
|
|
16
|
+
// the element's physical height; camera rig: eye offset ±ipd·m2v/2, frustum skewed so the
|
|
17
|
+
// convergence distance has zero disparity). So a subject on z = 0 has zero disparity, a subject in
|
|
18
|
+
// front is crossed, behind is uncrossed — the harness can measure the page's depth in millimetres.
|
|
19
|
+
//
|
|
20
|
+
// XRView / XRViewport fields are prototype GETTERS, as on the real browser, so a page that copies
|
|
21
|
+
// them with Object.assign / spread gets nothing here too (a black canvas on the real browser that
|
|
22
|
+
// works in a harness returning plain objects).
|
|
23
|
+
//
|
|
24
|
+
// Everything the harness reads is on window.__dxrCheck.
|
|
25
|
+
(() => {
|
|
26
|
+
if (window.__dxrCheck) return;
|
|
27
|
+
const CFG = window.__DXR_CHECK_CFG || {};
|
|
28
|
+
const DISPLAY = { widthM: 0.3456, heightM: 0.1944, px: [1920, 1080], viewerM: 0.65, eyeSepM: 0.063 };
|
|
29
|
+
const CANVAS_W = Object.getOwnPropertyDescriptor(HTMLCanvasElement.prototype, 'width');
|
|
30
|
+
const CANVAS_H = Object.getOwnPropertyDescriptor(HTMLCanvasElement.prototype, 'height');
|
|
31
|
+
const now = () => performance.now();
|
|
32
|
+
|
|
33
|
+
const H = (window.__dxrCheck = {
|
|
34
|
+
display: DISPLAY,
|
|
35
|
+
sessionRequests: [], // { at, mode, ok }
|
|
36
|
+
liveSessions: 0,
|
|
37
|
+
maxLiveSessions: 0,
|
|
38
|
+
isSessionSupportedCalls: [],
|
|
39
|
+
layers: [], // records, see FakeLayer
|
|
40
|
+
frames: 0,
|
|
41
|
+
framesWithViews: 0,
|
|
42
|
+
viewportCalls: 0,
|
|
43
|
+
modeRequests: [],
|
|
44
|
+
activeModeIndex: 1,
|
|
45
|
+
errors: [],
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
// ---------------------------------------------------------------- tiny mat4 (column-major)
|
|
49
|
+
function quatToMat(q) {
|
|
50
|
+
const x = q.x || 0, y = q.y || 0, z = q.z || 0, w = q.w === undefined ? 1 : q.w;
|
|
51
|
+
const n = Math.hypot(x, y, z, w) || 1;
|
|
52
|
+
const X = x / n, Y = y / n, Z = z / n, W = w / n;
|
|
53
|
+
return [
|
|
54
|
+
1 - 2 * (Y * Y + Z * Z), 2 * (X * Y + Z * W), 2 * (X * Z - Y * W), 0,
|
|
55
|
+
2 * (X * Y - Z * W), 1 - 2 * (X * X + Z * Z), 2 * (Y * Z + X * W), 0,
|
|
56
|
+
2 * (X * Z + Y * W), 2 * (Y * Z - X * W), 1 - 2 * (X * X + Y * Y), 0,
|
|
57
|
+
0, 0, 0, 1,
|
|
58
|
+
];
|
|
59
|
+
}
|
|
60
|
+
function poseMatrix(pos, quat, eye) {
|
|
61
|
+
const m = quatToMat(quat || {});
|
|
62
|
+
const px = (pos && pos.x) || 0, py = (pos && pos.y) || 0, pz = (pos && pos.z) || 0;
|
|
63
|
+
m[12] = px + m[0] * eye[0] + m[4] * eye[1] + m[8] * eye[2];
|
|
64
|
+
m[13] = py + m[1] * eye[0] + m[5] * eye[1] + m[9] * eye[2];
|
|
65
|
+
m[14] = pz + m[2] * eye[0] + m[6] * eye[1] + m[10] * eye[2];
|
|
66
|
+
return new Float32Array(m);
|
|
67
|
+
}
|
|
68
|
+
function invertRigid(m) {
|
|
69
|
+
// rotation transpose, translation -R^T t
|
|
70
|
+
const r = new Float32Array(16);
|
|
71
|
+
r[0] = m[0]; r[1] = m[4]; r[2] = m[8];
|
|
72
|
+
r[4] = m[1]; r[5] = m[5]; r[6] = m[9];
|
|
73
|
+
r[8] = m[2]; r[9] = m[6]; r[10] = m[10];
|
|
74
|
+
r[12] = -(m[0] * m[12] + m[1] * m[13] + m[2] * m[14]);
|
|
75
|
+
r[13] = -(m[4] * m[12] + m[5] * m[13] + m[6] * m[14]);
|
|
76
|
+
r[14] = -(m[8] * m[12] + m[9] * m[13] + m[10] * m[14]);
|
|
77
|
+
r[15] = 1;
|
|
78
|
+
return r;
|
|
79
|
+
}
|
|
80
|
+
function frustum(l, r, b, t, n, f) {
|
|
81
|
+
const o = new Float32Array(16);
|
|
82
|
+
o[0] = (2 * n) / (r - l); o[5] = (2 * n) / (t - b);
|
|
83
|
+
o[8] = (r + l) / (r - l); o[9] = (t + b) / (t - b);
|
|
84
|
+
o[10] = -(f + n) / (f - n); o[11] = -1; o[14] = (-2 * f * n) / (f - n);
|
|
85
|
+
return o;
|
|
86
|
+
}
|
|
87
|
+
const num = (v, d) => (typeof v === 'number' && isFinite(v) ? v : d);
|
|
88
|
+
|
|
89
|
+
// ---------------------------------------------------------------- XR object shapes (getters)
|
|
90
|
+
class FakeRigidTransform {
|
|
91
|
+
constructor(m) { this._m = m; }
|
|
92
|
+
get matrix() { return this._m; }
|
|
93
|
+
get position() { return new DOMPointReadOnly(this._m[12], this._m[13], this._m[14], 1); }
|
|
94
|
+
get orientation() {
|
|
95
|
+
const m = this._m, tr = m[0] + m[5] + m[10];
|
|
96
|
+
let x, y, z, w;
|
|
97
|
+
if (tr > 0) { const s = Math.sqrt(tr + 1) * 2; w = 0.25 * s; x = (m[6] - m[9]) / s; y = (m[8] - m[2]) / s; z = (m[1] - m[4]) / s; }
|
|
98
|
+
else if (m[0] > m[5] && m[0] > m[10]) { const s = Math.sqrt(1 + m[0] - m[5] - m[10]) * 2; w = (m[6] - m[9]) / s; x = 0.25 * s; y = (m[4] + m[1]) / s; z = (m[8] + m[2]) / s; }
|
|
99
|
+
else if (m[5] > m[10]) { const s = Math.sqrt(1 + m[5] - m[0] - m[10]) * 2; w = (m[8] - m[2]) / s; x = (m[4] + m[1]) / s; y = 0.25 * s; z = (m[9] + m[6]) / s; }
|
|
100
|
+
else { const s = Math.sqrt(1 + m[10] - m[0] - m[5]) * 2; w = (m[1] - m[4]) / s; x = (m[8] + m[2]) / s; y = (m[9] + m[6]) / s; z = 0.25 * s; }
|
|
101
|
+
return new DOMPointReadOnly(x, y, z, w);
|
|
102
|
+
}
|
|
103
|
+
get inverse() { return new FakeRigidTransform(invertRigid(this._m)); }
|
|
104
|
+
}
|
|
105
|
+
class FakeXRView {
|
|
106
|
+
constructor(eye, index, proj, xf) { this._eye = eye; this._i = index; this._p = proj; this._t = new FakeRigidTransform(xf); }
|
|
107
|
+
get eye() { return this._eye; }
|
|
108
|
+
get projectionMatrix() { return this._p; }
|
|
109
|
+
get transform() { return this._t; }
|
|
110
|
+
get recommendedViewportScale() { return null; }
|
|
111
|
+
requestViewportScale() {}
|
|
112
|
+
}
|
|
113
|
+
class FakeXRViewport {
|
|
114
|
+
constructor(x, y, w, h) { this._v = [x, y, w, h]; }
|
|
115
|
+
get x() { return this._v[0]; }
|
|
116
|
+
get y() { return this._v[1]; }
|
|
117
|
+
get width() { return this._v[2]; }
|
|
118
|
+
get height() { return this._v[3]; }
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
// ---------------------------------------------------------------- views from a layer's rig
|
|
122
|
+
function computeViews(session, L) {
|
|
123
|
+
const c = L.canvas;
|
|
124
|
+
const r = c.getBoundingClientRect();
|
|
125
|
+
const cssW = r.width || CANVAS_W.get.call(c) / 2 || 1;
|
|
126
|
+
const cssH = r.height || CANVAS_H.get.call(c) || 1;
|
|
127
|
+
const aspect = cssW / cssH;
|
|
128
|
+
const n = num(session.renderState.depthNear, 0.1), f = num(session.renderState.depthFar, 1000);
|
|
129
|
+
const rig = L.rig || {};
|
|
130
|
+
const type = rig.type === 'camera' ? 'camera' : 'display';
|
|
131
|
+
const out = [];
|
|
132
|
+
if (type === 'display') {
|
|
133
|
+
const vdh = num(rig.virtualDisplayHeight, num(L.init.virtualDisplayHeight, 0.24));
|
|
134
|
+
// Physical: the viewport is centred on the panel; one CSS px = displayWidth / panel px.
|
|
135
|
+
const mpp = DISPLAY.widthM / DISPLAY.px[0];
|
|
136
|
+
const elemHm = Math.max(1e-6, cssH * mpp);
|
|
137
|
+
const m2v = vdh / elemHm;
|
|
138
|
+
const cx = (r.left + cssW / 2 - innerWidth / 2) * mpp;
|
|
139
|
+
const cy = -(r.top + cssH / 2 - innerHeight / 2) * mpp;
|
|
140
|
+
const ipd = DISPLAY.eyeSepM * Math.max(0, num(rig.ipdFactor, 1));
|
|
141
|
+
const W = vdh * aspect, Hh = vdh;
|
|
142
|
+
for (let i = 0; i < 2; i++) {
|
|
143
|
+
const ex = ((i ? 1 : -1) * ipd) / 2;
|
|
144
|
+
const e = [(ex - cx) * m2v, (0 - cy) * m2v, DISPLAY.viewerM * m2v];
|
|
145
|
+
const l = ((-W / 2 - e[0]) * n) / e[2], rr = ((W / 2 - e[0]) * n) / e[2];
|
|
146
|
+
const b = ((-Hh / 2 - e[1]) * n) / e[2], t = ((Hh / 2 - e[1]) * n) / e[2];
|
|
147
|
+
out.push(new FakeXRView(i ? 'right' : 'left', i, frustum(l, rr, b, t, n, f), poseMatrix(rig.position, rig.orientation, e)));
|
|
148
|
+
}
|
|
149
|
+
} else {
|
|
150
|
+
const vfov = num(rig.verticalFov, (50 * Math.PI) / 180);
|
|
151
|
+
const invC = Math.max(0, num(rig.convergenceDiopters, 0));
|
|
152
|
+
const m2v = num(rig.metersToVirtual, 0) || 1;
|
|
153
|
+
const ipd = DISPLAY.eyeSepM * Math.max(0, num(rig.ipdFactor, 1)) * m2v;
|
|
154
|
+
const tY = Math.tan(vfov / 2), tX = tY * aspect;
|
|
155
|
+
for (let i = 0; i < 2; i++) {
|
|
156
|
+
const ex = ((i ? 1 : -1) * ipd) / 2;
|
|
157
|
+
const l = (-tX - ex * invC) * n, rr = (tX - ex * invC) * n;
|
|
158
|
+
out.push(new FakeXRView(i ? 'right' : 'left', i, frustum(l, rr, -tY * n, tY * n, n, f), poseMatrix(rig.position, rig.orientation, [ex, 0, 0])));
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
return out;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
// ---------------------------------------------------------------- the layer
|
|
165
|
+
const MODES = () => [
|
|
166
|
+
{ index: 0, viewCount: 1, tileColumns: 1, tileRows: 1, viewWidthPixels: 1920, viewHeightPixels: 1080, hardwareDisplay3D: false, isActive: H.activeModeIndex === 0, isRequestable: true, name: '2D' },
|
|
167
|
+
{ index: 1, viewCount: 2, tileColumns: 2, tileRows: 1, viewWidthPixels: 960, viewHeightPixels: 1080, hardwareDisplay3D: true, isActive: H.activeModeIndex === 1, isRequestable: true, name: 'Stereo' },
|
|
168
|
+
];
|
|
169
|
+
class FakeLayer {
|
|
170
|
+
constructor(session, canvas, init) {
|
|
171
|
+
if (!(canvas instanceof HTMLCanvasElement)) throw new TypeError('XRDisplayLayer: not a canvas');
|
|
172
|
+
if (!session || session.ended) throw new DOMException('session ended', 'InvalidStateError');
|
|
173
|
+
this.session = session;
|
|
174
|
+
this.canvas = canvas;
|
|
175
|
+
this.init = init ? { ...init } : {};
|
|
176
|
+
this.rig = init && init.viewRig ? { ...init.viewRig } : null;
|
|
177
|
+
this.closed = false;
|
|
178
|
+
session._layers.push(this);
|
|
179
|
+
const sameCanvasLive = H.layers.filter((x) => x.canvas === canvas && !x.layer.closed).length;
|
|
180
|
+
const rec = {
|
|
181
|
+
layer: this,
|
|
182
|
+
canvas,
|
|
183
|
+
selector: window.__dxrProbe ? window.__dxrProbe.selectorOf(canvas) : canvas.id || 'canvas',
|
|
184
|
+
createdAt: now(),
|
|
185
|
+
closedAt: null,
|
|
186
|
+
init: safeClone(this.init),
|
|
187
|
+
rigs: this.rig ? [safeClone(this.rig)] : [],
|
|
188
|
+
rigPushes: 0,
|
|
189
|
+
sameCanvasLiveAtCreate: sameCanvasLive,
|
|
190
|
+
coverAtCreate: null,
|
|
191
|
+
joinSamples: 0,
|
|
192
|
+
joinExposed: 0,
|
|
193
|
+
lastJoinSampleAt: 0,
|
|
194
|
+
};
|
|
195
|
+
this._rec = rec;
|
|
196
|
+
H.layers.push(rec);
|
|
197
|
+
// Cover state as the first frame after construction paints (a cover the page adds in the same
|
|
198
|
+
// task as add*() counts).
|
|
199
|
+
requestAnimationFrame(() => {
|
|
200
|
+
try { rec.coverAtCreate = window.__dxrProbe.occlusionAudit(canvas, 3, 3); } catch (e) { rec.coverAtCreate = { error: String(e) }; }
|
|
201
|
+
});
|
|
202
|
+
}
|
|
203
|
+
setViewRig(rig) {
|
|
204
|
+
this.rig = rig ? { ...rig } : null;
|
|
205
|
+
this._rec.rigPushes++;
|
|
206
|
+
if (this._rec.rigs.length < 5 || this._rec.rigPushes % 60 === 0) this._rec.rigs.push(safeClone(this.rig));
|
|
207
|
+
else this._rec.rigs[this._rec.rigs.length - 1] = safeClone(this.rig);
|
|
208
|
+
}
|
|
209
|
+
setViewerIndependent(v) { this._rec.viewerIndependent = !!v; }
|
|
210
|
+
getViewport(view) {
|
|
211
|
+
H.viewportCalls++;
|
|
212
|
+
if (!view || typeof view._i !== 'number') return null;
|
|
213
|
+
// Like the real browser: canvas.width split down the middle (NOT the drawing buffer).
|
|
214
|
+
const w = CANVAS_W.get.call(this.canvas), h = CANVAS_H.get.call(this.canvas);
|
|
215
|
+
const half = Math.floor(w / 2);
|
|
216
|
+
return new FakeXRViewport(view._i * half, 0, half, h);
|
|
217
|
+
}
|
|
218
|
+
getDisplayInfo() {
|
|
219
|
+
return Promise.resolve({
|
|
220
|
+
displayWidthMeters: DISPLAY.widthM,
|
|
221
|
+
displayHeightMeters: DISPLAY.heightM,
|
|
222
|
+
displayPixelWidth: DISPLAY.px[0],
|
|
223
|
+
displayPixelHeight: DISPLAY.px[1],
|
|
224
|
+
recommendedViewScaleX: 0.5,
|
|
225
|
+
recommendedViewScaleY: 1,
|
|
226
|
+
nominalViewerPosition: new DOMPointReadOnly(0, 0, DISPLAY.viewerM, 1),
|
|
227
|
+
});
|
|
228
|
+
}
|
|
229
|
+
getRenderingModes() { return Promise.resolve(MODES()); }
|
|
230
|
+
requestRenderingMode(i) {
|
|
231
|
+
H.modeRequests.push({ at: now(), index: i });
|
|
232
|
+
const m = MODES()[i];
|
|
233
|
+
if (!m) return Promise.reject(new DOMException('no such mode', 'NotSupportedError'));
|
|
234
|
+
const was = H.activeModeIndex;
|
|
235
|
+
H.activeModeIndex = i;
|
|
236
|
+
if (was !== i) {
|
|
237
|
+
setTimeout(() => {
|
|
238
|
+
for (const s of liveSessions) {
|
|
239
|
+
const e1 = new Event('renderingmodechange'); e1.mode = MODES()[i]; e1.modeIndex = i; s.dispatchEvent(e1);
|
|
240
|
+
const e2 = new Event('hardwaredisplaystatechange'); e2.state = m.hardwareDisplay3D ? '3d' : '2d'; s.dispatchEvent(e2);
|
|
241
|
+
}
|
|
242
|
+
}, 0);
|
|
243
|
+
}
|
|
244
|
+
return Promise.resolve();
|
|
245
|
+
}
|
|
246
|
+
close() {
|
|
247
|
+
if (this.closed) return;
|
|
248
|
+
this.closed = true;
|
|
249
|
+
this._rec.closedAt = now();
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
Object.defineProperty(FakeLayer, 'occlusionByDrawOrder', { value: true, enumerable: true });
|
|
253
|
+
window.XRDisplayLayer = FakeLayer;
|
|
254
|
+
|
|
255
|
+
function safeClone(o) {
|
|
256
|
+
if (!o) return o;
|
|
257
|
+
const out = {};
|
|
258
|
+
for (const k of Object.keys(o)) {
|
|
259
|
+
const v = o[k];
|
|
260
|
+
if (v && typeof v === 'object' && 'x' in v) out[k] = { x: v.x, y: v.y, z: v.z, w: v.w };
|
|
261
|
+
else if (typeof v !== 'function') out[k] = v;
|
|
262
|
+
}
|
|
263
|
+
return out;
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
// ---------------------------------------------------------------- the session
|
|
267
|
+
const liveSessions = new Set();
|
|
268
|
+
class FakeSession extends EventTarget {
|
|
269
|
+
constructor(mode) {
|
|
270
|
+
super();
|
|
271
|
+
this.mode = mode;
|
|
272
|
+
this.ended = false;
|
|
273
|
+
this._layers = [];
|
|
274
|
+
this.renderState = { depthNear: 0.1, depthFar: 1000, inlineVerticalFieldOfView: null, baseLayer: null };
|
|
275
|
+
this.inputSources = [];
|
|
276
|
+
this.visibilityState = 'visible';
|
|
277
|
+
this.trackingState = 'tracking';
|
|
278
|
+
this.environmentBlendMode = 'opaque';
|
|
279
|
+
liveSessions.add(this);
|
|
280
|
+
H.liveSessions = liveSessions.size;
|
|
281
|
+
H.maxLiveSessions = Math.max(H.maxLiveSessions, liveSessions.size);
|
|
282
|
+
}
|
|
283
|
+
requestReferenceSpace(type) {
|
|
284
|
+
return Promise.resolve({ type, getOffsetReferenceSpace() { return this; }, addEventListener() {}, removeEventListener() {} });
|
|
285
|
+
}
|
|
286
|
+
updateRenderState(s) { Object.assign(this.renderState, s || {}); }
|
|
287
|
+
requestAnimationFrame(cb) {
|
|
288
|
+
return window.requestAnimationFrame((t) => {
|
|
289
|
+
if (this.ended) return;
|
|
290
|
+
cb(t, this._frame());
|
|
291
|
+
});
|
|
292
|
+
}
|
|
293
|
+
cancelAnimationFrame(id) { window.cancelAnimationFrame(id); }
|
|
294
|
+
end() {
|
|
295
|
+
if (!this.ended) {
|
|
296
|
+
this.ended = true;
|
|
297
|
+
liveSessions.delete(this);
|
|
298
|
+
H.liveSessions = liveSessions.size;
|
|
299
|
+
for (const L of this._layers) L.close();
|
|
300
|
+
this.dispatchEvent(new Event('end'));
|
|
301
|
+
}
|
|
302
|
+
return Promise.resolve();
|
|
303
|
+
}
|
|
304
|
+
_primaryLayer() {
|
|
305
|
+
for (let i = this._layers.length - 1; i >= 0; i--) if (!this._layers[i].closed) return this._layers[i];
|
|
306
|
+
return null;
|
|
307
|
+
}
|
|
308
|
+
_frame() {
|
|
309
|
+
H.frames++;
|
|
310
|
+
const session = this;
|
|
311
|
+
const L = this._primaryLayer();
|
|
312
|
+
let views = null;
|
|
313
|
+
return {
|
|
314
|
+
session,
|
|
315
|
+
getViewerPose() {
|
|
316
|
+
if (!L) return null;
|
|
317
|
+
if (!views) { views = computeViews(session, L); H.framesWithViews++; }
|
|
318
|
+
return { views, transform: new FakeRigidTransform(poseMatrix(null, null, [0, 0, 0])), emulatedPosition: false };
|
|
319
|
+
},
|
|
320
|
+
getPose() { return null; },
|
|
321
|
+
};
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
const xr = new EventTarget();
|
|
326
|
+
xr.isSessionSupported = (mode) => {
|
|
327
|
+
H.isSessionSupportedCalls.push({ at: now(), mode, stack: (new Error().stack || '').split('\n').slice(2, 5).join(' | ') });
|
|
328
|
+
return Promise.resolve(mode === 'inline-3d' || mode === 'inline');
|
|
329
|
+
};
|
|
330
|
+
xr.requestSession = (mode) => {
|
|
331
|
+
if (mode !== 'inline-3d' && mode !== 'inline') {
|
|
332
|
+
H.sessionRequests.push({ at: now(), mode, ok: false });
|
|
333
|
+
return Promise.reject(new DOMException(`${mode} is not supported here`, 'NotSupportedError'));
|
|
334
|
+
}
|
|
335
|
+
H.sessionRequests.push({ at: now(), mode, ok: true });
|
|
336
|
+
return Promise.resolve(new FakeSession(mode));
|
|
337
|
+
};
|
|
338
|
+
Object.defineProperty(navigator, 'xr', { value: xr, configurable: true });
|
|
339
|
+
|
|
340
|
+
// ---------------------------------------------------------------- commit-time snapshots
|
|
341
|
+
// A ResizeObserver callback runs after every rAF callback and before paint: the one moment a
|
|
342
|
+
// WebGL drawing buffer without preserveDrawingBuffer is still readable, and exactly what the
|
|
343
|
+
// document commits this frame (the technique of tools/auto3d-shim/test/fake-xr.js).
|
|
344
|
+
const pending = []; // { index, maxW, resolve }
|
|
345
|
+
function snapCanvas(c, maxW) {
|
|
346
|
+
const bw = CANVAS_W.get.call(c), bh = CANVAS_H.get.call(c);
|
|
347
|
+
if (!bw || !bh) return { bufW: bw, bufH: bh, sw: 0, sh: 0, rgba: '' };
|
|
348
|
+
const sw = Math.max(2, Math.min(maxW, bw) & ~1);
|
|
349
|
+
const sh = Math.max(1, Math.round((bh * sw) / bw));
|
|
350
|
+
const t = document.createElement('canvas');
|
|
351
|
+
t.width = sw; t.height = sh;
|
|
352
|
+
const g = t.getContext('2d', { willReadFrequently: true });
|
|
353
|
+
g.drawImage(c, 0, 0, sw, sh);
|
|
354
|
+
const d = g.getImageData(0, 0, sw, sh).data;
|
|
355
|
+
let b = '';
|
|
356
|
+
for (let i = 0; i < d.length; i += 8192) b += String.fromCharCode.apply(null, d.subarray(i, i + 8192));
|
|
357
|
+
return { bufW: bw, bufH: bh, sw, sh, rgba: btoa(b) };
|
|
358
|
+
}
|
|
359
|
+
function tinyContent(c) {
|
|
360
|
+
// Fraction of 48x24 samples that hold anything (alpha and not the dominant colour).
|
|
361
|
+
const t = document.createElement('canvas'); t.width = 48; t.height = 24;
|
|
362
|
+
const g = t.getContext('2d', { willReadFrequently: true });
|
|
363
|
+
g.drawImage(c, 0, 0, 48, 24);
|
|
364
|
+
const d = g.getImageData(0, 0, 48, 24).data;
|
|
365
|
+
const hist = new Map();
|
|
366
|
+
for (let i = 0; i < d.length; i += 4) { const k = (d[i] >> 4) | ((d[i + 1] >> 4) << 4) | ((d[i + 2] >> 4) << 8) | ((d[i + 3] >> 4) << 12); hist.set(k, (hist.get(k) || 0) + 1); }
|
|
367
|
+
let dom = 0, best = -1; for (const [k, v] of hist) if (v > best) { best = v; dom = k; }
|
|
368
|
+
const dr = (dom & 15) * 16, dg = ((dom >> 4) & 15) * 16, db = ((dom >> 8) & 15) * 16, da = ((dom >> 12) & 15) * 16;
|
|
369
|
+
let n = 0; for (let i = 0; i < d.length; i += 4) if (Math.abs(d[i] - dr) + Math.abs(d[i + 1] - dg) + Math.abs(d[i + 2] - db) + Math.abs(d[i + 3] - da) > 48) n++;
|
|
370
|
+
return n / (d.length / 4);
|
|
371
|
+
}
|
|
372
|
+
H.snap = (index, maxW = 640) => new Promise((resolve) => pending.push({ index, maxW, resolve }));
|
|
373
|
+
// The layer-loss probe: end every live session, as the browser does when the weave service goes
|
|
374
|
+
// away. The SDK then closes each layer, paints image/video windows mono and calls a scene's
|
|
375
|
+
// onLayerLost; whatever each canvas shows afterwards is what a user would be left looking at.
|
|
376
|
+
H.endSessions = () => { const n = liveSessions.size; for (const s of [...liveSessions]) s.end(); return n; };
|
|
377
|
+
|
|
378
|
+
const probe = document.createElement('div');
|
|
379
|
+
Object.assign(probe.style, { position: 'fixed', left: '-10px', top: '0', width: '1px', height: '1px', pointerEvents: 'none' });
|
|
380
|
+
const ro = new ResizeObserver(() => {
|
|
381
|
+
// 1. requested snapshots
|
|
382
|
+
while (pending.length) {
|
|
383
|
+
const p = pending.shift();
|
|
384
|
+
const rec = H.layers[p.index];
|
|
385
|
+
try { p.resolve(rec ? snapCanvas(rec.canvas, p.maxW) : null); } catch (e) { p.resolve({ error: String(e) }); }
|
|
386
|
+
}
|
|
387
|
+
// 2. join-window exposure: during a new layer's first 1.2 s (the browser's measured worst-case
|
|
388
|
+
// join), would the raw side-by-side pair be on screen? (uncovered, and the canvas holds pixels)
|
|
389
|
+
const t = now();
|
|
390
|
+
for (const rec of H.layers) {
|
|
391
|
+
if (rec.layer.closed || t - rec.createdAt > 1250 || t - rec.lastJoinSampleAt < 90) continue;
|
|
392
|
+
rec.lastJoinSampleAt = t;
|
|
393
|
+
try {
|
|
394
|
+
const a = window.__dxrProbe.occlusionAudit(rec.canvas, 3, 2);
|
|
395
|
+
if (!a.visible || a.total === 0) continue;
|
|
396
|
+
rec.joinSamples++;
|
|
397
|
+
if (a.coveredFraction < 0.5 && tinyContent(rec.canvas) > 0.01) rec.joinExposed++;
|
|
398
|
+
} catch { /* ignore */ }
|
|
399
|
+
}
|
|
400
|
+
});
|
|
401
|
+
const tick = () => { probe.style.width = probe.style.width === '1px' ? '2px' : '1px'; requestAnimationFrame(tick); };
|
|
402
|
+
const start = () => { document.documentElement.appendChild(probe); ro.observe(probe); requestAnimationFrame(tick); };
|
|
403
|
+
if (document.documentElement) start(); else document.addEventListener('DOMContentLoaded', start, { once: true });
|
|
404
|
+
|
|
405
|
+
// ---------------------------------------------------------------- summary for the harness
|
|
406
|
+
H.summary = () => ({
|
|
407
|
+
sessionRequests: H.sessionRequests,
|
|
408
|
+
liveSessions: H.liveSessions,
|
|
409
|
+
maxLiveSessions: H.maxLiveSessions,
|
|
410
|
+
isSessionSupportedCalls: H.isSessionSupportedCalls,
|
|
411
|
+
frames: H.frames,
|
|
412
|
+
framesWithViews: H.framesWithViews,
|
|
413
|
+
viewportCalls: H.viewportCalls,
|
|
414
|
+
modeRequests: H.modeRequests,
|
|
415
|
+
activeModeIndex: H.activeModeIndex,
|
|
416
|
+
layers: H.layers.map((r, i) => ({
|
|
417
|
+
index: i,
|
|
418
|
+
selector: r.selector,
|
|
419
|
+
createdAt: r.createdAt,
|
|
420
|
+
closedAt: r.closedAt,
|
|
421
|
+
open: !r.layer.closed,
|
|
422
|
+
ageMs: now() - r.createdAt,
|
|
423
|
+
init: r.init,
|
|
424
|
+
rig: r.layer.rig ? safeClone(r.layer.rig) : null,
|
|
425
|
+
rigPushes: r.rigPushes,
|
|
426
|
+
sameCanvasLiveAtCreate: r.sameCanvasLiveAtCreate,
|
|
427
|
+
coverAtCreate: r.coverAtCreate,
|
|
428
|
+
joinSamples: r.joinSamples,
|
|
429
|
+
joinExposed: r.joinExposed,
|
|
430
|
+
viewerIndependent: !!r.viewerIndependent,
|
|
431
|
+
})),
|
|
432
|
+
now: now(),
|
|
433
|
+
});
|
|
434
|
+
H.layerCanvas = (i) => (H.layers[i] ? H.layers[i].canvas : null);
|
|
435
|
+
})();
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
// Minimal PNG codec. Decoder for Chrome screenshots: 8-bit, colour type 2 (RGB) or 6 (RGBA), not interlaced.
|
|
2
|
+
// Returns { width, height, data: Uint8Array RGBA }.
|
|
3
|
+
import { inflateSync, deflateSync } from 'node:zlib';
|
|
4
|
+
|
|
5
|
+
export function decodePng(buf) {
|
|
6
|
+
if (buf.readUInt32BE(0) !== 0x89504e47) throw new Error('not a PNG');
|
|
7
|
+
let off = 8, width = 0, height = 0, depth = 0, ctype = 0, interlace = 0;
|
|
8
|
+
const idat = [];
|
|
9
|
+
while (off < buf.length) {
|
|
10
|
+
const len = buf.readUInt32BE(off);
|
|
11
|
+
const type = buf.toString('ascii', off + 4, off + 8);
|
|
12
|
+
const body = buf.subarray(off + 8, off + 8 + len);
|
|
13
|
+
if (type === 'IHDR') {
|
|
14
|
+
width = body.readUInt32BE(0); height = body.readUInt32BE(4);
|
|
15
|
+
depth = body[8]; ctype = body[9]; interlace = body[12];
|
|
16
|
+
} else if (type === 'IDAT') idat.push(body);
|
|
17
|
+
else if (type === 'IEND') break;
|
|
18
|
+
off += 12 + len;
|
|
19
|
+
}
|
|
20
|
+
if (depth !== 8 || (ctype !== 2 && ctype !== 6) || interlace) throw new Error(`unsupported PNG (depth ${depth}, type ${ctype}, interlace ${interlace})`);
|
|
21
|
+
const bpp = ctype === 6 ? 4 : 3;
|
|
22
|
+
const raw = inflateSync(Buffer.concat(idat));
|
|
23
|
+
const stride = width * bpp;
|
|
24
|
+
const out = new Uint8Array(width * height * 4);
|
|
25
|
+
let prev = new Uint8Array(stride), cur = new Uint8Array(stride);
|
|
26
|
+
for (let y = 0; y < height; y++) {
|
|
27
|
+
const ft = raw[y * (stride + 1)];
|
|
28
|
+
const line = raw.subarray(y * (stride + 1) + 1, (y + 1) * (stride + 1));
|
|
29
|
+
for (let i = 0; i < stride; i++) {
|
|
30
|
+
const a = i >= bpp ? cur[i - bpp] : 0, b = prev[i], c = i >= bpp ? prev[i - bpp] : 0;
|
|
31
|
+
let v = line[i];
|
|
32
|
+
if (ft === 1) v += a;
|
|
33
|
+
else if (ft === 2) v += b;
|
|
34
|
+
else if (ft === 3) v += (a + b) >> 1;
|
|
35
|
+
else if (ft === 4) { const p = a + b - c, pa = Math.abs(p - a), pb = Math.abs(p - b), pc = Math.abs(p - c); v += pa <= pb && pa <= pc ? a : pb <= pc ? b : c; }
|
|
36
|
+
cur[i] = v & 255;
|
|
37
|
+
}
|
|
38
|
+
for (let x = 0; x < width; x++) {
|
|
39
|
+
const o = (y * width + x) * 4, s = x * bpp;
|
|
40
|
+
out[o] = cur[s]; out[o + 1] = cur[s + 1]; out[o + 2] = cur[s + 2]; out[o + 3] = bpp === 4 ? cur[s + 3] : 255;
|
|
41
|
+
}
|
|
42
|
+
[prev, cur] = [cur, prev];
|
|
43
|
+
}
|
|
44
|
+
return { width, height, data: out };
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
// Minimal PNG encoder (RGBA, filter 0), for the eye/page images written with --out.
|
|
48
|
+
|
|
49
|
+
const CRC_TABLE = (() => {
|
|
50
|
+
const t = new Uint32Array(256);
|
|
51
|
+
for (let n = 0; n < 256; n++) { let c = n; for (let k = 0; k < 8; k++) c = c & 1 ? 0xedb88320 ^ (c >>> 1) : c >>> 1; t[n] = c >>> 0; }
|
|
52
|
+
return t;
|
|
53
|
+
})();
|
|
54
|
+
function crc32(buf) {
|
|
55
|
+
let c = 0xffffffff;
|
|
56
|
+
for (let i = 0; i < buf.length; i++) c = CRC_TABLE[(c ^ buf[i]) & 255] ^ (c >>> 8);
|
|
57
|
+
return (c ^ 0xffffffff) >>> 0;
|
|
58
|
+
}
|
|
59
|
+
function chunk(type, body) {
|
|
60
|
+
const len = Buffer.alloc(4); len.writeUInt32BE(body.length);
|
|
61
|
+
const tb = Buffer.concat([Buffer.from(type, 'ascii'), body]);
|
|
62
|
+
const crc = Buffer.alloc(4); crc.writeUInt32BE(crc32(tb));
|
|
63
|
+
return Buffer.concat([len, tb, crc]);
|
|
64
|
+
}
|
|
65
|
+
export function encodePng(img) {
|
|
66
|
+
const { width, height, data } = img;
|
|
67
|
+
const raw = Buffer.alloc((width * 4 + 1) * height);
|
|
68
|
+
for (let y = 0; y < height; y++) {
|
|
69
|
+
raw[y * (width * 4 + 1)] = 0;
|
|
70
|
+
Buffer.from(data.buffer, data.byteOffset + y * width * 4, width * 4).copy(raw, y * (width * 4 + 1) + 1);
|
|
71
|
+
}
|
|
72
|
+
const ihdr = Buffer.alloc(13);
|
|
73
|
+
ihdr.writeUInt32BE(width, 0); ihdr.writeUInt32BE(height, 4); ihdr[8] = 8; ihdr[9] = 6; ihdr[10] = 0; ihdr[11] = 0; ihdr[12] = 0;
|
|
74
|
+
return Buffer.concat([Buffer.from([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a]), chunk('IHDR', ihdr), chunk('IDAT', deflateSync(raw)), chunk('IEND', Buffer.alloc(0))]);
|
|
75
|
+
}
|