shadowMap.fragment.fx 960 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. #ifdef GL_ES
  2. precision highp float;
  3. #endif
  4. vec4 pack(float depth)
  5. {
  6. const vec4 bit_shift = vec4(255.0 * 255.0 * 255.0, 255.0 * 255.0, 255.0, 1.0);
  7. const vec4 bit_mask = vec4(0.0, 1.0 / 255.0, 1.0 / 255.0, 1.0 / 255.0);
  8. vec4 res = fract(depth * bit_shift);
  9. res -= res.xxyz * bit_mask;
  10. return res;
  11. }
  12. // Thanks to http://devmaster.net/
  13. vec2 packHalf(float depth)
  14. {
  15. const vec2 bitOffset = vec2(1.0 / 255., 0.);
  16. vec2 color = vec2(depth, fract(depth * 255.));
  17. return color - (color.yy * bitOffset);
  18. }
  19. varying vec4 vPosition;
  20. #ifdef ALPHATEST
  21. varying vec2 vUV;
  22. uniform sampler2D diffuseSampler;
  23. #endif
  24. void main(void)
  25. {
  26. #ifdef ALPHATEST
  27. if (texture2D(diffuseSampler, vUV).a < 0.4)
  28. discard;
  29. #endif
  30. float depth = vPosition.z / vPosition.w;
  31. depth = depth * 0.5 + 0.5;
  32. #ifdef VSM
  33. float moment1 = depth;
  34. float moment2 = moment1 * moment1;
  35. gl_FragColor = vec4(packHalf(moment1), packHalf(moment2));
  36. #else
  37. gl_FragColor = pack(depth);
  38. #endif
  39. }