PDA

View Full Version : Problems with SSAO



icebear
05-27-2017, 12:39 AM
Hello :)

I am having some weird issues with my SSAO-shader, as you can see on the following pictures, showing an asteroid in a space-environment:
237523762377
2nd picture is SSAO only. 3rd picture shows the depthbuffer. It seems like it highlights every edge of the asteroid, which looks totally wrong. On meshes with sharper edges, like the cockpit, it doesn't show this behavior. I have no idea where this comes from, since I can't see any problems with the depthbuffer.

This is my SSAO-code:

float readDepth(vec2 uv)
{
float n = 1.0; // camera z near
float f = 100.0; // camera z far
float z = texture2D(uDepthTexture, uv).x;
return (2.0 * n) / (f + n - z * (f - n));
}

float compareDepths( in float depth1, in float depth2 ) {
float aoCap = 1.0;
float aoMultiplier=10000.0;
float depthTolerance=0.000;
float aorange = 10.0;// units in space the AO effect extends to (this gets divided by the camera far range
float diff = sqrt( clamp(1.0-(depth1-depth2) / (aorange/(camerarange.y-camerarange.x)),0.0,1.0) );
float ao = min(aoCap,max(0.0,depth1-depth2-depthTolerance) * aoMultiplier) * diff;
return ao;
}

vec3 addssao()
{
vec2 texCoord = vUv.xy;
vec4 fragcolor=vec4(0.0);

float depth = readDepth( texCoord );
float d;

float pw = 1.0 / (float)uResolution.x;
float ph = 1.0 / (float)uResolution.y;

float aoCap = 1.0;

float ao = 0.0;

float aoMultiplier=10000.0;

float depthTolerance = 0.001;

float aoscale=1.0;

d=readDepth( vec2(texCoord.x+pw,texCoord.y+ph));
ao+=compareDepths(depth,d)/aoscale;

d=readDepth( vec2(texCoord.x-pw,texCoord.y+ph));
ao+=compareDepths(depth,d)/aoscale;

d=readDepth( vec2(texCoord.x+pw,texCoord.y-ph));
ao+=compareDepths(depth,d)/aoscale;

d=readDepth( vec2(texCoord.x-pw,texCoord.y-ph));
ao+=compareDepths(depth,d)/aoscale;

pw*=2.0;
ph*=2.0;
aoMultiplier/=2.0;
aoscale*=1.2;

d=readDepth( vec2(texCoord.x+pw,texCoord.y+ph));
ao+=compareDepths(depth,d)/aoscale;

d=readDepth( vec2(texCoord.x-pw,texCoord.y+ph));
ao+=compareDepths(depth,d)/aoscale;

d=readDepth( vec2(texCoord.x+pw,texCoord.y-ph));
ao+=compareDepths(depth,d)/aoscale;

d=readDepth( vec2(texCoord.x-pw,texCoord.y-ph));
ao+=compareDepths(depth,d)/aoscale;

pw*=2.0;
ph*=2.0;
aoMultiplier/=2.0;
aoscale*=1.2;

d=readDepth( vec2(texCoord.x+pw,texCoord.y+ph));
ao+=compareDepths(depth,d)/aoscale;

d=readDepth( vec2(texCoord.x-pw,texCoord.y+ph));
ao+=compareDepths(depth,d)/aoscale;

d=readDepth( vec2(texCoord.x+pw,texCoord.y-ph));
ao+=compareDepths(depth,d)/aoscale;

d=readDepth( vec2(texCoord.x-pw,texCoord.y-ph));
ao+=compareDepths(depth,d)/aoscale;

pw*=2.0;
ph*=2.0;
aoMultiplier/=2.0;
aoscale*=1.2;

d=readDepth( vec2(texCoord.x+pw,texCoord.y+ph));
ao+=compareDepths(depth,d)/aoscale;

d=readDepth( vec2(texCoord.x-pw,texCoord.y+ph));
ao+=compareDepths(depth,d)/aoscale;

d=readDepth( vec2(texCoord.x+pw,texCoord.y-ph));
ao+=compareDepths(depth,d)/aoscale;

d=readDepth( vec2(texCoord.x-pw,texCoord.y-ph));
ao+=compareDepths(depth,d)/aoscale;

ao/=16.0;

fragcolor = vec4(1.0-ao);
fragcolor.a=1.0;

return fragcolor;
}

john_connor
05-27-2017, 08:46 AM
On meshes with sharper edges, like the cockpit, it doesn't show this behavior. I have no idea where this comes from, since I can't see any problems with the depthbuffer.

if you cant determine if and how your rendered image changes with your SSAO, one solution is to calculate the "image difference" between the "normal" color and the SSAO-corrected color, like this:


layout (location = 0) out vec4 FragmentOutput;

void main()
{
vec4 color = ShadeYourFragmentSomehow();
vec4 color_ssao = ComputeSSAOColor(color);

vec4 whatinterestsme = abs(color - color_ssao);

FragmentOutput = whatinterestsme;
}


then you can see a relatively black screen, only the difference will be visible

icebear
05-27-2017, 10:56 PM
I think i got it. In "compareDepth", I changed the "aoMultiplier"-value to 100. That means the SSAO isn't as strong as it could be, but at least those weird effects are gone and it still looks good enough for me. Thanks anway :)