@nebula-spatial/viewer 0.4.2 → 0.4.3

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,653 @@
1
+ var ce = Object.defineProperty;
2
+ var fe = (s, e, o) => e in s ? ce(s, e, { enumerable: !0, configurable: !0, writable: !0, value: o }) : s[e] = o;
3
+ var F = (s, e, o) => fe(s, typeof e != "symbol" ? e + "" : e, o);
4
+ import { d as re, S as k } from "./stage-BKqg5vNe.js";
5
+ import { LineBasicMaterial as me, WireframeGeometry as de, LineSegments as he, EdgesGeometry as V, Vector3 as Z, Group as K, MeshStandardMaterial as pe, Mesh as ge, PlaneGeometry as _e, BoxGeometry as ne, SphereGeometry as H, Matrix4 as be, CylinderGeometry as ie, CapsuleGeometry as ae, BufferGeometry as X, BufferAttribute as N } from "three";
6
+ function Q(s, e, o) {
7
+ if (e.throwIfAborted(), s instanceof Error && (s.name === "AbortError" || ["E_ABORTED", "E_SUPERSEDED"].includes(String(s.code)))) throw s;
8
+ const t = s && typeof s == "object" && "code" in s && typeof s.code == "string" ? s.code : "E_PHYSICS_INITIALIZE", r = s instanceof Error ? s.message : String(s);
9
+ return { code: t, source: o, message: `无法启用物理仿真(${o}):${r}` };
10
+ }
11
+ class x {
12
+ constructor(e, o) {
13
+ F(this, "root");
14
+ F(this, "bodies", /* @__PURE__ */ new Map());
15
+ F(this, "disposed", !1);
16
+ this.runtime = e, this.root = o.root;
17
+ const t = e.mujoco, r = e.model;
18
+ for (const [l, u] of o.bindings) {
19
+ const f = t.mj_name2id(r, t.mjtObj.mjOBJ_BODY.value, l);
20
+ if (f < 0) throw ee(`Physics body missing for visual link: ${l}`, "E_SIMULATION_BIND");
21
+ u.userData.bodyId = f, this.bodies.set(f, u);
22
+ }
23
+ if (this.bodies.size !== o.expectedLinkCount) throw ee("Visual body binding count mismatch.", "E_SIMULATION_BIND");
24
+ this.sync();
25
+ }
26
+ sync() {
27
+ if (this.disposed || !this.runtime.data) return;
28
+ const { xpos: e, xquat: o } = this.runtime.data;
29
+ for (const [t, r] of this.bodies) {
30
+ const l = t * 3, u = t * 4;
31
+ r.position.set(e[l], e[l + 1], e[l + 2]), r.quaternion.set(o[u + 1], o[u + 2], o[u + 3], o[u]);
32
+ }
33
+ this.root.updateMatrixWorld(!0);
34
+ }
35
+ dispose() {
36
+ this.disposed || (this.disposed = !0, this.bodies.clear(), re(this.root));
37
+ }
38
+ }
39
+ function ee(s, e) {
40
+ return Object.assign(new Error(s), { code: e });
41
+ }
42
+ class ye {
43
+ constructor(e) {
44
+ F(this, "entries", /* @__PURE__ */ new Map());
45
+ F(this, "objectUrls", /* @__PURE__ */ new Map());
46
+ this.source = e;
47
+ const o = e.original;
48
+ if (we(o))
49
+ for (const t of o.files) this.entries.set(O(t.path), t.file);
50
+ else typeof Blob < "u" && o instanceof Blob ? this.entries.set(e.entryPath ?? e.filename ?? "asset", o) : o instanceof ArrayBuffer ? this.entries.set(e.entryPath ?? e.filename ?? "asset", new Blob([o])) : typeof o == "object" && o !== null && "kind" in o && o.kind === "buffer" && this.entries.set(e.entryPath ?? e.filename ?? "asset", new Blob([o.buffer]));
51
+ }
52
+ paths() {
53
+ return [...this.entries.keys()];
54
+ }
55
+ hasLocalFiles() {
56
+ return this.entries.size > 0;
57
+ }
58
+ put(e, o) {
59
+ this.entries.set(O(e), o);
60
+ }
61
+ asSourceRecord(e = "") {
62
+ const o = O(e), t = o ? `${o}/` : "", r = {};
63
+ for (const [l, u] of this.entries)
64
+ (!t || l.startsWith(t)) && (r[t ? l.slice(t.length) : l] = u);
65
+ return r;
66
+ }
67
+ resolve(e, o = "") {
68
+ const t = this.find(e, o);
69
+ if (t) return t;
70
+ const r = O(o).split("/").slice(0, -1).join("/");
71
+ if (this.source.url) return O(`${r}/${e.replace(/^package:\/\//, "")}`);
72
+ throw P(`Simulation resource not found: ${e}`, "E_ASSET_RESOURCE_NOT_FOUND");
73
+ }
74
+ find(e, o = "") {
75
+ const t = O(e.replace(/^package:\/\//, "")), r = O(o).split("/").slice(0, -1).join("/");
76
+ for (const u of [O(`${r}/${e.replace(/^package:\/\//, "")}`), t])
77
+ if (this.entries.has(u)) return u;
78
+ const l = this.paths().filter(
79
+ (u) => u === t || u.endsWith(`/${t}`) || t.endsWith(`/${u}`)
80
+ );
81
+ return l.length === 1 ? l[0] : null;
82
+ }
83
+ async text(e) {
84
+ await this.ensure(e);
85
+ const o = this.entries.get(this.resolve(e));
86
+ if (!o) throw P(`Simulation resource not found: ${e}`, "E_ASSET_RESOURCE_NOT_FOUND");
87
+ return o.text();
88
+ }
89
+ async bytes(e) {
90
+ await this.ensure(e);
91
+ const o = this.entries.get(this.resolve(e));
92
+ if (!o) throw P(`Simulation resource not found: ${e}`, "E_ASSET_RESOURCE_NOT_FOUND");
93
+ return new Uint8Array(await o.arrayBuffer());
94
+ }
95
+ getBlob(e) {
96
+ const o = this.resolve(e), t = this.entries.get(o);
97
+ if (!t) throw P(`Simulation resource not found: ${e}`, "E_ASSET_RESOURCE_NOT_FOUND");
98
+ return t;
99
+ }
100
+ async ensure(e) {
101
+ const o = O(e);
102
+ if (this.entries.has(o)) return;
103
+ const t = await this.source.resolve(o);
104
+ if (!/^https?:\/\//i.test(t) && !/^blob:/i.test(t))
105
+ throw P(`Simulation resource not found: ${e}. Upload the complete asset folder or provide a resourceResolver/baseUrl.`, "E_ASSET_RESOURCE_NOT_FOUND");
106
+ const r = await fetch(t, { signal: this.source.signal });
107
+ if (!r.ok) throw P(`Simulation resource request failed (${r.status}): ${t}`, r.status === 401 || r.status === 403 ? "E_ASSET_AUTH" : "E_ASSET_FETCH");
108
+ const l = await r.blob();
109
+ this.source.signal.throwIfAborted(), this.entries.set(o, l);
110
+ }
111
+ objectUrl(e) {
112
+ const o = this.resolve(e);
113
+ let t = this.objectUrls.get(o);
114
+ return t || (t = URL.createObjectURL(this.getBlob(o)), this.objectUrls.set(o, t)), t;
115
+ }
116
+ objectUrlIfPresent(e, o = "") {
117
+ const t = O(o), r = t ? `${t}/` : "", l = O(e), u = r ? this.paths().filter((n) => {
118
+ if (!n.startsWith(r)) return !1;
119
+ const a = n.slice(r.length);
120
+ return a === l || l.endsWith(`/${a}`);
121
+ }) : [], f = u.length === 1 ? u[0] : this.find(e);
122
+ return f ? this.objectUrl(f) : null;
123
+ }
124
+ dispose() {
125
+ for (const e of this.objectUrls.values()) URL.revokeObjectURL(e);
126
+ this.objectUrls.clear(), this.entries.clear();
127
+ }
128
+ }
129
+ async function Ee(s, e) {
130
+ if (e.hasLocalFiles()) return e.text(s.entryPath ?? s.filename ?? e.paths()[0]);
131
+ const o = await fetch(s.url ?? await s.resolve(s.entryPath ?? ""), { signal: s.signal });
132
+ if (!o.ok) throw P(`Simulation asset request failed: ${o.status}`, "E_ASSET_FETCH");
133
+ return o.text();
134
+ }
135
+ function Qe(s) {
136
+ return O(s);
137
+ }
138
+ function O(s) {
139
+ const e = [];
140
+ for (const o of s.replaceAll("\\", "/").split("/"))
141
+ !o || o === "." || (o === ".." && e.length && e.at(-1) !== ".." ? e.pop() : e.push(o));
142
+ return e.join("/");
143
+ }
144
+ function we(s) {
145
+ return typeof s == "object" && s !== null && "kind" in s && s.kind === "files";
146
+ }
147
+ function P(s, e) {
148
+ return Object.assign(new Error(s), { code: e });
149
+ }
150
+ function Se() {
151
+ return { errors: [], warnings: [], approximations: [], droppedFeatures: [] };
152
+ }
153
+ function je(s) {
154
+ const e = Object.freeze({ schemaVersion: 1, ...s });
155
+ return Oe(e), e;
156
+ }
157
+ function Oe(s) {
158
+ if (!s.id.trim() || !s.name.trim()) throw z("Asset IR id and name are required.", "E_ASSET_IR_INVALID");
159
+ if (s.source.format !== "usd" && s.links.length === 0) throw z("Asset IR requires at least one link.", "E_ASSET_IR_INVALID");
160
+ const e = /* @__PURE__ */ new Set();
161
+ for (const t of s.links) {
162
+ if (!t.id || e.has(t.id)) throw z(`Invalid or duplicate link: ${t.id}`, "E_ASSET_IR_INVALID");
163
+ e.add(t.id);
164
+ }
165
+ const o = /* @__PURE__ */ new Set();
166
+ for (const t of s.joints) {
167
+ if (!t.id || o.has(t.id) || !e.has(t.parent) || !e.has(t.child))
168
+ throw z(`Invalid joint: ${t.id}`, "E_ASSET_IR_INVALID");
169
+ o.add(t.id);
170
+ }
171
+ }
172
+ function z(s, e) {
173
+ return Object.assign(new Error(s), { code: e });
174
+ }
175
+ async function ve(s, e, o) {
176
+ var n;
177
+ const t = /* @__PURE__ */ new Map(), r = /* @__PURE__ */ new Set(), l = /* @__PURE__ */ new Set(), u = async (a, c) => {
178
+ if (l.has(a)) throw J(`MJCF include cycle: ${a}`, "E_MJCF_INCLUDE_CYCLE");
179
+ l.add(a);
180
+ const p = Ie(c, a);
181
+ t.set(a, p);
182
+ for (const _ of p.querySelectorAll("worldbody body[name]")) r.add(_.getAttribute("name"));
183
+ for (const _ of p.querySelectorAll("include[file]")) {
184
+ const y = o.resolve(_.getAttribute("file"), a);
185
+ await u(y, await o.text(y));
186
+ }
187
+ l.delete(a);
188
+ };
189
+ await u(e, s);
190
+ const f = (n = t.get(e)) == null ? void 0 : n.querySelector("mujoco");
191
+ if (!f || r.size === 0) throw J("MJCF requires a mujoco root and at least one named body.", "E_MJCF_PARSE");
192
+ return je({
193
+ id: f.getAttribute("model") || e,
194
+ name: f.getAttribute("model") || e,
195
+ source: { format: "mjcf", uri: e },
196
+ links: [...r].map((a) => ({ id: a, name: a, visuals: [], collisions: [] })),
197
+ joints: [],
198
+ diagnostics: Se(),
199
+ extension: { files: o, xml: s, documents: t }
200
+ });
201
+ }
202
+ async function Ae(s) {
203
+ const e = s.extension.files, o = s.source.uri, t = o.split("/").slice(0, -1).join("/"), r = /* @__PURE__ */ new Map(), l = s.extension.documents;
204
+ for (const [n, a] of l) {
205
+ const c = a.querySelector("compiler");
206
+ for (const p of a.querySelectorAll("asset [file]")) {
207
+ const _ = (c == null ? void 0 : c.getAttribute(p.tagName === "texture" ? "texturedir" : "meshdir")) ?? (c == null ? void 0 : c.getAttribute("assetdir")) ?? "", y = e.resolve(`${_}/${p.getAttribute("file")}`, n);
208
+ await e.bytes(y);
209
+ }
210
+ }
211
+ for (const n of Ce(e.paths(), l)) {
212
+ if (n === o) continue;
213
+ const a = t && n.startsWith(`${t}/`) ? n.slice(t.length + 1) : n;
214
+ r.set(a, await e.bytes(n));
215
+ }
216
+ const u = s.extension.xml;
217
+ return { format: "mjcf", xml: Me(l) ? Ge(u) : u, files: r };
218
+ }
219
+ function Me(s) {
220
+ return s.size === 0 || ![...s.values()].some((o) => o.querySelector("freejoint, actuator") ? !0 : [...o.querySelectorAll("joint")].some(
221
+ (t) => (t.getAttribute("type") ?? "hinge").toLowerCase() !== "fixed"
222
+ )) ? !1 : ![...s.values()].some(
223
+ (o) => [...o.querySelectorAll("worldbody geom")].some((t) => {
224
+ var r;
225
+ return ((r = t.getAttribute("type")) == null ? void 0 : r.toLowerCase()) === "plane";
226
+ })
227
+ );
228
+ }
229
+ function Ge(s) {
230
+ 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"/>';
231
+ return /<worldbody\b[^>]*>/i.test(s) ? s.replace(/(<worldbody\b[^>]*>)/i, `$1${e}`) : s.replace(/<\/mujoco\s*>/i, `<worldbody>${e}</worldbody></mujoco>`);
232
+ }
233
+ function Ce(s, e) {
234
+ const o = (f) => f.replaceAll("\\", "/").split("/").pop() ?? "", t = /* @__PURE__ */ new Map();
235
+ for (const f of s) {
236
+ const n = o(f), a = t.get(n) ?? [];
237
+ a.push(f), t.set(n, a);
238
+ }
239
+ const r = new Set(e.keys()), l = /* @__PURE__ */ new Set(["include", "mesh", "texture", "hfield", "skin"]), u = /* @__PURE__ */ new Set(["fileleft", "fileright", "fileup", "filedown", "filefront", "fileback"]);
240
+ for (const f of e.values()) {
241
+ if (f.querySelector("plugin, extension, model, attach")) return s;
242
+ for (const n of f.querySelectorAll("*"))
243
+ for (const a of n.attributes) {
244
+ if (!a.name.startsWith("file")) continue;
245
+ if (a.name === "file") {
246
+ if (!l.has(n.tagName)) return s;
247
+ } else if (!(n.tagName === "texture" && u.has(a.name)))
248
+ return s;
249
+ if (!a.value) continue;
250
+ const c = t.get(o(a.value));
251
+ if (!c) return s;
252
+ for (const p of c) r.add(p);
253
+ }
254
+ }
255
+ return s.filter((f) => r.has(f));
256
+ }
257
+ function Ie(s, e) {
258
+ const o = new DOMParser().parseFromString(s, "application/xml"), t = o.querySelector("parsererror");
259
+ if (t) throw J(`MJCF XML parse failed (${e}): ${t.textContent ?? ""}`, "E_MJCF_PARSE");
260
+ return o;
261
+ }
262
+ function J(s, e) {
263
+ return Object.assign(new Error(s), { code: e });
264
+ }
265
+ const Pe = 1476466, Le = 0.72, De = 20, Fe = 1;
266
+ function Re() {
267
+ return new me({
268
+ color: Pe,
269
+ transparent: !0,
270
+ opacity: Le,
271
+ depthTest: !1
272
+ });
273
+ }
274
+ function Ne(s, e, o = "feature") {
275
+ const t = o === "mesh" ? new de(s) : Ue(s), r = new he(t, e);
276
+ return r.renderOrder = 9, r;
277
+ }
278
+ function Ue(s) {
279
+ let e = new V(s, De);
280
+ return te(s, e) || (e.dispose(), e = new V(s, Fe)), te(s, e) || (e.dispose(), e = new V(s, 0)), e;
281
+ }
282
+ const Te = 256, $e = 0.5;
283
+ function te(s, e) {
284
+ var f, n;
285
+ const o = e.getAttribute("position");
286
+ if (!o || o.count === 0) return !1;
287
+ const t = s.getAttribute("position");
288
+ if (!t || t.count < Te) return !0;
289
+ s.computeBoundingBox(), e.computeBoundingBox();
290
+ const r = (f = s.boundingBox) == null ? void 0 : f.getSize(new Z()), l = (n = e.boundingBox) == null ? void 0 : n.getSize(new Z());
291
+ if (!r || !l) return !1;
292
+ const u = r.length();
293
+ return !Number.isFinite(u) || u <= 0 ? !0 : l.length() / u >= $e;
294
+ }
295
+ const oe = 0.68;
296
+ function ke(s, e) {
297
+ if (e < 0) return oe;
298
+ const o = s.mat_roughness[e];
299
+ if (o >= 0) return o;
300
+ const t = s.mat_shininess[e];
301
+ return !Number.isFinite(t) || t <= 0 ? oe : Math.sqrt(2 / (t + 2));
302
+ }
303
+ function ze(s, e) {
304
+ if (e < 0) return 0;
305
+ const o = s.mat_metallic[e];
306
+ return Number.isFinite(o) && o >= 0 ? o : 0;
307
+ }
308
+ function se(s, e = !1, o) {
309
+ const t = s.model, r = s.mujoco, l = new K();
310
+ l.name = "MuJoCo scene";
311
+ const u = /* @__PURE__ */ new Map(), f = /* @__PURE__ */ new Map();
312
+ for (let n = 1; n < t.nbody; n += 1) {
313
+ const a = r.mj_id2name(t, r.mjtObj.mjOBJ_BODY.value, n);
314
+ if (!a) continue;
315
+ const c = new K();
316
+ c.name = a, c.userData.bodyId = n, u.set(n, c), f.set(a, c), l.add(c);
317
+ }
318
+ if (e && u.set(0, l), e) {
319
+ const n = Re();
320
+ let a = 0;
321
+ for (let c = 0; c < t.ngeom; c += 1) {
322
+ const p = r.mj_id2name(t, r.mjtObj.mjOBJ_GEOM.value, c);
323
+ if (p === "__viewer_default_ground__" || !Ve(t, c)) continue;
324
+ const _ = u.get(t.geom_bodyid[c]), y = Be(s, c);
325
+ if (!_) continue;
326
+ if (!y) {
327
+ o == null || o.droppedFeatures.push({
328
+ code: "E_MJCF_COLLISION_DEBUG_GEOMETRY_UNAVAILABLE",
329
+ message: `MJCF geom ${p ?? `#${c}`} 的 compiled type=${t.geom_type[c]} 无法准确还原为碰撞线框。`,
330
+ source: "mjcf-collision-debug"
331
+ });
332
+ continue;
333
+ }
334
+ const g = qe(s, c) ? "mesh" : "feature", i = Ne(y, n, g);
335
+ y.dispose(), i.userData.geomId = c, a += 1;
336
+ const m = c * 3, E = c * 4;
337
+ i.position.fromArray(t.geom_pos, m), i.quaternion.set(t.geom_quat[E + 1], t.geom_quat[E + 2], t.geom_quat[E + 3], t.geom_quat[E]), _.add(i);
338
+ }
339
+ a === 0 && n.dispose();
340
+ } else
341
+ for (let n = 0; n < t.ngeom; n += 1) {
342
+ if (t.geom_group[n] > 2) continue;
343
+ const a = u.get(t.geom_bodyid[n]), c = We(s, n);
344
+ if (!a || !c) continue;
345
+ const p = n * 4, _ = t.geom_matid[n], y = new pe({
346
+ opacity: t.geom_rgba[p + 3],
347
+ transparent: t.geom_rgba[p + 3] < 1,
348
+ roughness: ke(t, _),
349
+ metalness: ze(t, _)
350
+ });
351
+ if (y.color.setRGB(t.geom_rgba[p], t.geom_rgba[p + 1], t.geom_rgba[p + 2]), _ >= 0 && [0.5, 0.5, 0.5, 1].every((E, A) => t.geom_rgba[p + A] === E)) {
352
+ const E = _ * 4;
353
+ y.color.setRGB(t.mat_rgba[E], t.mat_rgba[E + 1], t.mat_rgba[E + 2]), y.opacity = t.mat_rgba[E + 3], y.transparent = y.opacity < 1;
354
+ }
355
+ const g = new ge(c, y), i = n * 3, m = n * 4;
356
+ g.position.fromArray(t.geom_pos, i), g.quaternion.set(t.geom_quat[m + 1], t.geom_quat[m + 2], t.geom_quat[m + 3], t.geom_quat[m]), a.add(g);
357
+ }
358
+ return { root: l, bindings: f, expectedLinkCount: f.size };
359
+ }
360
+ function Be(s, e) {
361
+ const o = s.model, t = s.mujoco, r = e * 3, l = o.geom_size[r], u = o.geom_size[r + 1], f = o.geom_size[r + 2], n = o.geom_type[e];
362
+ if (n === t.mjtGeom.mjGEOM_PLANE.value) return new _e(Math.max(l * 2, 2), Math.max(u * 2, 2), 10, 10);
363
+ if (n === t.mjtGeom.mjGEOM_BOX.value) return new ne(l * 2, u * 2, f * 2);
364
+ if (n === t.mjtGeom.mjGEOM_SPHERE.value) return new H(l, 20, 12);
365
+ if (n === t.mjtGeom.mjGEOM_ELLIPSOID.value) {
366
+ const a = new H(1, 24, 16);
367
+ return a.applyMatrix4(new be().makeScale(l, u, f)), a;
368
+ }
369
+ if (n === t.mjtGeom.mjGEOM_CYLINDER.value) {
370
+ const a = new ie(l, l, u * 2, 24);
371
+ return a.rotateX(Math.PI / 2), a;
372
+ }
373
+ if (n === t.mjtGeom.mjGEOM_CAPSULE.value) {
374
+ const a = new ae(l, u * 2, 6, 16);
375
+ return a.rotateX(Math.PI / 2), a;
376
+ }
377
+ return n === t.mjtGeom.mjGEOM_MESH.value ? He(s, o.geom_dataid[e]) : n === t.mjtGeom.mjGEOM_HFIELD.value ? Je(s, o.geom_dataid[e]) : null;
378
+ }
379
+ function qe(s, e) {
380
+ const o = s.model.geom_type[e];
381
+ return o === s.mujoco.mjtGeom.mjGEOM_MESH.value || o === s.mujoco.mjtGeom.mjGEOM_HFIELD.value;
382
+ }
383
+ function Ve(s, e) {
384
+ if (s.geom_contype[e] !== 0 || s.geom_conaffinity[e] !== 0) return !0;
385
+ for (let o = 0; o < s.npair; o += 1) if (s.pair_geom1[o] === e || s.pair_geom2[o] === e) return !0;
386
+ return !1;
387
+ }
388
+ function He(s, e) {
389
+ const o = s.model, t = o.mesh_graphadr[e];
390
+ if (t < 0) return null;
391
+ const r = o.mesh_vertadr[e], l = o.mesh_vertnum[e], u = o.mesh_graph[t], f = o.mesh_graph[t + 1];
392
+ if (u < 4 || f < 4) return null;
393
+ const n = t + 2 + 3 * u + 3 * f, a = new X();
394
+ return a.setAttribute("position", new N(Float32Array.from(o.mesh_vert.subarray(r * 3, (r + l) * 3)), 3)), a.setIndex(new N(Uint32Array.from(o.mesh_graph.subarray(n, n + f * 3)), 1)), a.computeVertexNormals(), a;
395
+ }
396
+ function Je(s, e) {
397
+ const o = s.model, t = o.hfield_nrow[e], r = o.hfield_ncol[e];
398
+ if (t < 2 || r < 2) return null;
399
+ const l = o.hfield_adr[e], u = e * 4, f = o.hfield_size[u], n = o.hfield_size[u + 1], a = o.hfield_size[u + 2], c = o.hfield_size[u + 3], p = t * r, _ = [];
400
+ for (let w = 0; w < t; w += 1) for (let v = 0; v < r; v += 1)
401
+ _.push(
402
+ -f + 2 * f * v / (r - 1),
403
+ -n + 2 * n * w / (t - 1),
404
+ o.hfield_data[l + w * r + v] * a
405
+ );
406
+ const y = [
407
+ [-f, -n, -c],
408
+ [f, -n, -c],
409
+ [-f, n, -c],
410
+ [f, n, -c]
411
+ ];
412
+ for (const w of y) _.push(...w);
413
+ const g = [];
414
+ for (let w = 0; w < t - 1; w += 1) for (let v = 0; v < r - 1; v += 1) {
415
+ const L = w * r + v, R = L + 1, D = L + r, U = D + 1;
416
+ g.push(L, D, R, R, D, U);
417
+ }
418
+ const i = p, m = p + 1, E = p + 2, A = p + 3;
419
+ g.push(i, m, E, m, A, E);
420
+ const M = 0, h = r - 1, b = (t - 1) * r, S = t * r - 1;
421
+ g.push(i, M, m, m, M, h), g.push(E, A, b, A, S, b), g.push(i, E, M, E, b, M), g.push(m, h, A, A, h, S);
422
+ const j = new X();
423
+ return j.setAttribute("position", new N(Float32Array.from(_), 3)), j.setIndex(g), j.computeVertexNormals(), j;
424
+ }
425
+ function We(s, e) {
426
+ const o = s.model, t = s.mujoco, r = e * 3, l = o.geom_size[r], u = o.geom_size[r + 1], f = o.geom_size[r + 2], n = o.geom_type[e];
427
+ if (n === t.mjtGeom.mjGEOM_BOX.value) return new ne(l * 2, u * 2, f * 2);
428
+ if (n === t.mjtGeom.mjGEOM_SPHERE.value) return new H(l, 24, 16);
429
+ if (n === t.mjtGeom.mjGEOM_CYLINDER.value) {
430
+ const i = new ie(l, l, u * 2, 24);
431
+ return i.rotateX(Math.PI / 2), i;
432
+ }
433
+ if (n === t.mjtGeom.mjGEOM_CAPSULE.value) {
434
+ const i = new ae(l, u * 2, 6, 16);
435
+ return i.rotateX(Math.PI / 2), i;
436
+ }
437
+ if (n !== t.mjtGeom.mjGEOM_MESH.value) return null;
438
+ const a = o.geom_dataid[e], c = o.mesh_vertadr[a], p = o.mesh_vertnum[a], _ = o.mesh_faceadr[a], y = o.mesh_facenum[a], g = new X();
439
+ return g.setAttribute("position", new N(Float32Array.from(o.mesh_vert.subarray(c * 3, (c + p) * 3)), 3)), g.setIndex(new N(Uint32Array.from(o.mesh_face.subarray(_ * 3, (_ + y) * 3)), 1)), g.computeVertexNormals(), g;
440
+ }
441
+ function Xe(s, e) {
442
+ const o = new ye(s);
443
+ let t = null, r = null, l = null, u = null, f = null, n = !1, a = !0, c = !1, p = null, _ = null, y, g = null, i = null, m = null;
444
+ const E = (h, b) => {
445
+ var j;
446
+ const S = Q(h, e.signal, `${e.format}:${b}`);
447
+ a = !1, c = !1, _ = { ...S, stage: b };
448
+ try {
449
+ i == null || i.endGrab();
450
+ } catch {
451
+ }
452
+ (j = e.reportSessionState) == null || j.call(e), e.reportError(h, { fatal: !1, stage: b, code: S.code });
453
+ }, A = () => {
454
+ n = !0, a = !1;
455
+ }, M = (async () => {
456
+ var h, b, S, j, w, v, L, R, D, U;
457
+ try {
458
+ const G = e.format;
459
+ if (G === "usd" || G === "usda" || G === "usdc" || G === "usdz") {
460
+ (h = e.reportProgress) == null || h.call(e, "usd", "正在解析 USD 场景与物理刚体...");
461
+ const d = await (await import("./usd-session-zyj46Bcg.js")).loadUsdSimulation(s, e, o);
462
+ if (n || e.signal.aborted)
463
+ throw d.dispose(), W();
464
+ g = d.asset, r = d.binding, l = d.dispose, u = d.hasPhysics ? d.step : null, f = d.reset, a = d.hasPhysics, c = d.hasPhysics, p = d.fallback ? { ...d.fallback, stage: "physics-fallback" } : d.visualWarning ? { ...d.visualWarning, stage: "visual-adaptation" } : null, (b = e.reportProgress) == null || b.call(e, "stage", "正在构建三维场景与环境光影..."), i = new k(r.root, e.options.simulationTheme, d.runtime), d.runtime && (t = d.runtime), m = d.collision ?? null, m && (m.root.visible = !1, i.attachAuxiliary(m.root));
465
+ return;
466
+ }
467
+ if (G === "urdf") {
468
+ (S = e.reportProgress) == null || S.call(e, "urdf", "正在解析 URDF 场景与运动学结构...");
469
+ const d = await (await import("./urdf-session-C_LAfkkM.js")).loadUrdfSimulation(s, e, o);
470
+ if (n || e.signal.aborted)
471
+ throw d.dispose(), W();
472
+ g = d.asset, r = d.binding, l = d.dispose, u = d.hasPhysics ? d.step : null, f = d.reset, a = d.hasPhysics, c = d.hasPhysics, p = d.fallback ? { ...d.fallback, stage: "physics-fallback" } : null, (j = e.reportProgress) == null || j.call(e, "stage", "正在构建三维场景与环境光影..."), i = new k(r.root, e.options.simulationTheme, d.runtime), d.runtime && (t = d.runtime), m = d.collision ?? null, m && (m.root.visible = !1, i.attachAuxiliary(m.root));
473
+ return;
474
+ }
475
+ let T = s.entryPath ?? s.filename;
476
+ if (!T && G === "mjcf") {
477
+ const C = o.paths().filter((d) => d.toLowerCase().endsWith(".xml"));
478
+ for (const d of ["scene.xml", "model.xml", "main.xml"]) {
479
+ const $ = C.find((I) => I.toLowerCase().endsWith(`/${d}`) || I.toLowerCase() === d);
480
+ if ($) {
481
+ T = $;
482
+ break;
483
+ }
484
+ }
485
+ }
486
+ T ?? (T = "asset.xml"), (w = e.reportProgress) == null || w.call(e, "fetch", "正在读取模型描述与依赖资源...");
487
+ const le = await Ee(s, o);
488
+ B(n, e.signal), (v = e.reportProgress) == null || v.call(e, "parse", "正在解析 MJCF 场景与连杆..."), g = await ve(le, T, o);
489
+ try {
490
+ (L = e.reportProgress) == null || L.call(e, "compile", "正在编译物理模型与参数...");
491
+ const C = await Ae(g);
492
+ B(n, e.signal), (R = e.reportProgress) == null || R.call(e, "runtime", "正在初始化物理内核...");
493
+ const { MujocoRuntime: d } = await import("./mujoco-runtime-s85kwjo7.js");
494
+ t = new d(), await t.initialize(C, e.options.mujocoBaseUrl);
495
+ } catch (C) {
496
+ const d = Q(C, e.signal, "mjcf-physics");
497
+ t == null || t.dispose(), t = null;
498
+ const { compileMjcfPreview: $ } = await import("./mjcf-visual--X39MVO9.js");
499
+ let I;
500
+ try {
501
+ I = $(g);
502
+ } catch (q) {
503
+ throw new Error(`${d.message};独立视觉预览也不可用:${q instanceof Error ? q.message : String(q)}`);
504
+ }
505
+ r = { root: I, sync() {
506
+ }, dispose() {
507
+ re(I);
508
+ } }, p = { ...d, stage: "physics-fallback" }, i = new k(I, e.options.simulationTheme), a = !1;
509
+ return;
510
+ }
511
+ c = !0, B(n, e.signal), (D = e.reportProgress) == null || D.call(e, "visual", "正在构建视觉渲染网格...");
512
+ const ue = se(t);
513
+ B(n, e.signal), (U = e.reportProgress) == null || U.call(e, "stage", "正在构建三维场景与环境光影..."), r = new x(t, ue), i = new k(r.root, e.options.simulationTheme, t);
514
+ const Y = se(t, !0, g.diagnostics);
515
+ Y.root.children.length > 0 ? (m = new x(t, Y), m.root.visible = !1) : m = null, m && i.attachAuxiliary(m.root);
516
+ } catch (G) {
517
+ throw i == null || i.dispose(), i = null, l ? (l(), l = null) : (m == null || m.dispose(), r == null || r.dispose(), t == null || t.dispose()), m = null, r = null, t = null, o.dispose(), G;
518
+ }
519
+ })();
520
+ return M.catch(() => {
521
+ }), {
522
+ id: `simulation-${Math.random().toString(36).slice(2)}`,
523
+ opened: M,
524
+ get root() {
525
+ return (i == null ? void 0 : i.root) ?? null;
526
+ },
527
+ getFitBounds() {
528
+ return (i == null ? void 0 : i.getModelBounds()) ?? null;
529
+ },
530
+ beforeRender(h, b, S) {
531
+ return i == null ? void 0 : i.beforeRender(h, b, S);
532
+ },
533
+ get capabilities() {
534
+ return { canPlay: c, canPause: c, canReset: c, hasPhysics: c, canToggleGround: !0, canToggleCollision: m !== null };
535
+ },
536
+ get warning() {
537
+ return p;
538
+ },
539
+ get physics() {
540
+ return _ ? { status: "unavailable", issue: _ } : c ? { status: "ready" } : (p == null ? void 0 : p.stage) === "physics-fallback" ? { status: "unavailable", issue: p } : { status: "not-authored" };
541
+ },
542
+ setTheme(h) {
543
+ i == null || i.setTheme(h);
544
+ },
545
+ setGroundVisible(h) {
546
+ i == null || i.setGroundVisible(h);
547
+ },
548
+ getGroundVisible() {
549
+ return (i == null ? void 0 : i.getGroundVisible()) ?? !0;
550
+ },
551
+ presentation: { preferredViewMode: "3d", interactionMode: "simulation", uiProfile: "simulation", fitPolicy: "fit-on-ready" },
552
+ beginDispose: A,
553
+ setCollisionVisible(h) {
554
+ if (n || !m) throw new Error("This asset has no collision display.");
555
+ m.root.visible = h;
556
+ },
557
+ getVisualDiagnostics() {
558
+ return n || !g || g.source.format !== "usd" ? null : structuredClone({ report: g.extension.visualAdaptation, diagnostics: g.diagnostics });
559
+ },
560
+ getPhysicsProperties() {
561
+ if (n || !c || !(t != null && t.model) || !t.mujoco) return null;
562
+ const h = t.model, b = t.mujoco;
563
+ return structuredClone({
564
+ units: { length: "m", mass: "kg", time: "s" },
565
+ timestep: h.opt.timestep,
566
+ gravity: Array.from(h.opt.gravity),
567
+ bodyCount: h.nbody - 1,
568
+ jointCount: h.njnt,
569
+ geomCount: h.ngeom,
570
+ bodies: Array.from({ length: h.nbody - 1 }, (S, j) => {
571
+ const w = j + 1;
572
+ return { id: w, name: b.mj_id2name(h, b.mjtObj.mjOBJ_BODY.value, w), mass: h.body_mass[w], inertia: Array.from(h.body_inertia.slice(w * 3, w * 3 + 3)) };
573
+ }),
574
+ diagnostics: g == null ? void 0 : g.diagnostics
575
+ });
576
+ },
577
+ get update() {
578
+ return t != null && t.model || u ? (h) => {
579
+ if (n || !c || !a || !r) return !1;
580
+ try {
581
+ if (u) {
582
+ const S = u(h);
583
+ return S && (m == null || m.sync()), S;
584
+ }
585
+ const b = t.step(h);
586
+ return b && (r.sync(), m == null || m.sync(), i == null || i.updateForceLine()), b;
587
+ } catch (b) {
588
+ return E(b, "step"), !1;
589
+ }
590
+ } : void 0;
591
+ },
592
+ play() {
593
+ !n && c && (a = !0);
594
+ },
595
+ pause() {
596
+ a = !1;
597
+ },
598
+ reset() {
599
+ if (!(n || !c || !r))
600
+ try {
601
+ f ? f() : (t == null || t.reset(), r.sync()), m == null || m.sync(), i == null || i.updateForceLine();
602
+ } catch (h) {
603
+ E(h, "reset");
604
+ }
605
+ },
606
+ beginGrab(h, b, S) {
607
+ if (!c || n) return !1;
608
+ try {
609
+ return (i == null ? void 0 : i.beginGrab(h, b, S)) ?? !1;
610
+ } catch (j) {
611
+ return E(j, "grab"), !1;
612
+ }
613
+ },
614
+ moveGrab(h, b, S) {
615
+ if (!(!c || n))
616
+ try {
617
+ i == null || i.moveGrab(h, b, S);
618
+ } catch (j) {
619
+ E(j, "grab");
620
+ }
621
+ },
622
+ endGrab() {
623
+ i == null || i.endGrab();
624
+ },
625
+ dispose() {
626
+ return A(), y ?? (y = M.catch(() => {
627
+ }).then(() => {
628
+ i == null || i.dispose(), i = null, m == null || m.root.removeFromParent(), m == null || m.dispose(), m = null, l ? (l(), l = null, r = null) : (r == null || r.dispose(), r = null, t == null || t.dispose()), o.dispose(), g = null, u = null, f = null, c = !1, p = null;
629
+ })), y;
630
+ }
631
+ };
632
+ }
633
+ function B(s, e) {
634
+ if (s || e.aborted) throw W();
635
+ }
636
+ function W() {
637
+ return Object.assign(new Error("Simulation load aborted."), { name: "AbortError", code: "E_ABORTED" });
638
+ }
639
+ const xe = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
640
+ __proto__: null,
641
+ createSimulationSession: Xe
642
+ }, Symbol.toStringTag, { value: "Module" }));
643
+ export {
644
+ x as S,
645
+ Ne as a,
646
+ Se as b,
647
+ Re as c,
648
+ je as d,
649
+ Qe as n,
650
+ Q as p,
651
+ Ee as r,
652
+ xe as s
653
+ };