@margoengine/xr 0.1.0 → 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 -201
- package/NOTICE +4 -4
- package/README.md +843 -592
- package/dist/ar-extra.d.ts +66 -1
- package/dist/ar-extra.js +81 -13
- package/dist/eyes.d.ts +21 -0
- package/dist/gizmo.js +2 -2
- package/dist/index.js +290 -144
- package/dist/interaction.js +2 -2
- package/dist/layers-BTUTAawG.js +3 -3
- package/dist/locomotion.js +2 -2
- package/dist/models.js +3 -3
- package/dist/near.js +7 -7
- package/dist/performance.js +2 -2
- package/dist/pointer.js +2 -2
- package/dist/scene-owner-BRObO-WJ.js +2 -2
- package/dist/scene-scope-DufD_3B6.js +3 -3
- package/dist/session.js +2 -2
- package/dist/shared/xr-run.d.ts +64 -64
- package/package.json +154 -153
package/dist/index.js
CHANGED
|
@@ -1,35 +1,48 @@
|
|
|
1
|
-
// @margo/core-built
|
|
1
|
+
// @margo/core-built 1.0.0
|
|
2
2
|
import {coreRequirement as __margoCoreRequirement,assertCore as __margoAssertCore} from '@margoengine/core/extension';
|
|
3
|
-
const r=(engine,version="
|
|
3
|
+
const r=(engine,version="1.0.0",source=import.meta.url)=>__margoRuntime(engine,version,source);
|
|
4
4
|
import { n as e } from "./errors-XvcwUnU4.js";
|
|
5
5
|
import { t } from "./scene-scope-DufD_3B6.js";
|
|
6
6
|
import { Camera as n, engineRuntime as __margoRuntime } from "@margoengine/core/extension";
|
|
7
7
|
import { Engine as i } from "@margoengine/core";
|
|
8
8
|
import { MargoError as a } from "@margoengine/math/errors";
|
|
9
|
+
function o(e) {
|
|
10
|
+
return e?.adapterInfo?.vendor === "margo-webgl2" ? "webgl2" : "native";
|
|
11
|
+
}
|
|
9
12
|
//#endregion
|
|
10
13
|
//#region ../shared/view-rect.js
|
|
11
|
-
function o() {
|
|
12
|
-
return globalThis[Symbol.for("margoengine.layers")];
|
|
13
|
-
}
|
|
14
14
|
function s(e) {
|
|
15
|
-
return
|
|
15
|
+
return e?.view?.viewport;
|
|
16
16
|
}
|
|
17
17
|
function c(e, t) {
|
|
18
|
+
let n = s(t);
|
|
19
|
+
if (!n) return;
|
|
20
|
+
e.setViewport(n[0], n[1], n[2], n[3], 0, 1);
|
|
21
|
+
let r = t.view.scissor ?? n;
|
|
22
|
+
e.setScissorRect(r[0], r[1], r[2], r[3]);
|
|
23
|
+
}
|
|
24
|
+
function l() {
|
|
25
|
+
return globalThis[Symbol.for("margoengine.layers")];
|
|
26
|
+
}
|
|
27
|
+
function u(e) {
|
|
28
|
+
return l()?.maskOf(e) ?? 1;
|
|
29
|
+
}
|
|
30
|
+
function d(e, t) {
|
|
18
31
|
if (!Number.isInteger(e) || e < 0 || e > 4294967295) throw RangeError(`${t} ${e} is not a 32-bit layer mask; pass an integer from 0 to 0xffffffff`);
|
|
19
32
|
return e >>> 0;
|
|
20
33
|
}
|
|
21
34
|
//#endregion
|
|
22
35
|
//#region ../shared/mesh-source.js
|
|
23
|
-
var
|
|
24
|
-
globalThis[
|
|
25
|
-
var
|
|
26
|
-
globalThis[
|
|
27
|
-
function
|
|
36
|
+
var f = Symbol.for("margoengine.mesh-sources.v1"), p = globalThis[f] ?? /* @__PURE__ */ new WeakMap();
|
|
37
|
+
globalThis[f] || Object.defineProperty(globalThis, f, { value: p });
|
|
38
|
+
var m = Symbol.for("margoengine.geometry-surfaces.v1"), h = globalThis[m] ?? /* @__PURE__ */ new WeakMap();
|
|
39
|
+
globalThis[m] || Object.defineProperty(globalThis, m, { value: h });
|
|
40
|
+
function g(e) {
|
|
28
41
|
if (e?.geometry) return {
|
|
29
42
|
kind: "core",
|
|
30
43
|
geometry: e.geometry
|
|
31
44
|
};
|
|
32
|
-
let t = e &&
|
|
45
|
+
let t = e && p.get(e);
|
|
33
46
|
if (!t || t.model.disposed) return null;
|
|
34
47
|
try {
|
|
35
48
|
e.check?.();
|
|
@@ -43,21 +56,21 @@ function p(e) {
|
|
|
43
56
|
}
|
|
44
57
|
//#endregion
|
|
45
58
|
//#region ../shared/unit-light.js
|
|
46
|
-
function
|
|
59
|
+
function _(e, t, n) {
|
|
47
60
|
let r = Math.hypot(n[0], n[1], n[2]);
|
|
48
61
|
e[t] = n[0] / r, e[t + 1] = n[1] / r, e[t + 2] = n[2] / r, e[t + 3] = n[3];
|
|
49
62
|
}
|
|
50
63
|
//#endregion
|
|
51
64
|
//#region ../shared/hdr-format.js
|
|
52
|
-
var
|
|
65
|
+
var v = (e) => typeof e == "string" && e.endsWith("float");
|
|
53
66
|
//#endregion
|
|
54
67
|
//#region ../shared/pbr-transmission.js
|
|
55
|
-
function
|
|
68
|
+
function y(e) {
|
|
56
69
|
return !!e.tint && !e.renderState && !e.extension?.replacesPhysical;
|
|
57
70
|
}
|
|
58
71
|
//#endregion
|
|
59
72
|
//#region src/renderer.ts
|
|
60
|
-
var
|
|
73
|
+
var b = class {
|
|
61
74
|
constructor(e) {
|
|
62
75
|
this.device = e, this.resources = /* @__PURE__ */ new Map(), this.eyes = [], this.used = /* @__PURE__ */ new Set(), this.pbr = /* @__PURE__ */ new Map();
|
|
63
76
|
}
|
|
@@ -101,10 +114,10 @@ var _ = class {
|
|
|
101
114
|
}
|
|
102
115
|
});
|
|
103
116
|
}
|
|
104
|
-
render(e, t, n, r, i, o,
|
|
105
|
-
let
|
|
117
|
+
render(e, t, n, r, i, o, s) {
|
|
118
|
+
let c = this.device, l = this.pipeline, d = /* @__PURE__ */ new Set();
|
|
106
119
|
for (let t of e.meshes.values()) {
|
|
107
|
-
let e =
|
|
120
|
+
let e = g(t);
|
|
108
121
|
e?.kind === "pbr" && d.add(e.source.model);
|
|
109
122
|
}
|
|
110
123
|
for (let [e, t] of this.pbr) if (!d.has(e) || e.disposed) {
|
|
@@ -115,18 +128,18 @@ var _ = class {
|
|
|
115
128
|
let f = this.used;
|
|
116
129
|
f.clear();
|
|
117
130
|
for (let e of this.resources.values()) e.count = 0;
|
|
118
|
-
for (let t of e.meshes.values()) if (t.geometry && e.store.visible[t.id] && (
|
|
131
|
+
for (let t of e.meshes.values()) if (t.geometry && e.store.visible[t.id] && (s === void 0 || u(t) & s)) {
|
|
119
132
|
let n = t.geometry;
|
|
120
133
|
f.add(n);
|
|
121
134
|
let r = this.resources.get(n);
|
|
122
135
|
if (!r) {
|
|
123
136
|
let t = [], i = (e, n) => {
|
|
124
|
-
if (e.byteLength >
|
|
125
|
-
let r =
|
|
137
|
+
if (e.byteLength > c.limits.maxBufferSize) throw RangeError("XR buffer exceeds device limit; use a smaller geometry or split it");
|
|
138
|
+
let r = c.createBuffer({
|
|
126
139
|
size: e.byteLength,
|
|
127
140
|
usage: n | GPUBufferUsage.COPY_DST
|
|
128
141
|
});
|
|
129
|
-
return t.push(r),
|
|
142
|
+
return t.push(r), c.queue.writeBuffer(r, 0, e), r;
|
|
130
143
|
};
|
|
131
144
|
try {
|
|
132
145
|
let t = new Float32Array(e.store.capacity * 24);
|
|
@@ -144,8 +157,8 @@ var _ = class {
|
|
|
144
157
|
}
|
|
145
158
|
if (r.data.length < e.store.capacity * 24) {
|
|
146
159
|
let t = new Float32Array(e.store.capacity * 24);
|
|
147
|
-
if (t.byteLength >
|
|
148
|
-
let n =
|
|
160
|
+
if (t.byteLength > c.limits.maxBufferSize) throw RangeError("XR buffer exceeds device limit; lower the engine store capacity");
|
|
161
|
+
let n = c.createBuffer({
|
|
149
162
|
size: t.byteLength,
|
|
150
163
|
usage: GPUBufferUsage.VERTEX | GPUBufferUsage.COPY_DST
|
|
151
164
|
});
|
|
@@ -154,57 +167,57 @@ var _ = class {
|
|
|
154
167
|
let i = r.count++ * 24;
|
|
155
168
|
r.data.set(t.matrix, i), r.data.set(t.material.data, i + 16);
|
|
156
169
|
}
|
|
157
|
-
for (let [e, t] of this.resources) f.has(e) ?
|
|
158
|
-
let
|
|
159
|
-
for (let
|
|
160
|
-
let d = r.views[
|
|
161
|
-
if (!
|
|
162
|
-
let e =
|
|
170
|
+
for (let [e, t] of this.resources) f.has(e) ? c.queue.writeBuffer(t.instance, 0, t.data, 0, t.count * 24) : (t.vertex.destroy(), t.index.destroy(), t.instance.destroy(), this.resources.delete(e));
|
|
171
|
+
let p = c.createCommandEncoder(), m = [];
|
|
172
|
+
for (let u = 0; u < r.views.length; u++) {
|
|
173
|
+
let d = r.views[u], f = t.getViewSubImage(n, d), h = f.getViewDescriptor(), g = this.eyes[u];
|
|
174
|
+
if (!g) {
|
|
175
|
+
let e = c.createBuffer({
|
|
163
176
|
size: 128,
|
|
164
177
|
usage: GPUBufferUsage.UNIFORM | GPUBufferUsage.COPY_DST
|
|
165
178
|
});
|
|
166
179
|
try {
|
|
167
|
-
|
|
180
|
+
g = {
|
|
168
181
|
uniform: e,
|
|
169
|
-
group:
|
|
170
|
-
layout:
|
|
182
|
+
group: c.createBindGroup({
|
|
183
|
+
layout: l.getBindGroupLayout(0),
|
|
171
184
|
entries: [{
|
|
172
185
|
binding: 0,
|
|
173
186
|
resource: { buffer: e }
|
|
174
187
|
}]
|
|
175
188
|
}),
|
|
176
189
|
data: /* @__PURE__ */ new Float32Array(32)
|
|
177
|
-
}, this.eyes.push(
|
|
190
|
+
}, this.eyes.push(g);
|
|
178
191
|
} catch (t) {
|
|
179
192
|
throw e.destroy(), t;
|
|
180
193
|
}
|
|
181
194
|
}
|
|
182
|
-
let y = t.projection?.(d) ?? d.projectionMatrix, b = d.transform.inverse.matrix, x =
|
|
195
|
+
let y = t.projection?.(d) ?? d.projectionMatrix, b = d.transform.inverse.matrix, x = g.data;
|
|
183
196
|
for (let e = 0; e < 4; e++) for (let t = 0; t < 4; t++) x[e * 4 + t] = y[t] * b[e * 4] + y[4 + t] * b[e * 4 + 1] + y[8 + t] * b[e * 4 + 2] + y[12 + t] * b[e * 4 + 3];
|
|
184
197
|
let S = d.transform.position;
|
|
185
|
-
if (x[16] = S.x, x[17] = S.y, x[18] = S.z, x[19] = 1,
|
|
186
|
-
if (
|
|
187
|
-
|
|
198
|
+
if (x[16] = S.x, x[17] = S.y, x[18] = S.z, x[19] = 1, _(x, 20, e.lighting.direction), x.set(e.lighting.radiance, 24), x.fill(1, 28, 31), e.lighting.ambient && x.set(e.lighting.ambient, 28), c.queue.writeBuffer(g.uniform, 0, x), !f.depthStencilTexture && !t.present) throw new a("unsupported", "XR projection layer has no depth texture; use a runtime that provides a depth buffer for the layer");
|
|
199
|
+
if (g.color?.width !== f.colorTexture.width || g.color?.height !== f.colorTexture.height || g.color?.format !== f.colorTexture.format) {
|
|
200
|
+
g.color?.destroy(), g.depth?.destroy(), g.color = void 0, g.depth = void 0, g.colorView = g.depthView = g.depthGroup = void 0;
|
|
188
201
|
try {
|
|
189
|
-
|
|
202
|
+
g.color = c.createTexture({
|
|
190
203
|
size: [f.colorTexture.width, f.colorTexture.height],
|
|
191
204
|
format: f.colorTexture.format,
|
|
192
205
|
sampleCount: 4,
|
|
193
206
|
usage: GPUTextureUsage.RENDER_ATTACHMENT
|
|
194
|
-
}),
|
|
207
|
+
}), g.depth = c.createTexture({
|
|
195
208
|
size: [f.colorTexture.width, f.colorTexture.height],
|
|
196
209
|
format: "depth24plus",
|
|
197
210
|
sampleCount: 4,
|
|
198
211
|
usage: GPUTextureUsage.RENDER_ATTACHMENT | GPUTextureUsage.TEXTURE_BINDING
|
|
199
|
-
}),
|
|
212
|
+
}), g.colorView = g.color.createView(), g.depthView = g.depth.createView(), g.depthGroup = c.createBindGroup({
|
|
200
213
|
layout: this.depthPipeline.getBindGroupLayout(0),
|
|
201
214
|
entries: [{
|
|
202
215
|
binding: 0,
|
|
203
|
-
resource:
|
|
216
|
+
resource: g.depthView
|
|
204
217
|
}]
|
|
205
218
|
});
|
|
206
219
|
} catch (e) {
|
|
207
|
-
throw
|
|
220
|
+
throw g.color?.destroy(), g.depth?.destroy(), g.color = g.depth = void 0, g.colorView = g.depthView = g.depthGroup = void 0, e;
|
|
208
221
|
}
|
|
209
222
|
}
|
|
210
223
|
let C = Object.assign(Object.create(e.camera), {
|
|
@@ -215,10 +228,10 @@ var _ = class {
|
|
|
215
228
|
S.z
|
|
216
229
|
]
|
|
217
230
|
});
|
|
218
|
-
if (
|
|
231
|
+
if (s !== void 0) {
|
|
219
232
|
let e = f.viewport;
|
|
220
|
-
|
|
221
|
-
id: `xr-${
|
|
233
|
+
g.view = {
|
|
234
|
+
id: `xr-${u}`,
|
|
222
235
|
camera: C,
|
|
223
236
|
viewport: [
|
|
224
237
|
e.x,
|
|
@@ -226,22 +239,22 @@ var _ = class {
|
|
|
226
239
|
e.width,
|
|
227
240
|
e.height
|
|
228
241
|
],
|
|
229
|
-
index:
|
|
242
|
+
index: u,
|
|
230
243
|
count: r.views.length,
|
|
231
|
-
layers:
|
|
244
|
+
layers: s
|
|
232
245
|
};
|
|
233
246
|
}
|
|
234
247
|
let w = {
|
|
235
248
|
...this.context,
|
|
236
|
-
...
|
|
249
|
+
...s === void 0 ? {} : { view: g.view },
|
|
237
250
|
backend: "webgpu",
|
|
238
|
-
device:
|
|
239
|
-
encoder:
|
|
251
|
+
device: c,
|
|
252
|
+
encoder: p,
|
|
240
253
|
width: f.colorTexture.width,
|
|
241
254
|
height: f.colorTexture.height,
|
|
242
255
|
format: f.colorTexture.format,
|
|
243
|
-
color:
|
|
244
|
-
depth:
|
|
256
|
+
color: g.colorView,
|
|
257
|
+
depth: g.depthView,
|
|
245
258
|
camera: C,
|
|
246
259
|
descriptor: {
|
|
247
260
|
...this.context.descriptor,
|
|
@@ -254,13 +267,13 @@ var _ = class {
|
|
|
254
267
|
}
|
|
255
268
|
};
|
|
256
269
|
for (let [e, t] of this.pbr) {
|
|
257
|
-
let n = t[
|
|
258
|
-
n || (n = e.createRenderer(
|
|
270
|
+
let n = t[u];
|
|
271
|
+
n || (n = e.createRenderer(c), t[u] = n), n.prepare(w);
|
|
259
272
|
}
|
|
260
|
-
let T = !
|
|
261
|
-
let t =
|
|
273
|
+
let T = !v(f.colorTexture.format) && this.glass(), E = f.colorTexture.createView(h), D = f.viewport, O = (e) => {
|
|
274
|
+
let t = p.beginRenderPass({
|
|
262
275
|
colorAttachments: [{
|
|
263
|
-
view:
|
|
276
|
+
view: g.colorView,
|
|
264
277
|
resolveTarget: E,
|
|
265
278
|
loadOp: e ? "clear" : "load",
|
|
266
279
|
storeOp: T ? "store" : "discard",
|
|
@@ -272,7 +285,7 @@ var _ = class {
|
|
|
272
285
|
}
|
|
273
286
|
}],
|
|
274
287
|
depthStencilAttachment: {
|
|
275
|
-
view:
|
|
288
|
+
view: g.depthView,
|
|
276
289
|
depthLoadOp: e ? "clear" : "load",
|
|
277
290
|
depthStoreOp: "store",
|
|
278
291
|
depthClearValue: 1
|
|
@@ -280,44 +293,44 @@ var _ = class {
|
|
|
280
293
|
});
|
|
281
294
|
return t.setViewport(D.x, D.y, D.width, D.height, 0, 1), t;
|
|
282
295
|
}, k = O(!0);
|
|
283
|
-
k.setPipeline(
|
|
296
|
+
k.setPipeline(l), k.setBindGroup(0, g.group);
|
|
284
297
|
for (let [e, t] of this.resources) t.count && (k.setVertexBuffer(0, t.vertex), k.setVertexBuffer(1, t.instance), k.setIndexBuffer(t.index, "uint32"), k.drawIndexed(e.indices.length, t.count));
|
|
285
298
|
let A = T ? { snapshot(e, t) {
|
|
286
|
-
return e.end(),
|
|
287
|
-
view:
|
|
299
|
+
return e.end(), p.beginRenderPass({ colorAttachments: [{
|
|
300
|
+
view: g.colorView,
|
|
288
301
|
resolveTarget: t.createView(),
|
|
289
302
|
loadOp: "load",
|
|
290
303
|
storeOp: "store"
|
|
291
304
|
}] }).end(), O(!1);
|
|
292
305
|
} } : void 0;
|
|
293
|
-
for (let e of this.pbr.values()) k = e[
|
|
306
|
+
for (let e of this.pbr.values()) k = e[u].draw(w, k, d.transform.inverse.matrix, A) ?? k;
|
|
294
307
|
if (k.end(), !f.depthStencilTexture) continue;
|
|
295
|
-
let j =
|
|
308
|
+
let j = p.beginRenderPass({
|
|
296
309
|
colorAttachments: [],
|
|
297
310
|
depthStencilAttachment: {
|
|
298
|
-
view: f.depthStencilTexture.createView(
|
|
311
|
+
view: f.depthStencilTexture.createView(h),
|
|
299
312
|
depthLoadOp: "clear",
|
|
300
313
|
depthStoreOp: "store",
|
|
301
314
|
depthClearValue: 1
|
|
302
315
|
}
|
|
303
316
|
});
|
|
304
|
-
j.setViewport(D.x, D.y, D.width, D.height, 0, 1), j.setPipeline(this.depthPipeline), j.setBindGroup(0,
|
|
305
|
-
let M = () =>
|
|
317
|
+
j.setViewport(D.x, D.y, D.width, D.height, 0, 1), j.setPipeline(this.depthPipeline), j.setBindGroup(0, g.depthGroup), j.draw(3), j.end();
|
|
318
|
+
let M = () => p.beginRenderPass({
|
|
306
319
|
colorAttachments: [],
|
|
307
320
|
depthStencilAttachment: {
|
|
308
|
-
view:
|
|
321
|
+
view: g.depthView,
|
|
309
322
|
depthLoadOp: "load",
|
|
310
323
|
depthStoreOp: "discard"
|
|
311
324
|
}
|
|
312
325
|
}).end();
|
|
313
|
-
o ?
|
|
326
|
+
o ? m.push(M) : M();
|
|
314
327
|
}
|
|
315
328
|
if (o) for (let [e, i] of r.views.entries()) {
|
|
316
329
|
let r = t.getViewSubImage(n, i);
|
|
317
330
|
if (o({
|
|
318
331
|
index: e,
|
|
319
|
-
device:
|
|
320
|
-
encoder:
|
|
332
|
+
device: c,
|
|
333
|
+
encoder: p,
|
|
321
334
|
image: r,
|
|
322
335
|
view: i,
|
|
323
336
|
scene: {
|
|
@@ -329,11 +342,11 @@ var _ = class {
|
|
|
329
342
|
binding: t
|
|
330
343
|
}) === !1) return;
|
|
331
344
|
}
|
|
332
|
-
for (let e of
|
|
333
|
-
|
|
345
|
+
for (let e of m) e();
|
|
346
|
+
c.queue.submit([p.finish()]), t.present?.(n);
|
|
334
347
|
}
|
|
335
348
|
glass() {
|
|
336
|
-
for (let e of this.pbr.keys()) for (let t of e.primitives) if (
|
|
349
|
+
for (let e of this.pbr.keys()) for (let t of e.primitives) if (y(t.material)) return !0;
|
|
337
350
|
return !1;
|
|
338
351
|
}
|
|
339
352
|
dispose() {
|
|
@@ -344,39 +357,101 @@ var _ = class {
|
|
|
344
357
|
for (let e of this.eyes) e.uniform.destroy(), e.color?.destroy(), e.depth?.destroy();
|
|
345
358
|
this.eyes.length = 0, this.pipeline = this.depthPipeline = void 0;
|
|
346
359
|
}
|
|
347
|
-
};
|
|
360
|
+
}, x = Symbol.for("margoengine.premultiplied-canvas");
|
|
361
|
+
function S(e, t) {
|
|
362
|
+
let n = e?.canvas;
|
|
363
|
+
if (!n) return !1;
|
|
364
|
+
let r = !!n[x];
|
|
365
|
+
return n[x] = t, r;
|
|
366
|
+
}
|
|
348
367
|
//#endregion
|
|
349
368
|
//#region src/eyes.ts
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
let
|
|
354
|
-
|
|
369
|
+
var C = "@vertex fn vertex(@builtin(vertex_index) i: u32) -> @builtin(position) vec4f {\n return vec4f(array<vec2f, 3>(vec2f(-1, -1), vec2f(3, -1), vec2f(-1, 3))[i], 0, 1);\n}\n@fragment fn fragment() -> @location(0) vec4f {\n return vec4f(1);\n}\n", w = "@vertex fn vertex(@builtin(vertex_index) i: u32) -> @builtin(position) vec4f {\n return vec4f(array<vec2f, 3>(vec2f(-1, -1), vec2f(3, -1), vec2f(-1, 3))[i], 0, 1);\n}\n@fragment fn fragment() -> @location(0) vec4f {\n return vec4f(0);\n}\n", T = 1e-6;
|
|
370
|
+
function E(e, t, n) {
|
|
371
|
+
let r = e > 1 ? "texture_depth_multisampled_2d" : "texture_depth_2d", i = "0.0";
|
|
372
|
+
for (let t = 0; t < e; t++) i += ` + select(0.0, 1.0, textureLoad(d, q, ${t}) > 0.0)`;
|
|
373
|
+
let a = n ? "textureLoad(k, q, 0).r" : `(${i}) / ${e}.0`, o = "textureLoad(d, q, 0)";
|
|
374
|
+
for (let t = 1; t < e; t++) o = `max(${o}, textureLoad(d, q, ${t}))`;
|
|
355
375
|
return `@group(0) @binding(0) var c: texture_2d<f32>;
|
|
356
|
-
@group(0) @binding(1) var d: ${
|
|
376
|
+
@group(0) @binding(1) var d: ${r};
|
|
357
377
|
@group(0) @binding(2) var<uniform> o: vec4i;
|
|
378
|
+
${n ? "@group(0) @binding(3) var k: texture_2d<f32>;" : ""}
|
|
358
379
|
@vertex fn vertex(@builtin(vertex_index) i: u32) -> @builtin(position) vec4f {
|
|
359
380
|
return vec4f(array<vec2f, 3>(vec2f(-1, -1), vec2f(3, -1), vec2f(-1, 3))[i], 0, 1);
|
|
360
381
|
}
|
|
361
382
|
@fragment fn color(@builtin(position) p: vec4f) -> @location(0) vec4f {
|
|
362
383
|
let q = vec2i(p.xy) - o.xy;
|
|
363
384
|
let c = textureLoad(c, q, 0);
|
|
364
|
-
${t ? `return vec4f(c.rgb, clamp(max((${
|
|
385
|
+
${t ? `return vec4f(c.rgb, clamp(max(max(${a}, c.a), max(c.r, max(c.g, c.b))), 0.0, 1.0));` : "return vec4f(c.rgb, 1.0);"}
|
|
365
386
|
}
|
|
366
387
|
@fragment fn depth(@builtin(position) p: vec4f) -> @builtin(frag_depth) f32 {
|
|
367
388
|
let q = vec2i(p.xy) - o.xy;
|
|
368
|
-
return 1.0 - ${
|
|
389
|
+
return 1.0 - ${o};
|
|
369
390
|
}
|
|
370
391
|
`;
|
|
371
392
|
}
|
|
372
|
-
var
|
|
393
|
+
var D = class {
|
|
373
394
|
constructor(e, t) {
|
|
374
395
|
this.engine = e, this.device = t, this.eyes = [], this.views = [], this.offset = /* @__PURE__ */ new Int32Array(4), this.options = {
|
|
375
396
|
views: this.views,
|
|
376
397
|
delta: 0,
|
|
377
398
|
culling: !0
|
|
399
|
+
}, this.ar = !1, this.alphaPipelines = /* @__PURE__ */ new Map(), this.alphaGraphs = /* @__PURE__ */ new Map(), this.alphaDescriptor = {
|
|
400
|
+
colorAttachments: [{
|
|
401
|
+
view: void 0,
|
|
402
|
+
loadOp: "load",
|
|
403
|
+
storeOp: "store"
|
|
404
|
+
}],
|
|
405
|
+
depthStencilAttachment: {
|
|
406
|
+
view: void 0,
|
|
407
|
+
depthReadOnly: !0
|
|
408
|
+
},
|
|
409
|
+
label: "XR AR alpha"
|
|
410
|
+
}, this.clearAlpha = (e) => {
|
|
411
|
+
if (!this.ar || e.backend !== "webgpu" || e.device !== this.device || !this.eyes.some((t) => t.view.id === e.view?.id)) return;
|
|
412
|
+
let t = e.descriptor.depthStencil.format, n = e.descriptor.multisample?.count ?? 1, r = `${e.format} ${t} ${n}`, i = this.alphaPipelines.get(r);
|
|
413
|
+
if (!i) {
|
|
414
|
+
let a = this.alphaModule ??= this.device.createShaderModule({
|
|
415
|
+
code: w,
|
|
416
|
+
label: "XR AR alpha"
|
|
417
|
+
});
|
|
418
|
+
i = this.device.createRenderPipeline({
|
|
419
|
+
layout: "auto",
|
|
420
|
+
vertex: {
|
|
421
|
+
module: a,
|
|
422
|
+
entryPoint: "vertex"
|
|
423
|
+
},
|
|
424
|
+
fragment: {
|
|
425
|
+
module: a,
|
|
426
|
+
entryPoint: "fragment",
|
|
427
|
+
targets: [{
|
|
428
|
+
format: e.format,
|
|
429
|
+
writeMask: 8
|
|
430
|
+
}]
|
|
431
|
+
},
|
|
432
|
+
depthStencil: {
|
|
433
|
+
format: t,
|
|
434
|
+
depthWriteEnabled: !1,
|
|
435
|
+
depthCompare: "equal"
|
|
436
|
+
},
|
|
437
|
+
multisample: { count: n },
|
|
438
|
+
label: "XR AR alpha"
|
|
439
|
+
}), this.alphaPipelines.set(r, i);
|
|
440
|
+
}
|
|
441
|
+
let a = this.alphaDescriptor, o = a.depthStencilAttachment;
|
|
442
|
+
a.colorAttachments[0].view = e.color, o.view = e.depth, t.includes("stencil") ? o.stencilReadOnly = !0 : delete o.stencilReadOnly;
|
|
443
|
+
let s = e.encoder.beginRenderPass(a);
|
|
444
|
+
c(s, e), s.setPipeline(i), s.draw(3), s.end();
|
|
378
445
|
};
|
|
379
446
|
}
|
|
447
|
+
watchAlpha() {
|
|
448
|
+
let e = this.engine.frameGraph;
|
|
449
|
+
this.alphaGraphs.has(e) || this.alphaGraphs.set(e, e.add({
|
|
450
|
+
name: "xr:ar-alpha",
|
|
451
|
+
order: T,
|
|
452
|
+
execute: this.clearAlpha
|
|
453
|
+
}));
|
|
454
|
+
}
|
|
380
455
|
place(e, t, n) {
|
|
381
456
|
let r = t.transform.matrix, i = e.camera, a = e.projection;
|
|
382
457
|
i.position = [
|
|
@@ -424,7 +499,7 @@ var y = class {
|
|
|
424
499
|
}
|
|
425
500
|
size(e, t, n) {
|
|
426
501
|
if (e.color?.width === t && e.color.height === n) return;
|
|
427
|
-
e.color?.destroy(), e.depth?.destroy(), e.color = e.depth = e.colorGroup = e.depthGroup = void 0;
|
|
502
|
+
e.color?.destroy(), e.depth?.destroy(), this.dropCover(e), e.color = e.depth = e.colorGroup = e.depthGroup = void 0;
|
|
428
503
|
let r = this.engine.renderTarget;
|
|
429
504
|
e.color = this.device.createTexture({
|
|
430
505
|
size: [t, n],
|
|
@@ -442,13 +517,49 @@ var y = class {
|
|
|
442
517
|
depth: e.depth
|
|
443
518
|
};
|
|
444
519
|
}
|
|
520
|
+
dropCover(e) {
|
|
521
|
+
e.cover?.samples.destroy(), e.cover?.resolved.destroy(), e.cover = void 0;
|
|
522
|
+
}
|
|
523
|
+
cover(e, t, n) {
|
|
524
|
+
let r = e.cover;
|
|
525
|
+
if (!r) {
|
|
526
|
+
let t = [e.depth.width, e.depth.height], n = (e, n) => this.device.createTexture({
|
|
527
|
+
size: t,
|
|
528
|
+
format: "r8unorm",
|
|
529
|
+
sampleCount: e,
|
|
530
|
+
usage: n,
|
|
531
|
+
label: "XR eye coverage"
|
|
532
|
+
});
|
|
533
|
+
r = e.cover = {
|
|
534
|
+
samples: n(4, GPUTextureUsage.RENDER_ATTACHMENT),
|
|
535
|
+
resolved: n(1, GPUTextureUsage.RENDER_ATTACHMENT | GPUTextureUsage.TEXTURE_BINDING)
|
|
536
|
+
}, e.colorGroup = void 0;
|
|
537
|
+
}
|
|
538
|
+
let i = t.beginRenderPass({
|
|
539
|
+
colorAttachments: [{
|
|
540
|
+
view: r.samples.createView(),
|
|
541
|
+
resolveTarget: r.resolved.createView(),
|
|
542
|
+
loadOp: "clear",
|
|
543
|
+
storeOp: "discard"
|
|
544
|
+
}],
|
|
545
|
+
depthStencilAttachment: {
|
|
546
|
+
view: e.depth.createView(),
|
|
547
|
+
depthReadOnly: !0,
|
|
548
|
+
...e.depth.format.includes("stencil") ? { stencilReadOnly: !0 } : {}
|
|
549
|
+
}
|
|
550
|
+
});
|
|
551
|
+
return i.setPipeline(n), i.draw(3), i.end(), r.resolved;
|
|
552
|
+
}
|
|
445
553
|
build(e, t, n) {
|
|
446
554
|
let r = `${e} ${t}`;
|
|
447
555
|
if (this.pipelines?.key === r && (!n || this.pipelines.depth)) return;
|
|
448
|
-
let i = this.engine.renderTarget.
|
|
449
|
-
code:
|
|
556
|
+
let { samples: i, depthFormat: a } = this.engine.renderTarget, s = t && i > 1 && o(this.device) === "webgl2", c = this.device.createShaderModule({
|
|
557
|
+
code: E(i, t, s),
|
|
450
558
|
label: "XR eye blit"
|
|
451
|
-
}),
|
|
559
|
+
}), l = s ? this.device.createShaderModule({
|
|
560
|
+
code: C,
|
|
561
|
+
label: "XR eye coverage"
|
|
562
|
+
}) : null, u = this.device.createBindGroupLayout({ entries: [
|
|
452
563
|
{
|
|
453
564
|
binding: 0,
|
|
454
565
|
visibility: 2,
|
|
@@ -466,27 +577,32 @@ var y = class {
|
|
|
466
577
|
binding: 2,
|
|
467
578
|
visibility: 2,
|
|
468
579
|
buffer: {}
|
|
469
|
-
}
|
|
470
|
-
|
|
471
|
-
|
|
580
|
+
},
|
|
581
|
+
...s ? [{
|
|
582
|
+
binding: 3,
|
|
583
|
+
visibility: 2,
|
|
584
|
+
texture: { sampleType: "unfilterable-float" }
|
|
585
|
+
}] : []
|
|
586
|
+
] }), d = this.device.createPipelineLayout({ bindGroupLayouts: [u] }), f = {
|
|
587
|
+
module: c,
|
|
472
588
|
entryPoint: "vertex"
|
|
473
589
|
};
|
|
474
590
|
this.pipelines = {
|
|
475
591
|
key: r,
|
|
476
592
|
color: this.device.createRenderPipeline({
|
|
477
|
-
layout:
|
|
478
|
-
vertex:
|
|
593
|
+
layout: d,
|
|
594
|
+
vertex: f,
|
|
479
595
|
fragment: {
|
|
480
|
-
module:
|
|
596
|
+
module: c,
|
|
481
597
|
entryPoint: "color",
|
|
482
598
|
targets: [{ format: e }]
|
|
483
599
|
}
|
|
484
600
|
}),
|
|
485
601
|
depth: n ? this.device.createRenderPipeline({
|
|
486
|
-
layout:
|
|
487
|
-
vertex:
|
|
602
|
+
layout: d,
|
|
603
|
+
vertex: f,
|
|
488
604
|
fragment: {
|
|
489
|
-
module:
|
|
605
|
+
module: c,
|
|
490
606
|
entryPoint: "depth",
|
|
491
607
|
targets: []
|
|
492
608
|
},
|
|
@@ -495,6 +611,24 @@ var y = class {
|
|
|
495
611
|
depthWriteEnabled: !0,
|
|
496
612
|
depthCompare: "always"
|
|
497
613
|
}
|
|
614
|
+
}) : void 0,
|
|
615
|
+
cover: l ? this.device.createRenderPipeline({
|
|
616
|
+
layout: "auto",
|
|
617
|
+
vertex: {
|
|
618
|
+
module: l,
|
|
619
|
+
entryPoint: "vertex"
|
|
620
|
+
},
|
|
621
|
+
fragment: {
|
|
622
|
+
module: l,
|
|
623
|
+
entryPoint: "fragment",
|
|
624
|
+
targets: [{ format: "r8unorm" }]
|
|
625
|
+
},
|
|
626
|
+
depthStencil: {
|
|
627
|
+
format: a,
|
|
628
|
+
depthWriteEnabled: !1,
|
|
629
|
+
depthCompare: "less"
|
|
630
|
+
},
|
|
631
|
+
multisample: { count: i }
|
|
498
632
|
}) : void 0
|
|
499
633
|
};
|
|
500
634
|
for (let e of this.eyes) e.colorGroup = e.depthGroup = void 0;
|
|
@@ -506,14 +640,16 @@ var y = class {
|
|
|
506
640
|
let i = r.views[e], o = t.getViewSubImage(n, i), u = this.eye(e);
|
|
507
641
|
this.place(u, i, t.projection?.(i) ?? i.projectionMatrix), this.size(u, o.viewport.width, o.viewport.height), u.view.clear = a ? { color: "#000000" } : void 0, u.view.layers = s, c.push(u.view), l.push(o);
|
|
508
642
|
}
|
|
509
|
-
this.options.delta = i, this.engine.render(this.options);
|
|
643
|
+
this.options.delta = i, this.ar = a, a ? (this.watchAlpha(), this.premultipliedBefore ??= S(this.engine, !0)) : this.unmark(), this.engine.render(this.options);
|
|
510
644
|
let u = this.device.createCommandEncoder({ label: "XR eyes" });
|
|
511
645
|
for (let t = 0; t < l.length; t++) {
|
|
512
646
|
let n = l[t], r = this.eyes[t], { x: i, y: o, width: s, height: c } = n.viewport, d = n.getViewDescriptor(), f = n.depthStencilTexture;
|
|
513
647
|
if (f && f.format !== "depth24plus") throw e("unsupported", `The XR layer's depth is ${f.format}; create the layer with depth24plus`);
|
|
514
648
|
this.build(n.colorTexture.format, a, !!f);
|
|
515
649
|
let p = this.pipelines;
|
|
516
|
-
this.offset[0] = i, this.offset[1] = o, this.device.queue.writeBuffer(r.uniform, 0, this.offset)
|
|
650
|
+
this.offset[0] = i, this.offset[1] = o, this.device.queue.writeBuffer(r.uniform, 0, this.offset);
|
|
651
|
+
let m = p.cover && this.cover(r, u, p.cover);
|
|
652
|
+
r.colorGroup ??= this.device.createBindGroup({
|
|
517
653
|
layout: p.color.getBindGroupLayout(0),
|
|
518
654
|
entries: [
|
|
519
655
|
{
|
|
@@ -527,16 +663,20 @@ var y = class {
|
|
|
527
663
|
{
|
|
528
664
|
binding: 2,
|
|
529
665
|
resource: { buffer: r.uniform }
|
|
530
|
-
}
|
|
666
|
+
},
|
|
667
|
+
...m ? [{
|
|
668
|
+
binding: 3,
|
|
669
|
+
resource: m.createView()
|
|
670
|
+
}] : []
|
|
531
671
|
]
|
|
532
672
|
});
|
|
533
|
-
let
|
|
673
|
+
let h = u.beginRenderPass({ colorAttachments: [{
|
|
534
674
|
view: n.colorTexture.createView(d),
|
|
535
675
|
loadOp: "load",
|
|
536
676
|
storeOp: "store"
|
|
537
677
|
}] });
|
|
538
|
-
if (
|
|
539
|
-
let
|
|
678
|
+
if (h.setViewport(i, o, s, c, 0, 1), h.setPipeline(p.color), h.setBindGroup(0, r.colorGroup), h.draw(3), h.end(), !f || !p.depth) continue;
|
|
679
|
+
let g = u.beginRenderPass({
|
|
540
680
|
colorAttachments: [],
|
|
541
681
|
depthStencilAttachment: {
|
|
542
682
|
view: f.createView(d),
|
|
@@ -544,7 +684,7 @@ var y = class {
|
|
|
544
684
|
depthStoreOp: "store"
|
|
545
685
|
}
|
|
546
686
|
});
|
|
547
|
-
|
|
687
|
+
g.setViewport(i, o, s, c, 0, 1), g.setPipeline(p.depth), g.setBindGroup(0, r.colorGroup), g.draw(3), g.end();
|
|
548
688
|
}
|
|
549
689
|
if (o) {
|
|
550
690
|
for (let e = 0; e < l.length; e++) if (o({
|
|
@@ -564,11 +704,17 @@ var y = class {
|
|
|
564
704
|
}
|
|
565
705
|
this.device.queue.submit([u.finish()]), t.present?.(n);
|
|
566
706
|
}
|
|
707
|
+
unmark() {
|
|
708
|
+
this.premultipliedBefore !== void 0 && (S(this.engine, this.premultipliedBefore), this.premultipliedBefore = void 0);
|
|
709
|
+
}
|
|
567
710
|
dispose() {
|
|
568
|
-
|
|
711
|
+
this.unmark(), this.ar = !1;
|
|
712
|
+
for (let e of this.eyes) e.color?.destroy(), e.depth?.destroy(), this.dropCover(e), e.uniform.destroy();
|
|
569
713
|
this.eyes.length = 0, this.views.length = 0, this.pipelines = void 0;
|
|
714
|
+
for (let e of this.alphaGraphs.values()) e();
|
|
715
|
+
this.alphaGraphs.clear(), this.alphaPipelines.clear(), this.alphaModule = void 0;
|
|
570
716
|
}
|
|
571
|
-
},
|
|
717
|
+
}, O = class {
|
|
572
718
|
constructor(e) {
|
|
573
719
|
if (this.source = e, this.targetRay = null, this.grip = null, this.targetRayMatrix = /* @__PURE__ */ new Float32Array(16), this.gripMatrix = /* @__PURE__ */ new Float32Array(16), this.buttons = /* @__PURE__ */ new Float32Array(96), this.axes = /* @__PURE__ */ new Float32Array(16), this.handMatrices = /* @__PURE__ */ new Float32Array(400), this.handTracked = /* @__PURE__ */ new Uint8Array(25), this.handRadii = /* @__PURE__ */ new Float32Array(25), this.buttonCount = 0, this.axisCount = 0, this.joints = e.hand ? Array.from(e.hand.values()) : [], this.joints.length > 25) throw RangeError("XR hand exceeds 25 joints; the controller buffers hold the 25 WebXR hand joints");
|
|
574
720
|
}
|
|
@@ -589,11 +735,11 @@ var y = class {
|
|
|
589
735
|
r && (this.handMatrices.set(r.transform.matrix, n * 16), this.handTracked[n] = 1, this.handRadii[n] = r.radius ?? 0);
|
|
590
736
|
}
|
|
591
737
|
}
|
|
592
|
-
},
|
|
738
|
+
}, k = [
|
|
593
739
|
"point",
|
|
594
740
|
"plane",
|
|
595
741
|
"mesh"
|
|
596
|
-
],
|
|
742
|
+
], A = class t {
|
|
597
743
|
constructor(e, t, n, r) {
|
|
598
744
|
this.host = e, this.session = t, this.source = n, this.transient = r, this.cancelled = !1, this.ended = () => this.cancel(), t.addEventListener("end", this.ended, { once: !0 });
|
|
599
745
|
}
|
|
@@ -603,8 +749,8 @@ var y = class {
|
|
|
603
749
|
if (i.enabledFeatures && !i.enabledFeatures.includes("hit-test")) throw e("unsupported", "The XR session has no 'hit-test' feature; request it in requiredFeatures or optionalFeatures");
|
|
604
750
|
if (!i.requestHitTestSource || !i.requestHitTestSourceForTransientInput) throw e("unsupported", "This browser has no WebXR hit testing; use an AR-capable browser and device");
|
|
605
751
|
let a = [...r.entityTypes ?? ["plane"]];
|
|
606
|
-
if (!a.length || a.some((e) => !
|
|
607
|
-
let o = r.offsetRay === void 0 ? void 0 :
|
|
752
|
+
if (!a.length || a.some((e) => !k.includes(e))) throw TypeError("HitTest entityTypes must list 'point', 'plane' or 'mesh'");
|
|
753
|
+
let o = r.offsetRay === void 0 ? void 0 : M(r.offsetRay), s = r.from ?? "viewer";
|
|
608
754
|
if (s !== "viewer" && (typeof s != "object" || !s)) throw TypeError("HitTest from must be 'viewer', an XRInputSource or {profile}");
|
|
609
755
|
if (s !== "viewer" && "profile" in s) {
|
|
610
756
|
if (typeof s.profile != "string" || !s.profile) throw TypeError("HitTest profile must be a non-empty string such as 'generic-touchscreen'");
|
|
@@ -632,11 +778,11 @@ var y = class {
|
|
|
632
778
|
if (this.cancelled || !t || e?.session !== this.session) return [];
|
|
633
779
|
let n = [];
|
|
634
780
|
if (this.source) for (let r of e.getHitTestResults?.(this.source) ?? []) {
|
|
635
|
-
let e =
|
|
781
|
+
let e = j(r, t);
|
|
636
782
|
e && n.push(e);
|
|
637
783
|
}
|
|
638
784
|
if (this.transient) for (let r of e.getHitTestResultsForTransientInput?.(this.transient) ?? []) for (let e of r.results) {
|
|
639
|
-
let i =
|
|
785
|
+
let i = j(e, t, r.inputSource);
|
|
640
786
|
i && n.push(i);
|
|
641
787
|
}
|
|
642
788
|
return n;
|
|
@@ -651,7 +797,7 @@ var y = class {
|
|
|
651
797
|
this.cancelled || (this.cancelled = !0, this.session.removeEventListener("end", this.ended), this.source?.cancel(), this.transient?.cancel());
|
|
652
798
|
}
|
|
653
799
|
};
|
|
654
|
-
function
|
|
800
|
+
function j(e, t, n) {
|
|
655
801
|
let r = e.getPose(t);
|
|
656
802
|
if (!r) return null;
|
|
657
803
|
let { matrix: i, position: a, orientation: o } = r.transform;
|
|
@@ -672,7 +818,7 @@ function C(e, t, n) {
|
|
|
672
818
|
...n && { inputSource: n }
|
|
673
819
|
};
|
|
674
820
|
}
|
|
675
|
-
function
|
|
821
|
+
function M(t) {
|
|
676
822
|
let n = t.origin ?? [
|
|
677
823
|
0,
|
|
678
824
|
0,
|
|
@@ -700,8 +846,8 @@ function w(t) {
|
|
|
700
846
|
}
|
|
701
847
|
//#endregion
|
|
702
848
|
//#region src/index.ts
|
|
703
|
-
var
|
|
704
|
-
async function
|
|
849
|
+
var N = () => globalThis, P = () => globalThis.navigator?.xr, F = /* @__PURE__ */ new WeakSet();
|
|
850
|
+
async function I(e, t = {}) {
|
|
705
851
|
let n = globalThis.navigator?.gpu;
|
|
706
852
|
if (!n) throw new a("unsupported", "createXREngine needs WebGPU; use a browser with WebGPU and WebXR");
|
|
707
853
|
let o = Object.getOwnPropertyDescriptor(n, "requestAdapter"), s = n.requestAdapter;
|
|
@@ -723,51 +869,51 @@ async function O(e, t = {}) {
|
|
|
723
869
|
o ? Object.defineProperty(n, "requestAdapter", o) : delete n.requestAdapter;
|
|
724
870
|
}
|
|
725
871
|
let l = r(c).gpu;
|
|
726
|
-
return l &&
|
|
872
|
+
return l && F.add(l), c;
|
|
727
873
|
}
|
|
728
|
-
var
|
|
729
|
-
function
|
|
730
|
-
let t =
|
|
874
|
+
var L = /* @__PURE__ */ new WeakMap();
|
|
875
|
+
function R(e) {
|
|
876
|
+
let t = L.get(e);
|
|
731
877
|
if (!t) {
|
|
732
878
|
let n = t = /* @__PURE__ */ new Set();
|
|
733
|
-
|
|
879
|
+
L.set(e, n), e.lost.then((e) => {
|
|
734
880
|
for (let t of [...n]) t.lostDevice(e);
|
|
735
881
|
});
|
|
736
882
|
}
|
|
737
883
|
return t;
|
|
738
884
|
}
|
|
739
|
-
var
|
|
885
|
+
var z = class e {coreRequirement=__margoCoreRequirement("1.0.0",import.meta.url);
|
|
740
886
|
get layers() {
|
|
741
887
|
return this.mask;
|
|
742
888
|
}
|
|
743
889
|
set layers(e) {
|
|
744
|
-
this.mask = e === void 0 ? void 0 :
|
|
890
|
+
this.mask = e === void 0 ? void 0 : d(e, "XR layers");
|
|
745
891
|
}
|
|
746
892
|
static async isSupported(e = "immersive-vr") {
|
|
747
|
-
return !
|
|
893
|
+
return !P() || e !== "inline" && (!N().XRGPUBinding || !globalThis.navigator?.gpu) ? !1 : P().isSessionSupported(e);
|
|
748
894
|
}
|
|
749
|
-
static attach(t, n = {}) {__margoAssertCore(t,__margoCoreRequirement("
|
|
895
|
+
static attach(t, n = {}) {__margoAssertCore(t,__margoCoreRequirement("1.0.0",import.meta.url));
|
|
750
896
|
return e.create(t, n);
|
|
751
897
|
}
|
|
752
898
|
static async create(n, i = {}) {
|
|
753
899
|
let { signal: o } = i;
|
|
754
|
-
o?.throwIfAborted(), i.layers !== void 0 &&
|
|
900
|
+
o?.throwIfAborted(), i.layers !== void 0 && d(i.layers, "XR layers");
|
|
755
901
|
let s = r(n);
|
|
756
902
|
if (s.backend !== "webgpu") throw new a("unsupported", "XR requires WebGPU or the full WebGL2 compatibility layer; use autoEngine with loadCompat; see https://margoengine.com/errors#webgpu");
|
|
757
903
|
if (s.disposed || s.hasPlugin("xr")) throw new a("state", "XR requires a live WebGPU engine without an XR plugin; create the engine with backend 'webgpu' and call XR.create once.");
|
|
758
|
-
if (!
|
|
759
|
-
if (s.gpu &&
|
|
904
|
+
if (!P()) throw new a("unsupported", "WebXR is unavailable; use a WebXR browser over HTTPS or localhost.");
|
|
905
|
+
if (s.gpu && F.has(s.gpu)) {
|
|
760
906
|
let t = new e(s.gpu, !0);
|
|
761
907
|
return t.layers = i.layers, s.use(t), t;
|
|
762
908
|
}
|
|
763
|
-
let
|
|
764
|
-
if (!
|
|
765
|
-
if (
|
|
909
|
+
let c = t(s), l = await navigator.gpu.requestAdapter({ xrCompatible: !!N().XRGPUBinding });
|
|
910
|
+
if (!l) throw new a("unsupported", "XR-compatible WebGPU adapter unavailable; use a headset browser with WebGPU XR support.; see https://margoengine.com/errors#webgpu");
|
|
911
|
+
if (l.info?.vendor === "margo-webgl2" && !N().XRGPUBinding?.webgl) throw new a("unsupported", "XR needs the browser's own WebGPU (XRGPUBinding) or, on the WebGL2 compatibility layer, its route: call installXR() from the layer's xr entry first; see https://margoengine.com/errors#webgpu");
|
|
766
912
|
o?.throwIfAborted();
|
|
767
|
-
let
|
|
913
|
+
let u = s.gpu ? [...s.gpu.features].filter((e) => l.features?.has(e)) : [], f = await l.requestDevice({ requiredFeatures: u }), p = new e(f);
|
|
768
914
|
p.layers = i.layers;
|
|
769
915
|
try {
|
|
770
|
-
return o?.throwIfAborted(),
|
|
916
|
+
return o?.throwIfAborted(), c.run(() => s.use(p)), p;
|
|
771
917
|
} catch (e) {
|
|
772
918
|
throw p.dispose(), e;
|
|
773
919
|
}
|
|
@@ -807,17 +953,17 @@ var j = class e {coreRequirement=__margoCoreRequirement("0.1.0",import.meta.url)
|
|
|
807
953
|
}, this.sourcesChanged = () => {
|
|
808
954
|
let e = this.session?.inputSources ?? [];
|
|
809
955
|
for (let t = this.controllers.length - 1; t >= 0; t--) Array.prototype.includes.call(e, this.controllers[t].source) || this.controllers.splice(t, 1);
|
|
810
|
-
for (let t of e) this.controllers.some((e) => e.source === t) || this.controllers.push(new
|
|
956
|
+
for (let t of e) this.controllers.some((e) => e.source === t) || this.controllers.push(new O(t));
|
|
811
957
|
}, this.ended = () => {
|
|
812
958
|
let e = this.session;
|
|
813
959
|
e && this.handle !== null && e.cancelAnimationFrame(this.handle), e?.removeEventListener("end", this.ended), e?.removeEventListener("inputsourceschange", this.sourcesChanged), this.controllers.length = 0, this.session = null, this.handle = null, this.last = null, this.callback = void 0, this.space = void 0, this.binding = void 0, this.seeThrough = !1, this.layer?.destroy(), this.layer = void 0, this.renderer?.dispose(), this.eyes?.dispose(), this.resume && (this.resume = !1, !this.disposed && !this.engine.disposed && this.engine.start());
|
|
814
|
-
}, this.route = t ? "engine" : "separate", t || (this.renderer = new
|
|
960
|
+
}, this.route = t ? "engine" : "separate", t || (this.renderer = new b(e)), R(e).add(this), t || e.addEventListener("uncapturederror", this.uncaptured);
|
|
815
961
|
}
|
|
816
962
|
lostDevice(e) {
|
|
817
963
|
this.disposed || (this.lost = !0, this.fail(new a("failed", `XR GPU device lost: ${e.message}; create a new XR with XR.create.`)));
|
|
818
964
|
}
|
|
819
965
|
setup(e) {
|
|
820
|
-
this.engine = e, this.route === "engine" && (this.eyes = new
|
|
966
|
+
this.engine = e, this.route === "engine" && (this.eyes = new D(e, this.device));
|
|
821
967
|
}
|
|
822
968
|
onFrame(e) {
|
|
823
969
|
if (typeof e != "function") throw TypeError("xr.onFrame needs a function; pass (state) => {...}");
|
|
@@ -871,7 +1017,7 @@ var j = class e {coreRequirement=__margoCoreRequirement("0.1.0",import.meta.url)
|
|
|
871
1017
|
"immersive-vr",
|
|
872
1018
|
"immersive-ar"
|
|
873
1019
|
].includes(e)) throw TypeError("Unknown XR mode; use 'inline', 'immersive-vr' or 'immersive-ar'");
|
|
874
|
-
if (e !== "inline" && !
|
|
1020
|
+
if (e !== "inline" && !N().XRGPUBinding) throw new a("unsupported", "Native WebGPU XR bindings are unavailable; desktop rendering remains active. Use a browser with XRGPUBinding or request an inline session.");
|
|
875
1021
|
t.signal?.throwIfAborted();
|
|
876
1022
|
let n = t.referenceSpace ?? (e === "inline" ? "viewer" : "local");
|
|
877
1023
|
if (![
|
|
@@ -886,10 +1032,10 @@ var j = class e {coreRequirement=__margoCoreRequirement("0.1.0",import.meta.url)
|
|
|
886
1032
|
let r;
|
|
887
1033
|
try {
|
|
888
1034
|
if (e === "inline" && [...t.requiredFeatures ?? [], ...t.optionalFeatures ?? []].includes("webgpu")) throw TypeError("WebGPU XR bindings do not support inline sessions; remove 'webgpu' from the features or request an immersive mode");
|
|
889
|
-
if (r = await
|
|
1035
|
+
if (r = await P().requestSession(e, {
|
|
890
1036
|
...t.init,
|
|
891
1037
|
requiredFeatures: [.../* @__PURE__ */ new Set([
|
|
892
|
-
...e === "inline" ||
|
|
1038
|
+
...e === "inline" || N().XRGPUBinding?.webgl ? [] : ["webgpu"],
|
|
893
1039
|
...n === "viewer" ? [] : [n],
|
|
894
1040
|
...t.requiredFeatures ?? []
|
|
895
1041
|
])],
|
|
@@ -899,7 +1045,7 @@ var j = class e {coreRequirement=__margoCoreRequirement("0.1.0",import.meta.url)
|
|
|
899
1045
|
if (this.space = await r.requestReferenceSpace(n), this.disposed || this.session !== r) throw new a("state", "XR session ended during initialization; request a new session.");
|
|
900
1046
|
return t.signal?.throwIfAborted(), this.last = null, this.handle = r.requestAnimationFrame(this.frame), r;
|
|
901
1047
|
}
|
|
902
|
-
this.binding = new (
|
|
1048
|
+
this.binding = new (N()).XRGPUBinding(r, this.device);
|
|
903
1049
|
let i = this.binding.getPreferredColorFormat();
|
|
904
1050
|
if (this.layer = this.binding.createProjectionLayer({
|
|
905
1051
|
colorFormat: i,
|
|
@@ -944,14 +1090,14 @@ var j = class e {coreRequirement=__margoCoreRequirement("0.1.0",import.meta.url)
|
|
|
944
1090
|
if (this.disposed) return;
|
|
945
1091
|
this.disposed = !0, this.eyeListeners.clear();
|
|
946
1092
|
let e = this.session;
|
|
947
|
-
this.ended(), this.device.removeEventListener("uncapturederror", this.uncaptured),
|
|
1093
|
+
this.ended(), this.device.removeEventListener("uncapturederror", this.uncaptured), L.get(this.device)?.delete(this), this.route === "separate" && this.device.destroy(), e && e.end().catch((e) => this.engine.onError(e)), this.engine?.sceneOf(this)?.removePlugin(this.name);
|
|
948
1094
|
}
|
|
949
1095
|
[Symbol.dispose ?? Symbol()]() {
|
|
950
1096
|
this.dispose();
|
|
951
1097
|
}
|
|
952
1098
|
};
|
|
953
|
-
function
|
|
1099
|
+
function B(e, t = "immersive-vr", n = {}) {
|
|
954
1100
|
return e.requestSession(t, n);
|
|
955
1101
|
}
|
|
956
1102
|
//#endregion
|
|
957
|
-
export {
|
|
1103
|
+
export { A as HitTest, z as XR, z as XRManager, O as XRController, I as createXREngine, B as useXRSession };
|