- Home /
ECS RenderMesh not visible
Hello,
I've made a simple script for ECS and Hybrid Renderer, everything works fine except RenderMesh
which do not render at all.
public class GameManager : MonoBehaviour
{
public Mesh newMesh;
public Material newMaterial;
void Start()
{
EntityManager manager = World.DefaultGameObjectInjectionWorld.EntityManager;
EntityArchetype archetype = manager.CreateArchetype(typeof(LocalToWorld), typeof(RenderMesh), typeof(Translation));
Entity entity = manager.CreateEntity(archetype);
manager.SetSharedComponentData<RenderMesh>(entity, new RenderMesh
{
mesh = newMesh,
material = newMaterial,
subMesh = 0,
layer = 0,
castShadows = ShadowCastingMode.On,
receiveShadows = true
});
}
}
Answer by Zyche · Mar 23, 2020 at 07:14 PM
Hey mate,
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.
Best regards
Answer by andrew-lukasik · Jan 02 at 07:58 PM
Most common issues:
Missing components
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/andRotation
to an entity will result inLocalToWorld
being added as well. AddingRenderBounds
andLocalToWorld
will result inWorldRenderBounds
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.
WorldRenderBounds with not encapsulating the mesh properly. This is caused by RenderBounds being offset or not sized properly i.e. it's not zero but it's not correct either. It's harder to investigate this case because meshes can be totally invisible or appear/hide for no apparent reason when camera moves. I strongly suggest you install this package, as it allows you to select entities and see their WorldRenderBounds in the Scene window: https://github.com/JonasDeM/EntitySelection
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 )
} );
}
}
}
RenderMeshUtility works like a charm <3 Indeed, it's adding some other components like "Builtin Material Property Unity | Rendering Layer", etc. Thank you !