Skip to content
Snippets Groups Projects
Commit 025605b2 authored by Maurice Manshanden's avatar Maurice Manshanden
Browse files

Fix incorrect winding order for splitted triangles

parent ec6eb465
No related branches found
No related tags found
No related merge requests found
......@@ -37,7 +37,7 @@ public class MeshBuilder
{
// Setup a ray along the edge
Vector3 origin = this.vertices[index1];
Vector3 direction = origin - this.vertices[index2];
Vector3 direction = this.vertices[index2] - origin;
// Calculate intersection
float t = Vector3.Dot(p - origin, n) / Vector3.Dot(direction, n);
......@@ -127,7 +127,7 @@ public class Splitter : MonoBehaviour
void SplitNode(MeshTreeNode node)
{
// Too small for splitting (TODO: make configurable?)
if (node.triangles.Length < 10)
if (node.triangles.Length < 100)
{
return;
}
......@@ -140,7 +140,7 @@ public class Splitter : MonoBehaviour
Vector3 p = this.mesh.GetCenter(node.triangles); // point on plane
float d = -n.x * p.x - n.y * p.y - n.z * p.z;
// For containing the triangles of the left and right children.
// Indices for left and right triangles
List<int> left = new List<int>();
List<int> right = new List<int>();
......@@ -174,28 +174,29 @@ public class Splitter : MonoBehaviour
// The difficult cases where one vertex is on one side, and the other
// two vertices are on the other side of the plane.
int s = -1, p1 = -1, p2 = -1, l = -1;
int s = -1, p1 = -1, p2 = -1;
bool l = false;
if (s1 && !s2 && !s3) { s = i1; p1 = i2; p2 = i3; l = 0; }
if (!s1 && s2 && !s3) { s = i2; p1 = i1; p2 = i3; l = 0; }
if (!s1 && !s2 && s3) { s = i3; p1 = i1; p2 = i2; l = 0; }
if (s1 && !s2 && !s3) { s = i1; p1 = i2; p2 = i3; l = true; }
if (!s1 && s2 && !s3) { s = i2; p1 = i3; p2 = i1; l = true; }
if (!s1 && !s2 && s3) { s = i3; p1 = i1; p2 = i2; l = true; }
if (!s1 && s2 && s3) { s = i1; p1 = i2; p2 = i3; l = 1; }
if (s1 && !s2 && s3) { s = i2; p1 = i1; p2 = i3; l = 1; }
if (s1 && s2 && !s3) { s = i3; p1 = i1; p2 = i2; l = 1; }
if (!s1 && s2 && s3) { s = i1; p1 = i2; p2 = i3; l = false; }
if (s1 && !s2 && s3) { s = i2; p1 = i3; p2 = i1; l = false; }
if (s1 && s2 && !s3) { s = i3; p1 = i1; p2 = i2; l = false; }
int x1 = mesh.SplitEdge(p1, s, p, n);
int x2 = mesh.SplitEdge(p2, s, p, n);
if (l == 0)
if (l)
{
left.AddRange(new[] { x1, x2, s });
right.AddRange(new[] { x1, p1, p2, p2, x2, x1 });
}
else
{
right.AddRange(new[] { x1, x2, s });
left.AddRange(new[] { x1, p1, p2, p2, x2, x1 });
right.AddRange(new[] { x1, x2, s });
}
}
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment