Advertisement
Guest User

SPherecammov

a guest
Jan 21st, 2020
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.01 KB | None | 0 0
  1. float map(vec3 pos)
  2. {
  3. vec3 q = pos;
  4.  
  5. float d = length(pos) - 0.25;
  6. float d2 = pos.y - (-0.25);
  7.  
  8. return min(d, d2);
  9. }
  10.  
  11. float castRay(vec3 ro, vec3 rd)
  12. {
  13. float t = 0.0;
  14. for(int i = 0; i < 100; i++)
  15. {
  16. vec3 pos = ro + t * rd;
  17.  
  18. float h = map(pos);
  19. if(h < 0.001)
  20. {
  21. break;
  22. }
  23. t += h;
  24.  
  25. if(t > 20.0) break;
  26.  
  27. }
  28.  
  29. if(t > 20.0) t = -1.0;
  30.  
  31. return t;
  32. }
  33.  
  34. vec3 calcNormal(vec3 pos)
  35. {
  36. vec2 e = vec2(0.0001, 0.0);
  37.  
  38. return normalize(vec3(
  39. map(pos+e.xyy) - map(pos-e.xyy),
  40. map(pos+e.yxy) - map(pos-e.yxy),
  41. map(pos+e.yyx) - map(pos-e.yyx)
  42. ));
  43. }
  44.  
  45. void mainImage( out vec4 fragColor, in vec2 fragCoord )
  46. {
  47. // Normalized pixel coordinates (from 0 to 1)
  48. vec2 p = (2.0 * fragCoord- iResolution.xy) / iResolution.y;
  49.  
  50. /*
  51. vec3 ro = vec3(0.0,0.0,1.0);
  52. vec3 rd = normalize(vec3(p, -1.5));
  53. */
  54.  
  55. vec3 col = vec3(0.0);
  56.  
  57.  
  58. // Camera Look at
  59.  
  60. float an = iTime;
  61. vec3 ro = vec3(1. * sin(an), 0.0, 1.0 * cos(an));
  62.  
  63. float cameraZoom = 2.5;
  64. vec3 target = vec3(0.1, 0., 0.);
  65.  
  66. vec3 ww = normalize(target - ro);
  67. vec3 uu = normalize(cross(ww, vec3(0.,1.,0.)));
  68. vec3 vv = normalize(cross(uu, ww));
  69.  
  70. vec3 rd = normalize(p.x * uu + p.y * vv + cameraZoom * ww);
  71.  
  72. // Rendering part
  73. float t = castRay(ro, rd);
  74.  
  75.  
  76. if(t > 0.)
  77. {
  78.  
  79. vec3 pos = ro + t * rd;
  80. vec3 nor = calcNormal(pos);
  81. vec3 sundir = normalize(vec3(0.8, 0.4, 0.2));
  82.  
  83.  
  84. float sundif = clamp(dot(nor, sundir), 0.0, 1.0);
  85.  
  86. float sun_sha = smoothstep(castRay(pos + nor * 0.001, sundir), 0.0, 1.0);
  87. float sky_dif = clamp(dot(nor,vec3(0.0,1.0,0.0)), 0.0, 1.0);;
  88.  
  89. col = vec3(1.0, 0.7, 0.5) * sundif * sun_sha;
  90. col += vec3(0.0, 0.2, 0.4) * sky_dif;
  91. }
  92. // Output to screen
  93. fragColor = vec4(col,1.0);
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement