|
@@ -1,44 +1,57 @@
|
|
|
using UnityEngine;
|
|
using UnityEngine;
|
|
|
|
|
+using System.Collections;
|
|
|
|
|
|
|
|
public static class Noise
|
|
public static class Noise
|
|
|
{
|
|
{
|
|
|
- public static float[,] GenerateNoiseMap(int mapWidth, int mapHeight, int seed, float scale, int octaves, float persistance, float lacunarity, Vector2 offset)
|
|
|
|
|
|
|
+
|
|
|
|
|
+ public enum NormalizeMode { Local, Global };
|
|
|
|
|
+
|
|
|
|
|
+ public static float[,] GenerateNoiseMap(int mapWidth, int mapHeight, int seed, float scale, int octaves, float persistance, float lacunarity, Vector2 offset, NormalizeMode normalizeMode)
|
|
|
{
|
|
{
|
|
|
float[,] noiseMap = new float[mapWidth, mapHeight];
|
|
float[,] noiseMap = new float[mapWidth, mapHeight];
|
|
|
|
|
|
|
|
System.Random prng = new System.Random(seed);
|
|
System.Random prng = new System.Random(seed);
|
|
|
Vector2[] octaveOffsets = new Vector2[octaves];
|
|
Vector2[] octaveOffsets = new Vector2[octaves];
|
|
|
- for (int i=0; i<octaves; i++)
|
|
|
|
|
|
|
+
|
|
|
|
|
+ float maxPossibleHeight = 0;
|
|
|
|
|
+ float amplitude = 1;
|
|
|
|
|
+ float frequency = 1;
|
|
|
|
|
+
|
|
|
|
|
+ for (int i = 0; i < octaves; i++)
|
|
|
{
|
|
{
|
|
|
float offsetX = prng.Next(-100000, 100000) + offset.x;
|
|
float offsetX = prng.Next(-100000, 100000) + offset.x;
|
|
|
- float offsetY = prng.Next(-100000, 100000) + offset.y;
|
|
|
|
|
|
|
+ float offsetY = prng.Next(-100000, 100000) - offset.y;
|
|
|
octaveOffsets[i] = new Vector2(offsetX, offsetY);
|
|
octaveOffsets[i] = new Vector2(offsetX, offsetY);
|
|
|
|
|
+
|
|
|
|
|
+ maxPossibleHeight += amplitude;
|
|
|
|
|
+ amplitude *= persistance;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- if(scale <= 0)
|
|
|
|
|
|
|
+ if (scale <= 0)
|
|
|
{
|
|
{
|
|
|
- scale = .0001f;
|
|
|
|
|
|
|
+ scale = 0.0001f;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- float maxNoiseHeight = float.MinValue;
|
|
|
|
|
- float minNoiseHeight = float.MaxValue;
|
|
|
|
|
|
|
+ float maxLocalNoiseHeight = float.MinValue;
|
|
|
|
|
+ float minLocalNoiseHeight = float.MaxValue;
|
|
|
|
|
+
|
|
|
|
|
+ float halfWidth = mapWidth / 2f;
|
|
|
|
|
+ float halfHeight = mapHeight / 2f;
|
|
|
|
|
|
|
|
- float halfWidth = mapWidth / 2;
|
|
|
|
|
- float halfHeight = mapHeight / 2;
|
|
|
|
|
|
|
|
|
|
- for(int y = 0; y < mapHeight; y++)
|
|
|
|
|
|
|
+ for (int y = 0; y < mapHeight; y++)
|
|
|
{
|
|
{
|
|
|
- for(int x = 0; x < mapWidth; x++)
|
|
|
|
|
|
|
+ for (int x = 0; x < mapWidth; x++)
|
|
|
{
|
|
{
|
|
|
- float amplitude = 1;
|
|
|
|
|
- float frequency = 1;
|
|
|
|
|
|
|
+
|
|
|
|
|
+ amplitude = 1;
|
|
|
|
|
+ frequency = 1;
|
|
|
float noiseHeight = 0;
|
|
float noiseHeight = 0;
|
|
|
|
|
|
|
|
- for(int i=0; i<octaves; i++)
|
|
|
|
|
|
|
+ for (int i = 0; i < octaves; i++)
|
|
|
{
|
|
{
|
|
|
-
|
|
|
|
|
- float sampleX = (x - halfWidth) / scale * frequency + octaveOffsets[i].x;
|
|
|
|
|
- float sampleY = (y - halfHeight) / scale * frequency + octaveOffsets[i].y;
|
|
|
|
|
|
|
+ float sampleX = (x - halfWidth + octaveOffsets[i].x) / scale * frequency;
|
|
|
|
|
+ float sampleY = (y - halfHeight + octaveOffsets[i].y) / scale * frequency;
|
|
|
|
|
|
|
|
float perlinValue = Mathf.PerlinNoise(sampleX, sampleY) * 2 - 1;
|
|
float perlinValue = Mathf.PerlinNoise(sampleX, sampleY) * 2 - 1;
|
|
|
noiseHeight += perlinValue * amplitude;
|
|
noiseHeight += perlinValue * amplitude;
|
|
@@ -47,15 +60,15 @@ public static class Noise
|
|
|
frequency *= lacunarity;
|
|
frequency *= lacunarity;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- if (noiseHeight > maxNoiseHeight)
|
|
|
|
|
|
|
+ if (noiseHeight > maxLocalNoiseHeight)
|
|
|
{
|
|
{
|
|
|
- maxNoiseHeight = noiseHeight;
|
|
|
|
|
|
|
+ maxLocalNoiseHeight = noiseHeight;
|
|
|
}
|
|
}
|
|
|
- else if(noiseHeight < minNoiseHeight)
|
|
|
|
|
|
|
+ else if (noiseHeight < minLocalNoiseHeight)
|
|
|
{
|
|
{
|
|
|
- minNoiseHeight = noiseHeight;
|
|
|
|
|
|
|
+ minLocalNoiseHeight = noiseHeight;
|
|
|
}
|
|
}
|
|
|
- noiseMap[y, x] = noiseHeight;
|
|
|
|
|
|
|
+ noiseMap[x, y] = noiseHeight;
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -63,10 +76,19 @@ public static class Noise
|
|
|
{
|
|
{
|
|
|
for (int x = 0; x < mapWidth; x++)
|
|
for (int x = 0; x < mapWidth; x++)
|
|
|
{
|
|
{
|
|
|
- noiseMap[x, y] = Mathf.InverseLerp(minNoiseHeight, maxNoiseHeight, noiseMap[x, y]);
|
|
|
|
|
|
|
+ if (normalizeMode == NormalizeMode.Local)
|
|
|
|
|
+ {
|
|
|
|
|
+ noiseMap[x, y] = Mathf.InverseLerp(minLocalNoiseHeight, maxLocalNoiseHeight, noiseMap[x, y]);
|
|
|
|
|
+ }
|
|
|
|
|
+ else
|
|
|
|
|
+ {
|
|
|
|
|
+ float normalizedHeight = (noiseMap[x, y] + 1) / (maxPossibleHeight / 0.9f);
|
|
|
|
|
+ noiseMap[x, y] = Mathf.Clamp(normalizedHeight, 0, int.MaxValue);
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
return noiseMap;
|
|
return noiseMap;
|
|
|
}
|
|
}
|
|
|
-}
|
|
|
|
|
|
|
+
|
|
|
|
|
+}
|