Hi, anyone have experience working with lods and ECS? Are there any examples of how to setup it up and work with it, what data does it need to function etc?
Regards Andreas
Hi, anyone have experience working with lods and ECS? Are there any examples of how to setup it up and work with it, what data does it need to function etc?
Regards Andreas
Just look at LODGroupSystem and MeshInstanceRendererSystem sources for understanding LOD logic in ECS. Here my fast writed sample for you, and demonstration how it works.
using Unity.Entities;
using Unity.Mathematics;
using Unity.Rendering;
using Unity.Transforms;
using UnityEngine;
using UnityEngine.Rendering;
public class LODBootstrap : MonoBehaviour
{
public static Material MeshMaterial
{
get { return _instance.mat; }
}
public static Mesh Lod1
{
get { return _instance.lod1; }
}
public static Mesh Lod2
{
get { return _instance.lod2; }
}
public static Mesh Lod3
{
get { return _instance.lod3; }
}
public static Mesh Lod4
{
get { return _instance.lod4; }
}
[SerializeField]
private Material mat;
[SerializeField]
private Mesh lod1;
[SerializeField]
private Mesh lod2;
[SerializeField]
private Mesh lod3;
[SerializeField]
private Mesh lod4;
private static LODBootstrap _instance;
void Awake()
{
_instance = this;
}
void Start()
{
World.Active.GetOrCreateManager<LODSampleSystem>();
}
}
using Unity.Entities;
using Unity.Mathematics;
using Unity.Rendering;
using Unity.Transforms;
using UnityEngine.Rendering;
[DisableAutoCreation]
public class LODSampleSystem : ComponentSystem
{
private ComponentGroup lodsGroup;
protected override void OnCreateManager()
{
base.OnCreateManager();
Entity lodGroup = EntityManager.CreateEntity(typeof(MeshLODGroupComponent), typeof(ActiveLODGroupMask), typeof(Position), typeof(MeshInstanceRenderer), typeof(MeshLODComponent));
EntityManager.SetComponentData(lodGroup, new MeshLODGroupComponent()
{
LODDistances = new float4(5, 10, 15, 20),
WorldReferencePoint = new float3() { x = 0, y = 0, z = 0 }
});
EntityManager.SetComponentData(lodGroup, new Position()
{
Value = new float3() { x = 0, y = 0, z = 0 }
});
EntityManager.SetSharedComponentData(lodGroup, new MeshInstanceRenderer()
{
material = LODBootstrap.MeshMaterial,
mesh = LODBootstrap.Lod1,
receiveShadows = true,
castShadows = ShadowCastingMode.On
});
EntityManager.SetComponentData(lodGroup, new MeshLODComponent()
{
Group = lodGroup,
LODMask = 1
});
Entity lod2Entity = EntityManager.CreateEntity(typeof(Position), typeof(MeshInstanceRenderer), typeof(MeshLODComponent));
EntityManager.SetComponentData(lod2Entity, new Position()
{
Value = new float3() { x = 0, y = 0, z = 0 }
});
EntityManager.SetSharedComponentData(lod2Entity, new MeshInstanceRenderer()
{
material = LODBootstrap.MeshMaterial,
mesh = LODBootstrap.Lod2,
receiveShadows = true,
castShadows = ShadowCastingMode.On
});
EntityManager.SetComponentData(lod2Entity, new MeshLODComponent()
{
Group = lodGroup,
LODMask = 2
});
Entity lod3Entity = EntityManager.CreateEntity(typeof(Position), typeof(MeshInstanceRenderer), typeof(MeshLODComponent));
EntityManager.SetComponentData(lod3Entity, new Position()
{
Value = new float3() { x = 0, y = 0, z = 0 }
});
EntityManager.SetSharedComponentData(lod3Entity, new MeshInstanceRenderer()
{
material = LODBootstrap.MeshMaterial,
mesh = LODBootstrap.Lod3,
receiveShadows = true,
castShadows = ShadowCastingMode.On
});
EntityManager.SetComponentData(lod3Entity, new MeshLODComponent()
{
Group = lodGroup,
LODMask = 4
});
Entity lod4Entity = EntityManager.CreateEntity(typeof(Position), typeof(MeshInstanceRenderer), typeof(MeshLODComponent));
EntityManager.SetComponentData(lod4Entity, new Position()
{
Value = new float3() { x = 0, y = 0, z = 0 }
});
EntityManager.SetSharedComponentData(lod4Entity, new MeshInstanceRenderer()
{
material = LODBootstrap.MeshMaterial,
mesh = LODBootstrap.Lod4,
receiveShadows = true,
castShadows = ShadowCastingMode.On
});
EntityManager.SetComponentData(lod4Entity, new MeshLODComponent()
{
Group = lodGroup,
LODMask = 8
});
}
protected override void OnUpdate()
{
}
}
Super! Thanks for taking the time to answer. Should get me started.
Another Thanks to you!
Another thank you!!! I could ask if did you notice any performance difference? In my case with LOD the fps get worse
This something that I have been thinking about too. There are a few trade-offs. When you add the LOD components then you add CPU overhead (LOD system execution) and decrease the effectiveness of the data chunking. (splitting a single shared component data into up to 4 different shared component data) This can also increase the number of draw calls since there will be a minimum of 1 DrawMeshInstanced call per different MeshRenderComponent.
I think that LODs will really only help if you are GPU constrained. To keep things simple for the CPU, reduce the number of unique MeshRenderComponents and reduce relationships between entities. (remove parenting, LODs, etc. when possible)