god_rays.glslf 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. #version GLSL_VERSION
  2. /*==============================================================================
  3. VARS
  4. ==============================================================================*/
  5. #var PRECISION highp
  6. #var DEPTH_RGBA 0
  7. #var WATER_EFFECTS 0
  8. #var STEPS_PER_PASS 10.0
  9. /*============================================================================*/
  10. precision PRECISION sampler2D;
  11. #include <precision_statement.glslf>
  12. #include <std.glsl>
  13. #include <depth_fetch.glslf>
  14. #include <procedural.glslf>
  15. #include <pack.glslf>
  16. uniform float u_time;
  17. uniform float u_radial_blur_step;
  18. uniform sampler2D u_input;
  19. #if DEPTH_RGBA
  20. uniform vec2 u_camera_range;
  21. #endif
  22. /*==============================================================================
  23. SHADER INTERFACE
  24. ==============================================================================*/
  25. #if DEPTH_RGBA && WATER_EFFECTS
  26. GLSL_IN float v_underwater;
  27. GLSL_IN vec2 v_texture_offset;
  28. #endif
  29. GLSL_IN vec2 v_texcoord;
  30. GLSL_IN vec4 v_sun_pos_clip;
  31. //------------------------------------------------------------------------------
  32. GLSL_OUT vec4 GLSL_OUT_FRAG_COLOR;
  33. /*==============================================================================
  34. MAIN
  35. ==============================================================================*/
  36. void main(void) {
  37. // delta from current pixel to sun position
  38. vec2 delta = (v_sun_pos_clip.xy - v_texcoord);
  39. float dist = length(delta);
  40. // Step vector (uv space)
  41. vec2 stepv = u_radial_blur_step * delta/dist;
  42. // Number of iterations between pixel and sun
  43. float iters = dist/u_radial_blur_step;
  44. //Correction for near the sun positions
  45. stepv *= min(iters, STEPS_PER_PASS) / STEPS_PER_PASS;
  46. iters = max(iters, STEPS_PER_PASS);
  47. vec2 uv = v_texcoord;
  48. // NOTE: Mac doesn't support iterations with float variables
  49. float intens = 0.0;
  50. const int steps_per_pass = int(STEPS_PER_PASS);
  51. int iters_int = int(iters + 0.5);
  52. for (int i = 0; i < steps_per_pass; i += 1) {
  53. if (i <= iters_int) {
  54. //On the first iteration Depth map is being unpacked and blurred.
  55. //On further iterations only performs blur of the given texture.
  56. #if DEPTH_RGBA
  57. float depth = depth_fetch(u_input, uv, u_camera_range);
  58. intens += max((1.0 - pow(dist, 0.3)) * step(0.9, depth), 0.0); //brighter at center
  59. #else
  60. vec4 input_col = GLSL_TEXTURE(u_input, uv);
  61. intens += unpack_float(input_col);
  62. #endif
  63. }
  64. uv += stepv;
  65. }
  66. #if DEPTH_RGBA && WATER_EFFECTS
  67. vec2 texcoord = v_texcoord + v_texture_offset;
  68. float noise =
  69. cellular2x2(
  70. vec2(2.5 * (texcoord.x),
  71. 2.5 * (texcoord.y) + 1.0 * u_time)
  72. ).x
  73. + 0.75 * cellular2x2(
  74. vec2(5.0 * (texcoord.x) - 0.66 * u_time,
  75. 5.0 * (texcoord.y) + 0.66 * u_time)
  76. ).x
  77. + 0.5 * snoise(
  78. vec2(7.5 * (texcoord.x) + 0.33 * u_time,
  79. 7.5 * (texcoord.y) - 0.33 * u_time)
  80. );
  81. noise *= clamp(1.2 - sqrt(0.2 * dist), 0.0, 1.0) * v_underwater;
  82. intens = max(noise, intens);
  83. #endif
  84. GLSL_OUT_FRAG_COLOR = pack(intens / STEPS_PER_PASS);
  85. }