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;
}
m.RecalculateNormals ();
orm.RecalculateBounds ();
? \$\endgroup\$