I have a question concerning the general usage of these APIs:
and
If I wanted to sort the VisibleLight array by something like distance-to-Camera, what is the correct behaviour for dealing with the index map?
Currently I have this scenario (in psuedo code)
// swap 2 Light's position in the array
VisibleLight tempLight = visibleLights[l];
visibleLights[l] = visibleLights[r];
visibleLights[r] = tempLight;
// swap the indices in the index map
int tempIndex = lightIndexMap[l];
lightIndexMap[l] = lightIndexMap[r];
lightIndexMap[r] = tempIndex;
cullingResults.SetLightIndexMap(lightIndexMap);
At this point, whether I do the custom sort or not (and assuming I’m understanding the API correctly), I’d assume that the Per-Object light culling result would be the same since it does its sorting/culling based on Unity’s internal heuristics. However the result I receive is different based on whether or not I do my custom sort. The sort itself works as intended, so my suspicion is that I am misusing the aforementioned API.
I’ll come back to explain per-object culling in more detail. Meanwhile here’s an example gist of LWRP when it used to sort lights per-camera on C# using LightIndexMap
Having said that we are working to add API to tell amount of visible lights and sort VisibleLights per camera in Unity side as a lot of users have requested this. Is this something you would benefit?
I now see how I was misinterpreting the API. I was assuming that I needed to GetLightIndexMap, reorder it based on my sort and then pass it back immediately after the sort. That doesn’t seem to be the case, as reflected in your sample. I was thinking the index map corresponded to where the Light exists in Unity’s internal “all the lights in the scene” array, not the original indices of the VisibleLight array handed to us by the CullingResults. All the same, it does seem like this could be made easier if VisibleLight just had a member that was the index it originally had, no? I may be missing something though.
In regards to an API for doing the sort outside of Managed Code, I certainly wouldn’t be opposed to it, but I’m personally fine without it. I could see it being useful in a broader scope to be able to provide ScriptableCullingParameters an enum or flags for heuristics on how Lights get sorted on cull. That would probably be able to hit a majority of cases. But personally I want the absolute control to sort however I like.