• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by DaiMangouDev · Nov 04, 2014 at 10:17 PM · c#editorarrayvector3distance

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];
     
         }
Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image robertbu · Nov 04, 2014 at 11:12 PM 0
Share

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.

avatar image DaiMangouDev · Nov 05, 2014 at 02:55 AM 0
Share

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...

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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 );     
 }    
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image DaiMangouDev · Nov 05, 2014 at 02:41 AM 0
Share

looks very clean and innovative, this is fine . Thank you for your assistance , I appreciate the help.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Im misunderstanding arrays. 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

Decrease value at half the speed it's increased at? (using Vector3.Distance) 1 Answer

C# - Creating an Array of Vector 3s 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges