# Generating mesh along path

I'm currently trying to generate a path along an array of points (Vector3) This is the result I'm currently getting:

• The dark gray points are the given path
• The red points are the generated vertices
• And the triangles are obviously the mesh I failed to generate correctly And from the bottom it looks like this, which is odd, because I'd expect all the missing triangles to be at the bottom. But some of them just don't exist: here's the code i wrote that generates the path mesh

//Removed code since there's no need in showing something that doesn't work


And here's a quick example of what it looks like when I drag a path: SOLUTION FOUND

As the accepted answer states, I was skipping triangles in my triangle for loop. Here is my new working code:

public static Mesh extrudeAlongPath(Vector3[] points, float width){
if (points.Length < 2)
return null;
Mesh m = new Mesh();
List<Vector3> verts = new List<Vector3>();
List<Vector3> norms = new List<Vector3>();

for (int i = 0; i < points.Length; i++) {
if(i != points.Length-1){
Vector3 perpendicularDirection = new Vector3(-( points[i+1].z-points[i].z ), points[i].y, points[i+1].x -points[i].x).normalized;
verts.Add (points[i]+perpendicularDirection*width);
norms.Add (Vector3.up);
verts.Add (points[i]+perpendicularDirection*-width);
norms.Add (Vector3.up);
}else{
Vector3 perpendicularDirection = new Vector3(-( points[i].z-points[i-1].z ), points[i].y, points[i].x -points[i-1].x).normalized;
verts.Add (points[i]+perpendicularDirection*-width);
norms.Add (Vector3.up);
verts.Add (points[i]+perpendicularDirection*width);
norms.Add (Vector3.up);
}
}
m.vertices = verts.ToArray ();
m.normals = norms.ToArray ();

List<int> tris = new List<int> ();
//Changed i+=3 to i++
for(int i = 0; i < m.vertices.Length-3; i++){
if(i%2 == 0){
tris.Add(i+2);
tris.Add(i+1);
tris.Add(i);
}else{
tris.Add(i);
tris.Add(i+1);
tris.Add(i+2);
}
}
m.triangles = tris.ToArray ();

m.name = "pathMesh";
m.RecalculateNormals ();
m.RecalculateBounds ();
m.Optimize ();
return m;
}

• Does anything chance when you remove m.RecalculateNormals (); or m.RecalculateBounds ();? – Static Jun 27 '15 at 8:42
• unfortunately not, the exact same result – Daniel Holst Jun 27 '15 at 8:43

## 1 Answer

Try to change the tris for loop to ++ instead of +=3:

for(int i = 0; i < m.vertices.Length-3; i++){
tris.Add(i+2);
tris.Add(i+1);
tris.Add(i);
}


Each 2 neighboring triangles have 2 shared vertices, so += 3 would skip 2 triangles each time.

• That definitely did something ! i.imgur.com/C5reRc6.png – Daniel Holst Jun 27 '15 at 8:44
• but how would i get those odd triangles to face upwards? – Daniel Holst Jun 27 '15 at 8:45
• I have to go to work now, but i'll check back in my break in about 3 hours or so :) thanks for helping me! – Daniel Holst Jun 27 '15 at 8:51
• I figured it out, added it to question – Daniel Holst Jun 27 '15 at 8:54