CCAnimationCurve.inl 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. #include "3d/CCAnimationCurve.h"
  2. NS_CC_BEGIN
  3. template <int componentSize>
  4. void AnimationCurve<componentSize>::evaluate(float time, float* dst, EvaluateType type) const
  5. {
  6. if (_count == 1 || time <= _keytime[0])
  7. {
  8. memcpy(dst, _value, _componentSizeByte);
  9. return;
  10. }
  11. else if (time >= _keytime[_count - 1])
  12. {
  13. memcpy(dst, &_value[(_count - 1) * componentSize], _componentSizeByte);
  14. return;
  15. }
  16. unsigned int index = determineIndex(time);
  17. float scale = (_keytime[index + 1] - _keytime[index]);
  18. float t = (time - _keytime[index]) / scale;
  19. float* fromValue = &_value[index * componentSize];
  20. float* toValue = fromValue + componentSize;
  21. switch (type) {
  22. case EvaluateType::INT_LINEAR:
  23. {
  24. for (auto i = 0; i < componentSize; i++) {
  25. dst[i] = fromValue[i] + (toValue[i] - fromValue[i]) * t;
  26. }
  27. }
  28. break;
  29. case EvaluateType::INT_NEAR:
  30. {
  31. float* src = std::abs(t) > 0.5f ? toValue : fromValue;
  32. memcpy(dst, src, _componentSizeByte);
  33. }
  34. break;
  35. case EvaluateType::INT_QUAT_SLERP:
  36. {
  37. // Evaluate.
  38. Quaternion quat;
  39. if (t >= 0)
  40. Quaternion::slerp(Quaternion(fromValue), Quaternion(toValue), t, &quat);
  41. else
  42. Quaternion::slerp(Quaternion(toValue), Quaternion(fromValue), t, &quat);
  43. dst[0] = quat.x, dst[1] = quat.y, dst[2] = quat.z, dst[3] = quat.w;
  44. }
  45. break;
  46. case EvaluateType::INT_USER_FUNCTION:
  47. {
  48. if (_evaluateFun)
  49. _evaluateFun(time, dst);
  50. }
  51. break;
  52. default:
  53. break;
  54. }
  55. }
  56. template <int componentSize>
  57. void AnimationCurve<componentSize>::setEvaluateFun(std::function<void(float time, float* dst)> fun)
  58. {
  59. _evaluateFun = fun;
  60. }
  61. //create animation curve
  62. template <int componentSize>
  63. AnimationCurve<componentSize>* AnimationCurve<componentSize>::create(float* keytime, float* value, int count)
  64. {
  65. int floatSize = sizeof(float);
  66. AnimationCurve* curve = new (std::nothrow) AnimationCurve();
  67. curve->_keytime = new float[count];
  68. memcpy(curve->_keytime, keytime, count * floatSize);
  69. int compoentSizeByte = componentSize * floatSize;
  70. int totalByte = count * compoentSizeByte;
  71. curve->_value = new float[totalByte / floatSize];
  72. memcpy(curve->_value, value, totalByte);
  73. curve->_count = count;
  74. curve->_componentSizeByte = compoentSizeByte;
  75. curve->autorelease();
  76. return curve;
  77. }
  78. template <int componentSize>
  79. float AnimationCurve<componentSize>::getStartTime() const
  80. {
  81. return _keytime[0];
  82. }
  83. template <int componentSize>
  84. float AnimationCurve<componentSize>::getEndTime() const
  85. {
  86. return _keytime[_count - 1];
  87. }
  88. template <int componentSize>
  89. AnimationCurve<componentSize>::AnimationCurve()
  90. : _value(nullptr)
  91. , _keytime(nullptr)
  92. , _count(0)
  93. , _componentSizeByte(0)
  94. , _evaluateFun(nullptr)
  95. {
  96. }
  97. template <int componentSize>
  98. AnimationCurve<componentSize>::~AnimationCurve()
  99. {
  100. CC_SAFE_DELETE_ARRAY(_keytime);
  101. CC_SAFE_DELETE_ARRAY(_value);
  102. }
  103. template <int componentSize>
  104. int AnimationCurve<componentSize>::determineIndex(float time) const
  105. {
  106. unsigned int min = 0;
  107. unsigned int max = _count - 1;
  108. unsigned int mid = 0;
  109. do
  110. {
  111. mid = (min + max) >> 1;
  112. if (time >= _keytime[mid] && time <= _keytime[mid + 1])
  113. return mid;
  114. else if (time < _keytime[mid])
  115. max = mid - 1;
  116. else
  117. min = mid + 1;
  118. } while (min <= max);
  119. // We should never hit this!
  120. return -1;
  121. }
  122. NS_CC_END