CCAnimationCurve.inl 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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;
  44. dst[1] = quat.y;
  45. dst[2] = quat.z;
  46. dst[3] = quat.w;
  47. }
  48. break;
  49. case EvaluateType::INT_USER_FUNCTION:
  50. {
  51. if (_evaluateFun)
  52. _evaluateFun(time, dst);
  53. }
  54. break;
  55. default:
  56. break;
  57. }
  58. }
  59. template <int componentSize>
  60. void AnimationCurve<componentSize>::setEvaluateFun(std::function<void(float time, float* dst)> fun)
  61. {
  62. _evaluateFun = fun;
  63. }
  64. //create animation curve
  65. template <int componentSize>
  66. AnimationCurve<componentSize>* AnimationCurve<componentSize>::create(float* keytime, float* value, int count)
  67. {
  68. int floatSize = sizeof(float);
  69. AnimationCurve* curve = new (std::nothrow) AnimationCurve();
  70. curve->_keytime = new float[count];
  71. memcpy(curve->_keytime, keytime, count * floatSize);
  72. int compoentSizeByte = componentSize * floatSize;
  73. int totalByte = count * compoentSizeByte;
  74. curve->_value = new float[totalByte / floatSize];
  75. memcpy(curve->_value, value, totalByte);
  76. curve->_count = count;
  77. curve->_componentSizeByte = compoentSizeByte;
  78. curve->autorelease();
  79. return curve;
  80. }
  81. template <int componentSize>
  82. float AnimationCurve<componentSize>::getStartTime() const
  83. {
  84. return _keytime[0];
  85. }
  86. template <int componentSize>
  87. float AnimationCurve<componentSize>::getEndTime() const
  88. {
  89. return _keytime[_count - 1];
  90. }
  91. template <int componentSize>
  92. AnimationCurve<componentSize>::AnimationCurve()
  93. : _value(nullptr)
  94. , _keytime(nullptr)
  95. , _count(0)
  96. , _componentSizeByte(0)
  97. , _evaluateFun(nullptr)
  98. {
  99. }
  100. template <int componentSize>
  101. AnimationCurve<componentSize>::~AnimationCurve()
  102. {
  103. CC_SAFE_DELETE_ARRAY(_keytime);
  104. CC_SAFE_DELETE_ARRAY(_value);
  105. }
  106. template <int componentSize>
  107. int AnimationCurve<componentSize>::determineIndex(float time) const
  108. {
  109. unsigned int min = 0;
  110. unsigned int max = _count - 1;
  111. unsigned int mid = 0;
  112. do
  113. {
  114. mid = (min + max) >> 1;
  115. if (time >= _keytime[mid] && time <= _keytime[mid + 1])
  116. return mid;
  117. else if (time < _keytime[mid])
  118. max = mid - 1;
  119. else
  120. min = mid + 1;
  121. } while (min <= max);
  122. // We should never hit this!
  123. return -1;
  124. }
  125. NS_CC_END