Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

In Unity how do I find the vertices on an edge (at runtime)?

Discussion in 'Scripting' started by Ninamori, Jul 24, 2016.

  1. Ninamori

    Ninamori

    Joined:
    Oct 30, 2015
    Posts:
    33
    I am looking to expand a system I have. I want to be able to find the vertices that are connected to an edge. How would I do this? At runtime mind you.

     
  2. neoshaman

    neoshaman

    Joined:
    Feb 11, 2011
    Posts:
    6,469
    Well try to access to triangle, triangle will lead you to vertex, each triangle being 3 edges by definition.
     
  3. ericbegue

    ericbegue

    Joined:
    May 31, 2013
    Posts:
    1,353
  4. Ninamori

    Ninamori

    Joined:
    Oct 30, 2015
    Posts:
    33
    Right. However how would I know which edges I want? I basically want to modify the edges near the outskirts of the mesh like in the image I show in the OP.
     
  5. ericbegue

    ericbegue

    Joined:
    May 31, 2013
    Posts:
    1,353
    To modify an edge, you need to modify the vertices defining that edge.

    The way Unity stores meshes does not give direct data about edges and their connectivity. Edge are implicit, only triangles and vertices are explicitly stored.

    If you want the edge vertices for that specific cube, you need to know how the cube is built. You could build the cube yourself or print out it's data to the console and study its vertices and triangles arrays to figure out the edges you want.

    If you want a more generalized solution, that is to query the vertices of a given edge of a given mesh, you would need to implement your own mesh data structure since the built-in Unity Mesh class does not support such query. You can get some inspiration from how Blender internally represents meshes: see BMesh.
     
  6. neoshaman

    neoshaman

    Joined:
    Feb 11, 2011
    Posts:
    6,469
    Or half edge topology.

    When do you need to alter the edges?

    A mesh is basically a graph, you can analyse the structure to infer what you want, in this case it seems you would want to avoid edges that border two co planar triangle. So looping through all triangles and finding shared edges of them, would also you to NOT mark edges in which the coplanarity of their triangle is verified.
     
    Kiwasi likes this.
  7. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Is this only for cubes? On a cube all vertices are connected to an edge. So just grab all vertices.

    For more complicated meshes you really need to define what constitutes an edge. Once you have that down, you can work out ways to find the edges.