In unity I have a script which is attached to a game object with a mesh filter. The purpose of the script is to generate a flat plane of 100x100 vertices. The code for the script compiles in monoDevelop without errors but when the play button is pressed in the scene the plane mesh renders successfully. However the last 97 squares of two triangles each are missing as can be seen in the photo:
My question is: why are the last 97 squares of the plane mesh not rendering? – I can’t find the bug in the code which is causing the error and was hoping someone might point it out. Here is the script to generate the mesh:
using UnityEngine;
using System.Collections;
public class PlaneScript : MonoBehaviour {
//Global variables
Vector3[] vertices = new Vector3[10000]; //Initialise the array of vertices to be 10000 (100x100)
int[] triangles = new int[58806]; //Set size of array of triangles
Vector3[] normals = new Vector3[10000]; //Number of normals same as the number of vertices
// Use this for initialization
void Start () {
MeshFilter mf = GetComponent<MeshFilter> ();
Mesh mesh = new Mesh ();
mf.mesh = mesh;
prepVerts ();
prepTris ();
prepNormals ();
mesh.vertices = vertices;
mesh.triangles = triangles;
mesh.normals = normals;
}
// Update is called once per frame
void Update () {
}
void prepVerts()
{
int pos;
//Intialise the vertices of the bezier curve
for (float i = 0; i<100; i++) {
for (float j = 0; j<100; j++) {
pos = (int)((i * 100) + j);
vertices [pos] = new Vector3 (j, 0f, i);
}
}
}
void prepTris()
{
int countSq = 0;
int index = 0;
//for (int j = 0; j<3; j++) { //Should be <100
for (int i = 0; i<9802; i++) { //Should be <100
if(countSq == 99) //If we have gotten to the end of the line(same as number of squares in row) increment i by 1
{
i+=1;
countSq = 0;
}
//Triangle 1
triangles [index] = i; //0, 1
triangles [index + 1] = i + 100; //2, 3
triangles [index + 2] = i + 1; //1, 4
//Triangle 2
triangles [index + 3] = i + 100; //2, 3
triangles [index + 4] = i + 101; //3, 5
triangles [index + 5] = i + 1; //1, 4
index += 6;
if(index>58805)
{
i = 9802;
}
countSq += 1;
}
}
void prepNormals()
{
int pos;
//Intialise the vertices of the bezier curve
for (float i = 0; i<100; i++) {
for (float j = 0; j<100; j++) {
pos = (int)((i * 100) + j);
normals[pos] = Vector3.up;
}
}
}
}