Search Unity

How do I get the normal of each triangle in mesh?

Discussion in 'Scripting' started by Broccoli, Aug 17, 2011.

  1. Broccoli

    Broccoli

    Joined:
    Sep 2, 2010
    Posts:
    28
    Hi,

    I have some problem with getting a normal vector of each triangle in a mesh(Also the startPoint of that vector too. I mean the center point between a three vertice that make this triangle and the normal from that point).

    I know that I can access mesh.normals but I think this isn't the normal from a center of each triangle right?


    Thank you : )
     
  2. Ntero

    Ntero

    Joined:
    Apr 29, 2010
    Posts:
    1,436
    Mathematically, if you think of a triangle being made of points P1, P2, P3, you do the normalized Cross Product of P1 to P2, and P1 to P3. There is a Vector3.Cross() function to help with that.

    It's been a while but that should give you the normal.

    The center point is just the average position of P1, P2 and P3 or ((P1 + P2 + P3) / 3).
     
    Last edited: Aug 17, 2011
    Novack and mysticvalleycsa like this.
  3. Broccoli

    Broccoli

    Joined:
    Sep 2, 2010
    Posts:
    28
    Thank you a lot! Ntero for the mathematically calculation

    How about if I have a series of vertice in mesh.vertices
    and I Don't really know that which 3 vertices in those vertice series will relate to each other and produce a triangle?

    is there is a way to find out this issue?
     
  4. bigmisterb

    bigmisterb

    Joined:
    Nov 6, 2010
    Posts:
    4,221
    In the Mesh, you have a triangle list. [0, 1, 2, 1, 2, 3, 2, 3, 4, 2, 4, 5]

    This list is broken up into sets of 3, so the triangle list really reads like this:

    [[0, 1, 2] [1, 2, 3][2, 3, 4][2, 4, 5]]

    So pull out 3 indexes from every 3 in the integer array. This is your triangle indexing.

    In the vertices of the mesh, you have a series of Vector3's. Each item in that array, corresponds to a triangle index.

    Same goes for normals (Vector3's) and UV's (Vector2's)... and colors and tangents and whatever else is there.

    So, your answer is.

    Code (csharp):
    1.  
    2. P1=vertices[triangles[triangle* 3]]
    3. P2=vertices[triangles[triangle * 3 + 1]]
    4. P3=vertices[triangles[triangle * 3 + 1]]
    5.  
    6. center = ((P1 + P2 + P3) / 3)
    7.  
    8. P1=normals[triangles[triangle * 3]]
    9. P2=normals[triangles[triangle * 3 + 1]]
    10. P3=normals[triangles[triangle * 3 + 2]]
    11.  
    12. faceNormal=((P1 + P2 + P3) / 3)
    13.  
     
    Last edited: Aug 17, 2011
    ILonion, Lcdscreen, Biell2015 and 3 others like this.
  5. Ntero

    Ntero

    Joined:
    Apr 29, 2010
    Posts:
    1,436
    The mesh.Triangles is your index list. It lists Vertex indices in groups of 3.
    Code (csharp):
    1.  
    2. //Mesh data calls can be VERY expensive so cache it first
    3. Vector3 verts = mesh.vertices;
    4. int indices = mesh.triangles;
    5. for(int in = 0; i < mesh.triangles.Length;)
    6. {
    7. Vector3 P1 = verts[indices[i++]];
    8. Vector3 P2 = verts[indices[i++]];
    9. Vector3 P3 = verts[indices[i++]];
    10. }
    11.  
    Loop through the indices and then get the vertices that the current indices point to to get each individual triangle point.

    Note: BigMisterB the Vertices in vertices can be reused and are not necessarily sorted based on the triangles. You need to go through mesh.triangles to get the proper vertex indices for each triangle.
     
    Last edited: Aug 17, 2011
    ILonion, yunsong97 and khaled24 like this.
  6. bigmisterb

    bigmisterb

    Joined:
    Nov 6, 2010
    Posts:
    4,221
    Sorry, I know that part, just skipped over it.. lol
     
  7. Broccoli

    Broccoli

    Joined:
    Sep 2, 2010
    Posts:
    28
    Hey, Thank you BigMisterB and Ntero

    This solve my problem : )
    At first I see the mesh.triangle but I don't know what the return value int[] really is

    now you make me understand it better, Thank again for your help.
     
  8. CoatlGames

    CoatlGames

    Joined:
    Apr 25, 2008
    Posts:
    773
    this was hell of a useful post, i was wondering the exact same myself

    thank you all