0
\$\begingroup\$

I'm trying to implement SSAO based on this tutorial (and other). My setup:

  • XNA 4.0
  • Deferred
  • depth = z/w

This is result: enter image description here enter image description here enter image description here

Lines across buildings are moving when I move camera. So I'm wondering is this problem in code or in parameters?

Here is code:

    float3 normal_from_depth(float depth0, float2 texcoords) {

    const float2 offset1 = float2(0.0, 0.001);
    const float2 offset2 = float2(0.001, 0.0);

    float depth1 = dot(tex2D(depth, texcoords + offset1).rgb, byte_to_float);
    float depth2 = dot(tex2D(depth, texcoords + offset2).rgb, byte_to_float);

    float3 p1 = float3(offset1, depth1 - depth0);
    float3 p2 = float3(offset2, depth2 - depth0);

    float3 normal = cross(p1, p2);
    //normal.z = -normal.z;

    return normalize(normal);
}

float4 Ssao(VSIO input) : COLOR0
{
    const float total_strength = 1.0;
    const float base = 0.2;

    const float area = 0.75;
    const float falloff = 0.000001;

    const float radius = 0.002;

    const int samples = 16;
    float3 sample_sphere[samples] = {
        float3(0.5381, 0.1856, -0.4319), float3(0.1379, 0.2486, 0.4430),
        float3(0.3371, 0.5679, -0.0057), float3(-0.6999, -0.0451, -0.0019),
        float3(0.0689, -0.1598, -0.8547), float3(0.0560, 0.0069, -0.1843),
        float3(-0.0146, 0.1402, 0.0762), float3(0.0100, -0.1924, -0.0344),
        float3(-0.3577, -0.5301, -0.4358), float3(-0.3169, 0.1063, 0.0158),
        float3(0.0103, -0.5869, 0.0046), float3(-0.0897, -0.4940, 0.3287),
        float3(0.7119, -0.0154, -0.0918), float3(-0.0533, 0.0596, -0.5411),
        float3(0.0352, -0.0631, 0.5460), float3(-0.4776, 0.2847, -0.0271)
    };

    //float3 random = normalize(tex2D(noise, input.uv * 4.0).rgb);

    float depth0 = dot(tex2D(depth, input.uv).rgb, byte_to_float);

    float3 position = float3(input.uv, depth0);
    float3 normal = normal_from_depth(depth0, input.uv);

    float radius_depth = radius / depth0;
    float occlusion = 0.0;
    for (int i = 0; i < samples; i++) {

        float3 ray = radius_depth * sample_sphere[i];
        float3 hemi_ray = position + sign(dot(ray, normal)) * ray;

        float occ_depth = dot(tex2D(depth, saturate(hemi_ray.xy)).rgb, byte_to_float);
        float difference = depth0 - occ_depth;

        occlusion += step(falloff, difference) * (1.0 - smoothstep(falloff, area, difference));
    }

    float ao = 1.0 - total_strength * occlusion * (1.0 / samples);
    return float4(ao, ao, ao, 1.f);
}
\$\endgroup\$
1
\$\begingroup\$

I guess it's something wrong with your normal buffer. Render out the normal buffer to screen and make sure it's in view space. (or world if the depth is so)

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.