Kiwano Engine v1.3.x
EaseFunctions.h
1// Copyright (c) 2016-2018 Kiwano - Nomango
2//
3// Permission is hereby granted, free of charge, to any person obtaining a copy
4// of this software and associated documentation files (the "Software"), to deal
5// in the Software without restriction, including without limitation the rights
6// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7// copies of the Software, and to permit persons to whom the Software is
8// furnished to do so, subject to the following conditions:
9//
10// The above copyright notice and this permission notice shall be included in
11// all copies or substantial portions of the Software.
12//
13// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19// THE SOFTWARE.
20
21#pragma once
22#include <kiwano/math/Scalar.h>
23
24namespace kiwano
25{
26namespace math
27{
28
29inline float Linear(float step)
30{
31 return step;
32}
33
34// Ease
35
36inline float EaseIn(float step, float rate)
37{
38 return math::Pow(step, rate);
39}
40
41inline float EaseOut(float step, float rate)
42{
43 return math::Pow(step, 1.f / rate);
44}
45
46inline float EaseInOut(float step, float rate)
47{
48 if (step < .5f)
49 return .5f * math::Pow(2 * step, rate);
50 return 1.f - .5f * math::Pow(2.f - 2 * step, rate);
51}
52
53// Exponential Ease
54
55inline float EaseExponentialIn(float step)
56{
57 return math::Pow(2.f, 10 * (step - 1));
58}
59
60inline float EaseExponentialOut(float step)
61{
62 return 1.f - math::Pow(2.f, -10 * step);
63}
64
65inline float EaseExponentialInOut(float step)
66{
67 if (step < .5f)
68 return .5f * math::Pow(2.f, 10 * (2 * step - 1));
69 return 0.5f * (2 - math::Pow(2, -10 * (step * 2 - 1)));
70}
71
72// Bounce Ease
73
74inline float EaseBounceOut(float step)
75{
76 if (step < 1 / 2.75f)
77 {
78 return 7.5625f * step * step;
79 }
80 else if (step < 2 / 2.75f)
81 {
82 step -= 1.5f / 2.75f;
83 return 7.5625f * step * step + 0.75f;
84 }
85 else if (step < 2.5f / 2.75f)
86 {
87 step -= 2.25f / 2.75f;
88 return 7.5625f * step * step + 0.9375f;
89 }
90
91 step -= 2.625f / 2.75f;
92 return 7.5625f * step * step + 0.984375f;
93}
94
95inline float EaseBounceIn(float step)
96{
97 return 1 - EaseBounceOut(1 - step);
98}
99
100inline float EaseBounceInOut(float step)
101{
102 if (step < 0.5f)
103 {
104 return EaseBounceIn(step * 2) * 0.5f;
105 }
106 else
107 {
108 return EaseBounceOut(step * 2 - 1) * 0.5f + 0.5f;
109 }
110}
111
112// Elastic Ease
113
114inline float EaseElasticIn(float step, float period)
115{
116 if (step == 0 || step == 1)
117 return step;
118
119 step = step - 1;
120 return -math::Pow(2, 10 * step) * math::Sin((step - period / 4) * 360.f / period);
121}
122
123inline float EaseElasticOut(float step, float period)
124{
125 if (step == 0 || step == 1)
126 return step;
127
128 return math::Pow(2, -10 * step) * math::Sin((step - period / 4) * 360.f / period) + 1;
129}
130
131inline float EaseElasticInOut(float step, float period)
132{
133 if (step == 0 || step == 1)
134 return step;
135
136 step = step * 2 - 1;
137 if (step < 0)
138 {
139 return -0.5f * math::Pow(2, 10 * step) * math::Sin((step - period / 4) * 360.f / period);
140 }
141 return math::Pow(2, -10 * step) * math::Sin((step - period / 4) * 360.f / period) * 0.5f + 1;
142}
143
144// Back Ease
145
146inline float EaseBackIn(float step)
147{
148 const float overshoot = 1.70158f;
149 return step * step * ((overshoot + 1) * step - overshoot);
150}
151
152inline float EaseBackOut(float step)
153{
154 const float overshoot = 1.70158f;
155 step = step - 1;
156 return step * step * ((overshoot + 1) * step + overshoot) + 1;
157}
158
159inline float EaseBackInOut(float step)
160{
161 const float overshoot = 1.70158f * 1.525f;
162
163 step = step * 2;
164 if (step < 1)
165 {
166 return (step * step * ((overshoot + 1) * step - overshoot)) / 2;
167 }
168
169 step = step - 2;
170 return (step * step * ((overshoot + 1) * step + overshoot)) / 2 + 1;
171}
172
173// Sine Ease
174
175inline float EaseSineIn(float step)
176{
177 return 1.f - math::Cos(step * 90);
178}
179
180inline float EaseSineOut(float step)
181{
182 return math::Sin(step * 90);
183}
184
185inline float EaseSineInOut(float step)
186{
187 return -0.5f * (math::Cos(step * 180) - 1);
188}
189
190// Quad Ease
191
192inline float EaseQuadIn(float step)
193{
194 return step * step;
195}
196
197inline float EaseQuadOut(float step)
198{
199 return -1 * step * (step - 2);
200}
201
202inline float EaseQuadInOut(float step)
203{
204 step = step * 2;
205 if (step < 1)
206 return 0.5f * step * step;
207 --step;
208 return -0.5f * (step * (step - 2) - 1);
209}
210
211// Cubic Ease
212
213inline float EaseCubicIn(float step)
214{
215 return step * step * step;
216}
217
218inline float EaseCubicOut(float step)
219{
220 step -= 1;
221 return (step * step * step + 1);
222}
223
224inline float EaseCubicInOut(float step)
225{
226 step = step * 2;
227 if (step < 1)
228 return 0.5f * step * step * step;
229 step -= 2;
230 return 0.5f * (step * step * step + 2);
231}
232
233// Quart Ease
234
235inline float EaseQuartIn(float step)
236{
237 return step * step * step * step;
238}
239
240inline float EaseQuartOut(float step)
241{
242 step -= 1;
243 return -(step * step * step * step - 1);
244}
245
246inline float EaseQuartInOut(float step)
247{
248 step = step * 2;
249 if (step < 1)
250 return 0.5f * step * step * step * step;
251 step -= 2;
252 return -0.5f * (step * step * step * step - 2);
253}
254
255// Quint Ease
256
257inline float EaseQuintIn(float step)
258{
259 return step * step * step * step * step;
260}
261
262inline float EaseQuintOut(float step)
263{
264 step -= 1;
265 return (step * step * step * step * step + 1);
266}
267
268inline float EaseQuintInOut(float step)
269{
270 step = step * 2;
271 if (step < 1)
272 return 0.5f * step * step * step * step * step;
273 step -= 2;
274 return 0.5f * (step * step * step * step * step + 2);
275}
276
277} // namespace math
278} // namespace kiwano