Advertisement
cajphrase

NativeLodJob

Jun 25th, 2024 (edited)
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.27 KB | None | 0 0
  1. public struct NativeLODJob : IJob
  2. {
  3.     public NativeHashSet<AABB> generatedMeshes;
  4.     public NativeList<AABB> meshesToRender;
  5.     public NativeList<AABB> meshesToGenerate;
  6.     public AABB totalBounds;
  7.     public float3 cameraPosition;
  8.     public frustum cameraFrustum;
  9.     public lodParams lodParams;
  10.  
  11.  
  12.     public void Execute()
  13.     {
  14.         var subdivisionQueue = new NativeQueue<AABB>(Allocator.Temp);
  15.         subdivisionQueue.Enqueue(totalBounds);
  16.  
  17.         while (subdivisionQueue.Count > 0)
  18.         {
  19.             var bounds = subdivisionQueue.Dequeue();
  20.             bool hasAllChildMeshes = true;
  21.             AABB newBounds = new MinMaxAABB(bounds.min, bounds.center);
  22.             for (int i = 0; i < 8; i++)
  23.             {
  24.                 var offset = Offsets[i];
  25.                 var b = newBounds;
  26.                 b.center = b.center + bounds.halfExtents * offset;
  27.                 if (!generatedMeshes.Contains(b))
  28.                 {
  29.                     hasAllChildMeshes = false;
  30.                     meshesToGenerate.Add(b);
  31.                 }
  32.             }
  33.             if (!hasAllChildMeshes)
  34.             {
  35.                 meshesToRender.Add(bounds);
  36.                 continue;
  37.             }
  38.             for (int i = 0; i < 8; i++)
  39.             {
  40.                 var offset = Offsets[i];
  41.                 var b = newBounds;
  42.                 b.center = b.center + bounds.halfExtents * offset;
  43.  
  44.                 if (ShouldSubdivide(b))
  45.                 {
  46.                     subdivisionQueue.Enqueue(b);
  47.                 }
  48.                 else if (cameraFrustum.Contains(b))
  49.                 {
  50.                     meshesToRender.Add(b);
  51.                 }
  52.             }
  53.         }
  54.     }
  55.  
  56.     private bool ShouldSubdivide(AABB bounds)
  57.     {
  58.         // do something with lodParams
  59.         var cp = CollisionUtils.ClosestPointOnOrInAABB(cameraPosition, bounds);
  60.         var offset = cp - cameraPosition;
  61.         var maxDist = lodParams.maxMultiplier * bounds.size;
  62.         return any(offset < maxDist);
  63.     }
  64.  
  65.     private static readonly int3[] Offsets =
  66.     {
  67.         new( 0, 0, 0),
  68.         new( 1, 0, 0),
  69.         new( 0, 0, 0),
  70.         new( 1, 0, 0),
  71.         new( 0, 0, 0),
  72.         new( 1, 0, 0),
  73.         new( 0, 0, 0),
  74.         new( 1, 0, 0),
  75.     }
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement