Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Robert Penner's Easing Functions ported to Unity
/**
* PennerEasing
* Calculates a float value between two target values using
* Robert Penner's easing equations for interpolation over a specified duration.
*
* @author Darren David darren-code@lookorfeel.com
* @version 1.0
*
* Credit/Thanks:
* Robert Penner - The easing equations we all know and love
* (http://robertpenner.com/easing/) [See License.txt for license info]
*
* Lee Brimelow - initial port of Penner's equations to WPF
* (http://thewpfblog.com/?p=12)
*
* Zeh Fernando - additional equations (out/in) from
* caurina.transitions.Tweener (http://code.google.com/p/tweener/)
* [See License.txt for license info]
*
* Michael Stevenson - Unity port
*/
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
/// <summary>
/// Calculates a float value between two target values using
/// Robert Penner's easing equations for interpolation over a specified duration.
/// </summary>
public class PennerEasing
{
/// <summary>
/// Enumeration of all easing equations.
/// </summary>
public enum Equations
{
Linear,
QuadEaseOut, QuadEaseIn, QuadEaseInOut, QuadEaseOutIn,
ExpoEaseOut, ExpoEaseIn, ExpoEaseInOut, ExpoEaseOutIn,
CubicEaseOut, CubicEaseIn, CubicEaseInOut, CubicEaseOutIn,
QuartEaseOut, QuartEaseIn, QuartEaseInOut, QuartEaseOutIn,
QuintEaseOut, QuintEaseIn, QuintEaseInOut, QuintEaseOutIn,
CircEaseOut, CircEaseIn, CircEaseInOut, CircEaseOutIn,
SineEaseOut, SineEaseIn, SineEaseInOut, SineEaseOutIn,
ElasticEaseOut, ElasticEaseIn, ElasticEaseInOut, ElasticEaseOutIn,
BounceEaseOut, BounceEaseIn, BounceEaseInOut, BounceEaseOutIn,
BackEaseOut, BackEaseIn, BackEaseInOut, BackEaseOutIn
}
/// <summary>
/// The easing equation to use.
/// </summary>
public readonly Equations equation;
/// <summary>
/// Starting value for the easing function.
/// </summary>
public readonly float from;
/// <summary>
/// Ending value for the easing function.
/// </summary>
public readonly float to;
/// <summary>
/// Duration of the ease.
/// </summary>
public readonly float duration;
public PennerEasing( Equations type, float from, float to, float duration = 1 )
{
this.equation = type;
this.from = from;
this.to = to;
this.duration = duration;
}
// Beware infinite loops. Loop over these values within a Coroutine,
// yielding after each value.
public IEnumerable<float> ValuesOverTime()
{
float startTime = Time.time;
for ( float t = 0; t < duration; t += Time.time - startTime ) {
yield return Evaluate( t );
}
yield return Evaluate( duration );
}
public float Evaluate (float time)
{
switch (equation) {
case Equations.Linear:
return Linear (time, from, to, duration);
break;
case Equations.QuadEaseOut:
return QuadEaseOut (time, from, to, duration);
break;
case Equations.QuadEaseIn:
return QuadEaseIn (time, from, to, duration);
break;
case Equations.QuadEaseInOut:
return QuadEaseInOut (time, from, to, duration);
break;
case Equations.QuadEaseOutIn:
return QuadEaseOutIn (time, from, to, duration);
break;
case Equations.ExpoEaseOut:
return ExpoEaseOut (time, from, to, duration);
break;
case Equations.ExpoEaseIn:
return ExpoEaseIn (time, from, to, duration);
break;
case Equations.ExpoEaseInOut:
return ExpoEaseInOut (time, from, to, duration);
break;
case Equations.ExpoEaseOutIn:
return ExpoEaseOutIn (time, from, to, duration);
break;
case Equations.CubicEaseOut:
return CubicEaseOut (time, from, to, duration);
break;
case Equations.CubicEaseIn:
return CubicEaseIn (time, from, to, duration);
break;
case Equations.CubicEaseInOut:
return CubicEaseInOut (time, from, to, duration);
break;
case Equations.CubicEaseOutIn:
return CubicEaseOutIn (time, from, to, duration);
break;
case Equations.QuartEaseOut:
return QuartEaseOut (time, from, to, duration);
break;
case Equations.QuartEaseIn:
return QuartEaseIn (time, from, to, duration);
break;
case Equations.QuartEaseInOut:
return QuartEaseInOut (time, from, to, duration);
break;
case Equations.QuartEaseOutIn:
return QuartEaseOutIn (time, from, to, duration);
break;
case Equations.QuintEaseOut:
return QuintEaseOut (time, from, to, duration);
break;
case Equations.QuintEaseIn:
return QuintEaseIn (time, from, to, duration);
break;
case Equations.QuintEaseInOut:
return QuintEaseInOut (time, from, to, duration);
break;
case Equations.QuintEaseOutIn:
return QuintEaseOutIn (time, from, to, duration);
break;
case Equations.CircEaseOut:
return CircEaseOut (time, from, to, duration);
break;
case Equations.CircEaseIn:
return CircEaseIn (time, from, to, duration);
break;
case Equations.CircEaseInOut:
return CircEaseInOut (time, from, to, duration);
break;
case Equations.CircEaseOutIn:
return CircEaseOutIn (time, from, to, duration);
break;
case Equations.SineEaseOut:
return SineEaseOut (time, from, to, duration);
break;
case Equations.SineEaseIn:
return SineEaseIn (time, from, to, duration);
break;
case Equations.SineEaseInOut:
return SineEaseInOut (time, from, to, duration);
break;
case Equations.SineEaseOutIn:
return SineEaseOutIn (time, from, to, duration);
break;
case Equations.ElasticEaseOut:
return ElasticEaseOut (time, from, to, duration);
break;
case Equations.ElasticEaseIn:
return ElasticEaseIn (time, from, to, duration);
break;
case Equations.ElasticEaseInOut:
return ElasticEaseInOut (time, from, to, duration);
break;
case Equations.ElasticEaseOutIn:
return ElasticEaseOutIn (time, from, to, duration);
break;
case Equations.BounceEaseOut:
return BounceEaseOut (time, from, to, duration);
break;
case Equations.BounceEaseIn:
return BounceEaseIn (time, from, to, duration);
break;
case Equations.BounceEaseInOut:
return BounceEaseInOut (time, from, to, duration);
break;
case Equations.BounceEaseOutIn:
return BounceEaseOutIn (time, from, to, duration);
break;
case Equations.BackEaseOut:
return BackEaseOut (time, from, to, duration);
break;
case Equations.BackEaseIn:
return BackEaseIn (time, from, to, duration);
break;
case Equations.BackEaseInOut:
return BackEaseInOut (time, from, to, duration);
break;
case Equations.BackEaseOutIn:
return BackEaseOutIn (time, from, to, duration);
break;
default:
Debug.LogError ("Equation not supported: " + equation);
return 0;
}
}
#region Linear
/// <summary>
/// Easing equation function for a simple linear tweening, with no easing.
/// </summary>
/// <param name="t">Current time in seconds.</param>
/// <param name="b">Starting value.</param>
/// <param name="c">Final value.</param>
/// <param name="d">Duration of ease.</param>
/// <returns>The correct value.</returns>
public static float Linear( float t, float b, float c, float d )
{
return c * t / d + b;
}
#endregion
#region Expo
/// <summary>
/// Easing equation function for an exponential (2^t) easing out:
/// decelerating from zero velocity.
/// </summary>
/// <param name="t">Current time in seconds.</param>
/// <param name="b">Starting value.</param>
/// <param name="c">Final value.</param>
/// <param name="d">Duration of ease.</param>
/// <returns>The correct value.</returns>
public static float ExpoEaseOut( float t, float b, float c, float d )
{
return ( t == d ) ? b + c : c * ( -Mathf.Pow( 2, -10 * t / d ) + 1 ) + b;
}
/// <summary>
/// Easing equation function for an exponential (2^t) easing in:
/// accelerating from zero velocity.
/// </summary>
/// <param name="t">Current time in seconds.</param>
/// <param name="b">Starting value.</param>
/// <param name="c">Final value.</param>
/// <param name="d">Duration of ease.</param>
/// <returns>The correct value.</returns>
public static float ExpoEaseIn( float t, float b, float c, float d )
{
return ( t == 0 ) ? b : c * Mathf.Pow( 2, 10 * ( t / d - 1 ) ) + b;
}
/// <summary>
/// Easing equation function for an exponential (2^t) easing in/out:
/// acceleration until halfway, then deceleration.
/// </summary>
/// <param name="t">Current time in seconds.</param>
/// <param name="b">Starting value.</param>
/// <param name="c">Final value.</param>
/// <param name="d">Duration of ease.</param>
/// <returns>The correct value.</returns>
public static float ExpoEaseInOut( float t, float b, float c, float d )
{
if ( t == 0 )
return b;
if ( t == d )
return b + c;
if ( ( t /= d / 2 ) < 1 )
return c / 2 * Mathf.Pow( 2, 10 * ( t - 1 ) ) + b;
return c / 2 * ( -Mathf.Pow( 2, -10 * --t ) + 2 ) + b;
}
/// <summary>
/// Easing equation function for an exponential (2^t) easing out/in:
/// deceleration until halfway, then acceleration.
/// </summary>
/// <param name="t">Current time in seconds.</param>
/// <param name="b">Starting value.</param>
/// <param name="c">Final value.</param>
/// <param name="d">Duration of ease.</param>
/// <returns>The correct value.</returns>
public static float ExpoEaseOutIn( float t, float b, float c, float d )
{
if ( t < d / 2 )
return ExpoEaseOut( t * 2, b, c / 2, d );
return ExpoEaseIn( ( t * 2 ) - d, b + c / 2, c / 2, d );
}
#endregion
#region Circular
/// <summary>
/// Easing equation function for a circular (sqrt(1-t^2)) easing out:
/// decelerating from zero velocity.
/// </summary>
/// <param name="t">Current time in seconds.</param>
/// <param name="b">Starting value.</param>
/// <param name="c">Final value.</param>
/// <param name="d">Duration of ease.</param>
/// <returns>The correct value.</returns>
public static float CircEaseOut( float t, float b, float c, float d )
{
return c * Mathf.Sqrt( 1 - ( t = t / d - 1 ) * t ) + b;
}
/// <summary>
/// Easing equation function for a circular (sqrt(1-t^2)) easing in:
/// accelerating from zero velocity.
/// </summary>
/// <param name="t">Current time in seconds.</param>
/// <param name="b">Starting value.</param>
/// <param name="c">Final value.</param>
/// <param name="d">Duration of ease.</param>
/// <returns>The correct value.</returns>
public static float CircEaseIn( float t, float b, float c, float d )
{
return -c * ( Mathf.Sqrt( 1 - ( t /= d ) * t ) - 1 ) + b;
}
/// <summary>
/// Easing equation function for a circular (sqrt(1-t^2)) easing in/out:
/// acceleration until halfway, then deceleration.
/// </summary>
/// <param name="t">Current time in seconds.</param>
/// <param name="b">Starting value.</param>
/// <param name="c">Final value.</param>
/// <param name="d">Duration of ease.</param>
/// <returns>The correct value.</returns>
public static float CircEaseInOut( float t, float b, float c, float d )
{
if ( ( t /= d / 2 ) < 1 )
return -c / 2 * ( Mathf.Sqrt( 1 - t * t ) - 1 ) + b;
return c / 2 * ( Mathf.Sqrt( 1 - ( t -= 2 ) * t ) + 1 ) + b;
}
/// <summary>
/// Easing equation function for a circular (sqrt(1-t^2)) easing in/out:
/// acceleration until halfway, then deceleration.
/// </summary>
/// <param name="t">Current time in seconds.</param>
/// <param name="b">Starting value.</param>
/// <param name="c">Final value.</param>
/// <param name="d">Duration of ease.</param>
/// <returns>The correct value.</returns>
public static float CircEaseOutIn( float t, float b, float c, float d )
{
if ( t < d / 2 )
return CircEaseOut( t * 2, b, c / 2, d );
return CircEaseIn( ( t * 2 ) - d, b + c / 2, c / 2, d );
}
#endregion
#region Quad
/// <summary>
/// Easing equation function for a quadratic (t^2) easing out:
/// decelerating from zero velocity.
/// </summary>
/// <param name="t">Current time in seconds.</param>
/// <param name="b">Starting value.</param>
/// <param name="c">Final value.</param>
/// <param name="d">Duration of ease.</param>
/// <returns>The correct value.</returns>
public static float QuadEaseOut( float t, float b, float c, float d )
{
return -c * ( t /= d ) * ( t - 2 ) + b;
}
/// <summary>
/// Easing equation function for a quadratic (t^2) easing in:
/// accelerating from zero velocity.
/// </summary>
/// <param name="t">Current time in seconds.</param>
/// <param name="b">Starting value.</param>
/// <param name="c">Final value.</param>
/// <param name="d">Duration of ease.</param>
/// <returns>The correct value.</returns>
public static float QuadEaseIn( float t, float b, float c, float d )
{
return c * ( t /= d ) * t + b;
}
/// <summary>
/// Easing equation function for a quadratic (t^2) easing in/out:
/// acceleration until halfway, then deceleration.
/// </summary>
/// <param name="t">Current time in seconds.</param>
/// <param name="b">Starting value.</param>
/// <param name="c">Final value.</param>
/// <param name="d">Duration of ease.</param>
/// <returns>The correct value.</returns>
public static float QuadEaseInOut( float t, float b, float c, float d )
{
if ( ( t /= d / 2 ) < 1 )
return c / 2 * t * t + b;
return -c / 2 * ( ( --t ) * ( t - 2 ) - 1 ) + b;
}
/// <summary>
/// Easing equation function for a quadratic (t^2) easing out/in:
/// deceleration until halfway, then acceleration.
/// </summary>
/// <param name="t">Current time in seconds.</param>
/// <param name="b">Starting value.</param>
/// <param name="c">Final value.</param>
/// <param name="d">Duration of ease.</param>
/// <returns>The correct value.</returns>
public static float QuadEaseOutIn( float t, float b, float c, float d )
{
if ( t < d / 2 )
return QuadEaseOut( t * 2, b, c / 2, d );
return QuadEaseIn( ( t * 2 ) - d, b + c / 2, c / 2, d );
}
#endregion
#region Sine
/// <summary>
/// Easing equation function for a sinusoidal (sin(t)) easing out:
/// decelerating from zero velocity.
/// </summary>
/// <param name="t">Current time in seconds.</param>
/// <param name="b">Starting value.</param>
/// <param name="c">Final value.</param>
/// <param name="d">Duration of ease.</param>
/// <returns>The correct value.</returns>
public static float SineEaseOut( float t, float b, float c, float d )
{
return c * Mathf.Sin( t / d * ( Mathf.PI / 2 ) ) + b;
}
/// <summary>
/// Easing equation function for a sinusoidal (sin(t)) easing in:
/// accelerating from zero velocity.
/// </summary>
/// <param name="t">Current time in seconds.</param>
/// <param name="b">Starting value.</param>
/// <param name="c">Final value.</param>
/// <param name="d">Duration of ease.</param>
/// <returns>The correct value.</returns>
public static float SineEaseIn( float t, float b, float c, float d )
{
return -c * Mathf.Cos( t / d * ( Mathf.PI / 2 ) ) + c + b;
}
/// <summary>
/// Easing equation function for a sinusoidal (sin(t)) easing in/out:
/// acceleration until halfway, then deceleration.
/// </summary>
/// <param name="t">Current time in seconds.</param>
/// <param name="b">Starting value.</param>
/// <param name="c">Final value.</param>
/// <param name="d">Duration of ease.</param>
/// <returns>The correct value.</returns>
public static float SineEaseInOut( float t, float b, float c, float d )
{
if ( ( t /= d / 2 ) < 1 )
return c / 2 * ( Mathf.Sin( Mathf.PI * t / 2 ) ) + b;
return -c / 2 * ( Mathf.Cos( Mathf.PI * --t / 2 ) - 2 ) + b;
}
/// <summary>
/// Easing equation function for a sinusoidal (sin(t)) easing in/out:
/// deceleration until halfway, then acceleration.
/// </summary>
/// <param name="t">Current time in seconds.</param>
/// <param name="b">Starting value.</param>
/// <param name="c">Final value.</param>
/// <param name="d">Duration of ease.</param>
/// <returns>The correct value.</returns>
public static float SineEaseOutIn( float t, float b, float c, float d )
{
if ( t < d / 2 )
return SineEaseOut( t * 2, b, c / 2, d );
return SineEaseIn( ( t * 2 ) - d, b + c / 2, c / 2, d );
}
#endregion
#region Cubic
/// <summary>
/// Easing equation function for a cubic (t^3) easing out:
/// decelerating from zero velocity.
/// </summary>
/// <param name="t">Current time in seconds.</param>
/// <param name="b">Starting value.</param>
/// <param name="c">Final value.</param>
/// <param name="d">Duration of ease.</param>
/// <returns>The correct value.</returns>
public static float CubicEaseOut( float t, float b, float c, float d )
{
return c * ( ( t = t / d - 1 ) * t * t + 1 ) + b;
}
/// <summary>
/// Easing equation function for a cubic (t^3) easing in:
/// accelerating from zero velocity.
/// </summary>
/// <param name="t">Current time in seconds.</param>
/// <param name="b">Starting value.</param>
/// <param name="c">Final value.</param>
/// <param name="d">Duration of ease.</param>
/// <returns>The correct value.</returns>
public static float CubicEaseIn( float t, float b, float c, float d )
{
return c * ( t /= d ) * t * t + b;
}
/// <summary>
/// Easing equation function for a cubic (t^3) easing in/out:
/// acceleration until halfway, then deceleration.
/// </summary>
/// <param name="t">Current time in seconds.</param>
/// <param name="b">Starting value.</param>
/// <param name="c">Final value.</param>
/// <param name="d">Duration of ease.</param>
/// <returns>The correct value.</returns>
public static float CubicEaseInOut( float t, float b, float c, float d )
{
if ( ( t /= d / 2 ) < 1 )
return c / 2 * t * t * t + b;
return c / 2 * ( ( t -= 2 ) * t * t + 2 ) + b;
}
/// <summary>
/// Easing equation function for a cubic (t^3) easing out/in:
/// deceleration until halfway, then acceleration.
/// </summary>
/// <param name="t">Current time in seconds.</param>
/// <param name="b">Starting value.</param>
/// <param name="c">Final value.</param>
/// <param name="d">Duration of ease.</param>
/// <returns>The correct value.</returns>
public static float CubicEaseOutIn( float t, float b, float c, float d )
{
if ( t < d / 2 )
return CubicEaseOut( t * 2, b, c / 2, d );
return CubicEaseIn( ( t * 2 ) - d, b + c / 2, c / 2, d );
}
#endregion
#region Quartic
/// <summary>
/// Easing equation function for a quartic (t^4) easing out:
/// decelerating from zero velocity.
/// </summary>
/// <param name="t">Current time in seconds.</param>
/// <param name="b">Starting value.</param>
/// <param name="c">Final value.</param>
/// <param name="d">Duration of ease.</param>
/// <returns>The correct value.</returns>
public static float QuartEaseOut( float t, float b, float c, float d )
{
return -c * ( ( t = t / d - 1 ) * t * t * t - 1 ) + b;
}
/// <summary>
/// Easing equation function for a quartic (t^4) easing in:
/// accelerating from zero velocity.
/// </summary>
/// <param name="t">Current time in seconds.</param>
/// <param name="b">Starting value.</param>
/// <param name="c">Final value.</param>
/// <param name="d">Duration of ease.</param>
/// <returns>The correct value.</returns>
public static float QuartEaseIn( float t, float b, float c, float d )
{
return c * ( t /= d ) * t * t * t + b;
}
/// <summary>
/// Easing equation function for a quartic (t^4) easing in/out:
/// acceleration until halfway, then deceleration.
/// </summary>
/// <param name="t">Current time in seconds.</param>
/// <param name="b">Starting value.</param>
/// <param name="c">Final value.</param>
/// <param name="d">Duration of ease.</param>
/// <returns>The correct value.</returns>
public static float QuartEaseInOut( float t, float b, float c, float d )
{
if ( ( t /= d / 2 ) < 1 )
return c / 2 * t * t * t * t + b;
return -c / 2 * ( ( t -= 2 ) * t * t * t - 2 ) + b;
}
/// <summary>
/// Easing equation function for a quartic (t^4) easing out/in:
/// deceleration until halfway, then acceleration.
/// </summary>
/// <param name="t">Current time in seconds.</param>
/// <param name="b">Starting value.</param>
/// <param name="c">Final value.</param>
/// <param name="d">Duration of ease.</param>
/// <returns>The correct value.</returns>
public static float QuartEaseOutIn( float t, float b, float c, float d )
{
if ( t < d / 2 )
return QuartEaseOut( t * 2, b, c / 2, d );
return QuartEaseIn( ( t * 2 ) - d, b + c / 2, c / 2, d );
}
#endregion
#region Quintic
/// <summary>
/// Easing equation function for a quintic (t^5) easing out:
/// decelerating from zero velocity.
/// </summary>
/// <param name="t">Current time in seconds.</param>
/// <param name="b">Starting value.</param>
/// <param name="c">Final value.</param>
/// <param name="d">Duration of ease.</param>
/// <returns>The correct value.</returns>
public static float QuintEaseOut( float t, float b, float c, float d )
{
return c * ( ( t = t / d - 1 ) * t * t * t * t + 1 ) + b;
}
/// <summary>
/// Easing equation function for a quintic (t^5) easing in:
/// accelerating from zero velocity.
/// </summary>
/// <param name="t">Current time in seconds.</param>
/// <param name="b">Starting value.</param>
/// <param name="c">Final value.</param>
/// <param name="d">Duration of ease.</param>
/// <returns>The correct value.</returns>
public static float QuintEaseIn( float t, float b, float c, float d )
{
return c * ( t /= d ) * t * t * t * t + b;
}
/// <summary>
/// Easing equation function for a quintic (t^5) easing in/out:
/// acceleration until halfway, then deceleration.
/// </summary>
/// <param name="t">Current time in seconds.</param>
/// <param name="b">Starting value.</param>
/// <param name="c">Final value.</param>
/// <param name="d">Duration of ease.</param>
/// <returns>The correct value.</returns>
public static float QuintEaseInOut( float t, float b, float c, float d )
{
if ( ( t /= d / 2 ) < 1 )
return c / 2 * t * t * t * t * t + b;
return c / 2 * ( ( t -= 2 ) * t * t * t * t + 2 ) + b;
}
/// <summary>
/// Easing equation function for a quintic (t^5) easing in/out:
/// acceleration until halfway, then deceleration.
/// </summary>
/// <param name="t">Current time in seconds.</param>
/// <param name="b">Starting value.</param>
/// <param name="c">Final value.</param>
/// <param name="d">Duration of ease.</param>
/// <returns>The correct value.</returns>
public static float QuintEaseOutIn( float t, float b, float c, float d )
{
if ( t < d / 2 )
return QuintEaseOut( t * 2, b, c / 2, d );
return QuintEaseIn( ( t * 2 ) - d, b + c / 2, c / 2, d );
}
#endregion
#region Elastic
/// <summary>
/// Easing equation function for an elastic (exponentially decaying sine wave) easing out:
/// decelerating from zero velocity.
/// </summary>
/// <param name="t">Current time in seconds.</param>
/// <param name="b">Starting value.</param>
/// <param name="c">Final value.</param>
/// <param name="d">Duration of ease.</param>
/// <returns>The correct value.</returns>
public static float ElasticEaseOut( float t, float b, float c, float d )
{
if ( ( t /= d ) == 1 )
return b + c;
float p = d * .3f;
float s = p / 4;
return ( c * Mathf.Pow( 2, -10 * t ) * Mathf.Sin( ( t * d - s ) * ( 2 * Mathf.PI ) / p ) + c + b );
}
/// <summary>
/// Easing equation function for an elastic (exponentially decaying sine wave) easing in:
/// accelerating from zero velocity.
/// </summary>
/// <param name="t">Current time in seconds.</param>
/// <param name="b">Starting value.</param>
/// <param name="c">Final value.</param>
/// <param name="d">Duration of ease.</param>
/// <returns>The correct value.</returns>
public static float ElasticEaseIn( float t, float b, float c, float d )
{
if ( ( t /= d ) == 1 )
return b + c;
float p = d * .3f;
float s = p / 4;
return -( c * Mathf.Pow( 2, 10 * ( t -= 1 ) ) * Mathf.Sin( ( t * d - s ) * ( 2 * Mathf.PI ) / p ) ) + b;
}
/// <summary>
/// Easing equation function for an elastic (exponentially decaying sine wave) easing in/out:
/// acceleration until halfway, then deceleration.
/// </summary>
/// <param name="t">Current time in seconds.</param>
/// <param name="b">Starting value.</param>
/// <param name="c">Final value.</param>
/// <param name="d">Duration of ease.</param>
/// <returns>The correct value.</returns>
public static float ElasticEaseInOut( float t, float b, float c, float d )
{
if ( ( t /= d / 2 ) == 2 )
return b + c;
float p = d * ( .3f * 1.5f );
float s = p / 4;
if ( t < 1 )
return -.5f * ( c * Mathf.Pow( 2, 10 * ( t -= 1 ) ) * Mathf.Sin( ( t * d - s ) * ( 2 * Mathf.PI ) / p ) ) + b;
return c * Mathf.Pow( 2, -10 * ( t -= 1 ) ) * Mathf.Sin( ( t * d - s ) * ( 2 * Mathf.PI ) / p ) * .5f + c + b;
}
/// <summary>
/// Easing equation function for an elastic (exponentially decaying sine wave) easing out/in:
/// deceleration until halfway, then acceleration.
/// </summary>
/// <param name="t">Current time in seconds.</param>
/// <param name="b">Starting value.</param>
/// <param name="c">Final value.</param>
/// <param name="d">Duration of ease.</param>
/// <returns>The correct value.</returns>
public static float ElasticEaseOutIn( float t, float b, float c, float d )
{
if ( t < d / 2 )
return ElasticEaseOut( t * 2, b, c / 2, d );
return ElasticEaseIn( ( t * 2 ) - d, b + c / 2, c / 2, d );
}
#endregion
#region Bounce
/// <summary>
/// Easing equation function for a bounce (exponentially decaying parabolic bounce) easing out:
/// decelerating from zero velocity.
/// </summary>
/// <param name="t">Current time in seconds.</param>
/// <param name="b">Starting value.</param>
/// <param name="c">Final value.</param>
/// <param name="d">Duration of ease.</param>
/// <returns>The correct value.</returns>
public static float BounceEaseOut( float t, float b, float c, float d )
{
if ( ( t /= d ) < ( 1 / 2.75f ) )
return c * ( 7.5625f * t * t ) + b;
else if ( t < ( 2 / 2.75f ) )
return c * ( 7.5625f * ( t -= ( 1.5f / 2.75f ) ) * t + .75f ) + b;
else if ( t < ( 2.5 / 2.75f ) )
return c * ( 7.5625f * ( t -= ( 2.25f / 2.75f ) ) * t + .9375f ) + b;
else
return c * ( 7.5625f * ( t -= ( 2.625f / 2.75f ) ) * t + .984375f ) + b;
}
/// <summary>
/// Easing equation function for a bounce (exponentially decaying parabolic bounce) easing in:
/// accelerating from zero velocity.
/// </summary>
/// <param name="t">Current time in seconds.</param>
/// <param name="b">Starting value.</param>
/// <param name="c">Final value.</param>
/// <param name="d">Duration of ease.</param>
/// <returns>The correct value.</returns>
public static float BounceEaseIn( float t, float b, float c, float d )
{
return c - BounceEaseOut( d - t, 0, c, d ) + b;
}
/// <summary>
/// Easing equation function for a bounce (exponentially decaying parabolic bounce) easing in/out:
/// acceleration until halfway, then deceleration.
/// </summary>
/// <param name="t">Current time in seconds.</param>
/// <param name="b">Starting value.</param>
/// <param name="c">Final value.</param>
/// <param name="d">Duration of ease.</param>
/// <returns>The correct value.</returns>
public static float BounceEaseInOut( float t, float b, float c, float d )
{
if ( t < d / 2 )
return BounceEaseIn( t * 2, 0, c, d ) * .5f + b;
else
return BounceEaseOut( t * 2 - d, 0, c, d ) * .5f + c * .5f + b;
}
/// <summary>
/// Easing equation function for a bounce (exponentially decaying parabolic bounce) easing out/in:
/// deceleration until halfway, then acceleration.
/// </summary>
/// <param name="t">Current time in seconds.</param>
/// <param name="b">Starting value.</param>
/// <param name="c">Final value.</param>
/// <param name="d">Duration of ease.</param>
/// <returns>The correct value.</returns>
public static float BounceEaseOutIn( float t, float b, float c, float d )
{
if ( t < d / 2 )
return BounceEaseOut( t * 2, b, c / 2, d );
return BounceEaseIn( ( t * 2 ) - d, b + c / 2, c / 2, d );
}
#endregion
#region Back
/// <summary>
/// Easing equation function for a back (overshooting cubic easing: (s+1)*t^3 - s*t^2) easing out:
/// decelerating from zero velocity.
/// </summary>
/// <param name="t">Current time in seconds.</param>
/// <param name="b">Starting value.</param>
/// <param name="c">Final value.</param>
/// <param name="d">Duration of ease.</param>
/// <returns>The correct value.</returns>
public static float BackEaseOut( float t, float b, float c, float d )
{
return c * ( ( t = t / d - 1 ) * t * ( ( 1.70158f + 1 ) * t + 1.70158f ) + 1 ) + b;
}
/// <summary>
/// Easing equation function for a back (overshooting cubic easing: (s+1)*t^3 - s*t^2) easing in:
/// accelerating from zero velocity.
/// </summary>
/// <param name="t">Current time in seconds.</param>
/// <param name="b">Starting value.</param>
/// <param name="c">Final value.</param>
/// <param name="d">Duration of ease.</param>
/// <returns>The correct value.</returns>
public static float BackEaseIn( float t, float b, float c, float d )
{
return c * ( t /= d ) * t * ( ( 1.70158f + 1 ) * t - 1.70158f ) + b;
}
/// <summary>
/// Easing equation function for a back (overshooting cubic easing: (s+1)*t^3 - s*t^2) easing in/out:
/// acceleration until halfway, then deceleration.
/// </summary>
/// <param name="t">Current time in seconds.</param>
/// <param name="b">Starting value.</param>
/// <param name="c">Final value.</param>
/// <param name="d">Duration of ease.</param>
/// <returns>The correct value.</returns>
public static float BackEaseInOut( float t, float b, float c, float d )
{
float s = 1.70158f;
if ( ( t /= d / 2 ) < 1 )
return c / 2 * ( t * t * ( ( ( s *= ( 1.525f ) ) + 1 ) * t - s ) ) + b;
return c / 2 * ( ( t -= 2 ) * t * ( ( ( s *= ( 1.525f ) ) + 1 ) * t + s ) + 2 ) + b;
}
/// <summary>
/// Easing equation function for a back (overshooting cubic easing: (s+1)*t^3 - s*t^2) easing out/in:
/// deceleration until halfway, then acceleration.
/// </summary>
/// <param name="t">Current time in seconds.</param>
/// <param name="b">Starting value.</param>
/// <param name="c">Final value.</param>
/// <param name="d">Duration of ease.</param>
/// <returns>The correct value.</returns>
public static float BackEaseOutIn( float t, float b, float c, float d )
{
if ( t < d / 2 )
return BackEaseOut( t * 2, b, c / 2, d );
return BackEaseIn( ( t * 2 ) - d, b + c / 2, c / 2, d );
}
#endregion
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment