Advertisement
Guest User

Shader Toy 704

a guest
May 18th, 2011
780
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.99 KB | None | 0 0
  1. #ifdef GL_ES
  2. precision highp float;
  3. #endif
  4.  
  5. uniform vec2 resolution;
  6. uniform float time;
  7.  
  8. float stime=sin(time);
  9. float ctime=cos(time);
  10.  
  11. float inObj(in vec3 p){
  12.   float oP=length(p);
  13.   p.x=sin(p.x)+stime;
  14.   p.z=sin(p.z)+ctime;
  15.   return float(min(length(p)-1.5-sin(oP-time*4.0),p.y+3.0));
  16. }
  17.  
  18. void main(void){
  19.   vec2 vPos=-1.0+2.0*gl_FragCoord.xy/resolution.xy;
  20.  
  21.   //Camera animation
  22.   vec3 vuv=vec3(stime,1,0);//view up vector
  23.   vec3 vrp=vec3(sin(time*0.7)*10.0,0,cos(time*0.9)*10.0); //view reference point
  24.   vec3 prp=vec3(sin(time*0.7)*20.0+vrp.x+20.0,
  25.                 stime*4.0+4.0+vrp.y+3.0,
  26.                 cos(time*0.6)*20.0+vrp.z+14.0); //camera position
  27.  
  28.   //Camera setup
  29.   vec3 vpn=normalize(vrp-prp);
  30.   vec3 u=normalize(cross(vuv,vpn));
  31.   vec3 v=cross(vpn,u);
  32.   vec3 vcv=(prp+vpn);
  33.   vec3 scrCoord=vcv+vPos.x*u*resolution.x/resolution.y+vPos.y*v;
  34.   vec3 scp=normalize(scrCoord-prp);
  35.  
  36.   //Raymarching
  37.   const vec3 e = vec3(0.1,0,0);
  38.   const float maxd=200.0;
  39.  
  40.   float s=0.1;
  41.   vec3 c,p,n;
  42.  
  43.   //speed optimization -advance ray (simple raytracing) until plane y=2.5
  44.   float f=-(prp.y-2.5)/scp.y;
  45.   if (f>0.0) p=prp+scp*f;
  46.   else f=maxd;
  47.  
  48.   for(int i=0;i<256;i++){
  49.     if (abs(s)<.01||f>maxd) break;
  50.     f+=s;
  51.     p=prp+scp*f;
  52.     s=inObj(p);
  53.   }
  54.  
  55.   if (f<maxd){
  56.     if(p.y<-2.5){
  57.       if (fract(p.x*.5)>.5)
  58.         if (fract(p.z*.5)>.5)
  59.           c=vec3(0,0,0);
  60.         else
  61.           c=vec3(1,1,1);
  62.       else
  63.         if (fract(p.z*.5)>.5)
  64.           c = vec3(1,1,1);
  65.         else
  66.           c = vec3(0,0,0);
  67.       n=vec3(0,1,0);
  68.     }
  69.     else{
  70.       float d=length(p);
  71.       c=vec3((sin(d*.25-time*4.0)+1.0)/2.0,
  72.              (stime+1.0)/2.0,
  73.              (sin(d-time*4.0)+1.0)/2.0); //color
  74.       n=normalize(
  75.         vec3(s-inObj(p-e.xyy),
  76.              s-inObj(p-e.yxy),
  77.              s-inObj(p-e.yyx)));
  78.     }
  79.     float b=dot(n,normalize(prp-p));
  80.     gl_FragColor=vec4((b*c+pow(b,54.0))*(1.0-f*.005),1.0);
  81.   }
  82.   else gl_FragColor=vec4(0,0,0,1);
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement