@nebula-spatial/viewer 0.5.14 → 0.5.16

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,786 +0,0 @@
1
- import { d as Je, f as ze, c as Ve, a as ke, b as $e, g as Be, w as Xe, h as Ie, p as se, S as je, e as He } from "./collision-debug-B2bN8P2g.js";
2
- import { disposeModelResources as ae } from "./resources-CHhCm2f7.js";
3
- import { D as We } from "./index-BSDalKkd.js";
4
- import { S as oe } from "./stage-CvP0yaff.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 fe, Group as Ee, Mesh as Ae, PlaneGeometry as Oe, BoxGeometry as Re, SphereGeometry as ie, Matrix4 as nt, CylinderGeometry as Fe, CapsuleGeometry as Te, 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 v of a.joints) {
13
- if (v.mimic || !["revolute", "prismatic"].includes(v.type) || !Number.isFinite(v.defaultValue)) continue;
14
- const _ = v.controlLimits ?? v.limits;
15
- if (!_ || !Number.isFinite(_.lower) || !Number.isFinite(_.upper) || _.lower >= _.upper) continue;
16
- const g = v.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 = v.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[v.id] = g, r.push({ id: v.id, home: g, far: G, duration: E, start: o, end: u });
22
- }
23
- if (!r.length) return;
24
- const c = e.autoPlay !== !1;
25
- let f = c ? 0 : X, n = c, i = !1, b = !1, p = !1, P = !c, M = !1, S = !0, w = c ? !0 : null;
26
- const m = (v) => {
27
- var _;
28
- if (S) {
29
- w = v;
30
- return;
31
- }
32
- w = null, (_ = e.onPlayingChange) == null || _.call(e, v);
33
- }, h = (v) => {
34
- if (n !== v) {
35
- if (n = v, v) {
36
- if (t.setPreviewing(!0), !n || M) return;
37
- t.setPlaying(!0);
38
- } else
39
- t.setPlaying(!1), t.setPreviewing(!1);
40
- n === v && m(v);
41
- }
42
- }, y = () => {
43
- M || (c && !b && (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
- f = X, i = !1, P = !1;
57
- }
58
- h(!0);
59
- }
60
- },
61
- pause() {
62
- M || (c && !b && (p = !0), h(!1));
63
- },
64
- reset() {
65
- M || (c && !b && (p = !0), h(!1), !M && (f = X, i = !1, P = !1, t.resetPreviewPose()));
66
- },
67
- dispose() {
68
- M || (M = !0, O(), h(!1), w = null);
69
- },
70
- update(v) {
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(v) || v <= 0 || (f += v, f < X)) return !1;
78
- if (f >= u)
79
- return i = !0, P = !0, h(!1), M ? !1 : (t.resetPreviewPose(), c && !p && !b && (b = !0, (E = e.onFinished) == null || E.call(e)), !0);
80
- const _ = r.find((o) => f < o.end), g = f - _.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 c && (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(), c = async (i, b) => {
101
- if (u.has(i)) throw ue(`MJCF include cycle: ${i}`, "E_MJCF_INCLUDE_CYCLE");
102
- u.add(i);
103
- const p = dt(b, 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 c(M, await a.text(M));
109
- }
110
- u.delete(i);
111
- };
112
- await c(e, t);
113
- const f = (n = r.get(e)) == null ? void 0 : n.querySelector("mujoco");
114
- if (!f || s.size === 0) throw ue("MJCF requires a mujoco root and at least one named body.", "E_MJCF_PARSE");
115
- return Je({
116
- id: f.getAttribute("model") || e,
117
- name: f.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 b = i.querySelector("compiler");
129
- for (const p of i.querySelectorAll("asset [file]")) {
130
- const P = (b == null ? void 0 : b.getAttribute(p.tagName === "texture" ? "texturedir" : "meshdir")) ?? (b == null ? void 0 : b.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 c = t.extension.xml;
140
- return { format: "mjcf", xml: ct(u) ? ft(c) : c, files: s };
141
- }
142
- function ct(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 ft(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 = (f) => f.replaceAll("\\", "/").split("/").pop() ?? "", r = /* @__PURE__ */ new Map();
158
- for (const f of t) {
159
- const n = a(f), i = r.get(n) ?? [];
160
- i.push(f), r.set(n, i);
161
- }
162
- const s = new Set(e.keys()), u = /* @__PURE__ */ new Set(["include", "mesh", "texture", "hfield", "skin"]), c = /* @__PURE__ */ new Set(["fileleft", "fileright", "fileup", "filedown", "filefront", "fileback"]);
163
- for (const f of e.values()) {
164
- if (f.querySelector("plugin, extension, model, attach")) return t;
165
- for (const n of f.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" && c.has(i.name)))
171
- return t;
172
- if (!i.value) continue;
173
- const b = r.get(a(i.value));
174
- if (!b) return t;
175
- for (const p of b) s.add(p);
176
- }
177
- }
178
- return t.filter((f) => s.has(f));
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], c = t.mesh_faceadr[e], f = t.mesh_normaladr[e], n = t.mesh_texcoordadr[e];
211
- let i = f >= 0, b = n >= 0;
212
- for (let S = 0; S < t.mesh_facenum[e]; S++) for (let w = 0; w < 3; w++) {
213
- const m = (c + 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
- f >= 0 && y >= 0 ? r.push(...t.mesh_normal.slice((f + y) * 3, (f + 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)) : b = !1;
219
- }
220
- const p = new fe();
221
- return p.setAttribute("position", new Z(a, 3)), i && r.length === a.length ? p.setAttribute("normal", new Z(r, 3)) : p.computeVertexNormals(), b && 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(), c = new a.MjvPerturb(), f = new a.MjvCamera();
227
- try {
228
- u.geomgroup.fill(1), a.mjv_updateScene(r, t.data, u, c, f, 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(), c.delete(), f.delete();
240
- }
241
- }
242
- function _t(t, e, a, r, s, u = /* @__PURE__ */ new Map()) {
243
- var w;
244
- const c = t.model, f = t.mujoco, n = c.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
- c.mat_reflectance[n] > 0 && i("MJCF_REFLECTANCE_UNSUPPORTED", "Legacy planar reflection is not reproduced by the PBR material.");
250
- const b = f.mjtTextureRole.mjNTEXROLE.value;
251
- if (!Array.from(c.mat_texid.slice(n * b, (n + 1) * b)).some((m) => Number(m) >= 0)) return;
252
- const M = !(c.geom_type[e] === f.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 = c.geom_size[e * 3] || 1, O = c.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 < b; m++) {
260
- const h = c.mat_texid[n * b + m];
261
- if (h < 0) continue;
262
- if (c.tex_type[h] !== f.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 = c.tex_width[h], O = c.tex_height[h], C = c.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 v = Number(c.tex_adr[h]), _ = new Uint8Array(y * O * 4);
276
- for (let E = 0; E < y * O; E++) {
277
- const o = v + E * C, d = E * 4, D = c.tex_data[o];
278
- _[d] = D, _[d + 1] = C >= 3 ? c.tex_data[o + 1] : D, _[d + 2] = C >= 3 ? c.tex_data[o + 2] : D, _[d + 3] = C === 4 ? c.tex_data[o + 3] : C === 2 ? c.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) && c.tex_colorspace[h] === f.mjtColorSpace.mjCOLORSPACE_SRGB.value ? Ce : tt, g.wrapS = g.wrapT = rt, g.magFilter = st, g.minFilter = ot, g.generateMipmaps = !0;
282
- const G = c.mat_texrepeat[n * 2], R = c.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 * (c.geom_size[e * 3] || 1), R * (c.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 = c.mat_roughness[n] >= 0 ? r.roughness : 1;
288
- else if (m === 4)
289
- r.metalnessMap = g, r.metalness = c.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 = c.mat_roughness[n] >= 0 ? r.roughness : 1, r.metalness = c.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 vt(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 c = /* @__PURE__ */ new Map(), f = /* @__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 b = new Ee();
317
- b.name = i, b.userData.bodyId = n, c.set(n, b), f.set(i, b), u.add(b);
318
- }
319
- try {
320
- if (c.set(0, u), e) {
321
- const n = Ve(), i = ke();
322
- let b = 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 = c.get(r.geom_bodyid[p]), S = bt(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 = $e(S, n, w, i);
337
- S.dispose(), m.userData.geomId = p, b += 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
- b === 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 b = c.get(r.geom_bodyid[i]), p = At(t, i);
347
- if (!b || !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: vt(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]), b.add(m);
364
- }
365
- }
366
- return { root: u, bindings: f, expectedLinkCount: f.size };
367
- } catch (n) {
368
- throw ae(u), n;
369
- }
370
- }
371
- function bt(t, e) {
372
- const a = t.model, r = t.mujoco, s = e * 3, u = a.geom_size[s], c = a.geom_size[s + 1], f = 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(c * 2, 2), 10, 10);
374
- if (n === r.mjtGeom.mjGEOM_BOX.value) return new Re(u * 2, c * 2, f * 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, c, f)), i;
379
- }
380
- if (n === r.mjtGeom.mjGEOM_CYLINDER.value) {
381
- const i = new Fe(u, u, c * 2, 24);
382
- return i.rotateX(Math.PI / 2), i;
383
- }
384
- if (n === r.mjtGeom.mjGEOM_CAPSULE.value) {
385
- const i = new Te(u, c * 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], c = a.mesh_graph[r], f = a.mesh_graph[r + 1];
403
- if (c < 4 || f < 4) return null;
404
- const n = r + 2 + 3 * c + 3 * f, i = new fe();
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 + f * 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], c = e * 4, f = a.hfield_size[c], n = a.hfield_size[c + 1], i = a.hfield_size[c + 2], b = a.hfield_size[c + 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
- -f + 2 * f * R / (s - 1),
414
- -n + 2 * n * G / (r - 1),
415
- a.hfield_data[u + G * s + R] * i
416
- );
417
- const M = [
418
- [-f, -n, -b],
419
- [f, -n, -b],
420
- [-f, n, -b],
421
- [f, n, -b]
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, v = (r - 1) * s, _ = r * s - 1;
432
- S.push(w, O, m, m, O, C), S.push(h, y, v, y, _, v), S.push(w, h, O, h, v, O), S.push(m, C, y, y, C, _);
433
- const g = new fe();
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], c = a.geom_size[s + 1], f = a.geom_size[s + 2], n = a.geom_type[e];
438
- if (n === r.mjtGeom.mjGEOM_PLANE.value) return new Oe((u || 5) * 2, (c || 5) * 2);
439
- if (n === r.mjtGeom.mjGEOM_BOX.value) return new Re(u * 2, c * 2, f * 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, c * 2, 24);
443
- return i.rotateX(Math.PI / 2), i;
444
- }
445
- if (n === r.mjtGeom.mjGEOM_CAPSULE.value) {
446
- const i = new Te(u, c * 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 Be(t), r = e.format === "usd" || e.format === "usda" || e.format === "usdc" || e.format === "usdz";
453
- let s = null, u = null, c = null, f = null, n = null, i = null, b = 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 v = !1, _ = !1, g = null, G = null, R, E = null, o = null, d = null, D = null, T = null, I = null, k = !1, H = "z", ee = !1;
461
- const $ = () => {
462
- o && (H = e.options.upAxis ?? "z", ee = !0, o.setUpAxis(H) && (u == null || u.sync(), d == null || d.sync()));
463
- }, B = (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 N;
469
- const F = se(l, e.signal, `${e.format}:${j}`);
470
- m = !1, C(), h == null || h.dispose(), h = void 0, P = null, v = !1, G = { ...F, stage: j };
471
- try {
472
- o == null || o.endGrab();
473
- } catch {
474
- }
475
- (N = e.reportSessionState) == null || N.call(e), e.reportError(l, { fatal: !1, stage: j, code: F.code });
476
- }, Ne = (l) => {
477
- var j;
478
- if (w) {
479
- l.dispose();
480
- return;
481
- }
482
- T = null, k = !1, E = l.asset, c = l.dispose, _ = l.hasAnimation === !0, f = l.hasPhysics || _ ? l.step : null, n = l.reset, m = !l.hasPhysics && l.hasAnimation === !0, v = 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 N;
486
- m = F, w || ((N = e.reportSessionState) == null || N.call(e), e.requestRender());
487
- },
488
- onFinished() {
489
- w || h == null || h.notifyIntroFinished();
490
- }
491
- }), y && (m = y.playing)), $(), (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
- k = !1, G = { ...j, stage: "usd-physics" }, v = !1, _ = (T == null ? void 0 : T.visual.hasAnimation) ?? _, f = T != null && T.visual.hasAnimation ? T.visual.step : null, n = T != null && T.visual.hasAnimation ? T.visual.reset : null, m = !!(T != null && T.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, N, J, ye, _e, ge, ve, be;
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-DSFtJk4b.js")).loadUsdSimulation(t, e, a);
520
- if (w || e.signal.aborted)
521
- throw A.dispose(), ce();
522
- T = A, u = { root: A.visual.root, sync() {
523
- }, dispose() {
524
- } }, c = A.dispose, _ = A.visual.hasAnimation, f = _ ? A.visual.step : null, n = A.visual.reset, m = !1, k = !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
- ), o.setCacheInvalidationCallback(e.invalidateTransmissionCaches), $(), W(), (F = e.reportProgress) == null || F.call(e, "usd-physics", "视觉场景已就绪,正在准备物理..."), I = A.completePhysics().then(Ne).catch(De);
531
- return;
532
- }
533
- if (U === "urdf" || U === "xacro") {
534
- (N = e.reportProgress) == null || N.call(e, "urdf", "正在解析 URDF 场景与运动学结构...");
535
- const A = await (await import("./urdf-session-DcGOHFg2.js")).loadUrdfSimulation(t, e, a);
536
- if (w || e.signal.aborted)
537
- throw A.dispose(), ce();
538
- E = A.asset, o = A.stage, P = A.inspection, c = A.dispose, f = A.step, n = A.reset, i = A.play, b = A.pause, p = A.isPlaying, M = () => A.capabilities, S = () => A.physics, m = !1, v = !1, g = A.warning ?? null, $(), 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-CFQQsRmc.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-D6T3a1uQ.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"), o.setCacheInvalidationCallback(e.invalidateTransmissionCaches), m = !1, $(), W();
578
- return;
579
- }
580
- v = !0, Q(w, e.signal), (ve = e.reportProgress) == null || ve.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), (be = e.reportProgress) == null || be.call(e, "stage", "正在构建三维场景与环境光影..."), u = new je(s, qe), o = new oe(u.root, e.options.simulationTheme, s), o.setCacheInvalidationCallback(e.invalidateTransmissionCaches);
583
- const Me = Ge(s, !0, E.diagnostics);
584
- Me.root.children.length > 0 ? (d = new je(s, Me, { accelerateRaycast: !1 }), d.root.visible = !1) : d = null, d && o.attachAuxiliary(d.root), $(), 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, c ? (c(), c = null) : (d == null || d.dispose(), u == null || u.dispose(), s == null || s.dispose()), d = null, u = null, s = null, a.dispose(), T = 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 : !k && !v && _ ? !0 : e.format === "urdf" || e.format === "xacro" || v ? !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: v || _ || !!y, canPause: v || _ || !!y, canReset: v || _ || !!h, hasPhysics: v, 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() : k ? { status: "initializing" } : v ? { 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 || !v || !(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, N) => {
679
- const J = N + 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 || f || r ? (l) => {
687
- if (G || w || !((p == null ? void 0 : p()) ?? m) || !u && !f || !f && !(s != null && s.model)) return !1;
688
- try {
689
- if (y) return y.update(l);
690
- const j = f ? f(l) : s.step(l);
691
- return j && (f || u.sync(), d == null || d.sync(), o == null || o.updateForceLine(), B()), 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 || !v || !(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()), B(!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) : v ? (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(), B(!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
- b && b(), m = !1, o == null || o.endGrab(), v && (s == null || s.clearJointTargets({ freeze: !0 })), h == null || h.setPlaying(!1), B(!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(), B(!0);
739
- } catch (l) {
740
- q(l, "reset");
741
- }
742
- },
743
- beginGrab(l, j, F) {
744
- if (w || y || !v && !S) return !1;
745
- try {
746
- const N = (o == null ? void 0 : o.beginGrab(l, j, F)) ?? !1;
747
- return N && O(), N;
748
- } catch (N) {
749
- return q(N, "grab"), !1;
750
- }
751
- },
752
- moveGrab(l, j, F) {
753
- if (!(w || !v && !S))
754
- try {
755
- o == null || o.moveGrab(l, j, F);
756
- } catch (N) {
757
- q(N, "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, c ? (c(), c = 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
- })), T = null, h == null || h.dispose(), h = void 0, C(), a.dispose(), E = null, P = null, f = null, n = null, i = null, b = null, p = null, M = null, S = null, v = !1, _ = !1, g = null, ee = !1;
768
- })), R;
769
- }
770
- };
771
- }
772
- function Q(t, e) {
773
- if (t || e.aborted) throw ce();
774
- }
775
- function ce() {
776
- return Object.assign(new Error("Simulation load aborted."), { name: "AbortError", code: "E_ABORTED" });
777
- }
778
- const Tt = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
779
- __proto__: null,
780
- createSimulationSession: St
781
- }, Symbol.toStringTag, { value: "Module" }));
782
- export {
783
- Tt as a,
784
- pt as c,
785
- Pe as s
786
- };