Hyluss

[Unity] Noise

Mar 2nd, 2018
340
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.42 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public static class Noise {
  6.  
  7.     //lacunarity - describe the construction of fractal
  8.  
  9.     public static float [,] GenerateNoiseMap(int mapWidth, int mapHeight, int seed, float scale, int octaves, float persistance, float lacunarity, Vector2 offset)
  10.     {
  11.         float[,] noiseMap = new float[mapWidth, mapHeight];
  12.  
  13.         System.Random prng = new System.Random(seed);
  14.         Vector2[] octaveOffsets = new Vector2[octaves];
  15.         for (int i = 0; i < octaves; i++)
  16.         {
  17.             float offsetX = prng.Next(-100000, 100000) + offset.x;
  18.             float offsetY = prng.Next(-100000, 100000) + offset.y;
  19.             octaveOffsets[i] = new Vector2(offsetX, offsetY);
  20.         }
  21.  
  22.         if(scale<=0)
  23.         {
  24.             scale = 0.001f;
  25.         }
  26.  
  27.         float maxNoiseHeight = float.MinValue;
  28.         float minNoiseHeight = float.MaxValue;
  29.  
  30.         for (int y = 0; y < mapHeight; y++)
  31.         {
  32.             for (int x = 0; x< mapWidth; x++)
  33.             {
  34.                 float amplitude = 1;
  35.                 float frequency = 1;
  36.                 float noiseHeight = 0;
  37.  
  38.                 for (int i = 0; i < octaves; i++)
  39.                 {
  40.                     float sampleX = x / scale * frequency + octaveOffsets[i].x;
  41.                     float sampleY = y / scale * frequency + octaveOffsets[i].y;
  42.  
  43.                     float perlinNoise = Mathf.PerlinNoise(sampleX, sampleY) * 2 - 1; //Returns value between 0.0 and 1.0
  44.                                                                              // noiseMap[x, y] = perlinNoise;
  45.                     noiseHeight += perlinNoise * amplitude;
  46.                     amplitude *= persistance;
  47.                     frequency *= lacunarity;
  48.                 }
  49.  
  50.                 if(noiseHeight > maxNoiseHeight)
  51.                 {
  52.                     maxNoiseHeight = noiseHeight;
  53.                 }
  54.                 else if (noiseHeight < minNoiseHeight)
  55.                 {
  56.                     minNoiseHeight = noiseHeight;
  57.                 }
  58.  
  59.                 noiseMap[x, y] = noiseHeight;
  60.             }
  61.         }
  62.  
  63.         for (int y = 0; y < mapHeight; y++)
  64.         {
  65.             for (int x = 0; x < mapWidth; x++)
  66.             {
  67.                 noiseMap[x, y] = Mathf.InverseLerp(minNoiseHeight, maxNoiseHeight, noiseMap[x, y]);
  68.             }
  69.         }
  70.  
  71.         return noiseMap;
  72.     }
  73. }
Advertisement
Comments
  • User was banned
Add Comment
Please, Sign In to add comment