BinarySearch() static private method

static private BinarySearch ( float values, float target ) : int
values float
target float After the first and before the last entry.
return int
                /// <summary>Evaluates the resulting value of a TranslateTimeline at a given time.
                /// SkeletonData can be accessed from Skeleton.Data or from SkeletonDataAsset.GetSkeletonData.
                /// If no SkeletonData is given, values are computed relative to setup pose instead of local-absolute.</summary>
                public static Vector2 Evaluate(this TranslateTimeline tt, float time, SkeletonData skeletonData = null)
                {
                    const int PREV_TIME = -3, PREV_X = -2, PREV_Y = -1;
                    const int X = 1, Y = 2;

                    var frames = tt.Frames;

                    if (time < frames[0])
                    {
                        return(Vector2.zero);
                    }

                    float x, y;

                    if (time >= frames[frames.Length - TranslateTimeline.ENTRIES])
                    {                     // Time is after last frame.
                        x = frames[frames.Length + PREV_X];
                        y = frames[frames.Length + PREV_Y];
                    }
                    else
                    {
                        int frame = Animation.BinarySearch(frames, time, TranslateTimeline.ENTRIES);
                        x = frames[frame + PREV_X];
                        y = frames[frame + PREV_Y];
                        float frameTime = frames[frame];
                        float percent   = tt.GetCurvePercent(frame / TranslateTimeline.ENTRIES - 1, 1 - (time - frameTime) / (frames[frame + PREV_TIME] - frameTime));

                        x += (frames[frame + X] - x) * percent;
                        y += (frames[frame + Y] - y) * percent;
                    }

                    Vector2 o = new Vector2(x, y);

                    if (skeletonData == null)
                    {
                        return(o);
                    }
                    else
                    {
                        var boneData = skeletonData.Bones.Items[tt.BoneIndex];
                        return(o + new Vector2(boneData.X, boneData.Y));
                    }
                }
Example #2
0
        static private void ApplyRotateTimeline(RotateTimeline rotateTimeline, Skeleton skeleton, float time, float alpha, bool setupPose,
                                                float[] timelinesRotation, int i, bool firstFrame)
        {
            if (alpha == 1)
            {
                rotateTimeline.Apply(skeleton, 0, time, null, 1, setupPose, false);
                return;
            }

            Bone bone = skeleton.bones.Items[rotateTimeline.boneIndex];

            float[] frames = rotateTimeline.frames;
            if (time < frames[0])
            {
                if (setupPose)
                {
                    bone.rotation = bone.data.rotation;
                }
                return;
            }

            float r2;

            if (time >= frames[frames.Length - RotateTimeline.ENTRIES])             // Time is after last frame.
            {
                r2 = bone.data.rotation + frames[frames.Length + RotateTimeline.PREV_ROTATION];
            }
            else
            {
                // Interpolate between the previous frame and the current frame.
                int   frame        = Animation.BinarySearch(frames, time, RotateTimeline.ENTRIES);
                float prevRotation = frames[frame + RotateTimeline.PREV_ROTATION];
                float frameTime    = frames[frame];
                float percent      = rotateTimeline.GetCurvePercent((frame >> 1) - 1,
                                                                    1 - (time - frameTime) / (frames[frame + RotateTimeline.PREV_TIME] - frameTime));

                r2  = frames[frame + RotateTimeline.ROTATION] - prevRotation;
                r2 -= (16384 - (int)(16384.499999999996 - r2 / 360)) * 360;
                r2  = prevRotation + r2 * percent + bone.data.rotation;
                r2 -= (16384 - (int)(16384.499999999996 - r2 / 360)) * 360;
            }

            // Mix between rotations using the direction of the shortest route on the first frame while detecting crosses.
            float r1 = setupPose ? bone.data.rotation : bone.rotation;
            float total, diff = r2 - r1;

            if (diff == 0)
            {
                if (firstFrame)
                {
                    timelinesRotation[i] = 0;
                    total = 0;
                }
                else
                {
                    total = timelinesRotation[i];
                }
            }
            else
            {
                diff -= (16384 - (int)(16384.499999999996 - diff / 360)) * 360;
                float lastTotal, lastDiff;
                if (firstFrame)
                {
                    lastTotal = 0;
                    lastDiff  = diff;
                }
                else
                {
                    lastTotal = timelinesRotation[i];                     // Angle and direction of mix, including loops.
                    lastDiff  = timelinesRotation[i + 1];                 // Difference between bones.
                }
                bool current = diff > 0, dir = lastTotal >= 0;
                // Detect cross at 0 (not 180).
                if (Math.Sign(lastDiff) != Math.Sign(diff) && Math.Abs(lastDiff) <= 90)
                {
                    // A cross after a 360 rotation is a loop.
                    if (Math.Abs(lastTotal) > 180)
                    {
                        lastTotal += 360 * Math.Sign(lastTotal);
                    }
                    dir = current;
                }
                total = diff + lastTotal - lastTotal % 360;                 // Store loops as part of lastTotal.
                if (dir != current)
                {
                    total += 360 * Math.Sign(lastTotal);
                }
                timelinesRotation[i] = total;
            }
            timelinesRotation[i + 1] = diff;
            r1           += total * alpha;
            bone.rotation = r1 - (16384 - (int)(16384.499999999996 - r1 / 360)) * 360;
        }
Example #3
0
        public override void Apply(Skeleton skeleton, float lastTime, float time, ExposedList <Event> firedEvents, float alpha, MixPose pose, MixDirection direction)
        {
            Slot slot = skeleton.slots.Items[slotIndex];

            float[] array = frames;
            if (time < array[0])
            {
                SlotData data = slot.data;
                switch (pose)
                {
                case MixPose.Setup:
                    slot.r  = data.r;
                    slot.g  = data.g;
                    slot.b  = data.b;
                    slot.a  = data.a;
                    slot.r2 = data.r2;
                    slot.g2 = data.g2;
                    slot.b2 = data.b2;
                    break;

                case MixPose.Current:
                    slot.r  += (slot.r - data.r) * alpha;
                    slot.g  += (slot.g - data.g) * alpha;
                    slot.b  += (slot.b - data.b) * alpha;
                    slot.a  += (slot.a - data.a) * alpha;
                    slot.r2 += (slot.r2 - data.r2) * alpha;
                    slot.g2 += (slot.g2 - data.g2) * alpha;
                    slot.b2 += (slot.b2 - data.b2) * alpha;
                    break;
                }
                return;
            }
            float num2;
            float num3;
            float num4;
            float num5;
            float num6;
            float num7;
            float num8;

            if (time >= array[array.Length - 8])
            {
                int num = array.Length;
                num2 = array[num + -7];
                num3 = array[num + -6];
                num4 = array[num + -5];
                num5 = array[num + -4];
                num6 = array[num + -3];
                num7 = array[num + -2];
                num8 = array[num + -1];
            }
            else
            {
                int num9 = Animation.BinarySearch(array, time, 8);
                num2 = array[num9 + -7];
                num3 = array[num9 + -6];
                num4 = array[num9 + -5];
                num5 = array[num9 + -4];
                num6 = array[num9 + -3];
                num7 = array[num9 + -2];
                num8 = array[num9 + -1];
                float num10        = array[num9];
                float curvePercent = GetCurvePercent(num9 / 8 - 1, 1f - (time - num10) / (array[num9 + -8] - num10));
                num2 += (array[num9 + 1] - num2) * curvePercent;
                num3 += (array[num9 + 2] - num3) * curvePercent;
                num4 += (array[num9 + 3] - num4) * curvePercent;
                num5 += (array[num9 + 4] - num5) * curvePercent;
                num6 += (array[num9 + 5] - num6) * curvePercent;
                num7 += (array[num9 + 6] - num7) * curvePercent;
                num8 += (array[num9 + 7] - num8) * curvePercent;
            }
            if (alpha == 1f)
            {
                slot.r  = num2;
                slot.g  = num3;
                slot.b  = num4;
                slot.a  = num5;
                slot.r2 = num6;
                slot.g2 = num7;
                slot.b2 = num8;
                return;
            }
            float r;
            float g;
            float b;
            float a;
            float r2;
            float g2;
            float b2;

            if (pose == MixPose.Setup)
            {
                r  = slot.data.r;
                g  = slot.data.g;
                b  = slot.data.b;
                a  = slot.data.a;
                r2 = slot.data.r2;
                g2 = slot.data.g2;
                b2 = slot.data.b2;
            }
            else
            {
                r  = slot.r;
                g  = slot.g;
                b  = slot.b;
                a  = slot.a;
                r2 = slot.r2;
                g2 = slot.g2;
                b2 = slot.b2;
            }
            slot.r  = r + (num2 - r) * alpha;
            slot.g  = g + (num3 - g) * alpha;
            slot.b  = b + (num4 - b) * alpha;
            slot.a  = a + (num5 - a) * alpha;
            slot.r2 = r2 + (num6 - r2) * alpha;
            slot.g2 = g2 + (num7 - g2) * alpha;
            slot.b2 = b2 + (num8 - b2) * alpha;
        }
Example #4
0
 private static void ApplyRotateTimeline(RotateTimeline rotateTimeline, Skeleton skeleton, float time, float alpha, MixPose pose, float[] timelinesRotation, int i, bool firstFrame)
 {
     if (firstFrame)
     {
         timelinesRotation[i] = 0f;
     }
     if (alpha == 1f)
     {
         rotateTimeline.Apply(skeleton, 0f, time, null, 1f, pose, MixDirection.In);
     }
     else
     {
         Bone    bone   = skeleton.bones.Items[rotateTimeline.boneIndex];
         float[] frames = rotateTimeline.frames;
         if (time < frames[0])
         {
             if (pose == MixPose.Setup)
             {
                 bone.rotation = bone.data.rotation;
             }
         }
         else
         {
             float num;
             float num7;
             if (time >= frames[frames.Length - 2])
             {
                 num = bone.data.rotation + frames[frames.Length + -1];
             }
             else
             {
                 int   index        = Animation.BinarySearch(frames, time, 2);
                 float num3         = frames[index + -1];
                 float num4         = frames[index];
                 float curvePercent = rotateTimeline.GetCurvePercent((index >> 1) - 1, 1f - ((time - num4) / (frames[index + -2] - num4)));
                 num  = frames[index + 1] - num3;
                 num -= (0x4000 - ((int)(16384.499999999996 - (num / 360f)))) * 360;
                 num  = (num3 + (num * curvePercent)) + bone.data.rotation;
                 num -= (0x4000 - ((int)(16384.499999999996 - (num / 360f)))) * 360;
             }
             float num6 = (pose != MixPose.Setup) ? bone.rotation : bone.data.rotation;
             float num8 = num - num6;
             if (num8 == 0f)
             {
                 num7 = timelinesRotation[i];
             }
             else
             {
                 float num9;
                 float num10;
                 num8 -= (0x4000 - ((int)(16384.499999999996 - (num8 / 360f)))) * 360;
                 if (firstFrame)
                 {
                     num9  = 0f;
                     num10 = num8;
                 }
                 else
                 {
                     num9  = timelinesRotation[i];
                     num10 = timelinesRotation[i + 1];
                 }
                 bool flag  = num8 > 0f;
                 bool flag2 = num9 >= 0f;
                 if ((Math.Sign(num10) != Math.Sign(num8)) && (Math.Abs(num10) <= 90f))
                 {
                     if (Math.Abs(num9) > 180f)
                     {
                         num9 += 360 * Math.Sign(num9);
                     }
                     flag2 = flag;
                 }
                 num7 = (num8 + num9) - (num9 % 360f);
                 if (flag2 != flag)
                 {
                     num7 += 360 * Math.Sign(num9);
                 }
                 timelinesRotation[i] = num7;
             }
             timelinesRotation[i + 1] = num8;
             num6         += num7 * alpha;
             bone.rotation = num6 - ((0x4000 - ((int)(16384.499999999996 - (num6 / 360f)))) * 360);
         }
     }
 }
        public override void Apply(Skeleton skeleton, float lastTime, float time, ExposedList <Event> firedEvents, float alpha, MixPose pose, MixDirection direction)
        {
            Slot slot = skeleton.slots.Items[this.slotIndex];

            float[] frames = this.frames;
            if (time < frames[0])
            {
                SlotData data = slot.data;
                if (pose != MixPose.Setup)
                {
                    if (pose != MixPose.Current)
                    {
                        return;
                    }
                }
                else
                {
                    slot.r = data.r;
                    slot.g = data.g;
                    slot.b = data.b;
                    slot.a = data.a;
                    return;
                }
                slot.r += (slot.r - data.r) * alpha;
                slot.g += (slot.g - data.g) * alpha;
                slot.b += (slot.b - data.b) * alpha;
                slot.a += (slot.a - data.a) * alpha;
            }
            else
            {
                float num;
                float num2;
                float num3;
                float num4;
                if (time >= frames[frames.Length - 5])
                {
                    int length = frames.Length;
                    num  = frames[length + -4];
                    num2 = frames[length + -3];
                    num3 = frames[length + -2];
                    num4 = frames[length + -1];
                }
                else
                {
                    int index = Animation.BinarySearch(frames, time, 5);
                    num  = frames[index + -4];
                    num2 = frames[index + -3];
                    num3 = frames[index + -2];
                    num4 = frames[index + -1];
                    float num7         = frames[index];
                    float curvePercent = base.GetCurvePercent((index / 5) - 1, 1f - ((time - num7) / (frames[index + -5] - num7)));
                    num  += (frames[index + 1] - num) * curvePercent;
                    num2 += (frames[index + 2] - num2) * curvePercent;
                    num3 += (frames[index + 3] - num3) * curvePercent;
                    num4 += (frames[index + 4] - num4) * curvePercent;
                }
                if (alpha == 1f)
                {
                    slot.r = num;
                    slot.g = num2;
                    slot.b = num3;
                    slot.a = num4;
                }
                else
                {
                    float r;
                    float g;
                    float b;
                    float a;
                    if (pose == MixPose.Setup)
                    {
                        r = slot.data.r;
                        g = slot.data.g;
                        b = slot.data.b;
                        a = slot.data.a;
                    }
                    else
                    {
                        r = slot.r;
                        g = slot.g;
                        b = slot.b;
                        a = slot.a;
                    }
                    slot.r = r + ((num - r) * alpha);
                    slot.g = g + ((num2 - g) * alpha);
                    slot.b = b + ((num3 - b) * alpha);
                    slot.a = a + ((num4 - a) * alpha);
                }
            }
        }