I'm having a problem with figuring out spaces during SSAO calculation. What i'm trying to achieve is basic ssao effectwith depth reconstruction (before diving in to more complicated normals reconstructions).
As far as i understand, most tutorial teach how to compute AO in view/eye space, which lead me to this code that recreates View Space Positions from depth:
vec3 viewSpacePosFromDepth(vec2 uv)
{
float depth = texture(depthTexture, uv).r; //Depth texture is standard Z/W depth
vec4 clipSpacePosition = vec4(uv * 2.0 - 1.0, 2.0 * depth - 1.0, 1.0);
vec4 position = InverseProjection * clipSpacePosition;
return position.xyz / position.w;
}
Results looks correct (here, slightly scaled):
However, when i try to apply SSAO, result is rotating with camera: https://streamable.com/twarwy (also note different result on left and right of a screen, with similar geometry)
Code itself is rather simple:
void main()
{
vec3 pos = viewSpacePosFromDepth(TexCoord); // View space position
float ao = 0.0;
for (int i = 0; i < KERNEL_SIZE; i++)
{
vec3 samplePos = pos + Samples[i]; // Samples are random points in [-1, 1] space, so we're still in View Space
vec4 offset = vec4(samplePos, 1.0);
offset = Projection * offset; // Moving back to clip space
offset.xy /= offset.w;
offset.xy = offset.xy * 0.5 + vec2(0.5);
float sampleDepth = viewSpacePosFromDepth(offset.xy).z; // again, sampleDepth is in View space
if (abs(pos.z - sampleDepth) < radius)
{
ao += step(sampleDepth, samplePos.z);
}
}
ao = 1.0 - AO / KERNEL_SIZE;
fragColor = vec4(pow(ao, 2.0));
}
I'm not following, why the result is rotating with camera, when i'm doing all the calculations in View Space. Am i missing something?