1 #include "GaloisLFSR.hpp"
2 #include "SimplexNoise.hpp"
3 #include "WorleyNoise.hpp"
6 #include <glm/gtx/norm.hpp>
11 constexpr float one_third = 1.0f/3.0f;
12 constexpr float one_sixth = 1.0f/6.0f;
19 SimplexNoise::SimplexNoise(std::uint64_t seed) noexcept
22 { -1.0f, 1.0f, 0.0f },
23 { 1.0f, -1.0f, 0.0f },
24 { -1.0f, -1.0f, 0.0f },
26 { -1.0f, 0.0f, 1.0f },
27 { 1.0f, 0.0f, -1.0f },
28 { -1.0f, 0.0f, -1.0f },
30 { 0.0f, -1.0f, 1.0f },
31 { 0.0f, 1.0f, -1.0f },
32 { 0.0f, -1.0f, -1.0f },
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
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
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
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
78 GaloisLFSR random(seed ^ 0x0123456789ACBDEF);
80 for (size_t i = 0; i < 256; ++i) {
81 perm[i] = random(value);
83 perm[i + 256] = perm[i];
84 perm12[i] = perm[i] % 12;
85 perm12[i + 256] = perm12[i];
90 float SimplexNoise::operator ()(const glm::vec3 &in) const noexcept {
91 float skew = (in.x + in.y + in.z) * one_third;
93 glm::vec3 skewed(glm::floor(in + skew));
94 float tr = (skewed.x + skewed.y + skewed.z) * one_sixth;
96 glm::vec3 unskewed(skewed - tr);
97 glm::vec3 relative(in - unskewed);
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;
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]);
109 glm::vec3 offset[4] = {
111 relative - second_float + one_sixth,
112 relative - third_float + one_third,
117 (int)(skewed.x) & 0xFF,
118 (int)(skewed.y) & 0xFF,
119 (int)(skewed.z) & 0xFF,
124 // I know 0.6 is wrong, but for some reason it looks better than 0.5
127 float t = glm::clamp(0.6f - glm::length2(offset[0]), 0.0f, 1.0f);
129 int corner = Perm12(index[0] + Perm(index[1] + Perm(index[2])));
130 n += t * t * glm::dot(Grad(corner), offset[0]);
133 t = glm::clamp(0.6f - glm::length2(offset[1]), 0.0f, 1.0f);
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]);
139 t = glm::clamp(0.6f - glm::length2(offset[2]), 0.0f, 1.0f);
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]);
145 t = glm::clamp(0.6f - glm::length2(offset[3]), 0.0f, 1.0f);
147 corner = Perm12(index[0] + 1 + Perm(index[1] + 1 + Perm(index[2] + 1)));
148 n += t * t * glm::dot(Grad(corner), offset[3]);
154 int SimplexNoise::Perm(int idx) const noexcept {
158 int SimplexNoise::Perm12(int idx) const noexcept {
162 const glm::vec3 &SimplexNoise::Grad(int idx) const noexcept {
167 WorleyNoise::WorleyNoise(unsigned int seed) noexcept
173 float WorleyNoise::operator ()(const glm::vec3 &in) const noexcept {
174 glm::vec3 center = glm::floor(in);
176 float closest = 1.0f; // cannot be farther away than 1.0
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) ^
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;
197 float distance = glm::distance(in, point);
198 if (distance < closest) {
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;