|
| 1 | +'''Copied from ursina engine so please check it out |
| 2 | +
|
| 3 | +
|
| 4 | + Translated from https://github.com/AndrewRayCode/easing-utils/blob/master/src/easing.js''' |
| 5 | + |
| 6 | +from math import cos, pi, sqrt, sin, asin, floor |
| 7 | + |
| 8 | + |
| 9 | +def linear(t): |
| 10 | + return t |
| 11 | + |
| 12 | + |
| 13 | +def in_sine(t): |
| 14 | + return -1 * cos(t * (pi / 2)) + 1 |
| 15 | + |
| 16 | + |
| 17 | +def out_sine(t): |
| 18 | + return sin(t * (pi / 2)) |
| 19 | + |
| 20 | + |
| 21 | +def in_out_sine(t): |
| 22 | + return -.5 * (cos(pi * t) - 1) |
| 23 | + |
| 24 | + |
| 25 | +def in_quad(t): |
| 26 | + return t * t |
| 27 | + |
| 28 | + |
| 29 | +def out_quad(t): |
| 30 | + return t * (2 - t) |
| 31 | + |
| 32 | + |
| 33 | +def in_out_quad(t): |
| 34 | + if t < .5: |
| 35 | + return 2 * t * t |
| 36 | + else: |
| 37 | + return - 1 + (4 - 2 * t) * t |
| 38 | + |
| 39 | + |
| 40 | +def in_cubic(t): |
| 41 | + return t * t * t |
| 42 | + |
| 43 | + |
| 44 | +def out_cubic(t): |
| 45 | + t1 = t - 1 |
| 46 | + return t1 * t1 * t1 + 1 |
| 47 | + |
| 48 | + |
| 49 | +def in_out_cubic(t): |
| 50 | + if t < .5: |
| 51 | + return 4 * t * t * t |
| 52 | + else: |
| 53 | + return (t - 1) * (2 * t - 2) * (2 * t - 2) + 1 |
| 54 | + |
| 55 | + |
| 56 | +def in_quart(t): |
| 57 | + return t * t * t * t |
| 58 | + |
| 59 | + |
| 60 | +def out_quart(t): |
| 61 | + t1 = t - 1 |
| 62 | + return 1 - t1 * t1 * t1 * t1 |
| 63 | + |
| 64 | + |
| 65 | +def in_out_quart(t): |
| 66 | + t1 = t - 1 |
| 67 | + if t < .5: |
| 68 | + return 8 * t * t * t * t |
| 69 | + else: |
| 70 | + 1 - 8 * t1 * t1 * t1 * t1 |
| 71 | + |
| 72 | + |
| 73 | +def in_quint(t): |
| 74 | + return t * t * t * t * t |
| 75 | + |
| 76 | + |
| 77 | +def out_quint(t): |
| 78 | + t1 = t - 1 |
| 79 | + return 1 + t1 * t1 * t1 * t1 * t1 |
| 80 | + |
| 81 | + |
| 82 | +def in_out_quint(t): |
| 83 | + t1 = t - 1 |
| 84 | + if t < .5: |
| 85 | + return 16 * t * t * t * t * t |
| 86 | + else: |
| 87 | + return 1 + 16 * t1 * t1 * t1 * t1 * t1 |
| 88 | + |
| 89 | + |
| 90 | +def in_expo(t): |
| 91 | + return pow(2, 10 * (t - 1)) |
| 92 | + |
| 93 | + |
| 94 | +def out_expo(t): |
| 95 | + return -pow(2, -10 * t) + 1 |
| 96 | + |
| 97 | + |
| 98 | +def in_out_expo(t): |
| 99 | + scaledTime = t * 2 |
| 100 | + scaledTime1 = scaledTime - 1 |
| 101 | + |
| 102 | + if scaledTime < 1: |
| 103 | + return .5 * pow(2, 10 * scaledTime1) |
| 104 | + |
| 105 | + return .5 * (-pow(2, -10 * scaledTime1) + 2) |
| 106 | + |
| 107 | + |
| 108 | +def in_circ(t): |
| 109 | + scaledTime = t / 1 |
| 110 | + return -1 * (sqrt(1 - scaledTime * t) - 1) |
| 111 | + |
| 112 | + |
| 113 | +def out_circ(t): |
| 114 | + t1 = t - 1 |
| 115 | + return sqrt(1 - t1 * t1) |
| 116 | + |
| 117 | + |
| 118 | +def in_out_circ(t): |
| 119 | + scaledTime = t * 2 |
| 120 | + scaledTime1 = scaledTime - 2 |
| 121 | + |
| 122 | + if scaledTime < 1: |
| 123 | + return -.5 * (sqrt(1 - scaledTime * scaledTime) - 1) |
| 124 | + |
| 125 | + return .5 * (sqrt(1 - scaledTime1 * scaledTime1) + 1) |
| 126 | + |
| 127 | + |
| 128 | +def in_back(t, magnitude=1.70158): |
| 129 | + return t * t * ((magnitude + 1) * t - magnitude) |
| 130 | + |
| 131 | + |
| 132 | +def out_back(t, magnitude=1.70158): |
| 133 | + scaledTime = (t / 1) - 1 |
| 134 | + return ( |
| 135 | + scaledTime * scaledTime * ((magnitude + 1) * scaledTime + magnitude) |
| 136 | + ) + 1 |
| 137 | + |
| 138 | + |
| 139 | +def in_out_back(t, magnitude=1.70158): |
| 140 | + scaledTime = t * 2 |
| 141 | + scaledTime2 = scaledTime - 2 |
| 142 | + s = magnitude * 1.525 |
| 143 | + |
| 144 | + if scaledTime < 1: |
| 145 | + return .5 * scaledTime * scaledTime * ( |
| 146 | + ((s + 1) * scaledTime) - s |
| 147 | + ) |
| 148 | + return .5 * ( |
| 149 | + scaledTime2 * scaledTime2 * ((s + 1) * scaledTime2 + s) + 2 |
| 150 | + ) |
| 151 | + |
| 152 | + |
| 153 | +def in_elastic(t, magnitude=.7): |
| 154 | + if t == 0 or t == 1: |
| 155 | + return t |
| 156 | + scaledTime = t / 1 |
| 157 | + scaledTime1 = scaledTime - 1 |
| 158 | + p = 1 - magnitude |
| 159 | + s = p / (2 * pi) * asin(1) |
| 160 | + |
| 161 | + return -( |
| 162 | + pow(2, 10 * scaledTime1) * |
| 163 | + sin((scaledTime1 - s) * (2 * pi) / p) |
| 164 | + ) |
| 165 | + |
| 166 | + |
| 167 | +def out_elastic(t, magnitude=.7): |
| 168 | + p = 1 - magnitude |
| 169 | + scaledTime = t * 2 |
| 170 | + |
| 171 | + if t == 0 or t == 1: |
| 172 | + return t |
| 173 | + |
| 174 | + s = p / (2 * pi) * asin(1) |
| 175 | + return ( |
| 176 | + pow(2, -10 * scaledTime) * |
| 177 | + sin((scaledTime - s) * (2 * pi) / p) |
| 178 | + ) + 1 |
| 179 | + |
| 180 | + |
| 181 | +def in_out_elastic(t, magnitude=0.65): |
| 182 | + p = 1 - magnitude |
| 183 | + if t == 0 or t == 1: |
| 184 | + return t |
| 185 | + |
| 186 | + scaledTime = t * 2 |
| 187 | + scaledTime1 = scaledTime - 1 |
| 188 | + s = p / (2 * pi) * asin(1) |
| 189 | + |
| 190 | + if scaledTime < 1: |
| 191 | + return -.5 * ( |
| 192 | + pow(2, 10 * scaledTime1) * |
| 193 | + sin((scaledTime1 - s) * (2 * pi) / p) |
| 194 | + ) |
| 195 | + |
| 196 | + return ( |
| 197 | + pow(2, -10 * scaledTime1) * |
| 198 | + sin((scaledTime1 - s) * (2 * pi) / p) * .5 |
| 199 | + ) + 1 |
| 200 | + |
| 201 | + |
| 202 | +def out_bounce(t): |
| 203 | + scaledTime = t / 1 |
| 204 | + |
| 205 | + if scaledTime < 1 / 2.75: |
| 206 | + return 7.5625 * scaledTime * scaledTime |
| 207 | + |
| 208 | + elif scaledTime < 2 / 2.75: |
| 209 | + scaledTime2 = scaledTime - (1.5 / 2.75) |
| 210 | + return (7.5625 * scaledTime2 * scaledTime2) + .75 |
| 211 | + |
| 212 | + elif scaledTime < 2.5 / 2.75: |
| 213 | + scaledTime2 = scaledTime - (2.25 / 2.75) |
| 214 | + return (7.5625 * scaledTime2 * scaledTime2) + 0.9375 |
| 215 | + |
| 216 | + else: |
| 217 | + scaledTime2 = scaledTime - (2.625 / 2.75) |
| 218 | + return (7.5625 * scaledTime2 * scaledTime2) + 0.984375 |
| 219 | + |
| 220 | + |
| 221 | +def in_bounce(t): |
| 222 | + return 1 - out_bounce(1 - t) |
| 223 | + |
| 224 | + |
| 225 | +def in_out_bounce(t): |
| 226 | + if t < .5: |
| 227 | + return in_bounce(t * 2) * .5 |
| 228 | + |
| 229 | + return (out_bounce((t * 2) - 1) * .5) + .5 |
| 230 | + |
| 231 | + |
| 232 | +# generate boomeranged versions of all the functions |
| 233 | +import sys |
| 234 | +from textwrap import dedent |
| 235 | + |
| 236 | +for e in dir(sys.modules[__name__]): |
| 237 | + item = getattr(sys.modules[__name__], e) |
| 238 | + if callable(item): |
| 239 | + exec(dedent(f''' |
| 240 | + def {e}_boomerang(t): |
| 241 | + if t < .5: |
| 242 | + return {e}(t*2) |
| 243 | + else: |
| 244 | + return {e}(1-((t-.5)*2)) |
| 245 | + ''')) |
| 246 | + |
| 247 | + |
| 248 | +# bezier code is translated from WebKit implementation |
| 249 | +class CubicBezier: |
| 250 | + __slots__ = ['a', 'b', 'c', 'd', 'cx', 'bx', 'ax', 'cy', 'by', 'ay'] |
| 251 | + |
| 252 | + def __init__(self, a, b, c, d): |
| 253 | + self.a = a |
| 254 | + self.b = b |
| 255 | + self.c = c |
| 256 | + self.d = d |
| 257 | + # pre-calculate the polynomial coefficients |
| 258 | + # irst and last control points are implied to be (0,0) and (1.0, 1.0) |
| 259 | + self.cx = 3.0 * a |
| 260 | + self.bx = 3.0 * (c - a) - self.cx |
| 261 | + self.ax = 1.0 - self.cx - self.bx |
| 262 | + |
| 263 | + self.cy = 3.0 * b |
| 264 | + self.by = 3.0 * (d - b) - self.cy |
| 265 | + self.ay = 1.0 - self.cy - self.by |
| 266 | + |
| 267 | + def sample_curve_x(self, t): |
| 268 | + return ((self.ax * t + self.bx) * t + self.cx) * t |
| 269 | + |
| 270 | + def sample_curve_y(self, t): |
| 271 | + return ((self.ay * t + self.by) * t + self.cy) * t |
| 272 | + |
| 273 | + def sample_curve_derivative_x(self, t): |
| 274 | + return (3.0 * self.ax * t + 2.0 * self.bx) * t + self.cx |
| 275 | + |
| 276 | + def calculate(self, x, epsilon=.0001): |
| 277 | + return self.sample_curve_y(self.solve_curve_x(x, epsilon)) |
| 278 | + |
| 279 | + def solve_curve_x(self, t, epsilon=.0001): |
| 280 | + # First try a few iterations of Newton's method -- normally very fast. |
| 281 | + t0 = 0 |
| 282 | + t1 = 0 |
| 283 | + t2 = 0 |
| 284 | + x2 = 0 |
| 285 | + # d2 = 0 |
| 286 | + # i = 0 |
| 287 | + |
| 288 | + # t2 = t |
| 289 | + # for i in range(8): |
| 290 | + # x2 = self.sample_curve_x(t2) - t |
| 291 | + # if abs(x2) < epsilon: |
| 292 | + # return t2 |
| 293 | + # d2 = self.sample_curve_derivative_x(t2) |
| 294 | + # if abs(d2) < epsilon: |
| 295 | + # break |
| 296 | + # |
| 297 | + # t2 = t2 - x2 / d2 |
| 298 | + |
| 299 | + # No solution found - use bi-section |
| 300 | + t0 = 0.0 |
| 301 | + t1 = 1.0 |
| 302 | + t2 = t |
| 303 | + |
| 304 | + if t2 < t0: |
| 305 | + return t0 |
| 306 | + if t2 > t1: |
| 307 | + return t1 |
| 308 | + |
| 309 | + while t0 < t1: |
| 310 | + x2 = self.sample_curve_x(t2) |
| 311 | + if abs(x2 - t) < epsilon: |
| 312 | + return t2 |
| 313 | + if t > x2: |
| 314 | + t0 = t2 |
| 315 | + else: |
| 316 | + t1 = t2 |
| 317 | + |
| 318 | + t2 = (t1 - t0) * .5 + t0 |
| 319 | + |
| 320 | + # Give up |
| 321 | + return t2 |
0 commit comments