Files
CyberGril/experiments/hel-702-3d-verify/capture/capture.mjs
T

141 lines
7.4 KiB
JavaScript

/* HEL-702 证据采集:headless Chromium (SwiftShader) 驱动验证页,截图 + 录屏 + 运行数据 */
import { chromium } from 'playwright';
import http from 'node:http';
import fs from 'node:fs';
import path from 'node:path';
const ROOT = path.resolve(path.dirname(new URL(import.meta.url).pathname), '..');
const PUB = path.join(ROOT, 'public');
const PORT = 8931;
const MIME = { '.html': 'text/html', '.js': 'text/javascript', '.json': 'application/json', '.vrm': 'model/gltf-binary', '.vrma': 'model/gltf-binary', '.png': 'image/png' };
const server = http.createServer((req, res) => {
const url = req.url.split('?')[0];
let p;
if (url === '/' || url === '/index.html') p = path.join(ROOT, 'public', 'index.html');
else if (url.startsWith('/assets/')) p = path.join(ROOT, url.slice(1));
else if (url === '/bundle.js') p = path.join(ROOT, 'public', 'bundle.js');
else { res.writeHead(404); res.end('nf'); return; }
fs.readFile(p, (e, buf) => {
if (e) { res.writeHead(404); res.end('nf'); return; }
res.writeHead(200, { 'content-type': MIME[path.extname(p)] || 'application/octet-stream' });
res.end(buf);
});
});
await new Promise((r) => server.listen(PORT, r));
const SHOT = process.argv.includes('--shot'); // 单镜头模式: --shot state=json cam=name out=xx.png
const arg = (k) => process.argv.slice(2).find((a) => a.startsWith(k + '='))?.slice(k.length + 1);
const outDir = path.join(ROOT, 'evidence', 'shots');
fs.mkdirSync(outDir, { recursive: true });
const browser = await chromium.launch({ args: ['--use-angle=swiftshader', '--enable-unsafe-swiftshader'] });
async function session({ video = false } = {}) {
const ctx = await browser.newContext({
viewport: { width: 960, height: 540 },
recordVideo: video ? { dir: path.join(ROOT, 'evidence', '_video'), size: { width: 960, height: 540 } } : undefined,
});
const page = await ctx.newPage();
page.on('console', (m) => { if (m.type() === 'error') console.error('[page err]', m.text()); });
page.on('pageerror', (e) => console.error('[page throw]', e.message, (e.stack?.split('\n').slice(1, 8).join('\n') || '')));
return { ctx, page };
}
const sleep = (ms) => new Promise((r) => setTimeout(r, ms));
if (SHOT) {
const { ctx, page } = await session();
const state = JSON.parse(arg('state') || '{}');
const cam = arg('cam') || 'main';
const model = arg('model') || 'twist';
const out = path.join(outDir, arg('out') || 'shot.png');
await page.goto(`http://localhost:${PORT}/?cam=${cam}&model=${model}`);
await page.waitForFunction(() => window.__app?.ready === true, null, { timeout: 60000 });
await sleep(600);
await page.evaluate((s) => window.__app.setState(s), state);
await sleep(arg('wait') ? parseInt(arg('wait')) : 1600);
await page.screenshot({ path: out });
const stats = await page.evaluate(() => JSON.parse(JSON.stringify(window.__stats)));
const probe = await page.evaluate(() => window.__app.probe());
console.log('shot saved:', out);
console.log('probe:', JSON.stringify(probe));
console.log('stats:', JSON.stringify({ fps: stats.fps, loadMs: stats.loadMs, info: stats.info, meta: stats.modelMeta }));
await ctx.close();
} else {
/* 完整证据序列(含录屏) */
const { ctx, page } = await session({ video: true });
await page.goto(`http://localhost:${PORT}/?cam=main&model=twist`);
await page.waitForFunction(() => window.__app?.ready === true, null, { timeout: 60000 });
await sleep(1200);
const cap = async (name, state, wait = 1500, caption) => {
await page.evaluate((c) => { const el = document.getElementById('caption'); el.textContent = c; el.style.opacity = 1; }, caption || '');
await page.evaluate((s) => window.__app.setState(s), state);
await sleep(wait);
await page.screenshot({ path: path.join(outDir, name) });
const probe = await page.evaluate(() => window.__app.probe());
globalProbes[name] = probe;
console.log('captured', name, JSON.stringify(probe));
};
const globalProbes = {};
await cap('01-meta-stand-day-sunny.png', { outfit: 'A', action: 'stand', time: 'day', weather: 'sunny', gaze: 'idle' }, 1200,
'① VRM 实时加载 · 站立 · 白天/晴(HUD 显示模型内嵌授权元数据)');
await cap('02-outfitA-front.png', { outfit: 'A', action: 'stand' }, 1500, '② 服装A:原始 Tops+Bottoms+Shoes');
await page.evaluate(() => window.__app.snapshotPose());
await cap('03-outfitB-front.png', { outfit: 'B', action: 'stand' }, 1800, '③ 服装B:自制连帽衫+长裤(不同几何,非改色)· 骨架未动');
await cap('04-sit-day.png', { action: 'sit' }, 2000, '④ 坐下:hips 对齐椅面高度 0.46m,过渡平滑');
await cap('05-read-day.png', { action: 'read', gaze: 'idle' }, 2000, '⑤ 阅读:书本绑定右手原始骨骼,视线落在书页');
await page.evaluate(() => window.__app.snapshotPose());
await cap('06-read-night-rain.png', { time: 'night', weather: 'rain' }, 2500, '⑥ 夜晚+雨:环境切换 · 姿态保持(HUD 显示断言结果)');
const decoupling = await page.evaluate(() => window.__app.assertPosePreserved());
console.log('decoupling assert:', JSON.stringify(decoupling));
await page.screenshot({ path: path.join(outDir, '06b-decouple-assert.png') });
await cap('07-gaze-camera.png', { gaze: 'camera' }, 1500, '⑦ 交互注意力:视线转向镜头 + happy 表情');
await cap('08-vrma-play.png', { action: 'vrma', gaze: 'idle' }, 2500, '⑧ 播放标准 VRMA 动画文件(跨模型重定向)');
await page.screenshot({ path: path.join(outDir, '08b-vrma-mid.png') });
/* 姿态保持 + 运行数据 */
const stats = await page.evaluate(() => JSON.parse(JSON.stringify(window.__stats)));
/* fps 稳态测量:连续采样 6s */
const fps = await page.evaluate(async () => {
await new Promise((r) => setTimeout(r, 6000));
return { fps: window.__stats.fps, info: window.__stats.info, heapMB: window.__stats.heapMB ?? null };
});
stats.steady = fps;
stats.decouplingAssert = decoupling;
stats.userAgent = await page.evaluate(() => navigator.userAgent);
stats.environment = 'headless chromium + SwiftShader software WebGL2 (无GPU沙箱的下界测量)';
stats.probes = globalProbes;
fs.writeFileSync(path.join(ROOT, 'evidence', 'runtime-stats.json'), JSON.stringify(stats, null, 2));
console.log('stats saved');
const video = page.video();
const vidPath = await video.path();
await ctx.close(); /* finalizes video */
if (vidPath && fs.existsSync(vidPath)) {
const dest = path.join(ROOT, 'evidence', 'hel-702-verify-demo.webm');
fs.copyFileSync(vidPath, dest);
console.log('video saved:', dest);
}
}
/* Seed-san 第二模型验证(管线通用性 + MToon/PBR 混合) */
{
const { ctx, page } = await session();
await page.goto(`http://localhost:${PORT}/?cam=main&model=seed`);
await page.waitForFunction(() => window.__app?.ready === true, null, { timeout: 60000 });
await sleep(1500);
await page.evaluate(() => window.__app.setState({ outfit: 'A', action: 'stand' }));
await sleep(1200);
await page.screenshot({ path: path.join(outDir, '09-seedsan-load.png') });
const meta = await page.evaluate(() => window.__stats.modelMeta);
const load = await page.evaluate(() => window.__stats.loadMs);
fs.writeFileSync(path.join(ROOT, 'evidence', 'seed-san-runtime.json'), JSON.stringify({ meta, loadMs: load }, null, 2));
console.log('seed-san ok:', JSON.stringify(meta), 'loadMs', load);
await ctx.close();
}
await browser.close();
server.close();