private static void ParseEvents(SpineArmatureEditor armatureEditor, Bones2D.JSONClass armtureObj) { if (armtureObj.ContainKey("events")) { Bones2D.JSONClass eventsObj = armtureObj["events"].AsObject; foreach (string name in eventsObj.GetKeys()) { Bones2D.JSONClass evtObj = eventsObj[name].AsObject; EventData evtData = new EventData(); if (evtObj.ContainKey("string")) { evtData.stringParam = evtObj["string"].ToString(); } if (evtObj.ContainKey("int")) { evtData.intParam = evtObj["int"].AsInt; } if (evtObj.ContainKey("float")) { evtData.floatParam = evtObj["float"].AsFloat; } eventPoseKV[name] = evtData; } } }
private static SpineData.AnimationBoneData ParseBoneAnimTimeline(SpineArmatureEditor armatureEditor, string boneName, Bones2D.JSONClass bonesAnimObj, string animType) { Bones2D.JSONArray animObjArr = bonesAnimObj[animType].AsArray; SpineData.AnimationBoneData spineAnimBoneData = new SpineData.AnimationBoneData(); spineAnimBoneData.name = boneName; spineAnimBoneData.timelines = new SpineData.BoneTimeline[animObjArr.Count]; for (int i = 0; i < animObjArr.Count; ++i) { Bones2D.JSONClass animObj = animObjArr[i].AsObject; SpineData.BoneTimeline timeline = new SpineData.BoneTimeline(); timeline.type = animType; spineAnimBoneData.timelines[i] = timeline; if (animObj.ContainKey("time")) { timeline.time = animObj["time"].AsFloat; } //The bone's rotation relative to the setup pose if (animObj.ContainKey("angle")) { timeline.angle = animObj["angle"].AsFloat; } //The bone's x,y relative to the setup pose if (animObj.ContainKey("x")) { timeline.x = animObj["x"].AsFloat; if (animType == "translate") { timeline.x *= armatureEditor.unit; } } if (animObj.ContainKey("y")) { timeline.y = animObj["y"].AsFloat; if (animType == "translate") { timeline.y *= armatureEditor.unit; } } if (animObj.ContainKey("curve")) { if (animObj["curve"] == "stepped") { timeline.tweenEasing = "stepped"; } else if (animObj["curve"] == "linear") { //default } else { timeline.curve = ConvertJsonArrayToFloatArr(animObj["curve"].AsArray); } } } return(spineAnimBoneData); }
public static void ParseAnimJsonData(SpineArmatureEditor armatureEditor) { string str = armatureEditor.animTextAsset.text.Replace("null", "\"null\""); Bones2D.JSONClass json = Bones2D.JSON.Parse(str.Replace("/", "_")).AsObject; armatureEditor.armatureData = new SpineData.ArmatureData(); GameObject go = new GameObject(armatureEditor.animTextAsset.name); Armature armature = go.AddComponent <Armature>(); armature.isUGUI = armatureEditor.isUGUI; armatureEditor.armature = go.transform; armatureEditor.bonesKV.Clear(); armatureEditor.slotsKV.Clear(); armatureEditor.bonesDataKV.Clear(); armatureEditor.slotsDataKV.Clear(); armatureEditor.slots.Clear(); armatureEditor.bones.Clear(); armatureEditor.ffdKV.Clear(); armatureEditor.animList.Clear(); eventPoseKV = new Dictionary <string, EventData>(); ParseArmtureData(armatureEditor, json); armatureEditor.InitShow(); }
public static void ParseTextureAtlas(SpineArmatureEditor armatureEditor, Texture2D texture, string atlasText) { using (TextReader reader = new StringReader(atlasText)) { int index = 0; string pngName = null; while (reader.Peek() != -1) { string line = reader.ReadLine().Trim(); if (line.Length > 0) { if (line.LastIndexOf(".png") > -1 || line.LastIndexOf(".PNG") > -1) { if (line.Contains(texture.name)) { pngName = line; } else { pngName = null; } } if (pngName != null && line.IndexOf(":") == -1) { SpineArmatureEditor.Atlas atlas = new SpineArmatureEditor.Atlas(); atlas.texture = texture; atlas.atlasText = atlasText; armatureEditor.atlasKV[line] = atlas; } } ++index; } reader.Close(); } }
public static void SetIKs(SpineArmatureEditor armatureEditor) { if (armatureEditor.armatureData.iks != null) { int len = armatureEditor.armatureData.iks.Length; for (int i = 0; i < len; ++i) { SpineData.IKData ikData = armatureEditor.armatureData.iks[i]; Transform targetIK = armatureEditor.bonesKV[ikData.target]; Transform startBone = armatureEditor.bonesKV[ikData.bones[0]]; SpineData.BoneData endBoneData = armatureEditor.bonesDataKV[ikData.bones[ikData.bones.Length - 1]]; Transform endBone = armatureEditor.bonesKV[ikData.bones[ikData.bones.Length - 1]]; BoneIK bi = startBone.gameObject.AddComponent <BoneIK>(); Vector3 v = Vector3.right * endBoneData.length * armatureEditor.unit; v = endBone.TransformPoint(v); GameObject go = new GameObject(ikData.name); go.transform.parent = endBone; go.transform.position = v; go.transform.localRotation = Quaternion.identity; go.transform.localScale = Vector3.zero; bi.damping = ikData.mix; bi.endTransform = go.transform; bi.targetIK = targetIK; bi.iterations = 20; bi.bendPositive = ikData.bendPositive; bi.rootBone = armatureEditor.armature; } } }
private static List <SpineData.AnimationBoneData> ParseAnimBones(SpineArmatureEditor armatureEditor, Bones2D.JSONClass bonesObj) { List <SpineData.AnimationBoneData> animationBoneDatas = new List <SpineData.AnimationBoneData>(); foreach (string boneName in bonesObj.GetKeys()) { Bones2D.JSONClass bonesAnimObj = bonesObj[boneName].AsObject; if (bonesAnimObj.ContainKey("rotate")) { animationBoneDatas.Add(ParseBoneAnimTimeline(armatureEditor, boneName, bonesAnimObj, "rotate")); } if (bonesAnimObj.ContainKey("translate")) { animationBoneDatas.Add(ParseBoneAnimTimeline(armatureEditor, boneName, bonesAnimObj, "translate")); } if (bonesAnimObj.ContainKey("scale")) { animationBoneDatas.Add(ParseBoneAnimTimeline(armatureEditor, boneName, bonesAnimObj, "scale")); } if (bonesAnimObj.ContainKey("shear")) { animationBoneDatas.Add(ParseBoneAnimTimeline(armatureEditor, boneName, bonesAnimObj, "shear")); } } return(animationBoneDatas); }
private static void ParseIKs(SpineArmatureEditor armatureEditor, Bones2D.JSONClass armtureObj) { if (armtureObj.ContainKey("ik")) { Bones2D.JSONArray iks = armtureObj["ik"].AsArray; SpineData.IKData[] ikDatas = new SpineData.IKData[iks.Count]; for (int i = 0; i < iks.Count; ++i) { Bones2D.JSONClass ikObj = iks[i].AsObject; SpineData.IKData ikData = new SpineData.IKData(); ikData.name = ikObj["name"].ToString(); ikData.target = ikObj["target"].ToString(); if (ikObj.ContainKey("bones")) { Bones2D.JSONArray bones = ikObj["bones"].AsArray; ikData.bones = new string[bones.Count]; for (int j = 0; j < bones.Count; ++j) { ikData.bones[j] = bones[j]; } } if (ikObj.ContainKey("mix")) { ikData.mix = ikObj["mix"].AsFloat; } if (ikObj.ContainKey("bendPositive")) { ikData.bendPositive = ikObj["bendPositive"].AsBool; } ikDatas[i] = ikData; } armatureEditor.armatureData.iks = ikDatas; } }
static void CreateWizard() { SpineArmatureEditor editor = ScriptableWizard.DisplayWizard <SpineArmatureEditor>("Create Spine", "Create"); editor.minSize = new Vector2(200, 500); if (Selection.activeObject != null) { string dirPath = AssetDatabase.GetAssetOrScenePath(Selection.activeObject); if (File.Exists(dirPath)) { dirPath = dirPath.Substring(0, dirPath.LastIndexOf("/")); } if (Directory.Exists(dirPath)) { string animJsonPath = null, textureFilePath = null; List <string> texturePaths = new List <string>(); foreach (string path in Directory.GetFiles(dirPath)) { if (path.IndexOf(".atlas") > -1 && path.LastIndexOf(".meta") == -1) { textureFilePath = path; continue; } if (path.IndexOf(".png") > -1 && path.LastIndexOf(".meta") == -1) { texturePaths.Add(path); continue; } if (path.IndexOf(".json") > -1 && path.LastIndexOf(".meta") == -1) { animJsonPath = path; } } string texturePath = null; if (texturePaths.Count > 0) { texturePath = texturePaths[0]; } if (!string.IsNullOrEmpty(animJsonPath) && !string.IsNullOrEmpty(texturePath) && !string.IsNullOrEmpty(textureFilePath)) { editor.altasTextAsset = LoadAtlas(Application.dataPath + "/" + textureFilePath.Substring(6)); editor.altasTexture = AssetDatabase.LoadAssetAtPath <Texture2D>(texturePath); editor.animTextAsset = AssetDatabase.LoadAssetAtPath <TextAsset>(animJsonPath); if (texturePaths.Count > 1) { editor.otherTextures = new Atlas[texturePaths.Count - 1]; } for (int i = 1; i < texturePaths.Count; ++i) { Atlas atlas = new Atlas(); atlas.atlasText = editor.altasTextAsset; atlas.texture = AssetDatabase.LoadAssetAtPath <Texture2D>(texturePaths[i]); editor.otherTextures[i - 1] = atlas; } } } } }
private static void ParseArmtureData(SpineArmatureEditor armatureEditor, Bones2D.JSONClass armtureObj) { ParseBones(armatureEditor, armtureObj); ParseSlots(armatureEditor, armtureObj); ParseIKs(armatureEditor, armtureObj); ParseSkins(armatureEditor, armtureObj); ParseEvents(armatureEditor, armtureObj); ParseAnimations(armatureEditor, armtureObj); }
public static void ShowSlots(SpineArmatureEditor armatureEditor) { if (armatureEditor.genericAnim || armatureEditor.isUGUI) { GameObject rootSlot = new GameObject("slots"); m_rootSlot = rootSlot.transform; m_rootSlot.SetParent(armatureEditor.armature); m_rootSlot.localScale = Vector3.one; m_rootSlot.localPosition = Vector3.zero; } foreach (Transform s in armatureEditor.slotsKV.Values) { Slot slot = s.GetComponent <Slot>(); SpineData.SlotData slotData = armatureEditor.slotsDataKV[s.name]; if (!string.IsNullOrEmpty(slotData.bone)) { if (armatureEditor.bonesKV.ContainsKey(slotData.bone)) { Transform parent = armatureEditor.bonesKV[slotData.bone]; if (m_rootSlot) { s.transform.parent = m_rootSlot; GameObject go = new GameObject(s.name); go.transform.parent = parent.transform; go.transform.localScale = Vector3.one; if (armatureEditor.isUGUI) { go.transform.localPosition = Vector3.zero; } else { go.transform.localPosition = new Vector3(0, 0, -slot.zOrder * armatureEditor.zoffset); } go.transform.localEulerAngles = Vector3.zero; slot.inheritSlot = go.transform; } else { s.transform.parent = parent.transform; } } } s.transform.localScale = Vector3.one; if (armatureEditor.isUGUI) { s.localPosition = Vector3.zero; } else { s.localPosition = new Vector3(0, 0, -slot.zOrder * armatureEditor.zoffset); } s.transform.localEulerAngles = Vector3.zero; } }
static void CreateSpineByDir_UnityImage() { SpineArmatureEditor editor = CreateSpineByDir(Bone2DSetupEditor.DisplayType.UGUIImage); if (editor) { editor.OnWizardCreate(); DestroyImmediate(editor); } }
static void CreateSpineByDir_SpriteFrame() { SpineArmatureEditor editor = CreateSpineByDir(Bone2DSetupEditor.DisplayType.Default); if (editor) { editor.OnWizardCreate(); DestroyImmediate(editor); } }
private static void ParseAnimations(SpineArmatureEditor armatureEditor, Bones2D.JSONClass armtureObj) { if (armtureObj.ContainKey("animations")) { Bones2D.JSONClass anims = armtureObj["animations"].AsObject; List <SpineData.AnimationData> animList = new List <SpineData.AnimationData>(); foreach (string animName in anims.GetKeys()) { SpineData.AnimationData animData = new SpineData.AnimationData(); animData.name = animName; animList.Add(animData); Bones2D.JSONClass animObj = anims[animName].AsObject; if (animObj.ContainKey("bones")) { Bones2D.JSONClass bonesObj = animObj["bones"].AsObject; animData.boneAnims = ParseAnimBones(armatureEditor, bonesObj).ToArray(); } if (animObj.ContainKey("slots")) { Bones2D.JSONClass slotsObj = animObj["slots"].AsObject; animData.slotAnims = ParseAnimSlots(slotsObj).ToArray(); } if (animObj.ContainKey("ik")) { } if (animObj.ContainKey("deform")) { Bones2D.JSONClass deformObj = animObj["deform"].AsObject; animData.deforms = ParseAnimDeform(armatureEditor, deformObj).ToArray(); } else if (animObj.ContainKey("ffd")) { Bones2D.JSONClass deformObj = animObj["ffd"].AsObject; animData.deforms = ParseAnimDeform(armatureEditor, deformObj).ToArray(); } if (animObj.ContainKey("events")) { Bones2D.JSONArray eventsArray = animObj["events"].AsArray; animData.events = ParseAnimEvents(eventsArray).ToArray(); } if (animObj.ContainKey("drawOrder")) { Bones2D.JSONArray drawOrderArray = animObj["drawOrder"].AsArray; animData.drawOrders = ParseDrawOrders(armatureEditor, drawOrderArray).ToArray(); } else if (animObj.ContainKey("draworder")) { Bones2D.JSONArray drawOrderArray = animObj["draworder"].AsArray; animData.drawOrders = ParseDrawOrders(armatureEditor, drawOrderArray).ToArray(); } } armatureEditor.armatureData.animations = animList.ToArray(); } }
static string[] GetTransformPaths(SpineArmatureEditor armatureEditor) { List <string> result = new List <string>(); result.Add(""); foreach (Transform t in armatureEditor.bones) { string path = AnimationUtility.CalculateTransformPath(t, armatureEditor.armature); result.Add(path); } return(result.ToArray()); }
public static void ShowBones(SpineArmatureEditor armatureEditor) { foreach (Transform b in armatureEditor.bonesKV.Values) { SpineData.BoneData boneData = armatureEditor.bonesDataKV[b.name]; if (!string.IsNullOrEmpty(boneData.parent)) { if (armatureEditor.bonesKV.ContainsKey(boneData.parent)) { Transform parent = armatureEditor.bonesKV[boneData.parent]; b.transform.parent = parent.transform; } } else { b.transform.parent = armatureEditor.armature; } b.transform.localPosition = new Vector3(boneData.x, boneData.y, 0f); b.transform.localScale = new Vector3(boneData.scaleX, boneData.scaleY, 1f); b.transform.localRotation = Quaternion.Euler(0f, 0f, boneData.rotation); GameObject inhertGo = null; Bone myBone = null; if (!boneData.inheritRotation) { inhertGo = new GameObject("_" + boneData.name); inhertGo.transform.parent = armatureEditor.armature; inhertGo.transform.localPosition = b.transform.localPosition; inhertGo.transform.localRotation = b.transform.localRotation; inhertGo.transform.localScale = b.transform.localScale; myBone = b.gameObject.AddComponent <Bone>(); myBone.inheritRotation = inhertGo.transform; inhertGo.hideFlags = HideFlags.NotEditable; } if (!boneData.inheritScale) { if (inhertGo == null) { inhertGo = new GameObject("_" + boneData.name); inhertGo.transform.parent = armatureEditor.armature; inhertGo.transform.localPosition = b.transform.localPosition; inhertGo.transform.localRotation = b.transform.localRotation; inhertGo.transform.localScale = b.transform.localScale; inhertGo.hideFlags = HideFlags.NotEditable; myBone = b.gameObject.AddComponent <Bones2D.Bone>(); } myBone.inheritScale = inhertGo.transform; } } }
static int GlobalBoneIndexToLocalBoneIndex(SpineArmatureEditor armatureEditor, int globalBoneIndex, Transform[] localBones) { Transform globalBone = armatureEditor.bones[globalBoneIndex]; int len = localBones.Length; for (int i = 0; i < len; ++i) { if (localBones[i] == globalBone) { return(i); } } return(globalBoneIndex); }
static string GetNodeRelativePath(SpineArmatureEditor armatureEditor, Transform node) { List <string> path = new List <string>(); while (node != armatureEditor.armature) { path.Add(node.name); node = node.parent; } string result = ""; for (int i = path.Count - 1; i >= 0; i--) { result += path[i] + "/"; } return(result.Substring(0, result.Length - 1)); }
static void CreateAvatar(SpineArmatureEditor armatureEditor, Animator animator, string path) { Avatar avatar = AvatarBuilder.BuildGenericAvatar(armatureEditor.armature.gameObject, ""); animator.avatar = avatar; AvatarMask avatarMask = new AvatarMask(); string[] transofrmPaths = GetTransformPaths(armatureEditor); avatarMask.transformCount = transofrmPaths.Length; for (int i = 0; i < transofrmPaths.Length; i++) { avatarMask.SetTransformPath(i, transofrmPaths[i]); avatarMask.SetTransformActive(i, true); } AssetDatabase.CreateAsset(avatar, path + "/" + armatureEditor.armature.name + "Avatar.asset"); AssetDatabase.CreateAsset(avatarMask, path + "/" + armatureEditor.armature.name + "Mask.asset"); }
private static SpineData.SkinAttachment GetSkinAttachment(SpineArmatureEditor armatureEditor, string skinName, string slotname, string attchmentname) { foreach (var skin in armatureEditor.armatureData.skins) { if (skin.skinName == skinName) { foreach (var att in skin.slots[slotname]) { if (att.name == attchmentname) { return(att); } } break; } } return(null); }
private static List <SpineData.AnimationDeformData> ParseAnimDeform(SpineArmatureEditor armatureEditor, Bones2D.JSONClass deformObj) { List <SpineData.AnimationDeformData> animationDeformDatas = new List <SpineData.AnimationDeformData>(); foreach (string skinName in deformObj.GetKeys()) { Bones2D.JSONClass skinObj = deformObj[skinName].AsObject; foreach (string slotName in skinObj.GetKeys()) { Bones2D.JSONClass slotObj = skinObj[slotName].AsObject; foreach (string attachmentName in slotObj.GetKeys()) { animationDeformDatas.Add(ParseDeformAnimTimeline(armatureEditor, skinName, slotName, attachmentName, slotObj[attachmentName].AsArray)); } } } return(animationDeformDatas); }
private static List <SpineData.AnimationDrawOrderData> ParseDrawOrders(SpineArmatureEditor armatureEditor, Bones2D.JSONArray drawOrdersArray) { List <SpineData.AnimationDrawOrderData> animDrawOrders = new List <SpineData.AnimationDrawOrderData>(); if (drawOrdersArray != null && drawOrdersArray.Count > 0) { for (int i = 0; i < drawOrdersArray.Count; ++i) { Bones2D.JSONClass drawOrderObj = drawOrdersArray[i].AsObject; SpineData.AnimationDrawOrderData orderData = new SpineData.AnimationDrawOrderData(); orderData.time = drawOrderObj["time"].AsFloat; if (drawOrderObj.ContainKey("offsets")) { Bones2D.JSONArray offsetsObj = drawOrderObj["offsets"].AsArray; orderData.offsets = new SpineData.DrawOrderOffset[offsetsObj.Count]; for (int j = 0; j < offsetsObj.Count; ++j) { Bones2D.JSONClass offsetObj = offsetsObj[j].AsObject; SpineData.DrawOrderOffset offset = new SpineData.DrawOrderOffset(); offset.slotName = offsetObj["slot"].ToString(); if (offsetObj.ContainKey("offset")) { offset.offset = offsetObj["offset"].AsInt; //the last offset is 0 if (offset.offset == 0) { int lastSlotIdx = armatureEditor.slotsDataKV[offset.slotName].index; offset.slotName = armatureEditor.armatureData.slots[lastSlotIdx + 1].name; offset.offset = -offsetsObj.Count; } } orderData.offsets[j] = offset; } } animDrawOrders.Add(orderData); } } return(animDrawOrders); }
public static void AddBones(SpineArmatureEditor armatureEditor) { m_rootBone = null; if (armatureEditor.armatureData.bones != null) { armatureEditor.bonesKV.Clear(); armatureEditor.bones.Clear(); int len = armatureEditor.armatureData.bones.Length; for (int i = 0; i < len; ++i) { SpineData.BoneData boneData = armatureEditor.armatureData.bones[i]; GameObject go = new GameObject(boneData.name); armatureEditor.bonesKV[boneData.name] = go.transform; if (m_rootBone == null) { m_rootBone = go.transform; } armatureEditor.bones.Add(go.transform); } } }
private static void ParseSlots(SpineArmatureEditor armatureEditor, Bones2D.JSONClass armtureObj) { if (armtureObj.ContainKey("slots")) { Bones2D.JSONArray slots = armtureObj["slots"].AsArray; SpineData.SlotData[] slotDatas = new SpineData.SlotData[slots.Count]; for (int i = 0; i < slots.Count; ++i) { Bones2D.JSONClass slotObj = slots[i].AsObject; SpineData.SlotData slotData = new SpineData.SlotData(); slotData.displayIndex = i; if (slotObj.ContainKey("name")) { slotData.name = slotObj["name"].ToString(); } if (slotObj.ContainKey("bone")) { slotData.bone = slotObj["bone"].ToString(); } if (slotObj.ContainKey("color")) { slotData.color = SpineArmatureEditor.HexToColor(slotObj["color"].ToString()); } if (slotObj.ContainKey("attachment")) { slotData.attachment = slotObj["attachment"].ToString(); } if (slotObj.ContainKey("blend")) { slotData.blend = slotObj["blend"].ToString(); } slotData.index = i; slotDatas[i] = slotData; armatureEditor.slotsDataKV[slotData.name] = slotData; } armatureEditor.armatureData.slots = slotDatas; } }
public static void AddSlot(SpineArmatureEditor armatureEditor) { m_rootSlot = null; if (armatureEditor.armatureData.slots != null) { armatureEditor.slotsKV.Clear(); int len = armatureEditor.armatureData.slots.Length; Armature armature = armatureEditor.armature.GetComponent <Armature>(); for (int i = 0; i < len; ++i) { SpineData.SlotData slotData = armatureEditor.armatureData.slots[i]; GameObject go = new GameObject(slotData.name); armatureEditor.slotsKV[slotData.name] = go.transform; Slot slot = go.AddComponent <Slot>(); slot.zOrder = i; slot.armature = armature; slot.blendMode = slot.ConvertBlendMode(slotData.blend.ToLower()); armatureEditor.slots.Add(slot); slot.color = slotData.color; } } }
/// <summary> /// set events /// </summary> static void CreateAnimEvent(SpineArmatureEditor armatureEditor, AnimationClip clip, SpineData.AnimationEventsData[] eventDatas) { if (eventDatas == null || eventDatas.Length == 0) { return; } if (armatureEditor.armature.gameObject.GetComponent <AnimEvent>() == null) { armatureEditor.armature.gameObject.AddComponent <AnimEvent>(); } List <AnimationEvent> evts = new List <AnimationEvent>(); foreach (SpineData.AnimationEventsData animEvtData in eventDatas) { AnimationEvent ae = new AnimationEvent(); ae.messageOptions = SendMessageOptions.DontRequireReceiver; string param = animEvtData.name + "$"; if (!string.IsNullOrEmpty(animEvtData.stringParam)) { param += animEvtData.stringParam; } ae.functionName = "OnAnimEvent"; ae.time = animEvtData.time; ae.stringParameter = param; ae.intParameter = animEvtData.intParam; ae.floatParameter = animEvtData.floatParam; evts.Add(ae); } if (evts.Count > 0) { AnimationUtility.SetAnimationEvents(clip, evts.ToArray()); } }
private static void CreateBonePose(SpineArmatureEditor armatureEditor) { if (armatureEditor.bonePoseKV == null) { armatureEditor.bonePoseKV = new Dictionary <string, BoneMatrix2D> (); for (int i = 0; i < armatureEditor.armatureData.bones.Length; ++i) { SpineData.BoneData boneData = armatureEditor.armatureData.bones [i]; BoneMatrix2D matrix = new BoneMatrix2D(); matrix.Rotate(boneData.rotation); matrix.Scale(boneData.scaleX, boneData.scaleY); matrix.Translate(boneData.x, boneData.y); if (!string.IsNullOrEmpty(boneData.parent)) { SpineData.BoneData parentBone = armatureEditor.bonesDataKV[boneData.parent]; if (parentBone != null && armatureEditor.bonePoseKV.ContainsKey(parentBone.name)) { matrix.Concat(armatureEditor.bonePoseKV [parentBone.name]); } } armatureEditor.bonePoseKV [boneData.name] = matrix; } } }
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; }
//return current mesh bones static Transform[] SetMeshVertex <T>(T sm, SpineData.SkinAttachment attachmentData, SpineArmatureEditor armatureEditor) { if (attachmentData.weights != null && attachmentData.weights.Count > 0) { List <Vector3> verticesList = new List <Vector3>(); List <float> weights = new List <float>(); List <Transform> bones = new List <Transform>(); Dictionary <int, bool> bonesKV = new Dictionary <int, bool>(); for (int i = 0; i < attachmentData.weights.Count; ++i) { int boneCount = (int)attachmentData.weights[i]; weights.Add(boneCount); Vector3 v = Vector3.zero; for (int j = 0; j < boneCount * 4; j += 4) { int boneIdx = (int)attachmentData.weights[i + j + 1]; float vx = attachmentData.weights[i + j + 2]; float vy = attachmentData.weights[i + j + 3]; float weight = attachmentData.weights[i + j + 4]; weights.Add(boneIdx); weights.Add(weight); //convert vertex Vector3 tempP = new Vector3(vx, vy, 0f); Transform bone = armatureEditor.bones[boneIdx]; tempP = bone.TransformPoint(tempP); v.x += tempP.x * weight; v.y += tempP.y * weight; if (!bonesKV.ContainsKey(boneIdx)) { bones.Add(armatureEditor.bones[boneIdx]); bonesKV[boneIdx] = true; } } verticesList.Add(v); i += boneCount * 4; } attachmentData.vertices = verticesList.ToArray(); if (sm is SpriteMesh) { (sm as SpriteMesh).vertices = attachmentData.vertices; } else if (sm is UIMesh) { (sm as UIMesh).vertices = attachmentData.vertices; } attachmentData.weights = weights; return(bones.ToArray()); } else { if (sm is SpriteMesh) { (sm as SpriteMesh).vertices = attachmentData.vertices; } else if (sm is UIMesh) { (sm as UIMesh).vertices = attachmentData.vertices; } } return(null); }
private static void ShowSpriteFrame(TextureFrame frame, SpineData.SkinAttachment attachmentData, Transform slot, Transform skinParent, SpineArmatureEditor armatureEditor) { GameObject go = new GameObject(); SpriteFrame newFrame = go.AddComponent <SpriteFrame>(); newFrame.CreateQuad(); newFrame.textureFrames = armatureEditor.m_TextureFrames; newFrame.frame = frame; newFrame.name = attachmentData.name; newFrame.pivot = Vector2.one * 0.5f; newFrame.transform.parent = skinParent; Vector3 localPos = Vector3.zero; localPos.x = attachmentData.x; localPos.y = attachmentData.y; newFrame.transform.localPosition = localPos; Vector3 localSc = Vector3.one; localSc.x = attachmentData.scaleX; localSc.y = attachmentData.scaleY; if (newFrame.frame.isRotated) { if (attachmentData.width > 0 && frame.rect.height > 0) { localSc.x *= attachmentData.width / frame.rect.height; } if (attachmentData.height > 0 && frame.rect.width > 0) { localSc.y *= attachmentData.height / frame.rect.width; } } else { if (attachmentData.width > 0 && frame.rect.width > 0) { localSc.x *= attachmentData.width / frame.rect.width; } if (attachmentData.height > 0 && frame.rect.height > 0) { localSc.y *= attachmentData.height / frame.rect.height; } } newFrame.transform.localScale = localSc; newFrame.color = attachmentData.color; newFrame.transform.localRotation = Quaternion.Euler(0, 0, attachmentData.rotation); if (armatureEditor.genImgCollider) { BoxCollider2D collider = newFrame.gameObject.AddComponent <BoxCollider2D>(); if (newFrame.frame.isRotated) { collider.size = new Vector2(newFrame.frame.rect.size.y, newFrame.frame.rect.size.x) * armatureEditor.unit; Vector2 center = new Vector2( -newFrame.frame.frameSize.width / 2 - newFrame.frame.frameSize.x + newFrame.frame.rect.width / 2, newFrame.frame.frameSize.height / 2 + newFrame.frame.frameSize.y - newFrame.frame.rect.height / 2); collider.offset = center * armatureEditor.unit; } else { collider.size = newFrame.frame.rect.size * armatureEditor.unit; Vector2 center = new Vector2( -newFrame.frame.frameSize.width / 2 - newFrame.frame.frameSize.x + newFrame.frame.rect.width / 2, newFrame.frame.frameSize.height / 2 + newFrame.frame.frameSize.y - newFrame.frame.rect.height / 2); collider.offset = center * armatureEditor.unit; } } newFrame.UpdateVertexColor(); }
static void ShowUIFrame(TextureFrame frame, SpineData.SkinAttachment attachmentData, Transform slot, Transform skinParent, SpineArmatureEditor armatureEditor, SpineData.SlotData slotData) { GameObject go = new GameObject(); UIFrame newFrame = go.AddComponent <UIFrame>(); newFrame.raycastTarget = false; newFrame.GetComponent <Graphic>().raycastTarget = false; newFrame.CreateQuad(); newFrame.frame = frame; newFrame.name = attachmentData.textureName; newFrame.transform.SetParent(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; if (newFrame.frame.isRotated) { if (attachmentData.width > 0) { localSc.x *= attachmentData.width / frame.rect.height; } if (attachmentData.height > 0) { localSc.y *= attachmentData.height / frame.rect.width; } } else { if (attachmentData.width > 0) { localSc.x *= attachmentData.width / frame.rect.width; } if (attachmentData.height > 0) { localSc.y *= attachmentData.height / frame.rect.height; } } newFrame.transform.localScale = localSc; newFrame.color = attachmentData.color; newFrame.transform.localRotation = Quaternion.Euler(0, 0, attachmentData.rotation); if (armatureEditor.genImgCollider) { BoxCollider2D collider = newFrame.gameObject.AddComponent <BoxCollider2D>(); if (newFrame.frame.isRotated) { collider.size = new Vector2(newFrame.frame.rect.size.y, newFrame.frame.rect.size.x) * armatureEditor.unit; Vector2 center = new Vector2( -newFrame.frame.frameSize.width / 2 - newFrame.frame.frameSize.x + newFrame.frame.rect.width / 2, newFrame.frame.frameSize.height / 2 + newFrame.frame.frameSize.y - newFrame.frame.rect.height / 2); collider.offset = center * armatureEditor.unit; } else { collider.size = newFrame.frame.rect.size * armatureEditor.unit; Vector2 center = new Vector2( -newFrame.frame.frameSize.width / 2 - newFrame.frame.frameSize.x + newFrame.frame.rect.width / 2, newFrame.frame.frameSize.height / 2 + newFrame.frame.frameSize.y - newFrame.frame.rect.height / 2); collider.offset = center * armatureEditor.unit; } } newFrame.UpdateAll(); }