Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question Anyone used the SSAO feature in your custom shader (URP 11) ?

Discussion in 'Shaders' started by Spikebor, Jun 26, 2021.

  1. Spikebor

    Spikebor

    Joined:
    May 30, 2019
    Posts:
    270
    Hello,
    Sorry for being a noob, i read this page and they did explain how to implement SSAO in your custom shaders,

    but i still don't get it.
    example
    can i have an Input.hlsl example, what is in that ?

    they tell us to have that function, but what is the body of the function ?
    same as question above for
    so after setting these above functions up, we can call this (in shadergraph custom function node right ?)
    My final intention is to get that SSAO float value and multiply it with my custom shader's shadow pass, and that's it.I wonder do any one did this before, or is there any tutorial on youtube.

    Thank you in advanced!
     
  2. Alehr

    Alehr

    Joined:
    Nov 16, 2011
    Posts:
    17
    I'm also wondering about this!
     
  3. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,238
    For Shader Graph you need a Custom Function node setup like this:
    upload_2021-7-19_13-38-55.png

    Code (csharp):
    1. #ifdef SHADERGRAPH_PREVIEW
    2. indirectAmbientOcclusion = 1.0;
    3. directAmbientOcclusion = 1.0;
    4. #else
    5. AmbientOcclusionFactor aoFactor = GetScreenSpaceAmbientOcclusion(screenUV);
    6. indirectAmbientOcclusion = aoFactor.indirectAmbientOcclusion;
    7. directAmbientOcclusion = aoFactor.directAmbientOcclusion;
    8. #endif

    All the other stuff mentioned in the upgrade guide appear to be mostly aimed towards people who are using the included URP shaders and slightly modifying them rather than people writing fully custom shaders, and certainly not anyone using Shader Graph. The
    normalizedScreenSpaceUV
    is the value the Screen Position node set to default already outputs, and the mentioned .hlsl files are all already included with any Shader Graph shader by default so there's nothing you need to do.

    The only issue is the additional .hlsl files are not included for previews, so just trying to call the function directly will show a compiler error even though the shader itself works totally fine.
     
    Last edited: Jul 19, 2021
    gigazelle likes this.
  4. shellyalon

    shellyalon

    Joined:
    Mar 26, 2013
    Posts:
    27
    Hej @bgolus ,
    Do you know what changed? Your solution worked perfectly in 2020.3. but now in 2021.3 LTS it doesn't.
    There is now an "After Opaque" checkbox. If I tick it, it displays SSAO, but not through the shadergraph, but for everything. When it's unticked, I can't get it to work.
    I used to use your code-snippet inside of the graph to color the AO and whatnot.
    upload_2022-5-9_21-1-0.png

    Sorry for reviving this old thread, I just thought maybe you have a clue.
     
  5. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,238
    Nope! Unity frequently changes a lot between each version of the SRP (and sometimes even within a version of the SRP) that breaks these kinds of things.

    ...

    To make it work now you have to add this to your shader:
    upload_2022-5-9_16-4-21.png

    And not use After Opaque, since that makes it run after all opaques and draws it over everything.
     
    gigazelle likes this.