# Unity 3D: A rough and ready computation of normals - useful for procedural meshes

Here is a rough and ready method to compute a meshes' vertex normals knowing the vertices and the indicies of your mesh.

`  List[] normalBuffer= new List[NumVerts];    for(int vl = 0; vl < normalBuffer.Length; ++vl) {     normalBuffer[vl] = new List();  }    for( int i = 0; i < NumIndices; i += 3 )  {    // get the three vertices that make the faces    Vector3 p1 = m_original[m_mesh.triangles[i+0]];    Vector3 p2 = m_original[m_mesh.triangles[i+1]];    Vector3 p3 = m_original[m_mesh.triangles[i+2]];    Vector3 v1 = p2 - p1;    Vector3 v2 = p3 - p1;    Vector3 normal = Vector3.Cross(v1, v2 );    normal.Normalize();    // Store the face's normal for each of the vertices that make up the face.    normalBuffer[m_mesh.triangles[i+0]].Add(normal);    normalBuffer[m_mesh.triangles[i+1]].Add(normal);    normalBuffer[m_mesh.triangles[i+2]].Add(normal);  }  for( int i = 0; i < NumVerts; ++i )  {   for (int j = 0; j < normalBuffer[i].Count; ++j) {    m_normals[i] += normalBuffer[i][j];   }       m_normals[i]  /= normalBuffer[i].Count;  }`

# Re: Unity 3D: A rough and ready computation of normals - useful for procedural meshes

Hmm... how about mesh.RecalculateNormals()? See here: http://unity3d.com/support/documentation/ScriptReference/Mesh.RecalculateNormals.html

# Re: Unity 3D: A rough and ready computation of normals - useful for procedural meshes

Good Point NeARAZ. I'm glad somebody is paying attention. I wasn't initially aware of this mesh method. I didn't want normals generated automatically via Unity import because I'm currently working with a real time vertex compositor that relies on tagged and known vertex indicies. It appears this mesh method doesn't add vertices and is suitable for my needs.