@adatechnology/game-vehicles 0.6.15 → 0.6.17

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.
package/dist/index.js CHANGED
@@ -1016,10 +1016,15 @@ function raycastMesh(THREE, raycaster, mesh, out) {
1016
1016
  if (geo.boundingSphere === null) geo.computeBoundingSphere();
1017
1017
  s.sphere.copy(geo.boundingSphere).applyMatrix4(mesh.matrixWorld);
1018
1018
  if (!raycaster.ray.intersectsSphere(s.sphere)) return;
1019
- const ti = rayIndexOf(geo);
1020
- if (!ti) return;
1021
1019
  s.inverse.copy(mesh.matrixWorld).invert();
1022
1020
  s.local.copy(raycaster.ray).applyMatrix4(s.inverse);
1021
+ intersectTriangles(THREE, raycaster, mesh, out);
1022
+ }
1023
+ function intersectTriangles(THREE, raycaster, mesh, out) {
1024
+ const geo = mesh.geometry;
1025
+ const s = scratchOf(THREE);
1026
+ const ti = rayIndexOf(geo);
1027
+ if (!ti) return;
1023
1028
  const material = mesh.material;
1024
1029
  const back = material.side === THREE.BackSide;
1025
1030
  const cull = material.side === THREE.FrontSide;
@@ -1067,6 +1072,37 @@ function collect(THREE, raycaster, object, recursive, out) {
1067
1072
  }
1068
1073
  if (recursive) for (const child of object.children) collect(THREE, raycaster, child, true, out);
1069
1074
  }
1075
+ function passesThreePrologue(THREE, raycaster, mesh) {
1076
+ const geo = mesh.geometry;
1077
+ const s = scratchOf(THREE);
1078
+ if (geo.boundingSphere === null) geo.computeBoundingSphere();
1079
+ s.sphere.copy(geo.boundingSphere).applyMatrix4(mesh.matrixWorld);
1080
+ s.local.copy(raycaster.ray).recast(raycaster.near);
1081
+ if (!s.sphere.containsPoint(s.local.origin)) {
1082
+ if (s.local.intersectSphere(s.sphere, s.point) === null) return false;
1083
+ if (s.local.origin.distanceToSquared(s.point) > (raycaster.far - raycaster.near) ** 2) return false;
1084
+ }
1085
+ s.inverse.copy(mesh.matrixWorld).invert();
1086
+ s.local.copy(raycaster.ray).applyMatrix4(s.inverse);
1087
+ return geo.boundingBox === null || s.local.intersectsBox(geo.boundingBox);
1088
+ }
1089
+ function alignedRange(mesh) {
1090
+ const geo = mesh.geometry;
1091
+ const count = geo.index ? geo.index.count : geo.attributes.position.count;
1092
+ const first = Math.max(0, geo.drawRange.start);
1093
+ const last = Math.min(count, geo.drawRange.start + geo.drawRange.count);
1094
+ return first % 3 === 0 && (last <= first || (last - first) % 3 === 0);
1095
+ }
1096
+ function raycastMeshesLikeThree(THREE, raycaster, objects) {
1097
+ const out = [];
1098
+ for (const object of objects) {
1099
+ if (!object.layers.test(raycaster.layers)) continue;
1100
+ if (!indexable(THREE, object) || !alignedRange(object)) object.raycast(raycaster, out);
1101
+ else if (passesThreePrologue(THREE, raycaster, object)) intersectTriangles(THREE, raycaster, object, out);
1102
+ }
1103
+ out.sort((x, y) => x.distance - y.distance);
1104
+ return out;
1105
+ }
1070
1106
  function raycastIndexed(THREE, raycaster, objects, recursive = false) {
1071
1107
  const out = [];
1072
1108
  for (const object of objects) collect(THREE, raycaster, object, recursive, out);
@@ -14448,12 +14484,13 @@ function createVehicleDestructible(kit, vehicleCtx, options = {}) {
14448
14484
  for (const child of o.children) collectSurfaces(child);
14449
14485
  };
14450
14486
  const surfaceRay = (ray) => {
14451
- sync();
14487
+ c.vehicle.updateWorldMatrix(true, true);
14452
14488
  surfaceRays.set(new V3(ray.origin[0], ray.origin[1], ray.origin[2]), new V3(ray.direction[0], ray.direction[1], ray.direction[2]));
14453
14489
  surfaceRays.far = ray.maxDistance;
14454
14490
  surfaceMeshes.length = 0;
14455
14491
  for (const child of c.vehicle.children) collectSurfaces(child);
14456
- for (const h of surfaceRays.intersectObjects(surfaceMeshes, false)) {
14492
+ const hits = options.bruteForceRays ? surfaceRays.intersectObjects(surfaceMeshes, false) : raycastMeshesLikeThree(THREE, surfaceRays, surfaceMeshes);
14493
+ for (const h of hits) {
14457
14494
  if (!h.face || !isDrawnSurface(h.object)) continue;
14458
14495
  const p = partOfSurface(h.object, c.vehicle.worldToLocal(h.point.clone()));
14459
14496
  if (!p || p.lost || ray.ignored?.(p.def.id)) continue;