]> git.localhorst.tv Git - blobs.git/blob - src/math/noise.cpp
randomize creature properties a bit
[blobs.git] / src / math / noise.cpp
1 #include "GaloisLFSR.hpp"
2 #include "SimplexNoise.hpp"
3 #include "WorleyNoise.hpp"
4
5 #include <cmath>
6 #include <glm/gtx/norm.hpp>
7
8
9 namespace {
10
11 constexpr float one_third = 1.0f/3.0f;
12 constexpr float one_sixth = 1.0f/6.0f;
13
14 }
15
16 namespace blobs {
17 namespace math {
18
19 SimplexNoise::SimplexNoise(std::uint64_t seed) noexcept
20 : grad({
21         {  1.0f,  1.0f,  0.0f },
22         { -1.0f,  1.0f,  0.0f },
23         {  1.0f, -1.0f,  0.0f },
24         { -1.0f, -1.0f,  0.0f },
25         {  1.0f,  0.0f,  1.0f },
26         { -1.0f,  0.0f,  1.0f },
27         {  1.0f,  0.0f, -1.0f },
28         { -1.0f,  0.0f, -1.0f },
29         {  0.0f,  1.0f,  1.0f },
30         {  0.0f, -1.0f,  1.0f },
31         {  0.0f,  1.0f, -1.0f },
32         {  0.0f, -1.0f, -1.0f },
33 })
34 , second_ints({
35                      // x>y x>z y>z
36         { 0, 0, 1 }, //  0   0   0  ZYX
37         { 0, 1, 0 }, //  0   0   1  YZX
38         { 0, 0, 1 }, //  0   1   0  illogical, but ZYX
39         { 0, 1, 0 }, //  0   1   1  YXZ
40         { 0, 0, 1 }, //  1   0   0  ZXY
41         { 1, 0, 0 }, //  1   0   1  illogical, but XYZ
42         { 1, 0, 0 }, //  1   1   0  XZY
43         { 1, 0, 0 }, //  1   1   1  XYZ
44 })
45 , third_ints({
46                      // x>y x>z y>z
47         { 0, 1, 1 }, //  0   0   0  ZYX
48         { 0, 1, 1 }, //  0   0   1  YZX
49         { 0, 1, 1 }, //  0   1   0  illogical, but ZYX
50         { 1, 1, 0 }, //  0   1   1  YXZ
51         { 1, 0, 1 }, //  1   0   0  ZXY
52         { 1, 1, 0 }, //  1   0   1  illogical, but XYZ
53         { 1, 0, 1 }, //  1   1   0  XZY
54         { 1, 1, 0 }, //  1   1   1  XYZ
55 })
56 , second_floats({
57                               // x>y x>z y>z
58         { 0.0f, 0.0f, 1.0f }, //  0   0   0  ZYX
59         { 0.0f, 1.0f, 0.0f }, //  0   0   1  YZX
60         { 0.0f, 0.0f, 1.0f }, //  0   1   0  illogical, but ZYX
61         { 0.0f, 1.0f, 0.0f }, //  0   1   1  YXZ
62         { 0.0f, 0.0f, 1.0f }, //  1   0   0  ZXY
63         { 1.0f, 0.0f, 0.0f }, //  1   0   1  illogical, but XYZ
64         { 1.0f, 0.0f, 0.0f }, //  1   1   0  XZY
65         { 1.0f, 0.0f, 0.0f }, //  1   1   1  XYZ
66 })
67 , third_floats({
68                               // x>y x>z y>z
69         { 0.0f, 1.0f, 1.0f }, //  0   0   0  ZYX
70         { 0.0f, 1.0f, 1.0f }, //  0   0   1  YZX
71         { 0.0f, 1.0f, 1.0f }, //  0   1   0  illogical, but ZYX
72         { 1.0f, 1.0f, 0.0f }, //  0   1   1  YXZ
73         { 1.0f, 0.0f, 1.0f }, //  1   0   0  ZXY
74         { 1.0f, 1.0f, 0.0f }, //  1   0   1  illogical, but XYZ
75         { 1.0f, 0.0f, 1.0f }, //  1   1   0  XZY
76         { 1.0f, 1.0f, 0.0f }, //  1   1   1  XYZ
77 }) {
78         GaloisLFSR random(seed ^ 0x0123456789ACBDEF);
79         unsigned char value;
80         for (size_t i = 0; i < 256; ++i) {
81                 perm[i] = random(value);
82                 perm[i] &= 0xFF;
83                 perm[i + 256] = perm[i];
84                 perm12[i] = perm[i] % 12;
85                 perm12[i + 256] = perm12[i];
86         }
87 }
88
89
90 float SimplexNoise::operator ()(const glm::vec3 &in) const noexcept {
91         float skew = (in.x + in.y + in.z) * one_third;
92
93         glm::vec3 skewed(glm::floor(in + skew));
94         float tr = (skewed.x + skewed.y + skewed.z) * one_sixth;
95
96         glm::vec3 unskewed(skewed - tr);
97         glm::vec3 relative(in - unskewed);
98
99         bool x_ge_y = relative.x >= relative.y;
100         bool x_ge_z = relative.x >= relative.z;
101         bool y_ge_z = relative.y >= relative.z;
102         unsigned int st = (x_ge_y << 2) | (x_ge_z << 1) | y_ge_z;
103
104         glm::ivec3 second_int(second_ints[st]);
105         glm::ivec3 third_int(third_ints[st]);
106         glm::vec3 second_float(second_floats[st]);
107         glm::vec3 third_float(third_floats[st]);
108
109         glm::vec3 offset[4] = {
110                 in - unskewed,
111                 relative - second_float + one_sixth,
112                 relative - third_float + one_third,
113                 relative - 0.5f,
114         };
115
116         int index[3] = {
117                 (int)(skewed.x) & 0xFF,
118                 (int)(skewed.y) & 0xFF,
119                 (int)(skewed.z) & 0xFF,
120         };
121
122         float n = 0.0f;
123
124         // I know 0.6 is wrong, but for some reason it looks better than 0.5
125
126         // 0
127         float t = glm::clamp(0.6f - glm::length2(offset[0]), 0.0f, 1.0f);
128         t *= t;
129         int corner = Perm12(index[0] + Perm(index[1] + Perm(index[2])));
130         n += t * t * glm::dot(Grad(corner), offset[0]);
131
132         // 1
133         t = glm::clamp(0.6f - glm::length2(offset[1]), 0.0f, 1.0f);
134         t *= t;
135         corner = Perm12(index[0] + second_int.x + Perm(index[1] + second_int.y + Perm(index[2] + second_int.z)));
136         n += t * t * glm::dot(Grad(corner), offset[1]);
137
138         // 2
139         t = glm::clamp(0.6f - glm::length2(offset[2]), 0.0f, 1.0f);
140         t *= t;
141         corner = Perm12(index[0] + third_int.x + Perm(index[1] + third_int.y + Perm(index[2] + third_int.z)));
142         n += t * t * glm::dot(Grad(corner), offset[2]);
143
144         // 3
145         t = glm::clamp(0.6f - glm::length2(offset[3]), 0.0f, 1.0f);
146         t *= t;
147         corner = Perm12(index[0] + 1 + Perm(index[1] + 1 + Perm(index[2] + 1)));
148         n += t * t * glm::dot(Grad(corner), offset[3]);
149
150         return 32.0f * n;
151 }
152
153
154 int SimplexNoise::Perm(int idx) const noexcept {
155         return perm[idx];
156 }
157
158 int SimplexNoise::Perm12(int idx) const noexcept {
159         return perm12[idx];
160 }
161
162 const glm::vec3 &SimplexNoise::Grad(int idx) const noexcept {
163         return grad[idx];
164 }
165
166
167 WorleyNoise::WorleyNoise(unsigned int seed) noexcept
168 : seed(seed)
169 , num_points(8) {
170
171 }
172
173 float WorleyNoise::operator ()(const glm::vec3 &in) const noexcept {
174         glm::vec3 center = glm::floor(in);
175
176         float closest = 1.0f;  // cannot be farther away than 1.0
177
178         for (int z = -1; z <= 1; ++z) {
179                 for (int y = -1; y <= 1; ++y) {
180                         for (int x = -1; x <= 1; ++x) {
181                                 glm::vec3 cube(center.x + x, center.y + y, center.z + z);
182                                 unsigned int cube_rand =
183                                         (unsigned(cube.x) * 130223) ^
184                                         (unsigned(cube.y) * 159899) ^
185                                         (unsigned(cube.z) * 190717) ^
186                                         seed;
187
188                                 for (int i = 0; i < num_points; ++i) {
189                                         glm::vec3 point(cube);
190                                         cube_rand = 190667 * cube_rand + 109807;
191                                         point.x += float(cube_rand % 262144) / 262144.0f;
192                                         cube_rand = 135899 * cube_rand + 189169;
193                                         point.y += float(cube_rand % 262144) / 262144.0f;
194                                         cube_rand = 159739 * cube_rand + 112139;
195                                         point.z += float(cube_rand % 262144) / 262144.0f;
196
197                                         float distance = glm::distance(in, point);
198                                         if (distance < closest) {
199                                                 closest = distance;
200                                         }
201                                 }
202                         }
203                 }
204         }
205
206         // closest ranges (0, 1), so normalizing to (-1,1) is trivial
207         // though heavily biased towards lower numbers
208         return 2.0f * closest - 1.0f;
209 }
210
211 }
212 }