@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.
- package/LICENSE +201 -0
- package/NOTICE +4 -0
- package/README.md +842 -2
- package/dist/ar-extra.d.ts +232 -0
- package/dist/ar-extra.js +315 -0
- package/dist/ar.d.ts +177 -0
- package/dist/ar.js +195 -0
- package/dist/body.d.ts +58 -0
- package/dist/body.js +134 -0
- package/dist/controller.d.ts +32 -0
- package/dist/device-check.d.ts +39 -0
- package/dist/device-check.js +155 -0
- package/dist/errors-XvcwUnU4.js +7 -0
- package/dist/eye-target.d.ts +25 -0
- package/dist/eyes.d.ts +45 -0
- package/dist/gizmo.d.ts +165 -0
- package/dist/gizmo.js +198 -0
- package/dist/hands.d.ts +48 -0
- package/dist/hands.js +198 -0
- package/dist/hit-test.d.ts +69 -0
- package/dist/index.d.ts +148 -0
- package/dist/index.js +1103 -0
- package/dist/interaction.d.ts +136 -0
- package/dist/interaction.js +205 -0
- package/dist/launch.d.ts +65 -0
- package/dist/launch.js +54 -0
- package/dist/layers-BTUTAawG.js +391 -0
- package/dist/layers.d.ts +247 -0
- package/dist/layers.js +1 -0
- package/dist/locomotion.d.ts +63 -0
- package/dist/locomotion.js +184 -0
- package/dist/models.d.ts +136 -0
- package/dist/models.js +335 -0
- package/dist/near.d.ts +465 -0
- package/dist/near.js +643 -0
- package/dist/occlusion.d.ts +49 -0
- package/dist/occlusion.js +218 -0
- package/dist/performance.d.ts +67 -0
- package/dist/performance.js +78 -0
- package/dist/pointer.d.ts +93 -0
- package/dist/pointer.js +205 -0
- package/dist/reflection.d.ts +70 -0
- package/dist/reflection.js +147 -0
- package/dist/renderer.d.ts +21 -0
- package/dist/scene-owner-BRObO-WJ.js +15 -0
- package/dist/scene-owner.d.ts +3 -0
- package/dist/scene-scope-DufD_3B6.js +42 -0
- package/dist/session.d.ts +60 -0
- package/dist/session.js +68 -0
- package/dist/shared/xr-run.d.ts +64 -0
- package/dist/types.d.ts +221 -0
- package/package.json +152 -4
package/dist/ar.d.ts
ADDED
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
import { Geometry, type EngineKernel, type PbrMaterial } from '@margoengine/core';
|
|
2
|
+
import type { XRFrameState, XRNativeFrame, XRNativeSession, XRSpace, XRView } from './types.js';
|
|
3
|
+
/** What the AR helpers need of an `XR`. */
|
|
4
|
+
export interface ARHost {
|
|
5
|
+
readonly session: XRNativeSession | null;
|
|
6
|
+
readonly referenceSpace: XRSpace | undefined;
|
|
7
|
+
onFrame(callback: (state: XRFrameState) => void): () => void;
|
|
8
|
+
}
|
|
9
|
+
/** The browser's XRPlane, as far as Margo reads it. */
|
|
10
|
+
export interface XRPlaneLike {
|
|
11
|
+
readonly planeSpace: XRSpace;
|
|
12
|
+
readonly polygon: readonly {
|
|
13
|
+
x: number;
|
|
14
|
+
y: number;
|
|
15
|
+
z: number;
|
|
16
|
+
}[];
|
|
17
|
+
readonly orientation?: 'horizontal' | 'vertical' | null;
|
|
18
|
+
readonly lastChangedTime: number;
|
|
19
|
+
readonly semanticLabel?: string | null;
|
|
20
|
+
}
|
|
21
|
+
/** One detected plane this frame. */
|
|
22
|
+
export interface DetectedPlane {
|
|
23
|
+
/** The browser's plane; the same object every frame while it is tracked. */
|
|
24
|
+
readonly plane: XRPlaneLike;
|
|
25
|
+
/** Column-major pose of the plane's centre in the reference space; its y axis is the plane's normal. */
|
|
26
|
+
readonly matrix: Float32Array;
|
|
27
|
+
/** The outline, x and z per point in the plane's own space (y is always 0 there). */
|
|
28
|
+
readonly polygon: Float32Array;
|
|
29
|
+
readonly orientation: 'horizontal' | 'vertical' | null;
|
|
30
|
+
/** What the device says the plane is, such as 'floor', 'wall' or 'table'; empty when it does not say. */
|
|
31
|
+
readonly label: string;
|
|
32
|
+
/** False while the plane's pose is unknown this frame. */
|
|
33
|
+
tracked: boolean;
|
|
34
|
+
}
|
|
35
|
+
/** Options for {@link ARPlanes.create}. */
|
|
36
|
+
export interface ARPlanesOptions {
|
|
37
|
+
/** Called when a plane is first seen. */
|
|
38
|
+
onAdd?: (plane: DetectedPlane) => void;
|
|
39
|
+
/** Called when a plane's outline, orientation or label changes. */
|
|
40
|
+
onChange?: (plane: DetectedPlane) => void;
|
|
41
|
+
/** Called when the device stops tracking a plane. */
|
|
42
|
+
onRemove?: (plane: DetectedPlane) => void;
|
|
43
|
+
/** Draws each plane as a mesh with this material; default none. */
|
|
44
|
+
show?: {
|
|
45
|
+
engine: EngineKernel;
|
|
46
|
+
material: PbrMaterial;
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Triangles of a simple polygon given as x, z pairs, by ear clipping; works for both windings and for concave outlines.
|
|
51
|
+
* Every triangle winds counter-clockwise seen from +y (its right-hand normal is +y), whichever way the outline runs.
|
|
52
|
+
* @throws RangeError for fewer than 3 points or a non-finite one.
|
|
53
|
+
*/
|
|
54
|
+
export declare function triangulate(polygon: ArrayLike<number>): Uint32Array;
|
|
55
|
+
/** A flat mesh of a plane outline (x, z pairs), in the plane's space, normal +y. */
|
|
56
|
+
export declare function planeGeometry(polygon: ArrayLike<number>): Geometry;
|
|
57
|
+
/**
|
|
58
|
+
* The planes an AR device has found, kept in step every frame. Start the session with the 'plane-detection' feature.
|
|
59
|
+
* `planes` holds this frame's planes; `onAdd`, `onChange` and `onRemove` tell what changed; `show` draws them.
|
|
60
|
+
* @throws MargoError 'state' without a session, 'unsupported' when the session has no 'plane-detection'.
|
|
61
|
+
*/
|
|
62
|
+
export declare class ARPlanes {
|
|
63
|
+
private readonly host;
|
|
64
|
+
private readonly options;
|
|
65
|
+
/** This frame's planes by the browser's plane object. */
|
|
66
|
+
readonly planes: Map<XRPlaneLike, DetectedPlane>;
|
|
67
|
+
private readonly changed;
|
|
68
|
+
private readonly meshes;
|
|
69
|
+
private readonly unsubscribe;
|
|
70
|
+
private constructor();
|
|
71
|
+
static create(xr: ARHost, options?: ARPlanesOptions): ARPlanes;
|
|
72
|
+
/** Reads `frame.detectedPlanes`; runs from the XR frame hook. */
|
|
73
|
+
update(frame: XRNativeFrame): void;
|
|
74
|
+
private draw;
|
|
75
|
+
/** Stops following the planes and removes their meshes. */
|
|
76
|
+
dispose(): void;
|
|
77
|
+
[Symbol.dispose](): void;
|
|
78
|
+
}
|
|
79
|
+
/** The browser's XRCPUDepthInformation, as far as Margo reads it. */
|
|
80
|
+
export interface XRDepthLike {
|
|
81
|
+
readonly width: number;
|
|
82
|
+
readonly height: number;
|
|
83
|
+
readonly rawValueToMeters: number;
|
|
84
|
+
readonly normDepthBufferFromNormView: {
|
|
85
|
+
readonly matrix: Float32Array;
|
|
86
|
+
};
|
|
87
|
+
getDepthInMeters(x: number, y: number): number;
|
|
88
|
+
/** XRViewGeometry of the sensor, where it differs from the view. */
|
|
89
|
+
readonly projectionMatrix?: Float32Array;
|
|
90
|
+
readonly transform?: XRView['transform'];
|
|
91
|
+
}
|
|
92
|
+
/** What `depthSensing` to start an AR session with, for {@link ARDepth}: CPU data, 32-bit floats or 16-bit integers. */
|
|
93
|
+
export declare const DEPTH_SENSING: {
|
|
94
|
+
readonly usagePreference: readonly ['cpu-optimized'];
|
|
95
|
+
readonly dataFormatPreference: readonly ['float32', 'luminance-alpha', 'unsigned-short'];
|
|
96
|
+
};
|
|
97
|
+
/**
|
|
98
|
+
* Real-world depth in an AR session started with the 'depth-sensing' feature and `init: {depthSensing: DEPTH_SENSING}`:
|
|
99
|
+
* distances at points of a view, and the world points behind them, to place things on real surfaces or test whether
|
|
100
|
+
* something is behind a real object.
|
|
101
|
+
* @throws MargoError 'state' without a session, 'unsupported' without depth sensing or with GPU-optimized depth.
|
|
102
|
+
*/
|
|
103
|
+
export declare class ARDepth {
|
|
104
|
+
private readonly host;
|
|
105
|
+
private constructor();
|
|
106
|
+
static create(xr: ARHost): ARDepth;
|
|
107
|
+
/** This frame's depth information for `view`, or null while the device has none. */
|
|
108
|
+
information(frame: XRNativeFrame, view: XRView): XRDepthLike | null;
|
|
109
|
+
/**
|
|
110
|
+
* Metres from the sensor to the real world at (`x`, `y`) of `view`, in normalized view coordinates (0 to 1, from the
|
|
111
|
+
* top left), or null without depth this frame.
|
|
112
|
+
* @throws RangeError for a point outside 0 to 1.
|
|
113
|
+
*/
|
|
114
|
+
depthAt(frame: XRNativeFrame, view: XRView, x: number, y: number): number | null;
|
|
115
|
+
/**
|
|
116
|
+
* The real-world point at (`x`, `y`) of `view`, in the reference space, or null without depth this frame. The depth
|
|
117
|
+
* is taken along the sensor's view axis.
|
|
118
|
+
*/
|
|
119
|
+
pointAt(frame: XRNativeFrame, view: XRView, x: number, y: number): [number, number, number] | null;
|
|
120
|
+
}
|
|
121
|
+
/** The light of the room this frame, in the reference space. */
|
|
122
|
+
export interface LightEstimate {
|
|
123
|
+
/**
|
|
124
|
+
* Nine RGB spherical harmonics coefficients of the room's light, coefficient-major (the order of @margoengine/math
|
|
125
|
+
* SH3), turned from the light probe's space into the reference space.
|
|
126
|
+
*/
|
|
127
|
+
readonly sh: Float32Array;
|
|
128
|
+
/** Unit direction towards the brightest light. */
|
|
129
|
+
readonly direction: [number, number, number];
|
|
130
|
+
/** The brightest light's RGB intensity. */
|
|
131
|
+
readonly intensity: [number, number, number];
|
|
132
|
+
}
|
|
133
|
+
/**
|
|
134
|
+
* The parts of `engine.lighting` that {@link ARLight} sets: `direction` xyz towards the sun with w its strength,
|
|
135
|
+
* `radiance` rgb the sun's colour with w the ambient strength, `ambient` the ambient colour.
|
|
136
|
+
*/
|
|
137
|
+
export interface LightingLike {
|
|
138
|
+
direction: Float32Array | number[];
|
|
139
|
+
radiance: Float32Array | number[];
|
|
140
|
+
ambient: Float32Array | number[];
|
|
141
|
+
}
|
|
142
|
+
/**
|
|
143
|
+
* Light estimation in an AR session started with the 'light-estimation' feature: each frame's estimate in `estimate`,
|
|
144
|
+
* and, with `lighting` given (such as `engine.lighting`), the engine's sun turned to the room's brightest light and its
|
|
145
|
+
* ambient set to the room's average light, so virtual things are lit like the real ones next to them.
|
|
146
|
+
* @throws MargoError 'state' without a session, 'unsupported' without the feature or the browser's light probes.
|
|
147
|
+
*/
|
|
148
|
+
export declare class ARLight {
|
|
149
|
+
private readonly host;
|
|
150
|
+
/** The browser's XRLightProbe; its `reflectionchange` event and `getReflectionCubeMap` serve ARReflection. */
|
|
151
|
+
readonly probe: {
|
|
152
|
+
probeSpace: XRSpace;
|
|
153
|
+
};
|
|
154
|
+
private readonly lighting?;
|
|
155
|
+
/** This frame's estimate, or null while the device has none. */
|
|
156
|
+
estimate: LightEstimate | null;
|
|
157
|
+
private readonly unsubscribe;
|
|
158
|
+
private readonly sh;
|
|
159
|
+
private readonly turn;
|
|
160
|
+
private readonly value;
|
|
161
|
+
private constructor();
|
|
162
|
+
static create(xr: ARHost, { lighting }?: {
|
|
163
|
+
lighting?: LightingLike;
|
|
164
|
+
}): Promise<ARLight>;
|
|
165
|
+
/**
|
|
166
|
+
* As {@link ARLight.create}, with the light probe asked for a reflection cube map in `reflectionFormat` (the WebXR
|
|
167
|
+
* Lighting Estimation API's XRLightProbeInit); `probe` then serves {@link ARReflection}.
|
|
168
|
+
*/
|
|
169
|
+
static withReflection(xr: ARHost, { lighting, reflectionFormat }?: {
|
|
170
|
+
lighting?: LightingLike;
|
|
171
|
+
reflectionFormat?: 'srgb8' | 'rgba16f';
|
|
172
|
+
}): Promise<ARLight>;
|
|
173
|
+
/** Reads this frame's estimate and applies it; runs from the XR frame hook. */
|
|
174
|
+
update(frame: XRNativeFrame): void;
|
|
175
|
+
dispose(): void;
|
|
176
|
+
[Symbol.dispose](): void;
|
|
177
|
+
}
|
package/dist/ar.js
ADDED
|
@@ -0,0 +1,195 @@
|
|
|
1
|
+
import { n as e } from "./errors-XvcwUnU4.js";
|
|
2
|
+
import { Geometry as t } from "@margoengine/core";
|
|
3
|
+
import { SH3 as n } from "@margoengine/math/sh";
|
|
4
|
+
//#region src/ar.ts
|
|
5
|
+
function r(e) {
|
|
6
|
+
let t = e.length / 2;
|
|
7
|
+
if (!Number.isInteger(t) || t < 3) throw RangeError("triangulate needs 3 or more x, z points; pass a plane outline");
|
|
8
|
+
for (let t = 0; t < e.length; t++) if (!Number.isFinite(e[t])) throw RangeError("triangulate needs finite points; pass a plane outline");
|
|
9
|
+
let n = (t) => e[t * 2], r = (t) => e[t * 2 + 1], i = 0;
|
|
10
|
+
for (let e = 0, a = t - 1; e < t; a = e++) i += n(a) * r(e) - n(e) * r(a);
|
|
11
|
+
let a = i >= 0 ? 1 : -1, o = (e, t, i) => ((n(t) - n(e)) * (r(i) - r(e)) - (r(t) - r(e)) * (n(i) - n(e))) * a, s = Array.from({ length: t }, (e, t) => t), c = [], l = 0;
|
|
12
|
+
for (; s.length > 3 && l++ < t * t;) {
|
|
13
|
+
let e = !1;
|
|
14
|
+
for (let t = 0; t < s.length; t++) {
|
|
15
|
+
let n = s[(t + s.length - 1) % s.length], r = s[t], i = s[(t + 1) % s.length];
|
|
16
|
+
if (o(n, r, i) <= 0) continue;
|
|
17
|
+
let a = !1;
|
|
18
|
+
for (let e of s) if (e !== n && e !== r && e !== i && o(n, r, e) >= 0 && o(r, i, e) >= 0 && o(i, n, e) >= 0) {
|
|
19
|
+
a = !0;
|
|
20
|
+
break;
|
|
21
|
+
}
|
|
22
|
+
if (!a) {
|
|
23
|
+
c.push(n, r, i), s.splice(t, 1), e = !0;
|
|
24
|
+
break;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
e || s.splice(1, 1);
|
|
28
|
+
}
|
|
29
|
+
if (s.length === 3 && o(s[0], s[1], s[2]) > 0 && c.push(s[0], s[1], s[2]), a > 0) for (let e = 0; e < c.length; e += 3) [c[e + 1], c[e + 2]] = [c[e + 2], c[e + 1]];
|
|
30
|
+
return Uint32Array.from(c);
|
|
31
|
+
}
|
|
32
|
+
function i(e) {
|
|
33
|
+
let n = e.length / 2, i = r(e), a = new Float32Array(n * 3), o = new Float32Array(n * 3);
|
|
34
|
+
for (let t = 0; t < n; t++) a[t * 3] = e[t * 2], a[t * 3 + 2] = e[t * 2 + 1], o[t * 3 + 1] = 1;
|
|
35
|
+
return new t(a, o, i);
|
|
36
|
+
}
|
|
37
|
+
var a = (e, t) => !e?.enabledFeatures || e.enabledFeatures.includes(t), o = class t {
|
|
38
|
+
constructor(e, t) {
|
|
39
|
+
this.host = e, this.options = t, this.planes = /* @__PURE__ */ new Map(), this.changed = /* @__PURE__ */ new Map(), this.meshes = /* @__PURE__ */ new Map(), this.unsubscribe = e.onFrame((e) => this.update(e.frame));
|
|
40
|
+
}
|
|
41
|
+
static create(n, r = {}) {
|
|
42
|
+
if (!n?.session) throw e("state", "ARPlanes needs an active XR session; await xr.requestSession first");
|
|
43
|
+
if (!a(n.session, "plane-detection")) throw e("unsupported", "The XR session has no 'plane-detection' feature; ask for it when starting the session");
|
|
44
|
+
return new t(n, r);
|
|
45
|
+
}
|
|
46
|
+
update(e) {
|
|
47
|
+
let t = this.host.referenceSpace, n = e.detectedPlanes;
|
|
48
|
+
if (t && n) {
|
|
49
|
+
for (let [e, t] of this.planes) n.has(e) || (this.planes.delete(e), this.changed.delete(e), this.meshes.get(e)?.dispose(), this.meshes.delete(e), this.options.onRemove?.(t));
|
|
50
|
+
for (let r of n) {
|
|
51
|
+
let n = e.getPose(r.planeSpace, t), i = this.planes.get(r), a = !i, o = !a && r.lastChangedTime > (this.changed.get(r) ?? -Infinity);
|
|
52
|
+
if (a || o) {
|
|
53
|
+
let e = new Float32Array(r.polygon.length * 2);
|
|
54
|
+
r.polygon.forEach((t, n) => {
|
|
55
|
+
e[n * 2] = t.x, e[n * 2 + 1] = t.z;
|
|
56
|
+
}), i = {
|
|
57
|
+
plane: r,
|
|
58
|
+
matrix: i?.matrix ?? /* @__PURE__ */ new Float32Array(16),
|
|
59
|
+
polygon: e,
|
|
60
|
+
orientation: r.orientation ?? null,
|
|
61
|
+
label: r.semanticLabel ?? "",
|
|
62
|
+
tracked: !1
|
|
63
|
+
}, this.planes.set(r, i), this.changed.set(r, r.lastChangedTime);
|
|
64
|
+
}
|
|
65
|
+
i = i, i.tracked = !!n, n && i.matrix.set(n.transform.matrix), a ? this.options.onAdd?.(i) : o && this.options.onChange?.(i), this.options.show && this.draw(i, n, a || o);
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
draw(e, t, n) {
|
|
70
|
+
let { engine: r, material: a } = this.options.show, o = this.meshes.get(e.plane);
|
|
71
|
+
if (o || this.meshes.set(e.plane, o = r.createMesh().setMaterial(a)), n && e.polygon.length >= 6 && o.setGeometry(i(e.polygon)), o.visible = !!t, t) {
|
|
72
|
+
let { position: e, orientation: n } = t.transform;
|
|
73
|
+
o.setPosition([
|
|
74
|
+
e.x,
|
|
75
|
+
e.y,
|
|
76
|
+
e.z
|
|
77
|
+
]).setQuaternion([
|
|
78
|
+
n.x,
|
|
79
|
+
n.y,
|
|
80
|
+
n.z,
|
|
81
|
+
n.w
|
|
82
|
+
]);
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
dispose() {
|
|
86
|
+
this.unsubscribe();
|
|
87
|
+
for (let e of this.meshes.values()) e.dispose();
|
|
88
|
+
this.meshes.clear(), this.planes.clear();
|
|
89
|
+
}
|
|
90
|
+
[Symbol.dispose]() {
|
|
91
|
+
this.dispose();
|
|
92
|
+
}
|
|
93
|
+
}, s = {
|
|
94
|
+
usagePreference: ["cpu-optimized"],
|
|
95
|
+
dataFormatPreference: [
|
|
96
|
+
"float32",
|
|
97
|
+
"luminance-alpha",
|
|
98
|
+
"unsigned-short"
|
|
99
|
+
]
|
|
100
|
+
}, c = class t {
|
|
101
|
+
constructor(e) {
|
|
102
|
+
this.host = e;
|
|
103
|
+
}
|
|
104
|
+
static create(n) {
|
|
105
|
+
let r = n?.session;
|
|
106
|
+
if (!r) throw e("state", "ARDepth needs an active XR session; await xr.requestSession first");
|
|
107
|
+
if (!a(r, "depth-sensing")) throw e("unsupported", "The XR session has no 'depth-sensing' feature; ask for it with init: {depthSensing}");
|
|
108
|
+
let i;
|
|
109
|
+
try {
|
|
110
|
+
i = r.depthUsage;
|
|
111
|
+
} catch (t) {
|
|
112
|
+
throw Object.assign(e("unsupported", "The XR session has no depth sensing; start it with init: {depthSensing: DEPTH_SENSING}"), { cause: t });
|
|
113
|
+
}
|
|
114
|
+
if (i !== void 0 && i !== "cpu-optimized") throw e("unsupported", "ARDepth reads CPU depth; start the session with usagePreference ['cpu-optimized']");
|
|
115
|
+
return new t(n);
|
|
116
|
+
}
|
|
117
|
+
information(e, t) {
|
|
118
|
+
let n = e.getDepthInformation;
|
|
119
|
+
return n ? n.call(e, t) ?? null : null;
|
|
120
|
+
}
|
|
121
|
+
depthAt(e, t, n, r) {
|
|
122
|
+
if (!(n >= 0 && n <= 1 && r >= 0 && r <= 1)) throw RangeError("ARDepth points run from 0 to 1 from the top left of the view; pass normalized coordinates");
|
|
123
|
+
return this.information(e, t)?.getDepthInMeters(n, r) ?? null;
|
|
124
|
+
}
|
|
125
|
+
pointAt(e, t, n, r) {
|
|
126
|
+
let i = this.information(e, t);
|
|
127
|
+
if (!i) return null;
|
|
128
|
+
let a = i.getDepthInMeters(n, r);
|
|
129
|
+
if (!(a > 0)) return null;
|
|
130
|
+
let o = i.projectionMatrix ?? t.projectionMatrix, s = (i.transform ?? t.transform).matrix, c = 2 * n - 1, l = 1 - 2 * r, u = (c + o[8]) / o[0], d = (l + o[9]) / o[5], f = u * a, p = d * a, m = -a;
|
|
131
|
+
return [
|
|
132
|
+
s[0] * f + s[4] * p + s[8] * m + s[12],
|
|
133
|
+
s[1] * f + s[5] * p + s[9] * m + s[13],
|
|
134
|
+
s[2] * f + s[6] * p + s[10] * m + s[14]
|
|
135
|
+
];
|
|
136
|
+
}
|
|
137
|
+
}, l = .28209479177387814, u = class t {
|
|
138
|
+
constructor(e, t, r) {
|
|
139
|
+
this.host = e, this.probe = t, this.lighting = r, this.estimate = null, this.sh = new n(), this.turn = {
|
|
140
|
+
x: 0,
|
|
141
|
+
y: 0,
|
|
142
|
+
z: 0,
|
|
143
|
+
w: 1
|
|
144
|
+
}, this.value = {
|
|
145
|
+
sh: /* @__PURE__ */ new Float32Array(27),
|
|
146
|
+
direction: [
|
|
147
|
+
0,
|
|
148
|
+
1,
|
|
149
|
+
0
|
|
150
|
+
],
|
|
151
|
+
intensity: [
|
|
152
|
+
0,
|
|
153
|
+
0,
|
|
154
|
+
0
|
|
155
|
+
]
|
|
156
|
+
}, this.unsubscribe = e.onFrame((e) => this.update(e.frame));
|
|
157
|
+
}
|
|
158
|
+
static create(e, { lighting: n } = {}) {
|
|
159
|
+
return t.withReflection(e, { lighting: n });
|
|
160
|
+
}
|
|
161
|
+
static async withReflection(n, { lighting: r, reflectionFormat: i } = {}) {
|
|
162
|
+
let o = n?.session;
|
|
163
|
+
if (!o) throw e("state", "ARLight needs an active XR session; await xr.requestSession first");
|
|
164
|
+
if (!a(o, "light-estimation") || !o.requestLightProbe) throw e("unsupported", "The XR session has no 'light-estimation' feature; ask for it when starting the session");
|
|
165
|
+
return new t(n, await o.requestLightProbe(i ? { reflectionFormat: i } : void 0), r);
|
|
166
|
+
}
|
|
167
|
+
update(e) {
|
|
168
|
+
let t = e.getLightEstimate?.call(e, this.probe), n = this.host.referenceSpace;
|
|
169
|
+
if (!t || !n) {
|
|
170
|
+
this.estimate = null;
|
|
171
|
+
return;
|
|
172
|
+
}
|
|
173
|
+
let r = this.value, i = t.primaryLightDirection, a = e.getPose(this.probe.probeSpace, n), o = a?.transform.matrix;
|
|
174
|
+
if (this.sh.fromArray(t.sphericalHarmonicsCoefficients), a) {
|
|
175
|
+
let e = a.transform.orientation;
|
|
176
|
+
this.turn.x = e.x, this.turn.y = e.y, this.turn.z = e.z, this.turn.w = e.w, this.sh.rotate(this.turn);
|
|
177
|
+
}
|
|
178
|
+
this.sh.toArray(r.sh), r.direction[0] = o ? o[0] * i.x + o[4] * i.y + o[8] * i.z : i.x, r.direction[1] = o ? o[1] * i.x + o[5] * i.y + o[9] * i.z : i.y, r.direction[2] = o ? o[2] * i.x + o[6] * i.y + o[10] * i.z : i.z;
|
|
179
|
+
let s = t.primaryLightIntensity;
|
|
180
|
+
r.intensity[0] = s.x, r.intensity[1] = s.y, r.intensity[2] = s.z, this.estimate = r;
|
|
181
|
+
let c = this.lighting;
|
|
182
|
+
if (!c) return;
|
|
183
|
+
let u = Math.max(s.x, s.y, s.z);
|
|
184
|
+
c.direction[0] = r.direction[0], c.direction[1] = r.direction[1], c.direction[2] = r.direction[2], c.direction[3] = u, u > 0 && (c.radiance[0] = s.x / u, c.radiance[1] = s.y / u, c.radiance[2] = s.z / u), c.radiance[3] = 1;
|
|
185
|
+
for (let e = 0; e < 3; e++) c.ambient[e] = Math.max(0, r.sh[e] * l);
|
|
186
|
+
}
|
|
187
|
+
dispose() {
|
|
188
|
+
this.unsubscribe();
|
|
189
|
+
}
|
|
190
|
+
[Symbol.dispose]() {
|
|
191
|
+
this.dispose();
|
|
192
|
+
}
|
|
193
|
+
};
|
|
194
|
+
//#endregion
|
|
195
|
+
export { c as ARDepth, u as ARLight, o as ARPlanes, s as DEPTH_SENSING, i as planeGeometry, r as triangulate };
|
package/dist/body.d.ts
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import type { XRNativeFrame, XRNativeSession, XRSpace } from './types.js';
|
|
2
|
+
/** The session feature descriptor that asks the browser for body tracking. */
|
|
3
|
+
export declare const BODY_TRACKING_FEATURE = "body-tracking";
|
|
4
|
+
/** The 83 XRBodyJoint names, in the spec's order. */
|
|
5
|
+
export declare const BODY_JOINT_NAMES: readonly string[];
|
|
6
|
+
/** XRBodyJoint to the humanoid bone name `@margoengine/rigging` uses (`HumanoidBone`); joints with no bone are absent. */
|
|
7
|
+
export declare const BODY_JOINT_TO_HUMANOID: Readonly<Record<string, string>>;
|
|
8
|
+
/** What `BodyTracking.create` needs of an `XR`: its session and reference space. */
|
|
9
|
+
export interface BodyTrackingHost {
|
|
10
|
+
readonly session: XRNativeSession | null;
|
|
11
|
+
readonly referenceSpace: XRSpace | undefined;
|
|
12
|
+
}
|
|
13
|
+
/** Options for {@link BodyTracking.create}. */
|
|
14
|
+
export interface BodyTrackingOptions {
|
|
15
|
+
/** Throw MargoError 'unsupported' instead of reporting `available: false` when body tracking is missing. Default false. */
|
|
16
|
+
required?: boolean;
|
|
17
|
+
}
|
|
18
|
+
/** One frame of body joint poses in the session's reference space; the object is reused, so copy what you keep. */
|
|
19
|
+
export interface BodyPose {
|
|
20
|
+
/** Joint order is {@link BODY_JOINT_NAMES}: 83 x [x, y, z] in metres. */
|
|
21
|
+
readonly positions: Float32Array;
|
|
22
|
+
/** 83 x unit quaternion [x, y, z, w]. */
|
|
23
|
+
readonly rotations: Float32Array;
|
|
24
|
+
/** True when the device emulated (guessed) any joint position. */
|
|
25
|
+
emulated: boolean;
|
|
26
|
+
/** A joint's [x, y, z] (a view into `positions`), or undefined for an unknown name. */
|
|
27
|
+
position(joint: string): Float32Array | undefined;
|
|
28
|
+
/** A joint's [x, y, z, w] (a view into `rotations`), or undefined for an unknown name. */
|
|
29
|
+
rotation(joint: string): Float32Array | undefined;
|
|
30
|
+
}
|
|
31
|
+
/** One humanoid bone's world pose. */
|
|
32
|
+
export interface HumanoidBonePose {
|
|
33
|
+
position: [number, number, number];
|
|
34
|
+
rotation: [number, number, number, number];
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Body tracking in a session started with the 'body-tracking' feature (an optional feature: few devices have it).
|
|
38
|
+
* `BodyTracking.create(xr)` never throws without the feature: check `available`, or pass `{required: true}` for a
|
|
39
|
+
* MargoError 'unsupported'. `read(frame)` gives all 83 joints or null (the spec reports every joint or none).
|
|
40
|
+
*/
|
|
41
|
+
export declare class BodyTracking {
|
|
42
|
+
private readonly host;
|
|
43
|
+
/** Whether the session was granted the 'body-tracking' feature. */
|
|
44
|
+
readonly available: boolean;
|
|
45
|
+
private readonly pose;
|
|
46
|
+
private readonly spaces;
|
|
47
|
+
private spacesFor?;
|
|
48
|
+
private constructor();
|
|
49
|
+
/** Reads the session's granted features. Throws 'state' without a session, 'unsupported' only with `required`. */
|
|
50
|
+
static create(xr: BodyTrackingHost, options?: BodyTrackingOptions): BodyTracking;
|
|
51
|
+
/**
|
|
52
|
+
* This frame's joint poses, or null without the feature, for another session's frame, while tracking is lost or when
|
|
53
|
+
* any joint has no pose.
|
|
54
|
+
*/
|
|
55
|
+
read(frame: XRNativeFrame): BodyPose | null;
|
|
56
|
+
}
|
|
57
|
+
/** A pose as world position and rotation per humanoid bone name, ready for `@margoengine/rigging` retargeting. */
|
|
58
|
+
export declare function humanoidPose(pose: BodyPose): Map<string, HumanoidBonePose>;
|
package/dist/body.js
ADDED
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
import { n as e } from "./errors-XvcwUnU4.js";
|
|
2
|
+
//#region src/body.ts
|
|
3
|
+
var t = "body-tracking", n = [
|
|
4
|
+
"index",
|
|
5
|
+
"middle",
|
|
6
|
+
"ring",
|
|
7
|
+
"little"
|
|
8
|
+
], r = (e) => [
|
|
9
|
+
`${e}-hand-palm`,
|
|
10
|
+
`${e}-hand-wrist`,
|
|
11
|
+
...[
|
|
12
|
+
"metacarpal",
|
|
13
|
+
"phalanx-proximal",
|
|
14
|
+
"phalanx-distal",
|
|
15
|
+
"tip"
|
|
16
|
+
].map((t) => `${e}-hand-thumb-${t}`),
|
|
17
|
+
...n.flatMap((t) => [
|
|
18
|
+
"metacarpal",
|
|
19
|
+
"phalanx-proximal",
|
|
20
|
+
"phalanx-intermediate",
|
|
21
|
+
"phalanx-distal",
|
|
22
|
+
"tip"
|
|
23
|
+
].map((n) => t === "middle" && n === "metacarpal" ? `${e}-hand-middle-phalanx-metacarpal` : `${e}-hand-${t}-${n}`))
|
|
24
|
+
], i = (e) => [
|
|
25
|
+
"shoulder",
|
|
26
|
+
"scapula",
|
|
27
|
+
"arm-upper",
|
|
28
|
+
"arm-lower",
|
|
29
|
+
"hand-wrist-twist"
|
|
30
|
+
].map((t) => `${e}-${t}`), a = (e) => [
|
|
31
|
+
"upper-leg",
|
|
32
|
+
"lower-leg",
|
|
33
|
+
"foot-ankle-twist",
|
|
34
|
+
"foot-ankle",
|
|
35
|
+
"foot-subtalar",
|
|
36
|
+
"foot-transverse",
|
|
37
|
+
"foot-ball"
|
|
38
|
+
].map((t) => `${e}-${t}`), o = Object.freeze([
|
|
39
|
+
"hips",
|
|
40
|
+
"spine-lower",
|
|
41
|
+
"spine-middle",
|
|
42
|
+
"spine-upper",
|
|
43
|
+
"chest",
|
|
44
|
+
"neck",
|
|
45
|
+
"head",
|
|
46
|
+
...i("left"),
|
|
47
|
+
...i("right"),
|
|
48
|
+
...r("left"),
|
|
49
|
+
...r("right"),
|
|
50
|
+
...a("left"),
|
|
51
|
+
...a("right")
|
|
52
|
+
]), s = {
|
|
53
|
+
hips: "hips",
|
|
54
|
+
"spine-lower": "spine",
|
|
55
|
+
"spine-middle": "chest",
|
|
56
|
+
"spine-upper": "upperChest",
|
|
57
|
+
neck: "neck",
|
|
58
|
+
head: "head"
|
|
59
|
+
}, c = {
|
|
60
|
+
shoulder: "Shoulder",
|
|
61
|
+
"arm-upper": "UpperArm",
|
|
62
|
+
"arm-lower": "LowerArm",
|
|
63
|
+
"hand-wrist": "Hand",
|
|
64
|
+
"upper-leg": "UpperLeg",
|
|
65
|
+
"lower-leg": "LowerLeg",
|
|
66
|
+
"foot-ankle": "Foot",
|
|
67
|
+
"foot-ball": "Toes"
|
|
68
|
+
}, l = (e) => e[0].toUpperCase() + e.slice(1), u = Object.freeze(Object.fromEntries(o.flatMap((e) => {
|
|
69
|
+
if (s[e]) return [[e, s[e]]];
|
|
70
|
+
let t = /^(left|right)-(.+)$/.exec(e);
|
|
71
|
+
if (!t) return [];
|
|
72
|
+
let [, n, r] = t;
|
|
73
|
+
if (c[r]) return [[e, n + c[r]]];
|
|
74
|
+
let i = /^hand-(thumb|index|middle|ring|little)-(?:phalanx-)?(metacarpal|proximal|intermediate|distal)$/.exec(r.replace("middle-phalanx-metacarpal", "middle-metacarpal"));
|
|
75
|
+
return !i || i[2] === "metacarpal" && i[1] !== "thumb" ? [] : [[e, n + l(i[1]) + l(i[2])]];
|
|
76
|
+
}))), d = new Map(o.map((e, t) => [e, t])), f = class t {
|
|
77
|
+
constructor(e, t) {
|
|
78
|
+
this.host = e, this.available = t, this.spaces = [];
|
|
79
|
+
let n = new Float32Array(o.length * 3), r = new Float32Array(o.length * 4);
|
|
80
|
+
this.pose = {
|
|
81
|
+
positions: n,
|
|
82
|
+
rotations: r,
|
|
83
|
+
emulated: !1,
|
|
84
|
+
position: (e) => d.has(e) ? n.subarray(d.get(e) * 3, d.get(e) * 3 + 3) : void 0,
|
|
85
|
+
rotation: (e) => d.has(e) ? r.subarray(d.get(e) * 4, d.get(e) * 4 + 4) : void 0
|
|
86
|
+
};
|
|
87
|
+
}
|
|
88
|
+
static create(n, r = {}) {
|
|
89
|
+
let i = n?.session;
|
|
90
|
+
if (!i) throw e("state", "BodyTracking needs an active XR session; await xr.requestSession first");
|
|
91
|
+
let a = !i.enabledFeatures || i.enabledFeatures.includes("body-tracking");
|
|
92
|
+
if (!a && r.required) throw e("unsupported", "The XR session has no 'body-tracking' feature; request it in optionalFeatures and use a device that supports it");
|
|
93
|
+
return new t(n, a);
|
|
94
|
+
}
|
|
95
|
+
read(e) {
|
|
96
|
+
let t = this.host.referenceSpace, n = e?.body;
|
|
97
|
+
if (!this.available || !t || !n || e.session !== this.host.session) return null;
|
|
98
|
+
if (this.spacesFor !== n) {
|
|
99
|
+
this.spaces.length = 0;
|
|
100
|
+
for (let e of o) this.spaces.push(n.get(e));
|
|
101
|
+
this.spacesFor = n;
|
|
102
|
+
}
|
|
103
|
+
let { positions: r, rotations: i } = this.pose, a = !1;
|
|
104
|
+
for (let n = 0; n < o.length; n++) {
|
|
105
|
+
let o = this.spaces[n], s = o && e.getPose(o, t);
|
|
106
|
+
if (!s) return null;
|
|
107
|
+
let c = s.transform;
|
|
108
|
+
r[n * 3] = c.position.x, r[n * 3 + 1] = c.position.y, r[n * 3 + 2] = c.position.z, i[n * 4] = c.orientation.x, i[n * 4 + 1] = c.orientation.y, i[n * 4 + 2] = c.orientation.z, i[n * 4 + 3] = c.orientation.w, a ||= s.emulatedPosition;
|
|
109
|
+
}
|
|
110
|
+
return this.pose.emulated = a, this.pose;
|
|
111
|
+
}
|
|
112
|
+
};
|
|
113
|
+
function p(e) {
|
|
114
|
+
let t = /* @__PURE__ */ new Map();
|
|
115
|
+
for (let [n, r] of Object.entries(u)) {
|
|
116
|
+
let i = d.get(n);
|
|
117
|
+
t.set(r, {
|
|
118
|
+
position: [
|
|
119
|
+
e.positions[i * 3],
|
|
120
|
+
e.positions[i * 3 + 1],
|
|
121
|
+
e.positions[i * 3 + 2]
|
|
122
|
+
],
|
|
123
|
+
rotation: [
|
|
124
|
+
e.rotations[i * 4],
|
|
125
|
+
e.rotations[i * 4 + 1],
|
|
126
|
+
e.rotations[i * 4 + 2],
|
|
127
|
+
e.rotations[i * 4 + 3]
|
|
128
|
+
]
|
|
129
|
+
});
|
|
130
|
+
}
|
|
131
|
+
return t;
|
|
132
|
+
}
|
|
133
|
+
//#endregion
|
|
134
|
+
export { o as BODY_JOINT_NAMES, u as BODY_JOINT_TO_HUMANOID, t as BODY_TRACKING_FEATURE, f as BodyTracking, p as humanoidPose };
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import type { XRInputSource, XRNativeFrame, XRSpace, XRPose } from './types.js';
|
|
2
|
+
/** Buffers are reused; read them during the frame callback or copy to retain a snapshot. */
|
|
3
|
+
export declare class XRController {
|
|
4
|
+
readonly source: XRInputSource;
|
|
5
|
+
/** Pose of the pointing ray this frame, or null when untracked. */
|
|
6
|
+
targetRay: XRPose | null;
|
|
7
|
+
/** Pose of the held grip this frame, or null when untracked or absent. */
|
|
8
|
+
grip: XRPose | null;
|
|
9
|
+
/** Column-major target-ray matrix; zero when untracked. */
|
|
10
|
+
readonly targetRayMatrix: Float32Array<ArrayBuffer>;
|
|
11
|
+
/** Column-major grip matrix; zero when untracked. */
|
|
12
|
+
readonly gripMatrix: Float32Array<ArrayBuffer>;
|
|
13
|
+
/** Per button: value, pressed, touched (32 buttons). */
|
|
14
|
+
readonly buttons: Float32Array<ArrayBuffer>;
|
|
15
|
+
/** Gamepad axes (up to 16). */
|
|
16
|
+
readonly axes: Float32Array<ArrayBuffer>;
|
|
17
|
+
/** 25 column-major joint matrices when hand tracking is granted. */
|
|
18
|
+
readonly handMatrices: Float32Array<ArrayBuffer>;
|
|
19
|
+
/** 1 for each joint that has a pose this frame. */
|
|
20
|
+
readonly handTracked: Uint8Array<ArrayBuffer>;
|
|
21
|
+
/** Each joint's radius in metres (WebXR Hand Input: a sphere at the joint touching the skin); 0 when untracked. */
|
|
22
|
+
readonly handRadii: Float32Array<ArrayBuffer>;
|
|
23
|
+
/** Buttons the gamepad reports. */
|
|
24
|
+
buttonCount: number;
|
|
25
|
+
/** Axes the gamepad reports. */
|
|
26
|
+
axisCount: number;
|
|
27
|
+
private readonly joints;
|
|
28
|
+
/** @param source The browser's input source. */
|
|
29
|
+
constructor(source: XRInputSource);
|
|
30
|
+
/** Reads this frame's poses and gamepad into the reused buffers. */
|
|
31
|
+
update(frame: XRNativeFrame, space: XRSpace): void;
|
|
32
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { checkXRRun, xrRunGaps, XR_RUN_MIN_FRAMES, XR_RUN_TARGETS } from './shared/xr-run.js';
|
|
2
|
+
import type { XRRun, XRRunObserved, XRRunTarget } from './shared/xr-run.js';
|
|
3
|
+
import type { XRFrameState, XRMode, XRNativeSession } from './types.js';
|
|
4
|
+
export { checkXRRun, xrRunGaps, XR_RUN_MIN_FRAMES, XR_RUN_TARGETS };
|
|
5
|
+
export type { XRRun, XRRunObserved, XRRunTarget };
|
|
6
|
+
/** What `runDeviceCheck` needs of an `XR`. */
|
|
7
|
+
export interface DeviceCheckHost {
|
|
8
|
+
requestSession(mode: XRMode, options: {
|
|
9
|
+
optionalFeatures?: string[];
|
|
10
|
+
requiredFeatures?: string[];
|
|
11
|
+
signal?: AbortSignal;
|
|
12
|
+
onFrame?: (state: XRFrameState) => void;
|
|
13
|
+
}): Promise<XRNativeSession>;
|
|
14
|
+
end(): Promise<void>;
|
|
15
|
+
}
|
|
16
|
+
/** Options for {@link runDeviceCheck}. */
|
|
17
|
+
export interface DeviceCheckOptions {
|
|
18
|
+
/** Session to run. Default 'immersive-vr'. */
|
|
19
|
+
mode?: XRMode;
|
|
20
|
+
/** How long the session runs in seconds, from its first frame. Default 20, about 1,400 frames on a 72 Hz headset. */
|
|
21
|
+
seconds?: number;
|
|
22
|
+
/** Features the session must have. Default none. */
|
|
23
|
+
requiredFeatures?: string[];
|
|
24
|
+
/** Features to ask for and record when granted. Default hand tracking, hit testing, anchors, planes, depth and light. */
|
|
25
|
+
optionalFeatures?: string[];
|
|
26
|
+
/** Asks the tester what they saw once the session is over. Default none: the run then proves no route. */
|
|
27
|
+
observe?: (run: XRRun) => Promise<XRRunObserved>;
|
|
28
|
+
/** Ends the session early; the promise rejects with the reason. */
|
|
29
|
+
signal?: AbortSignal;
|
|
30
|
+
/** Called every frame, for the page's own scene updates. */
|
|
31
|
+
onFrame?: (state: XRFrameState) => void;
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Runs one session for `seconds` and resolves with what happened. Call it from a user gesture. A failure in `onFrame`
|
|
35
|
+
* is recorded in `errors`, since finding those is what the run is for; one that stops the session starting rejects.
|
|
36
|
+
* A run with `native` false is a stand-in and proves no route (`xrRunGaps`).
|
|
37
|
+
* @throws RangeError when `seconds` is not above 0.
|
|
38
|
+
*/
|
|
39
|
+
export declare function runDeviceCheck(xr: DeviceCheckHost, options?: DeviceCheckOptions): Promise<XRRun>;
|