AnimationState.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  1. //
  2. // Copyright (c) 2008-2013 the Urho3D project.
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #include "Precompiled.h"
  23. #include "AnimatedModel.h"
  24. #include "Animation.h"
  25. #include "AnimationState.h"
  26. #include "Deserializer.h"
  27. #include "DrawableEvents.h"
  28. #include "Log.h"
  29. #include "Serializer.h"
  30. #include "DebugNew.h"
  31. namespace Urho3D
  32. {
  33. AnimationState::AnimationState(AnimatedModel* model, Animation* animation) :
  34. model_(model),
  35. animation_(animation),
  36. startBone_(0),
  37. looped_(false),
  38. weight_(0.0f),
  39. time_(0.0f),
  40. layer_(0)
  41. {
  42. SetStartBone(0);
  43. if (animation_)
  44. {
  45. // Setup a cache for last keyframe of each track
  46. lastKeyFrame_.Resize(animation_->GetNumTracks());
  47. }
  48. for (unsigned i = 0; i < lastKeyFrame_.Size(); ++i)
  49. lastKeyFrame_[i] = 0;
  50. }
  51. AnimationState::AnimationState(Node* node, Animation* animation) :
  52. node_(node),
  53. animation_(animation),
  54. startBone_(0),
  55. looped_(false),
  56. weight_(1.0f),
  57. time_(0.0f),
  58. layer_(0)
  59. {
  60. if (animation_)
  61. {
  62. // Setup a cache for last keyframe of each track
  63. lastKeyFrame_.Resize(animation_->GetNumTracks());
  64. // Setup animation track to scene node mapping
  65. if (node_)
  66. {
  67. const Vector<AnimationTrack>& tracks = animation_->GetTracks();
  68. for (unsigned i = 0; i < tracks.Size(); ++i)
  69. {
  70. const StringHash& nameHash = tracks[i].nameHash_;
  71. if (node_->GetNameHash() == nameHash || tracks.Size() == 1)
  72. trackToNodeMap_[i] = node_;
  73. else
  74. {
  75. Node* targetNode = node_->GetChild(nameHash, true);
  76. if (targetNode)
  77. trackToNodeMap_[i] = targetNode;
  78. else
  79. LOGWARNING("Node " + tracks[i].name_ + " not found for node animation " + animation_->GetName());
  80. }
  81. }
  82. }
  83. }
  84. for (unsigned i = 0; i < lastKeyFrame_.Size(); ++i)
  85. lastKeyFrame_[i] = 0;
  86. }
  87. AnimationState::~AnimationState()
  88. {
  89. }
  90. void AnimationState::SetStartBone(Bone* startBone)
  91. {
  92. if (!model_ || !animation_)
  93. return;
  94. Skeleton& skeleton = model_->GetSkeleton();
  95. if (!startBone)
  96. {
  97. Bone* rootBone = skeleton.GetRootBone();
  98. if (!rootBone)
  99. return;
  100. startBone = rootBone;
  101. }
  102. // Do not reassign if the start bone did not actually change, and we already have valid bone nodes
  103. if (startBone == startBone_ && !trackToBoneMap_.Empty())
  104. return;
  105. startBone_ = startBone;
  106. trackToBoneMap_.Clear();
  107. if (!startBone->node_)
  108. return;
  109. const Vector<AnimationTrack>& tracks = animation_->GetTracks();
  110. for (unsigned i = 0; i < tracks.Size(); ++i)
  111. {
  112. // Include those tracks that are either the start bone itself, or its children
  113. Bone* trackBone = 0;
  114. const StringHash& nameHash = tracks[i].nameHash_;
  115. if (nameHash == startBone->nameHash_)
  116. trackBone = startBone;
  117. else
  118. {
  119. Node* trackBoneNode = startBone->node_->GetChild(nameHash, true);
  120. if (trackBoneNode)
  121. trackBone = skeleton.GetBone(nameHash);
  122. }
  123. if (trackBone)
  124. trackToBoneMap_[i] = trackBone;
  125. }
  126. model_->MarkAnimationDirty();
  127. }
  128. void AnimationState::SetLooped(bool looped)
  129. {
  130. looped_ = looped;
  131. }
  132. void AnimationState::SetWeight(float weight)
  133. {
  134. // Weight can only be set in model mode. In node animation it is hardcoded to full
  135. if (model_)
  136. {
  137. weight = Clamp(weight, 0.0f, 1.0f);
  138. if (weight != weight_)
  139. {
  140. weight_ = weight;
  141. model_->MarkAnimationDirty();
  142. }
  143. }
  144. }
  145. void AnimationState::SetTime(float time)
  146. {
  147. if (!animation_)
  148. return;
  149. time = Clamp(time, 0.0f, animation_->GetLength());
  150. if (time != time_)
  151. {
  152. time_ = time;
  153. if (model_)
  154. model_->MarkAnimationDirty();
  155. }
  156. }
  157. void AnimationState::AddWeight(float delta)
  158. {
  159. if (delta == 0.0f)
  160. return;
  161. SetWeight(GetWeight() + delta);
  162. }
  163. void AnimationState::AddTime(float delta)
  164. {
  165. if (!animation_ || (!model_ && !node_))
  166. return;
  167. float length = animation_->GetLength();
  168. if (delta == 0.0f || length == 0.0f)
  169. return;
  170. float oldTime = GetTime();
  171. float time = oldTime + delta;
  172. if (looped_)
  173. {
  174. while (time >= length)
  175. time -= length;
  176. while (time < 0.0f)
  177. time += length;
  178. }
  179. SetTime(time);
  180. // Process animation triggers
  181. if (animation_->GetNumTriggers())
  182. {
  183. if (delta > 0.0f)
  184. {
  185. if (oldTime > time)
  186. oldTime -= length;
  187. }
  188. if (delta < 0.0f)
  189. {
  190. if (time > oldTime)
  191. time -= length;
  192. }
  193. if (oldTime > time)
  194. Swap(oldTime, time);
  195. const Vector<AnimationTriggerPoint>& triggers = animation_->GetTriggers();
  196. for (Vector<AnimationTriggerPoint>::ConstIterator i = triggers.Begin(); i != triggers.End(); ++i)
  197. {
  198. if (oldTime <= i->time_ && time > i->time_)
  199. {
  200. using namespace AnimationTrigger;
  201. Node* senderNode = model_ ? model_->GetNode() : node_;
  202. VariantMap eventData;
  203. eventData[P_NODE] = (void*)senderNode;
  204. eventData[P_NAME] = animation_->GetAnimationName();
  205. eventData[P_TIME] = i->time_;
  206. eventData[P_DATA] = i->data_;
  207. senderNode->SendEvent(E_ANIMATIONTRIGGER, eventData);
  208. }
  209. }
  210. }
  211. }
  212. void AnimationState::SetLayer(unsigned char layer)
  213. {
  214. if (layer != layer_)
  215. {
  216. layer_ = layer;
  217. if (model_)
  218. model_->MarkAnimationOrderDirty();
  219. }
  220. }
  221. AnimatedModel* AnimationState::GetModel() const
  222. {
  223. return model_;
  224. }
  225. Node* AnimationState::GetNode() const
  226. {
  227. return node_;
  228. }
  229. Bone* AnimationState::GetStartBone() const
  230. {
  231. return model_ ? startBone_ : 0;
  232. }
  233. float AnimationState::GetLength() const
  234. {
  235. return animation_ ? animation_->GetLength() : 0.0f;
  236. }
  237. void AnimationState::Apply()
  238. {
  239. if (!animation_ || !IsEnabled())
  240. return;
  241. if (model_)
  242. ApplyToModel();
  243. else
  244. ApplyToNodes();
  245. }
  246. void AnimationState::ApplyToModel()
  247. {
  248. // Check first if full weight or blending
  249. if (Equals(weight_, 1.0f))
  250. {
  251. for (HashMap<unsigned, Bone*>::ConstIterator i = trackToBoneMap_.Begin(); i != trackToBoneMap_.End(); ++i)
  252. {
  253. Bone* bone = i->second_;
  254. Node* boneNode = bone->node_;
  255. if (!boneNode || !bone->animated_)
  256. continue;
  257. ApplyTrackToNodeFullWeight(i->first_, boneNode);
  258. }
  259. }
  260. else
  261. {
  262. for (HashMap<unsigned, Bone*>::ConstIterator i = trackToBoneMap_.Begin(); i != trackToBoneMap_.End(); ++i)
  263. {
  264. Bone* bone = i->second_;
  265. Node* boneNode = bone->node_;
  266. if (!boneNode || !bone->animated_)
  267. continue;
  268. ApplyTrackToNodeBlended(i->first_, boneNode);
  269. }
  270. }
  271. }
  272. void AnimationState::ApplyToNodes()
  273. {
  274. // When applying to a node hierarchy, can only use full weight (nothing to blend to)
  275. for (HashMap<unsigned, WeakPtr<Node> >::ConstIterator i = trackToNodeMap_.Begin(); i != trackToNodeMap_.End(); ++i)
  276. {
  277. Node* node = i->second_;
  278. if (!node)
  279. continue;
  280. ApplyTrackToNodeFullWeight(i->first_, node);
  281. }
  282. }
  283. void AnimationState::ApplyTrackToNodeFullWeight(unsigned index, Node* node)
  284. {
  285. const AnimationTrack* track = animation_->GetTrack(index);
  286. if (track->keyFrames_.Empty())
  287. return;
  288. unsigned& frame = lastKeyFrame_[index];
  289. track->GetKeyFrameIndex(time_, frame);
  290. // Check if next frame to interpolate to is valid, or if wrapping is needed (looping animation only)
  291. unsigned nextFrame = frame + 1;
  292. bool interpolate = true;
  293. if (nextFrame >= track->keyFrames_.Size())
  294. {
  295. if (!looped_)
  296. {
  297. nextFrame = frame;
  298. interpolate = false;
  299. }
  300. else
  301. nextFrame = 0;
  302. }
  303. const AnimationKeyFrame* keyFrame = &track->keyFrames_[frame];
  304. unsigned char channelMask = track->channelMask_;
  305. if (!interpolate)
  306. {
  307. // No interpolation, full weight
  308. if (channelMask & CHANNEL_POSITION)
  309. node->SetPosition(keyFrame->position_);
  310. if (channelMask & CHANNEL_ROTATION)
  311. node->SetRotation(keyFrame->rotation_);
  312. if (channelMask & CHANNEL_SCALE)
  313. node->SetScale(keyFrame->scale_);
  314. }
  315. else
  316. {
  317. const AnimationKeyFrame* nextKeyFrame = &track->keyFrames_[nextFrame];
  318. float timeInterval = nextKeyFrame->time_ - keyFrame->time_;
  319. if (timeInterval < 0.0f)
  320. timeInterval += animation_->GetLength();
  321. float t = timeInterval > 0.0f ? (time_ - keyFrame->time_) / timeInterval : 1.0f;
  322. // Interpolation, full weight
  323. if (channelMask & CHANNEL_POSITION)
  324. node->SetPosition(keyFrame->position_.Lerp(nextKeyFrame->position_, t));
  325. if (channelMask & CHANNEL_ROTATION)
  326. node->SetRotation(keyFrame->rotation_.Slerp(nextKeyFrame->rotation_, t));
  327. if (channelMask & CHANNEL_SCALE)
  328. node->SetScale(keyFrame->scale_.Lerp(nextKeyFrame->scale_, t));
  329. }
  330. }
  331. void AnimationState::ApplyTrackToNodeBlended(unsigned index, Node* node)
  332. {
  333. const AnimationTrack* track = animation_->GetTrack(index);
  334. if (track->keyFrames_.Empty())
  335. return;
  336. unsigned& frame = lastKeyFrame_[index];
  337. track->GetKeyFrameIndex(time_, frame);
  338. // Check if next frame to interpolate to is valid, or if wrapping is needed (looping animation only)
  339. unsigned nextFrame = frame + 1;
  340. bool interpolate = true;
  341. if (nextFrame >= track->keyFrames_.Size())
  342. {
  343. if (!looped_)
  344. {
  345. nextFrame = frame;
  346. interpolate = false;
  347. }
  348. else
  349. nextFrame = 0;
  350. }
  351. const AnimationKeyFrame* keyFrame = &track->keyFrames_[frame];
  352. unsigned char channelMask = track->channelMask_;
  353. if (!interpolate)
  354. {
  355. // No interpolation, blend between old transform & animation
  356. if (channelMask & CHANNEL_POSITION)
  357. node->SetPosition(node->GetPosition().Lerp(keyFrame->position_, weight_));
  358. if (channelMask & CHANNEL_ROTATION)
  359. node->SetRotation(node->GetRotation().Slerp(keyFrame->rotation_, weight_));
  360. if (channelMask & CHANNEL_SCALE)
  361. node->SetScale(node->GetScale().Lerp(keyFrame->scale_, weight_));
  362. }
  363. else
  364. {
  365. const AnimationKeyFrame* nextKeyFrame = &track->keyFrames_[nextFrame];
  366. float timeInterval = nextKeyFrame->time_ - keyFrame->time_;
  367. if (timeInterval < 0.0f)
  368. timeInterval += animation_->GetLength();
  369. float t = timeInterval > 0.0f ? (time_ - keyFrame->time_) / timeInterval : 1.0f;
  370. // Interpolation, blend between old transform & animation
  371. if (channelMask & CHANNEL_POSITION)
  372. {
  373. node->SetPosition(node->GetPosition().Lerp(
  374. keyFrame->position_.Lerp(nextKeyFrame->position_, t), weight_));
  375. }
  376. if (channelMask & CHANNEL_ROTATION)
  377. {
  378. node->SetRotation(node->GetRotation().Slerp(
  379. keyFrame->rotation_.Slerp(nextKeyFrame->rotation_, t), weight_));
  380. }
  381. if (channelMask & CHANNEL_SCALE)
  382. {
  383. node->SetScale(node->GetScale().Lerp(
  384. keyFrame->scale_.Lerp(nextKeyFrame->scale_, t), weight_));
  385. }
  386. }
  387. }
  388. }