ssaoFragment.fs 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. #version 430
  2. layout (binding = 0) uniform sampler2D worldPositionSamp;
  3. layout (binding = 1) uniform sampler2D worldNormalSamp;
  4. layout (binding = 2) uniform sampler2D worldColorSamp;
  5. layout (binding = 3) uniform sampler2D worldDepthSamp;
  6. layout (binding = 4) uniform sampler2D ssaoNoise;
  7. const int numberOfSamples = 64;
  8. const vec3 ssaoKernel[64] =
  9. {
  10. vec3( 0.009, 0.006, -0.007), vec3( 0.008, -0.003, 0.014), vec3( 0.004, -0.005, -0.019), vec3( 0.016, -0.012, -0.014),
  11. vec3(-0.022, -0.018, -0.007), vec3(-0.018, -0.028, -0.006), vec3( 0.023, -0.028, 0.016), vec3( 0.010, -0.044, 0.000),
  12. vec3( 0.037, -0.035, 0.007), vec3( 0.028, 0.015, -0.048), vec3( 0.030, -0.023, 0.053), vec3(-0.003, 0.042, 0.059),
  13. vec3( 0.077, -0.020, -0.010), vec3( 0.029, -0.053, 0.064), vec3( 0.043, 0.058, -0.064), vec3( 0.102, 0.004, 0.028),
  14. vec3(-0.014, -0.110, 0.029), vec3(-0.019, 0.056, -0.110), vec3(-0.094, 0.019, -0.095), vec3(-0.092, 0.030, -0.108),
  15. vec3(-0.072, -0.120, 0.070), vec3(-0.071, 0.091, -0.121), vec3(-0.096, 0.148, -0.033), vec3(-0.080, 0.160, -0.068),
  16. vec3(-0.037, 0.189, -0.066), vec3( 0.024, -0.038, -0.212), vec3(-0.228, -0.018, 0.021), vec3(-0.169, 0.175, 0.007),
  17. vec3( 0.064, -0.150, 0.200), vec3(-0.017, -0.156, -0.223), vec3(-0.092, 0.240, 0.128), vec3(-0.264, -0.140, 0.044),
  18. vec3(-0.063, -0.311, -0.024), vec3(-0.232, -0.199, 0.136), vec3(-0.333, -0.005, 0.111), vec3( 0.245, -0.255, 0.099),
  19. vec3( 0.214, 0.198, -0.251), vec3( 0.192, -0.325, 0.139), vec3(-0.270, 0.294, -0.131), vec3( 0.303, -0.281, -0.147),
  20. vec3(-0.140, -0.275, 0.338), vec3( 0.387, -0.147, 0.237), vec3( 0.380, -0.260, 0.185), vec3(-0.172, 0.292, -0.390),
  21. vec3( 0.307, -0.081, -0.433), vec3( 0.065, -0.492, 0.256), vec3( 0.224, 0.274, -0.458), vec3( 0.131, -0.370, -0.455),
  22. vec3( 0.113, -0.508, -0.341), vec3(-0.012, 0.573, 0.295), vec3( 0.028, -0.417, -0.521), vec3(-0.555, 0.409, -0.037),
  23. vec3(-0.372, 0.605, 0.075), vec3( 0.352, -0.567, -0.316), vec3( 0.507, 0.558, 0.118), vec3( 0.460, 0.548, 0.329),
  24. vec3(-0.414, -0.391, 0.580), vec3( 0.792, 0.248, -0.122), vec3( 0.207, -0.654, -0.526), vec3( 0.223, -0.713, -0.485),
  25. vec3(-0.333, -0.446, -0.729), vec3( 0.757, 0.249, 0.506), vec3(-0.896, -0.145, -0.349), vec3(-0.125, -0.870, -0.476)
  26. };
  27. uniform mat4 proj;
  28. uniform int width;
  29. uniform int height;
  30. const float radius = 0.25;
  31. in vec2 varTex;
  32. out float color;
  33. void main()
  34. {
  35. vec3 fragPos = texture(worldPositionSamp, varTex).xyz;
  36. vec3 random = texture(ssaoNoise, varTex * vec2(width * 0.25, height * 0.25)).xyz * radius * 0.25;
  37. float occlusion = 0.0;
  38. for(int i = 0; i < numberOfSamples; i++)
  39. {
  40. vec4 projFragPos = proj * vec4(fragPos + ssaoKernel[i] * radius + random, 1.0);
  41. projFragPos.xyz /= projFragPos.w;
  42. float depth1 = texture(worldDepthSamp, projFragPos.xy).x;
  43. float depth2 = projFragPos.z;
  44. float rangeCheck = float(abs(depth2 - depth1) < radius);
  45. occlusion += float(depth2 >= depth1) * rangeCheck;
  46. }
  47. color = 1 - occlusion / numberOfSamples;
  48. }