Generating Mesh using Points

Hello all,

I am trying to connect some generated points to form a mesh, but despite my best efforts to try and understand how to piece the mesh and triangles together, I’m still struggling!

The image below is composed of an array, ‘vertices’, that I wish to connect. The points consist of a ‘torus_count’ number of points in a torus before moving to the next one along. This is best illustrated by the diagram below. I have also included the code in the OnGUI section which enabled me to produce this diagram.

In short, what is the best way now to go from here to generating a mesh array and a triangles array?

Many thanks,

Popuppirate

Code:

 void OnGUI (){
for (int i=0; i<vertices.Length; i++) {
               if (i < vertices.Length - torus_count)
               {

                   if (i % torus_count == torus_count-1)
                   {// Draws last line of each section
                       Gizmos.DrawLine(vertices*, vertices[i + torus_count]);* 

continue;
}
Gizmos.DrawLine(vertices*, vertices[i + torus_count]);
Gizmos.DrawLine(vertices[i + torus_count], vertices[i + torus_count+1]);
Gizmos.DrawLine(vertices[i + torus_count], vertices[i + 1]);
_Gizmos.DrawLine(vertices[i + 1], vertices);
}*_

}
}

[58072-screenshot-2.jpg|58072]

You can try somethig like this code. I hope you understand. You have to create a Mesh, then fill in the triangle and UV mapping data. I hope it helps:

private GameObject GenerateMesh(Vector3[] vertices, int torus_count)
{
    GameObject torus = new GameObject("Torus");

    MeshRenderer mr = torus.AddComponent<MeshRenderer>();
    mr.material = Resources.Load<Material>("YourMaterial");

    MeshFilter mf = torus.AddComponent<MeshFilter>();
    Mesh mesh = new Mesh();
    mesh.Clear(false);
    mf.mesh = mesh;
    mesh.name = "Torus_mesh";

    mesh.vertices = vertices;

    List<int> triangles = new List<int>();
    for (int row = 0; row < vertices.Length / torus_count - 1; ++row)
    {
        for (int col = 0; col < torus_count - 1; ++col)
        {
            triangles.Add(row * torus_count + col);
            triangles.Add(row * torus_count + col + 1);
            triangles.Add((row + 1) * torus_count + col);

            triangles.Add(row * torus_count + col + 1);
            triangles.Add((row + 1) * torus_count + col + 1);
            triangles.Add((row + 1) * torus_count + col);
        }
    }
    mesh.triangles = triangles.ToArray();

    List<Vector2> uv = new List<Vector2>();
    for (int i = 0; i < vertices.Length; ++i)
        uv.Add(new Vector2(0.0f, 0.0f)); // FIX this! to map your texture correctly
    mesh.uv = uv.ToArray();

    mesh.RecalculateNormals();
    mesh.Optimize();

    return torus;
}