-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRidge.cs
More file actions
75 lines (60 loc) · 1.93 KB
/
Copy pathRidge.cs
File metadata and controls
75 lines (60 loc) · 1.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
namespace RedOwl;
public static partial class Noise
{
public class Ridge : FractalBase
{
private float _exponent;
private float[] _spectralWeights = [];
public float Offset { get; set; }
public float Gain { get; set; }
public float Exponent
{
get => _exponent;
set
{
_exponent = value;
OnParamsChanged();
}
}
public Ridge(int seed) : base(seed)
{
Offset = 1f;
Gain = 2f;
Exponent = 1f;
}
public Ridge(Generator source) : base(source)
{
Offset = 1f;
Gain = 2f;
Exponent = 1f;
}
protected override float InitializeState() => 1f;
protected override float CombineOctave(int octave, float signal, float value, ref float state)
{
if (octave == 0) state = 1f;
// Ridge noise: Offset - abs(signal), then squared
signal = Offset - MathF.Abs(signal);
signal *= signal;
// Weight by current state (weight from previous octave)
signal *= state;
// Accumulate into value with spectral weight
value += signal * _spectralWeights[octave];
// Update state (weight) for next octave: clamp(signal * Gain, 0, 1)
state = signal * Gain;
if (state > 1f) state = 1f;
if (state < 0f) state = 0f;
return value;
}
protected override void OnParamsChanged() => PrecalculateWeights();
private void PrecalculateWeights()
{
var freq = 1f;
_spectralWeights = new float[OctaveCount];
for (var index = 0; index < OctaveCount; ++index)
{
_spectralWeights[index] = MathF.Pow(freq, -Exponent);
freq *= Lacunarity;
}
}
}
}