Exemple #1
0
        public static void CalculateWeights(this SpriteMeshData spriteMeshData, IWeightsGenerator weightsGenerator, ISelection selection, float filterTolerance)
        {
            Vector2[] controlPoints;
            Edge[]    bones;
            int[]     pins;

            spriteMeshData.GetControlPoints(out controlPoints, out bones, out pins);

            Vector2[] vertices = new Vector2[spriteMeshData.vertices.Count];

            for (int i = 0; i < spriteMeshData.vertices.Count; ++i)
            {
                vertices[i] = spriteMeshData.vertices[i].position;
            }

            BoneWeight[] boneWeights = weightsGenerator.Calculate(vertices, spriteMeshData.edges.ToArray(), controlPoints, bones, pins);

            Debug.Assert(boneWeights.Length == spriteMeshData.vertices.Count);

            for (int i = 0; i < spriteMeshData.vertices.Count; ++i)
            {
                if (selection == null || (selection.Count == 0 || selection.IsSelected(i)))
                {
                    EditableBoneWeight editableBoneWeight = EditableBoneWeightUtility.CreateFromBoneWeight(boneWeights[i]);

                    if (filterTolerance > 0f)
                    {
                        editableBoneWeight.FilterChannels(filterTolerance);
                        editableBoneWeight.NormalizeChannels();
                    }

                    spriteMeshData.vertices[i].editableBoneWeight = editableBoneWeight;
                }
            }
        }
Exemple #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();
        }
Exemple #3
0
        private static EditableBoneWeight Lerp(EditableBoneWeight first, EditableBoneWeight second, float t)
        {
            EditableBoneWeight result = new EditableBoneWeight();

            foreach (BoneWeightChannel channel in first)
            {
                if (!channel.enabled)
                {
                    continue;
                }

                BoneWeightData data = channel.boneWeightData;
                data.weight *= (1f - t);

                if (data.weight > 0f)
                {
                    result.AddChannel(data, true);
                }
            }

            foreach (BoneWeightChannel channel in second)
            {
                if (!channel.enabled)
                {
                    continue;
                }

                BoneWeightData data = channel.boneWeightData;
                data.weight *= t;

                if (data.weight > 0f)
                {
                    result.AddChannel(data, true);
                }
            }

            result.UnifyChannelsWithSameBoneIndex();

            if (result.GetWeightSum() > 1f)
            {
                result.NormalizeChannels();
            }

            result.FilterChannels(0f);
            result.ClampChannels(4, true);

            return(result);
        }
Exemple #4
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);
            }
        }