I have a 3D mesh and what I want to do is to draw the outline of my top 2D face, so here what I have acess to top mesh vertices and triangles indices and what i want is to order vertices so that I can draw my outline.
List<Vector3> GetSortedVerticesTest(Dictionary<CreateMesh.Faces, MeshData> mesh, Mesh mainMesh)
{
List<Vector3> borderVertices = mesh[CreateMesh.Faces.Top].vertices.Where(p => mesh[CreateMesh.Faces.other].vertices.Any(e => e == p)).ToList();
borderVertices = borderVertices.Distinct().ToList();
List<Vector3> sortedVertices = new List<Vector3>();
List<PointAngle> candidateAngles = new List<PointAngle>();
Vector3 startingPoint = borderVertices[0];
sortedVertices.Add(startingPoint);
borderVertices.Remove(startingPoint);
while (borderVertices.Count > 0)
{
List<Vector3> candidates = new List<Vector3>();
for (int i = 0; i < mesh[CreateMesh.Faces.Top].triangles.Count(); i += 3)
{
bool commonTriangle = false;
// verifie que le vertice courant est compris dans le triangle
for (int k = 0; k < 3; k++)
{
int triIndex = mesh[CreateMesh.Faces.Top].triangles[i + k];
Vector3 posToCompare = mainMesh.vertices[triIndex];
if (startingPoint == posToCompare)
{
commonTriangle = true;
}
}
if (commonTriangle)
{
for (int k = 0; k < 3; k++)
{
int triIndex = mesh[CreateMesh.Faces.Top].triangles[i + k];
Vector3 posToCompare = mainMesh.vertices[triIndex];
if (borderVertices.Contains(posToCompare))//epsilon
candidates.Add(posToCompare);
}
candidates.Remove(startingPoint);
if (candidates.Count != 0)
{
candidates.ForEach(c => candidateAngles.Add(new PointAngle { angle = Vector3.SignedAngle(c - startingPoint, transform.forward, Vector3.up), point = c }));
double min = candidateAngles.Min(kvp => kvp.angle);
var a = candidateAngles[0].point;
PointAngle pointsToKeep = new PointAngle();
pointsToKeep = candidateAngles.Where(s => s.angle == min).First();
sortedVertices.Add(pointsToKeep.point);
borderVertices.Remove(pointsToKeep.point);
print("vert coun" + borderVertices.Count);
startingPoint = pointsToKeep.point;
candidateAngles.Clear();
}
}
}
}
return sortedVertices;
}
here I m trying to get for all points candidates to get next vertice