ACurve.cs 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. using System.Collections.Generic;
  2. using UnityEngine;
  3. namespace CombatLibrary.CombatLibrary.CombatCore.Utility
  4. {
  5. public struct ACurve
  6. {
  7. public List<CurveInfo> CurveInfos;
  8. public CurveType CurveType;
  9. public ACurve(CurveInfo[] curveInfos)
  10. {
  11. CurveInfos = new List<CurveInfo>();
  12. CurveInfos.AddRange(curveInfos);
  13. CurveType = CurveType.HYT;
  14. }
  15. public float Evaluate(float t)
  16. {
  17. if (CurveInfos == null)
  18. {
  19. return t;
  20. }
  21. if (CurveInfos.Count < 2)
  22. {
  23. return t;
  24. }
  25. switch (CurveType)
  26. {
  27. case CurveType.HYT:
  28. return HYT(t);
  29. break;
  30. case CurveType.BSL:
  31. return BSL(t);
  32. break;
  33. case CurveType.Linear:
  34. return Linear(t);
  35. }
  36. return 0;
  37. }
  38. public Vector2 EvaluateForVector2(float t, float currTime)
  39. {
  40. if (CurveInfos == null)
  41. {
  42. return new Vector2();
  43. }
  44. if (CurveInfos.Count < 2)
  45. {
  46. return new Vector2();
  47. }
  48. switch (CurveType)
  49. {
  50. // case CurveType.HYT:
  51. // return HYT(t);
  52. // break;
  53. case CurveType.Linear:
  54. return LinearForVector2(t, currTime);
  55. case CurveType.BSL:
  56. return BSLForVector2(t, currTime);
  57. }
  58. return new Vector2();
  59. }
  60. // 一维线性插值
  61. private float Linear(float t)
  62. {
  63. CurveInfo c1 = default;
  64. CurveInfo c2 = default;
  65. // 查找当前t所在的区间
  66. for (int i = 0; i < CurveInfos.Count; i++)
  67. {
  68. if (CurveInfos[i].t > t)
  69. {
  70. int lindex = i - 1;
  71. if (lindex < 0)
  72. {
  73. return (float)CurveInfos[i].v;
  74. }
  75. c1 = CurveInfos[i - 1];
  76. c2 = CurveInfos[i];
  77. break;
  78. }
  79. }
  80. // 如果t超过最后一个点,返回最后一个点的值
  81. if (c2.t == 0)
  82. {
  83. return (float)CurveInfos[^1].v;
  84. }
  85. // 线性插值计算
  86. float timeRange = (float)(c2.t - c1.t);
  87. float valueRange = (float)(c2.v - c1.v);
  88. float normalizedTime = (t - (float)c1.t) / timeRange;
  89. return (float)c1.v + valueRange * normalizedTime;
  90. }
  91. // 二维线性插值
  92. private Vector2 LinearForVector2(float t, float currTime)
  93. {
  94. CurveInfo c1 = default;
  95. CurveInfo c2 = default;
  96. // 查找当前currTime所在的区间
  97. for (int i = 0; i < CurveInfos.Count; i++)
  98. {
  99. if (CurveInfos[i].t > currTime)
  100. {
  101. int lindex = i - 1;
  102. if (lindex < 0)
  103. {
  104. CurveInfo endInfo = CurveInfos[i];
  105. return new Vector2((float)endInfo.t, (float)endInfo.v);
  106. }
  107. c1 = CurveInfos[i - 1];
  108. c2 = CurveInfos[i];
  109. break;
  110. }
  111. }
  112. // 如果currTime超过最后一个点,返回最后一个点的值
  113. if (c2.t == 0)
  114. {
  115. CurveInfo endInfo = CurveInfos[^1];
  116. return new Vector2((float)endInfo.t, (float)endInfo.v);
  117. }
  118. // 线性插值计算
  119. float timeRange = (float)(c2.t - c1.t);
  120. float xRange = (float)(c2.t - c1.t);
  121. float yRange = (float)(c2.v - c1.v);
  122. float normalizedTime = (currTime - (float)c1.t) / timeRange;
  123. float x = (float)c1.t + xRange * normalizedTime;
  124. float y = (float)c1.v + yRange * normalizedTime;
  125. return new Vector2(x, y);
  126. }
  127. private Vector2 BSLForVector2(float t, float currTime)
  128. {
  129. CurveInfo c1 = default;
  130. CurveInfo c2 = default;
  131. for (int i = 0; i < CurveInfos.Count; i++)
  132. {
  133. if (CurveInfos[i].BSLt > currTime)
  134. {
  135. int lindex = i - 1;
  136. if (lindex < 0)
  137. {
  138. CurveInfo endInfo = CurveInfos[i];
  139. Vector2 endPos = new Vector2((float)endInfo.t, (float)endInfo.v);
  140. return endPos;
  141. }
  142. c1 = CurveInfos[i - 1];
  143. c2 = CurveInfos[i];
  144. break;
  145. }
  146. }
  147. if (c2.t == 0)
  148. {
  149. CurveInfo endInfo = CurveInfos[^1];
  150. Vector2 endPos = new Vector2(endInfo.t, endInfo.v);
  151. return endPos;
  152. }
  153. Vector2 pos1 = new Vector2((float)c1.t, (float)c1.v);
  154. Vector2 pos2 = new Vector2((float)c1.it, (float)c1.ot);
  155. Vector2 pos4 = new Vector2((float)c2.t, (float)c2.v);
  156. Vector2 pos3 = new Vector2((float)c2.it, (float)c2.ot);
  157. // float v11 = (float) c1.t;
  158. // float v12 = (float) c1.v;
  159. // float t11 = (float) c1.it;
  160. // float t12 = (float) c1.ot;
  161. //
  162. // float v21 = (float) c2.t;
  163. // float v22 = (float) c2.v;
  164. // float t21 = (float) c2.it;
  165. // float t22 = (float) c2.ot;
  166. Vector2 a = Vector2.Lerp(pos1, pos2, t);
  167. Vector2 b = Vector2.Lerp(pos2, pos3, t);
  168. Vector2 c = Vector2.Lerp(pos3, pos4, t);
  169. Vector2 d = Vector2.Lerp(a, b, t);
  170. Vector2 e = Vector2.Lerp(b, c, t);
  171. Vector2 f = Vector2.Lerp(d, e, t);
  172. return f;
  173. }
  174. private float BSL(float t)
  175. {
  176. CurveInfo c1 = default;
  177. CurveInfo c2 = default;
  178. for (int i = 0; i < CurveInfos.Count; i++)
  179. {
  180. if (CurveInfos[i].t > t)
  181. {
  182. int lindex = i - 1;
  183. if (lindex < 0)
  184. {
  185. return (float)CurveInfos[i].v;
  186. }
  187. c1 = CurveInfos[i - 1];
  188. c2 = CurveInfos[i];
  189. break;
  190. }
  191. }
  192. if (c2.t == 0)
  193. {
  194. return (float)CurveInfos[^1].v;
  195. }
  196. Vector2 pos1 = new Vector2((float)c1.t, (float)c1.v);
  197. Vector2 pos2 = new Vector2((float)c1.it, (float)c1.ot);
  198. Vector2 pos4 = new Vector2((float)c2.t, (float)c2.v);
  199. Vector2 pos3 = new Vector2((float)c2.it, (float)c2.ot);
  200. // float v11 = (float) c1.t;
  201. // float v12 = (float) c1.v;
  202. // float t11 = (float) c1.it;
  203. // float t12 = (float) c1.ot;
  204. //
  205. // float v21 = (float) c2.t;
  206. // float v22 = (float) c2.v;
  207. // float t21 = (float) c2.it;
  208. // float t22 = (float) c2.ot;
  209. Vector2 a = Vector2.Lerp(pos1, pos2, t);
  210. Vector2 b = Vector2.Lerp(pos2, pos3, t);
  211. Vector2 c = Vector2.Lerp(pos3, pos4, t);
  212. Vector2 d = Vector2.Lerp(a, b, t);
  213. Vector2 e = Vector2.Lerp(b, c, t);
  214. Vector2 f = Vector2.Lerp(d, e, t);
  215. return f.y;
  216. }
  217. private float HYT(float t)
  218. {
  219. CurveInfo c1 = default;
  220. CurveInfo c2 = default;
  221. for (int i = 0; i < CurveInfos.Count; i++)
  222. {
  223. if (CurveInfos[i].t > t)
  224. {
  225. int lindex = i - 1;
  226. if (lindex < 0)
  227. {
  228. return (float)CurveInfos[i].v;
  229. }
  230. c1 = CurveInfos[i - 1];
  231. c2 = CurveInfos[i];
  232. break;
  233. }
  234. }
  235. if (c2.t == 0)
  236. {
  237. return (float)CurveInfos[^1].v;
  238. }
  239. float ct = (float)c2.t - c1.t;
  240. t = (t - c1.t) / ct;
  241. float ot = c1.ot * ct;
  242. float it = c2.it * ct;
  243. float t2 = t * t;
  244. float t3 = t2 * t;
  245. float a = (2 * t3 - 3 * t2 + 1);
  246. float b = t3 - 2 * t2 + t;
  247. float c = t3 - t2;
  248. float d = -2 * t3 + 3 * t2;
  249. return a * c1.v + b * ot + c * it + d * c2.v;
  250. }
  251. }
  252. }