SimplexNoise.js 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. // Ported from Stefan Gustavson's java implementation
  2. // http://staffwww.itn.liu.se/~stegu/simplexnoise/simplexnoise.pdf
  3. // Sean McCullough [email protected]
  4. var SimplexNoise = function(gen) {
  5. this.rand = gen;
  6. this.grad3 = [
  7. [1,1,0],[-1,1,0],[1,-1,0],[-1,-1,0],
  8. [1,0,1],[-1,0,1],[1,0,-1],[-1,0,-1],
  9. [0,1,1],[0,-1,1],[0,1,-1],[0,-1,-1]
  10. ];
  11. this.simplex = [
  12. [0,1,2,3],[0,1,3,2],[0,0,0,0],[0,2,3,1],[0,0,0,0],[0,0,0,0],[0,0,0,0],[1,2,3,0],
  13. [0,2,1,3],[0,0,0,0],[0,3,1,2],[0,3,2,1],[0,0,0,0],[0,0,0,0],[0,0,0,0],[1,3,2,0],
  14. [0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0],
  15. [1,2,0,3],[0,0,0,0],[1,3,0,2],[0,0,0,0],[0,0,0,0],[0,0,0,0],[2,3,0,1],[2,3,1,0],
  16. [1,0,2,3],[1,0,3,2],[0,0,0,0],[0,0,0,0],[0,0,0,0],[2,0,3,1],[0,0,0,0],[2,1,3,0],
  17. [0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0],
  18. [2,0,1,3],[0,0,0,0],[0,0,0,0],[0,0,0,0],[3,0,1,2],[3,0,2,1],[0,0,0,0],[3,1,2,0],
  19. [2,1,0,3],[0,0,0,0],[0,0,0,0],[0,0,0,0],[3,1,0,2],[0,0,0,0],[3,2,0,1],[3,2,1,0]
  20. ];
  21. };
  22. SimplexNoise.prototype.setSeed = function(seed) {
  23. this.p = [];
  24. this.rand.seed = seed;
  25. for (var i=0; i<256; i++) {
  26. this.p[i] = Math.floor(this.rand.nextRange(0, 255));
  27. }
  28. this.perm = [];
  29. for(var i=0; i<512; i++) {
  30. this.perm[i]=this.p[i & 255];
  31. }
  32. }
  33. SimplexNoise.prototype.dot = function(g, x, y) {
  34. return g[0]*x + g[1]*y;
  35. };
  36. SimplexNoise.prototype.noise = function(xin, yin) {
  37. var n0, n1, n2;
  38. var F2 = 0.5*(Math.sqrt(3.0)-1.0);
  39. var s = (xin+yin)*F2;
  40. var i = Math.floor(xin+s);
  41. var j = Math.floor(yin+s);
  42. var G2 = (3.0-Math.sqrt(3.0))/6.0;
  43. var t = (i+j)*G2;
  44. var X0 = i-t;
  45. var Y0 = j-t;
  46. var x0 = xin-X0;
  47. var y0 = yin-Y0;
  48. var i1, j1;
  49. if(x0>y0) {i1=1; j1=0;}
  50. else {i1=0; j1=1;}
  51. var x1 = x0 - i1 + G2;
  52. var y1 = y0 - j1 + G2;
  53. var x2 = x0 - 1.0 + 2.0 * G2;
  54. var y2 = y0 - 1.0 + 2.0 * G2;
  55. var ii = i & 255;
  56. var jj = j & 255;
  57. var gi0 = this.perm[ii+this.perm[jj]] % 12;
  58. var gi1 = this.perm[ii+i1+this.perm[jj+j1]] % 12;
  59. var gi2 = this.perm[ii+1+this.perm[jj+1]] % 12;
  60. var t0 = 0.5 - x0*x0-y0*y0;
  61. if(t0<0) n0 = 0.0;
  62. else {
  63. t0 *= t0;
  64. n0 = t0 * t0 * this.dot(this.grad3[gi0], x0, y0);
  65. }
  66. var t1 = 0.5 - x1*x1-y1*y1;
  67. if(t1<0) n1 = 0.0;
  68. else {
  69. t1 *= t1;
  70. n1 = t1 * t1 * this.dot(this.grad3[gi1], x1, y1);
  71. }
  72. var t2 = 0.5 - x2*x2-y2*y2;
  73. if(t2<0) n2 = 0.0;
  74. else {
  75. t2 *= t2;
  76. n2 = t2 * t2 * this.dot(this.grad3[gi2], x2, y2);
  77. }
  78. return 70.0 * (n0 + n1 + n2);
  79. };