Last active
November 8, 2024 17:23
-
-
Save Kryzarel/bba64622057f21a1d6d44879f9cd7bd4 to your computer and use it in GitHub Desktop.
C# Easing Functions
This file contains 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 System; | |
namespace Kryz.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 | |
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.5) return InQuad(t * 2) / 2; | |
return 1 - InQuad((1 - t) * 2) / 2; | |
} | |
public static float InCubic(float t) => t * t * t; | |
public static float OutCubic(float t) => 1 - InCubic(1 - t); | |
public static float InOutCubic(float t) | |
{ | |
if (t < 0.5) return InCubic(t * 2) / 2; | |
return 1 - InCubic((1 - t) * 2) / 2; | |
} | |
public static float InQuart(float t) => t * t * t * t; | |
public static float OutQuart(float t) => 1 - InQuart(1 - t); | |
public static float InOutQuart(float t) | |
{ | |
if (t < 0.5) return InQuart(t * 2) / 2; | |
return 1 - InQuart((1 - t) * 2) / 2; | |
} | |
public static float InQuint(float t) => t * t * t * t * t; | |
public static float OutQuint(float t) => 1 - InQuint(1 - t); | |
public static float InOutQuint(float t) | |
{ | |
if (t < 0.5) return InQuint(t * 2) / 2; | |
return 1 - InQuint((1 - t) * 2) / 2; | |
} | |
public static float InSine(float t) => 1 - (float)Math.Cos(t * Math.PI / 2); | |
public static float OutSine(float t) => (float)Math.Sin(t * Math.PI / 2); | |
public static float InOutSine(float t) => (float)(Math.Cos(t * Math.PI) - 1) / -2; | |
public static float InExpo(float t) => (float)Math.Pow(2, 10 * (t - 1)); | |
public static float OutExpo(float t) => 1 - InExpo(1 - t); | |
public static float InOutExpo(float t) | |
{ | |
if (t < 0.5) return InExpo(t * 2) / 2; | |
return 1 - InExpo((1 - t) * 2) / 2; | |
} | |
public static float InCirc(float t) => -((float)Math.Sqrt(1 - t * t) - 1); | |
public static float OutCirc(float t) => 1 - InCirc(1 - t); | |
public static float InOutCirc(float t) | |
{ | |
if (t < 0.5) return InCirc(t * 2) / 2; | |
return 1 - InCirc((1 - t) * 2) / 2; | |
} | |
public static float InElastic(float t) => 1 - OutElastic(1 - t); | |
public static float OutElastic(float t) | |
{ | |
float p = 0.3f; | |
return (float)Math.Pow(2, -10 * t) * (float)Math.Sin((t - p / 4) * (2 * Math.PI) / p) + 1; | |
} | |
public static float InOutElastic(float t) | |
{ | |
if (t < 0.5) return InElastic(t * 2) / 2; | |
return 1 - InElastic((1 - t) * 2) / 2; | |
} | |
public static float InBack(float t) | |
{ | |
float s = 1.70158f; | |
return t * t * ((s + 1) * t - s); | |
} | |
public static float OutBack(float t) => 1 - InBack(1 - t); | |
public static float InOutBack(float t) | |
{ | |
if (t < 0.5) return InBack(t * 2) / 2; | |
return 1 - InBack((1 - t) * 2) / 2; | |
} | |
public static float InBounce(float t) => 1 - OutBounce(1 - t); | |
public static float OutBounce(float t) | |
{ | |
float div = 2.75f; | |
float mult = 7.5625f; | |
if (t < 1 / div) | |
{ | |
return mult * t * t; | |
} | |
else if (t < 2 / div) | |
{ | |
t -= 1.5f / div; | |
return mult * t * t + 0.75f; | |
} | |
else if (t < 2.5 / 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.5) return InBounce(t * 2) / 2; | |
return 1 - InBounce((1 - t) * 2) / 2; | |
} | |
} | |
} |
Noice
Noice
Noice
Noice
Noice
Noice
Noice
The InSine formula is slightly incorrect, it should be 1 - cos
, not -cos.
The InSine formula is slightly incorrect, it should be
1 - cos
, not-cos.
Nice catch. Fixed.
Also, wtf is up with all the "Noice" comments o.O
Also, wtf is up with all the "Noice" comments o.O
idk but it's kinda funny tbh
Noice
Noice
Noice
Noice
Noice
Noice
I smile every time I get a notification from here
Noice
Noice
I smile every time I get a notification from here
Noice
same :3
reminds me of how i felt 2 years ago
yus owo
Noice
Noice
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Noice