Advertisement
Guest User

Easing functions in Unity/C#

a guest
Jul 9th, 2019
930
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 31.34 KB | None | 0 0
  1. using UnityEngine;
  2.  
  3. /*
  4.  * Created by C.J. Kimberlin (http://cjkimberlin.com)
  5.  *
  6.  * The MIT License (MIT)
  7.  *
  8.  * Copyright (c) 2015
  9.  *
  10.  * Permission is hereby granted, free of charge, to any person obtaining a copy
  11.  * of this software and associated documentation files (the "Software"), to deal
  12.  * in the Software without restriction, including without limitation the rights
  13.  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  14.  * copies of the Software, and to permit persons to whom the Software is
  15.  * furnished to do so, subject to the following conditions:
  16.  *
  17.  * The above copyright notice and this permission notice shall be included in all
  18.  * copies or substantial portions of the Software.
  19.  *
  20.  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  21.  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  22.  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  23.  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  24.  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  25.  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  26.  * SOFTWARE.
  27.  *
  28.  *
  29.  * TERMS OF USE - EASING EQUATIONS
  30.  * Open source under the BSD License.
  31.  * Copyright (c)2001 Robert Penner
  32.  * All rights reserved.
  33.  * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
  34.  * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
  35.  * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
  36.  * Neither the name of the author nor the names of contributors may be used to endorse or promote products derived from this software without specific prior written permission.
  37.  *
  38.  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  39.  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
  40.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  41.  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  42.  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  43.  *
  44.  *
  45.  * ============= Description =============
  46.  *
  47.  * Below is an example of how to use the easing functions in the file. There is a getting function that will return the function
  48.  * from an enum. This is useful since the enum can be exposed in the editor and then the function queried during Start().
  49.  *
  50.  * EasingFunction.Ease ease = EasingFunction.Ease.EaseInOutQuad;
  51.  * EasingFunction.EasingFunc func = GetEasingFunction(ease;
  52.  *
  53.  * float value = func(0, 10, 0.67f);
  54.  *
  55.  * EasingFunction.EaseingFunc derivativeFunc = GetEasingFunctionDerivative(ease);
  56.  *
  57.  * float derivativeValue = derivativeFunc(0, 10, 0.67f);
  58.  */
  59.  
  60. public class EasingFunction
  61. {
  62.     public enum Ease
  63.     {
  64.         EaseInQuad = 0,
  65.         EaseOutQuad,
  66.         EaseInOutQuad,
  67.         EaseInCubic,
  68.         EaseOutCubic,
  69.         EaseInOutCubic,
  70.         EaseInQuart,
  71.         EaseOutQuart,
  72.         EaseInOutQuart,
  73.         EaseInQuint,
  74.         EaseOutQuint,
  75.         EaseInOutQuint,
  76.         EaseInSine,
  77.         EaseOutSine,
  78.         EaseInOutSine,
  79.         EaseInExpo,
  80.         EaseOutExpo,
  81.         EaseInOutExpo,
  82.         EaseInCirc,
  83.         EaseOutCirc,
  84.         EaseInOutCirc,
  85.         Linear,
  86.         Spring,
  87.         EaseInBounce,
  88.         EaseOutBounce,
  89.         EaseInOutBounce,
  90.         EaseInBack,
  91.         EaseOutBack,
  92.         EaseInOutBack,
  93.         EaseInElastic,
  94.         EaseOutElastic,
  95.         EaseInOutElastic,
  96.     }
  97.  
  98.     private const float NATURAL_LOG_OF_2 = 0.693147181f;
  99.  
  100.     //
  101.     // Easing functions
  102.     //
  103.  
  104.     public static float Linear(float start, float end, float value)
  105.     {
  106.         return Mathf.Lerp(start, end, value);
  107.     }
  108.  
  109.     public static float Spring(float start, float end, float value)
  110.     {
  111.         value = Mathf.Clamp01(value);
  112.         value = (Mathf.Sin(value * Mathf.PI * (0.2f + 2.5f * value * value * value)) * Mathf.Pow(1f - value, 2.2f) + value) * (1f + (1.2f * (1f - value)));
  113.         return start + (end - start) * value;
  114.     }
  115.  
  116.     public static float EaseInQuad(float start, float end, float value)
  117.     {
  118.         end -= start;
  119.         return end * value * value + start;
  120.     }
  121.  
  122.     public static float EaseOutQuad(float start, float end, float value)
  123.     {
  124.         end -= start;
  125.         return -end * value * (value - 2) + start;
  126.     }
  127.  
  128.     public static float EaseInOutQuad(float start, float end, float value)
  129.     {
  130.         value /= .5f;
  131.         end -= start;
  132.         if (value < 1) return end * 0.5f * value * value + start;
  133.         value--;
  134.         return -end * 0.5f * (value * (value - 2) - 1) + start;
  135.     }
  136.  
  137.     public static float EaseInCubic(float start, float end, float value)
  138.     {
  139.         end -= start;
  140.         return end * value * value * value + start;
  141.     }
  142.  
  143.     public static float EaseOutCubic(float start, float end, float value)
  144.     {
  145.         value--;
  146.         end -= start;
  147.         return end * (value * value * value + 1) + start;
  148.     }
  149.  
  150.     public static float EaseInOutCubic(float start, float end, float value)
  151.     {
  152.         value /= .5f;
  153.         end -= start;
  154.         if (value < 1) return end * 0.5f * value * value * value + start;
  155.         value -= 2;
  156.         return end * 0.5f * (value * value * value + 2) + start;
  157.     }
  158.  
  159.     public static float EaseInQuart(float start, float end, float value)
  160.     {
  161.         end -= start;
  162.         return end * value * value * value * value + start;
  163.     }
  164.  
  165.     public static float EaseOutQuart(float start, float end, float value)
  166.     {
  167.         value--;
  168.         end -= start;
  169.         return -end * (value * value * value * value - 1) + start;
  170.     }
  171.  
  172.     public static float EaseInOutQuart(float start, float end, float value)
  173.     {
  174.         value /= .5f;
  175.         end -= start;
  176.         if (value < 1) return end * 0.5f * value * value * value * value + start;
  177.         value -= 2;
  178.         return -end * 0.5f * (value * value * value * value - 2) + start;
  179.     }
  180.  
  181.     public static float EaseInQuint(float start, float end, float value)
  182.     {
  183.         end -= start;
  184.         return end * value * value * value * value * value + start;
  185.     }
  186.  
  187.     public static float EaseOutQuint(float start, float end, float value)
  188.     {
  189.         value--;
  190.         end -= start;
  191.         return end * (value * value * value * value * value + 1) + start;
  192.     }
  193.  
  194.     public static float EaseInOutQuint(float start, float end, float value)
  195.     {
  196.         value /= .5f;
  197.         end -= start;
  198.         if (value < 1) return end * 0.5f * value * value * value * value * value + start;
  199.         value -= 2;
  200.         return end * 0.5f * (value * value * value * value * value + 2) + start;
  201.     }
  202.  
  203.     public static float EaseInSine(float start, float end, float value)
  204.     {
  205.         end -= start;
  206.         return -end * Mathf.Cos(value * (Mathf.PI * 0.5f)) + end + start;
  207.     }
  208.  
  209.     public static float EaseOutSine(float start, float end, float value)
  210.     {
  211.         end -= start;
  212.         return end * Mathf.Sin(value * (Mathf.PI * 0.5f)) + start;
  213.     }
  214.  
  215.     public static float EaseInOutSine(float start, float end, float value)
  216.     {
  217.         end -= start;
  218.         return -end * 0.5f * (Mathf.Cos(Mathf.PI * value) - 1) + start;
  219.     }
  220.  
  221.     public static float EaseInExpo(float start, float end, float value)
  222.     {
  223.         end -= start;
  224.         return end * Mathf.Pow(2, 10 * (value - 1)) + start;
  225.     }
  226.  
  227.     public static float EaseOutExpo(float start, float end, float value)
  228.     {
  229.         end -= start;
  230.         return end * (-Mathf.Pow(2, -10 * value) + 1) + start;
  231.     }
  232.  
  233.     public static float EaseInOutExpo(float start, float end, float value)
  234.     {
  235.         value /= .5f;
  236.         end -= start;
  237.         if (value < 1) return end * 0.5f * Mathf.Pow(2, 10 * (value - 1)) + start;
  238.         value--;
  239.         return end * 0.5f * (-Mathf.Pow(2, -10 * value) + 2) + start;
  240.     }
  241.  
  242.     public static float EaseInCirc(float start, float end, float value)
  243.     {
  244.         end -= start;
  245.         return -end * (Mathf.Sqrt(1 - value * value) - 1) + start;
  246.     }
  247.  
  248.     public static float EaseOutCirc(float start, float end, float value)
  249.     {
  250.         value--;
  251.         end -= start;
  252.         return end * Mathf.Sqrt(1 - value * value) + start;
  253.     }
  254.  
  255.     public static float EaseInOutCirc(float start, float end, float value)
  256.     {
  257.         value /= .5f;
  258.         end -= start;
  259.         if (value < 1) return -end * 0.5f * (Mathf.Sqrt(1 - value * value) - 1) + start;
  260.         value -= 2;
  261.         return end * 0.5f * (Mathf.Sqrt(1 - value * value) + 1) + start;
  262.     }
  263.  
  264.     public static float EaseInBounce(float start, float end, float value)
  265.     {
  266.         end -= start;
  267.         float d = 1f;
  268.         return end - EaseOutBounce(0, end, d - value) + start;
  269.     }
  270.  
  271.     public static float EaseOutBounce(float start, float end, float value)
  272.     {
  273.         value /= 1f;
  274.         end -= start;
  275.         if (value < (1 / 2.75f))
  276.         {
  277.             return end * (7.5625f * value * value) + start;
  278.         }
  279.         else if (value < (2 / 2.75f))
  280.         {
  281.             value -= (1.5f / 2.75f);
  282.             return end * (7.5625f * (value) * value + .75f) + start;
  283.         }
  284.         else if (value < (2.5 / 2.75))
  285.         {
  286.             value -= (2.25f / 2.75f);
  287.             return end * (7.5625f * (value) * value + .9375f) + start;
  288.         }
  289.         else
  290.         {
  291.             value -= (2.625f / 2.75f);
  292.             return end * (7.5625f * (value) * value + .984375f) + start;
  293.         }
  294.     }
  295.  
  296.     public static float EaseInOutBounce(float start, float end, float value)
  297.     {
  298.         end -= start;
  299.         float d = 1f;
  300.         if (value < d * 0.5f) return EaseInBounce(0, end, value * 2) * 0.5f + start;
  301.         else return EaseOutBounce(0, end, value * 2 - d) * 0.5f + end * 0.5f + start;
  302.     }
  303.  
  304.     public static float EaseInBack(float start, float end, float value)
  305.     {
  306.         end -= start;
  307.         value /= 1;
  308.         float s = 1.70158f;
  309.         return end * (value) * value * ((s + 1) * value - s) + start;
  310.     }
  311.  
  312.     public static float EaseOutBack(float start, float end, float value)
  313.     {
  314.         float s = 1.70158f;
  315.         end -= start;
  316.         value = (value) - 1;
  317.         return end * ((value) * value * ((s + 1) * value + s) + 1) + start;
  318.     }
  319.  
  320.     public static float EaseInOutBack(float start, float end, float value)
  321.     {
  322.         float s = 1.70158f;
  323.         end -= start;
  324.         value /= .5f;
  325.         if ((value) < 1)
  326.         {
  327.             s *= (1.525f);
  328.             return end * 0.5f * (value * value * (((s) + 1) * value - s)) + start;
  329.         }
  330.         value -= 2;
  331.         s *= (1.525f);
  332.         return end * 0.5f * ((value) * value * (((s) + 1) * value + s) + 2) + start;
  333.     }
  334.  
  335.     public static float EaseInElastic(float start, float end, float value)
  336.     {
  337.         end -= start;
  338.  
  339.         float d = 1f;
  340.         float p = d * .3f;
  341.         float s;
  342.         float a = 0;
  343.  
  344.         if (value == 0) return start;
  345.  
  346.         if ((value /= d) == 1) return start + end;
  347.  
  348.         if (a == 0f || a < Mathf.Abs(end))
  349.         {
  350.             a = end;
  351.             s = p / 4;
  352.         }
  353.         else
  354.         {
  355.             s = p / (2 * Mathf.PI) * Mathf.Asin(end / a);
  356.         }
  357.  
  358.         return -(a * Mathf.Pow(2, 10 * (value -= 1)) * Mathf.Sin((value * d - s) * (2 * Mathf.PI) / p)) + start;
  359.     }
  360.  
  361.     public static float EaseOutElastic(float start, float end, float value)
  362.     {
  363.         end -= start;
  364.  
  365.         float d = 1f;
  366.         float p = d * .3f;
  367.         float s;
  368.         float a = 0;
  369.  
  370.         if (value == 0) return start;
  371.  
  372.         if ((value /= d) == 1) return start + end;
  373.  
  374.         if (a == 0f || a < Mathf.Abs(end))
  375.         {
  376.             a = end;
  377.             s = p * 0.25f;
  378.         }
  379.         else
  380.         {
  381.             s = p / (2 * Mathf.PI) * Mathf.Asin(end / a);
  382.         }
  383.  
  384.         return (a * Mathf.Pow(2, -10 * value) * Mathf.Sin((value * d - s) * (2 * Mathf.PI) / p) + end + start);
  385.     }
  386.  
  387.     public static float EaseInOutElastic(float start, float end, float value)
  388.     {
  389.         end -= start;
  390.  
  391.         float d = 1f;
  392.         float p = d * .3f;
  393.         float s;
  394.         float a = 0;
  395.  
  396.         if (value == 0) return start;
  397.  
  398.         if ((value /= d * 0.5f) == 2) return start + end;
  399.  
  400.         if (a == 0f || a < Mathf.Abs(end))
  401.         {
  402.             a = end;
  403.             s = p / 4;
  404.         }
  405.         else
  406.         {
  407.             s = p / (2 * Mathf.PI) * Mathf.Asin(end / a);
  408.         }
  409.  
  410.         if (value < 1) return -0.5f * (a * Mathf.Pow(2, 10 * (value -= 1)) * Mathf.Sin((value * d - s) * (2 * Mathf.PI) / p)) + start;
  411.         return a * Mathf.Pow(2, -10 * (value -= 1)) * Mathf.Sin((value * d - s) * (2 * Mathf.PI) / p) * 0.5f + end + start;
  412.     }
  413.  
  414.     //
  415.     // These are derived functions that the motor can use to get the speed at a specific time.
  416.     //
  417.     // The easing functions all work with a normalized time (0 to 1) and the returned value here
  418.     // reflects that. Values returned here should be divided by the actual time.
  419.     //
  420.     // TODO: These functions have not had the testing they deserve. If there is odd behavior around
  421.     //       dash speeds then this would be the first place I'd look.
  422.  
  423.     public static float LinearD(float start, float end, float value)
  424.     {
  425.         return end - start;
  426.     }
  427.  
  428.     public static float EaseInQuadD(float start, float end, float value)
  429.     {
  430.         return 2f * (end - start) * value;
  431.     }
  432.  
  433.     public static float EaseOutQuadD(float start, float end, float value)
  434.     {
  435.         end -= start;
  436.         return -end * value - end * (value - 2);
  437.     }
  438.  
  439.     public static float EaseInOutQuadD(float start, float end, float value)
  440.     {
  441.         value /= .5f;
  442.         end -= start;
  443.  
  444.         if (value < 1)
  445.         {
  446.             return end * value;
  447.         }
  448.  
  449.         value--;
  450.  
  451.         return end * (1 - value);
  452.     }
  453.  
  454.     public static float EaseInCubicD(float start, float end, float value)
  455.     {
  456.         return 3f * (end - start) * value * value;
  457.     }
  458.  
  459.     public static float EaseOutCubicD(float start, float end, float value)
  460.     {
  461.         value--;
  462.         end -= start;
  463.         return 3f * end * value * value;
  464.     }
  465.  
  466.     public static float EaseInOutCubicD(float start, float end, float value)
  467.     {
  468.         value /= .5f;
  469.         end -= start;
  470.  
  471.         if (value < 1)
  472.         {
  473.             return (3f / 2f) * end * value * value;
  474.         }
  475.  
  476.         value -= 2;
  477.  
  478.         return (3f / 2f) * end * value * value;
  479.     }
  480.  
  481.     public static float EaseInQuartD(float start, float end, float value)
  482.     {
  483.         return 4f * (end - start) * value * value * value;
  484.     }
  485.  
  486.     public static float EaseOutQuartD(float start, float end, float value)
  487.     {
  488.         value--;
  489.         end -= start;
  490.         return -4f * end * value * value * value;
  491.     }
  492.  
  493.     public static float EaseInOutQuartD(float start, float end, float value)
  494.     {
  495.         value /= .5f;
  496.         end -= start;
  497.  
  498.         if (value < 1)
  499.         {
  500.             return 2f * end * value * value * value;
  501.         }
  502.  
  503.         value -= 2;
  504.  
  505.         return -2f * end * value * value * value;
  506.     }
  507.  
  508.     public static float EaseInQuintD(float start, float end, float value)
  509.     {
  510.         return 5f * (end - start) * value * value * value * value;
  511.     }
  512.  
  513.     public static float EaseOutQuintD(float start, float end, float value)
  514.     {
  515.         value--;
  516.         end -= start;
  517.         return 5f * end * value * value * value * value;
  518.     }
  519.  
  520.     public static float EaseInOutQuintD(float start, float end, float value)
  521.     {
  522.         value /= .5f;
  523.         end -= start;
  524.  
  525.         if (value < 1)
  526.         {
  527.             return (5f / 2f) * end * value * value * value * value;
  528.         }
  529.  
  530.         value -= 2;
  531.  
  532.         return (5f / 2f) * end * value * value * value * value;
  533.     }
  534.  
  535.     public static float EaseInSineD(float start, float end, float value)
  536.     {
  537.         return (end - start) * 0.5f * Mathf.PI * Mathf.Sin(0.5f * Mathf.PI * value);
  538.     }
  539.  
  540.     public static float EaseOutSineD(float start, float end, float value)
  541.     {
  542.         end -= start;
  543.         return (Mathf.PI * 0.5f) * end * Mathf.Cos(value * (Mathf.PI * 0.5f));
  544.     }
  545.  
  546.     public static float EaseInOutSineD(float start, float end, float value)
  547.     {
  548.         end -= start;
  549.         return end * 0.5f * Mathf.PI * Mathf.Cos(Mathf.PI * value);
  550.     }
  551.     public static float EaseInExpoD(float start, float end, float value)
  552.     {
  553.         return (10f * NATURAL_LOG_OF_2 * (end - start) * Mathf.Pow(2f, 10f * (value - 1)));
  554.     }
  555.  
  556.     public static float EaseOutExpoD(float start, float end, float value)
  557.     {
  558.         end -= start;
  559.         return 5f * NATURAL_LOG_OF_2 * end * Mathf.Pow(2f, 1f - 10f * value);
  560.     }
  561.  
  562.     public static float EaseInOutExpoD(float start, float end, float value)
  563.     {
  564.         value /= .5f;
  565.         end -= start;
  566.  
  567.         if (value < 1)
  568.         {
  569.             return 5f * NATURAL_LOG_OF_2 * end * Mathf.Pow(2f, 10f * (value - 1));
  570.         }
  571.  
  572.         value--;
  573.  
  574.         return (5f * NATURAL_LOG_OF_2 * end) / (Mathf.Pow(2f, 10f * value));
  575.     }
  576.  
  577.     public static float EaseInCircD(float start, float end, float value)
  578.     {
  579.         return ((end - start) * value) / Mathf.Sqrt(1f - value * value);
  580.     }
  581.  
  582.     public static float EaseOutCircD(float start, float end, float value)
  583.     {
  584.         value--;
  585.         end -= start;
  586.         return (-end * value) / Mathf.Sqrt(1f - value * value);
  587.     }
  588.  
  589.     public static float EaseInOutCircD(float start, float end, float value)
  590.     {
  591.         value /= .5f;
  592.         end -= start;
  593.  
  594.         if (value < 1)
  595.         {
  596.             return (end * value) / (2f * Mathf.Sqrt(1f - value * value));
  597.         }
  598.  
  599.         value -= 2;
  600.  
  601.         return (-end * value) / (2f * Mathf.Sqrt(1f - value * value));
  602.     }
  603.  
  604.     public static float EaseInBounceD(float start, float end, float value)
  605.     {
  606.         end -= start;
  607.         float d = 1f;
  608.  
  609.         return EaseOutBounceD(0, end, d - value);
  610.     }
  611.  
  612.     public static float EaseOutBounceD(float start, float end, float value)
  613.     {
  614.         value /= 1f;
  615.         end -= start;
  616.  
  617.         if (value < (1 / 2.75f))
  618.         {
  619.             return 2f * end * 7.5625f * value;
  620.         }
  621.         else if (value < (2 / 2.75f))
  622.         {
  623.             value -= (1.5f / 2.75f);
  624.             return 2f * end * 7.5625f * value;
  625.         }
  626.         else if (value < (2.5 / 2.75))
  627.         {
  628.             value -= (2.25f / 2.75f);
  629.             return 2f * end * 7.5625f * value;
  630.         }
  631.         else
  632.         {
  633.             value -= (2.625f / 2.75f);
  634.             return 2f * end * 7.5625f * value;
  635.         }
  636.     }
  637.  
  638.     public static float EaseInOutBounceD(float start, float end, float value)
  639.     {
  640.         end -= start;
  641.         float d = 1f;
  642.  
  643.         if (value < d * 0.5f)
  644.         {
  645.             return EaseInBounceD(0, end, value * 2) * 0.5f;
  646.         }
  647.         else
  648.         {
  649.             return EaseOutBounceD(0, end, value * 2 - d) * 0.5f;
  650.         }
  651.     }
  652.  
  653.     public static float EaseInBackD(float start, float end, float value)
  654.     {
  655.         float s = 1.70158f;
  656.  
  657.         return 3f * (s + 1f) * (end - start) * value * value - 2f * s * (end - start) * value;
  658.     }
  659.  
  660.     public static float EaseOutBackD(float start, float end, float value)
  661.     {
  662.         float s = 1.70158f;
  663.         end -= start;
  664.         value = (value) - 1;
  665.  
  666.         return end * ((s + 1f) * value * value + 2f * value * ((s + 1f) * value + s));
  667.     }
  668.  
  669.     public static float EaseInOutBackD(float start, float end, float value)
  670.     {
  671.         float s = 1.70158f;
  672.         end -= start;
  673.         value /= .5f;
  674.  
  675.         if ((value) < 1)
  676.         {
  677.             s *= (1.525f);
  678.             return 0.5f * end * (s + 1) * value * value + end * value * ((s + 1f) * value - s);
  679.         }
  680.  
  681.         value -= 2;
  682.         s *= (1.525f);
  683.         return 0.5f * end * ((s + 1) * value * value + 2f * value * ((s + 1f) * value + s));
  684.     }
  685.  
  686.     public static float EaseInElasticD(float start, float end, float value)
  687.     {
  688.         end -= start;
  689.  
  690.         float d = 1f;
  691.         float p = d * .3f;
  692.         float s;
  693.         float a = 0;
  694.  
  695.         if (a == 0f || a < Mathf.Abs(end))
  696.         {
  697.             a = end;
  698.             s = p / 4;
  699.         }
  700.         else
  701.         {
  702.             s = p / (2 * Mathf.PI) * Mathf.Asin(end / a);
  703.         }
  704.  
  705.         float c = 2 * Mathf.PI;
  706.  
  707.         // From an online derivative calculator, kinda hoping it is right.
  708.         return ((-a) * d * c * Mathf.Cos((c * (d * (value - 1f) - s)) / p)) / p -
  709.             5f * NATURAL_LOG_OF_2 * a * Mathf.Sin((c * (d * (value - 1f) - s)) / p) *
  710.             Mathf.Pow(2f, 10f * (value - 1f) + 1f);
  711.     }
  712.  
  713.     public static float EaseOutElasticD(float start, float end, float value)
  714.     {
  715.         end -= start;
  716.  
  717.         float d = 1f;
  718.         float p = d * .3f;
  719.         float s;
  720.         float a = 0;
  721.  
  722.         if (a == 0f || a < Mathf.Abs(end))
  723.         {
  724.             a = end;
  725.             s = p * 0.25f;
  726.         }
  727.         else
  728.         {
  729.             s = p / (2 * Mathf.PI) * Mathf.Asin(end / a);
  730.         }
  731.  
  732.         return (a * Mathf.PI * d * Mathf.Pow(2f, 1f - 10f * value) *
  733.             Mathf.Cos((2f * Mathf.PI * (d * value - s)) / p)) / p - 5f * NATURAL_LOG_OF_2 * a *
  734.             Mathf.Pow(2f, 1f - 10f * value) * Mathf.Sin((2f * Mathf.PI * (d * value - s)) / p);
  735.     }
  736.  
  737.     public static float EaseInOutElasticD(float start, float end, float value)
  738.     {
  739.         end -= start;
  740.  
  741.         float d = 1f;
  742.         float p = d * .3f;
  743.         float s;
  744.         float a = 0;
  745.  
  746.         if (a == 0f || a < Mathf.Abs(end))
  747.         {
  748.             a = end;
  749.             s = p / 4;
  750.         }
  751.         else
  752.         {
  753.             s = p / (2 * Mathf.PI) * Mathf.Asin(end / a);
  754.         }
  755.  
  756.         if (value < 1)
  757.         {
  758.             value -= 1;
  759.  
  760.             return -5f * NATURAL_LOG_OF_2 * a * Mathf.Pow(2f, 10f * value) * Mathf.Sin(2 * Mathf.PI * (d * value - 2f) / p) -
  761.                 a * Mathf.PI * d * Mathf.Pow(2f, 10f * value) * Mathf.Cos(2 * Mathf.PI * (d * value - s) / p) / p;
  762.         }
  763.  
  764.         value -= 1;
  765.  
  766.         return a * Mathf.PI * d * Mathf.Cos(2f * Mathf.PI * (d * value - s) / p) / (p * Mathf.Pow(2f, 10f * value)) -
  767.             5f * NATURAL_LOG_OF_2 * a * Mathf.Sin(2f * Mathf.PI * (d * value - s) / p) / (Mathf.Pow(2f, 10f * value));
  768.     }
  769.  
  770.     public static float SpringD(float start, float end, float value)
  771.     {
  772.         value = Mathf.Clamp01(value);
  773.         end -= start;
  774.  
  775.         // Damn... Thanks http://www.derivative-calculator.net/
  776.         return end * (6f * (1f - value) / 5f + 1f) * (-2.2f * Mathf.Pow(1f - value, 1.2f) *
  777.             Mathf.Sin(Mathf.PI * value * (2.5f * value * value * value + 0.2f)) + Mathf.Pow(1f - value, 2.2f) *
  778.             (Mathf.PI * (2.5f * value * value * value + 0.2f) + 7.5f * Mathf.PI * value * value * value) *
  779.             Mathf.Cos(Mathf.PI * value * (2.5f * value * value * value + 0.2f)) + 1f) -
  780.             6f * end * (Mathf.Pow(1 - value, 2.2f) * Mathf.Sin(Mathf.PI * value * (2.5f * value * value * value + 0.2f)) + value
  781.             / 5f);
  782.  
  783.     }
  784.  
  785.     public delegate float Function(float s, float e, float v);
  786.  
  787.     /// <summary>
  788.     /// Returns the function associated to the easingFunction enum. This value returned should be cached as it allocates memory
  789.     /// to return.
  790.     /// </summary>
  791.     /// <param name="easingFunction">The enum associated with the easing function.</param>
  792.     /// <returns>The easing function</returns>
  793.     public static Function GetEasingFunction(Ease easingFunction)
  794.     {
  795.         if (easingFunction == Ease.EaseInQuad)
  796.         {
  797.             return EaseInQuad;
  798.         }
  799.  
  800.         if (easingFunction == Ease.EaseOutQuad)
  801.         {
  802.             return EaseOutQuad;
  803.         }
  804.  
  805.         if (easingFunction == Ease.EaseInOutQuad)
  806.         {
  807.             return EaseInOutQuad;
  808.         }
  809.  
  810.         if (easingFunction == Ease.EaseInCubic)
  811.         {
  812.             return EaseInCubic;
  813.         }
  814.  
  815.         if (easingFunction == Ease.EaseOutCubic)
  816.         {
  817.             return EaseOutCubic;
  818.         }
  819.  
  820.         if (easingFunction == Ease.EaseInOutCubic)
  821.         {
  822.             return EaseInOutCubic;
  823.         }
  824.  
  825.         if (easingFunction == Ease.EaseInQuart)
  826.         {
  827.             return EaseInQuart;
  828.         }
  829.  
  830.         if (easingFunction == Ease.EaseOutQuart)
  831.         {
  832.             return EaseOutQuart;
  833.         }
  834.  
  835.         if (easingFunction == Ease.EaseInOutQuart)
  836.         {
  837.             return EaseInOutQuart;
  838.         }
  839.  
  840.         if (easingFunction == Ease.EaseInQuint)
  841.         {
  842.             return EaseInQuint;
  843.         }
  844.  
  845.         if (easingFunction == Ease.EaseOutQuint)
  846.         {
  847.             return EaseOutQuint;
  848.         }
  849.  
  850.         if (easingFunction == Ease.EaseInOutQuint)
  851.         {
  852.             return EaseInOutQuint;
  853.         }
  854.  
  855.         if (easingFunction == Ease.EaseInSine)
  856.         {
  857.             return EaseInSine;
  858.         }
  859.  
  860.         if (easingFunction == Ease.EaseOutSine)
  861.         {
  862.             return EaseOutSine;
  863.         }
  864.  
  865.         if (easingFunction == Ease.EaseInOutSine)
  866.         {
  867.             return EaseInOutSine;
  868.         }
  869.  
  870.         if (easingFunction == Ease.EaseInExpo)
  871.         {
  872.             return EaseInExpo;
  873.         }
  874.  
  875.         if (easingFunction == Ease.EaseOutExpo)
  876.         {
  877.             return EaseOutExpo;
  878.         }
  879.  
  880.         if (easingFunction == Ease.EaseInOutExpo)
  881.         {
  882.             return EaseInOutExpo;
  883.         }
  884.  
  885.         if (easingFunction == Ease.EaseInCirc)
  886.         {
  887.             return EaseInCirc;
  888.         }
  889.  
  890.         if (easingFunction == Ease.EaseOutCirc)
  891.         {
  892.             return EaseOutCirc;
  893.         }
  894.  
  895.         if (easingFunction == Ease.EaseInOutCirc)
  896.         {
  897.             return EaseInOutCirc;
  898.         }
  899.  
  900.         if (easingFunction == Ease.Linear)
  901.         {
  902.             return Linear;
  903.         }
  904.  
  905.         if (easingFunction == Ease.Spring)
  906.         {
  907.             return Spring;
  908.         }
  909.  
  910.         if (easingFunction == Ease.EaseInBounce)
  911.         {
  912.             return EaseInBounce;
  913.         }
  914.  
  915.         if (easingFunction == Ease.EaseOutBounce)
  916.         {
  917.             return EaseOutBounce;
  918.         }
  919.  
  920.         if (easingFunction == Ease.EaseInOutBounce)
  921.         {
  922.             return EaseInOutBounce;
  923.         }
  924.  
  925.         if (easingFunction == Ease.EaseInBack)
  926.         {
  927.             return EaseInBack;
  928.         }
  929.  
  930.         if (easingFunction == Ease.EaseOutBack)
  931.         {
  932.             return EaseOutBack;
  933.         }
  934.  
  935.         if (easingFunction == Ease.EaseInOutBack)
  936.         {
  937.             return EaseInOutBack;
  938.         }
  939.  
  940.         if (easingFunction == Ease.EaseInElastic)
  941.         {
  942.             return EaseInElastic;
  943.         }
  944.  
  945.         if (easingFunction == Ease.EaseOutElastic)
  946.         {
  947.             return EaseOutElastic;
  948.         }
  949.  
  950.         if (easingFunction == Ease.EaseInOutElastic)
  951.         {
  952.             return EaseInOutElastic;
  953.         }
  954.  
  955.         return null;
  956.     }
  957.  
  958.     /// <summary>
  959.     /// Gets the derivative function of the appropriate easing function. If you use an easing function for position then this
  960.     /// function can get you the speed at a given time (normalized).
  961.     /// </summary>
  962.     /// <param name="easingFunction"></param>
  963.     /// <returns>The derivative function</returns>
  964.     public static Function GetEasingFunctionDerivative(Ease easingFunction)
  965.     {
  966.         if (easingFunction == Ease.EaseInQuad)
  967.         {
  968.             return EaseInQuadD;
  969.         }
  970.  
  971.         if (easingFunction == Ease.EaseOutQuad)
  972.         {
  973.             return EaseOutQuadD;
  974.         }
  975.  
  976.         if (easingFunction == Ease.EaseInOutQuad)
  977.         {
  978.             return EaseInOutQuadD;
  979.         }
  980.  
  981.         if (easingFunction == Ease.EaseInCubic)
  982.         {
  983.             return EaseInCubicD;
  984.         }
  985.  
  986.         if (easingFunction == Ease.EaseOutCubic)
  987.         {
  988.             return EaseOutCubicD;
  989.         }
  990.  
  991.         if (easingFunction == Ease.EaseInOutCubic)
  992.         {
  993.             return EaseInOutCubicD;
  994.         }
  995.  
  996.         if (easingFunction == Ease.EaseInQuart)
  997.         {
  998.             return EaseInQuartD;
  999.         }
  1000.  
  1001.         if (easingFunction == Ease.EaseOutQuart)
  1002.         {
  1003.             return EaseOutQuartD;
  1004.         }
  1005.  
  1006.         if (easingFunction == Ease.EaseInOutQuart)
  1007.         {
  1008.             return EaseInOutQuartD;
  1009.         }
  1010.  
  1011.         if (easingFunction == Ease.EaseInQuint)
  1012.         {
  1013.             return EaseInQuintD;
  1014.         }
  1015.  
  1016.         if (easingFunction == Ease.EaseOutQuint)
  1017.         {
  1018.             return EaseOutQuintD;
  1019.         }
  1020.  
  1021.         if (easingFunction == Ease.EaseInOutQuint)
  1022.         {
  1023.             return EaseInOutQuintD;
  1024.         }
  1025.  
  1026.         if (easingFunction == Ease.EaseInSine)
  1027.         {
  1028.             return EaseInSineD;
  1029.         }
  1030.  
  1031.         if (easingFunction == Ease.EaseOutSine)
  1032.         {
  1033.             return EaseOutSineD;
  1034.         }
  1035.  
  1036.         if (easingFunction == Ease.EaseInOutSine)
  1037.         {
  1038.             return EaseInOutSineD;
  1039.         }
  1040.  
  1041.         if (easingFunction == Ease.EaseInExpo)
  1042.         {
  1043.             return EaseInExpoD;
  1044.         }
  1045.  
  1046.         if (easingFunction == Ease.EaseOutExpo)
  1047.         {
  1048.             return EaseOutExpoD;
  1049.         }
  1050.  
  1051.         if (easingFunction == Ease.EaseInOutExpo)
  1052.         {
  1053.             return EaseInOutExpoD;
  1054.         }
  1055.  
  1056.         if (easingFunction == Ease.EaseInCirc)
  1057.         {
  1058.             return EaseInCircD;
  1059.         }
  1060.  
  1061.         if (easingFunction == Ease.EaseOutCirc)
  1062.         {
  1063.             return EaseOutCircD;
  1064.         }
  1065.  
  1066.         if (easingFunction == Ease.EaseInOutCirc)
  1067.         {
  1068.             return EaseInOutCircD;
  1069.         }
  1070.  
  1071.         if (easingFunction == Ease.Linear)
  1072.         {
  1073.             return LinearD;
  1074.         }
  1075.  
  1076.         if (easingFunction == Ease.Spring)
  1077.         {
  1078.             return SpringD;
  1079.         }
  1080.  
  1081.         if (easingFunction == Ease.EaseInBounce)
  1082.         {
  1083.             return EaseInBounceD;
  1084.         }
  1085.  
  1086.         if (easingFunction == Ease.EaseOutBounce)
  1087.         {
  1088.             return EaseOutBounceD;
  1089.         }
  1090.  
  1091.         if (easingFunction == Ease.EaseInOutBounce)
  1092.         {
  1093.             return EaseInOutBounceD;
  1094.         }
  1095.  
  1096.         if (easingFunction == Ease.EaseInBack)
  1097.         {
  1098.             return EaseInBackD;
  1099.         }
  1100.  
  1101.         if (easingFunction == Ease.EaseOutBack)
  1102.         {
  1103.             return EaseOutBackD;
  1104.         }
  1105.  
  1106.         if (easingFunction == Ease.EaseInOutBack)
  1107.         {
  1108.             return EaseInOutBackD;
  1109.         }
  1110.  
  1111.         if (easingFunction == Ease.EaseInElastic)
  1112.         {
  1113.             return EaseInElasticD;
  1114.         }
  1115.  
  1116.         if (easingFunction == Ease.EaseOutElastic)
  1117.         {
  1118.             return EaseOutElasticD;
  1119.         }
  1120.  
  1121.         if (easingFunction == Ease.EaseInOutElastic)
  1122.         {
  1123.             return EaseInOutElasticD;
  1124.         }
  1125.  
  1126.         return null;
  1127.     }
  1128. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement