77// https://www.febucci.com/2018/08/easing-functions/
88// https://easings.net/
99
10- using System . Diagnostics . Contracts ;
10+ using System ;
11+ using JetBrains . Annotations ;
1112using UnityEngine ;
1213
1314namespace DavidFDev . Tweening
@@ -30,131 +31,156 @@ public static class Ease
3031 /// <summary>
3132 /// Linearly reach the destination state.
3233 /// </summary>
34+ [ PublicAPI ]
3335 public static readonly EasingFunction Linear = t => t ;
3436
3537 /// <summary>
3638 /// Ease in (^2).
3739 /// </summary>
40+ [ PublicAPI ]
3841 public static readonly EasingFunction QuadIn = t => t * t ;
3942
4043 /// <summary>
4144 /// Ease out (^2).
4245 /// </summary>
46+ [ PublicAPI ]
4347 public static readonly EasingFunction QuadOut = t => 1 - ( 1 - t ) * ( 1 - t ) ;
4448
4549 /// <summary>
4650 /// Ease in and out (^2).
4751 /// </summary>
52+ [ PublicAPI ]
4853 public static readonly EasingFunction QuadInOut = t => t < 0.5f ? 2 * t * t : 1 - Mathf . Pow ( - 2 * t + 2 , 2 ) / 2 ;
4954
5055 /// <summary>
5156 /// Ease in (^3).
5257 /// </summary>
58+ [ PublicAPI ]
5359 public static readonly EasingFunction CubicIn = t => t * t * t ;
5460
5561 /// <summary>
5662 /// Ease out (^3).
5763 /// </summary>
64+ [ PublicAPI ]
5865 public static readonly EasingFunction CubicOut = t => 1 - Mathf . Pow ( 1 - t , 3 ) ;
5966
6067 /// <summary>
6168 /// Ease in and out (^3).
6269 /// </summary>
70+ [ PublicAPI ]
6371 public static readonly EasingFunction CubicInOut = t => t < 0.5 ? 4 * t * t * t : 1 - Mathf . Pow ( - 2 * t + 2 , 3 ) / 2 ;
6472
6573 /// <summary>
6674 /// Ease in (^4).
6775 /// </summary>
76+ [ PublicAPI ]
6877 public static readonly EasingFunction QuartIn = t => t * t * t * t ;
6978
7079 /// <summary>
7180 /// Ease out (^4).
7281 /// </summary>
82+ [ PublicAPI ]
7383 public static readonly EasingFunction QuartOut = t => 1 - Mathf . Pow ( 1 - t , 4 ) ;
7484
7585 /// <summary>
7686 /// Ease in and out (^4).
7787 /// </summary>
88+ [ PublicAPI ]
7889 public static readonly EasingFunction QuartInOut = t => t < 0.5f ? 8 * t * t * t * t : 1 - Mathf . Pow ( - 2 * t + 2 , 4 ) / 2 ;
7990
8091 /// <summary>
8192 /// Ease in using a sine wave.
8293 /// </summary>
94+ [ PublicAPI ]
8395 public static readonly EasingFunction SineIn = t => 1 - Mathf . Cos ( ( t * Mathf . PI ) / 2 ) ;
8496
8597 /// <summary>
8698 /// Ease out using a sine wave.
8799 /// </summary>
100+ [ PublicAPI ]
88101 public static readonly EasingFunction SineOut = t => Mathf . Sin ( ( t * Mathf . PI ) / 2 ) ;
89102
90103 /// <summary>
91104 /// Ease in and out using a sine wave.
92105 /// </summary>
106+ [ PublicAPI ]
93107 public static readonly EasingFunction SineInOut = t => - ( Mathf . Cos ( Mathf . PI * t ) - 1 ) / 2 ;
94108
95109 /// <summary>
96110 /// Ease in exponentially.
97111 /// </summary>
112+ [ PublicAPI ]
98113 public static readonly EasingFunction ExpoIn = t => t == 0 ? 0 : Mathf . Pow ( 2 , 10 * t - 10 ) ;
99114
100115 /// <summary>
101116 /// Ease out exponentially.
102117 /// </summary>
103- public static readonly EasingFunction ExpoOut = t => t == 1 ? 1 : 1 - Mathf . Pow ( 2 , - 10 * t ) ;
118+ [ PublicAPI ]
119+ public static readonly EasingFunction ExpoOut = t => Math . Abs ( t - 1 ) < Mathf . Epsilon ? 1 : 1 - Mathf . Pow ( 2 , - 10 * t ) ;
104120
105121 /// <summary>
106122 /// Ease in and out exponentially.
107123 /// </summary>
108- public static readonly EasingFunction ExpoInOut = t => t == 0 ? 0 : t == 1 ? 1 : t < 0.5 ? Mathf . Pow ( 2 , 20 * t - 10 ) / 2 : ( 2 - Mathf . Pow ( 2 , - 20 * t + 10 ) ) / 2 ;
124+ [ PublicAPI ]
125+ public static readonly EasingFunction ExpoInOut = t => t == 0 ? 0 : Math . Abs ( t - 1 ) < Mathf . Epsilon ? 1 : t < 0.5 ? Mathf . Pow ( 2 , 20 * t - 10 ) / 2 : ( 2 - Mathf . Pow ( 2 , - 20 * t + 10 ) ) / 2 ;
109126
110127 /// <summary>
111- /// Ease in cyclicly .
128+ /// Ease in cyclically .
112129 /// </summary>
130+ [ PublicAPI ]
113131 public static readonly EasingFunction CircIn = t => 1 - Mathf . Sqrt ( 1 - Mathf . Pow ( t , 2 ) ) ;
114132
115133 /// <summary>
116- /// Ease out cyclicly .
134+ /// Ease out cyclically .
117135 /// </summary>
136+ [ PublicAPI ]
118137 public static readonly EasingFunction CircOut = t => Mathf . Sqrt ( 1 - Mathf . Pow ( t - 1 , 2 ) ) ;
119138
120139 /// <summary>
121- /// Ease in and out cyclicly .
140+ /// Ease in and out cyclically .
122141 /// </summary>
142+ [ PublicAPI ]
123143 public static readonly EasingFunction CircInOut = t => t < 0.5f ? ( 1 - Mathf . Sqrt ( 1 - Mathf . Pow ( 2 * t , 2 ) ) ) / 2 : ( Mathf . Sqrt ( 1 - Mathf . Pow ( - 2 * t + 2 , 2 ) ) + 1 ) / 2 ;
124144
125145 /// <summary>
126146 /// In ease, slightly overshooting.
127147 /// </summary>
148+ [ PublicAPI ]
128149 public static readonly EasingFunction BackIn = t => C3 * t * t * t - C1 * t * t ;
129150
130151 /// <summary>
131152 /// Ease out, slightly overshooting.
132153 /// </summary>
154+ [ PublicAPI ]
133155 public static readonly EasingFunction BackOut = t => 1 + C3 * Mathf . Pow ( t - 1 , 3 ) + C1 * Mathf . Pow ( t - 1 , 2 ) ;
134156
135157 /// <summary>
136158 /// Ease in and out, slightly overshooting.
137159 /// </summary>
160+ [ PublicAPI ]
138161 public static readonly EasingFunction BackInOut = t => t < 0.5f
139162 ? ( Mathf . Pow ( 2 * t , 2 ) * ( ( C2 + 1 ) * 2 * t - C2 ) ) / 2
140163 : ( Mathf . Pow ( 2 * t - 2 , 2 ) * ( ( C2 + 1 ) * ( t * 2 - 2 ) + C2 ) + 2 ) / 2 ;
141164
142165 /// <summary>
143166 /// Ease in elastically.
144167 /// </summary>
145- public static readonly EasingFunction ElasticIn = t => t == 0 ? 0 : t == 1 ? 1 : - Mathf . Pow ( 2 , 10 * t - 10 ) * Mathf . Sin ( ( t * 10 - 10.75f ) * C4 ) ;
168+ [ PublicAPI ]
169+ public static readonly EasingFunction ElasticIn = t => t == 0 ? 0 : Math . Abs ( t - 1 ) < Mathf . Epsilon ? 1 : - Mathf . Pow ( 2 , 10 * t - 10 ) * Mathf . Sin ( ( t * 10 - 10.75f ) * C4 ) ;
146170
147171 /// <summary>
148172 /// Ease out elastically.
149173 /// </summary>
150- public static readonly EasingFunction ElasticOut = t => t == 0 ? 0 : t == 1 ? 1 : Mathf . Pow ( 2 , - 10 * t ) * Mathf . Sin ( ( t * 10 - 0.75f ) * C4 ) + 1 ;
174+ [ PublicAPI ]
175+ public static readonly EasingFunction ElasticOut = t => t == 0 ? 0 : Math . Abs ( t - 1 ) < Mathf . Epsilon ? 1 : Mathf . Pow ( 2 , - 10 * t ) * Mathf . Sin ( ( t * 10 - 0.75f ) * C4 ) + 1 ;
151176
152177 /// <summary>
153178 /// Ease in and out elastically.
154179 /// </summary>
180+ [ PublicAPI ]
155181 public static readonly EasingFunction ElasticInOut = t => t == 0
156182 ? 0
157- : t == 1
183+ : Math . Abs ( t - 1 ) < Mathf . Epsilon
158184 ? 1
159185 : t < 0.5f
160186 ? - ( Mathf . Pow ( 2 , 20 * t - 10 ) * Mathf . Sin ( ( 20 * t - 11.125f ) * C5 ) ) / 2
@@ -163,39 +189,37 @@ public static class Ease
163189 /// <summary>
164190 /// Ease in with a bounce.
165191 /// </summary>
192+ [ PublicAPI ]
166193 public static readonly EasingFunction BounceIn = t => 1 - BounceOut ( 1 - t ) ;
167194
168195 /// <summary>
169196 /// Ease out with a bounce.
170197 /// </summary>
198+ [ PublicAPI ]
171199 public static readonly EasingFunction BounceOut = t =>
172200 {
173201 if ( t < 1 / D1 )
174202 return N1 * t * t ;
175- else if ( t < 2 / D1 )
203+ if ( t < 2 / D1 )
176204 return N1 * ( t -= ( 1.5f / D1 ) ) * t + 0.75f ;
177- else if ( t < 2.5 / D1 )
205+ if ( t < 2.5 / D1 )
178206 return N1 * ( t -= ( 2.25f / D1 ) ) * t + 0.9375f ;
179- else
180- return N1 * ( t -= ( 2.625f / D1 ) ) * t + 0.984375f ;
207+ return N1 * ( t -= ( 2.625f / D1 ) ) * t + 0.984375f ;
181208 } ;
182209
183210 /// <summary>
184211 /// Ease in and out with a bounce.
185212 /// </summary>
213+ [ PublicAPI ]
186214 public static readonly EasingFunction BounceInOut = t => t < 0.5f
187215 ? BounceIn ( t * 2 ) * 0.5f
188216 : BackOut ( t * 2 - 1 ) * 0.5f + 0.5f ;
189217
190218 /// <summary>
191219 /// Mirrored animation - reaches destination state then returns back to original state.
192220 /// </summary>
193- public static readonly EasingFunction Spike = t =>
194- {
195- if ( t <= 0.5f )
196- return QuadIn ( t / 0.5f ) ;
197- return QuadIn ( ( 1 - t ) / 0.5f ) ;
198- } ;
221+ [ PublicAPI ]
222+ public static readonly EasingFunction Spike = t => t <= 0.5f ? QuadIn ( t / 0.5f ) : QuadIn ( ( 1 - t ) / 0.5f ) ;
199223
200224 private const float C1 = 1.70158f ;
201225 private const float C2 = C1 * 1.525f ;
@@ -214,7 +238,7 @@ public static class Ease
214238 /// </summary>
215239 /// <param name="easeType"></param>
216240 /// <returns></returns>
217- [ Pure ]
241+ [ PublicAPI , Pure , NotNull ]
218242 public static EasingFunction GetEasingFunction ( this EaseType easeType )
219243 {
220244 switch ( easeType )
@@ -286,7 +310,7 @@ public static EasingFunction GetEasingFunction(this EaseType easeType)
286310 /// </summary>
287311 /// <param name="easingFunction"></param>
288312 /// <returns>Null if the easing function is not part of the built-in collection.</returns>
289- [ Pure ]
313+ [ PublicAPI , Pure ]
290314 public static EaseType ? GetEaseType ( this EasingFunction easingFunction )
291315 {
292316 if ( easingFunction == Linear )
@@ -443,7 +467,7 @@ public static EasingFunction GetEasingFunction(this EaseType easeType)
443467 /// </summary>
444468 /// <param name="easingFunction"></param>
445469 /// <returns>Inverted easing function.</returns>
446- [ Pure ]
470+ [ PublicAPI , Pure , NotNull ]
447471 public static EasingFunction Invert ( this EasingFunction easingFunction )
448472 {
449473 return x => 1f - easingFunction ( x ) ;
@@ -539,17 +563,17 @@ public enum EaseType
539563 ExpoInOut ,
540564
541565 /// <summary>
542- /// Ease in cyclicly .
566+ /// Ease in cyclically .
543567 /// </summary>
544568 CircIn ,
545569
546570 /// <summary>
547- /// Ease out cyclicly .
571+ /// Ease out cyclically .
548572 /// </summary>
549573 CircOut ,
550574
551575 /// <summary>
552- /// Ease in and out cyclicly .
576+ /// Ease in and out cyclically .
553577 /// </summary>
554578 CircInOut ,
555579
0 commit comments