Skip to content

Instantly share code, notes, and snippets.

@tkyaji

tkyaji/SimpleEasing.cs

Last active Oct 16, 2020
Embed
What would you like to do?
using UnityEngine;
using System.Collections;
public class SimpleEasing {
private const float PI2 = Mathf.PI * 0.5f;
// Liner
public static float Liner(float t) {
return t;
}
public static float Liner(float start, float end, float duration, float time) {
float v = Liner(time / duration);
return start + (end - start) * v;
}
public static Vector3 Liner(Vector3 start, Vector3 end, float duration, float time) {
float v = Liner(time / duration);
return start + (end - start) * v;
}
// Quad
public static float QuadIn(float t) {
return powIn(2, t);
}
public static float QuadIn(float start, float end, float duration, float time) {
float v = QuadIn(time / duration);
return start + (end - start) * v;
}
public static Vector3 QuadIn(Vector3 start, Vector3 end, float duration, float time) {
float v = QuadIn(time / duration);
return start + (end - start) * v;
}
public static float QuadOut(float t) {
return powOut(2, t);
}
public static float QuadOut(float start, float end, float duration, float time) {
float v = QuadOut(time / duration);
return start + (end - start) * v;
}
public static Vector3 QuadOut(Vector3 start, Vector3 end, float duration, float time) {
float v = QuadOut(time / duration);
return start + (end - start) * v;
}
public static float QuadInOut(float t) {
return powInOut(2, t);
}
public static float QuadInOut(float start, float end, float duration, float time) {
float v = QuadInOut(time / duration);
return start + (end - start) * v;
}
public static Vector3 QuadInOut(Vector3 start, Vector3 end, float duration, float time) {
float v = QuadInOut(time / duration);
return start + (end - start) * v;
}
// Cubic
public static float CubicIn(float t) {
return powIn(3, t);
}
public static float CubicIn(float start, float end, float duration, float time) {
float v = CubicIn(time / duration);
return start + (end - start) * v;
}
public static Vector3 CubicIn(Vector3 start, Vector3 end, float duration, float time) {
float v = CubicIn(time / duration);
return start + (end - start) * v;
}
public static float CubicOut(float t) {
return powOut(3, t);
}
public static float CubicOut(float start, float end, float duration, float time) {
float v = CubicOut(time / duration);
return start + (end - start) * v;
}
public static Vector3 CubicOut(Vector3 start, Vector3 end, float duration, float time) {
float v = CubicOut(time / duration);
return start + (end - start) * v;
}
public static float CubicInOut(float t) {
return powInOut(3, t);
}
public static float CubicInOut(float start, float end, float duration, float time) {
float v = CubicInOut(time / duration);
return start + (end - start) * v;
}
public static Vector3 CubicInOut(Vector3 start, Vector3 end, float duration, float time) {
float v = CubicInOut(time / duration);
return start + (end - start) * v;
}
// Quart
public static float QuartIn(float t) {
return powIn(4, t);
}
public static float QuartIn(float start, float end, float duration, float time) {
float v = QuartIn(time / duration);
return start + (end - start) * v;
}
public static Vector3 QuartIn(Vector3 start, Vector3 end, float duration, float time) {
float v = QuartIn(time / duration);
return start + (end - start) * v;
}
public static float QuartOut(float t) {
return powOut(4, t);
}
public static float QuartOut(float start, float end, float duration, float time) {
float v = QuartOut(time / duration);
return start + (end - start) * v;
}
public static Vector3 QuartOut(Vector3 start, Vector3 end, float duration, float time) {
float v = QuartOut(time / duration);
return start + (end - start) * v;
}
public static float QuartInOut(float t) {
return powInOut(4, t);
}
public static float QuartInOut(float start, float end, float duration, float time) {
float v = QuartInOut(time / duration);
return start + (end - start) * v;
}
public static Vector3 QuartInOut(Vector3 start, Vector3 end, float duration, float time) {
float v = QuartInOut(time / duration);
return start + (end - start) * v;
}
// Quint
public static float QuintIn(float t) {
return powIn(5, t);
}
public static float QuintIn(float start, float end, float duration, float time) {
float v = QuintIn(time / duration);
return start + (end - start) * v;
}
public static Vector3 QuintIn(Vector3 start, Vector3 end, float duration, float time) {
float v = QuintIn(time / duration);
return start + (end - start) * v;
}
public static float QuintOut(float t) {
return powOut(5, t);
}
public static float QuintOut(float start, float end, float duration, float time) {
float v = QuintOut(time / duration);
return start + (end - start) * v;
}
public static Vector3 QuintOut(Vector3 start, Vector3 end, float duration, float time) {
float v = QuintOut(time / duration);
return start + (end - start) * v;
}
public static float QuintInOut(float t) {
return powInOut(5, t);
}
public static float QuintInOut(float start, float end, float duration, float time) {
float v = QuintInOut(time / duration);
return start + (end - start) * v;
}
public static Vector3 QuintInOut(Vector3 start, Vector3 end, float duration, float time) {
float v = QuintInOut(time / duration);
return start + (end - start) * v;
}
// Sine
public static float SineIn(float t) {
return 1 - Mathf.Cos(t * PI2);
}
public static float SineIn(float start, float end, float duration, float time) {
float v = SineIn(time / duration);
return start + (end - start) * v;
}
public static Vector3 SineIn(Vector3 start, Vector3 end, float duration, float time) {
float v = SineIn(time / duration);
return start + (end - start) * v;
}
public static float SineOut(float t) {
return Mathf.Sin(t * PI2);
}
public static float SineOut(float start, float end, float duration, float time) {
float v = SineOut(time / duration);
return start + (end - start) * v;
}
public static Vector3 SineOut(Vector3 start, Vector3 end, float duration, float time) {
float v = SineOut(time / duration);
return start + (end - start) * v;
}
public static float SineInOut(float t) {
return -0.5f * (Mathf.Cos(Mathf.PI * t) - 1);
}
public static float SineInOut(float start, float end, float duration, float time) {
float v = SineInOut(time / duration);
return start + (end - start) * v;
}
public static Vector3 SineInOut(Vector3 start, Vector3 end, float duration, float time) {
float v = SineInOut(time / duration);
return start + (end - start) * v;
}
// Back
public static float BackIn(float t) {
return t * t * (2.7f * t - 1.7f);
}
public static float BackIn(float start, float end, float duration, float time) {
float v = BackIn(time / duration);
return start + (end - start) * v;
}
public static Vector3 BackIn(Vector3 start, Vector3 end, float duration, float time) {
float v = BackIn(time / duration);
return start + (end - start) * v;
}
public static float BackOut(float t) {
t--;
return (t * t * (2.7f * t + 1.7f) + 1);
}
public static float BackOut(float start, float end, float duration, float time) {
float v = BackOut(time / duration);
return start + (end - start) * v;
}
public static Vector3 BackOut(Vector3 start, Vector3 end, float duration, float time) {
float v = BackOut(time / duration);
return start + (end - start) * v;
}
public static float BackInOut(float t) {
t *= 2.0f;
if (t < 1.0f) return 0.5f * (t * t * (2.525f * t - 1.525f));
t -= 2.0f;
return 0.5f * (t * t * (2.525f * t + 1.525f) + 2.0f);
}
public static float BackInOut(float start, float end, float duration, float time) {
float v = BackInOut(time / duration);
return start + (end - start) * v;
}
public static Vector3 BackInOut(Vector3 start, Vector3 end, float duration, float time) {
float v = BackInOut(time / duration);
return start + (end - start) * v;
}
// Circ
public static float CircIn(float t) {
return -(Mathf.Sqrt(1.0f - t * t) - 1.0f);
}
public static float CircIn(float start, float end, float duration, float time) {
float v = CircIn(time / duration);
return start + (end - start) * v;
}
public static Vector3 CircIn(Vector3 start, Vector3 end, float duration, float time) {
float v = CircIn(time / duration);
return start + (end - start) * v;
}
public static float CircOut(float t) {
t--;
return Mathf.Sqrt(1 - t * t);
}
public static float CircOut(float start, float end, float duration, float time) {
float v = CircOut(time / duration);
return start + (end - start) * v;
}
public static Vector3 CircOut(Vector3 start, Vector3 end, float duration, float time) {
float v = CircOut(time / duration);
return start + (end - start) * v;
}
public static float CircInOut(float t) {
t *= 2.0f;
if (t < 1.0f) return -0.5f * (Mathf.Sqrt(1.0f - t * t) - 1.0f);
t -= 2.0f;
return 0.5f * (Mathf.Sqrt(1 - t * t) + 1);
}
public static float CircInOut(float start, float end, float duration, float time) {
float v = CircInOut(time / duration);
return start + (end - start) * v;
}
public static Vector3 CircInOut(Vector3 start, Vector3 end, float duration, float time) {
float v = CircInOut(time / duration);
return start + (end - start) * v;
}
// Bounce
public static float BounceIn(float t) {
return 1 - bounceOut(1 - t);
}
public static float BounceIn(float start, float end, float duration, float time) {
float v = BounceIn(time / duration);
return start + (end - start) * v;
}
public static Vector3 BounceIn(Vector3 start, Vector3 end, float duration, float time) {
float v = BounceIn(time / duration);
return start + (end - start) * v;
}
public static float BounceOut(float t) {
return bounceOut(t);
}
public static float BounceOut(float start, float end, float duration, float time) {
float v = BounceOut(time / duration);
return start + (end - start) * v;
}
public static Vector3 BounceOut(Vector3 start, Vector3 end, float duration, float time) {
float v = BounceOut(time / duration);
return start + (end - start) * v;
}
public static float BounceInOut(float t) {
if (t < 0.5f) return bounceIn(t * 2) * 0.5f;
return bounceOut(t * 2 - 1) * 0.5f + 0.5f;
}
public static float BounceInOut(float start, float end, float duration, float time) {
float v = BounceInOut(time / duration);
return start + (end - start) * v;
}
public static Vector3 BounceInOut(Vector3 start, Vector3 end, float duration, float time) {
float v = BounceInOut(time / duration);
return start + (end - start) * v;
}
// Elastic
public static float ElasticIn(float t) {
const float pi2 = Mathf.PI * 2.0f;
if (Mathf.Round(t * 1000000) == 0 || Mathf.Round(t * 1000000) == 1000000) return t;
float s = 0.3f / pi2 * Mathf.Asin(1);
t -= 1;
return -(Mathf.Pow(2, 10 * t) * Mathf.Sin((t - s) * pi2 / 0.3f));
}
public static float ElasticIn(float start, float end, float duration, float time) {
float v = ElasticIn(time / duration);
return start + (end - start) * v;
}
public static Vector3 ElasticIn(Vector3 start, Vector3 end, float duration, float time) {
float v = ElasticIn(time / duration);
return start + (end - start) * v;
}
public static float ElasticOut(float t) {
const float pi2 = Mathf.PI * 2;
if (Mathf.Round(t * 1000000) == 0 || Mathf.Round(t * 1000000) == 1000000) return t;
float s = 0.3f / pi2 * Mathf.Asin(1);
return Mathf.Pow(2, -10 * t) * Mathf.Sin((t - s) * pi2 / 0.3f) + 1;
}
public static float ElasticOut(float start, float end, float duration, float time) {
float v = ElasticOut(time / duration);
return start + (end - start) * v;
}
public static Vector3 ElasticOut(Vector3 start, Vector3 end, float duration, float time) {
float v = ElasticOut(time / duration);
return start + (end - start) * v;
}
public static float ElasticInOut(float t) {
const float pi2 = Mathf.PI * 2;
float s = 0.45f / pi2 * Mathf.Asin(1);
t *= 2;
if (t < 1) {
t -= 1;
return -0.5f * (Mathf.Pow(2, 10 * t) * Mathf.Sin((t - s) * pi2 / 0.45f));
} else {
t -= 1;
return Mathf.Pow(2, -10 * t) * Mathf.Sin((t - s) * pi2 / 0.45f) * 0.5f + 1;
}
}
public static float ElasticInOut(float start, float end, float duration, float time) {
float v = ElasticInOut(time / duration);
return start + (end - start) * v;
}
public static Vector3 ElasticInOut(Vector3 start, Vector3 end, float duration, float time) {
float v = ElasticInOut(time / duration);
return start + (end - start) * v;
}
private static float powIn(float p, float t) {
return Mathf.Pow(t, p);
}
private static float powOut(float p, float t) {
return 1 - Mathf.Pow(1.0f - t, p);
}
private static float powInOut(float p, float t) {
t *= 2;
if (t < 1.0f) return 0.5f * Mathf.Pow(t, p);
return 1.0f - 0.5f * Mathf.Abs(Mathf.Pow(2.0f - t, p));
}
private static float bounceOut(float t) {
if (t < 1 / 2.75f) {
return (7.5625f * t * t);
} else if (t < 2 / 2.75f) {
t -= 1.5f / 2.75f;
return (7.5625f * t * t + 0.75f);
} else if (t < 2.5f / 2.75f) {
t -= 2.25f / 2.75f;
return (7.5625f * t * t + 0.9375f);
} else {
t -= 2.625f / 2.75f;
return (7.5625f * t * t + 0.984375f);
}
}
private static float bounceIn(float t) {
return 1 - bounceOut(1 - t);
}
public class WaitForEasing : CustomYieldInstruction {
private IEnumerator routine = null;
private System.Action<float> onValueChange;
private System.Func<float, float> func;
private float duration;
public WaitForEasing(System.Action<float> onValueChange, float duration, System.Func<float, float> func) {
this.onValueChange = onValueChange;
this.duration = duration;
this.func = func;
routine = Routine();
this.onValueChange.Invoke(0);
}
public override bool keepWaiting {
get { return routine.MoveNext(); }
}
private IEnumerator Routine() {
float tm = 0;
while (tm < duration) {
yield return null;
tm += Time.deltaTime;
float t = tm / this.duration;
float v = this.func(t);
this.onValueChange.Invoke(v);
}
this.onValueChange.Invoke(1.0f);
}
}
}
@tkyaji

This comment has been minimized.

Copy link
Owner Author

@tkyaji tkyaji commented Jul 1, 2020

Simple easing libraly for Unity.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment