KeyTraverser.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using UnityEngine;
  5. using UnityEngine.Timeline;
  6. namespace UnityEditor.Timeline.Utilities
  7. {
  8. class KeyTraverser
  9. {
  10. float[] m_KeyCache;
  11. int m_DirtyStamp = -1;
  12. int m_LastHash = -1;
  13. readonly TimelineAsset m_Asset;
  14. readonly float m_Epsilon;
  15. int m_LastIndex = -1;
  16. public int lastIndex
  17. {
  18. get { return m_LastIndex; }
  19. }
  20. public static IEnumerable<float> GetClipKeyTimes(TimelineClip clip)
  21. {
  22. if (clip == null || clip.animationClip == null || clip.animationClip.empty)
  23. return new float[0];
  24. return AnimationClipCurveCache.Instance.GetCurveInfo(clip.animationClip).keyTimes.
  25. Select(k => (float)clip.FromLocalTimeUnbound(k)). // convert to sequence time
  26. Where(k => k >= clip.start && k <= clip.end); // remove non visible keys
  27. }
  28. public static IEnumerable<float> GetTrackKeyTimes(AnimationTrack track)
  29. {
  30. if (track != null)
  31. {
  32. if (track.inClipMode)
  33. return track.clips.Where(c => c.recordable).
  34. SelectMany(x => GetClipKeyTimes(x));
  35. if (track.infiniteClip != null && !track.infiniteClip.empty)
  36. return AnimationClipCurveCache.Instance.GetCurveInfo(track.infiniteClip).keyTimes;
  37. }
  38. return new float[0];
  39. }
  40. static int CalcAnimClipHash(TrackAsset asset)
  41. {
  42. int hash = 0;
  43. if (asset != null)
  44. {
  45. AnimationTrack animTrack = asset as AnimationTrack;
  46. if (animTrack != null)
  47. {
  48. for (var i = 0; i != animTrack.clips.Length; ++i)
  49. {
  50. hash ^= (animTrack.clips[i]).Hash();
  51. }
  52. }
  53. foreach (var subTrack in asset.GetChildTracks())
  54. {
  55. if (subTrack != null)
  56. hash ^= CalcAnimClipHash(subTrack);
  57. }
  58. }
  59. return hash;
  60. }
  61. internal static int CalcAnimClipHash(TimelineAsset asset)
  62. {
  63. int hash = 0;
  64. foreach (var t in asset.GetRootTracks())
  65. {
  66. if (t != null)
  67. hash ^= CalcAnimClipHash(t);
  68. }
  69. return hash;
  70. }
  71. void RebuildKeyCache()
  72. {
  73. m_KeyCache = m_Asset.flattenedTracks.Where(x => (x as AnimationTrack) != null)
  74. .Cast<AnimationTrack>()
  75. .SelectMany(t => GetTrackKeyTimes(t)).
  76. OrderBy(x => x).ToArray();
  77. if (m_KeyCache.Length > 0)
  78. {
  79. float[] unique = new float[m_KeyCache.Length];
  80. unique[0] = m_KeyCache[0];
  81. int index = 0;
  82. for (int i = 1; i < m_KeyCache.Length; i++)
  83. {
  84. if (m_KeyCache[i] - unique[index] > m_Epsilon)
  85. {
  86. index++;
  87. unique[index] = m_KeyCache[i];
  88. }
  89. }
  90. m_KeyCache = unique;
  91. Array.Resize(ref m_KeyCache, index + 1);
  92. }
  93. }
  94. public KeyTraverser(TimelineAsset timeline, float epsilon)
  95. {
  96. m_Asset = timeline;
  97. m_Epsilon = epsilon;
  98. }
  99. void CheckCache(int dirtyStamp)
  100. {
  101. int hash = CalcAnimClipHash(m_Asset);
  102. if (dirtyStamp != m_DirtyStamp || hash != m_LastHash)
  103. {
  104. RebuildKeyCache();
  105. m_DirtyStamp = dirtyStamp;
  106. m_LastHash = hash;
  107. }
  108. }
  109. public float GetNextKey(float key, int dirtyStamp)
  110. {
  111. CheckCache(dirtyStamp);
  112. if (m_KeyCache.Length > 0)
  113. {
  114. if (key < m_KeyCache.Last() - m_Epsilon)
  115. {
  116. if (key > m_KeyCache[0] - m_Epsilon)
  117. {
  118. float t = key + m_Epsilon;
  119. // binary search
  120. int max = m_KeyCache.Length - 1;
  121. int min = 0;
  122. while (max - min > 1)
  123. {
  124. int imid = (min + max) / 2;
  125. if (t > m_KeyCache[imid])
  126. min = imid;
  127. else
  128. max = imid;
  129. }
  130. m_LastIndex = max;
  131. return m_KeyCache[max];
  132. }
  133. m_LastIndex = 0;
  134. return m_KeyCache[0];
  135. }
  136. if (key < m_KeyCache.Last() + m_Epsilon)
  137. {
  138. m_LastIndex = m_KeyCache.Length - 1;
  139. return Mathf.Max(key, m_KeyCache.Last());
  140. }
  141. }
  142. m_LastIndex = -1;
  143. return key;
  144. }
  145. public float GetPrevKey(float key, int dirtyStamp)
  146. {
  147. CheckCache(dirtyStamp);
  148. if (m_KeyCache.Length > 0)
  149. {
  150. if (key > m_KeyCache[0] + m_Epsilon)
  151. {
  152. if (key < m_KeyCache.Last() + m_Epsilon)
  153. {
  154. float t = key - m_Epsilon;
  155. // binary search
  156. int max = m_KeyCache.Length - 1;
  157. int min = 0;
  158. while (max - min > 1)
  159. {
  160. int imid = (min + max) / 2;
  161. if (t < m_KeyCache[imid])
  162. max = imid;
  163. else
  164. min = imid;
  165. }
  166. m_LastIndex = min;
  167. return m_KeyCache[min];
  168. }
  169. m_LastIndex = m_KeyCache.Length - 1;
  170. return m_KeyCache.Last();
  171. }
  172. if (key >= m_KeyCache[0] - m_Epsilon)
  173. {
  174. m_LastIndex = 0;
  175. return Mathf.Min(key, m_KeyCache[0]);
  176. }
  177. }
  178. m_LastIndex = -1;
  179. return key;
  180. }
  181. public int GetKeyCount(int dirtyStamp)
  182. {
  183. CheckCache(dirtyStamp);
  184. return m_KeyCache.Length;
  185. }
  186. }
  187. }