2
0

Solid.fsh 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. #version 100
  2. precision mediump float;
  3. /* Uniforms */
  4. uniform int uTextureUsage0;
  5. uniform sampler2D uTextureUnit0;
  6. uniform int uFogEnable;
  7. uniform int uFogType;
  8. uniform vec4 uFogColor;
  9. uniform float uFogStart;
  10. uniform float uFogEnd;
  11. uniform float uFogDensity;
  12. /* Varyings */
  13. varying vec2 vTextureCoord0;
  14. varying vec4 vVertexColor;
  15. varying float vFogCoord;
  16. float computeFog()
  17. {
  18. const float LOG2 = 1.442695;
  19. float FogFactor = 0.0;
  20. if (uFogType == 0) // Exp
  21. {
  22. FogFactor = exp2(-uFogDensity * vFogCoord * LOG2);
  23. }
  24. else if (uFogType == 1) // Linear
  25. {
  26. float Scale = 1.0 / (uFogEnd - uFogStart);
  27. FogFactor = (uFogEnd - vFogCoord) * Scale;
  28. }
  29. else if (uFogType == 2) // Exp2
  30. {
  31. FogFactor = exp2(-uFogDensity * uFogDensity * vFogCoord * vFogCoord * LOG2);
  32. }
  33. FogFactor = clamp(FogFactor, 0.0, 1.0);
  34. return FogFactor;
  35. }
  36. void main()
  37. {
  38. vec4 Color = vVertexColor;
  39. if (bool(uTextureUsage0))
  40. Color *= texture2D(uTextureUnit0, vTextureCoord0);
  41. if (bool(uFogEnable))
  42. {
  43. float FogFactor = computeFog();
  44. vec4 FogColor = uFogColor;
  45. FogColor.a = 1.0;
  46. Color = mix(FogColor, Color, FogFactor);
  47. }
  48. gl_FragColor = Color;
  49. }