Search Unity

SSAO with Range and Falloff

Discussion in 'Scripting' started by perprerp, Sep 1, 2014.

  1. perprerp

    perprerp

    Joined:
    Oct 29, 2013
    Posts:
    3
    After experiencing issues with SSAO noise on distant objects I wrote this little hack to make AO gradually fade out over distance. Example images are attached.

    edit: I chose this board based on similar posts here. Please let me know if the post should have been made elsewhere.

    INSTRUCTIONS

    In Shaders\frag_ao.cginc, replace the line

    Code (csharp):
    1.  
    2. occ /= sampleCount;
    3. return 1-occ;
    4.  
    with

    Code (csharp):
    1.  
    2. int depth_max        = 60;
    3. int depth_falloff    = 16;
    4. float depth_mod        = (clamp(depth,depth_falloff,depth_max) - depth_falloff) / (depth_max - depth_falloff);
    5. occ /= sampleCount;
    6. return 1.0-(occ-depth_mod);
    7.  
    AO will start fading out at distance depth_max until it disappears at distance depth_falloff. Tweak these values to your liking.

    If anyone wishes to contribute performance optimizations, feel free - writing shaders is not my strength. Among other things, the entire AO code for a pixel could be skipped in depth > depth_max.



    This is without the effect (the values are outside the visible depth range)
    without mod.jpg
    Gradual change ( depth_max and depth_falloff are distant)
    gradual.jpg
    Sudden change (depth_max and depth_falloff are near)
    sudden.jpg
     
    Leoo likes this.