@hackbuild/humanoid 0.0.0-stage → 0.1.0
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/LICENSE +21 -0
- package/README.md +138 -2
- package/dist/files.d.ts +33 -0
- package/dist/files.js +125 -0
- package/dist/g1.d.ts +18 -0
- package/dist/g1.js +24 -0
- package/dist/humanoid.iife.js +520 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.js +4 -0
- package/dist/runtime.d.ts +95 -0
- package/dist/runtime.js +55 -0
- package/dist/sim.d.ts +70 -0
- package/dist/sim.js +185 -0
- package/dist/single.d.ts +2 -0
- package/dist/single.js +4 -0
- package/dist/three.d.ts +36 -0
- package/dist/three.js +104 -0
- package/how.ts +29 -0
- package/llms.md +284 -0
- package/package.json +50 -4
|
@@ -0,0 +1,520 @@
|
|
|
1
|
+
var HackbuildHumanoid = (function(exports, three) {
|
|
2
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
|
+
// src/runtime.ts
|
|
4
|
+
/**
|
|
5
|
+
* MuJoCo in the page: Google DeepMind's WebAssembly build (`@mujoco/mujoco`,
|
|
6
|
+
* Apache-2.0), the single threaded one, so the page needs no cross-origin
|
|
7
|
+
* isolation. This library never bundles it: pass the factory a bundler
|
|
8
|
+
* imported, or it loads at run time from a URL (default the pinned copy on
|
|
9
|
+
* jsDelivr, its wasm read beside it), and the page is told whose licence
|
|
10
|
+
* applies. The types here cover the part of its surface this library uses.
|
|
11
|
+
*/
|
|
12
|
+
/** The @mujoco/mujoco version the default URL pins. */
|
|
13
|
+
const MUJOCO_VERSION = "4.0.1";
|
|
14
|
+
/** Where MuJoCo loads from when no factory is given. */
|
|
15
|
+
const MUJOCO_URL = `https://cdn.jsdelivr.net/npm/@mujoco/mujoco@${MUJOCO_VERSION}/mujoco.js`;
|
|
16
|
+
/** The line a page shows the first time MuJoCo loads. */
|
|
17
|
+
const MUJOCO_LICENCE = "mujoco by google deepmind, under the apache license 2.0 (https://github.com/google-deepmind/mujoco). it loads at run time and is not part of this library.";
|
|
18
|
+
/** A model or runtime this library cannot open, with the reason in words a page can show. */
|
|
19
|
+
var HumanoidError = class extends Error {
|
|
20
|
+
constructor(message) {
|
|
21
|
+
super(message);
|
|
22
|
+
this.name = "HumanoidError";
|
|
23
|
+
}
|
|
24
|
+
};
|
|
25
|
+
const loads = /* @__PURE__ */ new Map();
|
|
26
|
+
/** Loads MuJoCo once per factory or URL and resolves to the module. */
|
|
27
|
+
function loadMujoco(o = {}) {
|
|
28
|
+
const key = o.factory ?? o.url ?? "https://cdn.jsdelivr.net/npm/@mujoco/mujoco@4.0.1/mujoco.js";
|
|
29
|
+
const have = loads.get(key);
|
|
30
|
+
if (have) return have;
|
|
31
|
+
const p = (async () => {
|
|
32
|
+
let factory = o.factory;
|
|
33
|
+
const url = o.url ?? "https://cdn.jsdelivr.net/npm/@mujoco/mujoco@4.0.1/mujoco.js";
|
|
34
|
+
if (!factory) try {
|
|
35
|
+
factory = (await import(
|
|
36
|
+
/* @vite-ignore */
|
|
37
|
+
url
|
|
38
|
+
)).default;
|
|
39
|
+
} catch (e) {
|
|
40
|
+
throw new HumanoidError(`mujoco did not load from ${url}: ${e instanceof Error ? e.message : String(e)}. check the connection, or pass the factory from @mujoco/mujoco.`);
|
|
41
|
+
}
|
|
42
|
+
const opts = {};
|
|
43
|
+
const w = o.wasm;
|
|
44
|
+
if (w instanceof ArrayBuffer || ArrayBuffer.isView(w)) opts.wasmBinary = w;
|
|
45
|
+
else if (typeof w === "string") opts.locateFile = () => w;
|
|
46
|
+
else if (!o.factory) opts.locateFile = (path) => new URL(path, url).href;
|
|
47
|
+
const mj = await factory(opts);
|
|
48
|
+
o.onLicence?.(MUJOCO_LICENCE);
|
|
49
|
+
return mj;
|
|
50
|
+
})();
|
|
51
|
+
loads.set(key, p);
|
|
52
|
+
p.catch(() => loads.delete(key));
|
|
53
|
+
return p;
|
|
54
|
+
}
|
|
55
|
+
// src/files.ts
|
|
56
|
+
/**
|
|
57
|
+
* A robot as files: its MJCF and every file it names (included MJCF, meshes,
|
|
58
|
+
* textures, height fields, skins), from a URL or from files the person
|
|
59
|
+
* dropped. Paths are keyed the way MuJoCo looks them up in its virtual file
|
|
60
|
+
* system: an include by its own path, a mesh by meshdir (or assetdir) and
|
|
61
|
+
* its file, a texture by texturedir and its file. A file that does not
|
|
62
|
+
* arrive is listed in `missing`.
|
|
63
|
+
*/
|
|
64
|
+
const attrs = (tag) => Object.fromEntries([...tag.matchAll(/([A-Za-z_]+)\s*=\s*"([^"]*)"/g)].map((m) => [m[1], m[2]]));
|
|
65
|
+
const join = (dir, file) => (dir ? `${dir.replace(/\/+$/, "")}/${file}` : file).replace(/^\.\//, "");
|
|
66
|
+
/** The files one MJCF names, split into includes and the rest, joined onto its compiler's directories. */
|
|
67
|
+
function referencedFiles(xml, dirs = {}) {
|
|
68
|
+
const d = { ...dirs };
|
|
69
|
+
for (const m of xml.matchAll(/<compiler\b[^>]*>/g)) {
|
|
70
|
+
const a = attrs(m[0]);
|
|
71
|
+
if (a.meshdir !== void 0) d.meshdir = a.meshdir;
|
|
72
|
+
if (a.texturedir !== void 0) d.texturedir = a.texturedir;
|
|
73
|
+
if (a.assetdir !== void 0) d.assetdir = a.assetdir;
|
|
74
|
+
}
|
|
75
|
+
const includes = [...xml.matchAll(/<include\b[^>]*>/g)].map((m) => attrs(m[0]).file).filter((f) => !!f);
|
|
76
|
+
const files = [];
|
|
77
|
+
for (const m of xml.matchAll(/<(mesh|skin|hfield|texture)\b[^>]*>/g)) {
|
|
78
|
+
const f = attrs(m[0]).file;
|
|
79
|
+
if (!f) continue;
|
|
80
|
+
const dir = m[1] === "texture" ? d.texturedir ?? d.assetdir : d.meshdir ?? d.assetdir;
|
|
81
|
+
files.push(join(dir, f));
|
|
82
|
+
}
|
|
83
|
+
return {
|
|
84
|
+
includes,
|
|
85
|
+
files: [...new Set(files)]
|
|
86
|
+
};
|
|
87
|
+
}
|
|
88
|
+
/** Compiler directories set anywhere in a set of MJCF texts: an included file's compiler applies to the whole model. */
|
|
89
|
+
function dirsOf(xmls) {
|
|
90
|
+
const d = {};
|
|
91
|
+
for (const x of xmls) for (const m of x.matchAll(/<compiler\b[^>]*>/g)) {
|
|
92
|
+
const a = attrs(m[0]);
|
|
93
|
+
if (a.meshdir !== void 0) d.meshdir = a.meshdir;
|
|
94
|
+
if (a.texturedir !== void 0) d.texturedir = a.texturedir;
|
|
95
|
+
if (a.assetdir !== void 0) d.assetdir = a.assetdir;
|
|
96
|
+
}
|
|
97
|
+
return d;
|
|
98
|
+
}
|
|
99
|
+
/** Reads an MJCF and everything it names through `read`, includes first; a file `read` cannot give is listed as missing. */
|
|
100
|
+
async function gather(main, read, onFile) {
|
|
101
|
+
const dec = new TextDecoder();
|
|
102
|
+
const first = await read(main);
|
|
103
|
+
if (!first) throw new HumanoidError(`the model file ${main} did not load.`);
|
|
104
|
+
const xml = dec.decode(first);
|
|
105
|
+
const assets = /* @__PURE__ */ new Map();
|
|
106
|
+
const missing = [];
|
|
107
|
+
const texts = [xml];
|
|
108
|
+
const queue = [...referencedFiles(xml).includes];
|
|
109
|
+
while (queue.length) {
|
|
110
|
+
const inc = queue.shift();
|
|
111
|
+
if (assets.has(inc)) continue;
|
|
112
|
+
const b = await read(inc);
|
|
113
|
+
if (!b) {
|
|
114
|
+
missing.push(inc);
|
|
115
|
+
continue;
|
|
116
|
+
}
|
|
117
|
+
assets.set(inc, b);
|
|
118
|
+
const t = dec.decode(b);
|
|
119
|
+
texts.push(t);
|
|
120
|
+
queue.push(...referencedFiles(t).includes);
|
|
121
|
+
}
|
|
122
|
+
const dirs = dirsOf(texts);
|
|
123
|
+
const wanted = [...new Set(texts.flatMap((t) => referencedFiles(t, dirs).files))].filter((f) => !assets.has(f));
|
|
124
|
+
let done = 0;
|
|
125
|
+
await Promise.all(wanted.map(async (f) => {
|
|
126
|
+
const b = await read(f);
|
|
127
|
+
if (b) assets.set(f, b);
|
|
128
|
+
else missing.push(f);
|
|
129
|
+
onFile?.(f, ++done, wanted.length);
|
|
130
|
+
}));
|
|
131
|
+
return {
|
|
132
|
+
xml,
|
|
133
|
+
assets,
|
|
134
|
+
missing
|
|
135
|
+
};
|
|
136
|
+
}
|
|
137
|
+
/** A robot from the URL of its main MJCF; every file it names is fetched beside it. */
|
|
138
|
+
function fetchModel(url, o = {}) {
|
|
139
|
+
const f = o.fetch ?? globalThis.fetch.bind(globalThis);
|
|
140
|
+
const here = globalThis.location?.href;
|
|
141
|
+
const base = new URL(url, here);
|
|
142
|
+
const read = async (path) => {
|
|
143
|
+
try {
|
|
144
|
+
const r = await f(path === url ? base.href : new URL(path, base).href);
|
|
145
|
+
return r.ok ? new Uint8Array(await r.arrayBuffer()) : null;
|
|
146
|
+
} catch {
|
|
147
|
+
return null;
|
|
148
|
+
}
|
|
149
|
+
};
|
|
150
|
+
return gather(url, read, o.onFile);
|
|
151
|
+
}
|
|
152
|
+
/** A robot from dropped files (a folder keeps its paths): the main MJCF is the one with a worldbody that no other file includes. */
|
|
153
|
+
async function filesFrom(list) {
|
|
154
|
+
const files = /* @__PURE__ */ new Map();
|
|
155
|
+
for (const f of list) files.set((f.webkitRelativePath || f.name).replace(/\\/g, "/"), f);
|
|
156
|
+
const xmls = [...files.keys()].filter((p) => /\.xml$/i.test(p));
|
|
157
|
+
const texts = /* @__PURE__ */ new Map();
|
|
158
|
+
for (const p of xmls) texts.set(p, await files.get(p).text());
|
|
159
|
+
const included = new Set([...texts].flatMap(([p, t]) => referencedFiles(t).includes.map((i) => join(p.split("/").slice(0, -1).join("/"), i))));
|
|
160
|
+
const main = xmls.filter((p) => !included.has(p) && /<worldbody/.test(texts.get(p))).sort((a, b) => a.split("/").length - b.split("/").length || a.length - b.length)[0];
|
|
161
|
+
if (!main) throw new HumanoidError("no mjcf here: drop the robot's folder, with an .xml that has a worldbody.");
|
|
162
|
+
const root = main.split("/").slice(0, -1).join("/");
|
|
163
|
+
const read = async (path) => {
|
|
164
|
+
const f = files.get(path === main ? main : join(root, path));
|
|
165
|
+
return f ? new Uint8Array(await f.arrayBuffer()) : null;
|
|
166
|
+
};
|
|
167
|
+
return gather(main, read);
|
|
168
|
+
}
|
|
169
|
+
// src/sim.ts
|
|
170
|
+
const FREE = 0;
|
|
171
|
+
const isArrayLike = (v) => Array.isArray(v) || ArrayBuffer.isView(v);
|
|
172
|
+
/** A robot running in MuJoCo. */
|
|
173
|
+
var Humanoid = class Humanoid {
|
|
174
|
+
mj;
|
|
175
|
+
model;
|
|
176
|
+
data;
|
|
177
|
+
/** Joint names in model order (a free joint included). */
|
|
178
|
+
joints;
|
|
179
|
+
/** The joint each actuator drives, in actuator order: `targets[i]` is that joint's target for a position actuator. */
|
|
180
|
+
actuated;
|
|
181
|
+
bodies;
|
|
182
|
+
keyframes;
|
|
183
|
+
/** Files the model named that did not arrive. */
|
|
184
|
+
missing;
|
|
185
|
+
/** The model's timestep, seconds. */
|
|
186
|
+
timestep;
|
|
187
|
+
carry = 0;
|
|
188
|
+
gone = false;
|
|
189
|
+
ramp = null;
|
|
190
|
+
constructor(mj, model, missing) {
|
|
191
|
+
this.mj = mj;
|
|
192
|
+
this.model = model;
|
|
193
|
+
this.data = new mj.MjData(model);
|
|
194
|
+
const name = (type, i) => mj.mj_id2name(model, mj.mjtObj[type].value, i) || `${type.replace("mjOBJ_", "").toLowerCase()} ${i}`;
|
|
195
|
+
this.joints = Array.from({ length: model.njnt }, (_, i) => name("mjOBJ_JOINT", i));
|
|
196
|
+
this.bodies = Array.from({ length: model.nbody }, (_, i) => name("mjOBJ_BODY", i));
|
|
197
|
+
this.keyframes = Array.from({ length: model.nkey }, (_, i) => name("mjOBJ_KEY", i));
|
|
198
|
+
this.actuated = Array.from({ length: model.nu }, (_, i) => this.joints[model.actuator_trnid[i * 2]] ?? `actuator ${i}`);
|
|
199
|
+
this.timestep = model.opt.timestep;
|
|
200
|
+
this.missing = missing;
|
|
201
|
+
mj.mj_forward(model, this.data);
|
|
202
|
+
}
|
|
203
|
+
/** Compiles a robot from its files; a model MuJoCo cannot compile is an error with MuJoCo's reason. */
|
|
204
|
+
static open(mj, files) {
|
|
205
|
+
const vfs = new mj.MjVFS();
|
|
206
|
+
try {
|
|
207
|
+
for (const [path, bytes] of files.assets) vfs.addBuffer(path, bytes);
|
|
208
|
+
let model;
|
|
209
|
+
try {
|
|
210
|
+
model = mj.MjModel.from_xml_string(files.xml, vfs);
|
|
211
|
+
} catch (e) {
|
|
212
|
+
throw new HumanoidError(`mujoco could not compile the model: ${e instanceof Error ? e.message : String(e)}${files.missing?.length ? `. files it names that did not arrive: ${files.missing.slice(0, 3).join(", ")}` : ""}`);
|
|
213
|
+
}
|
|
214
|
+
return new Humanoid(mj, model, files.missing ?? []);
|
|
215
|
+
} finally {
|
|
216
|
+
vfs.delete();
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
/** Seconds simulated so far. */
|
|
220
|
+
get time() {
|
|
221
|
+
return this.data.time;
|
|
222
|
+
}
|
|
223
|
+
/** The actuators' controls: targets in radians for position actuators, a view on the model's data. */
|
|
224
|
+
get targets() {
|
|
225
|
+
return this.data.ctrl;
|
|
226
|
+
}
|
|
227
|
+
/** Back to a keyframe by name or index (its controls too), or to the model's rest state. */
|
|
228
|
+
reset(key) {
|
|
229
|
+
const k = typeof key === "string" ? this.keyframes.indexOf(key) : key;
|
|
230
|
+
if (typeof key === "string" && k < 0) throw new HumanoidError(`the model has no keyframe ${key}; it has ${this.keyframes.join(", ") || "none"}.`);
|
|
231
|
+
if (k === void 0) this.mj.mj_resetData(this.model, this.data);
|
|
232
|
+
else this.mj.mj_resetDataKeyframe(this.model, this.data, k);
|
|
233
|
+
this.mj.mj_forward(this.model, this.data);
|
|
234
|
+
this.carry = 0;
|
|
235
|
+
this.ramp = null;
|
|
236
|
+
}
|
|
237
|
+
/**
|
|
238
|
+
* Eases the targets named (by actuated joint, or an array in actuator order) from where they are to
|
|
239
|
+
* the new values over `seconds` of simulated time, on a smoothstep; others keep theirs. A sudden
|
|
240
|
+
* jump in targets can topple a robot that a ramp of half a second leaves standing (measured on the G1).
|
|
241
|
+
*/
|
|
242
|
+
moveTo(targets, seconds = .5) {
|
|
243
|
+
const from = Float64Array.from(this.data.ctrl);
|
|
244
|
+
const to = Float64Array.from(from);
|
|
245
|
+
if (isArrayLike(targets)) for (let i = 0; i < Math.min(to.length, targets.length); i++) to[i] = targets[i];
|
|
246
|
+
else for (const [name, v] of Object.entries(targets)) {
|
|
247
|
+
const i = this.actuated.indexOf(name);
|
|
248
|
+
if (i < 0) throw new HumanoidError(`no actuator drives ${name}.`);
|
|
249
|
+
to[i] = v;
|
|
250
|
+
}
|
|
251
|
+
this.ramp = {
|
|
252
|
+
from,
|
|
253
|
+
to,
|
|
254
|
+
t0: this.data.time,
|
|
255
|
+
dur: Math.max(0, seconds)
|
|
256
|
+
};
|
|
257
|
+
if (seconds <= 0) this.applyRamp();
|
|
258
|
+
}
|
|
259
|
+
/** True while a {@link moveTo} is still easing. */
|
|
260
|
+
get moving() {
|
|
261
|
+
return this.ramp !== null;
|
|
262
|
+
}
|
|
263
|
+
applyRamp() {
|
|
264
|
+
const r = this.ramp;
|
|
265
|
+
if (!r) return;
|
|
266
|
+
const k = r.dur > 0 ? Math.min(1, (this.data.time - r.t0) / r.dur) : 1;
|
|
267
|
+
const e = k * k * (3 - 2 * k);
|
|
268
|
+
for (let i = 0; i < r.to.length; i++) this.data.ctrl[i] = r.from[i] + (r.to[i] - r.from[i]) * e;
|
|
269
|
+
if (k >= 1) this.ramp = null;
|
|
270
|
+
}
|
|
271
|
+
/** Each actuated joint's target set to where the joint is now: the body holds its pose. */
|
|
272
|
+
hold() {
|
|
273
|
+
const { model, data } = this;
|
|
274
|
+
for (let i = 0; i < model.nu; i++) {
|
|
275
|
+
const j = model.actuator_trnid[i * 2];
|
|
276
|
+
data.ctrl[i] = data.qpos[model.jnt_qposadr[j]];
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
/** Advances by `seconds` in steps of the model's timestep, carrying the remainder; at most `max` seconds a call (default 0.1). Returns the steps run. */
|
|
280
|
+
step(seconds, max = .1) {
|
|
281
|
+
if (this.gone) return 0;
|
|
282
|
+
this.carry = Math.min(this.carry + Math.max(0, seconds), max);
|
|
283
|
+
const n = Math.floor(this.carry / this.timestep + 1e-6);
|
|
284
|
+
for (let i = 0; i < n; i++) {
|
|
285
|
+
this.applyRamp();
|
|
286
|
+
this.mj.mj_step(this.model, this.data);
|
|
287
|
+
}
|
|
288
|
+
this.carry = Math.max(0, this.carry - n * this.timestep);
|
|
289
|
+
return n;
|
|
290
|
+
}
|
|
291
|
+
/** A body's world pose, or null for a name the model lacks. */
|
|
292
|
+
pose(body) {
|
|
293
|
+
const i = typeof body === "number" ? body : this.bodies.indexOf(body);
|
|
294
|
+
if (i < 0 || i >= this.model.nbody) return null;
|
|
295
|
+
const p = this.data.xpos, q = this.data.xquat;
|
|
296
|
+
return {
|
|
297
|
+
pos: [
|
|
298
|
+
p[i * 3],
|
|
299
|
+
p[i * 3 + 1],
|
|
300
|
+
p[i * 3 + 2]
|
|
301
|
+
],
|
|
302
|
+
quat: [
|
|
303
|
+
q[i * 4],
|
|
304
|
+
q[i * 4 + 1],
|
|
305
|
+
q[i * 4 + 2],
|
|
306
|
+
q[i * 4 + 3]
|
|
307
|
+
]
|
|
308
|
+
};
|
|
309
|
+
}
|
|
310
|
+
/** The body carried by the first free joint (a robot's pelvis), or null when the robot is fixed to the world. */
|
|
311
|
+
get rootBody() {
|
|
312
|
+
for (let j = 0; j < this.model.njnt; j++) if (this.model.jnt_type[j] === FREE) return this.bodies[this.model.jnt_bodyid[j]] ?? null;
|
|
313
|
+
return null;
|
|
314
|
+
}
|
|
315
|
+
/** A shove: adds a velocity in m/s, in MuJoCo's world frame, to the body on the first free joint. False when there is none. */
|
|
316
|
+
push(vx, vy, vz = 0) {
|
|
317
|
+
for (let j = 0; j < this.model.njnt; j++) {
|
|
318
|
+
if (this.model.jnt_type[j] !== FREE) continue;
|
|
319
|
+
const d = this.model.jnt_dofadr[j];
|
|
320
|
+
this.data.qvel[d] += vx;
|
|
321
|
+
this.data.qvel[d + 1] += vy;
|
|
322
|
+
this.data.qvel[d + 2] += vz;
|
|
323
|
+
return true;
|
|
324
|
+
}
|
|
325
|
+
return false;
|
|
326
|
+
}
|
|
327
|
+
/** A hinge or slide joint's position and velocity, or null for a name the model lacks. */
|
|
328
|
+
joint(name) {
|
|
329
|
+
const j = this.joints.indexOf(name);
|
|
330
|
+
if (j < 0) return null;
|
|
331
|
+
return {
|
|
332
|
+
q: this.data.qpos[this.model.jnt_qposadr[j]],
|
|
333
|
+
dq: this.data.qvel[this.model.jnt_dofadr[j]]
|
|
334
|
+
};
|
|
335
|
+
}
|
|
336
|
+
/** Frees the model and its data. */
|
|
337
|
+
dispose() {
|
|
338
|
+
if (this.gone) return;
|
|
339
|
+
this.gone = true;
|
|
340
|
+
this.data.delete();
|
|
341
|
+
this.model.delete();
|
|
342
|
+
}
|
|
343
|
+
};
|
|
344
|
+
// src/g1.ts
|
|
345
|
+
/**
|
|
346
|
+
* The Unitree G1 (29 joints) from MuJoCo Menagerie, pinned to one commit:
|
|
347
|
+
* its scene, model and 35 meshes load at run time from GitHub and are not
|
|
348
|
+
* part of this library. The model holds its pose with position actuators;
|
|
349
|
+
* its keyframe "stand" is the robot upright with its arms down.
|
|
350
|
+
*/
|
|
351
|
+
/** The G1 from MuJoCo Menagerie, by URL, with its licence and download size. */
|
|
352
|
+
const G1 = {
|
|
353
|
+
name: "unitree g1, 29 joints",
|
|
354
|
+
/** The scene (floor and light) that includes the robot, at menagerie commit 98a70c5. */
|
|
355
|
+
url: "https://raw.githubusercontent.com/google-deepmind/mujoco_menagerie/98a70c569c4c1b6da467a20012f766ee1f8dc521/unitree_g1/scene.xml",
|
|
356
|
+
licence: "unitree g1 model and meshes from mujoco menagerie, bsd-3-clause, copyright unitree robotics. they load at run time and are not part of this library.",
|
|
357
|
+
/** The meshes the model uses, in bytes (vendor: GitHub's sizes). */
|
|
358
|
+
bytes: 19666440,
|
|
359
|
+
keyframe: "stand",
|
|
360
|
+
/** The 29 actuated joints in the model's order: legs, waist, arms. */
|
|
361
|
+
joints: [
|
|
362
|
+
"left_hip_pitch_joint",
|
|
363
|
+
"left_hip_roll_joint",
|
|
364
|
+
"left_hip_yaw_joint",
|
|
365
|
+
"left_knee_joint",
|
|
366
|
+
"left_ankle_pitch_joint",
|
|
367
|
+
"left_ankle_roll_joint",
|
|
368
|
+
"right_hip_pitch_joint",
|
|
369
|
+
"right_hip_roll_joint",
|
|
370
|
+
"right_hip_yaw_joint",
|
|
371
|
+
"right_knee_joint",
|
|
372
|
+
"right_ankle_pitch_joint",
|
|
373
|
+
"right_ankle_roll_joint",
|
|
374
|
+
"waist_yaw_joint",
|
|
375
|
+
"waist_roll_joint",
|
|
376
|
+
"waist_pitch_joint",
|
|
377
|
+
"left_shoulder_pitch_joint",
|
|
378
|
+
"left_shoulder_roll_joint",
|
|
379
|
+
"left_shoulder_yaw_joint",
|
|
380
|
+
"left_elbow_joint",
|
|
381
|
+
"left_wrist_roll_joint",
|
|
382
|
+
"left_wrist_pitch_joint",
|
|
383
|
+
"left_wrist_yaw_joint",
|
|
384
|
+
"right_shoulder_pitch_joint",
|
|
385
|
+
"right_shoulder_roll_joint",
|
|
386
|
+
"right_shoulder_yaw_joint",
|
|
387
|
+
"right_elbow_joint",
|
|
388
|
+
"right_wrist_roll_joint",
|
|
389
|
+
"right_wrist_pitch_joint",
|
|
390
|
+
"right_wrist_yaw_joint"
|
|
391
|
+
]
|
|
392
|
+
};
|
|
393
|
+
// src/three.ts
|
|
394
|
+
/**
|
|
395
|
+
* The robot in three.js: one mesh per visible geom (spheres, capsules,
|
|
396
|
+
* cylinders, boxes, ellipsoids, planes and MuJoCo's own meshes, each mesh's
|
|
397
|
+
* geometry shared), coloured from the geom's material or rgba, moved each
|
|
398
|
+
* frame from the geoms' world poses. MuJoCo is z up; the view's root turns
|
|
399
|
+
* that to y up and the robot's forward axis to +Z, the family's humanoid
|
|
400
|
+
* space. Collision geoms (group 3 and up) stay hidden unless asked for.
|
|
401
|
+
*/
|
|
402
|
+
const PLANE = 0;
|
|
403
|
+
const SPHERE = 2;
|
|
404
|
+
const CAPSULE = 3;
|
|
405
|
+
const ELLIPSOID = 4;
|
|
406
|
+
const CYLINDER = 5;
|
|
407
|
+
const BOX = 6;
|
|
408
|
+
const MESH = 7;
|
|
409
|
+
const TURN = {
|
|
410
|
+
x: -Math.PI / 2,
|
|
411
|
+
"-x": Math.PI / 2,
|
|
412
|
+
y: Math.PI,
|
|
413
|
+
"-y": 0
|
|
414
|
+
};
|
|
415
|
+
/** A three.js group that follows a {@link Humanoid}; add `root` to a scene and call `update` after each step. */
|
|
416
|
+
var HumanoidView = class {
|
|
417
|
+
sim;
|
|
418
|
+
root = new three.Group();
|
|
419
|
+
inner = new three.Group();
|
|
420
|
+
meshes = [];
|
|
421
|
+
shared = /* @__PURE__ */ new Map();
|
|
422
|
+
owned = [];
|
|
423
|
+
constructor(sim, o = {}) {
|
|
424
|
+
this.sim = sim;
|
|
425
|
+
const m = sim.model;
|
|
426
|
+
const groups = new Set(o.groups ?? [
|
|
427
|
+
0,
|
|
428
|
+
1,
|
|
429
|
+
2
|
|
430
|
+
]);
|
|
431
|
+
this.root.name = "humanoid";
|
|
432
|
+
this.root.rotation.y = TURN[o.forward ?? "x"];
|
|
433
|
+
this.inner.rotation.x = -Math.PI / 2;
|
|
434
|
+
this.root.add(this.inner);
|
|
435
|
+
for (let g = 0; g < m.ngeom; g++) {
|
|
436
|
+
const type = m.geom_type[g];
|
|
437
|
+
if (!groups.has(m.geom_group[g]) || type === PLANE && !o.floor) continue;
|
|
438
|
+
const geo = this.geometry(g, type);
|
|
439
|
+
if (!geo) continue;
|
|
440
|
+
const mat = m.geom_matid?.[g] ?? -1;
|
|
441
|
+
const rgba = mat >= 0 && m.mat_rgba ? m.mat_rgba.subarray(mat * 4, mat * 4 + 4) : m.geom_rgba.subarray(g * 4, g * 4 + 4);
|
|
442
|
+
const material = new three.MeshStandardMaterial({
|
|
443
|
+
roughness: .6,
|
|
444
|
+
metalness: .1,
|
|
445
|
+
transparent: rgba[3] < 1,
|
|
446
|
+
opacity: rgba[3]
|
|
447
|
+
});
|
|
448
|
+
material.color.setRGB(rgba[0], rgba[1], rgba[2]);
|
|
449
|
+
this.owned.push(material);
|
|
450
|
+
const mesh = new three.Mesh(geo, material);
|
|
451
|
+
mesh.matrixAutoUpdate = false;
|
|
452
|
+
mesh.castShadow = mesh.receiveShadow = type !== PLANE;
|
|
453
|
+
this.inner.add(mesh);
|
|
454
|
+
this.meshes.push({
|
|
455
|
+
mesh,
|
|
456
|
+
geom: g
|
|
457
|
+
});
|
|
458
|
+
}
|
|
459
|
+
this.update();
|
|
460
|
+
}
|
|
461
|
+
geometry(g, type) {
|
|
462
|
+
const m = this.sim.model;
|
|
463
|
+
const s = m.geom_size.subarray(g * 3, g * 3 + 3);
|
|
464
|
+
const own = (geo) => (this.owned.push(geo), geo);
|
|
465
|
+
switch (type) {
|
|
466
|
+
case SPHERE: return own(new three.SphereGeometry(s[0], 20, 14));
|
|
467
|
+
case ELLIPSOID: return own(new three.SphereGeometry(1, 20, 14).scale(s[0], s[1], s[2]));
|
|
468
|
+
case CAPSULE: return own(new three.CapsuleGeometry(s[0], 2 * s[1], 6, 16).rotateX(Math.PI / 2));
|
|
469
|
+
case CYLINDER: return own(new three.CylinderGeometry(s[0], s[0], 2 * s[1], 20).rotateX(Math.PI / 2));
|
|
470
|
+
case BOX: return own(new three.BoxGeometry(2 * s[0], 2 * s[1], 2 * s[2]));
|
|
471
|
+
case PLANE: return own(new three.PlaneGeometry(s[0] ? 2 * s[0] : 40, s[1] ? 2 * s[1] : 40));
|
|
472
|
+
case MESH: {
|
|
473
|
+
const id = m.geom_dataid[g];
|
|
474
|
+
if (id < 0) return null;
|
|
475
|
+
const have = this.shared.get(id);
|
|
476
|
+
if (have) return have;
|
|
477
|
+
const v = m.mesh_vertadr[id], nv = m.mesh_vertnum[id], f = m.mesh_faceadr[id], nf = m.mesh_facenum[id];
|
|
478
|
+
const geo = new three.BufferGeometry();
|
|
479
|
+
geo.setAttribute("position", new three.BufferAttribute(m.mesh_vert.slice(v * 3, (v + nv) * 3), 3));
|
|
480
|
+
geo.setIndex(new three.BufferAttribute(Uint32Array.from(m.mesh_face.subarray(f * 3, (f + nf) * 3)), 1));
|
|
481
|
+
geo.computeVertexNormals();
|
|
482
|
+
this.shared.set(id, geo);
|
|
483
|
+
return own(geo);
|
|
484
|
+
}
|
|
485
|
+
default: return null;
|
|
486
|
+
}
|
|
487
|
+
}
|
|
488
|
+
/** Moves every mesh to its geom's pose after the last step. */
|
|
489
|
+
update() {
|
|
490
|
+
const p = this.sim.data.geom_xpos, r = this.sim.data.geom_xmat;
|
|
491
|
+
for (const { mesh, geom: g } of this.meshes) {
|
|
492
|
+
const a = g * 9, b = g * 3;
|
|
493
|
+
mesh.matrix.set(r[a], r[a + 1], r[a + 2], p[b], r[a + 3], r[a + 4], r[a + 5], p[b + 1], r[a + 6], r[a + 7], r[a + 8], p[b + 2], 0, 0, 0, 1);
|
|
494
|
+
mesh.matrixWorldNeedsUpdate = true;
|
|
495
|
+
}
|
|
496
|
+
}
|
|
497
|
+
/** Meshes drawn. */
|
|
498
|
+
get count() {
|
|
499
|
+
return this.meshes.length;
|
|
500
|
+
}
|
|
501
|
+
/** Removes the group and frees its geometries and materials. */
|
|
502
|
+
dispose() {
|
|
503
|
+
this.root.removeFromParent();
|
|
504
|
+
for (const o of this.owned) o.dispose();
|
|
505
|
+
this.meshes = [];
|
|
506
|
+
}
|
|
507
|
+
};
|
|
508
|
+
exports.G1 = G1;
|
|
509
|
+
exports.Humanoid = Humanoid;
|
|
510
|
+
exports.HumanoidError = HumanoidError;
|
|
511
|
+
exports.HumanoidView = HumanoidView;
|
|
512
|
+
exports.MUJOCO_LICENCE = MUJOCO_LICENCE;
|
|
513
|
+
exports.MUJOCO_URL = MUJOCO_URL;
|
|
514
|
+
exports.MUJOCO_VERSION = MUJOCO_VERSION;
|
|
515
|
+
exports.fetchModel = fetchModel;
|
|
516
|
+
exports.filesFrom = filesFrom;
|
|
517
|
+
exports.loadMujoco = loadMujoco;
|
|
518
|
+
exports.referencedFiles = referencedFiles;
|
|
519
|
+
return exports;
|
|
520
|
+
})({}, THREE);
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
export { MUJOCO_LICENCE, MUJOCO_URL, MUJOCO_VERSION, HumanoidError, loadMujoco, type Mujoco, type MujocoOptions, type MjModel, type MjData, type MjVFS } from "./runtime.js";
|
|
2
|
+
export { fetchModel, filesFrom, referencedFiles, type ModelFiles } from "./files.js";
|
|
3
|
+
export { Humanoid, type Pose } from "./sim.js";
|
|
4
|
+
export { G1 } from "./g1.js";
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* MuJoCo in the page: Google DeepMind's WebAssembly build (`@mujoco/mujoco`,
|
|
3
|
+
* Apache-2.0), the single threaded one, so the page needs no cross-origin
|
|
4
|
+
* isolation. This library never bundles it: pass the factory a bundler
|
|
5
|
+
* imported, or it loads at run time from a URL (default the pinned copy on
|
|
6
|
+
* jsDelivr, its wasm read beside it), and the page is told whose licence
|
|
7
|
+
* applies. The types here cover the part of its surface this library uses.
|
|
8
|
+
*/
|
|
9
|
+
/** The @mujoco/mujoco version the default URL pins. */
|
|
10
|
+
export declare const MUJOCO_VERSION = "4.0.1";
|
|
11
|
+
/** Where MuJoCo loads from when no factory is given. */
|
|
12
|
+
export declare const MUJOCO_URL = "https://cdn.jsdelivr.net/npm/@mujoco/mujoco@4.0.1/mujoco.js";
|
|
13
|
+
/** The line a page shows the first time MuJoCo loads. */
|
|
14
|
+
export declare const MUJOCO_LICENCE = "mujoco by google deepmind, under the apache license 2.0 (https://github.com/google-deepmind/mujoco). it loads at run time and is not part of this library.";
|
|
15
|
+
/** A compiled model: sizes, options and the arrays this library reads. */
|
|
16
|
+
export interface MjModel {
|
|
17
|
+
nq: number;
|
|
18
|
+
nv: number;
|
|
19
|
+
nu: number;
|
|
20
|
+
nbody: number;
|
|
21
|
+
njnt: number;
|
|
22
|
+
ngeom: number;
|
|
23
|
+
nmesh: number;
|
|
24
|
+
nkey: number;
|
|
25
|
+
opt: {
|
|
26
|
+
timestep: number;
|
|
27
|
+
};
|
|
28
|
+
jnt_type: Int32Array;
|
|
29
|
+
jnt_qposadr: Int32Array;
|
|
30
|
+
jnt_dofadr: Int32Array;
|
|
31
|
+
jnt_bodyid: Int32Array;
|
|
32
|
+
actuator_trnid: Int32Array;
|
|
33
|
+
geom_type: Int32Array;
|
|
34
|
+
geom_size: Float64Array;
|
|
35
|
+
geom_rgba: Float32Array;
|
|
36
|
+
geom_group: Int32Array;
|
|
37
|
+
geom_dataid: Int32Array;
|
|
38
|
+
mesh_vertadr: Int32Array;
|
|
39
|
+
mesh_vertnum: Int32Array;
|
|
40
|
+
mesh_faceadr: Int32Array;
|
|
41
|
+
mesh_facenum: Int32Array;
|
|
42
|
+
mesh_vert: Float32Array;
|
|
43
|
+
mesh_face: Int32Array;
|
|
44
|
+
delete(): void;
|
|
45
|
+
}
|
|
46
|
+
/** A model's state; the arrays are views on the wasm heap, live after each step. */
|
|
47
|
+
export interface MjData {
|
|
48
|
+
time: number;
|
|
49
|
+
qpos: Float64Array;
|
|
50
|
+
qvel: Float64Array;
|
|
51
|
+
ctrl: Float64Array;
|
|
52
|
+
xpos: Float64Array;
|
|
53
|
+
xquat: Float64Array;
|
|
54
|
+
geom_xpos: Float64Array;
|
|
55
|
+
geom_xmat: Float64Array;
|
|
56
|
+
delete(): void;
|
|
57
|
+
}
|
|
58
|
+
/** MuJoCo's virtual file system, for the files a model names. */
|
|
59
|
+
export interface MjVFS {
|
|
60
|
+
addBuffer(name: string, bytes: Uint8Array): void;
|
|
61
|
+
delete(): void;
|
|
62
|
+
}
|
|
63
|
+
/** The MuJoCo module: the classes and functions this library calls. */
|
|
64
|
+
export interface Mujoco {
|
|
65
|
+
MjModel: {
|
|
66
|
+
from_xml_string(xml: string, vfs?: MjVFS): MjModel;
|
|
67
|
+
};
|
|
68
|
+
MjData: new (model: MjModel) => MjData;
|
|
69
|
+
MjVFS: new () => MjVFS;
|
|
70
|
+
mj_step(model: MjModel, data: MjData): void;
|
|
71
|
+
mj_forward(model: MjModel, data: MjData): void;
|
|
72
|
+
mj_resetData(model: MjModel, data: MjData): void;
|
|
73
|
+
mj_resetDataKeyframe(model: MjModel, data: MjData, key: number): void;
|
|
74
|
+
mj_id2name(model: MjModel, type: number, id: number): string;
|
|
75
|
+
mjtObj: Record<string, {
|
|
76
|
+
value: number;
|
|
77
|
+
}>;
|
|
78
|
+
}
|
|
79
|
+
/** Options for {@link loadMujoco}. */
|
|
80
|
+
export interface MujocoOptions {
|
|
81
|
+
/** The default export of `@mujoco/mujoco`, when a bundler imports it. */
|
|
82
|
+
factory?: (options?: Record<string, unknown>) => Promise<unknown>;
|
|
83
|
+
/** Where mujoco.js lives when no factory is given. Default {@link MUJOCO_URL}. */
|
|
84
|
+
url?: string;
|
|
85
|
+
/** The wasm as bytes or a URL. Default mujoco.wasm beside mujoco.js. */
|
|
86
|
+
wasm?: ArrayBuffer | Uint8Array | string;
|
|
87
|
+
/** Called once with {@link MUJOCO_LICENCE} when MuJoCo loads. */
|
|
88
|
+
onLicence?: (line: string) => void;
|
|
89
|
+
}
|
|
90
|
+
/** A model or runtime this library cannot open, with the reason in words a page can show. */
|
|
91
|
+
export declare class HumanoidError extends Error {
|
|
92
|
+
constructor(message: string);
|
|
93
|
+
}
|
|
94
|
+
/** Loads MuJoCo once per factory or URL and resolves to the module. */
|
|
95
|
+
export declare function loadMujoco(o?: MujocoOptions): Promise<Mujoco>;
|
package/dist/runtime.js
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* MuJoCo in the page: Google DeepMind's WebAssembly build (`@mujoco/mujoco`,
|
|
3
|
+
* Apache-2.0), the single threaded one, so the page needs no cross-origin
|
|
4
|
+
* isolation. This library never bundles it: pass the factory a bundler
|
|
5
|
+
* imported, or it loads at run time from a URL (default the pinned copy on
|
|
6
|
+
* jsDelivr, its wasm read beside it), and the page is told whose licence
|
|
7
|
+
* applies. The types here cover the part of its surface this library uses.
|
|
8
|
+
*/
|
|
9
|
+
/** The @mujoco/mujoco version the default URL pins. */
|
|
10
|
+
export const MUJOCO_VERSION = "4.0.1";
|
|
11
|
+
/** Where MuJoCo loads from when no factory is given. */
|
|
12
|
+
export const MUJOCO_URL = `https://cdn.jsdelivr.net/npm/@mujoco/mujoco@${MUJOCO_VERSION}/mujoco.js`;
|
|
13
|
+
/** The line a page shows the first time MuJoCo loads. */
|
|
14
|
+
export const MUJOCO_LICENCE = "mujoco by google deepmind, under the apache license 2.0 (https://github.com/google-deepmind/mujoco). it loads at run time and is not part of this library.";
|
|
15
|
+
/** A model or runtime this library cannot open, with the reason in words a page can show. */
|
|
16
|
+
export class HumanoidError extends Error {
|
|
17
|
+
constructor(message) {
|
|
18
|
+
super(message);
|
|
19
|
+
this.name = "HumanoidError";
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
const loads = new Map();
|
|
23
|
+
/** Loads MuJoCo once per factory or URL and resolves to the module. */
|
|
24
|
+
export function loadMujoco(o = {}) {
|
|
25
|
+
const key = o.factory ?? o.url ?? MUJOCO_URL;
|
|
26
|
+
const have = loads.get(key);
|
|
27
|
+
if (have)
|
|
28
|
+
return have;
|
|
29
|
+
const p = (async () => {
|
|
30
|
+
let factory = o.factory;
|
|
31
|
+
const url = o.url ?? MUJOCO_URL;
|
|
32
|
+
if (!factory) {
|
|
33
|
+
try {
|
|
34
|
+
factory = (await import(/* @vite-ignore */ url)).default;
|
|
35
|
+
}
|
|
36
|
+
catch (e) {
|
|
37
|
+
throw new HumanoidError(`mujoco did not load from ${url}: ${e instanceof Error ? e.message : String(e)}. check the connection, or pass the factory from @mujoco/mujoco.`);
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
const opts = {};
|
|
41
|
+
const w = o.wasm;
|
|
42
|
+
if (w instanceof ArrayBuffer || ArrayBuffer.isView(w))
|
|
43
|
+
opts.wasmBinary = w;
|
|
44
|
+
else if (typeof w === "string")
|
|
45
|
+
opts.locateFile = () => w;
|
|
46
|
+
else if (!o.factory)
|
|
47
|
+
opts.locateFile = (path) => new URL(path, url).href;
|
|
48
|
+
const mj = (await factory(opts));
|
|
49
|
+
o.onLicence?.(MUJOCO_LICENCE);
|
|
50
|
+
return mj;
|
|
51
|
+
})();
|
|
52
|
+
loads.set(key, p);
|
|
53
|
+
p.catch(() => loads.delete(key));
|
|
54
|
+
return p;
|
|
55
|
+
}
|