Advertisement
Guest User

ShaderToy Raymarching Sandbox

a guest
Jun 17th, 2011
556
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.91 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. //UTIL START
  9. vec2 ObjUnion(in vec2 obj0,in vec2 obj1){
  10.   if (obj0.x<obj1.x)
  11.     return obj0;
  12.   else
  13.     return obj1;
  14. }
  15. //UTIL END
  16.  
  17. //SCENE START
  18.  
  19. //OBJ0
  20. vec2 obj0(in vec3 p){
  21.   return vec2(p.y+3.0,0);
  22. }
  23. //OBJ0 COLOR
  24. vec3 obj0_c(in vec3 p){
  25.  if (fract(p.x*.5)>.5)
  26.    if (fract(p.z*.5)>.5)
  27.      return vec3(0,0,0);
  28.    else
  29.      return vec3(1,1,1);
  30.  else
  31.    if (fract(p.z*.5)>.5)
  32.      return vec3(1,1,1);
  33.    else
  34.         return vec3(0,0,0);
  35. }
  36.  
  37. //OBJ1
  38. vec2 obj1(in vec3 p){
  39.   p.x=p.x+sin(p.y*4.0+time*8.0)*0.3+sin(p.z*4.0-time)*0.3;
  40.   return vec2(distance(p,vec3(0,0,0))-1.9,1);
  41. }
  42. //OBJ1 COLOR
  43. vec3 obj1_c(in vec3 p){
  44.     return vec3(1.0,0.5,0.2);
  45. }
  46.  
  47. //OBJ
  48. vec2 inObj(in vec3 p){
  49.   return ObjUnion(obj0(p),obj1(p));
  50. }
  51.  
  52. //SCENE END
  53.  
  54. void main(void){
  55.   vec2 vPos=-1.0+2.0*gl_FragCoord.xy/resolution.xy;
  56.  
  57.   //Camera animation
  58.   vec3 vuv=vec3(0,1,0);//view up vector
  59.   vec3 vrp=vec3(0,0,0); //view reference point
  60.   vec3 prp=vec3(-sin(time)*8.0,4,cos(time)*8.0); //camera position
  61.  
  62.   //Camera setup
  63.   vec3 vpn=normalize(vrp-prp);
  64.   vec3 u=normalize(cross(vuv,vpn));
  65.   vec3 v=cross(vpn,u);
  66.   vec3 vcv=(prp+vpn);
  67.   vec3 scrCoord=vcv+vPos.x*u*resolution.x/resolution.y+vPos.y*v;
  68.   vec3 scp=normalize(scrCoord-prp);
  69.  
  70.   //Raymarching
  71.   const vec3 e=vec3(0.1,0,0);
  72.   const float maxd=200.0;
  73.  
  74.   vec2 s=vec2(0.1,0);
  75.   vec3 c,p,n;
  76.  
  77.   float f=1.0;
  78.   for(int i=0;i<256;i++){
  79.     if (abs(s.x)<.01||f>maxd) break;
  80.     f+=s.x;
  81.     p=prp+scp*f;
  82.     s=inObj(p);
  83.   }
  84.  
  85.   if (f<maxd){
  86.     if (s.y==0.0)
  87.       c=obj0_c(p);
  88.     else
  89.       c=obj1_c(p);
  90.     n=normalize(
  91.       vec3(s.x-inObj(p-e.xyy).x,
  92.            s.x-inObj(p-e.yxy).x,
  93.            s.x-inObj(p-e.yyx).x));
  94.     float b=dot(n,normalize(prp-p));
  95.     gl_FragColor=vec4((b*c+pow(b,54.0))*(1.0-f*.005),1.0);
  96.   }
  97.   else gl_FragColor=vec4(0,0,0,1);
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement