-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcapture-fog-density-rebalance-shots.ts
More file actions
330 lines (291 loc) · 10.8 KB
/
Copy pathcapture-fog-density-rebalance-shots.ts
File metadata and controls
330 lines (291 loc) · 10.8 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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
#!/usr/bin/env tsx
// SPDX-License-Identifier: AGPL-3.0-or-later
// Copyright (c) 2025-2026 Matthew Kissinger
/**
* Capture the 5 screenshot-gate PNGs for the `fog-density-rebalance` PR.
* See `docs/tasks/fog-density-rebalance.md` ("Screenshot evidence") for
* the required shots and the orchestrator's visual review contract.
*
* Usage:
* npx tsx scripts/capture-fog-density-rebalance-shots.ts
*
* Reuses the preview-server + engine-pose infrastructure pioneered by
* `scripts/capture-hosek-wilkie-shots.ts` and
* `scripts/capture-fog-tinted-by-sky-shots.ts`. The clear-weather framings
* diff directly against the post-tone-mapping-aces baseline captured in
* `docs/cycles/cycle-2026-04-21-atmosphere-polish-and-fixes/screenshots/post-tone-mapping-aces/`.
*
* Required outputs (written to this task's screenshot dir):
* - combat120-noon.png (ai_sandbox, clear)
* - ashau-dawn.png (a_shau_valley, clear)
* - openfrontier-noon.png (open_frontier, clear)
* - combat120-storm.png (ai_sandbox, STORM forced on)
* - combat120-underwater.png (ai_sandbox, underwater override)
*/
import { chromium, type Page } from 'playwright';
import { existsSync, mkdirSync, writeFileSync } from 'fs';
import { join } from 'path';
import { startServer, stopServer, type ServerHandle } from './preview-server';
type Pose = {
position: [number, number, number];
yawDeg: number;
pitchDeg: number;
};
type ShotPlan = {
filename: string;
mode: string;
pose: Pose;
description: string;
settleSec: number;
override?: 'storm' | 'underwater';
};
const PORT = 9104;
const VIEWPORT = { width: 1920, height: 1080 };
const STARTUP_TIMEOUT_MS = 90_000;
const OUTPUT_DIR = join(
process.cwd(),
'docs',
'cycles',
'cycle-2026-04-21-atmosphere-polish-and-fixes',
'screenshots',
'fog-density-rebalance'
);
function logStep(msg: string): void {
console.log(`[${new Date().toISOString()}] ${msg}`);
}
function poseTowardSun(azimuthRad: number, position: [number, number, number], pitchDeg: number): Pose {
const sx = Math.cos(azimuthRad);
const sz = Math.sin(azimuthRad);
const yawRad = Math.atan2(sx, -sz);
return { position, yawDeg: (yawRad * 180) / Math.PI, pitchDeg };
}
/**
* Camera framings match `capture-hosek-wilkie-shots.ts` byte-for-byte for
* the three clear-weather shots so the reviewer can diff
* `post-tone-mapping-aces/<shot>.png` against `fog-density-rebalance/<shot>.png`
* at identical framings. The storm + underwater shots reuse combat120's
* framing so weather-multiplier + underwater-override effects land in the
* same frame coordinates.
*/
function shotPlans(): ShotPlan[] {
return [
{
filename: 'combat120-noon',
mode: 'ai_sandbox',
pose: poseTowardSun(Math.PI * 0.25, [0, 80, 0], 20),
description: 'combat120 noon — distant terrain reads as terrain in fog, not flat white',
settleSec: 6,
},
{
filename: 'ashau-dawn',
mode: 'a_shau_valley',
pose: poseTowardSun(Math.PI * 0.15, [0, 300, 0], 5),
description: 'A Shau dawn — ridgelines visible through thin warm haze',
settleSec: 8,
},
{
filename: 'openfrontier-noon',
mode: 'open_frontier',
pose: poseTowardSun(Math.PI * 0.25, [0, 120, 0], 25),
description: 'Open Frontier noon — distant terrain visible, sky gradient clean',
settleSec: 6,
},
{
filename: 'combat120-storm',
mode: 'ai_sandbox',
pose: poseTowardSun(Math.PI * 0.25, [0, 80, 0], 20),
description: 'combat120 with storm forced on — fog visibly thicker than clear (x3.5 multiplier)',
settleSec: 6,
override: 'storm',
},
{
filename: 'combat120-underwater',
mode: 'ai_sandbox',
pose: poseTowardSun(Math.PI * 0.25, [0, 80, 0], 20),
description: 'combat120 underwater — fog snaps to teal (0x003344, density 0.04)',
settleSec: 6,
override: 'underwater',
},
];
}
async function waitForEngine(page: Page): Promise<void> {
await page.waitForFunction(
() => Boolean((window as any).__engine),
undefined,
{ timeout: STARTUP_TIMEOUT_MS }
);
}
async function startMode(page: Page, mode: string): Promise<void> {
logStep(`Starting mode ${mode}`);
await page.evaluate(async (m: string) => {
const engine = (window as any).__engine;
if (!engine?.startGameWithMode) throw new Error('engine.startGameWithMode unavailable');
await engine.startGameWithMode(m);
}, mode);
const deadline = Date.now() + 60_000;
while (Date.now() < deadline) {
const state = await page.evaluate(() => {
const e = (window as any).__engine;
return {
gameStarted: Boolean(e?.gameStarted),
phase: String(e?.startupFlow?.getState?.()?.phase ?? ''),
};
});
if (state.gameStarted || state.phase === 'live') return;
await page.waitForTimeout(250);
}
throw new Error(`Mode ${mode} did not enter live phase`);
}
async function dismissBriefingIfPresent(page: Page): Promise<void> {
const beginBtn = page.locator('[data-ref="beginBtn"]');
try {
if (await beginBtn.isVisible({ timeout: 1500 })) {
await beginBtn.click();
logStep('Dismissed mission briefing');
await page.waitForTimeout(500);
}
} catch {
// not present, fine
}
}
async function applyOverride(page: Page, override: 'storm' | 'underwater' | undefined): Promise<void> {
if (!override) return;
await page.evaluate((kind: 'storm' | 'underwater') => {
const engine = (window as any).__engine;
const weather = engine?.systemManager?.weatherSystem;
if (!weather) throw new Error('weatherSystem unavailable');
if (kind === 'storm') {
// The ai_sandbox mode does not enable weather config, so we have to
// prime the weather system with a minimal enabled config before
// calling setWeatherState; otherwise `update()` returns early and
// the storm darken factor never propagates to the atmosphere system.
weather.setWeatherConfig({
enabled: true,
initialState: 'clear',
transitionChance: 0,
cycleDuration: { min: 999, max: 999 },
});
weather.setWeatherState('storm', true);
} else if (kind === 'underwater') {
weather.setUnderwater(true);
}
}, override);
// Let a few frames pass so the weather/atmosphere state settles.
await page.waitForTimeout(500);
}
async function poseAndRender(page: Page, pose: Pose, viewport: { width: number; height: number }): Promise<void> {
await page.evaluate(
({ p, vp }: { p: Pose; vp: { width: number; height: number } }) => {
const engine = (window as any).__engine;
const renderer = engine?.renderer;
const camera = renderer?.camera;
const threeRenderer = renderer?.renderer;
const scene = renderer?.scene;
const pp = renderer?.postProcessing;
if (!engine || !camera || !threeRenderer || !scene) {
throw new Error('engine/camera/renderer/scene unavailable');
}
// Tick the atmosphere one more time with the current renderer so
// the fog-color path runs against the (possibly overridden) weather
// state before we freeze the RAF for the snap.
try {
engine.systemManager?.atmosphereSystem?.update?.(0);
} catch {
// non-fatal
}
engine.isLoopRunning = false;
if (engine.animationFrameId !== null && engine.animationFrameId !== undefined) {
cancelAnimationFrame(engine.animationFrameId);
engine.animationFrameId = null;
}
threeRenderer.setSize(vp.width, vp.height, true);
if (pp && typeof pp.setSize === 'function') pp.setSize(vp.width, vp.height);
if (typeof camera.aspect === 'number') {
camera.aspect = vp.width / vp.height;
if (typeof camera.updateProjectionMatrix === 'function') camera.updateProjectionMatrix();
}
camera.position.set(p.position[0], p.position[1], p.position[2]);
const yawRad = (p.yawDeg * Math.PI) / 180;
const pitchRad = (p.pitchDeg * Math.PI) / 180;
camera.rotation.order = 'YXZ';
camera.rotation.set(pitchRad, yawRad, 0);
camera.updateMatrixWorld(true);
const atm = engine.systemManager?.atmosphereSystem;
if (atm && typeof atm.syncDomePosition === 'function') {
atm.syncDomePosition(camera.position);
}
if (pp && typeof pp.beginFrame === 'function') pp.beginFrame();
threeRenderer.render(scene, camera);
if (pp && typeof pp.endFrame === 'function') pp.endFrame();
},
{ p: pose, vp: viewport }
);
}
async function snap(page: Page, outFile: string): Promise<void> {
const buffer = await page.screenshot({ type: 'png', fullPage: false });
writeFileSync(outFile, buffer);
logStep(`Wrote ${outFile} (${buffer.byteLength} bytes)`);
}
async function captureScenario(page: Page, plan: ShotPlan): Promise<void> {
await startMode(page, plan.mode);
await dismissBriefingIfPresent(page);
logStep(`Settling ${plan.settleSec}s for ${plan.mode}`);
await page.waitForTimeout(plan.settleSec * 1000);
await applyOverride(page, plan.override);
await page.addStyleTag({
content: `
body > *:not(canvas) { display: none !important; }
canvas { position: fixed !important; inset: 0 !important; }
`,
});
await poseAndRender(page, plan.pose, VIEWPORT);
const outFile = join(OUTPUT_DIR, `${plan.filename}.png`);
await snap(page, outFile);
await page.evaluate(() => {
const engine = (window as any).__engine;
if (engine && typeof engine.start === 'function') engine.start();
});
}
async function main(): Promise<void> {
logStep('Capturing fog-density-rebalance screenshots');
if (!existsSync(OUTPUT_DIR)) {
mkdirSync(OUTPUT_DIR, { recursive: true });
logStep(`Created ${OUTPUT_DIR}`);
}
let server: ServerHandle | null = null;
try {
server = await startServer({
mode: 'perf',
port: PORT,
buildIfMissing: false,
log: logStep,
});
const browser = await chromium.launch({ headless: true });
const context = await browser.newContext({ viewport: VIEWPORT });
const page = await context.newPage();
page.on('console', (msg) => {
if (msg.type() === 'error') {
console.error(`[browser] ${msg.text()}`);
}
});
const url = `http://127.0.0.1:${PORT}/?perf=1&uiTransitions=0`;
logStep(`Navigating to ${url}`);
await page.goto(url, { waitUntil: 'load', timeout: STARTUP_TIMEOUT_MS });
await waitForEngine(page);
for (const plan of shotPlans()) {
try {
await captureScenario(page, plan);
} catch (err) {
console.error(`Failed scenario ${plan.filename}:`, err);
}
}
await context.close();
await browser.close();
} finally {
if (server) await stopServer(server);
}
}
main().catch((err) => {
console.error('capture-fog-density-rebalance-shots failed:', err);
process.exit(1);
});