Açıklama Yok

ssao.frag 2.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. #version 450
  2. #extension GL_ARB_separate_shader_objects : enable
  3. #extension GL_ARB_shading_language_420pack : enable
  4. layout (binding = 0) uniform sampler2D samplerPositionDepth;
  5. layout (binding = 1) uniform sampler2D samplerNormal;
  6. layout (binding = 2) uniform sampler2D ssaoNoise;
  7. layout (constant_id = 0) const int SSAO_KERNEL_SIZE = 64;
  8. layout (constant_id = 1) const float SSAO_RADIUS = 0.5;
  9. layout (binding = 3) uniform UBOSSAOKernel
  10. {
  11. vec4 samples[SSAO_KERNEL_SIZE];
  12. } uboSSAOKernel;
  13. layout (binding = 4) uniform UBO
  14. {
  15. mat4 projection;
  16. } ubo;
  17. layout (location = 0) in vec2 inUV;
  18. layout (location = 0) out float outFragColor;
  19. void main()
  20. {
  21. // Get G-Buffer values
  22. vec3 fragPos = texture(samplerPositionDepth, inUV).rgb;
  23. vec3 normal = normalize(texture(samplerNormal, inUV).rgb * 2.0 - 1.0);
  24. // Get a random vector using a noise lookup
  25. ivec2 texDim = textureSize(samplerPositionDepth, 0);
  26. ivec2 noiseDim = textureSize(ssaoNoise, 0);
  27. const vec2 noiseUV = vec2(float(texDim.x)/float(noiseDim.x), float(texDim.y)/(noiseDim.y)) * inUV;
  28. vec3 randomVec = texture(ssaoNoise, noiseUV).xyz * 2.0 - 1.0;
  29. // Create TBN matrix
  30. vec3 tangent = normalize(randomVec - normal * dot(randomVec, normal));
  31. vec3 bitangent = cross(tangent, normal);
  32. mat3 TBN = mat3(tangent, bitangent, normal);
  33. // Calculate occlusion value
  34. float occlusion = 0.0f;
  35. for(int i = 0; i < SSAO_KERNEL_SIZE; i++)
  36. {
  37. vec3 samplePos = TBN * uboSSAOKernel.samples[i].xyz;
  38. samplePos = fragPos + samplePos * SSAO_RADIUS;
  39. // project
  40. vec4 offset = vec4(samplePos, 1.0f);
  41. offset = ubo.projection * offset;
  42. offset.xyz /= offset.w;
  43. offset.xyz = offset.xyz * 0.5f + 0.5f;
  44. float sampleDepth = -texture(samplerPositionDepth, offset.xy).w;
  45. #define RANGE_CHECK 1
  46. #ifdef RANGE_CHECK
  47. // Range check
  48. float rangeCheck = smoothstep(0.0f, 1.0f, SSAO_RADIUS / abs(fragPos.z - sampleDepth));
  49. occlusion += (sampleDepth >= samplePos.z ? 1.0f : 0.0f) * rangeCheck;
  50. #else
  51. occlusion += (sampleDepth >= samplePos.z ? 1.0f : 0.0f);
  52. #endif
  53. }
  54. occlusion = 1.0 - (occlusion / float(SSAO_KERNEL_SIZE));
  55. outFragColor = occlusion;
  56. }