Procedural, game-ready vehicles for three.js: 13 body styles, blending, seeded random vehicles, three LODs, colliders, 13 parametric mods, paint and livery, a rig that drives, and GLB export that a game can load and drive. Ships with a studio (browser or Tauri desktop app) and a Play mode.
60 lines
2.3 KiB
TypeScript
60 lines
2.3 KiB
TypeScript
/**
|
|
* Batch GLB export from the command line.
|
|
*
|
|
* bun scripts/export-glb.ts every body style → glb/
|
|
* bun scripts/export-glb.ts --library garage.json every entry of a saved library
|
|
* bun scripts/export-glb.ts --random 20 twenty random vehicles
|
|
* bun scripts/export-glb.ts --lod all all three LODs in each file
|
|
*/
|
|
import './glb-node.ts'
|
|
|
|
import { mkdir, readFile, writeFile } from 'node:fs/promises'
|
|
|
|
import {
|
|
BODY_STYLES,
|
|
exportVehicleGlb,
|
|
generateVehicle,
|
|
type LodLevel,
|
|
preset,
|
|
randomSpec,
|
|
type VehicleSpec,
|
|
} from '../src/generator/index.ts'
|
|
import { parseLibrary } from '../src/library/format.ts'
|
|
|
|
const args = process.argv.slice(2)
|
|
const flag = (name: string): string | undefined => {
|
|
const at = args.indexOf(`--${name}`)
|
|
return at >= 0 ? args[at + 1] : undefined
|
|
}
|
|
const outDir = flag('out') ?? 'glb'
|
|
const lodFlag = flag('lod')
|
|
const lod: LodLevel | 'all' = lodFlag === 'all' ? 'all' : (Number(lodFlag ?? 0) as LodLevel)
|
|
await mkdir(outDir, { recursive: true })
|
|
|
|
let jobs: { name: string; spec: VehicleSpec }[]
|
|
const libraryPath = flag('library')
|
|
const random = flag('random')
|
|
if (libraryPath) {
|
|
const library = parseLibrary(JSON.parse(await readFile(libraryPath, 'utf8')))
|
|
jobs = library.entries.map((e) => ({ name: e.name, spec: e.spec }))
|
|
} else if (random) {
|
|
jobs = Array.from({ length: Number(random) }, (_, k) => ({ name: `random-${k + 1}`, spec: randomSpec(k + 1) }))
|
|
} else {
|
|
jobs = BODY_STYLES.map((style) => ({ name: style, spec: preset(style) }))
|
|
}
|
|
|
|
const slug = (name: string): string => name.toLowerCase().replace(/[^a-z0-9]+/g, '-').replace(/^-|-$/g, '') || 'vehicle'
|
|
let total = 0
|
|
const used = new Set<string>()
|
|
for (const job of jobs) {
|
|
const vehicle = generateVehicle(job.spec, { lods: lod === 'all' ? [0, 1, 2] : [lod] })
|
|
const buffer = await exportVehicleGlb(vehicle, { lod: lod === 'all' ? 'all' : lod })
|
|
vehicle.dispose()
|
|
let file = slug(job.name)
|
|
for (let k = 2; used.has(file); k += 1) file = `${slug(job.name)}-${k}`
|
|
used.add(file)
|
|
await writeFile(`${outDir}/${file}.glb`, new Uint8Array(buffer))
|
|
total += buffer.byteLength
|
|
console.log(` ${file.padEnd(24)} ${(buffer.byteLength / 1024).toFixed(0).padStart(6)} KB`)
|
|
}
|
|
console.log(`\n${jobs.length} vehicles → ${outDir}/ · ${(total / 1024 / 1024).toFixed(1)} MB`)
|