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?
This is my current solution, but the LOD index doesn’t exactly match what’s rendered:
public static class EcsRenderingHelper
{
/// <summary> Calculate the index of the visible LOD. </summary>
public static int CalculateLodIndex(MeshLODGroupComponent meshLODGroup, float4x4 localToWorld, float3 cameraPos)
{
var worldRef = math.transform(localToWorld, meshLODGroup.LocalReferencePoint);
var dist = math.distance(cameraPos, worldRef);
if (dist < meshLODGroup.LODDistances0.x) return 0;
if (dist < meshLODGroup.LODDistances0.y) return 1;
if (dist < meshLODGroup.LODDistances0.z) return 2;
if (dist < meshLODGroup.LODDistances0.w) return 3;
if (dist < meshLODGroup.LODDistances1.x) return 4;
if (dist < meshLODGroup.LODDistances1.y) return 5;
if (dist < meshLODGroup.LODDistances1.z) return 6;
if (dist < meshLODGroup.LODDistances1.w) return 7;
// no mesh rendered
return -1;
}
public static Entity FindRenderedChild(EntityManager entityManager, Entity rootEntity, int lodMask)
{
var children = entityManager.GetBuffer<Child>(rootEntity);
for (int childIndex = 0; childIndex < children.Length; ++childIndex)
{
var childEntity = children[childIndex].Value;
if (entityManager.HasComponent<MeshLODComponent>(childEntity))
{
var meshlodComponent = entityManager.GetComponentData<MeshLODComponent>(childEntity);
if ((meshlodComponent.LODMask & lodMask) != 0)
{
return childEntity;
}
}
}
return Entity.Null;
}
}