AnimationEvent.cpp 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. // Copyright (C) 2009-present, Panagiotis Christopoulos Charitos and contributors.
  2. // All rights reserved.
  3. // Code licensed under the BSD License.
  4. // http://www.anki3d.org/LICENSE
  5. #include <AnKi/Scene/Events/AnimationEvent.h>
  6. #include <AnKi/Scene/SceneNode.h>
  7. #include <AnKi/Scene/SceneGraph.h>
  8. #include <AnKi/Resource/ResourceManager.h>
  9. namespace anki {
  10. AnimationEvent::AnimationEvent(CString animationFilename, CString channelName, SceneNode* movableSceneNode)
  11. : Event(0.0, 1.0)
  12. {
  13. if(!ANKI_EXPECT(movableSceneNode))
  14. {
  15. markForDeletion();
  16. return;
  17. }
  18. if(!ANKI_EXPECT(!ResourceManager::getSingleton().loadResource(animationFilename, m_anim)))
  19. {
  20. markForDeletion();
  21. return;
  22. }
  23. m_channelIndex = 0;
  24. for(const AnimationChannel& channel : m_anim->getChannels())
  25. {
  26. if(channel.m_name == channelName)
  27. {
  28. break;
  29. }
  30. ++m_channelIndex;
  31. }
  32. if(m_channelIndex == m_anim->getChannels().getSize())
  33. {
  34. ANKI_SCENE_LOGE("Can't initialize AnimationEvent. Channel not found: %s", channelName.cstr());
  35. markForDeletion();
  36. return;
  37. }
  38. init(m_anim->getStartingTime(), m_anim->getDuration());
  39. m_reanimate = true;
  40. m_associatedNodes.emplaceBack(movableSceneNode);
  41. }
  42. void AnimationEvent::update([[maybe_unused]] Second prevUpdateTime, Second crntTime)
  43. {
  44. Vec3 pos;
  45. Quat rot;
  46. F32 scale = 1.0;
  47. m_anim->interpolate(m_channelIndex, crntTime, pos, rot, scale);
  48. Transform trf;
  49. trf.setOrigin(pos.xyz0);
  50. // trf.setOrigin(Vec4(0.0f));
  51. trf.setRotation(Mat3x4(Vec3(0.0f), rot));
  52. // trf.setRotation(Mat3x4::getIdentity());
  53. trf.setScale(Vec4(scale, scale, scale, 0.0f));
  54. m_associatedNodes[0]->setLocalTransform(trf);
  55. }
  56. } // end namespace anki