Files
chenlin a8be5e0134 feat: release v0.5 with shared interactive arenas and textured RP1
Unify lighting and line-colour controls across robots, replace the football with a shared layout of draggable physical props, and import the supplied RP1 GLBs without changing trained physics or policies.

Include reproducible visual import/provenance, regression coverage and verification records. Verified 66 unit tests, 55 browser tests and production-build checks.
2026-10-30 14:14:03 +08:00

275 lines
9.3 KiB
JavaScript

import { test, expect } from "@playwright/test";
import { bootDuck } from "./support/simulation.js";
async function boot(page) {
await bootDuck(page);
await page.evaluate(() => window.__gameApi.pause());
}
test("all 63 RP1 material parts follow 25 physical bodies, including fixed fingers, with original shapes aligned", async ({
page,
}, info) => {
const errors = [];
page.on("pageerror", (e) => errors.push(e.message));
await boot(page);
expect(await page.evaluate(() => window.__gameApi.switchRobot("rp1"))).toBe(
true,
);
const inspect = () =>
page.evaluate(async () => {
const r = window.rl,
layer = r.scene.getObjectByName("body-visuals"),
rig = layer.parent;
rig.updateMatrixWorld(true);
const xml = new DOMParser().parseFromString(
await (await fetch("robots/rp1/visuals/rp1_hand_fixed.urdf")).text(),
"text/xml",
);
const joints = new Map(
[...xml.querySelectorAll("joint")].map((j) => [
j.querySelector("child").getAttribute("link"),
j,
]),
);
const Matrix = rig.matrix.constructor,
Euler = r.controls.object.rotation.constructor;
const correction = new Matrix().makeRotationX(Math.PI / 2);
const urdfRelative = (name, anchor) => {
const result = new Matrix();
while (name !== anchor) {
const joint = joints.get(name),
origin = joint.querySelector("origin");
const xyz = (origin?.getAttribute("xyz") ?? "0 0 0")
.split(/\s+/)
.map(Number);
const angles = (origin?.getAttribute("rpy") ?? "0 0 0")
.split(/\s+/)
.map(Number);
const matrix = new Matrix().makeRotationFromEuler(
new Euler(...angles, "ZYX"),
);
matrix.setPosition(...xyz);
result.premultiply(matrix);
name = joint.querySelector("parent").getAttribute("link");
}
return result.multiply(correction);
};
const bounds = (objects, frame) => {
const lo = [Infinity, Infinity, Infinity],
hi = [-Infinity, -Infinity, -Infinity];
const inverse = frame.matrixWorld.clone().invert(),
vector = frame.position.clone();
for (const mesh of objects) {
const transform = inverse.clone().multiply(mesh.matrixWorld),
attr = mesh.geometry.attributes.position;
for (let i = 0; i < attr.count; i++) {
vector.fromBufferAttribute(attr, i).applyMatrix4(transform);
vector.toArray().forEach((v, a) => {
lo[a] = Math.min(lo[a], v);
hi[a] = Math.max(hi[a], v);
});
}
}
return [...lo, ...hi];
};
let frameError = 0,
boundsError = 0,
handError = 0,
handParts = 0,
normals = 0,
screens = 0,
emissive = 0,
hidden = 0,
meshes = 0;
for (const frame of layer.children) {
const body = frame.name.slice("body-visual:".length),
id = r.mujoco.mj_name2id(
r.model,
r.mujoco.mjtObj.mjOBJ_BODY.value,
body,
);
const p = Array.from(r.data.xpos.slice(id * 3, id * 3 + 3)),
q = Array.from(r.data.xquat.slice(id * 4, id * 4 + 4));
frameError = Math.max(
frameError,
...p.map((v, i) => Math.abs(v - frame.position.toArray()[i])),
...[q[1], q[2], q[3], q[0]].map((v, i) =>
Math.abs(v - frame.quaternion.toArray()[i]),
),
);
const originals = rig.children.filter(
(m) =>
m.userData.geomId !== undefined &&
r.model.geom_bodyid[m.userData.geomId] === id,
);
hidden += originals.filter((m) => !m.visible).length;
const replacements = [];
frame.traverse((node) => {
if (node.userData.sourcePart) {
handParts++;
const actual = frame.matrixWorld
.clone()
.invert()
.multiply(node.matrixWorld);
const expected = urdfRelative(
node.userData.sourcePart,
`${body.split("_")[0]}_wrist_roll_link`,
);
handError = Math.max(
handError,
...actual.elements.map((v, i) =>
Math.abs(v - expected.elements[i]),
),
);
}
if (!node.isMesh) return;
meshes++;
replacements.push(node);
for (const m of [node.material].flat()) {
if (m.normalMap?.image?.width > 0) normals++;
if (m.map?.image?.width > 0) screens++;
if (m.emissiveIntensity > 1) emissive++;
}
});
if (!body.endsWith("_wrist_link")) {
const before = bounds(originals, frame),
after = bounds(replacements, frame);
boundsError = Math.max(
boundsError,
...before.map((v, i) => Math.abs(v - after[i])),
);
}
}
return {
bodies: layer.children.length,
frameError,
boundsError,
handError,
handParts,
normals,
screens,
emissive,
hidden,
meshes,
obs: r.buildObs().length,
actions: r.lastAction.length,
actuators: r.model.nu,
};
});
const initial = await inspect();
console.log("RP1 GLB alignment", initial);
expect(initial.bodies).toBe(25);
expect(initial.handParts).toBe(40);
expect(initial.frameError).toBeLessThan(1e-12);
expect(initial.boundsError).toBeLessThan(1e-7);
expect(initial.handError).toBeLessThan(1e-10);
expect(initial.normals).toBeGreaterThanOrEqual(63);
expect(initial.screens).toBeGreaterThanOrEqual(1);
expect(initial.emissive).toBeGreaterThanOrEqual(1);
expect(initial.hidden).toBe(25);
expect(initial.obs).toBe(243);
expect(initial.actions).toBe(24);
expect(initial.actuators).toBe(24);
await page.evaluate(() => {
const r = window.rl;
r.data.qpos[3] = Math.cos(0.4);
r.data.qpos[6] = Math.sin(0.4);
for (let j = 0; j < r.model.njnt; j++)
if (r.model.jnt_type[j] === 3)
r.data.qpos[r.model.jnt_qposadr[j]] += 0.12;
r.mujoco.mj_forward(r.model, r.data);
});
await page.waitForTimeout(200);
const posed = await inspect();
expect(posed.frameError).toBeLessThan(1e-12);
expect(posed.boundsError).toBeLessThan(1e-7);
expect(posed.handError).toBeLessThan(1e-10);
await page.evaluate(() => window.rl.resetSim());
await page.getByRole("button", { name: /Keyboard controls/ }).click();
await page.mouse.click(950, 750, { button: "right" });
await page.evaluate(() => {
const r = window.rl;
r.setPropsParked(true);
r.camera.position.set(2.4, 1.35, 1.3);
r.controls.target.set(0, 0.7, 0);
r.controls.update();
});
await page.waitForTimeout(300);
await page.screenshot({ path: info.outputPath("rp1-material-front.png") });
expect(errors).toEqual([]);
});
test("RP1 GLBs change no physics, observations or ONNX actions for walking, wave and dance", async ({
page,
}) => {
await boot(page);
let textured = false;
await page.route("**/robots/rp1/robot.json*", async (route) => {
const response = await route.fetch(),
config = await response.json();
if (!textured) delete config.render.body_visuals;
await route.fulfill({ response, json: config });
});
async function run() {
expect(await page.evaluate(() => window.__gameApi.switchRobot("rp1"))).toBe(
true,
);
return page.evaluate(async () => {
const r = window.rl,
result = [];
for (const scenario of ["walk", "wave", "dance"]) {
await r.resetSim();
await r.step(100);
if (scenario === "walk") r.command.set([0.4, 0, 0.15]);
else r.policyState.request(scenario, r.data.time);
for (let i = 0; i < 5; i++) {
await r.step(50);
result.push({
scenario,
time: r.data.time,
qpos: Array.from(r.data.qpos),
qvel: Array.from(r.data.qvel),
ctrl: Array.from(r.data.ctrl),
action: Array.from(r.lastAction),
obs: Array.from(r.buildObs()),
state: r.policyState.state,
reference: r.referenceFrame,
});
}
}
return result;
});
}
const old = await run();
await page.evaluate(() => window.__gameApi.switchRobot("microduck-legs"));
textured = true;
expect(await run()).toEqual(old);
});
test("a partial RP1 material load failure releases the candidate and retry succeeds", async ({
page,
}) => {
await boot(page);
let fail = true;
await page.route(
"**/robots/rp1/visuals/left_hip_roll_link__visual_0__left_hip_roll_link.glb",
(route) =>
fail ? route.fulfill({ status: 404, body: "missing" }) : route.continue(),
);
expect(await page.evaluate(() => window.__gameApi.switchRobot("rp1"))).toBe(
false,
);
await expect(page.getByRole("alert")).toContainText("404");
expect(await page.evaluate(() => window.__store.getState().robotId)).toBe(
"microduck-legs",
);
expect(await page.evaluate(() => window.rl.model.isDeleted())).toBe(false);
fail = false;
expect(await page.evaluate(() => window.__gameApi.retry())).toBe(true);
expect(
await page.evaluate(
() => window.rl.scene.getObjectByName("body-visuals").children.length,
),
).toBe(25);
});