Advertisement
adtither

NormalsCalculations

Apr 20th, 2014
222
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.03 KB | None | 0 0
  1.         public virtual MyVertexPositionNormal[] CalculateNormals(MyVertexPositionNormal[] vertices, UInt16[] indices)
  2.         {
  3.             for (int i = 0; i < vertices.Length; i++)
  4.                 vertices[i].Normal = new Vector3(0, 0, 0);
  5.  
  6.             for (int i = 0; i < indices.Length / 3; i++)
  7.             {
  8.                 int index1 = indices[i * 3];
  9.                 int index2 = indices[i * 3 + 1];
  10.                 int index3 = indices[i * 3 + 2];
  11.  
  12.                 Vector3 side1 = vertices[index1].Position - vertices[index3].Position;
  13.                 Vector3 side2 = vertices[index1].Position - vertices[index2].Position;
  14.                 Vector3 normal = Vector3.Cross(side1, side2);
  15.  
  16.                 vertices[index1].Normal += normal;
  17.                 vertices[index2].Normal += normal;
  18.                 vertices[index3].Normal += normal;
  19.             }
  20.  
  21.             for (int i = 0; i < vertices.Length; i++)
  22.             {
  23.                 vertices[i].Normal.Normalize();
  24.             }
  25.  
  26.             return vertices;
  27.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement