From 8ba88687dd56d221742c4c54101bf80741ce3237 Mon Sep 17 00:00:00 2001 From: Harald Csaszar Date: Wed, 13 Mar 2019 20:07:12 +0100 Subject: [PATCH 1/3] [csharp] Fixed differences in PathConstraint and IKConstraint between csharp and ref-impl. Just cosmetic and doc differences found. See #1294. --- spine-csharp/src/IkConstraint.cs | 125 ++++++++++++++++++----------- spine-csharp/src/PathConstraint.cs | 72 ++++++++++++----- 2 files changed, 129 insertions(+), 68 deletions(-) diff --git a/spine-csharp/src/IkConstraint.cs b/spine-csharp/src/IkConstraint.cs index cb7b05efb..d995a292c 100644 --- a/spine-csharp/src/IkConstraint.cs +++ b/spine-csharp/src/IkConstraint.cs @@ -31,55 +31,20 @@ using System; namespace Spine { + /// + /// + /// Stores the current pose for an IK constraint. An IK constraint adjusts the rotation of 1 or 2 constrained bones so the tip of + /// the last bone is as close to the target bone as possible. + /// + /// See IK constraints in the Spine User Guide. + /// public class IkConstraint : IConstraint { internal IkConstraintData data; internal ExposedList bones = new ExposedList(); internal Bone target; internal int bendDirection; internal bool compress, stretch; - internal float mix; - - public IkConstraintData Data { get { return data; } } - public int Order { get { return data.order; } } - - /// The bones that will be modified by this IK constraint. - public ExposedList Bones { - get { return bones; } - } - - /// The bone that is the IK target. - public Bone Target { - get { return target; } - set { target = value; } - } - - /// Controls the bend direction of the IK bones, either 1 or -1. - public int BendDirection { - get { return bendDirection; } - set { bendDirection = value; } - } - - /// - /// When true and only a single bone is being constrained, - /// if the target is too close, the bone is scaled to reach it. - public bool Compress { - get { return compress; } - set { compress = value; } - } - - /// - /// When true, if the target is out of range, the parent bone is scaled on the X axis to reach it. - /// If the parent bone has nonuniform scale, stretching is not applied. - public bool Stretch { - get { return stretch; } - set { stretch = value; } - } - - /// A percentage (0-1) that controls the mix between the constrained and unconstrained rotations. - public float Mix { - get { return mix; } - set { mix = value; } - } + internal float mix = 1; public IkConstraint (IkConstraintData data, Skeleton skeleton) { if (data == null) throw new ArgumentNullException("data", "data cannot be null."); @@ -96,6 +61,21 @@ namespace Spine { target = skeleton.FindBone(data.target.name); } + /// Copy constructor. + public IkConstraint (IkConstraint constraint, Skeleton skeleton) { + if (constraint == null) throw new ArgumentNullException("constraint cannot be null."); + if (skeleton == null) throw new ArgumentNullException("skeleton cannot be null."); + data = constraint.data; + bones = new ExposedList(constraint.Bones.Count); + foreach (Bone bone in constraint.Bones) + bones.Add(skeleton.Bones.Items[bone.data.index]); + target = skeleton.Bones.Items[constraint.target.data.index]; + mix = constraint.mix; + bendDirection = constraint.bendDirection; + compress = constraint.compress; + stretch = constraint.stretch; + } + /// Applies the constraint to the constrained bones. public void Apply () { Update(); @@ -114,12 +94,61 @@ namespace Spine { } } + + public int Order { + get { return data.order; } + } + + /// The bones that will be modified by this IK constraint. + public ExposedList Bones { + get { return bones; } + } + + /// The bone that is the IK target. + public Bone Target { + get { return target; } + set { target = value; } + } + + /// A percentage (0-1) that controls the mix between the constrained and unconstrained rotations. + public float Mix { + get { return mix; } + set { mix = value; } + } + + /// Controls the bend direction of the IK bones, either 1 or -1. + public int BendDirection { + get { return bendDirection; } + set { bendDirection = value; } + } + + /// + /// When true and only a single bone is being constrained, if the target is too close, the bone is scaled to reach it. + public bool Compress { + get { return compress; } + set { compress = value; } + } + + /// + /// When true, if the target is out of range, the parent bone is scaled to reach it. If more than one bone is being constrained + /// and the parent bone has local nonuniform scale, stretch is not applied. + public bool Stretch { + get { return stretch; } + set { stretch = value; } + } + + /// The IK constraint's setup pose data. + public IkConstraintData Data { + get { return data; } + } + override public string ToString () { return data.name; } /// Applies 1 bone IK. The target is specified in the world coordinate system. - static public void Apply (Bone bone, float targetX, float targetY, bool compress, bool stretch, bool uniform, float alpha) { + static public void Apply (Bone bone, float targetX, float targetY, bool compress, bool stretch, bool uniform, + float alpha) { if (!bone.appliedValid) bone.UpdateAppliedTransform(); Bone p = bone.parent; float id = 1 / (p.a * p.d - p.b * p.c); @@ -140,16 +169,14 @@ namespace Spine { if (uniform) sy *= s; } } - bone.UpdateWorldTransform(bone.ax, bone.ay, bone.arotation + rotationIK * alpha, sx, sy, bone.ashearX, - bone.ashearY); + bone.UpdateWorldTransform(bone.ax, bone.ay, bone.arotation + rotationIK * alpha, sx, sy, bone.ashearX, bone.ashearY); } - /// Adjusts the parent and child bone rotations so the tip of the child is as close to the target position as - /// possible. The target is specified in the world coordinate system. + /// Applies 2 bone IK. The target is specified in the world coordinate system. /// A direct descendant of the parent bone. static public void Apply (Bone parent, Bone child, float targetX, float targetY, int bendDir, bool stretch, float alpha) { if (alpha == 0) { - child.UpdateWorldTransform (); + child.UpdateWorldTransform(); return; } if (!parent.appliedValid) parent.UpdateAppliedTransform(); diff --git a/spine-csharp/src/PathConstraint.cs b/spine-csharp/src/PathConstraint.cs index d27b74c19..d5771cbb8 100644 --- a/spine-csharp/src/PathConstraint.cs +++ b/spine-csharp/src/PathConstraint.cs @@ -31,6 +31,14 @@ using System; namespace Spine { + + /// + /// + /// Stores the current pose for a path constraint. A path constraint adjusts the rotation, translation, and scale of the + /// constrained bones so they follow a {@link PathAttachment}. + /// + /// See Path constraints in the Spine User Guide. + /// public class PathConstraint : IConstraint { const int NONE = -1, BEFORE = -2, AFTER = -3; const float Epsilon = 0.00001f; @@ -44,15 +52,6 @@ namespace Spine { internal ExposedList world = new ExposedList(), curves = new ExposedList(), lengths = new ExposedList(); internal float[] segments = new float[10]; - public int Order { get { return data.order; } } - public float Position { get { return position; } set { position = value; } } - public float Spacing { get { return spacing; } set { spacing = value; } } - public float RotateMix { get { return rotateMix; } set { rotateMix = value; } } - public float TranslateMix { get { return translateMix; } set { translateMix = value; } } - public ExposedList Bones { get { return bones; } } - public Slot Target { get { return target; } set { target = value; } } - public PathConstraintData Data { get { return data; } } - public PathConstraint (PathConstraintData data, Skeleton skeleton) { if (data == null) throw new ArgumentNullException("data", "data cannot be null."); if (skeleton == null) throw new ArgumentNullException("skeleton", "skeleton cannot be null."); @@ -67,11 +66,26 @@ namespace Spine { translateMix = data.translateMix; } + /// Copy constructor. + public PathConstraint (PathConstraint constraint, Skeleton skeleton) { + if (constraint == null) throw new ArgumentNullException("constraint cannot be null."); + if (skeleton == null) throw new ArgumentNullException("skeleton cannot be null."); + data = constraint.data; + bones = new ExposedList(constraint.Bones.Count); + foreach (Bone bone in constraint.Bones) + bones.Add(skeleton.Bones.Items[bone.data.index]); + target = skeleton.slots.Items[constraint.target.data.index]; + position = constraint.position; + spacing = constraint.spacing; + rotateMix = constraint.rotateMix; + translateMix = constraint.translateMix; + } + /// Applies the constraint to the constrained bones. public void Apply () { Update(); } - + public void Update () { PathAttachment attachment = target.Attachment as PathAttachment; if (attachment == null) return; @@ -183,7 +197,7 @@ namespace Spine { float[] spacesItems = this.spaces.Items, output = this.positions.Resize(spacesCount * 3 + 2).Items, world; bool closed = path.Closed; int verticesLength = path.WorldVerticesLength, curveCount = verticesLength / 6, prevCurve = NONE; - float pathLength = 0; + float pathLength = 0; if (!path.ConstantSpeed) { float[] lengths = path.Lengths; @@ -207,14 +221,14 @@ namespace Spine { } else if (p < 0) { if (prevCurve != BEFORE) { prevCurve = BEFORE; - path.ComputeWorldVertices(target, 2, 4, world, 0); + path.ComputeWorldVertices(target, 2, 4, world, 0, 2); } AddBeforePosition(p, world, 0, output, o); continue; } else if (p > pathLength) { if (prevCurve != AFTER) { prevCurve = AFTER; - path.ComputeWorldVertices(target, verticesLength - 6, 4, world, 0); + path.ComputeWorldVertices(target, verticesLength - 6, 4, world, 0, 2); } AddAfterPosition(p - pathLength, world, 0, output, o); continue; @@ -235,10 +249,10 @@ namespace Spine { if (curve != prevCurve) { prevCurve = curve; if (closed && curve == curveCount) { - path.ComputeWorldVertices(target, verticesLength - 4, 4, world, 0); - path.ComputeWorldVertices(target, 0, 4, world, 4); + path.ComputeWorldVertices(target, verticesLength - 4, 4, world, 0, 2); + path.ComputeWorldVertices(target, 0, 4, world, 4, 2); } else - path.ComputeWorldVertices(target, curve * 6 + 2, 8, world, 0); + path.ComputeWorldVertices(target, curve * 6 + 2, 8, world, 0, 2); } AddCurvePosition(p, world[0], world[1], world[2], world[3], world[4], world[5], world[6], world[7], output, o, tangents || (i > 0 && space < PathConstraint.Epsilon)); @@ -250,15 +264,15 @@ namespace Spine { if (closed) { verticesLength += 2; world = this.world.Resize(verticesLength).Items; - path.ComputeWorldVertices(target, 2, verticesLength - 4, world, 0); - path.ComputeWorldVertices(target, 0, 2, world, verticesLength - 4); + path.ComputeWorldVertices(target, 2, verticesLength - 4, world, 0, 2); + path.ComputeWorldVertices(target, 0, 2, world, verticesLength - 4, 2); world[verticesLength - 2] = world[0]; world[verticesLength - 1] = world[1]; } else { curveCount--; verticesLength -= 4; world = this.world.Resize(verticesLength).Items; - path.ComputeWorldVertices(target, 2, verticesLength, world, 0); + path.ComputeWorldVertices(target, 2, verticesLength, world, 0, 2); } // Curve lengths. @@ -432,5 +446,25 @@ namespace Spine { output[o + 2] = (float)Math.Atan2(y - (y1 * uu + cy1 * ut * 2 + cy2 * tt), x - (x1 * uu + cx1 * ut * 2 + cx2 * tt)); } } + + public int Order { get { return data.order; } } + /// The position along the path. + public float Position { get { return position; } set { position = value; } } + /// The spacing between bones. + public float Spacing { get { return spacing; } set { spacing = value; } } + /// A percentage (0-1) that controls the mix between the constrained and unconstrained rotations. + public float RotateMix { get { return rotateMix; } set { rotateMix = value; } } + /// A percentage (0-1) that controls the mix between the constrained and unconstrained translations. + public float TranslateMix { get { return translateMix; } set { translateMix = value; } } + /// The bones that will be modified by this path constraint. + public ExposedList Bones { get { return bones; } } + /// The slot whose path attachment will be used to constrained the bones. + public Slot Target { get { return target; } set { target = value; } } + /// The path constraint's setup pose data. + public PathConstraintData Data { get { return data; } } + + override public string ToString () { + return data.name; + } } } From e35a088c8b866f98e142b614ce19da70497089b0 Mon Sep 17 00:00:00 2001 From: Harald Csaszar Date: Thu, 14 Mar 2019 13:55:00 +0100 Subject: [PATCH 2/3] [csharp] Minor: Fixed typo in comment, frack vs track. --- spine-csharp/src/AnimationState.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spine-csharp/src/AnimationState.cs b/spine-csharp/src/AnimationState.cs index 1f06c3af7..5f7f5e2d3 100644 --- a/spine-csharp/src/AnimationState.cs +++ b/spine-csharp/src/AnimationState.cs @@ -264,7 +264,7 @@ namespace Spine { } else { mix = to.mixTime / to.mixDuration; if (mix > 1) mix = 1; - if (blend != MixBlend.First) blend = from.mixBlend; // Track 0 ignores frack mix blend. + if (blend != MixBlend.First) blend = from.mixBlend; // Track 0 ignores track mix blend. } var eventBuffer = mix < from.eventThreshold ? this.events : null; From 7d646316a7b6f37c92e1a46310c149e92e0d40fb Mon Sep 17 00:00:00 2001 From: Harald Csaszar Date: Fri, 15 Mar 2019 14:15:34 +0100 Subject: [PATCH 3/3] [csharp] Fixed differences in TransformConstraint between csharp and ref-impl, potential bug fixed. See #1294. --- spine-csharp/src/TransformConstraint.cs | 66 ++++++++++++++++++------- 1 file changed, 47 insertions(+), 19 deletions(-) diff --git a/spine-csharp/src/TransformConstraint.cs b/spine-csharp/src/TransformConstraint.cs index b229e2865..a9be6c990 100644 --- a/spine-csharp/src/TransformConstraint.cs +++ b/spine-csharp/src/TransformConstraint.cs @@ -31,21 +31,19 @@ using System; namespace Spine { + /// + /// + /// Stores the current pose for a transform constraint. A transform constraint adjusts the world transform of the constrained + /// bones to match that of the target bone. + /// + /// See Transform constraints in the Spine User Guide. + /// public class TransformConstraint : IConstraint { internal TransformConstraintData data; internal ExposedList bones; internal Bone target; internal float rotateMix, translateMix, scaleMix, shearMix; - public TransformConstraintData Data { get { return data; } } - public int Order { get { return data.order; } } - public ExposedList Bones { get { return bones; } } - public Bone Target { get { return target; } set { target = value; } } - public float RotateMix { get { return rotateMix; } set { rotateMix = value; } } - public float TranslateMix { get { return translateMix; } set { translateMix = value; } } - public float ScaleMix { get { return scaleMix; } set { scaleMix = value; } } - public float ShearMix { get { return shearMix; } set { shearMix = value; } } - public TransformConstraint (TransformConstraintData data, Skeleton skeleton) { if (data == null) throw new ArgumentNullException("data", "data cannot be null."); if (skeleton == null) throw new ArgumentNullException("skeleton", "skeleton cannot be null."); @@ -57,11 +55,27 @@ namespace Spine { bones = new ExposedList(); foreach (BoneData boneData in data.bones) - bones.Add (skeleton.FindBone (boneData.name)); + bones.Add (skeleton.FindBone(boneData.name)); target = skeleton.FindBone(data.target.name); } + /// Copy constructor. + public TransformConstraint (TransformConstraint constraint, Skeleton skeleton) { + if (constraint == null) throw new ArgumentNullException("constraint cannot be null."); + if (skeleton == null) throw new ArgumentNullException("skeleton cannot be null."); + data = constraint.data; + bones = new ExposedList(constraint.Bones.Count); + foreach (Bone bone in constraint.Bones) + bones.Add(skeleton.Bones.Items[bone.data.index]); + target = skeleton.Bones.Items[constraint.target.data.index]; + rotateMix = constraint.rotateMix; + translateMix = constraint.translateMix; + scaleMix = constraint.scaleMix; + shearMix = constraint.shearMix; + } + + /// Applies the constraint to the constrained bones. public void Apply () { Update(); } @@ -116,13 +130,11 @@ namespace Spine { if (scaleMix > 0) { float s = (float)Math.Sqrt(bone.a * bone.a + bone.c * bone.c); - //float ts = (float)Math.sqrt(ta * ta + tc * tc); - if (s > 0.00001f) s = (s + ((float)Math.Sqrt(ta * ta + tc * tc) - s + data.offsetScaleX) * scaleMix) / s; + if (s != 0) s = (s + ((float)Math.Sqrt(ta * ta + tc * tc) - s + data.offsetScaleX) * scaleMix) / s; bone.a *= s; bone.c *= s; s = (float)Math.Sqrt(bone.b * bone.b + bone.d * bone.d); - //ts = (float)Math.Sqrt(tb * tb + td * td); - if (s > 0.00001f) s = (s + ((float)Math.Sqrt(tb * tb + td * td) - s + data.offsetScaleY) * scaleMix) / s; + if (s != 0) s = (s + ((float)Math.Sqrt(tb * tb + td * td) - s + data.offsetScaleY) * scaleMix) / s; bone.b *= s; bone.d *= s; modified = true; @@ -231,15 +243,15 @@ namespace Spine { float scaleX = bone.ascaleX, scaleY = bone.ascaleY; if (scaleMix != 0) { - if (scaleX > 0.00001f) scaleX = (scaleX + (target.ascaleX - scaleX + data.offsetScaleX) * scaleMix) / scaleX; - if (scaleY > 0.00001f) scaleY = (scaleY + (target.ascaleY - scaleY + data.offsetScaleY) * scaleMix) / scaleY; + if (scaleX != 0) scaleX = (scaleX + (target.ascaleX - scaleX + data.offsetScaleX) * scaleMix) / scaleX; + if (scaleY != 0) scaleY = (scaleY + (target.ascaleY - scaleY + data.offsetScaleY) * scaleMix) / scaleY; } float shearY = bone.ashearY; if (shearMix != 0) { float r = target.ashearY - shearY + data.offsetShearY; r -= (16384 - (int)(16384.499999999996 - r / 360)) * 360; - bone.shearY += r * shearMix; + shearY += r * shearMix; } bone.UpdateWorldTransform(x, y, rotation, scaleX, scaleY, bone.ashearX, shearY); @@ -266,8 +278,8 @@ namespace Spine { float scaleX = bone.ascaleX, scaleY = bone.ascaleY; if (scaleMix != 0) { - if (scaleX > 0.00001f) scaleX *= ((target.ascaleX - 1 + data.offsetScaleX) * scaleMix) + 1; - if (scaleY > 0.00001f) scaleY *= ((target.ascaleY - 1 + data.offsetScaleY) * scaleMix) + 1; + scaleX *= ((target.ascaleX - 1 + data.offsetScaleX) * scaleMix) + 1; + scaleY *= ((target.ascaleY - 1 + data.offsetScaleY) * scaleMix) + 1; } float shearY = bone.ashearY; @@ -277,6 +289,22 @@ namespace Spine { } } + public int Order { get { return data.order; } } + /// The bones that will be modified by this transform constraint. + public ExposedList Bones { get { return bones; } } + /// The target bone whose world transform will be copied to the constrained bones. + public Bone Target { get { return target; } set { target = value; } } + /// A percentage (0-1) that controls the mix between the constrained and unconstrained rotations. + public float RotateMix { get { return rotateMix; } set { rotateMix = value; } } + /// A percentage (0-1) that controls the mix between the constrained and unconstrained translations. + public float TranslateMix { get { return translateMix; } set { translateMix = value; } } + /// A percentage (0-1) that controls the mix between the constrained and unconstrained scales. + public float ScaleMix { get { return scaleMix; } set { scaleMix = value; } } + /// A percentage (0-1) that controls the mix between the constrained and unconstrained scales. + public float ShearMix { get { return shearMix; } set { shearMix = value; } } + /// The transform constraint's setup pose data. + public TransformConstraintData Data { get { return data; } } + override public string ToString () { return data.name; }