# SimplexNoise Modual for C#

By: a guest on Mar 10th, 2012  |  syntax: C#  |  size: 24.81 KB  |  views: 353  |  expires: Never
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
1. public class SimplexNoise {  //  Simplex noise in 2D, 3D and 4D
2.         /* 2D, 3D and 4D Simplex Noise functions return 'random' values in (-1, 1).
3.
4.         This algorithm was originally designed by Ken Perlin, but my code has been
5.          adapted from the implementation written by Stefan Gustavson (stegu@itn.liu.se)
6.
7.          Raw Simplex noise functions return the value generated by Ken's algorithm.
8.
9.          Scaled Raw Simplex noise functions adjust the range of values returned from the
10.          traditional (-1, 1) to whichever bounds are passed to the function.
11.
12.          Multi-Octave Simplex noise functions compine multiple noise values to create a
13.          more complex result. Each successive layer of noise is adjusted and scaled.
14.
15.          Scaled Multi-Octave Simplex noise functions scale the values returned from the
16.          traditional (-1,1) range to whichever range is passed to the function.
17.
18.          In many cases, you may think you only need a 1D noise function, but in practice
19.          2D  is almost always better.  For instance, if you're using the current frame
20.          number  as the parameter for the noise, all objects will end up with the same
21.          noise value  at each frame. By adding a second parameter on the second
22.          dimension, you can ensure that each gets a unique noise value and they don't
23.          all look identical.
24.          */
25.
26.
27.         // 2D Multi-octave Simplex noise.
28.         //
29.         // For each octave, a higher frequency/lower amplitude function will be added to the original.
30.         // The higher the persistence [0-1], the more of each succeeding octave will be added.
31.
32.
33.         // The gradients are the midpoints of the vertices of a cube.
34.         private static int[][] grad3 = new int[][] { new int[] {1,1,0}, new int[] {-1,1,0}, new int[] {1,-1,0}, new int[] {-1,-1,0},
35.                                                      new int[] {1,0,1}, new int[] {-1,0,1}, new int[] {1,0,-1}, new int[] {-1,0,-1},
36.                                                      new int[] {0,1,1}, new int[] {0,-1,1}, new int[] {0,1,-1}, new int[] {0,-1,-1}};
37.
38.         private static int[][] grad4 = new int[][] {new int[] {0,1,1,1}, new int[] {0,1,1,-1}, new int[] {0,1,-1,1}, new int[] {0,1,-1,-1},
39.                                                     new int[] {0,-1,1,1}, new int[] {0,-1,1,-1}, new int[] {0,-1,-1,1}, new int[] {0,-1,-1,-1},
40.                                                     new int[] {1,0,1,1}, new int[] {1,0,1,-1}, new int[] {1,0,-1,1}, new int[] {1,0,-1,-1},
41.                                                     new int[] {-1,0,1,1}, new int[] {-1,0,1,-1}, new int[] {-1,0,-1,1}, new int[] {-1,0,-1,-1},
42.                                                     new int[] {1,1,0,1}, new int[] {1,1,0,-1}, new int[] {1,-1,0,1}, new int[] {1,-1,0,-1},
43.                                                     new int[] {-1,1,0,1}, new int[] {-1,1,0,-1}, new int[] {-1,-1,0,1}, new int[] {-1,-1,0,-1},
44.                                                     new int[] {1,1,1,0}, new int[] {1,1,-1,0}, new int[] {1,-1,1,0}, new int[] {1,-1,-1,0},
45.                                                     new int[] {-1,1,1,0}, new int[] {-1,1,-1,0}, new int[] {-1,-1,1,0}, new int[] {-1,-1,-1,0}};
46.
47.         private static int[] p = new int[] {151,160,137,91,90,15,
48.                                             131,13,201,95,96,53,194,233,7,225,140,36,103,30,69,142,8,99,37,240,21,10,23,
49.                                             190, 6,148,247,120,234,75,0,26,197,62,94,252,219,203,117,35,11,32,57,177,33,
50.                                             88,237,149,56,87,174,20,125,136,171,168, 68,175,74,165,71,134,139,48,27,166,
51.                                             77,146,158,231,83,111,229,122,60,211,133,230,220,105,92,41,55,46,245,40,244,
52.                                             102,143,54, 65,25,63,161, 1,216,80,73,209,76,132,187,208, 89,18,169,200,196,
53.                                             135,130,116,188,159,86,164,100,109,198,173,186, 3,64,52,217,226,250,124,123,
54.                                             5,202,38,147,118,126,255,82,85,212,207,206,59,227,47,16,58,17,182,189,28,42,
55.                                             223,183,170,213,119,248,152, 2,44,154,163, 70,221,153,101,155,167, 43,172,9,
56.                                             129,22,39,253, 19,98,108,110,79,113,224,232,178,185, 112,104,218,246,97,228,
57.                                             251,34,242,193,238,210,144,12,191,179,162,241, 81,51,145,235,249,14,239,107,
58.                                             49,192,214, 31,181,199,106,157,184, 84,204,176,115,121,50,45,127, 4,150,254,
59.                                             138,236,205,93,222,114,67,29,24,72,243,141,128,195,78,66,215,61,156,180};
60.         //  To remove the need for index wrapping, double the permutation table length
61.         private static int[] perm = new int[512];
62.
63.         //  A lookup table to traverse the simplex around a given point in 4D.
64.         //  Details can be found where this table is used, in the 4D noise method.
65.         private static int[,] simplex = new int[,] {{0,1,2,3},{0,1,3,2},{0,0,0,0},{0,2,3,1},{0,0,0,0},{0,0,0,0},{0,0,0,0},{1,2,3,0},
66.                                                     {0,2,1,3},{0,0,0,0},{0,3,1,2},{0,3,2,1},{0,0,0,0},{0,0,0,0},{0,0,0,0},{1,3,2,0},
67.                                                     {0,0,0,0},{0,0,0,0},{0,0,0,0},{0,0,0,0},{0,0,0,0},{0,0,0,0},{0,0,0,0},{0,0,0,0},
68.                                                     {1,2,0,3},{0,0,0,0},{1,3,0,2},{0,0,0,0},{0,0,0,0},{0,0,0,0},{2,3,0,1},{2,3,1,0},
69.                                                     {1,0,2,3},{1,0,3,2},{0,0,0,0},{0,0,0,0},{0,0,0,0},{2,0,3,1},{0,0,0,0},{2,1,3,0},
70.                                                     {0,0,0,0},{0,0,0,0},{0,0,0,0},{0,0,0,0},{0,0,0,0},{0,0,0,0},{0,0,0,0},{0,0,0,0},
71.                                                     {2,0,1,3},{0,0,0,0},{0,0,0,0},{0,0,0,0},{3,0,1,2},{3,0,2,1},{0,0,0,0},{3,1,2,0},
72.                                                     {2,1,0,3},{0,0,0,0},{0,0,0,0},{0,0,0,0},{3,1,0,2},{0,0,0,0},{3,2,0,1},{3,2,1,0}};
73.
74.         public float octave_noise_2d(float octaves, float persistence, float scale, float x, float y) {
75.             float total = 0;
76.             float frequency = scale;
77.             float amplitude = 1;
78.
79.             // We have to keep track of the largest possible amplitude,
80.             // because each octave adds more, and we need a value in [-1, 1].
81.             float maxAmplitude = 0;
82.
83.             for (int i = 0; i < octaves; i++) {
84.                 total += raw_noise_2d(x * frequency, y * frequency) * amplitude;
85.
86.                 frequency *= 2;
87.                 maxAmplitude += amplitude;
88.                 amplitude *= persistence;
89.             }
90.
92.         }
93.
94.
95.         // 3D Multi-octave Simplex noise.
96.         //
97.         // For each octave, a higher frequency/lower amplitude function will be added to the original.
98.         // The higher the persistence [0-1], the more of each succeeding octave will be added.
99.         public float octave_noise_3d(float octaves, float persistence, float scale, float x, float y, float z) {
100.             float total = 0;
101.             float frequency = scale;
102.             float amplitude = 1;
103.
104.             // We have to keep track of the largest possible amplitude,
105.             // because each octave adds more, and we need a value in [-1, 1].
106.             float maxAmplitude = 0;
107.
108.             for (int i = 0; i < octaves; i++) {
109.                 total += raw_noise_3d(x * frequency, y * frequency, z * frequency) * amplitude;
110.
111.                 frequency *= 2;
112.                 maxAmplitude += amplitude;
113.                 amplitude *= persistence;
114.             }
115.
117.         }
118.
119.
120.         // 4D Multi-octave Simplex noise.
121.         //
122.         // For each octave, a higher frequency/lower amplitude function will be added to the original.
123.         // The higher the persistence [0-1], the more of each succeeding octave will be added.
124.         public float octave_noise_4d(float octaves, float persistence, float scale, float x, float y, float z, float w) {
125.             float total = 0;
126.             float frequency = scale;
127.             float amplitude = 1;
128.
129.             // We have to keep track of the largest possible amplitude,
130.             // because each octave adds more, and we need a value in [-1, 1].
131.             float maxAmplitude = 0;
132.
133.             for (int i = 0; i < octaves; i++) {
134.                 total += raw_noise_4d(x * frequency, y * frequency, z * frequency, w * frequency) * amplitude;
135.
136.                 frequency *= 2;
137.                 maxAmplitude += amplitude;
138.                 amplitude *= persistence;
139.             }
140.
142.         }
143.
144.
145.
146.         // 2D Scaled Multi-octave Simplex noise.
147.         //
148.         // Returned value will be between loBound and hiBound.
149.         public float scaled_octave_noise_2d(float octaves, float persistence, float scale, float loBound, float hiBound, float x, float y) {
150.             return octave_noise_2d(octaves, persistence, scale, x, y) * (hiBound - loBound) / 2 + (hiBound + loBound) / 2;
151.         }
152.
153.
154.         // 3D Scaled Multi-octave Simplex noise.
155.         //
156.         // Returned value will be between loBound and hiBound.
157.         public float scaled_octave_noise_3d(float octaves, float persistence, float scale, float loBound, float hiBound, float x, float y, float z) {
158.             return octave_noise_3d(octaves, persistence, scale, x, y, z) * (hiBound - loBound) / 2 + (hiBound + loBound) / 2;
159.         }
160.
161.         // 4D Scaled Multi-octave Simplex noise.
162.         //
163.         // Returned value will be between loBound and hiBound.
164.         public float scaled_octave_noise_4d(float octaves, float persistence, float scale, float loBound, float hiBound, float x, float y, float z, float w) {
165.             return octave_noise_4d(octaves, persistence, scale, x, y, z, w) * (hiBound - loBound) / 2 + (hiBound + loBound) / 2;
166.         }
167.
168.
169.
170.         // 2D Scaled Simplex raw noise.
171.         //
172.         // Returned value will be between loBound and hiBound.
173.         public float scaled_raw_noise_2d(float loBound, float hiBound, float x, float y) {
174.             return raw_noise_2d(x, y) * (hiBound - loBound) / 2 + (hiBound + loBound) / 2;
175.         }
176.
177.
178.         // 3D Scaled Simplex raw noise.
179.         //
180.         // Returned value will be between loBound and hiBound.
181.         public float scaled_raw_noise_3d(float loBound, float hiBound, float x, float y, float z) {
182.             return raw_noise_3d(x, y, z) * (hiBound - loBound) / 2 + (hiBound + loBound) / 2;
183.         }
184.
185.         // 4D Scaled Simplex raw noise.
186.         //
187.         // Returned value will be between loBound and hiBound.
188.         public float scaled_raw_noise_4d(float loBound, float hiBound, float x, float y, float z, float w) {
189.             return raw_noise_4d(x, y, z, w) * (hiBound - loBound) / 2 + (hiBound + loBound) / 2;
190.         }
191.
192.
193.
194.         // 2D raw Simplex noise
195.         public float raw_noise_2d(float x, float y) {
196.             // Noise contributions from the three corners
197.             float n0, n1, n2;
198.
199.             // Skew the input space to determine which simplex cell we're in
200.             float F2 = 0.5f * ((float)Math.Sqrt(3.0f) - 1.0f);
201.             // Hairy factor for 2D
202.             float s = (x + y) * F2;
203.             int i = fastfloor(x + s);
204.             int j = fastfloor(y + s);
205.
206.             float G2 = (3.0f - (float)Math.Sqrt(3.0f)) / 6.0f;
207.             float t = (i + j) * G2;
208.             // Unskew the cell origin back to (x,y) space
209.             float X0 = i - t;
210.             float Y0 = j - t;
211.             // The x,y distances from the cell origin
212.             float x0 = x - X0;
213.             float y0 = y - Y0;
214.
215.             // For the 2D case, the simplex shape is an equilateral triangle.
216.             // Determine which simplex we are in.
217.             int i1, j1; // Offsets for second (middle) corner of simplex in (i,j) coords
218.             if (x0 > y0) { i1 = 1; j1 = 0; } // lower triangle, XY order: (0,0)->(1,0)->(1,1)
219.             else { i1 = 0; j1 = 1; } // upper triangle, YX order: (0,0)->(0,1)->(1,1)
220.
221.             // A step of (1,0) in (i,j) means a step of (1-c,-c) in (x,y), and
222.             // a step of (0,1) in (i,j) means a step of (-c,1-c) in (x,y), where
223.             // c = (3-sqrt(3))/6
224.             float x1 = x0 - i1 + G2; // Offsets for middle corner in (x,y) unskewed coords
225.             float y1 = y0 - j1 + G2;
226.             float x2 = x0 - 1.0f + 2.0f * G2; // Offsets for last corner in (x,y) unskewed coords
227.             float y2 = y0 - 1.0f + 2.0f * G2;
228.
229.             // Work out the hashed gradient indices of the three simplex corners
230.             int ii = i & 255;
231.             int jj = j & 255;
232.             int gi0 = perm[ii + perm[jj]] % 12;
233.             int gi1 = perm[ii + i1 + perm[jj + j1]] % 12;
234.             int gi2 = perm[ii + 1 + perm[jj + 1]] % 12;
235.
236.             // Calculate the contribution from the three corners
237.             float t0 = 0.5f - x0 * x0 - y0 * y0;
238.             if (t0 < 0) n0 = 0.0f;
239.             else {
240.                 t0 *= t0;
241.                 n0 = t0 * t0 * dot(grad3[gi0], x0, y0); // (x,y) of grad3 used for 2D gradient
242.             }
243.
244.             float t1 = 0.5f - x1 * x1 - y1 * y1;
245.             if (t1 < 0) n1 = 0.0f;
246.             else {
247.                 t1 *= t1;
248.                 n1 = t1 * t1 * dot(grad3[gi1], x1, y1);
249.             }
250.
251.             float t2 = 0.5f - x2 * x2 - y2 * y2;
252.             if (t2 < 0) n2 = 0.0f;
253.             else {
254.                 t2 *= t2;
255.                 n2 = t2 * t2 * dot(grad3[gi2], x2, y2);
256.             }
257.
258.             // Add contributions from each corner to get the final noise value.
259.             // The result is scaled to return values in the interval [-1,1].
260.             return 70.0f * (n0 + n1 + n2);
261.         }
262.
263.
264.         // 3D raw Simplex noise
265.         public float raw_noise_3d(float x, float y, float z) {
266.             float n0, n1, n2, n3; // Noise contributions from the four corners
267.
268.             // Skew the input space to determine which simplex cell we're in
269.             float F3 = 1.0f / 3.0f;
270.             float s = (x + y + z) * F3; // Very nice and simple skew factor for 3D
271.             int i = fastfloor(x + s);
272.             int j = fastfloor(y + s);
273.             int k = fastfloor(z + s);
274.
275.             float G3 = 1.0f / 6.0f; // Very nice and simple unskew factor, too
276.             float t = (i + j + k) * G3;
277.             float X0 = i - t; // Unskew the cell origin back to (x,y,z) space
278.             float Y0 = j - t;
279.             float Z0 = k - t;
280.             float x0 = x - X0; // The x,y,z distances from the cell origin
281.             float y0 = y - Y0;
282.             float z0 = z - Z0;
283.
284.             // For the 3D case, the simplex shape is a slightly irregular tetrahedron.
285.             // Determine which simplex we are in.
286.             int i1, j1, k1; // Offsets for second corner of simplex in (i,j,k) coords
287.             int i2, j2, k2; // Offsets for third corner of simplex in (i,j,k) coords
288.
289.             if (x0 >= y0) {
290.                 if (y0 >= z0) { i1 = 1; j1 = 0; k1 = 0; i2 = 1; j2 = 1; k2 = 0; } // X Y Z order
291.                 else if (x0 >= z0) { i1 = 1; j1 = 0; k1 = 0; i2 = 1; j2 = 0; k2 = 1; } // X Z Y order
292.                 else { i1 = 0; j1 = 0; k1 = 1; i2 = 1; j2 = 0; k2 = 1; } // Z X Y order
293.             }
294.             else { // x0<y0
295.                 if (y0 < z0) { i1 = 0; j1 = 0; k1 = 1; i2 = 0; j2 = 1; k2 = 1; } // Z Y X order
296.                 else if (x0 < z0) { i1 = 0; j1 = 1; k1 = 0; i2 = 0; j2 = 1; k2 = 1; } // Y Z X order
297.                 else { i1 = 0; j1 = 1; k1 = 0; i2 = 1; j2 = 1; k2 = 0; } // Y X Z order
298.             }
299.
300.             // A step of (1,0,0) in (i,j,k) means a step of (1-c,-c,-c) in (x,y,z),
301.             // a step of (0,1,0) in (i,j,k) means a step of (-c,1-c,-c) in (x,y,z), and
302.             // a step of (0,0,1) in (i,j,k) means a step of (-c,-c,1-c) in (x,y,z), where
303.             // c = 1/6.
304.             float x1 = x0 - i1 + G3; // Offsets for second corner in (x,y,z) coords
305.             float y1 = y0 - j1 + G3;
306.             float z1 = z0 - k1 + G3;
307.             float x2 = x0 - i2 + 2.0f * G3; // Offsets for third corner in (x,y,z) coords
308.             float y2 = y0 - j2 + 2.0f * G3;
309.             float z2 = z0 - k2 + 2.0f * G3;
310.             float x3 = x0 - 1.0f + 3.0f * G3; // Offsets for last corner in (x,y,z) coords
311.             float y3 = y0 - 1.0f + 3.0f * G3;
312.             float z3 = z0 - 1.0f + 3.0f * G3;
313.
314.             // Work out the hashed gradient indices of the four simplex corners
315.             int ii = i & 255;
316.             int jj = j & 255;
317.             int kk = k & 255;
318.             int gi0 = perm[ii + perm[jj + perm[kk]]] % 12;
319.             int gi1 = perm[ii + i1 + perm[jj + j1 + perm[kk + k1]]] % 12;
320.             int gi2 = perm[ii + i2 + perm[jj + j2 + perm[kk + k2]]] % 12;
321.             int gi3 = perm[ii + 1 + perm[jj + 1 + perm[kk + 1]]] % 12;
322.
323.             // Calculate the contribution from the four corners
324.             float t0 = 0.6f - x0 * x0 - y0 * y0 - z0 * z0;
325.             if (t0 < 0) n0 = 0.0f;
326.             else {
327.                 t0 *= t0;
328.                 n0 = t0 * t0 * dot(grad3[gi0], x0, y0, z0);
329.             }
330.
331.             float t1 = 0.6f - x1 * x1 - y1 * y1 - z1 * z1;
332.             if (t1 < 0) n1 = 0.0f;
333.             else {
334.                 t1 *= t1;
335.                 n1 = t1 * t1 * dot(grad3[gi1], x1, y1, z1);
336.             }
337.
338.             float t2 = 0.6f - x2 * x2 - y2 * y2 - z2 * z2;
339.             if (t2 < 0) n2 = 0.0f;
340.             else {
341.                 t2 *= t2;
342.                 n2 = t2 * t2 * dot(grad3[gi2], x2, y2, z2);
343.             }
344.
345.             float t3 = 0.6f - x3 * x3 - y3 * y3 - z3 * z3;
346.             if (t3 < 0) n3 = 0.0f;
347.             else {
348.                 t3 *= t3;
349.                 n3 = t3 * t3 * dot(grad3[gi3], x3, y3, z3);
350.             }
351.
352.             // Add contributions from each corner to get the final noise value.
353.             // The result is scaled to stay just inside [-1,1]
354.             return 32.0f * (n0 + n1 + n2 + n3);
355.         }
356.
357.
358.         // 4D raw Simplex noise
359.         public float raw_noise_4d(float x, float y, float z, float w) {
360.             // The skewing and unskewing factors are hairy again for the 4D case
361.             float F4 = ((float)Math.Sqrt(5.0f) - 1.0f) / 4.0f;
362.             float G4 = (5.0f - (float)Math.Sqrt(5.0f)) / 20.0f;
363.             float n0, n1, n2, n3, n4; // Noise contributions from the five corners
364.
365.             // Skew the (x,y,z,w) space to determine which cell of 24 simplices we're in
366.             float s = (x + y + z + w) * F4; // Factor for 4D skewing
367.             int i = fastfloor(x + s);
368.             int j = fastfloor(y + s);
369.             int k = fastfloor(z + s);
370.             int l = fastfloor(w + s);
371.             float t = (i + j + k + l) * G4; // Factor for 4D unskewing
372.             float X0 = i - t; // Unskew the cell origin back to (x,y,z,w) space
373.             float Y0 = j - t;
374.             float Z0 = k - t;
375.             float W0 = l - t;
376.
377.             float x0 = x - X0; // The x,y,z,w distances from the cell origin
378.             float y0 = y - Y0;
379.             float z0 = z - Z0;
380.             float w0 = w - W0;
381.
382.             // For the 4D case, the simplex is a 4D shape I won't even try to describe.
383.             // To find out which of the 24 possible simplices we're in, we need to
384.             // determine the magnitude ordering of x0, y0, z0 and w0.
385.             // The method below is a good way of finding the ordering of x,y,z,w and
386.             // then find the correct traversal order for the simplex we're in.
387.             // First, six pair-wise comparisons are performed between each possible pair
388.             // of the four coordinates, and the results are used to add up binary bits
389.             // for an integer index.
390.             int c1 = (x0 > y0) ? 32 : 0;
391.             int c2 = (x0 > z0) ? 16 : 0;
392.             int c3 = (y0 > z0) ? 8 : 0;
393.             int c4 = (x0 > w0) ? 4 : 0;
394.             int c5 = (y0 > w0) ? 2 : 0;
395.             int c6 = (z0 > w0) ? 1 : 0;
396.             int c = c1 + c2 + c3 + c4 + c5 + c6;
397.
398.             int i1, j1, k1, l1; // The integer offsets for the second simplex corner
399.             int i2, j2, k2, l2; // The integer offsets for the third simplex corner
400.             int i3, j3, k3, l3; // The integer offsets for the fourth simplex corner
401.
402.             // simplex[c] is a 4-vector with the numbers 0, 1, 2 and 3 in some order.
403.             // Many values of c will never occur, since e.g. x>y>z>w makes x<z, y<w and x<w
404.             // impossible. Only the 24 indices which have non-zero entries make any sense.
405.             // We use a thresholding to set the coordinates in turn from the largest magnitude.
406.             // The number 3 in the "simplex" array is at the position of the largest coordinate.
407.             i1 = simplex[c, 0] >= 3 ? 1 : 0;
408.             j1 = simplex[c, 1] >= 3 ? 1 : 0;
409.             k1 = simplex[c, 2] >= 3 ? 1 : 0;
410.             l1 = simplex[c, 3] >= 3 ? 1 : 0;
411.             // The number 2 in the "simplex" array is at the second largest coordinate.
412.             i2 = simplex[c, 0] >= 2 ? 1 : 0;
413.             j2 = simplex[c, 1] >= 2 ? 1 : 0;
414.             k2 = simplex[c, 2] >= 2 ? 1 : 0;
415.             l2 = simplex[c, 3] >= 2 ? 1 : 0;
416.             // The number 1 in the "simplex" array is at the second smallest coordinate.
417.             i3 = simplex[c, 0] >= 1 ? 1 : 0;
418.             j3 = simplex[c, 1] >= 1 ? 1 : 0;
419.             k3 = simplex[c, 2] >= 1 ? 1 : 0;
420.             l3 = simplex[c, 3] >= 1 ? 1 : 0;
421.             // The fifth corner has all coordinate offsets = 1, so no need to look that up.
422.
423.             float x1 = x0 - i1 + G4; // Offsets for second corner in (x,y,z,w) coords
424.             float y1 = y0 - j1 + G4;
425.             float z1 = z0 - k1 + G4;
426.             float w1 = w0 - l1 + G4;
427.             float x2 = x0 - i2 + 2.0f * G4; // Offsets for third corner in (x,y,z,w) coords
428.             float y2 = y0 - j2 + 2.0f * G4;
429.             float z2 = z0 - k2 + 2.0f * G4;
430.             float w2 = w0 - l2 + 2.0f * G4;
431.             float x3 = x0 - i3 + 3.0f * G4; // Offsets for fourth corner in (x,y,z,w) coords
432.             float y3 = y0 - j3 + 3.0f * G4;
433.             float z3 = z0 - k3 + 3.0f * G4;
434.             float w3 = w0 - l3 + 3.0f * G4;
435.             float x4 = x0 - 1.0f + 4.0f * G4; // Offsets for last corner in (x,y,z,w) coords
436.             float y4 = y0 - 1.0f + 4.0f * G4;
437.             float z4 = z0 - 1.0f + 4.0f * G4;
438.             float w4 = w0 - 1.0f + 4.0f * G4;
439.
440.             // Work out the hashed gradient indices of the five simplex corners
441.             int ii = i & 255;
442.             int jj = j & 255;
443.             int kk = k & 255;
444.             int ll = l & 255;
445.             int gi0 = perm[ii + perm[jj + perm[kk + perm[ll]]]] % 32;
446.             int gi1 = perm[ii + i1 + perm[jj + j1 + perm[kk + k1 + perm[ll + l1]]]] % 32;
447.             int gi2 = perm[ii + i2 + perm[jj + j2 + perm[kk + k2 + perm[ll + l2]]]] % 32;
448.             int gi3 = perm[ii + i3 + perm[jj + j3 + perm[kk + k3 + perm[ll + l3]]]] % 32;
449.             int gi4 = perm[ii + 1 + perm[jj + 1 + perm[kk + 1 + perm[ll + 1]]]] % 32;
450.
451.             // Calculate the contribution from the five corners
452.             float t0 = 0.6f - x0 * x0 - y0 * y0 - z0 * z0 - w0 * w0;
453.             if (t0 < 0) n0 = 0.0f;
454.             else {
455.                 t0 *= t0;
456.                 n0 = t0 * t0 * dot(grad4[gi0], x0, y0, z0, w0);
457.             }
458.
459.             float t1 = 0.6f - x1 * x1 - y1 * y1 - z1 * z1 - w1 * w1;
460.             if (t1 < 0) n1 = 0.0f;
461.             else {
462.                 t1 *= t1;
463.                 n1 = t1 * t1 * dot(grad4[gi1], x1, y1, z1, w1);
464.             }
465.
466.             float t2 = 0.6f - x2 * x2 - y2 * y2 - z2 * z2 - w2 * w2;
467.             if (t2 < 0) n2 = 0.0f;
468.             else {
469.                 t2 *= t2;
470.                 n2 = t2 * t2 * dot(grad4[gi2], x2, y2, z2, w2);
471.             }
472.
473.             float t3 = 0.6f - x3 * x3 - y3 * y3 - z3 * z3 - w3 * w3;
474.             if (t3 < 0) n3 = 0.0f;
475.             else {
476.                 t3 *= t3;
477.                 n3 = t3 * t3 * dot(grad4[gi3], x3, y3, z3, w3);
478.             }
479.
480.             float t4 = 0.6f - x4 * x4 - y4 * y4 - z4 * z4 - w4 * w4;
481.             if (t4 < 0) n4 = 0.0f;
482.             else {
483.                 t4 *= t4;
484.                 n4 = t4 * t4 * dot(grad4[gi4], x4, y4, z4, w4);
485.             }
486.
487.             // Sum up and scale the result to cover the range [-1,1]
488.             return 27.0f * (n0 + n1 + n2 + n3 + n4);
489.         }
490.
491.
492.         int fastfloor(float x) { return x > 0 ? (int)x : (int)x - 1; }
493.
494.         unsafe float dot(int[] g, float x, float y) { return g[0] * x + g[1] * y; }
495.         unsafe float dot(int[] g, float x, float y, float z) { return g[0] * x + g[1] * y + g[2] * z; }
496.         unsafe float dot(int[] g, float x, float y, float z, float w) { return g[0] * x + g[1] * y + g[2] * z + g[3] * w; }
497.
498.     }
clone this paste RAW Paste Data
Top