1 #include "GaloisLFSR.hpp"
2 #include "SimplexNoise.hpp"
9 constexpr float one_third = 1.0f/3.0f;
10 constexpr float one_sixth = 1.0f/6.0f;
16 SimplexNoise::SimplexNoise(std::uint64_t seed) noexcept
19 { -1.0f, 1.0f, 0.0f },
20 { 1.0f, -1.0f, 0.0f },
21 { -1.0f, -1.0f, 0.0f },
23 { -1.0f, 0.0f, 1.0f },
24 { 1.0f, 0.0f, -1.0f },
25 { -1.0f, 0.0f, -1.0f },
27 { 0.0f, -1.0f, 1.0f },
28 { 0.0f, 1.0f, -1.0f },
29 { 0.0f, -1.0f, -1.0f },
33 { 0, 0, 1 }, // 0 0 0 ZYX
34 { 0, 1, 0 }, // 0 0 1 YZX
35 { 0, 0, 1 }, // 0 1 0 illogical, but ZYX
36 { 0, 1, 0 }, // 0 1 1 YXZ
37 { 0, 0, 1 }, // 1 0 0 ZXY
38 { 1, 0, 0 }, // 1 0 1 illogical, but XYZ
39 { 1, 0, 0 }, // 1 1 0 XZY
40 { 1, 0, 0 }, // 1 1 1 XYZ
44 { 0, 1, 1 }, // 0 0 0 ZYX
45 { 0, 1, 1 }, // 0 0 1 YZX
46 { 0, 1, 1 }, // 0 1 0 illogical, but ZYX
47 { 1, 1, 0 }, // 0 1 1 YXZ
48 { 1, 0, 1 }, // 1 0 0 ZXY
49 { 1, 1, 0 }, // 1 0 1 illogical, but XYZ
50 { 1, 0, 1 }, // 1 1 0 XZY
51 { 1, 1, 0 }, // 1 1 1 XYZ
55 { 0.0f, 0.0f, 1.0f }, // 0 0 0 ZYX
56 { 0.0f, 1.0f, 0.0f }, // 0 0 1 YZX
57 { 0.0f, 0.0f, 1.0f }, // 0 1 0 illogical, but ZYX
58 { 0.0f, 1.0f, 0.0f }, // 0 1 1 YXZ
59 { 0.0f, 0.0f, 1.0f }, // 1 0 0 ZXY
60 { 1.0f, 0.0f, 0.0f }, // 1 0 1 illogical, but XYZ
61 { 1.0f, 0.0f, 0.0f }, // 1 1 0 XZY
62 { 1.0f, 0.0f, 0.0f }, // 1 1 1 XYZ
66 { 0.0f, 1.0f, 1.0f }, // 0 0 0 ZYX
67 { 0.0f, 1.0f, 1.0f }, // 0 0 1 YZX
68 { 0.0f, 1.0f, 1.0f }, // 0 1 0 illogical, but ZYX
69 { 1.0f, 1.0f, 0.0f }, // 0 1 1 YXZ
70 { 1.0f, 0.0f, 1.0f }, // 1 0 0 ZXY
71 { 1.0f, 1.0f, 0.0f }, // 1 0 1 illogical, but XYZ
72 { 1.0f, 0.0f, 1.0f }, // 1 1 0 XZY
73 { 1.0f, 1.0f, 0.0f }, // 1 1 1 XYZ
75 GaloisLFSR random(seed ^ 0x0123456789ACBDEF);
77 for (size_t i = 0; i < 256; ++i) {
78 perm[i] = random(value);
80 perm[i + 256] = perm[i];
81 perm12[i] = perm[i] % 12;
82 perm12[i + 256] = perm12[i];
87 float SimplexNoise::operator ()(const glm::vec3 &in) const noexcept {
88 float skew = (in.x + in.y + in.z) * one_third;
90 glm::vec3 skewed(glm::floor(in + skew));
91 float tr = (skewed.x + skewed.y + skewed.z) * one_sixth;
93 glm::vec3 unskewed(skewed - tr);
94 glm::vec3 relative(in - unskewed);
96 bool x_ge_y = relative.x >= relative.y;
97 bool x_ge_z = relative.x >= relative.z;
98 bool y_ge_z = relative.y >= relative.z;
99 unsigned int st = (x_ge_y << 2) | (x_ge_z << 1) | y_ge_z;
101 glm::ivec3 second_int(second_ints[st]);
102 glm::ivec3 third_int(third_ints[st]);
103 glm::vec3 second_float(second_floats[st]);
104 glm::vec3 third_float(third_floats[st]);
106 glm::vec3 offset[4] = {
108 relative - second_float + one_sixth,
109 relative - third_float + one_third,
114 (int)(skewed.x) & 0xFF,
115 (int)(skewed.y) & 0xFF,
116 (int)(skewed.z) & 0xFF,
122 float t = glm::clamp(0.5f - dot(offset[0], offset[0]), 0.0f, 1.0f);
124 int corner = Perm12(index[0] + Perm(index[1] + Perm(index[2])));
125 n += t * t * dot(Grad(corner), offset[0]);
128 t = glm::clamp(0.5f - dot(offset[1], offset[1]), 0.0f, 1.0f);
130 corner = Perm12(index[0] + second_int.x + Perm(index[1] + second_int.y + Perm(index[2] + second_int.z)));
131 n += t * t * dot(Grad(corner), offset[1]);
134 t = glm::clamp(0.5f - dot(offset[2], offset[2]), 0.0f, 1.0f);
136 corner = Perm12(index[0] + third_int.x + Perm(index[1] + third_int.y + Perm(index[2] + third_int.z)));
137 n += t * t * dot(Grad(corner), offset[2]);
140 t = glm::clamp(0.5f - dot(offset[3], offset[3]), 0.0f, 1.0f);
142 corner = Perm12(index[0] + 1 + Perm(index[1] + 1 + Perm(index[2] + 1)));
143 n += t * t * dot(Grad(corner), offset[3]);
149 int SimplexNoise::Perm(int idx) const noexcept {
153 int SimplexNoise::Perm12(int idx) const noexcept {
157 const glm::vec3 &SimplexNoise::Grad(int idx) const noexcept {