// Headless renderer for three.js blockouts (macOS Chrome via puppeteer-core, Metal ANGLE: ~20 fps at 1080p on an M2). // // node previz/three/render.mjs stills [page.html] [t1 t2 ...] → out//still_.png // node previz/three/render.mjs frames [page.html] [--fps 30] → out//frames/%04d.jpg (shot range) // node previz/three/render.mjs floor [page.html] → out//floor.png (the painted floor texture) // options: --w 1920 --h 1080 --pass beauty|clay --cam shot|top|side // // Files are served from this folder through request interception (no local server); the page imports // three.js from node_modules with ?local and exposes: // window.__ready = true once assets are loaded; window.SHOT = {t0, t1} (song seconds of the shot); // window.frameAt(t, mimeType) → base64 image of song time t (a pure function of t: any frame renders alone); // window.floorPNG() → base64 (optional, e.g. a painted floor texture to reuse as a reference). // Opened directly in a browser the same page can play along with the song for review. // Install deps locally next to this file (npm install with package_three.json as package.json), never globally. import puppeteer from 'puppeteer-core'; import fs from 'node:fs/promises'; import path from 'node:path'; import { fileURLToPath } from 'node:url'; const ROOT = path.dirname(fileURLToPath(import.meta.url)); const CHROME = process.env.CHROME || '/Applications/Google Chrome.app/Contents/MacOS/Google Chrome'; const MIME = { '.html': 'text/html', '.js': 'text/javascript', '.mjs': 'text/javascript', '.json': 'application/json', '.png': 'image/png', '.jpg': 'image/jpeg', '.m4a': 'audio/mp4', '.wasm': 'application/wasm' }; const args = process.argv.slice(2); const opt = (name, dflt) => { const i = args.indexOf('--' + name); return i >= 0 ? args.splice(i, 2)[1] : dflt; }; const W = +opt('w', 1920), H = +opt('h', 1080), FPS = +opt('fps', 30), PASS = opt('pass', 'beauty'), CAM = opt('cam', 'shot'); const [mode = 'stills', page = 'scene.html', ...times] = args; const outDir = path.join(ROOT, 'out', path.basename(page, '.html') + (PASS === 'beauty' ? '' : '_' + PASS) + (CAM === 'shot' ? '' : '_' + CAM)); const browser = await puppeteer.launch({ executablePath: CHROME, headless: true, args: ['--use-angle=metal', '--enable-gpu', '--ignore-gpu-blocklist', '--hide-scrollbars', '--force-device-scale-factor=1'], }); try { const tab = await browser.newPage(); await tab.setViewport({ width: W, height: H, deviceScaleFactor: 1 }); tab.on('console', m => { if (m.type() === 'error' || m.type() === 'warning') console.log(`[page ${m.type()}]`, m.text()); }); tab.on('pageerror', e => console.error('[page error]', e.message)); await tab.setRequestInterception(true); tab.on('request', async req => { const u = new URL(req.url()); if (u.hostname !== 'blockout.local') return req.continue(); const file = path.join(ROOT, decodeURIComponent(u.pathname)); try { req.respond({ status: 200, contentType: MIME[path.extname(file)] || 'application/octet-stream', body: await fs.readFile(file) }); } catch { req.respond({ status: 404, body: 'not found' }); } }); await tab.goto(`http://blockout.local/${page}?render&local&w=${W}&h=${H}&pass=${PASS}&cam=${CAM}`); await tab.waitForFunction('window.__ready === true', { timeout: 120000 }); const gl = await tab.evaluate(() => { const g = document.querySelector('canvas').getContext('webgl2'); const ext = g.getExtension('WEBGL_debug_renderer_info'); return ext ? g.getParameter(ext.UNMASKED_RENDERER_WEBGL) : 'unknown'; }); console.log('GL renderer:', gl); const shot = await tab.evaluate(() => window.SHOT); const grab = async (t, file, type) => { const b64 = await tab.evaluate((t, type) => window.frameAt(t, type), t, type); await fs.writeFile(file, Buffer.from(b64, 'base64')); }; if (mode === 'floor') { await fs.mkdir(outDir, { recursive: true }); await fs.writeFile(path.join(outDir, 'floor.png'), Buffer.from(await tab.evaluate(() => window.floorPNG()), 'base64')); console.log(path.join(outDir, 'floor.png')); } else if (mode === 'stills') { await fs.mkdir(outDir, { recursive: true }); const list = times.length ? times.map(Number) : [shot.t0]; for (const t of list) { const file = path.join(outDir, `still_${t.toFixed(3)}.png`); await grab(t, file, 'image/png'); console.log(file); } } else if (mode === 'frames') { const dir = path.join(outDir, 'frames'); await fs.rm(dir, { recursive: true, force: true }); await fs.mkdir(dir, { recursive: true }); const n = Math.round(shot.t1 * FPS) - Math.round(shot.t0 * FPS); // same frame grid as roughcut.py const t0 = Math.round(shot.t0 * FPS) / FPS; const start = Date.now(); for (let i = 0; i < n; i++) { await grab(t0 + i / FPS, path.join(dir, String(i).padStart(4, '0') + '.jpg'), 'image/jpeg'); if (i % 20 === 0) console.log(`frame ${i}/${n} ${((Date.now() - start) / 1000).toFixed(1)} s`); } console.log(`${n} frames → ${dir} (t0 ${t0.toFixed(4)}, ${FPS} fps)`); } } finally { await browser.close(); }