TargetPositionCache.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. using UnityEngine;
  2. using System.Collections.Generic;
  3. using System;
  4. namespace Cinemachine
  5. {
  6. internal class TargetPositionCache
  7. {
  8. public static bool UseCache { get; set; }
  9. public const float CacheStepSize = 1 / 60.0f;
  10. public enum Mode { Disabled, Record, Playback }
  11. static Mode m_CacheMode = Mode.Disabled;
  12. #if UNITY_EDITOR
  13. static TargetPositionCache() { UnityEngine.SceneManagement.SceneManager.sceneLoaded += OnSceneLoaded; }
  14. static void OnSceneLoaded(UnityEngine.SceneManagement.Scene scene, UnityEngine.SceneManagement.LoadSceneMode mode) { ClearCache(); }
  15. #endif
  16. public static Mode CacheMode
  17. {
  18. get => m_CacheMode;
  19. set
  20. {
  21. if (value == m_CacheMode)
  22. return;
  23. m_CacheMode = value;
  24. switch (value)
  25. {
  26. default: case Mode.Disabled: ClearCache(); break;
  27. case Mode.Record: ClearCache(); break;
  28. case Mode.Playback: CreatePlaybackCurves(); break;
  29. }
  30. }
  31. }
  32. public static bool IsRecording => UseCache && m_CacheMode == Mode.Record;
  33. public static bool CurrentPlaybackTimeValid => UseCache && m_CacheMode == Mode.Playback && HasHurrentTime;
  34. public static bool IsEmpty => CacheTimeRange.IsEmpty;
  35. public static float CurrentTime { get; set; }
  36. // These are used during recording to manage camera cuts
  37. public static int CurrentFrame { get; set; }
  38. public static bool IsCameraCut { get; set; }
  39. class CacheCurve
  40. {
  41. public struct Item
  42. {
  43. public Vector3 Pos;
  44. public Quaternion Rot;
  45. public static Item Lerp(Item a, Item b, float t)
  46. {
  47. return new Item
  48. {
  49. Pos = Vector3.LerpUnclamped(a.Pos, b.Pos, t),
  50. Rot = Quaternion.SlerpUnclamped(a.Rot, b.Rot, t)
  51. };
  52. }
  53. public static Item Empty => new Item { Rot = Quaternion.identity };
  54. }
  55. public float StartTime;
  56. public float StepSize;
  57. public int Count => m_Cache.Count;
  58. List<Item> m_Cache;
  59. public CacheCurve(float startTime, float endTime, float stepSize)
  60. {
  61. StepSize = stepSize;
  62. StartTime = startTime;
  63. m_Cache = new List<Item>(Mathf.CeilToInt((StepSize * 0.5f + endTime - startTime) / StepSize));
  64. }
  65. public void Add(Item item) => m_Cache.Add(item);
  66. public void AddUntil(Item item, float time, bool isCut)
  67. {
  68. var prevIndex = m_Cache.Count - 1;
  69. var prevTime = prevIndex * StepSize;
  70. var timeRange = time - StartTime - prevTime;
  71. // If this sample is the first after a camera cut, we don't want to lerp the positions
  72. // in the event that some frames got skipped by timeline and the targets were
  73. // warped at the cut
  74. if (isCut)
  75. for (float t = StepSize; t <= timeRange; t += StepSize)
  76. Add(item);
  77. else
  78. {
  79. var prev = m_Cache[prevIndex];
  80. for (float t = StepSize; t <= timeRange; t += StepSize)
  81. Add(Item.Lerp(prev, item, t / timeRange));
  82. }
  83. }
  84. public Item Evaluate(float time)
  85. {
  86. var numItems = m_Cache.Count;
  87. if (numItems == 0)
  88. return Item.Empty;
  89. var s = time - StartTime;
  90. var index = Mathf.Clamp(Mathf.FloorToInt(s / StepSize), 0, numItems - 1);
  91. var v = m_Cache[index];
  92. if (index == numItems - 1)
  93. return v;
  94. return Item.Lerp(v, m_Cache[index + 1], (s - index * StepSize) / StepSize);
  95. }
  96. }
  97. class CacheEntry
  98. {
  99. public CacheCurve Curve;
  100. struct RecordingItem
  101. {
  102. public float Time;
  103. public bool IsCut;
  104. public CacheCurve.Item Item;
  105. }
  106. List<RecordingItem> RawItems = new List<RecordingItem>();
  107. public void AddRawItem(float time, bool isCut, Transform target)
  108. {
  109. // Preserve monotonic ordering
  110. var endTime = time - CacheStepSize;
  111. var maxItem = RawItems.Count - 1;
  112. var lastToKeep = maxItem;
  113. while (lastToKeep >= 0 && RawItems[lastToKeep].Time > endTime)
  114. --lastToKeep;
  115. if (lastToKeep == maxItem)
  116. {
  117. // Append only, nothing to remove
  118. RawItems.Add(new RecordingItem
  119. {
  120. Time = time,
  121. IsCut = isCut,
  122. Item = new CacheCurve.Item { Pos = target.position, Rot = target.rotation }
  123. });
  124. }
  125. else
  126. {
  127. // Trim off excess, overwrite the one after lastToKeep
  128. var trimStart = lastToKeep + 2;
  129. if (trimStart <= maxItem)
  130. RawItems.RemoveRange(trimStart, RawItems.Count - trimStart);
  131. RawItems[lastToKeep + 1] = new RecordingItem
  132. {
  133. Time = time,
  134. IsCut = isCut,
  135. Item = new CacheCurve.Item { Pos = target.position, Rot = target.rotation }
  136. };
  137. }
  138. }
  139. public void CreateCurves()
  140. {
  141. int maxItem = RawItems.Count - 1;
  142. float startTime = maxItem < 0 ? 0 : RawItems[0].Time;
  143. float endTime = maxItem < 0 ? 0 : RawItems[maxItem].Time;
  144. Curve = new CacheCurve(startTime, endTime, CacheStepSize);
  145. Curve.Add(maxItem < 0 ? CacheCurve.Item.Empty : RawItems[0].Item);
  146. for (int i = 1; i <= maxItem; ++i)
  147. Curve.AddUntil(RawItems[i].Item, RawItems[i].Time, RawItems[i].IsCut);
  148. RawItems.Clear();
  149. }
  150. }
  151. static Dictionary<Transform, CacheEntry> m_Cache;
  152. public struct TimeRange
  153. {
  154. public float Start;
  155. public float End;
  156. public bool IsEmpty => End < Start;
  157. public bool Contains(float time) => time >= Start && time <= End;
  158. public static TimeRange Empty
  159. { get => new TimeRange { Start = float.MaxValue, End = float.MinValue }; }
  160. public void Include(float time)
  161. {
  162. Start = Mathf.Min(Start, time);
  163. End = Mathf.Max(End, time);
  164. }
  165. }
  166. static TimeRange m_CacheTimeRange;
  167. public static TimeRange CacheTimeRange { get => m_CacheTimeRange; }
  168. public static bool HasHurrentTime { get => m_CacheTimeRange.Contains(CurrentTime); }
  169. public static void ClearCache()
  170. {
  171. m_Cache = CacheMode == Mode.Disabled ? null : new Dictionary<Transform, CacheEntry>();
  172. m_CacheTimeRange = TimeRange.Empty;
  173. CurrentTime = 0;
  174. CurrentFrame = 0;
  175. IsCameraCut = false;
  176. }
  177. static void CreatePlaybackCurves()
  178. {
  179. if (m_Cache == null)
  180. m_Cache = new Dictionary<Transform, CacheEntry>();
  181. var iter = m_Cache.GetEnumerator();
  182. while (iter.MoveNext())
  183. iter.Current.Value.CreateCurves();
  184. }
  185. const float kWraparoundSlush = 0.1f;
  186. /// <summary>
  187. /// If Recording, will log the target position at the CurrentTime.
  188. /// Otherwise, will fetch the cached position at CurrentTime.
  189. /// </summary>
  190. /// <param name="target">Target whose transform is tracked</param>
  191. /// <param name="position">Target's position at CurrentTime</param>
  192. public static Vector3 GetTargetPosition(Transform target)
  193. {
  194. if (!UseCache || CacheMode == Mode.Disabled)
  195. return target.position;
  196. // Wrap around during record?
  197. if (CacheMode == Mode.Record
  198. && !m_CacheTimeRange.IsEmpty
  199. && CurrentTime < m_CacheTimeRange.Start - kWraparoundSlush)
  200. {
  201. ClearCache();
  202. }
  203. if (CacheMode == Mode.Playback && !HasHurrentTime)
  204. return target.position;
  205. if (!m_Cache.TryGetValue(target, out var entry))
  206. {
  207. if (CacheMode != Mode.Record)
  208. return target.position;
  209. entry = new CacheEntry();
  210. m_Cache.Add(target, entry);
  211. }
  212. if (CacheMode == Mode.Record)
  213. {
  214. entry.AddRawItem(CurrentTime, IsCameraCut, target);
  215. m_CacheTimeRange.Include(CurrentTime);
  216. return target.position;
  217. }
  218. if (entry.Curve == null)
  219. return target.position;
  220. return entry.Curve.Evaluate(CurrentTime).Pos;
  221. }
  222. /// <summary>
  223. /// If Recording, will log the target rotation at the CurrentTime.
  224. /// Otherwise, will fetch the cached position at CurrentTime.
  225. /// </summary>
  226. /// <param name="target">Target whose transform is tracked</param>
  227. /// <param name="rotation">Target's rotation at CurrentTime</param>
  228. public static Quaternion GetTargetRotation(Transform target)
  229. {
  230. if (CacheMode == Mode.Disabled)
  231. return target.rotation;
  232. // Wrap around during record?
  233. if (CacheMode == Mode.Record
  234. && !m_CacheTimeRange.IsEmpty
  235. && CurrentTime < m_CacheTimeRange.Start - kWraparoundSlush)
  236. {
  237. ClearCache();
  238. }
  239. if (CacheMode == Mode.Playback && !HasHurrentTime)
  240. return target.rotation;
  241. if (!m_Cache.TryGetValue(target, out var entry))
  242. {
  243. if (CacheMode != Mode.Record)
  244. return target.rotation;
  245. entry = new CacheEntry();
  246. m_Cache.Add(target, entry);
  247. }
  248. if (CacheMode == Mode.Record)
  249. {
  250. if (m_CacheTimeRange.End <= CurrentTime)
  251. {
  252. entry.AddRawItem(CurrentTime, IsCameraCut, target);
  253. m_CacheTimeRange.Include(CurrentTime);
  254. }
  255. return target.rotation;
  256. }
  257. return entry.Curve.Evaluate(CurrentTime).Rot;
  258. }
  259. }
  260. }