- Home /
How do I find the farthest verts of a mesh relative to its object position and store them in an array
Hello , I am writing an editor script that finds all the verts in a mesh, calculate their distance from the object the mesh is on then determine which verts on the x, y and z axis are the farthest away from the objects transform.positions x, y and z axis respectively.
In the code below ,Right after I have calculated the distance of all the verts of the mesh from the object with the mesh I am having trouble finding the farthest verts on the x ,y and z axis and assigning them to an array.
what would be the best way to get this done?
any help is appreciated .
private Vector3 MeshPosition;
private Vector3[] Verts, Farthest;
private Vector3 VertPosition()
{
Verts = Object_With_Mesh.GetComponent<MeshFilter>().sharedMesh.vertices;
if (MeshPosition == new Vector3())
{
try
{
MeshPosition = Object_With_Mesh.GetComponent<Transform>().transform.position;
}
catch
{
}
}
int VertCountPosition = 0;
foreach (Vector3 Vert in Verts)
{
float distance = Mathf.Infinity;
Vector3 diff = Vert - MeshPosition;
float curDistance = diff.sqrMagnitude;
if (curDistance < distance)
{
Farthest[VertCountPosition] = Vert;
VertCountPosition += 1;
Debug.Log(Farthest[VertCountPosition]);
distance = curDistance;
}
}
return Farthest[VertCountPosition];
}
To make this work, you need an inner loop so that when you find a vertex that is farther away, you replace the closest vertex in the Farthest array, not just add it to the end of the Furthest array. As a simpler (but less efficient) alternate, you can create a struct that has an index to the original vertex position and the distance from the position to $$anonymous$$eshPosition. Then you can build an array of this struct and sort the resulting array. Pull out the top n to fill your Farthest[] array.
true , that could also work . What I had overlooked is the fact that i already had access to the distances of the verts of the mesh relative to the mesh position . here
foreach (Vector3 Vert in Verts)
{
EditorGUILayout.Vector3Field("Verts", Vert);
}
this would have displayed all the pert positions then I could have just compared each x, y,z value and find the largest number etc...
Answer by AlwaysSunny · Nov 04, 2014 at 10:52 PM
Just copied my function for sorting an array of Vector3's by their distance from a specified origin. You can adapt it to your situation pretty easily.
public static void SortDistances( ref Vector3[] positions, Vector3 origin ) {
float[] distances = new float[ positions.Length ];
for (int i = 0; i < positions.Length; i++) {
distances[i] = (positions[i] - origin).sqrMagnitude;
}
System.Array.Sort( distances, positions );
}
looks very clean and innovative, this is fine . Thank you for your assistance , I appreciate the help.
Your answer
Follow this Question
Related Questions
Are Unity distances that small? 1 Answer
How to display a list of methods and allow a choice of one of them 1 Answer
An Array of Classes with variables in C# 2 Answers
if close to one of a kind? 2 Answers
Distribute terrain in zones 3 Answers