@nebula-spatial/viewer 0.4.3 → 0.4.4

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