Created
September 3, 2014 22:04
-
-
Save 0b5vr/47d6948b9cd706560175 to your computer and use it in GitHub Desktop.
F
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
PShader shader; | |
void setup() | |
{ | |
size(480,480,P2D); | |
shader=loadShader("shader.glsl"); | |
} | |
void draw() | |
{ | |
shader.set("time",frameCount+71); | |
shader.set("size",width,height); | |
filter(shader); | |
} |
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
uniform int time; | |
uniform ivec2 size; | |
#ifdef GL_ES | |
precision mediump float; | |
#endif | |
#define PI 3.1415926 | |
#define BLUR true // slow as hell | |
#define ITER 79 | |
#define SHADOWITER 12 | |
vec2 v=vec2(0,1); | |
float t=mod(time*1./74,1.1); | |
float rew=0;//(1-pow(.8,max(0,(t-.9)*200))); | |
vec2 distFunc(vec3 p){ | |
vec2 ret; | |
// F | |
{ | |
vec3 b;float d;vec3 o; | |
o=p+vec3(.0+p.y*.1,.0,-1.7+16.*(pow(.8,t*50)+rew)); | |
float rs=sin((pow(.9,t*50)+rew)*1.2-.4); | |
float rc=cos((pow(.9,t*50)+rew)*1.2-.4); | |
o.yz=o.yz*mat2(rc,-rs,rs,rc); | |
rs=sin(.4-(pow(.94,t*50)+rew)*.6); | |
rc=cos(.4-(pow(.94,t*50)+rew)*.6); | |
o.xz=o.xz*mat2(rc,-rs,rs,rc); | |
b=abs(o+vec3(.0,.3,.0))-vec3(.3,.1,.1); | |
d=min(max(b.x,max(b.y,b.z)),0.0)+length(max(b,0.0)); | |
b=abs(o+vec3(.20,.0,.0))-vec3(.1,.4,.1); | |
d=min(d,min(max(b.x,max(b.y,b.z)),0.0)+length(max(b,0.0))); | |
b=abs(o+vec3(.0,-.02,.0))-vec3(.3,.1,.1); | |
d=min(d,min(max(b.x,max(b.y,b.z)),0.0)+length(max(b,0.0))); | |
ret=vec2(d,0.); | |
} | |
// explosion | |
{ | |
vec3 pp; | |
pp=p+v.xxy*(-1.+32.*(pow(.8,t*50)+rew)); | |
float rs=sin((pow(.9,t*50)*.7+rew)-.3); | |
float rc=cos((pow(.9,t*50)*.7+rew)-.3); | |
pp.yz=pp.yz*mat2(rc,-rs,rs,rc); | |
rs=sin(-(pow(.94,t*50)*.5+rew)+.2); | |
rc=cos(-(pow(.94,t*50)*.5+rew)+.2); | |
pp.xz=pp.xz*mat2(rc,-rs,rs,rc); | |
vec3 o;vec3 q;float d=1E3;float d2=1E3;vec2 k; | |
for(int c=0;c<16;c++) | |
{ | |
o=pp; | |
rc=cos(PI/8.*float(c)); | |
rs=sin(PI/8.*float(c)); | |
o.xy=o.xy*mat2(rc,-rs,rs,rc); | |
o=o-v.xyx*.3; | |
q=abs(o); | |
d=min(d,max(q.z-.1,max(q.x*1.5+o.y*.9,-o.y)-.9*0.5)); | |
d2=min(d2,max(q.z-.1,max(q.x*2.1+o.y*1.2,-o.y)-.9*0.5)); | |
} | |
if(d<ret.x)ret=vec2(d,1.); | |
if(d2<=ret.x)ret=vec2(d2,2.); | |
} | |
return ret; | |
} | |
vec3 getNormal(vec3 p){ | |
float d=0.0001; | |
return normalize(vec3( | |
distFunc(p+v.yxx*d).x-distFunc(p-v.yxx*d).x, | |
distFunc(p+v.xyx*d).x-distFunc(p-v.xyx*d).x, | |
distFunc(p+v.xxy*d).x-distFunc(p-v.xxy*d).x | |
)); | |
} | |
float shadow(vec3 p){ | |
vec3 l=normalize(vec3(.2,-.2,1)); | |
float rayL=.1; | |
vec3 rayP=p+l*rayL; | |
float dist; | |
for(int c=0;c<SHADOWITER;c++) | |
{ | |
dist=distFunc(rayP).x; | |
rayL+=dist; | |
rayP=p+l*rayL; | |
} | |
if(abs(dist)<.01){return .7;} | |
else{return 1.;} | |
} | |
void main(){ | |
for(int blur=0;blur<7;blur++) | |
{ | |
float blend=BLUR?sin(blur*PI/11.)*.2:1.; | |
t+=.1*1./74; | |
vec2 p=(gl_FragCoord.xy*2.-vec2(size.xy))/float(size.x); | |
vec3 camP=vec3(0.,0.,3.); | |
vec3 camC=vec3(0.,0.,1.); | |
vec3 camA=vec3(0.,1.,0.); | |
vec3 camS=normalize(cross(normalize(camC-camP),camA)); | |
vec3 camU=normalize(cross(normalize(camC-camP),camS)); | |
vec3 ray=normalize(camS*p.x+camU*p.y+(camC-camP)); | |
float dist=0.; | |
float col=0.; | |
float rayL=0.; | |
vec3 rayP=camP; | |
for(int c=0;c<ITER;c++){ | |
dist=distFunc(rayP).x; | |
col=distFunc(rayP).y; | |
rayL+=dist; | |
rayP=camP+ray*rayL; | |
} | |
if(abs(dist)<0.001){ | |
if(col==0.) | |
{ | |
float r=dot(-v.yxx,getNormal(rayP))*1.6; | |
float g=dot(-v.xyx,getNormal(rayP)); | |
float b=dot(v.xxy,getNormal(rayP))*2.; | |
gl_FragColor+=vec4(vec3(r,g+b*.6,b+r*.3+g*.3)*.7,1.)*blend; | |
gl_FragColor=vec4(gl_FragColor.xyz,1.); | |
}else if(col==1.){ | |
float frag=dot(-ray,getNormal(rayP)); | |
float sh=shadow(rayP); | |
gl_FragColor+=vec4(vec3(1.,.4,.0)*frag*sh,1.)*blend; | |
gl_FragColor=vec4(gl_FragColor.xyz,1.); | |
}else{ | |
float frag=dot(-ray,getNormal(rayP)); | |
float sh=shadow(rayP); | |
gl_FragColor+=vec4(vec3(1.,1.,.0)*frag*sh,1.)*blend; | |
gl_FragColor=vec4(gl_FragColor.xyz,1.); | |
} | |
}else{ | |
float d=mod(atan(p.y,p.x)+t*PI/4,PI/8.); | |
d=abs(d-3.1415926/16.)*200.-20.; | |
d=clamp(d,0.,1.); | |
float di=length(p.xy); | |
gl_FragColor+=vec4(vec3(.5-d*.5,d*1.,1.-d*.5)*pow(di*.9,.3),1.0)*blend; | |
gl_FragColor=vec4(gl_FragColor.xyz,1.); | |
} | |
if(!BLUR)break; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment