-
-
Save Chizaruu/4d4234a0f5ff81331cc66ed80496b28a to your computer and use it in GitHub Desktop.
C# Easing Functions for UnityEngine
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using static Unity.Mathematics.math; | |
| namespace TSK.Tweening | |
| { | |
| // Made with the help of this great post: https://joshondesign.com/2013/03/01/improvedEasingEquations | |
| // --------------------------------- Other Related Links -------------------------------------------------------------------- | |
| // Original equations, bad formulation: https://github.com/danro/jquery-easing/blob/master/jquery.easing.js | |
| // A few equations, very simplified: https://gist.github.com/gre/1650294 | |
| // Easings.net equations, simplified: https://github.com/ai/easings.net/blob/master/src/easings/easingsFunctions.ts | |
| // Original C# EasingFunctions: https://gist.github.com/Kryzarel/bba64622057f21a1d6d44879f9cd7bd4 | |
| public static class EasingFunctions | |
| { | |
| public static float Linear(float t) => t; | |
| public static float InQuad(float t) => t * t; | |
| public static float OutQuad(float t) => 1 - InQuad(1 - t); | |
| public static float InOutQuad(float t) | |
| { | |
| if (t < 0.5f) | |
| return InQuad(t * 2f) / 2f; | |
| return 1f - InQuad((1f - t) * 2f) / 2f; | |
| } | |
| public static float InCubic(float t) => t * t * t; | |
| public static float OutCubic(float t) => 1f - InCubic(1f - t); | |
| public static float InOutCubic(float t) | |
| { | |
| if (t < 0.5f) | |
| return InCubic(t * 2f) / 2f; | |
| return 1f - InCubic((1f - t) * 2f) / 2f; | |
| } | |
| public static float InQuart(float t) => t * t * t * t; | |
| public static float OutQuart(float t) => 1f - InQuart(1f - t); | |
| public static float InOutQuart(float t) | |
| { | |
| if (t < 0.5f) | |
| return InQuart(t * 2f) / 2f; | |
| return 1f - InQuart((1f - t) * 2f) / 2f; | |
| } | |
| public static float InQuint(float t) => t * t * t * t * t; | |
| public static float OutQuint(float t) => 1f - InQuint(1f - t); | |
| public static float InOutQuint(float t) | |
| { | |
| if (t < 0.5f) | |
| return InQuint(t * 2f) / 2f; | |
| return 1f - InQuint((1f - t) * 2f) / 2f; | |
| } | |
| public static float InSine(float t) => -cos(t * PI / 2f); | |
| public static float OutSine(float t) => sin(t * PI / 2f); | |
| public static float InOutSine(float t) => (cos(t * PI) - 1f) / -2f; | |
| public static float InExpo(float t) => pow(2f, 10f * (t - 1f)); | |
| public static float OutExpo(float t) => 1f - InExpo(1f - t); | |
| public static float InOutExpo(float t) | |
| { | |
| if (t < 0.5f) | |
| return InExpo(t * 2f) / 2f; | |
| return 1f - InExpo((1f - t) * 2f) / 2f; | |
| } | |
| public static float InCirc(float t) => -sqrt(1f - t * t) + 1f; | |
| public static float OutCirc(float t) => 1f - InCirc(1f - t); | |
| public static float InOutCirc(float t) | |
| { | |
| if (t < 0.5f) | |
| return InCirc(t * 2f) / 2f; | |
| return 1f - InCirc((1f - t) * 2f) / 2f; | |
| } | |
| public static float InElastic(float t) => 1f - OutElastic(1f - t); | |
| public static float OutElastic(float t) | |
| { | |
| float p = 0.3f; | |
| return pow(2f, -10f * t) * sin((t - p / 4f) * (2f * PI) / p) + 1f; | |
| } | |
| public static float InOutElastic(float t) | |
| { | |
| if (t < 0.5f) | |
| return InElastic(t * 2f) / 2f; | |
| return 1f - InElastic((1f - t) * 2f) / 2f; | |
| } | |
| public static float InBack(float t) | |
| { | |
| float s = 1.70158f; | |
| return t * t * ((s + 1f) * t - s); | |
| } | |
| public static float OutBack(float t) => 1f - InBack(1f - t); | |
| public static float InOutBack(float t) | |
| { | |
| if (t < 0.5f) | |
| return InBack(t * 2f) / 2f; | |
| return 1f - InBack((1f - t) * 2f) / 2f; | |
| } | |
| public static float InBounce(float t) => 1f - OutBounce(1f - t); | |
| public static float OutBounce(float t) | |
| { | |
| float div = 2.75f; | |
| float mult = 7.5625f; | |
| if (t < 1f / div) | |
| { | |
| return mult * t * t; | |
| } | |
| else if (t < 2f / div) | |
| { | |
| t -= 1.5f / div; | |
| return mult * t * t + 0.75f; | |
| } | |
| else if (t < 2.5f / div) | |
| { | |
| t -= 2.25f / div; | |
| return mult * t * t + 0.9375f; | |
| } | |
| else | |
| { | |
| t -= 2.625f / div; | |
| return mult * t * t + 0.984375f; | |
| } | |
| } | |
| public static float InOutBounce(float t) | |
| { | |
| if (t < 0.5f) | |
| return InBounce(t * 2f) / 2f; | |
| return 1f - InBounce((1f - t) * 2f) / 2f; | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment