CharacterTrackAnimator.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. #include "CharacterTrackAnimator.h"
  9. #include "AnimNode.h"
  10. #include "CharacterTrack.h"
  11. /*static*/ const float CCharacterTrackAnimator::s_minClipDuration = 0.01666666666f; // 1/60th of a second, or one-frame for 60Hz rendering
  12. //////////////////////////////////////////////////////////////////////////
  13. CCharacterTrackAnimator::CCharacterTrackAnimator()
  14. {
  15. m_forceAnimKeyChange = false;
  16. m_baseAnimState.m_layerPlaysAnimation[0] = m_baseAnimState.m_layerPlaysAnimation[1] = m_baseAnimState.m_layerPlaysAnimation[2] = false;
  17. ResetLastAnimKeys();
  18. m_baseAnimState.m_bTimeJumped[0] = m_baseAnimState.m_bTimeJumped[1] = m_baseAnimState.m_bTimeJumped[2] = false;
  19. m_baseAnimState.m_jumpTime[0] = m_baseAnimState.m_jumpTime[1] = m_baseAnimState.m_jumpTime[2] = 0.0f;
  20. }
  21. //////////////////////////////////////////////////////////////////////////
  22. void CCharacterTrackAnimator::ResetLastAnimKeys()
  23. {
  24. m_baseAnimState.m_lastAnimationKeys[0][0] = -1;
  25. m_baseAnimState.m_lastAnimationKeys[0][1] = -1;
  26. m_baseAnimState.m_lastAnimationKeys[1][0] = -1;
  27. m_baseAnimState.m_lastAnimationKeys[1][1] = -1;
  28. m_baseAnimState.m_lastAnimationKeys[2][0] = -1;
  29. m_baseAnimState.m_lastAnimationKeys[2][1] = -1;
  30. }
  31. //////////////////////////////////////////////////////////////////////////
  32. void CCharacterTrackAnimator::OnReset(IAnimNode* animNode)
  33. {
  34. ResetLastAnimKeys();
  35. ReleaseAllAnimations(animNode);
  36. m_baseAnimState.m_layerPlaysAnimation[0] = m_baseAnimState.m_layerPlaysAnimation[1] = m_baseAnimState.m_layerPlaysAnimation[2] = false;
  37. }
  38. //////////////////////////////////////////////////////////////////////////
  39. f32 CCharacterTrackAnimator::ComputeAnimKeyNormalizedTime(const ICharacterKey& key, float ectime) const
  40. {
  41. float endTime = key.GetValidEndTime();
  42. const float clipDuration = clamp_tpl(endTime - key.m_startTime, s_minClipDuration, key.m_duration);
  43. float t;
  44. f32 retNormalizedTime;
  45. if (clipDuration > s_minClipDuration)
  46. {
  47. t = (ectime - key.time) * key.m_speed;
  48. if (key.m_bLoop && t > clipDuration)
  49. {
  50. // compute t for repeating clip
  51. t = fmod(t, clipDuration);
  52. }
  53. t += key.m_startTime;
  54. t = clamp_tpl(t, key.m_startTime, endTime);
  55. }
  56. else
  57. {
  58. // clip has perceptably no length - set time to beginning or end frame, whichever comes first in time
  59. t = (key.m_startTime < endTime) ? key.m_startTime : endTime;
  60. }
  61. retNormalizedTime = clamp_tpl(t / key.m_duration, .0f, 1.0f);
  62. return retNormalizedTime;
  63. }
  64. //////////////////////////////////////////////////////////////////////////
  65. ILINE bool CCharacterTrackAnimator::IsAnimationPlaying(const SAnimState& animState) const
  66. {
  67. return animState.m_layerPlaysAnimation[0] || animState.m_layerPlaysAnimation[1] || animState.m_layerPlaysAnimation[2];
  68. }
  69. //////////////////////////////////////////////////////////////////////////
  70. void CCharacterTrackAnimator::ReleaseAllAnimations([[maybe_unused]] IAnimNode* animNode)
  71. {
  72. }
  73. //////////////////////////////////////////////////////////////////////////
  74. void CCharacterTrackAnimator::AnimateTrack([[maybe_unused]] class CCharacterTrack* pTrack, [[maybe_unused]] SAnimContext& ec, [[maybe_unused]] int layer, [[maybe_unused]] int trackIndex)
  75. {
  76. }
  77. void CCharacterTrackAnimator::ForceAnimKeyChange()
  78. {
  79. m_forceAnimKeyChange = true;
  80. }