Skip to content

Instantly share code, notes, and snippets.

@InitialXKO
Forked from Fonserbc/Easing.cs
Last active May 18, 2020 21:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save InitialXKO/d3768c9436124fc041a78e9c1c48e77b to your computer and use it in GitHub Desktop.
Save InitialXKO/d3768c9436124fc041a78e9c1c48e77b to your computer and use it in GitHub Desktop.
Compact and simple easing functions for Unity
using System;
using UnityEngine;
/*
* Most functions taken from Tween.js - Licensed under the MIT license
* at https://github.com/sole/tween.js
* Quadratic.Bezier by @fonserbc - Licensed under WTFPL license
*/
public delegate float EasingFunction(float k, float c = 0);
public class TypeValue : System.Attribute
{
private System.Type _value;
public TypeValue(System.Type value)
{
_value = value;
}
public System.Type Value
{
get { return _value; }
}
}
public static class TypeEnum
{
public static System.Type GetTypeValue(System.Enum value)
{
System.Type output = null;
System.Type type = value.GetType();
System.Reflection.FieldInfo fi = type.GetField(value.ToString());
TypeValue[] attrs = fi.GetCustomAttributes(typeof(TypeValue), false) as TypeValue[];
if (attrs.Length > 0)
{
output = attrs[0].Value;
}
return output;
}
}
public class StringValue : System.Attribute
{
private string _value;
public StringValue(string value)
{
_value = value;
}
public string Value
{
get { return _value; }
}
}
public static class StringEnum
{
public static string GetStringValue(System.Enum value)
{
string output = null;
System.Type type = value.GetType();
System.Reflection.FieldInfo fi = type.GetField(value.ToString());
StringValue[] attrs = fi.GetCustomAttributes(typeof(StringValue), false) as StringValue[];
if (attrs.Length > 0)
{
output = attrs[0].Value;
}
return output;
}
}
public static class Easing
{
public enum EasingType
{
[TypeValue(typeof(Easing))] [StringValue("Linear")] Linear,
[TypeValue(typeof(Sinusoidal))] [StringValue("In")] SineIn,
[TypeValue(typeof(Sinusoidal))] [StringValue("Out")] SineOut,
[TypeValue(typeof(Sinusoidal))] [StringValue("InOut")] SineInOut,
[TypeValue(typeof(Quadratic))] [StringValue("In")] QuadIn,
[TypeValue(typeof(Quadratic))] [StringValue("Out")] QuadOut,
[TypeValue(typeof(Quadratic))] [StringValue("InOut")] QuadInOut,
[TypeValue(typeof(Quadratic))] [StringValue("Bezier")] Bezier,
[TypeValue(typeof(Cubic))] [StringValue("In")] CubicIn,
[TypeValue(typeof(Cubic))] [StringValue("Out")] CubicOut,
[TypeValue(typeof(Cubic))] [StringValue("InOut")] CubicInOut,
[TypeValue(typeof(Quartic))] [StringValue("In")] QuartIn,
[TypeValue(typeof(Quartic))] [StringValue("Out")] QuartOut,
[TypeValue(typeof(Quartic))] [StringValue("InOut")] QuartInOut,
[TypeValue(typeof(Quintic))] [StringValue("In")] QuintIn,
[TypeValue(typeof(Quintic))] [StringValue("Out")] QuintOut,
[TypeValue(typeof(Quintic))] [StringValue("InOut")] QuintInOut,
[TypeValue(typeof(Exponential))] [StringValue("In")] ExpoIn,
[TypeValue(typeof(Exponential))] [StringValue("Out")] ExpoOut,
[TypeValue(typeof(Exponential))] [StringValue("InOut")] ExpoInOut,
[TypeValue(typeof(Circular))] [StringValue("In")] CircIn,
[TypeValue(typeof(Circular))] [StringValue("Out")] CircOut,
[TypeValue(typeof(Circular))] [StringValue("InOut")] CircInOut,
[TypeValue(typeof(Back))] [StringValue("In")] BackIn,
[TypeValue(typeof(Back))] [StringValue("Out")] BackOut,
[TypeValue(typeof(Back))] [StringValue("InOut")] BackInOut,
[TypeValue(typeof(Elastic))] [StringValue("In")] ElasticIn,
[TypeValue(typeof(Elastic))] [StringValue("Out")] ElasticOut,
[TypeValue(typeof(Elastic))] [StringValue("InOut")] ElasticInOut,
[TypeValue(typeof(Bounce))] [StringValue("In")] BounceIn,
[TypeValue(typeof(Bounce))] [StringValue("Out")] BounceOut,
[TypeValue(typeof(Bounce))] [StringValue("InOut")] BounceInOut
}
public static float Ease(float k, EasingType type = EasingType.Linear, float c = 0)
{
EasingFunction f;
f = (EasingFunction)Delegate.CreateDelegate(typeof(EasingFunction), TypeEnum.GetTypeValue(type), StringEnum.GetStringValue(type));
return f(k, c);
}
public static float Linear(float k)
{
return k;
}
public static class Quadratic
{
public static float In(float k)
{
return k * k;
}
public static float Out(float k)
{
return k * (2f - k);
}
public static float InOut(float k)
{
if ((k *= 2f) < 1f) return 0.5f * k * k;
return -0.5f * ((k -= 1f) * (k - 2f) - 1f);
}
/*
* Quadratic.Bezier(k,0) behaves like Quadratic.In(k)
* Quadratic.Bezier(k,1) behaves like Quadratic.Out(k)
*
* If you want to learn more check Alan Wolfe's post about it http://www.demofox.org/bezquad1d.html
*/
public static float Bezier(float k, float c)
{
return c * 2 * k * (1 - k) + k * k;
}
};
public static class Cubic
{
public static float In(float k)
{
return k * k * k;
}
public static float Out(float k)
{
return 1f + ((k -= 1f) * k * k);
}
public static float InOut(float k)
{
if ((k *= 2f) < 1f) return 0.5f * k * k * k;
return 0.5f * ((k -= 2f) * k * k + 2f);
}
};
public static class Quartic
{
public static float In(float k)
{
return k * k * k * k;
}
public static float Out(float k)
{
return 1f - ((k -= 1f) * k * k * k);
}
public static float InOut(float k)
{
if ((k *= 2f) < 1f) return 0.5f * k * k * k * k;
return -0.5f * ((k -= 2f) * k * k * k - 2f);
}
};
public static class Quintic
{
public static float In(float k)
{
return k * k * k * k * k;
}
public static float Out(float k)
{
return 1f + ((k -= 1f) * k * k * k * k);
}
public static float InOut(float k)
{
if ((k *= 2f) < 1f) return 0.5f * k * k * k * k * k;
return 0.5f * ((k -= 2f) * k * k * k * k + 2f);
}
};
public static class Sinusoidal
{
public static float In(float k)
{
return 1f - Mathf.Cos(k * Mathf.PI / 2f);
}
public static float Out(float k)
{
return Mathf.Sin(k * Mathf.PI / 2f);
}
public static float InOut(float k)
{
return 0.5f * (1f - Mathf.Cos(Mathf.PI * k));
}
};
public static class Exponential
{
public static float In(float k)
{
return Mathf.Approximately(k, 0f) ? 0f : Mathf.Pow(1024f, k - 1f);
}
public static float Out(float k)
{
return Mathf.Approximately(k, 1f) ? 1f : 1f - Mathf.Pow(2f, -10f * k);
}
public static float InOut(float k)
{
if (Mathf.Approximately(k, 0f)) return 0f;
if (Mathf.Approximately(k, 1f)) return 1f;
if ((k *= 2f) < 1f) return 0.5f * Mathf.Pow(1024f, k - 1f);
return 0.5f * (-Mathf.Pow(2f, -10f * (k - 1f)) + 2f);
}
};
public static class Circular
{
public static float In(float k)
{
return 1f - Mathf.Sqrt(1f - k * k);
}
public static float Out(float k)
{
return Mathf.Sqrt(1f - ((k -= 1f) * k));
}
public static float InOut(float k)
{
if ((k *= 2f) < 1f) return -0.5f * (Mathf.Sqrt(1f - k * k) - 1);
return 0.5f * (Mathf.Sqrt(1f - (k -= 2f) * k) + 1f);
}
};
public static class Elastic
{
public static float In(float k)
{
if (Mathf.Approximately(k, 0f)) return 0;
if (Mathf.Approximately(k, 1f)) return 1;
return -Mathf.Pow(2f, 10f * (k -= 1f)) * Mathf.Sin((k - 0.1f) * (2f * Mathf.PI) / 0.4f);
}
public static float Out(float k)
{
if (Mathf.Approximately(k, 0f)) return 0;
if (Mathf.Approximately(k, 1f)) return 1;
return Mathf.Pow(2f, -10f * k) * Mathf.Sin((k - 0.1f) * (2f * Mathf.PI) / 0.4f) + 1f;
}
public static float InOut(float k)
{
if ((k *= 2f) < 1f) return -0.5f * Mathf.Pow(2f, 10f * (k -= 1f)) * Mathf.Sin((k - 0.1f) * (2f * Mathf.PI) / 0.4f);
return Mathf.Pow(2f, -10f * (k -= 1f)) * Mathf.Sin((k - 0.1f) * (2f * Mathf.PI) / 0.4f) * 0.5f + 1f;
}
};
public static class Back
{
const float s = 1.70158f;
const float s2 = 2.5949095f;
public static float In(float k)
{
return k * k * ((s + 1f) * k - s);
}
public static float Out(float k)
{
return (k -= 1f) * k * ((s + 1f) * k + s) + 1f;
}
public static float InOut(float k)
{
if ((k *= 2f) < 1f) return 0.5f * (k * k * ((s2 + 1f) * k - s2));
return 0.5f * ((k -= 2f) * k * ((s2 + 1f) * k + s2) + 2f);
}
};
public static class Bounce
{
public static float In(float k)
{
return 1f - Out(1f - k);
}
public static float Out(float k)
{
if (k < (1f / 2.75f))
{
return 7.5625f * k * k;
}
else if (k < (2f / 2.75f))
{
return 7.5625f * (k -= (1.5f / 2.75f)) * k + 0.75f;
}
else if (k < (2.5f / 2.75f))
{
return 7.5625f * (k -= (2.25f / 2.75f)) * k + 0.9375f;
}
else
{
return 7.5625f * (k -= (2.625f / 2.75f)) * k + 0.984375f;
}
}
public static float InOut(float k)
{
if (k < 0.5f) return In(k * 2f) * 0.5f;
return Out(k * 2f - 1f) * 0.5f + 0.5f;
}
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment