Uneven lighting with shared vertices when building mesh

I’m in the process of creating a script that generates a mesh. The mesh will cover the top surface of several side-by-side cubes, and if the cube does not have a neighbor on a given side (right, left, forward, backward), it should get a rounded lip. I have the script up and running, and it works well. However, I have noticed that the rounded lip has uneven lighting.

To achieve the rounded lip, I’m creating 6 vertices. Example:

Vector3[] verts = new Vector3[] { 
  new Vector3(0, 0, 0),
  new Vector3(0, 0, .1f),
  new Vector3(1, 0, .1f),
  new Vector3(1, 0, 0),
  new Vector3(0, -.1f, .1f),
  new Vector3(1, -.1f, .1f)
};

And then using these shared vertices, I create two quads.

int[] tris = new int[] {
  0, 1, 2, 2, 3, 0,
  1, 4, 5, 5, 2, 1
};

When I build the mesh, everything is fine except that the lighting is uneven on either end of the rounded edge. Here are some screenshots of what’s happening (these are side-by-side rounded lips that were created in the mesh).



After spending more time looking into this issue, I figured out the problem was with the normals. At either end, the shared vertices on the edge weren’t perfectly between the top and bottom vertices. They were staggered toward the bottom or top. When they were side-by-side, each end reflected light differently.

I solved this by tracking where the edge vertices were in my mesh vertices array. Then I let Unity recalculate normals, and at the edge normals, I manually recalculated with (pseudo code):

edgeNormal = (topNormal + bottomNormal) * .5f;