Hello,
I am trying to create an entity by code and have it rendered using the HybridRendererV2. However the RenderMeshUtility fails to set the Mesh on the entity. I used the code from the ECS example however I ported it from a MonoBehavior to a SystemBase and set the Mesh & Material by code.
Mesh = MeshCreator.createMesh(1,1);
Material = MaterialCache.GetMaterial("Unlit");
var desc = new RenderMeshDescription(
Mesh,
Material,
shadowCastingMode: ShadowCastingMode.Off,
receiveShadows: true);
var prototype = EntityManager.CreateEntity();
RenderMeshUtility.AddComponents(
prototype,
EntityManager,
desc);
EntityManager.AddComponentData(prototype, new LocalToWorld());
The MeshCreator.createMesh() is my boiler plate code for creating a mesh which I know works because I used it on many other occasions. Replacing it with new Mesh() doesn’t change anything.
public static Mesh createMesh(float2 size, int2 quads)
{
int totalQuads = quads.x * quads.y;
Vector3[] verts = new Vector3[4 * totalQuads];
int[] tris = new int[6 * totalQuads];
Vector2[] uvs = new Vector2[4 * totalQuads];
int vertCount = 0;
int trisCount = 0;
for (int i = 0; i < quads.x; i++)
{
for (int j = 0; j < quads.y; j++)
{
var sizeX = size.x / quads.x;
var sizeY = size.y / quads.y;
// Verts
verts[vertCount] = (new float3(sizeX * i, sizeY * j, 0));
verts[vertCount + 1] = (new float3(sizeX * i, sizeY * (j + 1), 0));
verts[vertCount + 2] = (new float3(sizeX * (i + 1), sizeY * (j + 1), 0));
verts[vertCount + 3] = (new float3(sizeX * (i + 1), sizeY * j, 0));
// Triangles
tris[trisCount] = (vertCount);
tris[trisCount + 1] = (vertCount + 1);
tris[trisCount + 2] = (vertCount + 2);
tris[trisCount + 3] = (vertCount);
tris[trisCount + 4] = (vertCount + 2);
tris[trisCount + 5] = (vertCount + 3);
// UVS
uvs[vertCount] = (new float2((float)i / quads.x, (float)j / quads.y));
uvs[vertCount + 1] = (new float2((float)i / quads.x, (j + 1f) / quads.y));
uvs[vertCount + 2] = (new float2((i + 1f) / quads.x, (j + 1f) / quads.y));
uvs[vertCount + 3] = (new float2((i + 1f) / quads.x, (float)j / quads.y));
vertCount += 4;
trisCount += 6;
}
}
Mesh mesh = new Mesh();
mesh.vertices = verts;
mesh.triangles = tris;
mesh.uv = uvs;
mesh.SetTriangles(tris, 0);
mesh.RecalculateNormals();
mesh.RecalculateBounds();
return mesh;
}
When running the code, the entity is created and all variables on the RenderMesh component are set except for the mesh which is somehow ignored. In the Entities inspector no mesh is found under the RenderMesh component. Am I missing something here? Has anyone else run into this issue?
Unity 2020.3.15f2
Hybrid Renderer 0.11.0-p44
URP 10.6.0
Entities 0.17.0-p42