SkinComponent.h 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. // Copyright (C) 2009-2021, Panagiotis Christopoulos Charitos and contributors.
  2. // All rights reserved.
  3. // Code licensed under the BSD License.
  4. // http://www.anki3d.org/LICENSE
  5. #pragma once
  6. #include <AnKi/Scene/Components/SceneComponent.h>
  7. #include <AnKi/Resource/Forward.h>
  8. #include <AnKi/Collision/Aabb.h>
  9. #include <AnKi/Util/Forward.h>
  10. #include <AnKi/Util/WeakArray.h>
  11. #include <AnKi/Math.h>
  12. namespace anki
  13. {
  14. /// @addtogroup scene
  15. /// @{
  16. /// @memberof SkinComponent
  17. class AnimationPlayInfo
  18. {
  19. public:
  20. /// The time the animation will start after being pushed in SkinComponent::playAnimation().
  21. Second m_startTime = 0.0;
  22. /// Negative means infinite.
  23. F32 m_repeatTimes = 1.0f;
  24. /// The time from when the animation starts until it fully replaces the animations of previous tracks.
  25. Second m_blendInTime = 0.0f;
  26. /// The time from when the animation ends until it until it has zero influence to the animations of previous tracks.
  27. Second m_blendOutTime = 0.0f;
  28. };
  29. /// Skin component.
  30. class SkinComponent : public SceneComponent
  31. {
  32. ANKI_SCENE_COMPONENT(SkinComponent)
  33. public:
  34. static constexpr U32 MAX_ANIMATION_TRACKS = 4;
  35. SkinComponent(SceneNode* node);
  36. ~SkinComponent();
  37. /// Load the skeleton resource.
  38. ANKI_USE_RESULT Error loadSkeletonResource(CString filename);
  39. ANKI_USE_RESULT Error update(SceneNode& node, Second prevTime, Second crntTime, Bool& updated) override;
  40. void playAnimation(U32 track, AnimationResourcePtr anim, const AnimationPlayInfo& info);
  41. ConstWeakArray<Mat4> getBoneTransforms() const
  42. {
  43. return m_boneTrfs[m_crntBoneTrfs];
  44. }
  45. ConstWeakArray<Mat4> getPreviousFrameBoneTransforms() const
  46. {
  47. return m_boneTrfs[m_prevBoneTrfs];
  48. }
  49. const SkeletonResourcePtr& getSkeleronResource() const
  50. {
  51. return m_skeleton;
  52. }
  53. const Aabb& getBoneBoundingVolumeLocalSpace() const
  54. {
  55. return m_boneBoundingVolume;
  56. }
  57. Bool isEnabled() const
  58. {
  59. return m_skeleton.isCreated();
  60. }
  61. private:
  62. class Track
  63. {
  64. public:
  65. AnimationResourcePtr m_anim;
  66. Second m_absoluteStartTime = 0.0;
  67. Second m_relativeTimePassed = 0.0;
  68. Second m_blendInTime = 0.0;
  69. Second m_blendOutTime = 0.0f;
  70. F32 m_repeatTimes = 1.0f;
  71. };
  72. class Trf
  73. {
  74. public:
  75. Vec3 m_translation;
  76. Quat m_rotation;
  77. F32 m_scale;
  78. };
  79. SceneNode* m_node;
  80. SkeletonResourcePtr m_skeleton;
  81. Array<DynamicArray<Mat4>, 2> m_boneTrfs;
  82. DynamicArray<Trf> m_animationTrfs;
  83. Aabb m_boneBoundingVolume = Aabb(Vec3(-1.0f), Vec3(1.0f));
  84. Array<Track, MAX_ANIMATION_TRACKS> m_tracks;
  85. Second m_absoluteTime = 0.0;
  86. U8 m_crntBoneTrfs = 0;
  87. U8 m_prevBoneTrfs = 1;
  88. void visitBones(const Bone& bone, const Mat4& parentTrf, const BitSet<128, U8>& bonesAnimated, Vec4& minExtend,
  89. Vec4& maxExtend);
  90. };
  91. /// @}
  92. } // end namespace anki