@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,218 @@
|
|
|
1
|
+
import { n as e } from "./errors-XvcwUnU4.js";
|
|
2
|
+
//#region src/occlusion.ts
|
|
3
|
+
function t(e, t) {
|
|
4
|
+
return `struct U { m: mat4x4f, n: mat4x4f, p: vec4f, z: vec4f, v: vec4f, o: vec4f, s: vec4f }
|
|
5
|
+
@group(0) @binding(0) var<uniform> u: U;
|
|
6
|
+
@group(0) @binding(1) var scene: ${t > 1 ? "texture_depth_multisampled_2d" : "texture_depth_2d"};
|
|
7
|
+
@group(0) @binding(2) var real: ${e === "r16uint" ? "texture_2d<u32>" : "texture_2d<f32>"};
|
|
8
|
+
@vertex fn vertex(@builtin(vertex_index) i: u32) -> @builtin(position) vec4f {
|
|
9
|
+
return vec4f(array<vec2f, 3>(vec2f(-1, -1), vec2f(3, -1), vec2f(-1, 3))[i], 0, 1);
|
|
10
|
+
}
|
|
11
|
+
fn hidden(f: vec2f, z: f32) -> f32 {
|
|
12
|
+
let e = (f - u.v.xy) / u.v.zw;
|
|
13
|
+
let ndc = vec2f(2.0 * e.x - 1.0, 1.0 - 2.0 * e.y);
|
|
14
|
+
let w = u.z.y / (z + u.z.x);
|
|
15
|
+
let c = u.m * vec4f((ndc.x + u.p.z) * w / u.p.x, (ndc.y + u.p.w) * w / u.p.y, -w, 1.0);
|
|
16
|
+
if (c.w <= 0.0) { return 0.0; }
|
|
17
|
+
let b = u.n * vec4f(0.5 + 0.5 * c.x / c.w, 0.5 - 0.5 * c.y / c.w, 0.0, 1.0);
|
|
18
|
+
let t = clamp(vec2i(floor(b.xy * u.s.xy)), vec2i(0), vec2i(u.s.xy) - vec2i(1));
|
|
19
|
+
let d = f32(textureLoad(real, t, 0).r) * u.o.z;
|
|
20
|
+
if (d <= 0.0) { return 0.0; }
|
|
21
|
+
if (u.z.w > 0.0) { return clamp((c.w - d) / u.z.w + 0.5, 0.0, 1.0); }
|
|
22
|
+
return select(0.0, 1.0, c.w > d);
|
|
23
|
+
}
|
|
24
|
+
fn drawn(z: f32) -> bool {
|
|
25
|
+
return z != u.z.z && u.z.y / (z + u.z.x) > 0.0;
|
|
26
|
+
}
|
|
27
|
+
@fragment fn fragment(@builtin(position) f: vec4f) -> @location(0) vec4f {
|
|
28
|
+
let q = vec2i(f.xy - u.v.xy + u.o.xy);
|
|
29
|
+
var sum = 0.0;
|
|
30
|
+
var count = 0.0;
|
|
31
|
+
for (var i = 0; i < ${t}; i++) {
|
|
32
|
+
let z = textureLoad(scene, q, ${t > 1 ? "i" : "0"});
|
|
33
|
+
if (drawn(z)) { sum += hidden(f.xy, z); count += 1.0; }
|
|
34
|
+
}
|
|
35
|
+
if (count == 0.0) {
|
|
36
|
+
let low = vec2i(u.o.xy);
|
|
37
|
+
let high = low + vec2i(u.v.zw) - vec2i(1);
|
|
38
|
+
for (var dy = -1; dy <= 1; dy++) {
|
|
39
|
+
for (var dx = -1; dx <= 1; dx++) {
|
|
40
|
+
let n = clamp(q + vec2i(dx, dy), low, high);
|
|
41
|
+
let z = textureLoad(scene, n, 0);
|
|
42
|
+
if (drawn(z)) { sum += hidden(f.xy + vec2f(n - q), z); count += 1.0; }
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
return vec4f(0.0, 0.0, 0.0, select(0.0, sum / count, count > 0.0));
|
|
47
|
+
}
|
|
48
|
+
`;
|
|
49
|
+
}
|
|
50
|
+
function n(e, t, n, r) {
|
|
51
|
+
for (let i = 0; i < 4; i++) for (let a = 0; a < 4; a++) e[t + i * 4 + a] = n[a] * r[i * 4] + n[4 + a] * r[i * 4 + 1] + n[8 + a] * r[i * 4 + 2] + n[12 + a] * r[i * 4 + 3];
|
|
52
|
+
}
|
|
53
|
+
var r = class r {
|
|
54
|
+
constructor(e, t, n) {
|
|
55
|
+
this.session = e, this.frame = null, this.eyes = [], this.pipelines = /* @__PURE__ */ new Map(), this.disposed = !1, this.feather_ = 0, this.feather = n.feather ?? .05, this.enabled = n.enabled ?? !0, this.stops = [t.onFrame((e) => {
|
|
56
|
+
this.frame = e.frame;
|
|
57
|
+
}), t.onEye((e) => this.draw(e))];
|
|
58
|
+
}
|
|
59
|
+
static create(t, n = {}) {
|
|
60
|
+
let i = t?.session;
|
|
61
|
+
if (!i) throw e("state", "AROcclusion needs an active XR session; await xr.requestSession first");
|
|
62
|
+
if (i.enabledFeatures && !i.enabledFeatures.includes("depth-sensing")) throw e("unsupported", "The XR session has no 'depth-sensing' feature; ask for it with init: {depthSensing}");
|
|
63
|
+
let a, o;
|
|
64
|
+
try {
|
|
65
|
+
a = i.depthUsage, o = i.depthDataFormat;
|
|
66
|
+
} catch (t) {
|
|
67
|
+
throw Object.assign(e("unsupported", "The XR session has no depth sensing; start it with init: {depthSensing: DEPTH_SENSING}"), { cause: t });
|
|
68
|
+
}
|
|
69
|
+
if (a === "gpu-optimized" && o === "luminance-alpha") throw e("unsupported", "GPU-optimized depth in 'luminance-alpha' cannot be read by WebGPU (Depth Sensing, XRGPUDepthInformation); ask for 'float32' or 'unsigned-short'");
|
|
70
|
+
return new r(i, t, n);
|
|
71
|
+
}
|
|
72
|
+
get feather() {
|
|
73
|
+
return this.feather_;
|
|
74
|
+
}
|
|
75
|
+
set feather(e) {
|
|
76
|
+
if (!(Number.isFinite(e) && e >= 0)) throw RangeError("AROcclusion feather is in metres, 0 or more; pass a number such as 0.05");
|
|
77
|
+
this.feather_ = e;
|
|
78
|
+
}
|
|
79
|
+
information(e) {
|
|
80
|
+
if (this.session.depthUsage === "gpu-optimized") {
|
|
81
|
+
let t = e.binding?.getDepthInformation?.(e.view) ?? null;
|
|
82
|
+
return t ? { gpu: t } : null;
|
|
83
|
+
}
|
|
84
|
+
let t = this.frame?.getDepthInformation?.(e.view) ?? null;
|
|
85
|
+
return t ? { cpu: t } : null;
|
|
86
|
+
}
|
|
87
|
+
draw(r) {
|
|
88
|
+
if (this.disposed || !this.enabled || !r.scene) return;
|
|
89
|
+
let i = this.information(r);
|
|
90
|
+
if (!i) return;
|
|
91
|
+
let { device: a, encoder: o, image: s, view: c, scene: l } = r, u = i.gpu ?? i.cpu;
|
|
92
|
+
this.device !== a && this.reset(a);
|
|
93
|
+
let d = this.eyes[r.index];
|
|
94
|
+
d || (this.eyes[r.index] = d = {
|
|
95
|
+
uniform: a.createBuffer({
|
|
96
|
+
size: 448,
|
|
97
|
+
usage: GPUBufferUsage.UNIFORM | GPUBufferUsage.COPY_DST
|
|
98
|
+
}),
|
|
99
|
+
data: /* @__PURE__ */ new Float32Array(112)
|
|
100
|
+
});
|
|
101
|
+
let f, p;
|
|
102
|
+
if (i.gpu) {
|
|
103
|
+
let t = i.gpu.texture.format;
|
|
104
|
+
if (t !== "r32float" && t !== "r16uint") throw e("unsupported", `GPU depth arrives as ${t}; start the session with dataFormatPreference ['float32', 'unsigned-short'] so it arrives as r32float or r16uint`);
|
|
105
|
+
p = t, f = i.gpu.texture.createView(i.gpu.getViewDescriptor?.());
|
|
106
|
+
} else {
|
|
107
|
+
let t = i.cpu;
|
|
108
|
+
p = this.session.depthDataFormat === "float32" ? "r32float" : "r16uint";
|
|
109
|
+
let n = p === "r32float" ? 4 : 2;
|
|
110
|
+
if (t.data.byteLength < t.width * t.height * n) throw e("unsupported", `CPU depth holds ${t.data.byteLength} bytes for ${t.width} by ${t.height} values of ${n} bytes; the session's depthDataFormat must match the data the browser gives (report it to the browser)`);
|
|
111
|
+
(d.texture?.width !== t.width || d.texture.height !== t.height || d.texture.format !== p) && (d.texture?.destroy(), d.texture = a.createTexture({
|
|
112
|
+
size: [t.width, t.height],
|
|
113
|
+
format: p,
|
|
114
|
+
usage: GPUTextureUsage.TEXTURE_BINDING | GPUTextureUsage.COPY_DST,
|
|
115
|
+
label: "AR depth"
|
|
116
|
+
})), a.queue.writeTexture({ texture: d.texture }, t.data, { bytesPerRow: t.width * n }, [t.width, t.height]), f = d.texture.createView();
|
|
117
|
+
}
|
|
118
|
+
let m = l.texture.sampleCount, h = `${s.colorTexture.format} ${p} ${m}`, g = this.pipelines.get(h);
|
|
119
|
+
if (!g) {
|
|
120
|
+
let e = a.createShaderModule({
|
|
121
|
+
code: t(p, m),
|
|
122
|
+
label: "AR occlusion"
|
|
123
|
+
}), n = {
|
|
124
|
+
srcFactor: "zero",
|
|
125
|
+
dstFactor: "one-minus-src-alpha",
|
|
126
|
+
operation: "add"
|
|
127
|
+
};
|
|
128
|
+
g = a.createRenderPipeline({
|
|
129
|
+
layout: "auto",
|
|
130
|
+
vertex: {
|
|
131
|
+
module: e,
|
|
132
|
+
entryPoint: "vertex"
|
|
133
|
+
},
|
|
134
|
+
fragment: {
|
|
135
|
+
module: e,
|
|
136
|
+
entryPoint: "fragment",
|
|
137
|
+
targets: [{
|
|
138
|
+
format: s.colorTexture.format,
|
|
139
|
+
blend: {
|
|
140
|
+
color: n,
|
|
141
|
+
alpha: n
|
|
142
|
+
}
|
|
143
|
+
}]
|
|
144
|
+
},
|
|
145
|
+
label: "AR occlusion"
|
|
146
|
+
}), this.pipelines.set(h, g);
|
|
147
|
+
}
|
|
148
|
+
let _ = d.data, v = u.transform ?? c.transform, y = l.projection, b = s.viewport;
|
|
149
|
+
n(_, 96, v.inverse.matrix, c.transform.matrix), n(_, 0, u.projectionMatrix ?? c.projectionMatrix, _.subarray(96, 112)), _.set(u.normDepthBufferFromNormView.matrix, 16), _.set([
|
|
150
|
+
y[0],
|
|
151
|
+
y[5],
|
|
152
|
+
y[8],
|
|
153
|
+
y[9],
|
|
154
|
+
y[10],
|
|
155
|
+
y[14],
|
|
156
|
+
l.clear,
|
|
157
|
+
this.feather_
|
|
158
|
+
], 32), _.set([
|
|
159
|
+
b.x,
|
|
160
|
+
b.y,
|
|
161
|
+
b.width,
|
|
162
|
+
b.height,
|
|
163
|
+
l.origin[0],
|
|
164
|
+
l.origin[1],
|
|
165
|
+
u.rawValueToMeters,
|
|
166
|
+
0
|
|
167
|
+
], 40), _.set([
|
|
168
|
+
u.width,
|
|
169
|
+
u.height,
|
|
170
|
+
0,
|
|
171
|
+
0
|
|
172
|
+
], 48), a.queue.writeBuffer(d.uniform, 0, _, 0, 52);
|
|
173
|
+
let x = a.createBindGroup({
|
|
174
|
+
layout: g.getBindGroupLayout(0),
|
|
175
|
+
entries: [
|
|
176
|
+
{
|
|
177
|
+
binding: 0,
|
|
178
|
+
resource: {
|
|
179
|
+
buffer: d.uniform,
|
|
180
|
+
size: 208
|
|
181
|
+
}
|
|
182
|
+
},
|
|
183
|
+
{
|
|
184
|
+
binding: 1,
|
|
185
|
+
resource: l.texture.createView({ aspect: "depth-only" })
|
|
186
|
+
},
|
|
187
|
+
{
|
|
188
|
+
binding: 2,
|
|
189
|
+
resource: f
|
|
190
|
+
}
|
|
191
|
+
]
|
|
192
|
+
}), S = o.beginRenderPass({
|
|
193
|
+
colorAttachments: [{
|
|
194
|
+
view: s.colorTexture.createView(s.getViewDescriptor()),
|
|
195
|
+
loadOp: "load",
|
|
196
|
+
storeOp: "store"
|
|
197
|
+
}],
|
|
198
|
+
label: "AR occlusion"
|
|
199
|
+
});
|
|
200
|
+
S.setViewport(b.x, b.y, b.width, b.height, 0, 1), S.setPipeline(g), S.setBindGroup(0, x), S.draw(3), S.end();
|
|
201
|
+
}
|
|
202
|
+
reset(e) {
|
|
203
|
+
for (let e of this.eyes) e.uniform.destroy(), e.texture?.destroy();
|
|
204
|
+
this.eyes.length = 0, this.pipelines.clear(), this.device = e;
|
|
205
|
+
}
|
|
206
|
+
dispose() {
|
|
207
|
+
if (!this.disposed) {
|
|
208
|
+
this.disposed = !0;
|
|
209
|
+
for (let e of this.stops) e();
|
|
210
|
+
this.reset(void 0), this.frame = null;
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
[Symbol.dispose]() {
|
|
214
|
+
this.dispose();
|
|
215
|
+
}
|
|
216
|
+
};
|
|
217
|
+
//#endregion
|
|
218
|
+
export { r as AROcclusion, t as occlusionShader };
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
/// <reference lib="esnext.disposable" preserve="true" />
|
|
2
|
+
import type { XRSessionOptions } from './index.js';
|
|
3
|
+
import type { XRNativeSession, XRProjectionLayer, XRView } from './types.js';
|
|
4
|
+
/** Performance-related session negotiation options. */
|
|
5
|
+
export interface XRPerformanceSessionOptions {
|
|
6
|
+
/** Projection size relative to the runtime recommendation. Default native 1, or the compatibility installXR setting. Set before the session. */
|
|
7
|
+
framebufferScale?: number;
|
|
8
|
+
/** Request pose-synchronized AR camera images. Default false; choose required or optional browser permission. */
|
|
9
|
+
cameraAccess?: false | 'required' | 'optional';
|
|
10
|
+
}
|
|
11
|
+
/** Copies session options, adding projection scale and the camera-access feature without changing the inputs. */
|
|
12
|
+
export declare function performanceSessionOptions(options?: XRPerformanceSessionOptions, base?: XRSessionOptions): XRSessionOptions;
|
|
13
|
+
/** The session, layer and device accepted by XRPerformance. */
|
|
14
|
+
export interface XRPerformanceHost {
|
|
15
|
+
readonly session: XRNativeSession | null;
|
|
16
|
+
readonly projectionLayer: XRProjectionLayer | undefined;
|
|
17
|
+
readonly layerBinding: {
|
|
18
|
+
readonly device: GPUDevice;
|
|
19
|
+
readonly binding: object;
|
|
20
|
+
} | undefined;
|
|
21
|
+
}
|
|
22
|
+
/** Fixed foveation options. */
|
|
23
|
+
export interface XRPerformanceOptions {
|
|
24
|
+
/** Requested peripheral resolution reduction, from zero to one. Unset preserves the browser setting. */
|
|
25
|
+
fixedFoveation?: number;
|
|
26
|
+
/** Restore foveation and release references when aborted. */
|
|
27
|
+
signal?: AbortSignal;
|
|
28
|
+
}
|
|
29
|
+
/** Raw camera dimensions, supplied by the browser's Raw Camera Access module. */
|
|
30
|
+
export interface XRCameraLike {
|
|
31
|
+
readonly width: number;
|
|
32
|
+
readonly height: number;
|
|
33
|
+
}
|
|
34
|
+
/** A borrowed WebGL camera image; read only inside its XR frame, and never delete the texture. */
|
|
35
|
+
export interface XRCameraImage {
|
|
36
|
+
readonly camera: XRCameraLike;
|
|
37
|
+
readonly texture: WebGLTexture;
|
|
38
|
+
readonly context: WebGL2RenderingContext;
|
|
39
|
+
}
|
|
40
|
+
/** Live foveation and raw camera access for one immersive session. */
|
|
41
|
+
export declare class XRPerformance {
|
|
42
|
+
private readonly xr;
|
|
43
|
+
private readonly session;
|
|
44
|
+
private readonly layer;
|
|
45
|
+
private readonly initial;
|
|
46
|
+
private cameraBinding?;
|
|
47
|
+
private disposed;
|
|
48
|
+
private ended;
|
|
49
|
+
private readonly stops;
|
|
50
|
+
/** Attaches to an active session. Framebuffer scale is set earlier with performanceSessionOptions. */
|
|
51
|
+
static attach(xr: XRPerformanceHost, options?: XRPerformanceOptions): XRPerformance;
|
|
52
|
+
private constructor();
|
|
53
|
+
private active;
|
|
54
|
+
/** Actual foveation after runtime quantization, or null when unsupported. */
|
|
55
|
+
get fixedFoveation(): number | null;
|
|
56
|
+
/** Requests foveation from zero (off) to one (maximum). Unsupported runtimes reject. */
|
|
57
|
+
set fixedFoveation(value: number);
|
|
58
|
+
/**
|
|
59
|
+
* Reads a pose-synchronized camera texture during an active XR frame. Returns null when that view has no camera.
|
|
60
|
+
* Raw Camera Access specifies a WebGL texture, so native WebGPU sessions reject; the compatibility route uses its
|
|
61
|
+
* own XR-compatible WebGL2 context. Ask for camera-access before the session starts.
|
|
62
|
+
*/
|
|
63
|
+
cameraImage(view: XRView): XRCameraImage | null;
|
|
64
|
+
/** Restores the original foveation and removes listeners. Never deletes a borrowed camera texture. Idempotent. */
|
|
65
|
+
dispose(): void;
|
|
66
|
+
[Symbol.dispose]: () => void;
|
|
67
|
+
}
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
// @margo/core-built 1.0.0
|
|
2
|
+
import {coreRequirement as __margoCoreRequirement,assertCore as __margoAssertCore} from '@margoengine/core/extension';
|
|
3
|
+
|
|
4
|
+
import { n as e } from "./errors-XvcwUnU4.js";
|
|
5
|
+
//#region src/performance.ts
|
|
6
|
+
function t(e = {}, t = {}) {
|
|
7
|
+
let n = e.framebufferScale ?? t.framebufferScale;
|
|
8
|
+
if (n !== void 0 && (!Number.isFinite(Math.fround(n)) || Math.fround(n) <= 0)) throw RangeError("framebufferScale must be finite and positive; pass 1 for the runtime default");
|
|
9
|
+
let r = e.cameraAccess ?? !1;
|
|
10
|
+
if (r !== !1 && r !== "required" && r !== "optional") throw TypeError("cameraAccess must be false, required or optional; pass required to require camera permission");
|
|
11
|
+
let i = new Set(t.requiredFeatures ?? []), a = new Set(t.optionalFeatures ?? []);
|
|
12
|
+
return r === "required" && (i.add("camera-access"), a.delete("camera-access")), r === "optional" && !i.has("camera-access") && a.add("camera-access"), {
|
|
13
|
+
...t,
|
|
14
|
+
...n === void 0 ? {} : { framebufferScale: n },
|
|
15
|
+
requiredFeatures: [...i],
|
|
16
|
+
optionalFeatures: [...a]
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
var n = class t {
|
|
20
|
+
static attach(e, n = {}) {__margoAssertCore(e,__margoCoreRequirement("1.0.0",import.meta.url));
|
|
21
|
+
return new t(e, n);
|
|
22
|
+
}
|
|
23
|
+
constructor(t, n) {
|
|
24
|
+
if (this.xr = t, this.disposed = !1, this.ended = !1, this.stops = [], n.signal?.throwIfAborted(), !t.session || !t.projectionLayer) throw e("state", "XRPerformance needs an immersive session; await xr.requestSession first");
|
|
25
|
+
this.session = t.session, this.layer = t.projectionLayer.baseLayer ?? t.projectionLayer, this.initial = this.layer.fixedFoveation, n.fixedFoveation !== void 0 && (this.fixedFoveation = n.fixedFoveation);
|
|
26
|
+
let r = () => {
|
|
27
|
+
this.ended = !0, this.dispose();
|
|
28
|
+
};
|
|
29
|
+
if (this.session.addEventListener("end", r), this.stops.push(() => this.session.removeEventListener("end", r)), n.signal) {
|
|
30
|
+
let e = () => this.dispose();
|
|
31
|
+
n.signal.addEventListener("abort", e, { once: !0 }), this.stops.push(() => n.signal.removeEventListener("abort", e));
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
active() {
|
|
35
|
+
if (this.disposed) throw e("disposed", "XRPerformance is disposed; attach to a live immersive session");
|
|
36
|
+
if (this.xr.session !== this.session) throw e("state", "The XR session changed; attach XRPerformance to the new session");
|
|
37
|
+
}
|
|
38
|
+
get fixedFoveation() {
|
|
39
|
+
return this.active(), typeof this.layer.fixedFoveation == "number" ? this.layer.fixedFoveation : null;
|
|
40
|
+
}
|
|
41
|
+
set fixedFoveation(t) {
|
|
42
|
+
if (this.active(), !Number.isFinite(t) || t < 0 || t > 1) throw RangeError("fixedFoveation must be in [0, 1]; use 0 to disable foveation");
|
|
43
|
+
if (typeof this.layer.fixedFoveation != "number") throw e("unsupported", "This XR layer has no fixed foveation; use a runtime that exposes fixedFoveation");
|
|
44
|
+
this.layer.fixedFoveation = t;
|
|
45
|
+
}
|
|
46
|
+
cameraImage(t) {
|
|
47
|
+
if (this.active(), this.session.enabledFeatures && !this.session.enabledFeatures.includes("camera-access")) throw e("unsupported", "The session has no camera-access feature; request it with performanceSessionOptions before entering AR");
|
|
48
|
+
let n = this.xr.layerBinding?.device?.gl;
|
|
49
|
+
if (!n) throw e("unsupported", "Raw Camera Access exposes WebGL textures; use the WebGL2 compatibility XR route for cameraImage");
|
|
50
|
+
let r = t.camera;
|
|
51
|
+
if (!r) return null;
|
|
52
|
+
if (!this.cameraBinding) {
|
|
53
|
+
let t = globalThis.XRWebGLBinding;
|
|
54
|
+
if (!t) throw e("unsupported", "XRWebGLBinding is unavailable; use a browser with Raw Camera Access");
|
|
55
|
+
let r = new t(this.session, n);
|
|
56
|
+
if (typeof r.getCameraImage != "function") throw e("unsupported", "This XR binding has no camera images; use a runtime with getCameraImage");
|
|
57
|
+
this.cameraBinding = r;
|
|
58
|
+
}
|
|
59
|
+
let i = this.cameraBinding.getCameraImage(r);
|
|
60
|
+
return i ? {
|
|
61
|
+
camera: r,
|
|
62
|
+
texture: i,
|
|
63
|
+
context: n
|
|
64
|
+
} : null;
|
|
65
|
+
}
|
|
66
|
+
dispose() {
|
|
67
|
+
if (!this.disposed) {
|
|
68
|
+
this.disposed = !0, !this.ended && this.xr.session === this.session && typeof this.initial == "number" && (this.layer.fixedFoveation = this.initial);
|
|
69
|
+
for (let e of this.stops) e();
|
|
70
|
+
this.stops.length = 0, this.cameraBinding = void 0;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
[Symbol.dispose ?? Symbol()]() {
|
|
74
|
+
this.dispose();
|
|
75
|
+
}
|
|
76
|
+
};
|
|
77
|
+
//#endregion
|
|
78
|
+
export { n as XRPerformance, t as performanceSessionOptions };
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
/// <reference lib="esnext.disposable" preserve="true" />
|
|
2
|
+
import type { Mesh } from '@margoengine/core';
|
|
3
|
+
// @ts-ignore optional peer: its types apply when it is installed
|
|
4
|
+
import type { PickHit } from '@margoengine/selection';
|
|
5
|
+
import type { XRController } from './controller.js';
|
|
6
|
+
import type { XRFrameState, XRInputSource, XRNativeSession } from './types.js';
|
|
7
|
+
/** XR interaction modes, in default priority order: touch, grab, ray. */
|
|
8
|
+
export type XRPointerType = 'ray' | 'grab' | 'touch';
|
|
9
|
+
/** A selection picker; SpatialPicking from @margoengine/selection/spatial implements it. */
|
|
10
|
+
export interface XRPointerPicker {
|
|
11
|
+
ray(origin: ArrayLike<number>, direction: ArrayLike<number>, near?: number, far?: number, signal?: AbortSignal): Promise<PickHit | null>;
|
|
12
|
+
sphere(center: ArrayLike<number>, radius: number, signal?: AbortSignal): Promise<PickHit | null>;
|
|
13
|
+
}
|
|
14
|
+
/** The session and frame subscription of an XR manager. */
|
|
15
|
+
export interface XRPointerHost {
|
|
16
|
+
readonly session: XRNativeSession | null;
|
|
17
|
+
readonly controllers: readonly XRController[];
|
|
18
|
+
onFrame(callback: (state: XRFrameState) => void): () => void;
|
|
19
|
+
}
|
|
20
|
+
/** The same event vocabulary for controller rays, grip proximity and fingertip touch. */
|
|
21
|
+
export interface XRPointerEvent {
|
|
22
|
+
type: 'pointerover' | 'pointerout' | 'pointermove' | 'pointerdown' | 'pointerup' | 'pointercancel' | 'click';
|
|
23
|
+
pointerId: number;
|
|
24
|
+
pointerType: XRPointerType;
|
|
25
|
+
inputSource: XRInputSource;
|
|
26
|
+
controller: XRController;
|
|
27
|
+
hit: PickHit;
|
|
28
|
+
target: PickHit['mesh'];
|
|
29
|
+
buttons: number;
|
|
30
|
+
readonly stopped: boolean;
|
|
31
|
+
stopPropagation(): void;
|
|
32
|
+
/** Capture redirects subsequent events through release, even when the pointer misses. */
|
|
33
|
+
setPointerCapture(target?: Mesh): void;
|
|
34
|
+
releasePointerCapture(target?: Mesh): void;
|
|
35
|
+
hasPointerCapture(target?: Mesh): boolean;
|
|
36
|
+
}
|
|
37
|
+
/** Options are copied at attachment. Distances are metres. */
|
|
38
|
+
export interface XRPointerOptions {
|
|
39
|
+
/** Receives the hit event. React bindings bubble it through mesh parents. */
|
|
40
|
+
onEvent(event: XRPointerEvent): void;
|
|
41
|
+
/** Required error receiver for an attached asynchronous picker. */
|
|
42
|
+
onError(error: unknown): void;
|
|
43
|
+
/** Default ['touch', 'grab', 'ray']; the first mode with a hit wins per input. */
|
|
44
|
+
types?: readonly XRPointerType[];
|
|
45
|
+
/** Fingertip sphere radius, added to the tracked joint radius. Default 0.01. */
|
|
46
|
+
touchRadius?: number;
|
|
47
|
+
/** Grip sphere radius. Default 0.08. */
|
|
48
|
+
grabRadius?: number;
|
|
49
|
+
/** Ray range. Default 0.0001 and 1000. */
|
|
50
|
+
near?: number;
|
|
51
|
+
far?: number;
|
|
52
|
+
/** Aborting detaches listeners and cancels all active pointers. */
|
|
53
|
+
signal?: AbortSignal;
|
|
54
|
+
}
|
|
55
|
+
/** One stable pointer per input source, with mode priority, hover, press/release/click and capture. */
|
|
56
|
+
export declare class XRPointerEvents {
|
|
57
|
+
private readonly xr;
|
|
58
|
+
private readonly picker;
|
|
59
|
+
private readonly options;
|
|
60
|
+
private readonly stop;
|
|
61
|
+
private readonly pointers;
|
|
62
|
+
private readonly native;
|
|
63
|
+
private session;
|
|
64
|
+
private last;
|
|
65
|
+
private queue;
|
|
66
|
+
private running;
|
|
67
|
+
private pending;
|
|
68
|
+
private disposed;
|
|
69
|
+
private generation;
|
|
70
|
+
private readonly types;
|
|
71
|
+
private readonly touchRadius;
|
|
72
|
+
private readonly grabRadius;
|
|
73
|
+
private readonly near;
|
|
74
|
+
private readonly far;
|
|
75
|
+
static attach(xr: XRPointerHost, picker: XRPointerPicker, options: XRPointerOptions): XRPointerEvents;
|
|
76
|
+
private constructor();
|
|
77
|
+
private bind;
|
|
78
|
+
private unbind;
|
|
79
|
+
private readonly action;
|
|
80
|
+
private readonly sources;
|
|
81
|
+
private readonly ended;
|
|
82
|
+
private readonly abort;
|
|
83
|
+
private enqueue;
|
|
84
|
+
private drain;
|
|
85
|
+
private pick;
|
|
86
|
+
private update;
|
|
87
|
+
private fire;
|
|
88
|
+
private finish;
|
|
89
|
+
private cancel;
|
|
90
|
+
/** Cancels active pointers, removes session/frame/signal listeners and ignores pending readbacks. */
|
|
91
|
+
dispose(): void;
|
|
92
|
+
[Symbol.dispose](): void;
|
|
93
|
+
}
|
package/dist/pointer.js
ADDED
|
@@ -0,0 +1,205 @@
|
|
|
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, t } from "./errors-XvcwUnU4.js";
|
|
5
|
+
import { meshRuntime as n } from "@margoengine/core/extension";
|
|
6
|
+
//#region src/pointer.ts
|
|
7
|
+
var r = 1e3, i = class i {
|
|
8
|
+
static attach(e, t, n) {__margoAssertCore(e,__margoCoreRequirement("1.0.0",import.meta.url));
|
|
9
|
+
return n.signal?.throwIfAborted(), new i(e, t, n);
|
|
10
|
+
}
|
|
11
|
+
constructor(t, n, r) {
|
|
12
|
+
if (this.xr = t, this.picker = n, this.options = r, this.pointers = /* @__PURE__ */ new Map(), this.native = /* @__PURE__ */ new Map(), this.session = null, this.last = [], this.queue = [], this.running = !1, this.pending = new AbortController(), this.disposed = !1, this.generation = 0, this.action = (e) => {
|
|
13
|
+
let t = e.inputSource;
|
|
14
|
+
if (!t) return;
|
|
15
|
+
let n = this.native.get(t) ?? {};
|
|
16
|
+
e.type.startsWith("select") ? n.select = e.type === "selectstart" : n.squeeze = e.type === "squeezestart", this.native.set(t, n), this.enqueue(this.last.map((e) => e.source === t ? {
|
|
17
|
+
...e,
|
|
18
|
+
...n
|
|
19
|
+
} : e));
|
|
20
|
+
}, this.sources = () => {
|
|
21
|
+
let e = new Set(this.session?.inputSources);
|
|
22
|
+
for (let [t, n] of this.pointers) e.has(t) || (this.finish(n), this.pointers.delete(t), this.native.delete(t));
|
|
23
|
+
this.generation++, this.last = this.last.filter((t) => e.has(t.source)), this.queue = this.queue.map((t) => t.filter((t) => e.has(t.source)));
|
|
24
|
+
}, this.ended = () => {
|
|
25
|
+
this.unbind(), this.cancel();
|
|
26
|
+
}, this.abort = () => {
|
|
27
|
+
this.dispose();
|
|
28
|
+
}, this.types = [...r.types ?? [
|
|
29
|
+
"touch",
|
|
30
|
+
"grab",
|
|
31
|
+
"ray"
|
|
32
|
+
]], this.touchRadius = r.touchRadius ?? .01, this.grabRadius = r.grabRadius ?? .08, this.near = r.near ?? 1e-4, this.far = r.far ?? 1e3, typeof r.onEvent != "function" || typeof r.onError != "function" || !this.types.length || new Set(this.types).size !== this.types.length || this.types.some((e) => ![
|
|
33
|
+
"touch",
|
|
34
|
+
"grab",
|
|
35
|
+
"ray"
|
|
36
|
+
].includes(e)) || ![
|
|
37
|
+
this.touchRadius,
|
|
38
|
+
this.grabRadius,
|
|
39
|
+
this.near,
|
|
40
|
+
this.far
|
|
41
|
+
].every(Number.isFinite) || this.touchRadius < 0 || this.grabRadius < 0 || this.near <= 0 || this.far <= this.near) throw e("XRPointerEvents needs event/error receivers, unique pointer types and valid distances; pass valid pointer options", "arguments");
|
|
42
|
+
this.bind(), this.stop = t.onFrame(() => {
|
|
43
|
+
this.bind();
|
|
44
|
+
let e = this.xr.controllers.map((e) => {
|
|
45
|
+
let t = e.source, n = this.native.get(t), r = t.hand ? [...t.hand.keys()].indexOf("index-finger-tip") : -1, i = t.hand ? [...t.hand.keys()].indexOf("thumb-tip") : -1, a = r >= 0 && e.handTracked[r] ? e.handMatrices.slice(r * 16, r * 16 + 16) : null, o = i >= 0 && e.handTracked[i] ? e.handMatrices.subarray(i * 16, i * 16 + 16) : null, s = !!a && !!o && Math.hypot(a[12] - o[12], a[13] - o[13], a[14] - o[14]) < .025;
|
|
46
|
+
return {
|
|
47
|
+
controller: e,
|
|
48
|
+
source: t,
|
|
49
|
+
ray: e.targetRay ? e.targetRayMatrix.slice() : null,
|
|
50
|
+
grip: e.grip ? e.gripMatrix.slice() : null,
|
|
51
|
+
tip: a,
|
|
52
|
+
tipRadius: r >= 0 ? e.handRadii[r] : 0,
|
|
53
|
+
select: n?.select ?? (!!e.buttons[1] || s),
|
|
54
|
+
squeeze: n?.squeeze ?? (!!e.buttons[4] || s)
|
|
55
|
+
};
|
|
56
|
+
});
|
|
57
|
+
this.last = e, this.enqueue(e);
|
|
58
|
+
}), r.signal?.addEventListener("abort", this.abort, { once: !0 });
|
|
59
|
+
}
|
|
60
|
+
bind() {
|
|
61
|
+
if (this.session !== this.xr.session) {
|
|
62
|
+
this.unbind(), this.cancel(), this.session = this.xr.session;
|
|
63
|
+
for (let e of [
|
|
64
|
+
"selectstart",
|
|
65
|
+
"selectend",
|
|
66
|
+
"squeezestart",
|
|
67
|
+
"squeezeend"
|
|
68
|
+
]) this.session?.addEventListener(e, this.action);
|
|
69
|
+
this.session?.addEventListener("end", this.ended), this.session?.addEventListener("inputsourceschange", this.sources);
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
unbind() {
|
|
73
|
+
for (let e of [
|
|
74
|
+
"selectstart",
|
|
75
|
+
"selectend",
|
|
76
|
+
"squeezestart",
|
|
77
|
+
"squeezeend"
|
|
78
|
+
]) this.session?.removeEventListener(e, this.action);
|
|
79
|
+
this.session?.removeEventListener("end", this.ended), this.session?.removeEventListener("inputsourceschange", this.sources), this.session = null, this.native.clear();
|
|
80
|
+
}
|
|
81
|
+
enqueue(e) {
|
|
82
|
+
if (this.disposed) return;
|
|
83
|
+
let t = this.queue.at(-1);
|
|
84
|
+
t && t.length === e.length && t.every((t, n) => t.source === e[n].source && t.select === e[n].select && t.squeeze === e[n].squeeze) ? this.queue[this.queue.length - 1] = e : this.queue.push(e), this.drain();
|
|
85
|
+
}
|
|
86
|
+
async drain() {
|
|
87
|
+
if (!this.running) {
|
|
88
|
+
this.running = !0;
|
|
89
|
+
try {
|
|
90
|
+
for (; !this.disposed && this.queue.length;) {
|
|
91
|
+
let e = this.queue.shift(), t = this.generation, n = new Set(e.map((e) => e.source));
|
|
92
|
+
for (let [e, t] of this.pointers) n.has(e) || (this.finish(t), this.pointers.delete(e));
|
|
93
|
+
for (let n of e) {
|
|
94
|
+
let e = this.pointers.get(n.source);
|
|
95
|
+
e || (e = {
|
|
96
|
+
id: r++,
|
|
97
|
+
sample: n,
|
|
98
|
+
type: "ray",
|
|
99
|
+
hover: null,
|
|
100
|
+
down: null,
|
|
101
|
+
capture: null,
|
|
102
|
+
pressed: !1
|
|
103
|
+
}, this.pointers.set(n.source, e));
|
|
104
|
+
let i = this.pending.signal, a;
|
|
105
|
+
try {
|
|
106
|
+
a = await this.pick(n, e, i);
|
|
107
|
+
} catch (e) {
|
|
108
|
+
if (i.aborted) break;
|
|
109
|
+
throw e;
|
|
110
|
+
}
|
|
111
|
+
if (this.disposed || t !== this.generation) break;
|
|
112
|
+
e.sample = n, this.update(e, a);
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
} catch (e) {
|
|
116
|
+
this.cancel(), this.options.onError(e);
|
|
117
|
+
} finally {
|
|
118
|
+
this.running = !1, this.queue.length && !this.disposed && this.drain();
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
async pick(e, t, n) {
|
|
123
|
+
for (let r of t.pressed ? [t.type] : this.types) {
|
|
124
|
+
let t = null;
|
|
125
|
+
if (r === "ray" && e.ray && (t = await this.picker.ray(e.ray.subarray(12, 15), [
|
|
126
|
+
-e.ray[8],
|
|
127
|
+
-e.ray[9],
|
|
128
|
+
-e.ray[10]
|
|
129
|
+
], this.near, this.far, n)), r === "grab" && e.grip && (t = await this.picker.sphere(e.grip.subarray(12, 15), this.grabRadius, n)), r === "touch" && e.tip && (t = await this.picker.sphere(e.tip.subarray(12, 15), this.touchRadius + e.tipRadius, n)), t) return {
|
|
130
|
+
type: r,
|
|
131
|
+
hit: t
|
|
132
|
+
};
|
|
133
|
+
}
|
|
134
|
+
return {
|
|
135
|
+
type: t.type,
|
|
136
|
+
hit: null
|
|
137
|
+
};
|
|
138
|
+
}
|
|
139
|
+
update(e, t) {
|
|
140
|
+
let { sample: n } = e;
|
|
141
|
+
if (!(t.type === "ray" ? n.ray : t.type === "grab" ? n.grip : n.tip)) {
|
|
142
|
+
this.finish(e);
|
|
143
|
+
return;
|
|
144
|
+
}
|
|
145
|
+
let r = e.capture ?? t.hit;
|
|
146
|
+
if ((e.hover?.mesh !== r?.mesh || e.hover?.instanceId !== r?.instanceId || e.type !== t.type) && (e.hover && this.fire("pointerout", e, e.hover), this.disposed || (e.hover = r, e.type = t.type, r && this.fire("pointerover", e, r), this.disposed)) || (r && this.fire("pointermove", e, r), this.disposed)) return;
|
|
147
|
+
let i = t.type === "touch" ? !!t.hit : t.type === "grab" ? n.squeeze : n.select;
|
|
148
|
+
if (i && !e.pressed) e.pressed = !0, e.down = r, r && this.fire("pointerdown", e, r);
|
|
149
|
+
else if (!i && e.pressed) {
|
|
150
|
+
e.pressed = !1;
|
|
151
|
+
let n = e.capture, i = r ?? (t.type === "touch" ? e.down : null);
|
|
152
|
+
if (i && this.fire("pointerup", e, i), this.disposed) return;
|
|
153
|
+
i && (n || e.down?.mesh === i.mesh && e.down?.instanceId === i.instanceId) && this.fire("click", e, i), e.down = e.capture = null;
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
fire(e, t, r) {
|
|
157
|
+
let i = !1;
|
|
158
|
+
this.options.onEvent({
|
|
159
|
+
type: e,
|
|
160
|
+
pointerId: t.id,
|
|
161
|
+
pointerType: t.type,
|
|
162
|
+
controller: t.sample.controller,
|
|
163
|
+
inputSource: t.sample.source,
|
|
164
|
+
hit: r,
|
|
165
|
+
target: r.mesh,
|
|
166
|
+
buttons: +t.pressed,
|
|
167
|
+
get stopped() {
|
|
168
|
+
return i;
|
|
169
|
+
},
|
|
170
|
+
stopPropagation() {
|
|
171
|
+
i = !0;
|
|
172
|
+
},
|
|
173
|
+
setPointerCapture(e = r.mesh) {
|
|
174
|
+
t.pressed && (t.capture = {
|
|
175
|
+
...r,
|
|
176
|
+
mesh: n(e)
|
|
177
|
+
});
|
|
178
|
+
},
|
|
179
|
+
releasePointerCapture(e = r.mesh) {
|
|
180
|
+
t.capture?.mesh === e && (t.capture = null);
|
|
181
|
+
},
|
|
182
|
+
hasPointerCapture(e = r.mesh) {
|
|
183
|
+
return t.capture?.mesh === e;
|
|
184
|
+
}
|
|
185
|
+
});
|
|
186
|
+
}
|
|
187
|
+
finish(e) {
|
|
188
|
+
let t = e.pressed ? e.capture ?? e.down : null, n = e.hover;
|
|
189
|
+
e.pressed = !1, e.down = e.hover = e.capture = null, t && this.fire("pointercancel", e, t), n && this.fire("pointerout", e, n);
|
|
190
|
+
}
|
|
191
|
+
cancel() {
|
|
192
|
+
this.pending.abort(t("state", "XR pointer session changed; attach to a live session")), this.pending = new AbortController(), this.generation++, this.queue = [], this.last = [];
|
|
193
|
+
let e = [...this.pointers.values()];
|
|
194
|
+
this.pointers.clear();
|
|
195
|
+
for (let t of e) this.finish(t);
|
|
196
|
+
}
|
|
197
|
+
dispose() {
|
|
198
|
+
this.disposed || (this.disposed = !0, this.stop(), this.unbind(), this.cancel(), this.options.signal?.removeEventListener("abort", this.abort));
|
|
199
|
+
}
|
|
200
|
+
[Symbol.dispose]() {
|
|
201
|
+
this.dispose();
|
|
202
|
+
}
|
|
203
|
+
};
|
|
204
|
+
//#endregion
|
|
205
|
+
export { i as XRPointerEvents };
|