Search Unity

Determine if entity is visible by the camera

Discussion in 'Entity Component System' started by SteenPetersen, May 1, 2021.

  1. SteenPetersen

    SteenPetersen

    Joined:
    Mar 13, 2016
    Posts:
    103
    Hi there,

    So I am new to ECS and have a new question:

    When I create my entity, is there a way that I can find out or give an entity a world position? or transform? that I can access from the system?

    What I have done it create a map of entities that have 2 components:

    Code (CSharp):
    1. [Serializable]
    2. public struct PixelEntity : IComponentData
    3. {
    4.     public Color32 color;
    5.     public int2 position;
    6. }
    7.  
    8. [Serializable]
    9. public struct RequiresUpdate : IComponentData
    10. {
    11.     public bool dirty;
    12. }
    Code (CSharp):
    1.    private void GeneratePixelEntityMap()
    2.     {
    3.         Map = new Entity[width, height];
    4.        
    5.         for (int x = 0; x < width; x++)
    6.         {
    7.             for (int y = 0; y < height; y++)
    8.             {
    9.                 Map[x,y] = CreatePixelEntity(x, y);
    10.             }
    11.         }
    12.     }
    13.  
    14.     private Entity CreatePixelEntity(int x, int y)
    15.     {
    16.         Entity pixelEntity = entityManager.CreateEntity(
    17.             typeof(PixelEntity),
    18.             typeof(RequiresUpdate)
    19.         );
    20.  
    21.         PixelEntity pixelEntityData = entityManager.GetComponentData<PixelEntity>(pixelEntity);
    22.         pixelEntityData.position.x = x;
    23.         pixelEntityData.position.y = y;
    24.        
    25.         entityManager.SetComponentData(pixelEntity, pixelEntityData);
    26.  
    27.         RequiresUpdate requiresUpdateData = entityManager.GetComponentData<RequiresUpdate>(pixelEntity);
    28.         requiresUpdateData.dirty = true;
    29.        
    30.         entityManager.SetComponentData(pixelEntity, requiresUpdateData);
    31.  
    32.         return pixelEntity;
    33.     }
    Then in my system I want to know if the current pixel is visible to the camera or not:

    Code (CSharp):
    1.     protected override void OnUpdate()
    2.     {
    3.         if (LevelGenerator.instance.levelTexture == null) return;
    4.         Texture2D texture = LevelGenerator.instance.levelTexture;
    5.         NativeArray<Color32> texturePixels = texture.GetPixelData<Color32>(0);
    6.         int textureWidth = texture.width;
    7.        
    8.         Entities.ForEach((ref PixelEntity pixelEntity, ref RequiresUpdate update) =>
    9.         {
    10.             if (!update.dirty) return;
    11.             texturePixels[pixelEntity.position.y * textureWidth + pixelEntity.position.x] = pixelEntity.color;
    12.             update.dirty = false;
    13.        
    14.         }).Run();
    15.        
    16.         texture.Apply();
    17.     }
    So I was hoping to somehow use https://docs.unity3d.com/ScriptReference/Camera.WorldToScreenPoint.html to figure out if the pixel should be updated or not to make sure I am not updating pixels that are not visible.