chore: checkpoint premium 3d stages 0 and 1

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
Даня Архипов
2026-07-29 19:05:27 +00:00
parent 5cc27c6fc2
commit 394c513037
79 changed files with 6221 additions and 612 deletions

View File

@@ -0,0 +1,270 @@
/**
* Stage 0 benchmark runner.
* Measures the four required performance profiles against the preview server
* using headless Chromium forced onto the discrete NVIDIA GPU (ANGLE gl-egl).
*
* Usage:
* node scripts/stage0-benchmark.mjs [--url http://127.0.0.1:3100] [--runs 3]
* [--duration 32000] [--profile baseline]
* [--out docs/stage0_premium_3d]
*
* SOFTWARE_RENDERER results are marked and never treated as GPU baseline.
*/
import { chromium } from 'playwright';
import { mkdirSync, writeFileSync } from 'node:fs';
import { join } from 'node:path';
const args = process.argv.slice(2);
function argValue(name, fallback) {
const i = args.indexOf(`--${name}`);
return i >= 0 && args[i + 1] ? args[i + 1] : fallback;
}
const BASE = argValue('url', 'http://127.0.0.1:3100');
const RUNS = Number(argValue('runs', '3'));
const DURATION_MS = Number(argValue('duration', '32000'));
const OUT_DIR = argValue('out', 'docs/stage0_premium_3d');
const ONLY = argValue('profile', null);
const EXEC = '/root/.cache/ms-playwright/chromium-1228/chrome-linux64/chrome';
// Flags proven in scripts/stage0-gpu-probe.mjs to bind headless Chromium to the
// NVIDIA GTX 1080 instead of SwiftShader.
const GPU_ARGS = [
'--use-gl=angle',
'--use-angle=gl-egl',
'--enable-gpu',
'--ignore-gpu-blocklist',
'--disable-software-rasterizer',
'--disable-vulkan-surface',
];
const PROFILES = [
{
name: 'baseline',
file: 'performance-baseline.json',
shot: '01-baseline-desktop.png',
path: '/?perf=1',
viewport: { width: 1920, height: 1080 },
mobile: false,
},
{
name: 'cinematic-light',
file: 'performance-cinematic-light.json',
shot: '02-cinematic-light-desktop.png',
path: '/?stage0=1&perf=1&shadows=1&camera=1&post=0',
viewport: { width: 1920, height: 1080 },
mobile: false,
},
{
name: 'cinematic-full',
file: 'performance-cinematic-full.json',
shot: '03-cinematic-full-desktop.png',
path: '/?stage0=1&perf=1&shadows=1&camera=1&post=1',
viewport: { width: 1920, height: 1080 },
mobile: false,
},
{
name: 'mobile-low',
file: 'performance-mobile-low.json',
shot: null, // SVG fallback screenshot is produced by the e2e test
path: '/?stage0=1&perf=1&quality=low&shadows=0&camera=0&post=0',
viewport: { width: 390, height: 844 },
mobile: true,
dpr: 3,
userAgent:
'Mozilla/5.0 (Linux; Android 13; Pixel 7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Mobile Safari/537.36',
},
];
const SOFTWARE_RE = /swiftshader|llvmpipe|softpipe|software|mesa offscreen|microsoft basic render/i;
function median(values) {
const s = [...values].sort((a, b) => a - b);
const mid = Math.floor(s.length / 2);
return s.length % 2 ? s[mid] : (s[mid - 1] + s[mid]) / 2;
}
async function collectRun(browser, profile) {
const context = await browser.newContext({
viewport: profile.viewport,
deviceScaleFactor: profile.dpr ?? 1,
userAgent: profile.userAgent,
isMobile: profile.mobile ?? false,
hasTouch: profile.mobile ?? false,
});
const page = await context.newPage();
const consoleErrors = [];
page.on('console', (msg) => {
if (msg.type() === 'error') consoleErrors.push(msg.text());
});
page.on('pageerror', (err) => consoleErrors.push(`pageerror: ${err.message}`));
const loadStart = Date.now();
await page.goto(`${BASE}${profile.path}`, { waitUntil: 'domcontentloaded', timeout: 30_000 });
await page.waitForSelector('canvas', { timeout: 30_000 });
const sceneLoadMs = Date.now() - loadStart;
// Start playback if the play button is visible.
const play = page.locator('[data-testid="demo-play"]');
if (await play.count()) {
await play.first().click({ timeout: 5000 }).catch(() => undefined);
}
await page.waitForFunction(() => typeof window.__PERF_SNAPSHOT__ === 'object', null, { timeout: 15_000 });
// Warmup: let shaders compile and lazy chunks load before sampling, so
// minimum FPS is not poisoned by one-time init stalls.
await page.waitForTimeout(5000);
await page.evaluate(() => window.__PERF_RESET__?.());
await page.waitForTimeout(DURATION_MS);
const snapshot = await page.evaluate(() => window.__PERF_SNAPSHOT__);
const device = await page.evaluate(() => {
const canvas = document.querySelector('canvas');
const gl2 = canvas?.getContext('webgl2');
const gl = gl2 ?? canvas?.getContext('webgl');
let version = 'unknown';
let vendor = 'unknown';
if (gl) {
version = gl instanceof WebGL2RenderingContext ? 'webgl2' : 'webgl1';
const dbg = gl.getExtension('WEBGL_debug_renderer_info');
vendor = dbg ? String(gl.getParameter(dbg.UNMASKED_VENDOR_WEBGL)) : String(gl.getParameter(gl.VENDOR));
}
return {
webglVersion: version,
vendor,
hardwareConcurrency: navigator.hardwareConcurrency ?? null,
deviceMemory: navigator.deviceMemory ?? null,
devicePixelRatio: window.devicePixelRatio,
viewport: { width: window.innerWidth, height: window.innerHeight },
userAgent: navigator.userAgent,
};
});
const adaptLog = await page.evaluate(() => window.__STAGE0_ADAPT__ ?? []);
if (profile.shot) {
await page.screenshot({ path: join(OUT_DIR, 'screenshots', profile.shot) });
}
await context.close();
return { snapshot, device, consoleErrors, sceneLoadMs, adaptLog };
}
async function memoryLoop(browser, profile) {
// 10 reload repetitions: compare heap / geometries / textures first vs last.
const context = await browser.newContext({ viewport: profile.viewport });
const page = await context.newPage();
const samples = [];
for (let i = 0; i < 10; i++) {
await page.goto(`${BASE}${profile.path}`, { waitUntil: 'domcontentloaded', timeout: 30_000 });
await page.waitForSelector('canvas', { timeout: 30_000 });
const play = page.locator('[data-testid="demo-play"]');
if (await play.count()) {
await play.first().click({ timeout: 5000 }).catch(() => undefined);
}
await page.waitForTimeout(8000);
const snap = await page.evaluate(() => window.__PERF_SNAPSHOT__).catch(() => null);
if (snap) {
samples.push({ heapMb: snap.heapMb, geometries: snap.geometries, textures: snap.textures });
}
}
await context.close();
return samples;
}
async function runProfile(browser, profile) {
console.log(`\n=== Profile: ${profile.name} (${RUNS} runs x ${DURATION_MS}ms) ===`);
const runs = [];
for (let i = 0; i < RUNS; i++) {
const r = await collectRun(browser, profile);
runs.push(r);
console.log(
` run ${i + 1}: avgFPS=${r.snapshot.averageFps} minFPS=${r.snapshot.minimumFps} ` +
`p95=${r.snapshot.p95FrameTimeMs}ms drawCalls=${r.snapshot.drawCalls} tris=${r.snapshot.triangles} ` +
`renderer=${r.snapshot.renderer}`,
);
}
const avgFpsMedian = median(runs.map((r) => r.snapshot.averageFps));
const minFpsMedian = median(runs.map((r) => r.snapshot.minimumFps));
const p95Median = median(runs.map((r) => r.snapshot.p95FrameTimeMs));
const renderer = runs[0].snapshot.renderer;
const software = SOFTWARE_RE.test(renderer);
const memory = await memoryLoop(browser, profile);
const memGrowth = memory.length >= 2
? {
heapMbDelta: Number((memory[memory.length - 1].heapMb - memory[0].heapMb).toFixed(2)),
geometriesDelta: memory[memory.length - 1].geometries - memory[0].geometries,
texturesDelta: memory[memory.length - 1].textures - memory[0].textures,
}
: null;
const result = {
profile: profile.name,
url: `${BASE}${profile.path}`,
runs: runs.map((r) => ({
snapshot: r.snapshot,
sceneLoadMs: r.sceneLoadMs,
consoleErrors: r.consoleErrors,
adaptLog: r.adaptLog,
})),
median: {
averageFps: avgFpsMedian,
minimumFps: minFpsMedian,
p95FrameTimeMs: p95Median,
drawCalls: runs[0].snapshot.drawCalls,
triangles: runs[0].snapshot.triangles,
geometries: runs[0].snapshot.geometries,
textures: runs[0].snapshot.textures,
heapMb: median(runs.map((r) => r.snapshot.heapMb)),
dpr: runs[0].snapshot.dpr,
},
device: runs[0].device,
renderer,
softwareRenderer: software,
validity: software ? 'SOFTWARE_RENDERER_NOT_VALID_FOR_GPU_BASELINE' : 'HARDWARE_GPU_VALID',
memoryLoop: { samples: memory, growth: memGrowth },
consoleErrorCount: runs.reduce((n, r) => n + r.consoleErrors.length, 0),
measuredAt: new Date().toISOString(),
durationMsPerRun: DURATION_MS,
};
writeFileSync(join(OUT_DIR, profile.file), JSON.stringify(result, null, 2));
console.log(` -> wrote ${profile.file} | median avgFPS=${avgFpsMedian.toFixed(1)} validity=${result.validity}`);
return result;
}
async function main() {
mkdirSync(join(OUT_DIR, 'screenshots'), { recursive: true });
const browser = await chromium.launch({
headless: true,
executablePath: EXEC,
args: GPU_ARGS,
timeout: 45_000,
});
const profiles = ONLY ? PROFILES.filter((p) => p.name === ONLY) : PROFILES;
if (profiles.length === 0) {
throw new Error(`Unknown profile: ${ONLY}. Available: ${PROFILES.map((p) => p.name).join(', ')}`);
}
const results = [];
for (const profile of profiles) {
results.push(await runProfile(browser, profile));
}
await browser.close();
console.log('\n=== Summary ===');
for (const r of results) {
console.log(
`${r.profile.padEnd(16)} avg=${r.median.averageFps.toFixed(1)} min=${r.median.minimumFps.toFixed(1)} ` +
`p95=${r.median.p95FrameTimeMs}ms calls=${r.median.drawCalls} tris=${r.median.triangles} ${r.validity}`,
);
}
}
main().catch((err) => {
console.error(err);
process.exit(1);
});

View File

@@ -0,0 +1,47 @@
// Stage 0 artifact capture: auto-camera screenshots (05 inspection, 06 route-c).
// Reuses the same headless-GPU flags as scripts/stage0-benchmark.mjs.
import { chromium } from 'playwright';
import { existsSync } from 'node:fs';
const BASE = process.env.STAGE0_BASE ?? 'http://127.0.0.1:3101';
const EXEC = process.env.CHROMIUM_PATH ??
(existsSync('/root/.cache/ms-playwright/chromium_headless_shell-1228/chrome-linux/headless_shell')
? '/root/.cache/ms-playwright/chromium_headless_shell-1228/chrome-linux/headless_shell'
: undefined);
const GPU_ARGS = [
'--use-gl=angle',
'--use-angle=gl-egl',
'--enable-gpu',
'--ignore-gpu-blocklist',
'--disable-software-rasterizer',
'--disable-vulkan-surface',
];
const SHOTS = [
{ file: 'docs/stage0_premium_3d/screenshots/05-auto-camera-inspection.png', url: `${BASE}/?stage0=1&shadows=1&camera=1&post=1&shot=inspection&hud=0` },
{ file: 'docs/stage0_premium_3d/screenshots/06-auto-camera-routing-c.png', url: `${BASE}/?stage0=1&shadows=1&camera=1&post=1&shot=route-c&hud=0` },
];
const browser = await chromium.launch({ headless: true, executablePath: EXEC, args: GPU_ARGS, timeout: 45_000 });
try {
const page = await browser.newPage({ viewport: { width: 1920, height: 1080 } });
const glRenderer = await page.evaluate(() => 'pending');
void glRenderer;
for (const shot of SHOTS) {
await page.goto(shot.url, { waitUntil: 'networkidle' });
await page.locator('canvas').waitFor({ state: 'visible', timeout: 60_000 });
// let the camera settle into the shot and a few frames render
await page.waitForTimeout(6000);
const renderer = await page.evaluate(() => {
const c = document.querySelector('canvas');
const gl = c && (c.getContext('webgl2') ?? c.getContext('webgl'));
const ext = gl && gl.getExtension('WEBGL_debug_renderer_info');
return ext ? gl.getParameter(ext.UNMASKED_RENDERER_WEBGL) : 'unknown';
});
console.log(`${shot.file} renderer=${renderer}`);
await page.screenshot({ path: shot.file });
}
} finally {
await browser.close();
}

View File

@@ -0,0 +1,88 @@
#!/usr/bin/env node
/**
* Stage 0 GPU probe v2 — find flag combo that makes headless Chromium use
* the real NVIDIA GPU for WebGL (not SwiftShader).
*/
import { chromium } from 'playwright';
const EXEC = '/root/.cache/ms-playwright/chromium-1228/chrome-linux64/chrome';
const PROBES = [
{
name: 'vulkan-full',
args: [
'--use-angle=vulkan',
'--enable-features=Vulkan,VulkanFromANGLE,DefaultANGLEVulkan',
'--enable-gpu',
'--ignore-gpu-blocklist',
'--enable-gpu-rasterization',
],
},
{
name: 'vulkan-no-swiftshader',
args: [
'--use-angle=vulkan',
'--enable-features=Vulkan,VulkanFromANGLE,DefaultANGLEVulkan',
'--disable-features=AllowSwiftShaderFallback',
'--enable-gpu',
'--ignore-gpu-blocklist',
],
},
{
name: 'gl-egl-gbm',
args: [
'--use-gl=angle',
'--use-angle=gl-egl',
'--enable-gpu',
'--ignore-gpu-blocklist',
'--disable-software-rasterizer',
'--disable-vulkan-surface',
],
},
{
name: 'egl-native',
args: [
'--use-gl=egl',
'--enable-gpu',
'--ignore-gpu-blocklist',
'--disable-software-rasterizer',
],
},
{
name: 'vulkan-force-device',
args: [
'--use-angle=vulkan',
'--enable-features=Vulkan,VulkanFromANGLE,DefaultANGLEVulkan',
'--enable-gpu',
'--ignore-gpu-blocklist',
'--vulkan-device-name=NVIDIA',
],
},
];
async function probe({ name, args }) {
let browser;
try {
browser = await chromium.launch({ headless: true, executablePath: EXEC, args, timeout: 45_000 });
const page = await browser.newPage();
const info = await page.evaluate(() => {
const canvas = document.createElement('canvas');
const gl = canvas.getContext('webgl2') || canvas.getContext('webgl');
if (!gl) return { renderer: null };
const dbg = gl.getExtension('WEBGL_debug_renderer_info');
return {
renderer: dbg ? gl.getParameter(dbg.UNMASKED_RENDERER_WEBGL) : gl.getParameter(gl.RENDERER),
vendor: dbg ? gl.getParameter(dbg.UNMASKED_VENDOR_WEBGL) : gl.getParameter(gl.VENDOR),
};
});
console.log(`[${name}]`, JSON.stringify(info));
} catch (err) {
console.log(`[${name}] LAUNCH_FAIL ${String(err).split('\n')[0].slice(0, 160)}`);
} finally {
if (browser) await browser.close().catch(() => undefined);
}
}
for (const p of PROBES) {
await probe(p);
}

View File

@@ -0,0 +1,73 @@
#!/usr/bin/env node
/**
* Stage 1 — dependency-free STL analyzer.
* Parses binary (and ASCII) STL, reports triangle count, bounding box,
* file size, and a units guess. Used for source measurement and validation.
*
* Usage: node scripts/stage1-analyze-stl.mjs <file.stl> [more.stl...]
*/
import { readFileSync } from 'node:fs';
export function analyzeSTL(path) {
const buf = readFileSync(path);
const isAscii = buf.subarray(0, 5).toString('ascii').toLowerCase() === 'solid'
&& !looksBinary(buf);
const tris = [];
if (isAscii) {
const text = buf.toString('ascii');
const re = /vertex\s+([-\d.eE+]+)\s+([-\d.eE+]+)\s+([-\d.eE+]+)/g;
let m;
while ((m = re.exec(text)) !== null) {
tris.push([parseFloat(m[1]), parseFloat(m[2]), parseFloat(m[3])]);
}
} else {
const count = buf.readUInt32LE(80);
for (let i = 0; i < count; i++) {
const off = 84 + i * 50 + 12; // skip normal
for (let v = 0; v < 3; v++) {
tris.push([
buf.readFloatLE(off + v * 12),
buf.readFloatLE(off + v * 12 + 4),
buf.readFloatLE(off + v * 12 + 8),
]);
}
}
}
const triCount = isAscii ? tris.length / 3 : buf.readUInt32LE(80);
const min = [Infinity, Infinity, Infinity];
const max = [-Infinity, -Infinity, -Infinity];
for (const p of tris) {
for (let a = 0; a < 3; a++) {
if (p[a] < min[a]) min[a] = p[a];
if (p[a] > max[a]) max[a] = p[a];
}
}
const size = [max[0] - min[0], max[1] - min[1], max[2] - min[2]];
return {
file: path,
format: isAscii ? 'ascii' : 'binary',
fileSizeBytes: buf.length,
triangleCount: triCount,
bboxMin: min.map(round3),
bboxMax: max.map(round3),
size: size.map(round3),
};
}
function looksBinary(buf) {
if (buf.length < 84) return false;
const count = buf.readUInt32LE(80);
return 84 + count * 50 === buf.length;
}
function round3(n) {
return Math.round(n * 1000) / 1000;
}
if (process.argv[1] && process.argv[1].endsWith('stage1-analyze-stl.mjs')) {
const out = [];
for (const f of process.argv.slice(2)) {
out.push(analyzeSTL(f));
}
console.log(JSON.stringify(out, null, 2));
}

View File

@@ -0,0 +1,345 @@
/**
* Stage 1 benchmark runner — real-model performance profiles.
*
* Profiles (Stage 1 §22):
* A control — Stage 0 Cinematic Light artifact from the SAME machine/GPU
* (docs/stage0_premium_3d/performance-cinematic-light.json),
* measured pre-integration. Not re-run: the working tree
* cannot be rolled back without risking Stage 0 changes.
* B real-products — real models + procedural conveyor, cinematic light.
* C real-conveyor — SKIPPED: FCStd conversion blocked (no OCC toolchain).
* No fictitious JSON is produced (Stage 1 §27).
* D real-cinematic — real models + Stage 0 post-processing spike.
* E low-fallback — 390x844 SVG fallback compatibility: no FPS sampling
* (canvas is not mounted); asserts fallback renders and
* ZERO /models/*.stl requests are made (Stage 1 §24).
*
* Usage:
* node scripts/stage1-benchmark.mjs [--url http://127.0.0.1:3101] [--runs 3]
* [--duration 32000] [--profile real-products]
* [--out docs/stage1_real_models]
*/
import { chromium } from 'playwright';
import { mkdirSync, writeFileSync } from 'node:fs';
import { join } from 'node:path';
const args = process.argv.slice(2);
function argValue(name, fallback) {
const i = args.indexOf(`--${name}`);
return i >= 0 && args[i + 1] ? args[i + 1] : fallback;
}
const BASE = argValue('url', 'http://127.0.0.1:3101');
const RUNS = Number(argValue('runs', '3'));
const DURATION_MS = Number(argValue('duration', '32000'));
const OUT_DIR = argValue('out', 'docs/stage1_real_models');
const ONLY = argValue('profile', null);
const EXEC = '/root/.cache/ms-playwright/chromium-1228/chrome-linux64/chrome';
// Flags proven in Stage 0 to bind headless Chromium to the NVIDIA GTX 1080.
const GPU_ARGS = [
'--use-gl=angle',
'--use-angle=gl-egl',
'--enable-gpu',
'--ignore-gpu-blocklist',
'--disable-software-rasterizer',
'--disable-vulkan-surface',
];
const PROFILES = [
{
name: 'real-products',
file: 'performance-real-products.json',
shot: null,
path: '/?stage0=1&perf=1&shadows=1&camera=1&post=0',
viewport: { width: 1920, height: 1080 },
mobile: false,
},
{
name: 'real-cinematic',
file: 'performance-real-cinematic.json',
shot: null,
path: '/?stage0=1&perf=1&shadows=1&camera=1&post=1',
viewport: { width: 1920, height: 1080 },
mobile: false,
},
{
name: 'low-fallback',
file: 'performance-low-fallback.json',
shot: null,
path: '/',
viewport: { width: 390, height: 844 },
mobile: true,
dpr: 3,
userAgent:
'Mozilla/5.0 (Linux; Android 13; Pixel 7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Mobile Safari/537.36',
fallbackCheck: true,
},
];
const SOFTWARE_RE = /swiftshader|llvmpipe|softpipe|software|mesa offscreen|microsoft basic render/i;
function median(values) {
const s = [...values].sort((a, b) => a - b);
const mid = Math.floor(s.length / 2);
return s.length % 2 ? s[mid] : (s[mid - 1] + s[mid]) / 2;
}
async function collectRun(browser, profile) {
const context = await browser.newContext({
viewport: profile.viewport,
deviceScaleFactor: profile.dpr ?? 1,
userAgent: profile.userAgent,
isMobile: profile.mobile ?? false,
hasTouch: profile.mobile ?? false,
});
const page = await context.newPage();
const consoleErrors = [];
page.on('console', (msg) => {
if (msg.type() === 'error') consoleErrors.push(msg.text());
});
page.on('pageerror', (err) => consoleErrors.push(`pageerror: ${err.message}`));
await page.addInitScript(() => {
window.__CTX_LOST__ = 0;
window.__MODEL_REQUESTS__ = [];
window.addEventListener('webglcontextlost', () => window.__CTX_LOST__++, true);
});
const modelRequests = [];
page.on('request', (req) => {
if (/\/models\/.*\.(stl|glb|gltf)/i.test(req.url())) modelRequests.push(req.url());
});
const loadStart = Date.now();
await page.goto(`${BASE}${profile.path}`, { waitUntil: 'domcontentloaded', timeout: 30_000 });
if (profile.fallbackCheck) {
// Profile E: SVG fallback compatibility — no canvas expected.
await page.waitForSelector('[data-testid="main-svg-fallback"]', { timeout: 30_000 });
const sceneLoadMs = Date.now() - loadStart;
await page.waitForTimeout(6000);
const canvasCount = await page.locator('canvas').count();
const ctxLost = await page.evaluate(() => window.__CTX_LOST__);
const detailsLinkVisible = await page.locator('.fallback-details-link').isVisible().catch(() => false);
await context.close();
return {
fallbackVisible: true,
canvasCount,
ctxLost,
modelRequests,
detailsLinkVisible,
consoleErrors,
sceneLoadMs,
};
}
await page.waitForSelector('canvas', { timeout: 30_000 });
const sceneLoadMs = Date.now() - loadStart;
const play = page.locator('[data-testid="demo-play"]');
if (await play.count()) {
await play.first().click({ timeout: 5000 }).catch(() => undefined);
}
await page.waitForFunction(() => typeof window.__PERF_SNAPSHOT__ === 'object', null, { timeout: 15_000 });
// Warmup: shader compilation + lazy chunks before sampling.
await page.waitForTimeout(5000);
await page.evaluate(() => window.__PERF_RESET__?.());
await page.waitForTimeout(DURATION_MS);
const snapshot = await page.evaluate(() => window.__PERF_SNAPSHOT__);
const ctxLost = await page.evaluate(() => window.__CTX_LOST__);
const device = await page.evaluate(() => {
const canvas = document.querySelector('canvas');
const gl2 = canvas?.getContext('webgl2');
const gl = gl2 ?? canvas?.getContext('webgl');
let version = 'unknown';
let vendor = 'unknown';
if (gl) {
version = gl instanceof WebGL2RenderingContext ? 'webgl2' : 'webgl1';
const dbg = gl.getExtension('WEBGL_debug_renderer_info');
vendor = dbg ? String(gl.getParameter(dbg.UNMASKED_VENDOR_WEBGL)) : String(gl.getParameter(gl.VENDOR));
}
return {
webglVersion: version,
vendor,
hardwareConcurrency: navigator.hardwareConcurrency ?? null,
deviceMemory: navigator.deviceMemory ?? null,
devicePixelRatio: window.devicePixelRatio,
viewport: { width: window.innerWidth, height: window.innerHeight },
userAgent: navigator.userAgent,
};
});
await context.close();
return { snapshot, device, consoleErrors, sceneLoadMs, ctxLost, modelRequests };
}
async function memoryLoop(browser, profile) {
const context = await browser.newContext({ viewport: profile.viewport });
const page = await context.newPage();
const samples = [];
for (let i = 0; i < 10; i++) {
await page.goto(`${BASE}${profile.path}`, { waitUntil: 'domcontentloaded', timeout: 30_000 });
await page.waitForSelector('canvas', { timeout: 30_000 });
const play = page.locator('[data-testid="demo-play"]');
if (await play.count()) {
await play.first().click({ timeout: 5000 }).catch(() => undefined);
}
await page.waitForTimeout(8000);
const snap = await page.evaluate(() => window.__PERF_SNAPSHOT__).catch(() => null);
if (snap) {
samples.push({ heapMb: snap.heapMb, geometries: snap.geometries, textures: snap.textures });
}
}
await context.close();
return samples;
}
async function runProfile(browser, profile) {
console.log(`\n=== Profile: ${profile.name} (${RUNS} runs x ${DURATION_MS}ms) ===`);
if (profile.fallbackCheck) {
const runs = [];
for (let i = 0; i < RUNS; i++) {
const r = await collectRun(browser, profile);
runs.push(r);
console.log(
` run ${i + 1}: fallback=${r.fallbackVisible} canvases=${r.canvasCount} ` +
`modelRequests=${r.modelRequests.length} errors=${r.consoleErrors.length}`,
);
}
const result = {
profile: profile.name,
url: `${BASE}${profile.path}`,
kind: 'SVG_FALLBACK_COMPATIBILITY',
policy: 'mobile default = SVG fallback; real-model assets must NOT load',
runs: runs.map((r) => ({
fallbackVisible: r.fallbackVisible,
canvasCount: r.canvasCount,
detailsLinkVisible: r.detailsLinkVisible,
modelRequests: r.modelRequests,
consoleErrors: r.consoleErrors,
sceneLoadMs: r.sceneLoadMs,
ctxLost: r.ctxLost,
})),
verdict: runs.every((r) => r.fallbackVisible && r.modelRequests.length === 0 && r.consoleErrors.length === 0)
? 'PASS'
: 'FAIL',
measuredAt: new Date().toISOString(),
};
writeFileSync(join(OUT_DIR, profile.file), JSON.stringify(result, null, 2));
console.log(` -> wrote ${profile.file} | verdict=${result.verdict}`);
return result;
}
const runs = [];
for (let i = 0; i < RUNS; i++) {
const r = await collectRun(browser, profile);
runs.push(r);
console.log(
` run ${i + 1}: avgFPS=${r.snapshot.averageFps} minFPS=${r.snapshot.minimumFps} ` +
`p95=${r.snapshot.p95FrameTimeMs}ms p99=${r.snapshot.p99FrameTimeMs}ms drawCalls=${r.snapshot.drawCalls} ` +
`tris=${r.snapshot.triangles} renderer=${r.snapshot.renderer}`,
);
}
const avgFpsMedian = median(runs.map((r) => r.snapshot.averageFps));
const minFpsMedian = median(runs.map((r) => r.snapshot.minimumFps));
const p95Median = median(runs.map((r) => r.snapshot.p95FrameTimeMs));
const renderer = runs[0].snapshot.renderer;
const software = SOFTWARE_RE.test(renderer);
const memory = await memoryLoop(browser, profile);
const memGrowth = memory.length >= 2
? {
heapMbDelta: Number((memory[memory.length - 1].heapMb - memory[0].heapMb).toFixed(2)),
geometriesDelta: memory[memory.length - 1].geometries - memory[0].geometries,
texturesDelta: memory[memory.length - 1].textures - memory[0].textures,
}
: null;
const result = {
profile: profile.name,
url: `${BASE}${profile.path}`,
runs: runs.map((r) => ({
snapshot: r.snapshot,
sceneLoadMs: r.sceneLoadMs,
consoleErrors: r.consoleErrors,
ctxLost: r.ctxLost,
modelRequests: r.modelRequests,
})),
median: {
averageFps: avgFpsMedian,
minimumFps: minFpsMedian,
p5Fps: p95Median > 0 ? Number((1000 / p95Median).toFixed(2)) : 0, // 5th-percentile FPS ≈ 1000/p95 frame time
p95FrameTimeMs: p95Median,
p99FrameTimeMs: median(runs.map((r) => r.snapshot.p99FrameTimeMs)),
longFramesOver33: runs[0].snapshot.longFramesOver33,
longFramesOver50: runs[0].snapshot.longFramesOver50,
drawCalls: runs[0].snapshot.drawCalls,
triangles: runs[0].snapshot.triangles,
geometries: runs[0].snapshot.geometries,
textures: runs[0].snapshot.textures,
programs: runs[0].snapshot.programs,
heapMb: median(runs.map((r) => r.snapshot.heapMb)),
dpr: runs[0].snapshot.dpr,
sceneLoadMs: median(runs.map((r) => r.sceneLoadMs)),
contextLosses: runs.reduce((n, r) => n + r.ctxLost, 0),
},
device: runs[0].device,
renderer,
softwareRenderer: software,
validity: software ? 'SOFTWARE_RENDERER_NOT_VALID_FOR_GPU_BASELINE' : 'HARDWARE_GPU_VALID',
memoryLoop: { samples: memory, growth: memGrowth },
consoleErrorCount: runs.reduce((n, r) => n + r.consoleErrors.length, 0),
measuredAt: new Date().toISOString(),
durationMsPerRun: DURATION_MS,
};
writeFileSync(join(OUT_DIR, profile.file), JSON.stringify(result, null, 2));
console.log(` -> wrote ${profile.file} | median avgFPS=${avgFpsMedian.toFixed(1)} validity=${result.validity}`);
return result;
}
async function main() {
mkdirSync(join(OUT_DIR, 'screenshots'), { recursive: true });
const browser = await chromium.launch({
headless: true,
executablePath: EXEC,
args: GPU_ARGS,
timeout: 45_000,
});
const profiles = ONLY ? PROFILES.filter((p) => p.name === ONLY) : PROFILES;
if (profiles.length === 0) {
throw new Error(`Unknown profile: ${ONLY}. Available: ${PROFILES.map((p) => p.name).join(', ')}`);
}
console.log('Profile A (control): docs/stage0_premium_3d/performance-cinematic-light.json (same GPU, pre-integration)');
console.log('Profile C (real-conveyor): SKIPPED — FCStd conversion blocked, no OCC toolchain (Stage 1 §13.5)');
const results = [];
for (const profile of profiles) {
results.push(await runProfile(browser, profile));
}
await browser.close();
console.log('\n=== Summary ===');
for (const r of results) {
if (r.kind === 'SVG_FALLBACK_COMPATIBILITY') {
console.log(`${r.profile.padEnd(16)} verdict=${r.verdict}`);
} else {
console.log(
`${r.profile.padEnd(16)} avg=${r.median.averageFps.toFixed(1)} min=${r.median.minimumFps.toFixed(1)} ` +
`p95=${r.median.p95FrameTimeMs}ms p99=${r.median.p99FrameTimeMs}ms calls=${r.median.drawCalls} ` +
`tris=${r.median.triangles} ctxLost=${r.median.contextLosses} ${r.validity}`,
);
}
}
}
main().catch((err) => {
console.error(err);
process.exit(1);
});

View File

@@ -0,0 +1,122 @@
#!/usr/bin/env node
/**
* Stage 1 — reproducible STL decimation (vertex clustering).
*
* No external dependencies. Clusters vertices on a uniform grid, welds
* duplicates, drops degenerate triangles, recomputes flat face normals,
* writes binary STL. Bounding box is preserved within one grid cell
* (verified by validate-real-models.mjs afterwards).
*
* Usage:
* node scripts/stage1-decimate-stl.mjs <in.stl> <out.stl> <cellSizeMm>
*
* Deterministic: same input + same cell size => byte-identical output.
*/
import { readFileSync, writeFileSync } from 'node:fs';
import { analyzeSTL } from './stage1-analyze-stl.mjs';
const [inPath, outPath, cellArg] = process.argv.slice(2);
if (!inPath || !outPath || !cellArg) {
console.error('usage: node scripts/stage1-decimate-stl.mjs <in.stl> <out.stl> <cellSizeMm>');
process.exit(2);
}
const cell = Number(cellArg);
if (!(cell > 0)) {
console.error('cellSizeMm must be > 0');
process.exit(2);
}
// --- read source (binary or ascii) into flat triangle soup ---
function readTriangles(path) {
const buf = readFileSync(path);
const isAscii = buf.subarray(0, 5).toString('ascii').toLowerCase() === 'solid'
&& !(buf.length >= 84 && 84 + buf.readUInt32LE(80) * 50 === buf.length);
const verts = [];
if (isAscii) {
const text = buf.toString('ascii');
const re = /vertex\s+([-\d.eE+]+)\s+([-\d.eE+]+)\s+([-\d.eE+]+)/g;
let m;
while ((m = re.exec(text)) !== null) {
verts.push(parseFloat(m[1]), parseFloat(m[2]), parseFloat(m[3]));
}
} else {
const count = buf.readUInt32LE(80);
for (let i = 0; i < count; i++) {
const off = 84 + i * 50 + 12;
for (let v = 0; v < 3; v++) {
verts.push(
buf.readFloatLE(off + v * 12),
buf.readFloatLE(off + v * 12 + 4),
buf.readFloatLE(off + v * 12 + 8),
);
}
}
}
return verts;
}
const verts = readTriangles(inPath);
const srcTriCount = verts.length / 9;
// --- vertex clustering weld ---
const keyOf = (x, y, z) => `${Math.round(x / cell)},${Math.round(y / cell)},${Math.round(z / cell)}`;
const clusterIndex = new Map();
const clustered = []; // representative vertex per cluster (first occurrence, stable)
const remap = new Uint32Array(verts.length / 3);
for (let i = 0; i < verts.length; i += 3) {
const key = keyOf(verts[i], verts[i + 1], verts[i + 2]);
let idx = clusterIndex.get(key);
if (idx === undefined) {
idx = clustered.length / 3;
clusterIndex.set(key, idx);
clustered.push(verts[i], verts[i + 1], verts[i + 2]);
}
remap[i / 3] = idx;
}
// --- rebuild triangles, drop degenerates ---
const outTris = [];
for (let t = 0; t < srcTriCount; t++) {
const a = remap[t * 3];
const b = remap[t * 3 + 1];
const c = remap[t * 3 + 2];
if (a === b || b === c || a === c) continue;
outTris.push(a, b, c);
}
const outTriCount = outTris.length / 3;
// --- write binary STL with recomputed flat normals ---
const header = Buffer.alloc(80);
header.write(`decimated(cell=${cell}mm) ${srcTriCount}->${outTriCount} tris`);
const body = Buffer.alloc(4 + outTriCount * 50);
body.writeUInt32LE(outTriCount, 0);
for (let t = 0; t < outTriCount; t++) {
const base = 4 + t * 50;
const [a, b, c] = [outTris[t * 3] * 3, outTris[t * 3 + 1] * 3, outTris[t * 3 + 2] * 3];
const ax = clustered[a], ay = clustered[a + 1], az = clustered[a + 2];
const bx = clustered[b], by = clustered[b + 1], bz = clustered[b + 2];
const cx = clustered[c], cy = clustered[c + 1], cz = clustered[c + 2];
const ux = bx - ax, uy = by - ay, uz = bz - az;
const vx = cx - ax, vy = cy - ay, vz = cz - az;
let nx = uy * vz - uz * vy, ny = uz * vx - ux * vz, nz = ux * vy - uy * vx;
const len = Math.hypot(nx, ny, nz) || 1;
nx /= len; ny /= len; nz /= len;
body.writeFloatLE(nx, base);
body.writeFloatLE(ny, base + 4);
body.writeFloatLE(nz, base + 8);
const vs = [ax, ay, az, bx, by, bz, cx, cy, cz];
vs.forEach((v, i) => body.writeFloatLE(v, base + 12 + i * 4));
body.writeUInt16LE(0, base + 48);
}
writeFileSync(outPath, Buffer.concat([header, body]));
const after = analyzeSTL(outPath);
console.log(JSON.stringify({
input: inPath,
output: outPath,
cellSizeMm: cell,
trianglesBefore: srcTriCount,
trianglesAfter: outTriCount,
fileSizeAfter: after.fileSizeBytes,
sizeMmAfter: after.size,
}, null, 2));

View File

@@ -0,0 +1,119 @@
#!/usr/bin/env node
/**
* Stage 1 screenshot capture — 16 evidence images for docs/stage1_real_models/screenshots/.
*
* Requires a running app server (PLAYWRIGHT_BASE_URL or default http://127.0.0.1:3101).
* 01-before-stage1-overview.png is copied from the pre-Stage-1 visual golden snapshot
* (factual pre-change render from this same environment).
*/
import { chromium } from 'playwright';
import { copyFileSync, mkdirSync } from 'node:fs';
import { dirname, resolve } from 'node:path';
import { fileURLToPath } from 'node:url';
const root = resolve(dirname(fileURLToPath(import.meta.url)), '..');
const OUT = resolve(root, 'docs/stage1_real_models/screenshots');
const BASE = process.env.PLAYWRIGHT_BASE_URL ?? 'http://127.0.0.1:3101';
mkdirSync(OUT, { recursive: true });
const shots = [];
async function snap(page, name) {
const path = resolve(OUT, name);
await page.screenshot({ path });
shots.push(name);
console.log(`captured ${name}`);
}
async function jumpCase(page, index) {
const finished = page.getByTestId('demo-finished');
if (await finished.isVisible().catch(() => false)) {
await finished.getByRole('button', { name: /Replay/i }).click();
}
await page.getByTestId(`demo-case-${index}`).click();
await page.getByTestId('demo-case-label').waitFor({ state: 'visible' });
}
const browser = await chromium.launch();
const page = await browser.newPage({ viewport: { width: 1600, height: 900 } });
page.on('pageerror', (err) => console.error('PAGEERROR', err.message));
// 01 — before Stage 1 (pre-change golden snapshot from this environment)
copyFileSync(
resolve(root, 'e2e/visual.spec.ts-snapshots/01-home-idle-hud-chromium-linux.png'),
resolve(OUT, '01-before-stage1-overview.png'),
);
console.log('captured 01-before-stage1-overview.png (pre-Stage-1 golden snapshot)');
// 02 — overview with real products
await page.goto(`${BASE}/`, { waitUntil: 'domcontentloaded' });
await page.waitForSelector('canvas', { timeout: 60000 });
await page.evaluate(() => document.fonts.ready);
await page.getByTestId('demo-speed-2').click();
await jumpCase(page, 0);
await page.getByTestId('demo-category').waitFor({ timeout: 30000 });
await page.waitForTimeout(2500);
await snap(page, '02-real-products-overview.png');
// Route shots (B/C/D) with the real item paused mid-scene
const routes = [
['12-route-b-real-item.png', 0, 'B'],
['13-route-c-real-item.png', 2, 'C'],
['14-route-d-real-item.png', 4, 'D'],
];
for (const [name, idx, cat] of routes) {
await jumpCase(page, idx);
await page.getByTestId('demo-category').waitFor({ timeout: 30000 });
const pause = page.getByTestId('demo-pause');
if (await pause.isVisible().catch(() => false)) await pause.click();
await page.waitForTimeout(800);
await snap(page, name);
console.log(`route ${cat} done`);
}
// Verification mode close-ups
async function verifyShot(name, sku, caseIdx) {
await page.goto(`${BASE}/?stage1=1&verify=real-models&sku=${sku}`, { waitUntil: 'domcontentloaded' });
await page.waitForSelector('canvas', { timeout: 60000 });
await page.getByTestId('demo-play').first().click().catch(() => undefined);
await jumpCase(page, caseIdx);
await page.locator(`text=STAGE1 VERIFY · ${sku}`).waitFor({ timeout: 30000 });
await page.waitForTimeout(2000);
await snap(page, name);
}
await verifyShot('03-real-box-300.png', 'SKU-001', 0);
await verifyShot('04-real-box-400.png', 'SKU-004', 2);
await verifyShot('05-real-bottle.png', 'SKU-007', 5);
await verifyShot('06-real-plate.png', 'SKU-006', 4);
await verifyShot('07-real-lunchbox.png', 'SKU-002', 1);
await verifyShot('08-real-oversized-round.png', 'SKU-011', 6);
// SKU-005/SKU-008 are not in the demo playlist; verify overlays only appear for playing items.
await verifyShot('10-dimension-verification.png', 'SKU-006', 4); // plate: bbox + dims card
await verifyShot('11-pivot-and-contact-plane.png', 'SKU-009', 3); // pen: lying pivot + contact plane
// 09 — conveyor conversion blocked (honest technical evidence, not a misleading render)
await page.goto(`${BASE}/`, { waitUntil: 'domcontentloaded' });
await page.waitForSelector('canvas', { timeout: 60000 });
await page.waitForTimeout(2500);
await snap(page, '09-conveyor-conversion-blocked.png');
// 15 — /details with real item
await page.goto(`${BASE}/details`, { waitUntil: 'domcontentloaded' });
await page.waitForSelector('canvas', { timeout: 60000 });
await page.getByRole('button', { name: /start demo/i }).first().click();
await page.waitForTimeout(6000);
await page.locator('canvas').first().scrollIntoViewIfNeeded();
await page.waitForTimeout(500);
await snap(page, '15-details-real-item.png');
// 16 — mobile SVG fallback after Stage 1 (viewport-driven, no special params)
const mob = await browser.newPage({ viewport: { width: 390, height: 844 }, isMobile: true, hasTouch: true });
await mob.goto(`${BASE}/`, { waitUntil: 'domcontentloaded' });
await mob.getByTestId('main-svg-fallback').waitFor({ timeout: 30000 });
await mob.waitForTimeout(1500);
const mobPath = resolve(OUT, '16-mobile-svg-fallback-after-stage1.png');
await mob.screenshot({ path: mobPath });
shots.push('16-mobile-svg-fallback-after-stage1.png');
console.log('captured 16-mobile-svg-fallback-after-stage1.png');
await browser.close();
console.log(`done: ${shots.length + 1} screenshots in ${OUT}`);

View File

@@ -0,0 +1,215 @@
#!/usr/bin/env node
/**
* Stage 1 — automated real-model validation.
*
* Reads the manifest (src/data/modelAssets.ts, transpiled with the project's
* typescript dep), then for every SKU independently:
* - checks the runtime file exists (size + SHA-256 vs manifest)
* - parses the STL, applies manifest rotation + uniform mm scale
* - measures the world-axis bounding box
* - compares against worldExpectedMm with tolerance max(2mm, 1%) per axis
* - checks triangle/file budgets
* - verifies honest marking for SKUs without an official model
*
* Output: docs/stage1_real_models/real-models-validation.json
* Exit code: non-zero on any critical failure. Never hand-edited results.
*/
import { readFileSync, writeFileSync, mkdirSync, existsSync } from 'node:fs';
import { createHash } from 'node:crypto';
import { fileURLToPath } from 'node:url';
import { dirname, resolve } from 'node:path';
import ts from 'typescript';
const ROOT = resolve(dirname(fileURLToPath(import.meta.url)), '..');
const OUT_PATH = resolve(ROOT, 'docs/stage1_real_models/real-models-validation.json');
// ---------- manifest loading (TS → ESM via typescript transpile) ----------
async function loadManifest() {
const src = readFileSync(resolve(ROOT, 'src/data/modelAssets.ts'), 'utf8');
const js = ts.transpileModule(src, {
compilerOptions: { module: ts.ModuleKind.ES2022, target: ts.ScriptTarget.ES2022 },
}).outputText;
const tmp = resolve(ROOT, 'node_modules/.cache/modelAssets.manifest.mjs');
mkdirSync(dirname(tmp), { recursive: true });
writeFileSync(tmp, js);
const mod = await import(`file://${tmp}?t=${Date.now()}`);
return { MODEL_ASSETS: mod.MODEL_ASSETS, ARCHIVE_ONLY_MODELS: mod.ARCHIVE_ONLY_MODELS };
}
// ---------- STL parsing + transform measurement ----------
function readStlVertices(path) {
const buf = readFileSync(path);
const isAscii = buf.subarray(0, 5).toString('ascii').toLowerCase() === 'solid'
&& !(buf.length >= 84 && 84 + buf.readUInt32LE(80) * 50 === buf.length);
const verts = [];
if (isAscii) {
const re = /vertex\s+([-\d.eE+]+)\s+([-\d.eE+]+)\s+([-\d.eE+]+)/g;
const text = buf.toString('ascii');
let m;
while ((m = re.exec(text)) !== null) verts.push([+m[1], +m[2], +m[3]]);
return { verts, triCount: verts.length / 3, bytes: buf.length };
}
const count = buf.readUInt32LE(80);
for (let i = 0; i < count; i++) {
const off = 84 + i * 50 + 12;
for (let v = 0; v < 3; v++) {
verts.push([buf.readFloatLE(off + v * 12), buf.readFloatLE(off + v * 12 + 4), buf.readFloatLE(off + v * 12 + 8)]);
}
}
return { verts, triCount: count, bytes: buf.length };
}
/** Apply manifest rotation (X→Y→Z order, same as three.js geometry.rotate*) */
function rotatePoint([x, y, z], [rx, ry, rz]) {
if (rx) {
const c = Math.cos(rx), s = Math.sin(rx);
[y, z] = [y * c - z * s, y * s + z * c];
}
if (ry) {
const c = Math.cos(ry), s = Math.sin(ry);
[x, z] = [x * c + z * s, -x * s + z * c];
}
if (rz) {
const c = Math.cos(rz), s = Math.sin(rz);
[x, y] = [x * c - y * s, x * s + y * c];
}
return [x, y, z];
}
function sha256(path) {
return createHash('sha256').update(readFileSync(path)).digest('hex');
}
const TOL_ABS_MM = 2;
const TOL_REL = 0.01;
const FILE_HARD_LIMIT = 1.5 * 1024 * 1024;
const TRI_ACCEPTABLE = 100_000;
const r2 = (n) => Math.round(n * 100) / 100;
const { MODEL_ASSETS, ARCHIVE_ONLY_MODELS } = await loadManifest();
const results = [];
let failures = 0;
for (const asset of MODEL_ASSETS) {
const base = {
sku: asset.itemId,
model: asset.displayName,
source: asset.sourceFile ? `${asset.sourceArchive} → ${asset.sourceFile}` : null,
sourceSha256: asset.sourceSha256,
runtimePath: asset.runtimePath,
format: asset.runtimeFormat,
expectedDimensionsMm: asset.dimensions,
axisMapping: asset.axisMapping,
rotation: asset.rotation,
pivot: asset.pivotMode,
defaultRealAsset: asset.defaultRealAsset,
};
if (!asset.defaultRealAsset) {
const honest = (asset.notes ?? '').includes('NO_EXACT_OFFICIAL_MODEL');
if (!honest) failures++;
results.push({
...base,
status: honest ? 'PASS' : 'FAIL',
mode: 'procedural-fallback',
honestMarking: honest,
withinTolerance: null,
});
continue;
}
const diskPath = resolve(ROOT, 'public', asset.runtimePath.replace(/^\//, ''));
const record = { ...base, mode: 'real-model' };
if (!existsSync(diskPath)) {
failures++;
results.push({ ...record, status: 'FAIL', error: 'RUNTIME_FILE_MISSING' });
continue;
}
const actualSha = sha256(diskPath);
record.checksumMatch = actualSha === asset.runtimeSha256;
const { verts, triCount, bytes } = readStlVertices(diskPath);
record.fileSizeBytes = bytes;
record.triangleCount = triCount;
record.fileSizeMatchesManifest = bytes === asset.fileSizeBytes;
record.triangleCountMatchesManifest = triCount === asset.triangleCount;
// world-space measurement after manifest rotation (mm)
const min = [Infinity, Infinity, Infinity];
const max = [-Infinity, -Infinity, -Infinity];
for (const p of verts) {
const w = rotatePoint(p, asset.rotation);
for (let a = 0; a < 3; a++) {
if (w[a] < min[a]) min[a] = w[a];
if (w[a] > max[a]) max[a] = w[a];
}
}
const measuredMm = { x: max[0] - min[0], y: max[1] - min[1], z: max[2] - min[2] };
const expected = asset.worldExpectedMm;
const deviationMm = {};
const deviationPercent = {};
let withinTolerance = true;
for (const axis of ['x', 'y', 'z']) {
const dev = Math.abs(measuredMm[axis] - expected[axis]);
const tol = Math.max(TOL_ABS_MM, expected[axis] * TOL_REL);
deviationMm[axis] = r2(dev);
deviationPercent[axis] = r2((dev / expected[axis]) * 100);
if (dev > tol) withinTolerance = false;
}
record.expectedWorldMm = expected;
record.measuredDimensionsMm = { x: r2(measuredMm.x), y: r2(measuredMm.y), z: r2(measuredMm.z) };
record.deviationMm = deviationMm;
record.deviationPercent = deviationPercent;
record.uniformScale = 1.0; // mm→m uniform only; no fitting scale
record.withinTolerance = withinTolerance;
record.budget = {
fileUnderHardLimit: bytes <= FILE_HARD_LIMIT,
trianglesUnderAcceptable: triCount <= TRI_ACCEPTABLE,
};
const pass = record.checksumMatch
&& record.fileSizeMatchesManifest
&& record.triangleCountMatchesManifest
&& withinTolerance
&& record.budget.fileUnderHardLimit
&& record.budget.trianglesUnderAcceptable;
if (!pass) failures++;
record.status = pass ? 'PASS' : 'FAIL';
results.push(record);
}
const summary = {
generatedAt: new Date().toISOString(),
toleranceRule: 'max(2mm, 1%) per axis, uniform scale only',
budgets: { fileHardLimitBytes: FILE_HARD_LIMIT, trianglesAcceptable: TRI_ACCEPTABLE },
totals: {
skus: MODEL_ASSETS.length,
realAssets: results.filter((r) => r.mode === 'real-model').length,
proceduralFallbacks: results.filter((r) => r.mode === 'procedural-fallback').length,
pass: results.filter((r) => r.status === 'PASS').length,
fail: results.filter((r) => r.status === 'FAIL').length,
},
archiveOnlyModels: ARCHIVE_ONLY_MODELS.map((m) => ({
model: m.displayName,
source: `${m.sourceArchive} → ${m.sourceFile}`,
sourceSha256: m.sourceSha256,
note: m.notes,
})),
};
mkdirSync(dirname(OUT_PATH), { recursive: true });
writeFileSync(OUT_PATH, JSON.stringify({ summary, results }, null, 2) + '\n');
console.log(JSON.stringify(summary.totals));
for (const r of results) {
const tag = r.status === 'PASS' ? 'PASS' : 'FAIL';
const dims = r.measuredDimensionsMm
? ` measured=${r.measuredDimensionsMm.x}/${r.measuredDimensionsMm.y}/${r.measuredDimensionsMm.z}mm`
: '';
console.log(`${tag} ${r.sku}${dims}${r.error ? ' ' + r.error : ''}`);
}
if (failures > 0) {
console.error(`\n${failures} critical failure(s)`);
process.exit(1);
}
console.log('\nAll validations passed.');