@nebula-spatial/viewer 0.5.6 → 0.5.7

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,786 @@
1
+ import { d as Je, f as ze, c as Ve, a as $e, b as Be, g as ke, w as Xe, h as Ie, p as se, S as je, e as He } from "./collision-debug-B2CISPB9.js";
2
+ import { disposeModelResources as ae } from "./resources-CHhCm2f7.js";
3
+ import { D as We } from "./index-BTAC6njT.js";
4
+ import { S as oe } from "./stage-DUC-YNhD.js";
5
+ import { Color as Ye, LinearSRGBColorSpace as Ke, SRGBColorSpace as Ce, MeshPhysicalMaterial as Qe, Float32BufferAttribute as Z, DataTexture as Ze, RGBAFormat as xe, UnsignedByteType as et, NoColorSpace as tt, RepeatWrapping as rt, LinearFilter as st, LinearMipmapLinearFilter as ot, BufferGeometry as ce, Group as Ee, Mesh as Ae, PlaneGeometry as Oe, BoxGeometry as Re, SphereGeometry as ie, Matrix4 as nt, CylinderGeometry as Fe, CapsuleGeometry as Ne, BufferAttribute as le } from "three";
6
+ const X = 0.7, ne = 0.2, at = 110 * Math.PI / 180;
7
+ function it(t, e = {}) {
8
+ const a = t.getSnapshot();
9
+ if (a.controlMode !== "kinematic" || a.playing) return;
10
+ const r = [], s = {};
11
+ let u = X;
12
+ for (const b of a.joints) {
13
+ if (b.mimic || !["revolute", "prismatic"].includes(b.type) || !Number.isFinite(b.defaultValue)) continue;
14
+ const _ = b.controlLimits ?? b.limits;
15
+ if (!_ || !Number.isFinite(_.lower) || !Number.isFinite(_.upper) || _.lower >= _.upper) continue;
16
+ const g = b.defaultValue;
17
+ if (g < _.lower || g > _.upper) continue;
18
+ const G = Math.abs(_.upper - g) >= Math.abs(_.lower - g) ? _.upper : _.lower, R = Math.abs(G - g), E = b.type === "revolute" ? Math.max(0.6, R / at) : Math.max(0.6, 0.8 * R / (_.upper - _.lower));
19
+ if (!Number.isFinite(E) || E <= 0) continue;
20
+ const o = u;
21
+ u = o + 2 * E + ne, s[b.id] = g, r.push({ id: b.id, home: g, far: G, duration: E, start: o, end: u });
22
+ }
23
+ if (!r.length) return;
24
+ const f = e.autoPlay !== !1;
25
+ let c = f ? 0 : X, n = f, i = !1, v = !1, p = !1, P = !f, M = !1, S = !0, w = f ? !0 : null;
26
+ const m = (b) => {
27
+ var _;
28
+ if (S) {
29
+ w = b;
30
+ return;
31
+ }
32
+ w = null, (_ = e.onPlayingChange) == null || _.call(e, b);
33
+ }, h = (b) => {
34
+ if (n !== b) {
35
+ if (n = b, b) {
36
+ if (t.setPreviewing(!0), !n || M) return;
37
+ t.setPlaying(!0);
38
+ } else
39
+ t.setPlaying(!1), t.setPreviewing(!1);
40
+ n === b && m(b);
41
+ }
42
+ }, y = () => {
43
+ M || (f && !v && (p = !0), P = !0, i = !1, h(!1));
44
+ }, O = t.onManualInput(y), C = {
45
+ get playing() {
46
+ return n;
47
+ },
48
+ get finished() {
49
+ return i;
50
+ },
51
+ cancel: y,
52
+ play() {
53
+ if (!(M || n)) {
54
+ if (P || i) {
55
+ if (t.resetPreviewPose(), M) return;
56
+ c = X, i = !1, P = !1;
57
+ }
58
+ h(!0);
59
+ }
60
+ },
61
+ pause() {
62
+ M || (f && !v && (p = !0), h(!1));
63
+ },
64
+ reset() {
65
+ M || (f && !v && (p = !0), h(!1), !M && (c = X, i = !1, P = !1, t.resetPreviewPose()));
66
+ },
67
+ dispose() {
68
+ M || (M = !0, O(), h(!1), w = null);
69
+ },
70
+ update(b) {
71
+ var R, E;
72
+ if (M) return !1;
73
+ if (w !== null) {
74
+ const o = w;
75
+ w = null, (R = e.onPlayingChange) == null || R.call(e, o);
76
+ }
77
+ if (!n || M || !Number.isFinite(b) || b <= 0 || (c += b, c < X)) return !1;
78
+ if (c >= u)
79
+ return i = !0, P = !0, h(!1), M ? !1 : (t.resetPreviewPose(), f && !p && !v && (v = !0, (E = e.onFinished) == null || E.call(e)), !0);
80
+ const _ = r.find((o) => c < o.end), g = c - _.start;
81
+ let G;
82
+ if (g < _.duration)
83
+ G = _.home + (_.far - _.home) * Se(g / _.duration);
84
+ else if (g < _.duration + ne)
85
+ G = _.far;
86
+ else {
87
+ const o = (g - _.duration - ne) / _.duration;
88
+ G = _.far + (_.home - _.far) * Se(o);
89
+ }
90
+ return t.setPreviewPose({ ...s, [_.id]: G }), !0;
91
+ }
92
+ };
93
+ return f && (t.setPreviewing(!0), n && !M && t.setPlaying(!0)), S = !1, C;
94
+ }
95
+ function Se(t) {
96
+ return t * t * t * (t * (6 * t - 15) + 10);
97
+ }
98
+ async function lt(t, e, a) {
99
+ var n;
100
+ const r = /* @__PURE__ */ new Map(), s = /* @__PURE__ */ new Set(), u = /* @__PURE__ */ new Set(), f = async (i, v) => {
101
+ if (u.has(i)) throw ue(`MJCF include cycle: ${i}`, "E_MJCF_INCLUDE_CYCLE");
102
+ u.add(i);
103
+ const p = dt(v, i);
104
+ r.set(i, p);
105
+ for (const P of p.querySelectorAll("worldbody body[name]")) s.add(P.getAttribute("name"));
106
+ for (const P of p.querySelectorAll("include[file]")) {
107
+ const M = a.resolve(P.getAttribute("file"), i);
108
+ await f(M, await a.text(M));
109
+ }
110
+ u.delete(i);
111
+ };
112
+ await f(e, t);
113
+ const c = (n = r.get(e)) == null ? void 0 : n.querySelector("mujoco");
114
+ if (!c || s.size === 0) throw ue("MJCF requires a mujoco root and at least one named body.", "E_MJCF_PARSE");
115
+ return Je({
116
+ id: c.getAttribute("model") || e,
117
+ name: c.getAttribute("model") || e,
118
+ source: { format: "mjcf", uri: e },
119
+ links: [...s].map((i) => ({ id: i, name: i, visuals: [], collisions: [] })),
120
+ joints: [],
121
+ diagnostics: ze(),
122
+ extension: { files: a, xml: t, documents: r }
123
+ });
124
+ }
125
+ async function ut(t) {
126
+ const e = t.extension.files, a = t.source.uri, r = a.split("/").slice(0, -1).join("/"), s = /* @__PURE__ */ new Map(), u = t.extension.documents;
127
+ for (const [n, i] of u) {
128
+ const v = i.querySelector("compiler");
129
+ for (const p of i.querySelectorAll("asset [file]")) {
130
+ const P = (v == null ? void 0 : v.getAttribute(p.tagName === "texture" ? "texturedir" : "meshdir")) ?? (v == null ? void 0 : v.getAttribute("assetdir")) ?? "", M = e.resolve(`${P}/${p.getAttribute("file")}`, n);
131
+ await e.bytes(M);
132
+ }
133
+ }
134
+ for (const n of mt(e.paths(), u)) {
135
+ if (n === a) continue;
136
+ const i = r && n.startsWith(`${r}/`) ? n.slice(r.length + 1) : n;
137
+ s.set(i, await e.bytes(n));
138
+ }
139
+ const f = t.extension.xml;
140
+ return { format: "mjcf", xml: ft(u) ? ct(f) : f, files: s };
141
+ }
142
+ function ft(t) {
143
+ return t.size === 0 || ![...t.values()].some((a) => a.querySelector("freejoint, actuator") ? !0 : [...a.querySelectorAll("joint")].some(
144
+ (r) => (r.getAttribute("type") ?? "hinge").toLowerCase() !== "fixed"
145
+ )) ? !1 : ![...t.values()].some(
146
+ (a) => [...a.querySelectorAll("worldbody geom")].some((r) => {
147
+ var s;
148
+ return ((s = r.getAttribute("type")) == null ? void 0 : s.toLowerCase()) === "plane";
149
+ })
150
+ );
151
+ }
152
+ function ct(t) {
153
+ 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"/>';
154
+ return /<worldbody\b[^>]*>/i.test(t) ? t.replace(/(<worldbody\b[^>]*>)/i, `$1${e}`) : t.replace(/<\/mujoco\s*>/i, `<worldbody>${e}</worldbody></mujoco>`);
155
+ }
156
+ function mt(t, e) {
157
+ const a = (c) => c.replaceAll("\\", "/").split("/").pop() ?? "", r = /* @__PURE__ */ new Map();
158
+ for (const c of t) {
159
+ const n = a(c), i = r.get(n) ?? [];
160
+ i.push(c), r.set(n, i);
161
+ }
162
+ const s = new Set(e.keys()), u = /* @__PURE__ */ new Set(["include", "mesh", "texture", "hfield", "skin"]), f = /* @__PURE__ */ new Set(["fileleft", "fileright", "fileup", "filedown", "filefront", "fileback"]);
163
+ for (const c of e.values()) {
164
+ if (c.querySelector("plugin, extension, model, attach")) return t;
165
+ for (const n of c.querySelectorAll("*"))
166
+ for (const i of n.attributes) {
167
+ if (!i.name.startsWith("file")) continue;
168
+ if (i.name === "file") {
169
+ if (!u.has(n.tagName)) return t;
170
+ } else if (!(n.tagName === "texture" && f.has(i.name)))
171
+ return t;
172
+ if (!i.value) continue;
173
+ const v = r.get(a(i.value));
174
+ if (!v) return t;
175
+ for (const p of v) s.add(p);
176
+ }
177
+ }
178
+ return t.filter((c) => s.has(c));
179
+ }
180
+ function dt(t, e) {
181
+ const a = new DOMParser().parseFromString(t, "application/xml"), r = a.querySelector("parsererror");
182
+ if (r) throw ue(`MJCF XML parse failed (${e}): ${r.textContent ?? ""}`, "E_MJCF_PARSE");
183
+ return a;
184
+ }
185
+ function ue(t, e) {
186
+ return Object.assign(new Error(t), { code: e });
187
+ }
188
+ const x = 0.5, V = (t, e) => Number.isFinite(t) ? Math.min(1, Math.max(0, t)) : e;
189
+ function Pe(t, e) {
190
+ return t !== void 0 && Number.isFinite(t) && t >= 0 ? V(t, x) : e !== void 0 && Number.isFinite(e) && e >= 0 ? 1 - V(e, 0.5) : x;
191
+ }
192
+ function pt(t = {}) {
193
+ const e = t.color ?? [0.68, 0.72, 0.7, 1], a = new Ye().setRGB(e[0], e[1], e[2], t.colorSpace === "linear" ? Ke : Ce), r = V(e[3] ?? 1, 1), s = V(t.specular ?? 0.5, 0.5), u = Math.sqrt(0.08 * s);
194
+ return new Qe({
195
+ color: a,
196
+ opacity: r,
197
+ transparent: r < 1,
198
+ depthWrite: r === 1,
199
+ roughness: V(t.roughness ?? x, x),
200
+ metalness: V(t.metalness ?? 0, 0),
201
+ // Legacy specular 0.5 corresponds to the standard dielectric response.
202
+ specularIntensity: s <= 0.5 ? s * 2 : 1,
203
+ ior: s <= 0.5 ? 1.5 : (1 + u) / (1 - u),
204
+ emissive: a.clone().multiplyScalar(Math.max(0, t.emission ?? 0)),
205
+ map: t.map ?? null
206
+ });
207
+ }
208
+ function ht(t, e) {
209
+ var P, M;
210
+ const a = [], r = [], s = [], u = t.mesh_vertadr[e], f = t.mesh_faceadr[e], c = t.mesh_normaladr[e], n = t.mesh_texcoordadr[e];
211
+ let i = c >= 0, v = n >= 0;
212
+ for (let S = 0; S < t.mesh_facenum[e]; S++) for (let w = 0; w < 3; w++) {
213
+ const m = (f + S) * 3 + w, h = (u + t.mesh_face[m]) * 3;
214
+ a.push(...t.mesh_vert.slice(h, h + 3));
215
+ const y = ((P = t.mesh_facenormal) == null ? void 0 : P[m]) ?? -1;
216
+ c >= 0 && y >= 0 ? r.push(...t.mesh_normal.slice((c + y) * 3, (c + y) * 3 + 3)) : i = !1;
217
+ const O = ((M = t.mesh_facetexcoord) == null ? void 0 : M[m]) ?? -1;
218
+ n >= 0 && O >= 0 ? s.push(...t.mesh_texcoord.slice((n + O) * 2, (n + O) * 2 + 2)) : v = !1;
219
+ }
220
+ const p = new ce();
221
+ return p.setAttribute("position", new Z(a, 3)), i && r.length === a.length ? p.setAttribute("normal", new Z(r, 3)) : p.computeVertexNormals(), v && s.length === a.length / 3 * 2 && p.setAttribute("uv", new Z(s, 2)), p;
222
+ }
223
+ function yt(t) {
224
+ const e = /* @__PURE__ */ new Map();
225
+ if (!t.model.ntex) return e;
226
+ const a = t.mujoco, r = t.model, s = new a.MjvScene(r, Math.max(1e3, r.ngeom * 2)), u = new a.MjvOption(), f = new a.MjvPerturb(), c = new a.MjvCamera();
227
+ try {
228
+ u.geomgroup.fill(1), a.mjv_updateScene(r, t.data, u, f, c, a.mjtCatBit.mjCAT_ALL.value, s);
229
+ for (let n = 0; n < s.ngeom; n++) {
230
+ const i = s.geoms.get(n);
231
+ try {
232
+ i.objtype === a.mjtObj.mjOBJ_GEOM.value && e.set(i.objid, i.texuniform !== 0);
233
+ } finally {
234
+ i.delete();
235
+ }
236
+ }
237
+ return e;
238
+ } finally {
239
+ s.delete(), u.delete(), f.delete(), c.delete();
240
+ }
241
+ }
242
+ function _t(t, e, a, r, s, u = /* @__PURE__ */ new Map()) {
243
+ var w;
244
+ const f = t.model, c = t.mujoco, n = f.geom_matid[e];
245
+ if (n < 0) return;
246
+ const i = (m, h) => {
247
+ s != null && s.droppedFeatures.some((y) => y.code === m && y.source === `material:${n}`) || s == null || s.droppedFeatures.push({ code: m, message: h, source: `material:${n}` });
248
+ };
249
+ f.mat_reflectance[n] > 0 && i("MJCF_REFLECTANCE_UNSUPPORTED", "Legacy planar reflection is not reproduced by the PBR material.");
250
+ const v = c.mjtTextureRole.mjNTEXROLE.value;
251
+ if (!Array.from(f.mat_texid.slice(n * v, (n + 1) * v)).some((m) => Number(m) >= 0)) return;
252
+ const M = !(f.geom_type[e] === c.mjtGeom.mjGEOM_MESH.value) || !a.getAttribute("uv"), S = u.get(e);
253
+ if (S === void 0) throw new Error(`Missing compiled texture projection for geom ${e}`);
254
+ if (M) {
255
+ const m = a.getAttribute("position"), h = [], y = f.geom_size[e * 3] || 1, O = f.geom_size[e * 3 + 1] || y;
256
+ for (let C = 0; C < m.count; C++) h.push(m.getX(C) / (2 * (S ? 1 : y)), -m.getY(C) / (2 * (S ? 1 : O)));
257
+ a.setAttribute("uv", new Z(h, 2));
258
+ }
259
+ for (let m = 0; m < v; m++) {
260
+ const h = f.mat_texid[n * v + m];
261
+ if (h < 0) continue;
262
+ if (f.tex_type[h] !== c.mjtTexture.mjTEXTURE_2D.value) {
263
+ i("MJCF_TEXTURE_PROJECTION_UNSUPPORTED", "Cube/skybox surface textures require a dedicated projection and are not substituted with a 2D map.");
264
+ continue;
265
+ }
266
+ if (m === 0) {
267
+ i("MJCF_USER_TEXTURE_UNSUPPORTED", "User texture role has no defined PBR material slot.");
268
+ continue;
269
+ }
270
+ const y = f.tex_width[h], O = f.tex_height[h], C = f.tex_nchannel[h];
271
+ if (![1, 2, 3, 4].includes(C)) {
272
+ i("MJCF_TEXTURE_CHANNELS_UNSUPPORTED", "Unsupported texture channel count.");
273
+ continue;
274
+ }
275
+ const b = Number(f.tex_adr[h]), _ = new Uint8Array(y * O * 4);
276
+ for (let E = 0; E < y * O; E++) {
277
+ const o = b + E * C, d = E * 4, D = f.tex_data[o];
278
+ _[d] = D, _[d + 1] = C >= 3 ? f.tex_data[o + 1] : D, _[d + 2] = C >= 3 ? f.tex_data[o + 2] : D, _[d + 3] = C === 4 ? f.tex_data[o + 3] : C === 2 ? f.tex_data[o + 1] : 255, [2, 3, 4, 6].includes(m) && (_[d] = _[d + 1] = _[d + 2] = D);
279
+ }
280
+ const g = new Ze(_, y, O, xe, et);
281
+ g.colorSpace = [1, 7, 8].includes(m) && f.tex_colorspace[h] === c.mjtColorSpace.mjCOLORSPACE_SRGB.value ? Ce : tt, g.wrapS = g.wrapT = rt, g.magFilter = st, g.minFilter = ot, g.generateMipmaps = !0;
282
+ const G = f.mat_texrepeat[n * 2], R = f.mat_texrepeat[n * 2 + 1];
283
+ if (g.repeat.set(G, R), M ? g.offset.set(-0.5, -0.5) : S && g.repeat.set(G * (f.geom_size[e * 3] || 1), R * (f.geom_size[e * 3 + 1] || 1)), g.needsUpdate = !0, m === 1 || m === 8)
284
+ (w = r.map) == null || w.dispose(), r.map = g, m === 8 && (r.transparent = !0, r.depthWrite = !1);
285
+ else if (m === 2) r.aoMap = g;
286
+ else if (m === 3)
287
+ r.roughnessMap = g, r.roughness = f.mat_roughness[n] >= 0 ? r.roughness : 1;
288
+ else if (m === 4)
289
+ r.metalnessMap = g, r.metalness = f.mat_metallic[n] >= 0 ? r.metalness : 1;
290
+ else if (m === 5) r.normalMap = g;
291
+ else if (m === 6)
292
+ r.alphaMap = g, r.transparent = !0, r.depthWrite = !1;
293
+ else if (m === 7)
294
+ r.emissiveMap = g, r.emissive.setRGB(1, 1, 1), C === 4 && i("MJCF_EMISSIVE_EXPOSURE_UNSUPPORTED", "Emissive RGB is preserved; per-pixel exposure weight is not supported.");
295
+ else if (m === 9) {
296
+ for (const E of /* @__PURE__ */ new Set([r.aoMap, r.roughnessMap, r.metalnessMap])) E == null || E.dispose();
297
+ r.aoMap = r.roughnessMap = r.metalnessMap = g, r.roughness = f.mat_roughness[n] >= 0 ? r.roughness : 1, r.metalness = f.mat_metallic[n] >= 0 ? r.metalness : 1;
298
+ }
299
+ }
300
+ }
301
+ function gt(t, e) {
302
+ return e < 0 ? Pe() : Pe(t.mat_roughness[e], t.mat_shininess[e]);
303
+ }
304
+ function bt(t, e) {
305
+ if (e < 0) return 0;
306
+ const a = t.mat_metallic[e];
307
+ return Number.isFinite(a) && a >= 0 ? a : 0;
308
+ }
309
+ function Ge(t, e = !1, a) {
310
+ const r = t.model, s = t.mujoco, u = new Ee();
311
+ u.name = "MuJoCo scene";
312
+ const f = /* @__PURE__ */ new Map(), c = /* @__PURE__ */ new Map();
313
+ for (let n = 1; n < r.nbody; n += 1) {
314
+ const i = s.mj_id2name(r, s.mjtObj.mjOBJ_BODY.value, n);
315
+ if (!i) continue;
316
+ const v = new Ee();
317
+ v.name = i, v.userData.bodyId = n, f.set(n, v), c.set(i, v), u.add(v);
318
+ }
319
+ try {
320
+ if (f.set(0, u), e) {
321
+ const n = Ve(), i = $e();
322
+ let v = 0;
323
+ for (let p = 0; p < r.ngeom; p += 1) {
324
+ const P = s.mj_id2name(r, s.mjtObj.mjOBJ_GEOM.value, p);
325
+ if (P === "__viewer_default_ground__" || !Mt(r, p)) continue;
326
+ const M = f.get(r.geom_bodyid[p]), S = vt(t, p);
327
+ if (!M) continue;
328
+ if (!S) {
329
+ a == null || a.droppedFeatures.push({
330
+ code: "E_MJCF_COLLISION_DEBUG_GEOMETRY_UNAVAILABLE",
331
+ message: `MJCF geom ${P ?? `#${p}`} 的 compiled type=${r.geom_type[p]} 无法准确还原为碰撞线框。`,
332
+ source: "mjcf-collision-debug"
333
+ });
334
+ continue;
335
+ }
336
+ const w = wt(t, p) ? "mesh" : "feature", m = Be(S, n, w, i);
337
+ S.dispose(), m.userData.geomId = p, v += 1;
338
+ const h = p * 3, y = p * 4;
339
+ m.position.fromArray(r.geom_pos, h), m.quaternion.set(r.geom_quat[y + 1], r.geom_quat[y + 2], r.geom_quat[y + 3], r.geom_quat[y]), M.add(m);
340
+ }
341
+ v === 0 && (n.dispose(), i.dispose());
342
+ } else {
343
+ const n = yt(t);
344
+ for (let i = 0; i < r.ngeom; i += 1) {
345
+ if (r.geom_group[i] > 2 || s.mj_id2name(r, s.mjtObj.mjOBJ_GEOM.value, i) === "__viewer_default_ground__") continue;
346
+ const v = f.get(r.geom_bodyid[i]), p = At(t, i);
347
+ if (!v || !p) continue;
348
+ const P = i * 4, M = r.geom_matid[i], S = Array.from(r.geom_rgba.slice(P, P + 4));
349
+ M >= 0 && [0.5, 0.5, 0.5, 1].every((O, C) => S[C] === O) && S.splice(0, 4, ...Array.from(r.mat_rgba.slice(M * 4, M * 4 + 4)));
350
+ const w = pt({
351
+ color: S,
352
+ roughness: gt(r, M),
353
+ metalness: bt(r, M),
354
+ specular: M >= 0 ? r.mat_specular[M] : void 0,
355
+ emission: M >= 0 ? r.mat_emission[M] : void 0
356
+ });
357
+ try {
358
+ _t(t, i, p, w, a, n);
359
+ } catch (O) {
360
+ throw ae(new Ae(p, w)), O;
361
+ }
362
+ const m = new Ae(p, w), h = i * 3, y = i * 4;
363
+ r.geom_bodyid[i] === 0 && r.geom_type[i] === s.mjtGeom.mjGEOM_PLANE.value && (m.userData.excludeFromSimulationFit = !0), m.position.fromArray(r.geom_pos, h), m.quaternion.set(r.geom_quat[y + 1], r.geom_quat[y + 2], r.geom_quat[y + 3], r.geom_quat[y]), v.add(m);
364
+ }
365
+ }
366
+ return { root: u, bindings: c, expectedLinkCount: c.size };
367
+ } catch (n) {
368
+ throw ae(u), n;
369
+ }
370
+ }
371
+ function vt(t, e) {
372
+ const a = t.model, r = t.mujoco, s = e * 3, u = a.geom_size[s], f = a.geom_size[s + 1], c = a.geom_size[s + 2], n = a.geom_type[e];
373
+ if (n === r.mjtGeom.mjGEOM_PLANE.value) return new Oe(Math.max(u * 2, 2), Math.max(f * 2, 2), 10, 10);
374
+ if (n === r.mjtGeom.mjGEOM_BOX.value) return new Re(u * 2, f * 2, c * 2);
375
+ if (n === r.mjtGeom.mjGEOM_SPHERE.value) return new ie(u, 20, 12);
376
+ if (n === r.mjtGeom.mjGEOM_ELLIPSOID.value) {
377
+ const i = new ie(1, 24, 16);
378
+ return i.applyMatrix4(new nt().makeScale(u, f, c)), i;
379
+ }
380
+ if (n === r.mjtGeom.mjGEOM_CYLINDER.value) {
381
+ const i = new Fe(u, u, f * 2, 24);
382
+ return i.rotateX(Math.PI / 2), i;
383
+ }
384
+ if (n === r.mjtGeom.mjGEOM_CAPSULE.value) {
385
+ const i = new Ne(u, f * 2, 6, 16);
386
+ return i.rotateX(Math.PI / 2), i;
387
+ }
388
+ return n === r.mjtGeom.mjGEOM_MESH.value ? jt(t, a.geom_dataid[e]) : n === r.mjtGeom.mjGEOM_HFIELD.value ? Et(t, a.geom_dataid[e]) : null;
389
+ }
390
+ function wt(t, e) {
391
+ const a = t.model.geom_type[e];
392
+ return a === t.mujoco.mjtGeom.mjGEOM_MESH.value || a === t.mujoco.mjtGeom.mjGEOM_HFIELD.value;
393
+ }
394
+ function Mt(t, e) {
395
+ if (t.geom_contype[e] !== 0 || t.geom_conaffinity[e] !== 0) return !0;
396
+ for (let a = 0; a < t.npair; a += 1) if (t.pair_geom1[a] === e || t.pair_geom2[a] === e) return !0;
397
+ return !1;
398
+ }
399
+ function jt(t, e) {
400
+ const a = t.model, r = a.mesh_graphadr[e];
401
+ if (r < 0) return null;
402
+ const s = a.mesh_vertadr[e], u = a.mesh_vertnum[e], f = a.mesh_graph[r], c = a.mesh_graph[r + 1];
403
+ if (f < 4 || c < 4) return null;
404
+ const n = r + 2 + 3 * f + 3 * c, i = new ce();
405
+ return i.setAttribute("position", new le(Float32Array.from(a.mesh_vert.subarray(s * 3, (s + u) * 3)), 3)), i.setIndex(new le(Uint32Array.from(a.mesh_graph.subarray(n, n + c * 3)), 1)), i.computeVertexNormals(), i;
406
+ }
407
+ function Et(t, e) {
408
+ const a = t.model, r = a.hfield_nrow[e], s = a.hfield_ncol[e];
409
+ if (r < 2 || s < 2) return null;
410
+ const u = a.hfield_adr[e], f = e * 4, c = a.hfield_size[f], n = a.hfield_size[f + 1], i = a.hfield_size[f + 2], v = a.hfield_size[f + 3], p = r * s, P = [];
411
+ for (let G = 0; G < r; G += 1) for (let R = 0; R < s; R += 1)
412
+ P.push(
413
+ -c + 2 * c * R / (s - 1),
414
+ -n + 2 * n * G / (r - 1),
415
+ a.hfield_data[u + G * s + R] * i
416
+ );
417
+ const M = [
418
+ [-c, -n, -v],
419
+ [c, -n, -v],
420
+ [-c, n, -v],
421
+ [c, n, -v]
422
+ ];
423
+ for (const G of M) P.push(...G);
424
+ const S = [];
425
+ for (let G = 0; G < r - 1; G += 1) for (let R = 0; R < s - 1; R += 1) {
426
+ const E = G * s + R, o = E + 1, d = E + s, D = d + 1;
427
+ S.push(E, d, o, o, d, D);
428
+ }
429
+ const w = p, m = p + 1, h = p + 2, y = p + 3;
430
+ S.push(w, m, h, m, y, h);
431
+ const O = 0, C = s - 1, b = (r - 1) * s, _ = r * s - 1;
432
+ S.push(w, O, m, m, O, C), S.push(h, y, b, y, _, b), S.push(w, h, O, h, b, O), S.push(m, C, y, y, C, _);
433
+ const g = new ce();
434
+ return g.setAttribute("position", new le(Float32Array.from(P), 3)), g.setIndex(S), g.computeVertexNormals(), g;
435
+ }
436
+ function At(t, e) {
437
+ const a = t.model, r = t.mujoco, s = e * 3, u = a.geom_size[s], f = a.geom_size[s + 1], c = a.geom_size[s + 2], n = a.geom_type[e];
438
+ if (n === r.mjtGeom.mjGEOM_PLANE.value) return new Oe((u || 5) * 2, (f || 5) * 2);
439
+ if (n === r.mjtGeom.mjGEOM_BOX.value) return new Re(u * 2, f * 2, c * 2);
440
+ if (n === r.mjtGeom.mjGEOM_SPHERE.value) return new ie(u, 24, 16);
441
+ if (n === r.mjtGeom.mjGEOM_CYLINDER.value) {
442
+ const i = new Fe(u, u, f * 2, 24);
443
+ return i.rotateX(Math.PI / 2), i;
444
+ }
445
+ if (n === r.mjtGeom.mjGEOM_CAPSULE.value) {
446
+ const i = new Ne(u, f * 2, 6, 16);
447
+ return i.rotateX(Math.PI / 2), i;
448
+ }
449
+ return n !== r.mjtGeom.mjGEOM_MESH.value ? null : ht(a, a.geom_dataid[e]);
450
+ }
451
+ function St(t, e) {
452
+ const a = new ke(t), r = e.format === "usd" || e.format === "usda" || e.format === "usdc" || e.format === "usdz";
453
+ let s = null, u = null, f = null, c = null, n = null, i = null, v = null, p = null, P = null, M = null, S = null, w = !1, m = !1, h, y;
454
+ const O = () => {
455
+ y == null || y.cancel();
456
+ }, C = () => {
457
+ const l = y;
458
+ y = void 0, l == null || l.dispose();
459
+ };
460
+ let b = !1, _ = !1, g = null, G = null, R, E = null, o = null, d = null, D = null, N = null, I = null, $ = !1, H = "z", ee = !1;
461
+ const B = () => {
462
+ o && (H = e.options.upAxis ?? "z", ee = !0, o.setUpAxis(H) && (u == null || u.sync(), d == null || d.sync()));
463
+ }, k = (l = !1) => {
464
+ if (!s || !h || typeof s.getJointValues != "function") return;
465
+ const j = h.getSnapshot().joints.map((F) => F.id);
466
+ h.syncValues(s.getJointValues(j), l);
467
+ }, q = (l, j) => {
468
+ var T;
469
+ const F = se(l, e.signal, `${e.format}:${j}`);
470
+ m = !1, C(), h == null || h.dispose(), h = void 0, P = null, b = !1, G = { ...F, stage: j };
471
+ try {
472
+ o == null || o.endGrab();
473
+ } catch {
474
+ }
475
+ (T = e.reportSessionState) == null || T.call(e), e.reportError(l, { fatal: !1, stage: j, code: F.code });
476
+ }, Te = (l) => {
477
+ var j;
478
+ if (w) {
479
+ l.dispose();
480
+ return;
481
+ }
482
+ N = null, $ = !1, E = l.asset, f = l.dispose, _ = l.hasAnimation === !0, c = l.hasPhysics || _ ? l.step : null, n = l.reset, m = !l.hasPhysics && l.hasAnimation === !0, b = l.hasPhysics, g = l.fallback ? { ...l.fallback, stage: "physics-fallback" } : l.visualWarning ? { ...l.visualWarning, stage: "visual-adaptation" } : null, u = l.binding, s = l.runtime ?? null, h = l.inspection, P = h ?? null, d = l.collision ?? null, D = l.createCollision ?? null, o == null || o.setRuntime(l.runtime), o == null || o.replaceModel(l.binding.root), d && (d.root.visible = !1, o == null || o.attachAuxiliary(d.root), d.sync()), h && (y = it(h, {
483
+ autoPlay: e.options.jointIntro !== !1 && !(typeof globalThis.matchMedia == "function" && globalThis.matchMedia("(prefers-reduced-motion: reduce)").matches),
484
+ onPlayingChange(F) {
485
+ var T;
486
+ m = F, w || ((T = e.reportSessionState) == null || T.call(e), e.requestRender());
487
+ },
488
+ onFinished() {
489
+ w || h == null || h.notifyIntroFinished();
490
+ }
491
+ }), y && (m = y.playing)), B(), (j = e.reportSessionState) == null || j.call(e), e.requestRender();
492
+ }, De = (l) => {
493
+ var F;
494
+ if (w) return;
495
+ const j = se(l, e.signal, "usd-physics");
496
+ $ = !1, G = { ...j, stage: "usd-physics" }, b = !1, _ = (N == null ? void 0 : N.visual.hasAnimation) ?? _, c = N != null && N.visual.hasAnimation ? N.visual.step : null, n = N != null && N.visual.hasAnimation ? N.visual.reset : null, m = !!(N != null && N.visual.hasAnimation), (F = e.reportSessionState) == null || F.call(e), e.reportError(l, { fatal: !1, stage: "usd-physics", code: j.code });
497
+ }, me = () => {
498
+ var l;
499
+ w || (w = !0, m = !1, C(), (l = e.reportSessionState) == null || l.call(e));
500
+ }, Le = () => {
501
+ if (d) return !0;
502
+ const l = D;
503
+ return D = null, !l || !o || (d = l() ?? null, !d) ? !1 : (d.root.visible = !1, o.attachAuxiliary(d.root), d.sync(), !0);
504
+ };
505
+ let de = () => {
506
+ }, pe = () => {
507
+ }, he = !1;
508
+ const te = new Promise((l, j) => {
509
+ de = l, pe = j;
510
+ }), W = () => {
511
+ he || (he = !0, de());
512
+ };
513
+ return (async () => {
514
+ var l, j, F, T, J, ye, _e, ge, be, ve;
515
+ try {
516
+ const U = e.format;
517
+ if (U === "usd" || U === "usda" || U === "usdc" || U === "usdz") {
518
+ (l = e.reportProgress) == null || l.call(e, "usd-visual", "正在解析 USD 视觉场景...");
519
+ const A = await (await import("./usd-session-BUD3KtCr.js")).loadUsdSimulation(t, e, a);
520
+ if (w || e.signal.aborted)
521
+ throw A.dispose(), fe();
522
+ N = A, u = { root: A.visual.root, sync() {
523
+ }, dispose() {
524
+ } }, f = A.dispose, _ = A.visual.hasAnimation, c = _ ? A.visual.step : null, n = A.visual.reset, m = !1, $ = !0, g = A.visual.warning ? { ...A.visual.warning, stage: "visual-adaptation" } : null, (j = e.reportProgress) == null || j.call(e, "stage", "正在构建三维场景与环境光影..."), o = new oe(
525
+ A.visual.root,
526
+ e.options.simulationTheme,
527
+ void 0,
528
+ "simulation",
529
+ "visual"
530
+ ), B(), W(), (F = e.reportProgress) == null || F.call(e, "usd-physics", "视觉场景已就绪,正在准备物理..."), I = A.completePhysics().then(Te).catch(De);
531
+ return;
532
+ }
533
+ if (U === "urdf" || U === "xacro") {
534
+ (T = e.reportProgress) == null || T.call(e, "urdf", "正在解析 URDF 场景与运动学结构...");
535
+ const A = await (await import("./urdf-session-E0Nnr4Vs.js")).loadUrdfSimulation(t, e, a);
536
+ if (w || e.signal.aborted)
537
+ throw A.dispose(), fe();
538
+ E = A.asset, o = A.stage, P = A.inspection, f = A.dispose, c = A.step, n = A.reset, i = A.play, v = A.pause, p = A.isPlaying, M = () => A.capabilities, S = () => A.physics, m = !1, b = !1, g = A.warning ?? null, B(), W();
539
+ return;
540
+ }
541
+ let Y = t.entryPath ?? t.filename;
542
+ if (!Y && U === "mjcf") {
543
+ const L = a.paths().filter((A) => A.toLowerCase().endsWith(".xml"));
544
+ for (const A of ["scene.xml", "model.xml", "main.xml"]) {
545
+ const K = L.find((z) => z.toLowerCase().endsWith(`/${A}`) || z.toLowerCase() === A);
546
+ if (K) {
547
+ Y = K;
548
+ break;
549
+ }
550
+ }
551
+ }
552
+ Y ?? (Y = "asset.xml"), (J = e.reportProgress) == null || J.call(e, "fetch", "正在读取模型描述与依赖资源...");
553
+ const Ue = await Ie(t, a);
554
+ Q(w, e.signal), (ye = e.reportProgress) == null || ye.call(e, "parse", "正在解析 MJCF 场景与连杆..."), E = await lt(Ue, Y, a);
555
+ try {
556
+ (_e = e.reportProgress) == null || _e.call(e, "compile", "正在编译物理模型与参数...");
557
+ const L = await ut(E);
558
+ Q(w, e.signal), (ge = e.reportProgress) == null || ge.call(e, "runtime", "正在初始化物理内核...");
559
+ const { MujocoRuntime: A } = await import("./mujoco-runtime-CAyK0LHM.js");
560
+ s = new A(), await s.initialize(L, {
561
+ baseUrl: e.options.mujocoBaseUrl,
562
+ wasmModule: e.options.mujocoWasmModule
563
+ });
564
+ } catch (L) {
565
+ const A = se(L, e.signal, "mjcf-physics");
566
+ s == null || s.dispose(), s = null;
567
+ const { compileMjcfPreview: K } = await import("./mjcf-visual-BVT3h87h.js");
568
+ let z;
569
+ try {
570
+ z = K(E);
571
+ } catch (re) {
572
+ throw new Error(`${A.message};独立视觉预览也不可用:${re instanceof Error ? re.message : String(re)}`);
573
+ }
574
+ u = { root: z, sync() {
575
+ }, dispose() {
576
+ ae(z);
577
+ } }, g = { ...A, stage: "physics-fallback" }, o = new oe(z, e.options.simulationTheme, void 0, "simulation", "visual"), m = !1, B(), W();
578
+ return;
579
+ }
580
+ b = !0, Q(w, e.signal), (be = e.reportProgress) == null || be.call(e, "visual", "正在构建视觉渲染网格...");
581
+ const qe = Ge(s, !1, E.diagnostics), we = E.diagnostics.droppedFeatures.find((L) => L.code.startsWith("MJCF_"));
582
+ we && (g = { ...we, stage: "visual-material" }), Q(w, e.signal), (ve = e.reportProgress) == null || ve.call(e, "stage", "正在构建三维场景与环境光影..."), u = new je(s, qe), o = new oe(u.root, e.options.simulationTheme, s);
583
+ const Me = Ge(s, !0, E.diagnostics);
584
+ Me.root.children.length > 0 ? (d = new je(s, Me), d.root.visible = !1) : d = null, d && o.attachAuxiliary(d.root), B(), h = He(
585
+ s,
586
+ "mjcf",
587
+ () => {
588
+ u == null || u.sync(), d == null || d.sync();
589
+ },
590
+ e.requestRender,
591
+ (L) => e.reportError(L, { fatal: !1, stage: "joint-inspection", code: "E_MUJOCO_JOINT_INSPECTION" })
592
+ ), P = h ?? null, W();
593
+ } catch (U) {
594
+ throw o == null || o.dispose(), o = null, f ? (f(), f = null) : (d == null || d.dispose(), u == null || u.dispose(), s == null || s.dispose()), d = null, u = null, s = null, a.dispose(), N = null, U;
595
+ }
596
+ })().catch(pe), te.catch(() => {
597
+ }), {
598
+ id: `simulation-${Math.random().toString(36).slice(2)}`,
599
+ get autoPlay() {
600
+ return y ? y.playing : !$ && !b && _ ? !0 : e.format === "urdf" || e.format === "xacro" || b ? !1 : void 0;
601
+ },
602
+ get inspection() {
603
+ return P;
604
+ },
605
+ isPlaying() {
606
+ return (p == null ? void 0 : p()) ?? m;
607
+ },
608
+ opened: te,
609
+ get root() {
610
+ return (o == null ? void 0 : o.root) ?? null;
611
+ },
612
+ getFitBounds() {
613
+ return (o == null ? void 0 : o.getModelBounds()) ?? null;
614
+ },
615
+ beforeRender(l, j, F) {
616
+ return o == null ? void 0 : o.beforeRender(l, j, F);
617
+ },
618
+ get capabilities() {
619
+ const l = d !== null || D !== null;
620
+ return G ? { canPlay: !1, canPause: !1, canReset: !1, hasPhysics: !1, hasAnimation: !1, canToggleGround: !0, canToggleCollision: l, canReorientUpAxis: !0 } : (M == null ? void 0 : M()) ?? { canPlay: b || _ || !!y, canPause: b || _ || !!y, canReset: b || _ || !!h, hasPhysics: b, hasAnimation: _ || !!y, canToggleGround: !0, canToggleCollision: l, canReorientUpAxis: !0 };
621
+ },
622
+ get warning() {
623
+ return g && !g.notice ? Xe(g, g.stage) : g;
624
+ },
625
+ get physics() {
626
+ return G ? { status: "unavailable", issue: G } : S ? S() : $ ? { status: "initializing" } : b ? { status: "ready" } : (g == null ? void 0 : g.stage) === "physics-fallback" ? { status: "unavailable", issue: g } : { status: "not-authored" };
627
+ },
628
+ setTheme(l) {
629
+ o == null || o.setTheme(l);
630
+ },
631
+ setGroundVisible(l) {
632
+ o == null || o.setGroundVisible(l);
633
+ },
634
+ getGroundVisible() {
635
+ return (o == null ? void 0 : o.getGroundVisible()) ?? !0;
636
+ },
637
+ getGroundGridScale() {
638
+ return (o == null ? void 0 : o.getGroundGridScale()) ?? We;
639
+ },
640
+ getUpAxis() {
641
+ return o && ee ? H : null;
642
+ },
643
+ setUpAxis(l) {
644
+ if (!(w || !o))
645
+ try {
646
+ if (!o.setUpAxis(l)) return;
647
+ H = l, u == null || u.sync(), d == null || d.sync(), e.requestRender();
648
+ } catch (j) {
649
+ q(j, "up-axis");
650
+ }
651
+ },
652
+ presentation: { preferredViewMode: "3d", interactionMode: "simulation", uiProfile: "simulation", fitPolicy: "fit-on-ready" },
653
+ beginDispose: me,
654
+ setCollisionVisible(l) {
655
+ if (!w) {
656
+ if (M) {
657
+ const j = P;
658
+ j == null || j.setOverlay("collision", l);
659
+ return;
660
+ }
661
+ if (l && !Le()) throw new Error("This asset has no collision display.");
662
+ d && (d.root.visible = l), e.requestRender();
663
+ }
664
+ },
665
+ getVisualDiagnostics() {
666
+ return w || !E || E.source.format !== "usd" ? null : structuredClone({ report: E.extension.visualAdaptation, diagnostics: E.diagnostics });
667
+ },
668
+ getPhysicsProperties() {
669
+ if (w || !b || !(s != null && s.model) || !s.mujoco) return null;
670
+ const l = s.model, j = s.mujoco;
671
+ return structuredClone({
672
+ units: { length: "m", mass: "kg", time: "s" },
673
+ timestep: l.opt.timestep,
674
+ gravity: Array.from(l.opt.gravity),
675
+ bodyCount: l.nbody - 1,
676
+ jointCount: l.njnt,
677
+ geomCount: l.ngeom,
678
+ bodies: Array.from({ length: l.nbody - 1 }, (F, T) => {
679
+ const J = T + 1;
680
+ return { id: J, name: j.mj_id2name(l, j.mjtObj.mjOBJ_BODY.value, J), mass: l.body_mass[J], inertia: Array.from(l.body_inertia.slice(J * 3, J * 3 + 3)) };
681
+ }),
682
+ diagnostics: E == null ? void 0 : E.diagnostics
683
+ });
684
+ },
685
+ get update() {
686
+ return y || s != null && s.model || c || r ? (l) => {
687
+ if (G || w || !((p == null ? void 0 : p()) ?? m) || !u && !c || !c && !(s != null && s.model)) return !1;
688
+ try {
689
+ if (y) return y.update(l);
690
+ const j = c ? c(l) : s.step(l);
691
+ return j && (c || u.sync(), d == null || d.sync(), o == null || o.updateForceLine(), k()), j;
692
+ } catch (j) {
693
+ return q(j, "step"), !1;
694
+ }
695
+ } : void 0;
696
+ },
697
+ updatePaused(l) {
698
+ if (G || w || ((p == null ? void 0 : p()) ?? m)) return !1;
699
+ try {
700
+ if (y || !b || !(s != null && s.isJointDriveActive) || !u) return !1;
701
+ const j = s.stepJointDrive(l);
702
+ return j && (u.sync(), d == null || d.sync(), o == null || o.updateForceLine()), k(!s.isJointDriveActive), j;
703
+ } catch (j) {
704
+ return q(j, "joint-drive"), !1;
705
+ }
706
+ },
707
+ play() {
708
+ if (!(w || G || ((p == null ? void 0 : p()) ?? m)))
709
+ try {
710
+ if (y) {
711
+ o == null || o.endGrab(), y.play(), e.requestRender();
712
+ return;
713
+ }
714
+ i ? (i(), m = (p == null ? void 0 : p()) ?? !1) : b ? (o == null || o.endGrab(), n ? n() : (s == null || s.reset(), u == null || u.sync()), d == null || d.sync(), o == null || o.resetGround(), o == null || o.updateForceLine(), k(!0), m = !0, h == null || h.setPlaying(!0), e.requestRender()) : _ && (m = !0, e.requestRender());
715
+ } catch (l) {
716
+ q(l, "play");
717
+ }
718
+ },
719
+ pause() {
720
+ if (!(G || w))
721
+ try {
722
+ if (y) {
723
+ y.pause();
724
+ return;
725
+ }
726
+ v && v(), m = !1, o == null || o.endGrab(), b && (s == null || s.clearJointTargets({ freeze: !0 })), h == null || h.setPlaying(!1), k(!0);
727
+ } catch (l) {
728
+ q(l, "pause");
729
+ }
730
+ },
731
+ reset() {
732
+ if (!(G || w || !u && !n))
733
+ try {
734
+ if (y) {
735
+ y.reset(), o == null || o.resetGround(), e.requestRender();
736
+ return;
737
+ }
738
+ o == null || o.endGrab(), n ? n() : (s == null || s.reset(), u.sync()), d == null || d.sync(), o == null || o.resetGround(), o == null || o.updateForceLine(), k(!0);
739
+ } catch (l) {
740
+ q(l, "reset");
741
+ }
742
+ },
743
+ beginGrab(l, j, F) {
744
+ if (w || y || !b && !S) return !1;
745
+ try {
746
+ const T = (o == null ? void 0 : o.beginGrab(l, j, F)) ?? !1;
747
+ return T && O(), T;
748
+ } catch (T) {
749
+ return q(T, "grab"), !1;
750
+ }
751
+ },
752
+ moveGrab(l, j, F) {
753
+ if (!(w || !b && !S))
754
+ try {
755
+ o == null || o.moveGrab(l, j, F);
756
+ } catch (T) {
757
+ q(T, "grab");
758
+ }
759
+ },
760
+ endGrab() {
761
+ o == null || o.endGrab();
762
+ },
763
+ dispose() {
764
+ return me(), R ?? (R = te.catch(() => {
765
+ }).then(async () => {
766
+ o == null || o.dispose(), o = null, f ? (f(), f = null, o = null, u = null, d = null, D = null) : (d == null || d.root.removeFromParent(), d == null || d.dispose(), d = null, D = null, u == null || u.dispose(), u = null, s == null || s.dispose()), await (I == null ? void 0 : I.catch(() => {
767
+ })), N = null, h == null || h.dispose(), h = void 0, C(), a.dispose(), E = null, P = null, c = null, n = null, i = null, v = null, p = null, M = null, S = null, b = !1, _ = !1, g = null, ee = !1;
768
+ })), R;
769
+ }
770
+ };
771
+ }
772
+ function Q(t, e) {
773
+ if (t || e.aborted) throw fe();
774
+ }
775
+ function fe() {
776
+ return Object.assign(new Error("Simulation load aborted."), { name: "AbortError", code: "E_ABORTED" });
777
+ }
778
+ const Nt = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
779
+ __proto__: null,
780
+ createSimulationSession: St
781
+ }, Symbol.toStringTag, { value: "Module" }));
782
+ export {
783
+ Nt as a,
784
+ pt as c,
785
+ Pe as s
786
+ };