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.

Turn URP SSAO on and off at runtime

Discussion in 'Universal Render Pipeline' started by Volcore, Mar 1, 2021.

  1. Volcore

    Volcore

    Joined:
    Jan 14, 2020
    Posts:
    24
    Hi,

    I've recently stumbled upon the new SSAO feature in URP. It would be perfect for our game, where we have a 3D menu which needs high quality, high resolution rendering and shadowing, and the combat part, where performance is everything.

    Hence, I'd like to be able to control the SSAO at runtime. Turn it on in certain scenes where visual quality is important, and then turn it off in general (ideally at no performance penalty).

    I've been trying to figure out how we can achieve this with the new SSAO feature. The implementation itself is hidden (internal class), and there doesn't seem to be an official way to modify the renderer features.

    All I really need is to be able to check and uncheck this little box at runtime:

    upload_2021-3-1_15-33-0.png

    Is that currently supported? Is it a good idea, or am I missing something fundamental here?

    We're using Unity 2020.5 and URP 10.3.1.

    Thanks!
     
  2. Sky77

    Sky77

    Joined:
    Jan 30, 2014
    Posts:
    167
    Good luck. We tried but we had no luck in figuring this out. We were able to disable some rendered features but not the SSAO effect.

    Dealing with Renderer features at runtime in generale is very cumbersome.

    You can easily swap render assets, but it’s not an ideal way to do it.
     
  3. hibbygames

    hibbygames

    Joined:
    Mar 19, 2015
    Posts:
    35
    Make a script with using UnityEngine.Rendering.Universal and public ScriptableRendererFeature ssao;

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.Rendering.Universal;
    5.  
    6. public class controlSSAO : MonoBehaviour
    7. {
    8.  
    9.     public ScriptableRendererFeature ssao;
    10.  
    11.     public void toggleSSAO(bool _bool)
    12.     {
    13.         ssao.SetActive(_bool);
    14.     }
    15. }
    Attach your SSAO thing to it in the inspector, it's inside the forward renderer
    ssoa.jpg
    then just turn it on or off with ssao.SetActive(true); or ssao.SetActive(false)
     
  4. SparrowGS

    SparrowGS

    Joined:
    Apr 6, 2017
    Posts:
    2,536
    Hey, I've tried doing so and I can't assign the renderer feature as an independent object, it's just a value inside the ForwardRendererData class.

    I ended up doing it like so:
    Code (CSharp):
    1. public ForwardRendererData renderer;
    2.  
    3. public void SetSSAO(bool state) {
    4.  
    5.     renderer.rendererFeatures[0].SetActive(state);
    6. }
    Unity 2020.3.26
     
    tmonestudio likes this.
  5. Corvostudio

    Corvostudio

    Joined:
    Jul 19, 2015
    Posts:
    9
    Hello, in our project we are able to disable the SSAO just like SparrowGS describes, however on the final build when at runtime, disabling the AO leaves a weird lighting in the scene basically everything gets very dark. This doesn't happen if the SSAO is not present in the render features when starting up the build, but it happens only when starting up a build with the SSAO present in render features, and then disabling it with renderfeatures[x].SetActive(false). Also the bug never happen in the editor.

    We are using Unity 2021.3.3LTS
     
    kminko and jolix like this.
  6. funkyCoty

    funkyCoty

    Joined:
    May 22, 2018
    Posts:
    651
    It's likely because of shader stripping. Try disabling URP's post process shader stripping. Your build times will increase, but SSAO being on/off shouldnt be stripped as a result.
     
    _geo__ likes this.
  7. RayanAziz11

    RayanAziz11

    Joined:
    Aug 4, 2021
    Posts:
    5
    Thanks, it helped me :)
     
  8. jeffomatic

    jeffomatic

    Joined:
    Jun 24, 2021
    Posts:
    15
    We're also experiencing severe global darkening when SSAO is disabled (2021.3.6f1). Unfortunately, changing the post-process shader stripping per funkyCoty's suggestion above doesn't seem to have any effect.

    There is another thread in this forum that also mentions a darkening effect when disabling SSAO, but as of this writing, there's no solution there either.

    UPDATE: We ended up making two renderer assets, one with the SSAO feature, and one that omits the SSAO feature. We used QualitySettings.SetQualityLevel() to switch between the two. On our first attempt, we copied the renderer asset and disabled the SSAO feature without removing it. This caused the darkening described above, which was resolved when we removed the feature entirely. Changing shader stripping settings didn't have any influence in our case.
     
    Last edited: Aug 8, 2022
    fra3point and SparrowGS like this.
  9. _geo__

    _geo__

    Joined:
    Feb 26, 2014
    Posts:
    852
    To shed some of light on this I have tried turning SSAO off in URP with various levels of shader stripping. While turning off stripping "unused post processing shaders" helped somewhat with the colors it did (in my example) not help with shadows and other problems.

    Turning stripping off on all shaders (except for debug) solved the issue (though it comes at a cost).

    upload_2022-11-25_15-32-50.png

    I have also tried setting the "Intensity" of the SSAO render feature to zero (0f) at runtime. Sadly that just had the same result as turning it off (active = false). Usually that would be a good thing as the shader should not execute SSAO if not needed but in the case of missing shader variants due to stripping it is a disadvantage.

    If you are not looking for performance and only want to disable the SSAO due to aesthetic reasons then setting the "Intensity" to something close to zero works (I used 0.001f). Caveat: you still pay the performance cost.
     
    Last edited: Nov 25, 2022
    PraveenBalu likes this.
  10. fra3point

    fra3point

    Joined:
    Aug 20, 2012
    Posts:
    265
    Thank you! This workaround was the only one that worked in my case.

    I hope Unity fixes this in the near future... Meanwhile, I'll submit a bug report.
     
  11. Kronnect

    Kronnect

    Joined:
    Nov 16, 2014
    Posts:
    2,852
    SSAO should be linked to the volume system, otherwise it gets applied to all cameras which is not optimal in many cases.
     
    bluescrn likes this.