Hey guys,
I have this code that gives me back the vertices of the triangles facing upwards, works but the moment I set the triangles too I believe is on disorder so I get a total weird mesh.
How could I solve this, in the way that I will only have the vertices and triangles in a new mesh.
List<Vector3> verticesToExport = new List<Vector3>();//vertices of the triangles facing upwards.
List<int> trianglesToExport = new List<int>();//new triangle facing upwards.
//meshToCut is the original mesh, then I just try to replace it with the new data.
int[] triangles = meshToCut.triangles;
Vector3[] vertices = meshToCut.vertices;
for (int i = 0; i < triangles.Length; i += 3)
{
Vector3 corner = vertices[triangles[i]];
Vector3 a = vertices[triangles[i + 1]] - corner;
Vector3 b = vertices[triangles[i + 2]] - corner;
float projection = Vector3.Dot(Vector3.Cross(b, a), Vector3.down);
if (projection > 0f)
{
verticesToExport.Add(vertices[triangles[i]]);
verticesToExport.Add(vertices[triangles[i + 1]]);
verticesToExport.Add(vertices[triangles[i + 2]]);
trianglesToExport.Add(triangles[i]);
trianglesToExport.Add(triangles[i + 1]);
trianglesToExport.Add(triangles[i + 2]);
}
}
meshToCut.SetVertices(verticesToExport);
meshToCut.SetTriangles(trianglesToExport, 0);//probably I'm doing wrong here!
EDIT:
The model I’m trying to separate:
This is what happens if I just take the vertices and triangulate them again:
The mesh is indexed, meaning each triangle vertex is an INDEX referring to a vertex within a vertex array.
When you are building a new mesh, you’re building a new vertex array, and therefore you can’t use old indexes, unless the array is completely identical.
Therefore you’ll to record which vertices of the original mesh you’re going to use, save only THOSE into the new mesh, record which new index corresponds to which old index, and then remap indices.
In this case the mesh will display correctly, but it will be rendered inefficiently, as no vertices will be shared between triangles. So if you want to do it properly, you should remap.