SkinComponent.h 2.7 KB

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