Search Unity

  1. Unity 2019.2 is now released.
    Dismiss Notice

SSAO with depth cutoff parameter

Discussion in 'Shaders' started by PatHightree, Mar 28, 2011.

  1. PatHightree

    PatHightree

    Joined:
    Aug 18, 2009
    Posts:
    259
    Hi there,
    I had artifacts on far away geometry, so I added a depth cutoff parameter to the standard ssao shader.

    Have fun.
    Ciao, Patrick

    [edit]
    Before importing this package, check if you have the standard SSAO effect files in your project.
    - Standard Assets\Image Effects (Pro Only)\SsaoEffect.cs
    - Standard Assets\Image Effects (Pro Only)\_Sources\Shaders\SSAOShader.shader
    - Standard Assets\Image Effects (Pro Only)\_Sources\Shaders\frag_ao.cginc
    Please remove these before importing the package, otherwise unity will report a problem with the script name.
     

    Attached Files:

    Last edited: Apr 7, 2011
  2. Andrew_atl

    Andrew_atl

    Joined:
    Sep 21, 2010
    Posts:
    103
    Wow thanks man, this is exactly what I was needing.
     
  3. brad_ict

    brad_ict

    Joined:
    Sep 14, 2010
    Posts:
    69
    Awesome! Thanks Hightree. Found a bug in your SSAO script. The filename of the script needs to be changed to be the same as the class name or it fails to compile.
     
  4. PatHightree

    PatHightree

    Joined:
    Aug 18, 2009
    Posts:
    259
    If you're getting an error with the class name, try importing into an empty project or verify that you don't have a SSAOEffect.cs script in your project already.
     
  5. Frank Oz

    Frank Oz

    Joined:
    Oct 13, 2010
    Posts:
    1,561
    This could be VERY useful! I love the look of SSAO, but not how it would behave with fog. Sounds like this will avoid that problem entirely huh? Awesome, thank you!!!
     
  6. reissgrant

    reissgrant

    Joined:
    Aug 20, 2009
    Posts:
    726
    Very nice, thanks for sharing!
     
  7. Chaoss@Perdition

    Chaoss@Perdition

    Joined:
    Mar 10, 2011
    Posts:
    37
    Great idea, but it does not work "Can't add script" error, always says it hasnt finished compilation and unity has been open for 7 hours
     
  8. Frank Oz

    Frank Oz

    Joined:
    Oct 13, 2010
    Posts:
    1,561
    IIRC you need to edit something in it to get it to work properly?
     
  9. PatHightree

    PatHightree

    Joined:
    Aug 18, 2009
    Posts:
    259
    As I posted before, this script name problem pops up if you already have the standard ssao effect in your project.
    Try removing it before importing the package.
     
  10. Stardog

    Stardog

    Joined:
    Jun 28, 2010
    Posts:
    1,373
    This works flawlessly for me. I was having some major problems with blackness on terrain, but this allows somewhat of a fix. Thanks.
     
  11. Marnix

    Marnix

    Joined:
    Sep 7, 2011
    Posts:
    11
    I have uploaded another SSAO script based on what you created with the cutoff parameter. The cutoff parameter is out of the picture and I added an extra parameter which sets the fog density to the shader.

    The fog density uses the exp2 formula from DirectX. The other functions are not implemented, but it could be done very easily (The linear and the exp).
     

    Attached Files:

  12. Reanimate_L

    Reanimate_L

    Joined:
    Oct 10, 2009
    Posts:
    2,379
    is it working in 3.4?
     
  13. Marnix

    Marnix

    Joined:
    Sep 7, 2011
    Posts:
    11
    Yes it is. I've built and tested the small example scene in 3.4.
     
  14. Wolfos

    Wolfos

    Joined:
    Mar 17, 2011
    Posts:
    738
    Thanks, works fine on OS-X too. The original one was way too buggy to implement, but yours works fine.
    Doesn't cut off as much of the framerate as the original script either, 60 vs 80FPS.
     
  15. megmaltese

    megmaltese

    Joined:
    Mar 19, 2009
    Posts:
    2,153
    Great stuff guys, this should replace the original SSAO script of Unity IMHO.
     
  16. Marnix

    Marnix

    Joined:
    Sep 7, 2011
    Posts:
    11
  17. EvilFox

    EvilFox

    Joined:
    Jul 23, 2013
    Posts:
    83
    Although it's been two years, who could be useful.
    "SSAO with exp2 fog" noticeably slower than "original SSAO".
    But it can be fixed - frag_ao.cginc need to replace the lines:
    Code (csharp):
    1. float fogFactor = (1-(1/exp((normalizedDepth * 100 * _FogDensity)*(normalizedDepth * 100 * _FogDensity))));
    2. return saturate( (1-occ) + fogFactor );
    by this line:
    Code (csharp):
    1. return saturate( (1-occ) + (1-(1.07/exp2((normalizedDepth * 100 * _FogDensity)))) );
     
    Last edited: Oct 3, 2013
  18. Nirvan

    Nirvan

    Joined:
    Nov 16, 2013
    Posts:
    82
    Refreshing again, there are problems in unity 5 with fog and ambient occlusion which draws occlusion over it.
    Global fog solves this problem but global fog don't affect billboard trees on terrain (there is no TreeBillboard shader in project and anything like this, I think trees use default).

    I want to use shaders posted there but unity gives error: "Shader error in 'Hidden/SSAO': 'samples': array dimensions of function parameters must be explicit at Assets/SSAO with exp2 fog/frag_ao.cginc(2) (on d3d11)"

    Don't know how to repair this.

    Edit:
    Ok I analyzed both shaders and I edited current SSAO shader from unity 5 and now it works.
    Just added cutoff variable in SSAOShader.shader file.
    To frag_ao added
    Code (CSharp):
    1. return (depth > _DepthCutoff) ? 1f : (1-occ);
    on the end return, and updated editor script for camera to controll cutoff.
     
    Last edited: Oct 11, 2015