import { Box3, BufferAttribute, BufferGeometry, Mesh, type Object3D, Vector3 } from 'three' import { ConvexHull } from 'three/addons/math/ConvexHull.js' import { tagOf } from './rig.ts' /** * Collision shapes for physics engines. * * `hull` is one convex hull of the body, without the wheels: every engine * with a raycast-vehicle model (Rapier, cannon-es, Ammo, PhysX) wants the * chassis as a convex shape and casts the wheels itself, from the `wheels` * list. `boxes` is the cheaper compound for engines or LOD tiers that would * rather not have a hull: a chassis box and a cabin box. */ export interface VehicleCollider { readonly hull: BufferGeometry readonly boxes: readonly { readonly center: Vector3; readonly size: Vector3 }[] } const MAX_HULL_POINTS = 1500 export function buildCollider( root: Object3D, cabin: { readonly lowY: number; readonly beltY: number; readonly frontS: number; readonly rearS: number }, ): VehicleCollider { root.updateMatrixWorld(true) const inverse = root.matrixWorld.clone().invert() const points: Vector3[] = [] const insideWheel = (object: Object3D): boolean => { for (let o: Object3D | null = object; o && o !== root; o = o.parent) { const tag = tagOf(o) if (tag && (tag.role === 'wheel' || tag.role === 'wheelSpin' || tag.role === 'wheelCamber')) return true } return false } root.traverse((object) => { if (!(object instanceof Mesh) || insideWheel(object)) return const position = object.geometry.getAttribute('position') if (!position) return const stride = Math.max(1, Math.floor(position.count / 400)) for (let i = 0; i < position.count; i += stride) { points.push(new Vector3().fromBufferAttribute(position, i).applyMatrix4(object.matrixWorld).applyMatrix4(inverse)) } }) const sample = points.length > MAX_HULL_POINTS ? points.filter((_, i) => i % Math.ceil(points.length / MAX_HULL_POINTS) === 0) : points const hull = new ConvexHull().setFromPoints(sample) const positions: number[] = [] for (const face of hull.faces) { let edge = face.edge const loop: Vector3[] = [] do { loop.push(edge.head().point) edge = edge.next } while (edge !== face.edge) for (let k = 1; k < loop.length - 1; k += 1) { for (const p of [loop[0]!, loop[k]!, loop[k + 1]!]) positions.push(p.x, p.y, p.z) } } const geometry = new BufferGeometry() geometry.setAttribute('position', new BufferAttribute(new Float32Array(positions), 3)) geometry.computeVertexNormals() geometry.computeBoundingBox() const box = geometry.boundingBox ?? new Box3() const size = box.getSize(new Vector3()) const center = box.getCenter(new Vector3()) const chassisTop = Math.min(box.max.y, cabin.beltY) const chassis = { center: new Vector3(center.x, (cabin.lowY + chassisTop) / 2, center.z), size: new Vector3(size.x, Math.max(0.1, chassisTop - cabin.lowY), size.z), } const boxes = [chassis] if (box.max.y - chassisTop > 0.15) { boxes.push({ center: new Vector3(center.x, (chassisTop + box.max.y) / 2, (cabin.frontS + cabin.rearS) / 2), size: new Vector3(size.x * 0.82, box.max.y - chassisTop, Math.max(0.3, cabin.frontS - cabin.rearS)), }) } return { hull: geometry, boxes } }