I’m working on making a planet generator, and to start it off I’m trying to see if I can make a plane of procedurally generates squares.
I have it print out each time a vertex is made, and every time a square is made out of 2 triangles, as well as the coordinates, and even though the console prints out everything properly, only the top row of squares appear.
If it helps, this is my spaghetti-looking code:
(resolution is the amount of squares each side, so a res of 2 would make a 2 by 2 plane, 3 would make a 3 by 3 plane, etc)
int resolution=10;
int numVert = (resolution+1)*(resolution+1)+1;
int numTri = resolution*resolution*6;
Debug.Log(numVert);
Debug.Log(numTri);
Mesh mesh = new Mesh();
Debug.Log(resolution);
Vector3[] vertices = new Vector3[numVert];
int[] triangles = new int[numTri];
int i = 0;
int i2 = 0;
int x = 0;
int y = 0;
for (y = 0; y <= resolution; y++)
{
for (x = 0; x <= resolution; x++)
{
vertices[i+1] = new Vector3(x*2,-y*2,0);
i=i+1;
Debug.Log("Vertex made at "+x+","+y);
}
}
for (y = 1; y <= resolution; y++)
{
for (x = 1; x <= resolution; x++)
{
triangles[i2] = x+resolution+1;
triangles[i2+1] = x;
triangles[i2+2] = x+1;
triangles[i2+3] = x+resolution+1;
triangles[i2+4] = x+1;
triangles[i2+5] = x+resolution+2;
i2=i2+6;
Debug.Log("Square made at "+x+","+y);
}
}
mesh.vertices = vertices;
mesh.triangles = triangles;
GetComponent<MeshFilter>().mesh = mesh;
MeshCollider meshc = gameObject.AddComponent(typeof(MeshCollider)) as MeshCollider;
meshc.sharedMesh = mesh;
}