LinearMixerTransitionAsset.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. // Animancer // https://kybernetik.com.au/animancer // Copyright 2022 Kybernetik //
  2. using System;
  3. using UnityEngine;
  4. using Object = UnityEngine.Object;
  5. namespace Animancer
  6. {
  7. /// <inheritdoc/>
  8. /// https://kybernetik.com.au/animancer/api/Animancer/LinearMixerTransitionAsset
  9. [CreateAssetMenu(menuName = Strings.MenuPrefix + "Mixer Transition/Linear", order = Strings.AssetMenuOrder + 3)]
  10. [HelpURL(Strings.DocsURLs.APIDocumentation + "/" + nameof(LinearMixerTransitionAsset))]
  11. public class LinearMixerTransitionAsset : AnimancerTransitionAsset<LinearMixerTransition>
  12. {
  13. /// <inheritdoc/>
  14. [Serializable]
  15. public new class UnShared :
  16. UnShared<LinearMixerTransitionAsset, LinearMixerTransition, LinearMixerState>,
  17. LinearMixerState.ITransition
  18. { }
  19. }
  20. /// <inheritdoc/>
  21. /// https://kybernetik.com.au/animancer/api/Animancer/LinearMixerTransition
  22. [Serializable]
  23. public class LinearMixerTransition : MixerTransition<LinearMixerState, float>,
  24. LinearMixerState.ITransition, ICopyable<LinearMixerTransition>
  25. {
  26. /************************************************************************************************************************/
  27. [SerializeField]
  28. [Tooltip("Should setting the Parameter above the highest threshold increase the Speed of the mixer proportionally?")]
  29. private bool _ExtrapolateSpeed = true;
  30. /// <summary>[<see cref="SerializeField"/>]
  31. /// Should setting the <see cref="MixerState{TParameter}.Parameter"/> above the highest threshold increase the
  32. /// <see cref="AnimancerNode.Speed"/> of the mixer proportionally?
  33. /// </summary>
  34. public ref bool ExtrapolateSpeed => ref _ExtrapolateSpeed;
  35. /************************************************************************************************************************/
  36. /// <summary>
  37. /// Are all <see cref="ManualMixerTransition{TMixer}.Animations"/> assigned and
  38. /// <see cref="MixerTransition{TMixer, TParameter}.Thresholds"/> unique and sorted in ascending order?
  39. /// </summary>
  40. public override bool IsValid
  41. {
  42. get
  43. {
  44. if (!base.IsValid)
  45. return false;
  46. var previous = float.NegativeInfinity;
  47. var thresholds = Thresholds;
  48. for (int i = 0; i < thresholds.Length; i++)
  49. {
  50. var threshold = thresholds[i];
  51. if (threshold < previous)
  52. return false;
  53. else
  54. previous = threshold;
  55. }
  56. return true;
  57. }
  58. }
  59. /************************************************************************************************************************/
  60. /// <inheritdoc/>
  61. public override LinearMixerState CreateState()
  62. {
  63. State = new LinearMixerState();
  64. InitializeState();
  65. return State;
  66. }
  67. /************************************************************************************************************************/
  68. /// <inheritdoc/>
  69. public override void Apply(AnimancerState state)
  70. {
  71. State.ExtrapolateSpeed = _ExtrapolateSpeed;
  72. base.Apply(state);
  73. }
  74. /************************************************************************************************************************/
  75. /// <summary>Sorts all states so that their thresholds go from lowest to highest.</summary>
  76. /// <remarks>This method uses Bubble Sort which is inefficient for large numbers of states.</remarks>
  77. public void SortByThresholds()
  78. {
  79. var thresholdCount = Thresholds.Length;
  80. if (thresholdCount <= 1)
  81. return;
  82. var speedCount = Speeds.Length;
  83. var syncCount = SynchronizeChildren.Length;
  84. var previousThreshold = Thresholds[0];
  85. for (int i = 1; i < thresholdCount; i++)
  86. {
  87. var threshold = Thresholds[i];
  88. if (threshold >= previousThreshold)
  89. {
  90. previousThreshold = threshold;
  91. continue;
  92. }
  93. Thresholds.Swap(i, i - 1);
  94. Animations.Swap(i, i - 1);
  95. if (i < speedCount)
  96. Speeds.Swap(i, i - 1);
  97. if (i == syncCount && !SynchronizeChildren[i - 1])
  98. {
  99. var sync = SynchronizeChildren;
  100. Array.Resize(ref sync, ++syncCount);
  101. sync[i - 1] = true;
  102. sync[i] = false;
  103. SynchronizeChildren = sync;
  104. }
  105. else if (i < syncCount)
  106. {
  107. SynchronizeChildren.Swap(i, i - 1);
  108. }
  109. if (i == 1)
  110. {
  111. i = 0;
  112. previousThreshold = float.NegativeInfinity;
  113. }
  114. else
  115. {
  116. i -= 2;
  117. previousThreshold = Thresholds[i];
  118. }
  119. }
  120. }
  121. /************************************************************************************************************************/
  122. /// <inheritdoc/>
  123. public virtual void CopyFrom(LinearMixerTransition copyFrom)
  124. {
  125. CopyFrom((MixerTransition<LinearMixerState, float>)copyFrom);
  126. if (copyFrom == null)
  127. {
  128. _ExtrapolateSpeed = true;
  129. return;
  130. }
  131. _ExtrapolateSpeed = copyFrom._ExtrapolateSpeed;
  132. }
  133. /************************************************************************************************************************/
  134. #region Drawer
  135. #if UNITY_EDITOR
  136. /************************************************************************************************************************/
  137. /// <inheritdoc/>
  138. [UnityEditor.CustomPropertyDrawer(typeof(LinearMixerTransition), true)]
  139. public class Drawer : MixerTransitionDrawer
  140. {
  141. /************************************************************************************************************************/
  142. private static GUIContent _SortingErrorContent;
  143. private static GUIStyle _SortingErrorStyle;
  144. /// <inheritdoc/>
  145. protected override void DoThresholdGUI(Rect area, int index)
  146. {
  147. var color = GUI.color;
  148. if (index > 0)
  149. {
  150. var previousThreshold = CurrentThresholds.GetArrayElementAtIndex(index - 1);
  151. var currentThreshold = CurrentThresholds.GetArrayElementAtIndex(index);
  152. if (previousThreshold.floatValue >= currentThreshold.floatValue)
  153. {
  154. if (_SortingErrorContent == null)
  155. {
  156. _SortingErrorContent = new GUIContent(Editor.AnimancerGUI.LoadIcon("console.erroricon.sml"))
  157. {
  158. tooltip = "Linear Mixer Thresholds must always be unique and sorted in ascending order (click to sort)"
  159. };
  160. }
  161. if (_SortingErrorStyle == null)
  162. _SortingErrorStyle = new GUIStyle(GUI.skin.label)
  163. {
  164. padding = new RectOffset(),
  165. };
  166. var iconArea = Editor.AnimancerGUI.StealFromRight(ref area, area.height, Editor.AnimancerGUI.StandardSpacing);
  167. if (GUI.Button(iconArea, _SortingErrorContent, _SortingErrorStyle))
  168. {
  169. Editor.Serialization.RecordUndo(Context.Property);
  170. ((LinearMixerTransition)Context.Transition).SortByThresholds();
  171. }
  172. GUI.color = Editor.AnimancerGUI.ErrorFieldColor;
  173. }
  174. }
  175. base.DoThresholdGUI(area, index);
  176. GUI.color = color;
  177. }
  178. /************************************************************************************************************************/
  179. /// <inheritdoc/>
  180. protected override void AddThresholdFunctionsToMenu(UnityEditor.GenericMenu menu)
  181. {
  182. const string EvenlySpaced = "Evenly Spaced";
  183. var count = CurrentThresholds.arraySize;
  184. if (count <= 1)
  185. {
  186. menu.AddDisabledItem(new GUIContent(EvenlySpaced));
  187. }
  188. else
  189. {
  190. var first = CurrentThresholds.GetArrayElementAtIndex(0).floatValue;
  191. var last = CurrentThresholds.GetArrayElementAtIndex(count - 1).floatValue;
  192. if (last == first)
  193. last++;
  194. AddPropertyModifierFunction(menu, $"{EvenlySpaced} ({first} to {last})", (_) =>
  195. {
  196. for (int i = 0; i < count; i++)
  197. {
  198. CurrentThresholds.GetArrayElementAtIndex(i).floatValue = Mathf.Lerp(first, last, i / (float)(count - 1));
  199. }
  200. });
  201. }
  202. AddCalculateThresholdsFunction(menu, "From Speed",
  203. (state, threshold) => AnimancerUtilities.TryGetAverageVelocity(state, out var velocity) ? velocity.magnitude : float.NaN);
  204. AddCalculateThresholdsFunction(menu, "From Velocity X",
  205. (state, threshold) => AnimancerUtilities.TryGetAverageVelocity(state, out var velocity) ? velocity.x : float.NaN);
  206. AddCalculateThresholdsFunction(menu, "From Velocity Y",
  207. (state, threshold) => AnimancerUtilities.TryGetAverageVelocity(state, out var velocity) ? velocity.y : float.NaN);
  208. AddCalculateThresholdsFunction(menu, "From Velocity Z",
  209. (state, threshold) => AnimancerUtilities.TryGetAverageVelocity(state, out var velocity) ? velocity.z : float.NaN);
  210. AddCalculateThresholdsFunction(menu, "From Angular Speed (Rad)",
  211. (state, threshold) => AnimancerUtilities.TryGetAverageAngularSpeed(state, out var speed) ? speed : float.NaN);
  212. AddCalculateThresholdsFunction(menu, "From Angular Speed (Deg)",
  213. (state, threshold) => AnimancerUtilities.TryGetAverageAngularSpeed(state, out var speed) ? speed * Mathf.Rad2Deg : float.NaN);
  214. }
  215. /************************************************************************************************************************/
  216. private void AddCalculateThresholdsFunction(UnityEditor.GenericMenu menu, string label,
  217. Func<Object, float, float> calculateThreshold)
  218. {
  219. AddPropertyModifierFunction(menu, label, (property) =>
  220. {
  221. var count = CurrentAnimations.arraySize;
  222. for (int i = 0; i < count; i++)
  223. {
  224. var state = CurrentAnimations.GetArrayElementAtIndex(i).objectReferenceValue;
  225. if (state == null)
  226. continue;
  227. var threshold = CurrentThresholds.GetArrayElementAtIndex(i);
  228. var value = calculateThreshold(state, threshold.floatValue);
  229. if (!float.IsNaN(value))
  230. threshold.floatValue = value;
  231. }
  232. });
  233. }
  234. /************************************************************************************************************************/
  235. }
  236. /************************************************************************************************************************/
  237. #endif
  238. #endregion
  239. /************************************************************************************************************************/
  240. }
  241. }