Advertisement
RealDevMashup

NoiseCube Class

May 24th, 2024
484
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.37 KB | None | 0 0
  1. //Got desprate...used chat-GPT
  2.  
  3. public class NoiseCube
  4. {
  5.     private static readonly int[] perm = new int[512];
  6.     private static readonly int[] permMod12 = new int[512];
  7.     private static readonly int[] p = {151, 160, 137, 91, 90, 15,
  8.         131, 13, 201, 95, 96, 53, 194, 233, 7, 225, 140, 36, 103, 30, 69, 142, 8, 99, 37, 240, 21, 10, 23,
  9.         190, 6, 148, 247, 120, 234, 75, 0, 26, 197, 62, 94, 252, 219, 203, 117, 35, 11, 32, 57, 177, 33,
  10.         88, 237, 149, 56, 87, 174, 20, 125, 136, 171, 168, 68, 175, 74, 165, 71, 134, 139, 48, 27, 166,
  11.         77, 146, 158, 231, 83, 111, 229, 122, 60, 211, 133, 230, 220, 105, 92, 41, 55, 46, 245, 40, 244,
  12.         102, 143, 54, 65, 25, 63, 161, 1, 216, 80, 73, 209, 76, 132, 187, 208, 89, 18, 169, 200, 196,
  13.         135, 130, 116, 188, 159, 86, 164, 100, 109, 198, 173, 186, 3, 64, 52, 217, 226, 250, 124, 123, 5,
  14.         202, 38, 147, 118, 126, 255, 82, 85, 212, 207, 206, 59, 227, 47, 16, 58, 17, 182, 189, 28, 42,
  15.         223, 183, 170, 213, 119, 248, 152, 2, 44, 154, 163, 70, 221, 153, 101, 155, 167, 43, 172, 9,
  16.         129, 22, 39, 253, 19, 98, 108, 110, 79, 113, 224, 232, 178, 185, 112, 104, 218, 246, 97, 228,
  17.         251, 34, 242, 193, 238, 210, 144, 12, 191, 179, 162, 241, 81, 51, 145, 235, 249, 14, 239, 107,
  18.         49, 192, 214, 31, 181, 199, 106, 157, 184, 84, 204, 176, 115, 121, 50, 45, 127, 4, 150, 254, 138,
  19.         236, 205, 93, 222, 114, 67, 29, 24, 72, 243, 141, 128, 195, 78, 66, 215, 61, 156, 180
  20.     };
  21.  
  22.     public NoiseCube()
  23.     {
  24.         for (int i = 0; i < 512; i++)
  25.         {
  26.             perm[i] = p[i & 255];
  27.             permMod12[i] = perm[i] % 12;
  28.         }
  29.     }
  30.  
  31.     private static double Dot(int g, double x, double y, double z) {
  32.         switch (g) {
  33.             case 0: return  x + y; // (1,1,0)
  34.             case 1: return -x + y; // (-1,1,0)
  35.             case 2: return  x - y; // (1,-1,0)
  36.             case 3: return -x - y; // (-1,-1,0)
  37.             case 4: return  x + z; // (1,0,1)
  38.             case 5: return -x + z; // (-1,0,1)
  39.             case 6: return  x - z; // (1,0,-1)
  40.             case 7: return -x - z; // (-1,0,-1)
  41.             case 8: return  y + z; // (0,1,1)
  42.             case 9: return -y + z; // (0,-1,1)
  43.             case 10: return  y - z; // (0,1,-1)
  44.             case 11: return -y - z; // (0,-1,-1)
  45.             default: return 0; // Should never happen
  46.         }
  47.     }
  48.  
  49.     public float Noise(float xin, float yin, float zin)
  50.     {
  51.         double s = (xin + yin + zin) * (1.0 / 3.0); // Skew the input space to determine which simplex cell we're in
  52.         int i = FastFloor(xin + s);
  53.         int j = FastFloor(yin + s);
  54.         int k = FastFloor(zin + s);
  55.         double t = (i + j + k) * (1.0 / 6.0);
  56.         double X0 = i - t; // Unskew the cell origin back to (x,y,z) space
  57.         double Y0 = j - t;
  58.         double Z0 = k - t;
  59.         double x0 = xin - X0; // The x,y,z distances from the cell origin
  60.         double y0 = yin - Y0;
  61.         double z0 = zin - Z0;
  62.  
  63.         // For the 3D case, the simplex shape is a slightly irregular tetrahedron.
  64.         // Determine which simplex we are in.
  65.         int i1, j1, k1; // Offsets for second corner of simplex in (i,j,k) coords
  66.         int i2, j2, k2; // Offsets for third corner of simplex in (i,j,k) coords
  67.  
  68.         if (x0 >= y0)
  69.         {
  70.             if (y0 >= z0)
  71.             {
  72.                 i1 = 1; j1 = 0; k1 = 0; // X Y Z order
  73.                 i2 = 1; j2 = 1; k2 = 0;
  74.             }
  75.             else if (x0 >= z0)
  76.             {
  77.                 i1 = 1; j1 = 0; k1 = 0; // X Z Y order
  78.                 i2 = 1; j2 = 0; k2 = 1;
  79.             }
  80.             else
  81.             {
  82.                 i1 = 0; j1 = 0; k1 = 1; // Z X Y order
  83.                 i2 = 1; j2 = 0; k2 = 1;
  84.             }
  85.         }
  86.         else
  87.         {
  88.             if (y0 < z0)
  89.             {
  90.                 i1 = 0; j1 = 0; k1 = 1; // Z Y X order
  91.                 i2 = 0; j2 = 1; k2 = 1;
  92.             }
  93.             else if (x0 < z0)
  94.             {
  95.                 i1 = 0; j1 = 1; k1 = 0; // Y Z X order
  96.                 i2 = 0; j2 = 1; k2 = 1;
  97.             }
  98.             else
  99.             {
  100.                 i1 = 0; j1 = 1; k1 = 0; // Y X Z order
  101.                 i2 = 1; j2 = 1; k2 = 0;
  102.             }
  103.         }
  104.  
  105.         // A step of (1,0,0) in (i,j,k) means a step of (1-c,-c,-c) in (x,y,z), where c = 1/6.
  106.         double x1 = x0 - i1 + (1.0 / 6.0); // Offsets for second corner
  107.         double y1 = y0 - j1 + (1.0 / 6.0);
  108.         double z1 = z0 - k1 + (1.0 / 6.0);
  109.         double x2 = x0 - i2 + (1.0 / 3.0); // Offsets for third corner
  110.         double y2 = y0 - j2 + (1.0 / 3.0);
  111.         double z2 = z0 - k2 + (1.0 / 3.0);
  112.         double x3 = x0 - 1.0 + 0.5; // Offsets for last corner
  113.         double y3 = y0 - 1.0 + 0.5;
  114.         double z3 = z0 - 1.0 + 0.5;
  115.  
  116.         // Work out the hashed gradient indices of the four simplex corners
  117.         int ii = i & 255;
  118.         int jj = j & 255;
  119.         int kk = k & 255;
  120.         int gi0 = permMod12[ii + perm[jj + perm[kk]]];
  121.         int gi1 = permMod12[ii + i1 + perm[jj + j1 + perm[kk + k1]]];
  122.         int gi2 = permMod12[ii + i2 + perm[jj + j2 + perm[kk + k2]]];
  123.         int gi3 = permMod12[ii + 1 + perm[jj + 1 + perm[kk + 1]]];
  124.  
  125.         // Calculate the contribution from the four corners
  126.         double t0 = 0.6 - x0 * x0 - y0 * y0 - z0 * z0;
  127.         double n0;
  128.         if (t0 < 0) n0 = 0.0;
  129.         else {
  130.             t0 *= t0;
  131.             n0 = t0 * t0 * Dot(gi0, x0, y0, z0);
  132.         }
  133.         double t1 = 0.6 - x1 * x1 - y1 * y1 - z1 * z1;
  134.         double n1;
  135.         if (t1 < 0) n1 = 0.0;
  136.         else {
  137.             t1 *= t1;
  138.             n1 = t1 * t1 * Dot(gi1, x1, y1, z1);
  139.         }
  140.         double t2 = 0.6 - x2 * x2 - y2 * y2 - z2 * z2;
  141.         double n2;
  142.         if (t2 < 0) n2 = 0.0;
  143.         else {
  144.             t2 *= t2;
  145.             n2 = t2 * t2 * Dot(gi2, x2, y2, z2);
  146.         }
  147.         double t3 = 0.6 - x3 * x3 - y3 * y3 - z3 * z3;
  148.         double n3;
  149.         if (t3 < 0) n3 = 0.0;
  150.         else {
  151.             t3 *= t3;
  152.             n3 = t3 * t3 * Dot(gi3, x3, y3, z3);
  153.         }
  154.  
  155.         // Add contributions from each corner to get the final noise value.
  156.         // The result is scaled to return values in the interval [-1,1].
  157.         return (float)(32.0 * (n0 + n1 + n2 + n3));
  158.     }
  159.  
  160.     private static int FastFloor(double x)
  161.     {
  162.         return x > 0 ? (int)x : (int)x - 1;
  163.     }
  164. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement