background.fs 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. #version 430
  2. layout (binding = 0) uniform sampler2D depthSamp;
  3. const float minVariance = 4.096e-06;
  4. const float cutoff = 0.998;
  5. in vec4 varShadowPosition;
  6. out vec4 color;
  7. float chebyshevUpperBound(vec2 moments, float t) {
  8. float variance = moments.y - (moments.x * moments.x);
  9. variance = max(variance, minVariance);
  10. float d = t - moments.x;
  11. float pMax = variance / (variance + d * d);
  12. return max(pMax, float(t < moments.x));
  13. }
  14. float linstep(float from, float to, float v) {
  15. return clamp((v - from) / (to - from), 0.0, 1.0);
  16. }
  17. float reduceLightBleeding(float light, float Amount) {
  18. return linstep(Amount, 1.0, light);
  19. }
  20. float getShadow(vec2 varTex, float distanceToLight) {
  21. vec2 Moments = texture(depthSamp, varTex).xy;
  22. return reduceLightBleeding(chebyshevUpperBound(Moments, distanceToLight), cutoff);
  23. }
  24. void main(void) {
  25. vec3 pos = varShadowPosition.xyz / varShadowPosition.w;
  26. float shadow = getShadow(pos.xy, pos.z);
  27. shadow = shadow * 0.6 + 0.4;
  28. color = vec4(0.8, 0.8, 0.8, 1.0) * shadow;
  29. }