@nebula-spatial/viewer 0.5.0 → 0.5.2

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