@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
package/dist/sim.d.ts
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* One robot in MuJoCo: compiled from its files, stepped at the model's own
|
|
3
|
+
* timestep however the page's frames fall, reset to a keyframe, its joint
|
|
4
|
+
* targets written to the position actuators, and its bodies' poses read in
|
|
5
|
+
* MuJoCo's frame (z up, meters, quaternions w x y z). Every handle MuJoCo
|
|
6
|
+
* made is freed by `dispose`.
|
|
7
|
+
*/
|
|
8
|
+
import type { ModelFiles } from "./files.js";
|
|
9
|
+
import { type MjData, type MjModel, type Mujoco } from "./runtime.js";
|
|
10
|
+
/** A body's world pose: position x y z and quaternion w x y z, in MuJoCo's frame. */
|
|
11
|
+
export interface Pose {
|
|
12
|
+
pos: [number, number, number];
|
|
13
|
+
quat: [number, number, number, number];
|
|
14
|
+
}
|
|
15
|
+
/** A robot running in MuJoCo. */
|
|
16
|
+
export declare class Humanoid {
|
|
17
|
+
private mj;
|
|
18
|
+
readonly model: MjModel;
|
|
19
|
+
readonly data: MjData;
|
|
20
|
+
/** Joint names in model order (a free joint included). */
|
|
21
|
+
readonly joints: string[];
|
|
22
|
+
/** The joint each actuator drives, in actuator order: `targets[i]` is that joint's target for a position actuator. */
|
|
23
|
+
readonly actuated: string[];
|
|
24
|
+
readonly bodies: string[];
|
|
25
|
+
readonly keyframes: string[];
|
|
26
|
+
/** Files the model named that did not arrive. */
|
|
27
|
+
readonly missing: string[];
|
|
28
|
+
/** The model's timestep, seconds. */
|
|
29
|
+
readonly timestep: number;
|
|
30
|
+
private carry;
|
|
31
|
+
private gone;
|
|
32
|
+
private ramp;
|
|
33
|
+
private constructor();
|
|
34
|
+
/** Compiles a robot from its files; a model MuJoCo cannot compile is an error with MuJoCo's reason. */
|
|
35
|
+
static open(mj: Mujoco, files: Pick<ModelFiles, "xml" | "assets"> & {
|
|
36
|
+
missing?: string[];
|
|
37
|
+
}): Humanoid;
|
|
38
|
+
/** Seconds simulated so far. */
|
|
39
|
+
get time(): number;
|
|
40
|
+
/** The actuators' controls: targets in radians for position actuators, a view on the model's data. */
|
|
41
|
+
get targets(): Float64Array;
|
|
42
|
+
/** Back to a keyframe by name or index (its controls too), or to the model's rest state. */
|
|
43
|
+
reset(key?: string | number): void;
|
|
44
|
+
/**
|
|
45
|
+
* Eases the targets named (by actuated joint, or an array in actuator order) from where they are to
|
|
46
|
+
* the new values over `seconds` of simulated time, on a smoothstep; others keep theirs. A sudden
|
|
47
|
+
* jump in targets can topple a robot that a ramp of half a second leaves standing (measured on the G1).
|
|
48
|
+
*/
|
|
49
|
+
moveTo(targets: Record<string, number> | ArrayLike<number>, seconds?: number): void;
|
|
50
|
+
/** True while a {@link moveTo} is still easing. */
|
|
51
|
+
get moving(): boolean;
|
|
52
|
+
private applyRamp;
|
|
53
|
+
/** Each actuated joint's target set to where the joint is now: the body holds its pose. */
|
|
54
|
+
hold(): void;
|
|
55
|
+
/** 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. */
|
|
56
|
+
step(seconds: number, max?: number): number;
|
|
57
|
+
/** A body's world pose, or null for a name the model lacks. */
|
|
58
|
+
pose(body: string | number): Pose | null;
|
|
59
|
+
/** The body carried by the first free joint (a robot's pelvis), or null when the robot is fixed to the world. */
|
|
60
|
+
get rootBody(): string | null;
|
|
61
|
+
/** 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. */
|
|
62
|
+
push(vx: number, vy: number, vz?: number): boolean;
|
|
63
|
+
/** A hinge or slide joint's position and velocity, or null for a name the model lacks. */
|
|
64
|
+
joint(name: string): {
|
|
65
|
+
q: number;
|
|
66
|
+
dq: number;
|
|
67
|
+
} | null;
|
|
68
|
+
/** Frees the model and its data. */
|
|
69
|
+
dispose(): void;
|
|
70
|
+
}
|
package/dist/sim.js
ADDED
|
@@ -0,0 +1,185 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* One robot in MuJoCo: compiled from its files, stepped at the model's own
|
|
3
|
+
* timestep however the page's frames fall, reset to a keyframe, its joint
|
|
4
|
+
* targets written to the position actuators, and its bodies' poses read in
|
|
5
|
+
* MuJoCo's frame (z up, meters, quaternions w x y z). Every handle MuJoCo
|
|
6
|
+
* made is freed by `dispose`.
|
|
7
|
+
*/
|
|
8
|
+
import { HumanoidError } from "./runtime.js";
|
|
9
|
+
const FREE = 0;
|
|
10
|
+
const isArrayLike = (v) => Array.isArray(v) || ArrayBuffer.isView(v);
|
|
11
|
+
/** A robot running in MuJoCo. */
|
|
12
|
+
export class Humanoid {
|
|
13
|
+
mj;
|
|
14
|
+
model;
|
|
15
|
+
data;
|
|
16
|
+
/** Joint names in model order (a free joint included). */
|
|
17
|
+
joints;
|
|
18
|
+
/** The joint each actuator drives, in actuator order: `targets[i]` is that joint's target for a position actuator. */
|
|
19
|
+
actuated;
|
|
20
|
+
bodies;
|
|
21
|
+
keyframes;
|
|
22
|
+
/** Files the model named that did not arrive. */
|
|
23
|
+
missing;
|
|
24
|
+
/** The model's timestep, seconds. */
|
|
25
|
+
timestep;
|
|
26
|
+
carry = 0;
|
|
27
|
+
gone = false;
|
|
28
|
+
ramp = null;
|
|
29
|
+
constructor(mj, model, missing) {
|
|
30
|
+
this.mj = mj;
|
|
31
|
+
this.model = model;
|
|
32
|
+
this.data = new mj.MjData(model);
|
|
33
|
+
const name = (type, i) => mj.mj_id2name(model, mj.mjtObj[type].value, i) || `${type.replace("mjOBJ_", "").toLowerCase()} ${i}`;
|
|
34
|
+
this.joints = Array.from({ length: model.njnt }, (_, i) => name("mjOBJ_JOINT", i));
|
|
35
|
+
this.bodies = Array.from({ length: model.nbody }, (_, i) => name("mjOBJ_BODY", i));
|
|
36
|
+
this.keyframes = Array.from({ length: model.nkey }, (_, i) => name("mjOBJ_KEY", i));
|
|
37
|
+
this.actuated = Array.from({ length: model.nu }, (_, i) => this.joints[model.actuator_trnid[i * 2]] ?? `actuator ${i}`);
|
|
38
|
+
this.timestep = model.opt.timestep;
|
|
39
|
+
this.missing = missing;
|
|
40
|
+
mj.mj_forward(model, this.data);
|
|
41
|
+
}
|
|
42
|
+
/** Compiles a robot from its files; a model MuJoCo cannot compile is an error with MuJoCo's reason. */
|
|
43
|
+
static open(mj, files) {
|
|
44
|
+
const vfs = new mj.MjVFS();
|
|
45
|
+
try {
|
|
46
|
+
for (const [path, bytes] of files.assets)
|
|
47
|
+
vfs.addBuffer(path, bytes);
|
|
48
|
+
let model;
|
|
49
|
+
try {
|
|
50
|
+
model = mj.MjModel.from_xml_string(files.xml, vfs);
|
|
51
|
+
}
|
|
52
|
+
catch (e) {
|
|
53
|
+
const why = e instanceof Error ? e.message : String(e);
|
|
54
|
+
throw new HumanoidError(`mujoco could not compile the model: ${why}${files.missing?.length ? `. files it names that did not arrive: ${files.missing.slice(0, 3).join(", ")}` : ""}`);
|
|
55
|
+
}
|
|
56
|
+
return new Humanoid(mj, model, files.missing ?? []);
|
|
57
|
+
}
|
|
58
|
+
finally {
|
|
59
|
+
vfs.delete();
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
/** Seconds simulated so far. */
|
|
63
|
+
get time() {
|
|
64
|
+
return this.data.time;
|
|
65
|
+
}
|
|
66
|
+
/** The actuators' controls: targets in radians for position actuators, a view on the model's data. */
|
|
67
|
+
get targets() {
|
|
68
|
+
return this.data.ctrl;
|
|
69
|
+
}
|
|
70
|
+
/** Back to a keyframe by name or index (its controls too), or to the model's rest state. */
|
|
71
|
+
reset(key) {
|
|
72
|
+
const k = typeof key === "string" ? this.keyframes.indexOf(key) : key;
|
|
73
|
+
if (typeof key === "string" && k < 0)
|
|
74
|
+
throw new HumanoidError(`the model has no keyframe ${key}; it has ${this.keyframes.join(", ") || "none"}.`);
|
|
75
|
+
if (k === undefined)
|
|
76
|
+
this.mj.mj_resetData(this.model, this.data);
|
|
77
|
+
else
|
|
78
|
+
this.mj.mj_resetDataKeyframe(this.model, this.data, k);
|
|
79
|
+
this.mj.mj_forward(this.model, this.data);
|
|
80
|
+
this.carry = 0;
|
|
81
|
+
this.ramp = null;
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* Eases the targets named (by actuated joint, or an array in actuator order) from where they are to
|
|
85
|
+
* the new values over `seconds` of simulated time, on a smoothstep; others keep theirs. A sudden
|
|
86
|
+
* jump in targets can topple a robot that a ramp of half a second leaves standing (measured on the G1).
|
|
87
|
+
*/
|
|
88
|
+
moveTo(targets, seconds = 0.5) {
|
|
89
|
+
const from = Float64Array.from(this.data.ctrl);
|
|
90
|
+
const to = Float64Array.from(from);
|
|
91
|
+
if (isArrayLike(targets))
|
|
92
|
+
for (let i = 0; i < Math.min(to.length, targets.length); i++)
|
|
93
|
+
to[i] = targets[i];
|
|
94
|
+
else
|
|
95
|
+
for (const [name, v] of Object.entries(targets)) {
|
|
96
|
+
const i = this.actuated.indexOf(name);
|
|
97
|
+
if (i < 0)
|
|
98
|
+
throw new HumanoidError(`no actuator drives ${name}.`);
|
|
99
|
+
to[i] = v;
|
|
100
|
+
}
|
|
101
|
+
this.ramp = { from, to, t0: this.data.time, dur: Math.max(0, seconds) };
|
|
102
|
+
if (seconds <= 0)
|
|
103
|
+
this.applyRamp();
|
|
104
|
+
}
|
|
105
|
+
/** True while a {@link moveTo} is still easing. */
|
|
106
|
+
get moving() {
|
|
107
|
+
return this.ramp !== null;
|
|
108
|
+
}
|
|
109
|
+
applyRamp() {
|
|
110
|
+
const r = this.ramp;
|
|
111
|
+
if (!r)
|
|
112
|
+
return;
|
|
113
|
+
const k = r.dur > 0 ? Math.min(1, (this.data.time - r.t0) / r.dur) : 1;
|
|
114
|
+
const e = k * k * (3 - 2 * k);
|
|
115
|
+
for (let i = 0; i < r.to.length; i++)
|
|
116
|
+
this.data.ctrl[i] = r.from[i] + (r.to[i] - r.from[i]) * e;
|
|
117
|
+
if (k >= 1)
|
|
118
|
+
this.ramp = null;
|
|
119
|
+
}
|
|
120
|
+
/** Each actuated joint's target set to where the joint is now: the body holds its pose. */
|
|
121
|
+
hold() {
|
|
122
|
+
const { model, data } = this;
|
|
123
|
+
for (let i = 0; i < model.nu; i++) {
|
|
124
|
+
const j = model.actuator_trnid[i * 2];
|
|
125
|
+
data.ctrl[i] = data.qpos[model.jnt_qposadr[j]];
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
/** 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. */
|
|
129
|
+
step(seconds, max = 0.1) {
|
|
130
|
+
if (this.gone)
|
|
131
|
+
return 0;
|
|
132
|
+
this.carry = Math.min(this.carry + Math.max(0, seconds), max);
|
|
133
|
+
// counted by division, so float drift in the carry never drops a step
|
|
134
|
+
const n = Math.floor(this.carry / this.timestep + 1e-6);
|
|
135
|
+
for (let i = 0; i < n; i++) {
|
|
136
|
+
this.applyRamp();
|
|
137
|
+
this.mj.mj_step(this.model, this.data);
|
|
138
|
+
}
|
|
139
|
+
this.carry = Math.max(0, this.carry - n * this.timestep);
|
|
140
|
+
return n;
|
|
141
|
+
}
|
|
142
|
+
/** A body's world pose, or null for a name the model lacks. */
|
|
143
|
+
pose(body) {
|
|
144
|
+
const i = typeof body === "number" ? body : this.bodies.indexOf(body);
|
|
145
|
+
if (i < 0 || i >= this.model.nbody)
|
|
146
|
+
return null;
|
|
147
|
+
const p = this.data.xpos, q = this.data.xquat;
|
|
148
|
+
return { pos: [p[i * 3], p[i * 3 + 1], p[i * 3 + 2]], quat: [q[i * 4], q[i * 4 + 1], q[i * 4 + 2], q[i * 4 + 3]] };
|
|
149
|
+
}
|
|
150
|
+
/** The body carried by the first free joint (a robot's pelvis), or null when the robot is fixed to the world. */
|
|
151
|
+
get rootBody() {
|
|
152
|
+
for (let j = 0; j < this.model.njnt; j++)
|
|
153
|
+
if (this.model.jnt_type[j] === FREE)
|
|
154
|
+
return this.bodies[this.model.jnt_bodyid[j]] ?? null;
|
|
155
|
+
return null;
|
|
156
|
+
}
|
|
157
|
+
/** 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. */
|
|
158
|
+
push(vx, vy, vz = 0) {
|
|
159
|
+
for (let j = 0; j < this.model.njnt; j++) {
|
|
160
|
+
if (this.model.jnt_type[j] !== FREE)
|
|
161
|
+
continue;
|
|
162
|
+
const d = this.model.jnt_dofadr[j];
|
|
163
|
+
this.data.qvel[d] += vx;
|
|
164
|
+
this.data.qvel[d + 1] += vy;
|
|
165
|
+
this.data.qvel[d + 2] += vz;
|
|
166
|
+
return true;
|
|
167
|
+
}
|
|
168
|
+
return false;
|
|
169
|
+
}
|
|
170
|
+
/** A hinge or slide joint's position and velocity, or null for a name the model lacks. */
|
|
171
|
+
joint(name) {
|
|
172
|
+
const j = this.joints.indexOf(name);
|
|
173
|
+
if (j < 0)
|
|
174
|
+
return null;
|
|
175
|
+
return { q: this.data.qpos[this.model.jnt_qposadr[j]], dq: this.data.qvel[this.model.jnt_dofadr[j]] };
|
|
176
|
+
}
|
|
177
|
+
/** Frees the model and its data. */
|
|
178
|
+
dispose() {
|
|
179
|
+
if (this.gone)
|
|
180
|
+
return;
|
|
181
|
+
this.gone = true;
|
|
182
|
+
this.data.delete();
|
|
183
|
+
this.model.delete();
|
|
184
|
+
}
|
|
185
|
+
}
|
package/dist/single.d.ts
ADDED
package/dist/single.js
ADDED
package/dist/three.d.ts
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The robot in three.js: one mesh per visible geom (spheres, capsules,
|
|
3
|
+
* cylinders, boxes, ellipsoids, planes and MuJoCo's own meshes, each mesh's
|
|
4
|
+
* geometry shared), coloured from the geom's material or rgba, moved each
|
|
5
|
+
* frame from the geoms' world poses. MuJoCo is z up; the view's root turns
|
|
6
|
+
* that to y up and the robot's forward axis to +Z, the family's humanoid
|
|
7
|
+
* space. Collision geoms (group 3 and up) stay hidden unless asked for.
|
|
8
|
+
*/
|
|
9
|
+
import { Group } from "three";
|
|
10
|
+
import type { Humanoid } from "./sim.js";
|
|
11
|
+
/** Options for {@link HumanoidView}. */
|
|
12
|
+
export interface HumanoidViewOptions {
|
|
13
|
+
/** Geom groups to draw. Default 0, 1 and 2, MuJoCo's visible groups. */
|
|
14
|
+
groups?: number[];
|
|
15
|
+
/** Draw plane geoms (the scene's floor). Default false: the page has its own. */
|
|
16
|
+
floor?: boolean;
|
|
17
|
+
/** The axis the robot faces in its model, turned to +Z. Default "x", as MuJoCo Menagerie's humanoids face. */
|
|
18
|
+
forward?: "x" | "-x" | "y" | "-y";
|
|
19
|
+
}
|
|
20
|
+
/** A three.js group that follows a {@link Humanoid}; add `root` to a scene and call `update` after each step. */
|
|
21
|
+
export declare class HumanoidView {
|
|
22
|
+
private sim;
|
|
23
|
+
readonly root: Group<import("three").Object3DEventMap>;
|
|
24
|
+
private inner;
|
|
25
|
+
private meshes;
|
|
26
|
+
private shared;
|
|
27
|
+
private owned;
|
|
28
|
+
constructor(sim: Humanoid, o?: HumanoidViewOptions);
|
|
29
|
+
private geometry;
|
|
30
|
+
/** Moves every mesh to its geom's pose after the last step. */
|
|
31
|
+
update(): void;
|
|
32
|
+
/** Meshes drawn. */
|
|
33
|
+
get count(): number;
|
|
34
|
+
/** Removes the group and frees its geometries and materials. */
|
|
35
|
+
dispose(): void;
|
|
36
|
+
}
|
package/dist/three.js
ADDED
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The robot in three.js: one mesh per visible geom (spheres, capsules,
|
|
3
|
+
* cylinders, boxes, ellipsoids, planes and MuJoCo's own meshes, each mesh's
|
|
4
|
+
* geometry shared), coloured from the geom's material or rgba, moved each
|
|
5
|
+
* frame from the geoms' world poses. MuJoCo is z up; the view's root turns
|
|
6
|
+
* that to y up and the robot's forward axis to +Z, the family's humanoid
|
|
7
|
+
* space. Collision geoms (group 3 and up) stay hidden unless asked for.
|
|
8
|
+
*/
|
|
9
|
+
import { BoxGeometry, BufferAttribute, BufferGeometry, CapsuleGeometry, CylinderGeometry, Group, Mesh, MeshStandardMaterial, PlaneGeometry, SphereGeometry } from "three";
|
|
10
|
+
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
|
+
/** A three.js group that follows a {@link Humanoid}; add `root` to a scene and call `update` after each step. */
|
|
13
|
+
export class HumanoidView {
|
|
14
|
+
sim;
|
|
15
|
+
root = new Group();
|
|
16
|
+
inner = new Group();
|
|
17
|
+
meshes = [];
|
|
18
|
+
shared = new Map();
|
|
19
|
+
owned = [];
|
|
20
|
+
constructor(sim, o = {}) {
|
|
21
|
+
this.sim = sim;
|
|
22
|
+
const m = sim.model;
|
|
23
|
+
const groups = new Set(o.groups ?? [0, 1, 2]);
|
|
24
|
+
this.root.name = "humanoid";
|
|
25
|
+
this.root.rotation.y = TURN[o.forward ?? "x"];
|
|
26
|
+
this.inner.rotation.x = -Math.PI / 2;
|
|
27
|
+
this.root.add(this.inner);
|
|
28
|
+
for (let g = 0; g < m.ngeom; g++) {
|
|
29
|
+
const type = m.geom_type[g];
|
|
30
|
+
if (!groups.has(m.geom_group[g]) || (type === PLANE && !o.floor))
|
|
31
|
+
continue;
|
|
32
|
+
const geo = this.geometry(g, type);
|
|
33
|
+
if (!geo)
|
|
34
|
+
continue;
|
|
35
|
+
const mat = m.geom_matid?.[g] ?? -1;
|
|
36
|
+
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);
|
|
37
|
+
const material = new MeshStandardMaterial({ roughness: 0.6, metalness: 0.1, transparent: rgba[3] < 1, opacity: rgba[3] });
|
|
38
|
+
material.color.setRGB(rgba[0], rgba[1], rgba[2]);
|
|
39
|
+
this.owned.push(material);
|
|
40
|
+
const mesh = new Mesh(geo, material);
|
|
41
|
+
mesh.matrixAutoUpdate = false;
|
|
42
|
+
mesh.castShadow = mesh.receiveShadow = type !== PLANE;
|
|
43
|
+
this.inner.add(mesh);
|
|
44
|
+
this.meshes.push({ mesh, geom: g });
|
|
45
|
+
}
|
|
46
|
+
this.update();
|
|
47
|
+
}
|
|
48
|
+
geometry(g, type) {
|
|
49
|
+
const m = this.sim.model;
|
|
50
|
+
const s = m.geom_size.subarray(g * 3, g * 3 + 3);
|
|
51
|
+
const own = (geo) => (this.owned.push(geo), geo);
|
|
52
|
+
switch (type) {
|
|
53
|
+
case SPHERE:
|
|
54
|
+
return own(new SphereGeometry(s[0], 20, 14));
|
|
55
|
+
case ELLIPSOID:
|
|
56
|
+
return own(new SphereGeometry(1, 20, 14).scale(s[0], s[1], s[2]));
|
|
57
|
+
case CAPSULE:
|
|
58
|
+
return own(new CapsuleGeometry(s[0], 2 * s[1], 6, 16).rotateX(Math.PI / 2));
|
|
59
|
+
case CYLINDER:
|
|
60
|
+
return own(new CylinderGeometry(s[0], s[0], 2 * s[1], 20).rotateX(Math.PI / 2));
|
|
61
|
+
case BOX:
|
|
62
|
+
return own(new BoxGeometry(2 * s[0], 2 * s[1], 2 * s[2]));
|
|
63
|
+
case PLANE:
|
|
64
|
+
return own(new PlaneGeometry(s[0] ? 2 * s[0] : 40, s[1] ? 2 * s[1] : 40));
|
|
65
|
+
case MESH: {
|
|
66
|
+
const id = m.geom_dataid[g];
|
|
67
|
+
if (id < 0)
|
|
68
|
+
return null;
|
|
69
|
+
const have = this.shared.get(id);
|
|
70
|
+
if (have)
|
|
71
|
+
return have;
|
|
72
|
+
const v = m.mesh_vertadr[id], nv = m.mesh_vertnum[id], f = m.mesh_faceadr[id], nf = m.mesh_facenum[id];
|
|
73
|
+
const geo = new BufferGeometry();
|
|
74
|
+
geo.setAttribute("position", new BufferAttribute(m.mesh_vert.slice(v * 3, (v + nv) * 3), 3));
|
|
75
|
+
geo.setIndex(new BufferAttribute(Uint32Array.from(m.mesh_face.subarray(f * 3, (f + nf) * 3)), 1));
|
|
76
|
+
geo.computeVertexNormals();
|
|
77
|
+
this.shared.set(id, geo);
|
|
78
|
+
return own(geo);
|
|
79
|
+
}
|
|
80
|
+
default:
|
|
81
|
+
return null;
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
/** Moves every mesh to its geom's pose after the last step. */
|
|
85
|
+
update() {
|
|
86
|
+
const p = this.sim.data.geom_xpos, r = this.sim.data.geom_xmat;
|
|
87
|
+
for (const { mesh, geom: g } of this.meshes) {
|
|
88
|
+
const a = g * 9, b = g * 3;
|
|
89
|
+
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);
|
|
90
|
+
mesh.matrixWorldNeedsUpdate = true;
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
/** Meshes drawn. */
|
|
94
|
+
get count() {
|
|
95
|
+
return this.meshes.length;
|
|
96
|
+
}
|
|
97
|
+
/** Removes the group and frees its geometries and materials. */
|
|
98
|
+
dispose() {
|
|
99
|
+
this.root.removeFromParent();
|
|
100
|
+
for (const o of this.owned)
|
|
101
|
+
o.dispose();
|
|
102
|
+
this.meshes = [];
|
|
103
|
+
}
|
|
104
|
+
}
|
package/how.ts
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
// how this works: the fold of the package page and of the suite tool. Plain data, no imports;
|
|
2
|
+
// shape in tools/how.d.ts, checked by tsconfig.how.json, shipped in the tarball, never imported
|
|
3
|
+
// by the runtime entry. Text from the README.
|
|
4
|
+
const how: HowDoc = {
|
|
5
|
+
what: "a physics humanoid in the page: mujoco simulates a robot from its mjcf files, holds the joint targets you give its position actuators, and gives the body's pose to a three.js view.",
|
|
6
|
+
how: [
|
|
7
|
+
"mujoco, google deepmind's webassembly build, loads at run time from a factory or a url and is never bundled; it is single threaded, so the page needs no cross-origin isolation.",
|
|
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
|
+
"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
|
+
],
|
|
11
|
+
packages: ["@hackbuild/humanoid"],
|
|
12
|
+
install: "npm install @hackbuild/humanoid",
|
|
13
|
+
today: "git clone https://github.com/hackbuild/hackbuild-motion && cd hackbuild-motion && npm ci && npm run build && npm pack -w @hackbuild/humanoid",
|
|
14
|
+
code: `import { G1, Humanoid, fetchModel, loadMujoco } from "@hackbuild/humanoid";
|
|
15
|
+
import { HumanoidView } from "@hackbuild/humanoid/three";
|
|
16
|
+
|
|
17
|
+
const mj = await loadMujoco();
|
|
18
|
+
const robot = Humanoid.open(mj, await fetchModel(G1.url));
|
|
19
|
+
robot.reset(G1.keyframe);
|
|
20
|
+
const view = new HumanoidView(robot);
|
|
21
|
+
scene.add(view.root);
|
|
22
|
+
// each frame
|
|
23
|
+
robot.step(1 / 60);
|
|
24
|
+
view.update();`,
|
|
25
|
+
lang: "ts",
|
|
26
|
+
links: { page: "https://hackbuild.github.io/hackbuild-motion/humanoid/", source: "https://github.com/hackbuild/hackbuild-motion/tree/main/packages/humanoid" },
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
export default how;
|