GenericBaker.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. using UnityEngine;
  2. using System.Collections;
  3. using System;
  4. namespace RootMotion
  5. {
  6. /// <summary>
  7. /// Baker for Generic/Legacy animation.
  8. /// </summary>
  9. public class GenericBaker : Baker
  10. {
  11. /// <summary>
  12. /// If true, produced AnimationClips will be marked as Legacy and usable with the Legacy animation system.
  13. /// </summary>
  14. [Tooltip("If true, produced AnimationClips will be marked as Legacy and usable with the Legacy animation system.")]
  15. public bool markAsLegacy;
  16. /// <summary>
  17. /// Root Transform of the hierarchy to bake.
  18. /// </summary>
  19. [Tooltip("Root Transform of the hierarchy to bake.")]
  20. public Transform root;
  21. /// <summary>
  22. /// Root Node used for root motion.
  23. /// </summary>
  24. [Tooltip("Root Node used for root motion.")]
  25. public Transform rootNode;
  26. /// <summary>
  27. /// List of Transforms to ignore, rotation curves will not be baked for these Transforms.
  28. /// </summary>
  29. [Tooltip("List of Transforms to ignore, rotation curves will not be baked for these Transforms.")]
  30. public Transform[] ignoreList;
  31. /// <summary>
  32. /// LocalPosition curves will be baked for these Transforms only. If you are baking a character, the pelvis bone should be added to this array.
  33. /// </summary>
  34. [Tooltip("LocalPosition curves will be baked for these Transforms only. If you are baking a character, the pelvis bone should be added to this array.")]
  35. public Transform[] bakePositionList;
  36. private BakerTransform[] children = new BakerTransform[0];
  37. private BakerTransform rootChild;
  38. private int rootChildIndex = -1;
  39. void Awake()
  40. {
  41. // Find all the child Transforms of the Animator
  42. Transform[] childrenAndRoot = (Transform[])root.GetComponentsInChildren<Transform>();
  43. children = new BakerTransform[0];
  44. // Exlude the ignore list, construct the children array
  45. for (int i = 0; i < childrenAndRoot.Length; i++)
  46. {
  47. if (!IsIgnored(childrenAndRoot[i]))
  48. {
  49. Array.Resize(ref children, children.Length + 1);
  50. bool isRootNode = childrenAndRoot[i] == rootNode;
  51. if (isRootNode) rootChildIndex = children.Length - 1;
  52. children[children.Length - 1] = new BakerTransform(childrenAndRoot[i], root, BakePosition(childrenAndRoot[i]), isRootNode);
  53. }
  54. }
  55. }
  56. protected override Transform GetCharacterRoot()
  57. {
  58. return root;
  59. }
  60. protected override void OnStartBaking()
  61. {
  62. for (int i = 0; i < children.Length; i++)
  63. {
  64. children[i].Reset();
  65. if (i == rootChildIndex) children[i].SetRelativeSpace(root.position, root.rotation);
  66. }
  67. }
  68. protected override void OnSetLoopFrame(float time)
  69. {
  70. // TODO Change to SetLoopFrame like in HumanoidBaker
  71. for (int i = 0; i < children.Length; i++) children[i].AddLoopFrame(time);
  72. }
  73. protected override void OnSetCurves(ref AnimationClip clip)
  74. {
  75. // TODO Length Multiplier
  76. for (int i = 0; i < children.Length; i++) children[i].SetCurves(ref clip);
  77. }
  78. protected override void OnSetKeyframes(float time, bool lastFrame)
  79. {
  80. for (int i = 0; i < children.Length; i++) children[i].SetKeyframes(time);
  81. }
  82. // Is the specified Transform in the ignore list?
  83. private bool IsIgnored(Transform t)
  84. {
  85. for (int i = 0; i < ignoreList.Length; i++)
  86. {
  87. if (t == ignoreList[i]) return true;
  88. }
  89. return false;
  90. }
  91. // Should we record the localPosition channels of the Transform?
  92. private bool BakePosition(Transform t)
  93. {
  94. for (int i = 0; i < bakePositionList.Length; i++)
  95. {
  96. if (t == bakePositionList[i]) return true;
  97. }
  98. return false;
  99. }
  100. #if UNITY_EDITOR
  101. protected override void SetClipSettings(AnimationClip clip, UnityEditor.AnimationClipSettings settings)
  102. {
  103. clip.legacy = markAsLegacy;
  104. if (mode != Baker.Mode.AnimationClips)
  105. {
  106. clip.wrapMode = loop ? WrapMode.Loop : WrapMode.Default;
  107. }
  108. }
  109. #endif
  110. }
  111. }