GLS.AnimationUtils.pas 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966
  1. //
  2. // The multimedia graphics platform GLScene https://github.com/glscene
  3. //
  4. unit GLS.AnimationUtils;
  5. (* Main purpose is to give an easy way to create an interpolation. *)
  6. interface
  7. {$I GLScene.inc}
  8. uses
  9. System.SysUtils,
  10. System.Classes,
  11. System.Math,
  12. GLS.VectorTypes,
  13. GLS.VectorGeometry;
  14. type
  15. TEaseType=
  16. (
  17. etLinear,
  18. etQuadIn,
  19. etQuadOut,
  20. etQuadInOut,
  21. etQuadOutIn,
  22. etCubicIn,
  23. etCubicOut,
  24. etCubicInOut,
  25. etCubicOutIn,
  26. etQuintIn,
  27. etQuintOut,
  28. etQuintInOut,
  29. etQuintOutIn,
  30. etSineIn,
  31. etSineOut,
  32. etSineInOut,
  33. etSineOutIn,
  34. etCircIn,
  35. etCircOut,
  36. etCircInOut,
  37. etCircOutIn,
  38. etExpoIn,
  39. etExpoOut,
  40. etExpoInOut,
  41. etExpoOutIn,
  42. etElasticIn,
  43. etElasticOut,
  44. etElasticInOut,
  45. etElasticOutIn,
  46. etBackIn,
  47. etBackOut,
  48. etBackInOut,
  49. etBackOutIn,
  50. etBounceIn,
  51. etBounceOut,
  52. etBounceInOut,
  53. etBounceOutIn
  54. );
  55. function Tweener(Current, Target: TAffineVector; Time, Duration: Single; EaseType: TEaseType): TAffineVector; overload;
  56. function Tweener(Current, Target: TGLVector; Time, Duration: Single; EaseType: TEaseType): TGLVector; overload;
  57. function Tweener(Current, Target: TVector2f; Time, Duration: Single; EaseType: TEaseType): TVector2f; overload;
  58. function Tweener(Current, Target: Single; Time, Duration: Single; EaseType: TEaseType): Single; overload;
  59. //--------------------------------------------------------------------
  60. implementation
  61. //--------------------------------------------------------------------
  62. type
  63. TEaseFunction = function(t:Single; b:Single; c:Single; d:Single):Single;
  64. (*
  65. Easing equation function for a simple linear tweening, with no easing.
  66. t Current time (in frames or seconds).
  67. b Starting value.
  68. c Change needed in value.
  69. d Expected easing duration (in frames or seconds).
  70. Result The correct value.
  71. *)
  72. function easeNone (t:Single; b:Single; c:Single; d:Single):Single;
  73. begin
  74. Result := c*t/d + b;
  75. end;
  76. (*
  77. Easing equation function for a quadratic (t^2) easing in: accelerating from zero velocity.
  78. t Current time (in frames or seconds).
  79. b Starting value.
  80. c Change needed in value.
  81. d Expected easing duration (in frames or seconds).
  82. Result The correct value.
  83. *)
  84. function easeInQuad (t:Single; b:Single; c:Single; d:Single):Single;
  85. begin
  86. t := t/d;
  87. Result := c*t*t + b;
  88. end;
  89. (*
  90. Easing equation function for a quadratic (t^2) easing out: decelerating to zero velocity.
  91. t Current time (in frames or seconds).
  92. b Starting value.
  93. c Change needed in value.
  94. d Expected easing duration (in frames or seconds).
  95. Result The correct value.
  96. *)
  97. function easeOutQuad (t:Single; b:Single; c:Single; d:Single):Single;
  98. begin
  99. t := t/d;
  100. Result := -c *t*(t-2) + b;
  101. end;
  102. (*
  103. Easing equation function for a quadratic (t^2) easing in/out: acceleration until halfway, then deceleration.
  104. t Current time (in frames or seconds).
  105. b Starting value.
  106. c Change needed in value.
  107. d Expected easing duration (in frames or seconds).
  108. Result The correct value.
  109. *)
  110. function easeInOutQuad (t:Single; b:Single; c:Single; d:Single):Single;
  111. begin
  112. t := t/(d/2);
  113. if (t < 1) then
  114. Result := c/2*t*t + b
  115. else
  116. begin
  117. t := t-1;
  118. Result := -c/2 * (t*(t-2) - 1) + b;
  119. end;
  120. end;
  121. (*
  122. Easing equation function for a quadratic (t^2) easing out/in: deceleration until halfway, then acceleration.
  123. t Current time (in frames or seconds).
  124. b Starting value.
  125. c Change needed in value.
  126. d Expected easing duration (in frames or seconds).
  127. Result The correct value.
  128. *)
  129. function easeOutInQuad (t:Single; b:Single; c:Single; d:Single):Single;
  130. begin
  131. if (t < d/2) then
  132. Result := easeOutQuad (t*2, b, c/2, d)
  133. else
  134. Result := easeInQuad((t*2)-d, b+c/2, c/2, d);
  135. end;
  136. (*
  137. Easing equation function for a cubic (t^3) easing in: accelerating from zero velocity.
  138. t Current time (in frames or seconds).
  139. b Starting value.
  140. c Change needed in value.
  141. d Expected easing duration (in frames or seconds).
  142. Result The correct value.
  143. *)
  144. function easeInCubic (t:Single; b:Single; c:Single; d:Single):Single;
  145. begin
  146. t := t/d;
  147. Result := c*t*t*t + b;
  148. end;
  149. (*
  150. Easing equation function for a cubic (t^3) easing out: decelerating from zero velocity.
  151. t Current time (in frames or seconds).
  152. b Starting value.
  153. c Change needed in value.
  154. d Expected easing duration (in frames or seconds).
  155. Result The correct value.
  156. *)
  157. function easeOutCubic (t:Single; b:Single; c:Single; d:Single):Single;
  158. begin
  159. t := t/d-1;
  160. Result := c*(t*t*t + 1) + b;
  161. end;
  162. (*
  163. Easing equation function for a cubic (t^3) easing in/out: acceleration until halfway, then deceleration.
  164. t Current time (in frames or seconds).
  165. b Starting value.
  166. c Change needed in value.
  167. d Expected easing duration (in frames or seconds).
  168. Result The correct value.
  169. *)
  170. function easeInOutCubic (t:Single; b:Single; c:Single; d:Single):Single;
  171. begin
  172. t := t/(d/2);
  173. if (t < 1) then
  174. Result := c/2*t*t*t + b
  175. else
  176. begin
  177. t := t-2;
  178. Result := c/2*(t*t*t + 2) + b;
  179. end;
  180. end;
  181. (*
  182. Easing equation function for a cubic (t^3) easing out/in: deceleration until halfway, then acceleration.
  183. t Current time (in frames or seconds).
  184. b Starting value.
  185. c Change needed in value.
  186. d Expected easing duration (in frames or seconds).
  187. Result The correct value.
  188. *)
  189. function easeOutInCubic (t:Single; b:Single; c:Single; d:Single):Single;
  190. begin
  191. if (t < d/2) then
  192. Result := easeOutCubic (t*2, b, c/2, d)
  193. else
  194. Result := easeInCubic((t*2)-d, b+c/2, c/2, d);
  195. end;
  196. (*
  197. Easing equation function for a quartic (t^4) easing in: accelerating from zero velocity.
  198. t Current time (in frames or seconds).
  199. b Starting value.
  200. c Change needed in value.
  201. d Expected easing duration (in frames or seconds).
  202. Result The correct value.
  203. *)
  204. function easeInQuart (t:Single; b:Single; c:Single; d:Single):Single;
  205. begin
  206. t := t/d;
  207. Result := c*t*t*t*t + b;
  208. end;
  209. (*
  210. Easing equation function for a quartic (t^4) easing out: decelerating from zero velocity.
  211. t Current time (in frames or seconds).
  212. b Starting value.
  213. c Change needed in value.
  214. d Expected easing duration (in frames or seconds).
  215. Result The correct value.
  216. *)
  217. function easeOutQuart (t:Single; b:Single; c:Single; d:Single):Single;
  218. begin
  219. t := t/d-1;
  220. Result := -c * (t*t*t*t - 1) + b;
  221. end;
  222. (*
  223. Easing equation function for a quartic (t^4) easing in/out: acceleration until halfway, then deceleration.
  224. t Current time (in frames or seconds).
  225. b Starting value.
  226. c Change needed in value.
  227. d Expected easing duration (in frames or seconds).
  228. Result The correct value.
  229. *)
  230. function easeInOutQuart (t:Single; b:Single; c:Single; d:Single):Single;
  231. begin
  232. t := t/(d/2);
  233. if (t < 1) then
  234. Result := c/2*t*t*t*t + b
  235. else
  236. begin
  237. t := t-2;
  238. Result := -c/2 * (t*t*t*t - 2) + b;
  239. end;
  240. end;
  241. (*
  242. Easing equation function for a quartic (t^4) easing out/in: deceleration until halfway, then acceleration.
  243. t Current time (in frames or seconds).
  244. b Starting value.
  245. c Change needed in value.
  246. d Expected easing duration (in frames or seconds).
  247. Result The correct value.
  248. *)
  249. function easeOutInQuart (t:Single; b:Single; c:Single; d:Single):Single;
  250. begin
  251. if (t < d/2) then
  252. Result := easeOutQuart (t*2, b, c/2, d)
  253. else
  254. Result := easeInQuart((t*2)-d, b+c/2, c/2, d);
  255. end;
  256. (*
  257. Easing equation function for a quintic (t^5) easing in: accelerating from zero velocity.
  258. t Current time (in frames or seconds).
  259. b Starting value.
  260. c Change needed in value.
  261. d Expected easing duration (in frames or seconds).
  262. Result The correct value.
  263. *)
  264. function easeInQuint (t:Single; b:Single; c:Single; d:Single):Single;
  265. begin
  266. t := t/d;
  267. Result := c*t*t*t*t*t + b;
  268. end;
  269. (*
  270. Easing equation function for a quintic (t^5) easing out: decelerating from zero velocity.
  271. t Current time (in frames or seconds).
  272. b Starting value.
  273. c Change needed in value.
  274. d Expected easing duration (in frames or seconds).
  275. Result The correct value.
  276. *)
  277. function easeOutQuint (t:Single; b:Single; c:Single; d:Single):Single;
  278. begin
  279. t := t/d-1;
  280. Result := c*(t*t*t*t*t + 1) + b;
  281. end;
  282. (*
  283. Easing equation function for a quintic (t^5) easing in/out: acceleration until halfway, then deceleration.
  284. t Current time (in frames or seconds).
  285. b Starting value.
  286. c Change needed in value.
  287. d Expected easing duration (in frames or seconds).
  288. Result The correct value.
  289. *)
  290. function easeInOutQuint (t:Single; b:Single; c:Single; d:Single):Single;
  291. begin
  292. t := t/(d/2);
  293. if (t < 1) then
  294. Result := c/2*t*t*t*t*t + b
  295. else
  296. begin
  297. t := t-2;
  298. Result := c/2*(t*t*t*t*t + 2) + b;
  299. end;
  300. end;
  301. (*
  302. Easing equation function for a quintic (t^5) easing out/in: deceleration until halfway, then acceleration.
  303. t Current time (in frames or seconds).
  304. b Starting value.
  305. c Change needed in value.
  306. d Expected easing duration (in frames or seconds).
  307. Result The correct value.
  308. *)
  309. function easeOutInQuint (t:Single; b:Single; c:Single; d:Single):Single;
  310. begin
  311. if (t < d/2) then
  312. Result := easeOutQuint (t*2, b, c/2, d)
  313. else
  314. Result := easeInQuint((t*2)-d, b+c/2, c/2, d);
  315. end;
  316. (*
  317. Easing equation function for a sinusoidal (sin(t)) easing in: accelerating from zero velocity.
  318. t Current time (in frames or seconds).
  319. b Starting value.
  320. c Change needed in value.
  321. d Expected easing duration (in frames or seconds).
  322. Result The correct value.
  323. *)
  324. function easeInSine (t:Single; b:Single; c:Single; d:Single):Single;
  325. begin
  326. Result := -c * cos(t/d * (PI/2)) + c + b;
  327. end;
  328. (*
  329. Easing equation function for a sinusoidal (sin(t)) easing out: decelerating from zero velocity.
  330. t Current time (in frames or seconds).
  331. b Starting value.
  332. c Change needed in value.
  333. d Expected easing duration (in frames or seconds).
  334. Result The correct value.
  335. *)
  336. function easeOutSine (t:Single; b:Single; c:Single; d:Single):Single;
  337. begin
  338. Result := c * sin(t/d * (PI/2)) + b;
  339. end;
  340. (*
  341. Easing equation function for a sinusoidal (sin(t)) easing in/out: acceleration until halfway, then deceleration.
  342. t Current time (in frames or seconds).
  343. b Starting value.
  344. c Change needed in value.
  345. d Expected easing duration (in frames or seconds).
  346. Result The correct value.
  347. *)
  348. function easeInOutSine (t:Single; b:Single; c:Single; d:Single):Single;
  349. begin
  350. Result := -c/2 * (cos(PI*t/d) - 1) + b;
  351. end;
  352. (*
  353. Easing equation function for a sinusoidal (sin(t)) easing out/in: deceleration until halfway, then acceleration.
  354. t Current time (in frames or seconds).
  355. b Starting value.
  356. c Change needed in value.
  357. d Expected easing duration (in frames or seconds).
  358. Result The correct value.
  359. *)
  360. function easeOutInSine (t:Single; b:Single; c:Single; d:Single):Single;
  361. begin
  362. if (t < d/2) then
  363. Result := easeOutSine (t*2, b, c/2, d)
  364. else
  365. Result := easeInSine((t*2)-d, b+c/2, c/2, d);
  366. end;
  367. (*
  368. Easing equation function for an exponential (2^t) easing in: accelerating from zero velocity.
  369. t Current time (in frames or seconds).
  370. b Starting value.
  371. c Change needed in value.
  372. d Expected easing duration (in frames or seconds).
  373. Result The correct value.
  374. *)
  375. function easeInExpo (t:Single; b:Single; c:Single; d:Single):Single;
  376. begin
  377. if t=0 then
  378. Result := b
  379. else
  380. Result := c * Power(2, 10 * (t/d - 1)) + b - c * 0.001;
  381. end;
  382. (*
  383. Easing equation function for an exponential (2^t) easing out: decelerating from zero velocity.
  384. t Current time (in frames or seconds).
  385. b Starting value.
  386. c Change needed in value.
  387. d Expected easing duration (in frames or seconds).
  388. Result The correct value.
  389. *)
  390. function easeOutExpo (t:Single; b:Single; c:Single; d:Single):Single;
  391. begin
  392. if t=d then
  393. Result := b+c
  394. else
  395. Result := c * 1.001 * (-Power(2, -10 * t/d) + 1) + b;
  396. end;
  397. (*
  398. Easing equation function for an exponential (2^t) easing in/out: acceleration until halfway, then deceleration.
  399. t Current time (in frames or seconds).
  400. b Starting value.
  401. c Change needed in value.
  402. d Expected easing duration (in frames or seconds).
  403. Result The correct value.
  404. *)
  405. function easeInOutExpo (t:Single; b:Single; c:Single; d:Single):Single;
  406. begin
  407. if (t=0) then
  408. Result := b
  409. else
  410. if (t=d) then
  411. Result := b+c
  412. else
  413. begin
  414. t := t/(d/2);
  415. if (t < 1) then
  416. Result := c/2 * Power(2, 10 * (t - 1)) + b - c * 0.0005
  417. else
  418. begin
  419. t := t-1;
  420. Result := c/2 * 1.0005 * (-Power(2, -10 * t) + 2) + b;
  421. end;
  422. end;
  423. end;
  424. (*
  425. Easing equation function for an exponential (2^t) easing out/in: deceleration until halfway, then acceleration.
  426. t Current time (in frames or seconds).
  427. b Starting value.
  428. c Change needed in value.
  429. d Expected easing duration (in frames or seconds).
  430. Result The correct value.
  431. *)
  432. function easeOutInExpo (t:Single; b:Single; c:Single; d:Single):Single; begin
  433. if (t < d/2) then
  434. Result := easeOutExpo (t*2, b, c/2, d)
  435. else
  436. Result := easeInExpo((t*2)-d, b+c/2, c/2, d);
  437. end;
  438. (*
  439. Easing equation function for a circular (sqrt(1-t^2)) easing in: accelerating from zero velocity.
  440. t Current time (in frames or seconds).
  441. b Starting value.
  442. c Change needed in value.
  443. d Expected easing duration (in frames or seconds).
  444. Result The correct value.
  445. *)
  446. function easeInCirc (t:Single; b:Single; c:Single; d:Single):Single;
  447. begin
  448. t := t/d;
  449. Result := -c * (Sqrt(1 - t*t) - 1) + b;
  450. end;
  451. (*
  452. Easing equation function for a circular (sqrt(1-t^2)) easing out: decelerating from zero velocity.
  453. t Current time (in frames or seconds).
  454. b Starting value.
  455. c Change needed in value.
  456. d Expected easing duration (in frames or seconds).
  457. Result The correct value.
  458. *)
  459. function easeOutCirc (t:Single; b:Single; c:Single; d:Single):Single;
  460. begin
  461. t := t/d-1;
  462. Result := c * Sqrt(1 - t*t) + b;
  463. end;
  464. (*
  465. Easing equation function for a circular (sqrt(1-t^2)) easing in/out: acceleration until halfway, then deceleration.
  466. t Current time (in frames or seconds).
  467. b Starting value.
  468. c Change needed in value.
  469. d Expected easing duration (in frames or seconds).
  470. Result The correct value.
  471. *)
  472. function easeInOutCirc (t:Single; b:Single; c:Single; d:Single):Single;
  473. begin
  474. t := t/(d/2);
  475. if (t < 1) then
  476. Result := -c/2 * (Sqrt(1 - t*t) - 1) + b
  477. else
  478. begin
  479. t := t-2;
  480. Result := c/2 * (Sqrt(1 - t*t) + 1) + b;
  481. end;
  482. end;
  483. (*
  484. Easing equation function for a circular (sqrt(1-t^2)) easing out/in: deceleration until halfway, then acceleration.
  485. t Current time (in frames or seconds).
  486. b Starting value.
  487. c Change needed in value.
  488. d Expected easing duration (in frames or seconds).
  489. Result The correct value.
  490. *)
  491. function easeOutInCirc (t:Single; b:Single; c:Single; d:Single):Single;
  492. begin
  493. if (t < d/2) then
  494. Result := easeOutCirc (t*2, b, c/2, d)
  495. else
  496. Result := easeInCirc((t*2)-d, b+c/2, c/2, d);
  497. end;
  498. (*
  499. Easing equation function for an elastic (exponentially decaying sine wave) easing in: accelerating from zero velocity.
  500. t Current time (in frames or seconds).
  501. b Starting value.
  502. c Change needed in value.
  503. d Expected easing duration (in frames or seconds).
  504. a Amplitude.
  505. p Period.
  506. Result The correct value.
  507. *)
  508. function easeInElastic (t:Single; b:Single; c:Single; d:Single):Single;
  509. var
  510. p, a, s: Single;
  511. begin
  512. if t=0 then
  513. Result := b
  514. else
  515. begin
  516. t := t/d;
  517. if t=1 then
  518. Result := b+c
  519. else
  520. begin
  521. p := d*0.3;
  522. a := c;
  523. s := p/4;
  524. t := t-1;
  525. Result := -((a * Power(2, 10*t)) * sin((t*d-s)*(2*PI)/p )) + b;
  526. end;
  527. end;
  528. end;
  529. (*
  530. Easing equation function for an elastic (exponentially decaying sine wave) easing out: decelerating from zero velocity.
  531. t Current time (in frames or seconds).
  532. b Starting value.
  533. c Change needed in value.
  534. d Expected easing duration (in frames or seconds).
  535. a Amplitude.
  536. p Period.
  537. Result The correct value.
  538. *)
  539. function easeOutElastic (t:Single; b:Single; c:Single; d:Single):Single;
  540. var
  541. p, a, s: Single;
  542. begin
  543. if t=0 then
  544. Result := b
  545. else
  546. begin
  547. t := t/d;
  548. if t=1 then
  549. Result := b+c
  550. else
  551. begin
  552. p := d*0.3;
  553. a := c;
  554. s := p/4;
  555. Result := (a* Power(2,-10*t) * sin( (t*d-s)*(2*PI)/p ) + c + b);
  556. end;
  557. end;
  558. end;
  559. (*
  560. Easing equation function for an elastic (exponentially decaying sine wave) easing in/out: acceleration until halfway, then deceleration.
  561. t Current time (in frames or seconds).
  562. b Starting value.
  563. c Change needed in value.
  564. d Expected easing duration (in frames or seconds).
  565. a Amplitude.
  566. p Period.
  567. Result The correct value.
  568. *)
  569. function easeInOutElastic (t:Single; b:Single; c:Single; d:Single):Single;
  570. var
  571. p, a, s: Single;
  572. begin
  573. if t=0 then
  574. Result := b
  575. else
  576. begin
  577. t := t/(d/2);
  578. if t=2 then
  579. Result := b+c
  580. else
  581. begin
  582. p := d*0.3*1.5;
  583. a := c;
  584. s := p/4;
  585. t := t-1;
  586. if t<1 then
  587. Result := -0.5* ((a* Power(2,10*t)) * sin( (t*d-s)*(2*PI)/p)) + b
  588. else
  589. Result := (a* Power(2,-10*t)) * sin( (t*d-s)*(2*PI)/p )*0.5 + c + b;
  590. end;
  591. end;
  592. end;
  593. (*
  594. Easing equation function for an elastic (exponentially decaying sine wave) easing out/in: deceleration until halfway, then acceleration.
  595. t Current time (in frames or seconds).
  596. b Starting value.
  597. c Change needed in value.
  598. d Expected easing duration (in frames or seconds).
  599. a Amplitude.
  600. p Period.
  601. Result The correct value.
  602. *)
  603. function easeOutInElastic (t:Single; b:Single; c:Single; d:Single):Single;
  604. begin
  605. if (t < d/2) then
  606. Result := easeOutElastic (t*2, b, c/2, d)
  607. else
  608. Result := easeInElastic((t*2)-d, b+c/2, c/2, d)
  609. end;
  610. (*
  611. Easing equation function for a back (overshooting cubic easing: (s+1)*t^3 - s*t^2) easing in: accelerating from zero velocity.
  612. t Current time (in frames or seconds).
  613. b Starting value.
  614. c Change needed in value.
  615. d Expected easing duration (in frames or seconds).
  616. s Overshoot ammount: higher s means greater overshoot (0 produces cubic easing with no overshoot, and the default value of 1.70158 produces an overshoot of 10 percent).
  617. Result The correct value.
  618. *)
  619. function easeInBack (t:Single; b:Single; c:Single; d:Single):Single;
  620. var
  621. s: Single;
  622. begin
  623. s := 1.70158;
  624. t := t/d;
  625. Result := c*t*t*((s+1)*t - s) + b;
  626. end;
  627. (*
  628. Easing equation function for a back (overshooting cubic easing: (s+1)*t^3 - s*t^2) easing out: decelerating from zero velocity.
  629. t Current time (in frames or seconds).
  630. b Starting value.
  631. c Change needed in value.
  632. d Expected easing duration (in frames or seconds).
  633. s Overshoot ammount: higher s means greater overshoot (0 produces cubic easing with no overshoot, and the default value of 1.70158 produces an overshoot of 10 percent).
  634. Result The correct value.
  635. *)
  636. function easeOutBack (t:Single; b:Single; c:Single; d:Single):Single;
  637. var
  638. s: Single;
  639. begin
  640. s := 1.70158;
  641. t := t/d-1;
  642. Result := c*(t*t*((s+1)*t + s) + 1) + b;
  643. end;
  644. (*
  645. Easing equation function for a back (overshooting cubic easing: (s+1)*t^3 - s*t^2) easing in/out: acceleration until halfway, then deceleration.
  646. t Current time (in frames or seconds).
  647. b Starting value.
  648. c Change needed in value.
  649. d Expected easing duration (in frames or seconds).
  650. s Overshoot ammount: higher s means greater overshoot (0 produces cubic easing with no overshoot, and the default value of 1.70158 produces an overshoot of 10 percent).
  651. Result The correct value.
  652. *)
  653. function easeInOutBack (t:Single; b:Single; c:Single; d:Single):Single;
  654. var
  655. s: Single;
  656. begin
  657. s := 1.70158;
  658. t := t/(d/2);
  659. s := s*1.525;
  660. if (t < 1) then
  661. Result := c/2*(t*t*((s+1)*t - s)) + b
  662. else
  663. begin
  664. t := t-2;
  665. Result := c/2*(t*t*((s+1)*t + s) + 2) + b;
  666. end;
  667. end;
  668. (*
  669. Easing equation function for a back (overshooting cubic easing: (s+1)*t^3 - s*t^2) easing out/in: deceleration until halfway, then acceleration.
  670. t Current time (in frames or seconds).
  671. b Starting value.
  672. c Change needed in value.
  673. d Expected easing duration (in frames or seconds).
  674. s Overshoot ammount: higher s means greater overshoot (0 produces cubic easing with no overshoot, and the default value of 1.70158 produces an overshoot of 10 percent).
  675. Result The correct value.
  676. *)
  677. function easeOutInBack (t:Single; b:Single; c:Single; d:Single):Single;
  678. begin
  679. if (t < d/2) then
  680. Result := easeOutBack (t*2, b, c/2, d)
  681. else
  682. Result := easeInBack((t*2)-d, b+c/2, c/2, d);
  683. end;
  684. (*
  685. Easing equation function for a bounce (exponentially decaying parabolic bounce) easing out: decelerating from zero velocity.
  686. t Current time (in frames or seconds).
  687. b Starting value.
  688. c Change needed in value.
  689. d Expected easing duration (in frames or seconds).
  690. Result The correct value.
  691. *)
  692. function easeOutBounce (t:Single; b:Single; c:Single; d:Single):Single;
  693. begin
  694. t := t/d;
  695. if t < (1/2.75) then
  696. Result := c*(7.5625*t*t) + b
  697. else
  698. if t < (2/2.75) then
  699. begin
  700. t := t-(1.5/2.75);
  701. Result := c*(7.5625*t*t + 0.75) + b;
  702. end
  703. else
  704. if (t < (2.5/2.75)) then
  705. begin
  706. t := t-(2.25/2.75);
  707. Result := c*(7.5625*t*t + 0.9375) + b;
  708. end
  709. else
  710. begin
  711. t := t-(2.625/2.75);
  712. Result := c*(7.5625*t*t + 0.984375) + b;
  713. end;
  714. end;
  715. (*
  716. Easing equation function for a bounce (exponentially decaying parabolic bounce) easing in: accelerating from zero velocity.
  717. t Current time (in frames or seconds).
  718. b Starting value.
  719. c Change needed in value.
  720. d Expected easing duration (in frames or seconds).
  721. * Result The correct value.
  722. *)
  723. function easeInBounce (t:Single; b:Single; c:Single; d:Single):Single;
  724. begin
  725. Result := c - easeOutBounce (d-t, 0, c, d) + b;
  726. end;
  727. (*
  728. Easing equation function for a bounce (exponentially decaying parabolic bounce) easing in/out: acceleration until halfway, then deceleration.
  729. t Current time (in frames or seconds).
  730. b Starting value.
  731. c Change needed in value.
  732. d Expected easing duration (in frames or seconds).
  733. * Result The correct value.
  734. *)
  735. function easeInOutBounce (t:Single; b:Single; c:Single; d:Single):Single;
  736. begin
  737. if (t < d/2) then
  738. Result := easeInBounce(t*2, 0, c, d) * 0.5 + b
  739. else
  740. Result := easeOutBounce(t*2-d, 0, c, d) * 0.5 + c*0.5 + b;
  741. end;
  742. (*
  743. Easing equation function for a bounce (exponentially decaying parabolic bounce) easing out/in: deceleration until halfway, then acceleration.
  744. t Current time (in frames or seconds).
  745. b Starting value.
  746. c Change needed in value.
  747. d Expected easing duration (in frames or seconds).
  748. Result The correct value.
  749. *)
  750. function easeOutInBounce (t:Single; b:Single; c:Single; d:Single):Single;
  751. begin
  752. if (t < d/2) then
  753. Result := easeOutBounce(t*2, b, c/2, d)
  754. else
  755. Result := easeInBounce((t*2)-d, b+c/2, c/2, d);
  756. end;
  757. function GetEaseFunctionFromType(const EaseType: TEaseType): TEaseFunction;
  758. begin
  759. case EaseType of
  760. etLinear : Result := easeNone;
  761. etQuadIn : Result := easeInQuad;
  762. etQuadOut : Result := easeOutQuad;
  763. etQuadInOut : Result := easeInOutQuad;
  764. etQuadOutIn : Result := easeOutInQuad;
  765. etCubicIn : Result := easeInCubic;
  766. etCubicOut : Result := easeOutCubic;
  767. etCubicInOut : Result := easeInOutCubic;
  768. etCubicOutIn : Result := easeOutInCubic;
  769. etQuintIn : Result := easeInQuint;
  770. etQuintOut : Result := easeOutQuint;
  771. etQuintInOut : Result := easeInOutQuint;
  772. etQuintOutIn : Result := easeOutInQuint;
  773. etSineIn : Result := easeInSine;
  774. etSineOut : Result := easeOutSine;
  775. etSineInOut : Result := easeInOutSine;
  776. etSineOutIn : Result := easeOutInSine;
  777. etCircIn : Result := easeInCirc;
  778. etCircOut : Result := easeOutCirc;
  779. etCircInOut : Result := easeInOutCirc;
  780. etCircOutIn : Result := easeOutInCirc;
  781. etExpoIn : Result := easeInExpo;
  782. etExpoOut : Result := easeOutExpo;
  783. etExpoInOut : Result := easeInOutExpo;
  784. etExpoOutIn : Result := easeOutInExpo;
  785. etElasticIn : Result := easeInElastic;
  786. etElasticOut : Result := easeOutElastic;
  787. etElasticInOut : Result := easeInOutElastic;
  788. etElasticOutIn : Result := easeOutInElastic;
  789. etBackIn : Result := easeInBack;
  790. etBackOut : Result := easeOutBack;
  791. etBackInOut : Result := easeInOutBack;
  792. etBackOutIn : Result := easeOutBack;
  793. etBounceIn : Result := easeInBounce;
  794. etBounceOut : Result := easeOutBounce;
  795. etBounceInOut : Result := easeInOutBounce;
  796. etBounceOutIn : Result := easeOutInBounce
  797. else
  798. Result := nil;
  799. end;
  800. end;
  801. function Tweener(Current, Target: TAffineVector; Time, Duration: Single; EaseType: TEaseType): TAffineVector;
  802. var
  803. i: integer;
  804. EaseFunction : TEaseFunction;
  805. begin
  806. if Time > Duration then
  807. Time := Duration;
  808. EaseFunction := GetEaseFunctionFromType(EaseType);
  809. for i := 0 to 2 do
  810. begin
  811. Target.V[i] := Target.V[i]-Current.V[i];
  812. Result.V[i] := EaseFunction(Time, Current.V[i], Target.V[i], Duration);
  813. end;
  814. end;
  815. function Tweener(Current, Target: TGLVector; Time, Duration: Single; EaseType: TEaseType): TGLVector;
  816. var
  817. i: integer;
  818. EaseFunction : TEaseFunction;
  819. begin
  820. if Time > Duration then
  821. Time := Duration;
  822. EaseFunction := GetEaseFunctionFromType(EaseType);
  823. for i := 0 to 3 do
  824. begin
  825. Target.V[i] := Target.V[i]-Current.V[i];
  826. Result.V[i] := EaseFunction(Time, Current.V[i], Target.V[i], Duration);
  827. end;
  828. end;
  829. function Tweener(Current, Target: TVector2f; Time, Duration: Single; EaseType: TEaseType): TVector2f;
  830. var
  831. i: integer;
  832. EaseFunction : TEaseFunction;
  833. begin
  834. if Time > Duration then
  835. Time := Duration;
  836. EaseFunction := GetEaseFunctionFromType(EaseType);
  837. for i := 0 to 1 do
  838. begin
  839. Target.V[i] := Target.V[i]-Current.V[i];
  840. Result.V[i] := EaseFunction(Time, Current.V[i], Target.V[i], Duration);
  841. end;
  842. end;
  843. function Tweener(Current, Target: Single; Time, Duration: Single; EaseType: TEaseType): Single;
  844. var
  845. EaseFunction : TEaseFunction;
  846. begin
  847. if Time > Duration then
  848. Time := Duration;
  849. EaseFunction := GetEaseFunctionFromType(EaseType);
  850. Result := EaseFunction(Time, Current, Target-Current, Duration);
  851. end;
  852. end.