@margoengine/xr 0.0.0-stage → 1.0.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.
Files changed (52) hide show
  1. package/LICENSE +201 -0
  2. package/NOTICE +4 -0
  3. package/README.md +842 -2
  4. package/dist/ar-extra.d.ts +232 -0
  5. package/dist/ar-extra.js +315 -0
  6. package/dist/ar.d.ts +177 -0
  7. package/dist/ar.js +195 -0
  8. package/dist/body.d.ts +58 -0
  9. package/dist/body.js +134 -0
  10. package/dist/controller.d.ts +32 -0
  11. package/dist/device-check.d.ts +39 -0
  12. package/dist/device-check.js +155 -0
  13. package/dist/errors-XvcwUnU4.js +7 -0
  14. package/dist/eye-target.d.ts +25 -0
  15. package/dist/eyes.d.ts +45 -0
  16. package/dist/gizmo.d.ts +165 -0
  17. package/dist/gizmo.js +198 -0
  18. package/dist/hands.d.ts +48 -0
  19. package/dist/hands.js +198 -0
  20. package/dist/hit-test.d.ts +69 -0
  21. package/dist/index.d.ts +148 -0
  22. package/dist/index.js +1103 -0
  23. package/dist/interaction.d.ts +136 -0
  24. package/dist/interaction.js +205 -0
  25. package/dist/launch.d.ts +65 -0
  26. package/dist/launch.js +54 -0
  27. package/dist/layers-BTUTAawG.js +391 -0
  28. package/dist/layers.d.ts +247 -0
  29. package/dist/layers.js +1 -0
  30. package/dist/locomotion.d.ts +63 -0
  31. package/dist/locomotion.js +184 -0
  32. package/dist/models.d.ts +136 -0
  33. package/dist/models.js +335 -0
  34. package/dist/near.d.ts +465 -0
  35. package/dist/near.js +643 -0
  36. package/dist/occlusion.d.ts +49 -0
  37. package/dist/occlusion.js +218 -0
  38. package/dist/performance.d.ts +67 -0
  39. package/dist/performance.js +78 -0
  40. package/dist/pointer.d.ts +93 -0
  41. package/dist/pointer.js +205 -0
  42. package/dist/reflection.d.ts +70 -0
  43. package/dist/reflection.js +147 -0
  44. package/dist/renderer.d.ts +21 -0
  45. package/dist/scene-owner-BRObO-WJ.js +15 -0
  46. package/dist/scene-owner.d.ts +3 -0
  47. package/dist/scene-scope-DufD_3B6.js +42 -0
  48. package/dist/session.d.ts +60 -0
  49. package/dist/session.js +68 -0
  50. package/dist/shared/xr-run.d.ts +64 -0
  51. package/dist/types.d.ts +221 -0
  52. package/package.json +152 -4
@@ -0,0 +1,391 @@
1
+ // @margo/core-built 1.0.0
2
+ import {coreRequirement as __margoCoreRequirement,assertCore as __margoAssertCore} from '@margoengine/core/extension';
3
+
4
+ import { n as e } from "./errors-XvcwUnU4.js";
5
+ //#region ../shared/copy-external.js
6
+ var t = /* @__PURE__ */ new WeakSet(), n = /* @__PURE__ */ new WeakMap();
7
+ function r(e) {
8
+ return !!e && typeof e == "object" && ("videoWidth" in e || "displayWidth" in e);
9
+ }
10
+ var i = (e) => [e.videoWidth ?? e.displayWidth ?? e.naturalWidth ?? e.width ?? 0, e.videoHeight ?? e.displayHeight ?? e.naturalHeight ?? e.height ?? 0];
11
+ function a(e, a, o, s) {
12
+ let c = e.queue, l = a.source, u = r(l);
13
+ if (u && !t.has(c)) try {
14
+ return c.copyExternalImageToTexture(a, o, s);
15
+ } catch (e) {
16
+ if (!(e instanceof TypeError)) throw e;
17
+ t.add(c);
18
+ }
19
+ if (!u) return c.copyExternalImageToTexture(a, o, s);
20
+ let [d, f] = i(l), p = n.get(l);
21
+ if (!p || p.w !== d || p.h !== f) {
22
+ let e = typeof OffscreenCanvas == "function" ? new OffscreenCanvas(d, f) : document.createElement("canvas");
23
+ e.width = d, e.height = f, n.set(l, p = {
24
+ canvas: e,
25
+ w: d,
26
+ h: f
27
+ });
28
+ }
29
+ p.canvas.getContext("2d").drawImage(l, 0, 0), c.copyExternalImageToTexture({
30
+ ...a,
31
+ source: p.canvas
32
+ }, o, s);
33
+ }
34
+ //#endregion
35
+ //#region src/layers.ts
36
+ var o = (t) => e("unsupported", `${t} needs the WebXR Layers module; request the session with optionalFeatures: ['layers'] on a browser whose XRGPUBinding creates layers`), s = class t {
37
+ static attach(n) {__margoAssertCore(n,__margoCoreRequirement("1.0.0",import.meta.url));
38
+ let r = n.session;
39
+ if (!r || !n.layerBinding) throw e("state", "XRLayers needs an immersive session; await xr.requestSession(mode, { optionalFeatures: [\"layers\"] }) first");
40
+ if (r.enabledFeatures && !r.enabledFeatures.includes("layers")) throw o("XRLayers");
41
+ return new t(n, r);
42
+ }
43
+ constructor(e, t) {
44
+ this.xr = e, this.session = t, this.entries = [], this.disposed = !1, this.ended = () => this.dispose(), this.space = e.referenceSpace, this.stop = e.onFrame((e) => this.frame(e.frame)), t.addEventListener("end", this.ended);
45
+ }
46
+ quad(e) {
47
+ let [t, n] = e.size ?? [1, 1];
48
+ return this.add("createQuadLayer", e, "above", [
49
+ 0,
50
+ 1.5,
51
+ -2
52
+ ], {
53
+ width: u(t, "size[0]"),
54
+ height: u(n, "size[1]")
55
+ });
56
+ }
57
+ cylinder(e) {
58
+ return this.add("createCylinderLayer", e, "above", [
59
+ 0,
60
+ 1.5,
61
+ 0
62
+ ], {
63
+ radius: u(e.radius ?? 2, "radius"),
64
+ centralAngle: u(e.centralAngle ?? Math.PI / 4, "centralAngle"),
65
+ aspectRatio: u(e.aspectRatio ?? 2, "aspectRatio")
66
+ });
67
+ }
68
+ equirect(e) {
69
+ return this.add("createEquirectLayer", e, "below", [
70
+ 0,
71
+ 0,
72
+ 0
73
+ ], {
74
+ radius: Math.max(0, e.radius ?? 0),
75
+ centralHorizontalAngle: e.centralHorizontalAngle ?? 2 * Math.PI,
76
+ upperVerticalAngle: e.upperVerticalAngle ?? Math.PI / 2,
77
+ lowerVerticalAngle: e.lowerVerticalAngle ?? -Math.PI / 2
78
+ });
79
+ }
80
+ cube(e) {
81
+ let [t, n] = e.pixels;
82
+ if (t !== n) throw RangeError(`cube layer faces must be square, got ${t} by ${n}; pass pixels: [size, size]`);
83
+ let r = e.content;
84
+ if (typeof r != "function" && (!Array.isArray(r) || r.length !== 6)) throw TypeError("cube layer content needs six faces (+X, -X, +Y, -Y, +Z, -Z) or a drawing function; pass an array of six images");
85
+ return this.add("createCubeLayer", {
86
+ ...e,
87
+ content: r
88
+ }, "below", [
89
+ 0,
90
+ 0,
91
+ 0
92
+ ], {}, !0);
93
+ }
94
+ video(e, t = {}) {
95
+ this.live();
96
+ let n = globalThis.XRMediaBinding;
97
+ if (!n) throw o("XRLayers.video");
98
+ let r = t.shape ?? "quad", { space: i, follow: a } = this.placementSpace(t), s = {
99
+ space: i,
100
+ layout: t.layout ?? "mono",
101
+ invertStereo: t.invertStereo ?? !1,
102
+ transform: f(t.position ?? (r === "quad" ? [
103
+ 0,
104
+ 1.5,
105
+ -2
106
+ ] : [
107
+ 0,
108
+ 0,
109
+ 0
110
+ ]), t.orientation)
111
+ };
112
+ r === "quad" && (s.width = u(t.width ?? 1, "width")), r === "cylinder" && (s.radius = u(t.radius ?? 2, "radius"), s.centralAngle = u(t.centralAngle ?? Math.PI / 4, "centralAngle")), r === "equirect" && (s.radius = Math.max(0, t.radius ?? 0));
113
+ let c = {
114
+ quad: "createQuadLayer",
115
+ cylinder: "createCylinderLayer",
116
+ equirect: "createEquirectLayer"
117
+ }[r];
118
+ if (!c) throw TypeError(`video shape '${r}' is not one of 'quad', 'cylinder' or 'equirect'; pass one of those`);
119
+ let l = new n(this.session)[c](e, s);
120
+ return this.track(l, null, t.order ?? "above", a, !1, 1, !0);
121
+ }
122
+ dispose() {
123
+ if (this.disposed) return;
124
+ this.disposed = !0, this.stop(), this.session.removeEventListener("end", this.ended);
125
+ let e = this.entries.splice(0);
126
+ this.xr.session === this.session && this.show();
127
+ for (let t of e) t.layer.destroy();
128
+ }
129
+ [Symbol.dispose ?? Symbol()]() {
130
+ this.dispose();
131
+ }
132
+ live() {
133
+ if (this.disposed || this.xr.session !== this.session) throw e("state", "XRLayers belongs to a session that ended; attach a new XRLayers to the new session");
134
+ }
135
+ placementSpace(t) {
136
+ let n = t.space ?? this.xr.referenceSpace;
137
+ if (!n) throw e("state", "XRLayers has no reference space; attach after xr.requestSession resolves");
138
+ return {
139
+ space: n,
140
+ follow: !t.space
141
+ };
142
+ }
143
+ add(e, t, n, r, i, a = !1) {
144
+ this.live();
145
+ let { binding: s } = this.xr.layerBinding;
146
+ if (!s[e] || !s.getSubImage) throw o(`XRLayers ${e.slice(6, -5).toLowerCase()}`);
147
+ let [c, l] = t.pixels ?? [];
148
+ if (!(Number.isInteger(c) && Number.isInteger(l) && c > 0 && l > 0)) throw RangeError(`layer pixels must be two positive whole numbers, got ${String(t.pixels)}; pass pixels: [width, height]`);
149
+ let u = t.layout ?? "mono", { space: p, follow: m } = this.placementSpace(t), h = {
150
+ colorFormat: s.getPreferredColorFormat(),
151
+ textureUsage: 18,
152
+ space: p,
153
+ viewPixelWidth: c,
154
+ viewPixelHeight: l,
155
+ layout: u,
156
+ isStatic: t.isStatic ?? !1,
157
+ ...i
158
+ }, { quality: g, mipLevels: _ } = t;
159
+ if (g !== void 0 && ![
160
+ "default",
161
+ "text-optimized",
162
+ "graphics-optimized"
163
+ ].includes(g)) throw RangeError(`layer quality ${String(g)} is not supported; pass 'default', 'text-optimized' or 'graphics-optimized'`);
164
+ if (_ !== void 0) {
165
+ if (!(Number.isInteger(_) && _ >= 1)) throw RangeError(`layer mipLevels is a whole number from 1, got ${String(_)}; pass 1 for none`);
166
+ h.mipLevels = _;
167
+ }
168
+ a ? h.orientation = d(t.orientation ?? [
169
+ 0,
170
+ 0,
171
+ 0,
172
+ 1
173
+ ]) : h.transform = f(t.position ?? r, t.orientation);
174
+ let v = s[e](h);
175
+ return t.opacity !== void 0 && (v.opacity = Math.min(1, Math.max(0, t.opacity))), g !== void 0 && (v.quality = g), t.blendTextureSourceAlpha !== void 0 && (v.blendTextureSourceAlpha = t.blendTextureSourceAlpha), t.chromaticAberrationCorrection !== void 0 && (v.chromaticAberrationCorrection = t.chromaticAberrationCorrection), this.track(v, t.content, t.order ?? n, m, u === "stereo", a ? 6 : 1, t.isStatic ?? !1, u === "stereo-left-right" || u === "stereo-top-bottom");
176
+ }
177
+ track(e, t, n, r, i, a, o, s = !1) {
178
+ let c = this, l = {
179
+ layer: e,
180
+ content: t,
181
+ order: n,
182
+ follow: r,
183
+ stereo: i,
184
+ packed: s,
185
+ faces: a,
186
+ isStatic: o,
187
+ dirty: !0,
188
+ drawn: !1,
189
+ redraw() {
190
+ l.dirty = !0;
191
+ },
192
+ dispose() {
193
+ let t = c.entries.indexOf(l);
194
+ t < 0 || (c.entries.splice(t, 1), c.xr.session === c.session && c.show(), e.destroy());
195
+ }
196
+ }, u = t;
197
+ return Object.defineProperty(l, "content", {
198
+ get: () => u,
199
+ set: (e) => {
200
+ u = e, l.dirty = !0;
201
+ }
202
+ }), this.entries.push(l), this.show(), l;
203
+ }
204
+ show() {
205
+ this.xr.setLayers(this.entries.filter((e) => e.order === "below").map((e) => e.layer), this.entries.filter((e) => e.order === "above").map((e) => e.layer));
206
+ }
207
+ frame(e) {
208
+ if (this.disposed || e.session !== this.session) return;
209
+ let t = this.xr.referenceSpace, n = t !== this.space;
210
+ this.space = t;
211
+ let r = this.xr.layerBinding;
212
+ if (r) for (let i of this.entries) {
213
+ n && i.follow && t && (i.layer.space = t);
214
+ let o = i.content;
215
+ if (!o) continue;
216
+ if (!i.drawn) {
217
+ i.drawn = !0;
218
+ continue;
219
+ }
220
+ if (i.isStatic && !i.dirty && !i.layer.needsRedraw) continue;
221
+ i.dirty = !1;
222
+ let s = i.stereo ? ["left", "right"] : ["none"];
223
+ for (let t of s) {
224
+ let n = r.binding.getSubImage(i.layer, e, t), s = n.getViewDescriptor(), u = s.baseArrayLayer ?? 0, d = i.packed ? {
225
+ x: 0,
226
+ y: 0,
227
+ width: n.colorTexture.width,
228
+ height: n.colorTexture.height
229
+ } : n.viewport;
230
+ for (let e = 0; e < i.faces; e++) {
231
+ let i = c(o, t, e);
232
+ if (typeof i == "function") i({
233
+ device: r.device,
234
+ texture: n.colorTexture,
235
+ view: n.colorTexture.createView({
236
+ ...s,
237
+ baseArrayLayer: u + e
238
+ }),
239
+ viewport: d,
240
+ eye: t,
241
+ face: e
242
+ });
243
+ else {
244
+ let { x: t, y: o, width: s, height: c } = d, f = l(i);
245
+ a(r.device, { source: i }, {
246
+ texture: n.colorTexture,
247
+ origin: [
248
+ t,
249
+ o,
250
+ u + e
251
+ ]
252
+ }, [Math.min(s, f[0]), Math.min(c, f[1])]);
253
+ }
254
+ }
255
+ }
256
+ }
257
+ }
258
+ };
259
+ function c(e, t, n) {
260
+ if (typeof e == "function") return e;
261
+ if (Array.isArray(e)) return e[n];
262
+ if ("left" in e && "right" in e) {
263
+ let n = e;
264
+ return t === "right" ? n.right : n.left;
265
+ }
266
+ return e;
267
+ }
268
+ function l(e) {
269
+ let t = e;
270
+ return t.videoWidth ? [t.videoWidth, t.videoHeight] : t.displayWidth ? [t.displayWidth, t.displayHeight] : t.naturalWidth ? [t.naturalWidth, t.naturalHeight] : [t.width, t.height];
271
+ }
272
+ function u(e, t) {
273
+ if (!(Number.isFinite(e) && e > 0)) throw RangeError(`layer ${t} must be a positive number, got ${e}; pass a size above 0`);
274
+ return e;
275
+ }
276
+ var d = ([e, t, n, r]) => ({
277
+ x: e,
278
+ y: t,
279
+ z: n,
280
+ w: r
281
+ });
282
+ function f(t, n = [
283
+ 0,
284
+ 0,
285
+ 0,
286
+ 1
287
+ ]) {
288
+ let r = globalThis.XRRigidTransform;
289
+ if (!r) throw e("unsupported", "XRRigidTransform is missing; run inside a WebXR browser");
290
+ return new r({
291
+ x: t[0],
292
+ y: t[1],
293
+ z: t[2],
294
+ w: 1
295
+ }, d(n));
296
+ }
297
+ async function p(e, t = .5, n = 50) {
298
+ if (!(Number.isFinite(t) && Number.isFinite(n) && n >= 0)) throw RangeError(`pulse needs a finite intensity and a duration of 0 ms or more, got ${t} and ${n}; pass pulse(controller, 0.5, 50)`);
299
+ let r = ("source" in e ? e.source : e).gamepad, i = Math.min(1, Math.max(0, t)), a = r?.hapticActuators?.[0];
300
+ if (a?.pulse) return a.pulse(i, n);
301
+ let o = r?.vibrationActuator;
302
+ return o?.playEffect && (!o.effects || o.effects.includes("dual-rumble")) ? await o.playEffect("dual-rumble", {
303
+ duration: n,
304
+ strongMagnitude: i,
305
+ weakMagnitude: i
306
+ }) === "complete" : !1;
307
+ }
308
+ function m(e, t, n = {}, r) {
309
+ let i = n.speed ?? 7, a = n.gravity ?? 9.81, o = n.step ?? .02, s = n.maxTime ?? 2, c = n.ground ?? 0;
310
+ if (![
311
+ i,
312
+ a,
313
+ o,
314
+ s
315
+ ].every(Number.isFinite) || o <= 0 || s <= 0) throw RangeError("teleportArc needs finite speed and gravity and a positive step and maxTime; pass numbers such as { speed: 7, step: 0.02 }");
316
+ let l = Math.hypot(t[0], t[1], t[2]) || 1, u = t[0] / l * i, d = t[1] / l * i, f = t[2] / l * i, p = Math.ceil(s / o) + 1, m = r && r.points.length >= p * 3 ? r : {
317
+ points: new Float32Array(p * 3),
318
+ count: 0,
319
+ hit: null
320
+ };
321
+ m.count = 0, m.hit = null;
322
+ let h = (e, t) => typeof c == "number" ? c : c(e, t), g = e[0], _ = e[1], v = e[2];
323
+ for (let t = 0; t < p; t++) {
324
+ let n = Math.min(t * o, s), r = e[0] + u * n, i = e[1] + d * n - .5 * a * n * n, c = e[2] + f * n, l = h(r, c);
325
+ if (l !== null && i <= l && t > 0) {
326
+ let e = h(g, v) ?? l, t = _ - e, n = l - i, a = t + n > 0 ? t / (t + n) : 1, o = g + (r - g) * a, s = v + (c - v) * a;
327
+ return m.hit = [
328
+ o,
329
+ h(o, s) ?? l,
330
+ s
331
+ ], m.points.set(m.hit, m.count * 3), m.count++, m;
332
+ }
333
+ m.points[m.count * 3] = r, m.points[m.count * 3 + 1] = i, m.points[m.count * 3 + 2] = c, m.count++, g = r, _ = i, v = c;
334
+ }
335
+ return m;
336
+ }
337
+ var h = class t {
338
+ static attach(e) {__margoAssertCore(e,__margoCoreRequirement("1.0.0",import.meta.url));
339
+ return new t(e);
340
+ }
341
+ constructor(e) {
342
+ this.xr = e, this.feet = [
343
+ 0,
344
+ 0,
345
+ 0
346
+ ], this.floor = 0, this.tracked = !1, this.stop = e.onFrame((e) => {
347
+ let t = e.viewer?.transform.position;
348
+ t && (this.feet[0] = t.x, this.feet[1] = this.floor, this.feet[2] = t.z, this.tracked = !0);
349
+ });
350
+ }
351
+ teleport(e, t = 0) {
352
+ if (!e.every(Number.isFinite) || !Number.isFinite(t)) throw RangeError("teleport needs a finite [x, y, z] target and yaw; pass a floor point such as arc.hit");
353
+ this.offset(this.tracked ? this.feet : [
354
+ 0,
355
+ this.floor,
356
+ 0
357
+ ], e, t), this.feet[0] = e[0], this.feet[1] = this.floor = e[1], this.feet[2] = e[2], this.tracked = !0;
358
+ }
359
+ turn(e) {
360
+ if (!Number.isFinite(e)) throw RangeError(`turn needs a finite angle in radians, got ${e}; pass Math.PI / 6`);
361
+ let t = this.tracked ? [...this.feet] : [
362
+ 0,
363
+ this.floor,
364
+ 0
365
+ ];
366
+ this.offset(t, t, e);
367
+ }
368
+ dispose() {
369
+ this.stop();
370
+ }
371
+ [Symbol.dispose ?? Symbol()]() {
372
+ this.dispose();
373
+ }
374
+ offset(t, n, r) {
375
+ let i = this.xr.referenceSpace;
376
+ if (!this.xr.session || !i?.getOffsetReferenceSpace) throw e("state", "Locomotion needs an active XR session with a reference space; call it after xr.requestSession resolves");
377
+ let a = Math.cos(-r), o = Math.sin(-r), s = a * n[0] + o * n[2], c = -o * n[0] + a * n[2], l = f([
378
+ t[0] - s,
379
+ t[1] - n[1],
380
+ t[2] - c
381
+ ], [
382
+ 0,
383
+ Math.sin(-r / 2),
384
+ 0,
385
+ Math.cos(-r / 2)
386
+ ]);
387
+ this.xr.useReferenceSpace(i.getOffsetReferenceSpace(l));
388
+ }
389
+ };
390
+ //#endregion
391
+ export { m as i, s as n, p as r, h as t };
@@ -0,0 +1,247 @@
1
+ import type { XRController } from './controller.js';
2
+ import type { XRFrameState, XRInputSource, XRNativeFrame, XRNativeSession, XRSpace } from './types.js';
3
+ /** What layers and locomotion need of an `XR`; `XR` itself fits. */
4
+ export interface XRLayersHost {
5
+ readonly session: XRNativeSession | null;
6
+ readonly referenceSpace: XRSpace | undefined;
7
+ /** The session's WebGPU binding and device; undefined without an immersive session. */
8
+ readonly layerBinding: {
9
+ binding: XRLayerBinding;
10
+ device: GPUDevice;
11
+ } | undefined;
12
+ onFrame(callback: (state: XRFrameState) => void): () => void;
13
+ /** Shows `below`, then the scene's projection layer, then `above`. */
14
+ setLayers(below: readonly object[], above: readonly object[]): void;
15
+ /** Replaces the space of frame poses, controllers and hits (a teleport or a turn). */
16
+ useReferenceSpace(space: XRSpace): void;
17
+ }
18
+ /** The layer half of an `XRGPUBinding` (WebXR/WebGPU Binding, section 5.9). */
19
+ export interface XRLayerBinding {
20
+ getPreferredColorFormat(): GPUTextureFormat;
21
+ createQuadLayer?(init: object): XRCompositionLayer;
22
+ createCylinderLayer?(init: object): XRCompositionLayer;
23
+ createEquirectLayer?(init: object): XRCompositionLayer;
24
+ createCubeLayer?(init: object): XRCompositionLayer;
25
+ getSubImage?(layer: XRCompositionLayer, frame: XRNativeFrame, eye?: 'none' | 'left' | 'right'): {
26
+ colorTexture: GPUTexture;
27
+ viewport: {
28
+ x: number;
29
+ y: number;
30
+ width: number;
31
+ height: number;
32
+ };
33
+ getViewDescriptor(): GPUTextureViewDescriptor;
34
+ };
35
+ }
36
+ /** A WebXR composition layer (WebXR Layers, section 3.4). */
37
+ export interface XRCompositionLayer {
38
+ readonly layout?: string;
39
+ readonly needsRedraw?: boolean;
40
+ opacity?: number;
41
+ quality?: string;
42
+ blendTextureSourceAlpha?: boolean;
43
+ chromaticAberrationCorrection?: boolean;
44
+ space?: XRSpace;
45
+ destroy(): void;
46
+ }
47
+ /** Where a layer sits: a position in metres and an orientation quaternion in `space` (default the XR reference space). */
48
+ export interface LayerPlacement {
49
+ /** Default [0, 1.5, -2] for quads, [0, 1.5, 0] for cylinders (seen from their centre), [0, 0, 0] otherwise. */
50
+ position?: readonly [number, number, number];
51
+ /** Unit quaternion [x, y, z, w]. Default [0, 0, 0, 1]. */
52
+ orientation?: readonly [number, number, number, number];
53
+ /** The space the layer is placed in. Default the XR reference space, followed through teleports. */
54
+ space?: XRSpace;
55
+ /** Composited under the scene (shown where the scene draws nothing) or over it. Default 'above' for quads, cylinders and video, 'below' for equirect and cube layers. */
56
+ order?: 'below' | 'above';
57
+ }
58
+ /** An image the layer copies each frame it draws (WebGPU `copyExternalImageToTexture` sources). */
59
+ export type LayerImage = ImageBitmap | ImageData | HTMLImageElement | HTMLVideoElement | HTMLCanvasElement | OffscreenCanvas | VideoFrame;
60
+ /** What a layer draws: one image for both eyes, one per eye, or a function that draws with WebGPU. */
61
+ export type LayerContent = LayerImage | {
62
+ left: LayerImage;
63
+ right: LayerImage;
64
+ } | ((target: LayerTarget) => void);
65
+ /** One eye's (or one cube face's) texture view of a layer, for a drawing function. */
66
+ export interface LayerTarget {
67
+ readonly device: GPUDevice;
68
+ readonly texture: GPUTexture;
69
+ readonly view: GPUTextureView;
70
+ readonly viewport: {
71
+ x: number;
72
+ y: number;
73
+ width: number;
74
+ height: number;
75
+ };
76
+ readonly eye: 'none' | 'left' | 'right';
77
+ /** Cube face 0 to 5 (+X, -X, +Y, -Y, +Z, -Z); 0 for other layers. */
78
+ readonly face: number;
79
+ }
80
+ interface Common extends LayerPlacement {
81
+ /** Texture size of each eye's (or face's) image in pixels. */
82
+ pixels: readonly [number, number];
83
+ content: LayerContent;
84
+ /** 'mono' (default), 'stereo' (an image per eye), 'stereo-left-right' or 'stereo-top-bottom' (one image holding both eyes). */
85
+ layout?: 'mono' | 'stereo' | 'stereo-left-right' | 'stereo-top-bottom';
86
+ /** Draw once (and again after the compositor asks with needsRedraw) instead of every frame. Default false. */
87
+ isStatic?: boolean;
88
+ /** Layer opacity 0 to 1. Default 1. */
89
+ opacity?: number;
90
+ /** What the compositor favours when it resamples the layer: 'default', 'text-optimized' or 'graphics-optimized'. Default the browser's. */
91
+ quality?: 'default' | 'text-optimized' | 'graphics-optimized';
92
+ /** Blend the texture's alpha over what is behind it. Default true (the browser's). */
93
+ blendTextureSourceAlpha?: boolean;
94
+ /** Ask the compositor to correct chromatic aberration for this layer. Default the browser's. */
95
+ chromaticAberrationCorrection?: boolean;
96
+ /** Mip levels of the layer texture, 1 or more; above 1 the compositor filters it at a distance. Default 1. */
97
+ mipLevels?: number;
98
+ }
99
+ /** Options for {@link XRLayers.quad}. */
100
+ export interface QuadLayerOptions extends Common {
101
+ /** Size in metres. Default [1, 1]. */
102
+ size?: readonly [number, number];
103
+ }
104
+ /** Options for {@link XRLayers.cylinder}. */
105
+ export interface CylinderLayerOptions extends Common {
106
+ /** Default 2 metres. */
107
+ radius?: number;
108
+ /** Visible arc in radians. Default PI / 4. */
109
+ centralAngle?: number;
110
+ /** Width over height of the visible arc. Default 2. */
111
+ aspectRatio?: number;
112
+ }
113
+ /** Options for {@link XRLayers.equirect}. */
114
+ export interface EquirectLayerOptions extends Common {
115
+ /** Sphere radius in metres; 0 is an infinite sphere (a sky). Default 0. */
116
+ radius?: number;
117
+ /** Default 2 PI. */
118
+ centralHorizontalAngle?: number;
119
+ /** Default PI / 2. */
120
+ upperVerticalAngle?: number;
121
+ /** Default -PI / 2. */
122
+ lowerVerticalAngle?: number;
123
+ }
124
+ /** Options for {@link XRLayers.cube}: `pixels` is one face, square. */
125
+ export interface CubeLayerOptions extends Omit<Common, 'content' | 'layout' | 'position'> {
126
+ /** Six faces in the order +X, -X, +Y, -Y, +Z, -Z, or a drawing function called per face. */
127
+ content: readonly LayerImage[] | ((target: LayerTarget) => void);
128
+ layout?: 'mono' | 'stereo';
129
+ }
130
+ /** Options for {@link XRLayers.video}. */
131
+ export interface VideoLayerOptions extends LayerPlacement {
132
+ /** 'quad' (default), 'cylinder' or 'equirect'. */
133
+ shape?: 'quad' | 'cylinder' | 'equirect';
134
+ layout?: 'mono' | 'stereo-left-right' | 'stereo-top-bottom';
135
+ /** Quad width in metres; the height follows the video's aspect ratio. Default 1. */
136
+ width?: number;
137
+ /** Cylinder or equirect radius in metres. Defaults 2 and 0. */
138
+ radius?: number;
139
+ /** Cylinder arc in radians. Default PI / 4. */
140
+ centralAngle?: number;
141
+ invertStereo?: boolean;
142
+ }
143
+ /** A layer shown by {@link XRLayers}. */
144
+ export interface XRPanel {
145
+ readonly layer: XRCompositionLayer;
146
+ /** Replaces what the layer draws; a static layer draws it on the next frame. */
147
+ content: LayerContent | readonly LayerImage[] | null;
148
+ /** Draws a static layer again on the next frame. */
149
+ redraw(): void;
150
+ /** Removes the layer from the session and destroys it. */
151
+ dispose(): void;
152
+ }
153
+ /**
154
+ * The session's composition layers: crisp panels (quad, cylinder), 360 images and skies (equirect, cube) and video, each
155
+ * shown by the compositor at its own rate and resolution. Attach after `xr.requestSession` with the 'layers' feature;
156
+ * the layers go with the session. `using` or `dispose()` removes them.
157
+ */
158
+ export declare class XRLayers {
159
+ private readonly xr;
160
+ private readonly session;
161
+ private readonly entries;
162
+ private readonly stop;
163
+ private space;
164
+ private disposed;
165
+ /** @throws MargoError 'state' without an immersive session; 'unsupported' without the 'layers' feature. */
166
+ static attach(xr: XRLayersHost): XRLayers;
167
+ private constructor();
168
+ /** A flat panel, sharp text and UI drawn once at full resolution. Faces +Z; only its front shows. */
169
+ quad(options: QuadLayerOptions): XRPanel;
170
+ /** A curved panel: part of a cylinder around its centre, seen from inside. */
171
+ cylinder(options: CylinderLayerOptions): XRPanel;
172
+ /** An equirectangular image on the inside of a sphere: a 360 photo, or a sky with radius 0. */
173
+ equirect(options: EquirectLayerOptions): XRPanel;
174
+ /** A cube map sky around the viewer; only its orientation counts. */
175
+ cube(options: CubeLayerOptions): XRPanel;
176
+ /** A video shown by the compositor at the video's frame rate (XRMediaBinding); play and pause the element yourself. */
177
+ video(video: HTMLVideoElement, options?: VideoLayerOptions): XRPanel;
178
+ /** Removes every layer. Safe to call twice. */
179
+ dispose(): void;
180
+ [Symbol.dispose]: () => void;
181
+ private readonly ended;
182
+ private live;
183
+ private placementSpace;
184
+ private add;
185
+ private track;
186
+ private show;
187
+ private frame;
188
+ }
189
+ /**
190
+ * Plays a short vibration on a controller: Gamepad Extensions `hapticActuators[0].pulse` where present, else the
191
+ * Gamepad `vibrationActuator` 'dual-rumble' effect. Resolves true once played, false when the controller has no
192
+ * actuator. `intensity` 0 to 1 (default 0.5), `milliseconds` default 50.
193
+ */
194
+ export declare function pulse(input: XRController | XRInputSource, intensity?: number, milliseconds?: number): Promise<boolean>;
195
+ /** Options for {@link teleportArc}. */
196
+ export interface TeleportArcOptions {
197
+ /** Launch speed in metres per second. Default 7. */
198
+ speed?: number;
199
+ /** Downward acceleration in metres per second squared. Default 9.81. */
200
+ gravity?: number;
201
+ /** Time step between samples in seconds. Default 0.02. */
202
+ step?: number;
203
+ /** Longest flight in seconds. Default 2. */
204
+ maxTime?: number;
205
+ /** The ground: a height, or a function of (x, z) giving the height there or null for no ground. Default 0. */
206
+ ground?: number | ((x: number, z: number) => number | null);
207
+ }
208
+ /** A teleport arc: the sampled points [x, y, z, ...] up to the landing point, and where it lands (null when it does not). */
209
+ export interface TeleportArc {
210
+ points: Float32Array;
211
+ count: number;
212
+ hit: [number, number, number] | null;
213
+ }
214
+ /**
215
+ * The parabola a teleport pointer draws from `origin` along `direction` (a controller's target ray): points under
216
+ * constant gravity, ending where it first passes below the ground. Allocation free when `out` is passed back.
217
+ */
218
+ export declare function teleportArc(origin: readonly [number, number, number], direction: readonly [number, number, number], options?: TeleportArcOptions, out?: TeleportArc): TeleportArc;
219
+ /**
220
+ * Moves the player without moving their body: teleport to a floor point and snap turn, by offsetting the XR reference
221
+ * space (WebXR `getOffsetReferenceSpace`). Poses, controllers, hits and world-locked layers all follow the new space.
222
+ */
223
+ export declare class Locomotion {
224
+ private readonly xr;
225
+ private readonly stop;
226
+ private readonly feet;
227
+ /** The floor's height in the current space: 0 in the session's own space, the target's height after a teleport. */
228
+ private floor;
229
+ private tracked;
230
+ static attach(xr: XRLayersHost): Locomotion;
231
+ private constructor();
232
+ /**
233
+ * Puts the player's feet at `target` (in the current space), turned by `yaw` radians (positive turns left, as seen
234
+ * from above). Uses the last tracked viewer position; before the first frame it moves the space origin.
235
+ */
236
+ teleport(target: readonly [number, number, number], yaw?: number): void;
237
+ /** Turns the player by `yaw` radians about where they stand (positive turns left). */
238
+ turn(yaw: number): void;
239
+ dispose(): void;
240
+ [Symbol.dispose]: () => void;
241
+ /**
242
+ * Poses in the new space are t^-1 times poses in the old one, so t^-1 = T(target) R(yaw) T(-feet) carries the feet
243
+ * to the target turned by yaw; t = T(feet) R(-yaw) T(-target) = T(feet - R(-yaw) target) R(-yaw).
244
+ */
245
+ private offset;
246
+ }
247
+ export {};
package/dist/layers.js ADDED
@@ -0,0 +1 @@
1
+ export { t as Locomotion, n as XRLayers, r as pulse, i as teleportArc } from "./layers-BTUTAawG.js";