Search Unity

  1. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice
  2. Ever participated in one our Game Jams? Want pointers on your project? Our Evangelists will be available on Friday to give feedback. Come share your games with us!
    Dismiss Notice

Change shadow resolution from script

Discussion in 'Universal Render Pipeline' started by wahntin, Nov 28, 2019.

  1. wahntin

    wahntin

    Joined:
    Sep 17, 2019
    Posts:
    43
    Dear community and unity team,

    I'm trying to build a settings screen for our game and would like to have individual settings for anti aliasing and shadow resolution that would need to be set from a script class.

    I tried to achieve this using the UniversalRenderPipelineAsset provided by GraphicsSettings.renderPipelineAsset, but it does not have a setter for the mainLightShadowmapResolution. My guess is that this would also not be the intended way of doing this.

    One the other side, having custom UniversalRenderPipelineAsset for the different shadow resolutions would also override all the other settings provided by the asset (e.g. the anti aliasing).

    Is there another (intended) way to change the shadow map resolution?
     
    JimmyCushnie likes this.
  2. RunninglVlan

    RunninglVlan

    Joined:
    Nov 6, 2018
    Posts:
    25
    We have the same question. What is the correct way to change Quality settings using URP?
    We know that we can prepare Quality Levels with different assets and change Levels at runtime, but what about specific Quality settings? How can we change them, e.g. enable/disable shadows? UniversalRenderPipelineAsset.supportsMainLightShadows also has only getter.
     
  3. Bordeaux_Fox

    Bordeaux_Fox

    Joined:
    Nov 14, 2018
    Posts:
    358
    You always can change the shadow resolution for each light component individually. Just create a script with an array and list all light components of thescene in it and change the desired parameters.
     
  4. wahntin

    wahntin

    Joined:
    Sep 17, 2019
    Posts:
    43
    @Bordeaux_Fox Tried it, but does not work.
    The Light.shadowResolution param does not seem to be used by the Universal Render Pipeline.
     
  5. Bordeaux_Fox

    Bordeaux_Fox

    Joined:
    Nov 14, 2018
    Posts:
    358
    The SRP lights have their own additional parameters. For example, in HDRP for every light component there is a HDAdditionalLightData script with the new parameters inside. They also have their own shadowManager method. But this stuff is not so documented.
     
  6. wahntin

    wahntin

    Joined:
    Sep 17, 2019
    Posts:
    43
    Hm... Looks like the light only comes with an UniversalAdditionalLightData object and this does not have any properties related to shadows or something similar.
     
    Ruslank100, satchell and RunninglVlan like this.
  7. wahntin

    wahntin

    Joined:
    Sep 17, 2019
    Posts:
    43
    Hi Unity team,

    any suggestions on how to deal with this? How can we allow our users to independently configure shadow quality and anti aliasing quality?
     
    Ruslank100 and JimmyCushnie like this.
  8. BattleAngelAlita

    BattleAngelAlita

    Joined:
    Nov 20, 2016
    Posts:
    205
    Yeah, encapsulation is most terrible paradigm in OOP.
     
  9. Jobus

    Jobus

    Joined:
    Jun 7, 2013
    Posts:
    6
    I would like to know how to change shadow resolution from script as well.

    UniversalRenderPipelineAsset.mainLightShadowmapResolution seems like the obvious solution, but that property has no setter.

    Same goes for UniversalRenderPipelineAsset.supportsMainLightShadows for toggling shadows on and off entirely.
     
    Ruslank100 and JimmyCushnie like this.
  10. phil_lira

    phil_lira

    Unity Technologies

    Joined:
    Dec 17, 2014
    Posts:
    545
    We should enable setter for main light shadow resolution. Will add that to our backlog.

    About the support for main light shadow we prevent that to be changed at runtime as we use that at build time to strip shader variants.

    The way to configure quality is to configure pipeline assets per quality tier / platform and swap them at runtime.
     
    Jobus likes this.
  11. JimmyCushnie

    JimmyCushnie

    Joined:
    Jun 7, 2017
    Posts:
    128
    Do you have an ETA for changing shadow resolution at runtime?

    I find it frankly ridiculous that URP does not allow you to toggle shadows globally at runtime. This is a regression from Built-In, where you could do the following:
    Code (CSharp):
    1. QualitySettings.shadows = ShadowQuality.Disable;
    Shadow enable/disable is a very important graphics setting to have for low-end users, as shadows are quite expensive to render. I would be 10000% comfortable with giving up automatic shader variant stripping if it meant I got this feature back.

    Swapping pipeline assets is not a solution, because that requires changing all the settings at once. You'd have me make thousands of pipeline assets for each individual configuration a user might choose?
     
    Last edited: Feb 6, 2020
    Ruslank100 likes this.
  12. JimmyCushnie

    JimmyCushnie

    Joined:
    Jun 7, 2017
    Posts:
    128
    Another URP option that really should be changeable from script, but isn't: Shadow Cascade splits (cascade2Split and cascade4Split)
     
  13. phil_lira

    phil_lira

    Unity Technologies

    Joined:
    Dec 17, 2014
    Posts:
    545
    We have to do a pass on this API to review what we can expose. For now, to disable shadows you can set the shadowDistance to 0.
     
    andreiagmu and JimmyCushnie like this.
  14. MattESqr

    MattESqr

    Joined:
    Feb 5, 2020
    Posts:
    3
    And what about the per-light shadow quality that is no longer there?
     
  15. Fewes

    Fewes

    Joined:
    Jul 1, 2014
    Posts:
    198
    Would just like to add:
    Most SRP settings probably are fine being bundled together, but it makes no sense that shadow quality and anti-aliasing quality are coupled like that. Any serious game/application would let the user configure them individually. To support that with the current setup would require 9 different pipeline assets just to have 3 shadow quality levels and 3 anti-aliasing levels, and exponentially more if you want to tweak other settings.
    Fingers crossed this can be exposed to script!
     
    Last edited: Mar 4, 2020
  16. NuclearC00kie

    NuclearC00kie

    Joined:
    Apr 11, 2019
    Posts:
    21
    @phil_lira How does swapping the pipeline assets at runtime work in regards to stripping shader variants?
    Example:
    * In Graphics Settings, set the "main pipeline asset".
    * For a lower end platform, create a custom Quality tier and override the pipeline asset in the quality tier.
    --> How does the stripping work here? From what I can see, stripping will be done based on the "main pipeline asset".
    If this is true, there is no way right now to strip variants per platform.
    Second example:
    * In Graphics Settings, set the "default pipeline asset" but it doesn't have all features. (no shadows from additional lights for example)
    * In Quality Settings, create an "Ultra" tier with shadows from additional lights. If we now go from default to ultra, the shader variants for shadows from additional lights will have been stripped. How will that behave?

    Thanks
     
  17. phil_lira

    phil_lira

    Unity Technologies

    Joined:
    Dec 17, 2014
    Posts:
    545
    We used to consider only the main pipeline asset in graphics settings. In 2020.1 it collects all features from all pipeline assets in quality settings + graphics settings and strip based on that.
     
  18. phil_lira

    phil_lira

    Unity Technologies

    Joined:
    Dec 17, 2014
    Posts:
    545
    I think this behaviour is to be backported to 2019 LTS, I have to confirm it though if it has landed or not.
     
  19. NuclearC00kie

    NuclearC00kie

    Joined:
    Apr 11, 2019
    Posts:
    21
    Thank you @phil_lira .
    To maximize stripping, Should we then put the pipeline asset with the least amount of features in the graphics settings?
    For example if you target both PC and mobile and you have a different pipeline asset per platform, you probably want to put the mobile one, otherwise you will include the PC features in your mobile build?

    In general, why is there a main pipeline asset in graphics settings? What is the advantage of that over using the pipeline asset from the current quality setting?

    Thanks
     
  20. VoxelBoy

    VoxelBoy

    Joined:
    Nov 7, 2008
    Posts:
    215
    Until a setter is implemented for mainLightShadowmapResolution, you can use reflection to set the value. I'm doing this in Editor and on Android; it works fine.


    Code (CSharp):
    1. using System.Reflection;
    2. using UnityEngine.Rendering;
    3. using UnityEngine.Rendering.Universal;
    4. using ShadowResolution = UnityEngine.Rendering.Universal.ShadowResolution;
    5.  
    6. public partial class SROptions
    7. {
    8.     private System.Type universalRenderPipelineAssetType;
    9.     private FieldInfo mainLightShadowmapResolutionFieldInfo;
    10.  
    11.     private void InitializeShadowMapFieldInfo()
    12.     {
    13.         universalRenderPipelineAssetType = (GraphicsSettings.currentRenderPipeline as UniversalRenderPipelineAsset).GetType();
    14.         mainLightShadowmapResolutionFieldInfo = universalRenderPipelineAssetType.GetField("m_MainLightShadowmapResolution", BindingFlags.Instance | BindingFlags.NonPublic);
    15.     }
    16.    
    17.     public ShadowResolution MainLightShadowResolution
    18.     {
    19.         get
    20.         {
    21.             if (mainLightShadowmapResolutionFieldInfo == null)
    22.             {
    23.                 InitializeShadowMapFieldInfo();
    24.             }
    25.             return (ShadowResolution) mainLightShadowmapResolutionFieldInfo.GetValue(GraphicsSettings.currentRenderPipeline);
    26.         }
    27.         set
    28.         {
    29.             if (mainLightShadowmapResolutionFieldInfo == null)
    30.             {
    31.                 InitializeShadowMapFieldInfo();
    32.             }
    33.             mainLightShadowmapResolutionFieldInfo.SetValue(GraphicsSettings.currentRenderPipeline, value);
    34.         }
    35.     }
    36. }
    37.  
     
  21. An3Apps

    An3Apps

    Joined:
    Nov 22, 2017
    Posts:
    7
    You can make one pipeline asset for every non-scriptable setting and then change the other settings through your script. But you're right, they shoud allow changing shadow resolution at runtime.
     
    Ruslank100 likes this.
  22. JimmyCushnie

    JimmyCushnie

    Joined:
    Jun 7, 2017
    Posts:
    128
    Well, it's been 7 months, and the properties discussed in this thread are STILL private, for absolutely no reason whatsoever. I got sick of waiting for Unity to change four lines of code and did it myself.

    https://gist.github.com/JimmyCushnie/e998cdec15394d6b68a4dbbf700f66ce

    My UnityGraphicsBullshit class exposes getters and setters for:
    • Main light shadows enabled
    • Additional light shadows enabled
    • Main light shadow resolution
    • Additional light shadow resolution
    • Cascade 2 split
    • Cascade 4 split
    • Soft shadows enabled
    The code is public domain and you are free to use it for whatever.
     
  23. VoxelBoy

    VoxelBoy

    Joined:
    Nov 7, 2008
    Posts:
    215
    You do realize they probably have more important stuff to work on, right? You're coming across very entitled.
     
  24. JimmyCushnie

    JimmyCushnie

    Joined:
    Jun 7, 2017
    Posts:
    128
    I feel like I am entitled to something as basic as this. I pay about $100/month to license Unity for me and my team. $100/month should cover changing five or six "private"s to "public". I am very frustrated that I had to spend time hacking together a solution myself, and I am very frustrated that my codebase has to use a hacked together solution. This functionality should be native; the fact that it isn't, in a product that's officially "production ready", is ridiculous.
     
    Last edited: Jun 29, 2020
    Shaderic likes this.
unityunity