1 #include "GaloisLFSR.hpp"
2 #include "SimplexNoise.hpp"
3 #include "WorleyNoise.hpp"
10 constexpr float one_third = 1.0f/3.0f;
11 constexpr float one_sixth = 1.0f/6.0f;
17 SimplexNoise::SimplexNoise(std::uint64_t seed) noexcept
20 { -1.0f, 1.0f, 0.0f },
21 { 1.0f, -1.0f, 0.0f },
22 { -1.0f, -1.0f, 0.0f },
24 { -1.0f, 0.0f, 1.0f },
25 { 1.0f, 0.0f, -1.0f },
26 { -1.0f, 0.0f, -1.0f },
28 { 0.0f, -1.0f, 1.0f },
29 { 0.0f, 1.0f, -1.0f },
30 { 0.0f, -1.0f, -1.0f },
34 { 0, 0, 1 }, // 0 0 0 ZYX
35 { 0, 1, 0 }, // 0 0 1 YZX
36 { 0, 0, 1 }, // 0 1 0 illogical, but ZYX
37 { 0, 1, 0 }, // 0 1 1 YXZ
38 { 0, 0, 1 }, // 1 0 0 ZXY
39 { 1, 0, 0 }, // 1 0 1 illogical, but XYZ
40 { 1, 0, 0 }, // 1 1 0 XZY
41 { 1, 0, 0 }, // 1 1 1 XYZ
45 { 0, 1, 1 }, // 0 0 0 ZYX
46 { 0, 1, 1 }, // 0 0 1 YZX
47 { 0, 1, 1 }, // 0 1 0 illogical, but ZYX
48 { 1, 1, 0 }, // 0 1 1 YXZ
49 { 1, 0, 1 }, // 1 0 0 ZXY
50 { 1, 1, 0 }, // 1 0 1 illogical, but XYZ
51 { 1, 0, 1 }, // 1 1 0 XZY
52 { 1, 1, 0 }, // 1 1 1 XYZ
56 { 0.0f, 0.0f, 1.0f }, // 0 0 0 ZYX
57 { 0.0f, 1.0f, 0.0f }, // 0 0 1 YZX
58 { 0.0f, 0.0f, 1.0f }, // 0 1 0 illogical, but ZYX
59 { 0.0f, 1.0f, 0.0f }, // 0 1 1 YXZ
60 { 0.0f, 0.0f, 1.0f }, // 1 0 0 ZXY
61 { 1.0f, 0.0f, 0.0f }, // 1 0 1 illogical, but XYZ
62 { 1.0f, 0.0f, 0.0f }, // 1 1 0 XZY
63 { 1.0f, 0.0f, 0.0f }, // 1 1 1 XYZ
67 { 0.0f, 1.0f, 1.0f }, // 0 0 0 ZYX
68 { 0.0f, 1.0f, 1.0f }, // 0 0 1 YZX
69 { 0.0f, 1.0f, 1.0f }, // 0 1 0 illogical, but ZYX
70 { 1.0f, 1.0f, 0.0f }, // 0 1 1 YXZ
71 { 1.0f, 0.0f, 1.0f }, // 1 0 0 ZXY
72 { 1.0f, 1.0f, 0.0f }, // 1 0 1 illogical, but XYZ
73 { 1.0f, 0.0f, 1.0f }, // 1 1 0 XZY
74 { 1.0f, 1.0f, 0.0f }, // 1 1 1 XYZ
76 GaloisLFSR random(seed ^ 0x0123456789ACBDEF);
78 for (size_t i = 0; i < 256; ++i) {
79 perm[i] = random(value);
81 perm[i + 256] = perm[i];
82 perm12[i] = perm[i] % 12;
83 perm12[i + 256] = perm12[i];
88 float SimplexNoise::operator ()(const glm::vec3 &in) const noexcept {
89 float skew = (in.x + in.y + in.z) * one_third;
91 glm::vec3 skewed(glm::floor(in + skew));
92 float tr = (skewed.x + skewed.y + skewed.z) * one_sixth;
94 glm::vec3 unskewed(skewed - tr);
95 glm::vec3 relative(in - unskewed);
97 bool x_ge_y = relative.x >= relative.y;
98 bool x_ge_z = relative.x >= relative.z;
99 bool y_ge_z = relative.y >= relative.z;
100 unsigned int st = (x_ge_y << 2) | (x_ge_z << 1) | y_ge_z;
102 glm::ivec3 second_int(second_ints[st]);
103 glm::ivec3 third_int(third_ints[st]);
104 glm::vec3 second_float(second_floats[st]);
105 glm::vec3 third_float(third_floats[st]);
107 glm::vec3 offset[4] = {
109 relative - second_float + one_sixth,
110 relative - third_float + one_third,
115 (int)(skewed.x) & 0xFF,
116 (int)(skewed.y) & 0xFF,
117 (int)(skewed.z) & 0xFF,
122 // I know 0.6 is wrong, but for some reason it looks better than 0.5
125 float t = glm::clamp(0.6f - dot(offset[0], offset[0]), 0.0f, 1.0f);
127 int corner = Perm12(index[0] + Perm(index[1] + Perm(index[2])));
128 n += t * t * dot(Grad(corner), offset[0]);
131 t = glm::clamp(0.6f - dot(offset[1], offset[1]), 0.0f, 1.0f);
133 corner = Perm12(index[0] + second_int.x + Perm(index[1] + second_int.y + Perm(index[2] + second_int.z)));
134 n += t * t * dot(Grad(corner), offset[1]);
137 t = glm::clamp(0.6f - dot(offset[2], offset[2]), 0.0f, 1.0f);
139 corner = Perm12(index[0] + third_int.x + Perm(index[1] + third_int.y + Perm(index[2] + third_int.z)));
140 n += t * t * dot(Grad(corner), offset[2]);
143 t = glm::clamp(0.6f - dot(offset[3], offset[3]), 0.0f, 1.0f);
145 corner = Perm12(index[0] + 1 + Perm(index[1] + 1 + Perm(index[2] + 1)));
146 n += t * t * dot(Grad(corner), offset[3]);
152 int SimplexNoise::Perm(int idx) const noexcept {
156 int SimplexNoise::Perm12(int idx) const noexcept {
160 const glm::vec3 &SimplexNoise::Grad(int idx) const noexcept {
165 WorleyNoise::WorleyNoise(unsigned int seed) noexcept
171 float WorleyNoise::operator ()(const glm::vec3 &in) const noexcept {
172 glm::vec3 center = floor(in);
174 float closest = 1.0f; // cannot be farther away than 1.0
176 for (int z = -1; z <= 1; ++z) {
177 for (int y = -1; y <= 1; ++y) {
178 for (int x = -1; x <= 1; ++x) {
179 glm::vec3 cube(center.x + x, center.y + y, center.z + z);
180 unsigned int cube_rand =
181 (unsigned(cube.x) * 130223) ^
182 (unsigned(cube.y) * 159899) ^
183 (unsigned(cube.z) * 190717) ^
186 for (int i = 0; i < num_points; ++i) {
187 glm::vec3 point(cube);
188 cube_rand = 190667 * cube_rand + 109807;
189 point.x += float(cube_rand % 262144) / 262144.0f;
190 cube_rand = 135899 * cube_rand + 189169;
191 point.y += float(cube_rand % 262144) / 262144.0f;
192 cube_rand = 159739 * cube_rand + 112139;
193 point.z += float(cube_rand % 262144) / 262144.0f;
195 glm::vec3 diff(in - point);
196 float distance = sqrt(dot(diff, diff));
197 if (distance < closest) {
205 // closest ranges (0, 1), so normalizing to (-1,1) is trivial
206 // though heavily biased towards lower numbers
207 return 2.0f * closest - 1.0f;