Skip to content

Commit 863db6b

Browse files
author
Cory Leach
committed
Moved fractal methods into Utility class to reduce code duplication.
1 parent 68c9f5b commit 863db6b

File tree

7 files changed

+211
-249
lines changed

7 files changed

+211
-249
lines changed

Runtime/Utility/FractalUtility.cs

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
using System;
2+
3+
namespace Gameframe.Procgen
4+
{
5+
public static class FractalUtility
6+
{
7+
public static NoiseSample Sample1D(Func<float, uint, float, NoiseSample> action, float x, uint seed, float frequency, int octaves, float lacunarity = 2f, float persistence = 0.5f)
8+
{
9+
var sum = action(x, seed, frequency);
10+
var amplitude = 1f;
11+
var range = 1f;
12+
for (var i = 1; i < octaves; i++)
13+
{
14+
frequency *= lacunarity;
15+
amplitude *= persistence;
16+
range += amplitude;
17+
sum += action(x, seed, frequency) * amplitude;
18+
}
19+
return sum * (1 / range);
20+
}
21+
22+
public static float Float1D(Func<float, uint, float, float> action, float x, uint seed, float frequency, int octaves, float lacunarity = 2f, float persistence = 0.5f)
23+
{
24+
var sum = action(x, seed, frequency);
25+
var amplitude = 1f;
26+
var range = 1f;
27+
for (var i = 1; i < octaves; i++)
28+
{
29+
frequency *= lacunarity;
30+
amplitude *= persistence;
31+
range += amplitude;
32+
sum += action(x, seed, frequency) * amplitude;
33+
}
34+
return sum * (1 / range);
35+
}
36+
37+
public static NoiseSample Sample2D(Func<float, float, uint, float, NoiseSample> action, float x, float y, uint seed, float frequency, int octaves, float lacunarity = 2f, float persistence = 0.5f)
38+
{
39+
var sum = action(x, y, seed, frequency);
40+
var amplitude = 1f;
41+
var range = 1f;
42+
for (var i = 1; i < octaves; i++)
43+
{
44+
frequency *= lacunarity;
45+
amplitude *= persistence;
46+
range += amplitude;
47+
sum += action(x, y, seed, frequency) * amplitude;
48+
}
49+
return sum * (1 / range);
50+
}
51+
52+
public static float Float2D(Func<float, float, uint, float, float> action, float x, float y, uint seed, float frequency, int octaves, float lacunarity = 2f, float persistence = 0.5f)
53+
{
54+
var sum = action(x, y, seed, frequency);
55+
var amplitude = 1f;
56+
var range = 1f;
57+
for (var i = 1; i < octaves; i++)
58+
{
59+
frequency *= lacunarity;
60+
amplitude *= persistence;
61+
range += amplitude;
62+
sum += action(x, y, seed, frequency) * amplitude;
63+
}
64+
return sum * (1 / range);
65+
}
66+
67+
public static NoiseSample Sample3D(Func<float, float, float, uint, float, NoiseSample> action, float x, float y, float z, uint seed, float frequency, int octaves, float lacunarity = 2f, float persistence = 0.5f)
68+
{
69+
var sum = action(x, y, z, seed, frequency);
70+
var amplitude = 1f;
71+
var range = 1f;
72+
for (var i = 1; i < octaves; i++)
73+
{
74+
frequency *= lacunarity;
75+
amplitude *= persistence;
76+
range += amplitude;
77+
sum += action(x, y, z, seed, frequency) * amplitude;
78+
}
79+
return sum * (1 / range);
80+
}
81+
82+
public static float Float3D(Func<float, float, float, uint, float, float> action, float x, float y, float z, uint seed, float frequency, int octaves, float lacunarity = 2f, float persistence = 0.5f)
83+
{
84+
var sum = action(x, y, z, seed, frequency);
85+
var amplitude = 1f;
86+
var range = 1f;
87+
for (var i = 1; i < octaves; i++)
88+
{
89+
frequency *= lacunarity;
90+
amplitude *= persistence;
91+
range += amplitude;
92+
sum += action(x, y, z, seed, frequency) * amplitude;
93+
}
94+
return sum * (1 / range);
95+
}
96+
}
97+
}

Runtime/Utility/FractalUtility.cs.meta

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Runtime/Utility/PerlinGradientNoise.cs

Lines changed: 32 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -23,21 +23,9 @@ public static class PerlinGradientNoise
2323
/// <param name="lacunarity">frequency is multiplied by this number ever octave</param>
2424
/// <param name="persistence">measure of how much each octave will contribute. Values gt 1 make each octave larger. Values gt 0 but lt 1 make each successive octave contribution smaller.</param>
2525
/// <returns>Random value between 0 and 1</returns>
26-
public static float Fractal1D(float x, uint seed, float frequency, int octaves, float lacunarity = 2f,
27-
float persistence = 0.5f)
26+
public static float Fractal1D(float x, uint seed, float frequency, int octaves, float lacunarity = 2f, float persistence = 0.5f)
2827
{
29-
var sum = Noise1D(x * frequency, seed);
30-
var amplitude = 1f;
31-
var range = 1f;
32-
for (var i = 1; i < octaves; i++)
33-
{
34-
frequency *= lacunarity;
35-
amplitude *= persistence;
36-
range += amplitude;
37-
sum += Noise1D(x * frequency, seed) * amplitude;
38-
}
39-
40-
return sum / range;
28+
return FractalUtility.Float1D(Noise1D, x, seed, frequency, octaves, lacunarity, persistence);
4129
}
4230

4331
/// <summary>
@@ -50,22 +38,9 @@ public static float Fractal1D(float x, uint seed, float frequency, int octaves,
5038
/// <param name="lacunarity">frequency is multiplied by this number ever octave</param>
5139
/// <param name="persistence">measure of how much each octave will contribute. Values gt 1 make each octave larger. Values gt 0 but lt 1 make each successive octave contribution smaller.</param>
5240
/// <returns>Noise sample which includes value in range 0 to 1 and derivative of noise function at the sampled location</returns>
53-
public static NoiseSample FractalSample1D(float x, uint seed, float frequency, int octaves,
54-
float lacunarity = 2f,
55-
float persistence = 0.5f)
41+
public static NoiseSample FractalSample1D(float x, uint seed, float frequency, int octaves, float lacunarity = 2f, float persistence = 0.5f)
5642
{
57-
var sum = Sample1D(x, seed, frequency);
58-
var amplitude = 1f;
59-
var range = 1f;
60-
for (var i = 1; i < octaves; i++)
61-
{
62-
frequency *= lacunarity;
63-
amplitude *= persistence;
64-
range += amplitude;
65-
sum += Sample1D(x, seed, frequency) * amplitude;
66-
}
67-
68-
return sum * (1 / range);
43+
return FractalUtility.Sample1D(Sample1D, x, seed, frequency, octaves, lacunarity, persistence);
6944
}
7045

7146
/// <summary>
@@ -79,21 +54,9 @@ public static NoiseSample FractalSample1D(float x, uint seed, float frequency, i
7954
/// <param name="lacunarity">frequency is multiplied by this number ever octave</param>
8055
/// <param name="persistence">measure of how much each octave will contribute. Values gt 1 make each octave larger. Values gt 0 but lt 1 make each successive octave contribution smaller.</param>
8156
/// <returns>Random value between 0 and 1</returns>
82-
public static float Fractal2D(float x, float y, uint seed, float frequency, int octaves,
83-
float lacunarity = 2f, float persistence = 0.5f)
57+
public static float Fractal2D(float x, float y, uint seed, float frequency, int octaves, float lacunarity = 2f, float persistence = 0.5f)
8458
{
85-
var sum = Noise2D(x * frequency, y * frequency, seed);
86-
var amplitude = 1f;
87-
var range = 1f;
88-
for (var i = 1; i < octaves; i++)
89-
{
90-
frequency *= lacunarity;
91-
amplitude *= persistence;
92-
range += amplitude;
93-
sum += Noise2D(x * frequency, y * frequency, seed) * amplitude;
94-
}
95-
96-
return sum / range;
59+
return FractalUtility.Float2D(Noise2D, x, y, seed, frequency, octaves, lacunarity, persistence);
9760
}
9861

9962
/// <summary>
@@ -106,8 +69,7 @@ public static float Fractal2D(float x, float y, uint seed, float frequency, int
10669
/// <param name="lacunarity">frequency is multiplied by this number ever octave</param>
10770
/// <param name="persistence">measure of how much each octave will contribute. Values gt 1 make each octave larger. Values gt 0 but lt 1 make each successive octave contribution smaller.</param>
10871
/// <returns>Noise sample which includes value in range 0 to 1 and derivative of noise function at the sampled location</returns>
109-
public static NoiseSample FractalSample2D(Vector2 point, uint seed, float frequency, int octaves,
110-
float lacunarity = 2f, float persistence = 0.5f)
72+
public static NoiseSample FractalSample2D(Vector2 point, uint seed, float frequency, int octaves, float lacunarity = 2f, float persistence = 0.5f)
11173
{
11274
return FractalSample2D(point.x, point.y, seed, frequency, octaves, lacunarity, persistence);
11375
}
@@ -123,21 +85,9 @@ public static NoiseSample FractalSample2D(Vector2 point, uint seed, float freque
12385
/// <param name="lacunarity">frequency is multiplied by this number ever octave</param>
12486
/// <param name="persistence">measure of how much each octave will contribute. Values gt 1 make each octave larger. Values gt 0 but lt 1 make each successive octave contribution smaller.</param>
12587
/// <returns>Noise sample which includes value in range 0 to 1 and derivative of noise function at the sampled location</returns>
126-
public static NoiseSample FractalSample2D(float x, float y, uint seed, float frequency, int octaves,
127-
float lacunarity = 2f, float persistence = 0.5f)
88+
public static NoiseSample FractalSample2D(float x, float y, uint seed, float frequency, int octaves, float lacunarity = 2f, float persistence = 0.5f)
12889
{
129-
var sum = Sample2D(x, y, seed, frequency);
130-
var amplitude = 1f;
131-
var range = 1f;
132-
for (var i = 1; i < octaves; i++)
133-
{
134-
frequency *= lacunarity;
135-
amplitude *= persistence;
136-
range += amplitude;
137-
sum += Sample2D(x, y, seed, frequency) * amplitude;
138-
}
139-
140-
return sum * (1 / range);
90+
return FractalUtility.Sample2D(Sample2D, x, y, seed, frequency, octaves, lacunarity, persistence);
14191
}
14292

14393
/// <summary>
@@ -150,8 +100,7 @@ public static NoiseSample FractalSample2D(float x, float y, uint seed, float fre
150100
/// <param name="lacunarity">frequency is multiplied by this number ever octave</param>
151101
/// <param name="persistence">measure of how much each octave will contribute. Values gt 1 make each octave larger. Values gt 0 but lt 1 make each successive octave contribution smaller.</param>
152102
/// <returns>Random value between 0 and 1</returns>
153-
public static float Fractal2D(Vector2 position, uint seed, float frequency, int octaves, float lacunarity = 2,
154-
float persistence = 0.5f)
103+
public static float Fractal2D(Vector2 position, uint seed, float frequency, int octaves, float lacunarity = 2, float persistence = 0.5f)
155104
{
156105
return Fractal2D(position.x, position.y, seed, frequency, octaves, lacunarity, persistence);
157106
}
@@ -168,21 +117,9 @@ public static float Fractal2D(Vector2 position, uint seed, float frequency, int
168117
/// <param name="lacunarity">frequency is multiplied by this number ever octave</param>
169118
/// <param name="persistence">measure of how much each octave will contribute. Values gt 1 make each octave larger. Values gt 0 but lt 1 make each successive octave contribution smaller.</param>
170119
/// <returns>Random value between 0 and 1</returns>
171-
public static float Fractal3D(float x, float y, float z, uint seed, float frequency, int octaves,
172-
float lacunarity = 2f, float persistence = 0.5f)
120+
public static float Fractal3D(float x, float y, float z, uint seed, float frequency, int octaves, float lacunarity = 2f, float persistence = 0.5f)
173121
{
174-
var sum = Noise3D(x * frequency, y * frequency, z * frequency, seed);
175-
var amplitude = 1f;
176-
var range = 1f;
177-
for (var i = 1; i < octaves; i++)
178-
{
179-
frequency *= lacunarity;
180-
amplitude *= persistence;
181-
range += amplitude;
182-
sum += Noise3D(x * frequency, y * frequency, z * frequency, seed) * amplitude;
183-
}
184-
185-
return sum / range;
122+
return FractalUtility.Float3D(Noise3D, x, y, z, seed, frequency, octaves, lacunarity, persistence);
186123
}
187124

188125
/// <summary>
@@ -195,8 +132,7 @@ public static float Fractal3D(float x, float y, float z, uint seed, float freque
195132
/// <param name="lacunarity">frequency is multiplied by this number ever octave</param>
196133
/// <param name="persistence">measure of how much each octave will contribute. Values gt 1 make each octave larger. Values gt 0 but lt 1 make each successive octave contribution smaller.</param>
197134
/// <returns>Noise sample which includes value in range 0 to 1 and derivative of noise function at the sampled location</returns>
198-
public static NoiseSample FractalSample3D(Vector3 point, uint seed, float frequency, int octaves,
199-
float lacunarity = 2f, float persistence = 0.5f)
135+
public static NoiseSample FractalSample3D(Vector3 point, uint seed, float frequency, int octaves, float lacunarity = 2f, float persistence = 0.5f)
200136
{
201137
return FractalSample3D(point.x, point.y, point.z, seed, frequency, octaves, lacunarity, persistence);
202138
}
@@ -213,21 +149,9 @@ public static NoiseSample FractalSample3D(Vector3 point, uint seed, float freque
213149
/// <param name="lacunarity">frequency is multiplied by this number ever octave</param>
214150
/// <param name="persistence">measure of how much each octave will contribute. Values gt 1 make each octave larger. Values gt 0 but lt 1 make each successive octave contribution smaller.</param>
215151
/// <returns>Noise sample which includes value in range 0 to 1 and derivative of noise function at the sampled location</returns>
216-
public static NoiseSample FractalSample3D(float x, float y, float z, uint seed, float frequency, int octaves,
217-
float lacunarity = 2f, float persistence = 0.5f)
152+
public static NoiseSample FractalSample3D(float x, float y, float z, uint seed, float frequency, int octaves, float lacunarity = 2f, float persistence = 0.5f)
218153
{
219-
var sum = Sample3D(x, y, z, seed, frequency);
220-
var amplitude = 1f;
221-
var range = 1f;
222-
for (var i = 1; i < octaves; i++)
223-
{
224-
frequency *= lacunarity;
225-
amplitude *= persistence;
226-
range += amplitude;
227-
sum += Sample3D(x, y, z, seed, frequency) * amplitude;
228-
}
229-
230-
return sum * (1f / range);
154+
return FractalUtility.Sample3D(Sample3D, x, y, z, seed, frequency, octaves, lacunarity, persistence);
231155
}
232156

233157
/// <summary>
@@ -240,8 +164,7 @@ public static NoiseSample FractalSample3D(float x, float y, float z, uint seed,
240164
/// <param name="lacunarity">frequency is multiplied by this number ever octave</param>
241165
/// <param name="persistence">measure of how much each octave will contribute. Values gt 1 make each octave larger. Values gt 0 but lt 1 make each successive octave contribution smaller.</param>
242166
/// <returns>Random value between 0 and 1</returns>
243-
public static float Fractal3D(Vector3 position, uint seed, float frequency, int octaves, float lacunarity = 2,
244-
float persistence = 0.5f)
167+
public static float Fractal3D(Vector3 position, uint seed, float frequency, int octaves, float lacunarity = 2, float persistence = 0.5f)
245168
{
246169
return Fractal3D(position.x, position.y, position.z, seed, frequency, octaves, lacunarity, persistence);
247170
}
@@ -256,8 +179,10 @@ public static float Fractal3D(Vector3 position, uint seed, float frequency, int
256179
/// <param name="x">x position</param>
257180
/// <param name="seed">rng seed</param>
258181
/// <returns>value between 0 and 1 (inclusive)</returns>
259-
public static float Noise1D(float x, uint seed)
182+
public static float Noise1D(float x, uint seed, float frequency = 1f)
260183
{
184+
x *= frequency;
185+
261186
var x0 = Mathf.FloorToInt(x);
262187
var x1 = x0 + 1;
263188

@@ -330,8 +255,11 @@ public static NoiseSample Sample1D(float x, uint seed, float frequency = 1)
330255
/// <param name="y">y position</param>
331256
/// <param name="seed">rng seed</param>
332257
/// <returns>value between 0 and 1 (inclusive)</returns>
333-
public static float Noise2D(float x, float y, uint seed)
258+
public static float Noise2D(float x, float y, uint seed, float frequency = 1f)
334259
{
260+
x *= frequency;
261+
y *= frequency;
262+
335263
var x0 = Mathf.FloorToInt(x);
336264
var x1 = x0 + 1;
337265

@@ -449,9 +377,9 @@ public static NoiseSample Sample2D(float x, float y, uint seed, float frequency
449377
/// <param name="position">Vector2 position</param>
450378
/// <param name="seed">rng seed</param>
451379
/// <returns>value between 0 and 1 (inclusive)</returns>
452-
public static float Noise2D(Vector2 position, uint seed)
380+
public static float Noise2D(Vector2 position, uint seed, float frequency = 1f)
453381
{
454-
return Noise2D(position.x, position.y, seed);
382+
return Noise2D(position.x, position.y, seed, frequency);
455383
}
456384

457385
/// <summary>
@@ -462,8 +390,12 @@ public static float Noise2D(Vector2 position, uint seed)
462390
/// <param name="z">z position</param>
463391
/// <param name="seed">rng seed</param>
464392
/// <returns>value between 0 and 1 (inclusive)</returns>
465-
public static float Noise3D(float x, float y, float z, uint seed)
393+
public static float Noise3D(float x, float y, float z, uint seed, float frequency = 1f)
466394
{
395+
x *= frequency;
396+
y *= frequency;
397+
z *= frequency;
398+
467399
var x0 = Mathf.FloorToInt(x);
468400
var x1 = x0 + 1;
469401

@@ -625,9 +557,9 @@ public static NoiseSample Sample3D(float x, float y, float z, uint seed, float f
625557
/// <param name="position">Vector3 position</param>
626558
/// <param name="seed">rng seed</param>
627559
/// <returns>value between 0 and 1 (inclusive)</returns>
628-
public static float Noise3D(Vector3 position, uint seed)
560+
public static float Noise3D(Vector3 position, uint seed, float frequency = 1f)
629561
{
630-
return Noise3D(position.x, position.y, position.z, seed);
562+
return Noise3D(position.x, position.y, position.z, seed, frequency);
631563
}
632564

633565
#endregion

0 commit comments

Comments
 (0)