Advertisement
Guest User

Untitled

a guest
Oct 9th, 2015
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.10 KB | None | 0 0
  1. precision highp float;
  2.  
  3. #define MAX_VIEW_DIST 25.
  4.  
  5. // globals passed into us
  6. uniform float time;
  7. uniform vec2 resolution;
  8.  
  9. float t = time;
  10.  
  11. vec2 T(vec2 p,float q){ // 2D rotate
  12. return vec2(cos(q)*p.x+sin(q)*p.y,-sin(q)*p.x+cos(q)*p.y);
  13. }
  14. float Sphere(vec3 p, vec3 o, float r){
  15. float d=pow(cos(30.*p.x), 2.)*sin(30.*p.y)*cos(30.*p.z); // Get some cheap-ass noise value
  16. //d*=sin(t/10.)/20.; // Wom it with time
  17. d*=0.03;
  18. return length(p-o)-r+d; // Just add it to the freakin distance XD
  19. }
  20.  
  21. float Plane(vec3 p){
  22. float d=sin(p.x)/10.+sin(p.z)/10.; // Again with cheap-ass "noise"
  23. return p.y+d;
  24. }
  25.  
  26. vec3 DR(vec3 p,vec3 q){ // Domain repeat
  27. return mod(p,q)-q/2.;
  28. }
  29.  
  30. float h(vec3 p) { // Main distance func
  31. float d = Plane(p);
  32. p.xz = DR(p, vec3(2.)).xz;
  33. return min(Sphere(p,vec3(0,sin(t)+1.,0),.4), d);
  34. }
  35.  
  36. vec3 GetNormal(vec3 p) { // Get a normal up in this maw
  37. vec2 e=vec2(.001,0);
  38. return normalize(vec3(
  39. h(p+e.xyy)-h(p-e.xyy),
  40. h(p+e.yxy)-h(p-e.yxy),
  41. h(p+e.yyx)-h(p-e.yyx)
  42. ));
  43. }
  44.  
  45. float AO(vec3 p,vec3 q){ // Find AO coefficient at point p with normal q
  46. float o=0.,s=1.,r,d;
  47. for(float i=0.;i<5.;i++){
  48. r=.01+.12*i/4.;
  49. vec3 a=q*r+p;
  50. d=h(a);
  51. o+=-(d-r)*s;
  52. s*=.95;
  53. }
  54. return clamp(1.-3.*o,0.,1.);
  55. }
  56.  
  57. float SH(vec3 p,vec3 q){ // Calculate shadow amount: p=intersection point; q=light direction
  58. p+=q*.01;
  59. for (int i=0; i<24; i++) {
  60. float d=h(p);
  61. if (d<0.001)
  62. return float(i)/24.;
  63. p += q*d;
  64. }
  65.  
  66. return 1.;
  67. }
  68.  
  69. vec3 SK(vec3 q){ // Get sky col (uses raydir)
  70. vec3 p=mix(vec3(140,160,180),vec3(80,120,160),min(abs(q.y)*2.+.5,1.))/512.;
  71. return p*(1.+vec3(1,.7,.3)/sqrt(length(q-normalize(vec3(.4,.5,.9))))*4.);
  72. }
  73.  
  74. void main(void) {
  75. vec2 uv = (gl_FragCoord.xy * 2. - resolution) / resolution.y;
  76.  
  77. vec3 ro = vec3(1.4, cos(t/2.)+2., t); // Cam pos (ray origin)
  78. vec3 up = vec3(0, 1, 0); // Cam orientation vecs
  79. vec2 s = T(vec2(0., 1.), t/6.);
  80. vec3 fd = vec3(s.x, 0., s.y);
  81. vec3 right = -cross(fd,up);
  82.  
  83. float fov=1.8;//sin(t/1.5)*1.2+1.4;
  84. vec3 rd = normalize(right*uv.x + up*uv.y + fd*fov); // Ray dir
  85.  
  86. vec3 p = ro; // Current test position
  87. float d; // Distance of p from surface
  88. for(int i=0; i<256; i++) {
  89. d = h(p);
  90. if (d < .01)
  91. break;
  92. p += rd * d *0.5; // March along! NOTE SMALL STEP
  93. }
  94.  
  95. float hitdist = length(p-ro);
  96. vec3 bg=SK(rd);
  97. if (d < .01) { // We hit something!
  98. vec3 lp = ro + vec3(30,20,-20); // Light position
  99. vec3 ld = normalize(lp-p); // Light direction
  100. vec3 n = GetNormal(p); // Normal
  101. float diffuse = dot(n,ld); // Diffuse amount
  102. vec3 c = vec3(sin(t)+1.*0.5,-sin(t)+1.*sin(p.z)+1.*0.5,cos(p.y)+1.0*0.5) * diffuse * AO(p,n) * SH(p,ld);
  103.  
  104. c=mix(c,bg,pow(clamp(hitdist/MAX_VIEW_DIST,0.,1.), 1.6)); // Foggy
  105.  
  106. gl_FragColor = vec4(c, 1);
  107. } else // We fucking hit NOTHING
  108. gl_FragColor = vec4(bg, 1);
  109.  
  110.  
  111. // Postprocessing
  112. float q=uv.x*uv.y*sin(t)*7777.; // Film grain
  113. gl_FragColor*=1.+clamp(.1+mod(mod(q,223.)*mod(q,11.),.01)*100.,0.,5.)/15.;
  114. gl_FragColor*=1.-clamp(length(uv)/2.,0.,1.); // Vignette
  115. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement