@nebula-spatial/viewer 0.6.4 → 0.6.6

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.
@@ -0,0 +1,677 @@
1
+ var lt = Object.defineProperty;
2
+ var ct = (e, t, n) => t in e ? lt(e, t, { enumerable: !0, configurable: !0, writable: !0, value: n }) : e[t] = n;
3
+ var U = (e, t, n) => ct(e, typeof t != "symbol" ? t + "" : t, n);
4
+ import { I as ut, J as x, K as X, L as ft, N as dt, O as ht, U as mt, s as pt, M as bt, P as _t } from "./index-DyBfusq5.js";
5
+ import { Vector3 as Z } from "three";
6
+ const Et = /* @__PURE__ */ new Set([
7
+ "E_MJCF_INCLUDE_CYCLE",
8
+ "E_MJCF_PARSE",
9
+ "E_URDF_COMPILE",
10
+ "E_URDF_INERTIA",
11
+ "E_USD_INVALID_COLLISION_GEOMETRY",
12
+ "E_USD_INVALID_COLLISION_GROUP",
13
+ "E_USD_INVALID_EQUALITY_CONNECT",
14
+ "E_USD_INVALID_JOINT_REFERENCE",
15
+ "E_USD_INVALID_MASS",
16
+ "E_USD_INVALID_MIMIC",
17
+ "E_USD_INVALID_UNITS",
18
+ "E_USD_MISSING_COLLISION_GEOMETRY",
19
+ "E_USD_MULTIPLE_PHYSICS_SCENES",
20
+ "E_USD_PARTIAL_INERTIA",
21
+ "E_USD_UNREACHABLE_BODIES"
22
+ ]), yt = /* @__PURE__ */ new Set(["step", "reset", "grab", "up-axis"]), St = Object.freeze({
23
+ kind: "visual-adaptation",
24
+ title: "视觉适配信息",
25
+ summary: "部分材质细节采用近似表示,不影响资产结构与模型查看。",
26
+ tone: "info"
27
+ }), gt = Object.freeze({
28
+ kind: "unsupported-by-viewer",
29
+ title: "视觉预览模式",
30
+ summary: "当前仿真链路未能建立物理模型,资产视觉内容仍可正常查看。",
31
+ tone: "info"
32
+ });
33
+ function M(e, t) {
34
+ return e === "E_URDF_INERTIA" ? {
35
+ kind: "invalid-asset",
36
+ title: "物理定义无效",
37
+ summary: "该 URDF 未声明动力学仿真所需的有效惯性数据,动力学模型无法建立。",
38
+ tone: "warning"
39
+ } : {
40
+ kind: "invalid-asset",
41
+ title: "物理定义无效",
42
+ summary: t === "usd-physics" ? "该 USD 资产的物理参数或关节定义无效,物理模型无法建立。" : "该资产的物理参数缺失或无效,物理模型无法建立。",
43
+ tone: "warning"
44
+ };
45
+ }
46
+ function It(e) {
47
+ return e === "visual-material" ? {
48
+ kind: "visual-adaptation",
49
+ title: "视觉材质近似",
50
+ summary: "部分材质信息未完整转换,当前显示使用近似材质。",
51
+ tone: "info"
52
+ } : St;
53
+ }
54
+ function rt(e, t) {
55
+ const n = t.split(":").at(-1);
56
+ if (n && yt.has(n))
57
+ return {
58
+ kind: "runtime-failure",
59
+ title: "仿真运行异常",
60
+ summary: "仿真初始化或运行过程中发生异常,物理仿真已停止;资产视觉内容仍可正常查看。",
61
+ tone: "warning"
62
+ };
63
+ if (e === "E_MUJOCO_INITIALIZE") {
64
+ if (t === "mjcf-physics")
65
+ return {
66
+ kind: "runtime-failure",
67
+ title: "物理模型初始化失败",
68
+ summary: "MuJoCo 物理模型初始化失败,资产仍可进行视觉预览。",
69
+ tone: "warning"
70
+ };
71
+ if (t === "urdf-physics")
72
+ return {
73
+ kind: "runtime-failure",
74
+ title: "动力学初始化失败",
75
+ summary: "URDF 动力学初始化失败,当前保留运动学预览。",
76
+ tone: "warning"
77
+ };
78
+ if (t === "usd-physics")
79
+ return {
80
+ kind: "runtime-failure",
81
+ title: "物理模型初始化失败",
82
+ summary: "当前 USD 物理模型未能完成 MuJoCo 初始化,资产视觉内容仍可正常查看。",
83
+ tone: "warning"
84
+ };
85
+ }
86
+ return Et.has(e) ? M(e, t) : t === "usd-physics" || e.startsWith("E_USD_") ? {
87
+ kind: "unsupported-by-viewer",
88
+ title: "视觉预览模式",
89
+ summary: "当前 Viewer 未覆盖该 USD 资产使用的部分物理语义,视觉与结构信息可正常查看,物理仿真未启动。",
90
+ tone: "info"
91
+ } : e.startsWith("E_MJCF_") || e.startsWith("E_URDF_") ? M(e, t) : gt;
92
+ }
93
+ function Ct(e, t) {
94
+ const n = e.notice ?? (t === "physics-validation" ? M(e.code, e.source) : t === "visual-adaptation" || t === "visual-material" ? It(t) : rt(e.code, e.source ?? t));
95
+ return { ...e, stage: t, notice: n };
96
+ }
97
+ function Ft(e, t, n) {
98
+ if (t.throwIfAborted(), e instanceof Error && (e.name === "AbortError" || ["E_ABORTED", "E_SUPERSEDED"].includes(String(e.code)))) throw e;
99
+ const s = e && typeof e == "object" && "code" in e && typeof e.code == "string" ? e.code : "E_PHYSICS_INITIALIZE", i = e instanceof Error ? e.message : String(e);
100
+ return { code: s, source: n, message: i, notice: rt(s, n) };
101
+ }
102
+ function Jt(e, t, n, s, i, r) {
103
+ try {
104
+ return wt(e, t, n, s, r);
105
+ } catch (o) {
106
+ try {
107
+ i == null || i(o);
108
+ } catch {
109
+ }
110
+ return;
111
+ }
112
+ }
113
+ function wt(e, t, n, s, i) {
114
+ var V, z, W, G;
115
+ const r = e.model, o = e.mujoco, f = e.data;
116
+ if (!r || !o || !f) return;
117
+ const c = [], l = /* @__PURE__ */ new Map(), y = /* @__PURE__ */ new Map(), S = /* @__PURE__ */ new Set();
118
+ for (let a = 0; a < r.nflexvert; a++) S.add(Number(r.flex_vertbodyid[a]));
119
+ for (let a = 0; a < r.nflexnode; a++) S.add(Number(r.flex_nodebodyid[a]));
120
+ const I = /* @__PURE__ */ new Set();
121
+ for (let a = 0; a < r.nflex; a++) {
122
+ const u = o.mj_id2name(r, o.mjtObj.mjOBJ_FLEX.value, a);
123
+ u && I.add(u);
124
+ }
125
+ const g = /* @__PURE__ */ new Map(), v = /* @__PURE__ */ new Map(), st = (a) => {
126
+ try {
127
+ const u = Number(r.nu ?? 0);
128
+ if (!Number.isFinite(u) || u <= 0) return {};
129
+ const d = r.actuator_trntype, h = r.actuator_trnid, m = r.actuator_forcerange, p = r.actuator_biasprm;
130
+ if (!d || !h) return {};
131
+ for (let _ = 0; _ < u; _++) {
132
+ if (d[_] !== o.mjtTrn.mjTRN_JOINT.value || h[_ * 2] !== a) continue;
133
+ const j = Number(r.actuator_gear[_ * 6]);
134
+ if (!Number.isFinite(j)) continue;
135
+ const N = r.actuator_biastype[_] === o.mjtBias.mjBIAS_AFFINE.value, A = r.actuator_gaintype[_] === o.mjtGain.mjGAIN_FIXED.value, w = N ? -Number(p == null ? void 0 : p[_ * 10 + 1]) + 0 : void 0, b = N ? -Number(p == null ? void 0 : p[_ * 10 + 2]) + 0 : void 0, T = Number(r.actuator_gainprm[_ * 10]), at = {
136
+ type: A && N && w != null && w > 0 && T === w ? "position" : A && N && w === 0 && b != null && b > 0 && T === b ? "velocity" : A && r.actuator_biastype[_] === o.mjtBias.mjBIAS_NONE.value ? "motor" : "general",
137
+ ...Number.isFinite(w) ? { stiffness: w * j * j } : {},
138
+ ...Number.isFinite(b) ? { damping: b * j * j } : {}
139
+ }, Y = r.actuator(_);
140
+ let q;
141
+ try {
142
+ q = !!Y.forcelimited;
143
+ } finally {
144
+ Y.delete();
145
+ }
146
+ const H = Number(m == null ? void 0 : m[_ * 2]), K = Number(m == null ? void 0 : m[_ * 2 + 1]), Q = q && Number.isFinite(H) && Number.isFinite(K) ? Math.max(Math.abs(H), Math.abs(K)) * Math.abs(j) : void 0;
147
+ return { drive: at, ...Q != null ? { maxEffort: Q } : {} };
148
+ }
149
+ } catch {
150
+ }
151
+ return {};
152
+ };
153
+ for (let a = 0; a < r.njnt; a++) {
154
+ const u = r.jnt_type[a], d = u === o.mjtJoint.mjJNT_HINGE.value;
155
+ if (!d && u !== o.mjtJoint.mjJNT_SLIDE.value || S.has(r.jnt_bodyid[a])) continue;
156
+ const h = o.mj_id2name(r, o.mjtObj.mjOBJ_BODY.value, r.jnt_bodyid[a]);
157
+ if (h && [...I].some((N) => h.startsWith(`${N}_`))) continue;
158
+ const m = o.mj_id2name(r, o.mjtObj.mjOBJ_JOINT.value, a);
159
+ if (!m || o.mj_name2id(r, o.mjtObj.mjOBJ_JOINT.value, m) !== a) continue;
160
+ const p = r.jnt_bodyid[a], _ = r.body_parentid[p], j = r.jnt(a);
161
+ try {
162
+ const N = (V = i == null ? void 0 : i.mimics) == null ? void 0 : V.get(m), A = st(a), w = Number((z = r.body_mass) == null ? void 0 : z[p]), b = (W = i == null ? void 0 : i.metadata) == null ? void 0 : W.get(m), T = ((G = i == null ? void 0 : i.limits) == null ? void 0 : G.get(m)) ?? (j.limited ? { lower: r.jnt_range[a * 2], upper: r.jnt_range[a * 2 + 1] } : void 0);
163
+ c.push({
164
+ id: m,
165
+ name: m.split("/").pop() || m,
166
+ type: d ? "revolute" : "prismatic",
167
+ parent: _ > 0 ? o.mj_id2name(r, o.mjtObj.mjOBJ_BODY.value, _) : null,
168
+ child: o.mj_id2name(r, o.mjtObj.mjOBJ_BODY.value, p),
169
+ axis: Array.from(r.jnt_axis.slice(a * 3, a * 3 + 3)),
170
+ unit: d ? "rad" : "m",
171
+ defaultValue: f.qpos[r.jnt_qposadr[a]],
172
+ ...T ? { limits: T } : {},
173
+ ...(b == null ? void 0 : b.movingMass) != null ? { movingMass: b.movingMass } : Number.isFinite(w) && w > 0 ? { movingMass: w } : {},
174
+ ...b != null && b.drive ? { drive: b.drive } : A.drive ? { drive: A.drive } : {},
175
+ ...(b == null ? void 0 : b.maxEffort) != null ? { maxEffort: b.maxEffort } : A.maxEffort != null ? { maxEffort: A.maxEffort } : {},
176
+ ...N ? { mimic: { ...N } } : {}
177
+ }), l.set(m, a), y.set(m, p), g.has(p) || g.set(p, m), v.set(p, m);
178
+ } finally {
179
+ j.delete();
180
+ }
181
+ }
182
+ if (!c.length) return;
183
+ const P = /* @__PURE__ */ new Map(), ot = c.map((a) => {
184
+ const u = y.get(a.id);
185
+ let d = P.get(u);
186
+ if (!d) {
187
+ let h = r.body_parentid[u];
188
+ for (; h > 0 && !g.has(h); ) h = r.body_parentid[h];
189
+ d = v.get(h);
190
+ }
191
+ return P.set(u, a.id), { ...a, parentJointId: d ?? null };
192
+ }), k = ut(ot, t, {
193
+ controlMode: t === "usd" ? "kinematic" : "physics",
194
+ apply(a, u) {
195
+ if (t === "usd")
196
+ e.setJointValuesKinematic(a);
197
+ else {
198
+ const d = Object.fromEntries(u.map((h) => [h, a[h]]));
199
+ e.setJointTargets(d);
200
+ }
201
+ return n(), e.getJointValues(c.map((d) => d.id));
202
+ },
203
+ ...t === "mjcf" ? { isDriveActive: () => e.isJointDriveActive } : {},
204
+ requestRender: s
205
+ }), C = /* @__PURE__ */ new Map(), F = /* @__PURE__ */ new Map(), J = /* @__PURE__ */ new Map(), $ = /* @__PURE__ */ new Map();
206
+ for (let a = 1; a < r.nbody; a++) {
207
+ const u = o.mj_id2name(r, o.mjtObj.mjOBJ_BODY.value, a);
208
+ if (!u) continue;
209
+ C.set(u, a), F.set(a, u);
210
+ let d = a;
211
+ for (; d > 0 && !g.has(d); ) d = r.body_parentid[d];
212
+ J.set(u, g.get(d) ?? null);
213
+ }
214
+ for (const [a, u] of y) {
215
+ const d = [];
216
+ for (const [h, m] of F) {
217
+ let p = h;
218
+ for (; p > 0 && p !== u; ) p = r.body_parentid[p];
219
+ p === u && d.push(m);
220
+ }
221
+ $.set(a, Object.freeze(d));
222
+ }
223
+ let L = !1;
224
+ const D = () => !L && e.model === r && e.data === f;
225
+ return {
226
+ ...k,
227
+ pickRoots() {
228
+ return D() && (i != null && i.root) ? [i.root] : [];
229
+ },
230
+ objectForBody(a) {
231
+ var u;
232
+ return D() && C.has(a) ? ((u = i == null ? void 0 : i.objectForBody) == null ? void 0 : u.call(i, a)) ?? null : null;
233
+ },
234
+ jointForBody(a) {
235
+ return D() ? J.get(a) ?? null : null;
236
+ },
237
+ bodiesForJoint(a) {
238
+ return D() ? $.get(a) ?? [] : [];
239
+ },
240
+ jointWorldFrame(a) {
241
+ const u = l.get(a);
242
+ if (!D() || u === void 0) return null;
243
+ const d = new Z().fromArray(f.xanchor, u * 3), h = new Z().fromArray(f.xaxis, u * 3);
244
+ return !d.toArray().every(Number.isFinite) || !h.toArray().every(Number.isFinite) || h.lengthSq() < 1e-12 ? null : (i != null && i.root ? (i.root.updateWorldMatrix(!0, !1), d.applyMatrix4(i.root.matrixWorld), h.transformDirection(i.root.matrixWorld)) : h.normalize(), { position: d, axis: h });
245
+ },
246
+ dispose() {
247
+ L || (L = !0, k.dispose());
248
+ }
249
+ };
250
+ }
251
+ const vt = /<UDIM>/i;
252
+ function jt(e, t) {
253
+ const n = tt(e).replaceAll("\\", "/");
254
+ if (!vt.test(n)) return null;
255
+ const s = et(n), i = s.split("/").at(-1);
256
+ if (!i) return { paths: [], tiles: [] };
257
+ const r = Ut(i);
258
+ if (!r) return { paths: [], tiles: [] };
259
+ const o = [];
260
+ for (const l of new Set(t)) {
261
+ const y = tt(l).replaceAll("\\", "/"), S = et(y), I = S.split("/").at(-1);
262
+ if (!I) continue;
263
+ const g = r.exec(I);
264
+ if (!g) continue;
265
+ const v = Number(g[1]);
266
+ !Number.isInteger(v) || v < 1001 || v > 9999 || o.push({
267
+ path: l,
268
+ tile: v,
269
+ score: Ot(s, S)
270
+ });
271
+ }
272
+ if (!o.length) return { paths: [], tiles: [] };
273
+ const f = Math.max(...o.map((l) => l.score)), c = o.filter((l) => l.score === f).sort((l, y) => l.tile - y.tile || l.path.localeCompare(y.path));
274
+ return {
275
+ paths: c.map((l) => l.path),
276
+ tiles: c.map((l) => l.tile)
277
+ };
278
+ }
279
+ function $t(e) {
280
+ if (!e.tiles.length) return null;
281
+ const t = e.tiles.map(Nt), n = Math.min(...t.map(({ column: o }) => o)), s = Math.max(...t.map(({ column: o }) => o)), i = Math.min(...t.map(({ row: o }) => o)), r = Math.max(...t.map(({ row: o }) => o));
282
+ return {
283
+ minColumn: n,
284
+ maxColumn: s,
285
+ minRow: i,
286
+ maxRow: r,
287
+ columns: s - n + 1,
288
+ rows: r - i + 1
289
+ };
290
+ }
291
+ function Nt(e) {
292
+ const t = e - 1001;
293
+ return {
294
+ column: (t % 10 + 10) % 10,
295
+ row: Math.floor(t / 10)
296
+ };
297
+ }
298
+ function Ut(e) {
299
+ const t = e.split(/<UDIM>/i);
300
+ return t.length !== 2 ? null : new RegExp(`^${nt(t[0])}(\\d{4})${nt(t[1])}$`, "i");
301
+ }
302
+ function Ot(e, t) {
303
+ const n = e.split("/").slice(0, -1).filter(Boolean), s = t.split("/").slice(0, -1).filter(Boolean);
304
+ let i = 0;
305
+ for (; i < n.length && i < s.length; ) {
306
+ const r = n.at(-1 - i), o = s.at(-1 - i);
307
+ if ((r == null ? void 0 : r.toLowerCase()) !== (o == null ? void 0 : o.toLowerCase())) break;
308
+ i += 1;
309
+ }
310
+ return i;
311
+ }
312
+ function tt(e) {
313
+ const t = e.replace(/%3CUDIM%3E/gi, "<UDIM>");
314
+ try {
315
+ return decodeURIComponent(t);
316
+ } catch {
317
+ return t;
318
+ }
319
+ }
320
+ function et(e) {
321
+ const t = e.search(/[?#]/);
322
+ return t < 0 ? e : e.slice(0, t);
323
+ }
324
+ function nt(e) {
325
+ return e.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
326
+ }
327
+ class Vt {
328
+ constructor(t) {
329
+ U(this, "entries", /* @__PURE__ */ new Map());
330
+ U(this, "manifestPaths", /* @__PURE__ */ new Set());
331
+ U(this, "objectUrls", /* @__PURE__ */ new Map());
332
+ U(this, "objectUrlPaths", /* @__PURE__ */ new Map());
333
+ /**
334
+ * Reads that already failed, so a missing resource is not requested again by
335
+ * the visual adapter after the reference prefetch has recorded it.
336
+ */
337
+ U(this, "failures", /* @__PURE__ */ new Map());
338
+ U(this, "pending", /* @__PURE__ */ new Map());
339
+ U(this, "localPackage");
340
+ U(this, "disposed", !1);
341
+ this.source = t;
342
+ for (const s of t.resourcePaths ?? []) {
343
+ const i = E(s);
344
+ i && this.manifestPaths.add(i);
345
+ }
346
+ const n = t.original;
347
+ if (this.localPackage = it(n) || typeof Blob < "u" && n instanceof Blob || n instanceof ArrayBuffer || typeof n == "object" && n !== null && "kind" in n && n.kind === "buffer", it(n))
348
+ for (const s of n.files) this.entries.set(E(s.path), s.file);
349
+ else t.entryBlob && Tt(n) ? this.entries.set(t.entryPath ?? n.entryPath, t.entryBlob) : typeof Blob < "u" && n instanceof Blob ? this.entries.set(t.entryPath ?? t.filename ?? "asset", n) : n instanceof ArrayBuffer ? this.entries.set(t.entryPath ?? t.filename ?? "asset", new Blob([n])) : typeof n == "object" && n !== null && "kind" in n && n.kind === "buffer" && this.entries.set(t.entryPath ?? t.filename ?? "asset", new Blob([n.buffer]));
350
+ }
351
+ paths() {
352
+ return [.../* @__PURE__ */ new Set([...this.entries.keys(), ...this.manifestPaths])];
353
+ }
354
+ hasLocalFiles() {
355
+ return this.localPackage;
356
+ }
357
+ put(t, n) {
358
+ const s = E(t);
359
+ this.entries.set(s, n), this.manifestPaths.add(s);
360
+ }
361
+ hasBlob(t) {
362
+ const n = this.find(t);
363
+ return n !== null && this.entries.has(n);
364
+ }
365
+ relativePaths(t = "", n = !1) {
366
+ return [...this.relativeEntries(t, n)].map(([s]) => s);
367
+ }
368
+ asSourceRecord(t = "", n = !1) {
369
+ return Object.fromEntries(this.relativeEntries(t, n));
370
+ }
371
+ *relativeEntries(t = "", n = !1) {
372
+ const s = E(t), i = s ? `${s}/` : "";
373
+ for (const [r, o] of this.entries)
374
+ if (!i || r.startsWith(i)) yield [i ? r.slice(i.length) : r, o];
375
+ else if (n) {
376
+ const f = s.split("/"), c = r.split("/");
377
+ let l = 0;
378
+ for (; l < f.length && f[l] === c[l]; ) l++;
379
+ yield [[...f.slice(l).map(() => ".."), ...c.slice(l)].join("/"), o];
380
+ }
381
+ }
382
+ resolve(t, n = "") {
383
+ const s = this.find(t, n);
384
+ if (s) return s;
385
+ const i = E(n).split("/").slice(0, -1).join("/");
386
+ if (this.source.url) return E(`${i}/${t.replace(/^package:\/\//, "")}`);
387
+ throw O(`Simulation resource not found: ${t}`, "E_ASSET_RESOURCE_NOT_FOUND");
388
+ }
389
+ find(t, n = "") {
390
+ const s = E(t.replace(/^package:\/\//, ""));
391
+ if (R(s)) return this.entries.has(s) ? s : null;
392
+ const i = E(n).split("/").slice(0, -1).join("/");
393
+ for (const o of [E(`${i}/${t.replace(/^package:\/\//, "")}`), s])
394
+ if (this.entries.has(o)) return o;
395
+ const r = this.paths().filter(
396
+ (o) => !R(o) && (o === s || o.endsWith(`/${s}`) || s.endsWith(`/${o}`))
397
+ );
398
+ return r.length === 1 ? r[0] : null;
399
+ }
400
+ async text(t) {
401
+ await this.ensure(t);
402
+ const n = this.entries.get(this.resolve(t));
403
+ if (!n) throw O(`Simulation resource not found: ${t}`, "E_ASSET_RESOURCE_NOT_FOUND");
404
+ return n.text();
405
+ }
406
+ async bytes(t) {
407
+ await this.ensure(t);
408
+ const n = this.entries.get(this.resolve(t));
409
+ if (!n) throw O(`Simulation resource not found: ${t}`, "E_ASSET_RESOURCE_NOT_FOUND");
410
+ return new Uint8Array(await n.arrayBuffer());
411
+ }
412
+ getBlob(t) {
413
+ const n = this.resolve(t), s = this.entries.get(n);
414
+ if (!s) throw O(`Simulation resource not found: ${t}`, "E_ASSET_RESOURCE_NOT_FOUND");
415
+ return s;
416
+ }
417
+ async ensure(t, n = this.source.signal) {
418
+ const s = n instanceof AbortSignal ? { signal: n } : n;
419
+ if (this.disposed) throw new DOMException("The resource package was disposed.", "AbortError");
420
+ const i = E(t);
421
+ if (this.entries.has(i)) return;
422
+ const r = (s == null ? void 0 : s.signal) ?? this.source.signal;
423
+ if (!r) throw O(`No abort signal is available to read ${t}`, "E_ASSET_SIGNAL_REQUIRED");
424
+ x(r);
425
+ const o = this.failures.get(i);
426
+ if (o) throw o;
427
+ let f = this.pending.get(i);
428
+ if (!f) {
429
+ const l = new AbortController(), y = () => l.abort();
430
+ this.source.signal.aborted ? l.abort() : this.source.signal.addEventListener("abort", y, { once: !0 }), l.signal.addEventListener("abort", () => this.source.signal.removeEventListener("abort", y), { once: !0 });
431
+ const S = {
432
+ controller: l,
433
+ waiters: 0,
434
+ promise: Promise.resolve().then(() => this.readResource(i, t, l.signal, s ?? {}))
435
+ };
436
+ this.pending.set(i, S);
437
+ const I = () => {
438
+ this.source.signal.removeEventListener("abort", y), this.pending.get(i) === S && this.pending.delete(i);
439
+ };
440
+ S.promise.then(I, I), f = S;
441
+ }
442
+ const c = f;
443
+ return c.waiters++, new Promise((l, y) => {
444
+ let S = !1;
445
+ const I = () => S ? !1 : (S = !0, r.removeEventListener("abort", g), c.controller.signal.removeEventListener("abort", g), c.waiters--, !0), g = () => {
446
+ I() && (c.waiters === 0 && this.pending.get(i) === c && (this.pending.delete(i), c.controller.abort()), y(new DOMException("The operation was aborted.", "AbortError")));
447
+ };
448
+ r.addEventListener("abort", g, { once: !0 }), c.controller.signal.addEventListener("abort", g, { once: !0 }), (r.aborted || c.controller.signal.aborted) && g(), c.promise.then(
449
+ () => {
450
+ I() && l();
451
+ },
452
+ (v) => {
453
+ I() && y(v);
454
+ }
455
+ );
456
+ });
457
+ }
458
+ async readResource(t, n, s, i) {
459
+ try {
460
+ x(s);
461
+ const r = await this.source.resolve(t, { ...i, signal: s });
462
+ if (x(s), !/^https?:\/\//i.test(r) && !/^blob:/i.test(r))
463
+ throw O(`Simulation resource not found: ${n}. Upload the complete asset folder or provide a resourceResolver/baseUrl.`, X);
464
+ const o = await fetch(r, { signal: s });
465
+ if (!o.ok) {
466
+ const l = o.status === 401 || o.status === 403 ? ft : o.status === 404 || o.status === 410 ? X : dt;
467
+ throw O(`Simulation resource request failed (${o.status}): ${r}`, l);
468
+ }
469
+ const f = await o.blob();
470
+ x(s);
471
+ const c = await ht(f);
472
+ if (x(s), c) throw O(`Simulation resource is not usable (${c}): ${r}`, mt);
473
+ this.entries.set(t, f);
474
+ } catch (r) {
475
+ throw s.aborted ? new DOMException("The operation was aborted.", "AbortError") : (pt(r) === "aborted" || this.failures.set(t, r instanceof Error ? r : new Error(String(r))), r);
476
+ }
477
+ }
478
+ /** The recorded failure for a resource, if a previous read already failed. */
479
+ failureOf(t) {
480
+ return this.failures.get(E(t));
481
+ }
482
+ objectUrl(t) {
483
+ const n = this.resolve(t);
484
+ let s = this.objectUrls.get(n);
485
+ return s || (s = URL.createObjectURL(this.getBlob(n)), this.objectUrls.set(n, s), this.objectUrlPaths.set(s, n)), s;
486
+ }
487
+ pathForObjectUrl(t) {
488
+ return this.objectUrlPaths.get(t) ?? null;
489
+ }
490
+ /**
491
+ * Resolve a texture relative to the directory containing the entry layer.
492
+ * Pass the entry directory, not the entry file path.
493
+ */
494
+ findTexture(t, n = "") {
495
+ const s = this.findPresent(t, n);
496
+ if (s) return s;
497
+ const i = this.udimTextureSet(t, n);
498
+ return (i == null ? void 0 : i.paths.length) === 1 ? i.paths[0] : null;
499
+ }
500
+ udimTextureSet(t, n = "") {
501
+ const s = this.bindPackageReference(t, n), i = this.texturesUnderEntryDirectory(n);
502
+ return jt(s, i.length ? i : this.paths());
503
+ }
504
+ /**
505
+ * Package files below the entry directory named like UDIM tiles. A set the
506
+ * entry directory does not contain falls back to the whole package.
507
+ */
508
+ texturesUnderEntryDirectory(t) {
509
+ const n = E(t);
510
+ return n ? this.paths().filter((s) => s.startsWith(`${n}/`) && /\d{4}\.[^/]*$/i.test(s)) : [];
511
+ }
512
+ objectUrlIfPresent(t, n = "") {
513
+ const s = this.findTexture(t, n);
514
+ return s && this.entries.has(s) ? this.objectUrl(s) : null;
515
+ }
516
+ findPresent(t, n = "") {
517
+ if (R(t)) {
518
+ if (this.entries.has(t)) return t;
519
+ const c = this.bindPackageReference(t, n);
520
+ return c === t ? null : this.findPresent(c, n);
521
+ }
522
+ const s = E(n), i = s ? `${s}/` : "", r = E(t), o = E(i ? `${i}${r}` : r);
523
+ if (this.entries.has(o)) return o;
524
+ const f = i ? this.paths().filter((c) => {
525
+ if (!c.startsWith(i)) return !1;
526
+ const l = c.slice(i.length);
527
+ return l === r || r.endsWith(`/${l}`);
528
+ }) : [];
529
+ return f.length === 1 ? f[0] : this.find(t);
530
+ }
531
+ /**
532
+ * Rewrites a protocol reference (`https://bucket/asset/textures/a.png`,
533
+ * `file:///asset/textures/a.png`) into the package path it names, relative to
534
+ * the entry directory, preferring the longest tail of the URL path that
535
+ * repeats inside the package. The file name alone is never enough to bind,
536
+ * so an unrelated same-name file elsewhere in the package is not guessed.
537
+ * References that are not URLs, or that bind to nothing, are returned as-is.
538
+ */
539
+ bindPackageReference(t, n = "") {
540
+ const s = E(n), i = s ? `${s}/` : "", r = Dt(t).flatMap((f) => f.split("/")).filter(Boolean);
541
+ if (!r.length) return t;
542
+ let o = null;
543
+ for (const f of this.paths()) {
544
+ if (R(f) || i && !f.startsWith(i)) continue;
545
+ const c = f.split("/");
546
+ let l = 0;
547
+ for (; l < r.length && l < c.length && At(r.at(-1 - l), c.at(-1 - l)); ) l += 1;
548
+ l < 2 || (!o || l > o.depth) && (o = { path: f, depth: l });
549
+ }
550
+ return o ? i && o.path.startsWith(i) ? o.path.slice(i.length) : o.path : t;
551
+ }
552
+ dispose() {
553
+ this.disposed = !0;
554
+ for (const t of this.pending.values()) t.controller.abort();
555
+ this.pending.clear();
556
+ for (const t of this.objectUrls.values()) URL.revokeObjectURL(t);
557
+ this.objectUrls.clear(), this.objectUrlPaths.clear(), this.entries.clear(), this.manifestPaths.clear(), this.failures.clear();
558
+ }
559
+ }
560
+ async function zt(e, t) {
561
+ if (t.hasLocalFiles()) return t.text(e.entryPath ?? e.filename ?? t.paths()[0]);
562
+ if (e.entryBlob) return e.entryBlob.text();
563
+ const n = await fetch(e.url ?? await e.resolve(e.entryPath ?? ""), { signal: e.signal });
564
+ if (!n.ok) throw O(`Simulation asset request failed: ${n.status}`, "E_ASSET_FETCH");
565
+ return n.text();
566
+ }
567
+ function Wt(e) {
568
+ return E(e);
569
+ }
570
+ function E(e) {
571
+ if (R(e)) return e;
572
+ const t = [];
573
+ for (const n of e.replaceAll("\\", "/").split("/"))
574
+ !n || n === "." || (n === ".." && t.length && t.at(-1) !== ".." ? t.pop() : t.push(n));
575
+ return t.join("/");
576
+ }
577
+ function R(e) {
578
+ return /^[a-z][a-z\d+.-]*:/i.test(e) && !/^package:\/\//i.test(e);
579
+ }
580
+ function At(e, t) {
581
+ const n = e.search(/[?#]/);
582
+ return t === (n < 0 ? e : e.slice(0, n));
583
+ }
584
+ function Rt(e) {
585
+ try {
586
+ return decodeURIComponent(e);
587
+ } catch {
588
+ return e;
589
+ }
590
+ }
591
+ function Dt(e) {
592
+ if (!R(e) || /^(?:data|blob):/i.test(e)) return [];
593
+ let t;
594
+ try {
595
+ t = new URL(e, "file:///").pathname;
596
+ } catch {
597
+ t = e.replace(/^[a-z][a-z\d+.-]*:\/\/[^/]*/i, "").replace(/[?#].*$/, "");
598
+ }
599
+ return t.split("/").filter(Boolean).map(Rt);
600
+ }
601
+ function it(e) {
602
+ return typeof e == "object" && e !== null && "kind" in e && e.kind === "files";
603
+ }
604
+ function Tt(e) {
605
+ return typeof e == "object" && e !== null && "kind" in e && e.kind === "remote-entry";
606
+ }
607
+ function O(e, t) {
608
+ return Object.assign(new Error(e), { code: t });
609
+ }
610
+ function Gt() {
611
+ return { errors: [], warnings: [], approximations: [], droppedFeatures: [] };
612
+ }
613
+ function Yt(e, t = {}) {
614
+ const n = Object.freeze({ schemaVersion: 1, ...e });
615
+ return xt(n, t), n;
616
+ }
617
+ function xt(e, t = {}) {
618
+ if (!e.id.trim() || !e.name.trim()) throw B("Asset IR id and name are required.", "E_ASSET_IR_INVALID");
619
+ if (e.source.format !== "usd" && e.links.length === 0) throw B("Asset IR requires at least one link.", "E_ASSET_IR_INVALID");
620
+ const n = /* @__PURE__ */ new Set();
621
+ for (const i of e.links) {
622
+ if (!i.id || n.has(i.id)) throw B(`Invalid or duplicate link: ${i.id}`, "E_ASSET_IR_INVALID");
623
+ n.add(i.id);
624
+ }
625
+ const s = /* @__PURE__ */ new Set();
626
+ for (const i of e.joints) {
627
+ if (!i.id || s.has(i.id) || !t.allowInvalidGraph && (!n.has(i.parent) || !n.has(i.child)))
628
+ throw B(`Invalid joint: ${i.id}`, "E_ASSET_IR_INVALID");
629
+ s.add(i.id);
630
+ }
631
+ }
632
+ function B(e, t) {
633
+ return Object.assign(new Error(e), { code: t });
634
+ }
635
+ const Bt = {
636
+ refit() {
637
+ },
638
+ dispose() {
639
+ }
640
+ };
641
+ function qt(e, t = e.geometry) {
642
+ const n = t;
643
+ let s = n.boundsTree, i = !1;
644
+ if (!s) {
645
+ try {
646
+ s = new bt(t, { targetLeafSize: 20, indirect: !0, verbose: !1 });
647
+ } catch {
648
+ return Bt;
649
+ }
650
+ n.boundsTree = s, i = !0;
651
+ }
652
+ const r = e.raycast;
653
+ e.raycast = _t;
654
+ let o = !1;
655
+ return {
656
+ refit() {
657
+ o || s.refit();
658
+ },
659
+ dispose() {
660
+ o || (o = !0, e.raycast = r, i && n.boundsTree === s && delete n.boundsTree);
661
+ }
662
+ };
663
+ }
664
+ export {
665
+ Vt as S,
666
+ Ft as a,
667
+ Jt as b,
668
+ Yt as c,
669
+ Gt as d,
670
+ zt as e,
671
+ M as f,
672
+ Wt as n,
673
+ qt as p,
674
+ jt as r,
675
+ $t as u,
676
+ Ct as w
677
+ };