Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
1
Question by zardini123 · Oct 18, 2015 at 08:03 PM · meshmeshestriangulation

Triangulation Problems

I have a collection of points. The vertex order looks like this:

 0     1     2     3     4
 5     6     7     8     9
 10   11    12    13     14

The points are lay out like a plane, and i want the final result to look like a plane. I tried using the Triangulation script on the Unify Community Wiki, but it did not work, due to having points "inside" the outer points.

Currently, my code for triangulation is this:

 Vector3[] vertices = new Vector3[xMax * yMax];
 int[] triangles = new int[vertices.Length * 3];
 
 for (int x = 0; x < xMax; x++) {
     for (int y = 0; y < yMax; y++) {
 
         int v = x + (y * xMax);
 
         // Assign the vertex position (this isnt the problem)
         vertices[v] = vertexPos;
 
         // Assign triangles (the thing that doesn't work)
         // The if then statements are to make sure the indicies dont go out of bounds
         if (t < triangles.Length - 5) {
             if (v < vertices.Length - xMax) {
                 triangles[t++] = v;
                 triangles[t++] = v + 1;
                 triangles[t++] = v + xMax;
                 
                 triangles[t++] = v + 1;
                 triangles[t++] = v + xMax;
                 triangles[t++] = v + xMax + 1;
             }
         }
     }
 }

All the things I've tried have not resulted in a nice smooth flat plane like i would like it to be.

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 · Oct 18, 2015 at 10:26 PM

You have to multiply y by xMax, not yMax

 int v = x + (y * xMax);

Further more when creating the triangle indices you should reduce the limits of your for loops by 1

 for (int x = 0; x < xMax-1; x++) {
     for (int y = 0; y < yMax-1; y++) {
 

I just checked your 2 triangles and your second triangle is facing the wrong way. It need to be clockwise. The first one is clockwise, the second one is counter clockwise. So the second one is facing backwards.

The size of the triangles array is also too small. There are two rows with each 4 quads:

  0    1    2    3    4
    Q1   Q2   Q3   Q4
  5    6    7    8    9
    Q5   Q6   Q7   Q8
 10   11   12   13   14

That means since each quad requires two triangles and each triangle 3 indices you need

 int[] triangles = new int[ (xMax-1) * (yMax-1) * 2 * 3 ];

So finally it should look something like:

 Vector3[] vertices = new Vector3[xMax * yMax];
 int[] triangles = new int[ (xMax-1) * (yMax-1) * 6 ];
 // vertices
 for (int y = 0; y < yMax; y++) 
 {
     for (int x = 0; x < xMax; x++) 
     {
         int v = x + (y * xMax);
         vertices[v] = new Vector3(x,y,0);
     }
 }
 // triangles
 int t = 0;
 for (int y = 0; y < yMax-1; y++) 
 {
     for (int x = 0; x < xMax-1; x++) 
     {
         int v = x + (y * xMax);
         triangles[t++] = v;
         triangles[t++] = v + 1;
         triangles[t++] = v + xMax;
         
         triangles[t++] = v + 1;
         triangles[t++] = v + xMax + 1;
         triangles[t++] = v + xMax;
     }
 }

Comment
Add comment · Show 13 · 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 zardini123 · Oct 18, 2015 at 10:33 PM 0
Share

No, but thats not the problem. Variable "v" is the final vertex from our 2 variables x and y. Say we want vertex "8":

  0     1     2     3     4
  5     6     7     8     9
  10   11    12    13     14
  x$$anonymous$$ax = 5
  y$$anonymous$$ax = 3


Remember this:

 x + (y * max);


X would be 3, and Y would be 1 (remember starting value is 0). So if we multiplied Y by y$$anonymous$$ax, and add x:

 3 + (1 * 3) = 6

It does not equal 8.

But when we multiply it by x$$anonymous$$ax:

 3+(1 * 5) = 8

We do get 8.

The problem I am having is about the triangulation algorithm that would fit with my type of vertex alignment.

avatar image Eno-Khaon zardini123 · Oct 18, 2015 at 10:48 PM 1
Share

I think you may want to re-read @Bunny83's reponse. What was said and how you responded are both saying the same thing, pointing at what you don't seem to realize you did wrong.

Just to restate it in full, you currently have

 // Line 7
 int v = x + (y * y$$anonymous$$ax);

but what should be there is

 // Line 7
 int v = x + (y * x$$anonymous$$ax);
avatar image zardini123 Eno-Khaon · Oct 18, 2015 at 10:52 PM 0
Share

Oh lol thanks I didn't realize that. But what about triangulation?

avatar image Bunny83 zardini123 · Oct 18, 2015 at 11:35 PM 0
Share

Just in case you haven't seen the edit of my answer...

avatar image zardini123 · Oct 18, 2015 at 11:45 PM 0
Share

Why do i need to subtract one from the max for the for loop? When x$$anonymous$$ax = 3, and do: for (int x = 0; x < x$$anonymous$$ax; x++) {

It will loop from 0 to 2, which is a total of 3 iterations, which is what i want.

avatar image Bunny83 zardini123 · Oct 19, 2015 at 12:14 AM 0
Share

No, it's not what you want. You have 3 vertices along the y axis but only2 quads in between. Likewise you have 5 vertices along x but only 4 quads in between.

I was talking about the triangle generation, not about the vertex generation. Those are two seperate things and your vertex generation isn't really in the code you posted. You should seperate the vertex generation from the triangle index generation.

If you have a vertex field of 5 x 3 vertices you have a quad field of 4 x 2 quads

  0    1    2    3    4
    Q1   Q2   Q3   Q4
  5    6    7    8    9
    Q5   Q6   Q7   Q8
 10   11   12   13   14

edit

Actually your triangle array is too small btw ^^. You need:

 int[] triangles = new int[(x$$anonymous$$ax-1) * (y$$anonymous$$ax-1) * 6];

Since each inner iteration you create 6 indices and you (should) loop to (x$$anonymous$$ax-1) and (y$$anonymous$$ax-1), that's exact the length you need.

avatar image zardini123 Bunny83 · Oct 19, 2015 at 01:00 AM 0
Share

Wow okay so it worked. So just throw all the final statements like correct size triangles array and for loop stuff into a answer and i can make it the best answer.

Show more comments
Show more comments
avatar image zardini123 · Oct 20, 2015 at 02:19 AM 0
Share

@Bunny83 I am having a problem that for some reason I am having trouble fixing.

The amount of triangles in the x and y axis is 1 less than the x$$anonymous$$ax and y$$anonymous$$ax. I tried modifying the for loop length to be in both axis:

 for (int x = 0; x < x$$anonymous$$ax; x++) {

I adjusted the length of the arrays to match the for loop, but the problem is, is that that this method only works on 1 axis, which is the x axis.

The result in the y axis was that the last triangles on that axis was connecting to the other side of the mesh. How would I go about fixing this?

avatar image Bunny83 zardini123 · Oct 20, 2015 at 03:56 AM 0
Share

That shouldn't be the case. You either have setup your vertices wrong or your triangles.

The highest vertex index is this one:

 triangles[t++] = v + x$$anonymous$$ax + 1;

Since x and y is at max "(x$$anonymous$$ax-2) * (y$$anonymous$$ax-2)" the highest index is:

             v              +  x$$anonymous$$ax + 1;
 x        +    y    * x$$anonymous$$ax  +  x$$anonymous$$ax + 1;
 (x$$anonymous$$ax-2) + (y$$anonymous$$ax-2)* x$$anonymous$$ax  +  x$$anonymous$$ax + 1;

If you rearrange you get

 (x$$anonymous$$ax-2 + 1) + (y$$anonymous$$ax-2 + 1) * x$$anonymous$$ax

Which simply is

 x$$anonymous$$ax-1 + (y$$anonymous$$ax-1) * x$$anonymous$$ax

which is:

 x$$anonymous$$ax * y$$anonymous$$ax -1

and that is the highest index in the vertices array. The indices never cross the border so they won't wrap around unless you did not use the right limits.

$$anonymous$$eep in $$anonymous$$d that in line 17 of my code example in my answer you still need to use x$$anonymous$$ax, not (x$$anonymous$$ax-1) since you want to calculate the offset in the vertices array:

 int v = x + (y * x$$anonymous$$ax);
avatar image zardini123 Bunny83 · Oct 20, 2015 at 07:49 PM 0
Share

@Bunny83 I don't think you understand my problem.

What i am trying to ask is that I want my x and y values to go from x = 0 to x <= x$$anonymous$$ax WITHOUT modifying my value of x$$anonymous$$ax. This will result in x going through the loop x$$anonymous$$ax + 1 times, which is what I want.

When I do create a for loop that follows my current need, it results in the y axis having sets of triangles wrapping back to the other side.

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

32 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

Related Questions

Building a prism (adding thickness) from non-convex mesh 0 Answers

more than one mesh 1 Answer

Creating a convex MESH (not collider) 2 Answers

How can I remove redundant vertices in custom mesh? 0 Answers

Triangulating mesh from collider points 1 Answer


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