BsAnimationCurve.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #pragma once
  4. #include "BsCorePrerequisites.h"
  5. #include "BsCurveEvaluator.h"
  6. namespace BansheeEngine
  7. {
  8. /** @addtogroup Animation-Internal
  9. * @{
  10. */
  11. /** Animation keyframe, represented as an endpoint of a cubic hermite spline. */
  12. template <class T>
  13. struct TKeyframe
  14. {
  15. T value; /**< Value of the key. */
  16. T inTangent; /**< Input tangent (going from the previous key to this one) of the key. */
  17. T outTangent; /**< Output tangent (going from this key to next one) of the key. */
  18. float time; /**< Position of the key along the animation spline. */
  19. };
  20. /**
  21. * Animation spline represented by a set of keyframes, each representing an endpoint of a cubic hermite curve. The
  22. * spline can be evaluated at any time, and uses caching to speed up multiple sequential evaluations.
  23. */
  24. template <class T>
  25. class BS_CORE_EXPORT TAnimationCurve // Note: Curves are expected to be immutable for threading purposes
  26. {
  27. public:
  28. typedef TKeyframe<T> KeyFrame;
  29. TAnimationCurve();
  30. TAnimationCurve(const Vector<KeyFrame>& keyframes);
  31. /**
  32. * Evaluate the animation curve using caching. Caching can significantly speed of evaluation if the evaluation
  33. * happens sequential order (which should be true for most curves). If evaluation is not happening in sequential
  34. * order using the non-caching version of evaluate() might yield better performance.
  35. *
  36. * @param[i] animInstance Animation instance data holding the time to evaluate the curve at, and any cached
  37. * data from previous requests. Caller should ensure to maintain a persistent instance
  38. * of this data for every animation using this curve in order to ensure cache is
  39. * maintained.
  40. * @param[in] loop If true the curve will loop when it goes past the end or beggining. Otherwise the
  41. * curve value will be clamped.
  42. * @return Interpolated value from the curve at provided time.
  43. */
  44. T evaluate(const TCurveEvaluatorData<T>& animInstance, bool loop = true) const;
  45. /**
  46. * Evaluate the animation curve at the specified time. If evaluating multiple values in a sequential order consider
  47. * using the cached version of evaluate() for better performance.
  48. *
  49. * @param[i] time Time to evaluate the curve at.
  50. * @param[in] loop If true the curve will loop when it goes past the end or beggining. Otherwise the curve
  51. * value will be clamped.
  52. * @return Interpolated value from the curve at provided time.
  53. */
  54. T evaluate(float time, bool loop = true) const;
  55. /**
  56. * Splits a piece of the animation curve into a separate animation curve.
  57. *
  58. * @param[in] start Beginning time of the split curve.
  59. * @param[in] end End time of the split curve.
  60. * @return New curve with data corresponding to the provided split times.
  61. */
  62. TAnimationCurve<T> split(float start, float end);
  63. private:
  64. friend struct RTTIPlainType<TAnimationCurve<T>>;
  65. /**
  66. * Returns a pair of keys that can be used for interpolating to field the value at the provided time. This attempts
  67. * to find keys using the cache first, and if not possible falls back to a full search.
  68. *
  69. * @param[in] time Time for which to find the relevant keys from. It is expected to be clamped to a
  70. * valid range within the curve.
  71. * @param[in] animInstance Animation instance data holding the time to evaluate the curve at, and any cached
  72. * data from previous requests. Time is expected to be clamped to a valid range
  73. * within the curve.
  74. * @param[out] leftKey Index of the key to interpolate from.
  75. * @param[out] rightKey Index of the key to interpolate to.
  76. */
  77. void findKeys(float time, const TCurveEvaluatorData<T>& animInstance, UINT32& leftKey, UINT32& rightKey) const;
  78. /**
  79. * Returns a pair of keys that can be used for interpolating to field the value at the provided time.
  80. *
  81. * @param[in] time Time for which to find the relevant keys from. It is expected to be clamped to a
  82. * valid range within the curve.
  83. * @param[out] leftKey Index of the key to interpolate from.
  84. * @param[out] rightKey Index of the key to interpolate to.
  85. */
  86. void findKeys(float time, UINT32& leftKey, UINT32& rightKey) const;
  87. /** Returns a keyframe index nearest to the provided time. */
  88. UINT32 findKey(float time);
  89. /**
  90. * Calculates a key in-between the provided two keys.
  91. *
  92. * @param[in] lhs Key to interpolate from.
  93. * @param[in] rhs Key to interpolate to.
  94. * @param[in] t Curve time to interpolate the keys at.
  95. * @return Interpolated key value.
  96. */
  97. KeyFrame evaluateKey(const KeyFrame& lhs, const KeyFrame& rhs, float time);
  98. /**
  99. * Evaluates a value at the cached curve. Caller must ensure the request time falls within the cached curve range.
  100. *
  101. * @param[in] animInstance Animation instance data holding the time to evaluate the curve at, and any cached
  102. * data from previous requests.
  103. * @return Interpolated value from the curve at provided time.
  104. */
  105. T evaluateCache(const TCurveEvaluatorData<T>& animInstance) const;
  106. static const UINT32 CACHE_LOOKAHEAD;
  107. Vector<KeyFrame> mKeyframes;
  108. float mStart;
  109. float mEnd;
  110. float mLength;
  111. };
  112. /** An animation curve and its name. */
  113. template <class T>
  114. struct TNamedAnimationCurve
  115. {
  116. String name;
  117. TAnimationCurve<T> curve;
  118. };
  119. /** @} */
  120. }