What I’m trying to do
I’m trying to create a tool which creates a plane plane with a specific amount of subdivisions on the X and Y axis. (aka a grid)
What I’m having problems with
I can’t seem to figure out how to generate triangles/indices for my mesh.
This is the function which creates the mesh and generates all its values:
private void CreateTerrain()
{
GameObject go = new GameObject(terrainName);
MeshFilter mf = go.AddComponent<MeshFilter>();
MeshRenderer mr = go.AddComponent<MeshRenderer>();
Mesh mesh = new Mesh();
List<Vector3> vertices = new List<Vector3>();
List<int> triangles = new List<int>();
Vector3[] face = new Vector3[] {
new Vector3(0, 0, 0),
new Vector3(1, 0, 0),
new Vector3(1, 1, 0),
new Vector3(0, 1, 0)
};
for (uint x = 0; x < terrainSize.x; x++)
{
for (uint y = 0; y < terrainSize.y; y++)
{
Vector3[] currentFace = new Vector3[] {
new Vector3(0 + x, 0 + y, 0),
new Vector3(1 + x, 0 + y, 0),
new Vector3(1 + x, 1 + y, 0),
new Vector3(0 + x, 1 + y, 0)
};
vertices.Add(currentFace[0]);
vertices.Add(currentFace[1]);
vertices.Add(currentFace[2]);
vertices.Add(currentFace[3]);
triangles.Add(0 + (int)x * (int)y);
triangles.Add(1 + (int)x * (int)y);
triangles.Add(2 + (int)x * (int)y);
triangles.Add(0 + (int)x * (int)y);
triangles.Add(2 + (int)x * (int)y);
triangles.Add(3 + (int)x * (int)y);
}
}
mesh.vertices = vertices.ToArray();
mesh.triangles = triangles.ToArray();
mf.mesh = mesh;
mesh.RecalculateBounds();
mesh.RecalculateNormals();
}
However when calling this function it doesn’t seem to properly create the mesh shape that I want:
As you can see the shape isn’t anywhere close to a 10x10 plane/grid like it should be.