KeyframeGroup.cs 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. namespace Funly.SkyStudio
  6. {
  7. [Serializable]
  8. public class KeyframeGroup<T> : System.Object, IKeyframeGroup where T : IBaseKeyframe
  9. {
  10. public List<T> keyframes = new List<T>();
  11. [SerializeField]
  12. private string m_Name;
  13. public string name
  14. {
  15. get { return m_Name; }
  16. set { m_Name = value; }
  17. }
  18. [SerializeField]
  19. private string m_Id;
  20. public string id
  21. {
  22. get { return m_Id; }
  23. set { m_Id = value; }
  24. }
  25. public KeyframeGroup(string name)
  26. {
  27. this.name = name;
  28. id = Guid.NewGuid().ToString();
  29. }
  30. public void AddKeyFrame(T keyFrame)
  31. {
  32. keyframes.Add(keyFrame);
  33. SortKeyframes();
  34. }
  35. public void RemoveKeyFrame(T keyFrame)
  36. {
  37. if (keyframes.Count == 1)
  38. {
  39. Debug.LogError("You must have at least 1 keyframe in every group.");
  40. return;
  41. }
  42. keyframes.Remove(keyFrame);
  43. SortKeyframes();
  44. }
  45. public void RemoveKeyFrame(IBaseKeyframe keyframe)
  46. {
  47. RemoveKeyFrame((T)keyframe);
  48. }
  49. public int GetKeyFrameCount()
  50. {
  51. return keyframes.Count;
  52. }
  53. public T GetKeyframe(int index)
  54. {
  55. return keyframes[index];
  56. }
  57. public void SortKeyframes()
  58. {
  59. keyframes.Sort();
  60. }
  61. public float CurveAdjustedBlendingTime(InterpolationCurve curve, float t)
  62. {
  63. if (curve == InterpolationCurve.Linear)
  64. {
  65. return t;
  66. } else if (curve == InterpolationCurve.EaseInEaseOut)
  67. {
  68. float curveTime = t < .5f ? 2 * t * t : -1 + (4 - 2 * t) * t;
  69. return Mathf.Clamp01(curveTime);
  70. }
  71. return t;
  72. }
  73. // Get the keyframe that comes before this time.
  74. public T GetPreviousKeyFrame(float time)
  75. {
  76. T beforeKeyframe;
  77. T afterKeyframe;
  78. if (!GetSurroundingKeyFrames(time, out beforeKeyframe, out afterKeyframe))
  79. {
  80. return default(T);
  81. }
  82. return beforeKeyframe;
  83. }
  84. public bool GetSurroundingKeyFrames(float time, out T beforeKeyframe, out T afterKeyframe)
  85. {
  86. beforeKeyframe = default(T);
  87. afterKeyframe = default(T);
  88. int beforeIndex, afterIndex;
  89. if (GetSurroundingKeyFrames(time, out beforeIndex, out afterIndex))
  90. {
  91. beforeKeyframe = GetKeyframe(beforeIndex);
  92. afterKeyframe = GetKeyframe(afterIndex);
  93. return true;
  94. }
  95. return false;
  96. }
  97. public bool GetSurroundingKeyFrames(float time, out int beforeIndex, out int afterIndex)
  98. {
  99. beforeIndex = 0;
  100. afterIndex = 0;
  101. if (keyframes.Count == 0)
  102. {
  103. Debug.LogError("Can't return nearby keyframes since it's empty.");
  104. return false;
  105. }
  106. if (keyframes.Count == 1)
  107. {
  108. return true;
  109. }
  110. if (time < keyframes[0].time)
  111. {
  112. beforeIndex = keyframes.Count - 1;
  113. afterIndex = 0;
  114. return true;
  115. }
  116. int keyframeIndex = 0;
  117. for (int i = 0; i < keyframes.Count; i++)
  118. {
  119. if (keyframes[i].time >= time)
  120. {
  121. break;
  122. }
  123. keyframeIndex = i;
  124. }
  125. int nextKeyFrame = (keyframeIndex + 1) % keyframes.Count;
  126. beforeIndex = keyframeIndex;
  127. afterIndex = nextKeyFrame;
  128. return true;
  129. }
  130. public static float ProgressBetweenSurroundingKeyframes(float time, BaseKeyframe beforeKey, BaseKeyframe afterKey) {
  131. return ProgressBetweenSurroundingKeyframes(time, beforeKey.time, afterKey.time);
  132. }
  133. // FIXME - Rename to to percent between circular times.
  134. public static float ProgressBetweenSurroundingKeyframes(float time, float beforeKeyTime, float afterKeyTime)
  135. {
  136. if (afterKeyTime > beforeKeyTime && time <= beforeKeyTime)
  137. {
  138. return 0;
  139. }
  140. float rangeWidth = WidthBetweenCircularValues(beforeKeyTime, afterKeyTime);
  141. float valueWidth = WidthBetweenCircularValues(beforeKeyTime, time);
  142. // Find what percentage this time is between the 2 circular keyframes.
  143. float progress = valueWidth / rangeWidth;
  144. return Mathf.Clamp01(progress);
  145. }
  146. // FIXME - This should really be called distance between circular values.
  147. public static float WidthBetweenCircularValues(float begin, float end)
  148. {
  149. if (begin <= end)
  150. {
  151. return end - begin;
  152. }
  153. return (1 - begin) + end;
  154. }
  155. public void TrimToSingleKeyframe() {
  156. if (keyframes.Count == 1) {
  157. return;
  158. }
  159. keyframes.RemoveRange(1, keyframes.Count - 1);
  160. }
  161. // Returns -1 to move reverse direction, +1 to move in positive direction only.
  162. public InterpolationDirection GetShortestInterpolationDirection(float previousKeyValue, float nextKeyValue, float minValue, float maxValue)
  163. {
  164. // forward means values can only count upwards to next keyframe. Reverse assumes values can only go downards.
  165. float forwardDistance;
  166. float reverseDistance;
  167. CalculateCircularDistances(previousKeyValue, nextKeyValue, minValue, maxValue, out forwardDistance, out reverseDistance);
  168. if (reverseDistance > forwardDistance) {
  169. return InterpolationDirection.Reverse;
  170. } else {
  171. return InterpolationDirection.Foward;
  172. }
  173. }
  174. public void CalculateCircularDistances(float previousKeyValue, float nextKeyValue, float minValue, float maxValue, out float forwardDistance, out float reverseDistance)
  175. {
  176. if (nextKeyValue < previousKeyValue) {
  177. forwardDistance = (maxValue - previousKeyValue) + (nextKeyValue - minValue);
  178. } else {
  179. forwardDistance = nextKeyValue - previousKeyValue;
  180. }
  181. reverseDistance = (minValue + maxValue) - forwardDistance;
  182. }
  183. // This will consider the direction, and curve type to return a float value that's interpolated.
  184. public float InterpolateFloat(InterpolationCurve curve, InterpolationDirection direction,
  185. float time, float beforeTime, float nextTime,
  186. float previousKeyValue, float nextKeyValue,
  187. float minValue, float maxValue)
  188. {
  189. float progressBetweenFrames = ProgressBetweenSurroundingKeyframes(time, beforeTime, nextTime);
  190. float curvedTime = CurveAdjustedBlendingTime(curve, progressBetweenFrames);
  191. // Auto.
  192. if (direction == InterpolationDirection.Auto) {
  193. return AutoInterpolation(curvedTime, previousKeyValue, nextKeyValue);
  194. }
  195. InterpolationDirection moveDirection = direction;
  196. float forwardDistance;
  197. float reverseDistance;
  198. CalculateCircularDistances(previousKeyValue, nextKeyValue, minValue, maxValue, out forwardDistance, out reverseDistance);
  199. // Shortest path.
  200. if (moveDirection == InterpolationDirection.ShortestPath) {
  201. if (reverseDistance > forwardDistance) {
  202. moveDirection = InterpolationDirection.Foward;
  203. } else {
  204. moveDirection = InterpolationDirection.Reverse;
  205. }
  206. }
  207. // Forward.
  208. if (moveDirection == InterpolationDirection.Foward) {
  209. return ForwardInterpolation(curvedTime, previousKeyValue, nextKeyValue, minValue, maxValue, forwardDistance);
  210. }
  211. // Reverse.
  212. if (moveDirection == InterpolationDirection.Reverse) {
  213. return ReverseInterpolation(curvedTime, previousKeyValue, nextKeyValue, minValue, maxValue, reverseDistance);
  214. }
  215. Debug.LogError("Unhandled interpolation direction: " + moveDirection + ", returning min value.");
  216. return minValue;
  217. }
  218. // Standard interpolation without wrap around.
  219. public float AutoInterpolation(float curvedTime, float previousValue, float nextValue)
  220. {
  221. return Mathf.Lerp(previousValue, nextValue, curvedTime);
  222. }
  223. // Force values forward with wrap around.
  224. public float ForwardInterpolation(float time, float previousKeyValue, float nextKeyValue, float minValue, float maxValue, float distance)
  225. {
  226. if (previousKeyValue <= nextKeyValue) {
  227. return Mathf.Lerp(previousKeyValue, nextKeyValue, time);
  228. }
  229. // We know this is gonna rollover now to a lower value.
  230. float currentDistance = time * distance;
  231. float toMaxDistance = maxValue - previousKeyValue;
  232. // return before hitting the max rollover.
  233. if (currentDistance <= toMaxDistance) {
  234. return previousKeyValue + currentDistance;
  235. }
  236. return minValue + (currentDistance - toMaxDistance);
  237. }
  238. // Force values backwards with wrap around.
  239. public float ReverseInterpolation(float time, float previousKeyValue, float nextKeyValue, float minValue, float maxValue, float distance)
  240. {
  241. if (nextKeyValue <= previousKeyValue) {
  242. return Mathf.Lerp(previousKeyValue, nextKeyValue, time);
  243. }
  244. float currentDistance = time * distance;
  245. float toMinDistance = previousKeyValue - minValue;
  246. if (currentDistance <= toMinDistance) {
  247. return previousKeyValue - currentDistance;
  248. }
  249. return maxValue - (currentDistance - toMinDistance);
  250. }
  251. }
  252. }