EdAnimationCurve.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551
  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, unless a keyframe with the same time already exists in which case
  139. /// the existing keyframe is updated with the new value. Newly added keyframe will use the automatic tangent mode.
  140. /// </summary>
  141. /// <param name="time">Time at which to add/update the keyframe.</param>
  142. /// <param name="value">Value of the keyframe.</param>
  143. internal void AddOrUpdateKeyframe(float time, float value)
  144. {
  145. int keyframeIdx = -1;
  146. for (int i = 0; i < keyFrames.Length; i++)
  147. {
  148. if (MathEx.ApproxEquals(keyFrames[i].time, time))
  149. {
  150. keyframeIdx = i;
  151. break;
  152. }
  153. }
  154. if (keyframeIdx != -1)
  155. UpdateKeyframe(keyframeIdx, time, value);
  156. else
  157. AddKeyframe(time, value);
  158. }
  159. /// <summary>
  160. /// Adds a new keyframe to the animation curve. Keyframe will use the automatic tangent mode.
  161. /// </summary>
  162. /// <param name="time">Time at which to add the keyframe.</param>
  163. /// <param name="value">Value of the keyframe.</param>
  164. internal void AddKeyframe(float time, float value)
  165. {
  166. AddKeyframe(time, value, TangentMode.Auto);
  167. }
  168. /// <summary>
  169. /// Adds a new keyframe to the animation curve.
  170. /// </summary>
  171. /// <param name="time">Time at which to add the keyframe.</param>
  172. /// <param name="value">Value of the keyframe.</param>
  173. /// <param name="tangentMode">Tangent mode of the keyframe.</param>
  174. internal void AddKeyframe(float time, float value, TangentMode tangentMode)
  175. {
  176. KeyFrame[] newKeyFrames = new KeyFrame[keyFrames.Length + 1];
  177. newKeyFrames[newKeyFrames.Length - 1].time = float.PositiveInfinity;
  178. TangentMode[] newTangentModes = new TangentMode[tangentModes.Length + 1];
  179. int insertIdx = keyFrames.Length;
  180. for (int i = 0; i < keyFrames.Length; i++)
  181. {
  182. if (time < keyFrames[i].time)
  183. {
  184. insertIdx = i;
  185. break;
  186. }
  187. }
  188. Array.Copy(keyFrames, newKeyFrames, insertIdx);
  189. Array.Copy(tangentModes, newTangentModes, insertIdx);
  190. KeyFrame keyFrame = new KeyFrame();
  191. keyFrame.time = time;
  192. keyFrame.value = value;
  193. newKeyFrames[insertIdx] = keyFrame;
  194. newTangentModes[insertIdx] = tangentMode;
  195. if (insertIdx < keyFrames.Length)
  196. {
  197. int remaining = keyFrames.Length - insertIdx;
  198. Array.Copy(keyFrames, insertIdx, newKeyFrames, insertIdx + 1, remaining);
  199. Array.Copy(tangentModes, insertIdx, newTangentModes, insertIdx + 1, remaining);
  200. }
  201. tangentModes = newTangentModes;
  202. keyFrames = newKeyFrames;
  203. }
  204. /// <summary>
  205. /// Removes a keyframe at the specified index.
  206. /// </summary>
  207. /// <param name="index">Index of the keyframe, referencing the <see cref="KeyFrames"/> array.</param>
  208. internal void RemoveKeyframe(int index)
  209. {
  210. if (index < 0 || index >= KeyFrames.Length)
  211. return;
  212. KeyFrame[] newKeyFrames = new KeyFrame[KeyFrames.Length - 1];
  213. TangentMode[] newTangentModes = new TangentMode[tangentModes.Length - 1];
  214. Array.Copy(KeyFrames, newKeyFrames, index);
  215. Array.Copy(tangentModes, newTangentModes, index);
  216. if (index < newKeyFrames.Length)
  217. {
  218. int remaining = newKeyFrames.Length - index;
  219. Array.Copy(KeyFrames, index + 1, newKeyFrames, index, remaining);
  220. Array.Copy(tangentModes, index + 1, newTangentModes, index, remaining);
  221. }
  222. tangentModes = newTangentModes;
  223. keyFrames = newKeyFrames;
  224. }
  225. /// <summary>
  226. /// Updates key-frame time and value. Since keyframes are ordered by time the index of the keyframe might change,
  227. /// so a new index of the keyframe is returned by this method.
  228. /// </summary>
  229. /// <param name="index">Index of the keyframe to update, referencing the <see cref="KeyFrames"/> array.</param>
  230. /// <param name="time">Time to which to set the keyframe.</param>
  231. /// <param name="value">Value of the keyframe.</param>
  232. /// <returns>New index of the keyframe, referencing the <see cref="KeyFrames"/> array.</returns>
  233. internal int UpdateKeyframe(int index, float time, float value)
  234. {
  235. if (index < 0 || index >= keyFrames.Length)
  236. return -1;
  237. keyFrames[index].time = time;
  238. keyFrames[index].value = value;
  239. // Check if key moved before or after other keys. Animation curve automatically sorts
  240. // keys and if this happens our key indices will change. So we sort it here and modify
  241. // indices.
  242. int currentKeyIndex = index;
  243. int prevKeyIdx = currentKeyIndex - 1;
  244. while (prevKeyIdx >= 0)
  245. {
  246. if (time >= keyFrames[prevKeyIdx].time)
  247. break;
  248. KeyFrame temp = keyFrames[prevKeyIdx];
  249. keyFrames[prevKeyIdx] = keyFrames[currentKeyIndex];
  250. keyFrames[currentKeyIndex] = temp;
  251. TangentMode tempMode = tangentModes[prevKeyIdx];
  252. tangentModes[prevKeyIdx] = tangentModes[currentKeyIndex];
  253. tangentModes[currentKeyIndex] = tempMode;
  254. currentKeyIndex = prevKeyIdx;
  255. prevKeyIdx--;
  256. }
  257. int nextKeyIdx = currentKeyIndex + 1;
  258. while (nextKeyIdx < keyFrames.Length)
  259. {
  260. if (time <= keyFrames[nextKeyIdx].time)
  261. break;
  262. KeyFrame temp = keyFrames[nextKeyIdx];
  263. keyFrames[nextKeyIdx] = keyFrames[currentKeyIndex];
  264. keyFrames[currentKeyIndex] = temp;
  265. TangentMode tempMode = tangentModes[nextKeyIdx];
  266. tangentModes[nextKeyIdx] = tangentModes[currentKeyIndex];
  267. tangentModes[currentKeyIndex] = tempMode;
  268. currentKeyIndex = nextKeyIdx;
  269. nextKeyIdx++;
  270. }
  271. return currentKeyIndex;
  272. }
  273. /// <summary>
  274. /// Changes the tangent mode of a keyframe at the specified index.
  275. /// </summary>
  276. /// <param name="index">Index of the keyframe to update, referencing the <see cref="KeyFrames"/> array.</param>
  277. /// <param name="mode">New tangent mode of the keyframe.</param>
  278. internal void SetTangentMode(int index, TangentMode mode)
  279. {
  280. if (index < 0 || index >= tangentModes.Length)
  281. return;
  282. tangentModes[index] = mode;
  283. }
  284. /// <summary>
  285. /// Converts a keyframe tangent (slope) value into a 2D normal vector.
  286. /// </summary>
  287. /// <param name="tangent">Keyframe tangent (slope).</param>
  288. /// <returns>Normalized 2D vector pointing in the direction of the tangent.</returns>
  289. internal static Vector2 TangentToNormal(float tangent)
  290. {
  291. if(tangent == float.PositiveInfinity)
  292. return new Vector2(0, 1);
  293. Vector2 normal = new Vector2(1, tangent);
  294. return Vector2.Normalize(normal);
  295. }
  296. /// <summary>
  297. /// Converts a 2D normal vector into a keyframe tangent (slope).
  298. /// </summary>
  299. /// <param name="normal">Normalized 2D vector pointing in the direction of the tangent.</param>
  300. /// <returns>Keyframe tangent (slope).</returns>
  301. internal static float NormalToTangent(Vector2 normal)
  302. {
  303. // We know the X value must be one, use that to deduce pre-normalized length
  304. float length = 1/normal.x;
  305. // Use length to deduce the tangent (y coordinate)
  306. return MathEx.Sqrt(length*length - 1) * MathEx.Sign(normal.y);
  307. }
  308. /// <summary>
  309. /// Applies the changes of the editor curve, to the actual underlying animation curve.
  310. /// </summary>
  311. internal void Apply()
  312. {
  313. Array.Sort(keyFrames, (x, y) =>
  314. {
  315. return x.time.CompareTo(y.time);
  316. });
  317. UpdateTangents();
  318. native = new AnimationCurve(keyFrames);
  319. }
  320. /// <summary>
  321. /// Recalculates tangents for all keyframes using the keyframe values and set tangent modes.
  322. /// </summary>
  323. private void UpdateTangents()
  324. {
  325. if (keyFrames.Length == 0)
  326. return;
  327. if (keyFrames.Length == 1)
  328. {
  329. keyFrames[0].inTangent = 0.0f;
  330. keyFrames[0].outTangent = 0.0f;
  331. return;
  332. }
  333. // First keyframe
  334. {
  335. KeyFrame keyThis = keyFrames[0];
  336. KeyFrame keyNext = keyFrames[1];
  337. keyThis.inTangent = 0.0f;
  338. TangentMode tangentMode = tangentModes[0];
  339. if (tangentMode == TangentMode.Auto || tangentMode.HasFlag(TangentMode.OutAuto) || tangentMode.HasFlag(TangentMode.OutLinear))
  340. {
  341. float diff = keyNext.time - keyThis.time;
  342. if(!MathEx.ApproxEquals(diff, 0.0f))
  343. keyThis.outTangent = (keyNext.value - keyThis.value) / diff;
  344. else
  345. keyThis.outTangent = float.PositiveInfinity;
  346. }
  347. else if (tangentMode.HasFlag(TangentMode.OutStep))
  348. {
  349. keyThis.outTangent = float.PositiveInfinity;
  350. }
  351. keyFrames[0] = keyThis;
  352. }
  353. // Inner keyframes
  354. for(int i = 1; i < keyFrames.Length - 1; i++)
  355. {
  356. KeyFrame keyPrev = keyFrames[i - 1];
  357. KeyFrame keyThis = keyFrames[i];
  358. KeyFrame keyNext = keyFrames[i + 1];
  359. keyThis.inTangent = 0.0f;
  360. TangentMode tangentMode = tangentModes[i];
  361. if (tangentMode == TangentMode.Auto) // Both automatic
  362. {
  363. float diff = keyNext.time - keyPrev.time;
  364. if (!MathEx.ApproxEquals(diff, 0.0f))
  365. keyThis.outTangent = (keyNext.value - keyPrev.value) / diff;
  366. else
  367. keyThis.outTangent = float.PositiveInfinity;
  368. keyThis.inTangent = keyThis.outTangent;
  369. }
  370. else if (tangentMode == TangentMode.Free) // Both free
  371. {
  372. keyThis.inTangent = keyThis.outTangent;
  373. }
  374. else // Different per-tangent modes
  375. {
  376. // In tangent
  377. if (tangentMode.HasFlag(TangentMode.InAuto))
  378. {
  379. float diff = keyNext.time - keyPrev.time;
  380. if (!MathEx.ApproxEquals(diff, 0.0f))
  381. keyThis.inTangent = (keyNext.value - keyPrev.value)/diff;
  382. else
  383. keyThis.inTangent = float.PositiveInfinity;
  384. }
  385. else if (tangentMode.HasFlag(TangentMode.InLinear))
  386. {
  387. float diff = keyThis.time - keyPrev.time;
  388. if (!MathEx.ApproxEquals(diff, 0.0f))
  389. keyThis.inTangent = (keyThis.value - keyPrev.value) / diff;
  390. else
  391. keyThis.inTangent = float.PositiveInfinity;
  392. }
  393. else if (tangentMode.HasFlag(TangentMode.InStep))
  394. {
  395. keyThis.inTangent = float.PositiveInfinity;
  396. }
  397. // Out tangent
  398. if (tangentMode.HasFlag(TangentMode.OutAuto))
  399. {
  400. float diff = keyNext.time - keyPrev.time;
  401. if (!MathEx.ApproxEquals(diff, 0.0f))
  402. keyThis.outTangent = (keyNext.value - keyPrev.value) / diff;
  403. else
  404. keyThis.outTangent = float.PositiveInfinity;
  405. }
  406. else if (tangentMode.HasFlag(TangentMode.OutLinear))
  407. {
  408. float diff = keyNext.time - keyThis.time;
  409. if (!MathEx.ApproxEquals(diff, 0.0f))
  410. keyThis.outTangent = (keyNext.value - keyThis.value) / diff;
  411. else
  412. keyThis.outTangent = float.PositiveInfinity;
  413. }
  414. else if (tangentMode.HasFlag(TangentMode.OutStep))
  415. {
  416. keyThis.outTangent = float.PositiveInfinity;
  417. }
  418. }
  419. keyFrames[i] = keyThis;
  420. }
  421. // Last keyframe
  422. {
  423. KeyFrame keyThis = keyFrames[keyFrames.Length - 1];
  424. KeyFrame keyPrev = keyFrames[keyFrames.Length - 2];
  425. keyThis.outTangent = 0.0f;
  426. TangentMode tangentMode = tangentModes[tangentModes.Length - 1];
  427. if (tangentMode == TangentMode.Auto || tangentMode.HasFlag(TangentMode.InAuto) || tangentMode.HasFlag(TangentMode.InLinear))
  428. {
  429. float diff = keyThis.time - keyPrev.time;
  430. if (!MathEx.ApproxEquals(diff, 0.0f))
  431. keyThis.inTangent = (keyThis.value - keyPrev.value)/diff;
  432. else
  433. keyThis.inTangent = float.PositiveInfinity;
  434. }
  435. else if (tangentMode.HasFlag(TangentMode.InStep))
  436. {
  437. keyThis.inTangent = float.PositiveInfinity;
  438. }
  439. keyFrames[keyFrames.Length - 1] = keyThis;
  440. }
  441. }
  442. }
  443. /// <summary>
  444. /// Structure containing a reference to a keyframe as a curve index, and a keyframe index within that curve.
  445. /// </summary>
  446. internal struct KeyframeRef
  447. {
  448. public KeyframeRef(int curveIdx, int keyIdx)
  449. {
  450. this.curveIdx = curveIdx;
  451. this.keyIdx = keyIdx;
  452. }
  453. public int curveIdx;
  454. public int keyIdx;
  455. }
  456. /// <summary>
  457. /// Structure containing a reference to a keyframe tangent, as a keyframe reference and type of the tangent.
  458. /// </summary>
  459. internal struct TangentRef
  460. {
  461. public TangentRef(KeyframeRef keyframeRef, TangentType type)
  462. {
  463. this.keyframeRef = keyframeRef;
  464. this.type = type;
  465. }
  466. public KeyframeRef keyframeRef;
  467. public TangentType type;
  468. }
  469. /** @} */
  470. }