- Home /
This question was
closed Jul 03, 2017 at 01:19 PM by
DanaScully for the following reason:
Other
Question by
DanaScully · Jun 19, 2017 at 12:14 PM ·
meshtrianglesmesh vertices
How to recalculate mesh triangles
I am trying to get only visible vertices from a cloth mesh. In order to do so I am using Cam.WorldToViewportPoint to see weather the vertex is in the viewport or not and then I save it in a new mesh and then extract it as an .ObJ files. However I don't know how to calculate new triangles for my new mesh. Can someone please point me to the right direction?
void Update()
{
cloth.gameObject.AddComponent<MeshCollider>();
clothMesh = cloth.GetComponent<MeshCollider>().sharedMesh = cloth.clothMesh;
Vector3[] vertices = clothMesh.vertices;
int[] triangles = clothMesh.triangles;
Destroy(cloth.GetComponent<MeshCollider>());
List<Vector3> visibleVertices = new List<Vector3>();
List<int> newTriangles = new List<int>();
Vector3 visibleVertex = new Vector3();
for(int i=0; i < vertices.Length;i++)
{
visibleVertex = cam.WorldToViewportPoint(vertices[i]);
//---if vertex is visible
if(visibleVertex.x <= 1 && visibleVertex.x >= 0 && visibleVertex.y>= 0 && visibleVertex.y <=1)
{
visibleVertices.Add(vertices[i]);
// newTriangles.Add(i);
}
}
Mesh newMesh = new Mesh();
Vector3[] visibleVerts = new Vector3[visibleVertices.Count];
int[] newTris = new int[visibleVerts.Length*6];
for (int i = 0; i< visibleVertices.Count; i++)
{
visibleVerts[i] = visibleVertices[i];
}
newMesh.vertices = visibleVerts;
//newMesh.RecalculateNormals();
//newMesh.RecalculateBounds();
SaveMeshAsObj(newMesh);
}
Comment
Have you checked the Scripting API for the $$anonymous$$esh class? It has an example that explains how the triangles array is used.
Getting the triangles array is not my problem. It's calculating triangle array for the submesh I am extracting, without changing the original topology.