I am making a mesh slicer (slice mesh once using a plane, aka point and direction vector).
So far I can sort triangles to left and right of the cut and generate meshes from that. Now I come to the point where I need to actually cut the triangles which are at the cutting plane.
I can calculate the 2 new vertices where the line cuts the triangle, but I cannot wrap my head around how to make 3 triangles out of 1 per kind of cut.
If someone can help that would be really appreciated! Links, code examples, pseudocode all are welcome.
I already tried chatGTP and check out multiple open-source projects, but just cannot wrap my head around it.
Code:
public void BreakAtPosition(Vector3 position, Vector3 normal)
{
int[] originalTris = ogMesh.triangles;
Vector2[] originalUVs = ogMesh.uv;
Vector3[] originalNormals = ogMesh.normals;
NativeList<Vector3> rightNormals = new NativeList<Vector3>(newVertices.Length, Allocator.Temp);
NativeList<Vector3> leftNormals = new NativeList<Vector3>(newVertices.Length, Allocator.Temp);
NativeList<Vector3> rightVerts = new NativeList<Vector3>(newVertices.Length, Allocator.Temp);
NativeList<Vector3> leftVerts = new NativeList<Vector3>(newVertices.Length, Allocator.Temp);
NativeList<Vector2> rightUVs = new NativeList<Vector2>(newVertices.Length, Allocator.Temp);
NativeList<Vector2> leftUVs = new NativeList<Vector2>(newVertices.Length, Allocator.Temp);
NativeList<int> rightIndices = new NativeList<int>(newVertices.Length, Allocator.Temp);
NativeList<int> leftIndices = new NativeList<int>(newVertices.Length, Allocator.Temp);
NativeList<int> rightTris = new NativeList<int>(originalTris.Length, Allocator.Temp);
NativeList<int> leftTris = new NativeList<int>(originalTris.Length, Allocator.Temp);
for (int i = 0; i < originalTris.Length; i += 3)
{
int i1 = originalTris[i];
int i2 = originalTris[i + 1];
int i3 = originalTris[i + 2];
Vector3 v1 = newVertices[originalTris[i]];
Vector3 v2 = newVertices[originalTris[i + 1]];
Vector3 v3 = newVertices[originalTris[i + 2]];
Vector2 uv1 = originalUVs[originalTris[i]];
Vector2 uv2 = originalUVs[originalTris[i + 1]];
Vector2 uv3 = originalUVs[originalTris[i + 2]];
Vector3 n1 = originalNormals[originalTris[i]];
Vector3 n2 = originalNormals[originalTris[i + 1]];
Vector3 n3 = originalNormals[originalTris[i + 2]];
float vert1Side = MeshTools.GetVertexSide(v1, position, normal);
float vert2Side = MeshTools.GetVertexSide(v2, position, normal);
float vert3Side = MeshTools.GetVertexSide(v3, position, normal);
if (vert1Side < 0 && vert2Side < 0 && vert3Side < 0) //Verts to left
{
leftVerts.Add(v3);
leftVerts.Add(v2);
leftVerts.Add(v1);
leftIndices.Add(i1);
leftIndices.Add(i2);
leftIndices.Add(i3);
leftNormals.Add(n1);
leftNormals.Add(n2);
leftNormals.Add(n3);
leftUVs.Add(uv3);
leftUVs.Add(uv2);
leftUVs.Add(uv1);
leftTris.Add(leftVerts.Length - 1);
leftTris.Add(leftVerts.Length - 2);
leftTris.Add(leftVerts.Length - 3);
}
else if (vert1Side > 0 && vert2Side > 0 && vert3Side > 0) //Verts to right
{
rightVerts.Add(v3);
rightVerts.Add(v2);
rightVerts.Add(v1);
rightIndices.Add(i1);
rightIndices.Add(i2);
rightIndices.Add(i3);
rightNormals.Add(n1);
rightNormals.Add(n2);
rightNormals.Add(n3);
rightUVs.Add(uv3);
rightUVs.Add(uv2);
rightUVs.Add(uv1);
rightTris.Add(rightVerts.Length - 1);
rightTris.Add(rightVerts.Length - 2);
rightTris.Add(rightVerts.Length - 3);
}
else //Vertes split
{
//v1, v2, v3
// Calculate intersection points
float3[] intersectionPoints = new float3[2];
int numIntersections = 0;
if (vert1Side * vert2Side < 0)
{
MeshTools.IntersectPlane(v1, v2, position, normal, out intersectionPoints[numIntersections]);
numIntersections++;
}
if (vert2Side * vert3Side < 0)
{
MeshTools.IntersectPlane(v2, v3, position, normal, out intersectionPoints[numIntersections]);
numIntersections++;
}
if (vert3Side * vert1Side < 0)
{
MeshTools.IntersectPlane(v3, v1, position, normal, out intersectionPoints[numIntersections]);
numIntersections++;
}
if (numIntersections != 2)
{
Debug.LogError("Unexpected number of intersection points: " + numIntersections);
return;
}
Vector3 v4 = intersectionPoints[0];
Vector3 v5 = intersectionPoints[1];
//TODO generate triangles
}
}