RaenirSalazar

Untitled

Dec 30th, 2020
838
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.57 KB | None | 0 0
  1.     public float[] PopulateArray1D(int sizeX, int sizeY)
  2.     {
  3.         // Try to use .TempJob allocation instead of persistant. Keeps unintentional memory leaks minimized.
  4.         var unscaledArray = new NativeArray<float>(sizeX * sizeY, Allocator.TempJob);
  5.         var scaledArray = new NativeArray<float>(sizeX * sizeY, Allocator.TempJob);
  6.         var minQueue = new NativeQueue<float>(Allocator.TempJob);
  7.         var maxQueue = new NativeQueue<float>(Allocator.TempJob);
  8.         var min = new NativeArray<float>(1, Allocator.TempJob);
  9.         var max = new NativeArray<float>(1, Allocator.TempJob);
  10.  
  11.         JobHandle handle = new MapChunkEvaluateJob
  12.         {
  13.             sizeX = sizeX,
  14.             array = unscaledArray,
  15.             Min = minQueue.AsParallelWriter(),
  16.             Max = maxQueue.AsParallelWriter(),
  17.             f = frequency
  18.             // This inlines everything. It initializes the parallel threads and stops the main thread
  19.             // until it's done.
  20.         }.Schedule(unscaledArray.Length, 1);
  21.  
  22.         JobHandle handle2 = new ComputeMinMax
  23.         {
  24.             Mins = minQueue,
  25.             Maxs = maxQueue,
  26.             Min = min,
  27.             Max = max
  28.         }.Schedule(handle);
  29.  
  30.         handle2.Complete();
  31.  
  32.         new MapChunkNormalizeJob
  33.         {
  34.             unscaled_array = unscaledArray,
  35.             scaled_array = scaledArray,
  36.             myMin = min[0],
  37.             myMax = max[0]
  38.         }.Schedule(scaledArray.Length, 1, handle2).Complete();
  39.  
  40.         // This is if you really need the output array to be in a managed (regular) 2d array.
  41.         // I personally would just return the collapsedArray and work with that.
  42.         var outputArray = new float[sizeX * sizeY];
  43.         outputArray = scaledArray.ToArray();
  44.  
  45.         // DO NOT FORGET TO DEALLOCATE THE NATIVE ARRAY WHEN YOU ARE DONE!!!!!!
  46.         unscaledArray.Dispose();
  47.         scaledArray.Dispose();
  48.         minQueue.Dispose();
  49.         maxQueue.Dispose();
  50.         min.Dispose();
  51.         max.Dispose();
  52.  
  53.         return outputArray;
  54.     }
  55.  
  56.     [BurstCompile]
  57.     private struct ComputeMinMax : IJob
  58.     {
  59.         public NativeQueue<float> Mins;
  60.  
  61.         public NativeQueue<float> Maxs;
  62.  
  63.         // Output
  64.         public NativeArray<float> Min;
  65.         public NativeArray<float> Max;
  66.  
  67.         public void Execute()
  68.         {
  69.             float min = float.MaxValue;
  70.  
  71.             while (this.Mins.TryDequeue(out var f))
  72.             {
  73.                 if (f < min) min = f;
  74.             }
  75.  
  76.             float max = float.MinValue;
  77.  
  78.             while (this.Maxs.TryDequeue(out var f))
  79.             {
  80.                 if (f > max) max = f;
  81.             }
  82.  
  83.             this.Min[0] = min;
  84.             this.Max[0] = max;
  85.         }
  86.     }
  87.  
  88.     // Burst compile really important. Makes 1 second operations 10 milliseconds. It's amazing.
  89.     [BurstCompile]
  90.     public struct MapChunkNormalizeJob : IJobParallelFor
  91.     {
  92.  
  93.         // This is the "collapsedArray", the 2d array collapsed into a 1d array.
  94.         // The [WriteOnly] is not as nessisary as [ReadOnly] in jobs but it might help on the behind the scenes Unity optimization.
  95.         [ReadOnly] public NativeArray<float> unscaled_array;
  96.  
  97.         [WriteOnly] public NativeArray<float> scaled_array;
  98.  
  99.         [ReadOnly] public float myMin;
  100.         [ReadOnly] public float myMax;
  101.  
  102.         public void Execute(int index)
  103.         {
  104.             float min = myMin;
  105.             float max = myMax;
  106.  
  107.             scaled_array[index] = (unscaled_array[index] - min) / (max - min);
  108.         }
  109.     }
  110.  
  111.     // Burst compile really important. Makes 1 second operations 10 milliseconds. It's amazing.
  112.     [BurstCompile]
  113.     public struct MapChunkEvaluateJob : IJobParallelFor
  114.     {
  115.         // This is needed in order to determine X and Y coordinates from index.
  116.         [ReadOnly] public int sizeX;
  117.  
  118.         [ReadOnly] public float f;
  119.  
  120.         // This is the "collapsedArray", the 2d array collapsed into a 1d array.
  121.         // The [WriteOnly] is not as nessisary as [ReadOnly] in jobs but it might help on the behind the scenes Unity optimization.
  122.         [WriteOnly] public NativeArray<float> array;
  123.  
  124.         // Output
  125.         public NativeQueue<float>.ParallelWriter Min;
  126.  
  127.         public NativeQueue<float>.ParallelWriter Max;
  128.  
  129.         public void Execute(int index)
  130.         {
  131.             float min = float.MaxValue;
  132.             float max = float.MinValue;
  133.  
  134.             float amplitude = 1.0f;
  135.             float sum = 0;
  136.             float lacunarity = 1.25f;
  137.             float frequency = f;
  138.             float gain = 0.5f; // also persistence
  139.             // Previously: Unity.Mathematics.noise.snoise(new Vector2(index, i));
  140.             // Index = X value. i = Y value.
  141.             // We can obtain these values from the index using sizeX
  142.             // Use Unity.Mathematics.float2, it's optimized for Jobs.
  143.             // I dont know if snoise needs a float2 or int2 can be used and you can skip the casting to int.
  144.             for (int i = 0; i < 5; i++)
  145.             {
  146.                 float x = (index % sizeX) * frequency;
  147.                 float y = (index / sizeX) * frequency;
  148.  
  149.                 float value = Unity.Mathematics.noise.snoise(new Unity.Mathematics.float2(x, y));
  150.                 sum += value * amplitude;
  151.                 frequency *= lacunarity;
  152.                 amplitude *= gain;
  153.             }
  154.  
  155.             if (sum > max) max = sum;
  156.             if (sum < min) min = sum;
  157.  
  158.             this.Min.Enqueue(min);
  159.             this.Max.Enqueue(max);
  160.  
  161.             array[index] = sum;
  162.         }
  163.     }
Advertisement
Add Comment
Please, Sign In to add comment