- Home /
script created ECS Entity renders black
I'm trying to wrap my head around how ECS entities work in code, and I'm having trouble creating one that works. My goal is to be able to create an ECS entity in code without going through a GameObject.
I'm using the URP and unity 2020.1.12f1. I made a normal GameObject and attached the following code to it (and set the Material to a basic blue material). I do get a sphere in my scene, but it renders as black (as if the shader is broken). When I create a normal GameObject sphere, apply the associated material for it and look at it in the Entity Debugger, it has a bunch more components (AmbientProbeTag, BuiltinMatrialPropertyUnity_, etc).
I'm assuming it won't render properly cause some or all of these are missing, but I'm not sure how to figure out which of them I need, and what to set them to. Ideally I'd like a workflow that lets me go from a material (or more generally, an ECS system and a set of behaviors, in this case "URP" and "render correctly") to the set of components that I need to set up for it to work.
code:
using System.Collections.Generic;
using UnityEngine;
using Unity.Entities;
using Unity.Transforms;
using Unity.Rendering;
using Unity.Mathematics;
public class ECSSpawner : MonoBehaviour
{
[SerializeField] private Material unitMaterial;
void Start()
{
var em = World.DefaultGameObjectInjectionWorld.EntityManager;
var mesh = Resources.GetBuiltinResource<Mesh>("New-Sphere.fbx");
var arch = em.CreateArchetype(
typeof(Translation),
typeof(Rotation),
typeof(RenderMesh),
typeof(RenderBounds),
typeof(LocalToWorld)
);
var ent = em.CreateEntity(arch);
em.AddComponentData(ent, new Translation {Value = new float3(0, 1, 0)});
em.AddSharedComponentData(ent, new RenderMesh { mesh = mesh, material = unitMaterial});
print("Created an entity");
}
}
Answer by bodhizafa · Nov 12, 2020 at 11:51 PM
The code that works (at least in this instance) is as follows. I got this by reading MeshRendererConversion.cs in the hybrid renderer. Unfortunately it doesn't look like this logic from there (and the bunch of other stuff it does that I'll probably need at some point) is exposed publicly in a way that doesn't require a GameObject.
public class ECSSpawner : MonoBehaviour
{
[SerializeField] private Material unitMaterial;
void Start () {
var mesh = Resources.GetBuiltinResource<Mesh>("New-Sphere.fbx");
var world = World.DefaultGameObjectInjectionWorld;
var em = world.EntityManager;
var archetype = em.CreateArchetype(
typeof(RenderMesh),
typeof(RenderBounds),
typeof(WorldRenderBounds),
typeof(LocalToWorld),
typeof(AmbientProbeTag), // Seems to work without this
typeof(PerInstanceCullingTag), // works without this
typeof(BuiltinMaterialPropertyUnity_RenderingLayer), // works without this
typeof(BuiltinMaterialPropertyUnity_LightData),
typeof(WorldToLocal_Tag)
);
float3 position = new float3(0, 1, 0);
var entity = em.CreateEntity(archetype);
em.SetComponentData(entity, new LocalToWorld {
Value = float4x4.TRS(
translation: position,
rotation: quaternion.identity,
scale: Vector3.one
)
});
em.SetSharedComponentData(entity, new RenderMesh {
mesh = mesh,
material = unitMaterial,
subMesh = 0,
layer = 0,
castShadows = UnityEngine.Rendering.ShadowCastingMode.On,
receiveShadows = true
});
em.SetComponentData(entity, new RenderBounds { Value = mesh.bounds.ToAABB() });
em.SetComponentData(entity, new BuiltinMaterialPropertyUnity_RenderingLayer { Value = new uint4(1, 0, 0, 0) });
em.SetComponentData(entity, new BuiltinMaterialPropertyUnity_LightData { Value = new float4(0, 0, 1, 0) });
}
}
THANKS A LOT to @bodhizafa for his very useful & detailed answer, working!
Answer by andrew-lukasik · Jan 24 at 10:53 PM
Since this answer received a ping I'll let myself post a simple and universal (render pipeline-wise) answer:
using UnityEngine;
using UnityEngine.Rendering;
using Unity.Entities;
using Unity.Rendering;
using Unity.Transforms;
using Unity.Mathematics;
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
) );
float3 position = new float3( 1f , 2f , 3f );
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 )
} );
}
}
Answer by adrian2256 · Jan 09, 2021 at 05:14 PM
Seems like you need to set the "Baked Global Illumination" off, in the Lightning tab. I think they haven't added it yet to DOTS.
This appears unrelated to Baked Global Illu$$anonymous$$ation.
Your answer
![](https://koobas.hobune.stream/wayback/20220613015620im_/https://answers.unity.com/themes/thub/images/avi.jpg)