@margoengine/xr 0.0.0-stage → 0.1.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 +592 -3
- package/dist/ar-extra.d.ts +167 -0
- package/dist/ar-extra.js +247 -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 +24 -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 +957 -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 +153 -6
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
import type { Mesh } from '@margoengine/core';
|
|
2
|
+
import { Quaternion } from '@margoengine/math/quaternion';
|
|
3
|
+
import { Vector3 } from '@margoengine/math/vector3';
|
|
4
|
+
import type { XRController } from './controller.js';
|
|
5
|
+
import type { XRFrameState } from './types.js';
|
|
6
|
+
/** What the toolkit needs of an `XR`: its controllers and a per-frame hook. `XR` itself fits. */
|
|
7
|
+
export interface InteractionHost {
|
|
8
|
+
readonly controllers: readonly XRController[];
|
|
9
|
+
onFrame(callback: (state: XRFrameState) => void): () => void;
|
|
10
|
+
}
|
|
11
|
+
/** Something a controller or hand can pick up. Its pose is in the XR reference space (world units). */
|
|
12
|
+
export interface Grabbable {
|
|
13
|
+
readonly position: Vector3;
|
|
14
|
+
readonly orientation: Quaternion;
|
|
15
|
+
/** Reach in metres: a grip this close grabs it, and a ray grab aims at a sphere this big. */
|
|
16
|
+
radius: number;
|
|
17
|
+
/** False ignores it. Default true. */
|
|
18
|
+
enabled?: boolean;
|
|
19
|
+
/** Called after the toolkit moves it; `grabbable(mesh)` writes the mesh here. */
|
|
20
|
+
apply?(): void;
|
|
21
|
+
onGrab?(event: GrabEvent): void;
|
|
22
|
+
onRelease?(event: ReleaseEvent): void;
|
|
23
|
+
}
|
|
24
|
+
/** A grab: which controller holds the object and whether it was reached by the ray. */
|
|
25
|
+
export interface GrabEvent {
|
|
26
|
+
readonly controller: XRController;
|
|
27
|
+
readonly ray: boolean;
|
|
28
|
+
}
|
|
29
|
+
/** A release: the object's speed at release in metres per second, and the socket that took it, if any. */
|
|
30
|
+
export interface ReleaseEvent {
|
|
31
|
+
readonly controller: XRController;
|
|
32
|
+
readonly velocity: readonly [number, number, number];
|
|
33
|
+
readonly socket: Socket | null;
|
|
34
|
+
}
|
|
35
|
+
/** A place an object snaps to when released close by: a holster, a slot, a key in a lock. */
|
|
36
|
+
export interface Socket {
|
|
37
|
+
readonly position: Vector3;
|
|
38
|
+
readonly orientation: Quaternion;
|
|
39
|
+
/** Snap distance in metres. */
|
|
40
|
+
radius: number;
|
|
41
|
+
/** Which objects fit. Default every one. */
|
|
42
|
+
accepts?(object: Grabbable): boolean;
|
|
43
|
+
/** The object in the socket, or null. */
|
|
44
|
+
held: Grabbable | null;
|
|
45
|
+
/** True while a held object that fits is within reach. */
|
|
46
|
+
hovered?: boolean;
|
|
47
|
+
onSnap?(object: Grabbable): void;
|
|
48
|
+
onLeave?(object: Grabbable): void;
|
|
49
|
+
}
|
|
50
|
+
/** A flat rectangle a ray can point at and press: a menu, a keypad, a layer panel. Faces +Z. */
|
|
51
|
+
export interface RayPanel {
|
|
52
|
+
readonly position: Vector3;
|
|
53
|
+
readonly orientation: Quaternion;
|
|
54
|
+
/** Width and height in metres. */
|
|
55
|
+
size: readonly [number, number];
|
|
56
|
+
enabled?: boolean;
|
|
57
|
+
onPointer(event: PanelPointerEvent): void;
|
|
58
|
+
}
|
|
59
|
+
/** A ray's event on a panel: u from the left edge, v from the top edge, each 0 to 1. */
|
|
60
|
+
export interface PanelPointerEvent {
|
|
61
|
+
readonly type: 'enter' | 'move' | 'down' | 'up' | 'leave';
|
|
62
|
+
readonly u: number;
|
|
63
|
+
readonly v: number;
|
|
64
|
+
readonly distance: number;
|
|
65
|
+
readonly controller: XRController;
|
|
66
|
+
}
|
|
67
|
+
/** Options for {@link Interaction.attach}. */
|
|
68
|
+
export interface InteractionOptions {
|
|
69
|
+
grabbables?: Grabbable[];
|
|
70
|
+
sockets?: Socket[];
|
|
71
|
+
panels?: RayPanel[];
|
|
72
|
+
/** Longest ray in metres for ray grabs and panels. Default 5. */
|
|
73
|
+
rayLength?: number;
|
|
74
|
+
/** Grab objects at a distance along the ray. Default true. */
|
|
75
|
+
rayGrab?: boolean;
|
|
76
|
+
/** Thumb-tip to index-tip distance in metres that counts as a pinch. Default 0.02. */
|
|
77
|
+
pinch?: number;
|
|
78
|
+
}
|
|
79
|
+
/** Where one controller's ray points this frame, for drawing a pointer. */
|
|
80
|
+
export interface RayState {
|
|
81
|
+
readonly controller: XRController;
|
|
82
|
+
/** The panel under the ray, or null. */
|
|
83
|
+
panel: RayPanel | null;
|
|
84
|
+
/** Distance to the panel, or the ray length. */
|
|
85
|
+
distance: number;
|
|
86
|
+
/** The object held, or null. */
|
|
87
|
+
holding: Grabbable | null;
|
|
88
|
+
}
|
|
89
|
+
/** A grabbable that moves `mesh` (a root mesh, so its position is in world units). */
|
|
90
|
+
export declare function grabbable(mesh: Mesh, options?: {
|
|
91
|
+
radius?: number;
|
|
92
|
+
position?: readonly number[];
|
|
93
|
+
orientation?: readonly number[];
|
|
94
|
+
}): Grabbable & {
|
|
95
|
+
readonly mesh: Mesh;
|
|
96
|
+
};
|
|
97
|
+
/**
|
|
98
|
+
* Grab, ray UI and sockets for every controller and hand of an XR session: squeeze (or pinch) to pick up an object
|
|
99
|
+
* near the grip or along the ray, release over a socket to snap it in, and point the ray at a panel and press the
|
|
100
|
+
* trigger (or pinch) to click. Arrays passed in options stay live: push and remove objects at any time.
|
|
101
|
+
*/
|
|
102
|
+
export declare class Interaction {
|
|
103
|
+
private readonly xr;
|
|
104
|
+
readonly grabbables: Grabbable[];
|
|
105
|
+
readonly sockets: Socket[];
|
|
106
|
+
readonly panels: RayPanel[];
|
|
107
|
+
/** One entry per controller, in `xr.controllers` order. */
|
|
108
|
+
readonly rays: RayState[];
|
|
109
|
+
private readonly hands;
|
|
110
|
+
private readonly stop;
|
|
111
|
+
private readonly rayLength;
|
|
112
|
+
private readonly rayGrab;
|
|
113
|
+
private readonly pinch;
|
|
114
|
+
private readonly gp;
|
|
115
|
+
private readonly gq;
|
|
116
|
+
private readonly ro;
|
|
117
|
+
private readonly rd;
|
|
118
|
+
private readonly v;
|
|
119
|
+
private readonly q;
|
|
120
|
+
static attach(xr: InteractionHost, options?: InteractionOptions): Interaction;
|
|
121
|
+
private constructor();
|
|
122
|
+
/** Lets go of everything and stops following the controllers. */
|
|
123
|
+
dispose(): void;
|
|
124
|
+
[Symbol.dispose]: () => void;
|
|
125
|
+
private frame;
|
|
126
|
+
private update;
|
|
127
|
+
private pinched;
|
|
128
|
+
/** Sets ro and rd to the target ray's origin and -Z direction; false when untracked. */
|
|
129
|
+
private ray;
|
|
130
|
+
private pick;
|
|
131
|
+
private fits;
|
|
132
|
+
private release;
|
|
133
|
+
private point;
|
|
134
|
+
/** [distance, u, v] where the ray meets the panel's front within `limit`, or null. */
|
|
135
|
+
private panelHit;
|
|
136
|
+
}
|
|
@@ -0,0 +1,205 @@
|
|
|
1
|
+
// @margo/core-built 0.1.0
|
|
2
|
+
import {coreRequirement as __margoCoreRequirement,assertCore as __margoAssertCore} from '@margoengine/core/extension';
|
|
3
|
+
|
|
4
|
+
import { Quaternion as e } from "@margoengine/math/quaternion";
|
|
5
|
+
import { Vector3 as t } from "@margoengine/math/vector3";
|
|
6
|
+
//#region src/interaction.ts
|
|
7
|
+
function n(n, r = {}) {
|
|
8
|
+
let i = r.position ?? [
|
|
9
|
+
n.position.x,
|
|
10
|
+
n.position.y,
|
|
11
|
+
n.position.z
|
|
12
|
+
], a = r.orientation ?? [
|
|
13
|
+
0,
|
|
14
|
+
0,
|
|
15
|
+
0,
|
|
16
|
+
1
|
|
17
|
+
], o = {
|
|
18
|
+
mesh: n,
|
|
19
|
+
position: new t(i[0], i[1], i[2]),
|
|
20
|
+
orientation: new e(a[0], a[1], a[2], a[3]),
|
|
21
|
+
radius: r.radius ?? .1,
|
|
22
|
+
apply() {
|
|
23
|
+
n.setPosition([
|
|
24
|
+
o.position.x,
|
|
25
|
+
o.position.y,
|
|
26
|
+
o.position.z
|
|
27
|
+
]), n.setQuaternion([
|
|
28
|
+
o.orientation.x,
|
|
29
|
+
o.orientation.y,
|
|
30
|
+
o.orientation.z,
|
|
31
|
+
o.orientation.w
|
|
32
|
+
]);
|
|
33
|
+
}
|
|
34
|
+
};
|
|
35
|
+
return o.apply(), o;
|
|
36
|
+
}
|
|
37
|
+
var r = 0, i = 1, a = 4, o = 9, s = class n {
|
|
38
|
+
static attach(e, t = {}) {__margoAssertCore(e,__margoCoreRequirement("0.1.0",import.meta.url));
|
|
39
|
+
return new n(e, t);
|
|
40
|
+
}
|
|
41
|
+
constructor(n, r) {
|
|
42
|
+
if (this.xr = n, this.rays = [], this.hands = /* @__PURE__ */ new Map(), this.gp = new t(), this.gq = new e(), this.ro = new t(), this.rd = new t(), this.v = new t(), this.q = new e(), this.grabbables = r.grabbables ?? [], this.sockets = r.sockets ?? [], this.panels = r.panels ?? [], this.rayLength = r.rayLength ?? 5, this.rayGrab = r.rayGrab ?? !0, this.pinch = r.pinch ?? .02, !(this.rayLength > 0) || !(this.pinch > 0)) throw RangeError("Interaction rayLength and pinch must be positive metres; pass { rayLength: 5, pinch: 0.02 }");
|
|
43
|
+
this.stop = n.onFrame((e) => this.frame(e.delta));
|
|
44
|
+
}
|
|
45
|
+
dispose() {
|
|
46
|
+
this.stop();
|
|
47
|
+
for (let e of this.hands.values()) e.grabbed && this.release(e, [
|
|
48
|
+
0,
|
|
49
|
+
0,
|
|
50
|
+
0
|
|
51
|
+
]);
|
|
52
|
+
this.hands.clear(), this.rays.length = 0;
|
|
53
|
+
}
|
|
54
|
+
[Symbol.dispose ?? Symbol()]() {
|
|
55
|
+
this.dispose();
|
|
56
|
+
}
|
|
57
|
+
frame(n) {
|
|
58
|
+
let r = this.xr.controllers;
|
|
59
|
+
for (let [e, t] of this.hands) r.includes(e) || (t.grabbed && this.release(t, [
|
|
60
|
+
0,
|
|
61
|
+
0,
|
|
62
|
+
0
|
|
63
|
+
]), t.pressed &&= null, this.hands.delete(e));
|
|
64
|
+
this.rays.length = 0;
|
|
65
|
+
for (let i of r) {
|
|
66
|
+
let r = this.hands.get(i);
|
|
67
|
+
r || (r = {
|
|
68
|
+
ray: {
|
|
69
|
+
controller: i,
|
|
70
|
+
panel: null,
|
|
71
|
+
distance: this.rayLength,
|
|
72
|
+
holding: null
|
|
73
|
+
},
|
|
74
|
+
grabbed: null,
|
|
75
|
+
offsetPosition: new t(),
|
|
76
|
+
offsetOrientation: new e(),
|
|
77
|
+
grab: !1,
|
|
78
|
+
press: !1,
|
|
79
|
+
pressed: null,
|
|
80
|
+
history: []
|
|
81
|
+
}, this.hands.set(i, r)), this.rays.push(r.ray), this.update(i, r, n);
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
update(e, t, n) {
|
|
85
|
+
let a = e.grip ?? e.targetRay;
|
|
86
|
+
if (!a) return;
|
|
87
|
+
let o = this.pinched(e), s = e.source?.gamepad?.mapping === "xr-standard", c = s && e.buttonCount > i ? e.buttons[4] === 1 : o, u = s && e.buttonCount > r ? e.buttons[1] === 1 : o, { position: d, orientation: f } = a.transform;
|
|
88
|
+
if (this.gp.set(d.x, d.y, d.z), this.gq.set(f.x, f.y, f.z, f.w), this.ray(e), c && !t.grab && !t.grabbed && this.pick(e, t), t.grabbed) {
|
|
89
|
+
let e = t.grabbed;
|
|
90
|
+
e.position.copy(t.offsetPosition).applyQuaternion(this.gq).add(this.gp), e.orientation.copy(this.gq).multiply(t.offsetOrientation), e.apply?.(), t.history.push(e.position.x, e.position.y, e.position.z, n), t.history.length > 16 && t.history.splice(0, 4);
|
|
91
|
+
for (let t of this.sockets) t.hovered = this.fits(t, e);
|
|
92
|
+
c || this.release(t, l(t.history));
|
|
93
|
+
}
|
|
94
|
+
t.grab = c, t.ray.holding = t.grabbed, this.point(e, t, u && !t.grabbed), t.press = u;
|
|
95
|
+
}
|
|
96
|
+
pinched(e) {
|
|
97
|
+
let t = e.handMatrices;
|
|
98
|
+
return !e.handTracked[a] || !e.handTracked[o] ? !1 : Math.hypot(t[76] - t[156], t[77] - t[157], t[78] - t[158]) <= this.pinch;
|
|
99
|
+
}
|
|
100
|
+
ray(e) {
|
|
101
|
+
let t = e.targetRay;
|
|
102
|
+
if (!t) return !1;
|
|
103
|
+
let { position: n, orientation: r } = t.transform;
|
|
104
|
+
return this.ro.set(n.x, n.y, n.z), this.q.set(r.x, r.y, r.z, r.w), this.rd.set(0, 0, -1).applyQuaternion(this.q), !0;
|
|
105
|
+
}
|
|
106
|
+
pick(e, t) {
|
|
107
|
+
let n = null, r = Infinity, i = !1, a = /* @__PURE__ */ new Set();
|
|
108
|
+
for (let e of this.hands.values()) e.grabbed && a.add(e.grabbed);
|
|
109
|
+
for (let e of this.grabbables) {
|
|
110
|
+
if (e.enabled === !1 || a.has(e)) continue;
|
|
111
|
+
let t = e.position.distanceTo(this.gp);
|
|
112
|
+
t <= e.radius && t < r && (n = e, r = t);
|
|
113
|
+
}
|
|
114
|
+
if (!n && this.rayGrab && e.targetRay) for (let e of this.grabbables) {
|
|
115
|
+
if (e.enabled === !1 || a.has(e)) continue;
|
|
116
|
+
let t = c(this.ro, this.rd, e.position, e.radius);
|
|
117
|
+
t !== null && t <= this.rayLength && t < r && (n = e, r = t, i = !0);
|
|
118
|
+
}
|
|
119
|
+
if (n) {
|
|
120
|
+
this.q.copy(this.gq).invert(), t.offsetPosition.copy(n.position).sub(this.gp).applyQuaternion(this.q), t.offsetOrientation.copy(this.q).multiply(n.orientation), t.grabbed = n, t.history.length = 0;
|
|
121
|
+
for (let e of this.sockets) e.held === n && (e.held = null, e.onLeave?.(n));
|
|
122
|
+
n.onGrab?.({
|
|
123
|
+
controller: e,
|
|
124
|
+
ray: i
|
|
125
|
+
});
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
fits(e, t) {
|
|
129
|
+
return (e.held === null || e.held === t) && (e.accepts?.(t) ?? !0) && e.position.distanceTo(t.position) <= e.radius;
|
|
130
|
+
}
|
|
131
|
+
release(e, t) {
|
|
132
|
+
let n = e.grabbed;
|
|
133
|
+
e.grabbed = null, e.ray.holding = null;
|
|
134
|
+
let r = null, i = Infinity;
|
|
135
|
+
for (let e of this.sockets) {
|
|
136
|
+
e.hovered = !1;
|
|
137
|
+
let t = e.position.distanceTo(n.position);
|
|
138
|
+
this.fits(e, n) && t < i && (r = e, i = t);
|
|
139
|
+
}
|
|
140
|
+
r && (n.position.copy(r.position), n.orientation.copy(r.orientation), n.apply?.(), r.held = n, r.onSnap?.(n)), n.onRelease?.({
|
|
141
|
+
controller: e.ray.controller,
|
|
142
|
+
velocity: r ? [
|
|
143
|
+
0,
|
|
144
|
+
0,
|
|
145
|
+
0
|
|
146
|
+
] : t,
|
|
147
|
+
socket: r
|
|
148
|
+
});
|
|
149
|
+
}
|
|
150
|
+
point(e, t, n) {
|
|
151
|
+
let r = null, i = this.rayLength, a = 0, o = 0;
|
|
152
|
+
if (!t.grabbed && this.ray(e)) for (let e of this.panels) {
|
|
153
|
+
if (e.enabled === !1) continue;
|
|
154
|
+
let t = this.panelHit(e, i);
|
|
155
|
+
t && (r = e, [i, a, o] = t);
|
|
156
|
+
}
|
|
157
|
+
let s = t.ray, c = (t, n) => t.onPointer({
|
|
158
|
+
type: n,
|
|
159
|
+
u: a,
|
|
160
|
+
v: o,
|
|
161
|
+
distance: i,
|
|
162
|
+
controller: e
|
|
163
|
+
});
|
|
164
|
+
s.panel && s.panel !== r && c(s.panel, "leave"), r && s.panel !== r ? c(r, "enter") : r && c(r, "move"), s.panel = r, s.distance = i, n && !t.press && r && (t.pressed = r, c(r, "down")), !n && t.pressed && (c(t.pressed, "up"), t.pressed = null);
|
|
165
|
+
}
|
|
166
|
+
panelHit(e, t) {
|
|
167
|
+
let n = this.v.set(0, 0, 1).applyQuaternion(e.orientation), r = this.rd.dot(n);
|
|
168
|
+
if (r >= 0) return null;
|
|
169
|
+
let i = (n.x * (e.position.x - this.ro.x) + n.y * (e.position.y - this.ro.y) + n.z * (e.position.z - this.ro.z)) / r;
|
|
170
|
+
if (i <= 0 || i > t) return null;
|
|
171
|
+
let a = this.v.copy(this.rd).multiplyScalar(i).add(this.ro).sub(e.position).applyQuaternion(this.q.copy(e.orientation).invert()), o = a.x / e.size[0] + .5, s = .5 - a.y / e.size[1];
|
|
172
|
+
return o < 0 || o > 1 || s < 0 || s > 1 ? null : [
|
|
173
|
+
i,
|
|
174
|
+
o,
|
|
175
|
+
s
|
|
176
|
+
];
|
|
177
|
+
}
|
|
178
|
+
};
|
|
179
|
+
function c(e, t, n, r) {
|
|
180
|
+
let i = n.x - e.x, a = n.y - e.y, o = n.z - e.z, s = i * t.x + a * t.y + o * t.z, c = i * i + a * a + o * o - s * s;
|
|
181
|
+
return i * i + a * a + o * o <= r * r ? 0 : s < 0 || c > r * r ? null : s - Math.sqrt(r * r - c);
|
|
182
|
+
}
|
|
183
|
+
function l(e) {
|
|
184
|
+
let t = e.length / 4;
|
|
185
|
+
if (t < 2) return [
|
|
186
|
+
0,
|
|
187
|
+
0,
|
|
188
|
+
0
|
|
189
|
+
];
|
|
190
|
+
let n = 0;
|
|
191
|
+
for (let r = 1; r < t; r++) n += e[r * 4 + 3];
|
|
192
|
+
if (!(n > 0)) return [
|
|
193
|
+
0,
|
|
194
|
+
0,
|
|
195
|
+
0
|
|
196
|
+
];
|
|
197
|
+
let r = (t - 1) * 4;
|
|
198
|
+
return [
|
|
199
|
+
(e[r] - e[0]) / n,
|
|
200
|
+
(e[r + 1] - e[1]) / n,
|
|
201
|
+
(e[r + 2] - e[2]) / n
|
|
202
|
+
];
|
|
203
|
+
}
|
|
204
|
+
//#endregion
|
|
205
|
+
export { s as Interaction, n as grabbable };
|
package/dist/launch.d.ts
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
/** Where an AR view can be shown. 'webxr' means use an `XR` session instead of this entry. */
|
|
2
|
+
export type ARRoute = 'webxr' | 'quick-look' | 'scene-viewer' | 'none';
|
|
3
|
+
/** What {@link chooseARRoute} reads; each defaults to the browser's own value. */
|
|
4
|
+
export interface ARPlatform {
|
|
5
|
+
/** `navigator.userAgent`. */
|
|
6
|
+
userAgent?: string;
|
|
7
|
+
/** Whether `document.createElement('a').relList.supports('ar')`. */
|
|
8
|
+
quickLook?: boolean;
|
|
9
|
+
/** Whether the browser supports an 'immersive-ar' session. */
|
|
10
|
+
webxr?: boolean;
|
|
11
|
+
}
|
|
12
|
+
/** Options for {@link sceneViewerURL}. */
|
|
13
|
+
export interface SceneViewerOptions {
|
|
14
|
+
/** 'ar_preferred' starts in AR with a 3D switch, '3d_preferred' in 3D with an AR button, '3d_only' never offers AR. Default 'ar_preferred'. */
|
|
15
|
+
mode?: 'ar_preferred' | '3d_preferred' | '3d_only';
|
|
16
|
+
/** Name in the viewer, cut to 60 characters. Default none. */
|
|
17
|
+
title?: string;
|
|
18
|
+
/** Adds a button that opens this page. Default none. */
|
|
19
|
+
link?: string;
|
|
20
|
+
/** Looping audio synced to the first animation. Default none. */
|
|
21
|
+
sound?: string;
|
|
22
|
+
/** False stops pinch scaling in AR. Default true. */
|
|
23
|
+
resizable?: boolean;
|
|
24
|
+
/** True lets the model stand on a wall. Default false. */
|
|
25
|
+
verticalPlacement?: boolean;
|
|
26
|
+
/** Page Chrome opens when the Google app is missing. Default the current page. */
|
|
27
|
+
fallbackUrl?: string;
|
|
28
|
+
}
|
|
29
|
+
/** Options for {@link quickLookURL}. */
|
|
30
|
+
export interface QuickLookOptions {
|
|
31
|
+
/** False stops pinch scaling. Default true. */
|
|
32
|
+
allowsContentScaling?: boolean;
|
|
33
|
+
/** The page Quick Look's share button shares instead of the file. Default none. */
|
|
34
|
+
canonicalWebPageURL?: string;
|
|
35
|
+
}
|
|
36
|
+
/** Options for {@link launchAR}. */
|
|
37
|
+
export interface LaunchAROptions extends SceneViewerOptions, QuickLookOptions {
|
|
38
|
+
/** The model for iOS: a URL of a .usdz, or its bytes (made into a short-lived blob URL). */
|
|
39
|
+
usdz?: string | Blob | Uint8Array;
|
|
40
|
+
/** The model for Android: an https URL of a .glb or .gltf. Scene Viewer cannot read a blob. */
|
|
41
|
+
glb?: string;
|
|
42
|
+
/** The preview picture Quick Look needs inside its link. Default a 1 pixel transparent image. */
|
|
43
|
+
poster?: string;
|
|
44
|
+
/** Platform facts; default read from the browser. */
|
|
45
|
+
platform?: ARPlatform;
|
|
46
|
+
/** Where to send a device with neither viewer. Default: resolves with 'none' and does nothing. */
|
|
47
|
+
fallback?: (route: ARRoute) => void;
|
|
48
|
+
}
|
|
49
|
+
/** Picks the route: WebXR where it exists, else Quick Look on iOS, Scene Viewer on Android, else 'none'. */
|
|
50
|
+
export declare function chooseARRoute(platform?: ARPlatform): ARRoute;
|
|
51
|
+
/** The href of a Quick Look link: the USDZ URL with its options in the fragment. */
|
|
52
|
+
export declare function quickLookURL(usdz: string, options?: QuickLookOptions): string;
|
|
53
|
+
/**
|
|
54
|
+
* The Android intent URL that opens `glb` in Scene Viewer (the Google app), with a browser fallback.
|
|
55
|
+
* @throws MargoError code 'unsupported' when `glb`, `link`, `sound` or `fallbackUrl` is not an http or https URL
|
|
56
|
+
* (relative ones resolve against the page): `javascript:`, `data:`, `vbscript:`, `file:` and `blob:` are refused.
|
|
57
|
+
* @throws RangeError for a `mode` outside 'ar_preferred', '3d_preferred' and '3d_only'.
|
|
58
|
+
*/
|
|
59
|
+
export declare function sceneViewerURL(glb: string, options?: SceneViewerOptions): string;
|
|
60
|
+
/**
|
|
61
|
+
* Opens the model in the viewer the platform has. Call it from a tap: browsers block both viewers otherwise.
|
|
62
|
+
* Resolves with the route used; 'webxr' and 'none' open nothing (the caller starts its XR session, or `fallback` runs).
|
|
63
|
+
* @throws MargoError code 'state' when the route needs a model the options leave out.
|
|
64
|
+
*/
|
|
65
|
+
export declare function launchAR(options?: LaunchAROptions): ARRoute;
|
package/dist/launch.js
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { n as e } from "./errors-XvcwUnU4.js";
|
|
2
|
+
//#region src/launch.ts
|
|
3
|
+
var t = [
|
|
4
|
+
"ar_preferred",
|
|
5
|
+
"3d_preferred",
|
|
6
|
+
"3d_only"
|
|
7
|
+
];
|
|
8
|
+
function n(t, n, r = !1) {
|
|
9
|
+
let i;
|
|
10
|
+
try {
|
|
11
|
+
i = new URL(String(t), globalThis.document?.baseURI ?? globalThis.location?.href);
|
|
12
|
+
} catch {
|
|
13
|
+
throw e("unsupported", `${n} must be an https URL`);
|
|
14
|
+
}
|
|
15
|
+
if (i.protocol !== "https:" && i.protocol !== "http:" && !(r && i.protocol === "blob:")) throw e("unsupported", `${n} must be an http, https or (for the usdz) blob URL, not ${i.protocol}`);
|
|
16
|
+
return /^[a-z][a-z0-9+.-]*:/i.test(String(t)) ? String(t) : i.href;
|
|
17
|
+
}
|
|
18
|
+
var r = "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7";
|
|
19
|
+
function i(e = {}) {
|
|
20
|
+
if (e.webxr) return "webxr";
|
|
21
|
+
let t = e.userAgent ?? globalThis.navigator?.userAgent ?? "";
|
|
22
|
+
return e.quickLook ?? (typeof document < "u" && !!document.createElement("a").relList?.supports?.("ar")) ? "quick-look" : /Android/.test(t) ? "scene-viewer" : "none";
|
|
23
|
+
}
|
|
24
|
+
function a(e, t = {}) {
|
|
25
|
+
if (!e) throw RangeError("quickLookURL needs the URL of a .usdz");
|
|
26
|
+
e = n(e, "the usdz URL", !0);
|
|
27
|
+
let r = [];
|
|
28
|
+
return t.allowsContentScaling === !1 && r.push("allowsContentScaling=0"), t.canonicalWebPageURL && r.push("canonicalWebPageURL=" + encodeURIComponent(n(t.canonicalWebPageURL, "canonicalWebPageURL"))), r.length ? `${e.split("#")[0]}#${r.join("&")}` : e;
|
|
29
|
+
}
|
|
30
|
+
function o(e, r = {}) {
|
|
31
|
+
let { mode: i = "ar_preferred", title: a, link: o, sound: s, resizable: c, verticalPlacement: l, fallbackUrl: u } = r;
|
|
32
|
+
if (!t.includes(i)) throw RangeError(`mode must be one of ${t.join(", ")}`);
|
|
33
|
+
let d = ["file=" + encodeURIComponent(n(e, "glb")), "mode=" + encodeURIComponent(i)];
|
|
34
|
+
a && d.push("title=" + encodeURIComponent(a.slice(0, 60))), o && d.push("link=" + encodeURIComponent(n(o, "link"))), s && d.push("sound=" + encodeURIComponent(n(s, "sound"))), c === !1 && d.push("resizable=false"), l && d.push("enable_vertical_placement=true");
|
|
35
|
+
let f = n(u ?? globalThis.location?.href ?? e, "fallbackUrl");
|
|
36
|
+
return `intent://arvr.google.com/scene-viewer/1.0?${d.join("&")}#Intent;scheme=https;package=com.google.android.googlequicksearchbox;action=android.intent.action.VIEW;S.browser_fallback_url=${encodeURIComponent(f)};end;`;
|
|
37
|
+
}
|
|
38
|
+
function s(t = {}) {
|
|
39
|
+
let n = i(t.platform);
|
|
40
|
+
if (n === "quick-look") {
|
|
41
|
+
let { usdz: n } = t;
|
|
42
|
+
if (!n) throw e("state", "launchAR on iOS needs `usdz`: a URL or the bytes from exportUSDZ");
|
|
43
|
+
let i = typeof n == "string" ? null : new Blob([n], { type: "model/vnd.usdz+zip" }), o = i ? URL.createObjectURL(i) : n, s = a(o, t), c = document.createElement("a");
|
|
44
|
+
c.rel = "ar", c.href = s;
|
|
45
|
+
let l = document.createElement("img");
|
|
46
|
+
l.src = t.poster ?? r, c.append(l), c.style.display = "none", document.body.append(c), c.click(), c.remove(), i && setTimeout(() => URL.revokeObjectURL(o), 6e4);
|
|
47
|
+
} else if (n === "scene-viewer") {
|
|
48
|
+
if (!t.glb) throw e("state", "launchAR on Android needs `glb`: an https URL of a .glb");
|
|
49
|
+
globalThis.location.href = o(t.glb, t);
|
|
50
|
+
} else t.fallback?.(n);
|
|
51
|
+
return n;
|
|
52
|
+
}
|
|
53
|
+
//#endregion
|
|
54
|
+
export { i as chooseARRoute, s as launchAR, a as quickLookURL, o as sceneViewerURL };
|