Skip to content

Instantly share code, notes, and snippets.

@Youlean
Created January 23, 2020 17:47
Show Gist options
  • Save Youlean/109bb928d0447402fd020e402864f8df to your computer and use it in GitHub Desktop.
Save Youlean/109bb928d0447402fd020e402864f8df to your computer and use it in GitHub Desktop.
ZGE easing functions
// Easing
const float PI2 = PI * 2;
inline float LinearEase(float p)
{
return p;
}
inline float QuadraticEaseIn(float p)
{
return p * p;
}
inline float QuadraticEaseOut(float p)
{
return -1.0 * (p * (p - 2));
}
inline float QuadraticEaseInOut(float p)
{
if (p < 0.5)
{
return 2 * p * p;
}
else
{
return (-2 * p * p) + (4 * p) - 1;
}
}
inline float CubicEaseIn(float p)
{
return p * p * p;
}
inline float CubicEaseOut(float p)
{
float f = (p - 1);
return f * f * f + 1;
}
inline float CubicEaseInOut(float p)
{
if (p < 0.5)
{
return 4 * p * p * p;
}
else
{
float f = ((2 * p) - 2);
return 0.5 * f * f * f + 1;
}
}
inline float QuarticEaseIn(float p)
{
return p * p * p * p;
}
inline float QuarticEaseOut(float p)
{
float f = (p - 1);
return f * f * f * (1 - p) + 1;
}
inline float QuarticEaseInOut(float p)
{
if (p < 0.5)
{
return 8 * p * p * p * p;
}
else
{
float f = (p - 1);
return -8 * f * f * f * f + 1;
}
}
inline float QuinticEaseIn(float p)
{
return p * p * p * p * p;
}
inline float QuinticEaseOut(float p)
{
float f = (p - 1);
return f * f * f * f * f + 1;
}
inline float QuinticEaseInOut(float p)
{
if (p < 0.5)
{
return 16 * p * p * p * p * p;
}
else
{
float f = ((2 * p) - 2);
return 0.5 * f * f * f * f * f + 1;
}
}
inline float SineEaseIn(float p)
{
return sin(1.5707963 * p);
}
inline float SineEaseOut(float p)
{
return 1 + sin(1.5707963 * (p - 1.0));
}
inline float SineEaseInOut(float p)
{
return 0.5 * (1 + sin(3.1415926 * (p - 0.5)));
}
inline float CircularEaseIn(float p)
{
return 1 - sqrt(1 - (p * p));
}
inline float CircularEaseOut(float p)
{
return sqrt((2 - p) * p);
}
inline float CircularEaseInOut(float p)
{
if (p < 0.5)
{
return 0.5 * (1 - sqrt(1 - 4 * (p * p)));
}
else
{
return 0.5 * (sqrt(-1.0 * ((2 * p) - 3) * ((2 * p) - 1)) + 1);
}
}
inline float ExponentialEaseIn(float p)
{
return (p == 0.0) ? p : pow(2, 10 * (p - 1));
}
inline float ExponentialEaseOut(float p)
{
return (p == 1.0) ? p : 1 - pow(2, -10 * p);
}
inline float ExponentialEaseInOut(float p)
{
if (p == 0.0 || p == 1.0) return p;
if (p < 0.5)
{
return 0.5 * pow(2, (20 * p) - 10);
}
else
{
return -0.5 * pow(2, (-20 * p) + 10) + 1;
}
}
inline float ElasticEaseIn(float p)
{
float t2 = p * p;
return t2 * t2 * sin(p * PI * 4.5);
}
inline float ElasticEaseOut(float p)
{
float t2 = (p - 1) * (p - 1);
return 1 - t2 * t2 * cos(p * PI * 4.5);
}
inline float ElasticEaseInOut(float p)
{
float t2;
if (p < 0.45) {
t2 = p * p;
return 8 * t2 * t2 * sin(p * PI * 9);
}
else if (p < 0.55) {
return 0.5 + 0.75 * sin(p * PI * 4);
}
else {
t2 = (p - 1) * (p - 1);
return 1 - 8 * t2 * t2 * sin(p * PI * 9);
}
}
inline float BackEaseIn(float p)
{
return p * p * p - p * sin(p * PI);
}
inline float BackEaseOut(float p)
{
float f = (1 - p);
return 1 - (f * f * f - f * sin(f * PI));
}
inline float BackEaseInOut(float p)
{
if (p < 0.5)
{
float f = 2 * p;
return 0.5 * (f * f * f - f * sin(f * PI));
}
else
{
float f1 = (1 - (2 * p - 1));
return 0.5 * (1 - (f1 * f1 * f1 - f1 * sin(f1 * PI))) + 0.5;
}
}
inline float BounceEaseOut(float p)
{
if (p < 4 / 11.0)
{
return (121 * p * p) / 16.0;
}
else if (p < 8 / 11.0)
{
return (363 / 40.0 * p * p) - (99 / 10.0 * p) + 17 / 5.0;
}
else if (p < 9 / 10.0)
{
return (4356 / 361.0 * p * p) - (35442 / 1805.0 * p) + 16061 / 1805.0;
}
else
{
return (54 / 5.0 * p * p) - (513 / 25.0 * p) + 268 / 25.0;
}
}
inline float BounceEaseIn(float p)
{
return 1 - BounceEaseOut(1 - p);
}
inline float BounceEaseInOut(float p)
{
if (p < 0.5)
{
return 0.5 * BounceEaseIn(p * 2);
}
else
{
return 0.5 * BounceEaseOut(p * 2 - 1) + 0.5;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment