@adatechnology/game-vehicles-three 0.14.2 → 0.14.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1212,6 +1212,26 @@ function createVehicleRainCovers() {
1212
1212
  };
1213
1213
  }
1214
1214
 
1215
+ // src/test-area/bodywork-effect.ts
1216
+ function bodyworkValues(event, paint) {
1217
+ const direction = event.direction;
1218
+ return {
1219
+ ...event.hole ? { bore: event.hole.diameter } : {},
1220
+ ...paint !== void 0 ? { paint } : {},
1221
+ ...direction ? { dx: direction[0], dy: direction[1], dz: direction[2] } : {}
1222
+ };
1223
+ }
1224
+ var HEAVY_BORE = 0.012;
1225
+ var PISTOL_BORE = 9e-3;
1226
+ var PISTOL_LEVEL = 0.6;
1227
+ var LOUDEST = 1;
1228
+ var QUIETEST = 0.35;
1229
+ function skinHitSound(bore, ids) {
1230
+ const size = bore ?? 0;
1231
+ const volume = Math.min(LOUDEST, Math.max(QUIETEST, PISTOL_LEVEL * (size > 0 ? size / PISTOL_BORE : 0.6)));
1232
+ return { sound: size >= HEAVY_BORE ? ids.skinHitHeavy : ids.skinHit, volume };
1233
+ }
1234
+
1215
1235
  // src/test-area/lane-damage.ts
1216
1236
  import { crashVehicle, createVehicleDestructible } from "@adatechnology/game-vehicles";
1217
1237
  import { conditionFromDamage } from "@adatechnology/game-vehicles-driving";
@@ -1389,13 +1409,24 @@ var withoutPart = (total, part) => {
1389
1409
  };
1390
1410
  var bodyMassOf = (parts) => {
1391
1411
  const mass = parts.reduce((sum, part) => sum + part.mass, 0);
1392
- const centre = [0, 1, 2].map((k) => parts.reduce((sum, part) => sum + part.mass * part.box.center[k], 0) / mass);
1412
+ const at = (part) => part.massCentre ?? part.box.center;
1413
+ const centre = [0, 1, 2].map((k) => parts.reduce((sum, part) => sum + part.mass * at(part)[k], 0) / mass);
1393
1414
  const inertia = [0, 1, 2].map((k) => parts.reduce((sum, part) => {
1394
- const offset = [part.box.center[0] - centre[0], part.box.center[1] - centre[1], part.box.center[2] - centre[2]];
1415
+ const offset = [at(part)[0] - centre[0], at(part)[1] - centre[1], at(part)[2] - centre[2]];
1395
1416
  return sum + boxInertia(part.mass, part.box.size)[k] + lever(part.mass, offset, k);
1396
1417
  }, 0));
1397
1418
  return { mass, centre, inertia };
1398
1419
  };
1420
+ var partsInFrame = (parts, toFrame) => {
1421
+ const m = toFrame;
1422
+ const at = (row, column) => m[column * 4 + row];
1423
+ const moved = ([x, y, z]) => [0, 1, 2].map((row) => at(row, 0) * x + at(row, 1) * y + at(row, 2) * z + at(row, 3));
1424
+ return parts.map((part) => {
1425
+ const [sx, sy, sz] = part.box.size;
1426
+ const size = [0, 1, 2].map((row) => Math.abs(at(row, 0)) * sx + Math.abs(at(row, 1)) * sy + Math.abs(at(row, 2)) * sz);
1427
+ return { ...part, box: { ...part.box, center: moved(part.box.center), size }, ...part.massCentre ? { massCentre: moved(part.massCentre) } : {} };
1428
+ });
1429
+ };
1399
1430
 
1400
1431
  // src/crash-parts-bodies.ts
1401
1432
  var CLEAR_CHECK_STEPS = 30;
@@ -1707,11 +1738,61 @@ var DEFAULT_HULL_CEILING = 550;
1707
1738
  var HALF_TURN = Math.SQRT1_2;
1708
1739
  var wheelChildOf = (size, center) => {
1709
1740
  const axis = size[0] <= size[1] && size[0] <= size[2] ? 0 : size[2] <= size[1] ? 2 : 1;
1710
- const radius = Math.max(...size.filter((_, side) => side !== axis)) / 2;
1741
+ const radius = axis === 1 ? Math.max(size[0], size[2]) / 2 : size[1] / 2;
1711
1742
  const shape = { kind: "cylinder", halfHeight: size[axis] / 2, radius };
1712
1743
  if (axis === 1) return { shape, position: center };
1713
1744
  return { shape, position: center, rotation: axis === 0 ? [0, 0, HALF_TURN, HALF_TURN] : [HALF_TURN, 0, 0, HALF_TURN] };
1714
1745
  };
1746
+ var lowestOf = (shape) => {
1747
+ if (shape.kind !== "compound") return 0;
1748
+ let lowest = Infinity;
1749
+ for (const child of shape.children) {
1750
+ const own = child.shape;
1751
+ if (own.kind === "convex") for (let index = 1; index < own.points.length; index += 3) lowest = Math.min(lowest, own.points[index] + child.position[1]);
1752
+ else if (own.kind === "cylinder") lowest = Math.min(lowest, child.position[1] - (child.rotation ? own.radius : own.halfHeight));
1753
+ }
1754
+ return Number.isFinite(lowest) ? lowest : 0;
1755
+ };
1756
+ var tyreBoxesOf = (THREE, objects, toFrame) => {
1757
+ const merged = [];
1758
+ for (const object of objects) {
1759
+ const box = frameBoxOf(THREE, object, toFrame);
1760
+ if (box.isEmpty()) continue;
1761
+ const same = merged.find((other) => other.intersectsBox(box) && overlapShare(THREE, other, box) > TYRE_SAME_SHARE);
1762
+ if (same) same.union(box);
1763
+ else merged.push(box);
1764
+ }
1765
+ return merged.map((box) => ({ size: box.getSize(new THREE.Vector3()).toArray(), center: box.getCenter(new THREE.Vector3()).toArray() }));
1766
+ };
1767
+ var frameBoxOf = (THREE, object, toFrame) => {
1768
+ const box = new THREE.Box3();
1769
+ const matrix = new THREE.Matrix4();
1770
+ const vertex = new THREE.Vector3();
1771
+ object.updateWorldMatrix(true, true);
1772
+ object.traverse((child) => {
1773
+ const position = child.isMesh ? child.geometry.getAttribute("position") : void 0;
1774
+ if (!position) return;
1775
+ matrix.multiplyMatrices(toFrame, child.matrixWorld);
1776
+ for (let index = 0; index < position.count; index++) box.expandByPoint(vertex.fromBufferAttribute(position, index).applyMatrix4(matrix));
1777
+ });
1778
+ return box;
1779
+ };
1780
+ var TYRE_SAME_SHARE = 0.5;
1781
+ var overlapShare = (THREE, a, b) => {
1782
+ const shared = a.clone().intersect(b).getSize(new THREE.Vector3());
1783
+ const volume = (box) => {
1784
+ const size = box.getSize(new THREE.Vector3());
1785
+ return size.x * size.y * size.z;
1786
+ };
1787
+ return shared.x * shared.y * shared.z / Math.max(1e-9, Math.min(volume(a), volume(b)));
1788
+ };
1789
+ var framePartsOf = (THREE, vehicle, root) => {
1790
+ const drawn = vehicle.vehicle.vehicle;
1791
+ root.updateWorldMatrix(true, false);
1792
+ drawn.updateWorldMatrix(true, false);
1793
+ const toFrame = new THREE.Matrix4().copy(root.matrixWorld).invert().multiply(drawn.matrixWorld);
1794
+ return partsInFrame(vehicle.model.parts, toFrame.elements);
1795
+ };
1715
1796
  var runOverHalfOf = (parts) => {
1716
1797
  const half = [0, 0, 0];
1717
1798
  for (const { box } of parts) {
@@ -1741,21 +1822,32 @@ var createPhysicalCar = ({ THREE, services, vehicle, root, colliders, runOver })
1741
1822
  let crash;
1742
1823
  let chassis;
1743
1824
  let stopRunOver;
1825
+ let tyres;
1826
+ const tyresOf = () => {
1827
+ if (tyres) return tyres;
1828
+ root.updateWorldMatrix(true, true);
1829
+ const inverse = new THREE.Matrix4().copy(root.matrixWorld).invert();
1830
+ tyres = new Map(vehicle.model.parts.filter((part) => part.role === "tyre").map((part) => [part, tyreBoxesOf(THREE, vehicle.objectsOf(part.id), inverse)]));
1831
+ return tyres;
1832
+ };
1833
+ const standOnTyres = () => {
1834
+ let lowest = 0;
1835
+ for (const boxes of tyresOf().values()) for (const box of boxes) lowest = Math.min(lowest, box.center[1] - box.size[1] / 2);
1836
+ if (lowest === 0) return;
1837
+ root.position.add(new THREE.Vector3(0, -lowest, 0).applyQuaternion(root.quaternion));
1838
+ root.updateMatrixWorld(true);
1839
+ };
1744
1840
  const withWheels = (hulls) => {
1745
1841
  if (hulls.kind !== "compound") return hulls;
1746
- const tyres = vehicle.model.parts.filter((part) => part.role === "tyre");
1747
- tyres.forEach((part, index) => wheelHandles.set(part.id, hulls.children.length + index));
1748
- const inverse = new THREE.Matrix4().copy(root.matrixWorld).invert();
1749
- const drawn = new THREE.Box3();
1750
- const piece = new THREE.Box3();
1751
- const centre = new THREE.Vector3();
1752
- const size = new THREE.Vector3();
1753
- const children = tyres.map((part) => {
1754
- drawn.makeEmpty();
1755
- for (const object of vehicle.objectsOf(part.id)) drawn.union(piece.setFromObject(object).applyMatrix4(inverse));
1756
- if (drawn.isEmpty()) return wheelChildOf(part.box.size, part.box.center);
1757
- return wheelChildOf(drawn.getSize(size).toArray(), drawn.getCenter(centre).toArray());
1758
- });
1842
+ const children = [];
1843
+ for (const [part, boxes] of tyresOf()) {
1844
+ const handles = [];
1845
+ for (const box of boxes.length > 0 ? boxes : [{ size: part.box.size, center: part.box.center }]) {
1846
+ handles.push(hulls.children.length + children.length);
1847
+ children.push(wheelChildOf(box.size, box.center));
1848
+ }
1849
+ wheelHandles.set(part.id, handles);
1850
+ }
1759
1851
  return { ...hulls, children: [...hulls.children, ...children] };
1760
1852
  };
1761
1853
  const build = () => {
@@ -1766,7 +1858,8 @@ var createPhysicalCar = ({ THREE, services, vehicle, root, colliders, runOver })
1766
1858
  if (colliders.isHullPending()) return;
1767
1859
  root.updateWorldMatrix(true, false);
1768
1860
  root.matrixWorld.decompose(position, rotation, unusedScale);
1769
- const mass = bodyMassOf(vehicle.model.parts);
1861
+ const parts = framePartsOf(THREE, vehicle, root);
1862
+ const mass = bodyMassOf(parts);
1770
1863
  crash = createCrashParts({
1771
1864
  THREE,
1772
1865
  world,
@@ -1775,22 +1868,24 @@ var createPhysicalCar = ({ THREE, services, vehicle, root, colliders, runOver })
1775
1868
  colliders,
1776
1869
  mass,
1777
1870
  onPartLost: (part) => {
1778
- const handle = wheelHandles.get(part.id);
1779
- if (handle !== void 0 && chassis !== void 0) world.setChildShape(chassis, handle, null);
1871
+ if (chassis === void 0) return;
1872
+ for (const handle of wheelHandles.get(part.id) ?? []) world.setChildShape(chassis, handle, null);
1780
1873
  }
1781
1874
  });
1875
+ const shape = withWheels(crash.shape());
1876
+ const lift = new THREE.Vector3(0, Math.max(0, -lowestOf(shape)), 0).applyQuaternion(rotation);
1782
1877
  chassis = world.createBody({
1783
- shape: withWheels(crash.shape()),
1878
+ shape,
1784
1879
  motion: "dynamic",
1785
1880
  mass: mass.mass,
1786
1881
  inertia: mass.inertia,
1787
1882
  layer: "vehicle",
1788
1883
  user: 0,
1789
- position: [position.x, position.y, position.z],
1884
+ position: [position.x + lift.x, position.y + lift.y, position.z + lift.z],
1790
1885
  rotation: [rotation.x, rotation.y, rotation.z, rotation.w]
1791
1886
  });
1792
1887
  crash.bind(chassis);
1793
- stopRunOver = runOver?.track({ body: chassis, half: runOverHalfOf(vehicle.model.parts) });
1888
+ stopRunOver = runOver?.track({ body: chassis, half: runOverHalfOf(parts) });
1794
1889
  };
1795
1890
  const follow = () => {
1796
1891
  world.readPose(chassis, pose, 0);
@@ -1803,6 +1898,7 @@ var createPhysicalCar = ({ THREE, services, vehicle, root, colliders, runOver })
1803
1898
  };
1804
1899
  const stopPhase = services.loop.add({
1805
1900
  afterSteps: (_dt, frame) => {
1901
+ if (!chassis && root.parent && !tyres) standOnTyres();
1806
1902
  if (!chassis) return build();
1807
1903
  if (frame.steps > 0 && root.parent) follow();
1808
1904
  }
@@ -2331,12 +2427,12 @@ var defaultEffectIds = {
2331
2427
  ignite: "volumetric_fire",
2332
2428
  explode: "car_fuel_tank",
2333
2429
  glass: "glass_burst",
2334
- sparks: "impact_metal",
2430
+ sparks: "impact_bodywork",
2335
2431
  leak: "vehicle_leak",
2336
2432
  leakIgnited: "vehicle_leak_ignited",
2337
2433
  exhaust: "vehicle_exhaust"
2338
2434
  };
2339
- var defaultSoundIds = { horn: "horn_car", ...DEFAULT_VEHICLE_SOUND_KEYS };
2435
+ var defaultSoundIds = { horn: "horn_car", skinHit: "metal_thin", skinHitHeavy: "metal_bullet", ...DEFAULT_VEHICLE_SOUND_KEYS };
2340
2436
  var DEFAULT_TEST_WRECKS = [
2341
2437
  { key: "sedan_burnt", vehicle: "midsize_sedan", scenario: "burnt" },
2342
2438
  { key: "hatch_burnt", vehicle: "compact_hatch", scenario: "burnt" }
@@ -2765,7 +2861,10 @@ function createVehiclesTestModule(options = {}) {
2765
2861
  const r = root;
2766
2862
  const kit = r.userData.kit;
2767
2863
  const d = createVehicleDestructible2(kit, r.userData.vehicle, { id, frame: root, name: text.items[itemKey] ?? key, creaseQueue, puddles: false });
2768
- tapEffects(d);
2864
+ tapEffects(d, () => {
2865
+ const entry = placed.get(root.uuid);
2866
+ return (entry?.paint ?? primaryPaint(r.userData.vehicle))?.color.getHex();
2867
+ });
2769
2868
  const services = physicsServicesOf(host);
2770
2869
  if (services) {
2771
2870
  const colliders = physicalCarColliders(kit.THREE, d, root, { budget: COLLIDER_BUDGET, fleetKey: key, hullCeilingOf: fleet.hullCeilingOf, planCache, workers: host?.workers });
@@ -2826,8 +2925,8 @@ function createVehiclesTestModule(options = {}) {
2826
2925
  }
2827
2926
  return clipCache.get(key);
2828
2927
  };
2829
- const tapEffects = (d) => {
2830
- const request = (events) => {
2928
+ const tapEffects = (d, paint) => {
2929
+ const request = (events, ballistic = false) => {
2831
2930
  const fx = effects();
2832
2931
  if (!fx) return;
2833
2932
  for (const e of events) {
@@ -2837,13 +2936,17 @@ function createVehiclesTestModule(options = {}) {
2837
2936
  else if (e.type === "explode") fx.play({ effect: effectIds.explode, point: position });
2838
2937
  else if (e.type === "leak") fx.play({ effect: effectIds.leak, point: position });
2839
2938
  else if (e.type === "fracture" && /glass|window|windshield/.test(e.part ?? "")) fx.play({ effect: effectIds.glass, point: position });
2840
- else if (e.type === "hole") fx.play({ effect: /glass|window|windshield/.test(e.part ?? "") ? effectIds.glass : effectIds.sparks, point: position });
2939
+ else if (e.type === "hole" && /glass|window|windshield/.test(e.part ?? "")) fx.play({ effect: effectIds.glass, point: position });
2940
+ else if (e.type === "hole") {
2941
+ fx.play({ effect: effectIds.sparks, point: position, ...e.worldNormal ? { normal: e.worldNormal } : {}, values: bodyworkValues(e, paint()) });
2942
+ audio()?.play({ ...skinHitSound(e.hole?.diameter, soundIds), position, urgent: true });
2943
+ } else if (e.type === "dent" && ballistic) audio()?.play({ ...skinHitSound(void 0, soundIds), position, urgent: true });
2841
2944
  }
2842
2945
  };
2843
2946
  const hit = d.hit.bind(d);
2844
2947
  d.hit = (h) => {
2845
2948
  const events = hit(h);
2846
- request(events);
2949
+ request(events, h.type === "ballistic");
2847
2950
  return events;
2848
2951
  };
2849
2952
  if (d.update) {
@@ -5363,4 +5466,4 @@ export {
5363
5466
  createProvingModule,
5364
5467
  loadProvingModule
5365
5468
  };
5366
- //# sourceMappingURL=chunk-JLZI4QVH.js.map
5469
+ //# sourceMappingURL=chunk-CSHXBI4Q.js.map