@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
|
@@ -0,0 +1,232 @@
|
|
|
1
|
+
/// <reference lib="esnext.disposable" preserve="true" />
|
|
2
|
+
import { PbrMaterial, type EngineKernel, type Scene } from '@margoengine/core';
|
|
3
|
+
import type { XRSessionOptions } from './index.js';
|
|
4
|
+
import type { XRAnchor, XRFrameState, XRNativeSession, XRSpace } from './types.js';
|
|
5
|
+
/** Session and frame hooks accepted by AR tracking extras. */
|
|
6
|
+
export interface ARExtraHost {
|
|
7
|
+
readonly session: XRNativeSession | null;
|
|
8
|
+
readonly referenceSpace: XRSpace | undefined;
|
|
9
|
+
onFrame(callback: (state: XRFrameState) => void): () => void;
|
|
10
|
+
}
|
|
11
|
+
/** DOM overlay options. */
|
|
12
|
+
export interface DOMOverlayOptions {
|
|
13
|
+
/** Suppress duplicate XR-world select actions over the overlay. Default true. */
|
|
14
|
+
preventWorldSelect?: boolean;
|
|
15
|
+
/** Remove event listeners on abort. */
|
|
16
|
+
signal?: AbortSignal;
|
|
17
|
+
}
|
|
18
|
+
/** Adds the standard dom-overlay session feature and root while preserving other session init members. */
|
|
19
|
+
export declare function domOverlaySessionOptions(root: Element, base?: XRSessionOptions): XRSessionOptions;
|
|
20
|
+
/** Owns beforexrselect handling for a browser-managed DOM overlay. */
|
|
21
|
+
export declare class DOMOverlay {
|
|
22
|
+
private disposed;
|
|
23
|
+
private readonly stop;
|
|
24
|
+
/** Attaches after negotiating domOverlaySessionOptions. */
|
|
25
|
+
static attach(xr: ARExtraHost, root: Element, options?: DOMOverlayOptions): DOMOverlay;
|
|
26
|
+
private constructor();
|
|
27
|
+
/** Browser overlay presentation type, or null if it does not report a type. */
|
|
28
|
+
readonly type: string | null;
|
|
29
|
+
/** Removes listeners; the caller retains the DOM element. Idempotent. */
|
|
30
|
+
dispose(): void;
|
|
31
|
+
[Symbol.dispose]: () => void;
|
|
32
|
+
}
|
|
33
|
+
/** A bitmap and its physical printed width. The caller owns the bitmap. */
|
|
34
|
+
export interface TrackedImageInit {
|
|
35
|
+
readonly image: ImageBitmap;
|
|
36
|
+
readonly widthInMeters: number;
|
|
37
|
+
}
|
|
38
|
+
/** Chooses the current marker-tracking feature or legacy image-tracking feature explicitly. */
|
|
39
|
+
export interface ImageTrackingSessionOptions {
|
|
40
|
+
/** Use the image-tracking feature and getTrackedImageScores of Chrome's shipped module. Default false. */
|
|
41
|
+
legacy?: boolean;
|
|
42
|
+
}
|
|
43
|
+
/** Configures image targets before session negotiation. Default current marker-tracking feature. */
|
|
44
|
+
export declare function imageTrackingSessionOptions(images: readonly TrackedImageInit[], options?: ImageTrackingSessionOptions, base?: XRSessionOptions): XRSessionOptions;
|
|
45
|
+
/** A copied image observation, retained independently of the browser frame. */
|
|
46
|
+
export interface TrackedImage {
|
|
47
|
+
readonly index: number;
|
|
48
|
+
readonly trackingState: 'tracked' | 'emulated';
|
|
49
|
+
readonly measuredWidthInMeters: number;
|
|
50
|
+
/** Pose in the host reference space, or null while the image pose is unavailable. */
|
|
51
|
+
readonly matrix: Float32Array | null;
|
|
52
|
+
}
|
|
53
|
+
/** Image tracking subscriptions. */
|
|
54
|
+
export interface ImageTrackingOptions {
|
|
55
|
+
/** Called once per frame with copied observations. Default no callback; images remains available. */
|
|
56
|
+
onUpdate?: (images: readonly TrackedImage[]) => void;
|
|
57
|
+
/** Remove frame and session listeners on abort. */
|
|
58
|
+
signal?: AbortSignal;
|
|
59
|
+
}
|
|
60
|
+
/** Tracks supplied image targets with both the current and legacy browser module. */
|
|
61
|
+
export declare class ImageTracking {
|
|
62
|
+
private readonly host;
|
|
63
|
+
private readonly options;
|
|
64
|
+
/** Current copied observations, keyed by target index. */
|
|
65
|
+
readonly images: Map<number, TrackedImage>;
|
|
66
|
+
private readonly lifetime;
|
|
67
|
+
private disposed;
|
|
68
|
+
/** Attaches to the negotiated current or legacy image tracking session. */
|
|
69
|
+
static attach(host: ARExtraHost, options?: ImageTrackingOptions): ImageTracking;
|
|
70
|
+
private constructor();
|
|
71
|
+
/** Returns trackable/untrackable scores in target order; abort cancels the wait. */
|
|
72
|
+
trackability(signal?: AbortSignal): Promise<readonly string[]>;
|
|
73
|
+
private update;
|
|
74
|
+
/** Unsubscribes, cancels pending score waits and clears observations. Idempotent. */
|
|
75
|
+
dispose(): void;
|
|
76
|
+
[Symbol.dispose]: () => void;
|
|
77
|
+
}
|
|
78
|
+
/** Session options for the WebXR marker tracking module: markers (QR codes) the runtime finds itself, no targets. */
|
|
79
|
+
export declare function markerTrackingSessionOptions(base?: XRSessionOptions): XRSessionOptions;
|
|
80
|
+
/**
|
|
81
|
+
* Which tracking module a session runs: `'markers'` for the marker tracking module (discovered markers,
|
|
82
|
+
* `getMarkerTrackingResults`), `'images'` for the image tracking module (targets given at session start), or null.
|
|
83
|
+
* Both may be granted under the `'marker-tracking'` name, so this reads the session's members.
|
|
84
|
+
*/
|
|
85
|
+
export declare function trackingModule(session: XRNativeSession | null): 'markers' | 'images' | null;
|
|
86
|
+
/** The physical size of a found marker. */
|
|
87
|
+
export interface MarkerExtent {
|
|
88
|
+
readonly widthInMeters: number;
|
|
89
|
+
readonly heightInMeters: number;
|
|
90
|
+
}
|
|
91
|
+
/** A copied marker observation, retained independently of the browser frame. */
|
|
92
|
+
export interface TrackedMarker {
|
|
93
|
+
/**
|
|
94
|
+
* A number the tracker gives each marker the runtime reports as one object; it holds while the runtime keeps that
|
|
95
|
+
* object, so use it as a map key. A marker lost, or paused, may come back under a new number.
|
|
96
|
+
*/
|
|
97
|
+
readonly id: number;
|
|
98
|
+
/** `'emulated'` while the runtime holds a marker it can no longer see. */
|
|
99
|
+
readonly trackingState: 'tracked' | 'emulated';
|
|
100
|
+
/** The decoded payload (a QR code's text), or null while the runtime has none. */
|
|
101
|
+
readonly payload: string | null;
|
|
102
|
+
/** The marker's measured size, or null while unknown. */
|
|
103
|
+
readonly extent: MarkerExtent | null;
|
|
104
|
+
/** Pose of the marker centre in the host reference space (x-y is the marker face), or null while unavailable. */
|
|
105
|
+
readonly matrix: Float32Array | null;
|
|
106
|
+
}
|
|
107
|
+
/** Marker tracking subscriptions. */
|
|
108
|
+
export interface MarkerTrackingOptions {
|
|
109
|
+
/** Called once per frame with copied observations. Default no callback; markers remains available. */
|
|
110
|
+
onUpdate?: (markers: readonly TrackedMarker[]) => void;
|
|
111
|
+
/** Remove frame and session listeners on abort. */
|
|
112
|
+
signal?: AbortSignal;
|
|
113
|
+
}
|
|
114
|
+
/** Tracks the markers a marker tracking session finds, with pause and resume. */
|
|
115
|
+
export declare class MarkerTracking {
|
|
116
|
+
private readonly host;
|
|
117
|
+
private readonly options;
|
|
118
|
+
/** Current copied observations in the runtime's order; replaced every frame. */
|
|
119
|
+
markers: readonly TrackedMarker[];
|
|
120
|
+
private readonly lifetime;
|
|
121
|
+
private disposed;
|
|
122
|
+
private readonly ids;
|
|
123
|
+
private nextId;
|
|
124
|
+
private wanted;
|
|
125
|
+
/** Attaches after negotiating markerTrackingSessionOptions. */
|
|
126
|
+
static attach(host: ARExtraHost, options?: MarkerTrackingOptions): MarkerTracking;
|
|
127
|
+
private constructor();
|
|
128
|
+
/**
|
|
129
|
+
* True while the runtime looks for markers, or is asked to from the next frame; false after pause() or when the
|
|
130
|
+
* session does not say.
|
|
131
|
+
*/
|
|
132
|
+
get active(): boolean;
|
|
133
|
+
/** Stops the runtime looking for markers, to save power, as the next frame begins; observations empty from it. */
|
|
134
|
+
pause(): void;
|
|
135
|
+
/** Starts the runtime looking for markers again as the next frame begins. */
|
|
136
|
+
resume(): void;
|
|
137
|
+
private request;
|
|
138
|
+
private update;
|
|
139
|
+
/** Unsubscribes and clears observations; tracking itself is left as it is. Idempotent. */
|
|
140
|
+
dispose(): void;
|
|
141
|
+
[Symbol.dispose]: () => void;
|
|
142
|
+
}
|
|
143
|
+
/** Browser mesh detection object. */
|
|
144
|
+
export interface XRDetectedMesh {
|
|
145
|
+
readonly meshSpace: XRSpace;
|
|
146
|
+
readonly vertices: Float32Array;
|
|
147
|
+
readonly indices: Uint32Array;
|
|
148
|
+
readonly lastChangedTime: number;
|
|
149
|
+
readonly semanticLabel?: string | null;
|
|
150
|
+
}
|
|
151
|
+
/** Copied mesh geometry plus a current pose, usable after the browser frame ends. */
|
|
152
|
+
export interface DetectedMesh {
|
|
153
|
+
readonly source: XRDetectedMesh;
|
|
154
|
+
readonly vertices: Float32Array;
|
|
155
|
+
readonly indices: Uint32Array;
|
|
156
|
+
readonly matrix: Float32Array | null;
|
|
157
|
+
readonly label: string;
|
|
158
|
+
readonly lastChangedTime: number;
|
|
159
|
+
}
|
|
160
|
+
/** Mesh tracking options. */
|
|
161
|
+
export interface MeshDetectionOptions {
|
|
162
|
+
/** Called for new geometry. */
|
|
163
|
+
onAdd?: (mesh: DetectedMesh) => void;
|
|
164
|
+
/** Called when geometry or semantic label changes; pose alone does not count as geometry change. */
|
|
165
|
+
onChange?: (mesh: DetectedMesh) => void;
|
|
166
|
+
/** Called when the browser removes a mesh, or the tracker closes. */
|
|
167
|
+
onRemove?: (mesh: DetectedMesh) => void;
|
|
168
|
+
/** Optional drawing. Default no visualization. Scene defaults to engine.scene; material defaults to grey. */
|
|
169
|
+
show?: {
|
|
170
|
+
engine: EngineKernel;
|
|
171
|
+
scene?: Scene;
|
|
172
|
+
material?: PbrMaterial;
|
|
173
|
+
};
|
|
174
|
+
/** Close on abort. */
|
|
175
|
+
signal?: AbortSignal;
|
|
176
|
+
}
|
|
177
|
+
/** Tracks native triangle meshes and optionally displays them as Margo meshes. */
|
|
178
|
+
export declare class MeshDetection {
|
|
179
|
+
private readonly host;
|
|
180
|
+
private readonly options;
|
|
181
|
+
private readonly geometry;
|
|
182
|
+
/** Current copied records by the browser's stable mesh identity. */
|
|
183
|
+
readonly meshes: Map<XRDetectedMesh, DetectedMesh>;
|
|
184
|
+
private readonly lifetime;
|
|
185
|
+
private readonly visible;
|
|
186
|
+
private readonly drawScene?;
|
|
187
|
+
private disposed;
|
|
188
|
+
/** Loads normal generation lazily only for show, then subscribes to an active mesh-detection session. */
|
|
189
|
+
static create(host: ARExtraHost, options?: MeshDetectionOptions): Promise<MeshDetection>;
|
|
190
|
+
private constructor();
|
|
191
|
+
private update;
|
|
192
|
+
private draw;
|
|
193
|
+
/** Releases visualization meshes and subscriptions, and cancels waits. Idempotent. */
|
|
194
|
+
dispose(): void;
|
|
195
|
+
[Symbol.dispose]: () => void;
|
|
196
|
+
}
|
|
197
|
+
/** A browser anchor that can persist across sessions. */
|
|
198
|
+
export interface XRPersistentAnchor extends XRAnchor {
|
|
199
|
+
requestPersistentHandle(): Promise<string>;
|
|
200
|
+
}
|
|
201
|
+
/** The native persistent-anchor session API. */
|
|
202
|
+
export interface PersistentAnchorSession extends XRNativeSession {
|
|
203
|
+
readonly persistentAnchors: readonly string[];
|
|
204
|
+
restorePersistentAnchor(handle: string): Promise<XRAnchor>;
|
|
205
|
+
deletePersistentAnchor(handle: string): Promise<void>;
|
|
206
|
+
}
|
|
207
|
+
/** Persistent anchors remain browser/origin-owned until explicitly deleted. */
|
|
208
|
+
export declare class PersistentAnchors {
|
|
209
|
+
private readonly host;
|
|
210
|
+
private readonly lifetime;
|
|
211
|
+
private readonly session;
|
|
212
|
+
private disposed;
|
|
213
|
+
private readonly pendingSave;
|
|
214
|
+
private readonly pendingRestore;
|
|
215
|
+
/** Binds to an active anchors session. */
|
|
216
|
+
static attach(host: ARExtraHost, options?: {
|
|
217
|
+
signal?: AbortSignal;
|
|
218
|
+
}): PersistentAnchors;
|
|
219
|
+
private constructor();
|
|
220
|
+
/** Known origin-scoped persistent handles; the array is copied. */
|
|
221
|
+
get handles(): readonly string[];
|
|
222
|
+
private active;
|
|
223
|
+
/** Saves an anchor and returns the browser's handle. Abort is checked before and after the native operation. */
|
|
224
|
+
save(anchor: XRPersistentAnchor, signal?: AbortSignal): Promise<string>;
|
|
225
|
+
/** Restores tracking of a persistent handle; abort releases unclaimed tracking, retaining the saved handle. */
|
|
226
|
+
restore(handle: string, signal?: AbortSignal): Promise<XRAnchor>;
|
|
227
|
+
/** Explicitly deletes a saved anchor from the browser store. Abort checked before starting the native deletion. */
|
|
228
|
+
delete(handle: string, signal?: AbortSignal): Promise<void>;
|
|
229
|
+
/** Removes subscriptions. Persistent handles are kept until delete is called. Idempotent. */
|
|
230
|
+
dispose(): void;
|
|
231
|
+
[Symbol.dispose]: () => void;
|
|
232
|
+
}
|
package/dist/ar-extra.js
ADDED
|
@@ -0,0 +1,315 @@
|
|
|
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
|
+
import { t } from "./scene-owner-BRObO-WJ.js";
|
|
6
|
+
import { Geometry as n, PbrMaterial as r } from "@margoengine/core";
|
|
7
|
+
import { splitTRS as i } from "@margoengine/math/quat";
|
|
8
|
+
//#region src/ar-extra.ts
|
|
9
|
+
function a(e, t = {}) {
|
|
10
|
+
if (!e || e.nodeType !== 1) throw TypeError("DOM overlay needs an element; pass a container from document.createElement");
|
|
11
|
+
return {
|
|
12
|
+
...t,
|
|
13
|
+
requiredFeatures: [.../* @__PURE__ */ new Set([...t.requiredFeatures ?? [], "dom-overlay"])],
|
|
14
|
+
init: {
|
|
15
|
+
...t.init,
|
|
16
|
+
domOverlay: { root: e }
|
|
17
|
+
}
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
var o = class e {
|
|
21
|
+
static attach(t, n, r = {}) {__margoAssertCore(t,__margoCoreRequirement("1.0.0",import.meta.url));
|
|
22
|
+
return new e(t, n, r);
|
|
23
|
+
}
|
|
24
|
+
constructor(e, t, n) {
|
|
25
|
+
this.disposed = !1, n.signal?.throwIfAborted();
|
|
26
|
+
let r = c(e, "dom-overlay");
|
|
27
|
+
if (!t || t.nodeType !== 1) throw TypeError("DOM overlay needs an element; pass the root used to request the session");
|
|
28
|
+
let i = (e) => {
|
|
29
|
+
(n.preventWorldSelect ?? !0) && e.preventDefault();
|
|
30
|
+
}, a = () => this.dispose();
|
|
31
|
+
t.addEventListener("beforexrselect", i), r.addEventListener("end", a), n.signal?.addEventListener("abort", a, { once: !0 }), this.stop = () => {
|
|
32
|
+
t.removeEventListener("beforexrselect", i), r.removeEventListener("end", a), n.signal?.removeEventListener("abort", a);
|
|
33
|
+
}, this.type = r.domOverlayState?.type ?? null;
|
|
34
|
+
}
|
|
35
|
+
dispose() {
|
|
36
|
+
this.disposed || (this.disposed = !0, this.stop());
|
|
37
|
+
}
|
|
38
|
+
[Symbol.dispose ?? Symbol()]() {
|
|
39
|
+
this.dispose();
|
|
40
|
+
}
|
|
41
|
+
};
|
|
42
|
+
function s(e, t = {}, n = {}) {
|
|
43
|
+
if (!Array.isArray(e) || !e.length) throw TypeError("Provide image targets before entering AR; pass [{ image, widthInMeters: 0.2 }]");
|
|
44
|
+
let r = e.map((e) => {
|
|
45
|
+
if (!e?.image || !Number.isFinite(e.widthInMeters) || e.widthInMeters <= 0) throw RangeError("Each image target needs a bitmap and positive finite widthInMeters; pass the printed image width in metres");
|
|
46
|
+
return {
|
|
47
|
+
image: e.image,
|
|
48
|
+
widthInMeters: e.widthInMeters
|
|
49
|
+
};
|
|
50
|
+
}), i = t.legacy ? "image-tracking" : "marker-tracking";
|
|
51
|
+
return {
|
|
52
|
+
...n,
|
|
53
|
+
requiredFeatures: [.../* @__PURE__ */ new Set([...n.requiredFeatures ?? [], i])],
|
|
54
|
+
init: {
|
|
55
|
+
...n.init,
|
|
56
|
+
trackedImages: r
|
|
57
|
+
}
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
var c = (t, n) => {
|
|
61
|
+
let r = t.session;
|
|
62
|
+
if (!r) throw e("state", "AR tracking needs an active session; await xr.requestSession first");
|
|
63
|
+
if (r.enabledFeatures && !r.enabledFeatures.includes(n)) throw e("unsupported", `The session has no '${n}' feature; request it before entering AR`);
|
|
64
|
+
return r;
|
|
65
|
+
}, l = class {
|
|
66
|
+
constructor(e, t, n) {
|
|
67
|
+
this.session = e, this.close = n, this.abort = new AbortController(), this.stops = [], t?.throwIfAborted();
|
|
68
|
+
let r = () => n();
|
|
69
|
+
e.addEventListener("end", r), t?.addEventListener("abort", r, { once: !0 }), this.stops.push(() => e.removeEventListener("end", r), () => t?.removeEventListener("abort", r));
|
|
70
|
+
}
|
|
71
|
+
stop() {
|
|
72
|
+
this.abort.abort(e("disposed", "AR tracking is disposed; create a tracker for an active session"));
|
|
73
|
+
for (let e of this.stops) e();
|
|
74
|
+
this.stops.length = 0;
|
|
75
|
+
}
|
|
76
|
+
async wait(e, t) {
|
|
77
|
+
let n = t ? AbortSignal.any([this.abort.signal, t]) : this.abort.signal;
|
|
78
|
+
n.throwIfAborted();
|
|
79
|
+
let r = () => {}, i = new Promise((e, t) => {
|
|
80
|
+
let i = () => t(n.reason);
|
|
81
|
+
n.addEventListener("abort", i, { once: !0 }), r = () => n.removeEventListener("abort", i);
|
|
82
|
+
});
|
|
83
|
+
try {
|
|
84
|
+
return await Promise.race([e, i]);
|
|
85
|
+
} finally {
|
|
86
|
+
r();
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
}, u = class t {
|
|
90
|
+
static attach(e, n = {}) {__margoAssertCore(e,__margoCoreRequirement("1.0.0",import.meta.url));
|
|
91
|
+
return new t(e, n);
|
|
92
|
+
}
|
|
93
|
+
constructor(t, n) {
|
|
94
|
+
this.host = t, this.options = n, this.images = /* @__PURE__ */ new Map(), this.disposed = !1;
|
|
95
|
+
let r = t.session, i = r?.enabledFeatures?.includes("marker-tracking") ? "marker-tracking" : "image-tracking";
|
|
96
|
+
if (f(r) === "markers") throw e("unsupported", "The session runs the marker tracking module, which finds markers itself; use MarkerTracking");
|
|
97
|
+
this.lifetime = new l(c(t, i), n.signal, () => this.dispose()), this.lifetime.stops.push(t.onFrame((e) => this.update(e)));
|
|
98
|
+
}
|
|
99
|
+
async trackability(t) {
|
|
100
|
+
let n = this.lifetime.session, r = n.getImageTrackability ?? n.getTrackedImageScores;
|
|
101
|
+
if (!r) throw e("unsupported", "The session cannot report image trackability; use a runtime with the image tracking module");
|
|
102
|
+
return this.lifetime.abort.signal.throwIfAborted(), t?.throwIfAborted(), Object.freeze([...await this.lifetime.wait(r.call(n), t)]);
|
|
103
|
+
}
|
|
104
|
+
update(t) {
|
|
105
|
+
if (this.disposed || t.frame.session !== this.lifetime.session) return;
|
|
106
|
+
let n = t.frame.getImageTrackingResults;
|
|
107
|
+
if (!n) throw e("unsupported", "getMarkerTrackingResults" in t.frame ? "The session runs the marker tracking module, which finds markers itself; use MarkerTracking" : "The frame has no image tracking results; request marker-tracking or legacy image-tracking");
|
|
108
|
+
let r = /* @__PURE__ */ new Map(), i = this.host.referenceSpace;
|
|
109
|
+
for (let e of n.call(t.frame)) {
|
|
110
|
+
let n = i ? t.frame.getPose(e.imageSpace, i) : null;
|
|
111
|
+
r.set(e.index, Object.freeze({
|
|
112
|
+
index: e.index,
|
|
113
|
+
trackingState: e.trackingState,
|
|
114
|
+
measuredWidthInMeters: e.measuredWidthInMeters,
|
|
115
|
+
matrix: n ? new Float32Array(n.transform.matrix) : null
|
|
116
|
+
}));
|
|
117
|
+
}
|
|
118
|
+
this.images.clear();
|
|
119
|
+
for (let [e, t] of r) this.images.set(e, t);
|
|
120
|
+
this.options.onUpdate?.(Object.freeze([...this.images.values()]));
|
|
121
|
+
}
|
|
122
|
+
dispose() {
|
|
123
|
+
this.disposed || (this.disposed = !0, this.lifetime.stop(), this.images.clear());
|
|
124
|
+
}
|
|
125
|
+
[Symbol.dispose ?? Symbol()]() {
|
|
126
|
+
this.dispose();
|
|
127
|
+
}
|
|
128
|
+
};
|
|
129
|
+
function d(e = {}) {
|
|
130
|
+
return {
|
|
131
|
+
...e,
|
|
132
|
+
requiredFeatures: [.../* @__PURE__ */ new Set([...e.requiredFeatures ?? [], "marker-tracking"])]
|
|
133
|
+
};
|
|
134
|
+
}
|
|
135
|
+
function f(e) {
|
|
136
|
+
if (!e) return null;
|
|
137
|
+
let t = e;
|
|
138
|
+
return "markerTrackingActive" in t || typeof t.pauseMarkerTracking == "function" ? "markers" : typeof t.getImageTrackability == "function" || typeof t.getTrackedImageScores == "function" || t.enabledFeatures?.includes("image-tracking") ? "images" : null;
|
|
139
|
+
}
|
|
140
|
+
var p = class t {
|
|
141
|
+
static attach(e, n = {}) {__margoAssertCore(e,__margoCoreRequirement("1.0.0",import.meta.url));
|
|
142
|
+
return new t(e, n);
|
|
143
|
+
}
|
|
144
|
+
constructor(t, n) {
|
|
145
|
+
this.host = t, this.options = n, this.markers = Object.freeze([]), this.disposed = !1, this.ids = /* @__PURE__ */ new WeakMap(), this.nextId = 1, this.wanted = null;
|
|
146
|
+
let r = c(t, "marker-tracking");
|
|
147
|
+
if (f(r) === "images") throw e("unsupported", "The session runs the image tracking module; use ImageTracking with imageTrackingSessionOptions");
|
|
148
|
+
this.lifetime = new l(r, n.signal, () => this.dispose()), this.lifetime.stops.push(t.onFrame((e) => this.update(e)));
|
|
149
|
+
}
|
|
150
|
+
get active() {
|
|
151
|
+
return !this.disposed && (this.wanted ?? this.lifetime.session.markerTrackingActive === !0);
|
|
152
|
+
}
|
|
153
|
+
pause() {
|
|
154
|
+
this.request(!1);
|
|
155
|
+
}
|
|
156
|
+
resume() {
|
|
157
|
+
this.request(!0);
|
|
158
|
+
}
|
|
159
|
+
request(t) {
|
|
160
|
+
if (this.disposed) throw e("disposed", "Marker tracking is disposed; attach a tracker to an active session");
|
|
161
|
+
let n = t ? "resumeMarkerTracking" : "pauseMarkerTracking";
|
|
162
|
+
if (typeof this.lifetime.session[n] != "function") throw e("unsupported", `The session has no ${n}; use a runtime with the marker tracking module`);
|
|
163
|
+
this.wanted = t;
|
|
164
|
+
}
|
|
165
|
+
update(t) {
|
|
166
|
+
if (this.disposed || t.frame.session !== this.lifetime.session) return;
|
|
167
|
+
if (this.wanted !== null) {
|
|
168
|
+
let e = this.lifetime.session, t = this.wanted;
|
|
169
|
+
this.wanted = null, e[t ? "resumeMarkerTracking" : "pauseMarkerTracking"]();
|
|
170
|
+
}
|
|
171
|
+
let n = t.frame.getMarkerTrackingResults;
|
|
172
|
+
if (!n) throw e("unsupported", "The frame has no marker tracking results; request marker-tracking from a runtime with the marker module");
|
|
173
|
+
let r = this.host.referenceSpace, i = [];
|
|
174
|
+
for (let e of n.call(t.frame)) {
|
|
175
|
+
let n = e.marker, a = r ? t.frame.getPose(n.markerSpace, r) : null, o = n.extent, s = this.ids.get(n);
|
|
176
|
+
s === void 0 && this.ids.set(n, s = this.nextId++), i.push(Object.freeze({
|
|
177
|
+
id: s,
|
|
178
|
+
trackingState: e.trackingState,
|
|
179
|
+
payload: n.payloadString ?? null,
|
|
180
|
+
extent: o ? Object.freeze({
|
|
181
|
+
widthInMeters: o.widthInMeters,
|
|
182
|
+
heightInMeters: o.heightInMeters
|
|
183
|
+
}) : null,
|
|
184
|
+
matrix: a ? new Float32Array(a.transform.matrix) : null
|
|
185
|
+
}));
|
|
186
|
+
}
|
|
187
|
+
this.markers = Object.freeze(i), this.options.onUpdate?.(this.markers);
|
|
188
|
+
}
|
|
189
|
+
dispose() {
|
|
190
|
+
this.disposed || (this.disposed = !0, this.lifetime.stop(), this.markers = Object.freeze([]));
|
|
191
|
+
}
|
|
192
|
+
[Symbol.dispose ?? Symbol()]() {
|
|
193
|
+
this.dispose();
|
|
194
|
+
}
|
|
195
|
+
}, m = class a {
|
|
196
|
+
static async create(t, n = {}) {
|
|
197
|
+
let r = c(t, "mesh-detection");
|
|
198
|
+
n.signal?.throwIfAborted();
|
|
199
|
+
let i = n.show ? await import("@margoengine/geometry") : void 0;
|
|
200
|
+
if (n.signal?.throwIfAborted(), t.session !== r) throw e("state", "The XR session changed while preparing mesh display; create a tracker for the active session");
|
|
201
|
+
return new a(t, n, i);
|
|
202
|
+
}
|
|
203
|
+
constructor(e, n, r) {
|
|
204
|
+
this.host = e, this.options = n, this.geometry = r, this.meshes = /* @__PURE__ */ new Map(), this.visible = /* @__PURE__ */ new Map(), this.disposed = !1, this.lifetime = new l(c(e, "mesh-detection"), n.signal, () => this.dispose()), this.drawScene = n.show?.scene ?? n.show?.engine.scene, this.drawScene && this.lifetime.stops.push(t(this.drawScene, () => this.dispose())), this.lifetime.stops.push(e.onFrame((e) => this.update(e)));
|
|
205
|
+
}
|
|
206
|
+
update(t) {
|
|
207
|
+
if (this.disposed || t.frame.session !== this.lifetime.session) return;
|
|
208
|
+
let n = t.frame.detectedMeshes;
|
|
209
|
+
if (!n) throw e("unsupported", "The frame has no detected meshes; request the mesh-detection feature");
|
|
210
|
+
let r = [];
|
|
211
|
+
for (let [e, t] of this.meshes) n.has(e) || (this.meshes.delete(e), this.visible.get(e)?.dispose(), this.visible.delete(e), r.push(t));
|
|
212
|
+
for (let e of r) if (this.options.onRemove?.(e), this.disposed) return;
|
|
213
|
+
for (let e of n) {
|
|
214
|
+
let n = this.meshes.get(e), r = !n || n.lastChangedTime !== e.lastChangedTime || n.label !== (e.semanticLabel ?? ""), i = this.host.referenceSpace ? t.frame.getPose(e.meshSpace, this.host.referenceSpace) : null, a = Object.freeze({
|
|
215
|
+
source: e,
|
|
216
|
+
vertices: r ? e.vertices.slice() : n.vertices,
|
|
217
|
+
indices: r ? e.indices.slice() : n.indices,
|
|
218
|
+
matrix: i ? new Float32Array(i.transform.matrix) : null,
|
|
219
|
+
label: e.semanticLabel ?? "",
|
|
220
|
+
lastChangedTime: e.lastChangedTime
|
|
221
|
+
});
|
|
222
|
+
if (this.meshes.set(e, a), this.options.show && this.draw(a, r), n ? r && this.options.onChange?.(a) : this.options.onAdd?.(a), this.disposed) return;
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
draw(e, t) {
|
|
226
|
+
let a = this.options.show, o = this.drawScene, s = this.visible.get(e.source);
|
|
227
|
+
if (s || (s = o.createMesh().setMaterial(a.material ?? new r({ color: "#8899aa" })), this.visible.set(e.source, s)), s.visible = e.matrix !== null, t) {
|
|
228
|
+
let t = this.geometry.geometryData(e.vertices, new Float32Array(e.vertices.length), new Float32Array(e.vertices.length / 3 * 2), e.indices), r = this.geometry.computeVertexNormals(t).normals;
|
|
229
|
+
s.setGeometry(new n(e.vertices, r, e.indices));
|
|
230
|
+
}
|
|
231
|
+
if (e.matrix) {
|
|
232
|
+
let t = i(e.matrix);
|
|
233
|
+
if (!t.ok) throw RangeError("Detected mesh pose is not rigid; use a WebXR runtime that returns rigid mesh poses");
|
|
234
|
+
s.setPosition(t.position).setQuaternion(t.rotation).setScale(t.scale);
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
dispose() {
|
|
238
|
+
if (this.disposed) return;
|
|
239
|
+
this.disposed = !0, this.lifetime.stop();
|
|
240
|
+
for (let e of this.visible.values()) e.dispose();
|
|
241
|
+
this.visible.clear();
|
|
242
|
+
let e = [...this.meshes.values()];
|
|
243
|
+
this.meshes.clear();
|
|
244
|
+
for (let t of e) this.options.onRemove?.(t);
|
|
245
|
+
}
|
|
246
|
+
[Symbol.dispose ?? Symbol()]() {
|
|
247
|
+
this.dispose();
|
|
248
|
+
}
|
|
249
|
+
}, h = class t {
|
|
250
|
+
static attach(e, n = {}) {__margoAssertCore(e,__margoCoreRequirement("1.0.0",import.meta.url));
|
|
251
|
+
return new t(e, n.signal);
|
|
252
|
+
}
|
|
253
|
+
constructor(t, n) {
|
|
254
|
+
this.host = t, this.disposed = !1, this.pendingSave = /* @__PURE__ */ new WeakMap(), this.pendingRestore = /* @__PURE__ */ new Map();
|
|
255
|
+
let r = c(t, "anchors");
|
|
256
|
+
if (!Array.isArray(r.persistentAnchors) || typeof r.restorePersistentAnchor != "function" || typeof r.deletePersistentAnchor != "function") throw e("unsupported", "Persistent anchors are unavailable; use a browser with persistentAnchors and restorePersistentAnchor");
|
|
257
|
+
this.session = r, this.lifetime = new l(r, n, () => this.dispose());
|
|
258
|
+
}
|
|
259
|
+
get handles() {
|
|
260
|
+
return this.active(), Object.freeze([...this.session.persistentAnchors]);
|
|
261
|
+
}
|
|
262
|
+
active() {
|
|
263
|
+
if (this.disposed) throw e("disposed", "PersistentAnchors is disposed; attach to an active anchors session");
|
|
264
|
+
if (this.host.session !== this.session) throw e("state", "The XR session changed; attach to the active session");
|
|
265
|
+
}
|
|
266
|
+
async save(t, n) {
|
|
267
|
+
if (this.active(), n?.throwIfAborted(), typeof t?.requestPersistentHandle != "function") throw e("unsupported", "This anchor cannot persist; use an anchor with requestPersistentHandle");
|
|
268
|
+
let r = this.pendingSave.get(t);
|
|
269
|
+
if (!r) {
|
|
270
|
+
let e = new Set(this.session.persistentAnchors);
|
|
271
|
+
r = {
|
|
272
|
+
work: t.requestPersistentHandle(),
|
|
273
|
+
before: e,
|
|
274
|
+
waiters: 0,
|
|
275
|
+
claimed: !1
|
|
276
|
+
}, this.pendingSave.set(t, r);
|
|
277
|
+
}
|
|
278
|
+
r.waiters++;
|
|
279
|
+
let i;
|
|
280
|
+
try {
|
|
281
|
+
if (i = await r.work, n?.aborted) throw n.reason;
|
|
282
|
+
return this.active(), r.claimed = !0, i;
|
|
283
|
+
} finally {
|
|
284
|
+
r.waiters--, r.waiters === 0 && (this.pendingSave.delete(t), !r.claimed && i && !r.before.has(i) && await this.session.deletePersistentAnchor(i));
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
async restore(e, t) {
|
|
288
|
+
if (this.active(), t?.throwIfAborted(), typeof e != "string" || !e) throw TypeError("Pass a nonempty browser persistent anchor handle; read handles or save an anchor first");
|
|
289
|
+
let n = this.pendingRestore.get(e);
|
|
290
|
+
n || (n = {
|
|
291
|
+
work: this.session.restorePersistentAnchor(e),
|
|
292
|
+
waiters: 0,
|
|
293
|
+
claimed: !1
|
|
294
|
+
}, this.pendingRestore.set(e, n)), n.waiters++;
|
|
295
|
+
let r;
|
|
296
|
+
try {
|
|
297
|
+
if (r = await n.work, t?.aborted) throw t.reason;
|
|
298
|
+
return this.active(), n.claimed = !0, r;
|
|
299
|
+
} finally {
|
|
300
|
+
n.waiters--, n.waiters === 0 && (this.pendingRestore.delete(e), n.claimed || r?.delete());
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
async delete(e, t) {
|
|
304
|
+
if (this.active(), t?.throwIfAborted(), typeof e != "string" || !e) throw TypeError("Pass a nonempty persistent handle; read handles first");
|
|
305
|
+
await this.session.deletePersistentAnchor(e);
|
|
306
|
+
}
|
|
307
|
+
dispose() {
|
|
308
|
+
this.disposed || (this.disposed = !0, this.lifetime.stop());
|
|
309
|
+
}
|
|
310
|
+
[Symbol.dispose ?? Symbol()]() {
|
|
311
|
+
this.dispose();
|
|
312
|
+
}
|
|
313
|
+
};
|
|
314
|
+
//#endregion
|
|
315
|
+
export { o as DOMOverlay, u as ImageTracking, p as MarkerTracking, m as MeshDetection, h as PersistentAnchors, a as domOverlaySessionOptions, s as imageTrackingSessionOptions, d as markerTrackingSessionOptions, f as trackingModule };
|