Invisible entity (MeshInstanceRenderer or SetSharedComponentData issue)

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.

{
    "dependencies": {
        ...
        "com.unity.burst": "0.2.4-preview.23",
        "com.unity.entities": "0.0.12-preview.8"
     }
}
using Unity.Entities;
using Unity.Mathematics;
using Unity.Rendering;
using Unity.Transforms;
using UnityEngine;

public class Bootstrap : MonoBehaviour
{
    public Mesh PlayerMesh;
    public Material PlayerMaterial;

    private void Start()
    {
        var entityManager = World.Active.GetOrCreateManager<EntityManager>();

        var playerArchetype = entityManager.CreateArchetype(
            typeof(TransformMatrix),
            typeof(Position),
            typeof(MeshInstanceRenderer)
        );

        var player = entityManager.CreateEntity(playerArchetype);

        entityManager.SetComponentData(player, new Position { Value = new float3(0f, 0f, 1f) });

        entityManager.SetSharedComponentData(player, new MeshInstanceRenderer
        {
            mesh = PlayerMesh,
            material = PlayerMaterial
        });
    }
}

Is your player a skinned mesh? Because I don’t believe that’s supported yet.

Nope, i’m using a primitive cube and a material with the default built-in shader.


Not sure what to suggest then. I copied your code and it worked fine for me.

Maybe try recreate project, and paste code again?
Or recreate mesh and texture?
Refresh.

Thanks for your help, I think my only option is to make a fresh project like Antypodish suggest.

Just if future users end up here:

You need to include the hybrid renderer package

4 Likes

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.

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!

4906979–474983–manifest.json.txt (2.12 KB)

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)

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Unity.Entities;
using Unity.Collections;
using Unity.Rendering;
using Unity.Transforms;
using UnityEngine.Assertions;

public class WAMParticleSystemWIthECS : MonoBehaviour
{
    [SerializeField] Mesh mesh;
    [SerializeField] Material material;

   void Awake() {
        // Grab the entity manager instance
        EntityManager entityManager = World.Active.EntityManager;
        // Create an entity archetype
        EntityArchetype entityArchetype = entityManager.CreateArchetype(
            typeof(BoidComponent),
            typeof(Translation), // ADDED LINE
            typeof(RenderMesh),
            typeof(LocalToWorld)
            //typeof(WorldRenderBounds)
            );

        // Allocate NativeArray to collect the entities
        NativeArray<Entity> entities = new NativeArray<Entity>(4, Allocator.Temp);

        // Create an entity from the archetype
        entityManager.CreateEntity(entityArchetype, entities);

        for (int i = 0; i < entities.Length; i++) {
            Entity entity = entities[I];
            // Use the entity manager to set the component's data
            entityManager.SetComponentData<BoidComponent>(entity, new BoidComponent {
                position = new Unity.Mathematics.float3(0.1f, 0.1f, 0.1f),
                velocity = new Unity.Mathematics.float3(0.01f, 0.01f, 0.01f),
                rotation = Unity.Mathematics.quaternion.Euler(new Unity.Mathematics.float3(0f,10f,1f))
            });

            Assert.IsNotNull(mesh);
            entityManager.SetSharedComponentData(entity, new RenderMesh {
                mesh = mesh,
                material = material
            });

            //WorldRenderBounds wrb = entityManager.GetComponentData<WorldRenderBounds>(entity);
            //wrb.Value.Extents = new Unity.Mathematics.float3(100f, 100f, 100f);
            //entityManager.SetComponentData(entity, wrb);
        }

        // free
        entities.Dispose();
    }
}

I had to add a RenderBounds to make my entity appear properly on my side!

I spent a day debugging… The solution is to add typeof (RenderBounds) to your archetype like this:

BlockArchetype = manager.CreateArchetype (
                typeof (LocalToWorld),
                typeof (Translation),
                typeof (RenderMesh),
                typeof (RenderBounds)
            );

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:)

3 Likes

oh god …thank you …i search many result which doesn’t right . But you save me

Thanks, you really are amazing for posting this. My head was starting to hurt from hitting that wall so many times!

Adding

Adding RenderBounds for 2019.4.2f1 worked for me too. Thank you.

Not working is there any change in Oct 2020 I cant render the entity.

My code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Unity.Entities;
using Unity.Rendering;
using Unity.Transforms;
using Unity.Mathematics;

public class ECS : MonoBehaviour
{
    [SerializeField]
    private Mesh entityMesh;
    [SerializeField]
    private Material entityMaterial;

    void Start()
    {
        EntityManager entityManager = World.DefaultGameObjectInjectionWorld.EntityManager;

        EntityArchetype entityArchetype = entityManager.CreateArchetype(
            typeof(RenderMesh),
            typeof(LocalToWorld),
            typeof(RenderBounds)
        );

        Entity entity = entityManager.CreateEntity(entityArchetype);

        entityManager.SetSharedComponentData(entity, new RenderMesh {
            mesh = entityMesh,
            material = entityMaterial
        });
    }
}

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.

1 Like

Thanks for your respond.

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