Search Unity

Frustum culling issues

Discussion in 'Entity Component System' started by Bas-Smit, Apr 4, 2019.

  1. Bas-Smit

    Bas-Smit

    Joined:
    Dec 23, 2012
    Posts:
    274
    Im using 2019.1 beta with up to date packages. I create meshes on the fly, adjusting RenderMesh and Translation components as required:
    Code (CSharp):
    1. var rm = EntityManager.GetSharedComponentData<RenderMesh>(e);
    2. rm.material.mainTexture = texture;
    3. rm.mesh.SetVertices(verts);
    4. rm.mesh.normals = normals;
    5. rm.mesh.RecalculateBounds();
    6. var pos = EntityManager.GetComponentData<Translation>(e);
    7. pos.Value = new float3(x, y, z);
    8. EntityManager.SetComponentData(e, pos);
    9. EntityManager.SetSharedComponentData(e, rm);
    10.  
    I am seeing too aggressive frustum culling though, which I currently solve by setting a huge extent (note the * 20):
    Code (CSharp):
    1. var rb = EntityManager.GetComponentData<RenderBounds>(e);
    2. rb.Value.Center = new float3(x, y, z);
    3. rb.Value.Extents = rm.mesh.bounds.extents * 20;
    4. EntityManager.SetComponentData(e, rb);
    This is obviously not ideal, does anyone know how to solve this properly? I create my entities like so:
    Code (CSharp):
    1. EntityManager.CreateEntity(typeof(RenderMesh), typeof(Translation), typeof(LocalToWorld), typeof(RenderBounds), typeof(WorldRenderBounds)
     
  2. eizenhorn

    eizenhorn

    Joined:
    Oct 17, 2016
    Posts:
    2,685
    Why is "too aggressive"? It works as expected. You set you mesh bounding box sizes and if it's not inside frustum pyramid - it's culled.
     
  3. Bas-Smit

    Bas-Smit

    Joined:
    Dec 23, 2012
    Posts:
    274
    By too aggressive I mean it culls still visible meshes. Does the above code looks like it should work?
     
  4. eizenhorn

    eizenhorn

    Joined:
    Oct 17, 2016
    Posts:
    2,685
    Mesh visibility related on bounding box position and size. Are you shure your bounds correct? Show them in scene view by debug draw line for example.
     
    Bas-Smit likes this.
  5. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    5,203
    Looks like you are changing the bounding volume of the mesh after it has been created.

    The local space bounding volume currently is a cached component created first time you add the rendermesh. "MissingBoundingVolumeSystem" or something does it. We currently have no automatic event that auto-updates the local bounding volume, but you can simply update the local space render bounds after updating the mesh.

    Creating a larger bounding volume is not necessary. It just has to be the correct one.
     
  6. Bas-Smit

    Bas-Smit

    Joined:
    Dec 23, 2012
    Posts:
    274
    This did the trick, think I messed up setting Centre:
    Code (CSharp):
    1. var rb = EntityManager.GetComponentData<RenderBounds>(e);
    2. rb.Value = rm.mesh.bounds.ToAABB();
    3. EntityManager.SetComponentData(e, rb);
    It's a bit unfortunate we have to add asmdef references for things like ToAABB to show up, in this case Mathematics.Extensions.Hybrid is required.
     
  7. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    5,203
    Thats how dll's work... If you don't use asmdef all dlls are automatically available.