CurveEditUtility.cs 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using UnityEditorInternal;
  5. using UnityEngine;
  6. using Object = UnityEngine.Object;
  7. namespace UnityEditor.Timeline
  8. {
  9. // Utility class for editing animation clips from serialized properties
  10. static class CurveEditUtility
  11. {
  12. static bool IsRotationKey(EditorCurveBinding binding)
  13. {
  14. return binding.propertyName.Contains("localEulerAnglesRaw");
  15. }
  16. public static void AddKey(AnimationClip clip, EditorCurveBinding sourceBinding, SerializedProperty prop, double time)
  17. {
  18. if (sourceBinding.isPPtrCurve)
  19. {
  20. AddObjectKey(clip, sourceBinding, prop, time);
  21. }
  22. else if (IsRotationKey(sourceBinding))
  23. {
  24. AddRotationKey(clip, sourceBinding, prop, time);
  25. }
  26. else
  27. {
  28. AddFloatKey(clip, sourceBinding, prop, time);
  29. }
  30. }
  31. static void AddObjectKey(AnimationClip clip, EditorCurveBinding sourceBinding, SerializedProperty prop, double time)
  32. {
  33. if (prop.propertyType != SerializedPropertyType.ObjectReference)
  34. return;
  35. ObjectReferenceKeyframe[] curve = null;
  36. var info = AnimationClipCurveCache.Instance.GetCurveInfo(clip);
  37. var curveIndex = Array.IndexOf(info.objectBindings, sourceBinding);
  38. if (curveIndex >= 0)
  39. {
  40. curve = info.objectCurves[curveIndex];
  41. // where in the array does the evaluation land?
  42. var evalIndex = EvaluateIndex(curve, (float)time);
  43. if (KeyCompare(curve[evalIndex].time, (float)time, clip.frameRate) == 0)
  44. {
  45. curve[evalIndex].value = prop.objectReferenceValue;
  46. }
  47. // check the next key (always return the minimum value)
  48. else if (evalIndex < curve.Length - 1 && KeyCompare(curve[evalIndex + 1].time, (float)time, clip.frameRate) == 0)
  49. {
  50. curve[evalIndex + 1].value = prop.objectReferenceValue;
  51. }
  52. // resize the array
  53. else
  54. {
  55. if (time > curve[0].time)
  56. evalIndex++;
  57. var key = new ObjectReferenceKeyframe();
  58. key.time = (float)time;
  59. key.value = prop.objectReferenceValue;
  60. ArrayUtility.Insert(ref curve, evalIndex, key);
  61. }
  62. }
  63. else // curve doesn't exist, add it
  64. {
  65. curve = new ObjectReferenceKeyframe[1];
  66. curve[0].time = (float)time;
  67. curve[0].value = prop.objectReferenceValue;
  68. }
  69. AnimationUtility.SetObjectReferenceCurve(clip, sourceBinding, curve);
  70. EditorUtility.SetDirty(clip);
  71. }
  72. static void AddRotationKey(AnimationClip clip, EditorCurveBinding sourceBind, SerializedProperty prop, double time)
  73. {
  74. if (prop.propertyType != SerializedPropertyType.Quaternion)
  75. {
  76. return;
  77. }
  78. var updateCurves = new List<AnimationCurve>();
  79. var updateBindings = new List<EditorCurveBinding>();
  80. var info = AnimationClipCurveCache.Instance.GetCurveInfo(clip);
  81. for (var i = 0; i < info.bindings.Length; i++)
  82. {
  83. if (sourceBind.type != info.bindings[i].type)
  84. continue;
  85. if (info.bindings[i].propertyName.Contains("localEuler"))
  86. {
  87. updateBindings.Add(info.bindings[i]);
  88. updateCurves.Add(info.curves[i]);
  89. }
  90. }
  91. // use this instead of serialized properties because the editor will attempt to maintain
  92. // correct localeulers
  93. var eulers = ((Transform)prop.serializedObject.targetObject).localEulerAngles;
  94. if (updateBindings.Count == 0)
  95. {
  96. var propName = AnimationWindowUtility.GetPropertyGroupName(sourceBind.propertyName);
  97. updateBindings.Add(EditorCurveBinding.FloatCurve(sourceBind.path, sourceBind.type, propName + ".x"));
  98. updateBindings.Add(EditorCurveBinding.FloatCurve(sourceBind.path, sourceBind.type, propName + ".y"));
  99. updateBindings.Add(EditorCurveBinding.FloatCurve(sourceBind.path, sourceBind.type, propName + ".z"));
  100. var curveX = new AnimationCurve();
  101. var curveY = new AnimationCurve();
  102. var curveZ = new AnimationCurve();
  103. AddKeyFrameToCurve(curveX, (float)time, clip.frameRate, eulers.x, false);
  104. AddKeyFrameToCurve(curveY, (float)time, clip.frameRate, eulers.y, false);
  105. AddKeyFrameToCurve(curveZ, (float)time, clip.frameRate, eulers.z, false);
  106. updateCurves.Add(curveX);
  107. updateCurves.Add(curveY);
  108. updateCurves.Add(curveZ);
  109. }
  110. for (var i = 0; i < updateBindings.Count; i++)
  111. {
  112. var c = updateBindings[i].propertyName.Last();
  113. var value = eulers.x;
  114. if (c == 'y') value = eulers.y;
  115. else if (c == 'z') value = eulers.z;
  116. AddKeyFrameToCurve(updateCurves[i], (float)time, clip.frameRate, value, false);
  117. }
  118. UpdateEditorCurves(clip, updateBindings, updateCurves);
  119. }
  120. // Add a floating point curve key
  121. static void AddFloatKey(AnimationClip clip, EditorCurveBinding sourceBind, SerializedProperty prop, double time)
  122. {
  123. var updateCurves = new List<AnimationCurve>();
  124. var updateBindings = new List<EditorCurveBinding>();
  125. var updated = false;
  126. var info = AnimationClipCurveCache.Instance.GetCurveInfo(clip);
  127. for (var i = 0; i < info.bindings.Length; i++)
  128. {
  129. var binding = info.bindings[i];
  130. if (binding.type != sourceBind.type)
  131. continue;
  132. SerializedProperty valProp = null;
  133. var curve = info.curves[i];
  134. // perfect match on property path, editting a float
  135. if (prop.propertyPath.Equals(binding.propertyName))
  136. {
  137. valProp = prop;
  138. }
  139. // this is a child object
  140. else if (binding.propertyName.Contains(prop.propertyPath))
  141. {
  142. valProp = prop.serializedObject.FindProperty(binding.propertyName);
  143. }
  144. if (valProp != null)
  145. {
  146. var value = GetKeyValue(valProp);
  147. if (!float.IsNaN(value)) // Nan indicates an error retrieving the property value
  148. {
  149. updated = true;
  150. AddKeyFrameToCurve(curve, (float)time, clip.frameRate, value, valProp.propertyType == SerializedPropertyType.Boolean);
  151. updateCurves.Add(curve);
  152. updateBindings.Add(binding);
  153. }
  154. }
  155. }
  156. // Curves don't exist, add them
  157. if (!updated)
  158. {
  159. var propName = AnimationWindowUtility.GetPropertyGroupName(sourceBind.propertyName);
  160. if (!prop.hasChildren)
  161. {
  162. var value = GetKeyValue(prop);
  163. if (!float.IsNaN(value))
  164. {
  165. updateBindings.Add(EditorCurveBinding.FloatCurve(sourceBind.path, sourceBind.type, sourceBind.propertyName));
  166. var curve = new AnimationCurve();
  167. AddKeyFrameToCurve(curve, (float)time, clip.frameRate, value, prop.propertyType == SerializedPropertyType.Boolean);
  168. updateCurves.Add(curve);
  169. }
  170. }
  171. else
  172. {
  173. // special case because subproperties on color aren't 'visible' so you can't iterate over them
  174. if (prop.propertyType == SerializedPropertyType.Color)
  175. {
  176. updateBindings.Add(EditorCurveBinding.FloatCurve(sourceBind.path, sourceBind.type, propName + ".r"));
  177. updateBindings.Add(EditorCurveBinding.FloatCurve(sourceBind.path, sourceBind.type, propName + ".g"));
  178. updateBindings.Add(EditorCurveBinding.FloatCurve(sourceBind.path, sourceBind.type, propName + ".b"));
  179. updateBindings.Add(EditorCurveBinding.FloatCurve(sourceBind.path, sourceBind.type, propName + ".a"));
  180. var c = prop.colorValue;
  181. for (var i = 0; i < 4; i++)
  182. {
  183. var curve = new AnimationCurve();
  184. AddKeyFrameToCurve(curve, (float)time, clip.frameRate, c[i], prop.propertyType == SerializedPropertyType.Boolean);
  185. updateCurves.Add(curve);
  186. }
  187. }
  188. else
  189. {
  190. prop = prop.Copy();
  191. foreach (SerializedProperty cp in prop)
  192. {
  193. updateBindings.Add(EditorCurveBinding.FloatCurve(sourceBind.path, sourceBind.type, cp.propertyPath));
  194. var curve = new AnimationCurve();
  195. AddKeyFrameToCurve(curve, (float)time, clip.frameRate, GetKeyValue(cp), cp.propertyType == SerializedPropertyType.Boolean);
  196. updateCurves.Add(curve);
  197. }
  198. }
  199. }
  200. }
  201. UpdateEditorCurves(clip, updateBindings, updateCurves);
  202. }
  203. public static void RemoveKey(AnimationClip clip, EditorCurveBinding sourceBinding, SerializedProperty prop, double time)
  204. {
  205. if (sourceBinding.isPPtrCurve)
  206. {
  207. RemoveObjectKey(clip, sourceBinding, time);
  208. }
  209. else if (IsRotationKey(sourceBinding))
  210. {
  211. RemoveRotationKey(clip, sourceBinding, prop, time);
  212. }
  213. else
  214. {
  215. RemoveFloatKey(clip, sourceBinding, prop, time);
  216. }
  217. }
  218. public static void RemoveObjectKey(AnimationClip clip, EditorCurveBinding sourceBinding, double time)
  219. {
  220. var info = AnimationClipCurveCache.Instance.GetCurveInfo(clip);
  221. var curveIndex = Array.IndexOf(info.objectBindings, sourceBinding);
  222. if (curveIndex >= 0)
  223. {
  224. var curve = info.objectCurves[curveIndex];
  225. var evalIndex = GetKeyframeAtTime(curve, (float)time, clip.frameRate);
  226. if (evalIndex >= 0)
  227. {
  228. ArrayUtility.RemoveAt(ref curve, evalIndex);
  229. AnimationUtility.SetObjectReferenceCurve(clip, sourceBinding, curve.Length == 0 ? null : curve);
  230. EditorUtility.SetDirty(clip);
  231. }
  232. }
  233. }
  234. public static int GetObjectKeyCount(AnimationClip clip, EditorCurveBinding sourceBinding)
  235. {
  236. var info = AnimationClipCurveCache.Instance.GetCurveInfo(clip);
  237. var curveIndex = Array.IndexOf(info.objectBindings, sourceBinding);
  238. if (curveIndex >= 0)
  239. {
  240. var curve = info.objectCurves[curveIndex];
  241. return curve.Length;
  242. }
  243. return 0;
  244. }
  245. static void RemoveRotationKey(AnimationClip clip, EditorCurveBinding sourceBind, SerializedProperty prop, double time)
  246. {
  247. if (prop.propertyType != SerializedPropertyType.Quaternion)
  248. {
  249. return;
  250. }
  251. var updateCurves = new List<AnimationCurve>();
  252. var updateBindings = new List<EditorCurveBinding>();
  253. var info = AnimationClipCurveCache.Instance.GetCurveInfo(clip);
  254. for (var i = 0; i < info.bindings.Length; i++)
  255. {
  256. if (sourceBind.type != info.bindings[i].type)
  257. continue;
  258. if (info.bindings[i].propertyName.Contains("localEuler"))
  259. {
  260. updateBindings.Add(info.bindings[i]);
  261. updateCurves.Add(info.curves[i]);
  262. }
  263. }
  264. foreach (var c in updateCurves)
  265. {
  266. RemoveKeyFrameFromCurve(c, (float)time, clip.frameRate);
  267. }
  268. UpdateEditorCurves(clip, updateBindings, updateCurves);
  269. }
  270. // Removes the float keys from curves
  271. static void RemoveFloatKey(AnimationClip clip, EditorCurveBinding sourceBind, SerializedProperty prop, double time)
  272. {
  273. var updateCurves = new List<AnimationCurve>();
  274. var updateBindings = new List<EditorCurveBinding>();
  275. var info = AnimationClipCurveCache.Instance.GetCurveInfo(clip);
  276. for (var i = 0; i < info.bindings.Length; i++)
  277. {
  278. var binding = info.bindings[i];
  279. if (binding.type != sourceBind.type)
  280. continue;
  281. SerializedProperty valProp = null;
  282. var curve = info.curves[i];
  283. // perfect match on property path, editting a float
  284. if (prop.propertyPath.Equals(binding.propertyName))
  285. {
  286. valProp = prop;
  287. }
  288. // this is a child object
  289. else if (binding.propertyName.Contains(prop.propertyPath))
  290. {
  291. valProp = prop.serializedObject.FindProperty(binding.propertyName);
  292. }
  293. if (valProp != null)
  294. {
  295. RemoveKeyFrameFromCurve(curve, (float)time, clip.frameRate);
  296. updateCurves.Add(curve);
  297. updateBindings.Add(binding);
  298. }
  299. }
  300. // update the curve. Do this last to not mess with the curve caches we are iterating over
  301. UpdateEditorCurves(clip, updateBindings, updateCurves);
  302. }
  303. static void UpdateEditorCurve(AnimationClip clip, EditorCurveBinding binding, AnimationCurve curve)
  304. {
  305. if (curve.keys.Length == 0)
  306. AnimationUtility.SetEditorCurve(clip, binding, null);
  307. else
  308. AnimationUtility.SetEditorCurve(clip, binding, curve);
  309. }
  310. static void UpdateEditorCurves(AnimationClip clip, List<EditorCurveBinding> bindings, List<AnimationCurve> curves)
  311. {
  312. if (curves.Count == 0)
  313. return;
  314. for (var i = 0; i < curves.Count; i++)
  315. {
  316. UpdateEditorCurve(clip, bindings[i], curves[i]);
  317. }
  318. EditorUtility.SetDirty(clip);
  319. }
  320. public static void RemoveCurves(AnimationClip clip, SerializedProperty prop)
  321. {
  322. if (clip == null || prop == null)
  323. return;
  324. var toRemove = new List<EditorCurveBinding>();
  325. var info = AnimationClipCurveCache.Instance.GetCurveInfo(clip);
  326. for (var i = 0; i < info.bindings.Length; i++)
  327. {
  328. var binding = info.bindings[i];
  329. // check if we match directly, or with a child object
  330. if (prop.propertyPath.Equals(binding.propertyName) || binding.propertyName.Contains(prop.propertyPath))
  331. {
  332. toRemove.Add(binding);
  333. }
  334. }
  335. for (int i = 0; i < toRemove.Count; i++)
  336. {
  337. AnimationUtility.SetEditorCurve(clip, toRemove[i], null);
  338. }
  339. }
  340. // adds a stepped key frame to the given curve
  341. public static void AddKeyFrameToCurve(AnimationCurve curve, float time, float framerate, float value, bool stepped)
  342. {
  343. var key = new Keyframe();
  344. bool add = true;
  345. var keyIndex = GetKeyframeAtTime(curve, time, framerate);
  346. if (keyIndex != -1)
  347. {
  348. add = false;
  349. key = curve[keyIndex]; // retain the tangents and mode
  350. curve.RemoveKey(keyIndex);
  351. }
  352. key.value = value;
  353. key.time = GetKeyTime(time, framerate);
  354. keyIndex = curve.AddKey(key);
  355. if (stepped)
  356. {
  357. AnimationUtility.SetKeyBroken(curve, keyIndex, stepped);
  358. AnimationUtility.SetKeyLeftTangentMode(curve, keyIndex, AnimationUtility.TangentMode.Constant);
  359. AnimationUtility.SetKeyRightTangentMode(curve, keyIndex, AnimationUtility.TangentMode.Constant);
  360. key.outTangent = Mathf.Infinity;
  361. key.inTangent = Mathf.Infinity;
  362. }
  363. else if (add)
  364. {
  365. AnimationUtility.SetKeyLeftTangentMode(curve, keyIndex, AnimationUtility.TangentMode.ClampedAuto);
  366. AnimationUtility.SetKeyRightTangentMode(curve, keyIndex, AnimationUtility.TangentMode.ClampedAuto);
  367. }
  368. if (keyIndex != -1 && !stepped)
  369. {
  370. AnimationUtility.UpdateTangentsFromModeSurrounding(curve, keyIndex);
  371. AnimationUtility.SetKeyBroken(curve, keyIndex, false);
  372. }
  373. }
  374. // Removes a keyframe at the given time from the animation curve
  375. public static bool RemoveKeyFrameFromCurve(AnimationCurve curve, float time, float framerate)
  376. {
  377. var keyIndex = GetKeyframeAtTime(curve, time, framerate);
  378. if (keyIndex == -1)
  379. return false;
  380. curve.RemoveKey(keyIndex);
  381. return true;
  382. }
  383. // gets the value of the key
  384. public static float GetKeyValue(SerializedProperty prop)
  385. {
  386. switch (prop.propertyType)
  387. {
  388. case SerializedPropertyType.Integer:
  389. return prop.intValue;
  390. case SerializedPropertyType.Boolean:
  391. return prop.boolValue ? 1.0f : 0.0f;
  392. case SerializedPropertyType.Float:
  393. return prop.floatValue;
  394. default:
  395. Debug.LogError("Could not convert property type " + prop.propertyType.ToString() + " to float");
  396. break;
  397. }
  398. return float.NaN;
  399. }
  400. public static void SetFromKeyValue(SerializedProperty prop, float keyValue)
  401. {
  402. switch (prop.propertyType)
  403. {
  404. case SerializedPropertyType.Float:
  405. {
  406. prop.floatValue = keyValue;
  407. return;
  408. }
  409. case SerializedPropertyType.Integer:
  410. {
  411. prop.intValue = (int)keyValue;
  412. return;
  413. }
  414. case SerializedPropertyType.Boolean:
  415. {
  416. prop.boolValue = Math.Abs(keyValue) > 0.001f;
  417. return;
  418. }
  419. }
  420. Debug.LogError("Could not convert float to property type " + prop.propertyType.ToString());
  421. }
  422. // gets the index of the key, -1 if not found
  423. public static int GetKeyframeAtTime(AnimationCurve curve, float time, float frameRate)
  424. {
  425. var range = 0.5f / frameRate;
  426. var keys = curve.keys;
  427. for (var i = 0; i < keys.Length; i++)
  428. {
  429. var k = keys[i];
  430. if (k.time >= time - range && k.time < time + range)
  431. {
  432. return i;
  433. }
  434. }
  435. return -1;
  436. }
  437. public static int GetKeyframeAtTime(ObjectReferenceKeyframe[] curve, float time, float frameRate)
  438. {
  439. if (curve == null || curve.Length == 0)
  440. return -1;
  441. var range = 0.5f / frameRate;
  442. for (var i = 0; i < curve.Length; i++)
  443. {
  444. var t = curve[i].time;
  445. if (t >= time - range && t < time + range)
  446. {
  447. return i;
  448. }
  449. }
  450. return -1;
  451. }
  452. public static float GetKeyTime(float time, float frameRate)
  453. {
  454. return Mathf.Round(time * frameRate) / frameRate;
  455. }
  456. public static int KeyCompare(float timeA, float timeB, float frameRate)
  457. {
  458. if (Mathf.Abs(timeA - timeB) <= 0.5f / frameRate)
  459. return 0;
  460. return timeA < timeB ? -1 : 1;
  461. }
  462. // Evaluates an object (bool curve)
  463. public static Object Evaluate(ObjectReferenceKeyframe[] curve, float time)
  464. {
  465. return curve[EvaluateIndex(curve, time)].value;
  466. }
  467. // returns the index from evaluation
  468. public static int EvaluateIndex(ObjectReferenceKeyframe[] curve, float time)
  469. {
  470. if (curve == null || curve.Length == 0)
  471. throw new InvalidOperationException("Can not evaluate a PPtr curve with no entries");
  472. // clamp conditions
  473. if (time <= curve[0].time)
  474. return 0;
  475. if (time >= curve.Last().time)
  476. return curve.Length - 1;
  477. // binary search
  478. var max = curve.Length - 1;
  479. var min = 0;
  480. while (max - min > 1)
  481. {
  482. var imid = (min + max) / 2;
  483. if (Mathf.Approximately(curve[imid].time, time))
  484. return imid;
  485. if (curve[imid].time < time)
  486. min = imid;
  487. else if (curve[imid].time > time)
  488. max = imid;
  489. }
  490. return min;
  491. }
  492. // Shifts the animation clip so the time start at 0
  493. public static void ShiftBySeconds(this AnimationClip clip, float time)
  494. {
  495. var floatBindings = AnimationUtility.GetCurveBindings(clip);
  496. var objectBindings = AnimationUtility.GetObjectReferenceCurveBindings(clip);
  497. // update the float curves
  498. foreach (var bind in floatBindings)
  499. {
  500. var curve = AnimationUtility.GetEditorCurve(clip, bind);
  501. var keys = curve.keys;
  502. for (var i = 0; i < keys.Length; i++)
  503. keys[i].time += time;
  504. curve.keys = keys;
  505. AnimationUtility.SetEditorCurve(clip, bind, curve);
  506. }
  507. // update the PPtr curves
  508. foreach (var bind in objectBindings)
  509. {
  510. var curve = AnimationUtility.GetObjectReferenceCurve(clip, bind);
  511. for (var i = 0; i < curve.Length; i++)
  512. curve[i].time += time;
  513. AnimationUtility.SetObjectReferenceCurve(clip, bind, curve);
  514. }
  515. EditorUtility.SetDirty(clip);
  516. }
  517. public static void ScaleTime(this AnimationClip clip, float scale)
  518. {
  519. var floatBindings = AnimationUtility.GetCurveBindings(clip);
  520. var objectBindings = AnimationUtility.GetObjectReferenceCurveBindings(clip);
  521. // update the float curves
  522. foreach (var bind in floatBindings)
  523. {
  524. var curve = AnimationUtility.GetEditorCurve(clip, bind);
  525. var keys = curve.keys;
  526. for (var i = 0; i < keys.Length; i++)
  527. keys[i].time *= scale;
  528. curve.keys = keys.OrderBy(x => x.time).ToArray();
  529. AnimationUtility.SetEditorCurve(clip, bind, curve);
  530. }
  531. // update the PPtr curves
  532. foreach (var bind in objectBindings)
  533. {
  534. var curve = AnimationUtility.GetObjectReferenceCurve(clip, bind);
  535. for (var i = 0; i < curve.Length; i++)
  536. curve[i].time *= scale;
  537. curve = curve.OrderBy(x => x.time).ToArray();
  538. AnimationUtility.SetObjectReferenceCurve(clip, bind, curve);
  539. }
  540. EditorUtility.SetDirty(clip);
  541. }
  542. // Creates an opposing blend curve that matches the given curve to make sure the result is normalized
  543. public static AnimationCurve CreateMatchingCurve(AnimationCurve curve)
  544. {
  545. Keyframe[] keys = curve.keys;
  546. for (var i = 0; i != keys.Length; i++)
  547. {
  548. if (!Single.IsPositiveInfinity(keys[i].inTangent))
  549. keys[i].inTangent = -keys[i].inTangent;
  550. if (!Single.IsPositiveInfinity(keys[i].outTangent))
  551. keys[i].outTangent = -keys[i].outTangent;
  552. keys[i].value = 1.0f - keys[i].value;
  553. }
  554. return new AnimationCurve(keys);
  555. }
  556. // Sanitizes the keys on an animation to force the property to be normalized
  557. public static Keyframe[] SanitizeCurveKeys(Keyframe[] keys, bool easeIn)
  558. {
  559. if (keys.Length < 2)
  560. {
  561. if (easeIn)
  562. keys = new[] { new Keyframe(0, 0), new Keyframe(1, 1) };
  563. else
  564. keys = new[] { new Keyframe(0, 1), new Keyframe(1, 0) };
  565. }
  566. else if (easeIn)
  567. {
  568. keys[0].time = 0;
  569. keys[keys.Length - 1].time = 1;
  570. keys[keys.Length - 1].value = 1;
  571. }
  572. else
  573. {
  574. keys[0].time = 0;
  575. keys[0].value = 1;
  576. keys[keys.Length - 1].time = 1;
  577. }
  578. return keys;
  579. }
  580. }
  581. }