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; } } }
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; } } }
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 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; } } }
private static SpineData.AnimationDeformData ParseDeformAnimTimeline(SpineArmatureEditor armatureEditor, string skinName, string slotname, string attchmentname, Bones2D.JSONArray deformAnimObj) { SpineData.AnimationDeformData animDeformDatas = new SpineData.AnimationDeformData(); animDeformDatas.slotName = slotname; animDeformDatas.skinName = skinName; animDeformDatas.timelines = new SpineData.DeformTimeline[deformAnimObj.Count]; SpineData.SkinAttachment skinAtt = GetSkinAttachment(armatureEditor, skinName, slotname, attchmentname); bool haveWeight = (skinAtt == null || skinAtt.weights == null || skinAtt.weights.Count == 0) ? false : true; for (int i = 0; i < deformAnimObj.Count; ++i) { SpineData.DeformTimeline timeline = new SpineData.DeformTimeline(); animDeformDatas.timelines[i] = timeline; timeline.attachment = attchmentname; Bones2D.JSONClass animObj = deformAnimObj[i].AsObject; if (animObj.ContainKey("time")) { timeline.time = animObj["time"].AsFloat; } if (animObj.ContainKey("curve")) { if (animObj["curve"] == "stepped") { timeline.tweenEasing = "stepped"; } else if (animObj["curve"] == "linear") { //default } else { timeline.curve = ConvertJsonArrayToFloatArr(animObj["curve"].AsArray); } } if (animObj.ContainKey("offset")) { timeline.offset = animObj["offset"].AsInt / 2; } if (animObj.ContainKey("vertices")) { Bones2D.JSONArray verticesObj = animObj["vertices"].AsArray; int index = 0; int k = 0; timeline.vertices = new Vector3[verticesObj.Count / 2]; for (; k < verticesObj.Count && k + 1 < verticesObj.Count; k += 2) { timeline.vertices[index] = new Vector3(verticesObj[k].AsFloat * armatureEditor.unit, verticesObj[k + 1].AsFloat * armatureEditor.unit, 0f); ++index; } armatureEditor.ffdKV [attchmentname] = true; if (haveWeight) { CreateBonePose(armatureEditor); BoneMatrix2D matrix = new BoneMatrix2D(); int vertexIndex = 0; int offset = timeline.offset; int newOffset = 0; for (int j = 0; j < skinAtt.weights.Count; ++j) { int boneCount = (int)skinAtt.weights[j]; if (offset <= 0) { Vector3 v = timeline.vertices [vertexIndex]; Vector3 result = new Vector3(); for (int w = 0; w < boneCount * 4; w += 4) { int boneIndex = (int)skinAtt.weights [j + w + 1]; SpineData.BoneData boneData = armatureEditor.armatureData.bones [boneIndex]; float weight = skinAtt.weights [j + w + 4]; BoneMatrix2D boneMatrix = armatureEditor.bonePoseKV [boneData.name]; matrix.Identity(); matrix.a = boneMatrix.a; matrix.b = boneMatrix.b; matrix.c = boneMatrix.c; matrix.d = boneMatrix.d; matrix.Invert(); //to local Vector2 p = matrix.TransformPoint(v.x, v.y); result.x += p.x * weight; result.y += p.y * weight; } timeline.vertices [vertexIndex] = result; ++vertexIndex; if (vertexIndex >= timeline.vertices.Length) { break; } } else { ++newOffset; } offset -= boneCount; j += boneCount * 4; } timeline.offset = newOffset; } } } return(animDeformDatas); }
private static void ParseBones(SpineArmatureEditor armatureEditor, Bones2D.JSONClass armtureObj) { if (armtureObj.ContainKey("bones")) { Bones2D.JSONArray bones = armtureObj["bones"].AsArray; SpineData.BoneData[] boneDatas = new SpineData.BoneData[bones.Count]; for (int i = 0; i < bones.Count; ++i) { Bones2D.JSONClass boneObj = bones[i].AsObject; SpineData.BoneData boneData = new SpineData.BoneData(); if (boneObj.ContainKey("length")) { boneData.length = boneObj["length"].AsFloat; } if (boneObj.ContainKey("name")) { boneData.name = boneObj["name"].ToString(); } if (boneObj.ContainKey("parent")) { boneData.parent = boneObj["parent"].ToString(); } if (boneObj.ContainKey("inheritRotation")) { boneData.inheritRotation = boneObj["inheritRotation"].AsInt == 1?true:false; } if (boneObj.ContainKey("inheritScale")) { boneData.inheritScale = boneObj["inheritScale"].AsInt == 1?true:false; } if (boneObj.ContainKey("x")) { boneData.x = boneObj["x"].AsFloat * armatureEditor.unit; } if (boneObj.ContainKey("y")) { boneData.y = boneObj["y"].AsFloat * armatureEditor.unit; } if (boneObj.ContainKey("scaleX")) { boneData.scaleX = boneObj["scaleX"].AsFloat; } if (boneObj.ContainKey("scaleY")) { boneData.scaleY = boneObj["scaleY"].AsFloat; } if (boneObj.ContainKey("shearX")) { boneData.scaleX = boneObj["shearX"].AsFloat; } if (boneObj.ContainKey("shearY")) { boneData.scaleY = boneObj["shearY"].AsFloat; } if (boneObj.ContainKey("rotation")) { boneData.rotation = boneObj["rotation"].AsFloat; } boneData.index = i; boneDatas[i] = boneData; armatureEditor.bonesDataKV[boneData.name] = boneData; } armatureEditor.armatureData.bones = boneDatas; } }