opengl_fragment.glsl 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. uniform sampler2D baseTexture;
  2. uniform vec4 emissiveColor;
  3. uniform vec4 skyBgColor;
  4. uniform float fogDistance;
  5. uniform vec3 eyePosition;
  6. varying vec3 vNormal;
  7. varying vec3 vPosition;
  8. varying vec3 worldPosition;
  9. varying lowp vec4 varColor;
  10. #ifdef GL_ES
  11. varying mediump vec2 varTexCoord;
  12. #else
  13. centroid varying vec2 varTexCoord;
  14. #endif
  15. varying vec3 eyeVec;
  16. varying float vIDiff;
  17. const float e = 2.718281828459;
  18. const float BS = 10.0;
  19. const float fogStart = FOG_START;
  20. const float fogShadingParameter = 1.0 / (1.0 - fogStart);
  21. #if ENABLE_TONE_MAPPING
  22. /* Hable's UC2 Tone mapping parameters
  23. A = 0.22;
  24. B = 0.30;
  25. C = 0.10;
  26. D = 0.20;
  27. E = 0.01;
  28. F = 0.30;
  29. W = 11.2;
  30. equation used: ((x * (A * x + C * B) + D * E) / (x * (A * x + B) + D * F)) - E / F
  31. */
  32. vec3 uncharted2Tonemap(vec3 x)
  33. {
  34. return ((x * (0.22 * x + 0.03) + 0.002) / (x * (0.22 * x + 0.3) + 0.06)) - 0.03333;
  35. }
  36. vec4 applyToneMapping(vec4 color)
  37. {
  38. color = vec4(pow(color.rgb, vec3(2.2)), color.a);
  39. const float gamma = 1.6;
  40. const float exposureBias = 5.5;
  41. color.rgb = uncharted2Tonemap(exposureBias * color.rgb);
  42. // Precalculated white_scale from
  43. //vec3 whiteScale = 1.0 / uncharted2Tonemap(vec3(W));
  44. vec3 whiteScale = vec3(1.036015346);
  45. color.rgb *= whiteScale;
  46. return vec4(pow(color.rgb, vec3(1.0 / gamma)), color.a);
  47. }
  48. #endif
  49. void main(void)
  50. {
  51. vec3 color;
  52. vec2 uv = varTexCoord.st;
  53. vec4 base = texture2D(baseTexture, uv).rgba;
  54. #ifdef USE_DISCARD
  55. // If alpha is zero, we can just discard the pixel. This fixes transparency
  56. // on GPUs like GC7000L, where GL_ALPHA_TEST is not implemented in mesa,
  57. // and also on GLES 2, where GL_ALPHA_TEST is missing entirely.
  58. if (base.a == 0.0) {
  59. discard;
  60. }
  61. #endif
  62. color = base.rgb;
  63. vec4 col = vec4(color.rgb, base.a);
  64. col.rgb *= varColor.rgb;
  65. col.rgb *= emissiveColor.rgb * vIDiff;
  66. #if ENABLE_TONE_MAPPING
  67. col = applyToneMapping(col);
  68. #endif
  69. // Due to a bug in some (older ?) graphics stacks (possibly in the glsl compiler ?),
  70. // the fog will only be rendered correctly if the last operation before the
  71. // clamp() is an addition. Else, the clamp() seems to be ignored.
  72. // E.g. the following won't work:
  73. // float clarity = clamp(fogShadingParameter
  74. // * (fogDistance - length(eyeVec)) / fogDistance), 0.0, 1.0);
  75. // As additions usually come for free following a multiplication, the new formula
  76. // should be more efficient as well.
  77. // Note: clarity = (1 - fogginess)
  78. float clarity = clamp(fogShadingParameter
  79. - fogShadingParameter * length(eyeVec) / fogDistance, 0.0, 1.0);
  80. col = mix(skyBgColor, col, clarity);
  81. gl_FragColor = vec4(col.rgb, base.a);
  82. }