vsm_frag.glsl.js 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. export default /* glsl */`
  2. uniform sampler2D shadow_pass;
  3. uniform vec2 resolution;
  4. uniform float radius;
  5. #include <packing>
  6. void main() {
  7. float mean = 0.0;
  8. float squared_mean = 0.0;
  9. // This seems totally useless but it's a crazy work around for a Adreno compiler bug
  10. float depth = unpackRGBAToDepth( texture2D( shadow_pass, ( gl_FragCoord.xy ) / resolution ) );
  11. for ( float i = -1.0; i < 1.0 ; i += SAMPLE_RATE) {
  12. #ifdef HORIZONAL_PASS
  13. vec2 distribution = decodeHalfRGBA ( texture2D( shadow_pass, ( gl_FragCoord.xy + vec2( i, 0.0 ) * radius ) / resolution ) );
  14. mean += distribution.x;
  15. squared_mean += distribution.y * distribution.y + distribution.x * distribution.x;
  16. #else
  17. float depth = unpackRGBAToDepth( texture2D( shadow_pass, ( gl_FragCoord.xy + vec2( 0.0, i ) * radius ) / resolution ) );
  18. mean += depth;
  19. squared_mean += depth * depth;
  20. #endif
  21. }
  22. mean = mean * HALF_SAMPLE_RATE;
  23. squared_mean = squared_mean * HALF_SAMPLE_RATE;
  24. float std_dev = pow( squared_mean - mean * mean, 0.5 );
  25. gl_FragColor = encodeHalfRGBA( vec2( mean, std_dev ) );
  26. }
  27. `;