@nebula-spatial/viewer 0.5.4 → 0.5.6

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nebula-spatial/viewer",
3
- "version": "0.5.4",
3
+ "version": "0.5.6",
4
4
  "description": "Read-only Three.js viewer facade",
5
5
  "license": "ISC",
6
6
  "engines": {
@@ -10,6 +10,7 @@
10
10
  "files": [
11
11
  "dist",
12
12
  "openusd",
13
+ "basis",
13
14
  "README.md",
14
15
  "CHANGELOG.md",
15
16
  "FBX-MATERIAL-CONVERSION.md",
@@ -29,11 +30,12 @@
29
30
  },
30
31
  "sideEffects": true,
31
32
  "scripts": {
32
- "build": "npm run check:boundary && npm run typecheck && npm run build:bundle && npm run build:types && npm run check:dist",
33
+ "build": "npm run check:openusd && npm run check:boundary && npm run typecheck && npm run build:bundle && npm run build:types && npm run check:dist",
33
34
  "build:bundle": "vite build --config vite.config.ts",
34
35
  "build:types": "tsc --project tsconfig.build.json && node scripts/prune-dist-types.mjs",
35
36
  "check:boundary": "node scripts/check-boundary.mjs",
36
37
  "check:dist": "node scripts/check-dist.mjs",
38
+ "check:openusd": "node scripts/check-openusd-runtime.mjs",
37
39
  "typecheck": "tsc --project tsconfig.json --noEmit",
38
40
  "test": "vitest run --config vitest.config.ts",
39
41
  "pack:dry": "npm pack --dry-run",
@@ -1,349 +0,0 @@
1
- var x = Object.defineProperty;
2
- var G = (l, t, e) => t in l ? x(l, t, { enumerable: !0, configurable: !0, writable: !0, value: e }) : l[t] = e;
3
- var p = (l, t, e) => G(l, typeof t != "symbol" ? t + "" : t, e);
4
- import O from "@mujoco/mujoco";
5
- import F from "@mujoco/mujoco/mujoco.wasm?url";
6
- import { Quaternion as g, Vector3 as f } from "three";
7
- let I = 0;
8
- function A(l, t) {
9
- const e = `/viewer-simulation-${++I}`;
10
- l.mkdir(e);
11
- const i = /* @__PURE__ */ new Set([e]), o = /* @__PURE__ */ new Set(), n = (s, a) => {
12
- const c = s.replaceAll("\\", "/").split("/");
13
- if (c[0] === "" || c.some((u) => u === ".." || u.includes(":"))) throw M(`Invalid package path: ${s}`, "E_SIMULATION_PATH");
14
- const h = c.filter((u) => u && u !== ".");
15
- if (!h.length) throw M("Package path is empty.", "E_SIMULATION_PATH");
16
- let m = e;
17
- for (const u of h.slice(0, -1))
18
- m += `/${u}`, i.has(m) || (l.mkdir(m), i.add(m));
19
- const d = `${e}/${h.join("/")}`;
20
- return l.writeFile(d, a), o.add(d), d;
21
- }, r = () => {
22
- for (const s of o) l.unlink(s);
23
- o.clear();
24
- for (const s of [...i].reverse()) l.rmdir(s);
25
- i.clear();
26
- };
27
- try {
28
- for (const [s, a] of t) n(s, a);
29
- return { writeEntry: (s) => n("__viewer_entry.xml", s), dispose: r };
30
- } catch (s) {
31
- throw r(), s;
32
- }
33
- }
34
- function M(l, t) {
35
- return Object.assign(new Error(l), { code: t });
36
- }
37
- function J({
38
- target: l,
39
- point: t,
40
- velocity: e,
41
- stiffness: i,
42
- damping: o,
43
- maxForce: n,
44
- strength: r
45
- }) {
46
- const s = [
47
- i * (l[0] - t[0]) - o * e[0],
48
- i * (l[1] - t[1]) - o * e[1],
49
- i * (l[2] - t[2]) - o * e[2]
50
- ], a = Math.hypot(s[0], s[1], s[2]), c = n * r;
51
- if (a > c && a > 0) {
52
- const h = c / a;
53
- s[0] *= h, s[1] *= h, s[2] *= h;
54
- }
55
- return s;
56
- }
57
- const y = 1 / 240, R = 1 / 960, S = 0.25, L = 0.05, N = 80, B = 0.7, D = 60, C = 1e-4, q = 10, E = 3.5, T = 14;
58
- class Q {
59
- constructor() {
60
- p(this, "mujoco", null);
61
- p(this, "model", null);
62
- p(this, "data", null);
63
- p(this, "grab", null);
64
- p(this, "lastGrabForce", [0, 0, 0]);
65
- p(this, "initialControls", []);
66
- p(this, "accumulator", 0);
67
- p(this, "timestep", y);
68
- /** Authored ground pose, captured before any seating. Re-seats start here. */
69
- p(this, "initialGround", null);
70
- /** Ground pose currently in the model; restored verbatim by reset(). */
71
- p(this, "seatedGround", null);
72
- p(this, "worldRotation", new g());
73
- p(this, "topLevelBodies", []);
74
- p(this, "initialVelocities", []);
75
- }
76
- async initialize(t, e) {
77
- var a;
78
- this.mujoco ?? (this.mujoco = await O({ locateFile: (c) => c.endsWith(".wasm") ? e ? new URL(c, e).href : F : c }));
79
- const i = A(this.mujoco.FS, t.files);
80
- let o = null, n = null, r = null, s = null;
81
- try {
82
- o = this.mujoco.MjModel.from_xml_path(i.writeEntry(t.xml));
83
- for (const h of t.massOverrides ?? []) {
84
- const m = this.mujoco.mj_name2id(o, this.mujoco.mjtObj.mjOBJ_BODY.value, h.body);
85
- if (m <= 0 || !Number.isFinite(h.mass) || h.mass <= 0 || !(o.body_mass[m] > 0)) throw new Error(`Invalid mass override: ${h.body}`);
86
- const d = h.mass / o.body_mass[m];
87
- o.body_mass[m] = h.mass;
88
- for (let u = 0; u < 3; u++) o.body_inertia[m * 3 + u] *= d;
89
- }
90
- n = new this.mujoco.MjData(o), (a = t.massOverrides) != null && a.length && this.mujoco.mj_setConst(o, n), o.opt.timestep = y, this.worldRotation.identity(), this.applyInitialControls(n, t.initialControls), this.applyInitialVelocities(o, n, t.initialVelocities), this.mujoco.mj_forward(o, n);
91
- const c = this.mujoco.mj_name2id(o, this.mujoco.mjtObj.mjOBJ_GEOM.value, "__viewer_default_ground__");
92
- r = c < 0 ? null : { id: c, height: o.geom_pos[c * 3 + 2], sameFrame: o.geom_sameframe[c] }, s = this.seatDefaultGround(o, n);
93
- } catch (c) {
94
- throw n == null || n.delete(), o == null || o.delete(), v(c, "E_MUJOCO_INITIALIZE");
95
- } finally {
96
- i.dispose();
97
- }
98
- this.disposeModel(), this.model = o, this.data = n, this.initialControls = t.initialControls ?? [], this.initialVelocities = t.initialVelocities ?? [], this.accumulator = 0, this.timestep = y, this.initialGround = r, this.seatedGround = s, this.topLevelBodies = this.captureTopLevelBodies(o);
99
- }
100
- get defaultGroundHeight() {
101
- if (!this.mujoco || !this.model) return;
102
- const t = this.mujoco.mj_name2id(this.model, this.mujoco.mjtObj.mjOBJ_GEOM.value, "__viewer_default_ground__");
103
- return t < 0 ? void 0 : this.model.geom_pos[t * 3 + 2];
104
- }
105
- /**
106
- * Re-orients the compiled physics to the given authored up axis by rotating
107
- * every top-level body about the world origin. Gravity stays -Z and the
108
- * viewer ground stays the authored horizontal plane, so a cup that was
109
- * standing now genuinely topples and settles on its side — the alternative,
110
- * rotating only the rendered scene graph, would leave the visuals and the
111
- * simulation in different frames.
112
- *
113
- * The full current state (free-joint positions, orientations and velocities)
114
- * is rotated by the same delta, so a paused or running simulation is carried
115
- * over instead of being re-seeded.
116
- */
117
- setWorldRotation(t) {
118
- var n;
119
- if (!this.mujoco || !this.model || !this.data) return;
120
- const e = this.model, i = this.data;
121
- if (t.equals(this.worldRotation)) return;
122
- const o = t.clone().multiply(this.worldRotation.clone().invert());
123
- this.rotateReferenceFrames(this.topLevelBodies, t), this.rotateState(this.topLevelBodies, e, i, o), this.worldRotation.copy(t), this.mujoco.mj_forward(e, i), this.seatedGround = this.seatDefaultGround(e, i, (n = this.initialGround) == null ? void 0 : n.height), this.accumulator = 0;
124
- }
125
- captureTopLevelBodies(t) {
126
- const e = this.mujoco, i = [];
127
- for (let o = 1; o < t.nbody; o++) {
128
- if (t.body_parentid[o] !== 0) continue;
129
- const n = t.body_jntadr[o], r = t.body_jntnum[o] === 1 && n >= 0 && t.jnt_type[n] === e.mjtJoint.mjJNT_FREE.value ? n : -1, s = o * 3, a = o * 4;
130
- i.push({
131
- id: o,
132
- position: new f(t.body_pos[s], t.body_pos[s + 1], t.body_pos[s + 2]),
133
- quaternion: new g(t.body_quat[a + 1], t.body_quat[a + 2], t.body_quat[a + 3], t.body_quat[a]),
134
- freeJoint: r
135
- });
136
- }
137
- return i;
138
- }
139
- /** Writes the rotated reference pose, which reset() and fixed bases follow. */
140
- rotateReferenceFrames(t, e) {
141
- const i = this.model;
142
- for (const o of t) {
143
- const n = o.position.clone().applyQuaternion(e), r = e.clone().multiply(o.quaternion), s = o.id * 3, a = o.id * 4;
144
- if (i.body_pos[s] = n.x, i.body_pos[s + 1] = n.y, i.body_pos[s + 2] = n.z, i.body_quat[a] = r.w, i.body_quat[a + 1] = r.x, i.body_quat[a + 2] = r.y, i.body_quat[a + 3] = r.z, i.body_sameframe[o.id] && (i.body_sameframe[o.id] = 0), o.freeJoint < 0) continue;
145
- const c = i.jnt_qposadr[o.freeJoint];
146
- i.qpos0[c] = n.x, i.qpos0[c + 1] = n.y, i.qpos0[c + 2] = n.z, i.qpos0[c + 3] = r.w, i.qpos0[c + 4] = r.x, i.qpos0[c + 5] = r.y, i.qpos0[c + 6] = r.z;
147
- }
148
- }
149
- /** Rotates the live free-joint state so the simulation keeps its progress. */
150
- rotateState(t, e, i, o) {
151
- for (const n of t) {
152
- if (n.freeJoint < 0) continue;
153
- const r = e.jnt_qposadr[n.freeJoint], s = e.jnt_dofadr[n.freeJoint], a = new f(i.qpos[r], i.qpos[r + 1], i.qpos[r + 2]).applyQuaternion(o);
154
- i.qpos[r] = a.x, i.qpos[r + 1] = a.y, i.qpos[r + 2] = a.z;
155
- const c = new g(i.qpos[r + 4], i.qpos[r + 5], i.qpos[r + 6], i.qpos[r + 3]).premultiply(o);
156
- i.qpos[r + 3] = c.w, i.qpos[r + 4] = c.x, i.qpos[r + 5] = c.y, i.qpos[r + 6] = c.z;
157
- const h = new f(i.qvel[s], i.qvel[s + 1], i.qvel[s + 2]).applyQuaternion(o);
158
- i.qvel[s] = h.x, i.qvel[s + 1] = h.y, i.qvel[s + 2] = h.z;
159
- const m = new f(i.qvel[s + 3], i.qvel[s + 4], i.qvel[s + 5]).applyQuaternion(o);
160
- i.qvel[s + 3] = m.x, i.qvel[s + 4] = m.y, i.qvel[s + 5] = m.z;
161
- }
162
- }
163
- /** Lower only the viewer-owned plane, never authored bodies or authored floors.
164
- * Run after forward kinematics, not during stepping (the floor must not follow falling bodies).
165
- * `baseHeight` re-seats from a stable reference; omitting it lowers from the current pose.
166
- */
167
- seatDefaultGround(t, e, i) {
168
- const o = this.mujoco, n = o.mj_name2id(t, o.mjtObj.mjOBJ_GEOM.value, "__viewer_default_ground__");
169
- if (n < 0 || t.geom_bodyid[n] !== 0 || t.geom_type[n] !== o.mjtGeom.mjGEOM_PLANE.value) return null;
170
- let r = i ?? t.geom_pos[n * 3 + 2];
171
- for (let s = 0; s < t.ngeom; s++) {
172
- if (s === n || t.geom_type[s] === o.mjtGeom.mjGEOM_PLANE.value || !(t.geom_contype[s] & t.geom_conaffinity[n]) && !(t.geom_conaffinity[s] & t.geom_contype[n])) continue;
173
- const a = s * 6, c = s * 9, h = e.geom_xmat[c + 6], m = e.geom_xmat[c + 7], d = e.geom_xmat[c + 8], u = e.geom_xpos[s * 3 + 2] + h * t.geom_aabb[a] + m * t.geom_aabb[a + 1] + d * t.geom_aabb[a + 2], j = Math.abs(h) * t.geom_aabb[a + 3] + Math.abs(m) * t.geom_aabb[a + 4] + Math.abs(d) * t.geom_aabb[a + 5], _ = u - j, b = Math.max(1e-3, t.geom_margin[s] + t.geom_margin[n]);
174
- Number.isFinite(_) && _ < r && (r = Math.min(r, _ - b));
175
- }
176
- return r !== t.geom_pos[n * 3 + 2] && (t.geom_pos[n * 3 + 2] = r, t.geom_sameframe[n] = 0, o.mj_forward(t, e)), { id: n, height: r, sameFrame: t.geom_sameframe[n] };
177
- }
178
- beginGrab({
179
- bodyId: t,
180
- localPoint: e,
181
- target: i,
182
- stiffness: o,
183
- damping: n,
184
- maxForce: r,
185
- dampingRatio: s = B,
186
- maxTargetSpeed: a = E,
187
- targetResponse: c = T
188
- }) {
189
- var _;
190
- const h = Math.max(((_ = this.model) == null ? void 0 : _.body_mass[t]) ?? 1, 1e-6), m = o ?? N, d = n ?? 2 * Math.sqrt(m * h) * s, u = r ?? Math.max(C, h * D);
191
- this.setTimestep(this.grabTimestep(t, e, h, m));
192
- const j = [i[0], i[1], i[2]];
193
- this.grab = {
194
- bodyId: t,
195
- localPoint: [e[0], e[1], e[2]],
196
- target: [...j],
197
- requestedTarget: j,
198
- stiffness: m,
199
- damping: d,
200
- maxForce: u,
201
- strength: 1,
202
- maxTargetSpeed: a,
203
- targetResponse: c
204
- };
205
- }
206
- updateGrabTarget(t) {
207
- this.grab && (this.grab.requestedTarget = [t[0], t[1], t[2]]);
208
- }
209
- updateGrabStrength(t) {
210
- this.grab && (this.grab.strength = t);
211
- }
212
- applyGrabForce() {
213
- if (!this.grab || !this.model || !this.data || !this.mujoco) return;
214
- const { bodyId: t, localPoint: e, target: i, stiffness: o, damping: n, maxForce: r, strength: s } = this.grab, a = this.data.xmat.subarray(t * 9, t * 9 + 9), c = this.data.xpos.subarray(t * 3, t * 3 + 3), h = [
215
- c[0] + a[0] * e[0] + a[1] * e[1] + a[2] * e[2],
216
- c[1] + a[3] * e[0] + a[4] * e[1] + a[5] * e[2],
217
- c[2] + a[6] * e[0] + a[7] * e[1] + a[8] * e[2]
218
- ], m = this.data.cvel.subarray(t * 6, t * 6 + 6), d = this.data.xipos.subarray(t * 3, t * 3 + 3), u = [
219
- h[0] - d[0],
220
- h[1] - d[1],
221
- h[2] - d[2]
222
- ], j = [
223
- m[3] + m[1] * u[2] - m[2] * u[1],
224
- m[4] + m[2] * u[0] - m[0] * u[2],
225
- m[5] + m[0] * u[1] - m[1] * u[0]
226
- ], _ = J({ target: i, point: h, velocity: j, stiffness: o, damping: n, maxForce: r, strength: s });
227
- this.lastGrabForce = [..._], this.data.qfrc_applied.fill(0);
228
- const b = Math.min(
229
- Math.max(this.model.body_inertia[t * 3], 1e-9),
230
- Math.max(this.model.body_inertia[t * 3 + 1], 1e-9),
231
- Math.max(this.model.body_inertia[t * 3 + 2], 1e-9)
232
- ), w = [
233
- -b * q * m[0],
234
- -b * q * m[1],
235
- -b * q * m[2]
236
- ];
237
- this.mujoco.mj_applyFT(
238
- this.model,
239
- this.data,
240
- _,
241
- w,
242
- h,
243
- t,
244
- this.data.qfrc_applied
245
- );
246
- }
247
- endGrab() {
248
- this.data && this.data.qfrc_applied.fill(0), this.lastGrabForce = [0, 0, 0], this.grab = null, this.setTimestep(y);
249
- }
250
- step(t) {
251
- if (!this.mujoco || !this.model || !this.data || t <= 0) return !1;
252
- this.accumulator += Math.min(t, L);
253
- let e = !1;
254
- for (; this.accumulator >= this.timestep; )
255
- this.updateGrabTargetForStep(), this.applyGrabForce(), this.mujoco.mj_step(this.model, this.data), this.accumulator -= this.timestep, e = !0;
256
- return e;
257
- }
258
- updateGrabTargetForStep() {
259
- const t = this.grab;
260
- if (!t) return;
261
- const e = t.requestedTarget ?? t.target, i = (t.maxTargetSpeed ?? E) * Math.max(1, Math.sqrt(t.strength)), o = t.targetResponse ?? T, n = 1 - Math.exp(-o * this.timestep), r = i * this.timestep;
262
- for (let s = 0; s < 3; s++) {
263
- const a = e[s] - t.target[s], c = Math.sign(a) * Math.min(Math.abs(a) * n, r);
264
- t.target[s] += c;
265
- }
266
- }
267
- setJointValues(t) {
268
- if (!(!this.mujoco || !this.model || !this.data)) {
269
- for (const [e, i] of Object.entries(t)) {
270
- const o = this.mujoco.mj_name2id(this.model, this.mujoco.mjtObj.mjOBJ_JOINT.value, e);
271
- if (o < 0 || !Number.isFinite(i)) continue;
272
- const n = this.model.jnt_type[o], r = this.mujoco.mjtJoint.mjJNT_HINGE.value, s = this.mujoco.mjtJoint.mjJNT_SLIDE.value;
273
- n !== r && n !== s || (this.data.qpos[this.model.jnt_qposadr[o]] = i);
274
- }
275
- this.mujoco.mj_forward(this.model, this.data), this.seatDefaultGround(this.model, this.data), this.accumulator = 0;
276
- }
277
- }
278
- getJointValues(t) {
279
- const e = {};
280
- if (!this.mujoco || !this.model || !this.data) return e;
281
- for (const i of t) {
282
- const o = this.mujoco.mj_name2id(this.model, this.mujoco.mjtObj.mjOBJ_JOINT.value, i);
283
- if (o < 0) continue;
284
- const n = this.model.jnt_type[o], r = this.mujoco.mjtJoint.mjJNT_HINGE.value, s = this.mujoco.mjtJoint.mjJNT_SLIDE.value;
285
- (n === r || n === s) && (e[i] = this.data.qpos[this.model.jnt_qposadr[o]]);
286
- }
287
- return e;
288
- }
289
- reset() {
290
- if (!(!this.mujoco || !this.model || !this.data)) {
291
- if (this.endGrab(), this.seatedGround) {
292
- const { id: t, height: e, sameFrame: i } = this.seatedGround;
293
- this.model.geom_pos[t * 3 + 2] = e, this.model.geom_sameframe[t] = i;
294
- }
295
- this.mujoco.mj_resetData(this.model, this.data), this.applyInitialControls(this.data, this.initialControls), this.applyInitialVelocities(this.model, this.data, this.initialVelocities), this.mujoco.mj_forward(this.model, this.data), this.accumulator = 0;
296
- }
297
- }
298
- dispose() {
299
- this.disposeModel(), this.mujoco = null;
300
- }
301
- applyInitialControls(t, e) {
302
- if (e != null && e.length) {
303
- if (e.length !== t.ctrl.length) throw v(new Error(`Initial control count ${e.length} does not match MuJoCo actuator count ${t.ctrl.length}.`), "E_MUJOCO_INITIALIZE");
304
- t.ctrl.set(e);
305
- }
306
- }
307
- /**
308
- * A point force on a light body can excite a much higher rotational
309
- * frequency than its translational mass suggests. Refine physics only while
310
- * that body is grabbed instead of adding global damping to hide the error.
311
- */
312
- grabTimestep(t, e, i, o) {
313
- if (!this.model || !this.data) return y;
314
- const n = Math.min(
315
- Math.max(this.model.body_inertia[t * 3], 1e-9),
316
- Math.max(this.model.body_inertia[t * 3 + 1], 1e-9),
317
- Math.max(this.model.body_inertia[t * 3 + 2], 1e-9)
318
- ), r = this.data.xpos.subarray(t * 3, t * 3 + 3), s = this.data.xipos.subarray(t * 3, t * 3 + 3), a = this.data.xmat.subarray(t * 9, t * 9 + 9), c = [
319
- r[0] + a[0] * e[0] + a[1] * e[1] + a[2] * e[2],
320
- r[1] + a[3] * e[0] + a[4] * e[1] + a[5] * e[2],
321
- r[2] + a[6] * e[0] + a[7] * e[1] + a[8] * e[2]
322
- ], h = (c[0] - s[0]) ** 2 + (c[1] - s[1]) ** 2 + (c[2] - s[2]) ** 2, m = Math.sqrt(o * (1 / i + h / n)), d = S / Math.max(m, 1e-9);
323
- return Math.max(R, Math.min(y, d));
324
- }
325
- setTimestep(t) {
326
- var e;
327
- this.timestep = t, (e = this.model) != null && e.opt && (this.model.opt.timestep = t);
328
- }
329
- applyInitialVelocities(t, e, i) {
330
- for (const o of i ?? []) {
331
- const n = this.mujoco.mj_name2id(t, this.mujoco.mjtObj.mjOBJ_BODY.value, o.body), r = n >= 0 ? t.body_jntadr[n] : -1;
332
- if (r < 0 || t.jnt_type[r] !== this.mujoco.mjtJoint.mjJNT_FREE.value || o.velocity.length !== 6 || !o.velocity.every(Number.isFinite))
333
- throw v(new Error(`Invalid free-body initial velocity: ${o.body}`), "E_MUJOCO_INITIALIZE");
334
- const { worldRotation: s } = this, a = new f(o.velocity[0], o.velocity[1], o.velocity[2]).applyQuaternion(s), c = new f(o.velocity[3], o.velocity[4], o.velocity[5]).applyQuaternion(s);
335
- e.qvel.set([a.x, a.y, a.z, c.x, c.y, c.z], t.jnt_dofadr[r]);
336
- }
337
- }
338
- disposeModel() {
339
- var t, e;
340
- this.endGrab(), (t = this.data) == null || t.delete(), (e = this.model) == null || e.delete(), this.data = null, this.model = null, this.initialGround = null, this.seatedGround = null, this.topLevelBodies = [], this.initialControls = [], this.initialVelocities = [], this.accumulator = 0;
341
- }
342
- }
343
- function v(l, t) {
344
- const e = l instanceof Error ? l : new Error(String(l));
345
- return Object.assign(e, { code: t });
346
- }
347
- export {
348
- Q as MujocoRuntime
349
- };