[Bug] Index error in Voxelize.RemoveDegenerateSegments

I was getting the ‘Degenerate triangles may have been created’ error during contour triangulation and traced it back to a problem with the RemoveDegenerateSegments method in VoxelContour.cs removing the wrong parts.

This line:
simplified.RemoveRange(i, 4);

should be:
simplified.RemoveRange(i*4, 4);

This was causing some very odd vertices when it trimmed the tail of one vertex and the start of the next, but was masked in most cases by the triangulate error catching.

I think it should also have after removal:
i–;
Or equivalent subloop to re-check the newly shifted pair, since it’d otherwise go to the next index at the same time as shifting the indexed list back by removing and miss out a pair entirely.
(only an issue if 3 or more identical xz vertices in a row are passed in)
The original removeDegenerateSegments from the recast C++ source doesn’t seem to account for this part, so I don’t know if 3 identical is ruled out before this or if it’s just overlooked there too.

Hi

Wow. This is indeed a bug, and a really old one too. Thank you for reporting this! :slight_smile:
I will include the fix in the next update.

I agree that it should have i-- in that loop as well.