Search Unity

Resolved How to change shadowmap resolution via code in HDRP?

Discussion in 'Scripting' started by Velo222, Apr 25, 2021.

  1. Velo222

    Velo222

    Joined:
    Apr 29, 2012
    Posts:
    1,437
    For some reason Unity is not keeping the shadow map resolution that I set my directional light to use in the editor before runtime. I'm not quite sure why it's being changed at runtime, but I want to change the shadow map resolution under the "Shadows --> Shadow Map" category in my directional light data via code.

    What is the proper syntax to reference the Resolution, and then to change it in code?

    So far I have this simple line:
    Code (CSharp):
    1. public HDAdditionalLightData sunLightData;
    2.  
    3.  
    4. void Update() {
    5.  
    6. IntScalableSetting myResolutionValue = 4096;
    7.  
    8. sunLightData.shadowResolution.Value(myResolutionValue);
    9.  
    10. }

    But I don't know how to set the "IntScalableSetting" type value properly. It won't accept just an integer value by itself for some reason.


    Thanks for any help.
     
    Last edited: Apr 26, 2021
  2. Velo222

    Velo222

    Joined:
    Apr 29, 2012
    Posts:
    1,437
    Ok I found the solution. I guess there is an actual "set resolution" command now that I didn't see before.

    Code (CSharp):
    1. sunLightData.SetShadowResolution(4096);
    But it has to run every frame depending on the settings I think. Anyways it's working now.
     
  3. _geo__

    _geo__

    Joined:
    Feb 26, 2014
    Posts:
    1,341
    Sorry for necroing this. Since it shows up in Google here are some notes for future me (and others). The resolution actually operates in two modes ("custom resolution" and "level"). If you want to use the custom resolution you have to enable the override. If you want to use the levels instead then you have to disable the override.

    Code (CSharp):
    1. HDAdditionalLightData data = ... ;
    2.  
    3. // Does it use "custom" resolution?
    4. bool usesCustomResolution = data.shadowResolution.useOverride;
    5.  
    6. // Enable and set "custom" resolution.
    7. data.SetShadowResolutionOverride(true); // custom res will only take effect if enabled
    8. data.SetShadowResolution(1024);
    9.  
    10. // Use the quality levels instead of custom resolution
    11. data.SetShadowResolutionOverride(false); // levels will only take effect if res is disabled
    12. data.SetShadowResolutionLevel(1);
    upload_2022-11-22_17-20-51.png
     
    Noss32 likes this.