Search Unity

How to sort vertices in a mesh?

Discussion in 'Scripting' started by Mr_Admirals, Dec 27, 2018.

  1. Mr_Admirals

    Mr_Admirals

    Joined:
    May 13, 2017
    Posts:
    86
    Hello!

    I'm looking to do some mesh manipulation, but unfortunately, the meshes I'm using have their vertices strewn about the array randomly (model from Blender). To do what I want, I need them to be organized in a predictable manner. Does anyone have any thoughts or ideas on the best way to go about this?

    Thanks!

    UPDATE:

    I have a square planar mesh. The Y component can vary, but the X and Z components are locked in place only changing via the scale of the game object. For the purpose of optimizing a visual effect on the mesh and saving any theoretical artist time, I want to subdivide the mesh into separate meshes programmatically. I've figured out the general way to do this and it works well for separating the vertices into quadrants. However, when I go to construct the mesh, I get an absolute mess:



    This is because blender (and other 3d modelling programs) append newly generated vertices onto the back of the vertex list. So while I can separate the vertices based on quadrant, their ordering is still messed up - meaning that when I go to set the triangle indices, I get that mesh.

    If the vertices are ordered sequentially by row, then the triangle indices will be correct, and the mesh will actually behave like I'd expect it to. Now, in the time that I've posted this post, I've come up with an algorithm, but my concern is that it involves an excessive amount of looping.

    1. Get the number of rows in the mesh
    2. Get distance between each row
    3. Use row number and row distance to loop the entire mesh row number of times
    3a. Create separate list for each row and add vertices belonging to that row.
    4. Loop through all row lists, sorting the vertices in them.
    5. Loop through all row lists, appending the row lists together

    That should get me what I need. But I feel like there's a better way to do that as mesh cutting algorithms I've looked at don't seem to need to sort the vertices before hand - they just cut.

    So, any thoughts and ideas on how to improve the algorithm, or use a separate one altogether?

    Bonus question (this may be out of the scope of this forum): Is it possible to have separate game object meshes connected together? As in share vertices?
     
    Last edited: Dec 28, 2018
  2. exiguous

    exiguous

    Joined:
    Nov 21, 2010
    Posts:
    1,749
    Your chances are good that this gets the most unspecific question of 2018. There are still 3 days left but I can't imagine how one could beat THAT. Good luck!
     
  3. Mr_Admirals

    Mr_Admirals

    Joined:
    May 13, 2017
    Posts:
    86
    I appreciate the response. I wish it was to help me, but it at least bumped the thread up. By the way, you may not know because you seem new to helping people, but most people asking for assistance prefer to be treated kindly and not stamped down. Just a pointer.

    Anyway, when I mean predictable, I mean that each vertex is stored sequentially by row. So if I had a 3x3 mesh, it would ideally be stored like this starting at element 0:

    Vector3(-1, 0, -1),
    Vector3(0, 0, -1),
    Vector3(1, 0, -1),
    Vector3(-1, 0, 0),
    Vector3(0, 0, 0),
    Vector3(1, 0, 0),
    Vector3(-1, 0, 1),
    Vector3(0, 0, 1),
    Vector3(1, 0, 1)
     
    Armegalo, KiddUniverse and hippocoder like this.
  4. Schneider21

    Schneider21

    Joined:
    Feb 6, 2014
    Posts:
    3,512
    Hey guys. Let's keep it courteous here, okay?

    @Mr_Admirals, your question was really vague, and the Scripting sub especially tends to expect very specific questions. Perhaps consider editing your first post to include the information you supplied in your last post so as to not encourage any further sarcastic responses.

    @exiguous, I know it can get frustrating at times trying to help people. Our community is best served by being helpful and polite, though, not making jokes at newer members' expense. When you feel inclined to make a sarcastic or cutting response, the best thing to do is probably take a moment away from things for a bit until you're feeling better able to approach the question thoughtfully.

    - - -

    To your question, then, @Mr_Admirals, it actually sounds to me like a mesh might not be the ideal way to store the data you're trying to manipulate. What is it exactly you're trying to do here?
     
    Mr_Admirals and hippocoder like this.
  5. hippocoder

    hippocoder

    Digital Ape

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    Yeah give it a rest with the arguing. Unity staff may take time off but the half-submerged eye of the hippo sees ALL (but mostly helpful pointers from @Schneider21) :)

    Anyway - why do you want the verts ordered? is it for VFX spawn or other purpose?
     
  6. Mr_Admirals

    Mr_Admirals

    Joined:
    May 13, 2017
    Posts:
    86
    Thank you guys for stepping in. It probably wasn't the wisest of me to engage in the way that I did, so I definitely apologize for that and I'll be sure to ignore such a response in the future. As well as ensure my posts are more detailed in explaining my issue.

    I'll update the OP after I post this reply too but here's what's going on:

    I have a square planar mesh. The Y component can vary, but the X and Z components are locked in place only changing via the scale of the game object. For the purpose of optimizing a visual effect on the mesh and saving any theoretical artist time, I want to subdivide the mesh into separate meshes programmatically. I've figured out the general way to do this and it works well for separating the vertices into quadrants. However, when I go to construct the mesh, I get an absolute mess:

    upload_2018-12-28_11-41-9.png

    This is because blender (and other 3d modelling programs) append newly generated vertices onto the back of the vertex list. So while I can separate the vertices based on quadrant, their ordering is still messed up - meaning that when I go to set the triangle indices, I get that mesh.

    If the vertices are ordered sequentially by row, then the triangle indices will be correct, and the mesh will actually behave like I'd expect it to. Now, in the time that I've posted this post, I've come up with an algorithm, but my concern is that it involves an excessive amount of looping.

    1. Get the number of rows in the mesh
    2. Get distance between each row
    3. Use row number and row distance to loop the entire mesh row number of times
    3a. Create separate list for each row and add vertices belonging to that row.
    4. Loop through all row lists, sorting the vertices in them.
    5. Loop through all row lists, appending the row lists together

    That should get me what I need. But I feel like there's a better way to do that as mesh cutting algorithms I've looked at don't seem to need to sort the vertices before hand - they just cut.

    So, any thoughts and ideas on how to improve the algorithm, or use a separate one altogether?

    Bonus question (this may be out of the scope of this forum): Is it possible to have separate game object meshes connected together? As in share vertices?
     
  7. hippocoder

    hippocoder

    Digital Ape

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    Well the only thing that actually needs to change though, is the indices array. These are the indexes to the verts that define the triangle. Could it not be inferred in some way? If you shuffle the verts then you still need to recalculate these anyway.
     
  8. Mr_Admirals

    Mr_Admirals

    Joined:
    May 13, 2017
    Posts:
    86
    That is definitely true. At the moment though, I don't know what the pattern for the indices array would actually be. It's also difficult because the triangle indices are not packed as triangles, they're a stream of vertices, so Nth triangle depends on the N-1 triangle, and the N-1 triangle depends on the N-2 triangle.

    Anyway, I recall one of my teammates on a game design project permanently altering the location of objects programmatically in a script would you have any idea how to go about that? I'm thinking I can use that method to permanently alter the mesh, that way I don't have to worry about the overhead every time I press play.
     
  9. BenStorch

    BenStorch

    Joined:
    Aug 20, 2017
    Posts:
    7
    Hi Mr_Admirals,

    I was wondering whether you managed to solve your problem.

    I would like to do two things to the vertex order, for the perposes of vfx spawning or attraction,
    a) randomise/shuffle them
    b) sort them in the Y direction

    I could do more research into working with arrays, but if you have a quick answer to hand
    it might save me some time.

    Cheers.