예제 #1
0
 private void CopyMeshFromSpriteCache(SpriteCache sprite, SkinningCopySpriteData skinningSpriteData)
 {
     meshTool.SetupSprite(sprite);
     skinningSpriteData.vertices        = meshTool.mesh.vertices;
     skinningSpriteData.indices         = meshTool.mesh.indices;
     skinningSpriteData.edges           = meshTool.mesh.edges;
     skinningSpriteData.boneWeightNames = new List <string>();
     foreach (var bone in meshTool.mesh.bones)
     {
         skinningSpriteData.boneWeightNames.Add(bone.name);
     }
 }
예제 #2
0
        public SkinningCopyData CopySingle()
        {
            var skinningCopyData = new SkinningCopyData();

            skinningCopyData.pixelsPerUnit = pixelsPerUnit;

            BoneCache rootBone;
            var       selectedSprite = skinningCache.selectedSprite;
            var       selection      = skinningCache.skeletonSelection;

            if (selection.Count == 0)
            {
                if (selectedSprite == null || (skinningCache.mode == SkinningMode.SpriteSheet && skinningCache.hasCharacter))
                {
                    return(null);
                }
                var skeleton = skinningCache.GetEffectiveSkeleton(selectedSprite);
                if (skeleton.BoneCount == 0)
                {
                    return(null);
                }

                rootBone = skeleton.bones[0];
            }
            else
            {
                rootBone = selection.root;
            }

            var skinningSpriteData = new SkinningCopySpriteData();

            skinningCopyData.copyData.Add(skinningSpriteData);

            skinningSpriteData.spriteBones = GetSpriteBoneDataRecursively(rootBone);
            if (selectedSprite != null && skinningCache.mode == SkinningMode.Character)
            {
                // Offset the bones based on the currently selected Sprite in Character mode
                var characterPart = selectedSprite.GetCharacterPart();
                if (characterPart != null)
                {
                    var offset         = characterPart.position;
                    var rootSpriteBone = skinningSpriteData.spriteBones[0];
                    rootSpriteBone.position           = rootSpriteBone.position - offset;
                    skinningSpriteData.spriteBones[0] = rootSpriteBone;
                }
            }

            if (selectedSprite != null && meshTool != null)
            {
                CopyMeshFromSpriteCache(selectedSprite, skinningSpriteData);
            }
            return(skinningCopyData);
        }
예제 #3
0
        public SkinningCopyData CopyAll()
        {
            var skinningCopyData = new SkinningCopyData();

            skinningCopyData.pixelsPerUnit = pixelsPerUnit;

            var sprites = skinningCache.GetSprites();

            foreach (var sprite in sprites)
            {
                var skinningSpriteData = new SkinningCopySpriteData();
                skinningSpriteData.spriteName = sprite.name;

                var skeleton = skinningCache.GetEffectiveSkeleton(sprite);
                if (skeleton != null && skeleton.BoneCount > 0)
                {
                    if (skinningCache.hasCharacter)
                    {
                        skinningSpriteData.spriteBones = skeleton.bones.ToSpriteBone(Matrix4x4.identity).ToList();
                    }
                    else
                    {
                        skinningSpriteData.spriteBones = new List <SpriteBone>();
                        var bones = skeleton.bones.FindRoots();
                        foreach (var bone in bones)
                        {
                            GetSpriteBoneDataRecursively(skinningSpriteData.spriteBones, bone);
                        }
                    }
                }
                if (meshTool != null)
                {
                    CopyMeshFromSpriteCache(sprite, skinningSpriteData);
                }
                skinningCopyData.copyData.Add(skinningSpriteData);
            }

            if (meshTool != null)
            {
                meshTool.SetupSprite(null);
            }

            return(skinningCopyData);
        }
예제 #4
0
        public void PasteMesh(SpriteCache sprite, SkinningCopySpriteData skinningSpriteData, bool flipX, bool flipY, float scale, NewBonesStore newBonesStore)
        {
            if (sprite == null)
            {
                return;
            }

            meshTool.SetupSprite(sprite);
            meshTool.mesh.vertices = skinningSpriteData.vertices;
            if (!Mathf.Approximately(scale, 1f) || flipX || flipY)
            {
                var spriteRect = sprite.textureRect;
                foreach (var vertex in meshTool.mesh.vertices)
                {
                    var position = vertex.position;
                    if (!Mathf.Approximately(scale, 1f))
                    {
                        position = position * scale;
                    }
                    if (flipX)
                    {
                        position.x = spriteRect.width - vertex.position.x;
                    }
                    if (flipY)
                    {
                        position.y = spriteRect.height - vertex.position.y;
                    }
                    vertex.position = position;
                }
            }
            meshTool.mesh.indices = skinningSpriteData.indices;
            meshTool.mesh.edges   = skinningSpriteData.edges;

            int[]       copyBoneToNewBones = new int[skinningSpriteData.boneWeightNames.Count];
            BoneCache[] setBones           = null;

            if (newBonesStore != null && newBonesStore.newBones != null)
            {
                // Update bone weights with new bone indices
                var setBonesList = new List <BoneCache>();
                copyBoneToNewBones = new int[skinningSpriteData.boneWeightNames.Count];
                int index = 0;
                for (int i = 0; i < skinningSpriteData.boneWeightNames.Count; ++i)
                {
                    string oldBoneName = skinningSpriteData.boneWeightNames[i];
                    string newBoneName;
                    newBonesStore.newBoneNameDict.TryGetValue(oldBoneName, out newBoneName);
                    var newBone = newBonesStore.newBones.FirstOrDefault(bone => bone.name == newBoneName);
                    copyBoneToNewBones[i] = -1;
                    if (newBone == null)
                    {
                        continue;
                    }

                    for (int j = 0; j < skinningSpriteData.spriteBones.Count; ++j)
                    {
                        if (skinningSpriteData.spriteBones[j].name == oldBoneName)
                        {
                            copyBoneToNewBones[i] = index++;
                            setBonesList.Add(newBone);
                            break;
                        }
                    }
                }
                setBones = setBonesList.ToArray();
            }
            else
            {
                // Attempt to link weights based on existing bone names
                var skeleton       = skinningCache.GetEffectiveSkeleton(sprite);
                var characterBones = new List <BoneCache>();
                for (int i = 0; i < skinningSpriteData.boneWeightNames.Count; ++i)
                {
                    copyBoneToNewBones[i] = -1;
                    var boneName = skinningSpriteData.boneWeightNames[i];
                    for (int j = 0; j < skeleton.bones.Length; ++j)
                    {
                        if (skeleton.bones[j].name == boneName)
                        {
                            copyBoneToNewBones[i] = characterBones.Count;
                            characterBones.Add(skeleton.bones[j]);
                            break;
                        }
                    }
                }
                setBones = characterBones.ToArray();
            }

            // Remap new bone indexes from copied bone indexes
            foreach (var vertex in meshTool.mesh.vertices)
            {
                var editableBoneWeight = vertex.editableBoneWeight;

                for (var i = 0; i < editableBoneWeight.Count; ++i)
                {
                    if (!editableBoneWeight[i].enabled)
                    {
                        continue;
                    }

                    var boneIndex = copyBoneToNewBones[editableBoneWeight[i].boneIndex];
                    if (boneIndex != -1)
                    {
                        editableBoneWeight[i].boneIndex = boneIndex;
                    }
                }
            }

            // Update associated bones for mesh
            meshTool.mesh.SetCompatibleBoneSet(setBones);
            meshTool.mesh.bones = setBones; // Fixes weights for bones that do not exist

            // Update associated bones for character
            if (skinningCache.hasCharacter)
            {
                var characterPart = sprite.GetCharacterPart();
                if (characterPart != null)
                {
                    characterPart.bones = setBones;
                    skinningCache.events.characterPartChanged.Invoke(characterPart);
                }
            }

            meshTool.UpdateMesh();
        }
예제 #5
0
        public SkinningCopyData CopySingle(SpriteCache sprite)
        {
            var skinningCopyData = new SkinningCopyData();

            skinningCopyData.pixelsPerUnit = pixelsPerUnit;

            // Mesh
            var skinningSpriteData = new SkinningCopySpriteData();

            skinningCopyData.copyData.Add(skinningSpriteData);

            CopyMeshFromSpriteCache(sprite, skinningSpriteData);

            // Bones
            var rootBones = new List <BoneCache>();

            if (skinningCache.hasCharacter)
            {
                var characterPart = skinningCache.GetCharacterPart(sprite);
                if (characterPart != null && characterPart.bones != null)
                {
                    var bones = characterPart.bones.FindRoots();
                    foreach (var bone in bones)
                    {
                        rootBones.Add(bone);
                    }
                }
            }
            else
            {
                var skeleton = skinningCache.GetEffectiveSkeleton(sprite);
                if (skeleton != null && skeleton.BoneCount > 0)
                {
                    var bones = skeleton.bones.FindRoots();
                    foreach (var bone in bones)
                    {
                        rootBones.Add(bone);
                    }
                }
            }

            if (rootBones.Count > 0)
            {
                skinningSpriteData.spriteBones = new List <SpriteBone>();
                foreach (var rootBone in rootBones)
                {
                    var rootBoneIndex = skinningSpriteData.spriteBones.Count;
                    GetSpriteBoneDataRecursively(skinningSpriteData.spriteBones, rootBone);
                    if (skinningCache.hasCharacter)
                    {
                        // Offset the bones based on the currently selected Sprite in Character mode
                        var characterPart = sprite.GetCharacterPart();
                        if (characterPart != null)
                        {
                            var offset         = characterPart.position;
                            var rootSpriteBone = skinningSpriteData.spriteBones[rootBoneIndex];
                            rootSpriteBone.position = rootSpriteBone.position - offset;
                            skinningSpriteData.spriteBones[rootBoneIndex] = rootSpriteBone;
                        }
                    }
                }
            }

            return(skinningCopyData);
        }
예제 #6
0
        public void PasteMesh(SpriteCache sprite, SkinningCopySpriteData skinningSpriteData, bool flipX, bool flipY, float scale, BoneCache[] newBones)
        {
            if (sprite == null)
            {
                return;
            }

            meshTool.SetupSprite(sprite);
            meshTool.mesh.vertices = skinningSpriteData.vertices;
            if (!Mathf.Approximately(scale, 1f) || flipX || flipY)
            {
                var spriteRect = sprite.textureRect;
                foreach (var vertex in meshTool.mesh.vertices)
                {
                    var position = vertex.position;
                    if (!Mathf.Approximately(scale, 1f))
                    {
                        position = position * scale;
                    }
                    if (flipX)
                    {
                        position.x = spriteRect.width - vertex.position.x;
                    }
                    if (flipY)
                    {
                        position.y = spriteRect.height - vertex.position.y;
                    }
                    vertex.position = position;
                }
            }
            meshTool.mesh.indices = skinningSpriteData.indices;
            meshTool.mesh.edges   = skinningSpriteData.edges;

            if (newBones != null)
            {
                // Update bone weights with new bone indices
                int[] copyBoneToNewBones = new int[skinningSpriteData.boneWeightNames.Count];
                for (int i = 0; i < skinningSpriteData.boneWeightNames.Count; ++i)
                {
                    copyBoneToNewBones[i] = -1;
                    var boneName = skinningSpriteData.boneWeightNames[i];
                    for (int j = 0; j < skinningSpriteData.spriteBones.Count; ++j)
                    {
                        if (skinningSpriteData.spriteBones[j].name == boneName)
                        {
                            copyBoneToNewBones[i] = j;
                            break;
                        }
                    }
                }

                // Remap new bone indexes from copied bone indexes
                foreach (var vertex in meshTool.mesh.vertices)
                {
                    var editableBoneWeight = vertex.editableBoneWeight;

                    for (var i = 0; i < editableBoneWeight.Count; ++i)
                    {
                        if (!editableBoneWeight[i].enabled)
                        {
                            continue;
                        }

                        var boneIndex = copyBoneToNewBones[editableBoneWeight[i].boneIndex];

                        if (boneIndex != -1)
                        {
                            editableBoneWeight[i].boneIndex = boneIndex;
                        }
                    }
                }

                // Update associated bones for mesh
                meshTool.mesh.SetCompatibleBoneSet(newBones);
                meshTool.mesh.bones = newBones; // Fixes weights for bones that do not exist

                // Update associated bones for character
                if (skinningCache.hasCharacter)
                {
                    var characterPart = sprite.GetCharacterPart();
                    if (characterPart != null)
                    {
                        characterPart.bones = newBones;
                        skinningCache.events.characterPartChanged.Invoke(characterPart);
                    }
                }
            }
            meshTool.UpdateMesh();
        }