Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

how do you use rendermesh to render mesh from scratch?

Discussion in 'Graphics for ECS' started by mailfromthewilds, Apr 4, 2021.

Thread Status:
Not open for further replies.
  1. mailfromthewilds

    mailfromthewilds

    Joined:
    Jan 31, 2020
    Posts:
    217
    Code (CSharp):
    1.         EntityManager entityManager = World.DefaultGameObjectInjectionWorld.EntityManager;
    2.  
    3.         EntityArchetype entityArchetype = entityManager.CreateArchetype(
    4.             typeof(LevelComponent),
    5.             typeof(Translation),
    6.             typeof(RenderMesh),
    7.             typeof(LocalToWorld),
    8.             typeof(RenderBounds)
    9.         );
    10.  
    11.         NativeArray<Entity> entityArray = new NativeArray<Entity>(2000, Allocator.Persistent);
    12.  
    13.         entityManager.CreateEntity(entityArchetype, entityArray);
    14.  
    15.         for (int i = 0; i < entityArray.Length; i++)
    16.         {
    17.             Entity entity = entityArray[i];
    18.             entityManager.SetComponentData(entity, new LevelComponent { level = Random.Range(10, 20) });
    19.  
    20.             entityManager.SetSharedComponentData(entity, new RenderMesh
    21.             {
    22.                 mesh = mesh,
    23.                 material = material,
    24.             });
    25.         }
    26.  
    27.         entityArray.Dispose();
    i had no luck to be honest. bounds.ToAABB is removed and i have no idea how to set bounds, and from other posts it seems that my mesh is NOT displaying because it lacks bounds.

    of course am not sure if its true or yes, but if yes, how do you render mesh?



    BTW I know theres object to entity conversion, however im not sure if i should do things this way. Surely you would have to instantiate objects first (so they can change into entity) and instantiating thousands of objects is a waste of resources ??

    I need to spawn thousands of models
     
  2. mailfromthewilds

    mailfromthewilds

    Joined:
    Jan 31, 2020
    Posts:
    217
    nvm now i know, i needed to add RenderBounds to archetype, but without doing SetComponentData with RenderBounds

    anyway can you guys tell me if theres any unnedded code in my code above? or if its up to date?
     
  3. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,754
    Crate Game Object with required mesh and material. Add optional tags. Create prefab of Game Object.

    Use one of entity conversion methods, to create entity prefab.

    Instantiate many entities as you need, from that entity prefab.

    If in doubt, checkout DOTS Unity samples.

    Profile and stress test results, before rising any concerns.
     
  4. varnon

    varnon

    Joined:
    Jan 14, 2017
    Posts:
    52
    I'm not sure I totally follow the last part of your statement. But Yes, I think it is the RenderBounds you need. I add it and set it manually.
    Code (CSharp):
    1. entityManager.SetComponentData(entity, new RenderBounds { Value = new AABB { Center = mesh.bounds.center, Extents = mesh.bounds.extents } });
    I remember having a lot of issues with this a while back. Before this method, I would sometimes get inappropriate culling. The rest of what you have looks reasonable to me. BUT, Antypodish is right. The best way to test right now is to let the conversion system do its thing. I understand not wanting to do that. It is at least helpful for figuring out what components need to be added. Hybrid Renderer V2 for example adds a lot of additional required components and they aren't really documented anywhere because it is always changing. Converting a GameObject is really the best way to see what should happen, even if you ultimately add the components manually.
     
Thread Status:
Not open for further replies.