diff options
author | chai <215380520@qq.com> | 2024-05-23 10:08:29 +0800 |
---|---|---|
committer | chai <215380520@qq.com> | 2024-05-23 10:08:29 +0800 |
commit | 8722a9920c1f6119bf6e769cba270e63097f8e25 (patch) | |
tree | 2eaf9865de7fb1404546de4a4296553d8f68cc3b /Other/AstarPathfindingDemo/Packages/com.arongranberg.astar/Graphs/Grid/Jobs/JobPrepareGridRaycast.cs | |
parent | 3ba4020b69e5971bb0df7ee08b31d10ea4d01937 (diff) |
+ astar project
Diffstat (limited to 'Other/AstarPathfindingDemo/Packages/com.arongranberg.astar/Graphs/Grid/Jobs/JobPrepareGridRaycast.cs')
-rw-r--r-- | Other/AstarPathfindingDemo/Packages/com.arongranberg.astar/Graphs/Grid/Jobs/JobPrepareGridRaycast.cs | 61 |
1 files changed, 61 insertions, 0 deletions
diff --git a/Other/AstarPathfindingDemo/Packages/com.arongranberg.astar/Graphs/Grid/Jobs/JobPrepareGridRaycast.cs b/Other/AstarPathfindingDemo/Packages/com.arongranberg.astar/Graphs/Grid/Jobs/JobPrepareGridRaycast.cs new file mode 100644 index 0000000..e05347d --- /dev/null +++ b/Other/AstarPathfindingDemo/Packages/com.arongranberg.astar/Graphs/Grid/Jobs/JobPrepareGridRaycast.cs @@ -0,0 +1,61 @@ +using UnityEngine; +using Unity.Burst; +using Unity.Collections; +using Unity.Jobs; +using Unity.Collections.LowLevel.Unsafe; +using Pathfinding.Util; +using UnityEngine.Assertions; + +namespace Pathfinding.Graphs.Grid.Jobs { + /// <summary> + /// Prepares a set of raycast commands for a grid graph. + /// + /// Each ray will start at <see cref="raycastOffset"/> from the node's position. The end point of the raycast will be the start point + <see cref="raycastDirection"/>. + /// + /// See: <see cref="GraphCollision"/> + /// </summary> + [BurstCompile] + public struct JobPrepareGridRaycast : IJob { + public Matrix4x4 graphToWorld; + public IntBounds bounds; + public Vector3 raycastOffset; + public Vector3 raycastDirection; + public LayerMask raycastMask; + public PhysicsScene physicsScene; + + [WriteOnly] + public NativeArray<RaycastCommand> raycastCommands; + + public void Execute () { + float raycastLength = raycastDirection.magnitude; + var size = bounds.size; + + // In particular Unity 2022.2 seems to assert that RaycastCommands use normalized directions + var direction = raycastDirection.normalized; + var commands = raycastCommands.AsUnsafeSpan(); + + Assert.AreEqual(commands.Length, size.x * size.z); + +#if UNITY_2022_2_OR_NEWER + var queryParameters = new QueryParameters(raycastMask, false, QueryTriggerInteraction.Ignore, false); + // This is about 30% faster than setting each command individually. MemCpy is fast. + commands.Fill(new RaycastCommand(physicsScene, Vector3.zero, direction, queryParameters, raycastLength)); +#else + const int RaycastMaxHits = 1; +#endif + + for (int z = 0; z < size.z; z++) { + int zw = z * size.x; + for (int x = 0; x < size.x; x++) { + int idx = zw + x; + var pos = JobNodeGridLayout.NodePosition(graphToWorld, x + bounds.min.x, z + bounds.min.z); +#if UNITY_2022_2_OR_NEWER + commands[idx].from = pos + raycastOffset; +#else + commands[idx] = new RaycastCommand(pos + raycastOffset, direction, raycastLength, raycastMask, RaycastMaxHits); +#endif + } + } + } + } +} |