easing.h (4575B)
1 // SPDX-FileCopyrightText: 2019-2022 Connor McLaughlin <stenzek@gmail.com> 2 // SPDX-License-Identifier: (GPL-3.0 OR CC-BY-NC-ND-4.0) 3 4 #pragma once 5 #include "types.h" 6 #include <cmath> 7 8 // From https://github.com/nicolausYes/easing-functions/blob/master/src/easing.cpp 9 10 namespace Easing { 11 static constexpr float pi = 3.1415926545f; 12 13 template<typename T> 14 ALWAYS_INLINE_RELEASE static T InSine(T t) 15 { 16 return std::sin(1.5707963f * t); 17 } 18 19 template<typename T> 20 ALWAYS_INLINE_RELEASE static T OutSine(T t) 21 { 22 return 1 + std::sin(1.5707963f * (--t)); 23 } 24 25 template<typename T> 26 ALWAYS_INLINE_RELEASE static T InOutSine(T t) 27 { 28 return 0.5f * (1 + std::sin(3.1415926f * (t - 0.5f))); 29 } 30 31 template<typename T> 32 ALWAYS_INLINE_RELEASE static T InQuad(T t) 33 { 34 return t * t; 35 } 36 37 template<typename T> 38 ALWAYS_INLINE_RELEASE static T OutQuad(T t) 39 { 40 return t * (2 - t); 41 } 42 43 template<typename T> 44 ALWAYS_INLINE_RELEASE static T InOutQuad(T t) 45 { 46 return t < 0.5f ? 2 * t * t : t * (4 - 2 * t) - 1; 47 } 48 49 template<typename T> 50 ALWAYS_INLINE_RELEASE static T InCubic(T t) 51 { 52 return t * t * t; 53 } 54 55 template<typename T> 56 ALWAYS_INLINE_RELEASE static T OutCubic(T t) 57 { 58 return 1 + (--t) * t * t; 59 } 60 61 template<typename T> 62 ALWAYS_INLINE_RELEASE static T InOutCubic(T t) 63 { 64 return t < 0.5f ? 4 * t * t * t : 1 + (--t) * (2 * (--t)) * (2 * t); 65 } 66 67 template<typename T> 68 ALWAYS_INLINE_RELEASE static T InQuart(T t) 69 { 70 t *= t; 71 return t * t; 72 } 73 74 template<typename T> 75 ALWAYS_INLINE_RELEASE static T OutQuart(T t) 76 { 77 t = (--t) * t; 78 return 1 - t * t; 79 } 80 81 template<typename T> 82 ALWAYS_INLINE_RELEASE static T InOutQuart(T t) 83 { 84 if (t < 0.5) 85 { 86 t *= t; 87 return 8 * t * t; 88 } 89 else 90 { 91 t = (--t) * t; 92 return 1 - 8 * t * t; 93 } 94 } 95 96 template<typename T> 97 ALWAYS_INLINE_RELEASE static T InQuint(T t) 98 { 99 T t2 = t * t; 100 return t * t2 * t2; 101 } 102 103 template<typename T> 104 ALWAYS_INLINE_RELEASE static T OutQuint(T t) 105 { 106 T t2 = (--t) * t; 107 return 1 + t * t2 * t2; 108 } 109 110 template<typename T> 111 ALWAYS_INLINE_RELEASE static T InOutQuint(T t) 112 { 113 T t2; 114 if (t < 0.5) 115 { 116 t2 = t * t; 117 return 16 * t * t2 * t2; 118 } 119 else 120 { 121 t2 = (--t) * t; 122 return 1 + 16 * t * t2 * t2; 123 } 124 } 125 126 template<typename T> 127 ALWAYS_INLINE_RELEASE static T InExpo(T t) 128 { 129 return (std::pow(2, 8 * t) - 1) / 255; 130 } 131 132 template<typename T> 133 ALWAYS_INLINE_RELEASE static T OutExpo(T t) 134 { 135 return 1.0f - std::pow(2.0f, -8.0f * t); 136 } 137 138 template<typename T> 139 ALWAYS_INLINE_RELEASE static T InOutExpo(T t) 140 { 141 if (t < 0.5f) 142 { 143 return (std::pow(2, 16 * t) - 1) / 510; 144 } 145 else 146 { 147 return 1 - 0.5f * std::pow(2, -16 * (t - 0.5f)); 148 } 149 } 150 151 template<typename T> 152 ALWAYS_INLINE_RELEASE static T InCirc(T t) 153 { 154 return 1 - std::sqrt(1 - t); 155 } 156 157 template<typename T> 158 ALWAYS_INLINE_RELEASE static T OutCirc(T t) 159 { 160 return std::sqrt(t); 161 } 162 163 template<typename T> 164 ALWAYS_INLINE_RELEASE static T InOutCirc(T t) 165 { 166 if (t < 0.5f) 167 { 168 return (1 - std::sqrt(1 - 2 * t)) * 0.5f; 169 } 170 else 171 { 172 return (1 + std::sqrt(2 * t - 1)) * 0.5f; 173 } 174 } 175 176 template<typename T> 177 ALWAYS_INLINE_RELEASE static T InBack(T t) 178 { 179 return t * t * (2.70158f * t - 1.70158f); 180 } 181 182 template<typename T> 183 static T OutBack(T t) 184 { 185 t -= 1; 186 return 1 + t * t * (2.70158f * t + 1.70158f); 187 } 188 189 template<typename T> 190 ALWAYS_INLINE_RELEASE static T InOutBack(T t) 191 { 192 if (t < 0.5f) 193 { 194 return t * t * (7 * t - 2.5f) * 2; 195 } 196 else 197 { 198 return 1 + (--t) * t * 2 * (7 * t + 2.5f); 199 } 200 } 201 202 template<typename T> 203 ALWAYS_INLINE_RELEASE static T InElastic(T t) 204 { 205 T t2 = t * t; 206 return t2 * t2 * std::sin(t * pi * 4.5f); 207 } 208 209 template<typename T> 210 ALWAYS_INLINE_RELEASE static T OutElastic(T t) 211 { 212 T t2 = (t - 1) * (t - 1); 213 return 1 - t2 * t2 * std::cos(t * pi * 4.5f); 214 } 215 216 template<typename T> 217 ALWAYS_INLINE_RELEASE static T InOutElastic(T t) 218 { 219 T t2; 220 if (t < 0.45f) 221 { 222 t2 = t * t; 223 return 8 * t2 * t2 * std::sin(t * pi * 9); 224 } 225 else if (t < 0.55f) 226 { 227 return 0.5f + 0.75f * std::sin(t * pi * 4); 228 } 229 else 230 { 231 t2 = (t - 1) * (t - 1); 232 return 1 - 8 * t2 * t2 * std::sin(t * pi * 9); 233 } 234 } 235 236 template<typename T> 237 ALWAYS_INLINE_RELEASE static T InBounce(T t) 238 { 239 return std::pow(2, 6 * (t - 1)) * std::abs(sin(t * pi * 3.5f)); 240 } 241 242 template<typename T> 243 ALWAYS_INLINE_RELEASE static T OutBounce(T t) 244 { 245 return 1 - std::pow(2, -6 * t) * std::abs(std::cos(t * pi * 3.5f)); 246 } 247 248 template<typename T> 249 ALWAYS_INLINE_RELEASE static T InOutBounce(T t) 250 { 251 if (t < 0.5f) 252 { 253 return 8 * std::pow(2, 8 * (t - 1)) * std::abs(std::sin(t * pi * 7)); 254 } 255 else 256 { 257 return 1 - 8 * std::pow(2, -8 * t) * std::abs(std::sin(t * pi * 7)); 258 } 259 } 260 261 } // namespace Easing