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(unsigned int 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 },
32 GaloisLFSR random(seed ^ 0x0123456789ACBDEF);
34 for (size_t i = 0; i < 256; ++i) {
35 perm[i] = random(value);
37 perm[i + 256] = perm[i];
38 perm12[i] = perm[i] % 12;
39 perm12[i + 256] = perm12[i];
44 float SimplexNoise::operator ()(const glm::vec3 &in) const noexcept {
45 float skew = (in.x + in.y + in.z) * one_third;
47 glm::vec3 skewed(glm::floor(in + skew));
48 float tr = (skewed.x + skewed.y + skewed.z) * one_sixth;
50 glm::vec3 unskewed(skewed - tr);
51 glm::vec3 relative(in - unskewed);
53 glm::vec3 second, third;
55 if (relative.x >= relative.y) {
56 if (relative.y >= relative.z) {
59 } else if (relative.x >= relative.z) {
66 } else if (relative.y < relative.z) {
69 } else if (relative.x < relative.z) {
77 glm::vec3 offset[4] = {
79 relative - second + one_sixth,
80 relative - third + one_third,
85 (int)(skewed.x) & 0xFF,
86 (int)(skewed.y) & 0xFF,
87 (int)(skewed.z) & 0xFF,
93 float t = 0.6f - dot(offset[0], offset[0]);
96 int corner = Perm12(index[0] + Perm(index[1] + Perm(index[2])));
97 n += t * t * dot(Grad(corner), offset[0]);
101 t = 0.6f - dot(offset[1], offset[1]);
104 int corner = Perm12(index[0] + int(second.x) + Perm(index[1] + int(second.y) + Perm(index[2] + int(second.z))));
105 n += t * t * dot(Grad(corner), offset[1]);
109 t = 0.6f - dot(offset[2], offset[2]);
112 int corner = Perm12(index[0] + int(third.x) + Perm(index[1] + int(third.y) + Perm(index[2] + int(third.z))));
113 n += t * t * dot(Grad(corner), offset[2]);
117 t = 0.6f - dot(offset[3], offset[3]);
120 int corner = Perm12(index[0] + 1 + Perm(index[1] + 1 + Perm(index[2] + 1)));
121 n += t * t * dot(Grad(corner), offset[3]);
128 int SimplexNoise::Perm(int idx) const noexcept {
132 int SimplexNoise::Perm12(int idx) const noexcept {
136 const glm::vec3 &SimplexNoise::Grad(int idx) const noexcept {
141 WorleyNoise::WorleyNoise(unsigned int seed) noexcept
147 float WorleyNoise::operator ()(const glm::vec3 &in) const noexcept {
148 glm::vec3 center = floor(in);
150 float closest = 1.0f; // cannot be farther away than 1.0
152 for (int z = -1; z <= 1; ++z) {
153 for (int y = -1; y <= 1; ++y) {
154 for (int x = -1; x <= 1; ++x) {
155 glm::vec3 cube(center.x + x, center.y + y, center.z + z);
156 unsigned int cube_rand =
157 (unsigned(cube.x) * 130223) ^
158 (unsigned(cube.y) * 159899) ^
159 (unsigned(cube.z) * 190717) ^
162 for (int i = 0; i < num_points; ++i) {
163 glm::vec3 point(cube);
164 cube_rand = 190667 * cube_rand + 109807;
165 point.x += float(cube_rand % 262144) / 262144.0f;
166 cube_rand = 135899 * cube_rand + 189169;
167 point.y += float(cube_rand % 262144) / 262144.0f;
168 cube_rand = 159739 * cube_rand + 112139;
169 point.z += float(cube_rand % 262144) / 262144.0f;
171 glm::vec3 diff(in - point);
172 float distance = sqrt(dot(diff, diff));
173 if (distance < closest) {
181 // closest ranges (0, 1), so normalizing to (-1,1) is trivial
182 // though heavily biased towards lower numbers
183 return 2.0f * closest - 1.0f;