Vehicle Generator 0.1.0 (alpha)

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.
This commit is contained in:
CRTkafa 2026-10-11 08:22:23 +08:00
commit 6b394bcbfc
112 changed files with 17584 additions and 0 deletions

62
src/app/build.ts Normal file
View file

@ -0,0 +1,62 @@
/** Turning the spec into a vehicle on screen: coalesced rebuilds, coarse builds while a field is scrubbed, the rig pose. */
import { generateVehicle, type Vehicle } from '../generator/index.ts'
import { effectiveSpec, stage, state } from './context.ts'
import { renderInspector } from './inspector.ts'
import { renderStatus } from './status.ts'
export let vehicle: Vehicle | null = null
export let rebuildQueued = false
export let refit = false
export let scrubbing = false
export let scrubDirty = false
window.addEventListener('pointerdown', (e) => {
if ((e.target as Element | null)?.closest?.('[data-scrub]')) { scrubbing = true; scrubDirty = false }
}, true)
export const endScrub = (): void => {
if (!scrubbing) return
scrubbing = false
if (scrubDirty) rebuild()
}
window.addEventListener('pointerup', endScrub, true)
window.addEventListener('pointercancel', endScrub, true)
export let lastBuildMs = 0
export function rebuild(frame = false): void {
if (scrubbing) scrubDirty = true
refit ||= frame
if (rebuildQueued) return
rebuildQueued = true
requestAnimationFrame(() => {
rebuildQueued = false
const t0 = performance.now()
// While a field is scrubbed, build only the coarse level so dragging stays
// smooth; the full build follows on release.
const next = scrubbing ? generateVehicle(effectiveSpec(), { lods: [2], interior: false }) : generateVehicle(effectiveSpec())
lastBuildMs = performance.now() - t0
stage.setVehicle(next, refit || !vehicle)
if (resumeView) {
stage.camera.position.fromArray(resumeView.camera)
stage.controls.target.fromArray(resumeView.target)
stage.controls.update()
resumeView = null
}
refit = false
vehicle?.dispose()
vehicle = next
applyRig()
renderStatus()
if (state.tab === 'rig') renderInspector()
})
}
export function applyRig(): void {
if (!vehicle) return
vehicle.rig.setSteer(state.steer)
for (const part of vehicle.rig.openables) vehicle.rig.open(part, state.open[part] ?? 0)
vehicle.rig.setHeadlights(state.head)
vehicle.rig.setBrake(state.brake)
}
export let resumeView: { readonly camera: number[]; readonly target: number[] } | null = null
/** A dev reload's camera, applied once the first build is on screen. */
export function setResumeView(view: { readonly camera: number[]; readonly target: number[] }): void { resumeView = view }