Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Bug Entities losing their FrozenRendererSceneTag

Discussion in 'Graphics for ECS' started by Opeth001, Sep 21, 2020.

  1. Opeth001

    Opeth001

    Joined:
    Jan 28, 2017
    Posts:
    1,112
    Hello Everyone,

    I am making a 2.5D game and for performance reasons, I decided to divide my Map into regions.
    im enabling / disabling entities located in regions too far to be considered by Frustum Culling and Physics.

    but after disabling / enabling some entities loses their FrozenRendererSceneTag and makes the Hybrid Renderer slower.



    note: my full scene is static and all entities within the subscene are tagged by the FrozenRendererSceneTag before this system runs.


    Code (CSharp):
    1.  
    2.     [UpdateInGroup(typeof(SimulationSystemGroup))]
    3.     public class RegionChangedEventConsumerSystem : SystemBase
    4.     {
    5.         EndSimulationEntityCommandBufferSystem endSimulationEntityCommandBufferSystem;
    6.  
    7.         EntityQuery m_RegionsQuery;
    8.         EntityQuery m_RegionChangedEventQuery;
    9.         protected override void OnCreate()
    10.         {
    11.             endSimulationEntityCommandBufferSystem = World.GetExistingSystem<EndSimulationEntityCommandBufferSystem>();
    12.  
    13.  
    14.             var queryDescription = new EntityQueryDesc
    15.             {
    16.                 All = new ComponentType[] { ComponentType.ReadOnly<WorldMapRegion>()},
    17.                 Options = EntityQueryOptions.IncludeDisabled
    18.             };
    19.             m_RegionsQuery = GetEntityQuery(queryDescription);
    20.  
    21.             m_RegionChangedEventQuery = GetEntityQuery(ComponentType.ReadOnly<RegionChangedEvent>());
    22.  
    23.             //without this call the system will still run even without RegionChangedEvent Component
    24.             this.RequireForUpdate(m_RegionChangedEventQuery);
    25.         }
    26.  
    27.         protected override void OnUpdate()
    28.         {
    29.            // var commandBuffer = EntityManager;
    30.             var commandBuffer = endSimulationEntityCommandBufferSystem.CreateCommandBuffer();
    31.             Entities
    32.                 .WithoutBurst()
    33.                 .WithStructuralChanges()
    34.                 .ForEach((Entity entity, in RegionChangedEvent regionChangedEvent) =>
    35.                 {
    36.                     /* var commandBuffer2 = beginPresentationEntityCommandBufferSystem.CreateCommandBuffer();
    37.                      commandBuffer2.DestroyEntity(entity);
    38.                     */
    39.                     commandBuffer.DestroyEntity(entity);
    40.                     var currentActiveRegions = GeoUtils.GetConcernedRegionsAsNativeArray(regionChangedEvent.CurrentRegion, Allocator.Temp);
    41.  
    42.                     //Teleport or initialization
    43.                     if (math.abs(regionChangedEvent.CurrentRegion.x - regionChangedEvent.OldRegion.x) > 1 || (math.abs(regionChangedEvent.CurrentRegion.y - regionChangedEvent.OldRegion.y) > 1))
    44.                     {
    45.  
    46.                         EntityManager.AddComponent<Disabled>(m_RegionsQuery);
    47.                        
    48.                         //Activate all Included region
    49.                         for (var i=0; i< currentActiveRegions.Length ; i++)
    50.                         {
    51.                             m_RegionsQuery.SetSharedComponentFilter(new WorldMapRegion { value = currentActiveRegions[i] });
    52.                             commandBuffer.RemoveComponent<Disabled>(m_RegionsQuery);
    53.                             m_RegionsQuery.ResetFilter();
    54.                         }
    55.                    
    56.                         currentActiveRegions.Dispose();
    57.                         return;
    58.                     }
    59.                  
    60.  
    61.  
    62.  
    63.                     //Normal region culling behavior
    64.  
    65.                     //Regions to Enable
    66.                     var concernedRegions = new NativeList<int2>(9, Allocator.Temp);
    67.  
    68.                     for (var i = 0; i < currentActiveRegions.Length; i++)
    69.                         //add excluded Regions from Old Player Region (New Regions)
    70.                         if (math.abs(regionChangedEvent.OldRegion.x - currentActiveRegions[i].x) > 1 || (math.abs(regionChangedEvent.OldRegion.y - currentActiveRegions[i].y) > 1))
    71.                             concernedRegions.Add(currentActiveRegions[i]);
    72.  
    73.                     var regionsCount = concernedRegions.Length;
    74.                    
    75.                     //Enable all Included regions
    76.                     for (var i = 0; i < regionsCount; i ++)
    77.                     {
    78.                         var worldMapRegion = new WorldMapRegion { value = concernedRegions[i] };
    79.                         m_RegionsQuery.SetSharedComponentFilter(worldMapRegion);
    80.                        
    81.                         commandBuffer.RemoveComponent<Disabled>(m_RegionsQuery);
    82.                         m_RegionsQuery.ResetFilter();
    83.                     }
    84.                     currentActiveRegions.Dispose();
    85.  
    86.  
    87.  
    88.                     //Regions to Disable
    89.                     concernedRegions.Clear();
    90.                     var oldActiveRegions = GeoUtils.GetConcernedRegionsAsNativeArray(regionChangedEvent.OldRegion, Allocator.Temp);
    91.  
    92.                     for (var i = 0; i < oldActiveRegions.Length; i++)
    93.                         //add excluded Regions from Old Player Region (New Regions)
    94.                         if (math.abs(regionChangedEvent.CurrentRegion.x - oldActiveRegions[i].x) > 1 || (math.abs(regionChangedEvent.CurrentRegion.y - oldActiveRegions[i].y) > 1))
    95.                             concernedRegions.Add(oldActiveRegions[i]);
    96.  
    97.  
    98.                     regionsCount = concernedRegions.Length;
    99.                    
    100.                     //Disable all excluded regions
    101.                     for (var i = 0; i < regionsCount; i++)
    102.                     {
    103.                         var worldMapRegion = new WorldMapRegion { value = concernedRegions[i] };
    104.                         m_RegionsQuery.SetSharedComponentFilter(worldMapRegion);
    105.                        
    106.                         commandBuffer.AddComponent<Disabled>(m_RegionsQuery);
    107.                         m_RegionsQuery.ResetFilter();
    108.                     }
    109.  
    110.  
    111.                     oldActiveRegions.Dispose();
    112.                     concernedRegions.Dispose();
    113.  
    114.                 }).Run();
    115.            
    116.         }
    117.     }
     
    Last edited: Sep 21, 2020
  2. Opeth001

    Opeth001

    Joined:
    Jan 28, 2017
    Posts:
    1,112
    Anyone has an idea about this ?