- Home /
Plane mesh generation help
I have made a method to produce a plane mesh that has options for tiling on both axis as well as size. I have written code to produce the vertices, but I am having trouble calculating the triangles. I cannot see what I have done wrong, I have my code here along with the result it makes:
     public Mesh GeneratePlaneMesh (int width, int height,
             int widthTile, int heightTile) {
 
         List<Vector3> vertices = new List<Vector3> ();
         List<Vector3> normals = new List<Vector3> ();
         List<int> triangles = new List<int> ();
         
         float singleWidth = width / widthTile;
         float singleHeight = height / heightTile;
         int triangleCount = 0;
 
         for (int i2 = 0; i2 < heightTile; i2 ++) {
 
             for (int i = 0; i < widthTile; i ++) {
 
                 vertices.Add(new Vector3(singleWidth * i, 0, i2 * singleHeight));
                 normals.Add(Vector3.up);
 
                 Debug.Log("Added new vertice at (" + singleWidth * i + ", 0, " + i2 * singleHeight + ") with index " + vertices.IndexOf(new Vector3(singleWidth * i, 0, i2 * singleHeight)));
 
             }
 
         }
 
         Debug.Log ("Added a total of " + vertices.Count + " vertices");
 
         // Single plane vertex indice expressions:
         /*
          * 
          * 0 = (i2 * widthTile) + i;
          * 1 = (i2 * widthTile + 1) + i;
          * 2 = ((i2 + 1) * widthTile + 1) + i;
          * 3 = ((i2 + 1) * widthTile) + i;
          * 
          */
 
         // Single plane triangle vertex arangement:
         /*
          * 
          * 2,3,0,
          * 0,1,2
          * 
          */
 
         bool stop = false;
 
         for (int i2 = 0; i2 < heightTile; i2 ++) {
 
             for (int i = 0; i < widthTile; i ++) {
 
                 int t0 = (i2 * widthTile) + i;
                 int t1 = (i2 * widthTile + 1) + i;
                 int t2 = ((i2 + 1) * widthTile + 1) + i;
                 int t3 = ((i2 + 1) * widthTile) + i;
 
                 //if (triangleCount % 4 == 0) {
 
                 triangles.Add (t2);
                 triangles.Add (t3);
                 triangles.Add (t0);
                 triangles.Add (t0);
                 triangles.Add (t1);
                 triangles.Add (t2);
 
                 Debug.Log ("Indexes: 0 = " + t0 + ", 1 = " + t1 + ", 2 = " + t2 + ", 3 = " + t3);
                 Debug.Log("Added 2 triangles with indexes (" + t2 + ", " + t3 + ", " + t0 + ") and (" + t0 + ", " + t1 + ", " + t2 + ")");
 
                 if (t2 == vertices.Count - 1) {
 
                     stop = true;
                     break;
 
                 }
 
                 //}
 
                 //triangleCount ++;
 
             }
 
             if (stop) {
 
                 break;
 
             }
 
         }
 
         Mesh m = new Mesh ();
         m.vertices = vertices.ToArray ();
         m.triangles = triangles.ToArray ();
         m.normals = normals.ToArray ();
 
         return m;
 
     }
Result (shaded and wirframe): 

Note: the planes are currently being generated as 6x5 with widthTiling as 6 and heightTiling as 5
Answer by GoodKingJohn · Jun 02, 2015 at 05:37 PM
Nevermind, I just had my triangles wrong. The correct order of indexes was: 2,1,0,0,3,2.
Your answer
 
 
             Follow this Question
Related Questions
Generating Triangles Given a Set of Points 1 Answer
Voxel mesh generation not working. 1 Answer
automatically creating triangles 2 Answers
Setting triangles failing 0 Answers
Connecting flatshaded vertices 0 Answers
 koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                