I’m trying to write a Light Manager to that can request the rendering of shadow maps on demand. For now lets focus on a little part of this manager that simply refreshes the shadow maps while outside of play mode. Here is the code :
public void RefreshAllShadowMaps()
{
#if UNITY_EDITOR
Array.Sort(lightData, new LightObjectDistanceComparer(new Vector3(72.23f,43.4f,-103.14f)));
#else
Array.Sort(lightData, new LightObjectDistanceComparer(Camera.main.transform.position));
#endif
for (int i = 0; i < lightData.Length; i++)
{
HDCachedShadowManager.instance.ForceEvictLight(lightData[i]);
}
for (int i = 0; i < lightData.Length; i++)
{
var lData = lightData[i];
if (!HDCachedShadowManager.instance.WouldFitInAtlas(lData))
{
Debug.LogWarning($"No room in shadow map atlas for:{lData.gameObject.name}...skipping", lData.gameObject);
continue;
}
Debug.Log($"Requesting shadow rendering for:{lData.gameObject.name}", lData.gameObject);
HDCachedShadowManager.instance.ForceRegisterLight(lData);
lData.RequestShadowMapRendering();
}
}
Some additional info:
The size I have set for the Punctual Light cached atlas is 2048.
There are 59 lights in the lightData array. All with a resolution of 512.
By my calculations this means I can up to 16 lights in the atlas at a time.
The LightObjectDistanceComparer wors and the array does get sorted in according to the distance from the hard coded debug point.
All 59 lights are set to ShadowUpdateMode.OnDemand
Both preserveCachedShadow
and onDemandShadowRenderOnPlacement
are set to false
The behaviour I expect is that uppon calling this method the cached shadows would effectively be cleared and then the 16 closest lights to the debug point would render into the atlas. This means I should get Requesting shadow rendering for
logged 16 times and then get No room in shadow map atlas for
logged 43 times.
What I get instead is Requesting shadow rendering for
logged 59 times but lights closer to the debug point do not have shadows. If I clear the cached atlast using the checbox from the rendering debugger and call the method again I only see 2 random lights pop in there while in the scene it’s obvious that more lights have shadows, alltough not the expected ones.
What am I missing here ?