-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathnoise.cpp
More file actions
101 lines (83 loc) · 2.62 KB
/
noise.cpp
File metadata and controls
101 lines (83 loc) · 2.62 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#include <fea/util/noise.hpp>
#include <fea/assert.hpp>
namespace fea
{
Noise::Noise()
{
setSeed(0);
}
Noise::Noise(uint32_t seed)
{
setSeed(seed);
}
void Noise::setSeed(uint32_t seed)
{
mRandomEngine.seed(seed);
for(uint16_t i = 0; i < 256; i++)
{
mPerm[i] = i;
}
std::shuffle(mPerm.begin(), mPerm.begin() + 256, mRandomEngine);
std::copy(mPerm.begin(), mPerm.begin() + 256, mPerm.begin() + 256);
}
float Noise::white2D(float x, float y) const
{
return mWhiteNoise.get2d(x, y, mPerm.data());
}
float Noise::simplex2D(float x, float y) const
{
return raw_noise_2d(x, y, mPerm.data());
}
float Noise::simplex3D(float x, float y, float z) const
{
return raw_noise_3d(x, y, z, mPerm.data());
}
// 2D Multi-octave Simplex noise.
//
// For each octave, a higher frequency/lower amplitude function will be added to the original.
// The higher the persistence [0-1], the more of each succeeding octave will be added.
float Noise::simplexOctave2D(float x, float y, float scaleFactor, uint32_t octaves, float persistence) const
{
FEA_ASSERT(octaves > 0, "Amount of octaves must be greater than zero!");
float total = 0.0f;
float frequency = scaleFactor;
float amplitude = 1.0f;
// We have to keep track of the largest possible amplitude,
// because each octave adds more, and we need a value in [-1, 1].
float maxAmplitude = 0.0f;
for(uint32_t i = 0; i < octaves; i++)
{
total += simplex2D(x * frequency + 3000.f * i, y * frequency + 1500.0f * i) * amplitude;
frequency *= 2.0f;
maxAmplitude += amplitude;
amplitude *= persistence;
}
return total / maxAmplitude;
}
// 3D Multi-octave Simplex noise.
//
// For each octave, a higher frequency/lower amplitude function will be added to the original.
// The higher the persistence [0-1], the more of each succeeding octave will be added.
float Noise::simplexOctave3D(float x, float y, float z, float scaleFactor, uint32_t octaves, float persistence) const
{
FEA_ASSERT(octaves > 0, "Amount of octaves must be greater than zero!");
float total = 0.0f;
float frequency = scaleFactor;
float amplitude = 1.0f;
// We have to keep track of the largest possible amplitude,
// because each octave adds more, and we need a value in [-1, 1].
float maxAmplitude = 0.0f;
for(uint32_t i = 0; i < octaves; i++)
{
total += simplex3D(x * frequency, y * frequency, z * frequency) * amplitude;
frequency *= 2.0f;
maxAmplitude += amplitude;
amplitude *= persistence;
}
return total / maxAmplitude;
}
float Noise::voronoi2D(float x, float y) const
{
return mVoronoiNoise.get2d(x, y, mPerm.data());
}
}