-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathsimplexnoise.cpp
More file actions
147 lines (119 loc) · 3.5 KB
/
simplexnoise.cpp
File metadata and controls
147 lines (119 loc) · 3.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#include <fea/util/simplexnoise.hpp>
#include <cmath>
namespace fea
{
float raw_noise_2d(float x, float y, const uint8_t* perm)
{
const float F2 = 0.5f * (std::sqrt(3.0f) - 1.0f);
float s = (x + y) * F2;
int32_t i = fast_floor(x + s);
int32_t j = fast_floor(y + s);
const float G2 = (3.0f - std::sqrt(3.0f)) / 6.0f;
float t = (i + j) * G2;
float x0 = x - (i - t);
float y0 = y - (j - t);
uint8_t i1 = 0;
uint8_t j1 = 1;
if(x0 > y0)
{
i1 = 1;
j1 = 0;
}
float x1 = x0 - i1 + G2;
float y1 = y0 - j1 + G2;
float x2 = x0 - 1.0f + 2.0f * G2;
float y2 = y0 - 1.0f + 2.0f * G2;
uint8_t ii = i & 255;
uint8_t jj = j & 255;
uint8_t gi0 = perm[ii + perm[jj ] ] % 12;
uint8_t gi1 = perm[ii + perm[jj + j1] + i1] % 12;
uint8_t gi2 = perm[ii + perm[jj + 1 ] + 1 ] % 12;
float t0 = 0.5f - x0 * x0 - y0 * y0;
float t1 = 0.5f - x1 * x1 - y1 * y1;
float t2 = 0.5f - x2 * x2 - y2 * y2;
float n0 = (t0 < 0) ? 0.0f : t0*t0*t0*t0 * dot(grad3[gi0], x0, y0);
float n1 = (t1 < 0) ? 0.0f : t1*t1*t1*t1 * dot(grad3[gi1], x1, y1);
float n2 = (t2 < 0) ? 0.0f : t2*t2*t2*t2 * dot(grad3[gi2], x2, y2);
return 70.0f * (n0 + n1 + n2);
}
float raw_noise_3d(float x, float y, float z, const uint8_t* perm)
{
constexpr float F3 = 1.0f / 3.0f;
float s = (x + y + z) * F3;
float i = fast_floor(x + s);
float j = fast_floor(y + s);
float k = fast_floor(z + s);
constexpr float G3 = 1.0f / 6.0f;
float t = (i + j + k) * G3;
float x0 = x - (i - t);
float y0 = y - (j - t);
float z0 = z - (k - t);
uint8_t i1 = 0, j1 = 1, k1 = 0;
uint8_t i2 = 1, j2 = 1, k2 = 0;
if(x0 >= y0)
{
if(y0 >= z0)
{
i1 = 1; j1 = 0; k1 = 0;
i2 = 1; j2 = 1; k2 = 0;
}
else if(x0 >= z0)
{
i1 = 1; j1 = 0; k1 = 0;
i2 = 1; j2 = 0; k2 = 1;
}
else
{
i1 = 0; j1 = 0; k1 = 1;
i2 = 1; j2 = 0; k2 = 1;
}
}
else
{
if(y0 < z0)
{
i1 = 0; j1 = 0; k1 = 1;
i2 = 0; j2 = 1; k2 = 1;
}
else if(x0 < z0)
{
i1 = 0; j1 = 1; k1 = 0;
i2 = 0; j2 = 1; k2 = 1;
}
}
float x1 = x0 - i1 + G3;
float y1 = y0 - j1 + G3;
float z1 = z0 - k1 + G3;
float x2 = x0 - i2 + G3 * 2.0f;
float y2 = y0 - j2 + G3 * 2.0f;
float z2 = z0 - k2 + G3 * 2.0f;
float x3 = x0 - 1.0f + G3 * 3.0f;
float y3 = y0 - 1.0f + G3 * 3.0f;
float z3 = z0 - 1.0f + G3 * 3.0f;
uint8_t ii = int32_t(i);
uint8_t jj = int32_t(j);
uint8_t kk = int32_t(k);
uint8_t gi0 = perm[ii + perm[jj + perm[kk ] ] ] % 12;
uint8_t gi1 = perm[ii + perm[jj + perm[kk + k1] + j1] + i1] % 12;
uint8_t gi2 = perm[ii + perm[jj + perm[kk + k2] + j2] + i2] % 12;
uint8_t gi3 = perm[ii + perm[jj + perm[kk + 1 ] + 1 ] + 1 ] % 12;
float t0 = 0.5f - x0 * x0 - y0 * y0 - z0 * z0;
float t1 = 0.5f - x1 * x1 - y1 * y1 - z1 * z1;
float t2 = 0.5f - x2 * x2 - y2 * y2 - z2 * z2;
float t3 = 0.5f - x3 * x3 - y3 * y3 - z3 * z3;
float n0 = (t0 < 0) ? 0.0f : t0*t0*t0*t0 * dot(grad3[gi0], x0, y0, z0);
float n1 = (t1 < 0) ? 0.0f : t1*t1*t1*t1 * dot(grad3[gi1], x1, y1, z1);
float n2 = (t2 < 0) ? 0.0f : t2*t2*t2*t2 * dot(grad3[gi2], x2, y2, z2);
float n3 = (t3 < 0) ? 0.0f : t3*t3*t3*t3 * dot(grad3[gi3], x3, y3, z3);
return 32.0f * (n0 + n1 + n2 + n3);
}
float dot(const int8_t* grad, float x, float y)
{
return grad[0] * x + grad[1] * y;
}
float dot(const int8_t* grad, float x, float y, float z)
{
return grad[0] * x + grad[1] * y + grad[2] * z;
}
inline int fast_floor(float x){return ((x>=0)?((int)x):((x==(int)x)?(int)x:((int)x)-1));}
}