Advertisement
JontePonte

Noise before shader

Sep 27th, 2024
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 8.61 KB | None | 0 0
  1. using Unity.Jobs;
  2. using UnityEngine;
  3. using Unity.Mathematics;
  4. using Unity.Collections;
  5. using Unity.Burst;
  6. using UnityEngine.Rendering;
  7. using System.Security.Cryptography;
  8.  
  9. public class Noise
  10. {
  11.     NativeArray<float> noiseFrequencies;
  12.     NativeArray<float> noiseAmplitudes;
  13.     int octaves;
  14.  
  15.     int3 chunkDimensions;
  16.  
  17.     NativeArray<int3> cornerOffsets;
  18.  
  19.     float warpingStrength;
  20.     float warpingFrequency;
  21.  
  22.     float mountainHeightOffset;
  23.  
  24.     ComputeShader noiseShader;
  25.  
  26.     public Noise(float frequency, float persistance, float lacunarity, float warpingStrength, float warpingFrequency, float mountainHeightOffset, int octaves, ComputeShader noiseShader)
  27.     {
  28.         noiseFrequencies = new NativeArray<float>(octaves, Allocator.Persistent);
  29.         noiseAmplitudes = new NativeArray<float>(octaves, Allocator.Persistent);
  30.  
  31.         for (int i = 0; i < octaves; i++)
  32.         {
  33.             noiseFrequencies[i] = Mathf.Pow(lacunarity, i) * frequency;
  34.             noiseAmplitudes[i] = Mathf.Pow(persistance, i);
  35.         }
  36.  
  37.         this.octaves = octaves;
  38.         this.warpingStrength = warpingStrength;
  39.         this.warpingFrequency = warpingFrequency;
  40.         this.mountainHeightOffset = mountainHeightOffset;
  41.         this.noiseShader = noiseShader;
  42.  
  43.         chunkDimensions = new int3(MeshGenerator.chunkDimensions.x, MeshGenerator.chunkDimensions.y, MeshGenerator.chunkDimensions.z);
  44.  
  45.         cornerOffsets = new NativeArray<int3>(MeshGenerator.cornerOffsets.Length, Allocator.Persistent);
  46.  
  47.         for (int i = 0; i < cornerOffsets.Length; i++)
  48.         {
  49.             cornerOffsets[i] = new int3(MeshGenerator.cornerOffsets[i].x, MeshGenerator.cornerOffsets[i].y, MeshGenerator.cornerOffsets[i].z);
  50.         }
  51.     }
  52.  
  53.     ~Noise()
  54.     {
  55.         noiseFrequencies.Dispose();
  56.         noiseAmplitudes.Dispose();
  57.         cornerOffsets.Dispose();
  58.     }
  59.  
  60.     public NativeArray<float> FractalNoiseMap(Vector2 offset, int lod)
  61.     {
  62.         int x = (chunkDimensions.x - lod) / lod;
  63.         int z = ((chunkDimensions.z - lod) / lod) * chunkDimensions.x;
  64.  
  65.         NativeArray<float> noiseMap = new NativeArray<float>((x + z) * 8 + 8, Allocator.TempJob);
  66.  
  67.         Noise2DJob job = new Noise2DJob
  68.         {
  69.             noiseMap = noiseMap,
  70.             chunkDimensions = chunkDimensions,
  71.             cornerOffsets = cornerOffsets,
  72.  
  73.             octaves = octaves,
  74.             lod = lod,
  75.             noiseFrequencies = noiseFrequencies,
  76.             noiseAmplitudes = noiseAmplitudes,
  77.             offset = new float2(offset.x, offset.y),
  78.             warpingStrength = warpingStrength,
  79.             warpingFrequency = warpingFrequency,
  80.             mountainHeightOffset = mountainHeightOffset
  81.         };
  82.  
  83.         job.Schedule(x + z + 1, 10).Complete();
  84.  
  85.         return noiseMap;
  86.     }
  87.  
  88.     public NativeArray<float> Fractal3DNoiseMap(Vector2 offset, int lod)
  89.     {
  90.         float[] noiseGrid = new float[chunkDimensions.x / lod * chunkDimensions.y / lod * chunkDimensions.z / lod * 8];
  91.  
  92.         ComputeBuffer noiseGridBuffer = new ComputeBuffer(noiseGrid.Length, sizeof(float));
  93.         noiseGridBuffer.SetData(noiseGrid);
  94.  
  95.         noiseShader.SetBuffer(0, "noiseMap", noiseGridBuffer);
  96.         noiseShader.SetBuffer(1, "cornerOffsets", cornerOffsets);
  97.         noiseShader.SetInt("chunkDimensionsX", chunkDimensions.x);
  98.         noiseShader.SetInt("chunkDimensionsY", chunkDimensions.y);
  99.         noiseShader.SetInt("lod", lod);
  100.  
  101.  
  102.         Debug.Log(noiseGrid.Length / 20);
  103.         noiseShader.Dispatch(0, noiseGrid.Length / 20, 1, 1);
  104.  
  105.         noiseGridBuffer.GetData(noiseGrid);
  106.  
  107.         noiseGridBuffer.Dispose();
  108.  
  109.         //NativeArray<float> noiseMap = new NativeArray<float>(chunkDimensions.x / lod * chunkDimensions.y / lod * chunkDimensions.z / lod * 8, Allocator.TempJob);
  110.  
  111.         //Noise3DJob job = new Noise3DJob
  112.         //{
  113.         //    noiseMap = noiseMap,
  114.         //    chunkDimensions = chunkDimensions,
  115.         //    cornerOffsets = cornerOffsets,
  116.  
  117.         //    octaves = octaves,
  118.         //    noiseFrequencies = noiseFrequencies,
  119.         //    noiseAmplitudes = noiseAmplitudes,
  120.         //    lod = lod,
  121.         //    offset = new float2(offset.x, offset.y),
  122.         //    warpingFrequency = warpingFrequency,
  123.         //    warpingStrength = warpingStrength
  124.         //};
  125.  
  126.         //job.Schedule(chunkDimensions.x / lod * chunkDimensions.y / lod * chunkDimensions.z / lod, 10).Complete();
  127.  
  128.         NativeArray<float> noiseMap = new NativeArray<float>(chunkDimensions.x / lod * chunkDimensions.y / lod * chunkDimensions.z / lod * 8, Allocator.TempJob);
  129.         return noiseMap;
  130.     }
  131.  
  132.     [BurstCompile]
  133.     struct Noise2DJob : IJobParallelFor
  134.     {
  135.         [NativeDisableParallelForRestriction]
  136.         public NativeArray<float> noiseMap;
  137.  
  138.         [ReadOnly] public int3 chunkDimensions;
  139.         [ReadOnly] public NativeArray<int3> cornerOffsets;
  140.  
  141.         [ReadOnly] public int octaves;
  142.         [ReadOnly] public int lod;
  143.         [ReadOnly] public float2 offset;
  144.         [ReadOnly] public NativeArray<float> noiseFrequencies;
  145.         [ReadOnly] public NativeArray<float> noiseAmplitudes;
  146.         [ReadOnly] public float warpingStrength;
  147.         [ReadOnly] public float warpingFrequency;
  148.         [ReadOnly] public float mountainHeightOffset;
  149.  
  150.         public void Execute(int i)
  151.         {
  152.             int y = i / chunkDimensions.x * lod;
  153.             int x = i % chunkDimensions.x * lod;
  154.  
  155.             for (int j = 0; j < 8; j++)
  156.             {
  157.                 float xCoord = x + offset.x + cornerOffsets[j].x * lod;
  158.                 float yCoord = y + offset.y + cornerOffsets[j].z * lod;
  159.  
  160.                 float xWarp = FractalNoise((xCoord + 1000) * warpingFrequency, (yCoord - 1000) * warpingFrequency) * warpingStrength;
  161.                 float yWarp = FractalNoise((xCoord + 10000) * warpingFrequency, (yCoord - 10000) * warpingFrequency) * warpingStrength;
  162.  
  163.                 noiseMap[(i * 8) + j] = FractalNoise(xCoord + xWarp, yCoord + yWarp);
  164.             }
  165.         }
  166.  
  167.         float FractalNoise(float x, float y)
  168.         {
  169.             float noiseValue = 0;
  170.  
  171.             for (int i = 0; i < octaves; i++)
  172.             {
  173.                 noiseValue += (noise.snoise(new float2(x * noiseFrequencies[i], y * noiseFrequencies[i])) + 1) * .5f * noiseAmplitudes[i];
  174.             }
  175.  
  176.             return noiseValue;
  177.         }
  178.     }
  179.  
  180.     [BurstCompile]
  181.     struct Noise3DJob : IJobParallelFor
  182.     {
  183.         [NativeDisableParallelForRestriction]
  184.         [WriteOnly] public NativeArray<float> noiseMap;
  185.  
  186.         [ReadOnly] public int3 chunkDimensions;
  187.         [ReadOnly] public NativeArray<int3> cornerOffsets;
  188.  
  189.         [ReadOnly] public int octaves;
  190.         [ReadOnly] public float2 offset;
  191.         [ReadOnly] public NativeArray<float> noiseFrequencies;
  192.         [ReadOnly] public NativeArray<float> noiseAmplitudes;
  193.         [ReadOnly] public int lod;
  194.         [ReadOnly] public float warpingStrength;
  195.         [ReadOnly] public float warpingFrequency;
  196.  
  197.         public void Execute(int i)
  198.         {
  199.             int z = i / (chunkDimensions.x / lod * chunkDimensions.y / lod) * lod;
  200.             int y = (i / (chunkDimensions.x / lod) * lod) - z / lod * chunkDimensions.y;
  201.             int x = i % (chunkDimensions.x / lod) * lod;
  202.  
  203.             //Noise value at each corner of a voxel
  204.             for (int j = 0; j < 8; j++)
  205.             {            
  206.                 float xCoord = x + offset.x + cornerOffsets[j].x * lod;
  207.                 float yCoord = y + cornerOffsets[j].y * lod;
  208.                 float zCoord = z + offset.y + cornerOffsets[j].z * lod;
  209.  
  210.                 float xWarp = Fractal3DNoise((xCoord - 3000f) * warpingFrequency, (yCoord + 8000) * warpingFrequency, (zCoord - 10000) * warpingFrequency) * warpingStrength;
  211.                 float yWarp = Fractal3DNoise((xCoord + 1000) * warpingFrequency, (yCoord - 5000) * warpingFrequency, (zCoord + 30000) * warpingFrequency) * warpingStrength;
  212.                 float zWarp = Fractal3DNoise((xCoord - 11000) * warpingFrequency, (yCoord + 2000) * warpingFrequency, (zCoord + 9000) * warpingFrequency) * warpingStrength;
  213.  
  214.                 noiseMap[(i * 8) + j] = Fractal3DNoise(xCoord + xWarp, yCoord + yWarp, zCoord + zWarp);
  215.             }
  216.         }
  217.  
  218.         public float Fractal3DNoise(float x, float y, float z)
  219.         {
  220.             float noiseValue = 0;
  221.  
  222.             for (int i = 0; i < octaves; i++)
  223.             {
  224.                 noiseValue += (noise.snoise(new float3(x * noiseFrequencies[i], y * noiseFrequencies[i], z * noiseFrequencies[i]) + 1) * .5f * noiseAmplitudes[i]);
  225.             }
  226.  
  227.             return noiseValue;
  228.         }
  229.     }
  230. }
  231.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement