Advertisement
443eb9

Untitled

Sep 25th, 2023 (edited)
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.59 KB | None | 0 0
  1. using Unity.Entities;
  2. using Unity.Physics;
  3.  
  4. namespace HexagonSurvive.HexPhysics
  5. {
  6.     public struct RaycastTaskData : IComponentData
  7.     {
  8.         public bool isDisposed;
  9.         public RaycastHit hit;
  10.         public RaycastInput input;
  11.     }
  12. }
  13.  
  14. ---------------------------------------------------------------------
  15.  
  16. using Unity.Burst;
  17. using Unity.Collections;
  18. using Unity.Jobs;
  19. using Unity.Physics;
  20.  
  21. namespace HexagonSurvive.HexPhysics
  22. {
  23.     [BurstCompile]
  24.     public struct RaycastJob : IJobFor
  25.     {
  26.         [ReadOnly]
  27.         public CollisionWorld collisionWorld;
  28.         public NativeArray<RaycastTaskData> inputs;
  29.  
  30.         public void Execute(int index)
  31.         {
  32.             if (!inputs[index].isDisposed)
  33.                 return;
  34.  
  35.             RaycastHit hit;
  36.             collisionWorld.CastRay(inputs[index].input, out hit);
  37.             inputs[index] = new RaycastTaskData { hit = hit };
  38.         }
  39.     }
  40. }
  41.  
  42. ---------------------------------------------------------------------
  43.  
  44. using System;
  45. using Unity.Collections;
  46. using Unity.Entities;
  47. using Unity.Jobs;
  48. using Unity.Physics;
  49.  
  50. namespace HexagonSurvive.HexPhysics
  51. {
  52.     [UpdateInGroup(typeof(InitializationSystemGroup))]
  53.     public partial class BatchRaycastSystem : SystemBase
  54.     {
  55.         private JobHandle _jobHandle;
  56.         private RaycastJob _job;
  57.         private NativeArray<RaycastTaskData> _data;
  58.  
  59.         protected override void OnCreate()
  60.         {
  61.             RequireForUpdate<PhysicsWorldSingleton>();
  62.             _data = new NativeArray<RaycastTaskData>(16, Allocator.Persistent);
  63.  
  64.             for (int i = 0; i < _data.Length; i++)
  65.                 _data[i] = new RaycastTaskData { isDisposed = true };
  66.         }
  67.  
  68.         protected override void OnUpdate()
  69.         {
  70.             if (_jobHandle.IsCompleted)
  71.             {
  72.                 _jobHandle.Complete();
  73.                 _jobHandle = new RaycastJob
  74.                 {
  75.                     collisionWorld = SystemAPI.GetSingleton<PhysicsWorldSingleton>().CollisionWorld,
  76.                     inputs = _data
  77.                 }.Schedule(_data.Length, default);
  78.             }
  79.         }
  80.  
  81.         protected override void OnDestroy()
  82.         {
  83.             _data.Dispose();
  84.         }
  85.  
  86.         /// <summary>
  87.         /// Schedule a raycast task and you can get that result next frame.
  88.         /// </summary>
  89.         /// <param name="input">Input data</param>
  90.         /// <returns>Id of the task.</returns>
  91.         /// <exception cref="Exception">Thrown if the buffer is full.</exception>
  92.         public int ScheduleRaycastTask(RaycastInput input)
  93.         {
  94.             for (int i = 0; i < _data.Length; i++)
  95.             {
  96.                 if (_data[i].isDisposed)
  97.                 {
  98.                     _data[i] = new RaycastTaskData { input = input };
  99.                     return i;
  100.                 }
  101.             }
  102.  
  103.             throw new Exception("Raycast task buffer is too small! Expand it larger!");
  104.         }
  105.  
  106.         /// <summary>
  107.         /// Get the hit result of the task by id.
  108.         /// </summary>
  109.         /// <param name="id">Task id</param>
  110.         /// <param name="hit">The hit result</param>
  111.         /// <returns>If the task is done and the return value is valid.</returns>
  112.         public bool TryGetRaycastTaskResult(int id, out RaycastHit hit)
  113.         {
  114.             if (!_jobHandle.IsCompleted)
  115.             {
  116.                 hit = default;
  117.                 return false;
  118.             }
  119.  
  120.             hit = _data[id].hit;
  121.             _data[id] = new RaycastTaskData { isDisposed = true };
  122.             return true;
  123.         }
  124.     }
  125. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement