Instantly share code, notes, and snippets.

# jkosoy/perlin-noise-simplex.js

Forked from banksean/perlin-noise-classical.js
Created May 23, 2012 18:35
Show Gist options
• Save jkosoy/2776917 to your computer and use it in GitHub Desktop.
Minified Perlin Noise Simplex.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
 // https://gist.github.com/304522 // Ported from Stefan Gustavson's java implementation // http://staffwww.itn.liu.se/~stegu/simplexnoise/simplexnoise.pdf // Read Stefan's excellent paper for details on how this code works. // // Sean McCullough banksean@gmail.com /** * You can pass in a random number generator object if you like. * It is assumed to have a random() method. */ var SimplexNoise=function(a){a==undefined&&(a=Math);this.grad3=[[1,1,0],[-1,1,0],[1,-1,0],[-1,-1,0],[1,0,1],[-1,0,1],[1,0,-1],[-1,0,-1],[0,1,1],[0,-1,1],[0,1,-1],[0,-1,-1]];this.p=[];for(var b=0;b<256;b++)this.p[b]=Math.floor(a.random()*256);this.perm=[];for(var b=0;b<512;b++)this.perm[b]=this.p[b&255];this.simplex=[[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],[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],[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],[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],[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],[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],[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],[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]]};SimplexNoise.prototype.dot=function(a,b,c){return a[0]*b+a[1]*c};SimplexNoise.prototype.noise=function(a,b){var c,d,e,f=.5*(Math.sqrt(3)-1),g=(a+b)*f,h=Math.floor(a+g),i=Math.floor(b+g),j=(3-Math.sqrt(3))/6,k=(h+i)*j,l=h-k,m=i-k,n=a-l,o=b-m,p,q;if(n>o){p=1;q=0}else{p=0;q=1}var r=n-p+j,s=o-q+j,t=n-1+2*j,u=o-1+2*j,v=h&255,w=i&255,x=this.perm[v+this.perm[w]]%12,y=this.perm[v+p+this.perm[w+q]]%12,z=this.perm[v+1+this.perm[w+1]]%12,A=.5-n*n-o*o;if(A<0)c=0;else{A*=A;c=A*A*this.dot(this.grad3[x],n,o)}var B=.5-r*r-s*s;if(B<0)d=0;else{B*=B;d=B*B*this.dot(this.grad3[y],r,s)}var C=.5-t*t-u*u;if(C<0)e=0;else{C*=C;e=C*C*this.dot(this.grad3[z],t,u)}return 70*(c+d+e)};SimplexNoise.prototype.noise3d=function(a,b,c){var d,e,f,g,h=1/3,i=(a+b+c)*h,j=Math.floor(a+i),k=Math.floor(b+i),l=Math.floor(c+i),m=1/6,n=(j+k+l)*m,o=j-n,p=k-n,q=l-n,r=a-o,s=b-p,t=c-q,u,v,w,x,y,z;if(r>=s)if(s>=t){u=1;v=0;w=0;x=1;y=1;z=0}else if(r>=t){u=1;v=0;w=0;x=1;y=0;z=1}else{u=0;v=0;w=1;x=1;y=0;z=1}else if(s