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;
18 SimplexNoise::SimplexNoise(std::uint64_t seed) noexcept
21 { -1.0f, 1.0f, 0.0f },
22 { 1.0f, -1.0f, 0.0f },
23 { -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 },
29 { 0.0f, -1.0f, 1.0f },
30 { 0.0f, 1.0f, -1.0f },
31 { 0.0f, -1.0f, -1.0f },
35 { 0, 0, 1 }, // 0 0 0 ZYX
36 { 0, 1, 0 }, // 0 0 1 YZX
37 { 0, 0, 1 }, // 0 1 0 illogical, but ZYX
38 { 0, 1, 0 }, // 0 1 1 YXZ
39 { 0, 0, 1 }, // 1 0 0 ZXY
40 { 1, 0, 0 }, // 1 0 1 illogical, but XYZ
41 { 1, 0, 0 }, // 1 1 0 XZY
42 { 1, 0, 0 }, // 1 1 1 XYZ
46 { 0, 1, 1 }, // 0 0 0 ZYX
47 { 0, 1, 1 }, // 0 0 1 YZX
48 { 0, 1, 1 }, // 0 1 0 illogical, but ZYX
49 { 1, 1, 0 }, // 0 1 1 YXZ
50 { 1, 0, 1 }, // 1 0 0 ZXY
51 { 1, 1, 0 }, // 1 0 1 illogical, but XYZ
52 { 1, 0, 1 }, // 1 1 0 XZY
53 { 1, 1, 0 }, // 1 1 1 XYZ
57 { 0.0f, 0.0f, 1.0f }, // 0 0 0 ZYX
58 { 0.0f, 1.0f, 0.0f }, // 0 0 1 YZX
59 { 0.0f, 0.0f, 1.0f }, // 0 1 0 illogical, but ZYX
60 { 0.0f, 1.0f, 0.0f }, // 0 1 1 YXZ
61 { 0.0f, 0.0f, 1.0f }, // 1 0 0 ZXY
62 { 1.0f, 0.0f, 0.0f }, // 1 0 1 illogical, but XYZ
63 { 1.0f, 0.0f, 0.0f }, // 1 1 0 XZY
64 { 1.0f, 0.0f, 0.0f }, // 1 1 1 XYZ
68 { 0.0f, 1.0f, 1.0f }, // 0 0 0 ZYX
69 { 0.0f, 1.0f, 1.0f }, // 0 0 1 YZX
70 { 0.0f, 1.0f, 1.0f }, // 0 1 0 illogical, but ZYX
71 { 1.0f, 1.0f, 0.0f }, // 0 1 1 YXZ
72 { 1.0f, 0.0f, 1.0f }, // 1 0 0 ZXY
73 { 1.0f, 1.0f, 0.0f }, // 1 0 1 illogical, but XYZ
74 { 1.0f, 0.0f, 1.0f }, // 1 1 0 XZY
75 { 1.0f, 1.0f, 0.0f }, // 1 1 1 XYZ
77 GaloisLFSR random(seed ^ 0x0123456789ACBDEF);
79 for (size_t i = 0; i < 256; ++i) {
80 perm[i] = random(value);
82 perm[i + 256] = perm[i];
83 perm12[i] = perm[i] % 12;
84 perm12[i + 256] = perm12[i];
89 float SimplexNoise::operator ()(const glm::vec3 &in) const noexcept {
90 float skew = (in.x + in.y + in.z) * one_third;
92 glm::vec3 skewed(glm::floor(in + skew));
93 float tr = (skewed.x + skewed.y + skewed.z) * one_sixth;
95 glm::vec3 unskewed(skewed - tr);
96 glm::vec3 relative(in - unskewed);
98 bool x_ge_y = relative.x >= relative.y;
99 bool x_ge_z = relative.x >= relative.z;
100 bool y_ge_z = relative.y >= relative.z;
101 unsigned int st = (x_ge_y << 2) | (x_ge_z << 1) | y_ge_z;
103 glm::ivec3 second_int(second_ints[st]);
104 glm::ivec3 third_int(third_ints[st]);
105 glm::vec3 second_float(second_floats[st]);
106 glm::vec3 third_float(third_floats[st]);
108 glm::vec3 offset[4] = {
110 relative - second_float + one_sixth,
111 relative - third_float + one_third,
116 (int)(skewed.x) & 0xFF,
117 (int)(skewed.y) & 0xFF,
118 (int)(skewed.z) & 0xFF,
123 // I know 0.6 is wrong, but for some reason it looks better than 0.5
126 float t = glm::clamp(0.6f - glm::length2(offset[0]), 0.0f, 1.0f);
128 int corner = Perm12(index[0] + Perm(index[1] + Perm(index[2])));
129 n += t * t * glm::dot(Grad(corner), offset[0]);
132 t = glm::clamp(0.6f - glm::length2(offset[1]), 0.0f, 1.0f);
134 corner = Perm12(index[0] + second_int.x + Perm(index[1] + second_int.y + Perm(index[2] + second_int.z)));
135 n += t * t * glm::dot(Grad(corner), offset[1]);
138 t = glm::clamp(0.6f - glm::length2(offset[2]), 0.0f, 1.0f);
140 corner = Perm12(index[0] + third_int.x + Perm(index[1] + third_int.y + Perm(index[2] + third_int.z)));
141 n += t * t * glm::dot(Grad(corner), offset[2]);
144 t = glm::clamp(0.6f - glm::length2(offset[3]), 0.0f, 1.0f);
146 corner = Perm12(index[0] + 1 + Perm(index[1] + 1 + Perm(index[2] + 1)));
147 n += t * t * glm::dot(Grad(corner), offset[3]);
153 int SimplexNoise::Perm(int idx) const noexcept {
157 int SimplexNoise::Perm12(int idx) const noexcept {
161 const glm::vec3 &SimplexNoise::Grad(int idx) const noexcept {
166 WorleyNoise::WorleyNoise(unsigned int seed) noexcept
172 float WorleyNoise::operator ()(const glm::vec3 &in) const noexcept {
173 glm::vec3 center = glm::floor(in);
175 float closest = 1.0f; // cannot be farther away than 1.0
177 for (int z = -1; z <= 1; ++z) {
178 for (int y = -1; y <= 1; ++y) {
179 for (int x = -1; x <= 1; ++x) {
180 glm::vec3 cube(center.x + x, center.y + y, center.z + z);
181 unsigned int cube_rand =
182 (unsigned(cube.x) * 130223) ^
183 (unsigned(cube.y) * 159899) ^
184 (unsigned(cube.z) * 190717) ^
187 for (int i = 0; i < num_points; ++i) {
188 glm::vec3 point(cube);
189 cube_rand = 190667 * cube_rand + 109807;
190 point.x += float(cube_rand % 262144) / 262144.0f;
191 cube_rand = 135899 * cube_rand + 189169;
192 point.y += float(cube_rand % 262144) / 262144.0f;
193 cube_rand = 159739 * cube_rand + 112139;
194 point.z += float(cube_rand % 262144) / 262144.0f;
196 float distance = glm::distance(in, point);
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;