SkinComponent.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. #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. #include <AnKi/GpuMemory/GpuSceneBuffer.h>
  13. namespace anki {
  14. // Passed to SkinComponent::playAnimation
  15. class AnimationPlayInfo
  16. {
  17. public:
  18. // The time the animation will start after being pushed in SkinComponent::playAnimation()
  19. Second m_startTime = 0.0;
  20. // Negative means infinite
  21. F32 m_repeatTimes = 1.0f;
  22. // The time from when the animation starts until it fully replaces the animations of previous tracks
  23. Second m_blendInTime = 0.0f;
  24. // The time from when the animation ends until it until it has zero influence to the animations of previous tracks
  25. Second m_blendOutTime = 0.0f;
  26. // For example a value of 2.0 will play the animation in double speed
  27. F32 m_animationSpeedScale = 1.0f;
  28. };
  29. // Skin component
  30. class SkinComponent : public SceneComponent
  31. {
  32. ANKI_SCENE_COMPONENT(SkinComponent)
  33. public:
  34. static constexpr U32 kMaxAnimationTracks = 4;
  35. SkinComponent(SceneNode* node);
  36. ~SkinComponent();
  37. SkinComponent& setSkeletonFilename(CString filename);
  38. CString getSkeletonFilename() const;
  39. Bool hasSkeletonResource() const
  40. {
  41. return !!m_resource;
  42. }
  43. void playAnimation(U32 track, AnimationResourcePtr anim, const AnimationPlayInfo& info);
  44. Bool isValid() const
  45. {
  46. return m_resource.isCreated();
  47. }
  48. ANKI_INTERNAL ConstWeakArray<Mat3x4> getBoneTransforms() const
  49. {
  50. ANKI_ASSERT(isValid());
  51. return m_boneTrfs[m_crntBoneTrfs];
  52. }
  53. ANKI_INTERNAL ConstWeakArray<Mat3x4> getPreviousFrameBoneTransforms() const
  54. {
  55. ANKI_ASSERT(isValid());
  56. return m_boneTrfs[m_prevBoneTrfs];
  57. }
  58. ANKI_INTERNAL const SkeletonResourcePtr& getSkeleronResource() const
  59. {
  60. ANKI_ASSERT(isValid());
  61. return m_resource;
  62. }
  63. ANKI_INTERNAL const Aabb& getBoneBoundingVolumeLocalSpace() const
  64. {
  65. ANKI_ASSERT(isValid());
  66. return m_boneBoundingVolume;
  67. }
  68. ANKI_INTERNAL U32 getBoneTransformsGpuSceneOffset() const
  69. {
  70. ANKI_ASSERT(isValid());
  71. return m_gpuSceneBoneTransforms.getOffset();
  72. }
  73. ANKI_INTERNAL Bool gpuSceneReallocationsThisFrame() const
  74. {
  75. ANKI_ASSERT(isValid());
  76. return m_boneTransformsReallocatedThisFrame;
  77. }
  78. private:
  79. class Track
  80. {
  81. public:
  82. AnimationResourcePtr m_anim;
  83. Second m_absoluteStartTime = 0.0;
  84. Second m_relativeTimePassed = 0.0;
  85. Second m_blendInTime = 0.0;
  86. Second m_blendOutTime = 0.0f;
  87. F32 m_repeatTimes = 1.0f;
  88. F32 m_animationSpeedScale = 1.0f;
  89. };
  90. class Trf
  91. {
  92. public:
  93. Vec3 m_translation;
  94. Quat m_rotation;
  95. F32 m_scale;
  96. };
  97. SkeletonResourcePtr m_resource;
  98. Array<SceneDynamicArray<Mat3x4>, 2> m_boneTrfs;
  99. SceneDynamicArray<Trf> m_animationTrfs;
  100. Aabb m_boneBoundingVolume = Aabb(Vec3(-1.0f), Vec3(1.0f));
  101. Array<Track, kMaxAnimationTracks> m_tracks;
  102. Second m_absoluteTime = 0.0;
  103. U8 m_crntBoneTrfs = 0;
  104. U8 m_prevBoneTrfs = 1;
  105. Bool m_updatedLastFrame : 1 = true;
  106. Bool m_resourceDirty : 1 = true;
  107. Bool m_boneTransformsReallocatedThisFrame : 1 = false;
  108. GpuSceneBufferAllocation m_gpuSceneBoneTransforms;
  109. void update(SceneComponentUpdateInfo& info, Bool& updated) override;
  110. Error serialize(SceneSerializer& serializer) override;
  111. void visitBones(const Bone& bone, const Mat3x4& parentTrf, const BitSet<128, U8>& bonesAnimated, Vec4& minExtend, Vec4& maxExtend);
  112. };
  113. } // end namespace anki