Unity 3D Mesh Generation with Quads to Triangles

1k views Asked by At

My goal is to create a simple flat mesh with a grid of vertices. These vertices are then manipulated to create a simple terrain with some areas being raised and flat like a plateau, and these plateaus are connected to the areas below by slopes.

The problem is when I create the vertices, then create the triangles, and set it all up as a mesh, the triangles will not allow the terrain to be modified because if some vertices are moved up in the Y axis, then it wont produce flat plateaus with symmetrical slopes all around like I required.

In this case a picture speaks a million words.

What I dont want: enter image description here

You can see that the left side of the slope's triangles are different than those on the right, in the image below the left and right sides have the same triangles to create the outside of the slopes. This is what im trying to achieve.

mmm Another angle of the wrong triangles, here you can clearly seach each side is different, on the sides of the slope.

////////////////////////////////

What i want: enter image description here

I know the difference looks minimal but when creating these random flat plateaus raised over an otherwise flat grid of triangles I am trying to achieve a somewhat smooth look.

A solution I think to achieve the correct result could maybe be to create quads first, raise the vertices as desired to make random flat plateaus, and then convert all the quads to triangles in a way that will make the mesh symmetrical and good looking.

Heres my code:

public class GridMeshGenerator : MonoBehaviour
{
    public int xSize, zSize, totalVertices;
    public int[] plateauSizes;

    public float plateauHeight;

    public List<int> plateauVertexList;
    int[] plateauVertexArray;


    Mesh mesh;
private void Start()
{
    totalVertices = (xSize + 1) * (zSize + 1);
    GeneratePlateauVertexList();
    GenerateGridMesh();
}

void GeneratePlateauVertexList()
{
    plateauVertexList = new List<int>();
    var plateaus = 3;

    for (int j = 0; j < plateaus; j++)
    {
        var rndSize = plateauSizes[Random.Range(0, plateauSizes.Length)];
        var rndVertex = Random.Range((xSize * 2) + 2, totalVertices - ((zSize * rndSize) + 2));

        plateauVertexArray = new int[rndSize * rndSize];

        for (int k = 0, i = 0; k < rndSize; k++)
        {
            for (int l = 0; l < rndSize; l++, i++)
            {
                plateauVertexArray[i] = (rndVertex + ((xSize + 1) * k)) + l;

                if (!plateauVertexList.Contains(plateauVertexArray[i]))
                    plateauVertexList.Add(plateauVertexArray[i]);
            }
        }
    }
}

void GenerateGridMesh()
{
    GetComponent<MeshFilter>().mesh = mesh = new Mesh();
    mesh.name = "Procedural Grid";


    var vertices = new Vector3[totalVertices];
    for (int i = 0, z = 0; z <= zSize; z++)
    {
        for (int x = 0; x <= xSize; x++, i++)
        {
            vertices[i] = new Vector3(x, 0, z);
        }
    }

    foreach (int ii in plateauVertexList)
    {
        vertices[ii] = new Vector3(vertices[ii].x, plateauHeight, vertices[ii].z);
    }



    mesh.vertices = vertices;



    int[] triangles = new int[xSize * zSize * 6];
    for (int ti = 0, vi = 0, z = 0; z < zSize; z++, vi++)
    {
        for (int x = 0; x < xSize; x++, ti += 6, vi++)
        {
            triangles[ti] = vi;
            triangles[ti + 3] = triangles[ti + 2] = vi + 1;
            triangles[ti + 4] = triangles[ti + 1] = vi + xSize + 1;
            triangles[ti + 5] = vi + xSize + 2;
            
            //triangles[ti + 0] = vi + 0;
            //triangles[ti + 1] = vi + xSize + 1;
            //triangles[ti + 2] = vi + 1;
            //triangles[ti + 3] = vi + 1;
            //triangles[ti + 4] = vi + xSize + 1;
            //triangles[ti + 5] = vi + xSize + 2;
        }
    }
    mesh.triangles = triangles;
}

}

This last part I think I need to change how the triangles are made, some corners of the plateau should connect their triangles differently than others, but I'm pulling my hair out trying to figure out how to check which vertex from the List is a corner and then which corner and in which way the triangles should be connected. I'm lost, please help!

Thanks a lot.

0

There are 0 answers