Search Unity

Calculating normals at the extremes of a plane.

Discussion in 'Scripting' started by Kryber, Mar 23, 2014.

  1. Kryber

    Kryber

    Joined:
    Apr 14, 2013
    Posts:
    92
    Hello. In a plane, to calculate the vertex normals I use this algorithm:

    Code (csharp):
    1.         for(int z = 0; z < dimension; z++)
    2.         {
    3.             for(int x = 0; x < dimension; x++)
    4.             {
    5.                 Vector3 a = vertices[x + z * dimension];
    6.                 Vector3 b = vertices[x + 1 + z * dimension];
    7.                 Vector3 c = vertices[x + (z + 1) * dimension];
    8.  
    9.                 Vector3 side1 = b - a;
    10.                 Vector3 side2 = c - a;
    11.  
    12.                 Vector3 val = Vector3.Cross(side1, side2);
    13.                 val = Vector3.Normalize(val);
    14.  
    15.                 normals[x + z * dimension] = -val;
    16.             }
    17.         }
    But obviously it can not calculate the normal to the extremes because there are no other subsequent vertex. How can I calculate the normals also on the vertices at the ends of the mesh?

    I saw this tutorial...
    http://docs.unity3d.com/Documentation/Manual/ComputingNormalPerpendicularVector.html
     
  2. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,539
    If it's a plane, wouldn't all the normals be the same by definition?

    If it's not actually a plane, and instead is mesh, well... each triangle in the mesh can define a plane. On which the relative normals will all be the same, but your resulting mesh normals will be the average of the various normals from each triangle that touches that point. And when at the extremeties of a mesh, it's just that some of those triangles won't exist and won't be included in the average.
     
    Last edited: Mar 23, 2014
  3. Kryber

    Kryber

    Joined:
    Apr 14, 2013
    Posts:
    92
    Sorry I have not explained well ... Suppose you have a grid of this kind, and every vertex must calculate its normal that will be calculated according to the other adjacent vertices. Once you have reached the end of the grid, however, it can not calculate the normal because he has no more vertices adjacent to disposal.

    $Unity.png
     
  4. LightStriker

    LightStriker

    Joined:
    Aug 3, 2013
    Posts:
    2,717
    From your grid, numbering the vertices from left to right, and up to down, from 0 to 15...

    Normal of vertex 0 (v0) is (Cross(v1 - v0, v5 - v1) + Cross(v5 - v0, v4 - v0)) / 2

    In other word, in a completely smooth mesh, the normal of each vertex is the average of the normal of each triangle it is linked to.

    So v14 is (Cross(v14 - v10, v15 - v14) + Cross(v14 - v10, v14 - v9) + Cross(v14 - v9, v14 - v13)) / 3
     
  5. rcober

    rcober

    Joined:
    Jan 26, 2013
    Posts:
    52
    It depends on what you are doing, but in general, the normal at each vertex is an average of the normals of each triangle connected to it.

    In your example above, you would calculate the normal of each triangle (since each tri is a plane by definition). Then for each vertex, average the normals of all the triangles it touches. This is usually a weighted average, depending on the size of the triangle.

    BTW - If it is a plane - the normals are all the same anyway, as stated before
     
  6. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,539
    1) It has adjacent vertices... behind itself.

    2) If this is a plane! A plane by definition is a surface with a constant normal. That means the normal at all points (including these verts) should be equal! You've already calculated it... at every single vert.

    3) If this is NOT a plane. Then you're doing your calculations wrong. You're calculating the normal of a vert relative to the triangle. This would result in the normals of the face of the triangle... not the normal of the vert. The normal of the vert is an average of the face normals of all triangles that touch that vert. This is because a triangle can define its own plane, which all three verts lay in, making all 3 verts relative normal to just that triangle equal. BUT that doesn't consider the normal of the surrounding triangles which is the more important information we want... so we average them. And when you're at the extremeties... well, that just means there are fewer triangle face normals to average.
     
  7. Kryber

    Kryber

    Joined:
    Apr 14, 2013
    Posts:
    92
    D: I didn't understand. Thank you for your explanation. The normal anyway I have to find on a mesh-like terrain and not in a plane. I'm not English so I had misunderstood several tutorials on the net. Thank you.