Skip to content

fix issue where probuilder with quad topology instead of triangle would trigger out of bounds exception when being exported #619

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Aug 18, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
- [PBLD-242] Fixed a bug where edges were being incorrectly selected when one or both vertices were behind the camera's near plane, causing flipped lines and inconsistent selection behavior.
- [PBLD-226] Fixed a bug where ProBuilder faces could not selected when obscured by another GameObject
- [PBLD-164] Fixed a bug with UV autostitching where the position offset would not take into account the face rotation center offset.
- [PBLD-251] Fixed a bug which would cause out of bounds exceptions when exporting meshes with quad topology
- [PBLD-253] Removes call to internal API that is being removed.

## [6.0.6] - 2025-07-01
Expand Down
36 changes: 23 additions & 13 deletions Runtime/Core/ProBuilderMeshFunction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -409,27 +409,37 @@
// remove any indices that contain degenerate triangles
int numBadIndices = 0;
int[] indexes = submeshes[i].m_Indexes;
for (var tri = 0; tri < indexes.Length; tri += 3)

if (submeshes[i].m_Topology == MeshTopology.Triangles && indexes.Length % 3 == 0)
{
Vector3 ab = positions[indexes[tri + 1]] - positions[indexes[tri]];
Vector3 ac = positions[indexes[tri + 2]] - positions[indexes[tri]];
if (Vector3.Cross(ab, ac).sqrMagnitude < Mathf.Epsilon)
{
numBadIndices += 3;
}
else
for (var tri = 0; tri < indexes.Length; tri += 3)
{
submeshes[i].m_Indexes[tri - numBadIndices] = submeshes[i].m_Indexes[tri];
submeshes[i].m_Indexes[tri - numBadIndices + 1] = submeshes[i].m_Indexes[tri + 1];
submeshes[i].m_Indexes[tri - numBadIndices + 2] = submeshes[i].m_Indexes[tri + 2];
if (tri + 2 >= indexes.Length ||
indexes[tri] >= positions.Count ||
indexes[tri + 1] >= positions.Count ||
indexes[tri + 2] >= positions.Count)
continue;

Check warning on line 421 in Runtime/Core/ProBuilderMeshFunction.cs

View check run for this annotation

Codecov github.com / codecov/patch

Runtime/Core/ProBuilderMeshFunction.cs#L421

Added line #L421 was not covered by tests

Vector3 ab = positions[indexes[tri + 1]] - positions[indexes[tri]];
Vector3 ac = positions[indexes[tri + 2]] - positions[indexes[tri]];
if (Vector3.Cross(ab, ac).sqrMagnitude < Mathf.Epsilon)
{
numBadIndices += 3;
}
else
{
indexes[tri - numBadIndices] = indexes[tri];
indexes[tri - numBadIndices + 1] = indexes[tri + 1];
indexes[tri - numBadIndices + 2] = indexes[tri + 2];
}
}
}

int[] fixedIndices;
if (numBadIndices > 0)
{
fixedIndices = new int[submeshes[i].m_Indexes.Length - numBadIndices];
submeshes[i].m_Indexes.AsSpan(0, fixedIndices.Length).CopyTo(fixedIndices);
fixedIndices = new int[indexes.Length - numBadIndices];
Array.Copy(indexes, 0, fixedIndices, 0, fixedIndices.Length);
}
else
{
Expand Down
67 changes: 41 additions & 26 deletions Tests/Editor/Geometry/ProbuilderMeshDegenerateTriangleTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,42 +7,57 @@

public class ProbuilderMeshDegenerateTriangleTest
{
private ProBuilderMesh m_Mesh;

[SetUp]
public void SetUp()
[Test]
public void TestNormalsWithDegenerateTriangles()
{
// Create a Probuilder cube
m_Mesh = ShapeFactory.Instantiate(typeof(Cube));
}
var mesh = ShapeFactory.Instantiate(typeof(Cube));
try
{
// Collapse two of the shared vertices together.
var positions = new List<Vector3>(mesh.positions);
Vector3 svPosition = positions[mesh.sharedVertices[0][0]];
for (int v = 0; v < mesh.sharedVertices[1].Count; ++v)
{
positions[mesh.sharedVertices[1][v]] = svPosition;
}

[TearDown]
public void TearDown()
{
// Clean up the created GameObject
GameObject.DestroyImmediate(m_Mesh.gameObject);
mesh.positions = positions.ToList();
mesh.Rebuild();

// Check all the normals in use by faces to ensure none have invalid values.
foreach (int index in mesh.mesh.triangles)
{
var normal = mesh.normals[index];
Assert.IsFalse(float.IsNaN(normal.x) || float.IsNaN(normal.y) || float.IsNaN(normal.z), "Normals should not contain NaN values.");
Assert.IsFalse(float.IsInfinity(normal.x) || float.IsInfinity(normal.y) || float.IsInfinity(normal.z), "Normals should not contain Infinite values.");
Assert.IsFalse(normal == Vector3.zero, "Normals should not be zero vectors.");
}
}
finally
{
// Clean up the created GameObject
GameObject.DestroyImmediate(mesh.gameObject);
}
}

[Test]
public void TestNormalsWithDegenerateTriangles()
[Description("PBLD-251 : IndexOutOfRangeException appears in the Console when exporting certain ProBuilder meshes")]
public void TestOriginalDoorBugScenario()
{
// Collapse two of the shared vertices together.
var positions = new List<Vector3>(m_Mesh.positions);
Vector3 svPosition = positions[m_Mesh.sharedVertices[0][0]];
for (int v = 0; v < m_Mesh.sharedVertices[1].Count; ++v)
// Recreate the specific Door shape bug scenario
var door = ShapeFactory.Instantiate(typeof(Door));

try
{
positions[m_Mesh.sharedVertices[1][v]] = svPosition;
}
m_Mesh.positions = positions.ToList();
m_Mesh.Rebuild();
Assert.DoesNotThrow(() => door.ToMesh(MeshTopology.Quads));

// Check all the normals in use by faces to ensure none have invalid values.
foreach (int index in m_Mesh.mesh.triangles)
Assert.IsNotNull(door.mesh);
Assert.Greater(door.mesh.vertexCount, 0);
}
finally
{
var normal = m_Mesh.normals[index];
Assert.IsFalse(float.IsNaN(normal.x) || float.IsNaN(normal.y) || float.IsNaN(normal.z), "Normals should not contain NaN values.");
Assert.IsFalse(float.IsInfinity(normal.x) || float.IsInfinity(normal.y) || float.IsInfinity(normal.z), "Normals should not contain Infinite values.");
Assert.IsFalse(normal == Vector3.zero, "Normals should not be zero vectors.");
GameObject.DestroyImmediate(door.gameObject);
}
}
}