Search Unity

2020.2 URP SSAO not working with Unlit shaders

Discussion in 'Universal Render Pipeline' started by Ubawesome, Dec 22, 2020.

  1. Ubawesome

    Ubawesome

    Joined:
    Jul 18, 2015
    Posts:
    8
    Hi! I've been trying to get the new SSAO Render Feature to work with my custom shaders, but realized that it doesn't work at all with Unlit shaders. At least, not my custom unlit shaders made with Shader Graph, and not the default Universal Render Pipeline/Unlit shader.

    Is this intended? I would assume so, but is there a way to make the SSAO work with custom unlit shaders?

    Any help at all, like a link to a relevant thread, would be very much appreciated. Thank you in advance!
     
  2. Oxeren

    Oxeren

    Joined:
    Aug 14, 2013
    Posts:
    121
    The idea is that SSAO is used to calculate how exposed different areas of the surface are to ambient (well, and also directional) light. So it is not a pass on top of everything that darkens everything in cavities, but rather is used in lighting calculations, so it makes sense that it has no effect on unlit shaders.
    So you'll have to implement AO in your shaders. I haven't tried it myself, but I've glanced at SSAO sources and as far as I understand you can just sample _ScreenSpaceOcclusionTexture using screen position (to be able to use it just create a Texture2D property in your graph, set it's reference to _ScreenSpaceOcclusionTexture and set Exposed to off). There is also _AmbientOcclusionParam vector4 that contains, well, ambient occlusion parameters that you set in the renderer feature. As far as I can tell it contains direct light strength in this vector's w component, and x,y and z are just zeroes. You can use it in the same way as the occlusion texture - by adding a property with _AmbientOcclusionParam reference and unchecking Exposed.
     
    bobbaluba and tmonestudio like this.
  3. Ubawesome

    Ubawesome

    Joined:
    Jul 18, 2015
    Posts:
    8
    Wow, that's a lot of information. I think I can work with this. I'll take a crack at it, and if I can't figure it out it's probably for the best anyway because my shaders are starting to get a little bloated.

    Thank you for the detailed response!
     
  4. Ryan_McMahon

    Ryan_McMahon

    Joined:
    Oct 11, 2015
    Posts:
    5
    The correct variable name to get the URP SSAO Texture is "_SSAO_OcclusionTexture3" not "_ScreenSpaceOcclusionTexture". Hope that saves people some time.
     
    Stein_69 and TheFloatingSheep like this.
  5. ahnafnafee

    ahnafnafee

    Joined:
    May 18, 2020
    Posts:
    7
    Is the texture name the same in built-in RP as well?
     
  6. Steamc0re

    Steamc0re

    Joined:
    Nov 24, 2014
    Posts:
    144
    Where did you find this? I'm trying to write an unlit shader and this returns just flat grey.
    Searching for this everywhere only returns this forum thread.
     
  7. maurosso

    maurosso

    Joined:
    Feb 19, 2017
    Posts:
    50
    Using Oxeren's tip, I got it sort of working in this way:


    but if I try to add the _AmbientOcclusionParam4 param to use as a modificator, it seems to not do anything
     
  8. alchemist_wurzelpurzel

    alchemist_wurzelpurzel

    Joined:
    Sep 4, 2018
    Posts:
    43
    You can find all the used textures in ScreenSpaceAmbientOcclusion.cs under Packages/Universal RP/Runtime/RendererFeatures.

    Code (CSharp):
    1. // Constants
    2.             private const string k_SSAOAmbientOcclusionParamName = "_AmbientOcclusionParam";
    3.             private const string k_SSAOTextureName = "_ScreenSpaceOcclusionTexture";
    4.  
    5.             // Statics
    6.             private static readonly int s_BaseMapID = Shader.PropertyToID("_BaseMap");
    7.             private static readonly int s_SSAOParamsID = Shader.PropertyToID("_SSAOParams");
    8.             private static readonly int s_SSAOTexture1ID = Shader.PropertyToID("_SSAO_OcclusionTexture1");
    9.             private static readonly int s_SSAOTexture2ID = Shader.PropertyToID("_SSAO_OcclusionTexture2");
    10.             private static readonly int s_SSAOTexture3ID = Shader.PropertyToID("_SSAO_OcclusionTexture3");
    As far as I can tell the indexed Occlusion Textures are used for the Blur passes and "_ScreenSpaceOcclusionTexture" is the final AO Texture but I could be wrong.

    Also note that in URP 10 Unity 2020 Unlit Shaders do not have a DepthNormals prepass so if anything your SSAO should only use depth as a source.
    You can, however, easily copy paste all the related Unlit shader files to create your own Unlit shader and just add the DepthNormals prepass from the Lit shader to it. I have done this in our project to use as a template for Amplify Shader Editor and it pretty much just works out of the box.
     
    ShapesCorp likes this.
  9. Qriva

    Qriva

    Joined:
    Jun 30, 2019
    Posts:
    1,307
    If you read URP docs you can find instructions how to get SSAO in custom shaders, the only problem with Unlit in 2020 was that it wasn't writing to depth normals texture. Year ago I added required keywords and custom function in my shader graph to sample direct/indirect SSAO and I was really angry when I realized it does not work.

    Few days ago I made upgrade to 2021.2 and just checked if something changed and actually it works now.
     
  10. daneobyrd

    daneobyrd

    Joined:
    Mar 29, 2018
    Posts:
    101
    maurosso likes this.
  11. maurosso

    maurosso

    Joined:
    Feb 19, 2017
    Posts:
    50
    Small update:
    This approach gives a lot better effect. Plus u get a nice param to control the intensity .
     

    Attached Files:

    • 1.png
      1.png
      File size:
      56.6 KB
      Views:
      664
    • 2.png
      2.png
      File size:
      27.2 KB
      Views:
      591
    daneobyrd likes this.
  12. VBenincasa

    VBenincasa

    Joined:
    Feb 13, 2014
    Posts:
    12
    Both of those names only return this thread on Google, and both are coming out as empty textures for me.
     
  13. two7two

    two7two

    Joined:
    Aug 3, 2023
    Posts:
    9
    do you know how to get SSAO texture in HDRP? I'm trying to repeat this method in HDRP and so far to no avail(