public BoneMatrix2D Concat(BoneMatrix2D other) { var a = this.a * other.a; var b = 0.0f; var c = 0.0f; var d = this.d * other.d; var tx = this.tx * other.a + other.tx; var ty = this.ty * other.d + other.ty; if (this.b != 0.0f || this.c != 0.0f || other.b != 0.0f || other.c != 0.0f) { a += this.b * other.c; d += this.c * other.b; b += this.a * other.b + this.b * other.d; c += this.c * other.a + this.d * other.c; tx += this.ty * other.c; ty += this.tx * other.b; } this.a = a; this.b = b; this.c = c; this.d = d; this.tx = tx; this.ty = ty; return(this); }
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 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; } } }
public BoneMatrix2D Copy(BoneMatrix2D matrix) { return(this.SetTo(matrix.a, matrix.b, matrix.c, matrix.d, matrix.tx, matrix.ty)); }
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); }