Jump to content

  • Log In with Google      Sign In   
  • Create Account

Banner advertising on our site currently available from just $5!


1. Learn about the promo. 2. Sign up for GDNet+. 3. Set up your advert!


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.

  • You cannot reply to this topic
2 replies to this topic

#1 DethRaid   Members   -  Reputation: 104

Like
0Likes
Like

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 );
    
    float radiusx = SSAO_RADIUS / viewWidth;
    float radiusy = SSAO_RADIUS / viewHeight;
    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?



Sponsor:

#2 Eternal   Members   -  Reputation: 781

Like
1Likes
Like

Posted 28 August 2014 - 04:57 PM

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



#3 DethRaid   Members   -  Reputation: 104

Like
0Likes
Like

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.



PARTNERS