Skip to content

Instantly share code, notes, and snippets.

@kalineh
Created November 20, 2015 21:19
Show Gist options
  • Save kalineh/da7f048923dc18875ebd to your computer and use it in GitHub Desktop.
Save kalineh/da7f048923dc18875ebd to your computer and use it in GitHub Desktop.
Unity - Simple ease functions.
//
// extracted from http://dotween.demigiant.com/
//
using UnityEngine;
public class SimpleEase
: MonoBehaviour
{
static float _PiOver2 = Mathf.PI / 2.0f;
static float _TwoPi = Mathf.PI * 2.0f;
public static float Linear(float time, float duration = 1.0f, float overshootOrAmplitude = 0.1f, float period = 1.0f)
{
return time / duration;
}
public static float InSine(float time, float duration = 1.0f, float overshootOrAmplitude = 0.1f, float period = 1.0f)
{
return -(float)Mathf.Cos(time / duration * _PiOver2) + 1;
}
public static float OutSine(float time, float duration = 1.0f, float overshootOrAmplitude = 0.1f, float period = 1.0f)
{
return (float)Mathf.Sin(time / duration * _PiOver2);
}
public static float InOutSine(float time, float duration = 1.0f, float overshootOrAmplitude = 0.1f, float period = 1.0f)
{
return -0.5f * ((float)Mathf.Cos(Mathf.PI * time / duration) - 1);
}
public static float InQuad(float time, float duration = 1.0f, float overshootOrAmplitude = 0.1f, float period = 1.0f)
{
return (time /= duration) * time;
}
public static float OutQuad(float time, float duration = 1.0f, float overshootOrAmplitude = 0.1f, float period = 1.0f)
{
return -(time /= duration) * (time - 2);
}
public static float InOutQuad(float time, float duration = 1.0f, float overshootOrAmplitude = 0.1f, float period = 1.0f)
{
if ((time /= duration * 0.5f) < 1) return 0.5f * time * time;
return -0.5f * ((--time) * (time - 2) - 1);
}
public static float InCubic(float time, float duration = 1.0f, float overshootOrAmplitude = 0.1f, float period = 1.0f)
{
return (time /= duration) * time * time;
}
public static float OutCubic(float time, float duration = 1.0f, float overshootOrAmplitude = 0.1f, float period = 1.0f)
{
return ((time = time / duration - 1) * time * time + 1);
}
public static float InOutCubic(float time, float duration = 1.0f, float overshootOrAmplitude = 0.1f, float period = 1.0f)
{
if ((time /= duration * 0.5f) < 1) return 0.5f * time * time * time;
return 0.5f * ((time -= 2) * time * time + 2);
}
public static float InQuart(float time, float duration = 1.0f, float overshootOrAmplitude = 0.1f, float period = 1.0f)
{
return (time /= duration) * time * time * time;
}
public static float OutQuart(float time, float duration = 1.0f, float overshootOrAmplitude = 0.1f, float period = 1.0f)
{
return -((time = time / duration - 1) * time * time * time - 1);
}
public static float InOutQuart(float time, float duration = 1.0f, float overshootOrAmplitude = 0.1f, float period = 1.0f)
{
if ((time /= duration * 0.5f) < 1) return 0.5f * time * time * time * time;
return -0.5f * ((time -= 2) * time * time * time - 2);
}
public static float InQuint(float time, float duration = 1.0f, float overshootOrAmplitude = 0.1f, float period = 1.0f)
{
return (time /= duration) * time * time * time * time;
}
public static float OutQuint(float time, float duration = 1.0f, float overshootOrAmplitude = 0.1f, float period = 1.0f)
{
return ((time = time / duration - 1) * time * time * time * time + 1);
}
public static float InOutQuint(float time, float duration = 1.0f, float overshootOrAmplitude = 0.1f, float period = 1.0f)
{
if ((time /= duration * 0.5f) < 1) return 0.5f * time * time * time * time * time;
return 0.5f * ((time -= 2) * time * time * time * time + 2);
}
public static float InExpo(float time, float duration = 1.0f, float overshootOrAmplitude = 0.1f, float period = 1.0f)
{
return (time == 0) ? 0 : (float)Mathf.Pow(2, 10 * (time / duration - 1));
}
public static float OutExpo(float time, float duration = 1.0f, float overshootOrAmplitude = 0.1f, float period = 1.0f)
{
if (time == duration) return 1;
return (-(float)Mathf.Pow(2, -10 * time / duration) + 1);
}
public static float InOutExpo(float time, float duration = 1.0f, float overshootOrAmplitude = 0.1f, float period = 1.0f)
{
if (time == 0) return 0;
if (time == duration) return 1;
if ((time /= duration * 0.5f) < 1) return 0.5f * (float)Mathf.Pow(2, 10 * (time - 1));
return 0.5f * (-(float)Mathf.Pow(2, -10 * --time) + 2);
}
public static float InCirc(float time, float duration = 1.0f, float overshootOrAmplitude = 0.1f, float period = 1.0f)
{
return -((float)Mathf.Sqrt(1 - (time /= duration) * time) - 1);
}
public static float OutCirc(float time, float duration = 1.0f, float overshootOrAmplitude = 0.1f, float period = 1.0f)
{
return (float)Mathf.Sqrt(1 - (time = time / duration - 1) * time);
}
public static float InOutCirc(float time, float duration = 1.0f, float overshootOrAmplitude = 0.1f, float period = 1.0f)
{
if ((time /= duration * 0.5f) < 1) return -0.5f * ((float)Mathf.Sqrt(1 - time * time) - 1);
return 0.5f * ((float)Mathf.Sqrt(1 - (time -= 2) * time) + 1);
}
public static float InElastic(float time, float duration = 1.0f, float overshootOrAmplitude = 0.1f, float period = 1.0f)
{
float s0;
if (time == 0) return 0;
if ((time /= duration) == 1) return 1;
if (period == 0) period = duration * 0.3f;
if (overshootOrAmplitude < 1) {
overshootOrAmplitude = 1;
s0 = period / 4;
} else s0 = period / _TwoPi * (float)Mathf.Asin(1 / overshootOrAmplitude);
return -(overshootOrAmplitude * (float)Mathf.Pow(2, 10 * (time -= 1)) * (float)Mathf.Sin((time * duration - s0) * _TwoPi / period));
}
public static float OutElastic(float time, float duration = 1.0f, float overshootOrAmplitude = 0.1f, float period = 1.0f)
{
float s1;
if (time == 0) return 0;
if ((time /= duration) == 1) return 1;
if (period == 0) period = duration * 0.3f;
if (overshootOrAmplitude < 1) {
overshootOrAmplitude = 1;
s1 = period / 4;
} else s1 = period / _TwoPi * (float)Mathf.Asin(1 / overshootOrAmplitude);
return (overshootOrAmplitude * (float)Mathf.Pow(2, -10 * time) * (float)Mathf.Sin((time * duration - s1) * _TwoPi / period) + 1);
}
public static float InOutElastic(float time, float duration = 1.0f, float overshootOrAmplitude = 0.1f, float period = 1.0f)
{
float s;
if (time == 0) return 0;
if ((time /= duration * 0.5f) == 2) return 1;
if (period == 0) period = duration * (0.3f * 1.5f);
if (overshootOrAmplitude < 1) {
overshootOrAmplitude = 1;
s = period / 4;
} else s = period / _TwoPi * (float)Mathf.Asin(1 / overshootOrAmplitude);
if (time < 1) return -0.5f * (overshootOrAmplitude * (float)Mathf.Pow(2, 10 * (time -= 1)) * (float)Mathf.Sin((time * duration - s) * _TwoPi / period));
return overshootOrAmplitude * (float)Mathf.Pow(2, -10 * (time -= 1)) * (float)Mathf.Sin((time * duration - s) * _TwoPi / period) * 0.5f + 1;
}
public static float InBack(float time, float duration = 1.0f, float overshootOrAmplitude = 0.1f, float period = 1.0f)
{
return (time /= duration) * time * ((overshootOrAmplitude + 1) * time - overshootOrAmplitude);
}
public static float OutBack(float time, float duration = 1.0f, float overshootOrAmplitude = 0.1f, float period = 1.0f)
{
return ((time = time / duration - 1) * time * ((overshootOrAmplitude + 1) * time + overshootOrAmplitude) + 1);
}
public static float InOutBack(float time, float duration = 1.0f, float overshootOrAmplitude = 0.1f, float period = 1.0f)
{
if ((time /= duration * 0.5f) < 1) return 0.5f * (time * time * (((overshootOrAmplitude *= (1.525f)) + 1) * time - overshootOrAmplitude));
return 0.5f * ((time -= 2) * time * (((overshootOrAmplitude *= (1.525f)) + 1) * time + overshootOrAmplitude) + 2);
}
/*
public static float InBounce(float time, float duration = 1.0f, float overshootOrAmplitude = 0.1f, float period = 1.0f)
{
return Bounce.EaseIn(time, duration, overshootOrAmplitude, period);
}
public static float OutBounce(float time, float duration = 1.0f, float overshootOrAmplitude = 0.1f, float period = 1.0f)
{
return Bounce.EaseOut(time, duration, overshootOrAmplitude, period);
}
public static float InOutBounce(float time, float duration = 1.0f, float overshootOrAmplitude = 0.1f, float period = 1.0f)
{
return Bounce.EaseInOut(time, duration, overshootOrAmplitude, period);
}
public static float INTERNAL_Custom(float time, float duration = 1.0f, float overshootOrAmplitude = 0.1f, float period = 1.0f)
{
return customEase(time, duration, overshootOrAmplitude, period);
}
public static float INTERNAL_Zero(float time, float duration = 1.0f, float overshootOrAmplitude = 0.1f, float period = 1.0f)
{
return T();
}
*/
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment