SOURCE / PINNED RELEASE
Made of little things.
Drift City
- Release
- 098731aa0f0f…
- Author-recorded commit
- 2822ea0a4f5b…
- License
- LICENSE
- Author’s source reference
- nostr://npub12lm3rxsva8kfempyjkxupt09e02n3fezurkyadyqfejdc5e0r2kscqty6r/wss%3A%2F%2Fgit.napplet.soy%2F/n-a4c0039c855
Archive hash verified: b3323e408235502b…. The source-to-build association is the author’s claim; it has not been independently rebuilt.
import * as THREE from 'three';
import { NIGHT, NIGHT_GLSL, withLampLight } from './night.js';
/**
* Traffic vehicles, ported from Pursuit PIT's renderer (src/game/render.ts makeCarView and
* makeRigView): a sedan, a sports car, a pickup, a lifted truck, a box truck, and
* tractor-trailers (a box semi, a fuel tanker and a logging truck). Real sizes, in metres.
*
* Each car is a dark chassis core with ten knock-off panels over it (bumpers, hood, trunk,
* fenders, doors, rear quarters) that can be torn off in a crash, a tinted glasshouse, and
* the fixed details (wheels, lights, grille, mirrors, plates) merged into one mesh.
*
* Pursuit PIT models face -z with x to the right; here the model is built exactly as there
* inside a group turned half round, so it faces +z with x to the left, like the player's car.
*/
export type VehicleType = 'sedan' | 'sports' | 'pickup' | 'lifted' | 'box' | 'semi' | 'tanker' | 'logger';
export const VEHICLES: Record<VehicleType, { len: number; wid: number; mass: number; share: number }> = {
sedan: { len: 4.7, wid: 1.9, mass: 1500, share: 40 },
sports: { len: 4.3, wid: 1.85, mass: 1250, share: 10 },
pickup: { len: 5.5, wid: 2.0, mass: 2300, share: 15 },
lifted: { len: 5.6, wid: 2.15, mass: 2800, share: 7 },
box: { len: 6.5, wid: 2.2, mass: 2600, share: 10 },
semi: { len: 17, wid: 2.5, mass: 16000, share: 4 },
tanker: { len: 17, wid: 2.5, mass: 18000, share: 3 },
logger: { len: 17, wid: 2.5, mass: 17000, share: 3 },
};
export const RIGS = new Set<VehicleType>(['semi', 'tanker', 'logger']);
/** Where a rig's trailer pivots, from the front of the tractor (metres). */
export const RIG_HITCH = 5.2;
/** Paint colours for traffic (Pursuit PIT's suspect colours). */
export const TRAFFIC_COLORS = [0xc0392b, 0x2e6fd8, 0xf2f2ee, 0x1e1f24, 0x8a9199, 0xe8c547, 0x2e8b57, 0x7a3f9a, 0xd8752a, 0x5a3b26, 0x9fb7cf, 0xb3262e];
/** A body panel that can be knocked off. lat is to the right, lon forward (Pursuit PIT axes). */
export interface Part {
name: string;
lat: number;
lon: number;
y: number;
w: number;
l: number;
h: number;
}
export function partsFor(len: number, wid: number): Part[] {
const side = wid / 2 - 0.05;
return [
{ name: 'front bumper', lat: 0, lon: len / 2 - 0.15, y: 0.45, w: wid, l: 0.3, h: 0.3 },
{ name: 'rear bumper', lat: 0, lon: -(len / 2 - 0.15), y: 0.45, w: wid, l: 0.3, h: 0.3 },
{ name: 'hood', lat: 0, lon: len * 0.3, y: 0.89, w: wid * 0.92, l: len * 0.3, h: 0.07 },
{ name: 'trunk', lat: 0, lon: -len * 0.34, y: 0.89, w: wid * 0.92, l: len * 0.22, h: 0.07 },
{ name: 'left front fender', lat: -side, lon: len * 0.29, y: 0.62, w: 0.1, l: len * 0.26, h: 0.5 },
{ name: 'right front fender', lat: side, lon: len * 0.29, y: 0.62, w: 0.1, l: len * 0.26, h: 0.5 },
{ name: 'left door', lat: -side, lon: 0, y: 0.62, w: 0.1, l: len * 0.32, h: 0.5 },
{ name: 'right door', lat: side, lon: 0, y: 0.62, w: 0.1, l: len * 0.32, h: 0.5 },
{ name: 'left rear quarter', lat: -side, lon: -len * 0.29, y: 0.62, w: 0.1, l: len * 0.26, h: 0.5 },
{ name: 'right rear quarter', lat: side, lon: -len * 0.29, y: 0.62, w: 0.1, l: len * 0.26, h: 0.5 },
];
}
export interface VehicleModel {
type: VehicleType;
len: number;
wid: number;
/** The whole vehicle: origin on the ground at its centre, facing +z. */
root: THREE.Group;
/** Knock-off panels (cars only), matching parts. */
panels: THREE.Mesh[];
parts: Part[];
/** Lights and trim that go with a panel (panel index, mesh). */
attached: [number, THREE.Mesh][];
/** Brake lights, lit while braking. */
brakes: THREE.Mesh[];
/** The chassis core and the cabin, darkened when wrecked. */
core: THREE.Mesh;
cabin: THREE.Mesh;
/** A rig's trailer, turning on the fifth wheel (rotation.y is its angle to the tractor). */
trailer?: THREE.Group;
/**
* The parts merged into one mesh (one for the tractor or car, one for a rig's trailer)
* and where each part's triangles are in it, so one can be shown again on its own and
* dropped from the merge (a panel torn off), or darkened (a wreck).
*/
merged: THREE.Mesh[];
ranges: Map<THREE.Object3D, { mesh: THREE.Mesh; start: number; count: number }>;
}
const BOX_HOOD_TILT = -0.18;
const bodyLift = (type: VehicleType) => (type === 'lifted' ? 0.38 : type === 'sports' ? -0.07 : 0);
function shade(color: number, k: number): number {
return new THREE.Color(color).multiplyScalar(k).getHex();
}
function mergePieces(pieces: { geo: THREE.BufferGeometry; color: number; m: THREE.Matrix4 }[]): THREE.BufferGeometry {
const pos: number[] = [];
const col: number[] = [];
const c = new THREE.Color();
const v = new THREE.Vector3();
for (const p of pieces) {
const g = p.geo.index ? p.geo.toNonIndexed() : p.geo;
const a = g.getAttribute('position');
c.setHex(p.color);
for (let i = 0; i < a.count; i++) {
v.fromBufferAttribute(a, i).applyMatrix4(p.m);
pos.push(v.x, v.y, v.z);
col.push(c.r, c.g, c.b);
}
}
const geo = new THREE.BufferGeometry();
geo.setAttribute('position', new THREE.Float32BufferAttribute(pos, 3));
geo.setAttribute('color', new THREE.Float32BufferAttribute(col, 3));
geo.computeVertexNormals();
return geo;
}
/** A unit box whose top is narrowed and raked front and back: a car's glasshouse. */
function cabinGeometry(rearRake = 0.18): THREE.BufferGeometry {
const geo = new THREE.BoxGeometry(1, 1, 1);
const p = geo.getAttribute('position') as THREE.BufferAttribute;
for (let i = 0; i < p.count; i++) {
if (p.getY(i) <= 0) continue;
p.setX(i, p.getX(i) * 0.86);
p.setZ(i, p.getZ(i) < 0 ? p.getZ(i) + 0.28 : p.getZ(i) - rearRake);
}
geo.computeVertexNormals();
return geo;
}
// Shared geometry and materials.
const boxGeo = new THREE.BoxGeometry(1, 1, 1);
const tireGeo = new THREE.CylinderGeometry(0.31, 0.31, 0.22, 12);
const hubGeo = new THREE.CylinderGeometry(0.17, 0.17, 0.03, 10);
const rodGeo = new THREE.CylinderGeometry(1, 1, 1, 12);
const cabinGeo = cabinGeometry();
const truckCabGeo = cabinGeometry(0.04);
const detailMat = new THREE.MeshLambertMaterial({ vertexColors: true, flatShading: true });
const brakeOff = new THREE.MeshLambertMaterial({ color: 0x5a1210, flatShading: true });
export const brakeOn = new THREE.MeshBasicMaterial({ color: 0xff3326 });
const mats = new Map<number, THREE.MeshLambertMaterial>();
function mat(color: number): THREE.MeshLambertMaterial {
let m = mats.get(color);
if (!m) mats.set(color, (m = new THREE.MeshLambertMaterial({ color, flatShading: true })));
return m;
}
/** A material of its own (for panels and parts that get charred or tinted per car). */
function own(color: number): THREE.MeshLambertMaterial {
return new THREE.MeshLambertMaterial({ color, flatShading: true });
}
function box(parent: THREE.Object3D, color: number | THREE.Material, w: number, h: number, d: number, x: number, y: number, z: number): THREE.Mesh {
const mesh = new THREE.Mesh(boxGeo, typeof color === 'number' ? mat(color) : color);
mesh.scale.set(w, h, d);
mesh.position.set(x, y, z);
mesh.castShadow = true;
parent.add(mesh);
return mesh;
}
type Pieces = { geo: THREE.BufferGeometry; color: number; m: THREE.Matrix4 }[];
const placer = (list: Pieces) => (geo: THREE.BufferGeometry, color: number, x: number, y: number, z: number, sx = 1, sy = 1, sz = 1, rx = 0, ry = 0, rz = 0) =>
list.push({ geo, color, m: new THREE.Matrix4().compose(new THREE.Vector3(x, y, z), new THREE.Quaternion().setFromEuler(new THREE.Euler(rx, ry, rz)), new THREE.Vector3(sx, sy, sz)) });
/** Build a vehicle. id varies small details (a box truck's cargo box colour, a semi's trailer). */
export function buildVehicle(type: VehicleType, color: number, id: number): VehicleModel {
const root = new THREE.Group();
// Built in Pursuit PIT's axes (x right, -z forward), then turned to face +z.
const inner = new THREE.Group();
inner.rotation.y = Math.PI;
root.add(inner);
const model = RIGS.has(type) ? buildRig(inner, type, color, id) : buildCar(inner, type, color, id);
// Merge the parts: a car draws in one call (and its brake lights another) instead of
// two dozen, and looks no different.
const ranges: VehicleModel['ranges'] = new Map();
const keep = new Set<THREE.Object3D>(model.brakes);
const merged = [bake(root, root, model.trailer ? new Set([...keep, model.trailer]) : keep, ranges)];
if (model.trailer) merged.push(bake(model.trailer, model.trailer, keep, ranges));
return { ...model, root, type, len: VEHICLES[type].len, wid: VEHICLES[type].wid, merged, ranges };
}
/** The headlights' colour: those parts glow once the lights come on at night (see NIGHT). */
const HEADLIGHT = 0xf2eedc;
/**
* The one material every merged vehicle shares: its colours are in the vertices, and
* its headlights (the glow attribute) light up at night.
*/
const bakedMat = new THREE.MeshLambertMaterial({ vertexColors: true, flatShading: true });
bakedMat.onBeforeCompile = (shader) => {
shader.uniforms.nightLights = NIGHT;
shader.vertexShader = shader.vertexShader
.replace('#include <common>', '#include <common>\nattribute float glow;\nvarying float vGlow;')
.replace('#include <begin_vertex>', '#include <begin_vertex>\nvGlow = glow;');
shader.fragmentShader = shader.fragmentShader
.replace('#include <common>', `#include <common>\nvarying float vGlow;\n${NIGHT_GLSL}`)
.replace('#include <emissivemap_fragment>', '#include <emissivemap_fragment>\ntotalEmissiveRadiance += vColor * vGlow * nightLights * 3.0;');
};
// (And the street lamps' light, as they pass under them.)
withLampLight(bakedMat);
/** The lights coming on (0 by day to 1 at night): the traffic's tail lights glow too. */
export function setVehicleLights(n: number): void {
brakeOff.emissive.setRGB(0.55 * n, 0.04 * n, 0.03 * n);
}
const _bm = new THREE.Matrix4();
const _bv = new THREE.Vector3();
const _bc = new THREE.Color();
/**
* Merge every mesh under from (in its frame) into one vertex-coloured mesh added to
* into, leaving out anything in skip and below it. The originals stay where they are,
* hidden; ranges records where each one's triangles went.
*/
function bake(from: THREE.Object3D, into: THREE.Object3D, skip: Set<THREE.Object3D>, ranges: VehicleModel['ranges']): THREE.Mesh {
from.updateMatrixWorld(true);
const inv = from.matrixWorld.clone().invert();
const pos: number[] = [];
const col: number[] = [];
const glow: number[] = [];
const parts: [THREE.Mesh, number, number][] = [];
const visit = (o: THREE.Object3D) => {
if (o !== from && skip.has(o)) return;
if (o instanceof THREE.Mesh) {
const g = o.geometry.index ? o.geometry.toNonIndexed() : o.geometry;
const p = g.getAttribute('position');
const vc = g.getAttribute('color');
const mat = o.material as THREE.MeshLambertMaterial;
const lit = mat === mats.get(HEADLIGHT) ? 1 : 0;
_bm.multiplyMatrices(inv, o.matrixWorld);
const start = pos.length / 3;
for (let i = 0; i < p.count; i++) {
_bv.fromBufferAttribute(p, i).applyMatrix4(_bm);
pos.push(_bv.x, _bv.y, _bv.z);
_bc.copy(mat.color);
if (vc && mat.vertexColors) _bc.multiply(new THREE.Color(vc.getX(i), vc.getY(i), vc.getZ(i)));
col.push(_bc.r, _bc.g, _bc.b);
glow.push(lit);
}
if (g !== o.geometry) g.dispose();
parts.push([o, start, p.count]);
o.visible = false;
}
for (const c of o.children) visit(c);
};
visit(from);
const geo = new THREE.BufferGeometry();
geo.setAttribute('position', new THREE.Float32BufferAttribute(pos, 3));
geo.setAttribute('color', new THREE.Float32BufferAttribute(col, 3));
geo.setAttribute('glow', new THREE.Float32BufferAttribute(glow, 1));
geo.computeVertexNormals();
geo.computeBoundingSphere();
const mesh = new THREE.Mesh(geo, bakedMat);
mesh.castShadow = true;
mesh.userData.merged = true;
into.add(mesh);
for (const [o, start, count] of parts) ranges.set(o, { mesh, start, count });
return mesh;
}
/** Show a merged part on its own again (to fling it off) and drop it from the merge. */
export function unmerge(model: VehicleModel, part: THREE.Object3D): void {
const r = model.ranges.get(part);
part.visible = true;
if (!r) return;
const p = r.mesh.geometry.getAttribute('position') as THREE.BufferAttribute;
(p.array as Float32Array).fill(0, r.start * 3, (r.start + r.count) * 3);
p.needsUpdate = true;
model.ranges.delete(part);
}
/** Darken merged parts, as a fire does. */
export function charMerged(model: VehicleModel, parts: THREE.Object3D[], k: number): void {
for (const part of parts) {
const r = model.ranges.get(part);
if (!r) continue;
const c = r.mesh.geometry.getAttribute('color') as THREE.BufferAttribute;
for (let i = r.start * 3; i < (r.start + r.count) * 3; i++) (c.array as Float32Array)[i]! *= k;
c.needsUpdate = true;
}
}
/** Free a vehicle's merged geometry, when it's taken off the road. */
export function disposeVehicle(model: VehicleModel): void {
for (const m of model.merged) m.geometry.dispose();
}
function buildCar(group: THREE.Group, type: VehicleType, color: number, id: number): Omit<VehicleModel, 'root' | 'type' | 'len' | 'wid' | 'merged' | 'ranges'> {
const car = { len: VEHICLES[type].len, wid: VEHICLES[type].wid, color };
const lift = bodyLift(type);
const body = new THREE.Group();
body.position.y = lift;
group.add(body);
const big = type === 'box';
const truck = type === 'pickup' || type === 'lifted';
const cabinLen = big ? car.len * 0.62 : truck ? car.len * 0.3 : type === 'sports' ? car.len * 0.4 : car.len * 0.48;
const cabinZ = big ? car.len * 0.17 : truck ? -car.len * 0.1 : type === 'sports' ? car.len * 0.04 : -car.len * 0.02;
const cabinH = type === 'sports' ? 0.4 : type === 'sedan' ? 0.57 : 0.52;
const cabBase = truck ? 0.32 : 0;
const cabinTop = big ? 2.9 : 0.91 + cabBase + cabinH;
const front = -car.len / 2;
const rear = car.len / 2;
const boxFront = cabinZ - cabinLen / 2;
const cabFront = big ? front + 1.0 : boxFront;
const hoodLen = cabFront - front;
const hoodTop = 0.91 + cabBase;
const parts = partsFor(car.len, car.wid).map((pt) => ({ ...pt }));
if (truck) Object.assign(parts[2]!, { lon: -(front + cabFront) / 2, l: hoodLen - 0.12, y: hoodTop - 0.035 });
if (big) {
Object.assign(parts[2]!, { lon: -(front + cabFront) / 2, l: hoodLen - 0.06, y: 1.16 });
for (const i of [4, 5]) Object.assign(parts[i]!, { lon: -(front + cabFront) / 2, l: hoodLen - 0.2, y: 0.78, h: 0.62 });
for (const i of [6, 7]) Object.assign(parts[i]!, { lon: -(cabFront + boxFront) / 2, l: boxFront - cabFront - 0.1, y: 0.95, h: 0.8 });
}
const core = box(body, own(shade(car.color, 0.45)), car.wid - 0.18, 0.5, car.len - 0.55, 0, 0.6, 0);
const panelMat = own(car.color);
const panels = parts.map((pt) => box(body, panelMat, pt.w, pt.h, pt.l, pt.lat, pt.y, -pt.lon));
if (big) panels[2]!.rotation.x = BOX_HOOD_TILT;
let cabin: THREE.Mesh;
if (big) {
cabin = box(body, own([0xeeeeea, 0xdcdfe2, car.color][id % 3]!), car.wid, 1.95, cabinLen, 0, 1.925, cabinZ);
} else {
cabin = new THREE.Mesh(truck ? truckCabGeo : cabinGeo, own(0x2a3340));
cabin.scale.set(car.wid * 0.86, cabinH, cabinLen);
cabin.position.set(0, 0.91 + cabBase + cabinH / 2, cabinZ);
cabin.castShadow = true;
body.add(cabin);
}
const attached: [number, THREE.Mesh][] = [];
const lampY = truck ? 0.98 : big ? 0.92 : 0.72;
const brakes: THREE.Mesh[] = [];
for (const side of [-1, 1]) {
// Tail lenses double as the brake lights here.
const tail = box(body, brakeOff, 0.42, 0.16, 0.04, side * (car.wid / 2 - 0.3), 0.75, rear + 0.012);
brakes.push(tail);
attached.push([1, tail]);
attached.push([0, box(body, 0xf2eedc, truck ? 0.3 : 0.36, truck ? 0.18 : 0.12, 0.04, side * (car.wid / 2 - (truck ? 0.27 : 0.32)), lampY, front - 0.012)]);
attached.push([side < 0 ? 6 : 7, box(body, 0x222326, 0.014, 0.026, 0.13, side * (car.wid / 2 + 0.006), big ? 1.15 : 0.78, -parts[6]!.lon - car.len * 0.02)]);
}
attached.push([0, box(body, 0xe6e1cc, 0.42, 0.12, 0.02, 0, 0.47, front - 0.008)]);
attached.push([0, box(body, 0x141416, car.wid * 0.5, 0.07, 0.02, 0, 0.36, front - 0.008)]);
const pieces: Pieces = [];
const at = placer(pieces);
const wheelPieces: Pieces = [];
const wheelAt = placer(wheelPieces);
const axles = big ? [-0.34, 0.18, 0.36] : [-0.32, 0.32];
const wheelR = type === 'lifted' ? 0.46 : 0.31;
const tread = type === 'lifted' ? 1.5 : type === 'sports' ? 1.15 : 1;
const r = wheelR / 0.31;
const hub = 0xb6bbc1;
for (const side of [-1, 1]) {
const wx = side * (car.wid / 2 - 0.1 + (type === 'lifted' ? 0.08 : 0));
for (const a of axles) {
wheelAt(tireGeo, 0x141416, wx, wheelR, a * car.len, r, tread, r, 0, 0, Math.PI / 2);
wheelAt(hubGeo, hub, wx + side * 0.115 * tread, wheelR, a * car.len, r, 1, r, 0, 0, Math.PI / 2);
}
if (big) {
at(boxGeo, 0x1d1e21, side * (car.wid / 2 + 0.06), 1.72, cabFront + 0.3, 0.14, 0.03, 0.03);
at(boxGeo, 0x1d1e21, side * (car.wid / 2 + 0.14), 1.62, cabFront + 0.3, 0.06, 0.4, 0.18);
} else {
const mz = cabinZ - cabinLen * 0.36;
at(boxGeo, car.color, side * (car.wid / 2 + 0.05), 0.98 + cabBase, mz, 0.12, 0.09, 0.15);
at(boxGeo, 0x1d1e21, side * (car.wid / 2 - 0.04), 0.9 + cabBase, mz, 0.12, 0.08, 0.05);
}
at(boxGeo, 0x1a1b1e, side * (car.wid / 2 - (truck ? 0.27 : 0.32)), lampY, front - 0.004, truck ? 0.36 : 0.42, truck ? 0.24 : 0.16, 0.02);
at(boxGeo, 0xff9a1f, side * (car.wid / 2 - 0.07), truck ? lampY - 0.16 : lampY, front - 0.006, 0.08, 0.1, 0.02);
at(boxGeo, 0x1d1e21, side * (car.wid / 2 - 0.03), 0.36, 0, 0.06, 0.06, car.len * 0.36);
}
at(boxGeo, car.color, 0, 0.73, front + 0.2, car.wid - 0.06, 0.3, 0.4);
at(boxGeo, car.color, 0, 0.73, rear - 0.2, car.wid - 0.06, 0.3, 0.4);
if (!truck && !big) at(boxGeo, 0x1a1b1e, 0, 0.72, front - 0.006, car.wid * 0.42, 0.16, 0.03);
at(boxGeo, 0xe6e1cc, 0, 0.72, rear + 0.012, 0.42, 0.13, 0.02);
if (type === 'sedan' || type === 'sports') brakes.push(box(body, brakeOff, 0.3, 0.03, 0.04, 0, 0.893, rear - 0.06));
at(boxGeo, 0x2e2f33, car.wid / 2 - 0.45, 0.32, rear - 1.0, 0.26, 0.12, 0.45);
at(boxGeo, 0x3a3b3f, car.wid / 2 - 0.45, 0.31, rear - 0.45, 0.07, 0.07, 0.95);
if (big) {
const cabLen = boxFront - cabFront;
const rake = 0.46;
at(boxGeo, car.color, 0, 0.82, front + hoodLen / 2, car.wid - 0.1, 0.54, hoodLen);
at(boxGeo, car.color, 0, 1.0, front + hoodLen / 2, car.wid - 0.12, 0.26, hoodLen - 0.04, BOX_HOOD_TILT);
at(boxGeo, car.color, 0, 0.95, cabFront + cabLen / 2, car.wid * 0.96, 0.8, cabLen);
at(boxGeo, car.color, 0, 1.7, cabFront + 0.35 + (cabLen - 0.35) / 2, car.wid * 0.92, 0.7, cabLen - 0.35);
at(boxGeo, car.color, 0, 1.7 - 0.18 * Math.sin(rake), cabFront + 0.175 + 0.18 * Math.cos(rake), car.wid * 0.92, 0.78, 0.36, rake);
at(boxGeo, 0x2a3340, 0, 1.7, cabFront + 0.175 - 0.005, car.wid * 0.86, 0.74, 0.04, rake);
for (const side of [-1, 1]) at(boxGeo, 0x2a3340, side * (car.wid * 0.46 + 0.01), 1.68, cabFront + 0.35 + (cabLen - 0.35) * 0.45, 0.03, 0.46, (cabLen - 0.35) * 0.7);
const boxColor = (cabin.material as THREE.MeshLambertMaterial).color.getHex();
at(boxGeo, boxColor, 0, 2.5, boxFront - 0.4, car.wid * 0.96, 0.8, 0.8);
at(boxGeo, 0x9a9ea6, 0, 1.9, rear + 0.01, 0.03, 1.8, 0.02);
at(boxGeo, 0x16171a, 0, 0.8, front - 0.01, car.wid * 0.5, 0.26, 0.03);
attached.push([0, box(body, 0x9a9ea6, car.wid + 0.02, 0.2, 0.14, 0, 0.47, front - 0.05)]);
} else {
at(boxGeo, car.color, 0, cabinTop + 0.018, cabinZ + 0.05 * cabinLen, car.wid * 0.86 * 0.84, 0.036, cabinLen * 0.53);
}
if (!truck && !big) {
// Painted pillars and roof rails over the glasshouse, so it reads as a car's cabin
// with a windscreen and side windows rather than one tinted wedge.
const strut = (x0: number, y0: number, z0: number, x1: number, y1: number, z1: number, w: number, d: number, color: number) => {
const dir = new THREE.Vector3(x1 - x0, y1 - y0, z1 - z0);
const len = dir.length();
const q = new THREE.Quaternion().setFromUnitVectors(new THREE.Vector3(0, 1, 0), dir.normalize());
pieces.push({ geo: boxGeo, color, m: new THREE.Matrix4().compose(new THREE.Vector3((x0 + x1) / 2, (y0 + y1) / 2, (z0 + z1) / 2), q, new THREE.Vector3(w, len, d)) });
};
const sx = car.wid * 0.86;
const yb = 0.91;
const yt = 0.91 + cabinH;
// The glasshouse's corners (cabinGeometry): front and rear, at the sill and at the roof.
const zfb = cabinZ - 0.5 * cabinLen;
const zft = cabinZ - 0.22 * cabinLen;
const zrb = cabinZ + 0.5 * cabinLen;
const zrt = cabinZ + 0.32 * cabinLen;
const xAt = (y: number) => 0.5 * sx - ((y - yb) / cabinH) * 0.07 * sx + 0.012;
const xb = xAt(yb);
const xt = xAt(yt);
const zb = cabinZ + 0.04 * cabinLen;
const zbt = cabinZ + 0.06 * cabinLen;
for (const side of [-1, 1]) {
strut(side * xb, yb, zfb + 0.02, side * xt, yt - 0.05, zft - 0.02, 0.05, 0.08, car.color);
strut(side * xb, yb, zb, side * xt, yt - 0.04, zbt, 0.05, 0.13, car.color);
// A broad C-pillar, as on a three-box saloon.
strut(side * xb, yb, zrb - 0.2, side * xt, yt - 0.1, zrt - 0.1, 0.05, type === 'sedan' ? 0.34 : 0.2, car.color);
strut(side * xt, yt - 0.03, zft, side * xt, yt - 0.03, zrt, 0.05, 0.07, car.color);
// The waistline trim under the windows.
strut(side * (xb + 0.004), yb + 0.02, zfb + 0.05, side * (xb + 0.004), yb + 0.02, zrb - 0.05, 0.03, 0.04, 0x1d1e21);
}
// The screen surrounds.
strut(-xt, yt - 0.03, zft, xt, yt - 0.03, zft, 0.06, 0.06, car.color);
strut(-xt, yt - 0.03, zrt, xt, yt - 0.03, zrt, 0.06, 0.06, car.color);
}
if (truck) {
const steel = type === 'lifted' ? 0x1b1c1f : 0xb8bcc2;
at(boxGeo, car.color, 0, (0.58 + hoodTop - 0.07) / 2, front + hoodLen / 2, car.wid - 0.06, hoodTop - 0.07 - 0.58, hoodLen);
at(boxGeo, steel, 0, 0.92, front - 0.01, car.wid * 0.56, 0.44, 0.03);
at(boxGeo, 0x16171a, 0, 0.92, front - 0.02, car.wid * 0.48, 0.34, 0.03);
for (const y of [0.86, 0.98]) at(boxGeo, steel, 0, y, front - 0.035, car.wid * 0.48, 0.035, 0.02);
attached.push([0, box(body, steel, car.wid + 0.04, 0.24, 0.16, 0, 0.5, front - 0.07)]);
if (type === 'pickup') {
attached.push([2, box(body, panelMat, car.wid * 0.42, 0.05, hoodLen * 0.8, 0, hoodTop + 0.02, front + hoodLen / 2)]);
} else {
attached.push([2, box(body, 0x1b1c1f, 0.55, 0.09, 0.5, 0, hoodTop + 0.04, front + hoodLen * 0.55)]);
for (const side of [-1, 1]) attached.push([0, box(body, 0xc23b22, 0.08, 0.06, 0.1, side * 0.5, 0.42, front - 0.18)]);
at(boxGeo, 0x1b1c1f, 0, cabinTop + 0.08, cabFront + 0.3, car.wid * 0.7, 0.09, 0.1);
for (let k = 0; k < 4; k++) at(boxGeo, 0xf2f4ff, (k - 1.5) * car.wid * 0.17, cabinTop + 0.08, cabFront + 0.245, car.wid * 0.14, 0.06, 0.02);
for (const a of axles) for (const side of [-1, 1]) at(boxGeo, 0x1b1c1f, side * (car.wid / 2 + 0.03), 0.6, a * car.len, 0.12, 0.14, 1.15);
}
at(boxGeo, car.color, 0, 0.91 + cabBase / 2, cabinZ, car.wid - 0.08, cabBase, cabinLen);
const bedFront = cabinZ + cabinLen / 2;
const bedLen = rear - bedFront;
const bz = bedFront + bedLen / 2;
at(boxGeo, 0x1e1f22, 0, 0.9, bz, car.wid - 0.16, 0.04, bedLen - 0.04);
for (const side of [-1, 1]) at(boxGeo, car.color, side * (car.wid / 2 - 0.05), 1.03, bz, 0.1, 0.3, bedLen);
at(boxGeo, car.color, 0, 1.03, rear - 0.05, car.wid - 0.2, 0.3, 0.1);
}
if (type === 'lifted') {
for (const side of [-1, 1]) wheelAt(boxGeo, 0x1b1c1f, side * 0.45, lift + 0.3, 0, 0.12, 0.14, car.len * 0.75);
for (const a of axles) {
wheelAt(boxGeo, 0x1b1c1f, 0, wheelR, a * car.len, car.wid - 0.3, 0.12, 0.12);
for (const side of [-1, 1]) wheelAt(boxGeo, 0xc23b22, side * 0.55, (wheelR + lift + 0.3) / 2, a * car.len + 0.18, 0.07, lift + 0.3 - wheelR, 0.07);
}
}
if (type === 'sports') {
at(boxGeo, car.color, 0, 1.0, rear - 0.15, car.wid * 0.82, 0.04, 0.24);
for (const side of [-1, 1]) at(boxGeo, 0x1d1e21, side * car.wid * 0.3, 0.94, rear - 0.15, 0.04, 0.1, 0.06);
}
const details = new THREE.Mesh(mergePieces(pieces), detailMat);
details.castShadow = true;
body.add(details);
const wheels = new THREE.Mesh(mergePieces(wheelPieces), detailMat);
wheels.castShadow = true;
group.add(wheels);
return { panels, parts, attached, brakes, core, cabin };
}
function buildRig(group: THREE.Group, type: VehicleType, color: number, id: number): Omit<VehicleModel, 'root' | 'type' | 'len' | 'wid' | 'merged' | 'ranges'> {
const L = VEHICLES[type].len;
const W = VEHICLES[type].wid;
const front = -L / 2;
const rear = L / 2;
const pieces: Pieces = [];
const at = placer(pieces);
const trailerPieces: Pieces = [];
const tat = placer(trailerPieces);
const hitchZ = front + RIG_HITCH;
const chrome = 0xc9ccd0;
const dark = 0x1b1c1f;
const core = box(group, own(0x232427), W * 0.55, 0.32, 6.0, 0, 0.78, front + 3.1);
at(boxGeo, color, 0, 1.55, front + 1.0, W * 0.84, 1.0, 2.0);
const cab = box(group, own(color), W, 1.9, 2.3, 0, 2.0, front + 3.15);
at(boxGeo, chrome, 0, 1.45, front - 0.02, W * 0.62, 0.85, 0.05);
at(boxGeo, 0x9a9ea6, 0, 0.8, front + 0.1, W + 0.05, 0.32, 0.25);
at(boxGeo, 0x2a3340, 0, 2.47, front + 2.0 - 0.012, W * 0.88, 0.72, 0.03);
for (const side of [-1, 1]) {
at(boxGeo, 0xf2eedc, side * W * 0.3, 1.25, front - 0.012, 0.32, 0.18, 0.03);
at(boxGeo, 0xff9a1f, side * W * 0.4, 1.45, front - 0.012, 0.1, 0.1, 0.03);
at(boxGeo, 0x2a3340, side * (W / 2 + 0.005), 2.45, front + 2.7, 0.02, 0.6, 0.9);
at(boxGeo, dark, side * (W / 2 + 0.1), 2.3, front + 2.05, 0.2, 0.04, 0.04);
at(boxGeo, dark, side * (W / 2 + 0.22), 2.3, front + 2.05, 0.06, 0.4, 0.14);
at(rodGeo, chrome, side * (W / 2 - 0.12), 2.5, front + 4.4, 0.09, 2.4, 0.09);
at(rodGeo, chrome, side * (W / 2 - 0.3), 0.85, front + 3.2, 0.3, 1.2, 0.3, Math.PI / 2, 0, 0);
}
at(boxGeo, dark, 0, 1.0, front + 5.2, W * 0.8, 0.12, 1.6);
const z0 = front + 4.8;
const TL = rear - z0;
const tz = z0 + TL / 2;
let bottom = 1.25;
let cargo: THREE.Group | undefined;
if (type === 'semi') {
tat(boxGeo, [0xe6e8ea, 0xc9ccd0, 0xf0f0ea][id % 3]!, 0, 2.6, tz, W, 2.7, TL);
for (const side of [-1, 1]) tat(boxGeo, color, side * (W / 2 + 0.005), 2.0, tz, 0.01, 0.25, TL * 0.9);
tat(boxGeo, 0x9a9ea6, 0, 2.6, rear + 0.01, 0.03, 2.6, 0.02);
tat(boxGeo, 0x232427, 0, 1.0, tz, W * 0.5, 0.25, TL - 0.4);
} else if (type === 'tanker') {
tat(rodGeo, 0xc8cdd2, 0, 2.3, tz, 1.1, TL - 0.3, 1.1, Math.PI / 2, 0, 0);
for (const z of [z0 + 0.1, rear - 0.1]) tat(rodGeo, 0xaab0b6, 0, 2.3, z, 1.05, 0.12, 1.05, Math.PI / 2, 0, 0);
tat(boxGeo, 0x7d8288, 0, 3.42, tz, 0.5, 0.05, TL * 0.7);
tat(boxGeo, 0x232427, 0, 1.05, tz, W * 0.6, 0.3, TL);
tat(boxGeo, 0xd8352a, 0, 2.2, rear - 0.03, 0.35, 0.35, 0.02, 0, 0, Math.PI / 4);
bottom = 0.9;
} else {
tat(boxGeo, 0x3a2f28, 0, 1.3, tz, W, 0.14, TL);
tat(boxGeo, 0x232427, 0, 1.0, tz, W * 0.5, 0.45, TL - 0.4);
for (let i = 0; i < 4; i++) {
const z = z0 + 0.6 + (i * (TL - 1.2)) / 3;
for (const side of [-1, 1]) tat(boxGeo, 0x2a2b2f, side * (W / 2 - 0.06), 2.1, z, 0.1, 1.5, 0.1);
}
cargo = new THREE.Group();
const logMat = mat(0x6b4a2b);
const endMat = mat(0xb48a5a);
for (const [x, y] of [[-0.7, 1.7], [0, 1.7], [0.7, 1.7], [-0.35, 2.27], [0.35, 2.27]] as const) {
const log = new THREE.Mesh(rodGeo, logMat);
log.rotation.x = Math.PI / 2;
log.scale.set(0.33, TL - 0.5, 0.33);
log.position.set(x, y, tz);
log.castShadow = true;
cargo.add(log);
for (const z of [tz - (TL - 0.5) / 2 - 0.005, tz + (TL - 0.5) / 2 + 0.005]) {
const end = new THREE.Mesh(rodGeo, endMat);
end.rotation.x = Math.PI / 2;
end.scale.set(0.3, 0.01, 0.3);
end.position.set(x, y, z);
cargo.add(end);
}
}
bottom = 1.23;
}
tat(boxGeo, dark, 0, 0.6, rear - 0.15, W * 0.9, 0.14, 0.1);
for (const side of [-1, 1]) tat(boxGeo, dark, side * W * 0.3, (0.6 + bottom) / 2, rear - 0.15, 0.08, bottom - 0.6, 0.08);
tat(boxGeo, 0x232427, 0, 1.0, rear - 0.05, W * 0.95, 0.14, 0.08);
for (const [z, twin, onTrailer] of [
[front + 1.2, 1, 0],
[front + 4.0, 1.8, 0],
[front + 5.2, 1.8, 0],
[rear - 3.2, 1.8, 1],
[rear - 2.0, 1.8, 1],
] as const) {
const put = onTrailer ? tat : at;
for (const side of [-1, 1]) {
put(tireGeo, 0x141416, side * (W / 2 - 0.25), 0.5, z, 0.5 / 0.31, twin, 0.5 / 0.31, 0, 0, Math.PI / 2);
put(hubGeo, chrome, side * (W / 2 - 0.25 + 0.115 * twin), 0.5, z, 0.5 / 0.31, 1, 0.5 / 0.31, 0, 0, Math.PI / 2);
}
}
const details = new THREE.Mesh(mergePieces(pieces), detailMat);
details.castShadow = true;
group.add(details);
const trailer = new THREE.Group();
trailer.position.z = hitchZ;
group.add(trailer);
const trailerDetails = new THREE.Mesh(mergePieces(trailerPieces), detailMat);
trailerDetails.castShadow = true;
trailerDetails.position.z = -hitchZ;
trailer.add(trailerDetails);
if (cargo) {
cargo.position.z = -hitchZ;
trailer.add(cargo);
}
const brakes: THREE.Mesh[] = [];
for (const side of [-1, 1]) brakes.push(box(trailer, brakeOff, 0.3, 0.1, 0.03, side * (W / 2 - 0.25), 1.0, rear - 0.005 - hitchZ));
return { panels: [], parts: [], attached: [], brakes, core, cabin: cab, trailer };
}
/** A random vehicle type, by how common each is on the road. */
export function randomType(rand: () => number, allowRigs: boolean): VehicleType {
const types = (Object.keys(VEHICLES) as VehicleType[]).filter((t) => allowRigs || !RIGS.has(t));
const total = types.reduce((s, t) => s + VEHICLES[t].share, 0);
let pick = rand() * total;
for (const t of types) {
pick -= VEHICLES[t].share;
if (pick <= 0) return t;
}
return 'sedan';
}
