BsAnimationCurve.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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 "BsAnimationInstance.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 AnimationInstanceData<T>& animInstance, bool loop = true);
  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);
  55. private:
  56. friend struct RTTIPlainType<TAnimationCurve<T>>;
  57. /**
  58. * Returns a pair of keys that can be used for interpolating to field the value at the provided time. This attempts
  59. * to find keys using the cache first, and if not possible falls back to a full search.
  60. *
  61. * @param[in] time Time for which to find the relevant keys from. It is expected to be clamped to a
  62. * valid range within the curve.
  63. * @param[in] animInstance Animation instance data holding the time to evaluate the curve at, and any cached
  64. * data from previous requests. Time is expected to be clamped to a valid range
  65. * within the curve.
  66. * @param[out] leftKey Index of the key to interpolate from.
  67. * @param[out] rightKey Index of the key to interpolate to.
  68. */
  69. void findKeys(float time, const AnimationInstanceData<T>& animInstance, UINT32& leftKey, UINT32& rightKey);
  70. /**
  71. * Returns a pair of keys that can be used for interpolating to field the value at the provided time.
  72. *
  73. * @param[in] time Time for which to find the relevant keys from. It is expected to be clamped to a
  74. * valid range within the curve.
  75. * @param[out] leftKey Index of the key to interpolate from.
  76. * @param[out] rightKey Index of the key to interpolate to.
  77. */
  78. void findKeys(float time, UINT32& leftKey, UINT32& rightKey);
  79. /**
  80. * Evaluates a value at the cached curve. Caller must ensure the request time falls within the cached curve range.
  81. *
  82. * @param[in] animInstance Animation instance data holding the time to evaluate the curve at, and any cached
  83. * data from previous requests.
  84. * @return Interpolated value from the curve at provided time.
  85. */
  86. T evaluateCache(const AnimationInstanceData<T>& animInstance);
  87. static const UINT32 CACHE_LOOKAHEAD;
  88. Vector<KeyFrame> mKeyframes;
  89. float mStart;
  90. float mEnd;
  91. float mLength;
  92. };
  93. /** An animation curve and its name. */
  94. template <class T>
  95. struct TNamedAnimationCurve
  96. {
  97. String name;
  98. TAnimationCurve<T> curve;
  99. };
  100. /** @} */
  101. }