private void SmoothWeights(float value)
        {
            Debug.Assert(selection != null);

            for (int i = 0; i < spriteMeshData.vertexCount; ++i)
            {
                if (selection.Count == 0 && emptySelectionEditsAll ||
                    selection.Count > 0 && selection.Contains(i))
                {
                    var smoothValue = m_SmoothValues[i];

                    if (smoothValue >= maxSmoothIterations)
                    {
                        continue;
                    }

                    m_SmoothValues[i] = Mathf.Clamp(smoothValue + value, 0f, maxSmoothIterations);

                    float        lerpValue = GetLerpValue(m_SmoothValues[i]);
                    int          lerpIndex = GetLerpIndex(m_SmoothValues[i]);
                    BoneWeight[] smoothedBoneWeightsFloor = GetSmoothedBoneWeights(lerpIndex - 1);
                    BoneWeight[] smoothedBoneWeightsCeil  = GetSmoothedBoneWeights(lerpIndex);

                    BoneWeight boneWeight = EditableBoneWeightUtility.Lerp(smoothedBoneWeightsFloor[i], smoothedBoneWeightsCeil[i], lerpValue);
                    spriteMeshData.GetWeight(i).SetFromBoneWeight(boneWeight);
                }
            }
        }
Example #2
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;
                }
            }
        }
        private void SmoothWeights(float value)
        {
            Debug.Assert(selection != null);

            for (int i = 0; i < spriteMeshData.vertices.Count; ++i)
            {
                if (selection.Count == 0 && emptySelectionEditsAll ||
                    selection.Count > 0 && selection.IsSelected(i))
                {
                    m_SmoothValues[i] += value;
                    m_SmoothValues[i]  = Mathf.Clamp(m_SmoothValues[i], 0f, float.MaxValue);

                    float        lerpValue = GetLerpValue(m_SmoothValues[i]);
                    int          lerpIndex = GetLerpIndex(m_SmoothValues[i]);
                    BoneWeight[] smoothedBoneWeightsFloor = GetSmoothedBoneWeights(lerpIndex - 1);
                    BoneWeight[] smoothedBoneWeightsCeil  = GetSmoothedBoneWeights(lerpIndex);

                    BoneWeight boneWeight = EditableBoneWeightUtility.Lerp(smoothedBoneWeightsFloor[i], smoothedBoneWeightsCeil[i], lerpValue);
                    spriteMeshData.vertices[i].editableBoneWeight.SetFromBoneWeight(boneWeight);
                    spriteMeshData.vertices[i].editableBoneWeight.UnifyChannelsWithSameBoneIndex();
                }
            }
        }
Example #4
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);
        }
Example #5
0
 public Vertex2D(Vector2 position, BoneWeight weights)
 {
     m_Position           = position;
     m_EditableBoneWeight = EditableBoneWeightUtility.CreateFromBoneWeight(weights);
 }
Example #6
0
 public Vertex2D(Vector2 position)
 {
     m_Position           = position;
     m_EditableBoneWeight = EditableBoneWeightUtility.CreateFromBoneWeight(new BoneWeight());
 }
        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();
        }