/** * A simple 2d implementation of simplex noise by Ondrej Zara * * Based on a speed-improved simplex noise algorithm for 2D, 3D and 4D in Java. * Which is based on example code by Stefan Gustavson (stegu@itn.liu.se). * With Optimisations by Peter Eastman (peastman@drizzle.stanford.edu). * Better rank ordering method by Stefan Gustavson in 2012. */ /** * @class 2D simplex noise generator * @param {int} [gradients=256] Random gradients */ ROT.Noise.Simplex = function(gradients) { ROT.Noise.call(this); this._F2 = 0.5 * (Math.sqrt(3) - 1); this._G2 = (3 - Math.sqrt(3)) / 6; this._gradients = [ [ 0, -1], [ 1, -1], [ 1, 0], [ 1, 1], [ 0, 1], [-1, 1], [-1, 0], [-1, -1] ]; var permutations = []; var count = gradients || 256; for (var i=0;i y0) { i1 = 1; j1 = 0; } else { // lower triangle, XY order: (0,0)->(1,0)->(1,1) i1 = 0; j1 = 1; } // upper triangle, YX order: (0,0)->(0,1)->(1,1) // A step of (1,0) in (i,j) means a step of (1-c,-c) in (x,y), and // a step of (0,1) in (i,j) means a step of (-c,1-c) in (x,y), where // c = (3-sqrt(3))/6 var x1 = x0 - i1 + G2; // Offsets for middle corner in (x,y) unskewed coords var y1 = y0 - j1 + G2; var x2 = x0 - 1 + 2*G2; // Offsets for last corner in (x,y) unskewed coords var y2 = y0 - 1 + 2*G2; // Work out the hashed gradient indices of the three simplex corners var ii = i.mod(count); var jj = j.mod(count); // Calculate the contribution from the three corners var t0 = 0.5 - x0*x0 - y0*y0; if (t0 >= 0) { t0 *= t0; gi = indexes[ii+perms[jj]]; var grad = this._gradients[gi]; n0 = t0 * t0 * (grad[0] * x0 + grad[1] * y0); } var t1 = 0.5 - x1*x1 - y1*y1; if (t1 >= 0) { t1 *= t1; gi = indexes[ii+i1+perms[jj+j1]]; var grad = this._gradients[gi]; n1 = t1 * t1 * (grad[0] * x1 + grad[1] * y1); } var t2 = 0.5 - x2*x2 - y2*y2; if (t2 >= 0) { t2 *= t2; gi = indexes[ii+1+perms[jj+1]]; var grad = this._gradients[gi]; n2 = t2 * t2 * (grad[0] * x2 + grad[1] * y2); } // Add contributions from each corner to get the final noise value. // The result is scaled to return values in the interval [-1,1]. return 70 * (n0 + n1 + n2); }