# 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[i], vertices[i + torus_count]);
continue;
}
Gizmos.DrawLine(vertices[i], 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[i]);
}
}
}
```

**Answer** by mikelo
·
Nov 13, 2015 at 01:06 PM

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;
}
```

Hi mikelo,

This is almost exactly what I'm looking for - great work!

However, for whatever reason it only seems to be picking out every other triangle, see below. I'll try and figure out what's going on with it and if I can then I'll be very satisfied with this answer!

I think there is an error in lines 25 and 27. Try this:

```
triangles.Add(row * torus_count + col + 1);
triangles.Add((row + 1) * torus_count + col + 1);
triangles.Add((row + 1) * torus_count + col);
```

### Your answer

### Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.

### Follow this Question

### Related Questions

How can I remove shooted mesh of object? 0 Answers

Procedural Mesh Problems 0 Answers

How can I identify the location of mesh triangles at run time? (Procedural Generation) 0 Answers

Triangles become black when flipping triangles 0 Answers

Some triangles are black on a mesh generated by script 1 Answer