Ejemplo n.º 1
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);
                }
            }
        }
Ejemplo n.º 2
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();
        }
Ejemplo n.º 3
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);
                }
            }
        }
Ejemplo n.º 4
0
        public static void GetMultiEditChannelData(this SpriteMeshData spriteMeshData, ISelection selection, int channelIndex,
                                                   out bool channelEnabled, out BoneWeightData boneWeightData,
                                                   out bool isChannelEnabledMixed, out bool isBoneIndexMixed, out bool isWeightMixed)
        {
            if (selection == null)
            {
                throw new ArgumentNullException("selection is null");
            }

            bool first = true;

            channelEnabled        = false;
            boneWeightData        = new BoneWeightData();
            isChannelEnabledMixed = false;
            isBoneIndexMixed      = false;
            isWeightMixed         = false;

            foreach (int i in selection)
            {
                EditableBoneWeight editableBoneWeight = spriteMeshData.vertices[i].editableBoneWeight;

                BoneWeightData otherBoneWeightData = editableBoneWeight.GetBoneWeightData(channelIndex);

                if (first)
                {
                    channelEnabled           = editableBoneWeight.IsChannelEnabled(channelIndex);
                    boneWeightData.boneIndex = otherBoneWeightData.boneIndex;
                    boneWeightData.weight    = otherBoneWeightData.weight;

                    first = false;
                }
                else
                {
                    if (channelEnabled != editableBoneWeight.IsChannelEnabled(channelIndex))
                    {
                        isChannelEnabledMixed = true;
                        channelEnabled        = false;
                    }

                    if (boneWeightData.boneIndex != otherBoneWeightData.boneIndex)
                    {
                        isBoneIndexMixed         = true;
                        boneWeightData.boneIndex = -1;
                    }

                    if (boneWeightData.weight != otherBoneWeightData.weight)
                    {
                        isWeightMixed         = true;
                        boneWeightData.weight = 0f;
                    }
                }
            }
        }
Ejemplo n.º 5
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);
            }
        }
Ejemplo n.º 6
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);
        }
Ejemplo 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);
        }
Ejemplo 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);
        }
        private void SetWeight(float value, bool createNewChannel = true)
        {
            if (boneIndex == -1)
            {
                return;
            }

            Debug.Assert(selection != null);

            for (int i = 0; i < spriteMeshData.vertices.Count; ++i)
            {
                if (selection.Count == 0 && emptySelectionEditsAll ||
                    selection.Count > 0 && selection.IsSelected(i))
                {
                    EditableBoneWeight editableBoneWeight = spriteMeshData.vertices[i].editableBoneWeight;

                    int channel = editableBoneWeight.GetChannelFromBoneIndex(boneIndex);

                    if (channel == -1)
                    {
                        if (createNewChannel)
                        {
                            editableBoneWeight.AddChannel(new BoneWeightData(boneIndex, 0f), true);
                            channel = editableBoneWeight.GetChannelFromBoneIndex(boneIndex);
                        }
                        else
                        {
                            continue;
                        }
                    }

                    BoneWeightData data = editableBoneWeight.GetBoneWeightData(channel);
                    data.weight += value;

                    editableBoneWeight.SetBoneWeightData(channel, data);

                    if (editableBoneWeight.GetWeightSum() > 1f)
                    {
                        editableBoneWeight.CompensateOtherChannels(channel);
                    }

                    editableBoneWeight.FilterChannels(0f);
                }
            }
        }
Ejemplo n.º 10
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);
                }
            }
        }
Ejemplo n.º 11
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);
            }
        }
Ejemplo n.º 12
0
        public static void SetMultiEditChannelData(this SpriteMeshData spriteMeshData, ISelection selection, int channelIndex,
                                                   bool referenceChannelEnabled, bool newChannelEnabled, BoneWeightData referenceData, BoneWeightData newData)
        {
            if (selection == null)
            {
                throw new ArgumentNullException("selection is null");
            }

            bool channelEnabledChanged = referenceChannelEnabled != newChannelEnabled;
            bool boneIndexChanged      = referenceData.boneIndex != newData.boneIndex;
            bool weightChanged         = referenceData.weight != newData.weight;

            foreach (int i in selection)
            {
                EditableBoneWeight editableBoneWeight = spriteMeshData.vertices[i].editableBoneWeight;
                BoneWeightData     data = editableBoneWeight.GetBoneWeightData(channelIndex);

                if (channelEnabledChanged)
                {
                    editableBoneWeight.EnableChannel(channelIndex, newChannelEnabled);
                }

                if (boneIndexChanged)
                {
                    data.boneIndex = newData.boneIndex;
                }

                if (weightChanged)
                {
                    data.weight = newData.weight;
                }

                editableBoneWeight.SetBoneWeightData(channelIndex, data);

                if (channelEnabledChanged || weightChanged)
                {
                    editableBoneWeight.CompensateOtherChannels(channelIndex);
                }
            }
        }
Ejemplo n.º 13
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);
                }
            }
        }