feat: release v0.4 with textured Microban visuals
This commit is contained in:
@@ -0,0 +1,139 @@
|
||||
import test from "node:test";
|
||||
import assert from "node:assert/strict";
|
||||
import fs from "node:fs/promises";
|
||||
import * as THREE from "three";
|
||||
import {
|
||||
BodyVisuals,
|
||||
validateBodyGlb,
|
||||
} from "../src/platform/render/BodyVisuals.js";
|
||||
import { validatePackage } from "../src/platform/loader/SchemaValidator.js";
|
||||
const config = JSON.parse(
|
||||
await fs.readFile(
|
||||
new URL("../public/robots/microban/robot.json", import.meta.url),
|
||||
),
|
||||
);
|
||||
function glb(json) {
|
||||
const text = new TextEncoder().encode(JSON.stringify(json));
|
||||
const buffer = new ArrayBuffer(20 + text.length);
|
||||
const view = new DataView(buffer);
|
||||
[0x46546c67, 2, buffer.byteLength, text.length, 0x4e4f534a].forEach(
|
||||
(value, i) => view.setUint32(i * 4, value, true),
|
||||
);
|
||||
new Uint8Array(buffer, 20).set(text);
|
||||
return buffer;
|
||||
}
|
||||
test("all 20 Microban body GLBs embed their materials/textures and stay visual-only", async () => {
|
||||
validatePackage(config);
|
||||
assert.equal(config.render.body_visuals.length, 20);
|
||||
let textures = 0;
|
||||
for (const { body, path } of config.render.body_visuals) {
|
||||
assert.ok(!config.assets.some((asset) => asset.path === path));
|
||||
const bytes = await fs.readFile(
|
||||
new URL(`../public/robots/microban/${path}`, import.meta.url),
|
||||
);
|
||||
const json = validateBodyGlb(
|
||||
bytes.buffer.slice(bytes.byteOffset, bytes.byteOffset + bytes.byteLength),
|
||||
);
|
||||
assert.ok(json.materials.length > 0);
|
||||
if (body === "head")
|
||||
assert.ok(
|
||||
json.materials.some(
|
||||
(material) => material.pbrMetallicRoughness?.baseColorTexture,
|
||||
),
|
||||
"microban2 screen colour texture must be preserved",
|
||||
);
|
||||
textures += json.images?.length ?? 0;
|
||||
}
|
||||
assert.ok(textures >= 20);
|
||||
});
|
||||
test("visual schema rejects duplicate bodies, non-unit quaternions and external/escaping paths", () => {
|
||||
const duplicate = structuredClone(config);
|
||||
duplicate.render.body_visuals.push(duplicate.render.body_visuals[0]);
|
||||
assert.throws(() => validatePackage(duplicate), /duplicate body/);
|
||||
const zero = structuredClone(config);
|
||||
zero.render.body_visuals[0].quaternion = [0, 0, 0, 0];
|
||||
assert.throws(() => validatePackage(zero), /unit length/);
|
||||
for (const path of [
|
||||
"https://host/a.glb",
|
||||
"../a.glb",
|
||||
"/a.glb",
|
||||
"a.glb?x=1",
|
||||
]) {
|
||||
const invalid = structuredClone(config);
|
||||
invalid.render.body_visuals[0].path = path;
|
||||
assert.throws(() => validatePackage(invalid), /body_visuals/);
|
||||
}
|
||||
});
|
||||
test("GLB validation fails closed on LFS pointers, truncation and external image/buffer requests", () => {
|
||||
for (const bytes of [
|
||||
new ArrayBuffer(0),
|
||||
new TextEncoder().encode("version https://git-lfs.github.com/spec/v1")
|
||||
.buffer,
|
||||
glb({}).slice(0, -1),
|
||||
])
|
||||
assert.throws(() => validateBodyGlb(bytes), /invalid GLB/);
|
||||
for (const json of [
|
||||
{ images: [{ uri: "https://host/normal.png" }] },
|
||||
{ buffers: [{ uri: "../buffer.bin" }] },
|
||||
])
|
||||
assert.throws(() => validateBodyGlb(glb(json)), /embed all/);
|
||||
});
|
||||
test("body attachments track MuJoCo body transforms without touching simulation arrays", () => {
|
||||
const data = {
|
||||
xpos: [0, 0, 0, 1, 2, 3],
|
||||
xquat: [1, 0, 0, 0, 0.5, 0.5, 0.5, 0.5],
|
||||
};
|
||||
const before = structuredClone(data);
|
||||
const visuals = new BodyVisuals(data),
|
||||
frame = new THREE.Group();
|
||||
visuals.items.push({ id: 1, frame });
|
||||
visuals.sync();
|
||||
assert.deepEqual(frame.position.toArray(), [1, 2, 3]);
|
||||
assert.deepEqual(frame.quaternion.toArray(), [0.5, 0.5, 0.5, 0.5]);
|
||||
assert.deepEqual(data, before);
|
||||
});
|
||||
test("shared GLB geometries, materials, textures and ImageBitmaps are released exactly once", () => {
|
||||
const visuals = new BodyVisuals({}),
|
||||
scene = new THREE.Group();
|
||||
const geometry = new THREE.BoxGeometry(),
|
||||
texture = new THREE.Texture();
|
||||
let closed = 0;
|
||||
texture.source.data = { close: () => closed++ };
|
||||
const material = new THREE.MeshStandardMaterial({
|
||||
map: texture,
|
||||
normalMap: texture,
|
||||
});
|
||||
const counts = { geometry: 0, material: 0, texture: 0 };
|
||||
geometry.addEventListener("dispose", () => counts.geometry++);
|
||||
material.addEventListener("dispose", () => counts.material++);
|
||||
texture.addEventListener("dispose", () => counts.texture++);
|
||||
scene.add(
|
||||
new THREE.Mesh(geometry, material),
|
||||
new THREE.Mesh(geometry, material),
|
||||
);
|
||||
visuals.scenes.push(scene, scene);
|
||||
const parent = new THREE.Group();
|
||||
parent.add(visuals.root);
|
||||
visuals.dispose();
|
||||
visuals.dispose();
|
||||
assert.deepEqual(counts, { geometry: 1, material: 1, texture: 1 });
|
||||
assert.equal(closed, 1);
|
||||
assert.equal(parent.children.length, 0);
|
||||
});
|
||||
test("missing body bindings fail before any asset request", async () => {
|
||||
const mujoco = { mj_name2id: () => -1, mjtObj: { mjOBJ_BODY: { value: 1 } } };
|
||||
await assert.rejects(
|
||||
BodyVisuals.load(
|
||||
mujoco,
|
||||
{},
|
||||
{},
|
||||
{
|
||||
config,
|
||||
asset: () => {
|
||||
throw new Error("must not fetch");
|
||||
},
|
||||
},
|
||||
),
|
||||
/missing robot body trunk/,
|
||||
);
|
||||
});
|
||||
Reference in New Issue
Block a user