# vehicle-generator **Alpha · in development.** Things work, things change; the spec format and the API may still move between 0.x releases. Procedural, game-ready vehicles for [three.js](https://threejs.org). Give it a spec (a body style, some proportions, a paint job, a list of mods) and it builds a rigged car with three levels of detail, a physics collider and a GLB you can drop into any engine. The same spec always produces the same vertices. ![A generated sedan](media/sedan.png) It ships as a TypeScript library and as **Vehicle Generator**, a studio app for shaping cars, fitting parts, driving them and exporting them. The studio runs in a browser or as a desktop app. ![Every body style](media/styles.png) ## Status Version 0.1.0, alpha. **Works today** - The library: 13 body styles, blending, seeded random vehicles, three LODs, colliders, 13 mods, paint and livery, the rig, and GLB export that a game can load and drive. - The studio, in a browser or as a desktop app: garage, customs, properties, export, undo and autosave. - Play mode: drive any vehicle on a generated circuit or in traffic. **In progress** - Bodies drawn from authored cages. The sedan is the first: its shape comes from a control cage subdivided at run time. The other twelve styles still use the older lofted body and are moving over one by one. **Planned** - One-click import into Godot, Unity and Unreal (shown as *planned* in the studio's export menu; today you export GLB or spec JSON). - Physics values for game engines: mass, centre of mass, springs, dampers, steering lock and a torque curve, exported with the GLB. - Interiors with real seats, dashboards and door cards; more part variants per style; paint slots and finishes; a photo mode; two-wheelers. ## What you get - **13 body styles**: `sedan`, `hatchback`, `coupe`, `wagon`, `suv`, `pickup`, `van`, `sports`, `supercar`, `muscle`, `kei`, `buggy` and `truck` (a cab-over with a load box). Each is a preset of the same generator, so any two can be blended: a hatchback 40 % of the way to an SUV is a crossover. - **Real proportions.** Every preset is measured off a real vehicle of its segment: overall dimensions, wheelbase, overhangs, tyre size, and where the windshield, roof and rear glass start and end. The default sedan is 4.85 m long and 1.86 m wide on a 2.84 m wheelbase and 0.69 m tyres. - **Smooth bodies, not boxes.** The sedan's body is a subdivision surface: an authored control cage (`assets/src/sedan.py`) is fitted to the spec, subdivided with creases and split into panels with real gaps. The other styles are lofted from cross-sections driven by profile curves. Either way, hard creases sit where a real car has them: the shoulder, a character line along the flank, the rocker and the windshield base. Panel gaps are real grooves, and the wheel arches are cut into the sills with rolled lips. The glass is flush. Lamps are recessed reflectors behind clear lenses. The car also gets a grille, mirrors, door handles, wipers, licence plates, brake discs and calipers. Tyres have sidewalls and tread, and there are seven rim styles. An interior sits behind tinted, reflective glass. - **Under the panels.** Open the bonnet and there is an engine bay: firewall, inner wings, strut towers, an engine with its cam cover and intake, a radiator and fan, a battery, fluid reservoirs and hoses. The boot has a carpeted load floor and a spare wheel in its well. Hatchbacks, wagons, SUVs, vans and kei cars lift a whole tailgate. The supercar is mid-engined: the engine sits under the rear lid and the bonnet covers a front boot. - **Levels of detail.** For the default sedan, LOD0 is 56,828 triangles, LOD1 is 20,720 and LOD2 is 4,364. A `THREE.LOD` switches between them by distance. Stock vehicles stay under 60k triangles on LOD0, and mods may add up to 6k more. - **A rig that drives.** Wheels spin and the front pair steers with Ackermann geometry. Doors, bonnet and boot (or tailgate) open on hinges, and the steering wheel turns. The body rolls and pitches on its suspension while the wheels stay planted. Head-, tail- and brake lights switch on and off. Every pivot sits at its hinge or axle, so each motion is a single rotation. - **Collision.** You get a convex hull of the body and a two-box compound, plus wheel positions and radii for a raycast-vehicle setup. - **13 composable mods**: `spoiler`, `bodyKit`, `widebody`, `suspension`, `wheelSet`, `bullBar`, `roofRack`, `snorkel`, `exhaust`, `hoodScoop`, `lightBar`, `rollCage` and `mudFlaps`. Each is parametric and fits every body style, because it is placed against the body's own curves. - **Paint.** Finishes are solid, metallic, pearl and matte, built on `MeshPhysicalMaterial` clearcoat and iridescence. You also get two-tone (roof, lower or upper body), twin-stripe, side-stripe and rally liveries, separate rim, trim and caliper colours, curated palettes, and a seeded random that picks believable colours. - **GLB export that round-trips.** Node names, pivots, rig tags, vertex colours, clearcoat and animation clips all survive the export, and `createRig()` drives a re-imported GLB the same way it drives the generated car. ![Doors, bonnet and tailgate open, wheels steered, lights on](media/rig.png) ## Quick start Requires [Bun](https://bun.sh). ```sh bun install bun run dev # the studio at http://localhost:5173 ``` ### Use the library ```ts import { generateVehicle, preset, randomSpec, exportVehicleGlb } from 'vehicle-generator' const car = generateVehicle(preset('hatchback')) scene.add(car.group) // Drive it. car.rig.setSteer(0.3) // radians, positive is left; also turns the steering wheel car.rig.update(dt, speed) // rolls the wheels for a car moving at `speed` m/s car.rig.open('doorFL', 1) // 0 = shut, 1 = open; also 'hood' and 'trunk' car.rig.setBodyPose(roll, pitch) // lean the sprung body; the wheels stay planted car.rig.setBrake(true) car.rig.setHeadlights(true) // Physics. car.collider.hull // convex BufferGeometry of the body car.wheels // hub positions, radius, width, steer/drive flags // Repaint without a rebuild. car.setPaint({ ...car.spec.paint, primary: '#1f3a5f', finish: 'pearl' }) // A random vehicle, and a GLB of it. const other = generateVehicle(randomSpec(1234)) const glb = await exportVehicleGlb(other) // ArrayBuffer, LOD0 plus animation clips ``` The generator imports plain `three`, so it works with `WebGLRenderer` and with `WebGPURenderer`. The frame is glTF's: +Y up, the vehicle faces +Z, +X is its left, and the ground is y = 0. ### Load a GLB in a game and drive it ```ts import { createRig } from 'vehicle-generator' const gltf = await new GLTFLoader().loadAsync('hatchback.glb') const rig = createRig(gltf.scene) // reads the tags stored in glTF extras rig.setSteer(-0.2) rig.open('trunk', 1) ``` Clips named `open_` and `wheels_roll` come with the file, for engines that prefer animation clips to code. ## The spec A `VehicleSpec` is plain JSON: ```jsonc { "style": "pickup", "seed": 42, "body": { "length": 5.3, "width": 1.95, "roofHeight": 1.9, "cowl": 0.72, "bed": { "start": 0.02, "end": 0.42, "depth": 0.5 } }, "wheels": { "radius": 0.4, "width": 0.265, "rim": "spoke", "spokes": 6, "tread": "offroad", "camber": 0 }, "chassis": { "wheelbase": 3.4, "frontOverhang": 0.9, "track": 0.86, "rideHeight": 0, "drive": "rwd" }, "cargo": { "type": "none", "length": 0, "height": 0 }, "paint": { "primary": "#7a1f1f", "finish": "solid", "twoTone": "none", "livery": "none" }, "mods": [{ "kind": "bullBar", "params": { "height": 0.45 } }, { "kind": "suspension", "params": { "height": 0.1 } }] } ``` Start from `preset(style)` and change what you need. Longitudinal stations (`cowl`, `roofFront`, `roofRear`, `deck`) are fractions of the body length measured from the rear, so they stay put when the length changes. `blendSpecs(a, b, t)` interpolates two specs. `randomSpec(seed, { style })` draws one. ### Mods A mod is `{ kind, params }`. `MODS[kind].params` describes its controls (number ranges and option lists), and the studio builds its inspector from that. Some mods change the spec before the build: `widebody` widens the arches and the track, `suspension` moves the body, and `wheelSet` swaps the wheels. The rest add geometry afterwards. Each lands in its own node (`mod_roofRack`, `mod_spoiler`, and so on). Mods mounted on something that opens are parented to it: a boot-lid spoiler rides the lid and a hood scoop rides the bonnet. ## Node layout ``` vehicle_