Mesh rendering issues after adding faces


I am trying to add faces to a terrain mesh to close it into a “solid” object. I am projecting the mesh vertices into the bottom plane and adding the faces based on the edges of the original mesh. Afterward, I add the vertical faces based on the naked edges from the original.

the code of this last step is copied here:

for (int i = 0; i <= mTopologyEdges.Count - 1; i += 1)
u[0] = mTopologyVerices.MeshVertexIndices(mTopologyEdges.GetTopologyVertices(i).I)[0];
u[1] = mTopologyVerices.MeshVertexIndices(mTopologyEdges.GetTopologyVertices(i).J)[0];

  if ((mTopologyEdges.GetConnectedFaces(i).GetUpperBound(0)) < 1)
    faces.Add(new MeshFace(u[0], u[1], u[1] + vertexCount, u[0] + vertexCount));

The mesh ends up rendering with a gradient across the vertical axis. I am assuming this has to do with vertex normals? but I am not sure. Does anyone have any idea on how to fix this?

I am hoping to get something like the left model here as opposed to the right one

Thanks! (205.4 KB)

Try (M: a mesh) :

var MV = M.Vertices;
MV.CombineIdentical(true, true);
M.UnifyNormals(); // BTW: it works when it works

Thanks for the tip!
Did not help in this case though, but I am sure it will be useful for me some other time so I appreciate it !

Run these at the end of your mesh. After what @PeterFotiadis added. So First

M.Vertices.CombineIdentical(true, true);



@Michael_Pryor @PeterFotiadis

Thanks, guys! Actually in this particular case M.Vertices.CombineIdentical(true, true) would in fact cause this same rendering problem and perhaps ruin a properly rendered mesh. The issue with the mesh I was creating was that it did not have identical vertices to begin with. Notice how a simple mesh box with 8 corners will need 24 vertices(right) to render correctly instead of just 8(left).

To fix my problem I had to duplicate the corner vertices and assign the proper normals to them. you could run M.Vertices.CombineIdentical(FALSE, true) at the end to clean things up but have to make sure you are not ignoring vertices with different vertex normals.