RaenirSalazar

Untitled

Dec 30th, 2020
1,106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.37 KB | None | 0 0
  1.     public class MapChunk
  2.     {
  3.         public int MapSize;
  4.  
  5.         public MapChunk(int size)
  6.         {
  7.             MapSize = size;
  8.         }
  9.  
  10.         public struct MapChunkData
  11.         {
  12.             public float[] _mapChunk;
  13.             public int Size { get; set; }
  14.  
  15.             public MapChunkData(MapChunk mapChunk)
  16.             {
  17.                 _mapChunk = new float[mapChunk.MapSize];
  18.                 Size = mapChunk.MapSize;                
  19.             }
  20.  
  21.             public void Evaluate(int index)
  22.             {
  23.                 for (int i = 0; i < Size; i++)
  24.                 {
  25.                     _mapChunk[i] = Unity.Mathematics.noise.snoise(new Vector2(index, i));
  26.                 }
  27.             }
  28.         }
  29.     }
  30.  
  31.  
  32.     public struct MapChunkEvaluateJob : IJobParallelFor
  33.     {
  34.         public NativeArray<MapChunk.MapChunkData> chunks;
  35.  
  36.         public void Execute(int index)
  37.         {            
  38.             var chunk = chunks[index];
  39.             chunk.Evaluate(index);
  40.             chunks[index] = chunk;
  41.         }
  42.     }
  43.  
  44.     private MapChunkEvaluateJob _job;
  45.     private NativeArray<MapChunk.MapChunkData> _mapChunkDataArray;
  46.     List<MapChunk> mapChunks = new List<MapChunk>();
  47.     // wouldn't it be better if there was some heightmap array that the jobs wrote to directly?
  48.  
  49.     public void InitMapGeneratorJobs()
  50.     {
  51.         // setup        
  52.         MapChunk.MapChunkData[] mapChunkData = new MapChunk.MapChunkData[128];
  53.  
  54.         for (int i = 0; i < mapChunkData.Length; i++)
  55.         {
  56.             MapChunk mp = new MapChunk(128);
  57.             mapChunks.Add(mp);
  58.             mapChunkData[i] = new MapChunk.MapChunkData(mapChunks[i]);
  59.         }
  60.  
  61.         _mapChunkDataArray = new NativeArray<MapChunk.MapChunkData>(mapChunkData, Allocator.Persistent);
  62.  
  63.         _job = new MapChunkEvaluateJob
  64.         {
  65.             chunks = _mapChunkDataArray
  66.         };
  67.     }
  68.  
  69.     public void RunMapGeneratorJobs()
  70.     {
  71.         var jobHandle = _job.Schedule(128, 1); // can this be better?
  72.         jobHandle.Complete();
  73.  
  74.         float[,] parallelledHeightMap = new float[128, 128];
  75.        
  76.         // can this be better?
  77.         for (int i = 0; i < 128; i++)
  78.         {
  79.             for (int j = 0; j < 128; j++)
  80.             {
  81.                 parallelledHeightMap[i, j] = _job.chunks[i]._mapChunk[j]; // this???
  82.             }
  83.         }
  84.     }
Advertisement
Add Comment
Please, Sign In to add comment