SkinComponent.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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. #include <AnKi/Scene/Components/SkinComponent.h>
  6. #include <AnKi/Scene/SceneNode.h>
  7. #include <AnKi/Scene/SceneGraph.h>
  8. #include <AnKi/Resource/SkeletonResource.h>
  9. #include <AnKi/Resource/AnimationResource.h>
  10. #include <AnKi/Resource/ResourceManager.h>
  11. #include <AnKi/Util/BitSet.h>
  12. namespace anki
  13. {
  14. ANKI_SCENE_COMPONENT_STATICS(SkinComponent)
  15. SkinComponent::SkinComponent(SceneNode* node)
  16. : SceneComponent(node, getStaticClassId())
  17. , m_node(node)
  18. {
  19. }
  20. SkinComponent::~SkinComponent()
  21. {
  22. m_boneTrfs[0].destroy(m_node->getAllocator());
  23. m_boneTrfs[1].destroy(m_node->getAllocator());
  24. m_animationTrfs.destroy(m_node->getAllocator());
  25. }
  26. Error SkinComponent::loadSkeletonResource(CString fname)
  27. {
  28. ANKI_CHECK(m_node->getSceneGraph().getResourceManager().loadResource(fname, m_skeleton));
  29. m_boneTrfs[0].destroy(m_node->getAllocator());
  30. m_boneTrfs[1].destroy(m_node->getAllocator());
  31. m_animationTrfs.destroy(m_node->getAllocator());
  32. m_boneTrfs[0].create(m_node->getAllocator(), m_skeleton->getBones().getSize(), Mat4::getIdentity());
  33. m_boneTrfs[1].create(m_node->getAllocator(), m_skeleton->getBones().getSize(), Mat4::getIdentity());
  34. m_animationTrfs.create(m_node->getAllocator(), m_skeleton->getBones().getSize(),
  35. {Vec3(0.0f), Quat::getIdentity(), 1.0f});
  36. return Error::NONE;
  37. }
  38. void SkinComponent::playAnimation(U32 track, AnimationResourcePtr anim, const AnimationPlayInfo& info)
  39. {
  40. const Second animDuration = anim->getDuration();
  41. m_tracks[track].m_anim = anim;
  42. m_tracks[track].m_absoluteStartTime = m_absoluteTime + info.m_startTime;
  43. m_tracks[track].m_relativeTimePassed = 0.0;
  44. if(info.m_repeatTimes > 0.0)
  45. {
  46. m_tracks[track].m_blendInTime = min(animDuration * info.m_repeatTimes, info.m_blendInTime);
  47. m_tracks[track].m_blendOutTime = min(animDuration * info.m_repeatTimes, info.m_blendOutTime);
  48. }
  49. else
  50. {
  51. m_tracks[track].m_blendInTime = info.m_blendInTime;
  52. m_tracks[track].m_blendOutTime = 0.0; // Irrelevant
  53. }
  54. m_tracks[track].m_repeatTimes = info.m_repeatTimes;
  55. }
  56. Error SkinComponent::update(SceneNode& node, Second prevTime, Second crntTime, Bool& updated)
  57. {
  58. ANKI_ASSERT(&node == m_node);
  59. updated = false;
  60. if(!m_skeleton.isCreated())
  61. {
  62. return Error::NONE;
  63. }
  64. const Second dt = crntTime - prevTime;
  65. Vec4 minExtend(MAX_F32, MAX_F32, MAX_F32, 0.0f);
  66. Vec4 maxExtend(MIN_F32, MIN_F32, MIN_F32, 0.0f);
  67. BitSet<128> bonesAnimated(false);
  68. for(Track& track : m_tracks)
  69. {
  70. if(!track.m_anim.isCreated())
  71. {
  72. continue;
  73. }
  74. if(track.m_absoluteStartTime > m_absoluteTime)
  75. {
  76. // Hasn't started yet
  77. continue;
  78. }
  79. const Second clipDuration = track.m_anim->getDuration();
  80. const Second animationDuration = track.m_repeatTimes * clipDuration;
  81. if(track.m_repeatTimes > 0.0 && track.m_relativeTimePassed > animationDuration)
  82. {
  83. // Animation finished
  84. continue;
  85. }
  86. updated = true;
  87. const Second animTime = track.m_relativeTimePassed;
  88. track.m_relativeTimePassed += dt;
  89. // Iterate the animation channels and interpolate
  90. for(U32 i = 0; i < track.m_anim->getChannels().getSize(); ++i)
  91. {
  92. const AnimationChannel& channel = track.m_anim->getChannels()[i];
  93. const Bone* bone = m_skeleton->tryFindBone(channel.m_name.toCString());
  94. if(!bone)
  95. {
  96. ANKI_SCENE_LOGW("Animation is referencing unknown bone \"%s\"", &channel.m_name[0]);
  97. continue;
  98. }
  99. const U32 boneIdx = bone->getIndex();
  100. // Interpolate
  101. Vec3 position;
  102. Quat rotation;
  103. F32 scale;
  104. track.m_anim->interpolate(i, animTime, position, rotation, scale);
  105. // Blend with previous track
  106. if(bonesAnimated.get(boneIdx) && (track.m_blendInTime > 0.0 || track.m_blendOutTime > 0.0))
  107. {
  108. F32 blendInFactor;
  109. if(track.m_blendInTime > 0.0)
  110. {
  111. blendInFactor = min(1.0f, F32(animTime / track.m_blendInTime));
  112. }
  113. else
  114. {
  115. blendInFactor = 1.0f;
  116. }
  117. F32 blendOutFactor;
  118. if(track.m_blendOutTime > 0.0)
  119. {
  120. blendOutFactor = min(1.0f, F32((animationDuration - animTime) / track.m_blendOutTime));
  121. }
  122. else
  123. {
  124. blendOutFactor = 1.0f;
  125. }
  126. const F32 factor = blendInFactor * blendOutFactor;
  127. if(factor < 1.0f)
  128. {
  129. const Trf& prevTrf = m_animationTrfs[boneIdx];
  130. position = linearInterpolate(prevTrf.m_translation, position, factor);
  131. rotation = prevTrf.m_rotation.slerp(rotation, factor);
  132. scale = linearInterpolate(prevTrf.m_scale, scale, factor);
  133. }
  134. }
  135. // Store
  136. bonesAnimated.set(boneIdx);
  137. m_animationTrfs[boneIdx] = {position, rotation, scale};
  138. }
  139. }
  140. // Always update the 1st time
  141. updated = updated || (m_absoluteTime == 0.0);
  142. if(updated)
  143. {
  144. m_prevBoneTrfs = m_crntBoneTrfs;
  145. m_crntBoneTrfs = m_crntBoneTrfs ^ 1;
  146. // Walk the bone hierarchy to add additional transforms
  147. visitBones(m_skeleton->getRootBone(), Mat4::getIdentity(), bonesAnimated, minExtend, maxExtend);
  148. const Vec4 E(EPSILON, EPSILON, EPSILON, 0.0f);
  149. m_boneBoundingVolume.setMin(minExtend - E);
  150. m_boneBoundingVolume.setMax(maxExtend + E);
  151. }
  152. else
  153. {
  154. m_prevBoneTrfs = m_crntBoneTrfs;
  155. }
  156. m_absoluteTime += dt;
  157. return Error::NONE;
  158. }
  159. void SkinComponent::visitBones(const Bone& bone, const Mat4& parentTrf, const BitSet<128>& bonesAnimated,
  160. Vec4& minExtend, Vec4& maxExtend)
  161. {
  162. Mat4 outMat;
  163. if(bonesAnimated.get(bone.getIndex()))
  164. {
  165. const Trf& t = m_animationTrfs[bone.getIndex()];
  166. outMat = parentTrf * Mat4(t.m_translation.xyz1(), Mat3(t.m_rotation), t.m_scale);
  167. }
  168. else
  169. {
  170. outMat = parentTrf * bone.getTransform();
  171. }
  172. m_boneTrfs[m_crntBoneTrfs][bone.getIndex()] = outMat * bone.getVertexTransform();
  173. // Update volume
  174. const Vec4 bonePos = outMat * Vec4(0.0f, 0.0f, 0.0f, 1.0f);
  175. minExtend = minExtend.min(bonePos.xyz0());
  176. maxExtend = maxExtend.max(bonePos.xyz0());
  177. for(const Bone* child : bone.getChildren())
  178. {
  179. visitBones(*child, outMat, bonesAnimated, minExtend, maxExtend);
  180. }
  181. }
  182. } // end namespace anki