AnimationState.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2011 Lasse Öörni
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. //
  23. #include "Precompiled.h"
  24. #include "AnimatedModel.h"
  25. #include "Animation.h"
  26. #include "AnimationState.h"
  27. #include "Deserializer.h"
  28. #include "Serializer.h"
  29. #include "XMLElement.h"
  30. #include "DebugNew.h"
  31. AnimationState::AnimationState(AnimatedModel* model, Animation* animation) :
  32. model_(model),
  33. animation_(animation),
  34. startBone_(0),
  35. looped_(false),
  36. weight_(0.0f),
  37. time_(0.0f),
  38. layer_(0),
  39. useNlerp_(false)
  40. {
  41. SetStartBone(0);
  42. // Setup a cache for last keyframe of each track
  43. lastKeyFrame_.resize(animation_->GetNumTracks());
  44. for (unsigned i = 0; i < lastKeyFrame_.size(); ++i)
  45. lastKeyFrame_[i] = 0;
  46. }
  47. AnimationState::~AnimationState()
  48. {
  49. }
  50. void AnimationState::SetStartBone(Bone* startBone)
  51. {
  52. if (!model_)
  53. {
  54. startBone_ = 0;
  55. return;
  56. }
  57. Skeleton& skeleton = model_->GetSkeleton();
  58. const std::vector<Bone>& bones = skeleton.GetBones();
  59. Bone* rootBone = skeleton.GetRootBone();
  60. if (!rootBone)
  61. return;
  62. if (!startBone)
  63. startBone = rootBone;
  64. startBone_ = startBone;
  65. trackToBoneMap_.clear();
  66. if (!startBone->node_)
  67. return;
  68. const std::vector<AnimationTrack>& tracks = animation_->GetTracks();
  69. for (unsigned i = 0; i < tracks.size(); ++i)
  70. {
  71. // Include those tracks that are either the startbone itself, or its children
  72. Bone* trackBone = 0;
  73. const StringHash& nameHash = tracks[i].nameHash_;
  74. if (nameHash == startBone->nameHash_)
  75. trackBone = startBone;
  76. else
  77. {
  78. Node* trackBoneNode = startBone->node_->GetChild(nameHash, true);
  79. if (trackBoneNode)
  80. trackBone = skeleton.GetBone(nameHash);
  81. }
  82. if (trackBone)
  83. trackToBoneMap_[i] = trackBone;
  84. }
  85. model_->MarkAnimationDirty();
  86. }
  87. void AnimationState::SetLooped(bool looped)
  88. {
  89. looped_ = looped;
  90. }
  91. void AnimationState::SetWeight(float weight)
  92. {
  93. weight = Clamp(weight, 0.0f, 1.0f);
  94. if (weight != weight_)
  95. {
  96. weight_ = weight;
  97. model_->MarkAnimationDirty();
  98. }
  99. }
  100. void AnimationState::SetTime(float time)
  101. {
  102. time = Clamp(time, 0.0f, animation_->GetLength());
  103. if (time != time_)
  104. {
  105. time_ = time;
  106. model_->MarkAnimationDirty();
  107. }
  108. }
  109. void AnimationState::AddWeight(float delta)
  110. {
  111. if (delta == 0.0f)
  112. return;
  113. SetWeight(GetWeight() + delta);
  114. }
  115. void AnimationState::AddTime(float delta)
  116. {
  117. float length = animation_->GetLength();
  118. if ((delta == 0.0f) || (length == 0.0f))
  119. return;
  120. float time = GetTime() + delta;
  121. if (looped_)
  122. {
  123. while (time >= length)
  124. time -= length;
  125. while (time < 0.0f)
  126. time += length;
  127. }
  128. SetTime(time);
  129. }
  130. void AnimationState::SetLayer(int layer)
  131. {
  132. if (layer != layer_)
  133. {
  134. layer_ = layer;
  135. model_->MarkAnimationOrderDirty();
  136. }
  137. }
  138. void AnimationState::SetUseNlerp(bool enable)
  139. {
  140. useNlerp_ = enable;
  141. }
  142. Bone* AnimationState::GetStartBone() const
  143. {
  144. return model_ ? startBone_ : 0;
  145. }
  146. float AnimationState::GetLength() const
  147. {
  148. return animation_->GetLength();
  149. }
  150. void AnimationState::Apply()
  151. {
  152. if (!IsEnabled())
  153. return;
  154. // Check first if full weight or blending
  155. if (weight_ == 1.0f)
  156. {
  157. for (std::map<unsigned, Bone*>::const_iterator i = trackToBoneMap_.begin(); i != trackToBoneMap_.end(); ++i)
  158. {
  159. const AnimationTrack* track = animation_->GetTrack(i->first);
  160. Bone* bone = i->second;
  161. Node* boneNode = bone->node_;
  162. if ((!boneNode) || (!bone->animated_) || (!track->keyFrames_.size()))
  163. continue;
  164. unsigned& frame = lastKeyFrame_[i->first];
  165. track->GetKeyFrameIndex(time_, frame);
  166. // Check if next frame to interpolate to is valid, or if wrapping is needed (looping animation only)
  167. unsigned nextFrame = frame + 1;
  168. bool interpolate = true;
  169. if (nextFrame >= track->keyFrames_.size())
  170. {
  171. if (!looped_)
  172. {
  173. nextFrame = frame;
  174. interpolate = false;
  175. }
  176. else
  177. nextFrame = 0;
  178. }
  179. const AnimationKeyFrame* keyFrame = &track->keyFrames_[frame];
  180. unsigned char channelMask = track->channelMask_;
  181. if (!interpolate)
  182. {
  183. // No interpolation, full weight
  184. if (channelMask & CHANNEL_POSITION)
  185. boneNode->SetPosition(keyFrame->position_);
  186. if (channelMask & CHANNEL_ROTATION)
  187. boneNode->SetRotation(keyFrame->rotation_);
  188. if (channelMask & CHANNEL_SCALE)
  189. boneNode->SetScale(keyFrame->scale_);
  190. }
  191. else
  192. {
  193. const AnimationKeyFrame* nextKeyFrame = &track->keyFrames_[nextFrame];
  194. float timeInterval = nextKeyFrame->time_ - keyFrame->time_;
  195. if (timeInterval < 0.0f)
  196. timeInterval += animation_->GetLength();
  197. float t = timeInterval > 0.0f ? (time_ - keyFrame->time_) / timeInterval : 1.0f;
  198. // Interpolation, full weight
  199. if (channelMask & CHANNEL_POSITION)
  200. boneNode->SetPosition(keyFrame->position_.Lerp(nextKeyFrame->position_, t));
  201. if (channelMask & CHANNEL_ROTATION)
  202. {
  203. if (!useNlerp_)
  204. boneNode->SetRotation(keyFrame->rotation_.Slerp(nextKeyFrame->rotation_, t));
  205. else
  206. boneNode->SetRotation(keyFrame->rotation_.NlerpFast(nextKeyFrame->rotation_, t));
  207. }
  208. if (channelMask & CHANNEL_SCALE)
  209. boneNode->SetScale(keyFrame->scale_.Lerp(nextKeyFrame->scale_, t));
  210. }
  211. }
  212. }
  213. else
  214. {
  215. for (std::map<unsigned, Bone*>::const_iterator i = trackToBoneMap_.begin(); i != trackToBoneMap_.end(); ++i)
  216. {
  217. const AnimationTrack* track = animation_->GetTrack(i->first);
  218. Bone* bone = i->second;
  219. Node* boneNode = bone->node_;
  220. if ((!boneNode) || (!bone->animated_) || (!track->keyFrames_.size()))
  221. continue;
  222. unsigned& frame = lastKeyFrame_[i->first];
  223. track->GetKeyFrameIndex(time_, frame);
  224. // Check if next frame to interpolate to is valid, or if wrapping is needed (looping animation only)
  225. unsigned nextFrame = frame + 1;
  226. bool interpolate = true;
  227. if (nextFrame >= track->keyFrames_.size())
  228. {
  229. if (!looped_)
  230. {
  231. nextFrame = frame;
  232. interpolate = false;
  233. }
  234. else
  235. nextFrame = 0;
  236. }
  237. const AnimationKeyFrame* keyFrame = &track->keyFrames_[frame];
  238. unsigned char channelMask = track->channelMask_;
  239. if (!interpolate)
  240. {
  241. // No interpolation, blend between old transform & animation
  242. if (channelMask & CHANNEL_POSITION)
  243. boneNode->SetPosition(boneNode->GetPosition().Lerp(keyFrame->position_, weight_));
  244. if (channelMask & CHANNEL_ROTATION)
  245. {
  246. if (!useNlerp_)
  247. boneNode->SetRotation(boneNode->GetRotation().Slerp(keyFrame->rotation_, weight_));
  248. else
  249. boneNode->SetRotation(boneNode->GetRotation().NlerpFast(keyFrame->rotation_, weight_));
  250. }
  251. if (channelMask & CHANNEL_SCALE)
  252. boneNode->SetScale(boneNode->GetScale().Lerp(keyFrame->scale_, weight_));
  253. }
  254. else
  255. {
  256. const AnimationKeyFrame* nextKeyFrame = &track->keyFrames_[nextFrame];
  257. float timeInterval = nextKeyFrame->time_ - keyFrame->time_;
  258. if (timeInterval < 0.0f)
  259. timeInterval += animation_->GetLength();
  260. float t = timeInterval > 0.0f ? (time_ - keyFrame->time_) / timeInterval : 1.0f;
  261. // Interpolation, blend between old transform & animation
  262. if (channelMask & CHANNEL_POSITION)
  263. {
  264. boneNode->SetPosition(boneNode->GetPosition().Lerp(
  265. keyFrame->position_.Lerp(nextKeyFrame->position_, t), weight_));
  266. }
  267. if (channelMask & CHANNEL_ROTATION)
  268. {
  269. if (!useNlerp_)
  270. {
  271. boneNode->SetRotation(boneNode->GetRotation().Slerp(
  272. keyFrame->rotation_.Slerp(nextKeyFrame->rotation_, t), weight_));
  273. }
  274. else
  275. {
  276. boneNode->SetRotation(boneNode->GetRotation().NlerpFast(
  277. keyFrame->rotation_.NlerpFast(nextKeyFrame->rotation_, t), weight_));
  278. }
  279. }
  280. if (channelMask & CHANNEL_SCALE)
  281. {
  282. boneNode->SetScale(boneNode->GetScale().Lerp(
  283. keyFrame->scale_.Lerp(nextKeyFrame->scale_, t), weight_));
  284. }
  285. }
  286. }
  287. }
  288. }