SkinComponent.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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/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. SkinComponent::SkinComponent(SceneNode* node)
  14. : SceneComponent(node, kClassType)
  15. {
  16. }
  17. SkinComponent::~SkinComponent()
  18. {
  19. }
  20. SkinComponent& SkinComponent::setSkeletonFilename(CString fname)
  21. {
  22. SkeletonResourcePtr rsrc;
  23. const Error err = ResourceManager::getSingleton().loadResource(fname, rsrc);
  24. if(err)
  25. {
  26. ANKI_SCENE_LOGE("Failed to load skeleton");
  27. }
  28. else
  29. {
  30. m_resourceDirty = true;
  31. m_skeleton = std::move(rsrc);
  32. // Cleanup
  33. m_boneTrfs[0].destroy();
  34. m_boneTrfs[1].destroy();
  35. m_animationTrfs.destroy();
  36. GpuSceneBuffer::getSingleton().deferredFree(m_gpuSceneBoneTransforms);
  37. // Create
  38. const U32 boneCount = m_skeleton->getBones().getSize();
  39. m_boneTrfs[0].resize(boneCount, Mat3x4::getIdentity());
  40. m_boneTrfs[1].resize(boneCount, Mat3x4::getIdentity());
  41. m_animationTrfs.resize(boneCount, Trf{Vec3(0.0f), Quat::getIdentity(), 1.0f});
  42. m_gpuSceneBoneTransforms = GpuSceneBuffer::getSingleton().allocate(sizeof(Mat4) * boneCount * 2, 4);
  43. }
  44. return *this;
  45. }
  46. CString SkinComponent::getSkeletonFilename() const
  47. {
  48. return (m_skeleton) ? m_skeleton->getFilename() : "*Error*";
  49. }
  50. void SkinComponent::playAnimation(U32 trackIdx, AnimationResourcePtr anim, const AnimationPlayInfo& info)
  51. {
  52. const Second animDuration = anim->getDuration();
  53. Track& track = m_tracks[trackIdx];
  54. track.m_anim = anim;
  55. track.m_absoluteStartTime = m_absoluteTime + info.m_startTime;
  56. track.m_relativeTimePassed = 0.0;
  57. if(info.m_repeatTimes > 0.0)
  58. {
  59. track.m_blendInTime = min(animDuration * info.m_repeatTimes, info.m_blendInTime);
  60. track.m_blendOutTime = min(animDuration * info.m_repeatTimes, info.m_blendOutTime);
  61. }
  62. else
  63. {
  64. track.m_blendInTime = info.m_blendInTime;
  65. track.m_blendOutTime = 0.0; // Irrelevant
  66. }
  67. track.m_repeatTimes = info.m_repeatTimes;
  68. track.m_animationSpeedScale = max(0.1f, info.m_animationSpeedScale);
  69. }
  70. void SkinComponent::update(SceneComponentUpdateInfo& info, Bool& updated)
  71. {
  72. updated = false;
  73. if(!m_skeleton.isCreated())
  74. {
  75. return;
  76. }
  77. // Always update two frames because of the bone transforms buffer
  78. const Bool updatedLastFrame = m_updatedLastFrame;
  79. const Bool resourceDirty = m_resourceDirty;
  80. m_resourceDirty = false;
  81. Bool animationRun = false;
  82. const Second dt = info.m_dt;
  83. Vec4 minExtend(kMaxF32, kMaxF32, kMaxF32, 0.0f);
  84. Vec4 maxExtend(kMinF32, kMinF32, kMinF32, 0.0f);
  85. BitSet<128> bonesAnimated(false);
  86. for(Track& track : m_tracks)
  87. {
  88. if(!track.m_anim.isCreated())
  89. {
  90. continue;
  91. }
  92. if(track.m_absoluteStartTime > m_absoluteTime)
  93. {
  94. // Hasn't started yet
  95. continue;
  96. }
  97. const Second clipDuration = track.m_anim->getDuration();
  98. const Second animationDuration = track.m_repeatTimes * clipDuration;
  99. if(track.m_repeatTimes > 0.0 && track.m_relativeTimePassed > animationDuration)
  100. {
  101. // Animation finished
  102. continue;
  103. }
  104. animationRun = true;
  105. const Second animTime = track.m_relativeTimePassed;
  106. track.m_relativeTimePassed += dt * Second(track.m_animationSpeedScale);
  107. // Iterate the animation channels and interpolate
  108. for(U32 i = 0; i < track.m_anim->getChannels().getSize(); ++i)
  109. {
  110. const AnimationChannel& channel = track.m_anim->getChannels()[i];
  111. const Bone* bone = m_skeleton->tryFindBone(channel.m_name.toCString());
  112. if(!bone)
  113. {
  114. ANKI_SCENE_LOGW("Animation is referencing unknown bone \"%s\"", &channel.m_name[0]);
  115. continue;
  116. }
  117. const U32 boneIdx = bone->getIndex();
  118. // Interpolate
  119. Vec3 position;
  120. Quat rotation;
  121. F32 scale;
  122. track.m_anim->interpolate(i, animTime, position, rotation, scale);
  123. // Blend with previous track
  124. if(bonesAnimated.get(boneIdx) && (track.m_blendInTime > 0.0 || track.m_blendOutTime > 0.0))
  125. {
  126. F32 blendInFactor;
  127. if(track.m_blendInTime > 0.0)
  128. {
  129. blendInFactor = min(1.0f, F32(animTime / track.m_blendInTime));
  130. }
  131. else
  132. {
  133. blendInFactor = 1.0f;
  134. }
  135. F32 blendOutFactor;
  136. if(track.m_blendOutTime > 0.0)
  137. {
  138. blendOutFactor = min(1.0f, F32((animationDuration - animTime) / track.m_blendOutTime));
  139. }
  140. else
  141. {
  142. blendOutFactor = 1.0f;
  143. }
  144. const F32 factor = blendInFactor * blendOutFactor;
  145. if(factor < 1.0f)
  146. {
  147. const Trf& prevTrf = m_animationTrfs[boneIdx];
  148. position = linearInterpolate(prevTrf.m_translation, position, factor);
  149. rotation = prevTrf.m_rotation.slerp(rotation, factor);
  150. scale = linearInterpolate(prevTrf.m_scale, scale, factor);
  151. }
  152. }
  153. // Store
  154. bonesAnimated.set(boneIdx);
  155. m_animationTrfs[boneIdx] = {position, rotation, scale};
  156. }
  157. }
  158. if(updatedLastFrame || resourceDirty || animationRun)
  159. {
  160. m_prevBoneTrfs = m_crntBoneTrfs;
  161. m_crntBoneTrfs = m_crntBoneTrfs ^ 1;
  162. // Walk the bone hierarchy to add additional transforms
  163. visitBones(m_skeleton->getRootBone(), Mat3x4::getIdentity(), bonesAnimated, minExtend, maxExtend);
  164. const Vec4 e(kEpsilonf, kEpsilonf, kEpsilonf, 0.0f);
  165. m_boneBoundingVolume.setMin(minExtend - e);
  166. m_boneBoundingVolume.setMax(maxExtend + e);
  167. // Update the GPU scene
  168. const U32 boneCount = m_skeleton->getBones().getSize();
  169. DynamicArray<Mat3x4, MemoryPoolPtrWrapper<StackMemoryPool>> trfs(info.m_framePool);
  170. trfs.resize(boneCount * 2);
  171. for(U32 i = 0; i < boneCount; ++i)
  172. {
  173. trfs[i * 2 + 0] = getBoneTransforms()[i];
  174. trfs[i * 2 + 1] = getPreviousFrameBoneTransforms()[i];
  175. }
  176. GpuSceneMicroPatcher::getSingleton().newCopy(*info.m_framePool, m_gpuSceneBoneTransforms, trfs.getSizeInBytes(), trfs.getBegin());
  177. }
  178. else
  179. {
  180. m_prevBoneTrfs = m_crntBoneTrfs;
  181. }
  182. m_absoluteTime += dt;
  183. updated = updatedLastFrame || resourceDirty || animationRun;
  184. m_updatedLastFrame = resourceDirty || animationRun;
  185. }
  186. void SkinComponent::visitBones(const Bone& bone, const Mat3x4& parentTrf, const BitSet<128>& bonesAnimated, Vec4& minExtend, Vec4& maxExtend)
  187. {
  188. Mat3x4 outMat;
  189. if(bonesAnimated.get(bone.getIndex()))
  190. {
  191. const Trf& t = m_animationTrfs[bone.getIndex()];
  192. outMat = parentTrf.combineTransformations(Mat3x4(t.m_translation.xyz(), Mat3(t.m_rotation), Vec3(t.m_scale)));
  193. }
  194. else
  195. {
  196. outMat = parentTrf.combineTransformations(bone.getTransform());
  197. }
  198. m_boneTrfs[m_crntBoneTrfs][bone.getIndex()] = outMat.combineTransformations(bone.getVertexTransform());
  199. // Update volume
  200. const Vec3 bonePos = outMat * Vec4(0.0f, 0.0f, 0.0f, 1.0f);
  201. minExtend = minExtend.min(bonePos.xyz0());
  202. maxExtend = maxExtend.max(bonePos.xyz0());
  203. for(const Bone* child : bone.getChildren())
  204. {
  205. visitBones(*child, outMat, bonesAnimated, minExtend, maxExtend);
  206. }
  207. }
  208. } // end namespace anki