Beispiel #1
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);
        }
Beispiel #2
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);
                }
            }
        }
        public static void SetFromBoneWeight(this EditableBoneWeight editableBoneWeight, BoneWeight boneWeight)
        {
            editableBoneWeight.Clamp(4, false);

            while (editableBoneWeight.Count < 4)
            {
                editableBoneWeight.AddChannel(0, 0f, false);
            }

            for (var i = 0; i < 4; ++i)
            {
                var weight = boneWeight.GetWeight(i);
                editableBoneWeight[i].boneIndex = boneWeight.GetBoneIndex(i);
                editableBoneWeight[i].weight    = weight;
                editableBoneWeight[i].enabled   = weight > 0f;
            }
        }
Beispiel #4
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);
                }
            }
        }
Beispiel #6
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);
                }
            }
        }
        private static void Lerp(EditableBoneWeight first, EditableBoneWeight second, ref EditableBoneWeight result, float t)
        {
            result.Clear();

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

                var weight = channel.weight * (1f - t);

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

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

                var weight = channel.weight * t;

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

            result.UnifyChannelsWithSameBoneIndex();
            result.Clamp(4);

            if (result.Sum() > 1f)
            {
                result.Normalize();
            }

            result.FilterChannels(0f);
        }
Beispiel #8
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);
            }
        }
        protected void PrepareSmoothingBuffers()
        {
            if (m_SmoothValues == null || m_SmoothValues.Length != spriteMeshData.vertexCount)
            {
                m_SmoothValues = new float[spriteMeshData.vertexCount];
            }

            Array.Clear(m_SmoothValues, 0, m_SmoothValues.Length);

            m_SmoothedBoneWeights.Clear();

            BoneWeight[] boneWeights = new BoneWeight[spriteMeshData.vertexCount];

            for (int i = 0; i < spriteMeshData.vertexCount; i++)
            {
                EditableBoneWeight editableBoneWeight = spriteMeshData.GetWeight(i);
                boneWeights[i] = editableBoneWeight.ToBoneWeight(false);
            }

            m_SmoothedBoneWeights.Add(boneWeights);
        }
        public static void Normalize(this EditableBoneWeight editableBoneWeight)
        {
            ValidateChannels(editableBoneWeight);

            var sum = editableBoneWeight.Sum();

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

            var sumInv = 1f / sum;

            for (var i = 0; i < editableBoneWeight.Count; ++i)
            {
                if (editableBoneWeight[i].enabled)
                {
                    editableBoneWeight[i].weight *= sumInv;
                }
            }
        }
Beispiel #11
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);
                }
            }
        }
        public static void CompensateOtherChannels(this EditableBoneWeight editableBoneWeight, int masterChannel)
        {
            ValidateChannels(editableBoneWeight);

            var validChannelCount = 0;
            var sum = 0f;

            for (int i = 0; i < editableBoneWeight.Count; ++i)
            {
                if (i != masterChannel && editableBoneWeight[i].enabled)
                {
                    sum += editableBoneWeight[i].weight;
                    ++validChannelCount;
                }
            }

            if (validChannelCount == 0)
            {
                return;
            }

            var targetSum = 1f - editableBoneWeight[masterChannel].weight;

            for (var i = 0; i < editableBoneWeight.Count; ++i)
            {
                if (i != masterChannel && editableBoneWeight[i].enabled)
                {
                    if (sum == 0f)
                    {
                        editableBoneWeight[i].weight = targetSum / validChannelCount;
                    }
                    else
                    {
                        editableBoneWeight[i].weight *= targetSum / sum;
                    }
                }
            }
        }
Beispiel #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);
                }
            }
        }
Beispiel #14
0
        public void CalculateWeights(IWeightsGenerator weightsGenerator, ISelection <int> selection, float filterTolerance)
        {
            Debug.Assert(spriteMeshData != null);

            Vector2[] controlPoints;
            Edge[]    bones;
            int[]     pins;

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

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

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

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

            Debug.Assert(boneWeights.Length == spriteMeshData.vertexCount);

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

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

                    spriteMeshData.SetWeight(i, editableBoneWeight);
                }
            }
        }
Beispiel #15
0
        private void CreateVertex(Vector2 position, int edgeIndex)
        {
            position = MeshModuleUtility.ClampPositionToRect(position, spriteMeshData.frame);
            undoObject.RegisterCompleteObjectUndo("Create Vertex");

            BoneWeight boneWeight = new BoneWeight();

            Vector3Int indices;
            Vector3    barycentricCoords;

            if (spriteMeshData.FindTriangle(position, out indices, out barycentricCoords))
            {
                EditableBoneWeight bw1 = spriteMeshData.vertices[indices.x].editableBoneWeight;
                EditableBoneWeight bw2 = spriteMeshData.vertices[indices.y].editableBoneWeight;
                EditableBoneWeight bw3 = spriteMeshData.vertices[indices.z].editableBoneWeight;

                EditableBoneWeight result = new EditableBoneWeight();

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

                    BoneWeightData data = channel.boneWeightData;
                    data.weight *= barycentricCoords.x;

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

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

                    BoneWeightData data = channel.boneWeightData;
                    data.weight *= barycentricCoords.y;

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

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

                    BoneWeightData data = channel.boneWeightData;
                    data.weight *= barycentricCoords.z;

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

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

                boneWeight = result.ToBoneWeight(true);
            }
            else if (edgeIndex != -1)
            {
                Edge    edge = spriteMeshData.edges[edgeIndex];
                Vector2 pos1 = spriteMeshData.vertices[edge.index1].position;
                Vector2 pos2 = spriteMeshData.vertices[edge.index2].position;
                Vector2 dir1 = (position - pos1);
                Vector2 dir2 = (pos2 - pos1);
                float   t    = Vector2.Dot(dir1, dir2.normalized) / dir2.magnitude;
                t = Mathf.Clamp01(t);
                BoneWeight bw1 = spriteMeshData.vertices[edge.index1].editableBoneWeight.ToBoneWeight(true);
                BoneWeight bw2 = spriteMeshData.vertices[edge.index2].editableBoneWeight.ToBoneWeight(true);

                boneWeight = EditableBoneWeightUtility.Lerp(bw1, bw2, t);
            }

            spriteMeshData.CreateVertex(position, edgeIndex);
            spriteMeshData.vertices[spriteMeshData.vertices.Count - 1].editableBoneWeight.SetFromBoneWeight(boneWeight);
            spriteMeshData.Triangulate(triangulator);
        }
 public void SetWeight(int index, EditableBoneWeight weight)
 {
     m_Vertices[index].editableBoneWeight = weight;
 }
        private void CreateVertex(Vector2 position, int edgeIndex)
        {
            position = MathUtility.ClampPositionToRect(position, frame);
            cacheUndo.BeginUndoOperation(TextContent.createVertex);

            BoneWeight boneWeight = new BoneWeight();

            Vector3Int indices;
            Vector3    barycentricCoords;

            if (m_SpriteMeshDataController.FindTriangle(position, out indices, out barycentricCoords))
            {
                EditableBoneWeight bw1 = m_SpriteMeshData.GetWeight(indices.x);
                EditableBoneWeight bw2 = m_SpriteMeshData.GetWeight(indices.y);
                EditableBoneWeight bw3 = m_SpriteMeshData.GetWeight(indices.z);

                EditableBoneWeight result = new EditableBoneWeight();

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

                    var weight = channel.weight * barycentricCoords.x;

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

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

                    var weight = channel.weight * barycentricCoords.y;

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

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

                    var weight = channel.weight * barycentricCoords.z;

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

                result.UnifyChannelsWithSameBoneIndex();
                result.FilterChannels(0f);
                result.Clamp(4, true);

                boneWeight = result.ToBoneWeight(true);
            }
            else if (edgeIndex != -1)
            {
                Edge    edge = m_SpriteMeshData.edges[edgeIndex];
                Vector2 pos1 = m_SpriteMeshData.GetPosition(edge.index1);
                Vector2 pos2 = m_SpriteMeshData.GetPosition(edge.index2);
                Vector2 dir1 = (position - pos1);
                Vector2 dir2 = (pos2 - pos1);
                float   t    = Vector2.Dot(dir1, dir2.normalized) / dir2.magnitude;
                t = Mathf.Clamp01(t);
                BoneWeight bw1 = m_SpriteMeshData.GetWeight(edge.index1).ToBoneWeight(true);
                BoneWeight bw2 = m_SpriteMeshData.GetWeight(edge.index2).ToBoneWeight(true);

                boneWeight = EditableBoneWeightUtility.Lerp(bw1, bw2, t);
            }

            m_SpriteMeshDataController.CreateVertex(position, edgeIndex);
            m_SpriteMeshData.GetWeight(m_SpriteMeshData.vertexCount - 1).SetFromBoneWeight(boneWeight);
            Triangulate();
        }
Beispiel #18
0
 public static bool HasBoneIndex(this EditableBoneWeight editableBoneWeight, int boneIndex)
 {
     return(GetChannelFromBoneIndex(editableBoneWeight, boneIndex) > -1);
 }