If you are trying this one on newer versions of unity (2019.3.x), you NEED to add typeof(RenderBounds) to your archetype. Without this IT WILL NOT DISPLAY.
If you open up an Entity Debugger ( Systems since entities v 0.50 ) window it will list all component types that systems look for (aka “entity query”). If an Entity doesn’t match that list (query) - entity is simply ignored. And this is exactly what happens here.
Note: some specific components are added automatically by systems when criteria is met. For example: adding Translation or/and Rotation to an entity will result in LocalToWorld being added as well. Adding RenderBounds and LocalToWorld will result in WorldRenderBounds being added.
RenderBounds with zero size. You can identify this case in cases where mesh is visible ONLY while it’s world position (mesh origin) is visible to the camera and whole disappears immediately when it goes off screen.
Render Pipeline-specific components are missing because URP and HDRP require different data sets to work properly. This often results in meshes being black or not affected by lights. Fix: use RenderMeshUtility (see code sample below).
_
TL;DR:
Here is a preferable workflow to create mesh entities. It adds all required components and initializes their values properly.
using UnityEngine;
using UnityEngine.Rendering;
using Unity.Entities;
using Unity.Rendering;
using Unity.Transforms;
using Unity.Mathematics;
using Random = UnityEngine.Random;
public class LetsCreateARenderMeshEntity : MonoBehaviour
{
[SerializeField] Mesh _mesh = null;
[SerializeField] Material _material = null;
void Start ()
{
var entityManager = World.DefaultGameObjectInjectionWorld.EntityManager;
Entity prefab = entityManager.CreateEntity( typeof(Prefab) );// prefab is invisible & inactive
RenderMeshUtility.AddComponents( prefab , entityManager , new RenderMeshDescription(
mesh:_mesh , material:_material , shadowCastingMode:ShadowCastingMode.On , receiveShadows:true
) );
for( int i=0 ; i<1000 ; i++ )
{
float3 position = Random.insideUnitSphere * new float3( 100f , 100f , 100f );
quaternion rotation = quaternion.Euler( 0f , 0f , 0f );
float3 scale = Vector3.one;
Entity instance = entityManager.Instantiate( prefab );
entityManager.SetComponentData( instance , new LocalToWorld{
Value = float4x4.TRS( position , rotation , scale )
} );
}
}
}