@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,155 @@
|
|
|
1
|
+
import { n as e } from "./errors-XvcwUnU4.js";
|
|
2
|
+
//#region ../shared/xr-run.js
|
|
3
|
+
var t = [
|
|
4
|
+
{
|
|
5
|
+
id: "vr-webgpu",
|
|
6
|
+
mode: "immersive-vr",
|
|
7
|
+
route: "webgpu",
|
|
8
|
+
needs: "a VR headset whose browser has WebXR's WebGPU binding (XRGPUBinding)"
|
|
9
|
+
},
|
|
10
|
+
{
|
|
11
|
+
id: "vr-webgl2",
|
|
12
|
+
mode: "immersive-vr",
|
|
13
|
+
route: "webgl2",
|
|
14
|
+
needs: "a VR headset whose browser has WebXR on WebGL2"
|
|
15
|
+
},
|
|
16
|
+
{
|
|
17
|
+
id: "ar-webgpu",
|
|
18
|
+
mode: "immersive-ar",
|
|
19
|
+
route: "webgpu",
|
|
20
|
+
needs: "an AR phone or headset whose browser has WebXR's WebGPU binding (XRGPUBinding)"
|
|
21
|
+
},
|
|
22
|
+
{
|
|
23
|
+
id: "ar-webgl2",
|
|
24
|
+
mode: "immersive-ar",
|
|
25
|
+
route: "webgl2",
|
|
26
|
+
needs: "an AR phone or headset with WebXR on WebGL2"
|
|
27
|
+
}
|
|
28
|
+
], n = 300, r = (e) => typeof e == "string" && e.length > 0, i = (e) => Number.isInteger(e) && e >= 0, a = (e) => Number.isFinite(e) && e >= 0;
|
|
29
|
+
function o(e) {
|
|
30
|
+
if (typeof e != "object" || !e) return ["the run is not an object"];
|
|
31
|
+
let t = [], n = (e, n) => {
|
|
32
|
+
e || t.push(`${n} is missing or wrong`);
|
|
33
|
+
};
|
|
34
|
+
n(e.schema === 1, "schema"), n(r(e.date) && !Number.isNaN(Date.parse(e.date)), "date"), n(r(e.userAgent), "userAgent"), n(typeof e.native == "boolean", "native"), n([
|
|
35
|
+
"inline",
|
|
36
|
+
"immersive-vr",
|
|
37
|
+
"immersive-ar"
|
|
38
|
+
].includes(e.mode), "mode"), n([
|
|
39
|
+
"webgpu",
|
|
40
|
+
"webgl2",
|
|
41
|
+
"none"
|
|
42
|
+
].includes(e.route), "route"), n(Array.isArray(e.features) && e.features.every(r), "features");
|
|
43
|
+
for (let t of [
|
|
44
|
+
"views",
|
|
45
|
+
"frames",
|
|
46
|
+
"tracked",
|
|
47
|
+
"emulated"
|
|
48
|
+
]) n(i(e[t]), t);
|
|
49
|
+
n(e.tracked <= e.frames && e.emulated <= e.tracked, "tracked");
|
|
50
|
+
for (let t of [
|
|
51
|
+
"median",
|
|
52
|
+
"p95",
|
|
53
|
+
"max"
|
|
54
|
+
]) n(a(e.frameMs?.[t]), `frameMs.${t}`);
|
|
55
|
+
if (n(Array.isArray(e.inputs) && e.inputs.every((e) => r(e?.handedness) && r(e.targetRayMode) && Array.isArray(e.profiles) && typeof e.hand == "boolean" && i(e.buttons) && i(e.axes)), "inputs"), n(typeof e.endedEarly == "boolean", "endedEarly"), n(Array.isArray(e.errors) && e.errors.every((e) => typeof e == "string"), "errors"), e.observed !== void 0) for (let t of [
|
|
56
|
+
"scene",
|
|
57
|
+
"tracking",
|
|
58
|
+
"input",
|
|
59
|
+
"exit"
|
|
60
|
+
]) n(typeof e.observed?.[t] == "boolean", `observed.${t}`);
|
|
61
|
+
return t;
|
|
62
|
+
}
|
|
63
|
+
function s(e, t) {
|
|
64
|
+
let n = [], r = (e, t) => {
|
|
65
|
+
e || n.push(t);
|
|
66
|
+
};
|
|
67
|
+
r(e.native, "navigator.xr was a stand-in, not the browser"), r(e.mode === t.mode, `the mode was ${e.mode}, not ${t.mode}`), r(e.route === t.route, `the route was ${e.route}, not ${t.route}`), r(e.tracked >= 300, `only ${e.tracked} tracked frames, under 300`), r(e.views >= (t.mode === "immersive-vr" ? 2 : 1), `only ${e.views} views`), r(!e.errors.length, `${e.errors.length} errors`), r(!e.endedEarly, "the session ended early"), r(e.inputs.length > 0, "no input source was seen");
|
|
68
|
+
for (let t of [
|
|
69
|
+
"scene",
|
|
70
|
+
"tracking",
|
|
71
|
+
"input",
|
|
72
|
+
"exit"
|
|
73
|
+
]) r(e.observed?.[t] === !0, `the tester did not confirm ${t}`);
|
|
74
|
+
return n;
|
|
75
|
+
}
|
|
76
|
+
//#endregion
|
|
77
|
+
//#region src/device-check.ts
|
|
78
|
+
var c = [
|
|
79
|
+
"hand-tracking",
|
|
80
|
+
"hit-test",
|
|
81
|
+
"anchors",
|
|
82
|
+
"plane-detection",
|
|
83
|
+
"depth-sensing",
|
|
84
|
+
"light-estimation"
|
|
85
|
+
];
|
|
86
|
+
function l() {
|
|
87
|
+
let e = globalThis.navigator?.xr?.requestSession;
|
|
88
|
+
return typeof e == "function" && /\{\s*\[native code\]\s*\}\s*$/.test(Function.prototype.toString.call(e));
|
|
89
|
+
}
|
|
90
|
+
function u(e) {
|
|
91
|
+
return e === "inline" ? "none" : globalThis.XRGPUBinding?.webgl ? "webgl2" : "webgpu";
|
|
92
|
+
}
|
|
93
|
+
var d = (e, t) => e.length ? e[Math.min(e.length - 1, Math.floor(e.length * t))] : 0, f = (e) => ({
|
|
94
|
+
handedness: e.handedness,
|
|
95
|
+
targetRayMode: e.targetRayMode,
|
|
96
|
+
profiles: [...e.profiles],
|
|
97
|
+
hand: !!e.hand,
|
|
98
|
+
buttons: e.gamepad?.buttons.length ?? 0,
|
|
99
|
+
axes: e.gamepad?.axes.length ?? 0
|
|
100
|
+
});
|
|
101
|
+
async function p(t, n = {}) {
|
|
102
|
+
let { mode: r = "immersive-vr", seconds: i = 20, requiredFeatures: a, optionalFeatures: s = c, observe: p, signal: m, onFrame: h } = n;
|
|
103
|
+
if (!(i > 0) || !Number.isFinite(i)) throw RangeError("runDeviceCheck seconds must be above 0; pass the session length, such as 20");
|
|
104
|
+
let g = [], _ = /* @__PURE__ */ new Map(), v = [], y = {
|
|
105
|
+
schema: 1,
|
|
106
|
+
date: (/* @__PURE__ */ new Date()).toISOString(),
|
|
107
|
+
userAgent: globalThis.navigator?.userAgent ?? "unknown",
|
|
108
|
+
native: l(),
|
|
109
|
+
mode: r,
|
|
110
|
+
route: u(r),
|
|
111
|
+
features: [],
|
|
112
|
+
views: 0,
|
|
113
|
+
frames: 0,
|
|
114
|
+
tracked: 0,
|
|
115
|
+
emulated: 0,
|
|
116
|
+
frameMs: {
|
|
117
|
+
median: 0,
|
|
118
|
+
p95: 0,
|
|
119
|
+
max: 0
|
|
120
|
+
},
|
|
121
|
+
inputs: [],
|
|
122
|
+
endedEarly: !1,
|
|
123
|
+
errors: v
|
|
124
|
+
}, b = null, x = null, S = () => {}, C = !1, w = new Promise((e) => {
|
|
125
|
+
S = e;
|
|
126
|
+
}), T = await t.requestSession(r, {
|
|
127
|
+
...a && { requiredFeatures: a },
|
|
128
|
+
optionalFeatures: s,
|
|
129
|
+
...m && { signal: m },
|
|
130
|
+
onFrame(e) {
|
|
131
|
+
y.frames++, x !== null && g.push(e.time - x), x = e.time, b ??= e.time, e.viewer && (y.tracked++, e.viewer.emulatedPosition && y.emulated++, y.views = Math.max(y.views, e.viewer.views.length));
|
|
132
|
+
for (let t of e.inputs) _.has(t.source) || _.set(t.source, f(t.source));
|
|
133
|
+
try {
|
|
134
|
+
h?.(e);
|
|
135
|
+
} catch (e) {
|
|
136
|
+
v.push(String(e));
|
|
137
|
+
}
|
|
138
|
+
e.time - b >= i * 1e3 && (C = !0, S());
|
|
139
|
+
}
|
|
140
|
+
});
|
|
141
|
+
y.features = [...T.enabledFeatures ?? []], T.addEventListener("end", () => S(), { once: !0 });
|
|
142
|
+
let E = () => S();
|
|
143
|
+
m?.addEventListener("abort", E, { once: !0 }), m?.aborted && S(), await w, m?.removeEventListener("abort", E), y.endedEarly = !C, await t.end().catch((e) => v.push(String(e))), m?.throwIfAborted();
|
|
144
|
+
let D = g.sort((e, t) => e - t);
|
|
145
|
+
y.frameMs = {
|
|
146
|
+
median: d(D, .5),
|
|
147
|
+
p95: d(D, .95),
|
|
148
|
+
max: D.at(-1) ?? 0
|
|
149
|
+
}, y.inputs = [..._.values()], p && (y.observed = await p(y));
|
|
150
|
+
let O = o(y);
|
|
151
|
+
if (O.length) throw e("failed", `The device run is not well formed (${O.join("; ")}); report this as a bug`);
|
|
152
|
+
return y;
|
|
153
|
+
}
|
|
154
|
+
//#endregion
|
|
155
|
+
export { n as XR_RUN_MIN_FRAMES, t as XR_RUN_TARGETS, o as checkXRRun, p as runDeviceCheck, s as xrRunGaps };
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
//#region ../shared/errors.js
|
|
2
|
+
var e = (e, t) => Object.assign(Error(t), {
|
|
3
|
+
name: "MargoError",
|
|
4
|
+
code: e
|
|
5
|
+
}), t = (e, t) => `${e}; see https://margoengine.com/errors#${t}`, n = (n, r, i = n) => e(n, t(r, i)), r = (e, n) => RangeError(t(e, n));
|
|
6
|
+
//#endregion
|
|
7
|
+
export { e as n, r, n as t };
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import type { XRBinding, XRSubImage, XRView } from './types.js';
|
|
2
|
+
/** The scene depth of one eye, as the eye's renderer left it, for passes that compare against it. */
|
|
3
|
+
export interface XREyeSceneDepth {
|
|
4
|
+
/** Depth-only texture, multisampled where the eye renders with MSAA; read it with `textureLoad`. */
|
|
5
|
+
readonly texture: GPUTexture;
|
|
6
|
+
/** The column-major projection that wrote it: clip w is the view-space distance (-z), clip z / w the stored depth. */
|
|
7
|
+
readonly projection: ArrayLike<number>;
|
|
8
|
+
/** The texel of the eye's top-left pixel in `texture`. */
|
|
9
|
+
readonly origin: readonly [number, number];
|
|
10
|
+
/** The value an empty sample holds: 1 for forward depth, 0 for reversed. */
|
|
11
|
+
readonly clear: number;
|
|
12
|
+
}
|
|
13
|
+
/** One eye after scene rendering, before submission to the XR compositor. */
|
|
14
|
+
export interface XREyeTarget {
|
|
15
|
+
/** Stable index of the view within this frame. */
|
|
16
|
+
readonly index: number;
|
|
17
|
+
readonly device: GPUDevice;
|
|
18
|
+
readonly encoder: GPUCommandEncoder;
|
|
19
|
+
readonly image: XRSubImage;
|
|
20
|
+
readonly view: XRView;
|
|
21
|
+
/** The eye's scene depth, valid until the encoder is submitted. */
|
|
22
|
+
readonly scene?: XREyeSceneDepth;
|
|
23
|
+
/** The session's XR binding, for per-view resources such as GPU depth information. */
|
|
24
|
+
readonly binding?: XRBinding;
|
|
25
|
+
}
|
package/dist/eyes.d.ts
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import type { XREyeTarget } from './eye-target.js';
|
|
2
|
+
import { type Engine } from '@margoengine/core/extension';
|
|
3
|
+
import type { XRBinding, XRProjectionLayer, XRViewerPose } from './types.js';
|
|
4
|
+
/** Draws every eye of a viewer pose through `engine.render({views})` on the engine's device. */
|
|
5
|
+
export declare class EyeViews {
|
|
6
|
+
private readonly engine;
|
|
7
|
+
private readonly device;
|
|
8
|
+
private readonly eyes;
|
|
9
|
+
private readonly views;
|
|
10
|
+
private readonly offset;
|
|
11
|
+
private pipelines?;
|
|
12
|
+
private readonly options;
|
|
13
|
+
/** While true, the AR alpha pass (see `clearAlphaShader`) runs in this object's eye views. */
|
|
14
|
+
private ar;
|
|
15
|
+
/**
|
|
16
|
+
* The engine's premultiplied mark before AR eyes set it, while they hold it: with empty pixels at alpha 0 the eye is
|
|
17
|
+
* premultiplied colour over the real world, so TAA keeps coverage in its history and display transforms take
|
|
18
|
+
* straight colour, as on a TransparentCanvas (shared/straight-display.js).
|
|
19
|
+
*/
|
|
20
|
+
private premultipliedBefore?;
|
|
21
|
+
private readonly alphaPipelines;
|
|
22
|
+
private alphaModule?;
|
|
23
|
+
/** The frame graphs holding the AR alpha pass (one per scene drawn), with their removers. */
|
|
24
|
+
private readonly alphaGraphs;
|
|
25
|
+
private readonly alphaDescriptor;
|
|
26
|
+
constructor(engine: Engine, device: GPUDevice);
|
|
27
|
+
private readonly clearAlpha;
|
|
28
|
+
/** Adds the AR alpha pass to the frame graph of the scene about to be drawn, once per graph. */
|
|
29
|
+
private watchAlpha;
|
|
30
|
+
/** Moves eye `i`'s camera to `view`: its pose and its projection, with depth reversed. */
|
|
31
|
+
private place;
|
|
32
|
+
private eye;
|
|
33
|
+
/** Makes the eye's textures for a sub-image of `width` by `height`, keeping them while the size holds. */
|
|
34
|
+
private size;
|
|
35
|
+
private dropCover;
|
|
36
|
+
/** Records the eye's share of drawn samples (see `coverageShader`) and returns the texture holding it. */
|
|
37
|
+
private cover;
|
|
38
|
+
private build;
|
|
39
|
+
/** Draws this frame's eyes and puts them in `layer`. `delta` in seconds; `ar` keeps the real world behind the scene. */
|
|
40
|
+
render(binding: XRBinding, layer: XRProjectionLayer, viewer: XRViewerPose, delta: number, ar: boolean, onEye?: (target: XREyeTarget) => boolean | void, layers?: number): void;
|
|
41
|
+
/** Gives the engine back the premultiplied mark it had before AR eyes. */
|
|
42
|
+
private unmark;
|
|
43
|
+
/** Frees the eyes' textures and buffers; the engine and its device stay. */
|
|
44
|
+
dispose(): void;
|
|
45
|
+
}
|
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 1.0.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("1.0.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
|
+
}
|