Environment lighting: - ship all eight desktop studiolights/world datasets (WBENV1 float32) and a shared loader used by both the main viewport and the offscreen worker - add the N-panel "studio 环境" selector - rotate the world background together with the environment (backgroundRotation/backgroundIntensity), so camera view and offscreen renders match the desktop Mapping-node behaviour Verification (Chromium, OPFS, NOT_CLAIMED): - environment lighting + 8 world pixel identity - camera-view rotation correspondence (camera matrices, 207 samples) - offscreen vs on-screen background parity after hiding geometry (1305 samples) - metals case extended with material-field OPFS roundtrip - material Principled edits and rename undo/redo - eight studio worlds: pairwise frame differences + luminance rank correlation - undo history depth capped at 64 steps and undo across OPFS save/reopen Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
120 lines
6.5 KiB
TypeScript
120 lines
6.5 KiB
TypeScript
import { expect, test, type Page } from "@playwright/test";
|
||
import fs from "node:fs";
|
||
import path from "node:path";
|
||
import { exposeWebTools, observeNative } from "../fixtures/native-ui-workflow";
|
||
|
||
const root = path.resolve(import.meta.dirname, "../../..");
|
||
const outputDir = path.join(root, "tests/golden/WEB-MATERIAL-METALS-20261122");
|
||
fs.mkdirSync(outputDir, { recursive: true });
|
||
|
||
type SceneSnapshot = {
|
||
revision: number;
|
||
nodes: Array<{ id: string; name: string; type: string; dataId?: string }>;
|
||
materials: Array<{ id: string; name: string; baseColor: number[]; metallic: number; roughness: number }>;
|
||
};
|
||
|
||
const METALS = [
|
||
{ name: "GoldMetal", hex: "#FFD700", linear: [1, 0.679, 0], metallic: 1, roughness: 0.2, x: -3 },
|
||
{ name: "SilverMetal", hex: "#EDEDED", linear: [0.845, 0.845, 0.845], metallic: 1, roughness: 0.15, x: -1 },
|
||
{ name: "CopperMetal", hex: "#B87333", linear: [0.479, 0.171, 0.033], metallic: 1, roughness: 0.3, x: 1 },
|
||
{ name: "IronMetal", hex: "#8A8A8A", linear: [0.258, 0.258, 0.258], metallic: 1, roughness: 0.5, x: 3 },
|
||
];
|
||
|
||
async function readSnapshot(page: Page): Promise<SceneSnapshot> {
|
||
return page.evaluate(() => new Promise<SceneSnapshot>((resolve, reject) => {
|
||
const worker = (window as unknown as { __nativeWorkflowEngine: Worker }).__nativeWorkflowEngine;
|
||
const requestId = `metals-${Date.now()}-${Math.random().toString(16).slice(2)}`;
|
||
const timer = setTimeout(() => { worker.removeEventListener("message", onMessage); reject(new Error("snapshot timeout")); }, 30_000);
|
||
const onMessage = ({ data }: MessageEvent) => {
|
||
if (data?.requestId !== requestId) return;
|
||
clearTimeout(timer);
|
||
worker.removeEventListener("message", onMessage);
|
||
resolve(data?.result?.snapshot as SceneSnapshot);
|
||
};
|
||
worker.addEventListener("message", onMessage);
|
||
worker.postMessage({ requestId, command: { type: "snapshot" } });
|
||
}));
|
||
}
|
||
|
||
/** React tracks input values, so set through the native setter before dispatching. */
|
||
async function setInputValue(page: Page, label: string, value: string): Promise<void> {
|
||
await page.locator(`[aria-label="${label}"]`).first().evaluate((element, next) => {
|
||
const input = element as HTMLInputElement;
|
||
Object.getOwnPropertyDescriptor(HTMLInputElement.prototype, "value")!.set!.call(input, next);
|
||
input.dispatchEvent(new Event("input", { bubbles: true }));
|
||
input.dispatchEvent(new Event("change", { bubbles: true }));
|
||
}, value);
|
||
}
|
||
|
||
test("Principled BSDF metal presets on Suzanne: gold, silver, copper, iron", async ({ page }) => {
|
||
test.setTimeout(600_000);
|
||
await observeNative(page);
|
||
await page.goto("/?ui=web");
|
||
await page.getByTestId("blend-file-input").setInputFiles(path.join(root, "tests/files/web/basic_scene.blend"));
|
||
await expect(page.getByTestId("engine-status")).toContainText(/SceneIR r\d+/, { timeout: 120_000 });
|
||
await exposeWebTools(page);
|
||
|
||
for (const [index, metal] of METALS.entries()) {
|
||
const before = await readSnapshot(page);
|
||
await page.getByRole("button", { name: "添加", exact: true }).click();
|
||
await page.getByRole("menuitem", { name: "网格", exact: true }).click();
|
||
await page.getByRole("menuitem", { name: "猴头", exact: true }).click();
|
||
await expect(page.getByTestId("engine-status")).toContainText(/(已执行|原生操作已返回):mesh\.primitive_monkey_add/, { timeout: 120_000 });
|
||
await expect.poll(async () => (await readSnapshot(page)).nodes.filter((node) => node.type === "MESH").length, { timeout: 120_000 })
|
||
.toBe(before.nodes.filter((node) => node.type === "MESH").length + 1);
|
||
|
||
await page.getByTestId("object-location-row").getByRole("spinbutton", { name: "位置 X" }).fill(String(metal.x));
|
||
await page.waitForTimeout(300);
|
||
|
||
await page.getByRole("button", { name: "新增槽", exact: true }).click();
|
||
await expect(page.getByLabel("材质名称")).toBeVisible({ timeout: 60_000 });
|
||
const nameInput = page.getByLabel("材质名称");
|
||
await nameInput.fill(metal.name);
|
||
await nameInput.press("Enter");
|
||
await expect.poll(async () => (await readSnapshot(page)).materials.some((material) => material.name === metal.name), { timeout: 60_000 }).toBe(true);
|
||
|
||
await setInputValue(page, "材质基础色", metal.hex);
|
||
await setInputValue(page, "材质粗糙度", String(metal.roughness));
|
||
await setInputValue(page, "材质金属度", String(metal.metallic));
|
||
await page.getByRole("button", { name: "应用 Principled", exact: true }).click();
|
||
|
||
await expect.poll(async () => {
|
||
const material = (await readSnapshot(page)).materials.find((item) => item.name === metal.name);
|
||
return material ? Math.round(material.metallic * 100) : -1;
|
||
}, { timeout: 60_000 }).toBe(Math.round(metal.metallic * 100));
|
||
console.info(`metals: applied ${metal.name} (${index + 1}/${METALS.length})`);
|
||
}
|
||
|
||
const snapshot = await readSnapshot(page);
|
||
const suzanneCount = snapshot.nodes.filter((node) => node.type === "MESH" && /Suzanne/.test(node.name)).length;
|
||
expect(suzanneCount).toBeGreaterThanOrEqual(METALS.length);
|
||
for (const metal of METALS) {
|
||
const material = snapshot.materials.find((item) => item.name === metal.name);
|
||
expect(material, `material ${metal.name} missing`).toBeTruthy();
|
||
expect(material!.metallic).toBeCloseTo(metal.metallic, 3);
|
||
expect(material!.roughness).toBeCloseTo(metal.roughness, 2);
|
||
metal.linear.forEach((component, index) => expect(material!.baseColor[index]).toBeCloseTo(component, 2));
|
||
}
|
||
|
||
/* Material Preview must be lit by an environment, otherwise metallic=1 renders black. */
|
||
await expect(page.locator("canvas[data-studio-lighting]")).toHaveAttribute("data-preview-environment", /studio-(fallback|exr)|world/);
|
||
await page.mouse.move(4, 4);
|
||
await page.keyboard.press("Home");
|
||
await page.waitForTimeout(500);
|
||
await page.screenshot({ path: path.join(outputDir, "metals-four-suzanne.png") });
|
||
fs.writeFileSync(path.join(outputDir, "metals-production.json"), JSON.stringify({
|
||
schemaVersion: 1,
|
||
browser: "chromium",
|
||
storage: "OPFS",
|
||
workflow: "Add Monkey x4 -> location -> addMaterialSlot -> renameId -> setMaterialPrincipled",
|
||
metals: METALS,
|
||
snapshotRevision: snapshot.revision,
|
||
suzanneCount,
|
||
previewEnvironment: await page.locator("canvas[data-studio-lighting]").first().getAttribute("data-preview-environment"),
|
||
iblStatus: await page.locator("canvas[data-studio-lighting]").first().getAttribute("data-ibl-status"),
|
||
materials: snapshot.materials.filter((material) => METALS.some((metal) => metal.name === material.name)),
|
||
status: "PASS",
|
||
acceptance: "NOT_CLAIMED",
|
||
}, null, 2));
|
||
});
|