Esempio n. 1
0
        public static BoneWeight ToBoneWeight(this EditableBoneWeight editableBoneWeight, bool sortByWeight)
        {
            BoneWeight boneWeight = new BoneWeight();

            int channelCount = editableBoneWeight.GetChannelCount();

            if (channelCount > 0)
            {
                s_BoneWeightDataList.Clear();
                s_BoneWeightDataList.Capacity = editableBoneWeight.GetChannelCount();

                for (int i = 0; i < channelCount; ++i)
                {
                    s_BoneWeightDataList.Add(editableBoneWeight.GetBoneWeightData(i));
                }

                if (sortByWeight)
                {
                    s_BoneWeightDataList.Sort();
                }

                if (s_BoneWeightDataList.Count >= 1)
                {
                    boneWeight.boneIndex0 = s_BoneWeightDataList[0].boneIndex;
                    boneWeight.weight0    = s_BoneWeightDataList[0].weight;
                }

                if (s_BoneWeightDataList.Count >= 2)
                {
                    boneWeight.boneIndex1 = s_BoneWeightDataList[1].boneIndex;
                    boneWeight.weight1    = s_BoneWeightDataList[1].weight;
                }

                if (s_BoneWeightDataList.Count >= 3)
                {
                    boneWeight.boneIndex2 = s_BoneWeightDataList[2].boneIndex;
                    boneWeight.weight2    = s_BoneWeightDataList[2].weight;
                }

                if (s_BoneWeightDataList.Count >= 4)
                {
                    boneWeight.boneIndex3 = s_BoneWeightDataList[3].boneIndex;
                    boneWeight.weight3    = s_BoneWeightDataList[3].weight;
                }
            }

            return(boneWeight);
        }
Esempio n. 2
0
        public static void UnifyChannelsWithSameBoneIndex(this EditableBoneWeight editableBoneWeight)
        {
            int channelCount = editableBoneWeight.GetChannelCount();

            for (int i = 0; i < channelCount; ++i)
            {
                BoneWeightData data = editableBoneWeight.GetBoneWeightData(i);

                if (!editableBoneWeight.IsChannelEnabled(i))
                {
                    continue;
                }

                bool weightChanged = false;

                for (int j = i + 1; j < channelCount; ++j)
                {
                    BoneWeightData otherData = editableBoneWeight.GetBoneWeightData(j);

                    if (otherData.boneIndex == data.boneIndex)
                    {
                        weightChanged = true;
                        data.weight  += otherData.weight;
                        editableBoneWeight.EnableChannel(j, false);
                    }
                }

                if (weightChanged)
                {
                    editableBoneWeight.SetBoneWeightData(i, data);
                    editableBoneWeight.CompensateOtherChannels(i);
                }
            }
        }
Esempio n. 3
0
        public static void RemoveBone(this EditableBoneWeight editableBoneWeight, int boneIndex)
        {
            int channelCount = editableBoneWeight.GetChannelCount();

            for (int i = 0; i < channelCount; ++i)
            {
                BoneWeightData data = editableBoneWeight.GetBoneWeightData(i);

                if (data.boneIndex > boneIndex)
                {
                    data.boneIndex -= 1;
                }
                else if (data.boneIndex == boneIndex)
                {
                    data.boneIndex = 0;
                    data.weight    = 0f;
                    editableBoneWeight.EnableChannel(i, false);
                }

                editableBoneWeight.SetBoneWeightData(i, data);
            }

            editableBoneWeight.NormalizeChannels();
            editableBoneWeight.SortChannels();
        }
Esempio n. 4
0
        public static void CompensateOtherChannels(this EditableBoneWeight editableBoneWeight, int masterChannelIndex)
        {
            ValidateChannels(editableBoneWeight);

            int   validChannelCount = 0;
            float sum = 0f;

            for (int i = 0; i < editableBoneWeight.GetChannelCount(); ++i)
            {
                if (i != masterChannelIndex && editableBoneWeight.IsChannelEnabled(i))
                {
                    BoneWeightData data = editableBoneWeight.GetBoneWeightData(i);

                    sum += data.weight;
                    ++validChannelCount;
                }
            }

            if (validChannelCount == 0)
            {
                return;
            }

            BoneWeightData channelData = editableBoneWeight.GetBoneWeightData(masterChannelIndex);

            float targetSum = 1f - channelData.weight;

            for (int i = 0; i < editableBoneWeight.GetChannelCount(); ++i)
            {
                if (i != masterChannelIndex && editableBoneWeight.IsChannelEnabled(i))
                {
                    BoneWeightData data = editableBoneWeight.GetBoneWeightData(i);

                    if (sum == 0f)
                    {
                        data.weight = targetSum / validChannelCount;
                    }
                    else
                    {
                        data.weight = data.weight * targetSum / sum;
                    }

                    editableBoneWeight.SetBoneWeightData(i, data);
                }
            }
        }
Esempio n. 5
0
        public static void ClampChannels(this EditableBoneWeight editableBoneWeight, int numChannels, bool sortChannels = true)
        {
            if (sortChannels)
            {
                editableBoneWeight.SortChannels();
            }

            while (editableBoneWeight.GetChannelCount() > numChannels)
            {
                editableBoneWeight.RemoveChannel(numChannels);
            }
        }
Esempio n. 6
0
        public static void ValidateChannels(this EditableBoneWeight editableBoneWeight)
        {
            for (int channelIndex = 0; channelIndex < editableBoneWeight.GetChannelCount(); ++channelIndex)
            {
                BoneWeightData data = editableBoneWeight.GetBoneWeightData(channelIndex);

                if (!editableBoneWeight.IsChannelEnabled(channelIndex))
                {
                    data.weight = 0f;
                }

                data.weight = Mathf.Clamp01(data.weight);
                editableBoneWeight.SetBoneWeightData(channelIndex, data);
            }
        }
Esempio n. 7
0
        public static float GetWeightSum(this EditableBoneWeight editableBoneWeight)
        {
            float sum = 0f;

            for (int i = 0; i < editableBoneWeight.GetChannelCount(); ++i)
            {
                if (editableBoneWeight.IsChannelEnabled(i))
                {
                    BoneWeightData data = editableBoneWeight.GetBoneWeightData(i);

                    sum += data.weight;
                }
            }

            return(sum);
        }
Esempio n. 8
0
        public static int GetChannelFromBoneIndex(this EditableBoneWeight editableBoneWeight, int boneIndex)
        {
            for (int i = 0; i < editableBoneWeight.GetChannelCount(); ++i)
            {
                if (editableBoneWeight.IsChannelEnabled(i))
                {
                    BoneWeightData data = editableBoneWeight.GetBoneWeightData(i);

                    if (data.boneIndex == boneIndex)
                    {
                        return(i);
                    }
                }
            }

            return(-1);
        }
Esempio n. 9
0
        public static void FilterChannels(this EditableBoneWeight editableBoneWeight, float weightTolerance)
        {
            int channelCount = editableBoneWeight.GetChannelCount();

            for (int i = 0; i < channelCount; ++i)
            {
                BoneWeightData data = editableBoneWeight.GetBoneWeightData(i);

                if (data.weight <= weightTolerance)
                {
                    data.boneIndex = 0;
                    data.weight    = 0f;

                    editableBoneWeight.SetBoneWeightData(i, data);
                    editableBoneWeight.EnableChannel(i, false);
                }
            }
        }
Esempio n. 10
0
        public static void SmoothWeights(BoneWeight[] boneWeightIn, IList <int> indices, int boneCount, BoneWeight[] boneWeightOut)
        {
            Debug.Assert(boneWeightIn != null);
            Debug.Assert(boneWeightOut != null);
            Debug.Assert(boneWeightIn != boneWeightOut);
            Debug.Assert(boneWeightIn.Length == boneWeightOut.Length);

            PrepareTempBuffers(boneWeightIn.Length, boneCount);

            EditableBoneWeight editableBoneWeight = new EditableBoneWeight();

            for (int i = 0; i < boneWeightIn.Length; ++i)
            {
                editableBoneWeight.SetFromBoneWeight(boneWeightIn[i]);
                for (int channelIndex = 0; channelIndex < editableBoneWeight.GetChannelCount(); ++channelIndex)
                {
                    if (editableBoneWeight.IsChannelEnabled(channelIndex))
                    {
                        BoneWeightData boneWeightData = editableBoneWeight.GetBoneWeightData(channelIndex);
                        m_DataInTemp[i, boneWeightData.boneIndex] = boneWeightData.weight;
                    }
                }
            }

            SmoothPerVertexData(indices, m_DataInTemp, m_DataOutTemp);

            for (int i = 0; i < boneWeightIn.Length; ++i)
            {
                editableBoneWeight.Clear();

                for (int boneIndex = 0; boneIndex < boneCount; ++boneIndex)
                {
                    float weight     = m_DataOutTemp[i, boneIndex];
                    int   boneIndex2 = weight > 0f ? boneIndex : 0;
                    editableBoneWeight.AddChannel(new BoneWeightData(boneIndex2, weight), weight > 0);
                }

                editableBoneWeight.ClampChannels(4);
                editableBoneWeight.NormalizeChannels();

                boneWeightOut[i] = editableBoneWeight.ToBoneWeight(false);
            }
        }
Esempio n. 11
0
        public static void NormalizeChannels(this EditableBoneWeight editableBoneWeight)
        {
            ValidateChannels(editableBoneWeight);

            float sum = editableBoneWeight.GetWeightSum();

            if (sum == 0f || sum == 1f)
            {
                return;
            }

            for (int i = 0; i < editableBoneWeight.GetChannelCount(); ++i)
            {
                if (editableBoneWeight.IsChannelEnabled(i))
                {
                    BoneWeightData data = editableBoneWeight.GetBoneWeightData(i);

                    data.weight = data.weight / sum;

                    editableBoneWeight.SetBoneWeightData(i, data);
                }
            }
        }