예제 #1
0
        protected bool BuildTangentForMeshObjects(NullMeshObjects meshObjects, float thresHold, bool forceSmooth = false)
        {
            if (meshObjects == null || meshObjects.GetMeshObjectCount() == 0)
            {
                return(false);
            }

            int triangleCount = meshObjects.GetTriangleCount();

            if (triangleCount > 0)
            {
                //preparing data
                List <Vector3> vertices = new List <Vector3>();
                List <Vector2> uvs      = new List <Vector2>();

                List <Vector3> tangents          = Make <Vector3>(triangleCount * 3);
                List <Vector3> binormals         = Make <Vector3>(triangleCount * 3);
                List <byte>    smoothGroups      = Make <byte>(triangleCount);
                int            realTriangleCount = 0;

                for (int i = 0; i < meshObjects.GetMeshObjectCount(); i++)
                {
                    NullMeshObject meshObject = meshObjects[i];
                    if (meshObject.GetUVGroups().Count == 0 || meshObject.GetNormalData().Count == 0)
                    {
                        continue;
                    }
                    int count = meshObject.GetTriangleCount();
                    vertices.AddRange(meshObject.GetVertexData());
                    byte smoothGroup = meshObject.GetSmoothGroup();

                    if (forceSmooth && smoothGroup == 0)
                    {
                        smoothGroup = 1;
                    }
                    Set(smoothGroups, smoothGroup);
                    NullUVGroup uvGroup = meshObject.GetUVGroup(UVType.UVT_NORMAL_MAP);
                    if (uvGroup == null)
                    {
                        uvGroup = meshObject.GetUVGroup(UVType.UVT_DEFAULT);
                    }
                    uvs.AddRange(uvGroup.GetUVData());
                    realTriangleCount += count;
                }
                //do face-smoothing
                ComputeTengents(thresHold, vertices, realTriangleCount, smoothGroups, uvs, tangents, binormals);

                //update mesh objects normals
                for (int i = 0; i < meshObjects.GetMeshObjectCount(); i++)
                {
                    NullMeshObject meshObject = meshObjects[i];
                    if (meshObject.GetUVGroups().Count == 0 || meshObject.GetNormalData().Count == 0)
                    {
                        continue;
                    }
                    meshObject.BuildTangentArray();
                    int count = meshObject.GetTriangleCount();
                    for (int j = 0; j < count; j++)
                    {
                        //set tangent
                        meshObject.SetTangent(j * 3 + 0, tangents[j * 3]);
                        meshObject.SetTangent(j * 3 + 1, tangents[j * 3 + 1]);
                        meshObject.SetTangent(j * 3 + 2, tangents[j * 3 + 2]);

                        //set binormal
                        meshObject.SetBinormal(j * 3 + 0, binormals[j * 3]);
                        meshObject.SetBinormal(j * 3 + 1, binormals[j * 3 + 1]);
                        meshObject.SetBinormal(j * 3 + 2, binormals[j * 3 + 2]);
                    }
                }
                //delete temp buffer
                vertices.Clear();
                tangents.Clear();
                binormals.Clear();
                uvs.Clear();
                smoothGroups.Clear();
            }
            return(true);
        }