How to change shadowmap resolution via code in HDRP?

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:

public HDAdditionalLightData sunLightData;


void Update() {

IntScalableSetting myResolutionValue = 4096;

sunLightData.shadowResolution.Value(myResolutionValue);

}

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.

Ok I found the solution. I guess there is an actual “set resolution” command now that I didn’t see before.

sunLightData.SetShadowResolution(4096);

But it has to run every frame depending on the settings I think. Anyways it’s working now.

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.

HDAdditionalLightData data = ... ;

// Does it use "custom" resolution?
bool usesCustomResolution = data.shadowResolution.useOverride;

// Enable and set "custom" resolution.
data.SetShadowResolutionOverride(true); // custom res will only take effect if enabled
data.SetShadowResolution(1024);

// Use the quality levels instead of custom resolution
data.SetShadowResolutionOverride(false); // levels will only take effect if res is disabled
data.SetShadowResolutionLevel(1);

4 Likes