2
0

TransparentAlphaChannel.fsh 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. #version 100
  2. precision mediump float;
  3. /* Uniforms */
  4. uniform float uAlphaRef;
  5. uniform int uTextureUsage0;
  6. uniform sampler2D uTextureUnit0;
  7. uniform int uFogEnable;
  8. uniform int uFogType;
  9. uniform vec4 uFogColor;
  10. uniform float uFogStart;
  11. uniform float uFogEnd;
  12. uniform float uFogDensity;
  13. /* Varyings */
  14. varying vec2 vTextureCoord0;
  15. varying vec4 vVertexColor;
  16. varying float vFogCoord;
  17. float computeFog()
  18. {
  19. const float LOG2 = 1.442695;
  20. float FogFactor = 0.0;
  21. if (uFogType == 0) // Exp
  22. {
  23. FogFactor = exp2(-uFogDensity * vFogCoord * LOG2);
  24. }
  25. else if (uFogType == 1) // Linear
  26. {
  27. float Scale = 1.0 / (uFogEnd - uFogStart);
  28. FogFactor = (uFogEnd - vFogCoord) * Scale;
  29. }
  30. else if (uFogType == 2) // Exp2
  31. {
  32. FogFactor = exp2(-uFogDensity * uFogDensity * vFogCoord * vFogCoord * LOG2);
  33. }
  34. FogFactor = clamp(FogFactor, 0.0, 1.0);
  35. return FogFactor;
  36. }
  37. void main()
  38. {
  39. vec4 Color = vVertexColor;
  40. if (bool(uTextureUsage0))
  41. {
  42. Color *= texture2D(uTextureUnit0, vTextureCoord0);
  43. // TODO: uAlphaRef should rather control sharpness of alpha, don't know how to do that right now and this works in most cases.
  44. if (Color.a < uAlphaRef)
  45. discard;
  46. }
  47. if (bool(uFogEnable))
  48. {
  49. float FogFactor = computeFog();
  50. vec4 FogColor = uFogColor;
  51. FogColor.a = 1.0;
  52. Color = mix(FogColor, Color, FogFactor);
  53. }
  54. gl_FragColor = Color;
  55. }