RecorderTimeline.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Runtime.InteropServices;
  4. using UnityEngine;
  5. namespace Chronos
  6. {
  7. public interface IRecorder
  8. {
  9. void Reset();
  10. int EstimateMemoryUsage();
  11. }
  12. public abstract class RecorderTimeline<TComponent, TSnapshot> : ComponentTimeline<TComponent>, IRecorder where TComponent : Component
  13. {
  14. public RecorderTimeline(Timeline timeline, TComponent component) : base(timeline, component)
  15. {
  16. snapshots = new List<TSnapshot>();
  17. times = new List<float>();
  18. }
  19. public override void OnStartOrReEnable()
  20. {
  21. Reset();
  22. }
  23. public override void Update()
  24. {
  25. float timeScale = timeline.timeScale;
  26. if (lastTimeScale >= 0 && timeScale < 0) // Started rewind
  27. {
  28. laterSnapshot = CopySnapshot();
  29. laterTime = timeline.time;
  30. interpolatedSnapshot = laterSnapshot;
  31. canRewind = TryFindEarlierSnapshot(false);
  32. }
  33. if (timeScale > 0)
  34. {
  35. Progress();
  36. }
  37. else if (timeScale < 0)
  38. {
  39. Rewind();
  40. }
  41. lastTimeScale = timeScale;
  42. }
  43. #region Fields
  44. protected List<TSnapshot> snapshots;
  45. protected List<float> times;
  46. protected int capacity;
  47. protected float recordingTimer;
  48. protected float lastTimeScale = 1;
  49. protected bool canRewind;
  50. protected TSnapshot laterSnapshot;
  51. protected float laterTime;
  52. protected TSnapshot earlierSnapshot;
  53. protected float earlierTime;
  54. protected TSnapshot interpolatedSnapshot;
  55. #endregion
  56. #region Properties
  57. /// <summary>
  58. /// Indicates whether the recorder has exhausted its rewind capacity.
  59. /// </summary>
  60. public bool exhaustedRewind
  61. {
  62. get { return !canRewind; }
  63. }
  64. /// <summary>
  65. /// Returns the available rewind duration in seconds.
  66. /// </summary>
  67. public float availableRewindDuration
  68. {
  69. get
  70. {
  71. if (exhaustedRewind || times.Count == 0)
  72. {
  73. return 0;
  74. }
  75. return Mathf.Max(0, timeline.time - times[0]);
  76. }
  77. }
  78. #endregion
  79. #region Flow
  80. protected void Progress()
  81. {
  82. if (recordingTimer >= timeline.recordingInterval)
  83. {
  84. Record();
  85. recordingTimer = 0;
  86. }
  87. recordingTimer += timeline.deltaTime;
  88. }
  89. /// <summary>
  90. /// Forces the recprder to record a snapshot at the current frame.
  91. /// </summary>
  92. public void Record()
  93. {
  94. if (!timeline.rewindable)
  95. {
  96. return;
  97. }
  98. if (snapshots.Count == capacity)
  99. {
  100. snapshots.RemoveAt(0);
  101. times.RemoveAt(0);
  102. }
  103. snapshots.Add(CopySnapshot());
  104. times.Add(timeline.time);
  105. canRewind = true;
  106. }
  107. protected void Rewind()
  108. {
  109. if (canRewind)
  110. {
  111. if (timeline.time <= earlierTime)
  112. {
  113. canRewind = TryFindEarlierSnapshot(true);
  114. if (!canRewind)
  115. {
  116. // Make sure the last snapshot is perfectly in place
  117. interpolatedSnapshot = earlierSnapshot;
  118. ApplySnapshot(interpolatedSnapshot);
  119. timeline.SendMessage("OnExhaustRewind", SendMessageOptions.DontRequireReceiver);
  120. return;
  121. }
  122. }
  123. float t = (laterTime - timeline.time) / (laterTime - earlierTime);
  124. interpolatedSnapshot = LerpSnapshots(laterSnapshot, earlierSnapshot, t);
  125. ApplySnapshot(interpolatedSnapshot);
  126. }
  127. }
  128. protected void OnExhaustRewind()
  129. {
  130. if (Timekeeper.instance.debug)
  131. {
  132. Debug.LogWarning("Reached rewind limit.");
  133. }
  134. }
  135. #endregion
  136. #region Snapshots
  137. protected abstract TSnapshot LerpSnapshots(TSnapshot from, TSnapshot to, float t);
  138. protected abstract TSnapshot CopySnapshot();
  139. protected abstract void ApplySnapshot(TSnapshot snapshot);
  140. protected bool TryFindEarlierSnapshot(bool pop)
  141. {
  142. if (pop)
  143. {
  144. if (snapshots.Count < 1)
  145. {
  146. return false;
  147. }
  148. laterSnapshot = snapshots[snapshots.Count - 1];
  149. laterTime = times[times.Count - 1];
  150. snapshots.RemoveAt(snapshots.Count - 1);
  151. times.RemoveAt(times.Count - 1);
  152. }
  153. if (snapshots.Count < 1)
  154. {
  155. return false;
  156. }
  157. earlierSnapshot = snapshots[snapshots.Count - 1];
  158. earlierTime = times[times.Count - 1];
  159. return true;
  160. }
  161. /// <summary>
  162. /// Resets the snapshots.
  163. /// </summary>
  164. public override void Reset()
  165. {
  166. lastTimeScale = 1;
  167. if (timeline.recordingDuration < timeline.recordingInterval)
  168. {
  169. throw new ChronosException("The recording duration must be longer than or equal to interval.");
  170. }
  171. if (timeline.recordingInterval <= 0)
  172. {
  173. throw new ChronosException("The recording interval must be positive.");
  174. }
  175. snapshots.Clear();
  176. times.Clear();
  177. capacity = Mathf.CeilToInt(timeline.recordingDuration / timeline.recordingInterval);
  178. snapshots.Capacity = capacity;
  179. times.Capacity = capacity;
  180. recordingTimer = 0;
  181. Record();
  182. }
  183. /// <summary>
  184. /// Modifies all snapshots via the specified modifier delegate.
  185. /// </summary>
  186. public virtual void ModifySnapshots(SnapshotModifier modifier)
  187. {
  188. for (int i = 0; i < snapshots.Count; i++)
  189. {
  190. snapshots[i] = modifier(snapshots[i], times[i]);
  191. }
  192. }
  193. public delegate TSnapshot SnapshotModifier(TSnapshot snapshot, float time);
  194. #endregion
  195. internal static int EstimateMemoryUsage(float duration, float interval)
  196. {
  197. int structSize = Marshal.SizeOf(typeof(TSnapshot));
  198. int structAmount = Mathf.CeilToInt(duration / interval);
  199. int pointerAmount = 1;
  200. while (pointerAmount < structAmount) pointerAmount *= 2;
  201. int pointerSize = IntPtr.Size;
  202. return (structSize * structAmount) + (pointerSize * pointerAmount);
  203. }
  204. public int EstimateMemoryUsage()
  205. {
  206. return EstimateMemoryUsage(timeline.recordingDuration, timeline.recordingInterval);
  207. }
  208. }
  209. }