Repository navigation
Expand file tree
/
Copy pathshots.js
More file actions
101 lines (92 loc) · 5.03 KB
/
Copy pathshots.js
File metadata and controls
101 lines (92 loc) · 5.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/* Captures the README stills. Frame rate does not matter for a still, so the
adaptive quality controller is pinned to its top tier: the trace runs at
native resolution with the full step count and every noise octave enabled,
then the temporal accumulation is given enough jittered frames to converge
before the shot is taken. Camera drift is disabled so framing is exact.
usage: node shots.js <file> <outDir> [format] [quality] */
const { spawn } = require('child_process');
const fs = require('fs'), path = require('path'), os = require('os');
const FILE = process.argv[2] || 'gargantua.html';
const OUTDIR = process.argv[3] || 'screenshots';
const FMT = process.argv[4] || 'jpeg';
const QUAL = parseInt(process.argv[5] || '96', 10);
const W = 1792, H = 1008;
const SETTLE = 30; /* accumulation frames per shot */
const DEG = Math.PI / 180;
/* az, elevation in degrees, distance in M, and the simulation time to hold at */
const SHOTS = [
{ name: '01-hero', az: 2.05, el: 7.0, dist: 40, t: 52 },
{ name: '02-edge-on', az: 3.85, el: 1.8, dist: 47, t: 61 },
{ name: '03-inclined', az: 1.15, el: 15.0, dist: 76, t: 47 },
{ name: '04-photon-ring', az: 5.10, el: 4.5, dist: 62, t: 55 },
{ name: '05-wide', az: 0.62, el: 8.0, dist: 190, t: 58 },
{ name: '06-face', az: 4.40, el: 56.0, dist: 98, t: 66 }
];
const CHROME = 'C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe';
const PORT = 9450 + (Date.now() % 120);
const prof = fs.mkdtempSync(path.join(os.tmpdir(), 'bhshot-'));
const ch = spawn(CHROME, ['--headless=new', `--remote-debugging-port=${PORT}`,
`--user-data-dir=${prof}`, '--no-first-run', '--no-default-browser-check',
'--hide-scrollbars', '--mute-audio', '--use-angle=d3d11',
`--window-size=${W},${H}`, 'about:blank'], { stdio: ['ignore', 'pipe', 'pipe'] });
const sleep = ms => new Promise(r => setTimeout(r, ms));
(async () => {
fs.mkdirSync(OUTDIR, { recursive: true });
let t = null;
for (let i = 0; i < 80 && !t; i++) {
try {
const j = await (await fetch(`http://127.0.0.1:${PORT}/json/list`)).json();
t = j.find(x => x.type === 'page');
} catch (e) {}
if (!t) await sleep(250);
}
const ws = new WebSocket(t.webSocketDebuggerUrl);
let id = 0; const pend = new Map(); const errs = [];
await new Promise(r => { ws.onopen = r; });
ws.onmessage = e => {
const m = JSON.parse(e.data);
if (m.id && pend.has(m.id)) { pend.get(m.id)(m); pend.delete(m.id); }
else if (m.method === 'Runtime.exceptionThrown') errs.push('EXCEPTION ' + JSON.stringify(m.params.exceptionDetails.text));
};
const send = (method, params) => new Promise(res => { const i = ++id; pend.set(i, res); ws.send(JSON.stringify({ id: i, method, params: params || {} })); });
const ev = expr => send('Runtime.evaluate', { expression: expr, returnByValue: true }).then(r => r.result && r.result.result && r.result.result.value);
await send('Runtime.enable');
await send('Page.enable');
let src = fs.readFileSync(FILE, 'utf8');
/* pin the top quality tier and neutralise the adaptive controller */
src = src.replace('var qIdx = 1, qw = 1, qHold = 8,',
'var qIdx = SCALES.length-1, qw = SCALES.length-1, qHold = 1e9,');
src = src.replace('if(qHold > 0) qHold--;', 'if(true){ }');
src = src.replace('qCeil=SCALES.length-1; qHold=40;', 'qCeil=SCALES.length-1; qHold=1e9;');
/* hold framing still */
src = src.replace(' if(cam.idling){', ' if(false){');
src = src.replace(' frame++;', ` frame++;
if(window.__cam){ var c=window.__cam; window.__cam=0;
cam.az=cam.azT=c[0]; cam.el=cam.elT=c[1]; cam.dist=cam.distT=c[2];
simT=c[3]; resetAcc=1; window.__base=frame; }
window.__n=frame-(window.__base||0); window.__rt=RT.w+"x"+RT.h; window.__q=qw;`);
fs.writeFileSync('__shots.html', src);
await send('Page.navigate', { url: 'file:///' + path.resolve('__shots.html').replace(/\\/g, '/') });
await sleep(6000);
const only = process.env.BH_ONLY ? process.env.BH_ONLY.split(',') : null;
for (const s of SHOTS) {
if (only && !only.includes(s.name)) continue;
await ev(`window.__cam=[${s.az},${s.el * DEG},${s.dist},${s.t}]`);
/* wait on frame count, not wall time: top tier frames are slow */
let n = 0;
for (let i = 0; i < 900; i++) {
n = await ev('window.__n||0');
if (n >= SETTLE) break;
await sleep(200);
}
const shot = await send('Page.captureScreenshot',
FMT === 'png' ? { format: 'png' } : { format: 'jpeg', quality: QUAL });
const out = path.join(OUTDIR, s.name + (FMT === 'png' ? '.png' : '.jpg'));
if (shot.result && shot.result.data) fs.writeFileSync(out, Buffer.from(shot.result.data, 'base64'));
const meta = await ev(`window.__rt+' q='+window.__q`);
const kb = (fs.statSync(out).size / 1024).toFixed(0);
console.log(`${out.padEnd(30)} trace=${meta} settled=${n} frames ${kb} KB`);
}
if (errs.length) console.log(errs.join('\n'));
ws.close(); ch.kill(); setTimeout(() => process.exit(0), 300);
})().catch(e => { console.log('ERR ' + e.message); ch.kill(); process.exit(1); });