ssaoFragment.fs 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. #version 430
  2. layout (binding = 0) uniform sampler2D worldPositionSamp;
  3. layout (binding = 1) uniform sampler2D worldDepthSamp;
  4. layout (binding = 2) uniform sampler2D ssaoNoise;
  5. const int numberOfSamples = 64;
  6. const vec3 ssaoKernel[64] = {
  7. vec3(0.011682, -0.003627, 0.009722), vec3(0.015764, 0.021744, -0.015976), vec3(-0.020045, 0.032629, -0.027033), vec3(0.023833, -0.009981, 0.056909),
  8. vec3(-0.022371, 0.002217, 0.074821), vec3(0.079837, 0.026033, 0.041683), vec3(-0.064111, 0.019135, -0.086525), vec3(-0.059657, -0.084172, 0.070577),
  9. vec3(-0.093833, -0.027073, -0.101182), vec3(-0.088102, 0.112365, -0.063453), vec3(0.006216, 0.163006, 0.054144), vec3(-0.154702, 0.104328, 0.018421),
  10. vec3(-0.002524, 0.185987, -0.081623), vec3(0.154714, 0.015249, 0.153891), vec3(-0.051003, 0.200151, -0.110770), vec3(-0.068254, 0.142356, 0.193846),
  11. vec3(-0.183547, 0.191688, 0.011090), vec3(-0.215181, -0.159997, 0.084850), vec3(0.191741, -0.074246, -0.214144), vec3(-0.230607, -0.020322, -0.209913),
  12. vec3(-0.127767, 0.229756, 0.196351), vec3(0.284982, -0.189491, 0.032289), vec3(-0.155238, 0.323741, 0.015594), vec3(0.128016, 0.024124, -0.351646),
  13. vec3(-0.039729, 0.275108, 0.274455), vec3(0.230047, -0.224585, 0.248353), vec3(0.213946, -0.223074, 0.287130), vec3(-0.286808, -0.051429, 0.326347),
  14. vec3(0.325037, -0.167517, -0.267603), vec3(0.400496, -0.152350, 0.190050), vec3(0.450418, 0.087465, 0.155219), vec3(0.321040, -0.054098, 0.379482),
  15. vec3(-0.138326, 0.428701, 0.250899), vec3(0.436693, -0.018605, -0.301960), vec3(0.347054, 0.323834, -0.271583), vec3(0.520263, 0.192617, -0.092904),
  16. vec3(0.182182, -0.333513, 0.435669), vec3(-0.337270, -0.092780, -0.479771), vec3(-0.490179, -0.351030, 0.088546), vec3(-0.098339, -0.389115, 0.479107),
  17. vec3(-0.466230, -0.439320, -0.005353), vec3(0.243684, 0.453502, 0.406962), vec3(0.373559, -0.236580, 0.505865), vec3(-0.249184, -0.390539, -0.507979),
  18. vec3(0.135120, -0.408752, -0.555922), vec3(0.315168, -0.508461, 0.398420), vec3(-0.414646, 0.302526, -0.525217), vec3(0.531577, -0.528467, 0.025467),
  19. vec3(-0.485158, -0.389484, 0.446213), vec3(0.333460, 0.224401, 0.669925), vec3(0.221304, 0.412168, -0.645098), vec3(-0.531421, 0.029417, -0.613908),
  20. vec3(-0.680806, -0.467509, -0.061069), vec3(0.648694, 0.148779, 0.518628), vec3(-0.510980, -0.390245, 0.570205), vec3(-0.419819, 0.553692, -0.531791),
  21. vec3(0.580169, -0.519732, 0.431852), vec3(-0.497161, -0.576404, 0.491811), vec3(0.182698, -0.621941, -0.655488), vec3(-0.190361, 0.725918, 0.561883),
  22. vec3(-0.789754, 0.294172, 0.445196), vec3(-0.103338, 0.408322, -0.872394), vec3(0.093390, 0.779951, -0.593253), vec3(0.787384, 0.123125, 0.604042)
  23. };
  24. uniform mat4 proj;
  25. uniform int width;
  26. uniform int height;
  27. uniform vec3 eyePos;
  28. in vec2 varTex;
  29. out float color;
  30. void main() {
  31. vec3 fragPos = texture(worldPositionSamp, varTex).xyz;
  32. float radius = max(-fragPos.z * 0.02, 0.25);
  33. vec3 random = texture(ssaoNoise, varTex * vec2(width * 0.25, height * 0.25)).xyz * radius * 0.25;
  34. float occlusion = 0.0;
  35. for(int i = 0; i < numberOfSamples; i++) {
  36. vec4 projFragPos = proj * vec4(fragPos + ssaoKernel[i] * radius + random, 1.0);
  37. projFragPos.xyz /= projFragPos.w;
  38. float depth1 = texture(worldDepthSamp, projFragPos.xy).x;
  39. float depth2 = projFragPos.z;
  40. occlusion += float(depth2 >= depth1);
  41. }
  42. color = 1 - occlusion / numberOfSamples;
  43. }