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

Assigning Different RenderMeshes to Different Entities of Same Archetype Does Not Work

Discussion in 'Graphics for ECS' started by rthorat, Dec 6, 2020.

  1. rthorat

    rthorat

    Joined:
    Feb 18, 2018
    Posts:
    5
    I am new to DOTS, so maybe I am missing something here (documentation is not great and lots of changes with each new release). But from my reading, I should be able to assign different RenderMesh shared components to entities of the same archetype. And those entities will then be grouped together by their shared components. I have some basic code that draws a stack of cubes (5x5x5) in a loop. When those cubes use the same mesh, the code works fine and I see the stack correctly. When I generate a unique mesh instance for each cube, they stop rendering correctly. Instead, only the first cube renders. If I change my for loops to only generate the last cube, it renders correctly and in the correct position, so there is nothing wrong with my mesh creation or translation. (Also, I know generating a new mesh for each cube is not a good performance idea, but this is simply a test of the how the system works).

    Below is my basic code (the RenderBounds was added based on forum feedback, but it changed nothing). This code is called each iteration of the loop, and produces a new mesh for each cube. In the EntityDebugger, I can see that all cubes get created and all values are as expected, but they do not render. As I said, simply commenting out the mesh creation line and reusing the same mesh for everything works fine:


    Code (CSharp):
    1.  
    2. var mesh = CreateMesh(new int3(x, y, z), template.MeshData);
    3. var cube = entityManager.CreateEntity(archetype);
    4. entityManager.SetSharedComponentData(cube, new RenderMesh()
    5. {
    6.     mesh = mesh,
    7.     material = Material
    8. });
    9. entityManager.SetComponentData(cube, new Translation() { Value = new float3(x, y, z) });
    10. entityManager.SetComponentData(cube, new RenderBounds { Value = mesh.bounds.ToAABB() });
    11.  
    UPDATE: If I create the mesh once, then copy it using Instantiate, everything renders correctly. It's only when I create all 125 meshes using new Mesh() { }... that it does not work. Something really strange is going on under the hood here. I am trying to new() up 125 identical meshes. But when I do, only the first renders. If I create 1 mesh, then copy it repeatedly, everything works fine. In both cases I am creating 125 instances of the same mesh data, just doing it through 2 different methods. Yet one of those methods fails while the other works. I'm stumped.
     
    Last edited: Dec 6, 2020
  2. DreamingImLatios

    DreamingImLatios

    Joined:
    Jun 3, 2017
    Posts:
    3,983
    Are you manually rendering the cubes or using the Hybrid Renderer? The way you are generating meshes, each
    new Mesh()
    ends up having a different UnityEngine.Object instanceId and therefore a different hashcode. So each cube entity will end up in a different chunk.
     
  3. rthorat

    rthorat

    Joined:
    Feb 18, 2018
    Posts:
    5
    Rendering with the Hybrid Renderer v2. I know it will end up in a different chunk, but I am just experimenting right now. It doesn't make sense to me that it just doesn't render, even though I can see each cube in the Entity Debugger and see they have a RenderMesh attached. And Position/Mesh are exactly as expected. I have abandoned trying to make this work for now because what I did for the UPDATE above works for me. But this is still a mystery to me.
     
  4. DreamingImLatios

    DreamingImLatios

    Joined:
    Jun 3, 2017
    Posts:
    3,983
    Aside from the different shared components, are the archetypes the same?
     
  5. rthorat

    rthorat

    Joined:
    Feb 18, 2018
    Posts:
    5
    Yes, same archetype.