5
\$\begingroup\$

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

enter image description here

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:

enter image description here 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:

enter image description here

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;
}
\$\endgroup\$
  • \$\begingroup\$ Does anything chance when you remove m.RecalculateNormals (); or m.RecalculateBounds ();? \$\endgroup\$ – Static Jun 27 '15 at 8:42
  • \$\begingroup\$ unfortunately not, the exact same result \$\endgroup\$ – Daniel Holst Jun 27 '15 at 8:43
1
\$\begingroup\$

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.

\$\endgroup\$
  • \$\begingroup\$ That definitely did something ! i.imgur.com/C5reRc6.png \$\endgroup\$ – Daniel Holst Jun 27 '15 at 8:44
  • \$\begingroup\$ but how would i get those odd triangles to face upwards? \$\endgroup\$ – Daniel Holst Jun 27 '15 at 8:45
  • \$\begingroup\$ 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! \$\endgroup\$ – Daniel Holst Jun 27 '15 at 8:51
  • \$\begingroup\$ I figured it out, added it to question \$\endgroup\$ – Daniel Holst Jun 27 '15 at 8:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.