opengl_fragment.glsl 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. #define rendered texture0
  2. #define depthmap texture1
  3. uniform sampler2D rendered;
  4. uniform sampler2D depthmap;
  5. uniform vec3 sunPositionScreen;
  6. uniform float sunBrightness;
  7. uniform vec3 moonPositionScreen;
  8. uniform float moonBrightness;
  9. uniform lowp float volumetricLightStrength;
  10. uniform vec3 dayLight;
  11. #ifdef ENABLE_DYNAMIC_SHADOWS
  12. uniform vec3 v_LightDirection;
  13. #else
  14. const vec3 v_LightDirection = vec3(0.0, -1.0, 0.0);
  15. #endif
  16. #ifdef GL_ES
  17. varying mediump vec2 varTexCoord;
  18. #else
  19. centroid varying vec2 varTexCoord;
  20. #endif
  21. const float far = 1000.;
  22. float mapDepth(float depth)
  23. {
  24. return min(1., 1. / (1.00001 - depth) / far);
  25. }
  26. float noise(vec3 uvd) {
  27. return fract(dot(sin(uvd * vec3(13041.19699, 27723.29171, 61029.77801)), vec3(73137.11101, 37312.92319, 10108.89991)));
  28. }
  29. float sampleVolumetricLight(vec2 uv, vec3 lightVec, float rawDepth)
  30. {
  31. lightVec = 0.5 * lightVec / lightVec.z + 0.5;
  32. const float samples = 30.;
  33. float result = texture2D(depthmap, uv).r < 1. ? 0.0 : 1.0;
  34. float bias = noise(vec3(uv, rawDepth));
  35. vec2 samplepos;
  36. for (float i = 1.; i < samples; i++) {
  37. samplepos = mix(uv, lightVec.xy, (i + bias) / samples);
  38. if (min(samplepos.x, samplepos.y) > 0. && max(samplepos.x, samplepos.y) < 1.)
  39. result += texture2D(depthmap, samplepos).r < 1. ? 0.0 : 1.0;
  40. }
  41. return result / samples;
  42. }
  43. vec3 getDirectLightScatteringAtGround(vec3 v_LightDirection)
  44. {
  45. // Based on talk at 2002 Game Developers Conference by Naty Hoffman and Arcot J. Preetham
  46. const float beta_r0 = 1e-5; // Rayleigh scattering beta
  47. // These factors are calculated based on expected value of scattering factor of 1e-5
  48. // for Nitrogen at 532nm (green), 2e25 molecules/m3 in atmosphere
  49. const vec3 beta_r0_l = vec3(3.3362176e-01, 8.75378289198826e-01, 1.95342379700656) * beta_r0; // wavelength-dependent scattering
  50. const float atmosphere_height = 15000.; // height of the atmosphere in meters
  51. // sun/moon light at the ground level, after going through the atmosphere
  52. return exp(-beta_r0_l * atmosphere_height / (1e-5 - dot(v_LightDirection, vec3(0., 1., 0.))));
  53. }
  54. vec3 applyVolumetricLight(vec3 color, vec2 uv, float rawDepth)
  55. {
  56. vec3 lookDirection = normalize(vec3(uv.x * 2. - 1., uv.y * 2. - 1., rawDepth));
  57. const float boost = 4.0;
  58. float brightness = 0.;
  59. vec3 sourcePosition = vec3(-1., -1., -1);
  60. if (sunPositionScreen.z > 0. && sunBrightness > 0.) {
  61. brightness = sunBrightness;
  62. sourcePosition = sunPositionScreen;
  63. }
  64. else if (moonPositionScreen.z > 0. && moonBrightness > 0.) {
  65. brightness = moonBrightness * 0.05;
  66. sourcePosition = moonPositionScreen;
  67. }
  68. float cameraDirectionFactor = pow(clamp(dot(sourcePosition, vec3(0., 0., 1.)), 0.0, 0.7), 2.5);
  69. float viewAngleFactor = pow(max(0., dot(sourcePosition, lookDirection)), 8.);
  70. float lightFactor = brightness * sampleVolumetricLight(uv, sourcePosition, rawDepth) *
  71. (0.05 * cameraDirectionFactor + 0.95 * viewAngleFactor);
  72. color = mix(color, boost * getDirectLightScatteringAtGround(v_LightDirection) * dayLight, lightFactor);
  73. // a factor of 5 tested well
  74. color *= volumetricLightStrength * 5.0;
  75. // if (sunPositionScreen.z < 0.)
  76. // color.rg += 1. - clamp(abs((2. * uv.xy - 1.) - sunPositionScreen.xy / sunPositionScreen.z) * 1000., 0., 1.);
  77. // if (moonPositionScreen.z < 0.)
  78. // color.rg += 1. - clamp(abs((2. * uv.xy - 1.) - moonPositionScreen.xy / moonPositionScreen.z) * 1000., 0., 1.);
  79. return color;
  80. }
  81. void main(void)
  82. {
  83. vec2 uv = varTexCoord.st;
  84. vec3 color = texture2D(rendered, uv).rgb;
  85. // translate to linear colorspace (approximate)
  86. color = pow(color, vec3(2.2));
  87. if (volumetricLightStrength > 0.0) {
  88. float rawDepth = texture2D(depthmap, uv).r;
  89. color = applyVolumetricLight(color, uv, rawDepth);
  90. }
  91. gl_FragColor = vec4(color, 1.0); // force full alpha to avoid holes in the image.
  92. }