|
using UnityEngine; |
|
|
|
public static partial class Interpolation |
|
{ |
|
const float PI = Mathf.PI; |
|
const float HALF_PI = PI * 0.5f; |
|
const float TWO_PI = PI * 2f; |
|
|
|
const float C1 = 1.70158f; |
|
const float C2 = C1 * 1.525f; |
|
const float C3 = C1 + 1f; |
|
|
|
const float C4 = TWO_PI / 3f; |
|
const float C5 = TWO_PI / 4.5f; |
|
|
|
const float N1 = 7.5625f; |
|
const float D1 = 2.75f; |
|
|
|
private static float Linear(float t) |
|
{ |
|
return t; |
|
} |
|
|
|
/* Sine */ |
|
private static float EaseInSine(float t) |
|
{ |
|
return 1f - Mathf.Cos(t * HALF_PI); |
|
} |
|
|
|
private static float EaseOutSine(float t) |
|
{ |
|
return Mathf.Sin(t * HALF_PI); |
|
} |
|
|
|
private static float EaseInOutSine(float t) |
|
{ |
|
return -(Mathf.Cos(PI * t) - 1f) / 2f; |
|
} |
|
|
|
/* Quad */ |
|
private static float EaseInQuad(float t) |
|
{ |
|
return t * t; |
|
} |
|
|
|
private static float EaseOutQuad(float t) |
|
{ |
|
return 1f - EaseInQuad(1f - t); |
|
} |
|
|
|
private static float EaseInOutQuad(float t) |
|
{ |
|
if (t < 0.5f) |
|
{ |
|
return EaseInQuad(t * 2f) / 2f; |
|
} |
|
else |
|
{ |
|
return 1f - EaseInQuad((1f - t) * 2f) / 2f; |
|
} |
|
} |
|
|
|
/* Cubic */ |
|
private static float EaseInCubic(float t) |
|
{ |
|
return t * t * t; |
|
} |
|
|
|
private static float EaseOutCubic(float t) |
|
{ |
|
return 1f - EaseInCubic(1f - t); |
|
} |
|
|
|
private static float EaseInOutCubic(float t) |
|
{ |
|
if (t < 0.5f) |
|
{ |
|
return EaseInCubic(t * 2f) / 2f; |
|
} |
|
else |
|
{ |
|
return 1f - EaseInCubic((1f - t) * 2f) / 2f; |
|
} |
|
} |
|
|
|
/* Quart */ |
|
private static float EaseInQuart(float t) |
|
{ |
|
return t * t * t * t; |
|
} |
|
|
|
private static float EaseOutQuart(float t) |
|
{ |
|
return 1f - EaseInQuart(1f - t); |
|
} |
|
|
|
private static float EaseInOutQuart(float t) |
|
{ |
|
if (t < 0.5f) |
|
{ |
|
return EaseInQuart(t * 2f) / 2f; |
|
} |
|
else |
|
{ |
|
return 1f - EaseInQuart((1f - t) * 2f) / 2f; |
|
} |
|
} |
|
|
|
/* Quint */ |
|
private static float EaseInQuint(float t) |
|
{ |
|
return t * t * t * t * t; |
|
} |
|
|
|
private static float EaseOutQuint(float t) |
|
{ |
|
return 1f - EaseInQuint(1f - t); |
|
} |
|
|
|
private static float EaseInOutQuint(float t) |
|
{ |
|
if (t < 0.5f) |
|
{ |
|
return EaseInQuint(t * 2f) / 2f; |
|
} |
|
else |
|
{ |
|
return 1f - EaseInQuint((1f - t) * 2f) / 2f; |
|
} |
|
} |
|
|
|
/* Expo */ |
|
private static float EaseInExpo(float t) |
|
{ |
|
if (t == 0f) |
|
{ |
|
return 0f; |
|
} |
|
else |
|
{ |
|
return Mathf.Pow(2f, 10f * (t - 1f)); |
|
} |
|
} |
|
|
|
private static float EaseOutExpo(float t) |
|
{ |
|
if (t == 1f) |
|
{ |
|
return 1f; |
|
} |
|
else |
|
{ |
|
return 1f - EaseInExpo(1f - t); |
|
} |
|
} |
|
|
|
private static float EaseInOutExpo(float t) |
|
{ |
|
if (t < 0.5f) |
|
{ |
|
return EaseInExpo(t * 2f) / 2f; |
|
} |
|
else |
|
{ |
|
return 1f - EaseInExpo((1f - t) * 2f) / 2f; |
|
} |
|
} |
|
|
|
/* Circ */ |
|
private static float EaseInCirc(float t) |
|
{ |
|
return 1f - Mathf.Sqrt(1f - t * t); |
|
} |
|
|
|
private static float EaseOutCirc(float t) |
|
{ |
|
return Mathf.Sqrt(1f - (t - 1f) * (t - 1f)); |
|
} |
|
|
|
private static float EaseInOutCirc(float t) |
|
{ |
|
if (t < 0.5f) |
|
{ |
|
return (1f - Mathf.Sqrt(1f - (2f * t) * (2f * t))) / 2f; |
|
} |
|
else |
|
{ |
|
return (Mathf.Sqrt(1f - (-2f * t + 2f) * (-2f * t + 2f)) + 1f) / 2f; |
|
} |
|
} |
|
|
|
/* Back */ |
|
private static float EaseInBack(float t) |
|
{ |
|
return C3 * t * t * t - C1 * t * t; |
|
} |
|
|
|
private static float EaseOutBack(float t) |
|
{ |
|
float p = t - 1f; |
|
return 1f + C3 * p * p * p + C1 * p * p; |
|
} |
|
|
|
private static float EaseInOutBack(float t) |
|
{ |
|
if (t < 0.5f) |
|
{ |
|
return (Mathf.Pow(2f * t, 2f) * ((C2 + 1f) * 2f * t - C2)) / 2f; |
|
} |
|
else |
|
{ |
|
float p = 2f * t - 2f; |
|
return (Mathf.Pow(p, 2f) * ((C2 + 1f) * p + C2) + 2f) / 2f; |
|
} |
|
} |
|
|
|
/* Elastic */ |
|
private static float EaseInElastic(float t) |
|
{ |
|
if (t == 0f) |
|
{ |
|
return 0f; |
|
} |
|
if (t == 1f) |
|
{ |
|
return 1f; |
|
} |
|
return -Mathf.Pow(2f, 10f * t - 10f) * Mathf.Sin((t * 10f - 10.75f) * C4); |
|
} |
|
|
|
private static float EaseOutElastic(float t) |
|
{ |
|
if (t == 0f) |
|
{ |
|
return 0f; |
|
} |
|
if (t == 1f) |
|
{ |
|
return 1f; |
|
} |
|
return Mathf.Pow(2f, -10f * t) * Mathf.Sin((t * 10f - 0.75f) * C4) + 1f; |
|
} |
|
|
|
private static float EaseInOutElastic(float t) |
|
{ |
|
if (t == 0f) |
|
{ |
|
return 0f; |
|
} |
|
if (t == 1f) |
|
{ |
|
return 1f; |
|
} |
|
if (t < 0.5f) |
|
{ |
|
return -(Mathf.Pow(2f, 20f * t - 10f) * Mathf.Sin((20f * t - 11.125f) * C5)) / 2f; |
|
} |
|
else |
|
{ |
|
return (Mathf.Pow(2f, -20f * t + 10f) * Mathf.Sin((20f * t - 11.125f) * C5)) / 2f + 1f; |
|
} |
|
} |
|
|
|
/* Bounce */ |
|
private static float EaseOutBounce(float t) |
|
{ |
|
if (t < 1f / D1) |
|
{ |
|
return N1 * t * t; |
|
} |
|
else if (t < 2f / D1) |
|
{ |
|
t -= 1.5f / D1; |
|
return N1 * t * t + 0.75f; |
|
} |
|
else if (t < 2.5f / D1) |
|
{ |
|
t -= 2.25f / D1; |
|
return N1 * t * t + 0.9375f; |
|
} |
|
else |
|
{ |
|
t -= 2.625f / D1; |
|
return N1 * t * t + 0.984375f; |
|
} |
|
} |
|
|
|
private static float EaseInBounce(float t) |
|
{ |
|
return 1f - EaseOutBounce(1f - t); |
|
} |
|
|
|
private static float EaseInOutBounce(float t) |
|
{ |
|
if (t < 0.5f) |
|
{ |
|
return (1f - EaseOutBounce(1f - 2f * t)) / 2f; |
|
} |
|
else |
|
{ |
|
return (EaseOutBounce(2f * t - 1f) + 1f) / 2f; |
|
} |
|
} |
|
} |