Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Custom Easing functions

Discussion in 'Scripting' started by smetzzz, Jan 24, 2015.

  1. smetzzz

    smetzzz

    Joined:
    Mar 24, 2014
    Posts:
    145
    I am trying to find formulas for a wide range of custom easing functions like this site. http://easings.net
    to do things like Markus Eckert and his brilliant talk.

    I just want to us two points between 0,0 and 1,1 but have a toolbag of custom lerp options available. The AnimationCurve and its Hermite handles are simply too clunky and inflexible.
    I'm not interested in code heavy plugin solutions either.
    Any thoughts or ideas on how to calculate and then how these would look would be really helpful.
    Cheers!
     
  2. smetzzz

    smetzzz

    Joined:
    Mar 24, 2014
    Posts:
    145
  3. babelbrian

    babelbrian

    Joined:
    Jul 14, 2015
    Posts:
    11
    Did you ever implement these equations into Unity? It would be great to see how you did it.
     
  4. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,377
    luispedrofonseca and babelbrian like this.
  5. smetzzz

    smetzzz

    Joined:
    Mar 24, 2014
    Posts:
    145
    Thanks @lordofduct, those are the standard ease functions. They are pretty easy to implement but I am looking for cubic bezier curves which are the standard keyframe interpolation method of most all 3D programs and any programs serious about animation. Unity uses Hermite curves which ease fine but are limited in their ability to add real character for serious animators and motion graphics designers. I can only hope Unity supports this eventually but would like to find a way to add cubic bezier for simple stuff at least.
     
  6. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,377
    babelbrian asked for those equations, I included them.

    If you want an implementation of the 'cubic-bezier' function as shown in your link http://cubic-bezier.com, recheck the link I supplied. Just added a way to generate the configured cubic-bezier based on 4 input points in the same way that link you supplied works.

    It looks like:
    Code (csharp):
    1.  
    2.  
    3.         #region Configurable Cubic Bezier
    4.  
    5.         public static Ease CubicBezier(float p0, float p1, float p2, float p3)
    6.         {
    7.             return (c, s, e, d) =>
    8.             {
    9.                 var t = c / d;
    10.                 var it = 1f - t;
    11.                 var r = (Mathf.Pow(it, 3f) * p0)
    12.                       + (3f * Mathf.Pow(it, 2f) * t * p1)
    13.                       + (3f * it * Mathf.Pow(t, 2f) * p2)
    14.                       + (Mathf.Pow(t, 3f) * p3);
    15.                 return s + e * r;
    16.             };
    17.         }
    18.  
    19.         #endregion
    20.  
    21.  
    It's just an implementation of cubic bezier as found on wikipedia:
    https://en.wikipedia.org/wiki/Bézier_curve

    If you wanted a pretty editor, similar to the link, and similar to how unity has one for the 'AnimationCurve' class.

    You'd have to create a class to represent that bezier curve more explicitly and create an editor script for it.
     
    Last edited: Jul 22, 2015
  7. smetzzz

    smetzzz

    Joined:
    Mar 24, 2014
    Posts:
    145
  8. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,377
    Well, here is a start.

    I created the class for a CubicBezierCurve:
    https://github.com/lordofduct/spacepuppy-unity-framework/blob/master/SpacepuppyBase/Geom/ICurve.cs

    As well as the PropertyDrawer:
    https://github.com/lordofduct/space...Editor/Geom/CubicBezierCurvePropertyDrawer.cs

    Note I generalized the curve preview window into the method SPEditorGUI.DrawCurveSwatch. This way if we define any other kinds of curves that implement the ICurve interface, we just call the DrawCurveSwatch method. It's at the bottom of this helper class:
    https://github.com/lordofduct/space...ob/master/SpacepuppyBaseEditor/SPEditorGUI.cs

    All that's left is in the 'TODO' of the property drawer we open an actual editor window. I put the class in there, just didn't write it yet... don't have to the time to right this second:
    https://github.com/lordofduct/space...seEditor/Geom/CubicBezierCurveEditorWindow.cs

    I may get to it later, or someone else can tackle it.
     
  9. smetzzz

    smetzzz

    Joined:
    Mar 24, 2014
    Posts:
    145
    A great start! I would love to tackle this challenge but I too am really short on time.
     
  10. babelbrian

    babelbrian

    Joined:
    Jul 14, 2015
    Posts:
    11