Skip to content

Commit 8333ca6

Browse files
author
Cory Leach
committed
Adding new noise options to HeightMapLayerGenerator
1 parent 36db891 commit 8333ca6

File tree

2 files changed

+64
-32
lines changed

2 files changed

+64
-32
lines changed

Runtime/Utility/Noise.cs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,18 @@ public static class Noise
5252
return map;
5353
}
5454

55-
private static float FalloffCurve(float value, float a = 3, float b = 2.2f)
55+
public static float GenerateFalloffPoint(int x, int y, int width, int height, float a = 3f, float b = 2.2f, Vector2 offset = default)
56+
{
57+
x += Mathf.RoundToInt(width * offset.x);
58+
y += Mathf.RoundToInt(height * offset.y);
59+
60+
var valueY = (y + 0.5f) / (float) height * 2 - 1;
61+
var valueX = (x + 0.5f) / (float) width * 2 - 1;
62+
var value = Mathf.Max(Mathf.Abs(valueX), Mathf.Abs(valueY));
63+
return 1 - FalloffCurve(value, a, b);
64+
}
65+
66+
public static float FalloffCurve(float value, float a = 3, float b = 2.2f)
5667
{
5768
return Mathf.Pow(value, a) / (Mathf.Pow(value, a) + Mathf.Pow(b - b * value, a));
5869
}

Runtime/WorldMap/Layers/HeightMapLayerGenerator.cs

Lines changed: 52 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,19 @@ public class HeightMapLayerGenerator : WorldMapLayerGenerator
99
{
1010
public enum MapType
1111
{
12-
Noise,
12+
ValueNoise,
13+
SimplexNoise,
14+
PerlinNoise,
1315
Falloff,
14-
NoiseWithFalloff
16+
SimplexNoiseWithFalloff,
17+
PerlinNoiseWithFalloff,
18+
ValueNoiseWithFalloff
1519
}
1620

1721
[SerializeField] private bool step = false;
1822

1923
[SerializeField] private int stepCount = 10;
2024

21-
[SerializeField] private Vector2 offset = Vector2.zero;
22-
2325
[FormerlySerializedAs("noiseScale")] [SerializeField] private float frequency = 1;
2426

2527
[SerializeField] private int octaves = 4;
@@ -32,42 +34,61 @@ public enum MapType
3234

3335
[SerializeField] private float falloffB = 2.2f;
3436

35-
[SerializeField] private MapType mapType = MapType.Noise;
37+
[SerializeField] private Vector2 falloffOffset = Vector2.zero;
38+
39+
[SerializeField] private MapType mapType = MapType.SimplexNoise;
3640

3741
public HeightMapLayerData Generate(int width, int height, uint seed)
3842
{
39-
float[,] noiseMap;
40-
41-
switch (mapType)
43+
var heightMap = new float[width*height];
44+
for (var y = 0; y < height; y++)
4245
{
43-
case MapType.Noise:
44-
noiseMap = Noise.GenerateNoiseMap(width, height, seed, offset, frequency, octaves,
45-
persistence, lacunarity);
46-
break;
47-
case MapType.Falloff:
48-
noiseMap = Noise.GenerateFalloffMap(width, height, falloffA, falloffB);
49-
break;
50-
case MapType.NoiseWithFalloff:
51-
var falloffMap = Noise.GenerateFalloffMap(width, height, falloffA, falloffB);
52-
noiseMap = Noise.GenerateNoiseMap(width, height, seed, offset, frequency, octaves, persistence, lacunarity, falloffMap);
53-
break;
54-
default:
55-
throw new ArgumentOutOfRangeException();
56-
}
57-
58-
float[] heightMap = new float[width*height];
59-
for (int y = 0; y < height; y++)
60-
{
61-
for (int x = 0; x < width; x++)
46+
for (var x = 0; x < width; x++)
6247
{
63-
var val = noiseMap[x, y];
48+
var i = y * width + x;
49+
switch (mapType)
50+
{
51+
case MapType.ValueNoise:
52+
heightMap[i] = ValueNoise.Fractal2D(x, y, (uint) seed, frequency, octaves, lacunarity, persistence);
53+
break;
54+
case MapType.PerlinNoise:
55+
heightMap[i] = PerlinGradientNoise.Fractal2D(x, y, (uint) seed, frequency, octaves, lacunarity, persistence);
56+
break;
57+
case MapType.SimplexNoise:
58+
heightMap[i] = SimplexGradientNoise.FractalGradient2D(x, y, (uint) seed, frequency, octaves, lacunarity, persistence).value;
59+
break;
60+
case MapType.Falloff:
61+
heightMap[i] = Noise.GenerateFalloffPoint(x , y , width, height, falloffA, falloffB, falloffOffset);
62+
break;
63+
case MapType.SimplexNoiseWithFalloff:
64+
{
65+
var falloff = Noise.GenerateFalloffPoint(x , y, width, height, falloffA, falloffB, falloffOffset);
66+
heightMap[i] = SimplexGradientNoise.FractalGradient2D(x, y, (uint) seed, frequency, octaves, lacunarity, persistence).value;
67+
heightMap[i] *= falloff;
68+
}
69+
break;
70+
case MapType.PerlinNoiseWithFalloff:
71+
{
72+
var falloff = Noise.GenerateFalloffPoint(x, y, width, height, falloffA, falloffB, falloffOffset);
73+
heightMap[i] = PerlinGradientNoise.Fractal2D(x, y, (uint) seed, frequency, octaves, lacunarity, persistence);
74+
heightMap[i] *= falloff;
75+
}
76+
break;
77+
case MapType.ValueNoiseWithFalloff:
78+
{
79+
var falloff = Noise.GenerateFalloffPoint(x, y, width, height, falloffA, falloffB, falloffOffset);
80+
heightMap[i] = ValueNoise.Fractal2D(x, y, (uint) seed, frequency, octaves, lacunarity, persistence);
81+
heightMap[i] *= falloff;
82+
}
83+
break;
84+
default:
85+
throw new ArgumentOutOfRangeException();
86+
}
6487

6588
if (step)
6689
{
67-
val = Mathf.RoundToInt(val * stepCount) / (float)stepCount;
90+
heightMap[i] = Mathf.RoundToInt(heightMap[i] * stepCount) / (float)stepCount;
6891
}
69-
70-
heightMap[y * width + x] = val;
7192
}
7293
}
7394

0 commit comments

Comments
 (0)