Ejemplo n.º 1
0
        // key - control point index
        public static Dictionary <int, BoneAssignment> GetBoneAssignments(FbxMesh mesh, Skeleton skeleton)
        {
            var boneAssignments = new Dictionary <int, BoneAssignment>();
            var weightLists     = new Dictionary <int, List <(int, double)> >();

            int skinCount = mesh.GetDeformerCount(FbxDeformer.EDeformerType.eSkin);

            if (skinCount == 0)
            {
                return(null);
            }
            if (1 < skinCount)
            {
                FbxImportLog.LogMessage(mesh.GetNode(), "Warning! Multiple skins for the mesh");                   //??? Может ли быть в одном Mesh несколько Skins? Скорее всего нет, хоть API позволяет.
            }
            FbxSkin pSkin = FbxSkin.Cast(mesh.GetDeformer(0, FbxDeformer.EDeformerType.eSkin));

            int clusterCount = pSkin.GetClusterCount();

            for (int iCluster = 0; iCluster < clusterCount; iCluster++)
            {
                FbxCluster pCluster = pSkin.GetCluster(iCluster);

                FbxNode pLink = pCluster.GetLink();
                if (pLink == null)
                {
                    continue;
                }

                int weightCount = pCluster.GetControlPointIndicesCount();
                if (weightCount == 0)
                {
                    continue;
                }

                int boneIndex = skeleton.GetBoneIndexByNode(pLink);

                var weightIndices = IntArray.frompointer(pCluster.GetControlPointIndices());
                var weightValues  = DoubleArray.frompointer(pCluster.GetControlPointWeights());
                for (int i = 0; i < weightCount; i++)
                {
                    int    vertexIndex = weightIndices.getitem(i);
                    double weight      = weightValues.getitem(i);

                    if (!weightLists.TryGetValue(vertexIndex, out var lst))
                    {
                        lst = new List <(int, double)>();
                        weightLists[vertexIndex] = lst;
                    }

                    lst.Add((boneIndex, weight));
                }
            }

            foreach (var pair in weightLists)
            {
                boneAssignments[pair.Key] = ConvertBoneWeightListToBoneAssignment(pair.Value);
            }
            return(boneAssignments);
        }