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

Invisible entity (MeshInstanceRenderer or SetSharedComponentData issue)

Discussion in 'Graphics for ECS' started by Anthelmed, Aug 4, 2018.

  1. Anthelmed

    Anthelmed

    Joined:
    Mar 2, 2017
    Posts:
    15
    Hello, I'm not sure why, but my entity doesn't appear at all in my scene (game and scene window), and I don't have any errors.
    I think either the MeshInstanceRenderer not work or the SetSharedComponentData not update correctly.

    I'm using Unity 2018.2.1f1.

    Code (Json):
    1. {
    2.     "dependencies": {
    3.         ...
    4.         "com.unity.burst": "0.2.4-preview.23",
    5.         "com.unity.entities": "0.0.12-preview.8"
    6.      }
    7. }
    8.  
    Code (CSharp):
    1. using Unity.Entities;
    2. using Unity.Mathematics;
    3. using Unity.Rendering;
    4. using Unity.Transforms;
    5. using UnityEngine;
    6.  
    7. public class Bootstrap : MonoBehaviour
    8. {
    9.     public Mesh PlayerMesh;
    10.     public Material PlayerMaterial;
    11.  
    12.     private void Start()
    13.     {
    14.         var entityManager = World.Active.GetOrCreateManager<EntityManager>();
    15.  
    16.         var playerArchetype = entityManager.CreateArchetype(
    17.             typeof(TransformMatrix),
    18.             typeof(Position),
    19.             typeof(MeshInstanceRenderer)
    20.         );
    21.  
    22.         var player = entityManager.CreateEntity(playerArchetype);
    23.  
    24.         entityManager.SetComponentData(player, new Position { Value = new float3(0f, 0f, 1f) });
    25.  
    26.         entityManager.SetSharedComponentData(player, new MeshInstanceRenderer
    27.         {
    28.             mesh = PlayerMesh,
    29.             material = PlayerMaterial
    30.         });
    31.     }
    32. }
     
  2. tertle

    tertle

    Joined:
    Jan 25, 2011
    Posts:
    3,753
    Is your player a skinned mesh? Because I don't believe that's supported yet.
     
  3. Anthelmed

    Anthelmed

    Joined:
    Mar 2, 2017
    Posts:
    15
    Nope, i'm using a primitive cube and a material with the default built-in shader.


     
  4. tertle

    tertle

    Joined:
    Jan 25, 2011
    Posts:
    3,753
    Not sure what to suggest then. I copied your code and it worked fine for me.

    upload_2018-8-4_12-32-26.png
     
  5. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,754
    Maybe try recreate project, and paste code again?
    Or recreate mesh and texture?
    Refresh.
     
  6. Anthelmed

    Anthelmed

    Joined:
    Mar 2, 2017
    Posts:
    15
    Thanks for your help, I think my only option is to make a fresh project like Antypodish suggest.
     
  7. SidiaDev

    SidiaDev

    Joined:
    Mar 15, 2019
    Posts:
    4
    Just if future users end up here:

    You need to include the hybrid renderer package
     
  8. Rs

    Rs

    Joined:
    Aug 14, 2012
    Posts:
    74
    I stumbled into this trouble too. I do have the Hybrid Renderer package installed (Unity 2019.2.3.f1)
    I have attached the manifest.json file in case someone wants to check .
    I have this situation in the Entity Debugger which might give a clue.
    2019-08-28 15_57_52-.png

    I tested in Unity 2019.1.1 and 2019.2.3.f1 and the problem is the same. The Entities are created but I can see nothing.
    Am I doing something wrong?
    Or is the DOTS stack conflicting with Post Processing?
    Or, maybe more likely, I noticed that in every Entity, WorldRenderBounds extents is (0,0,0), could that be the cause?
    Any help would be super!
     

    Attached Files:

    Last edited: Aug 28, 2019
  9. Rs

    Rs

    Joined:
    Aug 14, 2012
    Posts:
    74
    Answering myself. I noticed that LocalToWorld was all 0 in every entity which led me to realize I forgot to add a Translation component when creating the archetype.
    Translation is pretty much needed for anything that has to be rendered, am I right?
    See ADDED LINE (line 22)

    Code (CSharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5. using Unity.Entities;
    6. using Unity.Collections;
    7. using Unity.Rendering;
    8. using Unity.Transforms;
    9. using UnityEngine.Assertions;
    10.  
    11. public class WAMParticleSystemWIthECS : MonoBehaviour
    12. {
    13.     [SerializeField] Mesh mesh;
    14.     [SerializeField] Material material;
    15.  
    16.    void Awake() {
    17.         // Grab the entity manager instance
    18.         EntityManager entityManager = World.Active.EntityManager;
    19.         // Create an entity archetype
    20.         EntityArchetype entityArchetype = entityManager.CreateArchetype(
    21.             typeof(BoidComponent),
    22.             typeof(Translation), // ADDED LINE
    23.             typeof(RenderMesh),
    24.             typeof(LocalToWorld)
    25.             //typeof(WorldRenderBounds)
    26.             );
    27.  
    28.         // Allocate NativeArray to collect the entities
    29.         NativeArray<Entity> entities = new NativeArray<Entity>(4, Allocator.Temp);
    30.  
    31.         // Create an entity from the archetype
    32.         entityManager.CreateEntity(entityArchetype, entities);
    33.  
    34.         for (int i = 0; i < entities.Length; i++) {
    35.             Entity entity = entities[I];
    36.             // Use the entity manager to set the component's data
    37.             entityManager.SetComponentData<BoidComponent>(entity, new BoidComponent {
    38.                 position = new Unity.Mathematics.float3(0.1f, 0.1f, 0.1f),
    39.                 velocity = new Unity.Mathematics.float3(0.01f, 0.01f, 0.01f),
    40.                 rotation = Unity.Mathematics.quaternion.Euler(new Unity.Mathematics.float3(0f,10f,1f))
    41.             });
    42.  
    43.             Assert.IsNotNull(mesh);
    44.             entityManager.SetSharedComponentData(entity, new RenderMesh {
    45.                 mesh = mesh,
    46.                 material = material
    47.             });
    48.  
    49.             //WorldRenderBounds wrb = entityManager.GetComponentData<WorldRenderBounds>(entity);
    50.             //wrb.Value.Extents = new Unity.Mathematics.float3(100f, 100f, 100f);
    51.             //entityManager.SetComponentData(entity, wrb);
    52.         }
    53.  
    54.         // free
    55.         entities.Dispose();
    56.     }
    57. }
    58.  
     
  10. Noxalus

    Noxalus

    Joined:
    Jan 9, 2018
    Posts:
    80
    I had to add a RenderBounds to make my entity appear properly on my side!
     
  11. tinaaaaalee

    tinaaaaalee

    Joined:
    Jan 14, 2020
    Posts:
    4
    I spent a day debugging... The solution is to add
    typeof (RenderBounds)
    to your archetype like this:
    Code (CSharp):
    1. BlockArchetype = manager.CreateArchetype (
    2.                 typeof (LocalToWorld),
    3.                 typeof (Translation),
    4.                 typeof (RenderMesh),
    5.                 typeof (RenderBounds)
    6.             );
    The reason is that in previous Entity package, RenderBounds is automatically added to your entity, but this is not the case for the newest Entity package.

    Hope it helps:)
     
  12. Ander_zh

    Ander_zh

    Joined:
    May 18, 2017
    Posts:
    2
    oh god ..thank you ..i search many result which doesn't right . But you save me
     
  13. SpectralEdge

    SpectralEdge

    Joined:
    Sep 17, 2012
    Posts:
    33
    Thanks, you really are amazing for posting this. My head was starting to hurt from hitting that wall so many times!
     
  14. MrBigly

    MrBigly

    Joined:
    Oct 30, 2017
    Posts:
    221
    Adding
    Adding RenderBounds for 2019.4.2f1 worked for me too. Thank you.
     
  15. OrientedPain

    OrientedPain

    Joined:
    Mar 26, 2018
    Posts:
    50
    Not working is there any change in Oct 2020 I cant render the entity.

    My code:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using Unity.Entities;
    5. using Unity.Rendering;
    6. using Unity.Transforms;
    7. using Unity.Mathematics;
    8.  
    9. public class ECS : MonoBehaviour
    10. {
    11.     [SerializeField]
    12.     private Mesh entityMesh;
    13.     [SerializeField]
    14.     private Material entityMaterial;
    15.  
    16.     void Start()
    17.     {
    18.         EntityManager entityManager = World.DefaultGameObjectInjectionWorld.EntityManager;
    19.  
    20.         EntityArchetype entityArchetype = entityManager.CreateArchetype(
    21.             typeof(RenderMesh),
    22.             typeof(LocalToWorld),
    23.             typeof(RenderBounds)
    24.         );
    25.  
    26.         Entity entity = entityManager.CreateEntity(entityArchetype);
    27.  
    28.         entityManager.SetSharedComponentData(entity, new RenderMesh {
    29.             mesh = entityMesh,
    30.             material = entityMaterial
    31.         });
    32.     }
    33. }
    34.  
     
  16. joelv

    joelv

    Unity Technologies

    Joined:
    Mar 20, 2015
    Posts:
    203
    I would suggest you try to instantiate your entities from a prefab, since that will always set up the correct components on conversion. If this is not possible you can read the conversion system setting up the required components in Unity.Rendering.Hybrid/MeshRendererConversion.cs in the hybrid renderer packages.

    We are currently in the process of moving some components around to reduce both GPU and CPU memory usage so the exact components needed might vary from release to release. A utility function for spawning renderable entities are planned but it's not yet implemented.
     
    Krajca likes this.
  17. OrientedPain

    OrientedPain

    Joined:
    Mar 26, 2018
    Posts:
    50
    Thanks for your respond.

    I have been added Translation Component and it works, maybe entities need position to appear at.