ProtoTracer  1.0
Real-time 3D rendering and animation engine
Loading...
Searching...
No Matches
SimplexNoise.tpp
Go to the documentation of this file.
1#pragma once
2
3template<size_t colors>
4SimplexNoise<colors>::SimplexNoise(int seed, GradientMaterial<colors>* gradientMaterial) {
5 this->gradientMaterial = gradientMaterial;
6
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);
11
12 uint8_t temp = p[swapFrom];
13 p[swapFrom] = p[swapTo];
14 p[swapTo] = temp;
15 }
16
17 for(int i = 0; i < 512; i++){
18 perm[i]=p[i & 255];
19 permMod12[i] = perm[i] % 12;
20 }
21}
22
23// 2D simplex noise
24template<size_t colors>
25float SimplexNoise<colors>::Noise(float xin, float yin) {
26 float n0, n1, n2; // Noise contributions from the three corners
27
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);
32 float t = (i+j)*G2;
33 float X0 = i-t; // Unskew the cell origin back to (x,y) space
34 float Y0 = j-t;
35 float x0 = xin-X0; // The x,y distances from the cell origin
36 float y0 = yin-Y0;
37
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
45 // c = (3-sqrt(3))/6
46
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;
51
52 // Work out the hashed gradient indices of the three simplex corners
53 int ii = i & 255;
54 int jj = j & 255;
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]];
58
59 // Calculate the contribution from the three corners
60 float t0 = 0.5f - x0*x0-y0*y0;
61 if(t0 < 0) n0 = 0.0f;
62 else {
63 t0 *= t0;
64 n0 = t0 * t0 * grad3[gi0].DotProduct(Vector3D(x0, y0, 0));//dot(grad3[gi0], x0, y0); // (x,y) of grad3 used for 2D gradient
65 }
66 float t1 = 0.5f - x1*x1-y1*y1;
67 if(t1 < 0) n1 = 0.0f;
68 else {
69 t1 *= t1;
70 n1 = t1 * t1 * grad3[gi1].DotProduct(Vector3D(x1, y1, 0));//dot(grad3[gi1], x1, y1);
71 }
72 float t2 = 0.5f - x2*x2-y2*y2;
73 if(t2 < 0) n2 = 0.0f;
74 else {
75 t2 *= t2;
76 n2 = t2 * t2 * grad3[gi2].DotProduct(Vector3D(x2, y2, 0));//dot(grad3[gi2], x2, y2);
77 }
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);
81}
82
83// 3D simplex noise
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
87
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);
93 float t = (i+j+k)*G3;
94 float X0 = i-t; // Unskew the cell origin back to (x,y,z) space
95 float Y0 = j-t;
96 float Z0 = k-t;
97 float x0 = xin-X0; // The x,y,z distances from the cell origin
98 float y0 = yin-Y0;
99 float z0 = zin-Z0;
100
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
105
106 if(x0>=y0) {
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
110 }
111 else { // x0<y0
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
115 }
116
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
120 // c = 1/6.
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;
130
131 // Work out the hashed gradient indices of the four simplex corners
132 int ii = i & 255;
133 int jj = j & 255;
134 int kk = k & 255;
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]]];
139
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;
143 else {
144 t0 *= t0;
145 n0 = t0 * t0 * grad3[gi0].DotProduct(Vector3D(x0, y0, z0));//dot(grad3[gi0], x0, y0, z0);
146 }
147
148 float t1 = 0.6f - x1*x1 - y1*y1 - z1*z1;
149 if(t1 < 0) n1 = 0.0f;
150 else {
151 t1 *= t1;
152 n1 = t1 * t1 * grad3[gi1].DotProduct(Vector3D(x1, y1, z1));;//dot(grad3[gi1], x1, y1, z1);
153 }
154
155 float t2 = 0.6f - x2*x2 - y2*y2 - z2*z2;
156 if(t2 < 0) n2 = 0.0f;
157 else {
158 t2 *= t2;
159 n2 = t2 * t2 * grad3[gi2].DotProduct(Vector3D(x2, y2, z2));//dot(grad3[gi2], x2, y2, z2);
160 }
161
162 float t3 = 0.6f - x3*x3 - y3*y3 - z3*z3;
163 if(t3 < 0) n3 = 0.0f;
164 else {
165 t3 *= t3;
166 n3 = t3 * t3 * grad3[gi3].DotProduct(Vector3D(x3, y3, z3));//dot(grad3[gi3], x3, y3, z3);
167 }
168
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);
172}
173
174template<size_t colors>
175void SimplexNoise<colors>::SetScale(Vector3D noiseScale){
176 this->noiseScale = noiseScale;
177}
178
179template<size_t colors>
180void SimplexNoise<colors>::SetZPosition(float zPosition){
181 this->zPosition = zPosition;
182}
183
184template<size_t colors>
185RGBColor SimplexNoise<colors>::GetRGB(const Vector3D& position, const Vector3D& normal, const Vector3D& uvw) {
186 Vector3D positionL = position;
187
188 positionL = positionL * noiseScale;
189
190 float noise = Noise(positionL.X, positionL.Y, zPosition);
191
192 return gradientMaterial->GetRGB(Vector3D(noise, 0, 0), Vector3D(), Vector3D());
193}