Last active
December 12, 2015 01:48
-
-
Save Wunkolo/4693676 to your computer and use it in GitHub Desktop.
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
#pragma once | |
#include <math.h> | |
namespace Ease | |
{ | |
////////////////////////////////////////////////////////////////////////// | |
//Clamping utils | |
template <class T> | |
const T& Clamped(const T& high, const T& low, const T& v) | |
{ | |
return v < low ? low : (high < v ? high : v); | |
} | |
template <class T> | |
void Clamp( const T& high, const T& low, T& v) | |
{ | |
if (v < low) | |
{ | |
v = low; | |
return; | |
} | |
else if (high < v) | |
{ | |
v = high; | |
return; | |
} | |
return; | |
} | |
/////////////////////////////////////////////////////////////////////////////// | |
namespace Linear | |
{ | |
template <class T, class R> | |
T EaseIn(T Value, T Destination, R factor) | |
{ | |
return Value + ( factor * (Destination - Value)); | |
} | |
template <class T, class R> | |
T EaseOut(T Value, T Destination, R factor) | |
{ | |
return (T)1 - EaseIn(Value, Destination, (R)1 - factor); | |
} | |
template <class T, class R> | |
T EaseInOut(T Value, T Destination, R factor) | |
{ | |
if (factor < (R)0.5) | |
{ | |
return EaseIn(Value,Destination,2 * factor) / (T)2; | |
} | |
else | |
{ | |
return (T)0.5 + EaseOut(Value,Destination,2 * factor - (R)1) / (T)2; | |
} | |
} | |
} | |
/////////////////////////////////////////////////////////////////////////////// | |
namespace SmoothStep | |
{ | |
template <class T, class R> | |
T EaseIn(T Value, T Destination, R factor) | |
{ | |
return Value + ( (std::pow(factor,(R)2) * ((R)3-(R)2*factor)) * (Destination - Value)); | |
} | |
template <class T, class R> | |
T EaseOut(T Value, T Destination, R factor) | |
{ | |
return (T)1 - EaseIn(Value, Destination, (R)1 - factor); | |
} | |
template <class T, class R> | |
T EaseInOut(T Value, T Destination, R factor) | |
{ | |
if (factor < (R)0.5) | |
{ | |
return EaseIn(Value,Destination,(R)2 * factor) / (T)2; | |
} | |
else | |
{ | |
return (T)0.5 + EaseOut(Value,Destination,(R)2 * factor - (R)1) / (T)2; | |
} | |
} | |
} | |
/////////////////////////////////////////////////////////////////////////////// | |
namespace SmootherStep | |
{ | |
template <class T, class R> | |
T EaseIn(T Value, T Destination, R factor) | |
{ | |
return Value + (( std::pow(factor,(R)3)*(factor*(factor*(R)6 - (R)15) + (R)10) ) * (Destination - Value)); | |
} | |
template <class T, class R> | |
T EaseOut(T Value, T Destination, R factor) | |
{ | |
return (T)1 - EaseIn(Value, Destination, (R)1 - factor); | |
} | |
template <class T, class R> | |
T EaseInOut(T Value, T Destination, R factor) | |
{ | |
if (factor < (R)0.5) | |
{ | |
return EaseIn(Value,Destination,(R)2 * factor) / (T)2; | |
} | |
else | |
{ | |
return (T)0.5 + EaseOut(Value,Destination,(R)2 * factor - (R)1) / (T)2; | |
} | |
} | |
} | |
/////////////////////////////////////////////////////////////////////////////// | |
namespace Circular | |
{ | |
template <class T, class R> | |
T EaseIn(T Value, T Destination, R factor) | |
{ | |
return Value + (( (R)1 - sqrt((R)1 - std::pow(factor,2))) * (Destination - Value)); | |
} | |
template <class T, class R> | |
T EaseOut(T Value, T Destination, R factor) | |
{ | |
return (T)1 - EaseIn(Value, Destination, (R)1 - factor); | |
} | |
template <class T, class R> | |
T EaseInOut(T Value, T Destination, R factor) | |
{ | |
if (factor < (R)0.5) | |
{ | |
return EaseIn(Value,Destination,(R)2 * factor) / (T)2; | |
} | |
else | |
{ | |
return (T)0.5 + EaseOut(Value,Destination,(R)2 * factor - (R)1) / (T)2; | |
} | |
} | |
} | |
/////////////////////////////////////////////////////////////////////////////// | |
namespace Quadratic | |
{ | |
template <class T, class R> | |
T EaseIn(T Value, T Destination, R factor) | |
{ | |
return Value + ((pow(factor,(R)2)) * (Destination - Value)); | |
} | |
template <class T, class R> | |
T EaseOut(T Value, T Destination, R factor) | |
{ | |
return (T)1 - EaseIn(Value, Destination, (R)1 - factor); | |
} | |
template <class T, class R> | |
T EaseInOut(T Value, T Destination, R factor) | |
{ | |
if (factor < (R)0.5) | |
{ | |
return EaseIn(Value,Destination,(R)2 * factor) / (T)2; | |
} | |
else | |
{ | |
return (T)0.5 + EaseOut(Value,Destination,2 * factor - (R)1) / (T)2; | |
} | |
} | |
} | |
/////////////////////////////////////////////////////////////////////////////// | |
namespace Cubic | |
{ | |
template <class T, class R> | |
T EaseIn(T Value, T Destination, R factor) | |
{ | |
return Value + ((pow(factor,3)) * (Destination - Value)); | |
} | |
template <class T, class R> | |
T EaseOut(T Value, T Destination, R factor) | |
{ | |
return (T)1 - EaseIn(Value, Destination, (R)1 - factor); | |
} | |
template <class T, class R> | |
T EaseInOut(T Value, T Destination, R factor) | |
{ | |
if (factor < (R)0.5) | |
{ | |
return EaseIn(Value,Destination,2 * factor) / (T)2; | |
} | |
else | |
{ | |
return (T)0.5 + EaseOut(Value,Destination,2 * factor - (R)1) / (T)2; | |
} | |
} | |
} | |
/////////////////////////////////////////////////////////////////////////////// | |
namespace Quartic | |
{ | |
template <class T, class R> | |
T EaseIn(T Value, T Destination, R factor) | |
{ | |
return Value + ((pow(factor,4)) * (Destination - Value)); | |
} | |
template <class T, class R> | |
T EaseOut(T Value, T Destination, R factor) | |
{ | |
return (T)1 - EaseIn(Value, Destination, (R)1 - factor); | |
} | |
template <class T, class R> | |
T EaseInOut(T Value, T Destination, R factor) | |
{ | |
if (factor < (R)0.5) | |
{ | |
return EaseIn(Value,Destination,2 * factor) / (T)2; | |
} | |
else | |
{ | |
return (T)0.5 + EaseOut(Value,Destination,2 * factor - (R)1) / (T)2; | |
} | |
} | |
} | |
/////////////////////////////////////////////////////////////////////////////// | |
namespace Quintic | |
{ | |
template <class T, class R> | |
T EaseIn(T Value, T Destination, R factor) | |
{ | |
return Value + ((pow(factor,5)) * (Destination - Value)); | |
} | |
template <class T, class R> | |
T EaseOut(T Value, T Destination, R factor) | |
{ | |
return (T)1 - EaseIn(Value, Destination, (R)1 - factor); | |
} | |
template <class T, class R> | |
T EaseInOut(T Value, T Destination, R factor) | |
{ | |
if (factor < (R)0.5) | |
{ | |
return EaseIn(Value,Destination,2 * factor) / (T)2; | |
} | |
else | |
{ | |
return (T)0.5 + EaseOut(Value,Destination,(R)2 * factor - (R)1) / (T)2; | |
} | |
} | |
} | |
/////////////////////////////////////////////////////////////////////////////// | |
namespace Elastic | |
{ | |
template <class T, class R> | |
T EaseIn(T Value, T Destination, R factor) | |
{ | |
R nfac= factor; | |
#ifdef M_PI | |
const double pi = M_PI; | |
#else | |
const double pi = std::acos(-1); | |
#endif | |
const R v = factor - (R)1; | |
nfac = -std::pow((R)2,(R)10*v) * std::sin((v - (R)0.3 / (R)4) * (R)2 * pi / (R)0.3); | |
return Value + ((nfac) * (Destination - Value)); | |
} | |
template <class T, class R> | |
T EaseOut(T Value, T Destination, R factor) | |
{ | |
return (T)1 - EaseIn(Value, Destination, (R)1 - factor); | |
} | |
template <class T, class R> | |
T EaseInOut(T Value, T Destination, R factor) | |
{ | |
if (factor < (R)0.5) | |
{ | |
return EaseIn(Value,Destination,(R)2 * factor) / (T)2; | |
} | |
else | |
{ | |
return (T)0.5 + EaseOut(Value,Destination,(R)2 * factor - (R)1) / (T)2; | |
} | |
} | |
} | |
/////////////////////////////////////////////////////////////////////////////// | |
namespace Exponential | |
{ | |
template <class T, class R> | |
T EaseIn(T Value, T Destination, R factor) | |
{ | |
const R v = factor - (R)1; | |
return factor==(T)0 ? Value : Value + ((std::pow((R)2, (R)10 * v )) * (Destination - Value)); | |
} | |
template <class T, class R> | |
T EaseOut(T Value, T Destination, R factor) | |
{ | |
return (T)1 - EaseIn(Value, Destination, (R)1 - factor); | |
} | |
template <class T, class R> | |
T EaseInOut(T Value, T Destination, R factor) | |
{ | |
if (factor < 0.5) | |
{ | |
return EaseIn(Value,Destination,2 * factor) / (T)2; | |
} | |
else | |
{ | |
return (T)0.5 + EaseOut(Value,Destination,2 * factor - (R)1) / (T)2; | |
} | |
} | |
} | |
/////////////////////////////////////////////////////////////////////////////// | |
namespace Sine | |
{ | |
template <class T, class R> | |
T EaseIn(T Value, T Destination, R factor) | |
{ | |
#ifdef M_PI | |
const double pi = M_PI; | |
#else | |
const double pi = std::acos(-1); | |
#endif | |
return Value + (( (T)1 - std::cos((T)factor * (T)pi / (T)2)) * (Destination - Value)); | |
} | |
template <class T, class R> | |
T EaseOut(T Value, T Destination, R factor) | |
{ | |
return (T)1 - EaseIn(Value, Destination, (R)1 - factor); | |
} | |
template <class T, class R> | |
T EaseInOut(T Value, T Destination, R factor) | |
{ | |
if (factor < (R)0.5) | |
{ | |
return EaseIn(Value,Destination,(R)2 * factor) / (T)2; | |
} | |
else | |
{ | |
return (T)0.5 + EaseOut(Value,Destination,2 * factor - (R)1) / (T)2; | |
} | |
} | |
} | |
/////////////////////////////////////////////////////////////////////////////// | |
namespace Bounce | |
{ | |
template <class T, class R> | |
T EaseIn(T Value, T Destination, R factor) | |
{ | |
const R v = (R)1 - factor; | |
R c; | |
R d; | |
if (v < ((R)1 / (R)2.75)) | |
{ | |
c = v; | |
d = (R)0; | |
} | |
else if (v < ((R)2 / (R)2.75)) | |
{ | |
c = v - (R)1.5 / (R)2.75; | |
d = (R)0.75; | |
} | |
else if (v < ((R)2.5 / (R)2.75)) | |
{ | |
c = v - (R)2.25 / (R)2.75; | |
d = (R)0.9375; | |
} | |
else | |
{ | |
c = v - (R)2.625 / (R)2.75; | |
d = (R)0.984275; | |
} | |
return Value + (((R)1 - ((R)7.5625 * c * c + d)) * (Destination - Value)); | |
} | |
template <class T, class R> | |
T EaseOut(T Value, T Destination, R factor) | |
{ | |
return (T)1 - EaseIn(Value, Destination, (R)1 - factor); | |
} | |
template <class T, class R> | |
T EaseInOut(T Value, T Destination, R factor) | |
{ | |
if (factor < (R)0.5) | |
{ | |
return EaseIn(Value,Destination,2 * factor) / (T)2; | |
} | |
else | |
{ | |
return (T)0.5 + EaseOut(Value,Destination,2 * factor - (R)1) / (T)2; | |
} | |
} | |
} | |
/////////////////////////////////////////////////////////////////////////////// | |
namespace Back | |
{ | |
template <class T, class R> | |
T EaseIn(T Value, T Destination, R factor) | |
{ | |
return Value + ((factor * factor * ( ((R)1.70158+(R)1) * factor - (R)1.70158)) * (Destination - Value)); | |
} | |
template <class T, class R> | |
T EaseOut(T Value, T Destination, R factor) | |
{ | |
return (T)1 - EaseIn(Value, Destination, (R)1 - factor); | |
} | |
template <class T, class R> | |
T EaseInOut(T Value, T Destination, R factor) | |
{ | |
if (factor < (R)0.5) | |
{ | |
return EaseIn(Value,Destination,2 * factor) / (T)2; | |
} | |
else | |
{ | |
return (T)0.5 + EaseOut(Value,Destination,2 * factor - (R)1) / (T)2; | |
} | |
} | |
} | |
/////////////////////////////////////////////////////////////////////////////// | |
//Quadratic Bezier | |
template <class T, class R> | |
T BezierQuad(T A, T B, T C, R factor) | |
{ | |
const R v = (R)1 - factor; | |
return (v * v) * A + 2 * v * factor * B + (factor * factor) * C; | |
} | |
//Cubic Bezier | |
template <class T, class R> | |
T BezierCube(T A, T B, T C, T D, R factor) | |
{ | |
const R v = (R)1 - factor; | |
return (v * BezierQuad(A,B,C,factor)) + (factor * BezierQuad(B,C,D,factor)); | |
} | |
//Catmull-Rom | |
//A and D provide directional information | |
//Returned values are between B and C (But not always!) | |
template <class T, class R> | |
T CatmullRom(T A, T B, T C, T D, R factor) | |
{ | |
return 0.5 * ( | |
((T)2 * B) + | |
(-A + C) * factor + | |
((T)2 * A - (T)5 * B + (T)4 * C - D) * factor * factor + | |
(-A + (T)3 * B - (T)3 * C + D) * factor * factor * factor | |
); | |
} | |
//Lerp | |
template <class T, class R> | |
T Lerp(T A, T B, R factor) | |
{ | |
return A + ( factor * (B - A)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment