Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Bug LOD not updating for entities with an LOD group that are moved by a system?

Discussion in 'Graphics for ECS' started by mexicanhatboy, Aug 14, 2023.

  1. mexicanhatboy

    mexicanhatboy

    Joined:
    Feb 5, 2019
    Posts:
    5
    Hi all,
    I'm working on a large scale forest visualisation (~1.5e7 trees). Each tree is an entity with an LOD Group. The idea is that when you are zoomed out, you see a constant set of around 50,000 static trees which are never moved and give a general overview of the density in the forest. When you zoom in close enough, the visualisation begins using a cache of entities that it can move around the map to draw more trees in the area you are looking at so you can see the exact makeup of the forest at any given point whilst never having more than a couple of hundred thousand entities. These "MoveableTrees" are moved by a System which executes an IJobEntity when the user moves the camera enough to warrant recalculating this.

    The LOD behaves normally for the static 50,000 trees, however it is often possible to approach the MoveableTrees when they have just been moved into a new location without their LOD updating. In order to diagnose the problem, I switched to a simple cube for the low LOD and a sphere for high LOD. You can see what I mean in the screenshot below: The moveable "trees" that are closest to the camera (and have just been teleported in) are still rendering as cubes, even though the static "trees" sitting behind them have already become spheres. the further away static trees are then cubes again, as expected.
    upload_2023-8-10_21-42-4.png

    My impression is that I (or Unity) am failing to update *something* when move the entities around. Currently all I do is update the LocalTransform of a given entity, my impression being that Unity should automatically update LOD components after this. Below you can see my code for moving the trees:
    Code (CSharp):
    1. [BurstCompile]
    2. public partial struct MoveMovableTrees :  IJobEntity
    3. {
    4.     public EntityCommandBuffer.ParallelWriter Ecb;
    5.     [ReadOnly]
    6.     public NativeList<LandClimTree> trees;
    7.     [ReadOnly]
    8.     public NativeList<uint> id;
    9.     [ReadOnly]
    10.     public NativeList<uint> nRenderTypes;
    11.     public void Execute([ChunkIndexInQuery] int chunkIndex, ref MovableTree movableTree, ref Tree tree,ref MeshLODGroupComponent lod, Entity entity)
    12.     {
    13.         if(movableTree.idInRenderType < nRenderTypes[movableTree.renderType])
    14.         {
    15.             int location = 0;
    16.             for(int i = 0; i < (int)movableTree.renderType; i++)
    17.                 location += (int)nRenderTypes[i]; //Add up all the trees in all the types before this. Change List this to a cumulative sum in future to save a bit of time.
    18.             location += movableTree.idInRenderType;
    19.             LandClimTree lctree = trees[(int)id[location]];
    20.             LocalTransform lt = LocalTransform.FromPosition(lctree.x, lctree.z, lctree.y);
    21.             lt.Scale = (lctree.height/tree.scale);
    22.             Ecb.SetComponent(chunkIndex, entity, lt);
    23.             movableTree.currentLandclimTreeID = lctree.id;
    24.         }
    25.         else
    26.         {
    27.             LocalTransform lt = LocalTransform.FromPosition(0, -100, 0);
    28.             lt.Scale = 1.0f;
    29.             Ecb.SetComponent(chunkIndex, entity, lt);
    30.         }
    31.     }
    32. }
    I'd be grateful of any help or ideas people might have. I have been unable to recreate this behaviour with GameObjects.

    EDIT: i just discovered that if I pull the camera far enough out and zoom it back in, the LODs update.

    PS. Labelled as a bug but might also just be a question in the case that I've missed something obvious...
     
    Last edited: Aug 14, 2023
  2. apkdev

    apkdev

    Joined:
    Dec 12, 2015
    Posts:
    263
    Hey, reposting my own comment since it helped someone back in the day. I'm pretty sure this bug is fixed by now, though.

     
    mexicanhatboy likes this.
  3. mexicanhatboy

    mexicanhatboy

    Joined:
    Feb 5, 2019
    Posts:
    5
    Holy hell, you are a certified genius. This worked perfectly, I've been struggling with this problem for several weeks!

    Of course, it's not the most elegant solution, but it does the trick for now. Thank you again!
     
    apkdev likes this.