OneTextureBlend.fsh 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. #version 100
  2. precision mediump float;
  3. /* Uniforms */
  4. uniform int uTextureUsage0;
  5. uniform sampler2D uTextureUnit0;
  6. uniform int uBlendType;
  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 Color0 = vVertexColor;
  40. vec4 Color1 = vec4(1.0, 1.0, 1.0, 1.0);
  41. if (bool(uTextureUsage0))
  42. Color1 = texture2D(uTextureUnit0, vTextureCoord0);
  43. vec4 FinalColor = Color0 * Color1;
  44. if (uBlendType == 1)
  45. {
  46. FinalColor.w = Color0.w;
  47. }
  48. else if (uBlendType == 2)
  49. {
  50. FinalColor.w = Color1.w;
  51. }
  52. if (bool(uFogEnable))
  53. {
  54. float FogFactor = computeFog();
  55. vec4 FogColor = uFogColor;
  56. FogColor.a = 1.0;
  57. FinalColor = mix(FogColor, FinalColor, FogFactor);
  58. }
  59. gl_FragColor = FinalColor;
  60. }