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