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();
        }
 private Vector2 ClampToFrame(Vector2 position)
 {
     return(MathUtility.ClampPositionToRect(position, frame));
 }
Esempio n. 3
0
        private SpriteCache TrySelect(Vector2 mousePosition)
        {
            m_Sprites.Remove(selectedSprite);

            if (selectedSprite != null)
            {
                m_Sprites.Add(selectedSprite);
            }

            var currentSelectedIndex = m_Sprites.FindIndex(x => x == selectedSprite) + 1;
            var notVisiblePart       = skinningCache.hasCharacter && skinningCache.mode == SkinningMode.Character
                ? skinningCache.character.parts.Where(x => !x.isVisible).Select(x => x.sprite) : new SpriteCache[0];

            for (int index = 0; index < m_Sprites.Count; ++index)
            {
                var sprite      = m_Sprites[(currentSelectedIndex + index) % m_Sprites.Count];
                var meshPreview = sprite.GetMeshPreview();
                if (notVisiblePart.Contains(sprite))
                {
                    continue;
                }

                Debug.Assert(meshPreview != null);

                var spritePosition = sprite.GetLocalToWorldMatrixFromMode().MultiplyPoint3x4(Vector3.zero);
                var ray            = new Ray((Vector3)mousePosition - spritePosition + Vector3.back, Vector3.forward);
                var bounds         = meshPreview.mesh.bounds;

                if (sprite.GetMesh().indices.Count >= 3)
                {
                    if (bounds.IntersectRay(ray))
                    {
                        var mesh = sprite.GetMesh();

                        Debug.Assert(mesh != null);

                        var indices = mesh.indices;
                        for (var i = 0; i < indices.Count; i += 3)
                        {
                            var p1 = meshPreview.vertices[indices[i]];
                            var p2 = meshPreview.vertices[indices[i + 1]];
                            var p3 = meshPreview.vertices[indices[i + 2]];

                            if (MathUtility.Intersect(p1, p2, p3, ray))
                            {
                                return(sprite);
                            }
                        }
                    }
                }
                else
                {
                    if (meshPreview.defaultMesh.bounds.IntersectRay(ray))
                    {
                        return(sprite);
                    }
                }
            }

            return(null);
        }