- Home /
Strangest Bug - Dodgy Triangle Pattern
I have the most bizarre bug. I'm making procedurally generated terrain. Each terrain section involves a grid of vertices that need to be connected to form triangles. In the code snippet are two algorithms for generating the indexes to form two different patterns of triangles. The first one (commented out) works just perfectly. With the second one, everything seems perfect except that it decreases the frame rate. After extensive testing, I am 99.9999% sure that the second algorithm is producing the correct triangle pattern with absolutely no flaws. I can only assume at this point that Unity simply does not like this second pattern. But of course that would be nonsensical. Both patterns have the same number of triangles, all of the same size.
Even more bizarre are some of the situations where it suddenly performs perfectly. If I leave out any one of the triangles from each repeating pattern within the grid, it performs perfectly. And it's not because there are less triangles to render. Because if I keep the full pattern but have 10 times less vertices, and thus triangles, it will still perform poorly.
It just doesn't seem to like this pattern...
Here is the code:
indicesFullDetail = new int[numIndicesFullDetail];
int index = 0;
// first pattern, works perfectly:
/*for (int row = 0; row < verticesSide - 1; row++) {
for (int col = 0; col < verticesSide - 1; col++) {
// For each grid cell output two triangles
indicesFullDetail[index++] = (row * verticesSide) + col;
indicesFullDetail[index++] = (row * verticesSide) + col + 1;
indicesFullDetail[index++] = ((row + 1) * verticesSide) + col;
indicesFullDetail[index++] = (row * verticesSide) + col + 1;
indicesFullDetail[index++] = ((row + 1) * verticesSide) + col + 1;
indicesFullDetail[index++] = ((row + 1) * verticesSide) + col;
}
}*/
// second pattern, performs poorly:
for(int row = 1; row < verticesSide; row += 2) {
for(int col = 1; col < verticesSide; col += 2) {
// left triangle(s)
indicesFullDetail[index++] = (row * verticesSide) + col;
indicesFullDetail[index++] = ((row + 1) * verticesSide) + col - 1;
indicesFullDetail[index++] = (row * verticesSide) + col - 1;
indicesFullDetail[index++] = (row * verticesSide) + col;
indicesFullDetail[index++] = (row * verticesSide) + col - 1;
indicesFullDetail[index++] = ((row - 1) * verticesSide) + col - 1;
// top triangle(s)
indicesFullDetail[index++] = (row * verticesSide) + col;
indicesFullDetail[index++] = ((row - 1) * verticesSide) + col - 1;
indicesFullDetail[index++] = ((row - 1) * verticesSide) + col;
indicesFullDetail[index++] = (row * verticesSide) + col;
indicesFullDetail[index++] = ((row - 1) * verticesSide) + col;
indicesFullDetail[index++] = ((row - 1) * verticesSide) + col + 1;
// right triangle(s)
indicesFullDetail[index++] = (row * verticesSide) + col;
indicesFullDetail[index++] = ((row - 1) * verticesSide) + col + 1;
indicesFullDetail[index++] = (row * verticesSide) + col + 1;
indicesFullDetail[index++] = (row * verticesSide) + col;
indicesFullDetail[index++] = (row * verticesSide) + col + 1;
indicesFullDetail[index++] = ((row + 1) * verticesSide) + col + 1;
// bottom triangle(s)
indicesFullDetail[index++] = (row * verticesSide) + col;
indicesFullDetail[index++] = ((row + 1) * verticesSide) + col + 1;
indicesFullDetail[index++] = ((row + 1) * verticesSide) + col;
indicesFullDetail[index++] = (row * verticesSide) + col;
indicesFullDetail[index++] = ((row + 1) * verticesSide) + col;
indicesFullDetail[index++] = ((row + 1) * verticesSide) + col - 1;
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613103114im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Mesh Colliders, best practice for large meshes? 1 Answer
Could someone help me with instantiate function? It's not working. Weird. 2 Answers
Scene loading time after upgrade from 5.5 to 5.6 4 Answers
Reflection probe smoothness on full reflection in build 0 Answers
Using ScreenCapture.CaptureScreenshot freezes Unity Editor (2019.4.26f1) 0 Answers