Skip to content

Instantly share code, notes, and snippets.

@jkosoy
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.
Save jkosoy/2776917 to your computer and use it in GitHub Desktop.
Minified Perlin Noise Simplex.
// 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<t){u=0;v=0;w=1;x=0;y=1;z=1}else if(r<t){u=0;v=1;w=0;x=0;y=1;z=1}else{u=0;v=1;w=0;x=1;y=1;z=0}var A=r-u+m,B=s-v+m,C=t-w+m,D=r-x+2*m,E=s-y+2*m,F=t-z+2*m,G=r-1+3*m,H=s-1+3*m,I=t-1+3*m,J=j&255,K=k&255,L=l&255,M=this.perm[J+this.perm[K+this.perm[L]]]%12,N=this.perm[J+u+this.perm[K+v+this.perm[L+w]]]%12,O=this.perm[J+x+this.perm[K+y+this.perm[L+z]]]%12,P=this.perm[J+1+this.perm[K+1+this.perm[L+1]]]%12,Q=.6-r*r-s*s-t*t;if(Q<0)d=0;else{Q*=Q;d=Q*Q*this.dot(this.grad3[M],r,s,t)}var R=.6-A*A-B*B-C*C;if(R<0)e=0;else{R*=R;e=R*R*this.dot(this.grad3[N],A,B,C)}var S=.6-D*D-E*E-F*F;if(S<0)f=0;else{S*=S;f=S*S*this.dot(this.grad3[O],D,E,F)}var T=.6-G*G-H*H-I*I;if(T<0)g=0;else{T*=T;g=T*T*this.dot(this.grad3[P],G,H,I)}return 32*(d+e+f+g)};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment