@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,719 @@
1
+ import { c as Ie, d as Ue, p as Ve, S as Le, w as ke, e as Je, a as oe, b as $e } from "./dynamic-raycast-Ctu2X5u5.js";
2
+ import { F as xe, G as ze, E as Xe, D as ue, d as We, H as Fe, S as Re } from "./index-DyBfusq5.js";
3
+ import { CylinderGeometry as Ye, MeshStandardMaterial as _e, InstancedMesh as Be, DynamicDrawUsage as He, BufferGeometry as Ke, BufferAttribute as Ge, DoubleSide as Qe, Mesh as Ze, DataTexture as et, RGBAFormat as tt, UnsignedByteType as rt, SRGBColorSpace as st, NoColorSpace as at, RepeatWrapping as nt, LinearFilter as it, LinearMipmapLinearFilter as lt, Vector3 as Y, Quaternion as ot, Matrix4 as ut } from "three";
4
+ import { a as ft, r as ct } from "./spark-renderer-dvFIUjr0.js";
5
+ const Z = 0.7, fe = 0.2, dt = 110 * Math.PI / 180;
6
+ function mt(l, e = {}) {
7
+ const g = l.getSnapshot();
8
+ if (g.controlMode !== "kinematic" || g.playing) return;
9
+ const b = [], s = {};
10
+ let a = Z;
11
+ for (const m of g.joints) {
12
+ if (m.mimic || !["revolute", "prismatic"].includes(m.type) || !Number.isFinite(m.defaultValue)) continue;
13
+ const A = m.controlLimits ?? m.limits;
14
+ if (!A || !Number.isFinite(A.lower) || !Number.isFinite(A.upper) || A.lower >= A.upper) continue;
15
+ const _ = m.defaultValue;
16
+ if (_ < A.lower || _ > A.upper) continue;
17
+ const j = Math.abs(A.upper - _) >= Math.abs(A.lower - _) ? A.upper : A.lower, R = Math.abs(j - _), v = m.type === "revolute" ? Math.max(0.6, R / dt) : Math.max(0.6, 0.8 * R / (A.upper - A.lower));
18
+ if (!Number.isFinite(v) || v <= 0) continue;
19
+ const G = a;
20
+ a = G + 2 * v + fe, s[m.id] = _, b.push({ id: m.id, home: _, far: j, duration: v, start: G, end: a });
21
+ }
22
+ if (!b.length) return;
23
+ const d = e.autoPlay !== !1;
24
+ let n = d ? 0 : Z, i = d, w = !1, M = !1, o = !1, y = !d, c = !1, F = !0, u = d ? !0 : null;
25
+ const P = (m) => {
26
+ var A;
27
+ if (F) {
28
+ u = m;
29
+ return;
30
+ }
31
+ u = null, (A = e.onPlayingChange) == null || A.call(e, m);
32
+ }, h = (m) => {
33
+ if (i !== m) {
34
+ if (i = m, m) {
35
+ if (l.setPreviewing(!0), !i || c) return;
36
+ l.setPlaying(!0);
37
+ } else
38
+ l.setPlaying(!1), l.setPreviewing(!1);
39
+ i === m && P(m);
40
+ }
41
+ }, C = () => {
42
+ c || (d && !M && (o = !0), y = !0, w = !1, h(!1));
43
+ }, L = l.onManualInput(C), O = {
44
+ get playing() {
45
+ return i;
46
+ },
47
+ get finished() {
48
+ return w;
49
+ },
50
+ cancel: C,
51
+ play() {
52
+ if (!(c || i)) {
53
+ if (y || w) {
54
+ if (l.resetPreviewPose(), c) return;
55
+ n = Z, w = !1, y = !1;
56
+ }
57
+ h(!0);
58
+ }
59
+ },
60
+ pause() {
61
+ c || (d && !M && (o = !0), h(!1));
62
+ },
63
+ reset() {
64
+ c || (d && !M && (o = !0), h(!1), !c && (n = Z, w = !1, y = !1, l.resetPreviewPose()));
65
+ },
66
+ dispose() {
67
+ c || (c = !0, L(), h(!1), u = null);
68
+ },
69
+ update(m) {
70
+ var R, v;
71
+ if (c) return !1;
72
+ if (u !== null) {
73
+ const G = u;
74
+ u = null, (R = e.onPlayingChange) == null || R.call(e, G);
75
+ }
76
+ if (!i || c || !Number.isFinite(m) || m <= 0 || (n += m, n < Z)) return !1;
77
+ if (n >= a)
78
+ return w = !0, y = !0, h(!1), c ? !1 : (l.resetPreviewPose(), d && !o && !M && (M = !0, (v = e.onFinished) == null || v.call(e)), !0);
79
+ const A = b.find((G) => n < G.end), _ = n - A.start;
80
+ let j;
81
+ if (_ < A.duration)
82
+ j = A.home + (A.far - A.home) * De(_ / A.duration);
83
+ else if (_ < A.duration + fe)
84
+ j = A.far;
85
+ else {
86
+ const G = (_ - A.duration - fe) / A.duration;
87
+ j = A.far + (A.home - A.far) * De(G);
88
+ }
89
+ return l.setPreviewPose({ ...s, [A.id]: j }), !0;
90
+ }
91
+ };
92
+ return d && (l.setPreviewing(!0), i && !c && l.setPlaying(!0)), F = !1, O;
93
+ }
94
+ function De(l) {
95
+ return l * l * l * (l * (6 * l - 15) + 10);
96
+ }
97
+ async function pt(l, e, g) {
98
+ var M;
99
+ const b = /* @__PURE__ */ new Map(), s = /* @__PURE__ */ new Set(), a = /* @__PURE__ */ new Set(), d = /* @__PURE__ */ new Set(), n = async (o, y) => {
100
+ if (d.has(o)) throw ce(`MJCF include cycle: ${o}`, "E_MJCF_INCLUDE_CYCLE");
101
+ d.add(o);
102
+ const c = wt(y, o);
103
+ b.set(o, c);
104
+ for (const F of c.querySelectorAll("worldbody body[name]")) s.add(F.getAttribute("name"));
105
+ for (const F of c.querySelectorAll("flexcomp[name], flex[name]")) a.add(F.getAttribute("name"));
106
+ for (const F of c.querySelectorAll("include[file]")) {
107
+ const u = g.resolve(F.getAttribute("file"), o);
108
+ await n(u, await g.text(u));
109
+ }
110
+ d.delete(o);
111
+ };
112
+ await n(e, l);
113
+ const i = (M = b.get(e)) == null ? void 0 : M.querySelector("mujoco"), w = [...b.values()].some(
114
+ (o) => o.querySelector("flexcomp, deformable flex") != null
115
+ );
116
+ if (!i || s.size === 0 && !w)
117
+ throw ce("MJCF requires a mujoco root and at least one named body or deformable flex.", "E_MJCF_PARSE");
118
+ return Ie({
119
+ id: i.getAttribute("model") || e,
120
+ name: i.getAttribute("model") || e,
121
+ source: { format: "mjcf", uri: e },
122
+ links: [...s.size > 0 ? s : a].map((o) => ({ id: o, name: o, visuals: [], collisions: [] })),
123
+ joints: [],
124
+ diagnostics: Ue(),
125
+ extension: { files: g, xml: l, documents: b }
126
+ });
127
+ }
128
+ async function yt(l) {
129
+ const e = l.extension.files, g = l.source.uri, b = g.split("/").slice(0, -1).join("/"), s = /* @__PURE__ */ new Map(), a = l.extension.documents, d = [...a.values()].map((o) => o.querySelector("compiler")).find((o) => o != null), n = (d == null ? void 0 : d.getAttribute("meshdir")) ?? (d == null ? void 0 : d.getAttribute("assetdir")) ?? "", i = (d == null ? void 0 : d.getAttribute("texturedir")) ?? (d == null ? void 0 : d.getAttribute("assetdir")) ?? "";
130
+ for (const [o, y] of a) {
131
+ const c = y.querySelector("compiler");
132
+ for (const F of y.querySelectorAll("asset [file]")) {
133
+ const u = (F.tagName === "texture" ? (c == null ? void 0 : c.getAttribute("texturedir")) ?? i : (c == null ? void 0 : c.getAttribute("meshdir")) ?? n) ?? (c == null ? void 0 : c.getAttribute("assetdir")) ?? "", P = e.resolve(`${u}/${F.getAttribute("file")}`, o);
134
+ await e.bytes(P);
135
+ }
136
+ for (const F of y.querySelectorAll("flexcomp[file], flex[file]")) {
137
+ const u = (c == null ? void 0 : c.getAttribute("meshdir")) ?? n, P = e.resolve(`${u}/${F.getAttribute("file")}`, o);
138
+ await e.bytes(P);
139
+ }
140
+ }
141
+ for (const o of gt(e.paths(), a)) {
142
+ if (o === g) continue;
143
+ const y = b && o.startsWith(`${b}/`) ? o.slice(b.length + 1) : o;
144
+ s.set(y, await e.bytes(o));
145
+ }
146
+ const w = l.extension.xml;
147
+ return { format: "mjcf", xml: ht(a) ? bt(w) : w, files: s };
148
+ }
149
+ function ht(l) {
150
+ return l.size === 0 || ![...l.values()].some((g) => g.querySelector("flexcomp, deformable flex") || g.querySelector("freejoint, actuator") ? !0 : [...g.querySelectorAll("joint")].some(
151
+ (b) => (b.getAttribute("type") ?? "hinge").toLowerCase() !== "fixed"
152
+ )) ? !1 : ![...l.values()].some(
153
+ (g) => [...g.querySelectorAll("worldbody geom")].some((b) => {
154
+ var s;
155
+ return ((s = b.getAttribute("type")) == null ? void 0 : s.toLowerCase()) === "plane";
156
+ })
157
+ );
158
+ }
159
+ function bt(l) {
160
+ const e = '<geom name="__viewer_default_ground__" type="plane" size="5 5 0.1" contype="1" conaffinity="1" friction="0.8 0.01 0.0002"/>';
161
+ return /<worldbody\b[^>]*>/i.test(l) ? l.replace(/(<worldbody\b[^>]*>)/i, `$1${e}`) : l.replace(/<\/mujoco\s*>/i, `<worldbody>${e}</worldbody></mujoco>`);
162
+ }
163
+ function gt(l, e) {
164
+ const g = (n) => n.replaceAll("\\", "/").split("/").pop() ?? "", b = /* @__PURE__ */ new Map();
165
+ for (const n of l) {
166
+ const i = g(n), w = b.get(i) ?? [];
167
+ w.push(n), b.set(i, w);
168
+ }
169
+ const s = new Set(e.keys()), a = /* @__PURE__ */ new Set(["include", "mesh", "texture", "hfield", "skin", "flexcomp", "flex"]), d = /* @__PURE__ */ new Set(["fileleft", "fileright", "fileup", "filedown", "filefront", "fileback"]);
170
+ for (const n of e.values()) {
171
+ if (n.querySelector("plugin, extension, model, attach")) return l;
172
+ for (const i of n.querySelectorAll("*"))
173
+ for (const w of i.attributes) {
174
+ if (!w.name.startsWith("file")) continue;
175
+ if (w.name === "file") {
176
+ if (!a.has(i.tagName)) return l;
177
+ } else if (!(i.tagName === "texture" && d.has(w.name)))
178
+ return l;
179
+ if (!w.value) continue;
180
+ const M = b.get(g(w.value));
181
+ if (!M) return l;
182
+ for (const o of M) s.add(o);
183
+ }
184
+ }
185
+ return l.filter((n) => s.has(n));
186
+ }
187
+ function wt(l, e) {
188
+ const g = new DOMParser().parseFromString(l, "application/xml"), b = g.querySelector("parsererror");
189
+ if (b) throw ce(`MJCF XML parse failed (${e}): ${b.textContent ?? ""}`, "E_MJCF_PARSE");
190
+ return g;
191
+ }
192
+ function ce(l, e) {
193
+ return Object.assign(new Error(l), { code: e });
194
+ }
195
+ function vt(l, e) {
196
+ var d;
197
+ const g = l.listFlexes();
198
+ if (!g.length) return null;
199
+ const b = [];
200
+ for (const n of g) {
201
+ if (n.vertexCount <= 0) continue;
202
+ const i = l.flexElements(n);
203
+ if (n.dim === 1) {
204
+ const c = new Ye(1, 1, 1, 8, 1, !1), F = new _e({
205
+ color: Ee(n.rgba),
206
+ roughness: 0.55,
207
+ metalness: 0,
208
+ transparent: n.rgba[3] < 1,
209
+ opacity: n.rgba[3]
210
+ }), u = new Be(c, F, Math.max(n.elementCount, 1));
211
+ u.instanceMatrix.setUsage(He), u.name = `flex:${n.name}`, u.userData.flexId = n.id, u.userData.flexName = n.name, u.userData.flexVertexCount = n.vertexCount, u.frustumCulled = !1, e.add(u), b.push({
212
+ flex: n,
213
+ geometry: c,
214
+ material: F,
215
+ object: u,
216
+ mesh: !1,
217
+ tube: {
218
+ elements: i,
219
+ radius: Math.max(1e-3, Number(((d = l.model) == null ? void 0 : d.flex_radius[n.id]) ?? 5e-3)),
220
+ mesh: u
221
+ }
222
+ });
223
+ continue;
224
+ }
225
+ const w = n.dim === 2 ? Array.from(i) : St(i);
226
+ if (!w.length) continue;
227
+ const M = new Ke();
228
+ M.setAttribute("position", new Ge(new Float32Array(n.vertexCount * 3), 3)), M.setIndex(w);
229
+ const o = new _e({
230
+ color: Ee(n.rgba),
231
+ side: Qe,
232
+ roughness: 0.55,
233
+ metalness: 0,
234
+ transparent: n.rgba[3] < 1,
235
+ opacity: n.rgba[3]
236
+ });
237
+ o.polygonOffset = !0, o.polygonOffsetFactor = -1, o.polygonOffsetUnits = -2, At(l, n.id, M, o, n.rgba);
238
+ const y = new Ze(M, o);
239
+ y.name = `flex:${n.name}`, y.userData.flexId = n.id, y.userData.flexName = n.name, y.userData.flexVertexCount = n.vertexCount, y.frustumCulled = !1, e.add(y), b.push({
240
+ flex: n,
241
+ attribute: M.getAttribute("position"),
242
+ geometry: M,
243
+ material: o,
244
+ object: y,
245
+ mesh: !0
246
+ });
247
+ }
248
+ if (!b.length) return null;
249
+ let s = 0;
250
+ const a = () => {
251
+ const n = s++;
252
+ for (const i of b) {
253
+ const w = l.readFlexVertices(i.flex.name);
254
+ if (!w) continue;
255
+ if (i.tube) {
256
+ const { elements: y, radius: c, mesh: F } = i.tube, u = new Y(), P = new Y(), h = new Y(), C = new Y(), L = new Y(0, 1, 0), O = new ot(), m = new Y(), A = new ut(), _ = Math.min(F.count, Math.floor(y.length / 2));
257
+ for (let j = 0; j < _; j++) {
258
+ const R = y[j * 2] * 3, v = y[j * 2 + 1] * 3;
259
+ u.set(w[R], w[R + 1], w[R + 2]), P.set(w[v], w[v + 1], w[v + 2]), C.subVectors(P, u);
260
+ const G = C.length();
261
+ h.addVectors(u, P).multiplyScalar(0.5), G > 1e-9 ? (O.setFromUnitVectors(L, C.divideScalar(G)), m.set(c, G, c)) : (O.identity(), m.set(c, 1e-6, c)), A.compose(h, O, m), F.setMatrixAt(j, A);
262
+ }
263
+ F.instanceMatrix.needsUpdate = !0, F.computeBoundingSphere();
264
+ continue;
265
+ }
266
+ if (!i.attribute) continue;
267
+ const M = i.attribute.array, o = Math.min(w.length, M.length);
268
+ for (let y = 0; y < o; y++) M[y] = w[y];
269
+ if (i.attribute.needsUpdate = !0, i.mesh) {
270
+ const y = i.flex.vertexCount > 2e4 ? 3 : i.flex.vertexCount > 4e3 ? 2 : 1;
271
+ (y === 1 || n % y === 0) && i.geometry.computeVertexNormals(), (i.flex.vertexCount <= 4e3 || n % 2 === 0) && (i.geometry.computeBoundingSphere(), i.geometry.computeBoundingBox()), i.raycast ?? (i.raycast = Ve(i.object, i.geometry)), i.raycast.refit();
272
+ }
273
+ }
274
+ };
275
+ return a(), {
276
+ sync: a,
277
+ dispose() {
278
+ var n;
279
+ for (const i of b)
280
+ (n = i.raycast) == null || n.dispose(), i.object.removeFromParent(), i.geometry.dispose(), i.material.dispose();
281
+ }
282
+ };
283
+ }
284
+ function At(l, e, g, b, s) {
285
+ var R;
286
+ const a = l.model, d = l.mujoco;
287
+ if (!a || !d) return;
288
+ const n = Number(a.flex_matid[e]), i = n >= 0 ? Array.from(a.mat_rgba.slice(n * 4, n * 4 + 4)) : s;
289
+ if (b.color.setRGB(i[0], i[1], i[2]), b.opacity = i[3], b.transparent = i[3] < 1, n < 0) return;
290
+ b.roughness = xe(a, n), b.metalness = ze(a, n);
291
+ const w = d.mjtTextureRole.mjNTEXROLE.value, M = Number(a.mat_texid[n * w + 1]);
292
+ if (M < 0 || a.tex_type[M] !== d.mjtTexture.mjTEXTURE_2D.value) return;
293
+ const o = Number(a.tex_width[M]), y = Number(a.tex_height[M]), c = Number(a.tex_nchannel[M]);
294
+ if (![1, 2, 3, 4].includes(c) || o <= 0 || y <= 0) return;
295
+ const F = Number(a.tex_adr[M]), u = new Uint8Array(o * y * 4);
296
+ for (let v = 0; v < o * y; v++) {
297
+ const G = F + v * c, D = v * 4, V = a.tex_data[G];
298
+ u[D] = V, u[D + 1] = c >= 3 ? a.tex_data[G + 1] : V, u[D + 2] = c >= 3 ? a.tex_data[G + 2] : V, u[D + 3] = c === 4 ? a.tex_data[G + 3] : c === 2 ? a.tex_data[G + 1] : 255;
299
+ }
300
+ const P = new et(u, o, y, tt, rt);
301
+ P.colorSpace = a.tex_colorspace[M] === d.mjtColorSpace.mjCOLORSPACE_SRGB.value ? st : at, P.wrapS = P.wrapT = nt, P.magFilter = it, P.minFilter = lt, P.generateMipmaps = !0, P.repeat.set(Number(a.mat_texrepeat[n * 2]), Number(a.mat_texrepeat[n * 2 + 1])), P.needsUpdate = !0;
302
+ const h = g.getAttribute("position");
303
+ let C = 1 / 0, L = -1 / 0, O = 1 / 0, m = -1 / 0;
304
+ for (let v = 0; v < h.count; v++)
305
+ C = Math.min(C, h.getX(v)), L = Math.max(L, h.getX(v)), O = Math.min(O, h.getY(v)), m = Math.max(m, h.getY(v));
306
+ const A = Math.max(L - C, 1e-6), _ = Math.max(m - O, 1e-6), j = new Float32Array(h.count * 2);
307
+ for (let v = 0; v < h.count; v++)
308
+ j[v * 2] = (h.getX(v) - C) / A, j[v * 2 + 1] = 1 - (h.getY(v) - O) / _;
309
+ g.setAttribute("uv", new Ge(j, 2)), (R = b.map) == null || R.dispose(), b.map = P, b.needsUpdate = !0;
310
+ }
311
+ function St(l) {
312
+ const e = /* @__PURE__ */ new Map(), g = (s, a, d) => {
313
+ const i = [s, a, d].sort((M, o) => M - o).join(","), w = e.get(i);
314
+ w ? w.count++ : e.set(i, { triangle: [s, a, d], count: 1 });
315
+ };
316
+ for (let s = 0; s + 3 < l.length; s += 4) {
317
+ const a = l[s], d = l[s + 1], n = l[s + 2], i = l[s + 3];
318
+ g(a, n, d), g(a, d, i), g(a, i, n), g(d, n, i);
319
+ }
320
+ const b = [];
321
+ for (const s of e.values())
322
+ s.count === 1 && b.push(...s.triangle);
323
+ return b;
324
+ }
325
+ function Ee(l) {
326
+ const e = (g) => Math.max(0, Math.min(255, Math.round(g * 255)));
327
+ return e(l[0]) << 16 | e(l[1]) << 8 | e(l[2]);
328
+ }
329
+ function Rt(l, e) {
330
+ const g = new Le(l), b = e.format === "usd" || e.format === "usda" || e.format === "usdc" || e.format === "usdz";
331
+ let s = null, a = null, d = null, n = null, i = null, w = null, M = null, o = null, y = null, c = null, F = null, u = !1, P = !1, h, C;
332
+ const L = () => {
333
+ C == null || C.cancel();
334
+ }, O = () => {
335
+ const r = C;
336
+ C = void 0, r == null || r.dispose();
337
+ };
338
+ let m = !1, A = !1, _ = null, j = !1, R = null, v = null, G, D = null, V = null, t = null, f = null, W = null, N = null, ee = null, J = !1, B = !1, U;
339
+ const me = {};
340
+ let H = null, te = "z", ne = !1;
341
+ const K = () => {
342
+ t && (te = e.options.upAxis ?? "z", ne = !0, t.setUpAxis(te) && (a == null || a.sync(), f == null || f.sync()));
343
+ }, Q = (r = !1) => {
344
+ if (!s || !h || typeof s.getJointValues != "function") return;
345
+ const p = h.getSnapshot().joints.map((E) => E.id);
346
+ h.syncValues(s.getJointValues(p), r);
347
+ }, $ = (r, p) => {
348
+ var T;
349
+ const E = oe(r, e.signal, `${e.format}:${p}`);
350
+ P = !1, O(), h == null || h.dispose(), h = void 0, y = null, m = !1, v = { ...E, stage: p };
351
+ try {
352
+ t == null || t.endGrab();
353
+ } catch {
354
+ }
355
+ (T = e.reportSessionState) == null || T.call(e), e.reportError(r, { fatal: !1, stage: p, code: E.code });
356
+ }, Ne = (r) => {
357
+ var E, T;
358
+ if (u) {
359
+ r.dispose();
360
+ return;
361
+ }
362
+ const p = (((E = r.runtime) == null ? void 0 : E.listFlexes().length) ?? 0) > 0;
363
+ N = null, J = !1, D = r.asset, d = r.dispose, j = r.hasAnimation === !0, n = r.hasPhysics || j ? r.step : null, i = r.reset, P = !r.hasPhysics && r.hasAnimation === !0, m = r.hasPhysics, R = r.fallback ? { ...r.fallback, stage: "physics-fallback" } : r.visualWarning ? { ...r.visualWarning, stage: "visual-adaptation" } : null, a = r.binding, s = r.runtime ?? null, V = r.physics ?? null, H = r.inspect ?? null, h = r.inspection, y = h ?? null, U = r.overlays;
364
+ for (const [q, x] of Object.entries(me)) U == null || U.set(q, x);
365
+ if (f = r.collision ?? null, W = r.createCollision ?? null, t == null || t.setRuntime(r.runtime), t == null || t.replaceModel(r.binding.root), f && (f.root.visible = !1, t == null || t.attachAuxiliary(f.root), f.sync()), h && (C = mt(h, {
366
+ autoPlay: e.options.jointIntro !== !1 && !(typeof globalThis.matchMedia == "function" && globalThis.matchMedia("(prefers-reduced-motion: reduce)").matches),
367
+ onPlayingChange(q) {
368
+ var x;
369
+ P = q, u || ((x = e.reportSessionState) == null || x.call(e), e.requestRender());
370
+ },
371
+ onFinished() {
372
+ u || h == null || h.notifyIntroFinished();
373
+ }
374
+ }), C && (P = C.playing)), K(), (T = e.reportSessionState) == null || T.call(e), e.requestRender(), p && t) {
375
+ const q = t.getModelBounds();
376
+ q.isEmpty() || (e.fit(q, { padding: 1.15 }), e.requestRender());
377
+ }
378
+ }, Te = (r) => {
379
+ var E;
380
+ if (u) return;
381
+ const p = oe(r, e.signal, "usd-physics");
382
+ J = !1, v = { ...p, stage: "usd-physics" }, m = !1, j = (N == null ? void 0 : N.visual.hasAnimation) ?? j, n = N != null && N.visual.hasAnimation ? N.visual.step : null, i = N != null && N.visual.hasAnimation ? N.visual.reset : null, P = !!(N != null && N.visual.hasAnimation), (E = e.reportSessionState) == null || E.call(e), e.reportError(r, { fatal: !1, stage: "usd-physics", code: p.code });
383
+ }, pe = () => {
384
+ var r;
385
+ u || (u = !0, P = !1, O(), (r = e.reportSessionState) == null || r.call(e));
386
+ }, ye = () => {
387
+ _ && (ct(_), _ = null);
388
+ }, he = () => {
389
+ if (f) return !0;
390
+ const r = W;
391
+ return W = null, !r || !t || (f = r() ?? null, !f) ? !1 : (f.root.visible = !1, t.attachAuxiliary(f.root), f.sync(), !0);
392
+ };
393
+ let be = () => {
394
+ }, ge = () => {
395
+ }, we = !1;
396
+ const ie = new Promise((r, p) => {
397
+ be = r, ge = p;
398
+ }), re = () => {
399
+ we || (we = !0, be());
400
+ };
401
+ return (async () => {
402
+ var r, p, E, T, q, x, ve, Ae, Se, Me;
403
+ try {
404
+ const k = e.format;
405
+ if (k === "usd" || k === "usda" || k === "usdc" || k === "usdz") {
406
+ (r = e.reportProgress) == null || r.call(e, "usd-visual", "正在解析 USD 视觉场景...");
407
+ const S = await (await import("./usd-session-CLU3UR4C.js")).loadUsdSimulation(l, e, g);
408
+ if (u || e.signal.aborted)
409
+ throw S.dispose(), de();
410
+ N = S, a = { root: S.visual.root, sync() {
411
+ }, dispose() {
412
+ } }, d = S.dispose, j = S.visual.hasAnimation, A = S.visual.hasGaussianSplats, n = j ? S.visual.step : null, i = S.visual.reset, P = !1, J = !0, R = S.visual.warning ? { ...S.visual.warning, stage: "visual-adaptation" } : null, B = S.texturesPending, B && S.texturesReady.then(() => {
413
+ var z;
414
+ u || (B = !1, (z = e.reportSessionState) == null || z.call(e), e.requestRender());
415
+ }), (p = e.reportProgress) == null || p.call(e, "stage", "正在构建三维场景与环境光影..."), t = new ue(
416
+ S.visual.root,
417
+ e.options.simulationTheme,
418
+ void 0,
419
+ "simulation",
420
+ "visual",
421
+ S.allowEmptyStage
422
+ ), t.setCacheInvalidationCallback(e.invalidateTransmissionCaches), K(), re(), (E = e.reportProgress) == null || E.call(e, "usd-physics", "视觉场景已就绪,正在准备物理..."), ee = S.completePhysics(t.root).then(Ne).catch(Te);
423
+ return;
424
+ }
425
+ if (k === "urdf" || k === "xacro") {
426
+ (T = e.reportProgress) == null || T.call(e, "urdf", "正在解析 URDF 场景与运动学结构...");
427
+ const S = await (await import("./urdf-session-DRHt0jW4.js")).loadUrdfSimulation(l, e, g);
428
+ if (u || e.signal.aborted)
429
+ throw S.dispose(), de();
430
+ D = S.asset, t = S.stage, y = S.inspection, d = S.dispose, n = S.step, i = S.reset, w = S.play, M = S.pause, o = S.isPlaying, c = () => S.capabilities, F = () => S.physics, P = !1, m = !1, R = S.warning ?? null, K(), re();
431
+ return;
432
+ }
433
+ let se = l.entryPath ?? l.filename;
434
+ if (!se && k === "mjcf") {
435
+ const I = g.paths().filter((S) => S.toLowerCase().endsWith(".xml"));
436
+ for (const S of ["scene.xml", "model.xml", "main.xml"]) {
437
+ const z = I.find((X) => X.toLowerCase().endsWith(`/${S}`) || X.toLowerCase() === S);
438
+ if (z) {
439
+ se = z;
440
+ break;
441
+ }
442
+ }
443
+ }
444
+ se ?? (se = "asset.xml"), (q = e.reportProgress) == null || q.call(e, "fetch", "正在读取模型描述与依赖资源...");
445
+ const qe = await Je(l, g);
446
+ ae(u, e.signal), (x = e.reportProgress) == null || x.call(e, "parse", "正在解析 MJCF 场景与连杆..."), D = await pt(qe, se, g);
447
+ try {
448
+ (ve = e.reportProgress) == null || ve.call(e, "compile", "正在编译物理模型与参数...");
449
+ const I = await yt(D);
450
+ V = I, ae(u, e.signal), (Ae = e.reportProgress) == null || Ae.call(e, "runtime", "正在初始化物理内核...");
451
+ const { MujocoRuntime: S } = await import("./mujoco-runtime-CZSNQ7An.js");
452
+ s = new S(), await s.initialize(I, {
453
+ baseUrl: e.options.mujocoBaseUrl,
454
+ wasmModule: e.options.mujocoWasmModule
455
+ });
456
+ } catch (I) {
457
+ const S = oe(I, e.signal, "mjcf-physics");
458
+ s == null || s.dispose(), s = null;
459
+ const { compileMjcfPreview: z } = await import("./mjcf-visual-5WZebsIs.js");
460
+ let X;
461
+ try {
462
+ X = z(D);
463
+ } catch (le) {
464
+ throw new Error(`${S.message};独立视觉预览也不可用:${le instanceof Error ? le.message : String(le)}`);
465
+ }
466
+ a = { root: X, sync() {
467
+ }, dispose() {
468
+ We(X);
469
+ } }, R = { ...S, stage: "physics-fallback" }, t = new ue(X, e.options.simulationTheme, void 0, "simulation", "visual"), t.setCacheInvalidationCallback(e.invalidateTransmissionCaches), P = !1, K(), re();
470
+ return;
471
+ }
472
+ m = !0, ae(u, e.signal), (Se = e.reportProgress) == null || Se.call(e, "visual", "正在构建视觉渲染网格...");
473
+ const Oe = Fe(s, !1, D.diagnostics), Pe = D.diagnostics.droppedFeatures.find((I) => I.code.startsWith("MJCF_"));
474
+ Pe && (R = { ...Pe, stage: "visual-material" }), ae(u, e.signal), (Me = e.reportProgress) == null || Me.call(e, "stage", "正在构建三维场景与环境光影..."), a = new Re(s, Oe);
475
+ const Ce = vt(s, a.root);
476
+ if (Ce) {
477
+ const I = a.sync.bind(a);
478
+ a.sync = () => {
479
+ I(), Ce.sync();
480
+ };
481
+ }
482
+ t = new ue(a.root, e.options.simulationTheme, s), t.setCacheInvalidationCallback(e.invalidateTransmissionCaches);
483
+ const je = Fe(s, !0, D.diagnostics);
484
+ je.root.children.length > 0 ? (f = new Re(s, je, { accelerateRaycast: !1 }), f.root.visible = !1) : f = null, f && t.attachAuxiliary(f.root), K(), s.listFlexes().length === 0 ? (h = $e(
485
+ s,
486
+ "mjcf",
487
+ () => {
488
+ a == null || a.sync(), f == null || f.sync();
489
+ },
490
+ e.requestRender,
491
+ (I) => e.reportError(I, { fatal: !1, stage: "joint-inspection", code: "E_MUJOCO_JOINT_INSPECTION" })
492
+ ), y = h ?? null) : y = null, re();
493
+ } catch (k) {
494
+ throw t == null || t.dispose(), t = null, d ? (d(), d = null) : (f == null || f.dispose(), a == null || a.dispose(), s == null || s.dispose()), f = null, a = null, s = null, g.dispose(), V = null, N = null, k;
495
+ }
496
+ })().catch(ge), ie.catch(() => {
497
+ }), {
498
+ id: `simulation-${Math.random().toString(36).slice(2)}`,
499
+ get autoPlay() {
500
+ return C ? C.playing : !J && !m && j ? !0 : e.format === "urdf" || e.format === "xacro" || m ? !1 : void 0;
501
+ },
502
+ get inspection() {
503
+ return y;
504
+ },
505
+ setInspectionOverlay(r, p) {
506
+ if (!b || u) return !1;
507
+ me[r] = p;
508
+ const E = U == null ? void 0 : U.set(r, p);
509
+ return e.requestRender(), E ?? J;
510
+ },
511
+ getInspectionOverlayCapabilities() {
512
+ return U ? { jointAxis: U.has("jointAxis"), centerOfMass: U.has("centerOfMass"), inertia: U.has("inertia") } : null;
513
+ },
514
+ isPlaying() {
515
+ return (o == null ? void 0 : o()) ?? P;
516
+ },
517
+ opened: ie,
518
+ get root() {
519
+ return (t == null ? void 0 : t.root) ?? null;
520
+ },
521
+ getFitBounds() {
522
+ return (t == null ? void 0 : t.getModelBounds()) ?? null;
523
+ },
524
+ beforeRender(r, p, E) {
525
+ return A && (_ == null ? void 0 : _.scene) !== p && (ye(), _ = ft(r, p, e.requestRender)), t == null ? void 0 : t.beforeRender(r, p, E);
526
+ },
527
+ get capabilities() {
528
+ const r = f !== null || W !== null;
529
+ return v ? { canPlay: !1, canPause: !1, canReset: !1, hasPhysics: !1, hasAnimation: !1, canToggleGround: !0, canToggleCollision: r, canReorientUpAxis: !0 } : (c == null ? void 0 : c()) ?? { canPlay: m || j || !!C, canPause: m || j || !!C, canReset: m || j || !!h, hasPhysics: m, hasAnimation: j || !!C, canToggleGround: !0, canToggleCollision: r, canReorientUpAxis: !0 };
530
+ },
531
+ get warning() {
532
+ return R && !R.notice ? ke(R, R.stage) : R;
533
+ },
534
+ get physics() {
535
+ return v ? { status: "unavailable", issue: v } : F ? F() : J ? { status: "initializing" } : m ? { status: "ready" } : (R == null ? void 0 : R.stage) === "physics-fallback" ? { status: "unavailable", issue: R } : { status: "not-authored" };
536
+ },
537
+ get backgroundLoading() {
538
+ return B && J ? {
539
+ stage: "textures+physics",
540
+ message: "正在加载纹理并准备物理..."
541
+ } : B ? {
542
+ stage: "textures",
543
+ message: "正在加载纹理..."
544
+ } : J ? {
545
+ stage: "physics",
546
+ message: "正在准备物理..."
547
+ } : null;
548
+ },
549
+ setTheme(r) {
550
+ t == null || t.setTheme(r);
551
+ },
552
+ setGroundVisible(r) {
553
+ t == null || t.setGroundVisible(r);
554
+ },
555
+ getGroundVisible() {
556
+ return (t == null ? void 0 : t.getGroundVisible()) ?? !0;
557
+ },
558
+ getGroundGridScale() {
559
+ return (t == null ? void 0 : t.getGroundGridScale()) ?? Xe;
560
+ },
561
+ getUpAxis() {
562
+ return t && ne ? te : null;
563
+ },
564
+ setUpAxis(r) {
565
+ if (!(u || !t))
566
+ try {
567
+ if (!t.setUpAxis(r)) return;
568
+ te = r, a == null || a.sync(), f == null || f.sync(), e.requestRender();
569
+ } catch (p) {
570
+ $(p, "up-axis");
571
+ }
572
+ },
573
+ presentation: { preferredViewMode: "3d", interactionMode: "simulation", uiProfile: "simulation", fitPolicy: "fit-on-ready" },
574
+ beginDispose: pe,
575
+ setCollisionVisible(r) {
576
+ if (!u) {
577
+ if (c) {
578
+ const p = y;
579
+ p == null || p.setOverlay("collision", r);
580
+ return;
581
+ }
582
+ if (r && !he()) throw new Error("This asset has no collision display.");
583
+ f && (f.root.visible = r), e.requestRender();
584
+ }
585
+ },
586
+ getCollisionRoot() {
587
+ return u || c || !he() ? null : (f == null || f.sync(), (f == null ? void 0 : f.root) ?? null);
588
+ },
589
+ getVisualDiagnostics() {
590
+ return u || !D || D.source.format !== "usd" ? null : structuredClone({
591
+ report: D.extension.visualAdaptation,
592
+ diagnostics: D.diagnostics,
593
+ usd: (H == null ? void 0 : H()) ?? null,
594
+ softbody: D.extension.softbodyContracts ?? null
595
+ });
596
+ },
597
+ getPhysicsProperties() {
598
+ if (u || !m || !(s != null && s.model) || !s.mujoco) return null;
599
+ const r = s.model, p = s.mujoco;
600
+ return structuredClone({
601
+ units: { length: "m", mass: "kg", time: "s" },
602
+ timestep: r.opt.timestep,
603
+ gravity: Array.from(r.opt.gravity),
604
+ bodyCount: r.nbody - 1,
605
+ jointCount: r.njnt,
606
+ geomCount: r.ngeom,
607
+ bodies: Array.from({ length: r.nbody - 1 }, (E, T) => {
608
+ const q = T + 1;
609
+ return { id: q, name: p.mj_id2name(r, p.mjtObj.mjOBJ_BODY.value, q), mass: r.body_mass[q], inertia: Array.from(r.body_inertia.slice(q * 3, q * 3 + 3)) };
610
+ }),
611
+ diagnostics: D == null ? void 0 : D.diagnostics
612
+ });
613
+ },
614
+ get update() {
615
+ return C || s != null && s.model || n || b ? (r) => {
616
+ if (v || u || !((o == null ? void 0 : o()) ?? P) || !a && !n || !n && !(s != null && s.model)) return !1;
617
+ try {
618
+ if (C) return C.update(r);
619
+ const p = n ? n(r) : s.step(r);
620
+ return p && (n || a.sync(), f == null || f.sync(), t == null || t.updateForceLine(), Q()), p;
621
+ } catch (p) {
622
+ return $(p, "step"), !1;
623
+ }
624
+ } : void 0;
625
+ },
626
+ updatePaused(r) {
627
+ if (v || u || ((o == null ? void 0 : o()) ?? P)) return !1;
628
+ try {
629
+ if (C || !m || !(s != null && s.isJointDriveActive) || !a) return !1;
630
+ const p = s.stepJointDrive(r);
631
+ return p && (a.sync(), f == null || f.sync(), t == null || t.updateForceLine()), Q(!s.isJointDriveActive), p;
632
+ } catch (p) {
633
+ return $(p, "joint-drive"), !1;
634
+ }
635
+ },
636
+ play() {
637
+ if (!(u || v || ((o == null ? void 0 : o()) ?? P)))
638
+ try {
639
+ if (C) {
640
+ t == null || t.endGrab(), C.play(), e.requestRender();
641
+ return;
642
+ }
643
+ w ? (w(), P = (o == null ? void 0 : o()) ?? !1) : m ? (t == null || t.endGrab(), i ? i() : (s == null || s.reset(), a == null || a.sync()), f == null || f.sync(), t == null || t.resetGround(), t == null || t.updateForceLine(), Q(!0), P = !0, h == null || h.setPlaying(!0), e.requestRender()) : j && (P = !0, e.requestRender());
644
+ } catch (r) {
645
+ $(r, "play");
646
+ }
647
+ },
648
+ pause() {
649
+ if (!(v || u))
650
+ try {
651
+ if (C) {
652
+ C.pause();
653
+ return;
654
+ }
655
+ M && M(), P = !1, t == null || t.endGrab(), m && (s == null || s.clearJointTargets({ freeze: !0 })), h == null || h.setPlaying(!1), Q(!0);
656
+ } catch (r) {
657
+ $(r, "pause");
658
+ }
659
+ },
660
+ reset() {
661
+ if (!(v || u || !a && !i))
662
+ try {
663
+ if (C) {
664
+ C.reset(), t == null || t.resetGround(), e.requestRender();
665
+ return;
666
+ }
667
+ t == null || t.endGrab(), i ? i() : (s == null || s.reset(), a.sync()), f == null || f.sync(), t == null || t.resetGround(), t == null || t.updateForceLine(), Q(!0);
668
+ } catch (r) {
669
+ $(r, "reset");
670
+ }
671
+ },
672
+ beginGrab(r, p, E) {
673
+ if (u || C || !m && !F) return !1;
674
+ try {
675
+ const T = (t == null ? void 0 : t.beginGrab(r, p, E)) ?? !1;
676
+ return T && L(), T;
677
+ } catch (T) {
678
+ return $(T, "grab"), !1;
679
+ }
680
+ },
681
+ moveGrab(r, p, E) {
682
+ if (!(u || !m && !F))
683
+ try {
684
+ t == null || t.moveGrab(r, p, E);
685
+ } catch (T) {
686
+ $(T, "grab");
687
+ }
688
+ },
689
+ endGrab() {
690
+ t == null || t.endGrab();
691
+ },
692
+ getScenePhysics() {
693
+ if (u || !m || !V || !a) return null;
694
+ const r = /* @__PURE__ */ new Map();
695
+ return a.root.traverse((p) => {
696
+ typeof p.userData.bodyId == "number" && p.name && r.set(p.name, p);
697
+ }), {
698
+ ...V,
699
+ visualBindings: r
700
+ };
701
+ },
702
+ dispose() {
703
+ return pe(), G ?? (G = ie.catch(() => {
704
+ }).then(async () => {
705
+ ye(), t == null || t.dispose(), t = null, d ? (d(), d = null, t = null, a = null, f = null, W = null) : (f == null || f.root.removeFromParent(), f == null || f.dispose(), f = null, W = null, a == null || a.dispose(), a = null, s == null || s.dispose()), await (ee == null ? void 0 : ee.catch(() => {
706
+ })), N = null, h == null || h.dispose(), h = void 0, O(), g.dispose(), D = null, y = null, V = null, H = null, n = null, i = null, w = null, M = null, o = null, c = null, F = null, m = !1, j = !1, R = null, ne = !1;
707
+ })), G;
708
+ }
709
+ };
710
+ }
711
+ function ae(l, e) {
712
+ if (l || e.aborted) throw de();
713
+ }
714
+ function de() {
715
+ return Object.assign(new Error("Simulation load aborted."), { name: "AbortError", code: "E_ABORTED" });
716
+ }
717
+ export {
718
+ Rt as createSimulationSession
719
+ };