# SSAO acting as depth filter (help needed)

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

## Recommended Posts

I've recently been working on a shaderpack for the Minecraft Shader Mod. I'm currently trying to write an SSAO function, but I'm getting some really strange results. I'm going to try to explain this clearly, but I'm so befuddled that who knows what will happen.

I'm using a rather simple SSAO algorithm:

1. Grab the depth of the current fragment
2. Compare that depth to the depths of 8 fragments in a circle around the current fragment
3. For each of those depths that is less than the depth from step 1, subtract a small amount from a variable that starts at 1
4. Multiply the lighting strength by the variable that was subtracted from

You'll notice that I don't account for the fragment's normal. I don't intend to. I just want a simple algorithm that I can get working, then I'll make it more accurate.

My code is as follows:

void calcSSAO( inout Pixel pixel ) {
//not that it works...
float ssaoFac = SSAO_STRENGTH;
float compareDepth = getDepthLinear( coord );

float occlusionPerSample = ssaoFac / float( SSAO_SAMPLES );

vec2 sampleCoord;
for( int i = 0; i < SSAO_SAMPLES; i++ ) {
sampleCoord = poisson( i ) * sampleScale;
if( getDepthLinear( sampleCoord ) < compareDepth ) {
ssaoFac -= occlusionPerSample;
}
}

pixel.directLighting = *= ssaoFac;
pixel.torchLighting *= ssaoFac;
}

• Pixel is a struct I've defined to hold a fragment's color, normal, world position, lighting, and whatever other per-pixel data I happen to need
• getDepthLinear is a function which samples the depth texture at a specified UV coordinate and returns the world-space depth
• coord is a varying which holds the UV position of the current fragment
• viewWidth and viewHeight are the width and height of the screen (in pixels)
• poisson is a function (because GLSL 120 arrays are poorly supported) which returns one of 32 poisson disk samples

If you want to see the whole shader, you can find it at http://pastebin.com/Eg7PEDCx

The result of this shader is http://i.imgur.com/5yqhLY3.png. Drawing just the SSAO term, I get http://i.imgur.com/3RXMwMv.png.

As you can see, fragments that are close to the camera have an SSAO term of 1, which extends until about 1.5 units from the camera. Then there is a small fade region, then the SSAO term is 0. I have absolutely no idea why I'm getting this result, and more importantly, I have no idea how to fix it. Do any of you have any ideas?

##### Share on other sites

Any chance line 357 should be sampleCoord = coord + poisson( i ) * sampleScale; ?

##### Share on other sites

Any chance line 357 should be sampleCoord = coord + poisson( i ) * sampleScale; ?

Yes. probably. Let me see if that helps...

Yes, that fixes it. Thank you very much!

• 45
• 11
• 17
• 11
• 13