Search Unity

Question All Entities of the same type are culled if just one leaves the camera frustrum

Discussion in 'Entity Component System' started by Studiomaurer, Nov 19, 2021.

  1. Studiomaurer

    Studiomaurer

    Joined:
    Sep 5, 2012
    Posts:
    56
    So I'm doing a city builder game:
    1. I create a pool of prefab entities
    2. From this pool, I automatically batch-generate a city using jobs from these entities using EntityCommandBuffer

    So far, everything is fine.

    3. During the game, I instantiate more Buildings from the same pool using the EntityManager
    4. If one Building of the same type leaves the camera frustrum, it is culled, but with it every other entity of the same type, on screen or not. So Buildings disappear, shadows stay. This only affects the new buildings, but they are set up identically to the initial ones

    So the cloned instances seem to be linked somehow. This behaviour is not affected by the static tag, so it's not batching. I noticed the PerInstanceCullingTag has been added automatically, but it is on any Meshrenderer in the scene.

    Also I see a "ChunkWorldRenderbounds" component. Is the Hybrid Renderer V2 Culling by Chunk? Is there a way to prevent it from doing this? I have tons of performance left over. If I could just exclude these couple of hundred buildings (max) from culling my Problem would be fixed. But how do I do it? Any hint is appreciated.

    Thanks in advance
     
  2. DreamingImLatios

    DreamingImLatios

    Joined:
    Jun 3, 2017
    Posts:
    4,271
    The way culling works in HR V2 is it uses the chunk bounds as an initial pass where it determines if the chunk is fully out (in which case it skips the chunk right then and there), fully in (in which case it skips reading WorldRenderBounds), and partial. Partial leads to per-instance culling.

    So the first thing to check is to make sure the ChunkWorldRenderBounds is updated to include many buildings. It should encapsulate all buildings in the chunk. My guess is it is only encapsulating one of the buildings. Does it matter which building goes outside the frustum when the behavior occurs?
     
  3. Micz84

    Micz84

    Joined:
    Jul 21, 2012
    Posts:
    451
    I remember I had similar issue and it was caused some bad data in mech bounding box component.
     
  4. Studiomaurer

    Studiomaurer

    Joined:
    Sep 5, 2012
    Posts:
    56
    I tried, it’s always the first building spawned. You‘re probably right with your assumption, is there a way to force update the chunk?
     
  5. DreamingImLatios

    DreamingImLatios

    Joined:
    Jun 3, 2017
    Posts:
    4,271
    Is RenderBoundsUpdateSystem running? If not, try dirtying the RenderBounds or LocalToWorld so that change filtering thinks it changed.
     
  6. Studiomaurer

    Studiomaurer

    Joined:
    Sep 5, 2012
    Posts:
    56
    Yes, the RenderBoundsUpdateSystem is running and picking up everything except the new entities (deleting other entities would trigger it)

    .I changed the code so the new player buildings are instatiated using the entitycomponentbuffer, just like the rest of the city (as opposed to many calls to the Entitymanager) to rule out the possibility that the buffer procedure would do anything different. This didn't help, so the problem most probably is appearing when I instantiate Objects one by one.

    So I wrote a System that would pick up a "Make Dirty Tag" which I attached to the Buildings when intializing. It then would cycle through the linkedEntityGroup of that Object (Parent Entity only contains Data, 1-3 renderMeshes are one layer deeper) and write a new Renderbound Component with similar data as the old one). This would cause the RenderBoundsUpdateSystem to Pick those up and update the Renderbounds (as far as I can tell, it does so correctly). Still, the culling behaviour remains.

    This is driving me crazy. Why are these Entities treated differently while being spawned from the very same Entity Prefab. The only difference is that the parent object gets a "userBuiltTag", so I can delete them when resettting the city. Maybe it's worth mentioning that I had to remove the Parent tag from all Children to prevent them from getting Picked up by the SimpleCullingJob every frame which would kill the performance. But this is true for all 2mio Objects in the scene, and they are performing fine.
     
    Last edited: Nov 22, 2021
  7. Studiomaurer

    Studiomaurer

    Joined:
    Sep 5, 2012
    Posts:
    56
    Ok. I couldn't find a solution, so I went for a brute force workaround. The problem would only appear with buildings that are built for the first time because they would be grouped into a new chunk that will fail to update properly. If other instances have been spawned during startup, the chunks will have fairly large ChunkWorldRenderBounds as the buildings are all over the place. So as a workaround I placed instances of every possible building in some far off topright and bottomleft position to generate a big ChunkWorldRenderBound. That's about 4k extra entities, but at least nothing is disappearing any more... performance is so relative with ECS :) Thanks @DreamingImLatios for putting me on the right track what the actual problem was.