Example #1
0
        /// <summary>
        /// Recalculates a mesh's normals while retaining smoothed common vertices.
        /// </summary>
        /// <param name="mesh"></param>
        internal static void RecalculateNormals(PolyMesh mesh)
        {
            //null checks
            if (mesh == null)
            {
                return;
            }

            List <List <int> > smooth = GetSmoothSeamLookup(mesh);

            mesh.RecalculateNormals();

            if (smooth != null)
            {
                Vector3[] normals = mesh.normals;

                foreach (List <int> l in smooth)
                {
                    Vector3 n = PolyMath.Average(normals, l);

                    foreach (int i in l)
                    {
                        normals[i] = n;
                    }
                }

                mesh.normals = normals;
            }
        }
Example #2
0
        /// <summary>
        /// Recalculates the normals of the Mesh from the triangles and vertices.
        /// </summary>
        internal void RecalculateNormals()
        {
            Vector3[] perTriangleNormal = new Vector3[vertexCount];
            int[]     perTriangleAvg    = new int[vertexCount];
            int[]     tris = GetTriangles();

            for (int i = 0; i < tris.Length; i += 3)
            {
                int a = tris[i], b = tris[i + 1], c = tris[i + 2];

                Vector3 cross = PolyMath.Normal(vertices[a], vertices[b], vertices[c]);

                perTriangleNormal[a].x += cross.x;
                perTriangleNormal[b].x += cross.x;
                perTriangleNormal[c].x += cross.x;

                perTriangleNormal[a].y += cross.y;
                perTriangleNormal[b].y += cross.y;
                perTriangleNormal[c].y += cross.y;

                perTriangleNormal[a].z += cross.z;
                perTriangleNormal[b].z += cross.z;
                perTriangleNormal[c].z += cross.z;

                perTriangleAvg[a]++;
                perTriangleAvg[b]++;
                perTriangleAvg[c]++;
            }


            for (int i = 0; i < vertexCount; i++)
            {
                normals[i].x = perTriangleNormal[i].x * (float)perTriangleAvg[i];
                normals[i].y = perTriangleNormal[i].y * (float)perTriangleAvg[i];
                normals[i].z = perTriangleNormal[i].z * (float)perTriangleAvg[i];
                normals[i].Normalize();
            }
        }