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