#version 430 layout (binding = 0) uniform sampler2D worldPositionSamp; layout (binding = 1) uniform sampler2D worldNormalSamp; layout (binding = 2) uniform sampler2D worldColorSamp; layout (binding = 3) uniform sampler2D worldDepthSamp; layout (binding = 4) uniform sampler2D ssaoNoise; const int numberOfSamples = 32; const vec3 ssaoKernel[32] = { vec3( 0.001, 0.000, -0.000), vec3(-0.002, 0.002, -0.003), vec3( 0.004, -0.007, 0.002), vec3(-0.002, 0.012, 0.010), vec3(-0.012, 0.018, -0.011), vec3(-0.013, 0.023, -0.023), vec3(-0.005, -0.021, 0.043), vec3( 0.034, -0.039, 0.034), vec3(-0.063, -0.040, 0.027), vec3( 0.095, -0.025, -0.001), vec3( 0.048, -0.089, 0.062), vec3(-0.084, 0.041, -0.105), vec3(-0.148, -0.047, -0.056), vec3(-0.039, 0.026, -0.186), vec3(-0.161, 0.065, -0.135), vec3(-0.203, 0.123, -0.079), vec3( 0.216, 0.091, -0.157), vec3( 0.066, 0.305, -0.051), vec3(-0.084, -0.332, 0.083), vec3(-0.050, 0.351, -0.165), vec3(-0.330, 0.127, -0.245), vec3(-0.020, 0.319, 0.348), vec3(-0.396, 0.281, -0.178), vec3(-0.376, -0.413, -0.069), vec3( 0.510, 0.153, -0.298), vec3(-0.451, -0.476, -0.074), vec3(-0.596, -0.302, 0.245), vec3( 0.311, -0.490, -0.499), vec3(-0.439, 0.464, 0.517), vec3( 0.397, -0.557, 0.551), vec3( 0.632, -0.677, 0.151), vec3( 0.574, -0.246, 0.781) }; uniform mat4 proj; uniform mat4 view; uniform int width; uniform int height; const float radius = 0.25; in vec2 varTex; out float color; void main() { vec3 fragPos = texture(worldPositionSamp, varTex).xyz; vec3 random = texture(ssaoNoise, varTex * vec2(width * 0.25, height * 0.25)).xyz * radius * 0.5; float occlusion = 0.0; for(int i = 0; i < numberOfSamples; i++) { vec4 projFragPos = proj * vec4(fragPos + ssaoKernel[i] * radius + random, 1.0); projFragPos.xyz /= projFragPos.w; projFragPos.xyz = projFragPos.xyz * 0.5 + 0.5; float depth1 = texture(worldDepthSamp, projFragPos.xy).x; float depth2 = projFragPos.z; float rangeCheck = float(abs(depth2 - depth1) < radius); occlusion += float(depth2 > depth1) * rangeCheck; } color = 1 - occlusion / numberOfSamples; }