EntitiesGraphicsSystem.RegisterMesh with URP/Lit, Black mesh?

Hello eveyone,

I’m having trouble getting a lit shader to work on my generated mesh.
The mesh appears completely black when using a URP/Lit material, but works fine with URP/Unlit materials.

Here are the components i add:

		ecb.AddComponent<LocalToWorld>(entity);
		ecb.AddComponent<WorldRenderBounds>(entity);
		ecb.AddComponent<MaterialMeshInfo>(entity);
		// enable later
		ecb.SetComponentEnabled<MaterialMeshInfo>(entity, false);

		ecb.AddComponent<RenderBounds>(entity);
		ecb.AddComponent<PerInstanceCullingTag>(entity);
		ecb.SetSharedComponent(entity, terrainChunkInfoSingleton.FilterSettings);

Later i have systems that calulate vertices, normals, and indices
and then in my Mesh instancing system i have a setup like this:

 		Mesh mesh = new Mesh();
                
                VertexAttributeDescriptor[] layout = new[] {
                    new VertexAttributeDescriptor(VertexAttribute.Position,  VertexAttributeFormat.Float32, 3, 0),
                    new VertexAttributeDescriptor(VertexAttribute.Normal,    VertexAttributeFormat.Float32, 3, 1),
                };
                
                mesh.SetVertexBufferParams(vertices.Length, layout);
                mesh.SetIndexBufferParams(indices.Length, IndexFormat.UInt32);
                NativeArray<float3> vertexArray = vertices.Reinterpret<float3>().AsNativeArray();
                NativeArray<float3> normalArray = normals.Reinterpret<float3>().AsNativeArray();
                NativeArray<int>    indexArray  = indices.Reinterpret<int>().AsNativeArray();
                
                mesh.SetVertexBufferData(vertexArray, 0, 0, vertices.Length, 0, MeshUpdateFlags.DontValidateIndices);
                mesh.SetVertexBufferData(normalArray, 0, 0, normals.Length,  1, MeshUpdateFlags.DontValidateIndices);
                mesh.SetIndexBufferData(indexArray, 0, 0, indices.Length, MeshUpdateFlags.DontValidateIndices);

                mesh.subMeshCount = 1;
                mesh.SetSubMesh(0, new SubMeshDescriptor(0, indices.Length));
		
		//TODO: calc bounds differently
                mesh.RecalculateBounds();
                
                BatchMeshID meshID = graphicsSystem.RegisterMesh(mesh);

                if (meshMapping.TryGetValue(entity, out BatchMeshID value)) {
                    graphicsSystem.UnregisterMesh(value);
                }
                
                meshMapping[entity] = meshID;

                SystemAPI.SetComponent(entity, new MaterialMeshInfo {
                    MeshID = meshID,
                    MaterialID = new BatchMaterialID(){value = materialID.ValueRO.Value}
                });
                SystemAPI.SetComponentEnabled<MaterialMeshInfo>(entity, true);

                SystemAPI.SetComponent(entity, new RenderBounds {
                    Value = mesh.bounds.ToAABB()
                });

material is correctly registered in a baker earlier. I checked my Normals, Indices and Verts, all seem fine.
There is a cube in the scene with the same lit material that works.

Here a screen of what it looks like, I’m drawing the tris and normals with debug.

What could I be missing?

I’d suggest making an unlit shader that renders the normals as colors.

1 Like

using RenderMeshUtility.AddComponents() worked

it uses:

var components = new FixedList128Bytes<ComponentType>
{
                    // Absolute minimum set of components required by Entities Graphics
                    // to be considered for rendering. Entities without these components will
                    // not match queries and will never be rendered.
                    ComponentType.ReadWrite<WorldRenderBounds>(),
                    ComponentType.ReadWrite<RenderFilterSettings>(),
                    ComponentType.ReadWrite<MaterialMeshInfo>(),
                    ComponentType.ChunkComponent<ChunkWorldRenderBounds>(),
                    ComponentType.ChunkComponent<EntitiesGraphicsChunkInfo>(),
                    // Extra transform related components required to render correctly
                    // using many default SRP shaders. Custom shaders could potentially
                    // work without it.
                    ComponentType.ReadWrite<WorldToLocal_Tag>(),
                    // Components required by Entities Graphics package visibility culling.
                    ComponentType.ReadWrite<RenderBounds>(),
                    ComponentType.ReadWrite<PerInstanceCullingTag>(),
                };

Added all of them and now it works.