Advertisement
JontePonte

Noise with mountains

Sep 25th, 2024
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 8.99 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.     public Noise(float frequency, float persistance, float lacunarity, float warpingStrength, float warpingFrequency, float mountainHeightOffset, int octaves)
  25.     {
  26.         noiseFrequencies = new NativeArray<float>(octaves, Allocator.Persistent);
  27.         noiseAmplitudes = new NativeArray<float>(octaves, Allocator.Persistent);
  28.  
  29.         for (int i = 0; i < octaves; i++)
  30.         {
  31.             noiseFrequencies[i] = Mathf.Pow(lacunarity, i) * frequency;
  32.             noiseAmplitudes[i] = Mathf.Pow(persistance, i);
  33.         }
  34.  
  35.         this.octaves = octaves;
  36.         this.warpingStrength = warpingStrength;
  37.         this.warpingFrequency = warpingFrequency;
  38.         this.mountainHeightOffset = mountainHeightOffset;
  39.  
  40.         chunkDimensions = new int3(MeshGenerator.chunkDimensions.x, MeshGenerator.chunkDimensions.y, MeshGenerator.chunkDimensions.z);
  41.  
  42.         cornerOffsets = new NativeArray<int3>(MeshGenerator.cornerOffsets.Length, Allocator.Persistent);
  43.  
  44.         for (int i = 0; i < cornerOffsets.Length; i++)
  45.         {
  46.             cornerOffsets[i] = new int3(MeshGenerator.cornerOffsets[i].x, MeshGenerator.cornerOffsets[i].y, MeshGenerator.cornerOffsets[i].z);
  47.         }
  48.     }
  49.  
  50.     ~Noise()
  51.     {
  52.         noiseFrequencies.Dispose();
  53.         noiseAmplitudes.Dispose();
  54.         cornerOffsets.Dispose();
  55.     }
  56.  
  57.     public NativeArray<float> FractalNoiseMap(Vector2 offset, int lod)
  58.     {
  59.         int x = (chunkDimensions.x - lod) / lod;
  60.         int z = ((chunkDimensions.z - lod) / lod) * chunkDimensions.x;
  61.  
  62.         NativeArray<float> noiseMap = new NativeArray<float>((x + z) * 8 + 8, Allocator.TempJob);
  63.  
  64.         Noise2DJob job = new Noise2DJob
  65.         {
  66.             noiseMap = noiseMap,
  67.             chunkDimensions = chunkDimensions,
  68.             cornerOffsets = cornerOffsets,
  69.  
  70.             octaves = octaves,
  71.             lod = lod,
  72.             noiseFrequencies = noiseFrequencies,
  73.             noiseAmplitudes = noiseAmplitudes,
  74.             offset = new float2(offset.x, offset.y),
  75.             warpingStrength = warpingStrength,
  76.             warpingFrequency = warpingFrequency,
  77.             mountainHeightOffset = mountainHeightOffset
  78.         };
  79.  
  80.         job.Schedule(x + z + 1, 10).Complete();
  81.  
  82.         return noiseMap;
  83.     }
  84.  
  85.     public NativeArray<float> Fractal3DNoiseMap(Vector2 offset, int lod)
  86.     {
  87.         NativeArray<float> noiseMap = new NativeArray<float>(chunkDimensions.x / lod * chunkDimensions.y / lod * chunkDimensions.z / lod * 8, Allocator.TempJob);
  88.  
  89.         Noise3DJob job = new Noise3DJob
  90.         {
  91.             noiseMap = noiseMap,
  92.             chunkDimensions = chunkDimensions,
  93.             cornerOffsets = cornerOffsets,
  94.  
  95.             octaves = octaves,
  96.             noiseFrequencies = noiseFrequencies,
  97.             noiseAmplitudes = noiseAmplitudes,
  98.             lod = lod,
  99.             offset = new float2(offset.x, offset.y),
  100.             warpingFrequency = warpingFrequency,
  101.             warpingStrength = warpingStrength
  102.         };
  103.  
  104.         job.Schedule(chunkDimensions.x / lod * chunkDimensions.y / lod * chunkDimensions.z / lod, 10).Complete();
  105.  
  106.         return noiseMap;
  107.     }
  108.  
  109.     [BurstCompile]
  110.     struct Noise2DJob : IJobParallelFor
  111.     {
  112.         [NativeDisableParallelForRestriction]
  113.         public NativeArray<float> noiseMap;
  114.  
  115.         [ReadOnly] public int3 chunkDimensions;
  116.         [ReadOnly] public NativeArray<int3> cornerOffsets;
  117.  
  118.         [ReadOnly] public int octaves;
  119.         [ReadOnly] public int lod;
  120.         [ReadOnly] public float2 offset;
  121.         [ReadOnly] public NativeArray<float> noiseFrequencies;
  122.         [ReadOnly] public NativeArray<float> noiseAmplitudes;
  123.         [ReadOnly] public float warpingStrength;
  124.         [ReadOnly] public float warpingFrequency;
  125.         [ReadOnly] public float mountainHeightOffset;
  126.  
  127.         public void Execute(int i)
  128.         {
  129.             int y = i / chunkDimensions.x * lod;
  130.             int x = i % chunkDimensions.x * lod;
  131.  
  132.             for (int j = 0; j < 8; j++)
  133.             {
  134.                 float xCoord = x + offset.x + cornerOffsets[j].x * lod;
  135.                 float yCoord = y + offset.y + cornerOffsets[j].y * lod;
  136.  
  137.                 float xWarp = FractalNoise((xCoord + 1000) * warpingFrequency, (yCoord - 1000) * warpingFrequency) * warpingStrength;
  138.                 float yWarp = FractalNoise((yCoord + 10000) * warpingFrequency, (yCoord - 10000) * warpingFrequency) * warpingStrength;
  139.  
  140.                 //float mountains = MountainFractalNoise(x + 6000 + cornerOffsets[j].x * lod, y - 7000 + cornerOffsets[j].z * lod);
  141.                 float regular = FractalNoise(x + xWarp + offset.x + cornerOffsets[j].x * lod, y + yWarp + offset.x + cornerOffsets[j].z * lod);
  142.  
  143.                 //float height01 = Utils.Remap(y, 0, chunkDimensions.y - 1, 0, 1);
  144.  
  145.                 noiseMap[(i * 8) + j] = regular; //BlendNoise(regular, mountains, height01);
  146.             }
  147.         }
  148.  
  149.         float FractalNoise(float x, float y)
  150.         {
  151.             float noiseValue = 0;
  152.  
  153.             for (int i = 0; i < octaves; i++)
  154.             {
  155.                 noiseValue += (noise.snoise(new float2(x * noiseFrequencies[i], y * noiseFrequencies[i])) + 1) * .5f * noiseAmplitudes[i];
  156.             }
  157.  
  158.             return noiseValue;
  159.         }
  160.  
  161.         float MountainFractalNoise(float x, float y)
  162.         {
  163.             float noiseValue = 0;
  164.  
  165.             for (int i = 0; i < octaves; i++)
  166.             {
  167.                 float frequency = noiseFrequencies[i] / 2f;
  168.                 float amplitude = noiseAmplitudes[i] * 2f;
  169.  
  170.                 noiseValue += (1 - math.abs(noise.snoise(new float2((x + offset.x) * frequency, (y + offset.y) * frequency)))) * amplitude;
  171.             }
  172.  
  173.             return noiseValue;
  174.         }
  175.  
  176.         float BlendNoise(float noise1, float noise2, float height)
  177.         {
  178.             float hMin = .4f; // Start blending at this height
  179.             float hMax = .7f; // End blending at this height
  180.  
  181.             if (height <= hMin)
  182.             {
  183.                 return noise1;
  184.             }
  185.  
  186.             else if (height >= hMax)
  187.             {
  188.                 return noise2;
  189.             }
  190.  
  191.             else
  192.             {
  193.                 float t = (height - hMin) / (hMax - hMin);
  194.                 return (1 - t) * noise1 + t * noise2;
  195.             }
  196.  
  197.         }
  198.  
  199.     }
  200.  
  201.     [BurstCompile]
  202.     struct Noise3DJob : IJobParallelFor
  203.     {
  204.         [NativeDisableParallelForRestriction]
  205.         [WriteOnly] public NativeArray<float> noiseMap;
  206.  
  207.         [ReadOnly] public int3 chunkDimensions;
  208.         [ReadOnly] public NativeArray<int3> cornerOffsets;
  209.  
  210.         [ReadOnly] public int octaves;
  211.         [ReadOnly] public float2 offset;
  212.         [ReadOnly] public NativeArray<float> noiseFrequencies;
  213.         [ReadOnly] public NativeArray<float> noiseAmplitudes;
  214.         [ReadOnly] public int lod;
  215.         [ReadOnly] public float warpingStrength;
  216.         [ReadOnly] public float warpingFrequency;
  217.  
  218.         public void Execute(int i)
  219.         {
  220.             int z = i / (chunkDimensions.x / lod * chunkDimensions.y / lod) * lod;
  221.             int y = (i / (chunkDimensions.x / lod) * lod) - z / lod * chunkDimensions.y;
  222.             int x = i % (chunkDimensions.x / lod) * lod;
  223.  
  224.             //Noise value at each corner of a voxel
  225.             for (int j = 0; j < 8; j++)
  226.             {
  227.                
  228.                 float xCoord = x + offset.x + cornerOffsets[j].x * lod;
  229.                 float yCoord = y + cornerOffsets[j].y * lod;
  230.                 float zCoord = z + offset.y + cornerOffsets[j].z * lod;
  231.  
  232.                 float xWarp = Fractal3DNoise((xCoord - 3000f) * warpingFrequency, (yCoord + 8000) * warpingFrequency, (zCoord - 10000) * warpingFrequency) * warpingStrength;
  233.                 float yWarp = Fractal3DNoise((xCoord + 1000) * warpingFrequency, (yCoord - 5000) * warpingFrequency, (zCoord + 30000) * warpingFrequency) * warpingStrength;
  234.                 float zWarp = Fractal3DNoise((xCoord - 11000) * warpingFrequency, (yCoord + 2000) * warpingFrequency, (zCoord + 9000) * warpingFrequency) * warpingStrength;
  235.  
  236.                 noiseMap[(i * 8) + j] = Fractal3DNoise(xCoord + xWarp, yCoord + yWarp, zCoord + zWarp);
  237.             }
  238.         }
  239.  
  240.         public float Fractal3DNoise(float x, float y, float z)
  241.         {
  242.             float noiseValue = 0;
  243.  
  244.             for (int i = 0; i < octaves; i++)
  245.             {
  246.                 noiseValue += (noise.snoise(new float3(x * noiseFrequencies[i], y * noiseFrequencies[i], z * noiseFrequencies[i]) + 1) * .5f * noiseAmplitudes[i]);
  247.             }
  248.  
  249.             return noiseValue;
  250.         }
  251.     }
  252. }
  253.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement