| | | 1 | | //======================================================================= |
| | | 2 | | // GravitasQuery3DService.Raycast.cs |
| | | 3 | | //======================================================================= |
| | | 4 | | // MIT License, Copyright (c) 2026-present David Oravsky (mrdav30) |
| | | 5 | | // See LICENSE file in the project root for full license information. |
| | | 6 | | //======================================================================= |
| | | 7 | | |
| | | 8 | | using System.Runtime.CompilerServices; |
| | | 9 | | using FixedMathSharp; |
| | | 10 | | using Gravitas.Colliders; |
| | | 11 | | using Gravitas.CollisionHandling; |
| | | 12 | | using Gravitas.Support; |
| | | 13 | | using GridForge.Grids; |
| | | 14 | | using GridForge.Spatial; |
| | | 15 | | using SwiftCollections; |
| | | 16 | | |
| | | 17 | | namespace Gravitas.Queries; |
| | | 18 | | |
| | | 19 | | /// <summary> |
| | | 20 | | /// Owns 3D raycast, swept-sphere, and X/Z circle query buffers for one <see cref="GravitasWorldContext"/>. |
| | | 21 | | /// </summary> |
| | | 22 | | /// <content>Owns query services, shared buffers, raycasts and swept-source preparation.</content> |
| | | 23 | | public sealed partial class GravitasQuery3DService |
| | | 24 | | { |
| | | 25 | | private readonly GravitasWorldContext _context; |
| | 10191 | 26 | | private readonly RaycastSegmentWorker _worker = new(); |
| | 10191 | 27 | | private readonly SweptSphereQueryWorker _sweepWorker = new(); |
| | 10191 | 28 | | private readonly ConvexSweepQueryWorker _convexSweepWorker = new(); |
| | 10191 | 29 | | private SwiftList<Vector3d> _bufferIntersectionPoints = new(); |
| | 10191 | 30 | | private readonly SwiftList<int> _meshTriangleCandidates = new(16); |
| | 10191 | 31 | | private readonly SwiftList<int> _planarCircleCandidates = new(); |
| | 10191 | 32 | | private readonly SwiftHashSet<int> _redundantColliderCheck = new(); |
| | 10191 | 33 | | private readonly SwiftHashSet<WorldVoxelIndex> _redundantVoxelCheck = new(); |
| | 10191 | 34 | | private readonly SwiftList<Voxel> _coveredVoxels = new(); |
| | 10191 | 35 | | private readonly GridTraceScratch _traceScratch = new(); |
| | | 36 | | |
| | | 37 | | private PhysicsLayerMask _currentLayerMask; |
| | | 38 | | private LSCollider? _currentExcludedCollider; |
| | | 39 | | private LSCollider? _currentSweepSourceCollider; |
| | | 40 | | private ContinuousSurfaceTriangleAdmission? _currentSurfaceAdmission; |
| | | 41 | | private bool _currentStaticSweepTargetsOnly; |
| | | 42 | | private bool _currentIncludeTriggers; |
| | | 43 | | |
| | | 44 | | /// <summary> |
| | | 45 | | /// Initializes a new raycast service for the supplied context. |
| | | 46 | | /// </summary> |
| | | 47 | | /// <param name="context">The owning world context.</param> |
| | 10191 | 48 | | public GravitasQuery3DService(GravitasWorldContext context) |
| | | 49 | | { |
| | 10191 | 50 | | ChronicleGuard.ThrowIfNull(context, nameof(context)); |
| | 10191 | 51 | | _context = context; |
| | 10191 | 52 | | } |
| | | 53 | | |
| | | 54 | | /// <summary> |
| | | 55 | | /// Gets the owning world context. |
| | | 56 | | /// </summary> |
| | 1 | 57 | | public GravitasWorldContext Context => _context; |
| | | 58 | | |
| | | 59 | | /// <summary> |
| | | 60 | | /// Gets the context-local raycast/sweep query version. |
| | | 61 | | /// </summary> |
| | | 62 | | public uint RaycastVersion { get; internal set; } |
| | | 63 | | |
| | | 64 | | /// <summary> |
| | | 65 | | /// Gets the context-local partition-traversed sphere/cone deduplication version. |
| | | 66 | | /// </summary> |
| | | 67 | | public uint CircleVersion { get; internal set; } |
| | | 68 | | |
| | | 69 | | internal int LastQueryCandidateCount { get; private set; } |
| | | 70 | | |
| | | 71 | | internal int LastMeshTriangleCandidateCount { get; private set; } |
| | | 72 | | |
| | | 73 | | /// <summary> |
| | | 74 | | /// Resets context-local raycast query buffers. |
| | | 75 | | /// </summary> |
| | | 76 | | public void Reset() |
| | | 77 | | { |
| | 114 | 78 | | EndQuery(); |
| | 114 | 79 | | ResetColliderRaycastVersions(); |
| | 114 | 80 | | ResetColliderCircleVersions(); |
| | 114 | 81 | | RaycastVersion = 0; |
| | 114 | 82 | | CircleVersion = 0; |
| | 114 | 83 | | ResetLastQueryCounters(); |
| | 114 | 84 | | ResetBatchCounters(0); |
| | 114 | 85 | | _bufferIntersectionPoints.FastClear(); |
| | 114 | 86 | | _meshTriangleCandidates.FastClear(); |
| | 114 | 87 | | _planarCircleCandidates.FastClear(); |
| | 114 | 88 | | _batch3DHits.Clear(); |
| | 114 | 89 | | _redundantColliderCheck.Clear(); |
| | 114 | 90 | | _redundantVoxelCheck.Clear(); |
| | 114 | 91 | | } |
| | | 92 | | |
| | | 93 | | /// <summary> |
| | | 94 | | /// Performs a raycast from an origin in a direction up to a maximum distance. |
| | | 95 | | /// </summary> |
| | | 96 | | public bool Raycast( |
| | | 97 | | Vector3d origin, |
| | | 98 | | Vector3d direction, |
| | | 99 | | Fixed64 maxDistance, |
| | | 100 | | out Physics3DHit raycastHit, |
| | | 101 | | PhysicsLayerMask layerMask) |
| | | 102 | | { |
| | 521 | 103 | | _currentLayerMask = layerMask; |
| | 521 | 104 | | if (direction == Vector3d.Zero || maxDistance <= Fixed64.Zero) |
| | | 105 | | { |
| | 3 | 106 | | ResetLastQueryCounters(); |
| | 3 | 107 | | raycastHit = default; |
| | 3 | 108 | | return false; |
| | | 109 | | } |
| | | 110 | | |
| | 518 | 111 | | Vector3d rayDirection = direction.Normalized; |
| | 518 | 112 | | Vector3d requestedDisplacement = rayDirection * maxDistance; |
| | 518 | 113 | | if (!Vector3d.TryAdd(origin, requestedDisplacement, out Vector3d end)) |
| | | 114 | | { |
| | 1 | 115 | | ResetLastQueryCounters(); |
| | 1 | 116 | | raycastHit = default; |
| | 1 | 117 | | return false; |
| | | 118 | | } |
| | | 119 | | |
| | 517 | 120 | | BeginRaycastTrace(origin, end); |
| | 517 | 121 | | bool hit = TryFindClosestHit(origin, end, rayDirection, out raycastHit); |
| | 517 | 122 | | _context.Diagnostics.EmitRayQuery( |
| | 517 | 123 | | origin, |
| | 517 | 124 | | end, |
| | 517 | 125 | | Fixed64.Zero, |
| | 517 | 126 | | layerMask.Bits, |
| | 517 | 127 | | hit, |
| | 517 | 128 | | hit ? 1 : 0, |
| | 517 | 129 | | raycastHit); |
| | 517 | 130 | | return hit; |
| | | 131 | | } |
| | | 132 | | |
| | | 133 | | /// <summary> |
| | | 134 | | /// Executes a raycast between two points and writes hits from closest to farthest into caller-owned storage. |
| | | 135 | | /// </summary> |
| | | 136 | | public int RaycastAll( |
| | | 137 | | Vector3d start3d, |
| | | 138 | | Vector3d end3d, |
| | | 139 | | PhysicsLayerMask layerMask, |
| | | 140 | | SwiftList<Physics3DHit> results) |
| | | 141 | | { |
| | 1892 | 142 | | ChronicleGuard.ThrowIfNull(results, nameof(results)); |
| | | 143 | | |
| | 1892 | 144 | | _currentLayerMask = layerMask; |
| | 1892 | 145 | | results.Clear(); |
| | | 146 | | |
| | 1892 | 147 | | if (!Vector3d.TrySubtract(end3d, start3d, out Vector3d segment) |
| | 1892 | 148 | | || !Vector3d.TryGetMagnitude(segment, out Fixed64 segmentLength) |
| | 1892 | 149 | | || segmentLength == Fixed64.Zero) |
| | | 150 | | { |
| | 82 | 151 | | ResetLastQueryCounters(); |
| | 82 | 152 | | return 0; |
| | | 153 | | } |
| | | 154 | | |
| | 1810 | 155 | | BeginRaycastTrace(start3d, end3d); |
| | 1810 | 156 | | AddAllHits(start3d, end3d, segment.Normalized, results); |
| | 1810 | 157 | | Physics3DHitSorter.SortByDistance(results); |
| | 1810 | 158 | | _context.Diagnostics.EmitRayQuery( |
| | 1810 | 159 | | start3d, |
| | 1810 | 160 | | end3d, |
| | 1810 | 161 | | Fixed64.Zero, |
| | 1810 | 162 | | layerMask.Bits, |
| | 1810 | 163 | | results.Count > 0, |
| | 1810 | 164 | | results.Count, |
| | 1810 | 165 | | results.Count > 0 ? results[0] : default); |
| | 1810 | 166 | | return results.Count; |
| | | 167 | | } |
| | | 168 | | |
| | | 169 | | /// <summary> |
| | | 170 | | /// Sweeps a sphere from an origin in a direction up to a maximum distance and returns the closest hit. |
| | | 171 | | /// </summary> |
| | | 172 | | public bool SweepSphere( |
| | | 173 | | Vector3d origin, |
| | | 174 | | Fixed64 radius, |
| | | 175 | | Vector3d direction, |
| | | 176 | | Fixed64 maxDistance, |
| | | 177 | | out Physics3DHit sweepHit, |
| | | 178 | | PhysicsLayerMask layerMask, |
| | | 179 | | LSCollider? excludedCollider = null) |
| | | 180 | | { |
| | 1836 | 181 | | ChronicleGuard.ThrowIfArgument(radius > ColliderGeometryPolicy.MaximumGeometryReach, |
| | 1836 | 182 | | nameof(radius), "Effective geometry reach must not exceed 4,096 world units."); |
| | 1834 | 183 | | if (direction == Vector3d.Zero || maxDistance <= Fixed64.Zero) |
| | | 184 | | { |
| | 3 | 185 | | ResetLastQueryCounters(); |
| | 3 | 186 | | sweepHit = default; |
| | 3 | 187 | | return false; |
| | | 188 | | } |
| | | 189 | | |
| | 1831 | 190 | | Vector3d sweepDirection = direction.Normalized; |
| | 1831 | 191 | | Vector3d requestedDisplacement = sweepDirection * maxDistance; |
| | 1831 | 192 | | if (!Vector3d.TryAdd(origin, requestedDisplacement, out Vector3d end)) |
| | | 193 | | { |
| | 1 | 194 | | ResetLastQueryCounters(); |
| | 1 | 195 | | sweepHit = default; |
| | 1 | 196 | | return false; |
| | | 197 | | } |
| | 1830 | 198 | | bool hit = SweepSphere(origin, end, radius, layerMask, excludedCollider, out sweepHit); |
| | 1830 | 199 | | _context.Diagnostics.EmitRayQuery( |
| | 1830 | 200 | | origin, |
| | 1830 | 201 | | end, |
| | 1830 | 202 | | radius, |
| | 1830 | 203 | | layerMask.Bits, |
| | 1830 | 204 | | hit, |
| | 1830 | 205 | | hit ? 1 : 0, |
| | 1830 | 206 | | sweepHit); |
| | 1830 | 207 | | return hit; |
| | | 208 | | } |
| | | 209 | | |
| | | 210 | | /// <summary> |
| | | 211 | | /// Sweeps a sphere between two points and writes hits from closest to farthest into caller-owned storage. |
| | | 212 | | /// </summary> |
| | | 213 | | public int SweepSphereAll( |
| | | 214 | | Vector3d start3d, |
| | | 215 | | Vector3d end3d, |
| | | 216 | | Fixed64 radius, |
| | | 217 | | PhysicsLayerMask layerMask, |
| | | 218 | | SwiftList<Physics3DHit> results, |
| | | 219 | | LSCollider? excludedCollider = null) |
| | | 220 | | { |
| | 12691 | 221 | | return SweepSphereAllCore( |
| | 12691 | 222 | | start3d, |
| | 12691 | 223 | | end3d, |
| | 12691 | 224 | | radius, |
| | 12691 | 225 | | layerMask, |
| | 12691 | 226 | | results, |
| | 12691 | 227 | | excludedCollider, |
| | 12691 | 228 | | includeTriggers: true, |
| | 12691 | 229 | | staticTargetsOnly: false); |
| | | 230 | | } |
| | | 231 | | |
| | | 232 | | internal int SweepSphereAgainstStaticAll( |
| | | 233 | | Vector3d start3d, |
| | | 234 | | Vector3d end3d, |
| | | 235 | | Fixed64 radius, |
| | | 236 | | PhysicsLayerMask layerMask, |
| | | 237 | | SwiftList<Physics3DHit> results, |
| | | 238 | | LSCollider? excludedCollider = null, |
| | | 239 | | bool includeTriggers = true) |
| | | 240 | | { |
| | 7224 | 241 | | return SweepSphereAllCore( |
| | 7224 | 242 | | start3d, |
| | 7224 | 243 | | end3d, |
| | 7224 | 244 | | radius, |
| | 7224 | 245 | | layerMask, |
| | 7224 | 246 | | results, |
| | 7224 | 247 | | excludedCollider, |
| | 7224 | 248 | | includeTriggers, |
| | 7224 | 249 | | staticTargetsOnly: true); |
| | | 250 | | } |
| | | 251 | | |
| | | 252 | | internal int SweepExactSourceAgainstStaticAll( |
| | | 253 | | LSCollider source, |
| | | 254 | | Vector3d displacement, |
| | | 255 | | PhysicsLayerMask layerMask, |
| | | 256 | | SwiftList<Physics3DHit> results, |
| | | 257 | | LSCollider? excludedCollider = null, |
| | | 258 | | bool includeTriggers = true, |
| | | 259 | | ContinuousSurfaceTriangleAdmission? surfaceAdmission = null) |
| | | 260 | | { |
| | 578 | 261 | | ChronicleGuard.ThrowIfNull(source, nameof(source)); |
| | 578 | 262 | | ChronicleGuard.ThrowIfNull(results, nameof(results)); |
| | 578 | 263 | | return SweepRegisteredSourceAll( |
| | 578 | 264 | | source, |
| | 578 | 265 | | displacement, |
| | 578 | 266 | | layerMask, |
| | 578 | 267 | | results, |
| | 578 | 268 | | excludedCollider, |
| | 578 | 269 | | includeTriggers, |
| | 578 | 270 | | staticTargetsOnly: true, |
| | 578 | 271 | | surfaceAdmission: surfaceAdmission); |
| | | 272 | | } |
| | | 273 | | |
| | | 274 | | /// <summary> |
| | | 275 | | /// Sweeps a capsule source by <paramref name="displacement"/> and returns the closest 3D target hit. |
| | | 276 | | /// </summary> |
| | | 277 | | public bool SweepCapsule( |
| | | 278 | | LSCapsuleCollider source, |
| | | 279 | | Vector3d displacement, |
| | | 280 | | PhysicsLayerMask layerMask, |
| | | 281 | | out Physics3DHit sweepHit, |
| | | 282 | | LSCollider? excludedCollider = null, |
| | | 283 | | bool includeTriggers = true) |
| | | 284 | | { |
| | 85 | 285 | | ChronicleGuard.ThrowIfNull(source, nameof(source)); |
| | 85 | 286 | | return SweepRegisteredSource(source, displacement, layerMask, out sweepHit, excludedCollider, includeTriggers); |
| | | 287 | | } |
| | | 288 | | |
| | | 289 | | /// <summary> |
| | | 290 | | /// Writes all 3D targets hit by sweeping a capsule source into caller-owned storage. |
| | | 291 | | /// </summary> |
| | | 292 | | public int SweepCapsuleAll( |
| | | 293 | | LSCapsuleCollider source, |
| | | 294 | | Vector3d displacement, |
| | | 295 | | PhysicsLayerMask layerMask, |
| | | 296 | | SwiftList<Physics3DHit> results, |
| | | 297 | | LSCollider? excludedCollider = null, |
| | | 298 | | bool includeTriggers = true) |
| | | 299 | | { |
| | 96 | 300 | | ChronicleGuard.ThrowIfNull(source, nameof(source)); |
| | 96 | 301 | | ChronicleGuard.ThrowIfNull(results, nameof(results)); |
| | 96 | 302 | | return SweepRegisteredSourceAll(source, displacement, layerMask, results, excludedCollider, includeTriggers); |
| | | 303 | | } |
| | | 304 | | |
| | | 305 | | /// <summary> |
| | | 306 | | /// Sweeps a cuboid source by <paramref name="displacement"/> and returns the closest 3D target hit. |
| | | 307 | | /// </summary> |
| | | 308 | | public bool SweepCuboid( |
| | | 309 | | LSCuboidCollider source, |
| | | 310 | | Vector3d displacement, |
| | | 311 | | PhysicsLayerMask layerMask, |
| | | 312 | | out Physics3DHit sweepHit, |
| | | 313 | | LSCollider? excludedCollider = null, |
| | | 314 | | bool includeTriggers = true) |
| | | 315 | | { |
| | 726 | 316 | | ChronicleGuard.ThrowIfNull(source, nameof(source)); |
| | 726 | 317 | | return SweepRegisteredSource(source, displacement, layerMask, out sweepHit, excludedCollider, includeTriggers); |
| | | 318 | | } |
| | | 319 | | |
| | | 320 | | /// <summary> |
| | | 321 | | /// Writes all 3D targets hit by sweeping a cuboid source into caller-owned storage. |
| | | 322 | | /// </summary> |
| | | 323 | | public int SweepCuboidAll( |
| | | 324 | | LSCuboidCollider source, |
| | | 325 | | Vector3d displacement, |
| | | 326 | | PhysicsLayerMask layerMask, |
| | | 327 | | SwiftList<Physics3DHit> results, |
| | | 328 | | LSCollider? excludedCollider = null, |
| | | 329 | | bool includeTriggers = true) |
| | | 330 | | { |
| | 707 | 331 | | ChronicleGuard.ThrowIfNull(source, nameof(source)); |
| | 707 | 332 | | ChronicleGuard.ThrowIfNull(results, nameof(results)); |
| | 707 | 333 | | return SweepRegisteredSourceAll(source, displacement, layerMask, results, excludedCollider, includeTriggers); |
| | | 334 | | } |
| | | 335 | | |
| | | 336 | | /// <summary> |
| | | 337 | | /// Sweeps a finite-cylinder source by <paramref name="displacement"/> and returns the closest 3D target hit. |
| | | 338 | | /// </summary> |
| | | 339 | | public bool SweepCylinder( |
| | | 340 | | LSCylinderCollider source, |
| | | 341 | | Vector3d displacement, |
| | | 342 | | PhysicsLayerMask layerMask, |
| | | 343 | | out Physics3DHit sweepHit, |
| | | 344 | | LSCollider? excludedCollider = null, |
| | | 345 | | bool includeTriggers = true) |
| | | 346 | | { |
| | 30 | 347 | | ChronicleGuard.ThrowIfNull(source, nameof(source)); |
| | 30 | 348 | | return SweepRegisteredSource(source, displacement, layerMask, out sweepHit, excludedCollider, includeTriggers); |
| | | 349 | | } |
| | | 350 | | |
| | | 351 | | /// <summary> |
| | | 352 | | /// Writes all 3D targets hit by sweeping a finite-cylinder source into caller-owned storage. |
| | | 353 | | /// </summary> |
| | | 354 | | public int SweepCylinderAll( |
| | | 355 | | LSCylinderCollider source, |
| | | 356 | | Vector3d displacement, |
| | | 357 | | PhysicsLayerMask layerMask, |
| | | 358 | | SwiftList<Physics3DHit> results, |
| | | 359 | | LSCollider? excludedCollider = null, |
| | | 360 | | bool includeTriggers = true) |
| | | 361 | | { |
| | 1 | 362 | | ChronicleGuard.ThrowIfNull(source, nameof(source)); |
| | 1 | 363 | | ChronicleGuard.ThrowIfNull(results, nameof(results)); |
| | 1 | 364 | | return SweepRegisteredSourceAll(source, displacement, layerMask, results, excludedCollider, includeTriggers); |
| | | 365 | | } |
| | | 366 | | |
| | | 367 | | /// <summary> |
| | | 368 | | /// Sweeps a finite-cone source by <paramref name="displacement"/> and returns the closest 3D target hit. |
| | | 369 | | /// </summary> |
| | | 370 | | public bool SweepCone( |
| | | 371 | | LSConeCollider source, |
| | | 372 | | Vector3d displacement, |
| | | 373 | | PhysicsLayerMask layerMask, |
| | | 374 | | out Physics3DHit sweepHit, |
| | | 375 | | LSCollider? excludedCollider = null, |
| | | 376 | | bool includeTriggers = true) |
| | | 377 | | { |
| | 25 | 378 | | ChronicleGuard.ThrowIfNull(source, nameof(source)); |
| | 25 | 379 | | return SweepRegisteredSource(source, displacement, layerMask, out sweepHit, excludedCollider, includeTriggers); |
| | | 380 | | } |
| | | 381 | | |
| | | 382 | | /// <summary> |
| | | 383 | | /// Writes all 3D targets hit by sweeping a finite-cone source into caller-owned storage. |
| | | 384 | | /// </summary> |
| | | 385 | | public int SweepConeAll( |
| | | 386 | | LSConeCollider source, |
| | | 387 | | Vector3d displacement, |
| | | 388 | | PhysicsLayerMask layerMask, |
| | | 389 | | SwiftList<Physics3DHit> results, |
| | | 390 | | LSCollider? excludedCollider = null, |
| | | 391 | | bool includeTriggers = true) |
| | | 392 | | { |
| | 1 | 393 | | ChronicleGuard.ThrowIfNull(source, nameof(source)); |
| | 1 | 394 | | ChronicleGuard.ThrowIfNull(results, nameof(results)); |
| | 1 | 395 | | return SweepRegisteredSourceAll(source, displacement, layerMask, results, excludedCollider, includeTriggers); |
| | | 396 | | } |
| | | 397 | | |
| | | 398 | | /// <summary> |
| | | 399 | | /// Sweeps a convex mesh source by <paramref name="displacement"/> and returns the closest 3D target hit. |
| | | 400 | | /// </summary> |
| | | 401 | | public bool SweepConvexMesh( |
| | | 402 | | LSMeshCollider source, |
| | | 403 | | Vector3d displacement, |
| | | 404 | | PhysicsLayerMask layerMask, |
| | | 405 | | out Physics3DHit sweepHit, |
| | | 406 | | LSCollider? excludedCollider = null, |
| | | 407 | | bool includeTriggers = true) |
| | | 408 | | { |
| | 15 | 409 | | ChronicleGuard.ThrowIfNull(source, nameof(source)); |
| | 15 | 410 | | return SweepRegisteredSource(source, displacement, layerMask, out sweepHit, excludedCollider, includeTriggers); |
| | | 411 | | } |
| | | 412 | | |
| | | 413 | | /// <summary> |
| | | 414 | | /// Writes all 3D targets hit by sweeping a convex mesh source into caller-owned storage. |
| | | 415 | | /// </summary> |
| | | 416 | | public int SweepConvexMeshAll( |
| | | 417 | | LSMeshCollider source, |
| | | 418 | | Vector3d displacement, |
| | | 419 | | PhysicsLayerMask layerMask, |
| | | 420 | | SwiftList<Physics3DHit> results, |
| | | 421 | | LSCollider? excludedCollider = null, |
| | | 422 | | bool includeTriggers = true) |
| | | 423 | | { |
| | 56 | 424 | | ChronicleGuard.ThrowIfNull(source, nameof(source)); |
| | 56 | 425 | | ChronicleGuard.ThrowIfNull(results, nameof(results)); |
| | 56 | 426 | | return SweepRegisteredSourceAll(source, displacement, layerMask, results, excludedCollider, includeTriggers); |
| | | 427 | | } |
| | | 428 | | |
| | | 429 | | /// <summary> |
| | | 430 | | /// Sweeps an authored compound source by <paramref name="displacement"/> and returns the closest 3D target hit. |
| | | 431 | | /// </summary> |
| | | 432 | | public bool SweepCompound( |
| | | 433 | | LSCompoundCollider source, |
| | | 434 | | Vector3d displacement, |
| | | 435 | | PhysicsLayerMask layerMask, |
| | | 436 | | out Physics3DHit sweepHit, |
| | | 437 | | LSCollider? excludedCollider = null, |
| | | 438 | | bool includeTriggers = true) |
| | | 439 | | { |
| | 10 | 440 | | ChronicleGuard.ThrowIfNull(source, nameof(source)); |
| | 10 | 441 | | return SweepRegisteredSource(source, displacement, layerMask, out sweepHit, excludedCollider, includeTriggers); |
| | | 442 | | } |
| | | 443 | | |
| | | 444 | | /// <summary> |
| | | 445 | | /// Writes all 3D targets hit by sweeping an authored compound source into caller-owned storage. |
| | | 446 | | /// </summary> |
| | | 447 | | public int SweepCompoundAll( |
| | | 448 | | LSCompoundCollider source, |
| | | 449 | | Vector3d displacement, |
| | | 450 | | PhysicsLayerMask layerMask, |
| | | 451 | | SwiftList<Physics3DHit> results, |
| | | 452 | | LSCollider? excludedCollider = null, |
| | | 453 | | bool includeTriggers = true) |
| | | 454 | | { |
| | 1 | 455 | | ChronicleGuard.ThrowIfNull(source, nameof(source)); |
| | 1 | 456 | | ChronicleGuard.ThrowIfNull(results, nameof(results)); |
| | 1 | 457 | | return SweepRegisteredSourceAll(source, displacement, layerMask, results, excludedCollider, includeTriggers); |
| | | 458 | | } |
| | | 459 | | |
| | | 460 | | private int SweepSphereAllCore( |
| | | 461 | | Vector3d start3d, |
| | | 462 | | Vector3d end3d, |
| | | 463 | | Fixed64 radius, |
| | | 464 | | PhysicsLayerMask layerMask, |
| | | 465 | | SwiftList<Physics3DHit> results, |
| | | 466 | | LSCollider? excludedCollider, |
| | | 467 | | bool includeTriggers, |
| | | 468 | | bool staticTargetsOnly) |
| | | 469 | | { |
| | 19915 | 470 | | ChronicleGuard.ThrowIfNull(results, nameof(results)); |
| | 19915 | 471 | | ChronicleGuard.ThrowIfArgument(!staticTargetsOnly && radius > ColliderGeometryPolicy.MaximumGeometryReach, |
| | 19915 | 472 | | nameof(radius), "Effective geometry reach must not exceed 4,096 world units."); |
| | | 473 | | |
| | 19914 | 474 | | results.Clear(); |
| | 19914 | 475 | | if (!Vector3d.TrySubtract(end3d, start3d, out Vector3d segment) |
| | 19914 | 476 | | || !Vector3d.TryGetMagnitude(segment, out Fixed64 segmentLength) |
| | 19914 | 477 | | || segmentLength <= Fixed64.Epsilon |
| | 19914 | 478 | | || radius <= Fixed64.Zero) |
| | | 479 | | { |
| | 133 | 480 | | ResetLastQueryCounters(); |
| | 133 | 481 | | return 0; |
| | | 482 | | } |
| | | 483 | | |
| | | 484 | | try |
| | | 485 | | { |
| | 19781 | 486 | | BeginSweepTrace(start3d, end3d, radius, layerMask, excludedCollider, includeTriggers, staticTargetsOnly); |
| | 19781 | 487 | | AddAllSweepHits(start3d, end3d, segment.Normalized, radius, results); |
| | 19781 | 488 | | Physics3DHitSorter.SortByDistance(results); |
| | 19781 | 489 | | _context.Diagnostics.EmitRayQuery( |
| | 19781 | 490 | | start3d, |
| | 19781 | 491 | | end3d, |
| | 19781 | 492 | | radius, |
| | 19781 | 493 | | layerMask.Bits, |
| | 19781 | 494 | | results.Count > 0, |
| | 19781 | 495 | | results.Count, |
| | 19781 | 496 | | results.Count > 0 ? results[0] : default); |
| | 19781 | 497 | | return results.Count; |
| | | 498 | | } |
| | | 499 | | finally |
| | | 500 | | { |
| | 19781 | 501 | | EndQuery(); |
| | 19781 | 502 | | } |
| | 19781 | 503 | | } |
| | | 504 | | |
| | | 505 | | private bool SweepRegisteredSource( |
| | | 506 | | LSCollider source, |
| | | 507 | | Vector3d displacement, |
| | | 508 | | PhysicsLayerMask layerMask, |
| | | 509 | | out Physics3DHit sweepHit, |
| | | 510 | | LSCollider? excludedCollider, |
| | | 511 | | bool includeTriggers) |
| | | 512 | | { |
| | 891 | 513 | | EnsureSourceBelongsToContext(source); |
| | | 514 | | try |
| | | 515 | | { |
| | 891 | 516 | | PrepareConvexSweepSource(source, displacement); |
| | 890 | 517 | | sweepHit = default; |
| | 890 | 518 | | if (!Vector3d.TryGetMagnitude(displacement, out Fixed64 displacementLength) |
| | 890 | 519 | | || displacementLength <= Fixed64.Epsilon) |
| | | 520 | | { |
| | 3 | 521 | | ResetLastQueryCounters(); |
| | 3 | 522 | | return false; |
| | | 523 | | } |
| | | 524 | | |
| | 887 | 525 | | BeginConvexSweepTrace(source, layerMask, excludedCollider, includeTriggers); |
| | 887 | 526 | | bool hit = TryFindClosestConvexSweepHit(source, displacement, out sweepHit); |
| | 887 | 527 | | _context.Diagnostics.EmitRayQuery( |
| | 887 | 528 | | source.Center, |
| | 887 | 529 | | source.Center + displacement, |
| | 887 | 530 | | source.ScaledRadius, |
| | 887 | 531 | | layerMask.Bits, |
| | 887 | 532 | | hit, |
| | 887 | 533 | | hit ? 1 : 0, |
| | 887 | 534 | | sweepHit); |
| | 887 | 535 | | return hit; |
| | | 536 | | } |
| | | 537 | | finally |
| | | 538 | | { |
| | 891 | 539 | | EndQuery(); |
| | 891 | 540 | | } |
| | 890 | 541 | | } |
| | | 542 | | |
| | | 543 | | private int SweepRegisteredSourceAll( |
| | | 544 | | LSCollider source, |
| | | 545 | | Vector3d displacement, |
| | | 546 | | PhysicsLayerMask layerMask, |
| | | 547 | | SwiftList<Physics3DHit> results, |
| | | 548 | | LSCollider? excludedCollider, |
| | | 549 | | bool includeTriggers, |
| | | 550 | | bool staticTargetsOnly = false, |
| | | 551 | | ContinuousSurfaceTriangleAdmission? surfaceAdmission = null) |
| | | 552 | | { |
| | 1440 | 553 | | EnsureSourceBelongsToContext(source); |
| | | 554 | | try |
| | | 555 | | { |
| | 1440 | 556 | | _currentSurfaceAdmission = surfaceAdmission; |
| | 1440 | 557 | | PrepareConvexSweepSource(source, displacement); |
| | 1440 | 558 | | results.Clear(); |
| | 1440 | 559 | | if (!Vector3d.TryGetMagnitude(displacement, out Fixed64 displacementLength) |
| | 1440 | 560 | | || displacementLength <= Fixed64.Epsilon) |
| | | 561 | | { |
| | 3 | 562 | | ResetLastQueryCounters(); |
| | 3 | 563 | | return 0; |
| | | 564 | | } |
| | | 565 | | |
| | 1437 | 566 | | BeginConvexSweepTrace(source, layerMask, excludedCollider, includeTriggers, staticTargetsOnly); |
| | 1437 | 567 | | AddAllConvexSweepHits(source, displacement, results); |
| | 1437 | 568 | | Physics3DHitSorter.SortByDistance(results); |
| | 1437 | 569 | | _context.Diagnostics.EmitRayQuery( |
| | 1437 | 570 | | source.Center, |
| | 1437 | 571 | | source.Center + displacement, |
| | 1437 | 572 | | source.ScaledRadius, |
| | 1437 | 573 | | layerMask.Bits, |
| | 1437 | 574 | | results.Count > 0, |
| | 1437 | 575 | | results.Count, |
| | 1437 | 576 | | results.Count > 0 ? results[0] : default); |
| | 1437 | 577 | | return results.Count; |
| | | 578 | | } |
| | | 579 | | finally |
| | | 580 | | { |
| | 1440 | 581 | | EndQuery(); |
| | 1440 | 582 | | } |
| | 1440 | 583 | | } |
| | | 584 | | |
| | | 585 | | private void EndQuery() |
| | | 586 | | { |
| | | 587 | | // Completed queries must not keep retired host object graphs alive. |
| | 27961 | 588 | | _currentExcludedCollider = null; |
| | 27961 | 589 | | _currentSweepSourceCollider = null; |
| | 27961 | 590 | | _currentSurfaceAdmission = null; |
| | 27961 | 591 | | _convexSweepWorker.ClearSource(); |
| | 27961 | 592 | | _coveredVoxels.Clear(); |
| | 27961 | 593 | | _traceScratch.Clear(); |
| | 27961 | 594 | | } |
| | | 595 | | |
| | | 596 | | private void BeginConvexSweepTrace( |
| | | 597 | | LSCollider source, |
| | | 598 | | PhysicsLayerMask layerMask, |
| | | 599 | | LSCollider? excludedCollider, |
| | | 600 | | bool includeTriggers, |
| | | 601 | | bool staticTargetsOnly = false) |
| | | 602 | | { |
| | 2346 | 603 | | _currentLayerMask = layerMask; |
| | 2346 | 604 | | _currentExcludedCollider = excludedCollider; |
| | 2346 | 605 | | _currentSweepSourceCollider = source; |
| | 2346 | 606 | | _currentIncludeTriggers = includeTriggers; |
| | 2346 | 607 | | _currentStaticSweepTargetsOnly = staticTargetsOnly; |
| | 2346 | 608 | | _redundantColliderCheck.Clear(); |
| | 2346 | 609 | | _redundantVoxelCheck.Clear(); |
| | 2346 | 610 | | ResetLastQueryCounters(); |
| | 2346 | 611 | | NextRaycastVersion(); |
| | 2346 | 612 | | } |
| | | 613 | | |
| | | 614 | | private void BeginRaycastTrace(Vector3d start, Vector3d end) |
| | | 615 | | { |
| | 3177 | 616 | | _redundantColliderCheck.Clear(); |
| | 3177 | 617 | | _redundantVoxelCheck.Clear(); |
| | 3177 | 618 | | _bufferIntersectionPoints.FastClear(); |
| | 3177 | 619 | | _currentExcludedCollider = null; |
| | 3177 | 620 | | _currentSweepSourceCollider = null; |
| | 3177 | 621 | | _currentStaticSweepTargetsOnly = false; |
| | 3177 | 622 | | _currentIncludeTriggers = true; |
| | 3177 | 623 | | ResetLastQueryCounters(); |
| | 3177 | 624 | | NextRaycastVersion(); |
| | 3177 | 625 | | _worker.PrepareSegmentCheck(start, end); |
| | 3177 | 626 | | } |
| | | 627 | | |
| | | 628 | | private void BeginSweepTrace( |
| | | 629 | | Vector3d start, |
| | | 630 | | Vector3d end, |
| | | 631 | | Fixed64 radius, |
| | | 632 | | PhysicsLayerMask layerMask, |
| | | 633 | | LSCollider? excludedCollider, |
| | | 634 | | bool includeTriggers = true, |
| | | 635 | | bool staticTargetsOnly = false) |
| | | 636 | | { |
| | 21616 | 637 | | _currentLayerMask = layerMask; |
| | 21616 | 638 | | _currentExcludedCollider = excludedCollider; |
| | 21616 | 639 | | _currentSweepSourceCollider = null; |
| | 21616 | 640 | | _currentIncludeTriggers = includeTriggers; |
| | 21616 | 641 | | _currentStaticSweepTargetsOnly = staticTargetsOnly; |
| | 21616 | 642 | | _redundantColliderCheck.Clear(); |
| | 21616 | 643 | | _redundantVoxelCheck.Clear(); |
| | 21616 | 644 | | ResetLastQueryCounters(); |
| | 21616 | 645 | | NextRaycastVersion(); |
| | 21616 | 646 | | _sweepWorker.Prepare(start, end, radius); |
| | 21616 | 647 | | } |
| | | 648 | | |
| | | 649 | | private bool SweepSphere( |
| | | 650 | | Vector3d start, |
| | | 651 | | Vector3d end, |
| | | 652 | | Fixed64 radius, |
| | | 653 | | PhysicsLayerMask layerMask, |
| | | 654 | | LSCollider? excludedCollider, |
| | | 655 | | out Physics3DHit sweepHit) |
| | | 656 | | { |
| | 1838 | 657 | | ChronicleGuard.ThrowIfArgument(radius > ColliderGeometryPolicy.MaximumGeometryReach, |
| | 1838 | 658 | | nameof(radius), "Effective geometry reach must not exceed 4,096 world units."); |
| | 1838 | 659 | | sweepHit = default; |
| | 1838 | 660 | | if (radius <= Fixed64.Zero) |
| | | 661 | | { |
| | 2 | 662 | | ResetLastQueryCounters(); |
| | 2 | 663 | | return false; |
| | | 664 | | } |
| | | 665 | | |
| | 1836 | 666 | | if (!Vector3d.TrySubtract(end, start, out Vector3d segment) |
| | 1836 | 667 | | || !Vector3d.TryGetMagnitude(segment, out Fixed64 segmentLength) |
| | 1836 | 668 | | || segmentLength <= Fixed64.Epsilon) |
| | | 669 | | { |
| | 4 | 670 | | ResetLastQueryCounters(); |
| | 4 | 671 | | return false; |
| | | 672 | | } |
| | | 673 | | |
| | 1832 | 674 | | Vector3d direction = segment.Normalized; |
| | | 675 | | try |
| | | 676 | | { |
| | 1832 | 677 | | BeginSweepTrace(start, end, radius, layerMask, excludedCollider); |
| | 1832 | 678 | | return TryFindClosestSweepHit(start, end, radius, direction, out sweepHit); |
| | | 679 | | } |
| | | 680 | | finally |
| | | 681 | | { |
| | 1832 | 682 | | EndQuery(); |
| | 1832 | 683 | | } |
| | 1832 | 684 | | } |
| | | 685 | | |
| | | 686 | | private bool TryFindClosestSweepHit( |
| | | 687 | | Vector3d start, |
| | | 688 | | Vector3d end, |
| | | 689 | | Fixed64 radius, |
| | | 690 | | Vector3d direction, |
| | | 691 | | out Physics3DHit sweepHit) |
| | | 692 | | { |
| | 1832 | 693 | | bool found = false; |
| | 1832 | 694 | | Physics3DHit closestHit = default; |
| | | 695 | | |
| | 1832 | 696 | | TraceSweepForClosestHit(start, end, radius, direction, ref found, ref closestHit); |
| | | 697 | | |
| | 1832 | 698 | | sweepHit = closestHit; |
| | 1832 | 699 | | return found; |
| | | 700 | | } |
| | | 701 | | |
| | | 702 | | private bool TryFindClosestHit(Vector3d start, Vector3d end, Vector3d direction, out Physics3DHit raycastHit) |
| | | 703 | | { |
| | 939 | 704 | | bool found = false; |
| | 939 | 705 | | Physics3DHit closestHit = default; |
| | | 706 | | |
| | 939 | 707 | | TraceLineForClosestHit(start, end, direction, ref found, ref closestHit); |
| | | 708 | | |
| | 939 | 709 | | raycastHit = closestHit; |
| | 939 | 710 | | return found; |
| | | 711 | | } |
| | | 712 | | |
| | | 713 | | private void AddAllHits(Vector3d start, Vector3d end, Vector3d direction, SwiftList<Physics3DHit> results) |
| | | 714 | | { |
| | 2238 | 715 | | TraceLineForAllHits(start, end, direction, results); |
| | 2238 | 716 | | } |
| | | 717 | | |
| | | 718 | | private void AddAllSweepHits( |
| | | 719 | | Vector3d start, |
| | | 720 | | Vector3d end, |
| | | 721 | | Vector3d direction, |
| | | 722 | | Fixed64 radius, |
| | | 723 | | SwiftList<Physics3DHit> results) |
| | | 724 | | { |
| | 19784 | 725 | | TraceSweepForAllHits(start, end, radius, direction, results); |
| | 19784 | 726 | | } |
| | | 727 | | |
| | | 728 | | private bool TryFindClosestConvexSweepHit( |
| | | 729 | | LSCollider source, |
| | | 730 | | Vector3d displacement, |
| | | 731 | | out Physics3DHit sweepHit) |
| | | 732 | | { |
| | 898 | 733 | | bool found = false; |
| | 898 | 734 | | Physics3DHit closestHit = default; |
| | | 735 | | |
| | 898 | 736 | | TraceConvexSweepForClosestHit(source, displacement, ref found, ref closestHit); |
| | | 737 | | |
| | 898 | 738 | | sweepHit = closestHit; |
| | 898 | 739 | | return found; |
| | | 740 | | } |
| | | 741 | | |
| | | 742 | | private void AddAllConvexSweepHits( |
| | | 743 | | LSCollider source, |
| | | 744 | | Vector3d displacement, |
| | | 745 | | SwiftList<Physics3DHit> results) |
| | | 746 | | { |
| | 1448 | 747 | | TraceConvexSweepForAllHits(source, displacement, results); |
| | 1448 | 748 | | } |
| | | 749 | | |
| | | 750 | | private bool TryBuildHitForCollider( |
| | | 751 | | int colliderId, |
| | | 752 | | Vector3d origin, |
| | | 753 | | Vector3d direction, |
| | | 754 | | out Physics3DHit hit) |
| | | 755 | | { |
| | 9244 | 756 | | hit = default; |
| | 9244 | 757 | | return _context.Physics.TryGetColliderById(colliderId, out LSCollider? current) |
| | 9244 | 758 | | && DoesCurrentColliderIntersectRay(current!) |
| | 9244 | 759 | | && TryBuildHit(current!, origin, direction, out hit); |
| | | 760 | | } |
| | | 761 | | |
| | | 762 | | private bool TryBuildSweepHitForCollider( |
| | | 763 | | int colliderId, |
| | | 764 | | Vector3d origin, |
| | | 765 | | Vector3d direction, |
| | | 766 | | out Physics3DHit hit) |
| | | 767 | | { |
| | 1083024 | 768 | | hit = default; |
| | 1083024 | 769 | | return _context.Physics.TryGetColliderById(colliderId, out LSCollider? current) |
| | 1083024 | 770 | | && IsSweepCandidate(current!) |
| | 1083024 | 771 | | && TryBuildSweepHit(current!, origin, direction, out hit); |
| | | 772 | | } |
| | | 773 | | |
| | | 774 | | private bool TryBuildConvexSweepHitForCollider(int colliderId, out Physics3DHit hit) |
| | | 775 | | { |
| | 104673 | 776 | | hit = default; |
| | 104673 | 777 | | if (!_context.Physics.TryGetColliderById(colliderId, out LSCollider? current) |
| | 104673 | 778 | | || !IsSweepCandidate(current!)) |
| | | 779 | | { |
| | 102389 | 780 | | return false; |
| | | 781 | | } |
| | | 782 | | |
| | 2284 | 783 | | bool found = _convexSweepWorker.TrySweepPreparedSource(current!, out hit, |
| | 2284 | 784 | | requireClosingInitialContact: _currentStaticSweepTargetsOnly, |
| | 2284 | 785 | | admitTriangle: current is LSMeshCollider { StaticSurface: not null } ? _currentSurfaceAdmission : null); |
| | 2284 | 786 | | LastMeshTriangleCandidateCount += _convexSweepWorker.LastMeshTriangleCandidateCount; |
| | 2284 | 787 | | return found; |
| | | 788 | | } |
| | | 789 | | |
| | | 790 | | private bool TryBuildHit( |
| | | 791 | | LSCollider collider, |
| | | 792 | | Vector3d origin, |
| | | 793 | | Vector3d direction, |
| | | 794 | | out Physics3DHit raycastHit) |
| | | 795 | | { |
| | 3507 | 796 | | Fixed64 closestDistance = Fixed64.MaxValue; |
| | 3507 | 797 | | Vector3d closestIntersection = Vector3d.Zero; |
| | | 798 | | |
| | 18744 | 799 | | for (int i = _bufferIntersectionPoints.Count - 1; i >= 0; i--) |
| | | 800 | | { |
| | 5865 | 801 | | Fixed64 dist = Vector3d.Distance(_bufferIntersectionPoints[i], origin); |
| | 5865 | 802 | | if (dist < closestDistance) |
| | | 803 | | { |
| | 5837 | 804 | | closestDistance = dist; |
| | 5837 | 805 | | closestIntersection = _bufferIntersectionPoints[i]; |
| | | 806 | | } |
| | | 807 | | } |
| | | 808 | | |
| | 3507 | 809 | | if (closestDistance == Fixed64.MaxValue) |
| | | 810 | | { |
| | 2 | 811 | | raycastHit = default; |
| | 2 | 812 | | return false; |
| | | 813 | | } |
| | | 814 | | |
| | 3505 | 815 | | Vector3d normal = collider.GetNormalAtPoint(closestIntersection); |
| | 3505 | 816 | | raycastHit = new Physics3DHit(collider, closestIntersection, normal, closestDistance, direction); |
| | 3505 | 817 | | return true; |
| | | 818 | | } |
| | | 819 | | |
| | | 820 | | private bool TryBuildSweepHit( |
| | | 821 | | LSCollider collider, |
| | | 822 | | Vector3d origin, |
| | | 823 | | Vector3d direction, |
| | | 824 | | out Physics3DHit sweepHit) |
| | | 825 | | { |
| | 19102 | 826 | | sweepHit = default; |
| | 19102 | 827 | | bool found = _sweepWorker.TrySweep(collider, out Vector3d sweepCenter, out Fixed64 distance); |
| | 19102 | 828 | | LastMeshTriangleCandidateCount += _sweepWorker.LastMeshTriangleCandidateCount; |
| | 19102 | 829 | | if (!found) |
| | 6717 | 830 | | return false; |
| | | 831 | | |
| | | 832 | | // Admitted source/target reach bounds a hit's surface separation even at world limits. |
| | 12385 | 833 | | bool resolved = ContinuousCollisionContactPolicy.TryResolveSweptSphereContact( |
| | 12385 | 834 | | collider, |
| | 12385 | 835 | | sweepCenter, |
| | 12385 | 836 | | direction, |
| | 12385 | 837 | | out ContactAnchor anchor, |
| | 12385 | 838 | | out Vector3d normal); |
| | | 839 | | System.Diagnostics.Debug.Assert(resolved); |
| | | 840 | | |
| | 12385 | 841 | | sweepHit = new Physics3DHit(collider, anchor, normal, distance, direction); |
| | 12385 | 842 | | return true; |
| | | 843 | | } |
| | | 844 | | |
| | | 845 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 846 | | private void ResetLastQueryCounters() |
| | | 847 | | { |
| | 28268 | 848 | | LastQueryCandidateCount = 0; |
| | 28268 | 849 | | LastMeshTriangleCandidateCount = 0; |
| | 28268 | 850 | | } |
| | | 851 | | |
| | | 852 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 853 | | private uint NextRaycastVersion() |
| | | 854 | | { |
| | 27139 | 855 | | _context.RefreshQueryPartitions(); |
| | 27139 | 856 | | RaycastVersion++; |
| | 27139 | 857 | | if (RaycastVersion == 0) |
| | | 858 | | { |
| | 1 | 859 | | ResetColliderRaycastVersions(); |
| | 1 | 860 | | RaycastVersion = 1; |
| | | 861 | | } |
| | | 862 | | |
| | 27139 | 863 | | return RaycastVersion; |
| | | 864 | | } |
| | | 865 | | |
| | | 866 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 867 | | private uint NextCircleVersion() |
| | | 868 | | { |
| | 703 | 869 | | _context.RefreshQueryPartitions(); |
| | 703 | 870 | | CircleVersion++; |
| | 703 | 871 | | if (CircleVersion == 0) |
| | | 872 | | { |
| | 1 | 873 | | ResetColliderCircleVersions(); |
| | 1 | 874 | | CircleVersion = 1; |
| | | 875 | | } |
| | | 876 | | |
| | 703 | 877 | | return CircleVersion; |
| | | 878 | | } |
| | | 879 | | |
| | | 880 | | private void ResetColliderRaycastVersions() |
| | | 881 | | { |
| | 234 | 882 | | for (int i = 0; i < _context.Physics.ColliderCount; i++) |
| | 2 | 883 | | _context.Physics.GetColliderByServiceIndex(i).RaycastVersion = 0; |
| | 115 | 884 | | } |
| | | 885 | | |
| | | 886 | | private void ResetColliderCircleVersions() |
| | | 887 | | { |
| | 234 | 888 | | for (int i = 0; i < _context.Physics.ColliderCount; i++) |
| | 2 | 889 | | _context.Physics.GetColliderByServiceIndex(i).CircleQueryVersion = 0; |
| | 115 | 890 | | } |
| | | 891 | | |
| | | 892 | | private bool DoesCurrentColliderIntersectRay(LSCollider current) |
| | | 893 | | { |
| | 9244 | 894 | | if (!_currentLayerMask.Includes(current.Layer) |
| | 9244 | 895 | | || current.RaycastVersion == RaycastVersion |
| | 9244 | 896 | | || !_redundantColliderCheck.Add(current.Id)) |
| | | 897 | | { |
| | 5181 | 898 | | return false; |
| | | 899 | | } |
| | | 900 | | |
| | 4063 | 901 | | current.RaycastVersion = RaycastVersion; |
| | 4063 | 902 | | _bufferIntersectionPoints.FastClear(); |
| | 4063 | 903 | | return current.ColliderOverlapsRay(_worker, ref _bufferIntersectionPoints); |
| | | 904 | | } |
| | | 905 | | |
| | | 906 | | private bool IsSweepCandidate(LSCollider current) |
| | | 907 | | { |
| | 1187670 | 908 | | if (ReferenceEquals(current, _currentExcludedCollider) |
| | 1187670 | 909 | | || ReferenceEquals(current, _currentSweepSourceCollider) |
| | 1187670 | 910 | | || IsExcludedByCurrentSweepSource(current) |
| | 1187670 | 911 | | || !_currentLayerMask.Includes(current.Layer) |
| | 1187670 | 912 | | || current.RaycastVersion == RaycastVersion |
| | 1187670 | 913 | | || !_redundantColliderCheck.Add(current.Id)) |
| | | 914 | | { |
| | 1166281 | 915 | | return false; |
| | | 916 | | } |
| | | 917 | | |
| | 21389 | 918 | | if (!_currentIncludeTriggers && current.IsTrigger) |
| | 2 | 919 | | return false; |
| | | 920 | | |
| | 21387 | 921 | | if (_currentStaticSweepTargetsOnly && !IsStaticStyleSweepTarget(current)) |
| | 1 | 922 | | return false; |
| | | 923 | | |
| | 21386 | 924 | | current.RaycastVersion = RaycastVersion; |
| | 21386 | 925 | | LastQueryCandidateCount++; |
| | 21386 | 926 | | return true; |
| | | 927 | | } |
| | | 928 | | |
| | | 929 | | private bool IsExcludedByCurrentSweepSource(LSCollider current) |
| | | 930 | | { |
| | 471365 | 931 | | if (_currentSweepSourceCollider != null |
| | 471365 | 932 | | && _context.Constraints3D.ShouldExcludeLinkedCollision(_currentSweepSourceCollider, current)) |
| | | 933 | | { |
| | 27 | 934 | | return true; |
| | | 935 | | } |
| | | 936 | | |
| | 471338 | 937 | | return _currentExcludedCollider != null |
| | 471338 | 938 | | && _context.Constraints3D.ShouldExcludeLinkedCollision(_currentExcludedCollider, current); |
| | | 939 | | } |
| | | 940 | | |
| | | 941 | | private void EnsureSourceBelongsToContext(LSCollider source) |
| | | 942 | | { |
| | 2369 | 943 | | ChronicleGuard.ThrowIfArgument( |
| | 2369 | 944 | | !ReferenceEquals(source.Context, _context), |
| | 2369 | 945 | | nameof(source), |
| | 2369 | 946 | | "Sweep source collider must belong to this query service context."); |
| | 2369 | 947 | | } |
| | | 948 | | |
| | | 949 | | private void PrepareConvexSweepSource(LSCollider source, Vector3d displacement) |
| | | 950 | | { |
| | | 951 | | switch (source) |
| | | 952 | | { |
| | | 953 | | case LSMeshCollider mesh: |
| | 87 | 954 | | _convexSweepWorker.PrepareConvexMeshSource(mesh, displacement); |
| | 86 | 955 | | return; |
| | | 956 | | case LSCompoundCollider compound: |
| | 23 | 957 | | _convexSweepWorker.PrepareCompoundSource(compound, displacement); |
| | 23 | 958 | | return; |
| | | 959 | | default: |
| | 2253 | 960 | | _convexSweepWorker.PreparePrimitiveSource(source, displacement); |
| | 2253 | 961 | | return; |
| | | 962 | | } |
| | | 963 | | } |
| | | 964 | | |
| | | 965 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 966 | | private static bool IsStaticStyleSweepTarget(LSCollider collider) |
| | | 967 | | { |
| | 7595 | 968 | | SolidBody? body = collider.Body; |
| | 7595 | 969 | | return collider.IsStatic || body!.IsKinematic; |
| | | 970 | | } |
| | | 971 | | |
| | | 972 | | } |