Advertisement
PifyZ

OSimplexNoise

Feb 18th, 2016
384
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. (function() {
  2.   var __imul = Math.imul ? Math.imul : function(a, b) {
  3.     return (a * (b >>> 16) << 16) + a * (b & 65535) | 0;
  4.   };
  5.  
  6.   function assert(truth) {
  7.     if (!truth) {
  8.       throw Error('Assertion failed');
  9.     }
  10.   }
  11.  
  12.   function main() {
  13.     var noiser = new OSimplexNoise();
  14.     noiser.setSeed1();
  15.     tileset.src = 'tileset.png';
  16.     tileset.onload = function() {
  17.       var can = document.getElementById('app');
  18.       var ctx = can.getContext('2d');
  19.       can.width = 200;
  20.       can.height = 200;
  21.  
  22.       for (var x = 0, count1 = can.width; x < count1; x = x + 1 | 0) {
  23.         for (var y = 0, count = can.height; y < count; y = y + 1 | 0) {
  24.           var a = noiser.noise2D(x / 100 | 0, y / 100 | 0);
  25.           a = (a + 1) * 128;
  26.           var b = a | 0;
  27.           ctx.fillStyle = 'rgb(' + b.toString() + ', ' + b.toString() + ', ' + b.toString() + ')';
  28.           ctx.fillRect(x, y, 1, 1);
  29.         }
  30.       }
  31.  
  32.       return;
  33.  
  34.       for (var y1 = 0, count3 = in_List.count(map); y1 < count3; y1 = y1 + 1 | 0) {
  35.         for (var x1 = 0, count2 = in_List.count(in_List.get(map, y1)); x1 < count2; x1 = x1 + 1 | 0) {
  36.           ctx.drawImage(tileset, __imul(in_List.get(in_List.get(map, y1), x1), CELL_SIZE), 0, CELL_SIZE, CELL_SIZE, __imul(x1, CELL_SIZE), __imul(y1, CELL_SIZE), CELL_SIZE, CELL_SIZE);
  37.         }
  38.       }
  39.     };
  40.   }
  41.  
  42.   function OSimplexNoise() {
  43.     this.seed = 0;
  44.     this.randArray = null;
  45.     this.seed = 0;
  46.     this.randArray = new Int8Array(256);
  47.   }
  48.  
  49.   OSimplexNoise.prototype.setSeed1 = function() {
  50.     for (var i = 0; i < 256; i = i + 1 | 0) {
  51.       this.randArray[i] = Math.random() * 256;
  52.     }
  53.   };
  54.  
  55.   OSimplexNoise.prototype.extrapolate2D = function(xsb, ysb, dx, dy) {
  56.     var gradients = new Int8Array([5, 2, 2, 5, -5, 2, -2, 5, 5, -2, 2, -5, -5, -2, -2, -5]);
  57.     var idx = this.randArray[this.randArray[xsb & 255] + ysb & 255] & 14;
  58.     return gradients[idx] * dx + gradients[idx + 1] * dy;
  59.   };
  60.  
  61.   OSimplexNoise.prototype.noise2D = function(x, y) {
  62.     x = +x;
  63.     y = +y;
  64.     var stretchOffset = (x + y) * OSimplexNoise.STRETCH_CONSTANT_2D;
  65.     var xs = x + stretchOffset;
  66.     var ys = y + stretchOffset;
  67.     var xsb = Math.floor(xs) | 0;
  68.     var ysb = Math.floor(ys) | 0;
  69.     var squishOffset = (xsb + ysb | 0) * OSimplexNoise.SQUISH_CONSTANT_2D;
  70.     var xb = xsb + squishOffset;
  71.     var yb = ysb + squishOffset;
  72.     var xins = xs - xsb;
  73.     var yins = ys - ysb;
  74.     var inSum = xins + yins;
  75.     var dx0 = x - xb;
  76.     var dy0 = y - yb;
  77.     var dxExt = 0;
  78.     var dyExt = 0;
  79.     var xsvExt = 0;
  80.     var ysvExt = 0;
  81.     var value = 0;
  82.     var dx1 = dx0 - 1 * OSimplexNoise.SQUISH_CONSTANT_2D;
  83.     var dy1 = dy0 - 0 - OSimplexNoise.SQUISH_CONSTANT_2D;
  84.     var attn1 = 2 - dx1 * dx1 - dy1 * dy1;
  85.  
  86.     if (attn1 > 0) {
  87.       attn1 *= attn1;
  88.       value += attn1 * attn1 * this.extrapolate2D(xsb + 1 | 0, ysb + 0 | 0, dx1, dy1);
  89.     }
  90.  
  91.     var dx2 = dx0 - 0 * OSimplexNoise.SQUISH_CONSTANT_2D;
  92.     var dy2 = dy0 - 1 - OSimplexNoise.SQUISH_CONSTANT_2D;
  93.     var attn2 = 2 - dx2 * dx2 - dy2 * dy2;
  94.  
  95.     if (attn2 > 0) {
  96.       attn2 *= attn2;
  97.       value += attn2 * attn2 * this.extrapolate2D(xsb + 0 | 0, ysb + 1 | 0, dx2, dy2);
  98.     }
  99.  
  100.     if (inSum <= 1) {
  101.       var zins = 1 - inSum;
  102.  
  103.       if (zins > xins || zins > yins) {
  104.         if (xins > yins) {
  105.           xsvExt = xsb + 1 | 0;
  106.           ysvExt = ysb - 1 | 0;
  107.           dxExt = dx0 - 1;
  108.           dyExt = dy0 + 1;
  109.         }
  110.  
  111.         else {
  112.           xsvExt = xsb - 1 | 0;
  113.           ysvExt = ysb + 1 | 0;
  114.           dxExt = dx0 + 1;
  115.           dyExt = dy0 - 1;
  116.         }
  117.       }
  118.  
  119.       else {
  120.         xsvExt = xsb + 1 | 0;
  121.         ysvExt = ysb + 1 | 0;
  122.         dxExt = dx0 - 1 - 2 * OSimplexNoise.SQUISH_CONSTANT_2D;
  123.         dyExt = dy0 - 1 - 2 * OSimplexNoise.SQUISH_CONSTANT_2D;
  124.       }
  125.     }
  126.  
  127.     else {
  128.       var zins1 = 2 - inSum;
  129.  
  130.       if (zins1 < xins || zins1 < yins) {
  131.         if (xins > yins) {
  132.           xsvExt = xsb + 2 | 0;
  133.           ysvExt = ysb + 0 | 0;
  134.           dxExt = dx0 - 2 - 2 * OSimplexNoise.SQUISH_CONSTANT_2D;
  135.           dyExt = dy0 + 0 - 2 * OSimplexNoise.SQUISH_CONSTANT_2D;
  136.         }
  137.  
  138.         else {
  139.           xsvExt = xsb + 0 | 0;
  140.           ysvExt = ysb + 2 | 0;
  141.           dxExt = dx0 + 0 - 2 * OSimplexNoise.SQUISH_CONSTANT_2D;
  142.           dyExt = dy0 - 2 - 2 * OSimplexNoise.SQUISH_CONSTANT_2D;
  143.         }
  144.       }
  145.  
  146.       else {
  147.         dxExt = dx0;
  148.         dyExt = dy0;
  149.         xsvExt = xsb;
  150.         ysvExt = ysb;
  151.       }
  152.  
  153.       xsb = xsb + 1 | 0;
  154.       ysb = ysb + 1 | 0;
  155.       dx0 -= 1 - 2 * OSimplexNoise.SQUISH_CONSTANT_2D;
  156.       dy0 -= 1 - 2 * OSimplexNoise.SQUISH_CONSTANT_2D;
  157.     }
  158.  
  159.     var attn0 = 2 - dx0 * dx0 - dy0 * dy0;
  160.  
  161.     if (attn0 > 0) {
  162.       attn0 *= attn0;
  163.       value += attn0 * attn0 * this.extrapolate2D(xsb, ysb, dx0, dy0);
  164.     }
  165.  
  166.     var attnExt = 2 - dxExt * dxExt - dyExt * dyExt;
  167.  
  168.     if (attnExt > 0) {
  169.       attnExt *= attnExt;
  170.       value += attnExt * attnExt * this.extrapolate2D(xsvExt, ysvExt, dxExt, dyExt);
  171.     }
  172.  
  173.     return value / OSimplexNoise.NORM_CONSTANT_2D;
  174.   };
  175.  
  176.   var in_List = {};
  177.  
  178.   in_List.get = function(self, index) {
  179.     assert(0 <= index && index < in_List.count(self));
  180.     return self[index];
  181.   };
  182.  
  183.   in_List.count = function(self) {
  184.     return self.length;
  185.   };
  186.  
  187.   var map = [[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [1, 7, 3, 6, 1, 1, 1, 1, 1, 1, 1, 7, 3, 6, 1], [1, 2, 0, 4, 1, 1, 1, 1, 1, 1, 7, 16, 0, 4, 1], [1, 2, 0, 4, 1, 1, 1, 1, 1, 7, 16, 0, 0, 4, 1], [1, 9, 15, 17, 3, 6, 1, 1, 7, 16, 0, 0, 0, 4, 1], [1, 1, 9, 15, 0, 4, 1, 7, 20, 5, 5, 5, 5, 8, 1], [1, 1, 1, 2, 0, 4, 1, 2, 4, 1, 1, 1, 1, 1, 1], [1, 1, 1, 2, 0, 4, 1, 2, 4, 1, 1, 1, 1, 1, 1], [1, 1, 1, 2, 0, 0, 0, 0, 8, 1, 1, 1, 1, 1, 1], [1, 1, 1, 2, 0, 4, 1, 1, 1, 1, 1, 7, 6, 1, 1], [1, 1, 1, 2, 0, 17, 3, 3, 3, 3, 3, 16, 4, 1, 1], [1, 7, 3, 5, 15, 0, 0, 18, 5, 5, 5, 5, 19, 6, 1], [1, 9, 15, 1, 2, 5, 15, 4, 1, 1, 1, 1, 2, 4, 1], [1, 1, 10, 1, 10, 1, 9, 8, 1, 1, 1, 1, 9, 8, 1], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]];
  188.   var CELL_SIZE = 16;
  189.   var tileset = new Image();
  190.  
  191.   // (1/sqrt(2+1)-1)/2
  192.   OSimplexNoise.STRETCH_CONSTANT_2D = -0.2113248654051871;
  193.  
  194.   // (sqrt(2+1)-1)/2
  195.   OSimplexNoise.SQUISH_CONSTANT_2D = 0.36602540378443865;
  196.   OSimplexNoise.NORM_CONSTANT_2D = 47;
  197.  
  198.   main();
  199. })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement