Search Unity

Resolved How to use managed type data in ECS like mesh filter and mesh renderer correctly?

Discussion in 'Entity Component System' started by Panos2, Jun 1, 2023.

  1. Panos2

    Panos2

    Joined:
    Sep 25, 2022
    Posts:
    9
    Hello everyone!

    I'm new to ECS and I'm trying to understand how it works. I've made progress in creating entities and adding basic data like positions, but now I'm wondering about using managed data for more complex components such as mesh filters, renders, or animation clips.

    I know that when I create a prefab of a cube and bake it, the renderer exists in the entity. However, I'm not exactly sure what happens in the background. My question is, what happens if I want to edit these components at runtime? Specifically, I'm interested in modifying the vertices and faces of a mesh.

    Based on my understanding from the documentation, it is possible to pass a reference of a managed type if I declare an IComponentData struct, instead of a struct, to a class. However, I've read that this can reduce performance and limit the functionality of ECS. Ideally, I want to fully leverage the benefits of ECS and use unmanaged types whenever possible. But I'm unsure if my case falls into the category where using only unmanaged data is not feasible.

    Could someone please provide some guidance on this matter? Any insights or examples would be greatly appreciated!
     
  2. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,769
    If you modify mesh, all entities sharing same mesh, will have the modified mesh.
    Unless you do that via shader using UV.

    Example on vid below uses only 3 different meshes. But Shader allows for many variations.


    If you want different meshes, then you need create new mesh and assign to entity.
     
  3. DreamingImLatios

    DreamingImLatios

    Joined:
    Jun 3, 2017
    Posts:
    4,262
  4. metageist

    metageist

    Joined:
    Mar 16, 2019
    Posts:
    27
    You might be interested in RenderMeshUtility, which is designed specifically for creating, editing and assigning runtime meshes to entities.

    https://docs.unity3d.com/Packages/c....0/api/Unity.Rendering.RenderMeshUtility.html

    Here is a simple example of how it works, supposing you want to create a mesh in an Entities.ForEach
    Code (CSharp):
    1.             // Create a mesh entity over all highlit cells
    2.             Entities.WithStructuralChanges().ForEach((Entity en, ref HighlightedData hd, ref CellCoordinatesData hcoo) =>
    3.             {
    4.                 // Create a new entity and add its transform components
    5.                 Entity meshEntity = EntityManager.CreateEntity();
    6.                 EntityManager.AddComponent<LocalToWorld>(meshEntity);
    7.                 EntityManager.AddComponentData(meshEntity, new LocalTransform() { Position = hcoo.CellPosition(Area.currentMap.cellRadius, 0.1f) });
    8.  
    9.                 // Load a mesh from assets
    10.                 var highlightMesh = Resources.Load<GameObject>("MyMesh").GetComponent<MeshFilter>().mesh;
    11.  
    12.                 // Create a RenderMeshDescription with basic values
    13.                 var renderMeshDescription = new RenderMeshDescription(ShadowCastingMode.On, true);
    14.  
    15.                 // Create a RenderMeshArray with the required mesh and material
    16.                 var renderMeshArray = new RenderMeshArray(new[] { Resources.Load<Material>("basemat_lit") }, new[] { highlightMesh });
    17.  
    18.                 // Create a MaterialMeshInfo instance which maps the first material and mesh from RenderMeshArray
    19.                 var materialMeshInfo = MaterialMeshInfo.FromRenderMeshArrayIndices(0, 0);
    20.  
    21.                 // Add the rendering Sharedcomponents using the helper class RenderMeshUtility
    22.                 RenderMeshUtility.AddComponents(meshEntity, EntityManager, renderMeshDescription, renderMeshArray, materialMeshInfo);
    23.  
    24.                 // Add RenderBounds
    25.                 EntityManager.AddComponentData(meshEntity, new RenderBounds() { Value = { Center = highlightMesh.bounds.center, Extents = highlightMesh.bounds.extents } });
    26.                 EntityManager.RemoveComponent<HighlightedData>(en);
    27.  
    28.             }).Run();
     
  5. Panos2

    Panos2

    Joined:
    Sep 25, 2022
    Posts:
    9
    Thank you for this invaluable information! Your insights have truly been a game-changer for me, making it much easier to grasp the intricacies of ECS. It's an incredibly fascinating concept, albeit challenging at times. Nevertheless, every second spent understanding ECS has been well worth it. I can't express enough gratitude for your help in navigating this complex topic. Your expertise is greatly appreciated, and I'm extremely grateful to have such a supportive community here on the Unity forums.
     
  6. metageist

    metageist

    Joined:
    Mar 16, 2019
    Posts:
    27
    You're welcome!
    Unity documentation is exhaustive, but I always found the lack of anecdotal examples the most challenging part of learning this architecture. Browsing the forums definitely feels like a mandatory part of mastering the technology.