Comments and answers for "Triangulation Problems"
http://answers.unity.com/questions/1084151/triangulation-problems.html
The latest comments and answers for the question "Triangulation Problems"Comment by Bunny83 on Bunny83's comment
http://answers.unity.com/comments/1085421/view.html
Well, that simply makes no sense. In a flattened array right after the end of the first row the second row starts. When you create indices that goes beyond the limits of a row you of course step into the next row. $$anonymous$$eep in mind that you add "1" to "v" to get the right most vertex. That's why "v" must be 1 smaller than the highest index for "x". Also keep in mind when xMax is your vertex **count** the highest value is (xMax-1). That's why for loops usually run from 0 to "< xMax" and not "<=xMax"
Take a look at those examples when xMax is 5:
for(int x = 0; x < xMax; x++)
// x will be 0, 1, 2, 3, 4 --> 5 elements highest value =4
for(int x = 0; x <= xMax; x++)
// x will be 0, 1, 2, 3, 4, 5 --> 6 elements highest value 5
If you have only 5 elements the index of the last element is 4
Apart from that, as explained above when it comes to generating the quads you have again one less. 5 vertices --> 4 quads.
I posted a quite clear example which doesn't wrap around. It seems you use a different for loop(s) for whatever reason. I explained the reasoning and the required limits as much in detail as possible. We don't know how your loop(s) now looks like, so how should we fix your problem without knowing where the problem is?
If xMax is 5 and yMax 3 as you stated initially you only have 15 vertices and 16 triangles to generate. If you have trouble to follow what numbers you should get, take a piece of paper and write it down. Then just debug the numbers you get and compare them with what you need. Then you can start to figure out where you've gone wrong in your calculations.Wed, 21 Oct 2015 04:06:51 GMTBunny83Comment by zardini123 on zardini123's comment
http://answers.unity.com/comments/1085269/view.html
@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 <= xMax WITHOUT modifying my value of xMax. This will result in x going through the loop xMax + 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.Tue, 20 Oct 2015 19:49:46 GMTzardini123Comment by Bunny83 on Bunny83's comment
http://answers.unity.com/comments/1084832/view.html
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 + xMax + 1;
Since x and y is at max "(xMax-2) * (yMax-2)" the highest index is:
v + xMax + 1;
x + y * xMax + xMax + 1;
(xMax-2) + (yMax-2)* xMax + xMax + 1;
If you rearrange you get
(xMax-2 + 1) + (yMax-2 + 1) * xMax
Which simply is
xMax-1 + (yMax-1) * xMax
which is:
xMax * yMax -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 mind that in line 17 of my code example in my answer you still need to use xMax, not (xMax-1) since you want to calculate the offset in the vertices array:
int v = x + (y * xMax);Tue, 20 Oct 2015 03:56:40 GMTBunny83Comment by zardini123 on zardini123's answer
http://answers.unity.com/comments/1084812/view.html
@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 xMax and yMax. I tried modifying the for loop length to be in both axis:
for (int x = 0; x < xMax; 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?Tue, 20 Oct 2015 02:19:32 GMTzardini123Comment by Bunny83 on Bunny83's comment
http://answers.unity.com/comments/1084267/view.html
I edited my answer and added a more complete example.Mon, 19 Oct 2015 02:35:33 GMTBunny83Comment by zardini123 on zardini123's comment
http://answers.unity.com/comments/1084251/view.html
But now i am having a weird problem. The last triangle is connecting to the other side of the mesh.Mon, 19 Oct 2015 01:09:37 GMTzardini123Comment by zardini123 on zardini123's comment
http://answers.unity.com/comments/1084249/view.html
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.Mon, 19 Oct 2015 01:00:25 GMTzardini123Comment by Bunny83 on Bunny83's comment
http://answers.unity.com/comments/1084236/view.html
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[(xMax-1) * (yMax-1) * 6];
Since each inner iteration you create 6 indices and you (should) loop to (xMax-1) and (yMax-1), that's exact the length you need.Mon, 19 Oct 2015 00:14:26 GMTBunny83Comment by zardini123 on zardini123's answer
http://answers.unity.com/comments/1084229/view.html
Why do i need to subtract one from the max for the for loop? When xMax = 3, and do:
for (int x = 0; x < xMax; x++) {
It will loop from 0 to 2, which is a total of 3 iterations, which is what i want.Sun, 18 Oct 2015 23:45:08 GMTzardini123Comment by Bunny83 on Bunny83's comment
http://answers.unity.com/comments/1084226/view.html
Just in case you haven't seen the edit of my answer...Sun, 18 Oct 2015 23:35:01 GMTBunny83Comment by zardini123 on zardini123's comment
http://answers.unity.com/comments/1084217/view.html
Oh lol thanks I didn't realize that.
But what about triangulation?Sun, 18 Oct 2015 22:52:44 GMTzardini123Comment by Eno-Khaon on Eno-Khaon's comment
http://answers.unity.com/comments/1084214/view.html
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 * yMax);
but what should be there is
// Line 7
int v = x + (y * xMax);Sun, 18 Oct 2015 22:48:14 GMTEno-KhaonComment by zardini123 on zardini123's answer
http://answers.unity.com/comments/1084209/view.html
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
xMax = 5
yMax = 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 yMax, and add x:
3 + (1 * 3) = 6
It does not equal 8.
But when we multiply it by xMax:
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.Sun, 18 Oct 2015 22:33:59 GMTzardini123Answer by Bunny83
http://answers.unity.com/answers/1084203/view.html
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;
}
}Sun, 18 Oct 2015 22:26:05 GMTBunny83