Generating a plane mesh with code C#

Trying to create a plane mesh with code. There is something about the order that the triangles are ordered that causes it not to render correctly. Can’t for the life of me figure out what.

GameObject createMeshBlock(float xval, float yval){
	GameObject block = new GameObject("Block");
	Mesh newMesh = new Mesh();
	block.AddComponent("MeshFilter");
	block.AddComponent("MeshRenderer");
	List verticeList = new List();
	List uvList = new List();
	List triList = new List();

	int width = 2;
	int height = 2;
	for(float x = 0; x < width; x++){
		for(float y = 0; y < height; y++){
			verticeList.Add(new Vector3(0f+x,0f,0f+y));
			verticeList.Add(new Vector3(1f+x,0f,0f+y));
			verticeList.Add(new Vector3(0f+x,0f,1f+y));
			verticeList.Add(new Vector3(1f+x,0f,1f+y));

			uvList.Add(new Vector2(0f+x,0f+y));
			uvList.Add(new Vector2(1f+x,0f+y));
			uvList.Add(new Vector2(0f+x,1f+y));
			uvList.Add(new Vector2(1f+x,1f+y));

			triList.Add(3+(int)x*4+(int)y*4);
			triList.Add(1+(int)x*4+(int)y*4);
			triList.Add(0+(int)x*4+(int)y*4);
			triList.Add(0+(int)x*4+(int)y*4);
			triList.Add(2+(int)x*4+(int)y*4);
			triList.Add(3+(int)x*4+(int)y*4);
		}
	}

	Vector3[] newVertices = new Vector3[verticeList.Count];
	int i = 0;
	for(i = 0; i < verticeList.Count;i++){
		newVertices _= verticeList*;*_

* }*
* Vector2[] newUV = new Vector2[uvList.Count];*
* for(i = 0; i < uvList.Count;i++){*
newUV = uvList*;*
* }*
* int[] newTriangles = new int[triList.Count];*
* for(i = 0; i < triList.Count;i++){*
newTriangles = triList*;*
* }*
* newMesh.vertices = newVertices;*
* newMesh.uv = newUV;*
* newMesh.triangles = newTriangles;*
* newMesh.RecalculateNormals();*
* block.GetComponent().mesh = newMesh;*
* block.renderer.material = rockMaterial;*
* return block;*
* }*
it renders like this:
![alt text][1]
_*[1]: http://i.imgur.com/WstOY.png*_

I think it is how youre building your triList.

try:

         triList.Add(3+(int)x*4+(int)y*4);
         triList.Add(1+(int)x*4+(int)y*4);
         triList.Add(0+(int)x*4+(int)y*4);
         triList.Add(1+(int)x*4+(int)y*4);
         triList.Add(2+(int)x*4+(int)y*4);
         triList.Add(3+(int)x*4+(int)y*4);

try it :

    for (float x = 0; x < width; x++)
    {
        for (float y = 0; y < height; y++)
        {
            verticeList.Add(new Vector3(0f + x, 0f, 0f + y));
            verticeList.Add(new Vector3(1f + x, 0f, 0f + y));
            verticeList.Add(new Vector3(0f + x, 0f, 1f + y));
            verticeList.Add(new Vector3(1f + x, 0f, 1f + y));
        }
    }

    for( int i = 0; i < (width * height); i++ )
    {
        triList.Add(3 + i * 4);
        triList.Add(1 + i * 4);
        triList.Add(0 + i * 4);
        triList.Add(0 + i * 4);
        triList.Add(2 + i * 4);
        triList.Add(3 + i * 4);
    }

The trick is to add the triangles in a specific clockwise order. Also, I think you’re adding too many vertices, try something like this:

private GameObject createMeshBlock()
{
    GameObject block = new GameObject("Block");
    Mesh newMesh = new Mesh();
    block.AddComponent<MeshFilter>();
    block.AddComponent<MeshRenderer>();
    List<Vector3> verticeList = new List<Vector3>();
    List<Vector2> uvList = new List<Vector2>();
    List<int> triList = new List<int>();
    int width = 2;
    int height = 2;
    for (int x = 0; x < width; x++)
    {
        for (int y = 0; y < height; y++)
        {
            verticeList.Add(new Vector3(x, 0f, y));
            uvList.Add(new Vector2(x, y));
            //Skip if a new square on the plane hasn't been formed
            if (x == 0 || y == 0)
                continue;
            //Adds the index of the three vertices in order to make up each of the two tris
            triList.Add(width * x + y); //Top right
            triList.Add(width * x + y - 1); //Bottom right
            triList.Add(width * (x - 1) + y - 1); //Bottom left - First triangle
            triList.Add(width * (x - 1) + y - 1); //Bottom left 
            triList.Add(width * (x - 1) + y); //Top left
            triList.Add(width * x + y); //Top right - Second triangle
        }
    }
    newMesh.vertices = verticeList.ToArray();
    newMesh.uv = uvList.ToArray();
    newMesh.triangles = triList.ToArray();
    newMesh.RecalculateNormals();
    block.GetComponent<MeshFilter>().mesh = newMesh;
    block.GetComponent<Renderer>().material = rockMaterial;
    return block;
}

@cjdev this is really helpful, thanks!

I found that it crashed for rectangles, when width != height. this fixes it-

lines 22-27: (change all the “width” to “height”)

         triList.Add(height * x + y); //Top right
         triList.Add(height * x + y - 1); //Bottom right
         triList.Add(height * (x - 1) + y - 1); //Bottom left - First triangle
         triList.Add(height * (x - 1) + y - 1); //Bottom left 
         triList.Add(height * (x - 1) + y); //Top left
         triList.Add(height * x + y); //Top right - Second triangle