Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Does the order of mesh vertices matter?

Discussion in 'Scripting' started by R-Lindsay, Jan 19, 2015.

  1. R-Lindsay

    R-Lindsay

    Joined:
    Aug 9, 2014
    Posts:
    287
    I am procedurally generating meshes. Suppose I was generating a triangle stripe, but instead of defining them in order, I had them jumbled, say I define triangle 1, 3, 5, 7, 9, then 2, 4, 6, 8, 10 (my problem isn't really about triangle stripes but the idea is the same).

    It still seems to render fine. Is there a performance hit? Is it worth worrying about?
     
  2. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,398
    The order of the vertex array is irrelevant; the order of the triangle array determines how triangles are rendered. For example, if [1, 2, 3] refers to vertices in a clockwise order, then [1, 3, 2] is counter-clockwise and the surface normal is reversed.

    --Eric
     
  3. R-Lindsay

    R-Lindsay

    Joined:
    Aug 9, 2014
    Posts:
    287
    Hi Eric,

    thanks for replying. I need to be more specific.

    I'm wondering if the order of the actual triangles (as defined in the triangle array) matters. Can I haphazzardly construct triangles that result in a closed mesh, or should I (say, for rendering performance reasons) construct triangles such that the edge of the next triangle is connected to an edge of a previous triangle. Assume that every triangle that is created is correctly defined with clockwise winding.
     
  4. angrypenguin

    angrypenguin

    Joined:
    Dec 29, 2011
    Posts:
    15,500
    I don't know how much it matters these days but, in the past, optimizing "mesh topography" was considered important. Calculations on vertices are/were cached, and if a vertex was used multiple times it was best to re-use it before it was removed from the cache, so the order in which verts were processed was important. Whether that's still a consideration these days I don't know.
     
    R-Lindsay likes this.
  5. R-Lindsay

    R-Lindsay

    Joined:
    Aug 9, 2014
    Posts:
    287
    Cheers.

    Vertices are generally reused immediately, but not always. For now I'll try to keep in mind caching but I won't go out of my way complicating my algorithm to guarantee it.
     
    angrypenguin likes this.
  6. angrypenguin

    angrypenguin

    Joined:
    Dec 29, 2011
    Posts:
    15,500
    It's worth researching more to see if it's still an issue. I believe the above was true back when lighting was done per-vertex. Now that it's done per-pixel it might not even be a thing.
     
    R-Lindsay likes this.
  7. Todd-Wasson

    Todd-Wasson

    Joined:
    Aug 7, 2014
    Posts:
    1,077
    My boats are procedurally generated:



    R. Lindsay, I don't think it really matters unless you're maybe using OpenGL and telling it to render them as actual triangle strips. With my boats, vertices are generated in vertical cross sections starting from the back of the boat and moving toward to the tip. I'm using Unity's rendering there (not OpenGL except for force vectors and so forth, which is irrelavent to your point). I didn't pay any attention to trying to order the triangles in any particular way. I did need to make sure the individual triangle vertices were ordered to get the normal direction right.

    So in other words, I generate the triangles as strips, but they're just stored in a Unity mesh class in whatever way was convenient. Each boat is multiple meshes pieced together this way, so they aren't true triangle strips I think. Basically, I wouldn't worry about it too much and just build a big list of triangles in whatever way is easy. :)
     
    angrypenguin and R-Lindsay like this.
  8. angrypenguin

    angrypenguin

    Joined:
    Dec 29, 2011
    Posts:
    15,500
    Correct, but there's nothing bad about that. My understanding is that triangle strips were a way to reduce CPU->GPU bandwidth usage by around 50% by re-using common vertices between adjacent triangles in the strip, back when the CPU submitted vertex draw data for every frame. These days the whole mesh is sent prior to drawing, so it's not an issue any more.
     
    R-Lindsay and Todd-Wasson like this.
  9. Todd-Wasson

    Todd-Wasson

    Joined:
    Aug 7, 2014
    Posts:
    1,077
    Good to know. Thanks, that makes sense.