Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Feref2 · Sep 19, 2021 at 12:45 AM · vector2procedural meshtrianglessorting order

Is there a way to sort a list/array by vectors (instead of individual values)?

I have a plane with many vertices and two empty diamond holes: alt text

Each intersection of the red lines and the 4 corners is a vertex, and that´s basically how it is supposed to look after procedurally ordering the triangles. I already have the vertex positions; but am struggling to find the triangle order, since not every row has the same amount of columns and viceversa. I HAVE AN IDEA, however, but it implies already having all of these vertices in order. I need them to be from left to right, from bottom to top.

The Sort() method ensures that they go in order, but as far as I can tell, it´s only with numbers (1, 2, 3, 4, 5...). I need the same, but with vectors. For example, (0, 1), (1, 1), (2, 1), (3, 1), (then it becomes a different row): (1, 2), (4, 2), (5, 2), (10, 2). where the number of rows and colums isn´t consistent, but prioritizes smaller z values and then, with rows with same z values, prioritizes smaller x values. Does something like this exist?

captura-de-pantalla-92.png (159.0 kB)
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Bunny83 · Sep 20, 2021 at 10:57 PM

I'm not sure I understand your conditions of sorting. Keep in mind that a sorting condition has to make sense for any two vertices you compare. So how would you actually decide which point should come before which? Of course you can simply sort them based on the y position first and if they happens to have the same y coordinate, sort them on the x coordinate. This can be done like this:

 list.Sort((a, b) => { if (a.y == b.y) return a.x.CompareTo(b.x); return a.y.CompareTo(b.y); });

of more readable

 list.Sort((a, b) => {
     if (a.y == b.y) // if same y, sort on x
         return a.x.CompareTo(b.x); 
     return a.y.CompareTo(b.y); // else sort on y
 });

So this will essentially give you all x coordinates sorted from lowest to largest if they are at the same y coordinate. Though the rows are essentially sorted bottom to top on the y axis.


Do you always have two diamond shaped holes? If so it would probably be cheaper to start with those edges and simply construct triangles connecting with the 4 corners. For example

DiamondHoles

The trick is to keep track of "unpaired" edges. Note that the edges need to be "facing" the right way. Unity's mesh triangles follow a clockwise winding, so a hole would have a counter clockwise winding. The outer edges Would be clockwise, but need to be treated special since they don't have any matching edge. Though you can simply create the first 4 triangles by picking an outer edge and create a triangle with the vertex that is closest. This will give you the 4 light red triangles. Of course whenever you create a triangle with an existing edge and a vertex you get two new edges. The edge you just used is now successfully paired and can be removed from the open edge list. So all you have to do is for each edge in the open list, just find the closest vertex in the "normal" direction of that edge. The normal of an edge would be the edge direction rotated clockwise 90°. So when you have the direction of the edge, you can get the normal by doing

 Vector2 dir = end - start;
 Vector2 normal = new Vector2(dir.y, -dir.x);

With the normal you can simply measure the projection of all vertices onto your edge normal. Of course those "behind" the edge would have negative values which we will ignore. From the vertices on the positive side you simply grab the one with the smallest distance to the center of the edge. With that vertex and our edge we simply construct another triangle. Of course again the new triangle will have two new edges (of course oriented the right way).


Note before you push new edges into the open edge list, you should search the list if the counter part edge is already in the list. If it is, instead of adding the edge we have to remove the counter part since the edge is now already paired. This would happen for example when constructing the top left green triangle from the top left diamond edge. Since the left edge meet with the red triangle on the left, those edges are paired and done. Constructing the bottom left green triangle would actually remove two open edges from the two adjacent red triangles. Continuing like this should fill the whole rectangle, leaving the holes free.


This should be just a matter of proper bookkeeping of the edges. Proper meshing, especially with holes, is not a simple topic. My solution should produce 14 triangles while your approach (which I think I don't really understand) would result in 34 triangles (17 quads with 2 triangles each).


I should note that my approach is just a theoretical solution which I haven't tried yet. It's just based on what you can do easily. It should in theory work even with more holes as long as the hole shapes are "convex".


diamondmeshing.png (9.8 kB)
Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Feref2 · Sep 21, 2021 at 12:53 AM 0
Share

Thanks a lot for your reply. No, it doesn´t either have a consistent amount of holes nor shapes. The diamonds were just a n example, they could be any shape in any amount, which is impredictible. Here is another image to clarify: alt text

First of all, it needs to be clear that the edges need to be created this way, which is generally creating horizontal lines and some vertical ones to ensure that they are all quads/triangles at the bottom and the top (although there might be more of them). Anyway, that´s how is supposed to look. As you can see, the number of elemnts per row or colum isn´t consistent either. My plan was to creat another array that would solve this telling me how much is left until the same position one row above (basically including a lot of invisible, imaginary vertices). For example, you can see that the distance between 4 and 11 is 7, but 6 is only 3 far away from 9. I could use this to get an ecuation or something that would give me 7 and 9 in those respective parts. However, I have to admit that am not entirely sure if this method will work.

captura-de-pantalla-93.png (174.9 kB)
avatar image Bunny83 Feref2 · Sep 21, 2021 at 01:28 AM 0
Share

Well, if you want to stick with this system, just use the sorting algorithm I posted in the beginning. It will sort your vertices the way you have shown.

avatar image Feref2 Bunny83 · Sep 21, 2021 at 07:34 PM 0
Share

Sorry, but am struggling to understand that code. Could you post some documentation or explain' (Sorry if am stupid).

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

127 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Determining where a mesh triangle faces 1 Answer

Odd lighting on procedurally generated mesh 2 Answers

Triangulating a 2D polygon with only hull vertices. 1 Answer

2D Vectors along 3D Triangle 0 Answers

Triangulation for my procedural 2D Polygon Collider is different than that of my Mesh, does anyone know why? 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges