Odd lighting on procedurally generated mesh

I’m making a bunch of meshes based on a set of 2d coordinates and an extrusion modifier. The shape creation is working well (mostly, but that’s another question), but I’m having trouble with normals/uvs. As you can see from this image, the lighting is very odd. I’ve followed [this tutorial][1], but still end up with this odd lighting. Any suggestions?

[16399-messedupcolors.jpg*_|16399]

The code specific to UV:

		//Create the UV map. Nothing special here, copied and pasted.
		Vector2[] uvs = new Vector2[AllVerts.Length]; 
		for (int i=0; i<uvs.Length; i++) {
			 uvs <em>= new Vector2(AllVerts_.x, AllVerts*.z);*_</em>

* }*

* newMesh.uv = uvs;*
* newMesh.triangles = NewTriangles;*

* newMesh.RecalculateNormals();*
The entire function:
* //Given a name, list of points, and a depth, return a procedurally generated shape.*
* GameObject CreateObject(string name, List points, float depth){*

* if(depth < 0){ //TODO: Swap this into something that moves the plane down, or reverses triangles or vertices, for negatives*
* depth = Mathf.Abs(depth);*
* }*

* GameObject newObject = new GameObject(name);*
* newObject.AddComponent();*
* newObject.AddComponent();*
* Mesh newMesh = new Mesh();*

* int howManyVerts = points.Count; //Count the number of verts on a flat surface*
_ Vector3 AllVerts = new Vector3[howManyVerts * 2]; //Twice as many verst as on a flab surface_
* List TriangleList = new List(); //Gotta put all of the triangles somewhere - defined by the index of 3 verts in AllVerts*

* for(int i = 0; i < howManyVerts; i++){*

AllVerts = new Vector3(points_.x, 0, points*.y); //Convert points to Vector3*
AllVerts[i+howManyVerts] = new Vector3(points.x, depth, points*.y); //Replaces with depth*_

* if(i < howManyVerts-1){*

* //First triangle*
* TriangleList.Add (i+howManyVerts);*
* TriangleList.Add (i+1);*
* TriangleList.Add (i);*
* //Second triangle*
* TriangleList.Add (i+howManyVerts+1);*
* TriangleList.Add (i+1);*
* TriangleList.Add (i+howManyVerts);*
* }*
* else{//For the final piece of wall*
* //First triangle*
* TriangleList.Add (i+howManyVerts);*
* TriangleList.Add (0);*
* TriangleList.Add (i);*
* //Second triangle*
* TriangleList.Add (i+howManyVerts);*
* TriangleList.Add (howManyVerts);*
* TriangleList.Add (0);*
* }*
* }*

* //Create data for caps*
* Vector2[] TopCapVertices= new Vector2[howManyVerts];*
* for(int i = 0; i<howManyVerts; i++){*
TopCapVertices = points*;*
* }*

* Triangulator tr = new Triangulator(TopCapVertices);*
* int[] indices = tr.Triangulate();*

* int[] NewTriangles = new int[TriangleList.Count + indices.Length];*

* //Add triangles from TriangleList*
* for(int i = 0; i<TriangleList.Count; i++){*
NewTriangles = TriangleList*;*
* }*
* //Add triangles from the Triangulator*
* for(int i = 0; i<indices.Length; i++){*
_ NewTriangles[i+TriangleList.Count] = indices*+howManyVerts;
}*_

* newMesh.vertices = AllVerts;*

* //Create the UV map. Nothing special here, copied and pasted.*
* Vector2[] uvs = new Vector2[AllVerts.Length];*
* for (int i=0; i<uvs.Length; i++) {*
uvs = new Vector2(AllVerts_.x, AllVerts*.z);
}*_

* newMesh.uv = uvs;*
* newMesh.triangles = NewTriangles;*

* newMesh.RecalculateNormals();*

* newObject.GetComponent().mesh = newMesh; *

* newObject.AddComponent();*

* //ADD MATERIAL*

* Material defaultMaterial = new Material(Shader.Find (“Diffuse”));*
* defaultMaterial.color = Color.blue;*

* newObject.renderer.material = defaultMaterial;*

* return newObject;*
* }*
*[1]: http://games.deozaan.com/unity/MeshTutorial.pdf*_
*

There are a few things wrong in your script (and the tutorial).

First you can’t use shared vertices it you want to have lighting (which requires normals). If you share the top front vertices with both surfaces (top and side) you can’t have a sharp edge which you need it the think should look like a box. The normals are part of the vertices. Each vertex has one normal. Since you share the vertices between two or more surfaces the normal which got calculated by RecalculateNormals is in between both surfaces which makes the box look like curved thing.

Next thing is the uv coordinates. Besides that you also need to split the vertices for the UVs, the way you calculate them is based on the vertex positions x and z values. However that doesn’t work for the side faces because the top and bottom vertices have the same x and z value (since they are on top of each other).

Unfortunately I can’t give you any advices how to fix this since I don’t know where your data is comming from and what’s the desired result. Why do you calculate UVs when you don’t use textures? Do you want to use textures in the end?

If RecalculateNormals give weird result, it’s because you probably share vertices between faces. If you want your normal to be “exact”, you need to have 4 uniques vertices per faces.