Files
yourpart3/frontend/scripts/optimize-glb.mjs
Torsten Schulz (local) 2ebd8216fa
All checks were successful
Deploy to production / deploy (push) Successful in 1m54s
perf(falukant): simplify house runtime geometry
2026-09-29 11:56:53 +02:00

127 lines
3.9 KiB
JavaScript
Executable File

#!/usr/bin/env node
/**
* Optimiert Falukant-Charakter- und Haus-GLBs mit Draco + Textur-Optimierung.
* Ausgabe: *_opt.glb im selben Verzeichnis.
* Voraussetzung: npm install (@gltf-transform/cli als Dev-Dep)
*
* Aufruf: npm run optimize-models
* Optional können MODEL_SOURCE_DIR und MODEL_OUTPUT_DIR gesetzt werden, um
* genau einen Modellordner aus dem separaten YourPart3Assets-Repository zu
* verarbeiten.
*/
import fs from 'fs';
import { spawnSync } from 'child_process';
import { fileURLToPath } from 'url';
import path from 'path';
const __dirname = path.dirname(fileURLToPath(import.meta.url));
const modelsRoot = path.join(__dirname, '..', 'models-src', '3d', 'falukant');
const publicModelsRoot = path.join(__dirname, '..', 'public', 'models', '3d', 'falukant');
const cli = path.join(__dirname, '..', 'node_modules', '.bin', 'gltf-transform');
function listSourceModels(sourceDir) {
return fs.readdirSync(sourceDir)
.filter((file) => file.endsWith('.glb'))
.filter((file) => !file.endsWith('_opt.glb'))
.sort((a, b) => a.localeCompare(b));
}
function ensureOutputDir(outputDir) {
fs.mkdirSync(outputDir, { recursive: true });
}
function removeStaleOptimizedModels(outputDir) {
if (!fs.existsSync(outputDir)) {
return;
}
const cleanAllModels = process.env.CLEAN_MODEL_OUTPUT === 'true';
for (const file of fs.readdirSync(outputDir)) {
if (file.endsWith('_opt.glb') || (cleanAllModels && file.endsWith('.glb'))) {
fs.unlinkSync(path.join(outputDir, file));
}
}
}
function optimizeModelDirectory({ name, sourceDir, outputDir, required = false }) {
if (!fs.existsSync(sourceDir)) {
if (required) {
console.error(`Source directory not found: ${sourceDir}`);
process.exitCode = 1;
}
return;
}
ensureOutputDir(outputDir);
removeStaleOptimizedModels(outputDir);
const models = listSourceModels(sourceDir);
if (models.length === 0) {
console.log(`No ${name} GLBs found.`);
return;
}
console.log(`Optimize Falukant ${name} GLBs (Draco + texture 1024)\n`);
const isHouse = name === 'house' || name === 'houses' || path.basename(sourceDir) === 'houses';
for (const f of models) {
const input = path.join(sourceDir, f);
const out = f.replace(/\.glb$/, '_opt.glb');
const output = path.join(outputDir, out);
try {
const result = spawnSync(
'node',
[
cli, 'optimize', input, output,
'--compress', 'draco',
'--texture-size', '1024',
// Draco reduces download size, while simplify prevents high-poly
// scans from exhausting browser GPU memory. Houses are small
// previews, so they get a deliberately stronger reduction.
'--simplify-ratio', isHouse ? '0.05' : '0.35',
'--simplify-error', isHouse ? '0.02' : '0.01',
],
{
stdio: 'inherit',
cwd: path.join(__dirname, '..'),
}
);
if (result.status !== 0) {
throw new Error(`gltf-transform exited with ${result.status}`);
}
} catch (e) {
console.error(`Failed: ${f}`);
process.exitCode = 1;
}
}
}
const customSourceDir = process.env.MODEL_SOURCE_DIR;
const customOutputDir = process.env.MODEL_OUTPUT_DIR;
if (Boolean(customSourceDir) !== Boolean(customOutputDir)) {
console.error('MODEL_SOURCE_DIR and MODEL_OUTPUT_DIR must be set together.');
process.exit(1);
}
if (customSourceDir) {
optimizeModelDirectory({
name: 'custom',
sourceDir: path.resolve(customSourceDir),
outputDir: path.resolve(customOutputDir),
required: true,
});
} else {
optimizeModelDirectory({
name: 'character',
sourceDir: path.join(modelsRoot, 'characters'),
outputDir: path.join(publicModelsRoot, 'characters'),
required: true,
});
optimizeModelDirectory({
name: 'house',
sourceDir: path.join(modelsRoot, 'houses'),
outputDir: path.join(publicModelsRoot, 'houses'),
});
}