shadowMap.fragment.fx 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. #ifndef FULLFLOAT
  2. vec4 pack(float depth)
  3. {
  4. const vec4 bit_shift = vec4(255.0 * 255.0 * 255.0, 255.0 * 255.0, 255.0, 1.0);
  5. const vec4 bit_mask = vec4(0.0, 1.0 / 255.0, 1.0 / 255.0, 1.0 / 255.0);
  6. vec4 res = fract(depth * bit_shift);
  7. res -= res.xxyz * bit_mask;
  8. return res;
  9. }
  10. #endif
  11. varying vec4 vPosition;
  12. #ifdef ALPHATEST
  13. varying vec2 vUV;
  14. uniform sampler2D diffuseSampler;
  15. #endif
  16. #ifdef CUBEMAP
  17. uniform vec3 lightPosition;
  18. uniform vec2 depthValues;
  19. #endif
  20. uniform vec2 biasAndScale;
  21. void main(void)
  22. {
  23. #ifdef ALPHATEST
  24. if (texture2D(diffuseSampler, vUV).a < 0.4)
  25. discard;
  26. #endif
  27. #ifdef CUBEMAP
  28. vec3 directionToLight = vPosition.xyz - lightPosition;
  29. float depth = length(directionToLight);
  30. depth = (depth - depthValues.x) / (depthValues.y - depthValues.x);
  31. depth = clamp(depth, 0., 1.0);
  32. #else
  33. float depth = vPosition.z / vPosition.w;
  34. depth = depth * 0.5 + 0.5;
  35. #endif
  36. depth += biasAndScale.x;
  37. #ifdef ESM
  38. depth = exp(-min(87., biasAndScale.y * depth));
  39. #endif
  40. #ifndef FULLFLOAT
  41. gl_FragColor = pack(depth);
  42. #else
  43. gl_FragColor = vec4(depth, 1.0, 1.0, 1.0);
  44. #endif
  45. }