4SimplexNoise<colors>::SimplexNoise(int seed, GradientMaterial<colors>* gradientMaterial) {
5 this->gradientMaterial = gradientMaterial;
7 //the seed determines the swaps that occur between the default order and the order we're actually going to use
8 for(int i = 0; i < 400; i++){
9 uint8_t swapFrom = static_cast<uint8_t>(std::rand() % 256);
10 uint8_t swapTo = static_cast<uint8_t>(std::rand() % 256);
12 uint8_t temp = p[swapFrom];
13 p[swapFrom] = p[swapTo];
17 for(int i = 0; i < 512; i++){
19 permMod12[i] = perm[i] % 12;
24template<size_t colors>
25float SimplexNoise<colors>::Noise(float xin, float yin) {
26 float n0, n1, n2; // Noise contributions from the three corners
28 // Skew the input space to determine which simplex cell we're in
29 float s = (xin+yin)*F2; // Hairy factor for 2D
30 int i = Mathematics::FFloor(xin+s);
31 int j = Mathematics::FFloor(yin+s);
33 float X0 = i-t; // Unskew the cell origin back to (x,y) space
35 float x0 = xin-X0; // The x,y distances from the cell origin
38 // For the 2D case, the simplex shape is an equilateral triangle.
39 // Determine which simplex we are in.
40 int i1, j1; // Offsets for second (middle) corner of simplex in (i,j) coords
41 if(x0>y0) {i1=1; j1=0;} // lower triangle, XY order: (0,0)->(1,0)->(1,1)
42 else {i1=0; j1=1;} // upper triangle, YX order: (0,0)->(0,1)->(1,1)
43 // A step of (1,0) in (i,j) means a step of (1-c,-c) in (x,y), and
44 // a step of (0,1) in (i,j) means a step of (-c,1-c) in (x,y), where
47 float x1 = x0 - i1 + G2; // Offsets for middle corner in (x,y) unskewed coords
48 float y1 = y0 - j1 + G2;
49 float x2 = x0 - 1.0f + 2.0f * G2; // Offsets for last corner in (x,y) unskewed coords
50 float y2 = y0 - 1.0f + 2.0f * G2;
52 // Work out the hashed gradient indices of the three simplex corners
55 int gi0 = permMod12[ii+perm[jj]];
56 int gi1 = permMod12[ii+i1+perm[jj+j1]];
57 int gi2 = permMod12[ii+1+perm[jj+1]];
59 // Calculate the contribution from the three corners
60 float t0 = 0.5f - x0*x0-y0*y0;
64 n0 = t0 * t0 * grad3[gi0].DotProduct(Vector3D(x0, y0, 0));//dot(grad3[gi0], x0, y0); // (x,y) of grad3 used for 2D gradient
66 float t1 = 0.5f - x1*x1-y1*y1;
70 n1 = t1 * t1 * grad3[gi1].DotProduct(Vector3D(x1, y1, 0));//dot(grad3[gi1], x1, y1);
72 float t2 = 0.5f - x2*x2-y2*y2;
76 n2 = t2 * t2 * grad3[gi2].DotProduct(Vector3D(x2, y2, 0));//dot(grad3[gi2], x2, y2);
78 // Add contributions from each corner to get the final noise value.
79 // The result is scaled to return values in the interval [-1,1].
80 return 70.0f * (n0 + n1 + n2);
84template<size_t colors>
85float SimplexNoise<colors>::Noise(float xin, float yin, float zin) {
86 float n0, n1, n2, n3; // Noise contributions from the four corners
88 // Skew the input space to determine which simplex cell we're in
89 float s = (xin+yin+zin)*F3; // Very nice and simple skew factor for 3D
90 int i = Mathematics::FFloor(xin+s);
91 int j = Mathematics::FFloor(yin+s);
92 int k = Mathematics::FFloor(zin+s);
94 float X0 = i-t; // Unskew the cell origin back to (x,y,z) space
97 float x0 = xin-X0; // The x,y,z distances from the cell origin
101 // For the 3D case, the simplex shape is a slightly irregular tetrahedron.
102 // Determine which simplex we are in.
103 int i1, j1, k1; // Offsets for second corner of simplex in (i,j,k) coords
104 int i2, j2, k2; // Offsets for third corner of simplex in (i,j,k) coords
107 if(y0>=z0) { i1=1; j1=0; k1=0; i2=1; j2=1; k2=0; } // X Y Z order
108 else if(x0>=z0) { i1=1; j1=0; k1=0; i2=1; j2=0; k2=1; } // X Z Y order
109 else { i1=0; j1=0; k1=1; i2=1; j2=0; k2=1; } // Z X Y order
112 if(y0<z0) { i1=0; j1=0; k1=1; i2=0; j2=1; k2=1; } // Z Y X order
113 else if(x0<z0) { i1=0; j1=1; k1=0; i2=0; j2=1; k2=1; } // Y Z X order
114 else { i1=0; j1=1; k1=0; i2=1; j2=1; k2=0; } // Y X Z order
117 // A step of (1,0,0) in (i,j,k) means a step of (1-c,-c,-c) in (x,y,z),
118 // a step of (0,1,0) in (i,j,k) means a step of (-c,1-c,-c) in (x,y,z), and
119 // a step of (0,0,1) in (i,j,k) means a step of (-c,-c,1-c) in (x,y,z), where
121 float x1 = x0 - i1 + G3; // Offsets for second corner in (x,y,z) coords
122 float y1 = y0 - j1 + G3;
123 float z1 = z0 - k1 + G3;
124 float x2 = x0 - i2 + 2.0f * G3; // Offsets for third corner in (x,y,z) coords
125 float y2 = y0 - j2 + 2.0f * G3;
126 float z2 = z0 - k2 + 2.0f * G3;
127 float x3 = x0 - 1.0f + 3.0f * G3; // Offsets for last corner in (x,y,z) coords
128 float y3 = y0 - 1.0f + 3.0f * G3;
129 float z3 = z0 - 1.0f + 3.0f * G3;
131 // Work out the hashed gradient indices of the four simplex corners
135 int gi0 = permMod12[ii+perm[jj+perm[kk]]];
136 int gi1 = permMod12[ii+i1+perm[jj+j1+perm[kk+k1]]];
137 int gi2 = permMod12[ii+i2+perm[jj+j2+perm[kk+k2]]];
138 int gi3 = permMod12[ii+1+perm[jj+1+perm[kk+1]]];
140 // Calculate the contribution from the four corners
141 float t0 = 0.6f - x0*x0 - y0*y0 - z0*z0;
142 if(t0 < 0) n0 = 0.0f;
145 n0 = t0 * t0 * grad3[gi0].DotProduct(Vector3D(x0, y0, z0));//dot(grad3[gi0], x0, y0, z0);
148 float t1 = 0.6f - x1*x1 - y1*y1 - z1*z1;
149 if(t1 < 0) n1 = 0.0f;
152 n1 = t1 * t1 * grad3[gi1].DotProduct(Vector3D(x1, y1, z1));;//dot(grad3[gi1], x1, y1, z1);
155 float t2 = 0.6f - x2*x2 - y2*y2 - z2*z2;
156 if(t2 < 0) n2 = 0.0f;
159 n2 = t2 * t2 * grad3[gi2].DotProduct(Vector3D(x2, y2, z2));//dot(grad3[gi2], x2, y2, z2);
162 float t3 = 0.6f - x3*x3 - y3*y3 - z3*z3;
163 if(t3 < 0) n3 = 0.0f;
166 n3 = t3 * t3 * grad3[gi3].DotProduct(Vector3D(x3, y3, z3));//dot(grad3[gi3], x3, y3, z3);
169 // Add contributions from each corner to get the final noise value.
170 // The result is scaled to stay just inside [-1,1]
171 return 32.0f * (n0 + n1 + n2 + n3);
174template<size_t colors>
175void SimplexNoise<colors>::SetScale(Vector3D noiseScale){
176 this->noiseScale = noiseScale;
179template<size_t colors>
180void SimplexNoise<colors>::SetZPosition(float zPosition){
181 this->zPosition = zPosition;
184template<size_t colors>
185RGBColor SimplexNoise<colors>::GetRGB(const Vector3D& position, const Vector3D& normal, const Vector3D& uvw) {
186 Vector3D positionL = position;
188 positionL = positionL * noiseScale;
190 float noise = Noise(positionL.X, positionL.Y, zPosition);
192 return gradientMaterial->GetRGB(Vector3D(noise, 0, 0), Vector3D(), Vector3D());