82 lines
2.2 KiB
JavaScript
Executable File
82 lines
2.2 KiB
JavaScript
Executable File
#!/usr/bin/env node
|
|
/**
|
|
* Optimiert alle Falukant-Charakter-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
|
|
*/
|
|
|
|
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 sourceDir = path.join(__dirname, '..', 'models-src', '3d', 'falukant', 'characters');
|
|
const outputDir = path.join(__dirname, '..', 'public', 'models', '3d', 'falukant', 'characters');
|
|
const cli = path.join(__dirname, '..', 'node_modules', '.bin', 'gltf-transform');
|
|
|
|
function listSourceModels() {
|
|
return fs.readdirSync(sourceDir)
|
|
.filter((file) => file.endsWith('.glb'))
|
|
.filter((file) => !file.endsWith('_opt.glb'))
|
|
.sort((a, b) => a.localeCompare(b));
|
|
}
|
|
|
|
function ensureOutputDir() {
|
|
fs.mkdirSync(outputDir, { recursive: true });
|
|
}
|
|
|
|
function removeStaleOptimizedModels() {
|
|
if (!fs.existsSync(outputDir)) {
|
|
return;
|
|
}
|
|
for (const file of fs.readdirSync(outputDir)) {
|
|
if (file.endsWith('_opt.glb')) {
|
|
fs.unlinkSync(path.join(outputDir, file));
|
|
}
|
|
}
|
|
}
|
|
|
|
console.log('Optimize Falukant character GLBs (Draco + texture 1024)\n');
|
|
|
|
if (!fs.existsSync(sourceDir)) {
|
|
console.error(`Source directory not found: ${sourceDir}`);
|
|
process.exit(1);
|
|
}
|
|
|
|
ensureOutputDir();
|
|
removeStaleOptimizedModels();
|
|
|
|
const models = listSourceModels();
|
|
if (models.length === 0) {
|
|
console.log('No source GLBs found.');
|
|
process.exit(0);
|
|
}
|
|
|
|
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'],
|
|
{
|
|
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;
|
|
}
|
|
}
|
|
|
|
console.log(`\nDone. Optimized models written to ${outputDir}`);
|