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.

Question new RenderMeshArray: Mesh[] and Material[] are null

Discussion in 'Graphics for ECS' started by imbaguru, Jun 15, 2023.

  1. imbaguru

    imbaguru

    Joined:
    Nov 1, 2020
    Posts:
    1
    Dear Unity Forum,
    I am trying to spawn new Entities and attach a RenderMeshArray component to them for procedural terrain during RunTime. For some reason the Meshes- and the Material-Array are always NULL when I check the newly created Entity in the Inspector, thus nothing is being rendered.
    When I use the RenderMeshArray component from an Entity that was converted in a SubScene, I can at least exchange the Meshes and Materials in the existing array. But as soon as I create a new array and assign it to the component and add the component to the entity it is also null.

    Code (CSharp):
    1.  
    2. Entity tileEnt = EM.CreateEntity(typeof(LocalToWorld), typeof(RenderMeshArray), typeof(RenderBounds));
    3. EM.AddComponentData<LocalTransform>(tileEnt, new LocalTransform { Position = new float3() { 0, 0, 0 }, Scale = 1f });
    4.  
    5. Mesh tileMesh = job.CreateMesh();
    6. tileMesh.RecalculateBounds();
    7.  
    8. EM.SetComponentData<RenderBounds>(tileEnt, new RenderBounds
    9.                     {
    10.                         Value = tileMesh.bounds.ToAABB()
    11.                     });
    12.  
    13. RenderMeshArray rma = EM.GetSharedComponentManaged<RenderMeshArray>(tileEnt);
    14.  
    15. rma.Meshes = new Mesh[] { tileMesh };
    16. rma.Materials = new Material[] { TerrainMaterial };
    17.                    
    18.  
    19. RenderMeshDescription renderMeshDescription = new RenderMeshDescription() { FilterSettings = RenderFilterSettings.Default };
    20. MaterialMeshInfo meshInfo = new MaterialMeshInfo()
    21.                     {
    22.                         Mesh = -1,
    23.                         Material = -1
    24.                     };
    25.  
    26. RenderMeshUtility.AddComponents(tileEnt, EM, renderMeshDescription, rma, meshInfo);
    27.  
    28.  

    As explained at the top, the code above resutls in the two arrays on the RenderMeshArray component to be NULL like on the screenshot. The created meshes are valid, they are being rendered when assigned to a RenderMeshArray component that was created during the conversion. But as stated, I cannot reassign the array to fit all the meshes I need, because as soon as I reassign one of the two arrays of the component they will always be null.
    Also: Adding the RenderMeshArray later on and not when creating the entity ( rma = new RenderMeshArray(){} ) does not solve the problem.

    I feel like I am missing some part of the whole concept of entity rendering with the RenderMeshArray component here. If some could help me out here, I would appreciate :)
     

    Attached Files:

  2. JussiKnuuttila

    JussiKnuuttila

    Unity Technologies

    Joined:
    Jun 7, 2019
    Posts:
    335
    It's possible that AddComponents does not do anything since you have already added the RenderMeshArray component before, and the "Add" versions typically don't modify the component value if the entity already has that component.

    I would suggest not adding the components yourself, so create an empty entity with CreateEntity and then use RenderMeshUtility.AddComponents. Alternatively, set the values of the components with the "SetComponentData" after calling AddComponents.
     
  3. shalm_

    shalm_

    Joined:
    Apr 20, 2023
    Posts:
    2
    Try to add Rotation = quaternion.identity to
    1. EM.AddComponentData<LocalTransform>(tileEnt, new LocalTransform { Position = new float3() { 0, 0, 0 }, Scale = 1f });
     
  4. iamarugin

    iamarugin

    Joined:
    Dec 17, 2014
    Posts:
    866
    I have the same problem, but I am adding components using RenderMeshUtility.AddComponents
     
  5. iamarugin

    iamarugin

    Joined:
    Dec 17, 2014
    Posts:
    866
    I got it. We need to use a consturctor, whan creating RenderMeshArray and not assigning array to properties directly