ssao2.fragment.fx 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. // SSAO 2 Shader
  2. precision highp float;
  3. uniform sampler2D textureSampler;
  4. uniform float near;
  5. uniform float far;
  6. uniform float radius;
  7. varying vec2 vUV;
  8. float perspectiveDepthToViewZ( const in float invClipZ, const in float near, const in float far ) {
  9. return ( near * far ) / ( ( far - near ) * invClipZ - far );
  10. }
  11. float viewZToPerspectiveDepth( const in float viewZ, const in float near, const in float far ) {
  12. return ( near * far / viewZ + far) / ( far - near );
  13. }
  14. float viewZToOrthographicDepth( const in float viewZ, const in float near, const in float far ) {
  15. return ( viewZ + near ) / ( near - far );
  16. }
  17. #ifdef SSAO
  18. uniform sampler2D randomSampler;
  19. uniform sampler2D normalSampler;
  20. uniform float randTextureTiles;
  21. uniform float samplesFactor;
  22. uniform vec3 sampleSphere[SAMPLES];
  23. uniform float totalStrength;
  24. uniform float base;
  25. uniform float xViewport;
  26. uniform float yViewport;
  27. uniform float maxZ;
  28. uniform float minZAspect;
  29. uniform vec2 texelSize;
  30. uniform mat4 projection;
  31. void main()
  32. {
  33. vec3 random = texture2D(randomSampler, vUV * randTextureTiles).rgb;
  34. float depth = abs(texture2D(textureSampler, vUV).r);
  35. vec3 normal = texture2D(normalSampler, vUV).rgb;
  36. float occlusion = 0.0;
  37. float correctedRadius = min(radius, minZAspect * depth / near);
  38. vec3 vViewRay = vec3((vUV.x * 2.0 - 1.0)*xViewport, (vUV.y * 2.0 - 1.0)*yViewport, 1.0);
  39. vec3 origin = vViewRay * depth;
  40. vec3 rvec = random * 2.0 - 1.0;
  41. rvec.z = 0.0;
  42. vec3 tangent = normalize(rvec - normal * dot(rvec, normal));
  43. vec3 bitangent = cross(normal, tangent);
  44. mat3 tbn = mat3(tangent, bitangent, normal);
  45. float difference;
  46. if (depth > maxZ) {
  47. gl_FragColor = vec4(1.0, 1.0, 1.0, 1.0);
  48. return;
  49. }
  50. for (int i = 0; i < SAMPLES; ++i) {
  51. // get sample position:
  52. vec3 samplePosition = tbn * sampleSphere[i];
  53. samplePosition = samplePosition * correctedRadius + origin;
  54. // project sample position:
  55. vec4 offset = vec4(samplePosition, 1.0);
  56. offset = projection * offset;
  57. offset.xyz /= offset.w;
  58. offset.xy = offset.xy * 0.5 + 0.5;
  59. if (offset.x < 0.0 || offset.y < 0.0 || offset.x > 1.0 || offset.y > 1.0) {
  60. continue;
  61. }
  62. // get sample linearDepth:
  63. float sampleDepth = abs(texture2D(textureSampler, offset.xy).r);
  64. // range check & accumulate:
  65. float rangeCheck = abs(depth - sampleDepth) < correctedRadius ? 1.0 : 0.0;
  66. difference = samplePosition.z - sampleDepth;
  67. //occlusion += step(fallOff, difference) * (1.0 - smoothstep(fallOff, area, difference)) * rangeCheck;
  68. occlusion += (difference >= 1e-5 ? 1.0 : 0.0) * rangeCheck;
  69. }
  70. // float screenEdgeFactor = clamp(vUV.x * 10.0, 0.0, 1.0) * clamp(vUV.y * 10.0, 0.0, 1.0) * clamp((1.0 - vUV.x) * 10.0, 0.0, 1.0) * clamp((1.0 - vUV.y) * 10.0, 0.0, 1.0);
  71. float ao = 1.0 - totalStrength * occlusion * samplesFactor;
  72. float result = clamp(ao + base, 0.0, 1.0);
  73. gl_FragColor = vec4(vec3(result), 1.0);
  74. }
  75. #endif
  76. #ifdef BILATERAL_BLUR
  77. uniform sampler2D depthSampler;
  78. uniform float outSize;
  79. uniform float samplerOffsets[SAMPLES];
  80. vec4 blur9(sampler2D image, vec2 uv, float resolution, vec2 direction) {
  81. vec4 color = vec4(0.0);
  82. vec2 off1 = vec2(1.3846153846) * direction;
  83. vec2 off2 = vec2(3.2307692308) * direction;
  84. color += texture2D(image, uv) * 0.2270270270;
  85. color += texture2D(image, uv + (off1 / resolution)) * 0.3162162162;
  86. color += texture2D(image, uv - (off1 / resolution)) * 0.3162162162;
  87. color += texture2D(image, uv + (off2 / resolution)) * 0.0702702703;
  88. color += texture2D(image, uv - (off2 / resolution)) * 0.0702702703;
  89. return color;
  90. }
  91. vec4 blur13(sampler2D image, vec2 uv, float resolution, vec2 direction) {
  92. vec4 color = vec4(0.0);
  93. vec2 off1 = vec2(1.411764705882353) * direction;
  94. vec2 off2 = vec2(3.2941176470588234) * direction;
  95. vec2 off3 = vec2(5.176470588235294) * direction;
  96. color += texture2D(image, uv) * 0.1964825501511404;
  97. color += texture2D(image, uv + (off1 / resolution)) * 0.2969069646728344;
  98. color += texture2D(image, uv - (off1 / resolution)) * 0.2969069646728344;
  99. color += texture2D(image, uv + (off2 / resolution)) * 0.09447039785044732;
  100. color += texture2D(image, uv - (off2 / resolution)) * 0.09447039785044732;
  101. color += texture2D(image, uv + (off3 / resolution)) * 0.010381362401148057;
  102. color += texture2D(image, uv - (off3 / resolution)) * 0.010381362401148057;
  103. return color;
  104. }
  105. vec4 blur13Bilateral(sampler2D image, vec2 uv, float resolution, vec2 direction) {
  106. vec4 color = vec4(0.0);
  107. vec2 off1 = vec2(1.411764705882353) * direction;
  108. vec2 off2 = vec2(3.2941176470588234) * direction;
  109. vec2 off3 = vec2(5.176470588235294) * direction;
  110. float compareDepth = abs(texture2D(depthSampler, uv).r);
  111. float sampleDepth;
  112. float weight;
  113. float weightSum = 30.0;
  114. color += texture2D(image, uv) * 30.0;
  115. sampleDepth = abs(texture2D(depthSampler, uv + (off1 / resolution)).r);
  116. weight = clamp(1.0 / ( 0.003 + abs(compareDepth - sampleDepth)), 0.0, 30.0);
  117. weightSum += weight;
  118. color += texture2D(image, uv + (off1 / resolution)) * weight;
  119. sampleDepth = abs(texture2D(depthSampler, uv - (off1 / resolution)).r);
  120. weight = clamp(1.0 / ( 0.003 + abs(compareDepth - sampleDepth)), 0.0, 30.0);
  121. weightSum += weight;
  122. color += texture2D(image, uv - (off1 / resolution)) * weight;
  123. sampleDepth = abs(texture2D(depthSampler, uv + (off2 / resolution)).r);
  124. weight = clamp(1.0 / ( 0.003 + abs(compareDepth - sampleDepth)), 0.0, 30.0);
  125. weightSum += weight;
  126. color += texture2D(image, uv + (off2 / resolution)) * weight;
  127. sampleDepth = abs(texture2D(depthSampler, uv - (off2 / resolution)).r);
  128. weight = clamp(1.0 / ( 0.003 + abs(compareDepth - sampleDepth)), 0.0, 30.0);
  129. weightSum += weight;
  130. color += texture2D(image, uv - (off2 / resolution)) * weight;
  131. sampleDepth = abs(texture2D(depthSampler, uv + (off3 / resolution)).r);
  132. weight = clamp(1.0 / ( 0.003 + abs(compareDepth - sampleDepth)), 0.0, 30.0);
  133. weightSum += weight;
  134. color += texture2D(image, uv + (off3 / resolution)) * weight;
  135. sampleDepth = abs(texture2D(depthSampler, uv - (off3 / resolution)).r);
  136. weight = clamp(1.0 / ( 0.003 + abs(compareDepth - sampleDepth)), 0.0, 30.0);
  137. weightSum += weight;
  138. color += texture2D(image, uv - (off3 / resolution)) * weight;
  139. return color / weightSum;
  140. }
  141. void main()
  142. {
  143. #if EXPENSIVE
  144. float compareDepth = abs(texture2D(depthSampler, vUV).r);
  145. float texelsize = 1.0 / outSize;
  146. float result = 0.0;
  147. float weightSum = 0.0;
  148. for (int i = 0; i < SAMPLES; ++i)
  149. {
  150. #ifdef BILATERAL_BLUR_H
  151. vec2 direction = vec2(1.0, 0.0);
  152. vec2 sampleOffset = vec2(texelsize * samplerOffsets[i], 0.0);
  153. #else
  154. vec2 direction = vec2(0.0, 1.0);
  155. vec2 sampleOffset = vec2(0.0, texelsize * samplerOffsets[i]);
  156. #endif
  157. vec2 samplePos = vUV + sampleOffset;
  158. float sampleDepth = abs(texture2D(depthSampler, samplePos).r);
  159. float weight = clamp(1.0 / ( 0.003 + abs(compareDepth - sampleDepth)), 0.0, 30000.0);
  160. result += texture2D(textureSampler, samplePos).r * weight;
  161. weightSum += weight;
  162. }
  163. result /= weightSum;
  164. gl_FragColor.rgb = vec3(result);
  165. gl_FragColor.a = 1.0;
  166. #else
  167. vec4 color;
  168. #ifdef BILATERAL_BLUR_H
  169. vec2 direction = vec2(1.0, 0.0);
  170. color = blur13Bilateral(textureSampler, vUV, outSize, direction);
  171. #else
  172. vec2 direction = vec2(0.0, 1.0);
  173. color = blur13Bilateral(textureSampler, vUV, outSize, direction);
  174. #endif
  175. gl_FragColor.rgb = vec3(color.r);
  176. gl_FragColor.a = 1.0;
  177. #endif
  178. }
  179. #endif