Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice
  3. Dismiss Notice

How to get rendered LOD mesh for entity?

Discussion in 'Graphics for ECS' started by SLGSimon, Jan 1, 2020.

  1. SLGSimon

    SLGSimon

    Joined:
    Jul 23, 2019
    Posts:
    80
    I am trying to get the actively rendered mesh sub-entity from an entity with a MeshLODGroupComponent, is there any way to retrieve this?

    I have had a quick look through LodRequirementsUpdateSystem, InstancedRenderMeshBatchGroup, and RenderMeshSystemV2, but I don't fully understand what's going in there yet. Do I need to retrieve it from the chunk data?

    Alternatively can I just do the lod calculation myself to pick which mesh is used?
     
  2. SLGSimon

    SLGSimon

    Joined:
    Jul 23, 2019
    Posts:
    80
    This is my current solution, but the LOD index doesn't exactly match what's rendered:

    Code (CSharp):
    1.  
    2. public static class EcsRenderingHelper
    3. {
    4.     /// <summary> Calculate the index of the visible LOD. </summary>
    5.     public static int CalculateLodIndex(MeshLODGroupComponent meshLODGroup, float4x4 localToWorld, float3 cameraPos)
    6.     {
    7.         var worldRef = math.transform(localToWorld, meshLODGroup.LocalReferencePoint);
    8.         var dist = math.distance(cameraPos, worldRef);
    9.  
    10.         if (dist < meshLODGroup.LODDistances0.x) return 0;
    11.         if (dist < meshLODGroup.LODDistances0.y) return 1;
    12.         if (dist < meshLODGroup.LODDistances0.z) return 2;
    13.         if (dist < meshLODGroup.LODDistances0.w) return 3;
    14.         if (dist < meshLODGroup.LODDistances1.x) return 4;
    15.         if (dist < meshLODGroup.LODDistances1.y) return 5;
    16.         if (dist < meshLODGroup.LODDistances1.z) return 6;
    17.         if (dist < meshLODGroup.LODDistances1.w) return 7;
    18.  
    19.         // no mesh rendered
    20.         return -1;
    21.     }
    22.  
    23.     public static Entity FindRenderedChild(EntityManager entityManager, Entity rootEntity, int lodMask)
    24.     {
    25.         var children = entityManager.GetBuffer<Child>(rootEntity);
    26.  
    27.         for (int childIndex = 0; childIndex < children.Length; ++childIndex)
    28.         {
    29.             var childEntity = children[childIndex].Value;
    30.  
    31.             if (entityManager.HasComponent<MeshLODComponent>(childEntity))
    32.             {
    33.                 var meshlodComponent = entityManager.GetComponentData<MeshLODComponent>(childEntity);
    34.                 if ((meshlodComponent.LODMask & lodMask) != 0)
    35.                 {
    36.                     return childEntity;
    37.                 }
    38.             }
    39.         }
    40.  
    41.         return Entity.Null;
    42.     }
    43. }