Files
VibemakerSandbox/app/e2e/microban-visuals.spec.js
T

213 lines
7.1 KiB
JavaScript

import { test, expect } from "@playwright/test";
async function boot(page) {
await page.goto("/");
await expect(
page.getByRole("button", { name: "Pause", exact: true }),
).toBeEnabled();
await page.evaluate(() => window.__gameApi.pause());
}
test("20 textured GLBs replace only Microban visuals, align to original geometry and follow all bodies", async ({
page,
}, info) => {
const errors = [];
page.on("pageerror", (error) => errors.push(error.message));
await boot(page);
expect(
await page.evaluate(() => window.__gameApi.switchRobot("microban")),
).toBe(true);
const inspect = () =>
page.evaluate(() => {
const r = window.rl,
layer = r.scene.getObjectByName("body-visuals"),
rig = layer.parent;
rig.updateMatrixWorld(true);
let maxFrameError = 0,
maxBoundsError = 0,
maps = 0,
colorMaps = 0,
meshes = 0,
hidden = 0;
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);
const v = vector.toArray();
for (let axis = 0; axis < 3; axis++) {
lo[axis] = Math.min(lo[axis], v[axis]);
hi[axis] = Math.max(hi[axis], v[axis]);
}
}
}
return [...lo, ...hi];
};
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));
const q = Array.from(r.data.xquat.slice(id * 4, id * 4 + 4));
maxFrameError = Math.max(
maxFrameError,
...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(
(mesh) =>
mesh.userData.geomId !== undefined &&
r.model.geom_bodyid[mesh.userData.geomId] === id,
);
const replacements = [];
frame.traverse((object) => {
if (!object.isMesh) return;
meshes++;
replacements.push(object);
const materials = [object.material].flat();
maps += materials.filter(
(material) => material.normalMap?.image?.width > 0,
).length;
colorMaps += materials.filter(
(material) => material.map?.image?.width > 0,
).length;
});
hidden += originals.filter((mesh) => !mesh.visible).length;
const oldBounds = bounds(originals, frame),
newBounds = bounds(replacements, frame);
maxBoundsError = Math.max(
maxBoundsError,
...oldBounds.map((v, i) => Math.abs(v - newBounds[i])),
);
}
return {
count: layer.children.length,
maps,
colorMaps,
meshes,
hidden,
maxFrameError,
maxBoundsError,
obs: r.buildObs().length,
};
});
const initial = await inspect();
console.log("Microban visual alignment", initial);
expect(initial.count).toBe(20);
expect(initial.maps).toBeGreaterThanOrEqual(20);
expect(initial.colorMaps).toBeGreaterThanOrEqual(1);
expect(initial.hidden).toBeGreaterThan(20);
expect(initial.maxFrameError).toBeLessThan(1e-12);
expect(initial.maxBoundsError).toBeLessThan(0.0031);
expect(initial.obs).toBe(63);
// Change root yaw and every hinge, including head: no bind-pose-only rig.
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 i = 7; i < r.model.nq; i++) r.data.qpos[i] += 0.12;
r.mujoco.mj_forward(r.model, r.data);
});
await page.waitForTimeout(150);
const articulated = await inspect();
expect(articulated.maxFrameError).toBeLessThan(1e-12);
expect(articulated.maxBoundsError).toBeLessThan(0.0031);
await page.evaluate(() => window.rl.resetSim());
await page.waitForTimeout(150);
// Front 3/4 view for a useful visual acceptance artifact.
await page.evaluate(() => {
window.rl.camera.position.set(0.58, 0.38, 0.58);
window.rl.controls.target.set(0, 0.17, 0);
window.rl.controls.update();
});
await page.waitForTimeout(200);
const screenshot = info.outputPath("textured-microban.png");
await page.screenshot({ path: screenshot });
await info.attach("textured-microban.png", {
path: screenshot,
contentType: "image/png",
});
expect(errors).toEqual([]);
});
test("failed visual candidate rolls back without hiding the old robot; retry loads textures", async ({
page,
}) => {
await boot(page);
const before = await page.evaluate(
() => window.__gameApi.diagnostics().sceneChildren,
);
let fail = true;
await page.route(
"**/robots/microban/visuals/LINK10__visual_0__LINK10.glb",
(route) =>
fail
? route.fulfill({ status: 404, body: "missing visual" })
: route.continue(),
);
expect(
await page.evaluate(() => window.__gameApi.switchRobot("microban")),
).toBe(false);
expect(await page.evaluate(() => window.__store.getState().robotId)).toBe(
"microduck-legs",
);
expect(
await page.evaluate(() => window.__gameApi.diagnostics().sceneChildren),
).toBe(before);
expect(
await page.evaluate(
() => window.rl.scene.getObjectByName("body-visuals") === undefined,
),
).toBe(true);
fail = false;
expect(
await page.evaluate(() => window.__gameApi.switchRobot("microban")),
).toBe(true);
expect(
await page.evaluate(
() => window.rl.scene.getObjectByName("body-visuals").children.length,
),
).toBe(20);
});
test("visual replacement leaves deterministic physics and ONNX actions unchanged", async ({
page,
}) => {
await boot(page);
let textured = false;
await page.route("**/robots/microban/robot.json", async (route) => {
const response = await route.fetch(),
config = await response.json();
if (!textured) delete config.render.body_visuals;
await route.fulfill({ json: config });
});
async function run() {
expect(
await page.evaluate(() => window.__gameApi.switchRobot("microban")),
).toBe(true);
return page.evaluate(async () => {
const r = window.rl;
await r.resetSim();
await r.step(80);
return {
qpos: Array.from(r.data.qpos),
qvel: Array.from(r.data.qvel),
action: Array.from(r.lastAction),
};
});
}
const white = await run();
expect(
await page.evaluate(() => window.__gameApi.switchRobot("microduck-legs")),
).toBe(true);
textured = true;
const visual = await run();
expect(visual).toEqual(white);
});