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
Re: Unity 3D: A rough and ready computation of normals - useful for procedural meshes
Re: Unity 3D: A rough and ready computation of normals - useful for procedural meshes
Thanks, that's what I needed.