EdAnimationCurve.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. using System;
  4. using BansheeEngine;
  5. namespace BansheeEditor
  6. {
  7. /** @addtogroup AnimationEditor
  8. * @{
  9. */
  10. /// <summary>
  11. /// Type of tangent on a keyframe in an animation curve.
  12. /// </summary>
  13. internal enum TangentType
  14. {
  15. In = 1 << 0,
  16. Out = 1 << 1
  17. }
  18. /// <summary>
  19. /// Flags that are used for describing how are tangents calculated for a specific keyframe in an animation curve.
  20. /// Modes for "in" and "out" tangents can be combined.
  21. /// </summary>
  22. [Flags]
  23. internal enum TangentMode
  24. {
  25. /// <summary>
  26. /// Both tangents are calculated automatically based on the two surrounding keyframes.
  27. /// </summary>
  28. Auto = 0,
  29. /// <summary>
  30. /// Left tangent is calculated automatically based on the two surrounding keyframes.
  31. /// </summary>
  32. InAuto = TangentType.In | 1 << 2,
  33. /// <summary>
  34. /// Left tangent is manually adjusted by the user.
  35. /// </summary>
  36. InFree = TangentType.In | 1 << 3,
  37. /// <summary>
  38. /// Tangent is calculated automatically based on the previous keyframe.
  39. /// </summary>
  40. InLinear = TangentType.In | 1 << 4,
  41. /// <summary>
  42. /// Tangent is infinite, ensuring there is a instantaneus jump between previous and current keyframe value.
  43. /// </summary>
  44. InStep = TangentType.In | 1 << 5,
  45. /// <summary>
  46. /// Right tangents are calculated automatically based on the two surrounding keyframes.
  47. /// </summary>
  48. OutAuto = TangentType.Out | 1 << 6,
  49. /// <summary>
  50. /// Right tangent is manually adjusted by the user.
  51. /// </summary>
  52. OutFree = TangentType.Out | 1 << 7,
  53. /// <summary>
  54. /// Tangent is calculated automatically based on the next keyframe.
  55. /// </summary>
  56. OutLinear = TangentType.Out | 1 << 8,
  57. /// <summary>
  58. /// Tangent is infinite, ensuring there is a instantaneus jump between current and next keyframe value.
  59. /// </summary>
  60. OutStep = TangentType.Out | 1 << 9,
  61. /// <summary>
  62. /// Both tangents are manually adjusted by the user.
  63. /// </summary>
  64. Free = 1 << 10,
  65. }
  66. /// <summary>
  67. /// <see cref="AnimationCurve"/> wrapper for use in editor only. Allows easier manipulation of animation keyframes, and
  68. /// also stores keyframe tangent modes which are not required for non-editor curves.
  69. /// </summary>
  70. internal class EdAnimationCurve
  71. {
  72. private AnimationCurve native;
  73. private KeyFrame[] keyFrames;
  74. private TangentMode[] tangentModes;
  75. /// <summary>
  76. /// Returns tangent modes for each keyframe. Array is guaranteed to be the same size as <see cref="KeyFrames"/>.
  77. /// If modifying the array values, make sure to call <see cref="Apply"/> to save the changes on the curve.
  78. /// </summary>
  79. public TangentMode[] TangentModes
  80. {
  81. get { return tangentModes; }
  82. }
  83. /// <summary>
  84. /// All keyframes belonging to the animation curve. If modifying the keyframe values, make sure to call
  85. /// <see cref="Apply"/> to save the changes on the curve.
  86. /// </summary>
  87. public KeyFrame[] KeyFrames
  88. {
  89. get { return keyFrames; }
  90. }
  91. /// <summary>
  92. /// Returns the non-editor version of the curve.
  93. /// </summary>
  94. public AnimationCurve Normal
  95. {
  96. get { return native; }
  97. }
  98. /// <summary>
  99. /// Creates a new animation curve with zero keyframes.
  100. /// </summary>
  101. internal EdAnimationCurve()
  102. {
  103. keyFrames = new KeyFrame[0];
  104. native = new AnimationCurve(keyFrames);
  105. tangentModes = new TangentMode[0];
  106. }
  107. /// <summary>
  108. /// Creates a new editor animation curve using an existing animation curve as a basis.
  109. /// </summary>
  110. /// <param name="native">Animation curve to retrieve the keyframes from.</param>
  111. /// <param name="tangentModes">A set of tangent modes for each keyframe. Should be the same size as the number
  112. /// of keyframes in the provided animation. Can be null in which case all keyframes will
  113. /// have tangents set to automatic.</param>
  114. internal EdAnimationCurve(AnimationCurve native, TangentMode[] tangentModes)
  115. {
  116. this.native = native;
  117. keyFrames = native.KeyFrames;
  118. this.tangentModes = new TangentMode[keyFrames.Length];
  119. if (tangentModes != null)
  120. {
  121. int numTangents = Math.Min(keyFrames.Length, tangentModes.Length);
  122. Array.Copy(tangentModes, this.tangentModes, numTangents);
  123. }
  124. Apply();
  125. }
  126. /// <summary>
  127. /// Evaluate the animation curve at the specified time.
  128. /// </summary>
  129. /// <param name="time">Time to evaluate the curve at. </param>
  130. /// <param name="loop">If true the curve will loop when it goes past the end or beggining. Otherwise the curve
  131. /// value will be clamped.</param>
  132. /// <returns>Interpolated value from the curve at provided time.</returns>
  133. internal float Evaluate(float time, bool loop = true)
  134. {
  135. return native.Evaluate(time, loop);
  136. }
  137. /// <summary>
  138. /// Adds a new keyframe to the animation curve. Keyframe will use the automatic tangent mode.
  139. /// </summary>
  140. /// <param name="time">Time at which to add the keyframe.</param>
  141. /// <param name="value">Value of the keyframe.</param>
  142. internal void AddKeyframe(float time, float value)
  143. {
  144. AddKeyframe(time, value, TangentMode.Auto);
  145. }
  146. /// <summary>
  147. /// Adds a new keyframe to the animation curve.
  148. /// </summary>
  149. /// <param name="time">Time at which to add the keyframe.</param>
  150. /// <param name="value">Value of the keyframe.</param>
  151. /// <param name="tangentMode">Tangent mode of the keyframe.</param>
  152. internal void AddKeyframe(float time, float value, TangentMode tangentMode)
  153. {
  154. KeyFrame[] newKeyFrames = new KeyFrame[keyFrames.Length + 1];
  155. newKeyFrames[newKeyFrames.Length - 1].time = float.PositiveInfinity;
  156. TangentMode[] newTangentModes = new TangentMode[tangentModes.Length + 1];
  157. int insertIdx = keyFrames.Length;
  158. for (int i = 0; i < keyFrames.Length; i++)
  159. {
  160. if (time < keyFrames[i].time)
  161. {
  162. insertIdx = i;
  163. break;
  164. }
  165. }
  166. Array.Copy(keyFrames, newKeyFrames, insertIdx);
  167. Array.Copy(tangentModes, newTangentModes, insertIdx);
  168. KeyFrame keyFrame = new KeyFrame();
  169. keyFrame.time = time;
  170. keyFrame.value = value;
  171. newKeyFrames[insertIdx] = keyFrame;
  172. newTangentModes[insertIdx] = tangentMode;
  173. if (insertIdx < keyFrames.Length)
  174. {
  175. int remaining = keyFrames.Length - insertIdx;
  176. Array.Copy(keyFrames, insertIdx, newKeyFrames, insertIdx + 1, remaining);
  177. Array.Copy(tangentModes, insertIdx, newTangentModes, insertIdx + 1, remaining);
  178. }
  179. tangentModes = newTangentModes;
  180. keyFrames = newKeyFrames;
  181. }
  182. /// <summary>
  183. /// Removes a keyframe at the specified index.
  184. /// </summary>
  185. /// <param name="index">Index of the keyframe, referencing the <see cref="KeyFrames"/> array.</param>
  186. internal void RemoveKeyframe(int index)
  187. {
  188. if (index < 0 || index >= KeyFrames.Length)
  189. return;
  190. KeyFrame[] newKeyFrames = new KeyFrame[KeyFrames.Length - 1];
  191. TangentMode[] newTangentModes = new TangentMode[tangentModes.Length - 1];
  192. Array.Copy(KeyFrames, newKeyFrames, index);
  193. Array.Copy(tangentModes, newTangentModes, index);
  194. if (index < newKeyFrames.Length)
  195. {
  196. int remaining = newKeyFrames.Length - index;
  197. Array.Copy(KeyFrames, index + 1, newKeyFrames, index, remaining);
  198. Array.Copy(tangentModes, index + 1, newTangentModes, index, remaining);
  199. }
  200. tangentModes = newTangentModes;
  201. keyFrames = newKeyFrames;
  202. }
  203. /// <summary>
  204. /// Updates key-frame time and value. Since keyframes are ordered by time the index of the keyframe might change,
  205. /// so a new index of the keyframe is returned by this method.
  206. /// </summary>
  207. /// <param name="index">Index of the keyframe to update, referencing the <see cref="KeyFrames"/> array.</param>
  208. /// <param name="time">Time to which to set the keyframe.</param>
  209. /// <param name="value">Value of the keyframe.</param>
  210. /// <returns>New index of the keyframe, referencing the <see cref="KeyFrames"/> array.</returns>
  211. internal int UpdateKeyframe(int index, float time, float value)
  212. {
  213. if (index < 0 || index >= keyFrames.Length)
  214. return -1;
  215. keyFrames[index].time = time;
  216. keyFrames[index].value = value;
  217. // Check if key moved before or after other keys. Animation curve automatically sorts
  218. // keys and if this happens our key indices will change. So we sort it here and modify
  219. // indices.
  220. int currentKeyIndex = index;
  221. int prevKeyIdx = currentKeyIndex - 1;
  222. while (prevKeyIdx >= 0)
  223. {
  224. if (time >= keyFrames[prevKeyIdx].time)
  225. break;
  226. KeyFrame temp = keyFrames[prevKeyIdx];
  227. keyFrames[prevKeyIdx] = keyFrames[currentKeyIndex];
  228. keyFrames[currentKeyIndex] = temp;
  229. TangentMode tempMode = tangentModes[prevKeyIdx];
  230. tangentModes[prevKeyIdx] = tangentModes[currentKeyIndex];
  231. tangentModes[currentKeyIndex] = tempMode;
  232. currentKeyIndex = prevKeyIdx;
  233. prevKeyIdx--;
  234. }
  235. int nextKeyIdx = currentKeyIndex + 1;
  236. while (nextKeyIdx < keyFrames.Length)
  237. {
  238. if (time <= keyFrames[nextKeyIdx].time)
  239. break;
  240. KeyFrame temp = keyFrames[nextKeyIdx];
  241. keyFrames[nextKeyIdx] = keyFrames[currentKeyIndex];
  242. keyFrames[currentKeyIndex] = temp;
  243. TangentMode tempMode = tangentModes[nextKeyIdx];
  244. tangentModes[nextKeyIdx] = tangentModes[currentKeyIndex];
  245. tangentModes[currentKeyIndex] = tempMode;
  246. currentKeyIndex = nextKeyIdx;
  247. nextKeyIdx++;
  248. }
  249. return currentKeyIndex;
  250. }
  251. /// <summary>
  252. /// Changes the tangent mode of a keyframe at the specified index.
  253. /// </summary>
  254. /// <param name="index">Index of the keyframe to update, referencing the <see cref="KeyFrames"/> array.</param>
  255. /// <param name="mode">New tangent mode of the keyframe.</param>
  256. internal void SetTangentMode(int index, TangentMode mode)
  257. {
  258. if (index < 0 || index >= tangentModes.Length)
  259. return;
  260. tangentModes[index] = mode;
  261. }
  262. /// <summary>
  263. /// Converts a keyframe tangent (slope) value into a 2D normal vector.
  264. /// </summary>
  265. /// <param name="tangent">Keyframe tangent (slope).</param>
  266. /// <returns>Normalized 2D vector pointing in the direction of the tangent.</returns>
  267. internal static Vector2 TangentToNormal(float tangent)
  268. {
  269. if(tangent == float.PositiveInfinity)
  270. return new Vector2(0, 1);
  271. Vector2 normal = new Vector2(1, tangent);
  272. return Vector2.Normalize(normal);
  273. }
  274. /// <summary>
  275. /// Converts a 2D normal vector into a keyframe tangent (slope).
  276. /// </summary>
  277. /// <param name="normal">Normalized 2D vector pointing in the direction of the tangent.</param>
  278. /// <returns>Keyframe tangent (slope).</returns>
  279. internal static float NormalToTangent(Vector2 normal)
  280. {
  281. // We know the X value must be one, use that to deduce pre-normalized length
  282. float length = 1/normal.x;
  283. // Use length to deduce the tangent (y coordinate)
  284. return MathEx.Sqrt(length*length - 1) * MathEx.Sign(normal.y);
  285. }
  286. /// <summary>
  287. /// Applies the changes of the editor curve, to the actual underlying animation curve.
  288. /// </summary>
  289. internal void Apply()
  290. {
  291. Array.Sort(keyFrames, (x, y) =>
  292. {
  293. return x.time.CompareTo(y.time);
  294. });
  295. UpdateTangents();
  296. native = new AnimationCurve(keyFrames);
  297. }
  298. /// <summary>
  299. /// Recalculates tangents for all keyframes using the keyframe values and set tangent modes.
  300. /// </summary>
  301. private void UpdateTangents()
  302. {
  303. if (keyFrames.Length == 0)
  304. return;
  305. if (keyFrames.Length == 1)
  306. {
  307. keyFrames[0].inTangent = 0.0f;
  308. keyFrames[0].outTangent = 0.0f;
  309. return;
  310. }
  311. // First keyframe
  312. {
  313. KeyFrame keyThis = keyFrames[0];
  314. KeyFrame keyNext = keyFrames[1];
  315. keyThis.inTangent = 0.0f;
  316. TangentMode tangentMode = tangentModes[0];
  317. if (tangentMode == TangentMode.Auto || tangentMode.HasFlag(TangentMode.OutAuto) || tangentMode.HasFlag(TangentMode.OutLinear))
  318. {
  319. float diff = keyNext.time - keyThis.time;
  320. if(!MathEx.ApproxEquals(diff, 0.0f))
  321. keyThis.outTangent = (keyNext.value - keyThis.value) / diff;
  322. else
  323. keyThis.outTangent = float.PositiveInfinity;
  324. }
  325. else if (tangentMode.HasFlag(TangentMode.OutStep))
  326. {
  327. keyThis.outTangent = float.PositiveInfinity;
  328. }
  329. keyFrames[0] = keyThis;
  330. }
  331. // Inner keyframes
  332. for(int i = 1; i < keyFrames.Length - 1; i++)
  333. {
  334. KeyFrame keyPrev = keyFrames[i - 1];
  335. KeyFrame keyThis = keyFrames[i];
  336. KeyFrame keyNext = keyFrames[i + 1];
  337. keyThis.inTangent = 0.0f;
  338. TangentMode tangentMode = tangentModes[i];
  339. if (tangentMode == TangentMode.Auto) // Both automatic
  340. {
  341. float diff = keyNext.time - keyPrev.time;
  342. if (!MathEx.ApproxEquals(diff, 0.0f))
  343. keyThis.outTangent = (keyNext.value - keyPrev.value) / diff;
  344. else
  345. keyThis.outTangent = float.PositiveInfinity;
  346. keyThis.inTangent = keyThis.outTangent;
  347. }
  348. else if (tangentMode == TangentMode.Free) // Both free
  349. {
  350. keyThis.inTangent = keyThis.outTangent;
  351. }
  352. else // Different per-tangent modes
  353. {
  354. // In tangent
  355. if (tangentMode.HasFlag(TangentMode.InAuto))
  356. {
  357. float diff = keyNext.time - keyPrev.time;
  358. if (!MathEx.ApproxEquals(diff, 0.0f))
  359. keyThis.inTangent = (keyNext.value - keyPrev.value)/diff;
  360. else
  361. keyThis.inTangent = float.PositiveInfinity;
  362. }
  363. else if (tangentMode.HasFlag(TangentMode.InLinear))
  364. {
  365. float diff = keyThis.time - keyPrev.time;
  366. if (!MathEx.ApproxEquals(diff, 0.0f))
  367. keyThis.inTangent = (keyThis.value - keyPrev.value) / diff;
  368. else
  369. keyThis.inTangent = float.PositiveInfinity;
  370. }
  371. else if (tangentMode.HasFlag(TangentMode.InStep))
  372. {
  373. keyThis.inTangent = float.PositiveInfinity;
  374. }
  375. // Out tangent
  376. if (tangentMode.HasFlag(TangentMode.OutAuto))
  377. {
  378. float diff = keyNext.time - keyPrev.time;
  379. if (!MathEx.ApproxEquals(diff, 0.0f))
  380. keyThis.outTangent = (keyNext.value - keyPrev.value) / diff;
  381. else
  382. keyThis.outTangent = float.PositiveInfinity;
  383. }
  384. else if (tangentMode.HasFlag(TangentMode.OutLinear))
  385. {
  386. float diff = keyNext.time - keyThis.time;
  387. if (!MathEx.ApproxEquals(diff, 0.0f))
  388. keyThis.outTangent = (keyNext.value - keyThis.value) / diff;
  389. else
  390. keyThis.outTangent = float.PositiveInfinity;
  391. }
  392. else if (tangentMode.HasFlag(TangentMode.OutStep))
  393. {
  394. keyThis.outTangent = float.PositiveInfinity;
  395. }
  396. }
  397. keyFrames[i] = keyThis;
  398. }
  399. // Last keyframe
  400. {
  401. KeyFrame keyThis = keyFrames[keyFrames.Length - 1];
  402. KeyFrame keyPrev = keyFrames[keyFrames.Length - 2];
  403. keyThis.outTangent = 0.0f;
  404. TangentMode tangentMode = tangentModes[tangentModes.Length - 1];
  405. if (tangentMode == TangentMode.Auto || tangentMode.HasFlag(TangentMode.InAuto) || tangentMode.HasFlag(TangentMode.InLinear))
  406. {
  407. float diff = keyThis.time - keyPrev.time;
  408. if (!MathEx.ApproxEquals(diff, 0.0f))
  409. keyThis.inTangent = (keyThis.value - keyPrev.value)/diff;
  410. else
  411. keyThis.inTangent = float.PositiveInfinity;
  412. }
  413. else if (tangentMode.HasFlag(TangentMode.InStep))
  414. {
  415. keyThis.inTangent = float.PositiveInfinity;
  416. }
  417. keyFrames[keyFrames.Length - 1] = keyThis;
  418. }
  419. }
  420. }
  421. /// <summary>
  422. /// Structure containing a reference to a keyframe as a curve index, and a keyframe index within that curve.
  423. /// </summary>
  424. internal struct KeyframeRef
  425. {
  426. public KeyframeRef(int curveIdx, int keyIdx)
  427. {
  428. this.curveIdx = curveIdx;
  429. this.keyIdx = keyIdx;
  430. }
  431. public int curveIdx;
  432. public int keyIdx;
  433. }
  434. /// <summary>
  435. /// Structure containing a reference to a keyframe tangent, as a keyframe reference and type of the tangent.
  436. /// </summary>
  437. internal struct TangentRef
  438. {
  439. public TangentRef(KeyframeRef keyframeRef, TangentType type)
  440. {
  441. this.keyframeRef = keyframeRef;
  442. this.type = type;
  443. }
  444. public KeyframeRef keyframeRef;
  445. public TangentType type;
  446. }
  447. /** @} */
  448. }