# SSAO camera angle issue

This topic is 1672 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Trying to get some decent SSAO running and I've come upon an annoying problem. These 3 images are of the same area in the level. The first 2 are roughly correct (I hope). Ignore the weird background, separate issue.

See images:

[attachment=16442:ssao1.jpg][attachment=16443:ssao2.jpg][attachment=16444:ssao3.jpg]

For about 75% of viewing angles it looks consistent. However when the camera then faces one direction (image 3), it starts playing up and darkening most objects. What could be causing this? The depth map and normal map both look fine and it's not the gaussian blur either. The main hlsl shader code is as follows:

float Radius;
float RandomTile;
float Bias;
float FarClip;

texture DepthBuffer;
sampler2D depthSampler = sampler_state
{
Texture = <DepthBuffer>;
MipFilter = NONE;
MagFilter = POINT;
MinFilter = POINT;
};

texture NormalBuffer;
sampler2D normalSampler = sampler_state
{
Texture = <NormalBuffer>;
MipFilter = NONE;
MagFilter = LINEAR;
MinFilter = LINEAR;
};

texture RandomMap;
sampler2D randomSampler = sampler_state
{
Texture = <RandomMap>;
MipFilter = NONE;
MagFilter = POINT;
MinFilter = POINT;
};

{
float4 Position : POSITION0;
float2 TexCoord : TEXCOORD0;
};

{
float4 Position : POSITION0;
float2 TexCoord : TEXCOORD0;
};

{

output.Position = float4(input.Position.xyz,1);
output.TexCoord = input.TexCoord;

return output;
}

half3 DecodeNormal (half4 enc)
{
float kScale = 1.7777;
float3 nn = enc.xyz*float3(2*kScale,2*kScale,0) + float3(-kScale,-kScale,1);
float g = 2.0 / dot(nn.xyz,nn.xyz);
float3 n;
n.xy = g*nn.xy;
n.z = g-1;
return n;
}

#define SAMPLE_COUNT 13
float3 RAND_SAMPLES[SAMPLE_COUNT] =
{
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.0352,-0.0631, 0.5460)
};

{
float depth =tex2D(depthSampler,input.TexCoord).r;
clip(-depth + 0.9999f);
float totalOcclusion = 0;
depth *= FarClip;
half3 normal = DecodeNormal(tex2D(normalSampler, input.TexCoord));
normal.y = -normal.y;
half3 normalScaled = normal * 0.25f;
half3 randNormal = (tex2D(randomSampler, input.TexCoord*RandomTile).rgb* 2.0 - 1.0);
for (int i = 0; i < SAMPLE_COUNT; i++)
{
half3 randomDirection = reflect(RAND_SAMPLES[i], randNormal);
randomDirection *= sign( dot(normal , randomDirection) );
randomDirection += normalScaled;
half modifiedDepth = depth -(randomDirection.z * Radius.x);
half2 offset = randomDirection.xy * scale;
float newDepth = tex2D(depthSampler, input.TexCoord + offset).r * FarClip;
float deltaDepth = saturate(modifiedDepth - newDepth );
}
totalOcclusion /= SAMPLE_COUNT;

}

technique SSAO
{
pass Pass1
{
}
}


Help!

##### Share on other sites

Ok so it's definitely a problem with the normal buffer. Not entirely sure what though.....?

##### Share on other sites

- Some thing to check : are you normalizing normals before encoding them?

- I had issues with the used encoding/decoding method : I had to negate the normal before encoding and after decoding (logically).

normal.y = -normal.y; <- why?

Cheers!

Edited by kauna

##### Share on other sites

Figured it out, finally. I wasn't getting the normals into view space so they were only correct for what I assume is the default view. That's sorted but now I'm having further issues with halo-ing. On 80% of models, it works as intended. However, if you look at the palm tree in the first image for example, you'll see it's darkening the right-hand side of the tree. I'm finding that I get an annoying amount of halo-ing for any radius that's sufficiently large. To get rid of it, I seeem to have to shrink the radius to so small a value that the effect is barely noticeable :-|

Ideas?

Thanks

##### Share on other sites

Is the darkening happening on the object or on the background behind the object? If the latter, then you should be able to fix it by ignoring the occlusion term when deltaDepth is bigger than some value (a good value to start with is your AO radius). It looks like you already do this for small terms too? (What is "Bias"?).