public BoneMatrix2D Invert(BoneMatrix2D target) { var a = this.a; var b = this.b; var c = this.c; var d = this.d; var tx = this.tx; var ty = this.ty; if (b == 0 && c == 0) { target.b = target.c = 0; if (a == 0 || d == 0) { target.a = target.d = target.tx = target.ty = 0; } else { a = target.a = 1 / a; d = target.d = 1 / d; target.tx = -a * tx; target.ty = -d * ty; } return(this); } var determinant = a * d - b * c; if (determinant == 0) { target.Identity(); return(this); } determinant = 1 / determinant; var k = target.a = d * determinant; b = target.b = -b * determinant; c = target.c = -c * determinant; d = target.d = a * determinant; target.tx = -(k * tx + c * ty); target.ty = -(b * tx + d * ty); return(this); }
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); }