示例#1
0
        /// <summary>
        /// Changes the sprite mesh.
        /// </summary>
        /// <param name="sm">Will replace SpriteMesh</param>
        /// <param name="texture">new Texture.</param>
        /// <param name="mat">Mat.</param>
        public void ChangeSpriteMesh(SpriteMesh sm, Texture texture, Material mat = null, bool useMaterialBlock = true)
        {
            if (!sm || !texture)
            {
                return;
            }

            TextureFrame frame = new TextureFrame();

            frame.material  = mat == null ? sm.material : mat;
            frame.texture   = texture;
            frame.isRotated = false;

            frame.rect.x             = 0;
            frame.rect.y             = 0;
            frame.rect.width         = texture.width;
            frame.rect.height        = texture.height;
            frame.frameSize          = new Rect(0, 0, texture.width, texture.height);
            frame.atlasTextureSize.x = texture.width;
            frame.atlasTextureSize.y = texture.height;
            if (!sm.isCreatedMesh)
            {
                sm.CreateMesh();
            }
            sm.frame = frame;
            if (mat)
            {
                sm.material = mat;
            }

            if (useMaterialBlock)
            {
                sm.render.GetPropertyBlock(materialPropertyBlock);
                m_MatBlock.SetTexture("_MainTex", texture);
                sm.render.SetPropertyBlock(materialPropertyBlock);
            }
            else
            {
                sm.material.mainTexture = texture;
            }
        }
示例#2
0
        static void ShowSpriteMesh(TextureFrame frame, SpineData.SkinAttachment attachmentData, Transform slot, Transform skinParent, SpineArmatureEditor armatureEditor)
        {
            GameObject go = new GameObject(attachmentData.name);
            SpriteMesh sm = go.AddComponent <SpriteMesh>();

            sm.transform.parent = skinParent;

            Vector3 localPos = Vector3.zero;

            localPos.x = attachmentData.x;
            localPos.y = attachmentData.y;
            go.transform.localPosition = localPos;

            Vector3 localSc = Vector3.one;

            localSc.x = attachmentData.scaleX;
            localSc.y = attachmentData.scaleY;
            go.transform.localScale = localSc;

            go.transform.localRotation = Quaternion.Euler(0, 0, attachmentData.rotation);

            Transform[] bones = SetMeshVertex <SpriteMesh>(sm, attachmentData, armatureEditor);
            sm.uvs       = attachmentData.uvs;
            sm.triangles = attachmentData.triangles;
            sm.colors    = new Color[sm.vertices.Length];
            for (int i = 0; i < sm.colors.Length; ++i)
            {
                sm.colors[i] = Color.white;
            }
            if (armatureEditor.genMeshCollider && attachmentData.edges != null)
            {
                sm.edges = attachmentData.edges;
            }
            if (attachmentData.weights != null && attachmentData.weights.Count > 0)
            {
                sm.CreateMesh();
                if (armatureEditor.ffdKV.ContainsKey(attachmentData.textureName))
                {
                    //Vertex controller
                    sm.vertControlTrans = new Transform[sm.vertices.Length];
                    for (int i = 0; i < sm.vertices.Length; ++i)
                    {
                        GameObject gov = new GameObject(go.name + "_v" + i);
                        gov.transform.parent        = go.transform;
                        gov.transform.localPosition = sm.vertices[i];
                        gov.transform.localScale    = Vector3.zero;
                        sm.vertControlTrans[i]      = gov.transform;
                        gov.SetActive(false);
                    }
                }
            }
            else
            {
                sm.CreateMesh();
                //Vertex controller
                sm.vertControlTrans = new Transform[sm.vertices.Length];
                for (int i = 0; i < sm.vertices.Length; ++i)
                {
                    GameObject gov = new GameObject(go.name + "_v" + i);
                    gov.transform.parent        = go.transform;
                    gov.transform.localPosition = sm.vertices[i];
                    gov.transform.localScale    = Vector3.zero;
                    sm.vertControlTrans[i]      = gov.transform;
                    gov.SetActive(false);
                }
            }

            if (attachmentData.weights != null && attachmentData.weights.Count > 0)
            {
                List <Armature.BoneWeightClass> boneWeights = new List <Armature.BoneWeightClass>();
                for (int i = 0; i < attachmentData.weights.Count; ++i)
                {
                    int boneCount = (int)attachmentData.weights[i];                    //骨骼数量
                    List <KeyValuePair <int, float> > boneWeightList = new List <KeyValuePair <int, float> >();
                    for (int j = 0; j < boneCount * 2; j += 2)
                    {
                        int   boneIdx = (int)attachmentData.weights[i + j + 1];
                        float weight  = attachmentData.weights[i + j + 2];
                        boneWeightList.Add(new KeyValuePair <int, float>(boneIdx, weight));
                    }
                    //sort boneWeightList,desc
                    boneWeightList.Sort(delegate(KeyValuePair <int, float> x, KeyValuePair <int, float> y) {
                        if (x.Value == y.Value)
                        {
                            return(0);
                        }
                        return(x.Value < y.Value? 1: -1);
                    });
                    Armature.BoneWeightClass bw = new Armature.BoneWeightClass();
                    for (int j = 0; j < boneWeightList.Count; ++j)
                    {
                        if (j == 0)
                        {
                            bw.boneIndex0 = GlobalBoneIndexToLocalBoneIndex(armatureEditor, boneWeightList[j].Key, bones);
                            bw.weight0    = boneWeightList[j].Value;
                        }
                        else if (j == 1)
                        {
                            bw.boneIndex1 = GlobalBoneIndexToLocalBoneIndex(armatureEditor, boneWeightList[j].Key, bones);
                            bw.weight1    = boneWeightList[j].Value;
                        }
                        else if (j == 2)
                        {
                            bw.boneIndex2 = GlobalBoneIndexToLocalBoneIndex(armatureEditor, boneWeightList[j].Key, bones);
                            bw.weight2    = boneWeightList[j].Value;
                        }
                        else if (j == 3)
                        {
                            bw.boneIndex3 = GlobalBoneIndexToLocalBoneIndex(armatureEditor, boneWeightList[j].Key, bones);
                            bw.weight3    = boneWeightList[j].Value;
                        }
                        else if (j == 4)
                        {
                            bw.boneIndex4 = GlobalBoneIndexToLocalBoneIndex(armatureEditor, boneWeightList[j].Key, bones);
                            bw.weight4    = boneWeightList[j].Value;
                            break;
                        }
                    }
                    boneWeights.Add(bw);
                    i += boneCount * 2;
                }
                Matrix4x4[] matrixArray = new Matrix4x4[bones.Length];
                for (int i = 0; i < matrixArray.Length; ++i)
                {
                    Transform bone = bones[i];
                    matrixArray[i]  = bone.worldToLocalMatrix * armatureEditor.armature.localToWorldMatrix;
                    matrixArray[i] *= Matrix4x4.TRS(slot.localPosition, slot.localRotation, slot.localScale);
                }
                sm.bones     = bones;
                sm.bindposes = matrixArray;
                sm.weights   = boneWeights.ToArray();
            }
            sm.color = attachmentData.color;
            sm.UpdateMesh();
            sm.UpdateVertexColor();
            sm.frame = frame;
        }