Interp.cs 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. using UnityEngine;
  2. using System.Collections;
  3. namespace RootMotion {
  4. /// <summary>
  5. /// Interpolation mode.
  6. /// </summary>
  7. [System.Serializable]
  8. public enum InterpolationMode
  9. {
  10. None,
  11. InOutCubic,
  12. InOutQuintic,
  13. InOutSine,
  14. InQuintic,
  15. InQuartic,
  16. InCubic,
  17. InQuadratic,
  18. InElastic,
  19. InElasticSmall,
  20. InElasticBig,
  21. InSine,
  22. InBack,
  23. OutQuintic,
  24. OutQuartic,
  25. OutCubic,
  26. OutInCubic,
  27. OutInQuartic,
  28. OutElastic,
  29. OutElasticSmall,
  30. OutElasticBig,
  31. OutSine,
  32. OutBack,
  33. OutBackCubic,
  34. OutBackQuartic,
  35. BackInCubic,
  36. BackInQuartic,
  37. };
  38. /// <summary>
  39. /// Class for various interpolation methods.
  40. /// </summary>
  41. public class Interp
  42. {
  43. #region Public methods
  44. /// <summary>
  45. /// Interpolate the specified t by InterpolationMode mode.
  46. /// </summary>
  47. /// <param name='t'>
  48. /// T.
  49. /// </param>
  50. /// <param name='mode'>
  51. /// InterpolationMode.
  52. /// </param>
  53. public static float Float(float t, InterpolationMode mode) {
  54. float interpT = 0;
  55. switch (mode) {
  56. case InterpolationMode.None:
  57. interpT = Interp.None(t, 0, 1);
  58. break;
  59. case InterpolationMode.InOutCubic:
  60. interpT = Interp.InOutCubic(t, 0, 1);
  61. break;
  62. case InterpolationMode.InOutQuintic:
  63. interpT = Interp.InOutQuintic(t, 0, 1);
  64. break;
  65. case InterpolationMode.InQuintic:
  66. interpT = Interp.InQuintic(t, 0, 1);
  67. break;
  68. case InterpolationMode.InQuartic:
  69. interpT = Interp.InQuartic(t, 0, 1);
  70. break;
  71. case InterpolationMode.InCubic:
  72. interpT = Interp.InCubic(t, 0, 1);
  73. break;
  74. case InterpolationMode.InQuadratic:
  75. interpT = Interp.InQuadratic(t, 0, 1);
  76. break;
  77. case InterpolationMode.OutQuintic:
  78. interpT = Interp.OutQuintic(t, 0, 1);
  79. break;
  80. case InterpolationMode.OutQuartic:
  81. interpT = Interp.OutQuartic(t, 0, 1);
  82. break;
  83. case InterpolationMode.OutCubic:
  84. interpT = Interp.OutCubic(t, 0, 1);
  85. break;
  86. case InterpolationMode.OutInCubic:
  87. interpT = Interp.OutInCubic(t, 0, 1);
  88. break;
  89. case InterpolationMode.OutInQuartic:
  90. interpT = Interp.OutInCubic(t, 0, 1);
  91. break;
  92. case InterpolationMode.BackInCubic:
  93. interpT = Interp.BackInCubic(t, 0, 1);
  94. break;
  95. case InterpolationMode.BackInQuartic:
  96. interpT = Interp.BackInQuartic(t, 0, 1);
  97. break;
  98. case InterpolationMode.OutBackCubic:
  99. interpT = Interp.OutBackCubic(t, 0, 1);
  100. break;
  101. case InterpolationMode.OutBackQuartic:
  102. interpT = Interp.OutBackQuartic(t, 0, 1);
  103. break;
  104. case InterpolationMode.OutElasticSmall:
  105. interpT = Interp.OutElasticSmall(t, 0, 1);
  106. break;
  107. case InterpolationMode.OutElasticBig:
  108. interpT = Interp.OutElasticBig(t, 0, 1);
  109. break;
  110. case InterpolationMode.InElasticSmall:
  111. interpT = Interp.InElasticSmall(t, 0, 1);
  112. break;
  113. case InterpolationMode.InElasticBig:
  114. interpT = Interp.InElasticBig(t, 0, 1);
  115. break;
  116. case InterpolationMode.InSine:
  117. interpT = Interp.InSine(t, 0, 1);
  118. break;
  119. case InterpolationMode.OutSine:
  120. interpT = Interp.OutSine(t, 0, 1);
  121. break;
  122. case InterpolationMode.InOutSine:
  123. interpT = Interp.InOutSine(t, 0, 1);
  124. break;
  125. case InterpolationMode.InElastic:
  126. interpT = Interp.OutElastic(t, 0, 1);
  127. break;
  128. case InterpolationMode.OutElastic:
  129. interpT = Interp.OutElastic(t, 0, 1);
  130. break;
  131. case InterpolationMode.InBack:
  132. interpT = Interp.InBack(t, 0, 1);
  133. break;
  134. case InterpolationMode.OutBack:
  135. interpT = Interp.OutBack(t, 0, 1);
  136. break;
  137. default: interpT = 0;
  138. break;
  139. }
  140. return interpT;
  141. }
  142. /// <summary>
  143. /// Interpolate between two verctors by InterpolationMode mode
  144. /// </summary>
  145. public static Vector3 V3(Vector3 v1, Vector3 v2, float t, InterpolationMode mode) {
  146. float interpT = Interp.Float(t, mode);
  147. return ((1 - interpT) * v1) + (interpT * v2);
  148. }
  149. /// <summary>
  150. /// Linear interpolation of value towards target.
  151. /// </summary>
  152. public static float LerpValue(float value, float target, float increaseSpeed, float decreaseSpeed) {
  153. if (value == target) return target;
  154. if (value < target) return Mathf.Clamp(value + Time.deltaTime * increaseSpeed, -Mathf.Infinity, target);
  155. else return Mathf.Clamp(value - Time.deltaTime * decreaseSpeed, target, Mathf.Infinity);
  156. }
  157. #endregion Public methods
  158. #region Interpolation modes
  159. private static float None (float t, float b, float c) { // time, b, distance,
  160. return b + c * (t);
  161. }
  162. private static float InOutCubic(float t, float b, float c) {
  163. float ts = t * t;
  164. float tc = ts * t;
  165. return b + c * (-2 * tc + 3 * ts);
  166. }
  167. private static float InOutQuintic (float t, float b, float c) {
  168. float ts = t * t;
  169. float tc = ts * t;
  170. return b + c * (6 * tc * ts + -15 * ts * ts + 10 * tc);
  171. }
  172. private static float InQuintic (float t, float b, float c) {
  173. float ts = t * t;
  174. float tc = ts * t;
  175. return b + c * (tc * ts);
  176. }
  177. private static float InQuartic (float t, float b, float c) {
  178. float ts = t * t;
  179. return b + c * (ts * ts);
  180. }
  181. private static float InCubic (float t, float b, float c) {
  182. float ts = t * t;
  183. float tc = ts * t;
  184. return b + c * (tc);
  185. }
  186. private static float InQuadratic (float t, float b, float c) {
  187. float ts = t * t;
  188. return b + c * (ts);
  189. }
  190. private static float OutQuintic (float t, float b, float c) {
  191. float ts = t * t;
  192. float tc = ts * t;
  193. return b + c * (tc * ts + -5 * ts * ts + 10 * tc + -10 * ts + 5 * t);
  194. }
  195. private static float OutQuartic (float t, float b, float c) {
  196. float ts = t * t;
  197. float tc = ts * t;
  198. return b + c * (-1 * ts * ts + 4 * tc + -6 * ts + 4 * t);
  199. }
  200. private static float OutCubic (float t, float b, float c) {
  201. float ts = t * t;
  202. float tc = ts * t;
  203. return b + c * (tc + -3 * ts + 3 * t);
  204. }
  205. private static float OutInCubic (float t, float b, float c) {
  206. float ts = t * t;
  207. float tc = ts * t;
  208. return b + c * (4 * tc + -6 * ts + 3 * t);
  209. }
  210. private static float OutInQuartic (float t, float b, float c) {
  211. float ts = t * t;
  212. float tc = ts * t;
  213. return b + c * (6 * tc + -9 * ts + 4 * t);
  214. }
  215. private static float BackInCubic (float t, float b, float c) {
  216. float ts = t * t;
  217. float tc = ts * t;
  218. return b + c *(4 * tc + -3 * ts);
  219. }
  220. private static float BackInQuartic (float t, float b, float c) {
  221. float ts = t * t;
  222. float tc = ts * t;
  223. return b + c * (2 * ts * ts + 2 * tc + -3 * ts);
  224. }
  225. private static float OutBackCubic (float t, float b, float c) {
  226. float ts = t * t;
  227. float tc = ts * t;
  228. return b + c * (4 * tc + -9 * ts + 6 * t);
  229. }
  230. private static float OutBackQuartic (float t, float b, float c) {
  231. float ts = t * t;
  232. float tc = ts * t;
  233. return b + c * (-2 * ts * ts + 10 * tc + -15 * ts + 8 * t);
  234. }
  235. private static float OutElasticSmall (float t, float b, float c) {
  236. float ts = t * t;
  237. float tc = ts * t;
  238. return b + c * (33 * tc * ts + -106 * ts * ts + 126 * tc + -67 * ts + 15 * t);
  239. }
  240. private static float OutElasticBig (float t, float b, float c) {
  241. float ts = t * t;
  242. float tc = ts * t;
  243. return b+c*(56*tc*ts + -175*ts*ts + 200*tc + -100*ts + 20*t);
  244. }
  245. private static float InElasticSmall (float t, float b, float c) {
  246. float ts = t * t;
  247. float tc = ts * t;
  248. return b + c * (33 * tc * ts + -59 * ts * ts + 32 * tc + -5 * ts);
  249. }
  250. private static float InElasticBig (float t, float b, float c) {
  251. float ts = t * t;
  252. float tc = ts * t;
  253. return b + c * (56 * tc * ts + -105 * ts * ts + 60 * tc + -10 * ts);
  254. }
  255. private static float InSine (float t, float b, float c) {
  256. c -= b;
  257. return -c * Mathf.Cos(t / 1 * (Mathf.PI / 2)) + c + b;
  258. }
  259. private static float OutSine (float t, float b, float c) {
  260. c -= b;
  261. return c * Mathf.Sin(t / 1 * (Mathf.PI / 2)) + b;
  262. }
  263. private static float InOutSine (float t, float b, float c) {
  264. c -= b;
  265. return -c / 2 * (Mathf.Cos(Mathf.PI * t / 1) - 1) + b;
  266. }
  267. private static float InElastic (float t, float b, float c) {
  268. c -= b;
  269. float d = 1f;
  270. float p = d * .3f;
  271. float s = 0;
  272. float a = 0;
  273. if (t == 0) return b;
  274. if ((t /= d) == 1) return b + c;
  275. if (a == 0f || a < Mathf.Abs(c)){
  276. a = c;
  277. s = p / 4;
  278. }else{
  279. s = p / (2 * Mathf.PI) * Mathf.Asin(c / a);
  280. }
  281. return -(a * Mathf.Pow(2, 10 * (t-=1)) * Mathf.Sin((t * d - s) * (2 * Mathf.PI) / p)) + b;
  282. }
  283. private static float OutElastic (float t, float b, float c) {
  284. c -= b;
  285. float d = 1f;
  286. float p = d * .3f;
  287. float s = 0;
  288. float a = 0;
  289. if (t == 0) return b;
  290. if ((t /= d) == 1) return b + c;
  291. if (a == 0f || a < Mathf.Abs(c)){
  292. a = c;
  293. s = p / 4;
  294. }else{
  295. s = p / (2 * Mathf.PI) * Mathf.Asin(c / a);
  296. }
  297. return (a * Mathf.Pow(2, -10 * t) * Mathf.Sin((t * d - s) * (2 * Mathf.PI) / p) + c + b);
  298. }
  299. private static float InBack(float t, float b, float c){
  300. c -= b;
  301. t /= 1;
  302. float s = 1.70158f;
  303. return c * (t) * t * ((s + 1) * t - s) + b;
  304. }
  305. private static float OutBack (float t, float b, float c) {
  306. float s = 1.70158f;
  307. c -= b;
  308. t = (t / 1) - 1;
  309. return c * ((t) * t * ((s + 1) * t + s) + 1) + b;
  310. }
  311. #endregion Interpolation modes
  312. }
  313. }