Any algorithm to have many evenly placed triangles for this mesh?

CURRENT:

Example of what im trying to do:
8202261--1070160--upload_2022-6-13_20-9-30.png

    Mesh CreateMesh(int num)
    {
        nodePositions = new Vector3[transform.childCount];

        for (int i = 0; i < nodePositions.Length; i++)
            nodePositions[i] = transform.GetChild(i).localPosition;


        int x; //Counter

        //Create a new mesh
        Mesh mesh = new Mesh();

        //Vertices
        var vertex = new Vector3[nodePositions.Length];

        for (x = 0; x < nodePositions.Length; x++)
        {
            vertex[x] = nodePositions[x];
        }

        //UVs
        var uvs = new Vector2[vertex.Length];

        for (x = 0; x < vertex.Length; x++)
        {
            if ((x % 2) == 0)
            {
                uvs[x] = new Vector2(0, 0);
            }
            else
            {
                uvs[x] = new Vector2(1, 1);
            }
        }

        //Triangles
        var tris = new int[3 * (vertex.Length - 2)];    //3 verts per triangle * num triangles
        int C1, C2, C3;

        if (num == 0)
        {
            C1 = 0;
            C2 = 1;
            C3 = 2;

            for (x = 0; x < tris.Length; x += 3)
            {
                tris[x] = C1;
                tris[x + 1] = C2;
                tris[x + 2] = C3;

                C2++;
                C3++;
            }
        }
        else
        {
            C1 = 0;
            C2 = vertex.Length - 1;
            C3 = vertex.Length - 2;

            for (x = 0; x < tris.Length; x += 3)
            {
                tris[x] = C1;
                tris[x + 1] = C2;
                tris[x + 2] = C3;

                C2--;
                C3--;
            }
        }

        //Assign data to mesh
        mesh.vertices = vertex;
        mesh.uv = uvs;
        mesh.triangles = tris;

        //Recalculations
        mesh.RecalculateNormals();
        mesh.RecalculateBounds();
        mesh.Optimize();

        //Name the mesh
        mesh.name = "MyMesh";

        //Return the mesh
        return mesh;
    }

Lots of algorithms, just not easy ones. :slight_smile:

The solution you propose above will involve remeshing, which involves topics such as graph relaxation and boolean mesh operators, and also falls into the Constructive Solid Geometry (CSG) area of study.

For the record, the graphics engine usually doesn’t care.

sounds kinda complex, i already spent few weeks on this before but didnt get good results

“Good results” would imply that you have a “this is a good tesselation and this is a bad tesselation” criteria, which I don’t actually see you state anywhere above.

sorry, you throw too many complex words at once, im confused

I believe the term you’re looking for (which is a rough synonym for “evenly spread triangles”) is Delaunay triangulation.

There’s several approaches to generate a delaunay triangulation out of a boundary polygon. Some resources:

https://github.com/OskarSigvardsson/unity-delaunay
https://github.com/mattatz/unity-triangulation2D

Once you have a base delaunay triangulation, you can further “smooth” vertex placement by moving them to the average of their neighbors. Some terms loosely related to this are “variational” (aka energy-based) smoothing and laplacian smoothing.

1 Like