daily pastebin goal
8%
SHARE
TWEET

Untitled

a guest Apr 16th, 2018 203 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 (stegu@itn.liu.se).
  5.  * Optimisations by Peter Eastman (peastman@drizzle.stanford.edu).
  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. // Passing in seed will seed this Noise instance
  18. class Noise {
  19.   constructor(seed) {
  20.     class Grad {
  21.       constructor(x, y, z) {
  22.         this.x = x
  23.         this.y = y
  24.         this.z = z
  25.       }
  26.  
  27.       dot2(x, y) {
  28.         return this.x * x + this.y * y
  29.       }
  30.  
  31.       dot3(x, y, z) {
  32.         return this.x * x + this.y * y + this.z * z
  33.       }
  34.     }
  35.  
  36.     this.grad3 = [
  37.       new Grad(1, 1, 0),
  38.       new Grad(-1, 1, 0),
  39.       new Grad(1, -1, 0),
  40.       new Grad(-1, -1, 0),
  41.       new Grad(1, 0, 1),
  42.       new Grad(-1, 0, 1),
  43.       new Grad(1, 0, -1),
  44.       new Grad(-1, 0, -1),
  45.       new Grad(0, 1, 1),
  46.       new Grad(0, -1, 1),
  47.       new Grad(0, 1, -1),
  48.       new Grad(0, -1, -1),
  49.     ]
  50.  
  51.     this.p = [
  52.       151,
  53.       160,
  54.       137,
  55.       91,
  56.       90,
  57.       15,
  58.       131,
  59.       13,
  60.       201,
  61.       95,
  62.       96,
  63.       53,
  64.       194,
  65.       233,
  66.       7,
  67.       225,
  68.       140,
  69.       36,
  70.       103,
  71.       30,
  72.       69,
  73.       142,
  74.       8,
  75.       99,
  76.       37,
  77.       240,
  78.       21,
  79.       10,
  80.       23,
  81.       190,
  82.       6,
  83.       148,
  84.       247,
  85.       120,
  86.       234,
  87.       75,
  88.       0,
  89.       26,
  90.       197,
  91.       62,
  92.       94,
  93.       252,
  94.       219,
  95.       203,
  96.       117,
  97.       35,
  98.       11,
  99.       32,
  100.       57,
  101.       177,
  102.       33,
  103.       88,
  104.       237,
  105.       149,
  106.       56,
  107.       87,
  108.       174,
  109.       20,
  110.       125,
  111.       136,
  112.       171,
  113.       168,
  114.       68,
  115.       175,
  116.       74,
  117.       165,
  118.       71,
  119.       134,
  120.       139,
  121.       48,
  122.       27,
  123.       166,
  124.       77,
  125.       146,
  126.       158,
  127.       231,
  128.       83,
  129.       111,
  130.       229,
  131.       122,
  132.       60,
  133.       211,
  134.       133,
  135.       230,
  136.       220,
  137.       105,
  138.       92,
  139.       41,
  140.       55,
  141.       46,
  142.       245,
  143.       40,
  144.       244,
  145.       102,
  146.       143,
  147.       54,
  148.       65,
  149.       25,
  150.       63,
  151.       161,
  152.       1,
  153.       216,
  154.       80,
  155.       73,
  156.       209,
  157.       76,
  158.       132,
  159.       187,
  160.       208,
  161.       89,
  162.       18,
  163.       169,
  164.       200,
  165.       196,
  166.       135,
  167.       130,
  168.       116,
  169.       188,
  170.       159,
  171.       86,
  172.       164,
  173.       100,
  174.       109,
  175.       198,
  176.       173,
  177.       186,
  178.       3,
  179.       64,
  180.       52,
  181.       217,
  182.       226,
  183.       250,
  184.       124,
  185.       123,
  186.       5,
  187.       202,
  188.       38,
  189.       147,
  190.       118,
  191.       126,
  192.       255,
  193.       82,
  194.       85,
  195.       212,
  196.       207,
  197.       206,
  198.       59,
  199.       227,
  200.       47,
  201.       16,
  202.       58,
  203.       17,
  204.       182,
  205.       189,
  206.       28,
  207.       42,
  208.       223,
  209.       183,
  210.       170,
  211.       213,
  212.       119,
  213.       248,
  214.       152,
  215.       2,
  216.       44,
  217.       154,
  218.       163,
  219.       70,
  220.       221,
  221.       153,
  222.       101,
  223.       155,
  224.       167,
  225.       43,
  226.       172,
  227.       9,
  228.       129,
  229.       22,
  230.       39,
  231.       253,
  232.       19,
  233.       98,
  234.       108,
  235.       110,
  236.       79,
  237.       113,
  238.       224,
  239.       232,
  240.       178,
  241.       185,
  242.       112,
  243.       104,
  244.       218,
  245.       246,
  246.       97,
  247.       228,
  248.       251,
  249.       34,
  250.       242,
  251.       193,
  252.       238,
  253.       210,
  254.       144,
  255.       12,
  256.       191,
  257.       179,
  258.       162,
  259.       241,
  260.       81,
  261.       51,
  262.       145,
  263.       235,
  264.       249,
  265.       14,
  266.       239,
  267.       107,
  268.       49,
  269.       192,
  270.       214,
  271.       31,
  272.       181,
  273.       199,
  274.       106,
  275.       157,
  276.       184,
  277.       84,
  278.       204,
  279.       176,
  280.       115,
  281.       121,
  282.       50,
  283.       45,
  284.       127,
  285.       4,
  286.       150,
  287.       254,
  288.       138,
  289.       236,
  290.       205,
  291.       93,
  292.       222,
  293.       114,
  294.       67,
  295.       29,
  296.       24,
  297.       72,
  298.       243,
  299.       141,
  300.       128,
  301.       195,
  302.       78,
  303.       66,
  304.       215,
  305.       61,
  306.       156,
  307.       180,
  308.     ]
  309.     // To remove the need for index wrapping, double the permutation table length
  310.     this.perm = new Array(512)
  311.     this.gradP = new Array(512)
  312.  
  313.     // Skewing and unskewing factors for 2, 3, and 4 dimensions
  314.     this.F2 = 0.5 * (Math.sqrt(3) - 1)
  315.     this.G2 = (3 - Math.sqrt(3)) / 6
  316.  
  317.     this.F3 = 1 / 3
  318.     this.G3 = 1 / 6
  319.  
  320.     this.seed(seed || 0)
  321.   }
  322.  
  323.   // This isn't a very good seeding function, but it works ok. It supports 2^16
  324.   // different seed values. Write something better if you need more seeds.
  325.   seed(seed) {
  326.     if (seed > 0 && seed < 1) {
  327.       // Scale the seed out
  328.       seed *= 65536
  329.     }
  330.  
  331.     seed = Math.floor(seed)
  332.     if (seed < 256) {
  333.       seed |= seed << 8
  334.     }
  335.  
  336.     const p = this.p
  337.  
  338.     for (let i = 0; i < 256; i++) {
  339.       let v
  340.  
  341.       if (i & 1) {
  342.         v = p[i] ^ (seed & 255)
  343.       }
  344.       else {
  345.         v = p[i] ^ ((seed >> 8) & 255)
  346.       }
  347.  
  348.       const perm = this.perm
  349.       const gradP = this.gradP
  350.  
  351.       perm[i] = perm[i + 256] = v
  352.       gradP[i] = gradP[i + 256] = this.grad3[v % 12]
  353.     }
  354.   }
  355.  
  356.   // 2D simplex noise
  357.   simplex2(xin, yin) {
  358.     let n0 // Noise contributions from the three corners
  359.     let n1
  360.     let n2
  361.     // Skew the input space to determine which simplex cell we're in
  362.     const s = (xin + yin) * this.F2 // Hairy factor for 2D
  363.     let i = Math.floor(xin + s)
  364.     let j = Math.floor(yin + s)
  365.     const t = (i + j) * this.G2
  366.     const x0 = xin - i + t // The x,y distances from the cell origin, unskewed.
  367.     const y0 = yin - j + t
  368.  
  369.     // For the 2D case, the simplex shape is an equilateral triangle.
  370.     // Determine which simplex we are in.
  371.     let i1 // Offsets for second (middle) corner of simplex in (i,j) coords
  372.  
  373.     let j1
  374.  
  375.     if (x0 > y0) {
  376.       // lower triangle, XY order: (0,0)->(1,0)->(1,1)
  377.       i1 = 1
  378.       j1 = 0
  379.     }
  380.     else {
  381.       // upper triangle, YX order: (0,0)->(0,1)->(1,1)
  382.       i1 = 0
  383.       j1 = 1
  384.     }
  385.     // A step of (1,0) in (i,j) means a step of (1-c,-c) in (x,y), and
  386.     // a step of (0,1) in (i,j) means a step of (-c,1-c) in (x,y), where
  387.     // c = (3-sqrt(3))/6
  388.     const x1 = x0 - i1 + this.G2 // Offsets for middle corner in (x,y) unskewed coords
  389.     const y1 = y0 - j1 + this.G2
  390.     const x2 = x0 - 1 + 2 * this.G2 // Offsets for last corner in (x,y) unskewed coords
  391.     const y2 = y0 - 1 + 2 * this.G2
  392.  
  393.     // Work out the hashed gradient indices of the three simplex corners
  394.     i &= 255
  395.     j &= 255
  396.  
  397.     const perm = this.perm
  398.     const gradP = this.gradP
  399.     const gi0 = gradP[i + perm[j]]
  400.     const gi1 = gradP[i + i1 + perm[j + j1]]
  401.     const gi2 = gradP[i + 1 + perm[j + 1]]
  402.     // Calculate the contribution from the three corners
  403.     let t0 = 0.5 - x0 * x0 - y0 * y0
  404.  
  405.     if (t0 < 0) {
  406.       n0 = 0
  407.     }
  408.     else {
  409.       t0 *= t0
  410.       n0 = t0 * t0 * gi0.dot2(x0, y0) // (x,y) of grad3 used for 2D gradient
  411.     }
  412.     let t1 = 0.5 - x1 * x1 - y1 * y1
  413.  
  414.     if (t1 < 0) {
  415.       n1 = 0
  416.     }
  417.     else {
  418.       t1 *= t1
  419.       n1 = t1 * t1 * gi1.dot2(x1, y1)
  420.     }
  421.     let t2 = 0.5 - x2 * x2 - y2 * y2
  422.  
  423.     if (t2 < 0) {
  424.       n2 = 0
  425.     }
  426.     else {
  427.       t2 *= t2
  428.       n2 = t2 * t2 * gi2.dot2(x2, y2)
  429.     }
  430.     // Add contributions from each corner to get the final noise value.
  431.     // The result is scaled to return values in the interval [-1,1].
  432.     return 70 * (n0 + n1 + n2)
  433.   }
  434.  
  435.   // 3D simplex noise
  436.   simplex3(xin, yin, zin) {
  437.     let n0 // Noise contributions from the four corners
  438.     let n1
  439.     let n2
  440.     let n3
  441.  
  442.     // Skew the input space to determine which simplex cell we're in
  443.     const s = (xin + yin + zin) * this.F3 // Hairy factor for 2D
  444.     let i = Math.floor(xin + s)
  445.     let j = Math.floor(yin + s)
  446.     let k = Math.floor(zin + s)
  447.  
  448.     const t = (i + j + k) * this.G3
  449.     const x0 = xin - i + t // The x,y distances from the cell origin, unskewed.
  450.     const y0 = yin - j + t
  451.     const z0 = zin - k + t
  452.  
  453.     // For the 3D case, the simplex shape is a slightly irregular tetrahedron.
  454.     // Determine which simplex we are in.
  455.     let i1 // Offsets for second corner of simplex in (i,j,k) coords
  456.  
  457.     let j1
  458.     let k1
  459.     let i2 // Offsets for third corner of simplex in (i,j,k) coords
  460.     let j2
  461.     let k2
  462.  
  463.     if (x0 >= y0) {
  464.       if (y0 >= z0) {
  465.         i1 = 1
  466.         j1 = 0
  467.         k1 = 0
  468.         i2 = 1
  469.         j2 = 1
  470.         k2 = 0
  471.       }
  472.       else if (x0 >= z0) {
  473.         i1 = 1
  474.         j1 = 0
  475.         k1 = 0
  476.         i2 = 1
  477.         j2 = 0
  478.         k2 = 1
  479.       }
  480.       else {
  481.         i1 = 0
  482.         j1 = 0
  483.         k1 = 1
  484.         i2 = 1
  485.         j2 = 0
  486.         k2 = 1
  487.       }
  488.     }
  489.     else if (y0 < z0) {
  490.       i1 = 0
  491.       j1 = 0
  492.       k1 = 1
  493.       i2 = 0
  494.       j2 = 1
  495.       k2 = 1
  496.     }
  497.     else if (x0 < z0) {
  498.       i1 = 0
  499.       j1 = 1
  500.       k1 = 0
  501.       i2 = 0
  502.       j2 = 1
  503.       k2 = 1
  504.     }
  505.     else {
  506.       i1 = 0
  507.       j1 = 1
  508.       k1 = 0
  509.       i2 = 1
  510.       j2 = 1
  511.       k2 = 0
  512.     }
  513.     // A step of (1,0,0) in (i,j,k) means a step of (1-c,-c,-c) in (x,y,z),
  514.     // a step of (0,1,0) in (i,j,k) means a step of (-c,1-c,-c) in (x,y,z), and
  515.     // a step of (0,0,1) in (i,j,k) means a step of (-c,-c,1-c) in (x,y,z), where
  516.     // c = 1/6.
  517.     const x1 = x0 - i1 + G3 // Offsets for second corner
  518.     const y1 = y0 - j1 + G3
  519.     const z1 = z0 - k1 + G3
  520.  
  521.     const x2 = x0 - i2 + 2 * G3 // Offsets for third corner
  522.     const y2 = y0 - j2 + 2 * G3
  523.     const z2 = z0 - k2 + 2 * G3
  524.  
  525.     const x3 = x0 - 1 + 3 * G3 // Offsets for fourth corner
  526.     const y3 = y0 - 1 + 3 * G3
  527.     const z3 = z0 - 1 + 3 * G3
  528.  
  529.     // Work out the hashed gradient indices of the four simplex corners
  530.     i &= 255
  531.     j &= 255
  532.     k &= 255
  533.  
  534.     const perm = this.perm
  535.     const gradP = this.gradP
  536.     const gi0 = gradP[i + perm[j + perm[k]]]
  537.     const gi1 = gradP[i + i1 + perm[j + j1 + perm[k + k1]]]
  538.     const gi2 = gradP[i + i2 + perm[j + j2 + perm[k + k2]]]
  539.     const gi3 = gradP[i + 1 + perm[j + 1 + perm[k + 1]]]
  540.  
  541.     // Calculate the contribution from the four corners
  542.     let t0 = 0.5 - x0 * x0 - y0 * y0 - z0 * z0
  543.  
  544.     if (t0 < 0) {
  545.       n0 = 0
  546.     }
  547.     else {
  548.       t0 *= t0
  549.       n0 = t0 * t0 * gi0.dot3(x0, y0, z0) // (x,y) of grad3 used for 2D gradient
  550.     }
  551.     let t1 = 0.5 - x1 * x1 - y1 * y1 - z1 * z1
  552.  
  553.     if (t1 < 0) {
  554.       n1 = 0
  555.     }
  556.     else {
  557.       t1 *= t1
  558.       n1 = t1 * t1 * gi1.dot3(x1, y1, z1)
  559.     }
  560.     let t2 = 0.5 - x2 * x2 - y2 * y2 - z2 * z2
  561.  
  562.     if (t2 < 0) {
  563.       n2 = 0
  564.     }
  565.     else {
  566.       t2 *= t2
  567.       n2 = t2 * t2 * gi2.dot3(x2, y2, z2)
  568.     }
  569.     let t3 = 0.5 - x3 * x3 - y3 * y3 - z3 * z3
  570.  
  571.     if (t3 < 0) {
  572.       n3 = 0
  573.     }
  574.     else {
  575.       t3 *= t3
  576.       n3 = t3 * t3 * gi3.dot3(x3, y3, z3)
  577.     }
  578.     // Add contributions from each corner to get the final noise value.
  579.     // The result is scaled to return values in the interval [-1,1].
  580.     return 32 * (n0 + n1 + n2 + n3)
  581.   }
  582.  
  583.   // 2D Perlin Noise
  584.   perlin2(x, y) {
  585.     // Find unit grid cell containing point
  586.     let X = Math.floor(x)
  587.  
  588.     let Y = Math.floor(y)
  589.  
  590.     // Get relative xy coordinates of point within that cell
  591.     x -= X
  592.     y -= Y
  593.     // Wrap the integer cells at 255 (smaller integer period can be introduced here)
  594.     X &= 255
  595.     Y &= 255
  596.  
  597.     // Calculate noise contributions from each of the four corners
  598.     const perm = this.perm
  599.     const gradP = this.gradP
  600.     const n00 = gradP[X + perm[Y]].dot2(x, y)
  601.     const n01 = gradP[X + perm[Y + 1]].dot2(x, y - 1)
  602.     const n10 = gradP[X + 1 + perm[Y]].dot2(x - 1, y)
  603.     const n11 = gradP[X + 1 + perm[Y + 1]].dot2(x - 1, y - 1)
  604.  
  605.     // Compute the fade curve value for x
  606.     const u = this.fade(x)
  607.  
  608.     // Interpolate the four results
  609.     return this.lerp(this.lerp(n00, n10, u), this.lerp(n01, n11, u), this.fade(y))
  610.   }
  611.  
  612.   fade(t) {
  613.     return t * t * t * (t * (t * 6 - 15) + 10)
  614.   }
  615.  
  616.   lerp(a, b, t) {
  617.     return (1 - t) * a + t * b
  618.   }
  619.  
  620.   // 3D Perlin Noise
  621.   perlin3(x, y, z) {
  622.     // Find unit grid cell containing point
  623.     let X = Math.floor(x)
  624.  
  625.     let Y = Math.floor(y)
  626.     let Z = Math.floor(z)
  627.  
  628.     // Get relative xyz coordinates of point within that cell
  629.     x -= X
  630.     y -= Y
  631.     z -= Z
  632.     // Wrap the integer cells at 255 (smaller integer period can be introduced here)
  633.     X &= 255
  634.     Y &= 255
  635.     Z &= 255
  636.  
  637.     // Calculate noise contributions from each of the eight corners
  638.     const perm = this.perm
  639.     const gradP = this.gradP
  640.     const n000 = gradP[X + perm[Y + perm[Z]]].dot3(x, y, z)
  641.     const n001 = gradP[X + perm[Y + perm[Z + 1]]].dot3(x, y, z - 1)
  642.     const n010 = gradP[X + perm[Y + 1 + perm[Z]]].dot3(x, y - 1, z)
  643.     const n011 = gradP[X + perm[Y + 1 + perm[Z + 1]]].dot3(x, y - 1, z - 1)
  644.     const n100 = gradP[X + 1 + perm[Y + perm[Z]]].dot3(x - 1, y, z)
  645.     const n101 = gradP[X + 1 + perm[Y + perm[Z + 1]]].dot3(x - 1, y, z - 1)
  646.     const n110 = gradP[X + 1 + perm[Y + 1 + perm[Z]]].dot3(x - 1, y - 1, z)
  647.     const n111 = gradP[X + 1 + perm[Y + 1 + perm[Z + 1]]].dot3(x - 1, y - 1, z - 1)
  648.  
  649.     // Compute the fade curve value for x, y, z
  650.     const u = this.fade(x)
  651.     const v = this.fade(y)
  652.     const w = this.fade(z)
  653.  
  654.     // Interpolate
  655.     return this.lerp(
  656.       this.lerp(this.lerp(n000, n100, u), this.lerp(n001, n101, u), w),
  657.       this.lerp(this.lerp(n010, n110, u), this.lerp(n011, n111, u), w),
  658.       v
  659.     )
  660.   }
  661. }
  662.  
  663. /*
  664.   for(var i=0; i<256; i++) {
  665.     perm[i] = perm[i + 256] = p[i];
  666.     gradP[i] = gradP[i + 256] = grad3[perm[i] % 12];
  667.   } */
  668.  
  669. global.Noise = Noise
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top