Advertisement
JontePonte

Noise

Sep 17th, 2024
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.06 KB | None | 0 0
  1. using Unity.Jobs;
  2. using UnityEngine;
  3. using Unity.Mathematics;
  4. using System;
  5. using System.Collections;
  6. using Unity.Collections;
  7.  
  8. public class Noise
  9. {
  10.     float[] noiseFrequencies;
  11.     float[] noiseAmplitudes;
  12.  
  13.     float density;
  14.     int octaves;
  15.  
  16.     Vector3Int chunkDimensions;
  17.  
  18.     public Noise(float frequency, float persistance, float lacunarity, float density, int octaves)
  19.     {
  20.         noiseFrequencies = new float[octaves];
  21.         noiseAmplitudes = new float[octaves];
  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.density = density;
  30.         this.octaves = octaves;
  31.  
  32.         chunkDimensions = MeshGenerator.chunkDimensions;
  33.     }
  34.    
  35.     public float[] FractalNoiseMap()
  36.     {
  37.         float[] noiseMap = new float[chunkDimensions.x * chunkDimensions.z * 8];
  38.         Vector3Int[] cornerOffsets = MeshGenerator.cornerOffsets;
  39.  
  40.         int length = chunkDimensions.x * chunkDimensions.z;
  41.  
  42.         for (int i = 0; i < length; i++)
  43.         {
  44.             int y = i / chunkDimensions.x;
  45.             int x = i % chunkDimensions.x;
  46.  
  47.             for (int j = 0; j < 8; j++)
  48.             {
  49.                 //Noise value at each corner of a voxel
  50.                 noiseMap[(i * 8) + j] = FractalNoise(x + cornerOffsets[j].x, y + cornerOffsets[j].z);
  51.             }
  52.         }
  53.  
  54.         return noiseMap;
  55.     }
  56.  
  57.     public float[] Fractal3DNoiseMap()
  58.     {
  59.         float[] noiseMap = new float[chunkDimensions.x * chunkDimensions.y * chunkDimensions.z * 8];
  60.         Vector3Int[] cornerOffsets = MeshGenerator.cornerOffsets;
  61.  
  62.         int length = chunkDimensions.x * chunkDimensions.y * chunkDimensions.z;
  63.  
  64.         for (int i = 0, h = 0; i < length; i++)
  65.         {
  66.             int z = i / (chunkDimensions.x * chunkDimensions.y);
  67.             int x = (i % (chunkDimensions.x * chunkDimensions.y)) / chunkDimensions.y;
  68.             int y = i % chunkDimensions.y;
  69.  
  70.             //Noise value at each corner of a voxel
  71.             for (int j = 0; j < 8; j++, h++)
  72.             {
  73.                 int xCoord = x + cornerOffsets[j].x;
  74.                 int yCoord = y + cornerOffsets[j].y;
  75.                 int zCoord = z + cornerOffsets[j].z;
  76.  
  77.                 noiseMap[h] = Fractal3DNoise(xCoord, yCoord, zCoord);
  78.             }
  79.         }
  80.  
  81.         return noiseMap;
  82.     }
  83.  
  84.     float FractalNoise(int x, int y)
  85.     {
  86.         float noise = 0;
  87.         for (int i = 0; i < octaves; i++)
  88.         {
  89.             noise += Mathf.PerlinNoise(x * noiseFrequencies[i], y * noiseFrequencies[i]) * noiseAmplitudes[i];
  90.         }
  91.  
  92.         return noise;
  93.     }
  94.  
  95.     public float Fractal3DNoise(int x, int y, int z)
  96.     {
  97.         float noise = 0;
  98.  
  99.         for (int i = 0; i < octaves; i++)
  100.         {
  101.             noise += Perlin.Noise(x * noiseFrequencies[i], y * noiseFrequencies[i], z * noiseFrequencies[i]) * noiseAmplitudes[i];
  102.         }
  103.  
  104.         return noise; //- density;
  105.     }
  106.  
  107.  
  108.     struct Noise2DJob : IJobParallelFor
  109.     {
  110.         [WriteOnly] public float[] noiseMap;
  111.  
  112.         [ReadOnly] public int3 chunkDimensions;
  113.         [ReadOnly] public int3[] cornerOffsets;
  114.  
  115.         [ReadOnly] public int octaves;
  116.         [ReadOnly] public float[] noiseFrequencies;
  117.         [ReadOnly] public float[] noiseAmplitudes;
  118.  
  119.         public void Execute(int i)
  120.         {
  121.             int y = i / chunkDimensions.x;
  122.             int x = i % chunkDimensions.x;
  123.  
  124.             for (int j = 0; j < 8; j++)
  125.             {
  126.                 //Noise value at each corner of a voxel
  127.                 noiseMap[(i * 8) + j] = FractalNoise(x + cornerOffsets[j].x, y + cornerOffsets[j].z);
  128.             }
  129.         }
  130.  
  131.         float FractalNoise(int x, int y)
  132.         {
  133.             float noise = 0;
  134.             for (int i = 0; i < octaves; i++)
  135.             {
  136.                 noise += Mathf.PerlinNoise(x * noiseFrequencies[i], y * noiseFrequencies[i]) * noiseAmplitudes[i];
  137.             }
  138.  
  139.             return noise;
  140.         }
  141.     }
  142. }
  143.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement