BsAnimationClip.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #include "BsAnimationClip.h"
  4. #include "BsResources.h"
  5. #include "BsSkeleton.h"
  6. #include "BsAnimationClipRTTI.h"
  7. namespace BansheeEngine
  8. {
  9. AnimationClip::AnimationClip()
  10. : Resource(false), mVersion(0), mCurves(bs_shared_ptr_new<AnimationCurves>()), mIsAdditive(false)
  11. {
  12. }
  13. AnimationClip::AnimationClip(const SPtr<AnimationCurves>& curves, bool isAdditive)
  14. : Resource(false), mVersion(0), mCurves(curves), mIsAdditive(isAdditive)
  15. {
  16. }
  17. HAnimationClip AnimationClip::create(bool isAdditive)
  18. {
  19. return static_resource_cast<AnimationClip>(gResources()._createResourceHandle(_createPtr(nullptr, isAdditive)));
  20. }
  21. HAnimationClip AnimationClip::create(const SPtr<AnimationCurves>& curves, bool isAdditive)
  22. {
  23. return static_resource_cast<AnimationClip>(gResources()._createResourceHandle(_createPtr(curves, isAdditive)));
  24. }
  25. SPtr<AnimationClip> AnimationClip::createEmpty()
  26. {
  27. AnimationClip* rawPtr = new (bs_alloc<AnimationClip>()) AnimationClip();
  28. SPtr<AnimationClip> newClip = bs_core_ptr<AnimationClip>(rawPtr);
  29. newClip->_setThisPtr(newClip);
  30. return newClip;
  31. }
  32. SPtr<AnimationClip> AnimationClip::_createPtr(const SPtr<AnimationCurves>& curves, bool isAdditive)
  33. {
  34. AnimationClip* rawPtr = new (bs_alloc<AnimationClip>()) AnimationClip(curves, isAdditive);
  35. SPtr<AnimationClip> newClip = bs_core_ptr<AnimationClip>(rawPtr);
  36. newClip->_setThisPtr(newClip);
  37. newClip->initialize();
  38. return newClip;
  39. }
  40. void AnimationClip::addPositionCurve(const String& name, const TAnimationCurve<Vector3>& curve)
  41. {
  42. SPtr<AnimationCurves> newCurves = bs_shared_ptr_new<AnimationCurves>();
  43. newCurves->rotation = mCurves->rotation;
  44. newCurves->scale = mCurves->scale;
  45. newCurves->generic = mCurves->generic;
  46. for(auto& entry : mCurves->position)
  47. {
  48. if (entry.name != name)
  49. newCurves->position.push_back(entry);
  50. }
  51. newCurves->position.push_back({ name, curve });
  52. mCurves = newCurves;
  53. buildNameMapping();
  54. mVersion++;
  55. }
  56. void AnimationClip::addRotationCurve(const String& name, const TAnimationCurve<Quaternion>& curve)
  57. {
  58. SPtr<AnimationCurves> newCurves = bs_shared_ptr_new<AnimationCurves>();
  59. newCurves->position = mCurves->position;
  60. newCurves->scale = mCurves->scale;
  61. newCurves->generic = mCurves->generic;
  62. for (auto& entry : mCurves->rotation)
  63. {
  64. if (entry.name != name)
  65. newCurves->rotation.push_back(entry);
  66. }
  67. newCurves->rotation.push_back({ name, curve });
  68. mCurves = newCurves;
  69. buildNameMapping();
  70. mVersion++;
  71. }
  72. void AnimationClip::addScaleCurve(const String& name, const TAnimationCurve<Vector3>& curve)
  73. {
  74. SPtr<AnimationCurves> newCurves = bs_shared_ptr_new<AnimationCurves>();
  75. newCurves->position = mCurves->position;
  76. newCurves->rotation = mCurves->rotation;
  77. newCurves->generic = mCurves->generic;
  78. for (auto& entry : mCurves->scale)
  79. {
  80. if (entry.name != name)
  81. newCurves->scale.push_back(entry);
  82. }
  83. newCurves->scale.push_back({ name, curve });
  84. mCurves = newCurves;
  85. buildNameMapping();
  86. mVersion++;
  87. }
  88. void AnimationClip::addGenericCurve(const String& name, const TAnimationCurve<float>& curve)
  89. {
  90. SPtr<AnimationCurves> newCurves = bs_shared_ptr_new<AnimationCurves>();
  91. newCurves->position = mCurves->position;
  92. newCurves->rotation = mCurves->rotation;
  93. newCurves->scale = mCurves->scale;
  94. for (auto& entry : mCurves->generic)
  95. {
  96. if (entry.name != name)
  97. newCurves->generic.push_back(entry);
  98. }
  99. mCurves = newCurves;
  100. buildNameMapping();
  101. mVersion++;
  102. }
  103. void AnimationClip::removePositionCurve(const String& name)
  104. {
  105. SPtr<AnimationCurves> newCurves = bs_shared_ptr_new<AnimationCurves>();
  106. newCurves->rotation = mCurves->rotation;
  107. newCurves->scale = mCurves->scale;
  108. newCurves->generic = mCurves->generic;
  109. for (auto& entry : mCurves->position)
  110. {
  111. if (entry.name != name)
  112. newCurves->position.push_back(entry);
  113. }
  114. mCurves = newCurves;
  115. buildNameMapping();
  116. mVersion++;
  117. }
  118. void AnimationClip::removeRotationCurve(const String& name)
  119. {
  120. SPtr<AnimationCurves> newCurves = bs_shared_ptr_new<AnimationCurves>();
  121. newCurves->position = mCurves->position;
  122. newCurves->scale = mCurves->scale;
  123. newCurves->generic = mCurves->generic;
  124. for (auto& entry : mCurves->rotation)
  125. {
  126. if (entry.name != name)
  127. newCurves->rotation.push_back(entry);
  128. }
  129. mCurves = newCurves;
  130. buildNameMapping();
  131. mVersion++;
  132. }
  133. void AnimationClip::removeScaleCurve(const String& name)
  134. {
  135. SPtr<AnimationCurves> newCurves = bs_shared_ptr_new<AnimationCurves>();
  136. newCurves->position = mCurves->position;
  137. newCurves->rotation = mCurves->rotation;
  138. newCurves->generic = mCurves->generic;
  139. for (auto& entry : mCurves->scale)
  140. {
  141. if (entry.name != name)
  142. newCurves->scale.push_back(entry);
  143. }
  144. mCurves = newCurves;
  145. buildNameMapping();
  146. mVersion++;
  147. }
  148. void AnimationClip::removeGenericCurve(const String& name)
  149. {
  150. SPtr<AnimationCurves> newCurves = bs_shared_ptr_new<AnimationCurves>();
  151. newCurves->position = mCurves->position;
  152. newCurves->rotation = mCurves->rotation;
  153. newCurves->scale = mCurves->scale;
  154. for (auto& entry : mCurves->generic)
  155. {
  156. if (entry.name != name)
  157. newCurves->generic.push_back(entry);
  158. }
  159. mCurves = newCurves;
  160. buildNameMapping();
  161. mVersion++;
  162. }
  163. void AnimationClip::buildNameMapping()
  164. {
  165. mNameMapping.clear();
  166. auto registerEntries = [&](auto& curve, CurveType type)
  167. {
  168. UINT32 typeIdx = (UINT32)type;
  169. for (UINT32 i = 0; i < (UINT32)curve.size(); i++)
  170. {
  171. auto& entry = curve[i];
  172. auto iterFind = mNameMapping.find(entry.name);
  173. if (iterFind == mNameMapping.end())
  174. {
  175. UINT32* indices = mNameMapping[entry.name];
  176. memset(indices, -1, sizeof(UINT32) * 4);
  177. indices[typeIdx] = i;
  178. }
  179. else
  180. mNameMapping[entry.name][typeIdx] = i;
  181. }
  182. };
  183. registerEntries(mCurves->position, CurveType::Position);
  184. registerEntries(mCurves->rotation, CurveType::Rotation);
  185. registerEntries(mCurves->scale, CurveType::Scale);
  186. registerEntries(mCurves->generic, CurveType::Generic);
  187. }
  188. void AnimationClip::initialize()
  189. {
  190. buildNameMapping();
  191. Resource::initialize();
  192. }
  193. void AnimationClip::getBoneMapping(const Skeleton& skeleton, AnimationCurveMapping* mapping) const
  194. {
  195. UINT32 numBones = skeleton.getNumBones();
  196. for(UINT32 i = 0; i < numBones; i++)
  197. {
  198. const SkeletonBoneInfo& boneInfo = skeleton.getBoneInfo(i);
  199. auto iterFind = mNameMapping.find(boneInfo.name);
  200. if(iterFind != mNameMapping.end())
  201. {
  202. const UINT32* indices = iterFind->second;
  203. mapping[i].position = indices[(UINT32)CurveType::Position];
  204. mapping[i].rotation = indices[(UINT32)CurveType::Rotation];
  205. mapping[i].scale = indices[(UINT32)CurveType::Scale];
  206. }
  207. else
  208. mapping[i] = { (UINT32)-1, (UINT32)-1, (UINT32)-1 };
  209. }
  210. }
  211. RTTITypeBase* AnimationClip::getRTTIStatic()
  212. {
  213. return AnimationClipRTTI::instance();
  214. }
  215. RTTITypeBase* AnimationClip::getRTTI() const
  216. {
  217. return getRTTIStatic();
  218. }
  219. }