Guest User

Untitled

a guest
Jul 21st, 2015
394
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.  * A speed-improved perlin and simplex noise algorithms for 2D.
  3.  *
  4.  * Based on example code by Stefan Gustavson ([email protected]).
  5.  * Optimisations by Peter Eastman ([email protected]).
  6.  * Better rank ordering method by Stefan Gustavson in 2012.
  7.  * Converted to Javascript by Joseph Gentle.
  8.  *
  9.  * Version 2012-03-09
  10.  *
  11.  * This code was placed in the public domain by its original author,
  12.  * Stefan Gustavson. You may use it as you see fit, but
  13.  * attribution is appreciated.
  14.  *
  15.  */
  16.  
  17. function NoiseObject(){
  18.  
  19.   function Grad(x, y, z) {
  20.     this.x = x; this.y = y; this.z = z;
  21.   }
  22.  
  23.   Grad.prototype.dot2 = function(x, y) {
  24.     return this.x*x + this.y*y;
  25.   };
  26.  
  27.   Grad.prototype.dot3 = function(x, y, z) {
  28.     return this.x*x + this.y*y + this.z*z;
  29.   };
  30.  
  31.   var grad3 = [new Grad(1,1,0),new Grad(-1,1,0),new Grad(1,-1,0),new Grad(-1,-1,0),
  32.                new Grad(1,0,1),new Grad(-1,0,1),new Grad(1,0,-1),new Grad(-1,0,-1),
  33.                new Grad(0,1,1),new Grad(0,-1,1),new Grad(0,1,-1),new Grad(0,-1,-1)];
  34.  
  35.   var p = [151,160,137,91,90,15,
  36.   131,13,201,95,96,53,194,233,7,225,140,36,103,30,69,142,8,99,37,240,21,10,23,
  37.   190, 6,148,247,120,234,75,0,26,197,62,94,252,219,203,117,35,11,32,57,177,33,
  38.   88,237,149,56,87,174,20,125,136,171,168, 68,175,74,165,71,134,139,48,27,166,
  39.   77,146,158,231,83,111,229,122,60,211,133,230,220,105,92,41,55,46,245,40,244,
  40.   102,143,54, 65,25,63,161, 1,216,80,73,209,76,132,187,208, 89,18,169,200,196,
  41.   135,130,116,188,159,86,164,100,109,198,173,186, 3,64,52,217,226,250,124,123,
  42.   5,202,38,147,118,126,255,82,85,212,207,206,59,227,47,16,58,17,182,189,28,42,
  43.   223,183,170,213,119,248,152, 2,44,154,163, 70,221,153,101,155,167, 43,172,9,
  44.   129,22,39,253, 19,98,108,110,79,113,224,232,178,185, 112,104,218,246,97,228,
  45.   251,34,242,193,238,210,144,12,191,179,162,241, 81,51,145,235,249,14,239,107,
  46.   49,192,214, 31,181,199,106,157,184, 84,204,176,115,121,50,45,127, 4,150,254,
  47.   138,236,205,93,222,114,67,29,24,72,243,141,128,195,78,66,215,61,156,180];
  48.   // To remove the need for index wrapping, double the permutation table length
  49.   var perm = new Array(512);
  50.   var gradP = new Array(512);
  51.  
  52.   // This isn't a very good seeding function, but it works ok. It supports 2^16
  53.   // different seed values. Write something better if you need more seeds.
  54.   this.seed = function(seed) {
  55.     if(seed > 0 && seed < 1) {
  56.       // Scale the seed out
  57.       seed *= 65536;
  58.     }
  59.  
  60.     seed = Math.floor(seed);
  61.     if(seed < 256) {
  62.       seed |= seed << 8;
  63.     }
  64.  
  65.     for(var i = 0; i < 256; i++) {
  66.       var v;
  67.       if (i & 1) {
  68.         v = p[i] ^ (seed & 255);
  69.       } else {
  70.         v = p[i] ^ ((seed>>8) & 255);
  71.       }
  72.  
  73.       perm[i] = perm[i + 256] = v;
  74.       gradP[i] = gradP[i + 256] = grad3[v % 12];
  75.     }
  76.   };
  77.  
  78.   this.seed(0);
  79.  
  80.   /*
  81.   for(var i=0; i<256; i++) {
  82.     perm[i] = perm[i + 256] = p[i];
  83.     gradP[i] = gradP[i + 256] = grad3[perm[i] % 12];
  84.   }*/
  85.  
  86.   // Skewing and unskewing factors for 2, 3, and 4 dimensions
  87.   var F2 = 0.5*(Math.sqrt(3)-1);
  88.   var G2 = (3-Math.sqrt(3))/6;
  89.  
  90.   var F3 = 1/3;
  91.   var G3 = 1/6;
  92.  
  93.   // 2D simplex noise
  94.   this.simplex2 = function(xin, yin) {
  95.     var n0, n1, n2; // Noise contributions from the three corners
  96.     // Skew the input space to determine which simplex cell we're in
  97.     var s = (xin+yin)*F2; // Hairy factor for 2D
  98.     var i = Math.floor(xin+s);
  99.     var j = Math.floor(yin+s);
  100.     var t = (i+j)*G2;
  101.     var x0 = xin-i+t; // The x,y distances from the cell origin, unskewed.
  102.     var y0 = yin-j+t;
  103.     // For the 2D case, the simplex shape is an equilateral triangle.
  104.     // Determine which simplex we are in.
  105.     var i1, j1; // Offsets for second (middle) corner of simplex in (i,j) coords
  106.     if(x0>y0) { // lower triangle, XY order: (0,0)->(1,0)->(1,1)
  107.       i1=1; j1=0;
  108.     } else {    // upper triangle, YX order: (0,0)->(0,1)->(1,1)
  109.       i1=0; j1=1;
  110.     }
  111.     // A step of (1,0) in (i,j) means a step of (1-c,-c) in (x,y), and
  112.     // a step of (0,1) in (i,j) means a step of (-c,1-c) in (x,y), where
  113.     // c = (3-sqrt(3))/6
  114.     var x1 = x0 - i1 + G2; // Offsets for middle corner in (x,y) unskewed coords
  115.     var y1 = y0 - j1 + G2;
  116.     var x2 = x0 - 1 + 2 * G2; // Offsets for last corner in (x,y) unskewed coords
  117.     var y2 = y0 - 1 + 2 * G2;
  118.     // Work out the hashed gradient indices of the three simplex corners
  119.     i &= 255;
  120.     j &= 255;
  121.     var gi0 = gradP[i+perm[j]];
  122.     var gi1 = gradP[i+i1+perm[j+j1]];
  123.     var gi2 = gradP[i+1+perm[j+1]];
  124.     // Calculate the contribution from the three corners
  125.     var t0 = 0.5 - x0*x0-y0*y0;
  126.     if(t0<0) {
  127.       n0 = 0;
  128.     } else {
  129.       t0 *= t0;
  130.       n0 = t0 * t0 * gi0.dot2(x0, y0);  // (x,y) of grad3 used for 2D gradient
  131.     }
  132.     var t1 = 0.5 - x1*x1-y1*y1;
  133.     if(t1<0) {
  134.       n1 = 0;
  135.     } else {
  136.       t1 *= t1;
  137.       n1 = t1 * t1 * gi1.dot2(x1, y1);
  138.     }
  139.     var t2 = 0.5 - x2*x2-y2*y2;
  140.     if(t2<0) {
  141.       n2 = 0;
  142.     } else {
  143.       t2 *= t2;
  144.       n2 = t2 * t2 * gi2.dot2(x2, y2);
  145.     }
  146.     // Add contributions from each corner to get the final noise value.
  147.     // The result is scaled to return values in the interval [-1,1].
  148.     return 70 * (n0 + n1 + n2);
  149.   };
  150.  
  151.   // 3D simplex noise
  152.   this.simplex3 = function(xin, yin, zin) {
  153.     var n0, n1, n2, n3; // Noise contributions from the four corners
  154.  
  155.     // Skew the input space to determine which simplex cell we're in
  156.     var s = (xin+yin+zin)*F3; // Hairy factor for 2D
  157.     var i = Math.floor(xin+s);
  158.     var j = Math.floor(yin+s);
  159.     var k = Math.floor(zin+s);
  160.  
  161.     var t = (i+j+k)*G3;
  162.     var x0 = xin-i+t; // The x,y distances from the cell origin, unskewed.
  163.     var y0 = yin-j+t;
  164.     var z0 = zin-k+t;
  165.  
  166.     // For the 3D case, the simplex shape is a slightly irregular tetrahedron.
  167.     // Determine which simplex we are in.
  168.     var i1, j1, k1; // Offsets for second corner of simplex in (i,j,k) coords
  169.     var i2, j2, k2; // Offsets for third corner of simplex in (i,j,k) coords
  170.     if(x0 >= y0) {
  171.       if(y0 >= z0)      { i1=1; j1=0; k1=0; i2=1; j2=1; k2=0; }
  172.       else if(x0 >= z0) { i1=1; j1=0; k1=0; i2=1; j2=0; k2=1; }
  173.       else              { i1=0; j1=0; k1=1; i2=1; j2=0; k2=1; }
  174.     } else {
  175.       if(y0 < z0)      { i1=0; j1=0; k1=1; i2=0; j2=1; k2=1; }
  176.       else if(x0 < z0) { i1=0; j1=1; k1=0; i2=0; j2=1; k2=1; }
  177.       else             { i1=0; j1=1; k1=0; i2=1; j2=1; k2=0; }
  178.     }
  179.     // A step of (1,0,0) in (i,j,k) means a step of (1-c,-c,-c) in (x,y,z),
  180.     // a step of (0,1,0) in (i,j,k) means a step of (-c,1-c,-c) in (x,y,z), and
  181.     // a step of (0,0,1) in (i,j,k) means a step of (-c,-c,1-c) in (x,y,z), where
  182.     // c = 1/6.
  183.     var x1 = x0 - i1 + G3; // Offsets for second corner
  184.     var y1 = y0 - j1 + G3;
  185.     var z1 = z0 - k1 + G3;
  186.  
  187.     var x2 = x0 - i2 + 2 * G3; // Offsets for third corner
  188.     var y2 = y0 - j2 + 2 * G3;
  189.     var z2 = z0 - k2 + 2 * G3;
  190.  
  191.     var x3 = x0 - 1 + 3 * G3; // Offsets for fourth corner
  192.     var y3 = y0 - 1 + 3 * G3;
  193.     var z3 = z0 - 1 + 3 * G3;
  194.  
  195.     // Work out the hashed gradient indices of the four simplex corners
  196.     i &= 255;
  197.     j &= 255;
  198.     k &= 255;
  199.     var gi0 = gradP[i+   perm[j+   perm[k   ]]];
  200.     var gi1 = gradP[i+i1+perm[j+j1+perm[k+k1]]];
  201.     var gi2 = gradP[i+i2+perm[j+j2+perm[k+k2]]];
  202.     var gi3 = gradP[i+ 1+perm[j+ 1+perm[k+ 1]]];
  203.  
  204.     // Calculate the contribution from the four corners
  205.     var t0 = 0.5 - x0*x0-y0*y0-z0*z0;
  206.     if(t0<0) {
  207.       n0 = 0;
  208.     } else {
  209.       t0 *= t0;
  210.       n0 = t0 * t0 * gi0.dot3(x0, y0, z0);  // (x,y) of grad3 used for 2D gradient
  211.     }
  212.     var t1 = 0.5 - x1*x1-y1*y1-z1*z1;
  213.     if(t1<0) {
  214.       n1 = 0;
  215.     } else {
  216.       t1 *= t1;
  217.       n1 = t1 * t1 * gi1.dot3(x1, y1, z1);
  218.     }
  219.     var t2 = 0.5 - x2*x2-y2*y2-z2*z2;
  220.     if(t2<0) {
  221.       n2 = 0;
  222.     } else {
  223.       t2 *= t2;
  224.       n2 = t2 * t2 * gi2.dot3(x2, y2, z2);
  225.     }
  226.     var t3 = 0.5 - x3*x3-y3*y3-z3*z3;
  227.     if(t3<0) {
  228.       n3 = 0;
  229.     } else {
  230.       t3 *= t3;
  231.       n3 = t3 * t3 * gi3.dot3(x3, y3, z3);
  232.     }
  233.     // Add contributions from each corner to get the final noise value.
  234.     // The result is scaled to return values in the interval [-1,1].
  235.     return 32 * (n0 + n1 + n2 + n3);
  236.  
  237.   };
  238.  
  239.   // ##### Perlin noise stuff
  240.  
  241.   function fade(t) {
  242.     return t*t*t*(t*(t*6-15)+10);
  243.   }
  244.  
  245.   function lerp(a, b, t) {
  246.     return (1-t)*a + t*b;
  247.   }
  248.  
  249.   // 2D Perlin Noise
  250.   this.perlin2 = function(x, y) {
  251.     // Find unit grid cell containing point
  252.     var X = Math.floor(x), Y = Math.floor(y);
  253.     // Get relative xy coordinates of point within that cell
  254.     x = x - X; y = y - Y;
  255.     // Wrap the integer cells at 255 (smaller integer period can be introduced here)
  256.     X = X & 255; Y = Y & 255;
  257.  
  258.     // Calculate noise contributions from each of the four corners
  259.     var n00 = gradP[X+perm[Y]].dot2(x, y);
  260.     var n01 = gradP[X+perm[Y+1]].dot2(x, y-1);
  261.     var n10 = gradP[X+1+perm[Y]].dot2(x-1, y);
  262.     var n11 = gradP[X+1+perm[Y+1]].dot2(x-1, y-1);
  263.  
  264.     // Compute the fade curve value for x
  265.     var u = fade(x);
  266.  
  267.     // Interpolate the four results
  268.     return lerp(
  269.         lerp(n00, n10, u),
  270.         lerp(n01, n11, u),
  271.        fade(y));
  272.   };
  273.  
  274.   // 3D Perlin Noise
  275.   this.perlin3 = function(x, y, z) {
  276.     // Find unit grid cell containing point
  277.     var X = Math.floor(x), Y = Math.floor(y), Z = Math.floor(z);
  278.     // Get relative xyz coordinates of point within that cell
  279.     x = x - X; y = y - Y; z = z - Z;
  280.     // Wrap the integer cells at 255 (smaller integer period can be introduced here)
  281.     X = X & 255; Y = Y & 255; Z = Z & 255;
  282.  
  283.     // Calculate noise contributions from each of the eight corners
  284.     var n000 = gradP[X+  perm[Y+  perm[Z  ]]].dot3(x,   y,     z);
  285.     var n001 = gradP[X+  perm[Y+  perm[Z+1]]].dot3(x,   y,   z-1);
  286.     var n010 = gradP[X+  perm[Y+1+perm[Z  ]]].dot3(x,   y-1,   z);
  287.     var n011 = gradP[X+  perm[Y+1+perm[Z+1]]].dot3(x,   y-1, z-1);
  288.     var n100 = gradP[X+1+perm[Y+  perm[Z  ]]].dot3(x-1,   y,   z);
  289.     var n101 = gradP[X+1+perm[Y+  perm[Z+1]]].dot3(x-1,   y, z-1);
  290.     var n110 = gradP[X+1+perm[Y+1+perm[Z  ]]].dot3(x-1, y-1,   z);
  291.     var n111 = gradP[X+1+perm[Y+1+perm[Z+1]]].dot3(x-1, y-1, z-1);
  292.  
  293.     // Compute the fade curve value for x, y, z
  294.     var u = fade(x);
  295.     var v = fade(y);
  296.     var w = fade(z);
  297.  
  298.     // Interpolate
  299.     return lerp(
  300.         lerp(
  301.           lerp(n000, n100, u),
  302.           lerp(n001, n101, u), w),
  303.         lerp(
  304.           lerp(n010, n110, u),
  305.           lerp(n011, n111, u), w),
  306.        v);
  307.   };
  308.  
  309. }
  310.  
  311. var noise = new NoiseObject();
Advertisement
Add Comment
Please, Sign In to add comment