Advertisement
JontePonte

Noise 2

Sep 22nd, 2024
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.57 KB | None | 0 0
  1. using Unity.Jobs;
  2. using UnityEngine;
  3. using Unity.Mathematics;
  4. using Unity.Collections;
  5. using Unity.Burst;
  6.  
  7. public class Noise
  8. {
  9.     NativeArray<float> noiseFrequencies;
  10.     NativeArray<float> noiseAmplitudes;
  11.     int octaves;
  12.     int lod;
  13.  
  14.     int3 chunkDimensions;
  15.  
  16.     NativeArray<int3> cornerOffsets;
  17.  
  18.     public Noise(float frequency, float persistance, float lacunarity, int octaves, int lod)
  19.     {
  20.         noiseFrequencies = new NativeArray<float>(octaves, Allocator.Persistent);
  21.         noiseAmplitudes = new NativeArray<float>(octaves, Allocator.Persistent);
  22.  
  23.         for (int i = 0; i < octaves; i++)
  24.         {
  25.             noiseFrequencies[i] = Mathf.Pow(lacunarity, i) * frequency;
  26.             noiseAmplitudes[i] = Mathf.Pow(persistance, i);
  27.         }
  28.  
  29.         this.octaves = octaves;
  30.         this.lod = lod;
  31.  
  32.         chunkDimensions = new int3(MeshGenerator.chunkDimensions.x, MeshGenerator.chunkDimensions.y, MeshGenerator.chunkDimensions.z);
  33.  
  34.         cornerOffsets = new NativeArray<int3>(MeshGenerator.cornerOffsets.Length, Allocator.Persistent);
  35.  
  36.         for (int i = 0; i < cornerOffsets.Length; i++)
  37.         {
  38.             cornerOffsets[i] = new int3(MeshGenerator.cornerOffsets[i].x, MeshGenerator.cornerOffsets[i].y, MeshGenerator.cornerOffsets[i].z);
  39.         }
  40.     }
  41.  
  42.     ~Noise()
  43.     {
  44.         noiseFrequencies.Dispose();
  45.         noiseAmplitudes.Dispose();
  46.         cornerOffsets.Dispose();
  47.     }
  48.  
  49.     public NativeArray<float> FractalNoiseMap()
  50.     {
  51.         int x = (chunkDimensions.x - lod) / lod;
  52.         int z = ((chunkDimensions.z - lod) / lod) * chunkDimensions.x;
  53.  
  54.         NativeArray<float> noiseMap = new NativeArray<float>((x + z) * 8 + 8, Allocator.TempJob);
  55.  
  56.         Noise2DJob job = new Noise2DJob
  57.         {
  58.             noiseMap = noiseMap,
  59.             chunkDimensions = chunkDimensions,
  60.             cornerOffsets = cornerOffsets,
  61.  
  62.             octaves = octaves,
  63.             lod = lod,
  64.             noiseFrequencies = noiseFrequencies,
  65.             noiseAmplitudes = noiseAmplitudes
  66.         };
  67.  
  68.         job.Schedule(x + z + 1, 10).Complete();
  69.  
  70.         return noiseMap;
  71.     }
  72.  
  73.     public NativeArray<float> Fractal3DNoiseMap()
  74.     {
  75.         NativeArray<float> noiseMap = new NativeArray<float>(chunkDimensions.x * chunkDimensions.y * chunkDimensions.z* 8, Allocator.TempJob);
  76.  
  77.         Noise3DJob job = new Noise3DJob
  78.         {
  79.             noiseMap = noiseMap,
  80.             chunkDimensions = chunkDimensions,
  81.             cornerOffsets = cornerOffsets,
  82.  
  83.             octaves = octaves,
  84.             noiseFrequencies = noiseFrequencies,
  85.             noiseAmplitudes = noiseAmplitudes,
  86.             lod = lod
  87.            
  88.         };
  89.  
  90.         job.Schedule(chunkDimensions.x * chunkDimensions.y * chunkDimensions.z , 10).Complete();
  91.  
  92.         return noiseMap;
  93.     }
  94.  
  95.     [BurstCompile]
  96.     struct Noise2DJob : IJobParallelFor
  97.     {
  98.         [NativeDisableParallelForRestriction]
  99.         [WriteOnly] public NativeArray<float> noiseMap;
  100.  
  101.         [ReadOnly] public int3 chunkDimensions;
  102.         [ReadOnly] public NativeArray<int3> cornerOffsets;
  103.  
  104.         [ReadOnly] public int octaves;
  105.         [ReadOnly] public int lod;
  106.         [ReadOnly] public NativeArray<float> noiseFrequencies;
  107.         [ReadOnly] public NativeArray<float> noiseAmplitudes;
  108.  
  109.         public void Execute(int i)
  110.         {
  111.             int y = i / chunkDimensions.x; //* lod;
  112.             int x = i % chunkDimensions.x; //* lod;
  113.  
  114.             for (int j = 0; j < 8; j++)
  115.             {
  116.                 //Noise value at each corner of a voxel
  117.                 noiseMap[(i * 8) + j] = FractalNoise(x + (cornerOffsets[j].x / lod), y + (cornerOffsets[j].z / lod));
  118.             }
  119.         }
  120.  
  121.         float FractalNoise(int x, int y)
  122.         {
  123.             float noiseValue = 0;
  124.  
  125.             for (int i = 0; i < octaves; i++)
  126.             {
  127.                 noiseValue += (noise.snoise(new float2(x * noiseFrequencies[i], y * noiseFrequencies[i])) + 1) * .5f * noiseAmplitudes[i];
  128.             }
  129.  
  130.             return noiseValue;
  131.         }
  132.     }
  133.  
  134.     [BurstCompile]
  135.     struct Noise3DJob : IJobParallelFor
  136.     {
  137.         [NativeDisableParallelForRestriction]
  138.         [WriteOnly] public NativeArray<float> noiseMap;
  139.  
  140.         [ReadOnly] public int3 chunkDimensions;
  141.         [ReadOnly] public NativeArray<int3> cornerOffsets;
  142.  
  143.         [ReadOnly] public int octaves;
  144.         [ReadOnly] public NativeArray<float> noiseFrequencies;
  145.         [ReadOnly] public NativeArray<float> noiseAmplitudes;
  146.         [ReadOnly] public int lod;
  147.  
  148.         public void Execute(int i)
  149.         {
  150.             int z = i / (chunkDimensions.x * chunkDimensions.y);
  151.             int y = i % (chunkDimensions.x * chunkDimensions.y) / chunkDimensions.x;
  152.             int x = i % chunkDimensions.x;
  153.  
  154.             //Noise value at each corner of a voxel
  155.             for (int j = 0; j < 8; j++)
  156.             {
  157.                 int xCoord = x + (cornerOffsets[j].x / lod);
  158.                 int yCoord = y + (cornerOffsets[j].y / lod);
  159.                 int zCoord = z + (cornerOffsets[j].z / lod);
  160.  
  161.                 noiseMap[(i * 8) + j] = Fractal3DNoise(xCoord, yCoord, zCoord);
  162.             }
  163.         }
  164.  
  165.         public float Fractal3DNoise(int x, int y, int z)
  166.         {
  167.             float noiseValue = 0;
  168.  
  169.             for (int i = 0; i < octaves; i++)
  170.             {
  171.                 noiseValue += (noise.snoise(new float3(x * noiseFrequencies[i], y * noiseFrequencies[i], z * noiseFrequencies[i]) + 1) * .5f * noiseAmplitudes[i]);
  172.             }
  173.  
  174.             return noiseValue; //- density;
  175.         }
  176.     }
  177. }
  178.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement