Advertisement
Guest User

SimplexNoise C#

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