@hackbuild/humanoid 0.2.1 → 0.3.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/README.md +30 -6
- package/dist/follow-three.d.ts +61 -0
- package/dist/follow-three.js +135 -0
- package/dist/follow.d.ts +24 -0
- package/dist/follow.js +32 -0
- package/dist/frame.d.ts +12 -0
- package/dist/frame.js +10 -0
- package/dist/humanoid.iife.js +262 -7
- package/dist/index.d.ts +2 -0
- package/dist/index.js +2 -0
- package/dist/rest.d.ts +14 -0
- package/dist/rest.js +31 -0
- package/dist/runtime.d.ts +6 -0
- package/dist/sonic.d.ts +3 -1
- package/dist/three.d.ts +4 -1
- package/dist/three.js +2 -1
- package/how.ts +1 -0
- package/llms.md +79 -1
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
Run a physics humanoid in the page: MuJoCo simulates a robot from its MJCF files, NVIDIA's GEAR-SONIC policy balances the Unitree G1, and you pose it, shove it and draw it.
|
|
4
4
|
|
|
5
|
-
[](https://www.npmjs.com/package/@hackbuild/humanoid) [](../../LICENSE) ](https://www.npmjs.com/package/@hackbuild/humanoid) [](../../LICENSE)  
|
|
6
6
|
|
|
7
7
|
## What it does
|
|
8
8
|
|
|
@@ -75,6 +75,25 @@ GEAR-SONIC turns a reference motion into a token (the encoder) and the token plu
|
|
|
75
75
|
|
|
76
76
|
`Humanoid.open` compiles the files in MuJoCo's virtual file system. `targets` is the actuators' control array, a live view: for a position actuator it is the joint's target angle. `moveTo` eases the targets it names in over simulated time and leaves the others alone; a sudden jump can topple a robot that a ramp of half a second leaves standing. After writing `qpos` by hand, call `forward()` before reading poses; after `dispose()` the data's arrays must not be read (`disposed` says so). `step(seconds)` runs whole timesteps and carries the remainder, at most 0.1 s a call, so a slow frame does not run away. Poses come back in MuJoCo's frame: z up, meters, quaternions w x y z.
|
|
77
77
|
|
|
78
|
+
### a body that follows
|
|
79
|
+
|
|
80
|
+
`BodyFollow` in `/three` drives a humanoid body (a VRM or a GLB person with VRM 1.0 bone names, as `@hackbuild/avatar` loads one) from the robot. Once, it turns the body's limbs to the robot's zero pose, worked out from the model's body offsets without touching the running robot: a T pose's arms come down to the sides and the forearms point forward, as the G1's do at zero. Then each frame it gives every mapped bone the turn its robot body has made since that pose, and moves the hips with the pelvis, scaled to the body's hip height (`travel: false` keeps them in place). `G1_FOLLOW` maps the G1's bodies to the bones; another robot passes its own `map`. Bones the body lacks, bodies the robot lacks and segments that could not be posed are listed in `missing` and left as they are; `reset()` puts the body back as it was.
|
|
81
|
+
|
|
82
|
+
```ts
|
|
83
|
+
import { load } from "@hackbuild/avatar";
|
|
84
|
+
import { loadAvatar } from "@hackbuild/avatar/three";
|
|
85
|
+
import { BodyFollow } from "@hackbuild/humanoid/three";
|
|
86
|
+
|
|
87
|
+
await load(); // the bone mapper's wasm
|
|
88
|
+
const person = await loadAvatar("person.glb");
|
|
89
|
+
person.root.position.x = 0.9; // beside the robot
|
|
90
|
+
scene.add(person.root);
|
|
91
|
+
const follow = new BodyFollow(robot, person);
|
|
92
|
+
// each frame, after the robot steps
|
|
93
|
+
follow.update();
|
|
94
|
+
person.update(dt);
|
|
95
|
+
```
|
|
96
|
+
|
|
78
97
|
## API overview
|
|
79
98
|
|
|
80
99
|
| export | what it does |
|
|
@@ -86,6 +105,9 @@ GEAR-SONIC turns a reference motion into a token (the encoder) and the token plu
|
|
|
86
105
|
| [`Humanoid`](../../docs/api/humanoid/index.md#humanoid) | step, reset, targets, moveTo, hold, push, poses, dispose |
|
|
87
106
|
| [`G1`](../../docs/api/humanoid/index.md#g1) | the Unitree G1 from MuJoCo Menagerie: url, joints, keyframe, licence |
|
|
88
107
|
| [`HumanoidView`](../../docs/api/humanoid/three.md#humanoidview) | the robot in three.js, from `@hackbuild/humanoid/three` |
|
|
108
|
+
| [`BodyFollow`](../../docs/api/humanoid/three.md#bodyfollow) | a humanoid body that follows the robot, from `@hackbuild/humanoid/three` |
|
|
109
|
+
| [`G1_FOLLOW`](../../docs/api/humanoid/index.md#g1_follow) | the G1's bodies mapped to humanoid bones, and the segments posed to its zero pose |
|
|
110
|
+
| [`restPoses`](../../docs/api/humanoid/index.md#restposes) | every body's world pose at qpos0, from the model alone |
|
|
89
111
|
| [`Sonic`](../../docs/api/humanoid/sonic.md#sonic) | load the GEAR-SONIC policy, from `@hackbuild/humanoid/sonic` |
|
|
90
112
|
| [`SonicDriver`](../../docs/api/humanoid/sonic.md#sonicdriver) | run it on a G1: advance, reference, reset |
|
|
91
113
|
| [`G1_SONIC`](../../docs/api/humanoid/sonic.md#g1_sonic) | the G1 deploy model the policy runs on: url, asset host, licence |
|
|
@@ -102,7 +124,7 @@ The full reference is in [docs/api/humanoid](../../docs/api/humanoid/README.md).
|
|
|
102
124
|
| stepping | nothing more | 0.084 ms per 2 ms step for the G1, about 24 times real time (measured) |
|
|
103
125
|
| the policy | onnxruntime-web on wasm, one thread | 5.7 ms a 20 ms tick in headless Chrome on an Apple M1 (measured) |
|
|
104
126
|
| drawing | three.js 0.186 or later, WebGL | the G1 draws 35 meshes |
|
|
105
|
-
| single file build | `dist/humanoid.iife.js` | `HackbuildHumanoid` global with `THREE` from the page; MuJoCo still loads from its URL |
|
|
127
|
+
| single file build | `dist/humanoid.iife.js` | `HackbuildHumanoid` global with [`THREE`](../../docs/concepts.md#builds-that-run-from-file) from the page; MuJoCo still loads from its URL |
|
|
106
128
|
|
|
107
129
|
## Errors and troubleshooting
|
|
108
130
|
|
|
@@ -130,19 +152,21 @@ Changes are in the repository [CHANGELOG](../../CHANGELOG.md). Code is MIT ([LIC
|
|
|
130
152
|
|
|
131
153
|
| check | result |
|
|
132
154
|
|---|---|
|
|
133
|
-
| `npx vitest run packages/humanoid` |
|
|
155
|
+
| `npx vitest run packages/humanoid` | 25 of 25 tests pass: the 7 below; rest poses equal MuJoCo's at qpos0 through turned body frames; a T pose body posed to the robot's zero pose, then every follower segment pointing as the robot's does after hip, knee, shoulder, elbow, wrist, ankle and waist turns and a moved, turned pelvis, the hips, spine, a foot and a hand turned by their robot bodies' turns, the hips travelling at the body's scale, travel off, reset, missing bones and bodies listed, nothing after dispose; unnamed parts named by kind and index, a tendon actuator kept out of the joints, an ease leaving other targets and a controller's output alone, a NaN frame, quoted and commented references and cube faces, a mesh folder reached with `..`, a view after dispose; and for the policy the gains and action scale from motor armature, the policy joint order, gravity in the body frame, the anchor ignoring heading, the encoder and decoder layouts, ten ticks oldest first, a driver on a 29 joint model with a stand in runtime, the refusal of a robot without the G1's joints or with them on servos, and a reset or dispose landing while a tick awaits inference (counted) |
|
|
134
156
|
| the 7 runtime and view tests | 7 of 7 tests pass on MuJoCo 4.0.1 in node: references and compiler directories, a dropped folder with an include and a missing mesh, compiling through the virtual files, names, uneven frames stepped at the timestep, a fall under gravity, a keyframe's pose and controls, an actuator reaching its target, an eased move, hold, a push, the compile error naming missing files, the three.js view's meshes and their motion (counted) |
|
|
135
157
|
| the G1 from MuJoCo Menagerie in headless Chrome on an Apple M1 | MuJoCo loads in 206 ms, the G1's 36 files (19.7 MB) arrive in 886 ms with none missing, it compiles with 29 actuated joints and draws 35 meshes; on its stand keyframe the pelvis holds 0.792 m for 5 s; 0.084 ms per step (measured) |
|
|
136
158
|
| the demo page's own check, headless, MuJoCo from jsDelivr | loads in 362 ms, the G1's files in 528 ms; it stands at 0.792 m and eases into a crouch at 0.671 m at 59.9 frames a second (measured) |
|
|
137
159
|
| GEAR-SONIC on the deploy G1 in node, MuJoCo 4.0.1 and onnxruntime-web 1.23.2 | of seven guesses at the open layout details only one stands; with the tilt anchor it stands 30 s at 0.766 m within 1.1 degrees, steps 0.5 m to catch a 1.0 m/s shove and catches 1.5 m/s, where the position actuators alone fall at 0.3 m/s; it holds a crouch at 0.680 m and a raised arm; 2.6 to 4.5 ms a tick, 15 s simulated in 4.5 s (measured) |
|
|
138
160
|
| the demo page's policy check, headless Chrome | MuJoCo in 332 ms, the deploy G1 in 527 ms, the policy in 3,368 ms; it stands at 0.766 m and is at 0.766 m 4 s after a 1 m/s shove, at 59.9 frames a second, 5.7 ms a tick (measured) |
|
|
161
|
+
| `BodyFollow` on both real G1 models in node | after a crouch every follower segment points within 0.00 degrees of the robot's; the G1's thigh and upper arm segments start at the roll joint while their bones copy the yaw link a few centimetres away, so a hip yaw of 0.6 rad leaves the thigh 2.75 degrees off and a shoulder yaw of 1.0 rad the upper arm 1.85 degrees off (measured, research probe and review) |
|
|
162
|
+
| `BodyFollow` with the dog simulator's person on the docs page, headless Chrome | the person stands beside the G1 and crouches, raises an arm, steps after a shove under the policy and falls with it on its actuators (frame strips looked at; the hips drop 0.918 to 0.774 m in the crouch, measured) |
|
|
139
163
|
| crouches and shoves on the G1 in node | eased over 0.5 s or more, crouches from hips -0.2 to -0.6 radians all hold (pelvis 0.778 to 0.671 m); the same targets set at once topple it at two depths of five; a 0.3 m/s shove topples it (measured) |
|
|
140
164
|
|
|
141
165
|
### Not verified by anything
|
|
142
166
|
|
|
143
167
|
- The policy tracks static poses only: no reference motion, walking command or the 774 MB planner runs, and the low latency and v1.1 variants are untried.
|
|
144
168
|
- On its actuators alone (no policy) the robot holds targets but does not balance; a shove topples it.
|
|
145
|
-
-
|
|
169
|
+
- A follower copies rotations, so a body with other proportions keeps its own limb lengths: its feet may slide or sink a little against the robot's; no foot contact is enforced. Only the dog simulator's GLB person has been looked at, no VRM.
|
|
146
170
|
- Robots other than the G1 and the test model are untried, and so are textures in MJCF.
|
|
147
171
|
- Nobody has watched it at real frame rate in a visible browser; the checks are headless.
|
|
148
172
|
|
|
@@ -150,8 +174,8 @@ Changes are in the repository [CHANGELOG](../../CHANGELOG.md). Code is MIT ([LIC
|
|
|
150
174
|
|
|
151
175
|
| file | gzip (measured) | ceiling |
|
|
152
176
|
|---|---|---|
|
|
153
|
-
| `dist/index.js` (main entry, imports external) |
|
|
154
|
-
| `dist/humanoid.iife.js` (single file build) |
|
|
177
|
+
| `dist/index.js` (main entry, imports external) | 7,502 bytes | 7,877 bytes |
|
|
178
|
+
| `dist/humanoid.iife.js` (single file build) | 15,563 bytes | 16,341 bytes |
|
|
155
179
|
|
|
156
180
|
Measured 2026-11-30 by `node tools/lint-packages.mjs` (gzip level 9) from the packed
|
|
157
181
|
tarball. Generated from `tools/size-budget.json`; CI fails when a file moves more than
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A humanoid body (a VRM, a GLB person, any rig with VRM 1.0 bone names)
|
|
3
|
+
* that follows a robot in MuJoCo. Once, it turns the body's limbs to the
|
|
4
|
+
* robot's zero pose (a T pose's arms come down to the sides, the forearms
|
|
5
|
+
* forward); then each frame it gives every mapped bone the rotation its
|
|
6
|
+
* robot body has made since that pose, and moves the hips with the pelvis,
|
|
7
|
+
* scaled to the body's height. Rotations are worked in the body's root
|
|
8
|
+
* space, so the body may stand anywhere, turned any way.
|
|
9
|
+
*/
|
|
10
|
+
import { type Object3D } from "three";
|
|
11
|
+
import { type FollowMap } from "./follow.js";
|
|
12
|
+
import { type Forward } from "./frame.js";
|
|
13
|
+
import type { Humanoid } from "./sim.js";
|
|
14
|
+
/** A body to drive: its root and its bones by VRM 1.0 name, as `@hackbuild/avatar` loads one. */
|
|
15
|
+
export interface FollowBody {
|
|
16
|
+
root: Object3D;
|
|
17
|
+
bones: Partial<Record<string, Object3D>>;
|
|
18
|
+
}
|
|
19
|
+
/** Options for {@link BodyFollow}. */
|
|
20
|
+
export interface BodyFollowOptions {
|
|
21
|
+
/** Robot bodies to humanoid bones. Default {@link G1_FOLLOW}. */
|
|
22
|
+
map?: FollowMap;
|
|
23
|
+
/** The axis the robot faces in its model, as {@link HumanoidView} takes it. Default "x". */
|
|
24
|
+
forward?: Forward;
|
|
25
|
+
/** Move the hips with the robot's root body, scaled to the body's hip height. Default true; false turns the body in place. */
|
|
26
|
+
travel?: boolean;
|
|
27
|
+
}
|
|
28
|
+
/** Drives a humanoid body from a {@link Humanoid}: call `update` after each step. */
|
|
29
|
+
export declare class BodyFollow {
|
|
30
|
+
private sim;
|
|
31
|
+
private body;
|
|
32
|
+
/** Bones of the map the body lacks, robot bodies the robot lacks, and segments that could not be posed for want of either; those are left as they are. */
|
|
33
|
+
readonly missing: string[];
|
|
34
|
+
private entries;
|
|
35
|
+
private saved;
|
|
36
|
+
private turn;
|
|
37
|
+
private turnInv;
|
|
38
|
+
private rootEntry;
|
|
39
|
+
private hipsRest;
|
|
40
|
+
private bodyRest;
|
|
41
|
+
private scale;
|
|
42
|
+
private travel;
|
|
43
|
+
private _q;
|
|
44
|
+
private _r;
|
|
45
|
+
private _p;
|
|
46
|
+
private _root;
|
|
47
|
+
private _v;
|
|
48
|
+
private _w;
|
|
49
|
+
constructor(sim: Humanoid, body: FollowBody, o?: BodyFollowOptions);
|
|
50
|
+
/** Poses the body from the robot's last step; does nothing once the robot is disposed. */
|
|
51
|
+
update(): void;
|
|
52
|
+
/** Puts every bone this follower touched back as it found it. */
|
|
53
|
+
reset(): void;
|
|
54
|
+
private keep;
|
|
55
|
+
/** A node's world rotation in the body's root space. */
|
|
56
|
+
private rootQuat;
|
|
57
|
+
private rootPos;
|
|
58
|
+
/** Gives a node a rotation in root space, through its parent's. */
|
|
59
|
+
private setRootQuat;
|
|
60
|
+
private setRootPos;
|
|
61
|
+
}
|
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A humanoid body (a VRM, a GLB person, any rig with VRM 1.0 bone names)
|
|
3
|
+
* that follows a robot in MuJoCo. Once, it turns the body's limbs to the
|
|
4
|
+
* robot's zero pose (a T pose's arms come down to the sides, the forearms
|
|
5
|
+
* forward); then each frame it gives every mapped bone the rotation its
|
|
6
|
+
* robot body has made since that pose, and moves the hips with the pelvis,
|
|
7
|
+
* scaled to the body's height. Rotations are worked in the body's root
|
|
8
|
+
* space, so the body may stand anywhere, turned any way.
|
|
9
|
+
*/
|
|
10
|
+
import { Quaternion, Vector3 } from "three";
|
|
11
|
+
import { G1_FOLLOW } from "./follow.js";
|
|
12
|
+
import { frameTurn } from "./frame.js";
|
|
13
|
+
import { restPoses } from "./rest.js";
|
|
14
|
+
/** Drives a humanoid body from a {@link Humanoid}: call `update` after each step. */
|
|
15
|
+
export class BodyFollow {
|
|
16
|
+
sim;
|
|
17
|
+
body;
|
|
18
|
+
/** Bones of the map the body lacks, robot bodies the robot lacks, and segments that could not be posed for want of either; those are left as they are. */
|
|
19
|
+
missing = [];
|
|
20
|
+
entries = [];
|
|
21
|
+
saved = new Map();
|
|
22
|
+
turn;
|
|
23
|
+
turnInv;
|
|
24
|
+
rootEntry = null;
|
|
25
|
+
hipsRest = new Vector3();
|
|
26
|
+
bodyRest = new Vector3();
|
|
27
|
+
scale = 1;
|
|
28
|
+
travel;
|
|
29
|
+
// this follower's own scratch
|
|
30
|
+
_q = new Quaternion();
|
|
31
|
+
_r = new Quaternion();
|
|
32
|
+
_p = new Quaternion();
|
|
33
|
+
_root = new Quaternion();
|
|
34
|
+
_v = new Vector3();
|
|
35
|
+
_w = new Vector3();
|
|
36
|
+
constructor(sim, body, o = {}) {
|
|
37
|
+
this.sim = sim;
|
|
38
|
+
this.body = body;
|
|
39
|
+
const map = o.map ?? G1_FOLLOW;
|
|
40
|
+
this.travel = o.travel ?? true;
|
|
41
|
+
this.turn = frameTurn(o.forward);
|
|
42
|
+
this.turnInv = this.turn.clone().invert();
|
|
43
|
+
const rest = restPoses(sim.model);
|
|
44
|
+
const id = (name) => sim.bodies.indexOf(name);
|
|
45
|
+
for (const [bone, link] of Object.entries(map.bones)) {
|
|
46
|
+
const node = body.bones[bone], i = id(link);
|
|
47
|
+
if (!node)
|
|
48
|
+
this.missing.push(bone);
|
|
49
|
+
if (i < 0)
|
|
50
|
+
this.missing.push(link);
|
|
51
|
+
if (!node || i < 0)
|
|
52
|
+
continue;
|
|
53
|
+
const [w, x, y, z] = rest[i].quat;
|
|
54
|
+
this.entries.push({ bone, node, body: i, restInv: new Quaternion(x, y, z, w).invert(), posed: new Quaternion() });
|
|
55
|
+
}
|
|
56
|
+
for (const s of map.segments)
|
|
57
|
+
for (const b of [s.bone, s.child])
|
|
58
|
+
if (body.bones[b])
|
|
59
|
+
this.keep(body.bones[b]);
|
|
60
|
+
for (const e of this.entries)
|
|
61
|
+
this.keep(e.node);
|
|
62
|
+
// pose the limbs to the robot's zero pose, parents first
|
|
63
|
+
body.root.updateMatrixWorld(true);
|
|
64
|
+
for (const s of map.segments) {
|
|
65
|
+
const node = body.bones[s.bone], child = body.bones[s.child], a = id(s.from), b = id(s.to);
|
|
66
|
+
if (!node || !child || a < 0 || b < 0) {
|
|
67
|
+
if (node)
|
|
68
|
+
this.missing.push(`${s.bone} to ${s.child} segment`); // the bone keeps its own rest pose
|
|
69
|
+
continue;
|
|
70
|
+
}
|
|
71
|
+
const pa = rest[a].pos, pb = rest[b].pos;
|
|
72
|
+
const want = this._w.set(pb[0] - pa[0], pb[1] - pa[1], pb[2] - pa[2]).applyQuaternion(this.turn).normalize();
|
|
73
|
+
const have = this.rootPos(child, this._v).sub(this.rootPos(node, new Vector3())).normalize();
|
|
74
|
+
if (have.lengthSq() === 0 || want.lengthSq() === 0)
|
|
75
|
+
continue;
|
|
76
|
+
this.setRootQuat(node, this._q.setFromUnitVectors(have, want).multiply(this.rootQuat(node, this._r)));
|
|
77
|
+
}
|
|
78
|
+
for (const e of this.entries)
|
|
79
|
+
this.rootQuat(e.node, e.posed);
|
|
80
|
+
this.rootEntry = this.entries.find((e) => e.bone === map.root) ?? null;
|
|
81
|
+
if (this.rootEntry) {
|
|
82
|
+
this.rootPos(this.rootEntry.node, this.hipsRest);
|
|
83
|
+
const p = rest[this.rootEntry.body].pos;
|
|
84
|
+
this.bodyRest.set(p[0], p[1], p[2]).applyQuaternion(this.turn);
|
|
85
|
+
this.scale = this.bodyRest.y > 0 ? this.hipsRest.y / this.bodyRest.y : 1;
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
/** Poses the body from the robot's last step; does nothing once the robot is disposed. */
|
|
89
|
+
update() {
|
|
90
|
+
if (this.sim.disposed)
|
|
91
|
+
return;
|
|
92
|
+
this.body.root.updateMatrixWorld(true);
|
|
93
|
+
const q = this.sim.data.xquat;
|
|
94
|
+
for (const e of this.entries) {
|
|
95
|
+
const k = e.body * 4;
|
|
96
|
+
// the robot body's turn since qpos0, carried into root space: T q q0^-1 T^-1
|
|
97
|
+
this._q.set(q[k + 1], q[k + 2], q[k + 3], q[k]);
|
|
98
|
+
this._q.premultiply(this.turn).multiply(e.restInv).multiply(this.turnInv);
|
|
99
|
+
this.setRootQuat(e.node, this._q.multiply(e.posed));
|
|
100
|
+
if (e === this.rootEntry && this.travel) {
|
|
101
|
+
const x = this.sim.data.xpos, b = e.body * 3;
|
|
102
|
+
this._v.set(x[b], x[b + 1], x[b + 2]).applyQuaternion(this.turn).sub(this.bodyRest).multiplyScalar(this.scale).add(this.hipsRest);
|
|
103
|
+
this.setRootPos(e.node, this._v);
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
/** Puts every bone this follower touched back as it found it. */
|
|
108
|
+
reset() {
|
|
109
|
+
for (const [node, s] of this.saved)
|
|
110
|
+
node.quaternion.copy(s.q), node.position.copy(s.p);
|
|
111
|
+
this.body.root.updateMatrixWorld(true);
|
|
112
|
+
}
|
|
113
|
+
keep(node) {
|
|
114
|
+
if (!this.saved.has(node))
|
|
115
|
+
this.saved.set(node, { q: node.quaternion.clone(), p: node.position.clone() });
|
|
116
|
+
}
|
|
117
|
+
/** A node's world rotation in the body's root space. */
|
|
118
|
+
rootQuat(node, out) {
|
|
119
|
+
return node.getWorldQuaternion(out).premultiply(this.body.root.getWorldQuaternion(this._root).invert());
|
|
120
|
+
}
|
|
121
|
+
rootPos(node, out) {
|
|
122
|
+
return this.body.root.worldToLocal(node.getWorldPosition(out));
|
|
123
|
+
}
|
|
124
|
+
/** Gives a node a rotation in root space, through its parent's. */
|
|
125
|
+
setRootQuat(node, q) {
|
|
126
|
+
const parent = node.parent ? this.rootQuat(node.parent, this._p) : this._p.identity();
|
|
127
|
+
node.quaternion.copy(parent.invert().multiply(q));
|
|
128
|
+
node.updateMatrixWorld(true);
|
|
129
|
+
}
|
|
130
|
+
setRootPos(node, p) {
|
|
131
|
+
const world = this.body.root.localToWorld(this._w.copy(p));
|
|
132
|
+
node.position.copy(node.parent ? node.parent.worldToLocal(world) : world);
|
|
133
|
+
node.updateMatrixWorld(true);
|
|
134
|
+
}
|
|
135
|
+
}
|
package/dist/follow.d.ts
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/** Robot bodies to humanoid bones (VRM 1.0 names), and the segments posed to the robot's zero pose first. */
|
|
2
|
+
export interface FollowMap {
|
|
3
|
+
/** Humanoid bone to the robot body whose rotation it copies, parents before children. */
|
|
4
|
+
bones: Record<string, string>;
|
|
5
|
+
/** The bone that travels with the robot; it must be in `bones`. */
|
|
6
|
+
root: string;
|
|
7
|
+
/**
|
|
8
|
+
* Bones turned once, before following, so the segment from `bone` to `child` points as the
|
|
9
|
+
* robot's segment from body `from` to body `to` does at qpos0: a T pose's arms come down
|
|
10
|
+
* to a robot's arms at its sides. Parents before children.
|
|
11
|
+
*/
|
|
12
|
+
segments: {
|
|
13
|
+
bone: string;
|
|
14
|
+
child: string;
|
|
15
|
+
from: string;
|
|
16
|
+
to: string;
|
|
17
|
+
}[];
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* The Unitree G1, both models (MuJoCo Menagerie's and the gear-sonic deploy model share
|
|
21
|
+
* these body names). At its zero pose the legs hang straight, the arms hang at the sides and
|
|
22
|
+
* the forearms point forward, elbows at a right angle (measured).
|
|
23
|
+
*/
|
|
24
|
+
export declare const G1_FOLLOW: FollowMap;
|
package/dist/follow.js
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
// Which robot body turns which humanoid bone, for a body that follows the
|
|
2
|
+
// robot (BodyFollow in ./three). Plain data, so another robot brings its own.
|
|
3
|
+
const side = (s) => {
|
|
4
|
+
const S = s === "left" ? "left" : "right";
|
|
5
|
+
return {
|
|
6
|
+
bones: {
|
|
7
|
+
[`${s}UpperLeg`]: `${S}_hip_yaw_link`,
|
|
8
|
+
[`${s}LowerLeg`]: `${S}_knee_link`,
|
|
9
|
+
[`${s}Foot`]: `${S}_ankle_roll_link`,
|
|
10
|
+
[`${s}UpperArm`]: `${S}_shoulder_yaw_link`,
|
|
11
|
+
[`${s}LowerArm`]: `${S}_elbow_link`,
|
|
12
|
+
[`${s}Hand`]: `${S}_wrist_yaw_link`,
|
|
13
|
+
},
|
|
14
|
+
segments: [
|
|
15
|
+
{ bone: `${s}UpperLeg`, child: `${s}LowerLeg`, from: `${S}_hip_roll_link`, to: `${S}_knee_link` },
|
|
16
|
+
{ bone: `${s}LowerLeg`, child: `${s}Foot`, from: `${S}_knee_link`, to: `${S}_ankle_pitch_link` },
|
|
17
|
+
{ bone: `${s}UpperArm`, child: `${s}LowerArm`, from: `${S}_shoulder_roll_link`, to: `${S}_elbow_link` },
|
|
18
|
+
{ bone: `${s}LowerArm`, child: `${s}Hand`, from: `${S}_elbow_link`, to: `${S}_wrist_roll_link` },
|
|
19
|
+
],
|
|
20
|
+
};
|
|
21
|
+
};
|
|
22
|
+
const L = side("left"), R = side("right");
|
|
23
|
+
/**
|
|
24
|
+
* The Unitree G1, both models (MuJoCo Menagerie's and the gear-sonic deploy model share
|
|
25
|
+
* these body names). At its zero pose the legs hang straight, the arms hang at the sides and
|
|
26
|
+
* the forearms point forward, elbows at a right angle (measured).
|
|
27
|
+
*/
|
|
28
|
+
export const G1_FOLLOW = {
|
|
29
|
+
bones: { hips: "pelvis", spine: "torso_link", ...L.bones, ...R.bones },
|
|
30
|
+
root: "hips",
|
|
31
|
+
segments: [...L.segments, ...R.segments],
|
|
32
|
+
};
|
package/dist/frame.d.ts
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { Quaternion } from "three";
|
|
2
|
+
/** The axis a robot faces in its model. */
|
|
3
|
+
export type Forward = "x" | "-x" | "y" | "-y";
|
|
4
|
+
/** Turn about +Y that brings each forward axis to +Z, after z up becomes y up. */
|
|
5
|
+
export declare const TURN: {
|
|
6
|
+
readonly x: number;
|
|
7
|
+
readonly "-x": number;
|
|
8
|
+
readonly y: number;
|
|
9
|
+
readonly "-y": 0;
|
|
10
|
+
};
|
|
11
|
+
/** MuJoCo world to humanoid space as one rotation: z up to y up, then the forward axis to +Z. */
|
|
12
|
+
export declare function frameTurn(forward?: Forward): Quaternion;
|
package/dist/frame.js
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
// The turn from MuJoCo's frame (z up, the robot facing its forward axis) to
|
|
2
|
+
// the family's humanoid space (y up, facing +Z), shared by the view and the
|
|
3
|
+
// body follower so both put the robot the same way round.
|
|
4
|
+
import { Euler, Quaternion } from "three";
|
|
5
|
+
/** Turn about +Y that brings each forward axis to +Z, after z up becomes y up. */
|
|
6
|
+
export const TURN = { x: -Math.PI / 2, "-x": Math.PI / 2, y: Math.PI, "-y": 0 };
|
|
7
|
+
/** MuJoCo world to humanoid space as one rotation: z up to y up, then the forward axis to +Z. */
|
|
8
|
+
export function frameTurn(forward = "x") {
|
|
9
|
+
return new Quaternion().setFromEuler(new Euler(0, TURN[forward], 0)).multiply(new Quaternion().setFromEuler(new Euler(-Math.PI / 2, 0, 0)));
|
|
10
|
+
}
|
package/dist/humanoid.iife.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
var HackbuildHumanoid = (function(exports, three) {
|
|
2
|
+
if (!globalThis.THREE) throw new Error("@hackbuild/humanoid: no THREE on the page. import three in a module script, set globalThis.THREE, then load this file with defer.");
|
|
2
3
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
4
|
// src/runtime.ts
|
|
4
5
|
/**
|
|
@@ -453,6 +454,263 @@ var HackbuildHumanoid = (function(exports, three) {
|
|
|
453
454
|
"right_wrist_yaw_joint"
|
|
454
455
|
]
|
|
455
456
|
};
|
|
457
|
+
// src/follow.ts
|
|
458
|
+
const side = (s) => {
|
|
459
|
+
const S = s === "left" ? "left" : "right";
|
|
460
|
+
return {
|
|
461
|
+
bones: {
|
|
462
|
+
[`${s}UpperLeg`]: `${S}_hip_yaw_link`,
|
|
463
|
+
[`${s}LowerLeg`]: `${S}_knee_link`,
|
|
464
|
+
[`${s}Foot`]: `${S}_ankle_roll_link`,
|
|
465
|
+
[`${s}UpperArm`]: `${S}_shoulder_yaw_link`,
|
|
466
|
+
[`${s}LowerArm`]: `${S}_elbow_link`,
|
|
467
|
+
[`${s}Hand`]: `${S}_wrist_yaw_link`
|
|
468
|
+
},
|
|
469
|
+
segments: [
|
|
470
|
+
{
|
|
471
|
+
bone: `${s}UpperLeg`,
|
|
472
|
+
child: `${s}LowerLeg`,
|
|
473
|
+
from: `${S}_hip_roll_link`,
|
|
474
|
+
to: `${S}_knee_link`
|
|
475
|
+
},
|
|
476
|
+
{
|
|
477
|
+
bone: `${s}LowerLeg`,
|
|
478
|
+
child: `${s}Foot`,
|
|
479
|
+
from: `${S}_knee_link`,
|
|
480
|
+
to: `${S}_ankle_pitch_link`
|
|
481
|
+
},
|
|
482
|
+
{
|
|
483
|
+
bone: `${s}UpperArm`,
|
|
484
|
+
child: `${s}LowerArm`,
|
|
485
|
+
from: `${S}_shoulder_roll_link`,
|
|
486
|
+
to: `${S}_elbow_link`
|
|
487
|
+
},
|
|
488
|
+
{
|
|
489
|
+
bone: `${s}LowerArm`,
|
|
490
|
+
child: `${s}Hand`,
|
|
491
|
+
from: `${S}_elbow_link`,
|
|
492
|
+
to: `${S}_wrist_roll_link`
|
|
493
|
+
}
|
|
494
|
+
]
|
|
495
|
+
};
|
|
496
|
+
};
|
|
497
|
+
const L = side("left");
|
|
498
|
+
const R = side("right");
|
|
499
|
+
/**
|
|
500
|
+
* The Unitree G1, both models (MuJoCo Menagerie's and the gear-sonic deploy model share
|
|
501
|
+
* these body names). At its zero pose the legs hang straight, the arms hang at the sides and
|
|
502
|
+
* the forearms point forward, elbows at a right angle (measured).
|
|
503
|
+
*/
|
|
504
|
+
const G1_FOLLOW = {
|
|
505
|
+
bones: {
|
|
506
|
+
hips: "pelvis",
|
|
507
|
+
spine: "torso_link",
|
|
508
|
+
...L.bones,
|
|
509
|
+
...R.bones
|
|
510
|
+
},
|
|
511
|
+
root: "hips",
|
|
512
|
+
segments: [...L.segments, ...R.segments]
|
|
513
|
+
};
|
|
514
|
+
// src/rest.ts
|
|
515
|
+
/** Quaternion product a b, both [w, x, y, z]. */
|
|
516
|
+
function qmul(a, b) {
|
|
517
|
+
return [
|
|
518
|
+
a[0] * b[0] - a[1] * b[1] - a[2] * b[2] - a[3] * b[3],
|
|
519
|
+
a[0] * b[1] + a[1] * b[0] + a[2] * b[3] - a[3] * b[2],
|
|
520
|
+
a[0] * b[2] - a[1] * b[3] + a[2] * b[0] + a[3] * b[1],
|
|
521
|
+
a[0] * b[3] + a[1] * b[2] - a[2] * b[1] + a[3] * b[0]
|
|
522
|
+
];
|
|
523
|
+
}
|
|
524
|
+
/** v turned by the unit quaternion q ([w, x, y, z]). */
|
|
525
|
+
function qrotate(q, v) {
|
|
526
|
+
const [w, x, y, z] = q;
|
|
527
|
+
const tx = 2 * (y * v[2] - z * v[1]), ty = 2 * (z * v[0] - x * v[2]), tz = 2 * (x * v[1] - y * v[0]);
|
|
528
|
+
return [
|
|
529
|
+
v[0] + w * tx + (y * tz - z * ty),
|
|
530
|
+
v[1] + w * ty + (z * tx - x * tz),
|
|
531
|
+
v[2] + w * tz + (x * ty - y * tx)
|
|
532
|
+
];
|
|
533
|
+
}
|
|
534
|
+
/**
|
|
535
|
+
* Every body's world pose at qpos0 (hinge and slide joints at their reference, a free root
|
|
536
|
+
* where the model places it), from the chain of body offsets. Body 0 is the world.
|
|
537
|
+
*/
|
|
538
|
+
function restPoses(model) {
|
|
539
|
+
const out = [{
|
|
540
|
+
pos: [
|
|
541
|
+
0,
|
|
542
|
+
0,
|
|
543
|
+
0
|
|
544
|
+
],
|
|
545
|
+
quat: [
|
|
546
|
+
1,
|
|
547
|
+
0,
|
|
548
|
+
0,
|
|
549
|
+
0
|
|
550
|
+
]
|
|
551
|
+
}];
|
|
552
|
+
for (let i = 1; i < model.nbody; i++) {
|
|
553
|
+
const parent = out[model.body_parentid[i]];
|
|
554
|
+
const p = model.body_pos, q = model.body_quat;
|
|
555
|
+
const local = [
|
|
556
|
+
q[i * 4],
|
|
557
|
+
q[i * 4 + 1],
|
|
558
|
+
q[i * 4 + 2],
|
|
559
|
+
q[i * 4 + 3]
|
|
560
|
+
];
|
|
561
|
+
const off = qrotate(parent.quat, [
|
|
562
|
+
p[i * 3],
|
|
563
|
+
p[i * 3 + 1],
|
|
564
|
+
p[i * 3 + 2]
|
|
565
|
+
]);
|
|
566
|
+
out.push({
|
|
567
|
+
pos: [
|
|
568
|
+
parent.pos[0] + off[0],
|
|
569
|
+
parent.pos[1] + off[1],
|
|
570
|
+
parent.pos[2] + off[2]
|
|
571
|
+
],
|
|
572
|
+
quat: qmul(parent.quat, local)
|
|
573
|
+
});
|
|
574
|
+
}
|
|
575
|
+
return out;
|
|
576
|
+
}
|
|
577
|
+
// src/frame.ts
|
|
578
|
+
/** Turn about +Y that brings each forward axis to +Z, after z up becomes y up. */
|
|
579
|
+
const TURN = {
|
|
580
|
+
x: -Math.PI / 2,
|
|
581
|
+
"-x": Math.PI / 2,
|
|
582
|
+
y: Math.PI,
|
|
583
|
+
"-y": 0
|
|
584
|
+
};
|
|
585
|
+
/** MuJoCo world to humanoid space as one rotation: z up to y up, then the forward axis to +Z. */
|
|
586
|
+
function frameTurn(forward = "x") {
|
|
587
|
+
return new three.Quaternion().setFromEuler(new three.Euler(0, TURN[forward], 0)).multiply(new three.Quaternion().setFromEuler(new three.Euler(-Math.PI / 2, 0, 0)));
|
|
588
|
+
}
|
|
589
|
+
// src/follow-three.ts
|
|
590
|
+
/**
|
|
591
|
+
* A humanoid body (a VRM, a GLB person, any rig with VRM 1.0 bone names)
|
|
592
|
+
* that follows a robot in MuJoCo. Once, it turns the body's limbs to the
|
|
593
|
+
* robot's zero pose (a T pose's arms come down to the sides, the forearms
|
|
594
|
+
* forward); then each frame it gives every mapped bone the rotation its
|
|
595
|
+
* robot body has made since that pose, and moves the hips with the pelvis,
|
|
596
|
+
* scaled to the body's height. Rotations are worked in the body's root
|
|
597
|
+
* space, so the body may stand anywhere, turned any way.
|
|
598
|
+
*/
|
|
599
|
+
/** Drives a humanoid body from a {@link Humanoid}: call `update` after each step. */
|
|
600
|
+
var BodyFollow = class {
|
|
601
|
+
sim;
|
|
602
|
+
body;
|
|
603
|
+
/** Bones of the map the body lacks, robot bodies the robot lacks, and segments that could not be posed for want of either; those are left as they are. */
|
|
604
|
+
missing = [];
|
|
605
|
+
entries = [];
|
|
606
|
+
saved = /* @__PURE__ */ new Map();
|
|
607
|
+
turn;
|
|
608
|
+
turnInv;
|
|
609
|
+
rootEntry = null;
|
|
610
|
+
hipsRest = new three.Vector3();
|
|
611
|
+
bodyRest = new three.Vector3();
|
|
612
|
+
scale = 1;
|
|
613
|
+
travel;
|
|
614
|
+
_q = new three.Quaternion();
|
|
615
|
+
_r = new three.Quaternion();
|
|
616
|
+
_p = new three.Quaternion();
|
|
617
|
+
_root = new three.Quaternion();
|
|
618
|
+
_v = new three.Vector3();
|
|
619
|
+
_w = new three.Vector3();
|
|
620
|
+
constructor(sim, body, o = {}) {
|
|
621
|
+
this.sim = sim;
|
|
622
|
+
this.body = body;
|
|
623
|
+
const map = o.map ?? G1_FOLLOW;
|
|
624
|
+
this.travel = o.travel ?? true;
|
|
625
|
+
this.turn = frameTurn(o.forward);
|
|
626
|
+
this.turnInv = this.turn.clone().invert();
|
|
627
|
+
const rest = restPoses(sim.model);
|
|
628
|
+
const id = (name) => sim.bodies.indexOf(name);
|
|
629
|
+
for (const [bone, link] of Object.entries(map.bones)) {
|
|
630
|
+
const node = body.bones[bone], i = id(link);
|
|
631
|
+
if (!node) this.missing.push(bone);
|
|
632
|
+
if (i < 0) this.missing.push(link);
|
|
633
|
+
if (!node || i < 0) continue;
|
|
634
|
+
const [w, x, y, z] = rest[i].quat;
|
|
635
|
+
this.entries.push({
|
|
636
|
+
bone,
|
|
637
|
+
node,
|
|
638
|
+
body: i,
|
|
639
|
+
restInv: new three.Quaternion(x, y, z, w).invert(),
|
|
640
|
+
posed: new three.Quaternion()
|
|
641
|
+
});
|
|
642
|
+
}
|
|
643
|
+
for (const s of map.segments) for (const b of [s.bone, s.child]) if (body.bones[b]) this.keep(body.bones[b]);
|
|
644
|
+
for (const e of this.entries) this.keep(e.node);
|
|
645
|
+
body.root.updateMatrixWorld(true);
|
|
646
|
+
for (const s of map.segments) {
|
|
647
|
+
const node = body.bones[s.bone], child = body.bones[s.child], a = id(s.from), b = id(s.to);
|
|
648
|
+
if (!node || !child || a < 0 || b < 0) {
|
|
649
|
+
if (node) this.missing.push(`${s.bone} to ${s.child} segment`);
|
|
650
|
+
continue;
|
|
651
|
+
}
|
|
652
|
+
const pa = rest[a].pos, pb = rest[b].pos;
|
|
653
|
+
const want = this._w.set(pb[0] - pa[0], pb[1] - pa[1], pb[2] - pa[2]).applyQuaternion(this.turn).normalize();
|
|
654
|
+
const have = this.rootPos(child, this._v).sub(this.rootPos(node, new three.Vector3())).normalize();
|
|
655
|
+
if (have.lengthSq() === 0 || want.lengthSq() === 0) continue;
|
|
656
|
+
this.setRootQuat(node, this._q.setFromUnitVectors(have, want).multiply(this.rootQuat(node, this._r)));
|
|
657
|
+
}
|
|
658
|
+
for (const e of this.entries) this.rootQuat(e.node, e.posed);
|
|
659
|
+
this.rootEntry = this.entries.find((e) => e.bone === map.root) ?? null;
|
|
660
|
+
if (this.rootEntry) {
|
|
661
|
+
this.rootPos(this.rootEntry.node, this.hipsRest);
|
|
662
|
+
const p = rest[this.rootEntry.body].pos;
|
|
663
|
+
this.bodyRest.set(p[0], p[1], p[2]).applyQuaternion(this.turn);
|
|
664
|
+
this.scale = this.bodyRest.y > 0 ? this.hipsRest.y / this.bodyRest.y : 1;
|
|
665
|
+
}
|
|
666
|
+
}
|
|
667
|
+
/** Poses the body from the robot's last step; does nothing once the robot is disposed. */
|
|
668
|
+
update() {
|
|
669
|
+
if (this.sim.disposed) return;
|
|
670
|
+
this.body.root.updateMatrixWorld(true);
|
|
671
|
+
const q = this.sim.data.xquat;
|
|
672
|
+
for (const e of this.entries) {
|
|
673
|
+
const k = e.body * 4;
|
|
674
|
+
this._q.set(q[k + 1], q[k + 2], q[k + 3], q[k]);
|
|
675
|
+
this._q.premultiply(this.turn).multiply(e.restInv).multiply(this.turnInv);
|
|
676
|
+
this.setRootQuat(e.node, this._q.multiply(e.posed));
|
|
677
|
+
if (e === this.rootEntry && this.travel) {
|
|
678
|
+
const x = this.sim.data.xpos, b = e.body * 3;
|
|
679
|
+
this._v.set(x[b], x[b + 1], x[b + 2]).applyQuaternion(this.turn).sub(this.bodyRest).multiplyScalar(this.scale).add(this.hipsRest);
|
|
680
|
+
this.setRootPos(e.node, this._v);
|
|
681
|
+
}
|
|
682
|
+
}
|
|
683
|
+
}
|
|
684
|
+
/** Puts every bone this follower touched back as it found it. */
|
|
685
|
+
reset() {
|
|
686
|
+
for (const [node, s] of this.saved) node.quaternion.copy(s.q), node.position.copy(s.p);
|
|
687
|
+
this.body.root.updateMatrixWorld(true);
|
|
688
|
+
}
|
|
689
|
+
keep(node) {
|
|
690
|
+
if (!this.saved.has(node)) this.saved.set(node, {
|
|
691
|
+
q: node.quaternion.clone(),
|
|
692
|
+
p: node.position.clone()
|
|
693
|
+
});
|
|
694
|
+
}
|
|
695
|
+
/** A node's world rotation in the body's root space. */
|
|
696
|
+
rootQuat(node, out) {
|
|
697
|
+
return node.getWorldQuaternion(out).premultiply(this.body.root.getWorldQuaternion(this._root).invert());
|
|
698
|
+
}
|
|
699
|
+
rootPos(node, out) {
|
|
700
|
+
return this.body.root.worldToLocal(node.getWorldPosition(out));
|
|
701
|
+
}
|
|
702
|
+
/** Gives a node a rotation in root space, through its parent's. */
|
|
703
|
+
setRootQuat(node, q) {
|
|
704
|
+
const parent = node.parent ? this.rootQuat(node.parent, this._p) : this._p.identity();
|
|
705
|
+
node.quaternion.copy(parent.invert().multiply(q));
|
|
706
|
+
node.updateMatrixWorld(true);
|
|
707
|
+
}
|
|
708
|
+
setRootPos(node, p) {
|
|
709
|
+
const world = this.body.root.localToWorld(this._w.copy(p));
|
|
710
|
+
node.position.copy(node.parent ? node.parent.worldToLocal(world) : world);
|
|
711
|
+
node.updateMatrixWorld(true);
|
|
712
|
+
}
|
|
713
|
+
};
|
|
456
714
|
// src/three.ts
|
|
457
715
|
/**
|
|
458
716
|
* The robot in three.js: one mesh per visible geom (spheres, capsules,
|
|
@@ -469,12 +727,6 @@ var HackbuildHumanoid = (function(exports, three) {
|
|
|
469
727
|
const CYLINDER = 5;
|
|
470
728
|
const BOX = 6;
|
|
471
729
|
const MESH = 7;
|
|
472
|
-
const TURN = {
|
|
473
|
-
x: -Math.PI / 2,
|
|
474
|
-
"-x": Math.PI / 2,
|
|
475
|
-
y: Math.PI,
|
|
476
|
-
"-y": 0
|
|
477
|
-
};
|
|
478
730
|
/** A three.js group that follows a {@link Humanoid}; add `root` to a scene and call `update` after each step. */
|
|
479
731
|
var HumanoidView = class {
|
|
480
732
|
sim;
|
|
@@ -1031,7 +1283,9 @@ var HackbuildHumanoid = (function(exports, three) {
|
|
|
1031
1283
|
}
|
|
1032
1284
|
}
|
|
1033
1285
|
};
|
|
1286
|
+
exports.BodyFollow = BodyFollow;
|
|
1034
1287
|
exports.G1 = G1;
|
|
1288
|
+
exports.G1_FOLLOW = G1_FOLLOW;
|
|
1035
1289
|
exports.G1_SONIC = G1_SONIC;
|
|
1036
1290
|
exports.Humanoid = Humanoid;
|
|
1037
1291
|
exports.HumanoidError = HumanoidError;
|
|
@@ -1052,6 +1306,7 @@ var HackbuildHumanoid = (function(exports, three) {
|
|
|
1052
1306
|
exports.intoBody = intoBody;
|
|
1053
1307
|
exports.loadMujoco = loadMujoco;
|
|
1054
1308
|
exports.referencedFiles = referencedFiles;
|
|
1309
|
+
exports.restPoses = restPoses;
|
|
1055
1310
|
exports.toPolicyOrder = toPolicyOrder;
|
|
1056
1311
|
return exports;
|
|
1057
|
-
})({}, THREE);
|
|
1312
|
+
})({}, globalThis.THREE);
|
package/dist/index.d.ts
CHANGED
|
@@ -2,3 +2,5 @@ export { MUJOCO_LICENCE, MUJOCO_URL, MUJOCO_VERSION, HumanoidError, loadMujoco,
|
|
|
2
2
|
export { fetchModel, filesFrom, referencedFiles, type ModelFiles } from "./files.js";
|
|
3
3
|
export { Humanoid, type Pose } from "./sim.js";
|
|
4
4
|
export { G1 } from "./g1.js";
|
|
5
|
+
export { G1_FOLLOW, type FollowMap } from "./follow.js";
|
|
6
|
+
export { restPoses } from "./rest.js";
|
package/dist/index.js
CHANGED
|
@@ -2,3 +2,5 @@ export { MUJOCO_LICENCE, MUJOCO_URL, MUJOCO_VERSION, HumanoidError, loadMujoco }
|
|
|
2
2
|
export { fetchModel, filesFrom, referencedFiles } from "./files.js";
|
|
3
3
|
export { Humanoid } from "./sim.js";
|
|
4
4
|
export { G1 } from "./g1.js";
|
|
5
|
+
export { G1_FOLLOW } from "./follow.js";
|
|
6
|
+
export { restPoses } from "./rest.js";
|
package/dist/rest.d.ts
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { MjModel } from "./runtime.js";
|
|
2
|
+
import type { Pose } from "./sim.js";
|
|
3
|
+
type Q = [number, number, number, number];
|
|
4
|
+
type V = [number, number, number];
|
|
5
|
+
/** Quaternion product a b, both [w, x, y, z]. */
|
|
6
|
+
export declare function qmul(a: Readonly<Q>, b: Readonly<Q>): Q;
|
|
7
|
+
/** v turned by the unit quaternion q ([w, x, y, z]). */
|
|
8
|
+
export declare function qrotate(q: Readonly<Q>, v: Readonly<V>): V;
|
|
9
|
+
/**
|
|
10
|
+
* Every body's world pose at qpos0 (hinge and slide joints at their reference, a free root
|
|
11
|
+
* where the model places it), from the chain of body offsets. Body 0 is the world.
|
|
12
|
+
*/
|
|
13
|
+
export declare function restPoses(model: Pick<MjModel, "nbody" | "body_parentid" | "body_pos" | "body_quat">): Pose[];
|
|
14
|
+
export {};
|
package/dist/rest.js
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
/** Quaternion product a b, both [w, x, y, z]. */
|
|
2
|
+
export function qmul(a, b) {
|
|
3
|
+
return [
|
|
4
|
+
a[0] * b[0] - a[1] * b[1] - a[2] * b[2] - a[3] * b[3],
|
|
5
|
+
a[0] * b[1] + a[1] * b[0] + a[2] * b[3] - a[3] * b[2],
|
|
6
|
+
a[0] * b[2] - a[1] * b[3] + a[2] * b[0] + a[3] * b[1],
|
|
7
|
+
a[0] * b[3] + a[1] * b[2] - a[2] * b[1] + a[3] * b[0],
|
|
8
|
+
];
|
|
9
|
+
}
|
|
10
|
+
/** v turned by the unit quaternion q ([w, x, y, z]). */
|
|
11
|
+
export function qrotate(q, v) {
|
|
12
|
+
const [w, x, y, z] = q;
|
|
13
|
+
// t = 2 (q.xyz cross v); v + w t + q.xyz cross t
|
|
14
|
+
const tx = 2 * (y * v[2] - z * v[1]), ty = 2 * (z * v[0] - x * v[2]), tz = 2 * (x * v[1] - y * v[0]);
|
|
15
|
+
return [v[0] + w * tx + (y * tz - z * ty), v[1] + w * ty + (z * tx - x * tz), v[2] + w * tz + (x * ty - y * tx)];
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Every body's world pose at qpos0 (hinge and slide joints at their reference, a free root
|
|
19
|
+
* where the model places it), from the chain of body offsets. Body 0 is the world.
|
|
20
|
+
*/
|
|
21
|
+
export function restPoses(model) {
|
|
22
|
+
const out = [{ pos: [0, 0, 0], quat: [1, 0, 0, 0] }];
|
|
23
|
+
for (let i = 1; i < model.nbody; i++) {
|
|
24
|
+
const parent = out[model.body_parentid[i]]; // parents come before their children
|
|
25
|
+
const p = model.body_pos, q = model.body_quat;
|
|
26
|
+
const local = [q[i * 4], q[i * 4 + 1], q[i * 4 + 2], q[i * 4 + 3]];
|
|
27
|
+
const off = qrotate(parent.quat, [p[i * 3], p[i * 3 + 1], p[i * 3 + 2]]);
|
|
28
|
+
out.push({ pos: [parent.pos[0] + off[0], parent.pos[1] + off[1], parent.pos[2] + off[2]], quat: qmul(parent.quat, local) });
|
|
29
|
+
}
|
|
30
|
+
return out;
|
|
31
|
+
}
|
package/dist/runtime.d.ts
CHANGED
|
@@ -29,6 +29,12 @@ export interface MjModel {
|
|
|
29
29
|
jnt_qposadr: Int32Array;
|
|
30
30
|
jnt_dofadr: Int32Array;
|
|
31
31
|
jnt_bodyid: Int32Array;
|
|
32
|
+
/** Each body's parent body; the world is body 0 and its own parent. */
|
|
33
|
+
body_parentid: Int32Array;
|
|
34
|
+
/** Each body's position in its parent's frame at qpos0, 3 per body. */
|
|
35
|
+
body_pos: Float64Array;
|
|
36
|
+
/** Each body's orientation in its parent's frame at qpos0, [w, x, y, z] per body. */
|
|
37
|
+
body_quat: Float64Array;
|
|
32
38
|
actuator_trnid: Int32Array;
|
|
33
39
|
/** What each actuator moves: 0 a joint. */
|
|
34
40
|
actuator_trntype: Int32Array;
|
package/dist/sonic.d.ts
CHANGED
|
@@ -121,9 +121,11 @@ export interface SonicOptions {
|
|
|
121
121
|
ort?: Ort;
|
|
122
122
|
/** Where to load onnxruntime-web from. Default {@link ORT_URL}; its wasm is read beside it. */
|
|
123
123
|
ortUrl?: string;
|
|
124
|
-
/** The encoder
|
|
124
|
+
/** The encoder as a URL or bytes. Default {@link SONIC}'s pinned URL. */
|
|
125
125
|
encoder?: string | Uint8Array | ArrayBuffer;
|
|
126
|
+
/** The decoder as a URL or bytes. Default {@link SONIC}'s pinned URL. */
|
|
126
127
|
decoder?: string | Uint8Array | ArrayBuffer;
|
|
128
|
+
/** The fetch the policy files load through. Default the page's own. */
|
|
127
129
|
fetch?: typeof fetch;
|
|
128
130
|
/** Called with {@link SONIC}'s licence line when the weights load. */
|
|
129
131
|
onLicence?: (line: string) => void;
|
package/dist/three.d.ts
CHANGED
|
@@ -8,6 +8,7 @@
|
|
|
8
8
|
*/
|
|
9
9
|
import { Group } from "three";
|
|
10
10
|
import type { Humanoid } from "./sim.js";
|
|
11
|
+
import { type Forward } from "./frame.js";
|
|
11
12
|
/** Options for {@link HumanoidView}. */
|
|
12
13
|
export interface HumanoidViewOptions {
|
|
13
14
|
/** Geom groups to draw. Default 0, 1 and 2, MuJoCo's visible groups. */
|
|
@@ -15,7 +16,7 @@ export interface HumanoidViewOptions {
|
|
|
15
16
|
/** Draw plane geoms (the scene's floor). Default false: the page has its own. */
|
|
16
17
|
floor?: boolean;
|
|
17
18
|
/** The axis the robot faces in its model, turned to +Z. Default "x", as MuJoCo Menagerie's humanoids face. */
|
|
18
|
-
forward?:
|
|
19
|
+
forward?: Forward;
|
|
19
20
|
}
|
|
20
21
|
/** A three.js group that follows a {@link Humanoid}; add `root` to a scene and call `update` after each step. */
|
|
21
22
|
export declare class HumanoidView {
|
|
@@ -34,3 +35,5 @@ export declare class HumanoidView {
|
|
|
34
35
|
/** Removes the group and frees its geometries and materials. */
|
|
35
36
|
dispose(): void;
|
|
36
37
|
}
|
|
38
|
+
export { BodyFollow, type BodyFollowOptions, type FollowBody } from "./follow-three.js";
|
|
39
|
+
export type { Forward } from "./frame.js";
|
package/dist/three.js
CHANGED
|
@@ -7,8 +7,8 @@
|
|
|
7
7
|
* space. Collision geoms (group 3 and up) stay hidden unless asked for.
|
|
8
8
|
*/
|
|
9
9
|
import { BoxGeometry, BufferAttribute, BufferGeometry, CapsuleGeometry, CylinderGeometry, Group, Mesh, MeshStandardMaterial, PlaneGeometry, SphereGeometry } from "three";
|
|
10
|
+
import { TURN } from "./frame.js";
|
|
10
11
|
const PLANE = 0, SPHERE = 2, CAPSULE = 3, ELLIPSOID = 4, CYLINDER = 5, BOX = 6, MESH = 7;
|
|
11
|
-
const TURN = { x: -Math.PI / 2, "-x": Math.PI / 2, y: Math.PI, "-y": 0 };
|
|
12
12
|
/** A three.js group that follows a {@link Humanoid}; add `root` to a scene and call `update` after each step. */
|
|
13
13
|
export class HumanoidView {
|
|
14
14
|
sim;
|
|
@@ -107,3 +107,4 @@ export class HumanoidView {
|
|
|
107
107
|
this.meshes = [];
|
|
108
108
|
}
|
|
109
109
|
}
|
|
110
|
+
export { BodyFollow } from "./follow-three.js";
|
package/how.ts
CHANGED
|
@@ -8,6 +8,7 @@ const how: HowDoc = {
|
|
|
8
8
|
"the robot's mjcf and every file it names (includes, meshes, textures) are read from a url or a dropped folder into mujoco's virtual file system, and the model compiles there.",
|
|
9
9
|
"each frame runs whole timesteps of the model and carries the rest; targets go to the position actuators, a keyframe resets the pose, and the view moves one mesh per visible geom.",
|
|
10
10
|
"nvidia's gear-sonic policy can drive the g1 instead: an encoder and a decoder 50 times a second turn a reference pose and the robot's recent state into joint targets, and a pd loop turns those into motor torques at every step, so it balances and catches a shove.",
|
|
11
|
+
"a humanoid body can follow the robot: its limbs are first turned to the robot's zero pose, then each bone takes the turn its robot body has made since, and the hips move with the pelvis at the body's scale.",
|
|
11
12
|
],
|
|
12
13
|
packages: ["@hackbuild/humanoid"],
|
|
13
14
|
install: "npm install @hackbuild/humanoid",
|
package/llms.md
CHANGED
|
@@ -45,12 +45,28 @@ function filesFrom(list: Iterable<File>): Promise<ModelFiles>
|
|
|
45
45
|
|
|
46
46
|
A robot from a file list: an `<input type="file" webkitdirectory>` keeps each file's path in the folder; loose files are keyed by name only, so a model whose meshes sit in a folder needs the folder picked.
|
|
47
47
|
|
|
48
|
+
```ts
|
|
49
|
+
interface FollowMap {
|
|
50
|
+
bones: Record<string, string>;
|
|
51
|
+
root: string;
|
|
52
|
+
segments: { bone: string; child: string; from: string; to: string }[];
|
|
53
|
+
}
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
Robot bodies to humanoid bones (VRM 1.0 names), and the segments posed to the robot's zero pose first.
|
|
57
|
+
|
|
48
58
|
```ts
|
|
49
59
|
const G1: { readonly bytes: 19666440; readonly joints: readonly ["left_hip_pitch_joint", "left_hip_roll_joint", "left_hip_yaw_joint", "left_knee_joint", "left_ankle_pitch_joint", "left_ankle_roll_joint", "right_hip_pitch_joint", "right_hip_roll_joint", "right_hip_yaw_joint", "right_knee_joint", "right_ankle_pitch_joint", "right_ankle_roll_joint", "waist_yaw_joint", "waist_roll_joint", "waist_pitch_joint", "left_shoulder_pitch_joint", "left_shoulder_roll_joint", "left_shoulder_yaw_joint", "left_elbow_joint", "left_wrist_roll_joint", "left_wrist_pitch_joint", "left_wrist_yaw_joint", "right_shoulder_pitch_joint", "right_shoulder_roll_joint", "right_shoulder_yaw_joint", "right_elbow_joint", "right_wrist_roll_joint", "right_wrist_pitch_joint", "right_wrist_yaw_joint"]; readonly keyframe: "stand"; readonly 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."; readonly name: "unitree g1, 29 joints"; readonly url: "https://raw.githubusercontent.com/google-deepmind/mujoco_menagerie/98a70c569c4c1b6da467a20012f766ee1f8dc521/unitree_g1/scene.xml" }
|
|
50
60
|
```
|
|
51
61
|
|
|
52
62
|
The G1 from MuJoCo Menagerie, by URL, with its licence and download size.
|
|
53
63
|
|
|
64
|
+
```ts
|
|
65
|
+
const G1_FOLLOW: FollowMap
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
The Unitree G1, both models (MuJoCo Menagerie's and the gear-sonic deploy model share these body names).
|
|
69
|
+
|
|
54
70
|
```ts
|
|
55
71
|
class Humanoid {
|
|
56
72
|
readonly actuated: string[];
|
|
@@ -118,6 +134,9 @@ interface MjModel {
|
|
|
118
134
|
actuator_biastype: Int32Array;
|
|
119
135
|
actuator_trnid: Int32Array;
|
|
120
136
|
actuator_trntype: Int32Array;
|
|
137
|
+
body_parentid: Int32Array;
|
|
138
|
+
body_pos: Float64Array;
|
|
139
|
+
body_quat: Float64Array;
|
|
121
140
|
geom_dataid: Int32Array;
|
|
122
141
|
geom_group: Int32Array;
|
|
123
142
|
geom_rgba: Float32Array;
|
|
@@ -232,6 +251,12 @@ function referencedFiles(text: string, dirs?: { assetdir?: string; meshdir?: str
|
|
|
232
251
|
|
|
233
252
|
The files one MJCF names, split into includes and the rest, joined onto its compiler's directories.
|
|
234
253
|
|
|
254
|
+
```ts
|
|
255
|
+
function restPoses(model: Pick<MjModel, "nbody" | "body_parentid" | "body_pos" | "body_quat">): Pose[]
|
|
256
|
+
```
|
|
257
|
+
|
|
258
|
+
Every body's world pose at qpos0 (hinge and slide joints at their reference, a free root where the model places it), from the chain of body offsets.
|
|
259
|
+
|
|
235
260
|
### @hackbuild/humanoid/sonic
|
|
236
261
|
|
|
237
262
|
```ts
|
|
@@ -366,6 +391,42 @@ A model order array (29) in the policy's order.
|
|
|
366
391
|
|
|
367
392
|
### @hackbuild/humanoid/three
|
|
368
393
|
|
|
394
|
+
```ts
|
|
395
|
+
class BodyFollow {
|
|
396
|
+
constructor(sim: Humanoid, body: FollowBody, o?: BodyFollowOptions);
|
|
397
|
+
readonly missing: string[];
|
|
398
|
+
reset(): void;
|
|
399
|
+
update(): void;
|
|
400
|
+
}
|
|
401
|
+
```
|
|
402
|
+
|
|
403
|
+
Drives a humanoid body from a Humanoid: call `update` after each step.
|
|
404
|
+
|
|
405
|
+
```ts
|
|
406
|
+
interface BodyFollowOptions {
|
|
407
|
+
forward?: Forward;
|
|
408
|
+
map?: FollowMap;
|
|
409
|
+
travel?: boolean;
|
|
410
|
+
}
|
|
411
|
+
```
|
|
412
|
+
|
|
413
|
+
Options for BodyFollow.
|
|
414
|
+
|
|
415
|
+
```ts
|
|
416
|
+
interface FollowBody {
|
|
417
|
+
bones: Partial<Record<string, Object3D>>;
|
|
418
|
+
root: Object3D;
|
|
419
|
+
}
|
|
420
|
+
```
|
|
421
|
+
|
|
422
|
+
A body to drive: its root and its bones by VRM 1.0 name, as `@hackbuild/avatar` loads one.
|
|
423
|
+
|
|
424
|
+
```ts
|
|
425
|
+
type Forward = "x" | "-x" | "y" | "-y"
|
|
426
|
+
```
|
|
427
|
+
|
|
428
|
+
The axis a robot faces in its model.
|
|
429
|
+
|
|
369
430
|
```ts
|
|
370
431
|
class HumanoidView {
|
|
371
432
|
constructor(sim: Humanoid, o?: HumanoidViewOptions);
|
|
@@ -381,7 +442,7 @@ A three.js group that follows a Humanoid; add `root` to a scene and call `update
|
|
|
381
442
|
```ts
|
|
382
443
|
interface HumanoidViewOptions {
|
|
383
444
|
floor?: boolean;
|
|
384
|
-
forward?:
|
|
445
|
+
forward?: Forward;
|
|
385
446
|
groups?: number[];
|
|
386
447
|
}
|
|
387
448
|
```
|
|
@@ -414,6 +475,23 @@ robot.moveTo({ left_knee_joint: 0.6, right_knee_joint: 0.6, left_hip_pitch_joint
|
|
|
414
475
|
renderer.setAnimationLoop(() => (robot.step(1 / 60), view.update(), renderer.render(scene, camera)));
|
|
415
476
|
```
|
|
416
477
|
|
|
478
|
+
### from the concepts
|
|
479
|
+
|
|
480
|
+
```ts
|
|
481
|
+
import { load } from "@hackbuild/avatar";
|
|
482
|
+
import { loadAvatar } from "@hackbuild/avatar/three";
|
|
483
|
+
import { BodyFollow } from "@hackbuild/humanoid/three";
|
|
484
|
+
|
|
485
|
+
await load(); // the bone mapper's wasm
|
|
486
|
+
const person = await loadAvatar("person.glb");
|
|
487
|
+
person.root.position.x = 0.9; // beside the robot
|
|
488
|
+
scene.add(person.root);
|
|
489
|
+
const follow = new BodyFollow(robot, person);
|
|
490
|
+
// each frame, after the robot steps
|
|
491
|
+
follow.update();
|
|
492
|
+
person.update(dt);
|
|
493
|
+
```
|
|
494
|
+
|
|
417
495
|
## Mistakes to avoid
|
|
418
496
|
|
|
419
497
|
- Do not bundle @mujoco/mujoco or copy mujoco.wasm into the package build; pass its factory from your bundler or let `loadMujoco` load it from its URL.
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@hackbuild/humanoid",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"description": "A physics humanoid in the page: MuJoCo (Google DeepMind's WebAssembly build, loaded at run time and never bundled) runs a robot from its MJCF files, holds joint targets with its position actuators, and gives the body's pose to a three.js view. Starts with the Unitree G1, and runs NVIDIA's GEAR-SONIC policy on it so it balances (weights and onnxruntime-web loaded at run time).",
|
|
3
|
+
"version": "0.3.0",
|
|
4
|
+
"description": "A physics humanoid in the page: MuJoCo (Google DeepMind's WebAssembly build, loaded at run time and never bundled) runs a robot from its MJCF files, holds joint targets with its position actuators, and gives the body's pose to a three.js view. Starts with the Unitree G1, and runs NVIDIA's GEAR-SONIC policy on it so it balances (weights and onnxruntime-web loaded at run time). A humanoid body (a VRM or GLB person) can follow it.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "MIT",
|
|
7
7
|
"author": "Moheeb Zara <hackbuildvideo@gmail.com>",
|