• FEATURED

View more

View more

View more

### Image of the Day Submit

IOTD | Top Screenshots

### The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.

# SSAO acting as depth filter (help needed)

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

2 replies to this topic

### #1DethRaid  Members

Posted 28 August 2014 - 03:00 PM

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 );

vec2 sampleScale = vec2( radiusx, radiusy );

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?

### #2Eternal  Members

Posted 28 August 2014 - 04:57 PM

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

### #3DethRaid  Members

Posted 28 August 2014 - 08:21 PM

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!

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.