postprocessing.frag.ts 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /**
  2. * Copyright (c) 2019-2021 mol* contributors, licensed under MIT, See LICENSE file for more info.
  3. *
  4. * @author Alexander Rose <alexander.rose@weirdbyte.de>
  5. * @author Áron Samuel Kovács <aron.kovacs@mail.muni.cz>
  6. */
  7. export const postprocessing_frag = `
  8. precision highp float;
  9. precision highp int;
  10. precision highp sampler2D;
  11. uniform sampler2D tSsaoDepth;
  12. uniform sampler2D tColor;
  13. uniform sampler2D tDepth;
  14. uniform sampler2D tOutlines;
  15. uniform vec2 uTexSize;
  16. uniform float uNear;
  17. uniform float uFar;
  18. uniform float uFogNear;
  19. uniform float uFogFar;
  20. uniform vec3 uFogColor;
  21. uniform bool uTransparentBackground;
  22. uniform float uOcclusionBias;
  23. uniform float uOcclusionRadius;
  24. uniform float uOutlineScale;
  25. uniform float uOutlineThreshold;
  26. uniform float uMaxPossibleViewZDiff;
  27. const vec3 occlusionColor = vec3(0.0);
  28. #include common
  29. float getViewZ(const in float depth) {
  30. #if dOrthographic == 1
  31. return orthographicDepthToViewZ(depth, uNear, uFar);
  32. #else
  33. return perspectiveDepthToViewZ(depth, uNear, uFar);
  34. #endif
  35. }
  36. float getDepth(const in vec2 coords) {
  37. return unpackRGBAToDepth(texture2D(tDepth, coords));
  38. }
  39. bool isBackground(const in float depth) {
  40. return depth == 1.0;
  41. }
  42. float getOutline(const in vec2 coords, out float closestTexel) {
  43. float backgroundViewZ = uFar + 3.0 * uMaxPossibleViewZDiff;
  44. vec2 invTexSize = 1.0 / uTexSize;
  45. float selfDepth = getDepth(coords);
  46. float selfViewZ = isBackground(selfDepth) ? backgroundViewZ : getViewZ(selfDepth);
  47. float outline = 1.0;
  48. closestTexel = 1.0;
  49. for (int y = -dOutlineScale; y <= dOutlineScale; y++) {
  50. for (int x = -dOutlineScale; x <= dOutlineScale; x++) {
  51. if (x * x + y * y > dOutlineScale * dOutlineScale) {
  52. continue;
  53. }
  54. vec2 sampleCoords = coords + vec2(float(x), float(y)) * invTexSize;
  55. vec4 sampleOutlineCombined = texture2D(tOutlines, sampleCoords);
  56. float sampleOutline = sampleOutlineCombined.r;
  57. float sampleOutlineDepth = unpackRGToUnitInterval(sampleOutlineCombined.gb);
  58. if (sampleOutline == 0.0 && sampleOutlineDepth < closestTexel && abs(selfViewZ - sampleOutlineDepth) > uMaxPossibleViewZDiff) {
  59. outline = 0.0;
  60. closestTexel = sampleOutlineDepth;
  61. }
  62. }
  63. }
  64. return outline;
  65. }
  66. float getSsao(vec2 coords) {
  67. float rawSsao = unpackRGToUnitInterval(texture2D(tSsaoDepth, coords).xy);
  68. if (rawSsao > 0.999) {
  69. return 1.0;
  70. } else if (rawSsao > 0.001) {
  71. return rawSsao;
  72. }
  73. // treat values close to 0.0 as errors and return no occlusion
  74. return 1.0;
  75. }
  76. void main(void) {
  77. vec2 coords = gl_FragCoord.xy / uTexSize;
  78. vec4 color = texture2D(tColor, coords);
  79. float viewDist;
  80. float fogFactor;
  81. #ifdef dOcclusionEnable
  82. float depth = getDepth(coords);
  83. if (!isBackground(depth)) {
  84. viewDist = abs(getViewZ(depth));
  85. fogFactor = smoothstep(uFogNear, uFogFar, viewDist);
  86. float occlusionFactor = getSsao(coords);
  87. if (!uTransparentBackground) {
  88. color.rgb = mix(mix(occlusionColor, uFogColor, fogFactor), color.rgb, occlusionFactor);
  89. } else {
  90. color.rgb = mix(occlusionColor * (1.0 - fogFactor), color.rgb, occlusionFactor);
  91. }
  92. }
  93. #endif
  94. // outline needs to be handled after occlusion to keep them clean
  95. #ifdef dOutlineEnable
  96. float closestTexel;
  97. float outline = getOutline(coords, closestTexel);
  98. if (outline == 0.0) {
  99. color.rgb *= outline;
  100. viewDist = abs(getViewZ(closestTexel));
  101. fogFactor = smoothstep(uFogNear, uFogFar, viewDist);
  102. if (!uTransparentBackground) {
  103. color.rgb = mix(color.rgb, uFogColor, fogFactor);
  104. } else {
  105. color.a = 1.0 - fogFactor;
  106. }
  107. }
  108. #endif
  109. gl_FragColor = color;
  110. }
  111. `;