Created
January 4, 2018 08:09
-
-
Save AnastasiaDunbar/d1ec3f0f678a00ddc5947b1a3fdd10ea to your computer and use it in GitHub Desktop.
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
// title: Perlin Noise | |
// author: Anastasia Dunbar | |
// script: js | |
var width=240,height=136; | |
function mix(a,b,t){return(t*(b-a))+a;} | |
function mod(a,b){return((a%b)+b)%b;} | |
function fract(x){return x-Math.floor(x);} | |
function clamp(a,b,c){return Math.min(Math.max(a,b),c);} | |
function dot(a,b){for(var s=0,i=0;i<a.length;i++){s+=a[i]*b[i];}return s;} | |
function sign(x){return x>0?1:x<0?-1:0;} | |
function pow2(a,b){return Math.pow(Math.abs(a),b)*sign(a);} | |
function pal(i,r,g,b){ | |
i=i<0?0:i;i=i>15?15:i; | |
if(r==undefined&&g==undefined&&b==undefined){return[peek(0x3fc0+(i*3)),peek(0x3fc0+(i*3)+1),peek(0x3fc0+(i*3)+2)];}else{ | |
poke(0x3fc0+(i*3)+2,clamp(b,0,255));poke(0x3fc0+(i*3)+1,clamp(g,0,255));poke(0x3fc0+(i*3),clamp(r,0,255)); | |
} | |
} | |
var seed={ | |
a:500+(Math.random()*1e4), | |
fx:500+(Math.random()*1e4), | |
fy:500+(Math.random()*1e4), | |
px:Math.random()-.5, | |
py:Math.random()-.5 | |
} | |
function pseudorandom2D(x,y){return fract(Math.sin(dot([x+seed.px,y+seed.py],[seed.fx,seed.fy]))*seed.a);} | |
function perlin(x,y){ | |
return mix( | |
mix( | |
pseudorandom2D(Math.floor(x) ,Math.floor(y)), | |
pseudorandom2D(Math.floor(x)+1,Math.floor(y)), | |
fract(x) | |
), | |
mix( | |
pseudorandom2D(Math.floor(x) ,Math.floor(y)+1), | |
pseudorandom2D(Math.floor(x)+1,Math.floor(y)+1), | |
fract(x) | |
), | |
fract(y) | |
) | |
} | |
function f(x,y){ | |
x/=15;y/=15; | |
var iterations=4,sum=0; | |
for(var i=0;i<iterations;i++){ | |
seed.a=500+((fract(Math.sin((i+.512)*512)*725.63))*1e4); | |
sum+=perlin(x*(i+1),y*(i+1)); | |
} | |
return sum/iterations; | |
} | |
cls(0); | |
var i=0,stop=false,t=0, | |
s=function(x){return(Math.sin(x/6)+2)/3;}, | |
r=function(){ | |
for(var j=0;j<16;j++){ | |
pal( | |
j, | |
~~((pow2(j/15,s(t)))*255), | |
~~((pow2(j/15,s(t/2)))*255), | |
~~((pow2(j/15,s(t/3)))*255) | |
); | |
} | |
}; | |
function scanline(){ | |
if(stop){ | |
r(); | |
t+=1; | |
} | |
} | |
function TIC(){ | |
if(!stop){ | |
for(var j=0;j<100;j++){ | |
if(i<width*height){ | |
var x=i%width,y=Math.floor(i/width); | |
pix(x,y,Math.floor(clamp(f(x,y),0,1)*16)); | |
i++; | |
}else{ | |
stop=true; | |
} | |
} | |
r(); | |
t+=.5; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment