@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
package/dist/gizmo.d.ts
ADDED
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
/// <reference lib="esnext.disposable" preserve="true" />
|
|
2
|
+
import type { XRController } from './controller.js';
|
|
3
|
+
import type { XRFrameState, XRInputSource, XRNativeSession } from './types.js';
|
|
4
|
+
/** What `XRGizmo` needs of an `XR`: its session, controllers and a per-frame hook. `XR` itself fits. */
|
|
5
|
+
export interface XRGizmoHost {
|
|
6
|
+
readonly session: XRNativeSession | null;
|
|
7
|
+
readonly controllers: readonly XRController[];
|
|
8
|
+
onFrame(callback: (state: XRFrameState) => void): () => void;
|
|
9
|
+
}
|
|
10
|
+
type V3 = [number, number, number];
|
|
11
|
+
/** The world ray a driver hands the gizmo in place of a pointer: `TransformAim` of `@margoengine/controls/transform`. */
|
|
12
|
+
export interface XRGizmoAim {
|
|
13
|
+
ray: {
|
|
14
|
+
origin: V3;
|
|
15
|
+
direction: V3;
|
|
16
|
+
};
|
|
17
|
+
/** The frame of the ray when it grabbed, a stand-in screen `screen` pixels square. */
|
|
18
|
+
b: {
|
|
19
|
+
position: V3;
|
|
20
|
+
forward: V3;
|
|
21
|
+
right: V3;
|
|
22
|
+
up: V3;
|
|
23
|
+
distance: number;
|
|
24
|
+
tanHalf: number;
|
|
25
|
+
};
|
|
26
|
+
screen: number;
|
|
27
|
+
/** Kept by the gizmo: the ray's nearest point along a dragged axis and what steadies it near parallel. */
|
|
28
|
+
along: number;
|
|
29
|
+
from: V3;
|
|
30
|
+
bias: number;
|
|
31
|
+
parallel: boolean;
|
|
32
|
+
/** The size factor of a two-point scale, or null for a ray drag. */
|
|
33
|
+
factor: number | null;
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* The gizmo a ray drives, by its pointer methods and its ray hook `aim`. `TransformControls` of
|
|
37
|
+
* `@margoengine/controls/transform` fits.
|
|
38
|
+
*/
|
|
39
|
+
export interface XRGizmoTarget {
|
|
40
|
+
/** True while a handle is dragged. */
|
|
41
|
+
readonly dragging: boolean;
|
|
42
|
+
readonly enabled: boolean;
|
|
43
|
+
/** The lit or dragged handle. */
|
|
44
|
+
readonly axis: string | null;
|
|
45
|
+
/** While set, the gizmo picks and drags by this ray; the end of a drag clears it. */
|
|
46
|
+
aim: XRGizmoAim | null;
|
|
47
|
+
/** Where the gizmo is and the unit vector from it to the eye, or null with nothing attached. */
|
|
48
|
+
frame(): {
|
|
49
|
+
origin: readonly number[];
|
|
50
|
+
eye: readonly number[];
|
|
51
|
+
} | null;
|
|
52
|
+
pick(x: number, y: number): string | null;
|
|
53
|
+
setAxis(axis: string | null): void;
|
|
54
|
+
pointerDown(x: number, y: number): void;
|
|
55
|
+
pointerMove(x: number, y: number): void;
|
|
56
|
+
begin(action: {
|
|
57
|
+
op: 'scale';
|
|
58
|
+
axis: 'XYZ';
|
|
59
|
+
}, x: number, y: number): void;
|
|
60
|
+
endDrag(): void;
|
|
61
|
+
}
|
|
62
|
+
/** A hover, grab or release of the gizmo by a controller. */
|
|
63
|
+
export interface XRGizmoEvent {
|
|
64
|
+
readonly controller: XRController;
|
|
65
|
+
/** The handle's name (`X`, `XY`, `RZ`...), `scale` for a two-hand scale, or null when a hover ends. */
|
|
66
|
+
readonly handle: string | null;
|
|
67
|
+
}
|
|
68
|
+
/** Options of `XRGizmo`, copied at attachment; every field is optional. */
|
|
69
|
+
export interface XRGizmoOptions {
|
|
70
|
+
/** The action that grabs: the primary `select` (trigger or pinch) or `squeeze` (grip). Default `select`. */
|
|
71
|
+
button?: 'select' | 'squeeze';
|
|
72
|
+
/** Which hands drive the gizmo. Default all three: `left`, `right` and `none`. */
|
|
73
|
+
hands?: readonly XRInputSource['handedness'][];
|
|
74
|
+
/** A second hand's press during a grab turns it into a two-hand scale. Default true. */
|
|
75
|
+
twoHandScale?: boolean;
|
|
76
|
+
/** Hands closer than this at the second press, in metres, do not start a two-hand scale. Default 0.02. */
|
|
77
|
+
minSeparation?: number;
|
|
78
|
+
/** Called when the handle under a ray changes. */
|
|
79
|
+
onHover?(event: XRGizmoEvent): void;
|
|
80
|
+
/** Called when a hand grabs a handle, or the second hand starts a two-hand scale. */
|
|
81
|
+
onGrab?(event: XRGizmoEvent): void;
|
|
82
|
+
/** Called when a grab or a two-hand scale ends. */
|
|
83
|
+
onRelease?(event: XRGizmoEvent): void;
|
|
84
|
+
/** Aborting disposes the driver. */
|
|
85
|
+
signal?: AbortSignal;
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* Drives a transform gizmo with world rays: an origin and a direction in world space (the direction need not be unit).
|
|
89
|
+
* `XRGizmo` feeds it controller rays; any other ray source (a tracked stylus, a second camera) can call it directly.
|
|
90
|
+
*/
|
|
91
|
+
export declare class GizmoRayDriver {
|
|
92
|
+
readonly gizmo: XRGizmoTarget;
|
|
93
|
+
constructor(gizmo: XRGizmoTarget);
|
|
94
|
+
/** True while a handle is dragged. */
|
|
95
|
+
get dragging(): boolean;
|
|
96
|
+
private take;
|
|
97
|
+
/**
|
|
98
|
+
* Lights the handle a world ray points at, as a hovering pointer does, and returns its name or null. During a drag it
|
|
99
|
+
* returns the dragged handle and changes nothing.
|
|
100
|
+
*/
|
|
101
|
+
rayHover(origin: readonly number[], direction: readonly number[]): string | null;
|
|
102
|
+
/**
|
|
103
|
+
* Grabs the handle a world ray points at and starts a drag, as a pointer press does. Returns false when the ray points
|
|
104
|
+
* at no handle or a drag is under way. Follow with `rayMove` and end with `rayUp`.
|
|
105
|
+
*/
|
|
106
|
+
rayDown(origin: readonly number[], direction: readonly number[]): boolean;
|
|
107
|
+
/** Continues a `rayDown` drag with the ray's new place. */
|
|
108
|
+
rayMove(origin: readonly number[], direction: readonly number[]): void;
|
|
109
|
+
/**
|
|
110
|
+
* Starts a two-point scale in any mode: `scaleTo(factor)` then sizes everything attached about the gizmo, with the
|
|
111
|
+
* scale snap, until `rayUp`. Returns false when nothing is attached or a drag is under way.
|
|
112
|
+
*/
|
|
113
|
+
scaleDown(): boolean;
|
|
114
|
+
/** Sets the size of a `scaleDown` drag to `factor` times the size it began with (1 leaves it as it was). */
|
|
115
|
+
scaleTo(factor: number): void;
|
|
116
|
+
/** Ends a `rayDown` or `scaleDown` drag, as a pointer release does; with no drag, the highlight of `rayHover` goes out. */
|
|
117
|
+
rayUp(): void;
|
|
118
|
+
}
|
|
119
|
+
/**
|
|
120
|
+
* Drives a transform gizmo from XR controller rays: a ray lights the handle it points at, the grab button takes it, the
|
|
121
|
+
* handle then follows the ray under the gizmo's own constraints and snapping, and a second hand's press scales by the
|
|
122
|
+
* ratio of the hands' separation. Poses are read in the XR reference space, taken as world space.
|
|
123
|
+
*/
|
|
124
|
+
export declare class XRGizmo {
|
|
125
|
+
private readonly xr;
|
|
126
|
+
private readonly options;
|
|
127
|
+
private readonly stop;
|
|
128
|
+
private session;
|
|
129
|
+
private holder;
|
|
130
|
+
private second;
|
|
131
|
+
private separation;
|
|
132
|
+
private hover;
|
|
133
|
+
private pointer;
|
|
134
|
+
private handle;
|
|
135
|
+
private disposed;
|
|
136
|
+
private readonly start;
|
|
137
|
+
private readonly end;
|
|
138
|
+
private readonly hands;
|
|
139
|
+
private readonly twoHandScale;
|
|
140
|
+
private readonly minSeparation;
|
|
141
|
+
private readonly gizmo;
|
|
142
|
+
/** Attaches the driver; `dispose()` or `using` detaches it. */
|
|
143
|
+
static attach(xr: XRGizmoHost, gizmo: XRGizmoTarget, options?: XRGizmoOptions): XRGizmo;
|
|
144
|
+
private constructor();
|
|
145
|
+
/** The controller holding a handle (the first hand of a two-hand scale), or null. */
|
|
146
|
+
get grabbing(): XRController | null;
|
|
147
|
+
/** True while two hands scale. */
|
|
148
|
+
get scaling(): boolean;
|
|
149
|
+
private bind;
|
|
150
|
+
private unbind;
|
|
151
|
+
/** The controller's target ray as a world origin and direction (its -Z axis), or null when untracked or not a driving hand. */
|
|
152
|
+
private ray;
|
|
153
|
+
private of;
|
|
154
|
+
private readonly press;
|
|
155
|
+
private readonly lift;
|
|
156
|
+
/** Ends whatever is held. A hand still pressing after a two-hand scale presses again to grab. */
|
|
157
|
+
private release;
|
|
158
|
+
private readonly frame;
|
|
159
|
+
private readonly ended;
|
|
160
|
+
private readonly abort;
|
|
161
|
+
/** Lets go of the gizmo and removes the session, frame and signal listeners. */
|
|
162
|
+
dispose(): void;
|
|
163
|
+
[Symbol.dispose](): void;
|
|
164
|
+
}
|
|
165
|
+
export {};
|
package/dist/gizmo.js
ADDED
|
@@ -0,0 +1,198 @@
|
|
|
1
|
+
// @margo/core-built 0.1.0
|
|
2
|
+
import {coreRequirement as __margoCoreRequirement,assertCore as __margoAssertCore} from '@margoengine/core/extension';
|
|
3
|
+
|
|
4
|
+
import { r as e } from "./errors-XvcwUnU4.js";
|
|
5
|
+
import { add as t, cross as n, dot as r, normalize as i, scale as a, sub as o } from "@margoengine/math/vec3";
|
|
6
|
+
//#region src/gizmo.ts
|
|
7
|
+
var s = [
|
|
8
|
+
"frame",
|
|
9
|
+
"pick",
|
|
10
|
+
"setAxis",
|
|
11
|
+
"pointerDown",
|
|
12
|
+
"pointerMove",
|
|
13
|
+
"begin",
|
|
14
|
+
"endDrag"
|
|
15
|
+
], c = 300, l = c / 2, u = .5, d = class {
|
|
16
|
+
constructor(t) {
|
|
17
|
+
if (this.gizmo = t, !t || s.some((e) => typeof t[e] != "function")) throw e("GizmoRayDriver needs a gizmo with frame, pick, setAxis, pointerDown, pointerMove, begin and endDrag; pass a TransformControls", "arguments");
|
|
18
|
+
}
|
|
19
|
+
get dragging() {
|
|
20
|
+
return this.gizmo.dragging;
|
|
21
|
+
}
|
|
22
|
+
take(t, r) {
|
|
23
|
+
let a = Math.hypot(r[0], r[1], r[2]);
|
|
24
|
+
if (!(a > 0 && Number.isFinite(a) && Number.isFinite(t[0] + t[1] + t[2]))) throw e("GizmoRayDriver ray must be a finite origin and a non-zero direction; pass [x, y, z] and [x, y, z]", "arguments");
|
|
25
|
+
let s = this.gizmo.enabled ? this.gizmo.frame() : null;
|
|
26
|
+
if (!s) return null;
|
|
27
|
+
let l = [
|
|
28
|
+
t[0],
|
|
29
|
+
t[1],
|
|
30
|
+
t[2]
|
|
31
|
+
], d = [
|
|
32
|
+
r[0] / a,
|
|
33
|
+
r[1] / a,
|
|
34
|
+
r[2] / a
|
|
35
|
+
], f = i(n(d, Math.abs(d[1]) < .99 ? [
|
|
36
|
+
0,
|
|
37
|
+
1,
|
|
38
|
+
0
|
|
39
|
+
] : [
|
|
40
|
+
1,
|
|
41
|
+
0,
|
|
42
|
+
0
|
|
43
|
+
]));
|
|
44
|
+
return {
|
|
45
|
+
ray: {
|
|
46
|
+
origin: l,
|
|
47
|
+
direction: d
|
|
48
|
+
},
|
|
49
|
+
b: {
|
|
50
|
+
position: l,
|
|
51
|
+
forward: d,
|
|
52
|
+
right: f,
|
|
53
|
+
up: n(f, d),
|
|
54
|
+
distance: 1,
|
|
55
|
+
tanHalf: u
|
|
56
|
+
},
|
|
57
|
+
screen: c,
|
|
58
|
+
reach: Math.max(.001, Math.hypot(...o(s.origin, l))),
|
|
59
|
+
along: 0,
|
|
60
|
+
from: l,
|
|
61
|
+
bias: 0,
|
|
62
|
+
parallel: !1,
|
|
63
|
+
factor: null
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
rayHover(e, t) {
|
|
67
|
+
let n = this.gizmo, r = this.take(e, t);
|
|
68
|
+
if (n.dragging) return n.axis;
|
|
69
|
+
n.aim = r;
|
|
70
|
+
let i = r && n.pick(l, l);
|
|
71
|
+
return n.aim = null, n.setAxis(i), i;
|
|
72
|
+
}
|
|
73
|
+
rayDown(e, t) {
|
|
74
|
+
let n = this.gizmo;
|
|
75
|
+
return n.dragging || !this.rayHover(e, t) ? !1 : (n.aim = this.take(e, t), n.pointerDown(l, l), n.dragging || (n.aim = null), n.dragging);
|
|
76
|
+
}
|
|
77
|
+
rayMove(e, n) {
|
|
78
|
+
let i = this.gizmo, s = i.aim, c = s && s.factor === null && i.dragging ? this.take(e, n) : null;
|
|
79
|
+
if (!s || !c) return;
|
|
80
|
+
s.ray = c.ray;
|
|
81
|
+
let u = s.b, d = o(t(c.ray.origin, a(c.ray.direction, s.reach)), u.position), f = r(d, u.forward), p = f * u.tanHalf;
|
|
82
|
+
f > 0 && i.pointerMove((r(d, u.right) / p + 1) * l, (1 - r(d, u.up) / p) * l);
|
|
83
|
+
}
|
|
84
|
+
scaleDown() {
|
|
85
|
+
let e = this.gizmo, n = e.dragging ? null : e.frame(), r = n && this.take(t(n.origin, n.eye), a(n.eye, -1));
|
|
86
|
+
return r ? (r.factor = 1, e.aim = r, e.begin({
|
|
87
|
+
op: "scale",
|
|
88
|
+
axis: "XYZ"
|
|
89
|
+
}, l, l), e.dragging || (e.aim = null), e.dragging) : !1;
|
|
90
|
+
}
|
|
91
|
+
scaleTo(t) {
|
|
92
|
+
if (!(Number.isFinite(t) && t > 0)) throw e("GizmoRayDriver scaleTo factor must be a finite number above 0; pass a ratio such as 1.5", "arguments");
|
|
93
|
+
let n = this.gizmo, r = n.aim;
|
|
94
|
+
r && r.factor !== null && n.dragging && (r.factor = t, n.pointerMove(l, l));
|
|
95
|
+
}
|
|
96
|
+
rayUp() {
|
|
97
|
+
let e = this.gizmo;
|
|
98
|
+
e.dragging ? e.aim && e.endDrag() : e.setAxis(null);
|
|
99
|
+
}
|
|
100
|
+
}, f = [
|
|
101
|
+
"left",
|
|
102
|
+
"right",
|
|
103
|
+
"none"
|
|
104
|
+
], p = class t {
|
|
105
|
+
static attach(e, n, r = {}) {__margoAssertCore(e,__margoCoreRequirement("0.1.0",import.meta.url));
|
|
106
|
+
return r.signal?.throwIfAborted(), new t(e, n, r);
|
|
107
|
+
}
|
|
108
|
+
constructor(t, n, r) {
|
|
109
|
+
this.xr = t, this.options = r, this.session = null, this.holder = null, this.second = null, this.separation = 0, this.hover = null, this.pointer = null, this.handle = null, this.disposed = !1, this.press = (e) => {
|
|
110
|
+
let t = this.of(e), n = this.ray(t);
|
|
111
|
+
if (!t || !n || this.second || t === this.holder) return;
|
|
112
|
+
if (!this.holder) {
|
|
113
|
+
if (this.gizmo.dragging || !this.gizmo.rayDown(n[0], n[1])) return;
|
|
114
|
+
this.holder = t, this.handle = this.gizmo.rayHover(n[0], n[1]), this.options.onGrab?.({
|
|
115
|
+
controller: t,
|
|
116
|
+
handle: this.handle
|
|
117
|
+
});
|
|
118
|
+
return;
|
|
119
|
+
}
|
|
120
|
+
let r = this.ray(this.holder), i = r ? Math.hypot(...n[0].map((e, t) => e - r[0][t])) : 0;
|
|
121
|
+
this.twoHandScale && i >= this.minSeparation && (this.gizmo.rayUp(), this.gizmo.scaleDown() ? (this.second = t, this.separation = i, this.options.onGrab?.({
|
|
122
|
+
controller: t,
|
|
123
|
+
handle: "scale"
|
|
124
|
+
})) : this.release());
|
|
125
|
+
}, this.lift = (e) => {
|
|
126
|
+
let t = this.of(e);
|
|
127
|
+
t && (t === this.holder || t === this.second) && this.release();
|
|
128
|
+
}, this.frame = () => {
|
|
129
|
+
this.bind();
|
|
130
|
+
let e = this.xr.controllers;
|
|
131
|
+
if (this.holder) {
|
|
132
|
+
let t = this.ray(this.holder), n = this.second && this.ray(this.second);
|
|
133
|
+
!e.includes(this.holder) || this.second && !e.includes(this.second) || !this.gizmo.dragging ? this.release() : this.second ? t && n && this.gizmo.scaleTo(Math.max(1e-6, Math.hypot(...n[0].map((e, n) => e - t[0][n])) / this.separation)) : t && this.gizmo.rayMove(t[0], t[1]);
|
|
134
|
+
return;
|
|
135
|
+
}
|
|
136
|
+
if (this.gizmo.dragging) return;
|
|
137
|
+
let t = null, n = null;
|
|
138
|
+
for (let r of e) {
|
|
139
|
+
let e = this.ray(r);
|
|
140
|
+
if (e && (n = r, t = this.gizmo.rayHover(e[0], e[1]), t)) break;
|
|
141
|
+
}
|
|
142
|
+
!n && this.hover && this.gizmo.rayUp(), n ??= this.pointer, t !== this.hover && n && (this.hover = t, this.pointer = n, this.options.onHover?.({
|
|
143
|
+
controller: n,
|
|
144
|
+
handle: t
|
|
145
|
+
}));
|
|
146
|
+
}, this.ended = () => this.unbind(), this.abort = () => this.dispose();
|
|
147
|
+
let i = r.button ?? "select";
|
|
148
|
+
if (this.hands = [...r.hands ?? f], this.twoHandScale = r.twoHandScale ?? !0, this.minSeparation = r.minSeparation ?? .02, typeof t?.onFrame != "function" || !Array.isArray(t.controllers)) throw e("XRGizmo needs an XR host with controllers and onFrame; pass the XR manager", "arguments");
|
|
149
|
+
if (this.gizmo = new d(n), i !== "select" && i !== "squeeze" || !this.hands.length || this.hands.some((e) => !f.includes(e)) || !(Number.isFinite(this.minSeparation) && this.minSeparation > 0)) throw e("XRGizmo needs button 'select' or 'squeeze', hands from 'left', 'right' and 'none', and minSeparation above 0; pass valid options", "arguments");
|
|
150
|
+
this.start = i + "start", this.end = i + "end", this.bind(), this.stop = t.onFrame(this.frame), r.signal?.addEventListener("abort", this.abort, { once: !0 });
|
|
151
|
+
}
|
|
152
|
+
get grabbing() {
|
|
153
|
+
return this.holder;
|
|
154
|
+
}
|
|
155
|
+
get scaling() {
|
|
156
|
+
return !!this.second;
|
|
157
|
+
}
|
|
158
|
+
bind() {
|
|
159
|
+
this.session !== this.xr.session && (this.unbind(), this.session = this.xr.session, this.session?.addEventListener(this.start, this.press), this.session?.addEventListener(this.end, this.lift), this.session?.addEventListener("end", this.ended));
|
|
160
|
+
}
|
|
161
|
+
unbind() {
|
|
162
|
+
this.release(), this.session?.removeEventListener(this.start, this.press), this.session?.removeEventListener(this.end, this.lift), this.session?.removeEventListener("end", this.ended), this.session = null;
|
|
163
|
+
}
|
|
164
|
+
ray(e) {
|
|
165
|
+
if (!e?.targetRay || !this.hands.includes(e.source.handedness)) return null;
|
|
166
|
+
let t = e.targetRayMatrix;
|
|
167
|
+
return [[
|
|
168
|
+
t[12],
|
|
169
|
+
t[13],
|
|
170
|
+
t[14]
|
|
171
|
+
], [
|
|
172
|
+
-t[8],
|
|
173
|
+
-t[9],
|
|
174
|
+
-t[10]
|
|
175
|
+
]];
|
|
176
|
+
}
|
|
177
|
+
of(e) {
|
|
178
|
+
let t = e.inputSource;
|
|
179
|
+
return this.xr.controllers.find((e) => e.source === t);
|
|
180
|
+
}
|
|
181
|
+
release() {
|
|
182
|
+
let e = this.second ?? this.holder;
|
|
183
|
+
if (!e) return;
|
|
184
|
+
let t = this.second ? "scale" : this.handle;
|
|
185
|
+
this.holder = this.second = null, this.gizmo.dragging && this.gizmo.rayUp(), this.options.onRelease?.({
|
|
186
|
+
controller: e,
|
|
187
|
+
handle: t
|
|
188
|
+
});
|
|
189
|
+
}
|
|
190
|
+
dispose() {
|
|
191
|
+
this.disposed || (this.disposed = !0, this.stop(), this.unbind(), this.hover && !this.gizmo.dragging && this.gizmo.rayUp(), this.hover = null, this.options.signal?.removeEventListener("abort", this.abort));
|
|
192
|
+
}
|
|
193
|
+
[Symbol.dispose]() {
|
|
194
|
+
this.dispose();
|
|
195
|
+
}
|
|
196
|
+
};
|
|
197
|
+
//#endregion
|
|
198
|
+
export { d as GizmoRayDriver, p as XRGizmo };
|
package/dist/hands.d.ts
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
/// <reference lib="esnext.disposable" preserve="true" />
|
|
2
|
+
// @ts-ignore optional peer: its types apply when it is installed
|
|
3
|
+
import type { PBRModel } from '@margoengine/gltf/pbr';
|
|
4
|
+
import type { XRController } from './controller.js';
|
|
5
|
+
import type { XRFrameState, XRNativeSession } from './types.js';
|
|
6
|
+
/** The 25 joint names in WebXR Hand Input order. */
|
|
7
|
+
export declare const HAND_JOINT_NAMES: readonly string[];
|
|
8
|
+
/** Session and inputs accepted by skinned hands. */
|
|
9
|
+
export interface SkinnedHandsHost {
|
|
10
|
+
readonly session: XRNativeSession | null;
|
|
11
|
+
readonly controllers: readonly XRController[];
|
|
12
|
+
onFrame(callback: (state: XRFrameState) => void): () => void;
|
|
13
|
+
}
|
|
14
|
+
/** Caller-owned glTF hand models. At least one hand must be provided. */
|
|
15
|
+
export interface HandModels {
|
|
16
|
+
/** A skinned glTF whose joint node names match WebXR or jointNames. */
|
|
17
|
+
left?: PBRModel;
|
|
18
|
+
/** A separate right-hand model. Mirroring is authored in the asset. */
|
|
19
|
+
right?: PBRModel;
|
|
20
|
+
}
|
|
21
|
+
/** Options for skinned hand tracking. */
|
|
22
|
+
export interface SkinnedHandsOptions {
|
|
23
|
+
/** WebXR joint name to glTF node name. Default the WebXR names, as in generic-hand profile assets. */
|
|
24
|
+
jointNames?: Readonly<Record<string, string>>;
|
|
25
|
+
/** Dispose when aborted; the caller retains the models. */
|
|
26
|
+
signal?: AbortSignal;
|
|
27
|
+
}
|
|
28
|
+
/** Drives actual GPU-skinned glTF meshes from the tracked 25-joint hands, retaining asset inverse binds and weights. */
|
|
29
|
+
export declare class SkinnedHands {
|
|
30
|
+
private readonly xr;
|
|
31
|
+
private readonly rigs;
|
|
32
|
+
private readonly stops;
|
|
33
|
+
private session;
|
|
34
|
+
private stopSession?;
|
|
35
|
+
private disposed;
|
|
36
|
+
private readonly local;
|
|
37
|
+
private readonly inverse;
|
|
38
|
+
private readonly scratch;
|
|
39
|
+
/** Loads the skinning entry lazily, then attaches borrowed models. No model URLs or network work are implicit. */
|
|
40
|
+
static create(xr: SkinnedHandsHost, models: HandModels, options?: SkinnedHandsOptions): Promise<SkinnedHands>;
|
|
41
|
+
private constructor();
|
|
42
|
+
private listen;
|
|
43
|
+
private update;
|
|
44
|
+
private pose;
|
|
45
|
+
/** Stops tracking and skinning, and restores the borrowed models' original local transforms and visibility. */
|
|
46
|
+
dispose(): void;
|
|
47
|
+
[Symbol.dispose]: () => void;
|
|
48
|
+
}
|
package/dist/hands.js
ADDED
|
@@ -0,0 +1,198 @@
|
|
|
1
|
+
import { n as e } from "./errors-XvcwUnU4.js";
|
|
2
|
+
import { t } from "./scene-owner-BRObO-WJ.js";
|
|
3
|
+
import { composeMatrix as n, invertMatrixStable as r, multiplyMatrices as i } from "@margoengine/math/mat4";
|
|
4
|
+
import { splitTRS as a } from "@margoengine/math/quat";
|
|
5
|
+
//#region src/hands.ts
|
|
6
|
+
var o = Object.freeze([
|
|
7
|
+
"wrist",
|
|
8
|
+
"thumb-metacarpal",
|
|
9
|
+
"thumb-phalanx-proximal",
|
|
10
|
+
"thumb-phalanx-distal",
|
|
11
|
+
"thumb-tip",
|
|
12
|
+
...[
|
|
13
|
+
"index",
|
|
14
|
+
"middle",
|
|
15
|
+
"ring",
|
|
16
|
+
"pinky"
|
|
17
|
+
].flatMap((e) => [
|
|
18
|
+
"metacarpal",
|
|
19
|
+
"phalanx-proximal",
|
|
20
|
+
"phalanx-intermediate",
|
|
21
|
+
"phalanx-distal",
|
|
22
|
+
"tip"
|
|
23
|
+
].map((t) => `${e}-finger-${t}`))
|
|
24
|
+
]), s = class s {
|
|
25
|
+
static async create(e, t, n = {}) {
|
|
26
|
+
if (n.signal?.throwIfAborted(), typeof e?.onFrame != "function" || !Array.isArray(e.controllers)) throw TypeError("SkinnedHands needs an XR host and hand models; pass the XR from XR.create and { left, right }");
|
|
27
|
+
if (!t || !t.left && !t.right || t.left && t.left === t.right) throw TypeError("Provide at least one separate skinned hand model; pass { left, right }");
|
|
28
|
+
let { Animation: r } = await import("@margoengine/animation");
|
|
29
|
+
return n.signal?.throwIfAborted(), new s(e, t, n, r);
|
|
30
|
+
}
|
|
31
|
+
constructor(n, r, i, a) {
|
|
32
|
+
this.xr = n, this.rigs = [], this.stops = [], this.session = null, this.disposed = !1, this.local = /* @__PURE__ */ new Float64Array(16), this.inverse = /* @__PURE__ */ new Float64Array(16), this.scratch = /* @__PURE__ */ new Float64Array(52);
|
|
33
|
+
try {
|
|
34
|
+
for (let t of ["left", "right"]) {
|
|
35
|
+
let n = r[t];
|
|
36
|
+
if (!n) continue;
|
|
37
|
+
if (n.disposed) throw e("disposed", "A hand model is disposed; load a live skinned glTF hand");
|
|
38
|
+
let s = n.asset.doc.nodes ?? [], c = n.asset.doc.skins ?? [], l = o.map((e) => {
|
|
39
|
+
let t = i.jointNames?.[e] ?? e, n = s.flatMap((e, n) => e.name === t ? [n] : []);
|
|
40
|
+
if (n.length !== 1) throw TypeError(`Hand joint '${t}' needs exactly one glTF node; use jointNames to map all 25 joints`);
|
|
41
|
+
return n[0];
|
|
42
|
+
});
|
|
43
|
+
if (new Set(l).size !== 25 || !c.some((e) => l.every((t) => e.joints.includes(t)))) throw TypeError("Hand models need one skin containing all 25 unique joints; export a WebXR-compatible skinned glTF");
|
|
44
|
+
let u = n.nodes.map((e) => {
|
|
45
|
+
let t = e.engine.store.transforms, n = e.id;
|
|
46
|
+
return {
|
|
47
|
+
position: [
|
|
48
|
+
t.x[n],
|
|
49
|
+
t.y[n],
|
|
50
|
+
t.z[n]
|
|
51
|
+
],
|
|
52
|
+
rotation: [
|
|
53
|
+
t.qx[n],
|
|
54
|
+
t.qy[n],
|
|
55
|
+
t.qz[n],
|
|
56
|
+
t.qw[n]
|
|
57
|
+
],
|
|
58
|
+
scale: [
|
|
59
|
+
t.sx[n],
|
|
60
|
+
t.sy[n],
|
|
61
|
+
t.sz[n]
|
|
62
|
+
]
|
|
63
|
+
};
|
|
64
|
+
}), d = s.map(() => -1), f = [];
|
|
65
|
+
s.forEach((e, t) => e.children?.forEach((e) => {
|
|
66
|
+
d[e] = t;
|
|
67
|
+
}));
|
|
68
|
+
let p = (e) => {
|
|
69
|
+
f.push(e), s[e].children?.forEach(p);
|
|
70
|
+
};
|
|
71
|
+
d.forEach((e, t) => {
|
|
72
|
+
e === -1 && p(t);
|
|
73
|
+
});
|
|
74
|
+
let m = new a(n);
|
|
75
|
+
try {
|
|
76
|
+
n.root.scene.use(m);
|
|
77
|
+
} catch (e) {
|
|
78
|
+
throw m.dispose(), e;
|
|
79
|
+
}
|
|
80
|
+
m.playing = !1, this.rigs.push({
|
|
81
|
+
hand: t,
|
|
82
|
+
model: n,
|
|
83
|
+
player: m,
|
|
84
|
+
visible: n.root.visible,
|
|
85
|
+
rest: u,
|
|
86
|
+
parents: d,
|
|
87
|
+
order: f,
|
|
88
|
+
indices: l,
|
|
89
|
+
world: s.map(() => /* @__PURE__ */ new Float64Array(16)),
|
|
90
|
+
sources: /* @__PURE__ */ new WeakMap()
|
|
91
|
+
}), n.root.visible = !1;
|
|
92
|
+
}
|
|
93
|
+
for (let e of new Set(this.rigs.map((e) => e.model.root.scene))) this.stops.push(t(e, () => this.dispose()));
|
|
94
|
+
if (this.listen(n.session), this.stops.push(n.onFrame((e) => this.update(e))), i.signal) {
|
|
95
|
+
let e = () => this.dispose();
|
|
96
|
+
i.signal.addEventListener("abort", e, { once: !0 }), this.stops.push(() => i.signal.removeEventListener("abort", e));
|
|
97
|
+
}
|
|
98
|
+
} catch (e) {
|
|
99
|
+
throw this.dispose(), e;
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
listen(e) {
|
|
103
|
+
if (e === this.session) return;
|
|
104
|
+
if (this.stopSession?.(), this.session = e, !e) {
|
|
105
|
+
this.stopSession = void 0;
|
|
106
|
+
return;
|
|
107
|
+
}
|
|
108
|
+
let t = () => {
|
|
109
|
+
for (let e of this.rigs) e.model.disposed || (e.model.root.visible = !1);
|
|
110
|
+
};
|
|
111
|
+
e.addEventListener("end", t), this.stopSession = () => e.removeEventListener("end", t);
|
|
112
|
+
}
|
|
113
|
+
update(e) {
|
|
114
|
+
if (!(this.disposed || e.frame.session !== this.xr.session)) {
|
|
115
|
+
this.listen(this.xr.session);
|
|
116
|
+
for (let e of this.rigs) {
|
|
117
|
+
if (e.model.disposed) continue;
|
|
118
|
+
let t = this.xr.controllers.find((t) => t.source.handedness === e.hand && t.source.hand), n = !!t && t.source.hand.size === 25 && t.handTracked.every(Boolean) && !t.source.skipRendering;
|
|
119
|
+
e.model.root.visible = n, n && this.pose(e, t);
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
pose(e, t) {
|
|
124
|
+
let s = e.sources.get(t.source);
|
|
125
|
+
if (!s) {
|
|
126
|
+
if (s = Array.from(t.source.hand.keys()).map((t) => {
|
|
127
|
+
let n = o.indexOf(t);
|
|
128
|
+
if (n < 0) throw TypeError(`Unknown WebXR joint '${t}'; supply the standard 25-joint hand`);
|
|
129
|
+
return e.indices[n];
|
|
130
|
+
}), new Set(s).size !== 25) throw TypeError("WebXR hand joint names repeat; supply the standard 25-joint hand");
|
|
131
|
+
e.sources.set(t.source, s);
|
|
132
|
+
}
|
|
133
|
+
let c = new Map(s.map((e, t) => [e, t])), l = e.model.root, u = l.engine.store, d = u.transforms, f = [];
|
|
134
|
+
for (let e = l.id; e !== -1; e = u.parents[e]) f.push(e);
|
|
135
|
+
let p = new Float64Array([
|
|
136
|
+
1,
|
|
137
|
+
0,
|
|
138
|
+
0,
|
|
139
|
+
0,
|
|
140
|
+
0,
|
|
141
|
+
1,
|
|
142
|
+
0,
|
|
143
|
+
0,
|
|
144
|
+
0,
|
|
145
|
+
0,
|
|
146
|
+
1,
|
|
147
|
+
0,
|
|
148
|
+
0,
|
|
149
|
+
0,
|
|
150
|
+
0,
|
|
151
|
+
1
|
|
152
|
+
]);
|
|
153
|
+
for (let e = f.length - 1; e >= 0; e--) {
|
|
154
|
+
let t = f[e];
|
|
155
|
+
n([
|
|
156
|
+
d.x[t],
|
|
157
|
+
d.y[t],
|
|
158
|
+
d.z[t]
|
|
159
|
+
], [
|
|
160
|
+
d.qx[t],
|
|
161
|
+
d.qy[t],
|
|
162
|
+
d.qz[t],
|
|
163
|
+
d.qw[t]
|
|
164
|
+
], [
|
|
165
|
+
d.sx[t],
|
|
166
|
+
d.sy[t],
|
|
167
|
+
d.sz[t]
|
|
168
|
+
], this.local), i(p, this.local, this.inverse), p.set(this.inverse);
|
|
169
|
+
}
|
|
170
|
+
for (let o of e.order) {
|
|
171
|
+
let s = e.parents[o] < 0 ? p : e.world[e.parents[o]], l = c.get(o), u = e.world[o];
|
|
172
|
+
if (l === void 0) {
|
|
173
|
+
let t = e.rest[o];
|
|
174
|
+
n(t.position, t.rotation, t.scale, this.local), i(s, this.local, u);
|
|
175
|
+
} else {
|
|
176
|
+
if (u.set(t.handMatrices.subarray(l * 16, l * 16 + 16)), !r(s, this.inverse, this.scratch)) throw RangeError("Hand parent transform is singular; use a nonsingular hand model root");
|
|
177
|
+
i(this.inverse, u, this.local);
|
|
178
|
+
let n = a(this.local);
|
|
179
|
+
if (!n.ok) throw RangeError(`Hand local transform has ${n.reason}; use an unscaled or uniformly scaled model root`);
|
|
180
|
+
e.model.nodes[o].setPosition(n.position).setQuaternion(n.rotation).setScale(n.scale);
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
dispose() {
|
|
185
|
+
if (!this.disposed) {
|
|
186
|
+
this.disposed = !0, this.stopSession?.(), this.stopSession = void 0;
|
|
187
|
+
for (let e of this.stops) e();
|
|
188
|
+
this.stops.length = 0;
|
|
189
|
+
for (let e of this.rigs) e.model.root.scene.removePlugin(e.player.name), e.model.disposed || (e.rest.forEach((t, n) => e.model.nodes[n].setPosition(t.position).setQuaternion(t.rotation).setScale(t.scale)), e.model.root.visible = e.visible);
|
|
190
|
+
this.rigs.length = 0;
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
[Symbol.dispose ?? Symbol()]() {
|
|
194
|
+
this.dispose();
|
|
195
|
+
}
|
|
196
|
+
};
|
|
197
|
+
//#endregion
|
|
198
|
+
export { o as HAND_JOINT_NAMES, s as SkinnedHands };
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import type { XRAnchor, XRHitTestResult, XRHitTestTrackableType, XRInputSource, XRNativeFrame, XRNativeSession, XRSpace } from './types.js';
|
|
2
|
+
/** What `HitTest.create` needs of an `XR`: its session and reference space (the frame callback's space). */
|
|
3
|
+
export interface HitTestHost {
|
|
4
|
+
readonly session: XRNativeSession | null;
|
|
5
|
+
readonly referenceSpace: XRSpace | undefined;
|
|
6
|
+
}
|
|
7
|
+
/** Options for {@link HitTest.create}. */
|
|
8
|
+
export interface HitTestOptions {
|
|
9
|
+
/**
|
|
10
|
+
* Where the ray starts: `'viewer'` (the centre of the view, for a placement reticle; default), an input source (its
|
|
11
|
+
* target ray: a controller or gaze), or `{profile}` for transient inputs of that profile, such as screen taps
|
|
12
|
+
* (`'generic-touchscreen'`).
|
|
13
|
+
*/
|
|
14
|
+
from?: 'viewer' | XRInputSource | {
|
|
15
|
+
profile: string;
|
|
16
|
+
};
|
|
17
|
+
/** Real-world features the ray hits. Default `['plane']`. */
|
|
18
|
+
entityTypes?: readonly XRHitTestTrackableType[];
|
|
19
|
+
/** The ray in the source's space: origin (default [0, 0, 0]) and direction (default [0, 0, -1]). */
|
|
20
|
+
offsetRay?: {
|
|
21
|
+
origin?: readonly [number, number, number];
|
|
22
|
+
direction?: readonly [number, number, number];
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
/** One hit: its pose in the session's reference space, and the input source for a transient-input hit. */
|
|
26
|
+
export interface Hit {
|
|
27
|
+
/** Column-major 4x4 pose: y is the surface normal for a plane hit. */
|
|
28
|
+
readonly matrix: Float32Array;
|
|
29
|
+
readonly position: [number, number, number];
|
|
30
|
+
/** Unit quaternion [x, y, z, w]. */
|
|
31
|
+
readonly orientation: [number, number, number, number];
|
|
32
|
+
/** The tap or controller that produced it (transient-input sources only). */
|
|
33
|
+
readonly inputSource?: XRInputSource;
|
|
34
|
+
/** The browser's result, for `createAnchor()` and other module features. */
|
|
35
|
+
readonly result: XRHitTestResult;
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Hit tests in an `immersive-ar` session: `await HitTest.create(xr, {from: 'viewer'})` after `xr.requestSession(
|
|
39
|
+
* 'immersive-ar', {requiredFeatures: ['hit-test']})`, then `hits.results(state.frame)` in the frame callback, nearest
|
|
40
|
+
* first. The source is cancelled with `cancel()` or when the session ends. Throws MargoError 'unsupported' when the
|
|
41
|
+
* session was started without the 'hit-test' feature or the browser lacks the module, 'state' without a session.
|
|
42
|
+
*/
|
|
43
|
+
export declare class HitTest {
|
|
44
|
+
private readonly host;
|
|
45
|
+
private readonly session;
|
|
46
|
+
private readonly source;
|
|
47
|
+
private readonly transient;
|
|
48
|
+
private cancelled;
|
|
49
|
+
private constructor();
|
|
50
|
+
/** Subscribes to hit tests from `options.from` in `xr`'s current session. */
|
|
51
|
+
static create(xr: HitTestHost, options?: HitTestOptions): Promise<HitTest>;
|
|
52
|
+
/** Whether the source has been cancelled (by `cancel()` or the session ending). */
|
|
53
|
+
get active(): boolean;
|
|
54
|
+
/**
|
|
55
|
+
* This frame's hits, nearest first, as poses in the XR reference space. Empty once cancelled, for another session's
|
|
56
|
+
* frame, or while the device has not found a surface.
|
|
57
|
+
*/
|
|
58
|
+
results(frame: XRNativeFrame): Hit[];
|
|
59
|
+
/** The nearest hit of this frame, or null. */
|
|
60
|
+
first(frame: XRNativeFrame): Hit | null;
|
|
61
|
+
/**
|
|
62
|
+
* Anchors `hit` where the device keeps tracking it (WebXR Anchors Module, session feature 'anchors'); read the
|
|
63
|
+
* anchor's pose with `frame.getPose(anchor.anchorSpace, space)` each frame.
|
|
64
|
+
*/
|
|
65
|
+
anchor(hit: Hit): Promise<XRAnchor>;
|
|
66
|
+
/** Ends the subscription. */
|
|
67
|
+
cancel(): void;
|
|
68
|
+
private readonly ended;
|
|
69
|
+
}
|