roamwright 0.61.4 → 0.61.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/examples/animals.js +6 -4
- package/examples/crowds.js +4 -3
- package/examples/effects.js +7 -6
- package/examples/figure-motion.js +7 -5
- package/examples/fliers.js +3 -4
- package/examples/fortification.js +4 -3
- package/examples/house-kit.js +9 -3
- package/examples/pacing.js +2 -2
- package/examples/rooms.js +13 -8
- package/examples/ships.js +2 -2
- package/examples/small-world-land.js +5 -6
- package/examples/small-world-town.js +9 -3
- package/examples/soundscape.js +6 -6
- package/examples/towns.js +2 -2
- package/geometry/animal-canine-breeds.js +152 -7
- package/geometry/animal-canine-head.js +66 -9
- package/geometry/animal-ear.js +74 -12
- package/geometry/fort-city-walls.js +10 -8
- package/geometry/scatter-instances.js +103 -0
- package/geometry/timber-fence.js +117 -0
- package/index.js +4 -0
- package/package.json +1 -1
- package/rendering/particles.js +78 -1
- package/rendering/scatter-instances.js +246 -0
- package/tools/README.md +1 -0
- package/tools/beasts-near.cjs +498 -0
- package/tools/beasts-near.test.cjs +105 -0
- package/examples/stage-kits.js +0 -172
|
@@ -0,0 +1,498 @@
|
|
|
1
|
+
// Generic close-up animal inspector built on a Roamwright page driver (`page-driver.cjs`),
|
|
2
|
+
// scene playback helpers (`scene-run.cjs`), and contact sheets (`contact-sheet.cjs`).
|
|
3
|
+
//
|
|
4
|
+
// Opens a world at configured places or scenes where each animal kind lives, raises nearby
|
|
5
|
+
// herds and figures at once rather than waiting for streaming, freezes daylight, and takes
|
|
6
|
+
// eye-height side, front, and three-quarter close-ups — plus optional scene beat captures.
|
|
7
|
+
'use strict';
|
|
8
|
+
const fs = require('fs');
|
|
9
|
+
const path = require('path');
|
|
10
|
+
const { slugify } = require('./stills.cjs');
|
|
11
|
+
const { writeContactSheet } = require('./contact-sheet.cjs');
|
|
12
|
+
const { readyPageForSceneInPage, seekSceneInPage, startSceneInPage, waitSceneReady } = require('./scene-run.cjs');
|
|
13
|
+
|
|
14
|
+
const DEFAULT_VIEWS = ['side', 'front', 'three'];
|
|
15
|
+
const DEFAULT_HOUR = 0.42; // clear late-morning sun for crisp ear and fur shading
|
|
16
|
+
const DEFAULT_WIDTH = 1200;
|
|
17
|
+
const DEFAULT_HEIGHT = 900;
|
|
18
|
+
const DEFAULT_FOV = 34;
|
|
19
|
+
|
|
20
|
+
const VALUE_FLAGS = {
|
|
21
|
+
'--scene': (out, v) => (out.scene = v),
|
|
22
|
+
'--at': (out, v) => (out.at = Number(v)),
|
|
23
|
+
'--actor': (out, v) => (out.actor = v),
|
|
24
|
+
'--hour': (out, v) => {
|
|
25
|
+
out.hour = Number(v);
|
|
26
|
+
out.hourExplicit = true;
|
|
27
|
+
},
|
|
28
|
+
'--prefix': (out, v) => (out.prefix = v),
|
|
29
|
+
'--width': (out, v) => (out.width = Number(v)),
|
|
30
|
+
'--height': (out, v) => (out.height = Number(v)),
|
|
31
|
+
'--views': (out, v) =>
|
|
32
|
+
(out.views = v
|
|
33
|
+
.split(',')
|
|
34
|
+
.map(s => s.trim())
|
|
35
|
+
.filter(Boolean)),
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Parses CLI arguments for a world's `beasts-near` wrapper.
|
|
40
|
+
*
|
|
41
|
+
* @param {string[]} argv Raw CLI arguments (excluding `node` and script path).
|
|
42
|
+
* @returns {object}
|
|
43
|
+
*/
|
|
44
|
+
function parseBeastsNearArgs(argv) {
|
|
45
|
+
const out = {
|
|
46
|
+
kinds: [],
|
|
47
|
+
scene: '',
|
|
48
|
+
at: null,
|
|
49
|
+
actor: '',
|
|
50
|
+
hour: DEFAULT_HOUR,
|
|
51
|
+
hourExplicit: false,
|
|
52
|
+
prefix: 'beasts-near',
|
|
53
|
+
width: DEFAULT_WIDTH,
|
|
54
|
+
height: DEFAULT_HEIGHT,
|
|
55
|
+
views: DEFAULT_VIEWS.slice(),
|
|
56
|
+
sheet: false,
|
|
57
|
+
beatOnly: false,
|
|
58
|
+
list: false,
|
|
59
|
+
};
|
|
60
|
+
for (let i = 0; i < argv.length; i++) {
|
|
61
|
+
const arg = argv[i];
|
|
62
|
+
if (arg === '--sheet') out.sheet = true;
|
|
63
|
+
else if (arg === '--beat-only') out.beatOnly = true;
|
|
64
|
+
else if (arg === '--list') out.list = true;
|
|
65
|
+
else if (!arg.startsWith('--')) out.kinds.push(arg);
|
|
66
|
+
else {
|
|
67
|
+
const eq = arg.indexOf('=');
|
|
68
|
+
const flag = eq >= 0 ? arg.slice(0, eq) : arg;
|
|
69
|
+
const apply = VALUE_FLAGS[flag];
|
|
70
|
+
if (!apply) throw new Error(`Unknown option: ${arg}`);
|
|
71
|
+
if (eq < 0 && i + 1 >= argv.length) throw new Error(`Missing value for ${flag}`);
|
|
72
|
+
apply(out, eq >= 0 ? arg.slice(eq + 1) : argv[++i]);
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
return out;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* Computes camera position, look-at target, and vertical FOV for a close-up eye-height view
|
|
80
|
+
* (`side`, `front`, or `three`) around a framed animal, picking the sunlit lateral side.
|
|
81
|
+
*
|
|
82
|
+
* @param {object} target Framed beast geometry (`{ origin, minY, height, length, yaw, head, headFocus, zoom }`).
|
|
83
|
+
* @param {'side'|'front'|'three'} view Requested camera angle.
|
|
84
|
+
* @param {number[]|null} [sunDir] Optional unit vector toward the sun `[x, y, z]`.
|
|
85
|
+
* @returns {{ pos: number[], aim: number[], fov: number }}
|
|
86
|
+
*/
|
|
87
|
+
function frameBeastView(target, view, sunDir = null) {
|
|
88
|
+
const height = Math.max(0.15, target.height);
|
|
89
|
+
const length = Math.max(height * 0.9, target.length || height * 1.25);
|
|
90
|
+
const yaw = target.yaw || 0;
|
|
91
|
+
const fx = Math.sin(yaw),
|
|
92
|
+
fz = Math.cos(yaw);
|
|
93
|
+
// Three.js Y-rotation maps local +x to (cos yaw, 0, -sin yaw).
|
|
94
|
+
const rx = Math.cos(yaw),
|
|
95
|
+
rz = -Math.sin(yaw);
|
|
96
|
+
const sideSign = sunDir && sunDir.length >= 3 && rx * sunDir[0] + rz * sunDir[2] < 0 ? -1 : 1;
|
|
97
|
+
const head = target.head || [target.origin[0] + fx * length * 0.28, target.minY + height * 0.78, target.origin[2] + fz * length * 0.28];
|
|
98
|
+
const body = [target.origin[0] + fx * length * 0.1, target.minY + height * 0.6, target.origin[2] + fz * length * 0.1];
|
|
99
|
+
const zoom = target.zoom || 1;
|
|
100
|
+
|
|
101
|
+
if (view === 'front' || view === 'three') {
|
|
102
|
+
const isFront = view === 'front';
|
|
103
|
+
const dist = Math.max(isFront ? 0.32 : 0.36, height * (isFront ? 1.32 : 1.48) * zoom);
|
|
104
|
+
const angle = sideSign * (isFront ? 0.1 : 0.68);
|
|
105
|
+
const dx = fx * Math.cos(angle) + rx * Math.sin(angle);
|
|
106
|
+
const dz = fz * Math.cos(angle) + rz * Math.sin(angle);
|
|
107
|
+
const back = isFront ? 0 : length * 0.05;
|
|
108
|
+
const aim = [head[0] - fx * back, head[1] + height * (isFront ? 0.03 : 0.02), head[2] - fz * back];
|
|
109
|
+
return { pos: [aim[0] + dx * dist, aim[1] + height * (isFront ? 0.04 : 0.06), aim[2] + dz * dist], aim, fov: DEFAULT_FOV };
|
|
110
|
+
}
|
|
111
|
+
const dist = Math.max(0.45, Math.max(height * 1.75, length * 1.05) * zoom);
|
|
112
|
+
const aim = target.headFocus
|
|
113
|
+
? [head[0] - fx * length * 0.08, head[1] - height * 0.04, head[2] - fz * length * 0.08]
|
|
114
|
+
: [body[0], body[1] + height * 0.04, body[2]];
|
|
115
|
+
return { pos: [aim[0] + rx * sideSign * dist, aim[1] + height * 0.03, aim[2] + rz * sideSign * dist], aim, fov: DEFAULT_FOV };
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* Finds a herd matching `build` on the active space and teleports the visitor beside one member
|
|
120
|
+
* so streaming orders and raises the herd at once. Passed to `page.evaluate`.
|
|
121
|
+
*
|
|
122
|
+
* @param {{ debug: string, build: string, coat?: string, member?: number }} arg
|
|
123
|
+
*/
|
|
124
|
+
function locateHerdBeastInPage({ debug, build, coat = '', member = 0 }) {
|
|
125
|
+
const app = window[debug];
|
|
126
|
+
const spaceId = app.space?.id;
|
|
127
|
+
const crowds = (app.crowds || []).filter(
|
|
128
|
+
c =>
|
|
129
|
+
c.space === spaceId &&
|
|
130
|
+
c.beasts &&
|
|
131
|
+
c.beasts.input?.build === build &&
|
|
132
|
+
(!coat || c.beasts.input?.coat === coat || c.beasts.input?.look?.breed === coat) &&
|
|
133
|
+
c.list?.length > 0,
|
|
134
|
+
);
|
|
135
|
+
if (!crowds.length) return null;
|
|
136
|
+
const cp = app.camera.position;
|
|
137
|
+
crowds.sort((a, b) => Math.hypot(a.cx - cp.x, a.cz - cp.z) - Math.hypot(b.cx - cp.x, b.cz - cp.z));
|
|
138
|
+
const crowd = crowds[0];
|
|
139
|
+
const idx = Math.min(member, crowd.list.length - 1);
|
|
140
|
+
const g = crowd.list[idx];
|
|
141
|
+
const x = Number.isFinite(g.gx) ? g.gx : (g.legs?.[0]?.x0 ?? g.ax ?? crowd.cx);
|
|
142
|
+
const z = Number.isFinite(g.gz) ? g.gz : (g.legs?.[0]?.z0 ?? g.az ?? crowd.cz);
|
|
143
|
+
if (app.state.fly) app.setFly(false);
|
|
144
|
+
app.holdQuality?.(true);
|
|
145
|
+
app.state.mode = 'play';
|
|
146
|
+
app.placePlayer(x + 2.5, z + 2.5, Math.atan2(-2.5, -2.5));
|
|
147
|
+
const y = app.space.ground(x, z);
|
|
148
|
+
app.camera.position.set(x + 2.5, y + 1.2, z + 2.5);
|
|
149
|
+
app.camera.lookAt(x, y + 0.5, z);
|
|
150
|
+
return { crowdIndex: app.crowds.indexOf(crowd), memberIndex: idx, x, z, total: crowd.list.length };
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/**
|
|
154
|
+
* Freezes a raised herd beast in LOD 0 at the requested hour and returns its world bounds and heading.
|
|
155
|
+
* Passed to `page.evaluate`.
|
|
156
|
+
*
|
|
157
|
+
* @param {{ debug: string, crowdIndex: number, memberIndex: number, hour: number }} arg
|
|
158
|
+
*/
|
|
159
|
+
function settleHerdBeastInPage({ debug, crowdIndex, memberIndex, hour }) {
|
|
160
|
+
const app = window[debug];
|
|
161
|
+
const crowd = app.crowds[crowdIndex];
|
|
162
|
+
if (!crowd?.herd) return null;
|
|
163
|
+
app.env.flow = false;
|
|
164
|
+
app.env.override = -1;
|
|
165
|
+
app.env.ovTarget = -1;
|
|
166
|
+
app.env.goal = -1;
|
|
167
|
+
app.env.time = hour;
|
|
168
|
+
app.holdQuality?.(true);
|
|
169
|
+
app.state.mode = 'inspect';
|
|
170
|
+
app.state.debug = false;
|
|
171
|
+
const g = crowd.list[memberIndex];
|
|
172
|
+
const approxX = Number.isFinite(g.gx) ? g.gx : (g.legs?.[0]?.x0 ?? g.ax);
|
|
173
|
+
const approxZ = Number.isFinite(g.gz) ? g.gz : (g.legs?.[0]?.z0 ?? g.az);
|
|
174
|
+
const approxY = Number.isFinite(g.gy) ? g.gy : app.space.ground(approxX, approxZ);
|
|
175
|
+
app.camera.position.set(approxX + 1.8, approxY + 0.9, approxZ + 1.8);
|
|
176
|
+
app.camera.lookAt(approxX, approxY + 0.45, approxZ);
|
|
177
|
+
app.state.measuring = true;
|
|
178
|
+
if (typeof app.step === 'function') app.step(0.02);
|
|
179
|
+
|
|
180
|
+
const level0 = crowd.herd.levels?.[0];
|
|
181
|
+
const sampleMesh = level0?.rests?.[0]?.[0]?.[0] || level0?.moments?.[0]?.[0] || crowd.herd.all?.[0];
|
|
182
|
+
if (!sampleMesh?.geometry) return null;
|
|
183
|
+
const geo = sampleMesh.geometry;
|
|
184
|
+
if (!geo.boundingBox) geo.computeBoundingBox();
|
|
185
|
+
const bb = geo.boundingBox;
|
|
186
|
+
const s = g.s || 1;
|
|
187
|
+
const x = Number.isFinite(g.gx) ? g.gx : approxX;
|
|
188
|
+
const z = Number.isFinite(g.gz) ? g.gz : approxZ;
|
|
189
|
+
const y = Number.isFinite(g.gy) ? g.gy : app.space.ground(x, z);
|
|
190
|
+
const yaw = Number.isFinite(g.dir) ? g.dir : g.pathYaw || 0;
|
|
191
|
+
const height = (bb.max.y - bb.min.y) * s;
|
|
192
|
+
const length = (bb.max.z - bb.min.z) * s;
|
|
193
|
+
const fx = Math.sin(yaw),
|
|
194
|
+
fz = Math.cos(yaw);
|
|
195
|
+
// Herd animals frequently rest sitting or sniffing lower than peak height; aim at 62% of peak height.
|
|
196
|
+
const head = [x + fx * bb.max.z * s * 0.65, y + (bb.min.y + (bb.max.y - bb.min.y) * 0.62) * s, z + fz * bb.max.z * s * 0.65];
|
|
197
|
+
const sun = app.env?.sunDir ? [app.env.sunDir.x, app.env.sunDir.y, app.env.sunDir.z] : null;
|
|
198
|
+
return { origin: [x, y, z], minY: y + bb.min.y * s, height, length, yaw, head, sun };
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
/**
|
|
202
|
+
* Freezes a raised scene or world actor in near LOD at the requested daylight hour and extracts
|
|
203
|
+
* its world bounding box, head bone position (when rigged), and facing yaw. Passed to `page.evaluate`.
|
|
204
|
+
*
|
|
205
|
+
* @param {{ debug: string, actorId: string, hour: number | null }} arg
|
|
206
|
+
*/
|
|
207
|
+
function settleActorInPage({ debug, actorId, hour }) {
|
|
208
|
+
const app = window[debug];
|
|
209
|
+
const a = app.actors?.[actorId];
|
|
210
|
+
if (!a?.group) return null;
|
|
211
|
+
const { THREE } = app;
|
|
212
|
+
if (app.director) app.director.paused = true;
|
|
213
|
+
app.env.flow = false;
|
|
214
|
+
if (Number.isFinite(hour)) {
|
|
215
|
+
// Active scenes lock the sky through env.override; clear it so the requested daylight hour takes effect.
|
|
216
|
+
app.env.override = -1;
|
|
217
|
+
app.env.ovTarget = -1;
|
|
218
|
+
app.env.goal = -1;
|
|
219
|
+
app.env.time = hour;
|
|
220
|
+
}
|
|
221
|
+
app.holdQuality?.(true);
|
|
222
|
+
app.state.mode = 'inspect';
|
|
223
|
+
app.state.debug = false;
|
|
224
|
+
app.state.measuring = true;
|
|
225
|
+
if (typeof app.step === 'function') {
|
|
226
|
+
app.step(0.05);
|
|
227
|
+
app.step(0.05);
|
|
228
|
+
}
|
|
229
|
+
if (a.hi) a.hi.visible = true;
|
|
230
|
+
if (a.lo) a.lo.visible = false;
|
|
231
|
+
a.group.visible = true;
|
|
232
|
+
a.group.updateMatrixWorld(true);
|
|
233
|
+
|
|
234
|
+
const box = new THREE.Box3().setFromObject(a.group);
|
|
235
|
+
const height = Math.max(0.12, box.max.y - box.min.y);
|
|
236
|
+
const length = Math.max(box.max.x - box.min.x, box.max.z - box.min.z);
|
|
237
|
+
const yaw = Number.isFinite(a.group.rotation?.y) ? a.group.rotation.y : a.yaw || 0;
|
|
238
|
+
let head = null;
|
|
239
|
+
// Quadruped head bone is index 5 (motion/quadruped.js BONE.head); read its exact world position when present.
|
|
240
|
+
const headBone = a.rig?.bones?.[5];
|
|
241
|
+
if (headBone) {
|
|
242
|
+
const v = new THREE.Vector3();
|
|
243
|
+
headBone.getWorldPosition(v);
|
|
244
|
+
head = [v.x, v.y, v.z];
|
|
245
|
+
}
|
|
246
|
+
const sun = app.env?.sunDir ? [app.env.sunDir.x, app.env.sunDir.y, app.env.sunDir.z] : null;
|
|
247
|
+
return { origin: [a.group.position.x, a.group.position.y, a.group.position.z], minY: box.min.y, height, length, yaw, head, sun };
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
/**
|
|
251
|
+
* Positions the camera for one close-up frame and draws twice so TAA/exposure settle while frozen.
|
|
252
|
+
* Passed to `page.evaluate`.
|
|
253
|
+
*
|
|
254
|
+
* @param {{ debug: string, pos: number[], aim: number[], fov: number }} arg
|
|
255
|
+
*/
|
|
256
|
+
function poseCameraAndDrawInPage({ debug, pos, aim, fov }) {
|
|
257
|
+
const app = window[debug];
|
|
258
|
+
app.state.mode = 'inspect';
|
|
259
|
+
app.camera.fov = fov;
|
|
260
|
+
app.camera.updateProjectionMatrix();
|
|
261
|
+
app.camera.position.set(pos[0], pos[1], pos[2]);
|
|
262
|
+
app.camera.lookAt(aim[0], aim[1], aim[2]);
|
|
263
|
+
app.camera.updateMatrixWorld(true);
|
|
264
|
+
if (typeof app.draw === 'function') {
|
|
265
|
+
app.draw();
|
|
266
|
+
app.draw();
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
async function playSceneToBeat(page, driver, id, targetAt) {
|
|
271
|
+
await page.evaluate(readyPageForSceneInPage, { debug: driver.debug, keepHud: false, clearMeasuring: true });
|
|
272
|
+
await page.evaluate(startSceneInPage, { debug: driver.debug, id });
|
|
273
|
+
if (!(await waitSceneReady(page, driver.debug))) throw new Error(`Scene ${id} failed to prepare`);
|
|
274
|
+
await page.evaluate(seekSceneInPage, { debug: driver.debug, targetSeconds: targetAt, settleSeconds: 0.5, flushMs: 80 });
|
|
275
|
+
await page.waitForFunction(
|
|
276
|
+
([d, t]) => {
|
|
277
|
+
const dir = window[d].director;
|
|
278
|
+
return !dir.ev || dir.ending || dir.t >= t;
|
|
279
|
+
},
|
|
280
|
+
[driver.debug, targetAt],
|
|
281
|
+
{ timeout: 60000 },
|
|
282
|
+
);
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
async function captureTargetViews(page, driver, target, { stem, views, headFocus = true, zoom = 1, tiles = null }) {
|
|
286
|
+
const framed = { ...target, headFocus, zoom };
|
|
287
|
+
const files = [];
|
|
288
|
+
for (const view of views) {
|
|
289
|
+
const shot = frameBeastView(framed, view, target.sun);
|
|
290
|
+
await page.evaluate(poseCameraAndDrawInPage, { debug: driver.debug, ...shot });
|
|
291
|
+
await page.waitForTimeout(120);
|
|
292
|
+
const filename = `${stem}-${view}.jpg`;
|
|
293
|
+
const filePath = path.join(driver.OUT, filename);
|
|
294
|
+
const buf = await page.screenshot({ path: filePath, type: 'jpeg', quality: 90 });
|
|
295
|
+
console.log('saved', filename, JSON.stringify({ view, height: +target.height.toFixed(2) }));
|
|
296
|
+
files.push(filePath);
|
|
297
|
+
if (tiles) tiles.push({ image: buf.toString('base64'), label: `${stem} · ${view}` });
|
|
298
|
+
}
|
|
299
|
+
return files;
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
/**
|
|
303
|
+
* Runs close-up animal inspections for configured kind specs and/or an explicit scene beat.
|
|
304
|
+
*
|
|
305
|
+
* @param {object} driver Page driver from `createPageDriver`.
|
|
306
|
+
* @param {object} config
|
|
307
|
+
* @param {Record<string, object>} config.catalog World-specific animal kind presets (`herd` or `scene`).
|
|
308
|
+
* @param {string[]} [config.defaultKinds] Kinds captured when none are passed on the CLI.
|
|
309
|
+
* @param {(scene: object) => string} [config.routeForScene] Maps a scene catalog entry to its route address.
|
|
310
|
+
* @param {string[]} [config.argv=process.argv.slice(2)] CLI arguments.
|
|
311
|
+
*/
|
|
312
|
+
async function runBeastsNear(
|
|
313
|
+
driver,
|
|
314
|
+
{ catalog, defaultKinds = Object.keys(catalog), routeForScene = () => '', argv = process.argv.slice(2) },
|
|
315
|
+
) {
|
|
316
|
+
const opts = parseBeastsNearArgs(argv);
|
|
317
|
+
if (opts.list) {
|
|
318
|
+
console.log('Available kinds:', Object.keys(catalog).join(', '));
|
|
319
|
+
return [];
|
|
320
|
+
}
|
|
321
|
+
fs.mkdirSync(driver.OUT, { recursive: true });
|
|
322
|
+
const browser = await driver.chromium.launch({ executablePath: driver.shell, ...driver.GPU });
|
|
323
|
+
const saved = [];
|
|
324
|
+
const tiles = opts.sheet ? [] : null;
|
|
325
|
+
try {
|
|
326
|
+
const ctx = await browser.newContext({ viewport: { width: opts.width, height: opts.height } });
|
|
327
|
+
await driver.applyStorage(ctx, { quality: 'high', muted: '1' });
|
|
328
|
+
const openPage = async address => {
|
|
329
|
+
const page = await ctx.newPage();
|
|
330
|
+
page.on('pageerror', e => console.log('PAGE ERROR', e.message.slice(0, 300)));
|
|
331
|
+
page.on('console', m => m.type() === 'error' && console.log('CONSOLE', m.text().slice(0, 300)));
|
|
332
|
+
await driver.enter(page, driver.routeUrl(address), { titledMs: 180000 });
|
|
333
|
+
await page.waitForTimeout(1200);
|
|
334
|
+
await page.addStyleTag({ content: `body > *:not(${driver.canvasSelector || '#c'}):not(canvas) { display: none !important; }` });
|
|
335
|
+
return page;
|
|
336
|
+
};
|
|
337
|
+
|
|
338
|
+
if (opts.scene) {
|
|
339
|
+
let page = await openPage('');
|
|
340
|
+
const meta = await page.evaluate(
|
|
341
|
+
([d, id]) => {
|
|
342
|
+
const ev = window[d].CONFIG.events.find(e => e.id === id || e.title === id);
|
|
343
|
+
return ev ? { id: ev.id, book: ev.book || 1, space: ev.space, dur: ev.dur, cast: Object.keys(ev.cast || {}) } : null;
|
|
344
|
+
},
|
|
345
|
+
[driver.debug, opts.scene],
|
|
346
|
+
);
|
|
347
|
+
if (!meta) throw new Error(`Unknown scene id: ${opts.scene}`);
|
|
348
|
+
const address = routeForScene(meta);
|
|
349
|
+
if (address) {
|
|
350
|
+
await page.close();
|
|
351
|
+
page = await openPage(address);
|
|
352
|
+
}
|
|
353
|
+
const targetAt = Number.isFinite(opts.at) ? opts.at : Math.min(10, (meta.dur || 20) * 0.35);
|
|
354
|
+
await playSceneToBeat(page, driver, meta.id, targetAt);
|
|
355
|
+
await page.evaluate(
|
|
356
|
+
([d, hour, explicit]) => {
|
|
357
|
+
const app = window[d];
|
|
358
|
+
app.director.paused = true;
|
|
359
|
+
app.state.measuring = true;
|
|
360
|
+
app.env.flow = false;
|
|
361
|
+
if (explicit) {
|
|
362
|
+
app.env.override = -1;
|
|
363
|
+
app.env.ovTarget = -1;
|
|
364
|
+
app.env.goal = -1;
|
|
365
|
+
app.env.time = hour;
|
|
366
|
+
if (typeof app.step === 'function') app.step(0.05);
|
|
367
|
+
}
|
|
368
|
+
if (typeof app.draw === 'function') {
|
|
369
|
+
app.draw();
|
|
370
|
+
app.draw();
|
|
371
|
+
}
|
|
372
|
+
},
|
|
373
|
+
[driver.debug, opts.hour, opts.hourExplicit],
|
|
374
|
+
);
|
|
375
|
+
const beatName = `${opts.prefix}-scene-${slugify(meta.id)}-${Math.round(targetAt)}s.jpg`;
|
|
376
|
+
const beatPath = path.join(driver.OUT, beatName);
|
|
377
|
+
const beatBuf = await page.screenshot({ path: beatPath, type: 'jpeg', quality: 90 });
|
|
378
|
+
console.log('saved', beatName);
|
|
379
|
+
saved.push(beatPath);
|
|
380
|
+
if (tiles) tiles.push({ image: beatBuf.toString('base64'), label: `${meta.id} @ ${targetAt}s` });
|
|
381
|
+
|
|
382
|
+
if (!opts.beatOnly) {
|
|
383
|
+
const actorId =
|
|
384
|
+
opts.actor ||
|
|
385
|
+
(await page.evaluate(
|
|
386
|
+
([d, castIds]) => {
|
|
387
|
+
const actors = window[d].actors || {};
|
|
388
|
+
return castIds.find(id => actors[id]?.active && actors[id]?.def?.look?.kind === 'hound') || '';
|
|
389
|
+
},
|
|
390
|
+
[driver.debug, meta.cast],
|
|
391
|
+
));
|
|
392
|
+
if (actorId) {
|
|
393
|
+
const target = await page.evaluate(settleActorInPage, {
|
|
394
|
+
debug: driver.debug,
|
|
395
|
+
actorId,
|
|
396
|
+
hour: opts.hourExplicit ? opts.hour : null,
|
|
397
|
+
});
|
|
398
|
+
if (target) {
|
|
399
|
+
saved.push(
|
|
400
|
+
...(await captureTargetViews(page, driver, target, {
|
|
401
|
+
stem: `${opts.prefix}-${slugify(meta.id)}-${slugify(actorId)}`,
|
|
402
|
+
views: opts.views,
|
|
403
|
+
headFocus: true,
|
|
404
|
+
tiles,
|
|
405
|
+
})),
|
|
406
|
+
);
|
|
407
|
+
}
|
|
408
|
+
}
|
|
409
|
+
}
|
|
410
|
+
await page.close();
|
|
411
|
+
}
|
|
412
|
+
|
|
413
|
+
const selected = opts.kinds.length ? (opts.kinds.includes('all') ? Object.keys(catalog) : opts.kinds) : opts.scene ? [] : defaultKinds;
|
|
414
|
+
for (const kind of selected) {
|
|
415
|
+
const spec = catalog[kind];
|
|
416
|
+
if (!spec) throw new Error(`Unknown kind '${kind}'. Available: ${Object.keys(catalog).join(', ')}`);
|
|
417
|
+
const page = await openPage(spec.address || '');
|
|
418
|
+
try {
|
|
419
|
+
let target = null;
|
|
420
|
+
if (spec.type === 'herd') {
|
|
421
|
+
const found = await page.evaluate(locateHerdBeastInPage, {
|
|
422
|
+
debug: driver.debug,
|
|
423
|
+
build: spec.build,
|
|
424
|
+
coat: spec.coat || '',
|
|
425
|
+
member: spec.member || 0,
|
|
426
|
+
});
|
|
427
|
+
if (!found) throw new Error(`No '${spec.build}' herd found at '${spec.address || '/'}'`);
|
|
428
|
+
await page.waitForFunction(
|
|
429
|
+
([d, ci]) => Boolean(window[d].crowds?.[ci]?.herd?.all?.some(m => m.parent)),
|
|
430
|
+
[driver.debug, found.crowdIndex],
|
|
431
|
+
{ timeout: 60000 },
|
|
432
|
+
);
|
|
433
|
+
await page.waitForTimeout(300);
|
|
434
|
+
target = await page.evaluate(settleHerdBeastInPage, {
|
|
435
|
+
debug: driver.debug,
|
|
436
|
+
crowdIndex: found.crowdIndex,
|
|
437
|
+
memberIndex: found.memberIndex,
|
|
438
|
+
hour: opts.hour,
|
|
439
|
+
});
|
|
440
|
+
} else if (spec.type === 'scene') {
|
|
441
|
+
if (spec.lookOverride) {
|
|
442
|
+
await page.evaluate(
|
|
443
|
+
([d, actorId, override]) => {
|
|
444
|
+
const a = window[d].actors?.[actorId];
|
|
445
|
+
if (a && !a.built && a.def?.look) a.def = { ...a.def, look: { ...a.def.look, ...override } };
|
|
446
|
+
},
|
|
447
|
+
[driver.debug, spec.actor, spec.lookOverride],
|
|
448
|
+
);
|
|
449
|
+
}
|
|
450
|
+
await playSceneToBeat(page, driver, spec.scene, spec.at);
|
|
451
|
+
target = await page.evaluate(settleActorInPage, { debug: driver.debug, actorId: spec.actor, hour: opts.hour });
|
|
452
|
+
} else {
|
|
453
|
+
throw new Error(`Unsupported kind spec type '${spec.type}' for '${kind}'`);
|
|
454
|
+
}
|
|
455
|
+
if (!target) throw new Error(`Failed to frame '${kind}'`);
|
|
456
|
+
saved.push(
|
|
457
|
+
...(await captureTargetViews(page, driver, target, {
|
|
458
|
+
stem: `${opts.prefix}-${slugify(kind)}`,
|
|
459
|
+
views: opts.views,
|
|
460
|
+
headFocus: spec.headFocus !== false,
|
|
461
|
+
zoom: spec.zoom || 1,
|
|
462
|
+
tiles,
|
|
463
|
+
})),
|
|
464
|
+
);
|
|
465
|
+
} finally {
|
|
466
|
+
await page.close();
|
|
467
|
+
}
|
|
468
|
+
}
|
|
469
|
+
|
|
470
|
+
if (tiles && tiles.length) {
|
|
471
|
+
const sheetName = `${opts.prefix}-sheet.jpg`;
|
|
472
|
+
const sheetPath = path.join(driver.OUT, sheetName);
|
|
473
|
+
await writeContactSheet(browser, tiles, sheetPath, {
|
|
474
|
+
columns: Math.min(3, Math.max(1, opts.views.length)),
|
|
475
|
+
cellWidth: 640,
|
|
476
|
+
cellHeight: 480,
|
|
477
|
+
});
|
|
478
|
+
console.log('saved', sheetName);
|
|
479
|
+
saved.push(sheetPath);
|
|
480
|
+
}
|
|
481
|
+
} finally {
|
|
482
|
+
await driver.closeBrowser(browser);
|
|
483
|
+
}
|
|
484
|
+
return saved;
|
|
485
|
+
}
|
|
486
|
+
|
|
487
|
+
module.exports = {
|
|
488
|
+
DEFAULT_FOV,
|
|
489
|
+
DEFAULT_HOUR,
|
|
490
|
+
DEFAULT_VIEWS,
|
|
491
|
+
frameBeastView,
|
|
492
|
+
locateHerdBeastInPage,
|
|
493
|
+
parseBeastsNearArgs,
|
|
494
|
+
poseCameraAndDrawInPage,
|
|
495
|
+
runBeastsNear,
|
|
496
|
+
settleActorInPage,
|
|
497
|
+
settleHerdBeastInPage,
|
|
498
|
+
};
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
const assert = require('node:assert/strict');
|
|
3
|
+
const test = require('node:test');
|
|
4
|
+
const { frameBeastView, locateHerdBeastInPage, parseBeastsNearArgs, poseCameraAndDrawInPage } = require('./beasts-near.cjs');
|
|
5
|
+
|
|
6
|
+
test('parseBeastsNearArgs parses kinds, scene beat options, and flags cleanly', () => {
|
|
7
|
+
const parsed = parseBeastsNearArgs([
|
|
8
|
+
'wolf',
|
|
9
|
+
'dog',
|
|
10
|
+
'--scene',
|
|
11
|
+
'chase',
|
|
12
|
+
'--at',
|
|
13
|
+
'14.5',
|
|
14
|
+
'--actor=lead',
|
|
15
|
+
'--hour=0.38',
|
|
16
|
+
'--prefix=before',
|
|
17
|
+
'--width=1000',
|
|
18
|
+
'--height=750',
|
|
19
|
+
'--views=side,front',
|
|
20
|
+
'--sheet',
|
|
21
|
+
]);
|
|
22
|
+
assert.deepEqual(parsed.kinds, ['wolf', 'dog']);
|
|
23
|
+
assert.equal(parsed.scene, 'chase');
|
|
24
|
+
assert.equal(parsed.at, 14.5);
|
|
25
|
+
assert.equal(parsed.actor, 'lead');
|
|
26
|
+
assert.equal(parsed.hour, 0.38);
|
|
27
|
+
assert.equal(parsed.hourExplicit, true);
|
|
28
|
+
assert.equal(parsed.prefix, 'before');
|
|
29
|
+
assert.equal(parsed.width, 1000);
|
|
30
|
+
assert.equal(parsed.height, 750);
|
|
31
|
+
assert.deepEqual(parsed.views, ['side', 'front']);
|
|
32
|
+
assert.equal(parsed.sheet, true);
|
|
33
|
+
assert.throws(() => parseBeastsNearArgs(['--bogus']), /Unknown option/);
|
|
34
|
+
assert.throws(() => parseBeastsNearArgs(['--scene']), /Missing value/);
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
test('frameBeastView places eye-height side, front, and three-quarter cameras on the sunlit side', () => {
|
|
38
|
+
const target = { origin: [10, 0, 20], minY: 0, height: 0.9, length: 1.2, yaw: 0, head: [10, 0.72, 20.4], headFocus: true };
|
|
39
|
+
const sunRight = [0.8, 0.5, 0.2];
|
|
40
|
+
const sunLeft = [-0.8, 0.5, 0.2];
|
|
41
|
+
|
|
42
|
+
const sideR = frameBeastView(target, 'side', sunRight);
|
|
43
|
+
const sideL = frameBeastView(target, 'side', sunLeft);
|
|
44
|
+
assert.ok(sideR.pos[0] > target.origin[0] + 0.5, 'side view picks +x when sun is on +x');
|
|
45
|
+
assert.ok(sideL.pos[0] < target.origin[0] - 0.5, 'side view flips to -x when sun is on -x');
|
|
46
|
+
assert.ok(Math.abs(sideR.pos[1] - sideR.aim[1]) < 0.1, 'side view stays at eye height');
|
|
47
|
+
|
|
48
|
+
const front = frameBeastView(target, 'front', sunRight);
|
|
49
|
+
assert.ok(front.pos[2] > target.head[2] + 0.5, 'front view stands ahead of the nose');
|
|
50
|
+
|
|
51
|
+
const three = frameBeastView(target, 'three', sunRight);
|
|
52
|
+
assert.ok(
|
|
53
|
+
three.pos[0] > target.head[0] + 0.3 && three.pos[2] > target.head[2] + 0.3,
|
|
54
|
+
'three-quarter view stands ahead and to the sunlit side',
|
|
55
|
+
);
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
test('locateHerdBeastInPage and poseCameraAndDrawInPage talk strictly through window[debug]', () => {
|
|
59
|
+
let placed = null;
|
|
60
|
+
let drawn = 0;
|
|
61
|
+
const camera = {
|
|
62
|
+
fov: 50,
|
|
63
|
+
position: {
|
|
64
|
+
x: 0,
|
|
65
|
+
y: 0,
|
|
66
|
+
z: 0,
|
|
67
|
+
set(x, y, z) {
|
|
68
|
+
this.x = x;
|
|
69
|
+
this.y = y;
|
|
70
|
+
this.z = z;
|
|
71
|
+
},
|
|
72
|
+
},
|
|
73
|
+
lookAt() {},
|
|
74
|
+
updateProjectionMatrix() {},
|
|
75
|
+
updateMatrixWorld() {},
|
|
76
|
+
};
|
|
77
|
+
globalThis.window = {
|
|
78
|
+
APP: {
|
|
79
|
+
space: { id: 'valley', ground: () => 2 },
|
|
80
|
+
state: { fly: false, mode: 'play' },
|
|
81
|
+
camera,
|
|
82
|
+
crowds: [
|
|
83
|
+
{ space: 'valley', cx: 5, cz: 8, beasts: { input: { build: 'dog', coat: 'cur' } }, list: [{ gx: 6, gz: 9 }] },
|
|
84
|
+
{ space: 'other', cx: 0, cz: 0, beasts: { input: { build: 'dog', coat: 'cur' } }, list: [{ gx: 0, gz: 0 }] },
|
|
85
|
+
],
|
|
86
|
+
placePlayer(x, z, yaw) {
|
|
87
|
+
placed = { x, z, yaw };
|
|
88
|
+
},
|
|
89
|
+
draw() {
|
|
90
|
+
drawn++;
|
|
91
|
+
},
|
|
92
|
+
},
|
|
93
|
+
};
|
|
94
|
+
try {
|
|
95
|
+
const found = locateHerdBeastInPage({ debug: 'APP', build: 'dog', coat: 'cur' });
|
|
96
|
+
assert.deepEqual(found, { crowdIndex: 0, memberIndex: 0, x: 6, z: 9, total: 1 });
|
|
97
|
+
assert.ok(placed && Math.hypot(placed.x - 6, placed.z - 9) > 2);
|
|
98
|
+
|
|
99
|
+
poseCameraAndDrawInPage({ debug: 'APP', pos: [1, 2, 3], aim: [0, 1, 0], fov: 34 });
|
|
100
|
+
assert.equal(camera.fov, 34);
|
|
101
|
+
assert.equal(drawn, 2);
|
|
102
|
+
} finally {
|
|
103
|
+
delete globalThis.window;
|
|
104
|
+
}
|
|
105
|
+
});
|