Search Unity

Change shadow resolution from script

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

  1. wahntin

    wahntin

    Joined:
    Sep 17, 2019
    Posts:
    83
    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?
     
    Ruslank100 and cxode like this.
  2. RunninglVlan

    RunninglVlan

    Joined:
    Nov 6, 2018
    Posts:
    182
    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:
    589
    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:
    83
    @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:
    589
    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:
    83
    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:
    83
    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?
     
    rivanavur, Ruslank100 and cxode like this.
  8. BattleAngelAlita

    BattleAngelAlita

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

    Jobus

    Joined:
    Jun 7, 2013
    Posts:
    7
    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.
     
    zhuhaiyia1, Ruslank100 and cxode like this.
  10. phil_lira

    phil_lira

    Unity Technologies

    Joined:
    Dec 17, 2014
    Posts:
    584
    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.
     
  11. cxode

    cxode

    Joined:
    Jun 7, 2017
    Posts:
    268
    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
  12. cxode

    cxode

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

    phil_lira

    Unity Technologies

    Joined:
    Dec 17, 2014
    Posts:
    584
    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.
     
  14. MattESqr

    MattESqr

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

    Fewes

    Joined:
    Jul 1, 2014
    Posts:
    259
    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:
    32
    @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:
    584
    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:
    584
    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:
    32
    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
     
    IAmChiagozie likes this.
  20. VoxelBoy

    VoxelBoy

    Joined:
    Nov 7, 2008
    Posts:
    240
    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:
    8
    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. cxode

    cxode

    Joined:
    Jun 7, 2017
    Posts:
    268
    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:
    240
    You do realize they probably have more important stuff to work on, right? You're coming across very entitled.
     
  24. cxode

    cxode

    Joined:
    Jun 7, 2017
    Posts:
    268
    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
  25. MD_Alpha

    MD_Alpha

    Joined:
    Jan 11, 2020
    Posts:
    1
    So when can we finally set shadow resolution in URP with Script ? its very needed when you are creating a 3d game for mobile devices
     
  26. Benfont

    Benfont

    Joined:
    Nov 11, 2017
    Posts:
    21
    cxode likes this.
  27. xDavidLeon

    xDavidLeon

    Joined:
    Jun 9, 2014
    Posts:
    123
    Thanks @JimmyCushnie ! Looks good!

    PC games have given options to change shadow resolution since the 90s. We shouldn't be hacking together solutions in 2020.
     
    cxode and Ruslank100 like this.
  28. DanjelRicci

    DanjelRicci

    Joined:
    Mar 8, 2010
    Posts:
    310
    So is this not yet implemented? Really? Well thanks a lot @JimmyCushnie, that will do in the meantime!
     
    projectorgames_unity likes this.
  29. projectorgames_unity

    projectorgames_unity

    Joined:
    Oct 15, 2018
    Posts:
    107
    Is this still not done? :| Well, subscribed to the thread for when it is!
     
  30. BattleAngelAlita

    BattleAngelAlita

    Joined:
    Nov 20, 2016
    Posts:
    400
    Huge numbers of quality settings in U/HD-RP are not exposed to user, and lot of doesn't even implemented. Maybe unity will add it, sometime in 2022-2023-2024, or maybe not, who knows.
     
  31. FloBeber

    FloBeber

    Joined:
    Jun 9, 2015
    Posts:
    166
    THANK YOU for sharing guys! Really helpful.
     
    cxode likes this.
  32. victorlapin

    victorlapin

    Joined:
    Mar 30, 2020
    Posts:
    3
    Another thank you for @JimmyCushnie
    I literally got a facepalm realizing that we can't set those graphics parameters.
     
    cxode likes this.
  33. chrismarch

    chrismarch

    Joined:
    Jul 24, 2013
    Posts:
    472
    QualitySettings.shadowDistance seems to be ignored in unity 2019.4.16f1, URP 7.5.1. Do I need to edit the pipeline assets at runtime? That has the unfortunate side effect of modifying an asset file when done in the editor... I just want to improve shadows linearly during close ups.
     
    Last edited: Feb 10, 2021
  34. rivanavur

    rivanavur

    Joined:
    Jun 21, 2020
    Posts:
    4
    can anybody help me for using this code?
     
    Last edited: Feb 20, 2021
  35. LeePerry

    LeePerry

    Joined:
    Aug 27, 2013
    Posts:
    7
    I'm in the same boat as most of you it seems, trying to simply toggle on/off shadows at runtime (super frustrating BTW, been banging my head on a "toggle shadows" button for hours now).

    Could someone maybe lend me a little guidance with the syntax of setting the Shadows Max Distance to 0? Can't seem to get that to work, but, since it was suggested above I have to assume it's editable at runtime.
     
    Last edited: Feb 22, 2021
  36. Inc8877

    Inc8877

    Joined:
    Dec 14, 2018
    Posts:
    13
    Hi guys, I created a thread where I posted my solution to get around the limitations.
    Hope this will be helpful to someone :)
    Solution
     
    andreiagmu and swantonb like this.
  37. Rich_A

    Rich_A

    Joined:
    Nov 22, 2016
    Posts:
    338
    Over a year later, has shadow resolution been added?
     
  38. Imakhiil

    Imakhiil

    Joined:
    Oct 11, 2013
    Posts:
    96
    Yeah, it'd be good to have some update on this.
     
  39. Xtro

    Xtro

    Joined:
    Apr 17, 2013
    Posts:
    608
    .........
     
  40. Baraff

    Baraff

    Joined:
    Aug 16, 2014
    Posts:
    255
    A fairly easy solution to this is simply set up additional quality levels and attach different universalRP settings files with the settings the way you like.

    EG. UniversalRP-LowQuality attached to Project Settings Quality Levels Low
    and
    UniversalRP-LowQualityNoShadows attached to LowNoShadows
    Then you simple change it as you please with
    QualitySettings.SetQualityLevel(0);
    QualitySettings.SetQualityLevel(1);
    ...

    It works for some simple changes like shadows off at least.
     
  41. cxode

    cxode

    Joined:
    Jun 7, 2017
    Posts:
    268
    Modern games should have more granular options than that. If I opened up a new game where the graphics settings were just a choice between "low", "medium", "high", and "ultra", I would be very disappointed. Users should be able to fine-tune the lighting quality, and other rendering settings, for their personal desired tradeoff between fidelity and performance.
     
  42. EidLoi

    EidLoi

    Joined:
    Nov 20, 2019
    Posts:
    7
    To be fair URP is targeted at low-end devices (not that this is an excuse for the HUGE performance impact of HDRP).

    What I did in my project: Shadow quality settings are in the predefined assets. Anti-aliasing setting is changed on the active asset I grab from GraphicsSettings. Texture quality is set on QualitySettings. It is a bit roundabout, but the URP asset doesn't really have anything of interest besides the shadow setup and I don't want players to tweak the cascades. Post processing has it's own separate stuff (you can change a few things in the asset, but are unlikely to allow changes by players). With that, this should be workable for a URP PC setup:

    Code (CSharp):
    1. QualitySettings.vSyncCount = vSyncEnabled ? 1 : 0;
    2.  
    3. UniversalRenderPipelineAsset urp = (UniversalRenderPipelineAsset)GraphicsSettings.renderPipelineAsset;
    4. if (isNiceQuality) // or do what stepping you need
    5. {
    6.   urp.msaaSampleCount = (int)MsaaQuality._8x; // Constant form the UnityEngine.Rendering.Universal namespace
    7.  QualitySettings.masterTextureLimit = 0; // Mipmap level, 0->full res, 1 -> 1/2 res...
    8. }
    9. else
    10. {
    11.   urp.msaaSampleCount = (int)MsaaQuality.Disabled;
    12.   QualitySettings.masterTextureLimit = 2;
    13. }
    You have to mind though that after switching the asset (so the shadow settings), you prolly need to re-apply the quality setting as it is not stored to disc on runtime (it's a ScriptableObject, with all its quirks).

    NOTE: The quality settings in the player settings OVERRIDE the default I am grabbing here from the Graphics settings. The code here doesn't consider what you have there.

    You could use the quality levels to control the texture quality setup and all the related config by
    simply changing the quality level (this includes vsync and the base URP asset). SO you could go with the usual UI where you select the Quality level, but when the user changes something it becomes "custom" and you apply the correct URP asset and overrides. Just make sure the order of changes are correct. See also GraphicsSettings.currentRenderPipeline.
     
    Last edited: Sep 3, 2021
    elektrobeard likes this.
  43. Baraff

    Baraff

    Joined:
    Aug 16, 2014
    Posts:
    255
    On a mobile device it is more than fine, in fact you really don't want to be presenting too many options on Mobile. In any case, you can include as many variants as you please as they are just settings. Don't twist or misconstrue this to mean that better access to settings via script would not be useful and more flexible but here I merely pointed out a very simple solution which I'm sure someone will find useful. I know I would have found it useful when I went looking for why setting shadows off no longer worked using quality.settings.
     
    Last edited: Sep 7, 2021
  44. BonneCW

    BonneCW

    Joined:
    Jan 22, 2017
    Posts:
    123
    Not really. We're developing with URP for high-end PC and consoles, but also want to support older PCs and last gen and as we're a small team HDRP is overkill for us to use. So having different quality settings to adjust during runtime is important here as well and graphics quality of URP also isn't just low-end like, you can do pretty cool and demanding stuff as well ;)
     
  45. cxode

    cxode

    Joined:
    Jun 7, 2017
    Posts:
    268
    Yes. It's the universal render pipeline, not the low-end render pipeline. I too am developing a high-end PC game using it.

    I'd like to remind everybody that the feature we're arguing about is literally removing the keyword
    internal
    from seven property setters in UniversalRenderPipelineAsset.cs. This change would be incredibly easy for Unity to make, for a ton of benefit to us developers and our players. As far as I can tell, the only reason they haven't done this is that Unity chooses their priorities with dice rolls and we haven't been so lucky yet.
     
    malkere, Gravesend, funkyCoty and 2 others like this.
  46. cxode

    cxode

    Joined:
    Jun 7, 2017
    Posts:
    268
    Also, bump for my hacky reflection solution.
     
  47. Matt-Cranktrain

    Matt-Cranktrain

    Joined:
    Sep 10, 2013
    Posts:
    129
    Hey, just wanted to thank you for sharing this!! My Googling in confusion got me to this thread and your script was an easy way to get access to these strangely private fields. No idea why it is all locked down like this, it seems like such a clear deviation from the stated mission of "URP can do/will do everything SRP did". Thanks for sharing.
     
    cxode likes this.
  48. GameDeveloper1111

    GameDeveloper1111

    Joined:
    Jul 24, 2020
    Posts:
    100
    Thank you for this. Do you call these functions in the editor or at runtime?
     
  49. cxode

    cxode

    Joined:
    Jun 7, 2017
    Posts:
    268
    I'm using them at runtime, my players call the functions by customizing their graphics settings. They should work in the editor as well but I don't know why you'd want to do that.
     
    GameDeveloper1111 likes this.
  50. GameDeveloper1111

    GameDeveloper1111

    Joined:
    Jul 24, 2020
    Posts:
    100
    How to allow 8192 shadow map resolution:

    Move
    com.unity.render-pipelines.universal@VERSION
    from PackageCache to Packages folder. Edit the ShadowResolution enum, which is located at
    com.unity.render-pipelines.universal@VERSION/Runtime/Data/UniversalRenderPipelineAsset.cs


    If anyone knows of a pitfall regarding this, please let me know.

    Meta: This was small and somewhat related so I posted here instead of making a new thread.
     
    spatial_io, cecarlsen, krifx and 3 others like this.