postprocessing.frag.ts 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /**
  2. * Copyright (c) 2019-2020 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. return 0.0;
  74. }
  75. void main(void) {
  76. vec2 coords = gl_FragCoord.xy / uTexSize;
  77. vec4 color = texture2D(tColor, coords);
  78. float viewDist;
  79. float fogFactor;
  80. #ifdef dOcclusionEnable
  81. float depth = getDepth(coords);
  82. if (!isBackground(depth)) {
  83. viewDist = abs(getViewZ(depth));
  84. fogFactor = smoothstep(uFogNear, uFogFar, viewDist);
  85. float occlusionFactor = getSsao(coords);
  86. if (!uTransparentBackground) {
  87. color.rgb = mix(mix(occlusionColor, uFogColor, fogFactor), color.rgb, occlusionFactor);
  88. } else {
  89. color.rgb = mix(occlusionColor * (1.0 - fogFactor), color.rgb, occlusionFactor);
  90. }
  91. }
  92. #endif
  93. // outline needs to be handled after occlusion to keep them clean
  94. #ifdef dOutlineEnable
  95. float closestTexel;
  96. float outline = getOutline(coords, closestTexel);
  97. if (outline == 0.0) {
  98. color.rgb *= outline;
  99. viewDist = abs(getViewZ(closestTexel));
  100. fogFactor = smoothstep(uFogNear, uFogFar, viewDist);
  101. if (!uTransparentBackground) {
  102. color.rgb = mix(color.rgb, uFogColor, fogFactor);
  103. } else {
  104. color.a = 1.0 - fogFactor;
  105. }
  106. }
  107. #endif
  108. gl_FragColor = color;
  109. }
  110. `;