Render Anything Engine: tell your AI what to make. Videos, motion graphics, 3D and games as React and TypeScript, rendered on your own GPU. Rust + Bevy, macOS, Linux and Windows.
TypeScript 3dagent-skillsai-agentsai-videobevyclaude-codegame-enginegpulinuxmacosmotion-designmotion-graphicsreactrusttypescriptvideo-editingvideo-renderingwgpuwindows
// A playable held to the native render (docs/playables.feature PA9): one composition frame drawn by `raea render` and
// by the playable host (a preview build opened at the composition's size, no one playing, so both play themselves),
// compared where the native frame drew something over its background. The playable is drawn in WebKit, as an
// iPhone's ad view draws it (raea doesn't test in Chrome).
// `bun tools/parity.ts GAME.tsx COMPOSITION FRAME OUT_DIR [BAR]` prints the numbers, writes native.png, playable.png
// and diff.png, and exits 1 when the mean colour difference over the drawn pixels is over BAR (0-255, default 16).
import { mkdirSync } from "node:fs";
import { resolve } from "node:path";
import { webkit } from "playwright-core";
const [game, comp, at, outArg, barArg = "16"] = process.argv.slice(2);
if (!game || !comp || !at || !outArg) throw new Error("usage: bun tools/parity.ts GAME.tsx COMPOSITION FRAME OUT_DIR [BAR]");
const out = resolve(outArg);
mkdirSync(out, { recursive: true });
const name = game.split("/").pop()!.replace(/\.tsx$/, "");
const run = async (cmd: string[]) => {
const p = Bun.spawn(cmd, { stdout: "pipe", stderr: "pipe" });
const [o, e] = [await new Response(p.stdout).text(), await new Response(p.stderr).text()];
if ((await p.exited) !== 0) throw new Error(`${cmd.join(" ")} failed:\n${o}\n${e}`);
return o;
};
// the native frame, and the composition's size from the registry the playable reads
await run(["raea", "render", resolve(game), "-o", `${out}/native.png`, "--composition", comp, "--frame", at]);
await run(["raea", "playable", resolve(game), "-o", out, "--networks", "preview"]);
const size = (await run(["ffprobe", "-v", "error", "-select_streams", "v", "-show_entries", "stream=width,height", "-of", "csv=p=0", `${out}/native.png`])).trim();
const [W, H] = size.split(",").map(Number);
const browser = await webkit.launch();
const p = await browser.newPage({ viewport: { width: W, height: H }, deviceScaleFactor: 1 });
const problems: string[] = [];
p.on("pageerror", (e) => problems.push(`exception: ${e.message}`));
await p.route("**/*", (r) => {
const u = r.request().url();
if (/^(file|data|blob):/.test(u)) return r.continue();
problems.push(`network request: ${u}`);
return r.abort();
});
await p.goto(`file://${out}/${name}-preview.html#still=${at}&comp=${comp}`);
await p.waitForFunction(() => (globalThis as { __raeaStill?: unknown }).__raeaStill, null, { timeout: 10_000 }).catch(() => problems.push("the still never drew"));
await p.screenshot({ path: `${out}/playable.png` });
await browser.close();
const raw = async (png: string) => new Uint8Array(await new Response(Bun.spawn(["ffmpeg", "-v", "error", "-i", png, "-f", "rawvideo", "-pix_fmt", "rgb24", "-"], { stdout: "pipe" }).stdout).arrayBuffer());
const [a, b] = [await raw(`${out}/native.png`), await raw(`${out}/playable.png`)];
if (a.length !== b.length) throw new Error(`parity: sizes differ (${a.length} vs ${b.length} bytes)`);
await run(["ffmpeg", "-v", "error", "-y", "-i", `${out}/native.png`, "-i", `${out}/playable.png`, "-filter_complex", "blend=all_mode=difference", `${out}/diff.png`]);
// the drawn pixels: those of the native frame away from its background (the corner's colour)
const bg = [a[0], a[1], a[2]];
let n = 0, diff = 0, covered = 0;
const sumA = [0, 0, 0], sumB = [0, 0, 0];
for (let i = 0; i < a.length; i += 3) {
if (Math.abs(a[i] - bg[0]) + Math.abs(a[i + 1] - bg[1]) + Math.abs(a[i + 2] - bg[2]) < 24) continue;
n++;
for (let k = 0; k < 3; k++) {
diff += Math.abs(a[i + k] - b[i + k]) / 3;
sumA[k] += a[i + k];
sumB[k] += b[i + k];
}
if (Math.abs(b[i] - bg[0]) + Math.abs(b[i + 1] - bg[1]) + Math.abs(b[i + 2] - bg[2]) >= 24) covered++;
}
const hex = (s: number[]) => "#" + s.map((v) => Math.round(v / n).toString(16).padStart(2, "0")).join("");
const luma = (s: number[]) => 0.2126 * s[0] + 0.7152 * s[1] + 0.0722 * s[2];
const mean = diff / Math.max(1, n);
const bar = Number(barArg);
console.log(`${comp} frame ${at}: ${n} drawn pixels (${((100 * n) / (a.length / 3)).toFixed(1)}% of the frame), the playable covers ${((100 * covered) / Math.max(1, n)).toFixed(1)}% of them`);
console.log(`mean colour native ${hex(sumA)} playable ${hex(sumB)}, brightness ${(luma(sumB) / luma(sumA)).toFixed(3)}x`);
console.log(`mean difference ${mean.toFixed(1)} / 255 (bar ${bar}): ${mean <= bar ? "ok" : "OVER"}`);
for (const p of problems) console.log(`PROBLEM: ${p}`);
process.exit(mean <= bar && !problems.length ? 0 : 1);