Search Unity

Modifying the VisibleLight List and Remapping Light Indices

Discussion in 'Universal Render Pipeline' started by equalsequals, Dec 10, 2019.

  1. equalsequals

    equalsequals

    Joined:
    Sep 27, 2010
    Posts:
    130
    Hey all,

    I have a question concerning the general usage of these APIs:

    https://docs.unity3d.com/ScriptReference/Rendering.CullingResults.GetLightIndexMap.html
    and
    https://docs.unity3d.com/ScriptReference/Rendering.CullingResults.SetLightIndexMap.html

    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)

    Code (CSharp):
    1. // swap 2 Light's position in the array
    2. VisibleLight tempLight = visibleLights[l];
    3. visibleLights[l] = visibleLights[r];
    4. visibleLights[r] = tempLight;
    5.  
    6. // swap the indices in the index map
    7. int tempIndex = lightIndexMap[l];
    8. lightIndexMap[l] = lightIndexMap[r];
    9. lightIndexMap[r] = tempIndex;
    10.  
    11. cullingResults.SetLightIndexMap(lightIndexMap);
    12.  
    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.

    Any help would be greatly appreciated!

    Cheers,
    ==
     
    Last edited: Dec 10, 2019
  2. raidzhi

    raidzhi

    Joined:
    Jan 13, 2016
    Posts:
    31
    I think the SRP not support change culling results directly now.
     
  3. raidzhi

    raidzhi

    Joined:
    Jan 13, 2016
    Posts:
    31
    But we really need do some custom culling or resort things after engine's culling.
    That really sucks , we can not inject culling things.
     
  4. equalsequals

    equalsequals

    Joined:
    Sep 27, 2010
    Posts:
    130
  5. raidzhi

    raidzhi

    Joined:
    Jan 13, 2016
    Posts:
    31
  6. phil_lira

    phil_lira

    Unity Technologies

    Joined:
    Dec 17, 2014
    Posts:
    505
    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
    https://gist.github.com/phi-lira/6670451882c63c0b4c8138c9fdbfa174

    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?
     
  7. equalsequals

    equalsequals

    Joined:
    Sep 27, 2010
    Posts:
    130
    Thanks Felipe! I've got it working correctly now.

    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.

    Thanks again!
     
unityunity