-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbake-vegetation-impostor.mjs
More file actions
282 lines (268 loc) · 13.1 KB
/
Copy pathbake-vegetation-impostor.mjs
File metadata and controls
282 lines (268 loc) · 13.1 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
// SPDX-License-Identifier: AGPL-3.0-or-later
// Copyright (c) 2025-2026 Matthew Kissinger
//
// Standalone octahedral impostor baker for the engine-agnostic vegetation library.
//
// WHY STANDALONE: scripts/bake-static-impostor-atlases.ts is archetype-driven — it
// only bakes GLBs registered in the FENCED src/config/staticImpostorArchetypes.ts and
// loads them from the engine's /models/ route. Baking a vegetation-library asset
// through it would require registering the asset in that fenced engine registry, which
// the vegetation-library task forbids. This tool reuses the SAME orthographic
// azimuth x elevation capture math (MeshBasicMaterial base color + MeshNormalMaterial
// + MeshDepthMaterial passes into an N-column x M-row atlas) but drives it against a
// GLB served straight from public/assets/vegetation/<id>/ by the standalone
// http-server at :8765 — no engine registry, no fenced files touched.
//
// SELF-CONTAINED: at startup it writes a tiny throwaway three.js bake page plus a
// same-origin copy of the three.js modules it needs into public/_bake/ (the plain
// http-server does NOT serve /node_modules, and bare 'three' specifiers need an
// import map resolved to a served path). It removes public/_bake/ on exit.
//
// PREREQUISITE: the dev http-server must be serving public/ at the --base origin
// (default http://localhost:8765).
//
// Usage:
// node scripts/bake-vegetation-impostor.mjs \
// --url http://localhost:8765/assets/vegetation/jungle-tree/jungle-tree.glb \
// --out public/assets/vegetation/jungle-tree/impostor \
// --columns 8 --rows 3 --tile 256
//
// Writes atlas.base-color.png, atlas.normal.png, atlas.depth.png and metadata.json
// into --out, and prints the captured mesh bounds (for the descriptor) to stdout.
import { chromium } from 'playwright';
import { cpSync, mkdirSync, rmSync, writeFileSync } from 'node:fs';
import { join, resolve } from 'node:path';
function arg(name, fallback) {
const i = process.argv.indexOf(`--${name}`);
if (i >= 0 && i + 1 < process.argv.length) return process.argv[i + 1];
return fallback;
}
const BASE = arg('base', 'http://localhost:8765');
const MODEL_URL = arg('url', `${BASE}/assets/vegetation/jungle-tree/jungle-tree.glb`);
const OUT_DIR = resolve(process.cwd(), arg('out', 'public/assets/vegetation/jungle-tree/impostor'));
const COLUMNS = Number(arg('columns', '8'));
const ROWS = Number(arg('rows', '3'));
const TILE = Number(arg('tile', '256'));
const TIMEOUT = 120_000;
const PUBLIC = resolve(process.cwd(), 'public');
const BAKE_DIR = join(PUBLIC, '_bake');
const VENDOR = join(BAKE_DIR, 'vendor', 'three');
const NM = resolve(process.cwd(), 'node_modules', 'three');
// Elevation rows from grazing to near-overhead, reported in metadata.
const ELEVATION_ROWS_DEG = [14, 43, 72];
const BAKE_HTML = `<!DOCTYPE html>
<html><head><meta charset="utf-8"></head><body>
<script type="importmap">
{ "imports": {
"three": "/_bake/vendor/three/build/three.module.js",
"three/addons/": "/_bake/vendor/three/examples/jsm/"
} }
</scr`+`ipt>
<script type="module">
import * as THREE from 'three';
import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js';
const loader = new GLTFLoader();
const box = new THREE.Box3();
const center = new THREE.Vector3();
const size = new THREE.Vector3();
const direction = new THREE.Vector3();
const target = new THREE.Vector3();
function loadModel(url) {
return new Promise((res, rej) => loader.load(url, (g) => res(g.scene), undefined, rej));
}
function normalizeForPlacement(root) {
root.updateMatrixWorld(true);
box.setFromObject(root); box.getCenter(center);
root.position.x -= center.x; root.position.z -= center.z; root.position.y -= box.min.y;
root.updateMatrixWorld(true);
}
function materialArray(m) { return Array.isArray(m) ? m : [m]; }
function makeBaseColorMaterial(s) {
const m = new THREE.MeshBasicMaterial({
color: s && s.color instanceof THREE.Color ? s.color : new THREE.Color(0xffffff),
map: (s && s.map) || null,
vertexColors: Boolean(s && s.vertexColors),
transparent: Boolean(s && s.transparent) || Number((s && s.opacity) ?? 1) < 1,
opacity: Number((s && s.opacity) ?? 1),
alphaTest: Number((s && s.alphaTest) ?? 0),
side: (s && s.side) || THREE.FrontSide,
depthWrite: s ? s.depthWrite : true, depthTest: s ? s.depthTest : true,
});
if (m.transparent && m.alphaTest === 0) m.alphaTest = 0.35;
return m;
}
function applyBaseColorMaterials(root) {
const recs = [];
root.traverse((c) => {
if (!c.isMesh) return;
recs.push({ mesh: c, material: c.material });
const conv = materialArray(c.material).map(makeBaseColorMaterial);
c.material = Array.isArray(c.material) ? conv : conv[0];
});
return () => { for (const r of recs) { const t = materialArray(r.mesh.material); r.mesh.material = r.material; for (const x of t) x.dispose(); } };
}
function makeAtlasCanvas(w, h) {
const c = document.createElement('canvas'); c.width = w; c.height = h;
const ctx = c.getContext('2d', { alpha: true }); ctx.clearRect(0, 0, w, h); return { canvas: c, ctx };
}
function alphaBleedCanvas(canvas, passes = 32) {
const ctx = canvas.getContext('2d', { alpha: true });
const image = ctx.getImageData(0, 0, canvas.width, canvas.height);
const data = image.data;
let colored = new Uint8Array(canvas.width * canvas.height);
for (let p = 0, i = 0; p < colored.length; p++, i += 4) colored[p] = data[i + 3] > 0 ? 1 : 0;
for (let pass = 0; pass < passes; pass++) {
let filled = 0;
const next = new Uint8ClampedArray(data);
const nextColored = new Uint8Array(colored);
for (let y = 0; y < canvas.height; y++) {
for (let x = 0; x < canvas.width; x++) {
const p = y * canvas.width + x;
if (colored[p]) continue;
let r = 0, g = 0, b = 0, count = 0;
for (let oy = -1; oy <= 1; oy++) {
const ny = y + oy;
if (ny < 0 || ny >= canvas.height) continue;
for (let ox = -1; ox <= 1; ox++) {
if (ox === 0 && oy === 0) continue;
const nx = x + ox;
if (nx < 0 || nx >= canvas.width) continue;
const np = ny * canvas.width + nx;
if (!colored[np]) continue;
const ni = np * 4;
r += data[ni]; g += data[ni + 1]; b += data[ni + 2]; count++;
}
}
if (count > 0) {
const i = p * 4;
next[i] = Math.round(r / count);
next[i + 1] = Math.round(g / count);
next[i + 2] = Math.round(b / count);
nextColored[p] = 1;
filled++;
}
}
}
if (filled === 0) break;
data.set(next);
colored = nextColored;
}
ctx.putImageData(image, 0, 0);
}
function elevationForRow(row, rows) {
if (rows <= 1) return THREE.MathUtils.degToRad(35);
const high = THREE.MathUtils.degToRad(72), low = THREE.MathUtils.degToRad(14);
return high + (low - high) * (row / (rows - 1));
}
function drawAtlasPass(o) {
const { scene, renderer, camera, sourceCanvas, atlas, columns, rows, tileW, tileH, bounds } = o;
const distance = Math.max(bounds.radius * 4, 8);
for (let row = 0; row < rows; row++) {
const elev = elevationForRow(row, rows), horiz = Math.cos(elev);
for (let col = 0; col < columns; col++) {
const az = (col / columns) * Math.PI * 2;
direction.set(Math.cos(az) * horiz, Math.sin(elev), Math.sin(az) * horiz).normalize();
target.fromArray(bounds.center);
camera.position.copy(target).addScaledVector(direction, distance);
camera.up.set(0, 1, 0); camera.lookAt(target); camera.updateMatrixWorld(true);
renderer.clear(true, true, true); renderer.render(scene, camera);
atlas.ctx.drawImage(sourceCanvas, col * tileW, row * tileH, tileW, tileH);
}
}
}
window.__bakeImpostor = async function (cfg) {
const { url, columns, rows, tileW, tileH } = cfg;
const atlasW = columns * tileW, atlasH = rows * tileH;
const root = await loadModel(url);
normalizeForPlacement(root);
root.traverse((c) => { if (c.isMesh) { c.castShadow = false; c.receiveShadow = false; c.frustumCulled = false; } });
box.setFromObject(root); box.getCenter(center); box.getSize(size);
const planeWidth = Math.max(Math.hypot(size.x, size.z), 0.1);
const planeHeight = Math.max(size.y, 0.1);
const pad = 1.08;
const radius = Math.max(size.length() * 0.5, 0.1);
const scene = new THREE.Scene(); scene.add(root);
const camera = new THREE.OrthographicCamera(
-planeWidth * pad * 0.5, planeWidth * pad * 0.5, planeHeight * pad * 0.5, -planeHeight * pad * 0.5,
0.01, Math.max(radius * 10, 100));
const renderer = new THREE.WebGLRenderer({ alpha: true, antialias: true, preserveDrawingBuffer: true });
renderer.setPixelRatio(1); renderer.setSize(tileW, tileH, false);
renderer.setClearColor(0x000000, 0); renderer.outputColorSpace = THREE.SRGBColorSpace;
document.body.appendChild(renderer.domElement);
const baseAtlas = makeAtlasCanvas(atlasW, atlasH);
const normalAtlas = makeAtlasCanvas(atlasW, atlasH);
const depthAtlas = makeAtlasCanvas(atlasW, atlasH);
const bounds = { center: [center.x, center.y, center.z], size: [size.x, size.y, size.z], radius };
const shared = { scene, renderer, camera, sourceCanvas: renderer.domElement, columns, rows, tileW, tileH, bounds };
const restore = applyBaseColorMaterials(root);
drawAtlasPass({ ...shared, atlas: baseAtlas }); restore();
const nMat = new THREE.MeshNormalMaterial({ side: THREE.DoubleSide });
scene.overrideMaterial = nMat; drawAtlasPass({ ...shared, atlas: normalAtlas }); nMat.dispose();
const dMat = new THREE.MeshDepthMaterial({ depthPacking: THREE.BasicDepthPacking, side: THREE.DoubleSide });
scene.overrideMaterial = dMat; drawAtlasPass({ ...shared, atlas: depthAtlas }); dMat.dispose();
scene.overrideMaterial = null;
alphaBleedCanvas(baseAtlas.canvas);
alphaBleedCanvas(normalAtlas.canvas);
renderer.dispose(); renderer.domElement.remove(); scene.remove(root);
return {
baseColorPng: baseAtlas.canvas.toDataURL('image/png'),
normalPng: normalAtlas.canvas.toDataURL('image/png'),
depthPng: depthAtlas.canvas.toDataURL('image/png'),
bounds, atlasSize: [atlasW, atlasH], tileSize: [tileW, tileH], columns, rows,
};
};
window.__impostorBakerReady = true;
</scr`+`ipt></body></html>
`;
function writeScratch() {
rmSync(BAKE_DIR, { recursive: true, force: true });
mkdirSync(join(VENDOR, 'build'), { recursive: true });
mkdirSync(join(VENDOR, 'examples', 'jsm', 'loaders'), { recursive: true });
mkdirSync(join(VENDOR, 'examples', 'jsm', 'utils'), { recursive: true });
cpSync(join(NM, 'build', 'three.module.js'), join(VENDOR, 'build', 'three.module.js'));
cpSync(join(NM, 'build', 'three.core.js'), join(VENDOR, 'build', 'three.core.js'));
cpSync(join(NM, 'examples', 'jsm', 'loaders', 'GLTFLoader.js'), join(VENDOR, 'examples', 'jsm', 'loaders', 'GLTFLoader.js'));
cpSync(join(NM, 'examples', 'jsm', 'utils', 'BufferGeometryUtils.js'), join(VENDOR, 'examples', 'jsm', 'utils', 'BufferGeometryUtils.js'));
cpSync(join(NM, 'examples', 'jsm', 'utils', 'SkeletonUtils.js'), join(VENDOR, 'examples', 'jsm', 'utils', 'SkeletonUtils.js'));
writeFileSync(join(BAKE_DIR, 'impostor-bake.html'), BAKE_HTML, 'utf-8');
}
function writeDataUrlPng(dataUrl, path) {
writeFileSync(path, Buffer.from(dataUrl.replace(/^data:image\/png;base64,/, ''), 'base64'));
}
async function main() {
writeScratch();
const browser = await chromium.launch({ headless: true });
const page = await browser.newPage({ viewport: { width: 320, height: 320 } });
page.on('console', (m) => { if (m.type() === 'error') console.error(`[page:error] ${m.text()}`); });
page.on('pageerror', (e) => console.error(`[pageerror] ${e.message}`));
try {
await page.goto(`${BASE}/_bake/impostor-bake.html`, { waitUntil: 'domcontentloaded', timeout: TIMEOUT });
await page.waitForFunction(() => Boolean(window.__impostorBakerReady), undefined, { timeout: TIMEOUT });
const result = await page.evaluate((c) => window.__bakeImpostor(c), { url: MODEL_URL, columns: COLUMNS, rows: ROWS, tileW: TILE, tileH: TILE });
mkdirSync(OUT_DIR, { recursive: true });
writeDataUrlPng(result.baseColorPng, join(OUT_DIR, 'atlas.base-color.png'));
writeDataUrlPng(result.normalPng, join(OUT_DIR, 'atlas.normal.png'));
writeDataUrlPng(result.depthPng, join(OUT_DIR, 'atlas.depth.png'));
writeFileSync(join(OUT_DIR, 'metadata.json'), `${JSON.stringify({
generator: 'scripts/bake-vegetation-impostor.mjs',
generatedAt: new Date().toISOString(),
modelUrl: MODEL_URL,
columns: result.columns, rows: result.rows,
tileSize: result.tileSize, atlasSize: result.atlasSize,
projection: 'octahedral-upper-hemisphere',
elevationRowsDeg: ELEVATION_ROWS_DEG.slice(0, result.rows),
bounds: result.bounds,
}, null, 2)}\n`, 'utf-8');
console.log('BAKE_OK');
console.log(JSON.stringify({ bounds: result.bounds, atlasSize: result.atlasSize, out: OUT_DIR }, null, 2));
} finally {
await page.close().catch(() => {});
await browser.close().catch(() => {});
rmSync(BAKE_DIR, { recursive: true, force: true });
}
}
main().catch((e) => {
rmSync(BAKE_DIR, { recursive: true, force: true });
console.error(e && e.stack ? e.stack : String(e));
process.exit(1);
});