ssao-blur.frag.ts 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /**
  2. * Copyright (c) 2019-2020 mol* contributors, licensed under MIT, See LICENSE file for more info.
  3. *
  4. * @author Áron Samuel Kovács <aron.kovacs@mail.muni.cz>
  5. */
  6. export default `
  7. precision highp float;
  8. precision highp int;
  9. precision highp sampler2D;
  10. uniform sampler2D tSsaoDepth;
  11. uniform vec2 uTexSize;
  12. uniform float uKernel[dOcclusionKernelSize];
  13. uniform float uBlurDirectionX;
  14. uniform float uBlurDirectionY;
  15. uniform float uMaxPossibleViewZDiff;
  16. uniform float uNear;
  17. uniform float uFar;
  18. #include common
  19. float getViewZ(const in float depth) {
  20. #if dOrthographic == 1
  21. return orthographicDepthToViewZ(depth, uNear, uFar);
  22. #else
  23. return perspectiveDepthToViewZ(depth, uNear, uFar);
  24. #endif
  25. }
  26. bool isBackground(const in float depth) {
  27. return depth >= 0.99;
  28. }
  29. void main(void) {
  30. vec2 coords = gl_FragCoord.xy / uTexSize;
  31. vec2 packedDepth = texture2D(tSsaoDepth, coords).zw;
  32. float selfDepth = unpackRGToUnitInterval(packedDepth);
  33. // if background and if second pass
  34. if (isBackground(selfDepth) && uBlurDirectionY != 0.0) {
  35. gl_FragColor = vec4(packUnitIntervalToRG(1.0), packedDepth);
  36. return;
  37. }
  38. float selfViewZ = getViewZ(selfDepth);
  39. vec2 offset = vec2(uBlurDirectionX, uBlurDirectionY) / uTexSize;
  40. float sum = 0.0;
  41. float kernelSum = 0.0;
  42. // only if kernelSize is odd
  43. for (int i = -dOcclusionKernelSize / 2; i <= dOcclusionKernelSize / 2; i++) {
  44. vec2 sampleCoords = coords + float(i) * offset;
  45. vec4 sampleSsaoDepth = texture2D(tSsaoDepth, sampleCoords);
  46. float sampleDepth = unpackRGToUnitInterval(sampleSsaoDepth.zw);
  47. if (isBackground(sampleDepth)) {
  48. continue;
  49. }
  50. if (abs(float(i)) > 1.0) { // abs is not defined for int in webgl1
  51. float sampleViewZ = getViewZ(sampleDepth);
  52. if (abs(selfViewZ - sampleViewZ) > uMaxPossibleViewZDiff) {
  53. continue;
  54. }
  55. }
  56. float kernel = uKernel[int(abs(float(i)))]; // abs is not defined for int in webgl1
  57. float sampleValue = unpackRGToUnitInterval(sampleSsaoDepth.xy);
  58. sum += kernel * sampleValue;
  59. kernelSum += kernel;
  60. }
  61. gl_FragColor = vec4(packUnitIntervalToRG(sum / kernelSum), packedDepth);
  62. }
  63. `;