Skip to content

Commit 95e7dab

Browse files
author
Cory Leach
committed
Modified HightMapLayerGenerator to use Simplex 2D fractal noise.
1 parent 863db6b commit 95e7dab

File tree

5 files changed

+25
-79
lines changed

5 files changed

+25
-79
lines changed

Runtime/Utility/Noise.cs

Lines changed: 3 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -16,75 +16,17 @@ public static class Noise
1616
/// <param name="persistence"></param>
1717
/// <param name="lacunarity"></param>
1818
/// <returns></returns>
19-
public static float[,] GenerateNoiseMap(int mapWidth, int mapHeight, int seed, Vector2 offset, float scale, int octaves = 4, float persistence = 0.5f, float lacunarity = 2f, float[,] falloffMap = null)
19+
public static float[,] GenerateNoiseMap(int mapWidth, int mapHeight, uint seed, Vector2 offset, float frequency, int octaves = 4, float persistence = 0.5f, float lacunarity = 2f, float[,] falloffMap = null)
2020
{
2121
var noiseMap = new float[mapWidth, mapHeight];
2222

2323
//Generate some random offsets based on the seed
24-
var rng = new System.Random(seed);
25-
var octaveOffsets = new Vector2[octaves];
26-
for (int i = 0; i < octaves; i++)
27-
{
28-
octaveOffsets[i] = new Vector2
29-
{
30-
x = rng.Next(-100000, 100000) + offset.x,
31-
y = rng.Next(-100000, 100000) + offset.y
32-
};
33-
}
34-
35-
scale = Mathf.Max(scale, 0.0000001f);
36-
37-
var maxNoiseHeight = float.MinValue;
38-
var minNoiseHeight = float.MaxValue;
39-
40-
var halfWidth = mapWidth * 0.5f;
41-
var halfHeight = mapHeight * 0.5f;
42-
43-
for (int y = 0; y < mapHeight; y++)
44-
{
45-
for (int x = 0; x < mapWidth; x++)
46-
{
47-
float amplitude = 1;
48-
float frequency = 1;
49-
float noiseHeight = 0;
50-
51-
for (int i = 0; i < octaves; i++)
52-
{
53-
var sampleX = (x-halfWidth) / scale * frequency + octaveOffsets[i].x;
54-
var sampleY = (y-halfHeight) / scale * frequency + octaveOffsets[i].y;
55-
56-
//Multiply by 2 and subtract one to create values in the range range -1 to 1
57-
//We can come back and normalize values later
58-
var perlinValue = Mathf.PerlinNoise(sampleX, sampleY) * 2 - 1;
59-
noiseHeight += perlinValue * amplitude;
60-
amplitude *= persistence;
61-
frequency *= lacunarity;
62-
}
63-
64-
//Track min and max values to normalize values later
65-
if (noiseHeight > maxNoiseHeight)
66-
{
67-
maxNoiseHeight = noiseHeight;
68-
}
69-
else if (noiseHeight < minNoiseHeight)
70-
{
71-
minNoiseHeight = noiseHeight;
72-
}
73-
74-
noiseMap[x, y] = noiseHeight;
75-
}
76-
}
77-
78-
//Normalize Noise Map so all values are in range 0 to 1
7924
for (int y = 0; y < mapHeight; y++)
8025
{
8126
for (int x = 0; x < mapWidth; x++)
8227
{
83-
noiseMap[x, y] = Mathf.InverseLerp(minNoiseHeight, maxNoiseHeight, noiseMap[x, y]);
84-
if (falloffMap != null)
85-
{
86-
noiseMap[x, y] = Mathf.Clamp01(noiseMap[x, y] - falloffMap[x,y]);
87-
}
28+
var sample = SimplexGradientNoise.FractalGradient2D(x + offset.x, y + offset.y, seed, frequency, octaves, lacunarity, persistence);
29+
noiseMap[x, y] = sample.value * (1f - falloffMap[x, y]);
8830
}
8931
}
9032

Runtime/Visualizers/NoiseVisualizer2D.cs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@ public class NoiseVisualizer2D : MonoBehaviour
2121

2222
private Texture2D _texture;
2323

24-
private float minValue = float.MaxValue;
25-
private float maxValue = float.MinValue;
2624

2725
[ContextMenu("ResetMinMax")]
2826
public void ResetMinMax()
@@ -53,6 +51,12 @@ public enum Dimension
5351

5452
[SerializeField] private Vector3 offset;
5553

54+
[SerializeField]
55+
private float minValue = float.MaxValue;
56+
[SerializeField]
57+
private float maxValue = float.MinValue;
58+
59+
5660
private void OnEnable()
5761
{
5862
Generate();
@@ -128,8 +132,7 @@ private void Generate()
128132
persistence);
129133
break;
130134
case Dimension.Perlin3D:
131-
v = PerlinGradientNoise.Fractal3D(point.x, point.y, point.z, seed, frequency, octaves,
132-
lacunarity, persistence);
135+
v = PerlinGradientNoise.Fractal3D(point.x, point.y, point.z, seed, frequency, octaves, lacunarity, persistence);
133136
break;
134137
case Dimension.SamplePerlin2D:
135138
v = PerlinGradientNoise.FractalSample2D(point.x, point.y, seed, frequency, octaves, lacunarity, persistence).value;

Runtime/Visualizers/SurfaceMeshVisualizer.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,10 @@ private void OnDrawGizmosSelected()
101101
}
102102
}
103103

104-
float minSample = float.MaxValue;
105-
float maxSample = float.MinValue;
104+
[SerializeField]
105+
private float minSample = float.MaxValue;
106+
[SerializeField]
107+
private float maxSample = float.MinValue;
106108

107109
[ContextMenu("ResetMinMax")]
108110
public void ResetMinMax()
@@ -143,7 +145,7 @@ private void Refresh()
143145
}
144146
}
145147

146-
Debug.Log($"Min: {minSample} Max:{maxSample}");
148+
//Debug.Log($"Min: {minSample} Max:{maxSample}");
147149

148150
mesh.vertices = vertices;
149151
mesh.normals = normals;

Runtime/WorldMap/Layers/HeightMapLayerGenerator.cs

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using UnityEngine;
3+
using UnityEngine.Serialization;
34

45
namespace Gameframe.Procgen
56
{
@@ -16,10 +17,10 @@ public enum MapType
1617
[SerializeField] private bool step = false;
1718

1819
[SerializeField] private int stepCount = 10;
19-
20+
2021
[SerializeField] private Vector2 offset = Vector2.zero;
2122

22-
[SerializeField] private float noiseScale = 1;
23+
[FormerlySerializedAs("noiseScale")] [SerializeField] private float frequency = 1;
2324

2425
[SerializeField] private int octaves = 4;
2526

@@ -33,23 +34,22 @@ public enum MapType
3334

3435
[SerializeField] private MapType mapType = MapType.Noise;
3536

36-
public HeightMapLayerData Generate(int width, int height, int seed)
37+
public HeightMapLayerData Generate(int width, int height, uint seed)
3738
{
3839
float[,] noiseMap;
3940

4041
switch (mapType)
4142
{
4243
case MapType.Noise:
43-
noiseMap = Noise.GenerateNoiseMap(width, height, seed, offset, noiseScale, octaves,
44+
noiseMap = Noise.GenerateNoiseMap(width, height, seed, offset, frequency, octaves,
4445
persistence, lacunarity);
4546
break;
4647
case MapType.Falloff:
4748
noiseMap = Noise.GenerateFalloffMap(width, height, falloffA, falloffB);
4849
break;
4950
case MapType.NoiseWithFalloff:
5051
var falloffMap = Noise.GenerateFalloffMap(width, height, falloffA, falloffB);
51-
noiseMap = Noise.GenerateNoiseMap(width, height, seed, offset, noiseScale, octaves,
52-
persistence, lacunarity, falloffMap);
52+
noiseMap = Noise.GenerateNoiseMap(width, height, seed, offset, frequency, octaves, persistence, lacunarity, falloffMap);
5353
break;
5454
default:
5555
throw new ArgumentOutOfRangeException();
@@ -66,11 +66,11 @@ public HeightMapLayerData Generate(int width, int height, int seed)
6666
{
6767
val = Mathf.RoundToInt(val * stepCount) / (float)stepCount;
6868
}
69-
69+
7070
heightMap[y * width + x] = val;
7171
}
7272
}
73-
73+
7474
return new HeightMapLayerData
7575
{
7676
heightMap = heightMap
@@ -79,7 +79,7 @@ public HeightMapLayerData Generate(int width, int height, int seed)
7979

8080
public override void AddToWorld(WorldMapData worldMapData)
8181
{
82-
worldMapData.layers.Add(Generate(worldMapData.width,worldMapData.height,worldMapData.seed));
82+
worldMapData.layers.Add(Generate(worldMapData.width,worldMapData.height,(uint)worldMapData.seed));
8383
}
8484

8585
private void OnValidate()
@@ -96,4 +96,3 @@ private void OnValidate()
9696
}
9797

9898
}
99-

Runtime/WorldMap/WorldMapData.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,4 @@ public object GetLayer(System.Type type)
4949
return null;
5050
}
5151
}
52-
}
52+
}

0 commit comments

Comments
 (0)