AnimationState.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505
  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. AnimationStateTrack::AnimationStateTrack() :
  34. track_(0),
  35. bone_(0),
  36. weight_(1.0f),
  37. keyFrame_(0)
  38. {
  39. }
  40. AnimationStateTrack::~AnimationStateTrack()
  41. {
  42. }
  43. AnimationState::AnimationState(AnimatedModel* model, Animation* animation) :
  44. model_(model),
  45. animation_(animation),
  46. startBone_(0),
  47. looped_(false),
  48. weight_(0.0f),
  49. time_(0.0f),
  50. layer_(0)
  51. {
  52. // Set default start bone (use all tracks.)
  53. SetStartBone(0);
  54. }
  55. AnimationState::AnimationState(Node* node, Animation* animation) :
  56. node_(node),
  57. animation_(animation),
  58. startBone_(0),
  59. looped_(false),
  60. weight_(1.0f),
  61. time_(0.0f),
  62. layer_(0)
  63. {
  64. if (animation_)
  65. {
  66. // Setup animation track to scene node mapping
  67. if (node_)
  68. {
  69. const Vector<AnimationTrack>& tracks = animation_->GetTracks();
  70. stateTracks_.Clear();
  71. for (unsigned i = 0; i < tracks.Size(); ++i)
  72. {
  73. const StringHash& nameHash = tracks[i].nameHash_;
  74. AnimationStateTrack stateTrack;
  75. stateTrack.track_ = &tracks[i];
  76. if (node_->GetNameHash() == nameHash || tracks.Size() == 1)
  77. stateTrack.node_ = node_;
  78. else
  79. {
  80. Node* targetNode = node_->GetChild(nameHash, true);
  81. if (targetNode)
  82. stateTrack.node_ = targetNode;
  83. else
  84. LOGWARNING("Node " + tracks[i].name_ + " not found for node animation " + animation_->GetName());
  85. }
  86. if (stateTrack.node_)
  87. stateTracks_.Push(stateTrack);
  88. }
  89. }
  90. }
  91. }
  92. AnimationState::~AnimationState()
  93. {
  94. }
  95. void AnimationState::SetStartBone(Bone* startBone)
  96. {
  97. if (!model_ || !animation_)
  98. return;
  99. Skeleton& skeleton = model_->GetSkeleton();
  100. if (!startBone)
  101. {
  102. Bone* rootBone = skeleton.GetRootBone();
  103. if (!rootBone)
  104. return;
  105. startBone = rootBone;
  106. }
  107. // Do not reassign if the start bone did not actually change, and we already have valid bone nodes
  108. if (startBone == startBone_ && !stateTracks_.Empty())
  109. return;
  110. startBone_ = startBone;
  111. const Vector<AnimationTrack>& tracks = animation_->GetTracks();
  112. stateTracks_.Clear();
  113. if (!startBone->node_)
  114. return;
  115. for (unsigned i = 0; i < tracks.Size(); ++i)
  116. {
  117. AnimationStateTrack stateTrack;
  118. stateTrack.track_ = &tracks[i];
  119. // Include those tracks that are either the start bone itself, or its children
  120. Bone* trackBone = 0;
  121. const StringHash& nameHash = tracks[i].nameHash_;
  122. if (nameHash == startBone->nameHash_)
  123. trackBone = startBone;
  124. else
  125. {
  126. Node* trackBoneNode = startBone->node_->GetChild(nameHash, true);
  127. if (trackBoneNode)
  128. trackBone = skeleton.GetBone(nameHash);
  129. }
  130. if (trackBone && trackBone->node_)
  131. {
  132. stateTrack.bone_ = trackBone;
  133. stateTrack.node_ = trackBone->node_;
  134. stateTracks_.Push(stateTrack);
  135. }
  136. }
  137. model_->MarkAnimationDirty();
  138. }
  139. void AnimationState::SetLooped(bool looped)
  140. {
  141. looped_ = looped;
  142. }
  143. void AnimationState::SetWeight(float weight)
  144. {
  145. // Weight can only be set in model mode. In node animation it is hardcoded to full
  146. if (model_)
  147. {
  148. weight = Clamp(weight, 0.0f, 1.0f);
  149. if (weight != weight_)
  150. {
  151. weight_ = weight;
  152. model_->MarkAnimationDirty();
  153. }
  154. }
  155. }
  156. void AnimationState::SetTime(float time)
  157. {
  158. if (!animation_)
  159. return;
  160. time = Clamp(time, 0.0f, animation_->GetLength());
  161. if (time != time_)
  162. {
  163. time_ = time;
  164. if (model_)
  165. model_->MarkAnimationDirty();
  166. }
  167. }
  168. void AnimationState::SetBoneWeight(unsigned index, float weight)
  169. {
  170. if (index >= stateTracks_.Size())
  171. return;
  172. stateTracks_[index].weight_ = Clamp(weight, 0.0f, 1.0f);
  173. }
  174. void AnimationState::SetBoneWeight(const String& name, float weight)
  175. {
  176. SetBoneWeight(GetTrackIndex(name), weight);
  177. }
  178. void AnimationState::SetBoneWeight(StringHash nameHash, float weight)
  179. {
  180. SetBoneWeight(GetTrackIndex(nameHash), weight);
  181. }
  182. void AnimationState::AddWeight(float delta)
  183. {
  184. if (delta == 0.0f)
  185. return;
  186. SetWeight(GetWeight() + delta);
  187. }
  188. void AnimationState::AddTime(float delta)
  189. {
  190. if (!animation_ || (!model_ && !node_))
  191. return;
  192. float length = animation_->GetLength();
  193. if (delta == 0.0f || length == 0.0f)
  194. return;
  195. float oldTime = GetTime();
  196. float time = oldTime + delta;
  197. if (looped_)
  198. {
  199. while (time >= length)
  200. time -= length;
  201. while (time < 0.0f)
  202. time += length;
  203. }
  204. SetTime(time);
  205. // Process animation triggers
  206. if (animation_->GetNumTriggers())
  207. {
  208. if (delta > 0.0f)
  209. {
  210. if (oldTime > time)
  211. oldTime -= length;
  212. }
  213. if (delta < 0.0f)
  214. {
  215. if (time > oldTime)
  216. time -= length;
  217. }
  218. if (oldTime > time)
  219. Swap(oldTime, time);
  220. const Vector<AnimationTriggerPoint>& triggers = animation_->GetTriggers();
  221. for (Vector<AnimationTriggerPoint>::ConstIterator i = triggers.Begin(); i != triggers.End(); ++i)
  222. {
  223. if (oldTime <= i->time_ && time > i->time_)
  224. {
  225. using namespace AnimationTrigger;
  226. Node* senderNode = model_ ? model_->GetNode() : node_;
  227. VariantMap eventData;
  228. eventData[P_NODE] = (void*)senderNode;
  229. eventData[P_NAME] = animation_->GetAnimationName();
  230. eventData[P_TIME] = i->time_;
  231. eventData[P_DATA] = i->data_;
  232. senderNode->SendEvent(E_ANIMATIONTRIGGER, eventData);
  233. }
  234. }
  235. }
  236. }
  237. void AnimationState::SetLayer(unsigned char layer)
  238. {
  239. if (layer != layer_)
  240. {
  241. layer_ = layer;
  242. if (model_)
  243. model_->MarkAnimationOrderDirty();
  244. }
  245. }
  246. AnimatedModel* AnimationState::GetModel() const
  247. {
  248. return model_;
  249. }
  250. Node* AnimationState::GetNode() const
  251. {
  252. return node_;
  253. }
  254. Bone* AnimationState::GetStartBone() const
  255. {
  256. return model_ ? startBone_ : 0;
  257. }
  258. float AnimationState::GetBoneWeight(unsigned index) const
  259. {
  260. return index < stateTracks_.Size() ? stateTracks_[index].weight_ : 0.0f;
  261. }
  262. float AnimationState::GetBoneWeight(const String& name) const
  263. {
  264. return GetBoneWeight(GetTrackIndex(name));
  265. }
  266. float AnimationState::GetBoneWeight(StringHash nameHash) const
  267. {
  268. return GetBoneWeight(GetTrackIndex(nameHash));
  269. }
  270. unsigned AnimationState::GetTrackIndex(const String& name) const
  271. {
  272. for (unsigned i = 0; i < stateTracks_.Size(); ++i)
  273. {
  274. Node* node = stateTracks_[i].node_;
  275. if (node && node->GetName() == name)
  276. return i;
  277. }
  278. return M_MAX_UNSIGNED;
  279. }
  280. unsigned AnimationState::GetTrackIndex(StringHash nameHash) const
  281. {
  282. for (unsigned i = 0; i < stateTracks_.Size(); ++i)
  283. {
  284. Node* node = stateTracks_[i].node_;
  285. if (node && node->GetNameHash() == nameHash)
  286. return i;
  287. }
  288. return M_MAX_UNSIGNED;
  289. }
  290. float AnimationState::GetLength() const
  291. {
  292. return animation_ ? animation_->GetLength() : 0.0f;
  293. }
  294. void AnimationState::Apply()
  295. {
  296. if (!animation_ || !IsEnabled())
  297. return;
  298. if (model_)
  299. ApplyToModel();
  300. else
  301. ApplyToNodes();
  302. }
  303. void AnimationState::ApplyToModel()
  304. {
  305. for (Vector<AnimationStateTrack>::Iterator i = stateTracks_.Begin(); i != stateTracks_.End(); ++i)
  306. {
  307. AnimationStateTrack& stateTrack = *i;
  308. float finalWeight = weight_ * stateTrack.weight_;
  309. // Do not apply if zero effective weight or the bone has animation disabled
  310. if (Equals(finalWeight, 0.0f) || !stateTrack.bone_->animated_)
  311. continue;
  312. if (Equals(finalWeight, 1.0f))
  313. ApplyTrackFullWeight(stateTrack);
  314. else
  315. ApplyTrackBlended(stateTrack, finalWeight);
  316. }
  317. }
  318. void AnimationState::ApplyToNodes()
  319. {
  320. // When applying to a node hierarchy, can only use full weight (nothing to blend to)
  321. for (Vector<AnimationStateTrack>::Iterator i = stateTracks_.Begin(); i != stateTracks_.End(); ++i)
  322. ApplyTrackFullWeight(*i);
  323. }
  324. void AnimationState::ApplyTrackFullWeight(AnimationStateTrack& stateTrack)
  325. {
  326. const AnimationTrack* track = stateTrack.track_;
  327. Node* node = stateTrack.node_;
  328. if (track->keyFrames_.Empty() || !node)
  329. return;
  330. unsigned& frame = stateTrack.keyFrame_;
  331. track->GetKeyFrameIndex(time_, frame);
  332. // Check if next frame to interpolate to is valid, or if wrapping is needed (looping animation only)
  333. unsigned nextFrame = frame + 1;
  334. bool interpolate = true;
  335. if (nextFrame >= track->keyFrames_.Size())
  336. {
  337. if (!looped_)
  338. {
  339. nextFrame = frame;
  340. interpolate = false;
  341. }
  342. else
  343. nextFrame = 0;
  344. }
  345. const AnimationKeyFrame* keyFrame = &track->keyFrames_[frame];
  346. unsigned char channelMask = track->channelMask_;
  347. if (!interpolate)
  348. {
  349. // No interpolation, full weight
  350. if (channelMask & CHANNEL_POSITION)
  351. node->SetPosition(keyFrame->position_);
  352. if (channelMask & CHANNEL_ROTATION)
  353. node->SetRotation(keyFrame->rotation_);
  354. if (channelMask & CHANNEL_SCALE)
  355. node->SetScale(keyFrame->scale_);
  356. }
  357. else
  358. {
  359. const AnimationKeyFrame* nextKeyFrame = &track->keyFrames_[nextFrame];
  360. float timeInterval = nextKeyFrame->time_ - keyFrame->time_;
  361. if (timeInterval < 0.0f)
  362. timeInterval += animation_->GetLength();
  363. float t = timeInterval > 0.0f ? (time_ - keyFrame->time_) / timeInterval : 1.0f;
  364. // Interpolation, full weight
  365. if (channelMask & CHANNEL_POSITION)
  366. node->SetPosition(keyFrame->position_.Lerp(nextKeyFrame->position_, t));
  367. if (channelMask & CHANNEL_ROTATION)
  368. node->SetRotation(keyFrame->rotation_.Slerp(nextKeyFrame->rotation_, t));
  369. if (channelMask & CHANNEL_SCALE)
  370. node->SetScale(keyFrame->scale_.Lerp(nextKeyFrame->scale_, t));
  371. }
  372. }
  373. void AnimationState::ApplyTrackBlended(AnimationStateTrack& stateTrack, float weight)
  374. {
  375. const AnimationTrack* track = stateTrack.track_;
  376. Node* node = stateTrack.node_;
  377. if (track->keyFrames_.Empty() || !node)
  378. return;
  379. unsigned& frame = stateTrack.keyFrame_;
  380. track->GetKeyFrameIndex(time_, frame);
  381. // Check if next frame to interpolate to is valid, or if wrapping is needed (looping animation only)
  382. unsigned nextFrame = frame + 1;
  383. bool interpolate = true;
  384. if (nextFrame >= track->keyFrames_.Size())
  385. {
  386. if (!looped_)
  387. {
  388. nextFrame = frame;
  389. interpolate = false;
  390. }
  391. else
  392. nextFrame = 0;
  393. }
  394. const AnimationKeyFrame* keyFrame = &track->keyFrames_[frame];
  395. unsigned char channelMask = track->channelMask_;
  396. if (!interpolate)
  397. {
  398. // No interpolation, blend between old transform & animation
  399. if (channelMask & CHANNEL_POSITION)
  400. node->SetPosition(node->GetPosition().Lerp(keyFrame->position_, weight));
  401. if (channelMask & CHANNEL_ROTATION)
  402. node->SetRotation(node->GetRotation().Slerp(keyFrame->rotation_, weight));
  403. if (channelMask & CHANNEL_SCALE)
  404. node->SetScale(node->GetScale().Lerp(keyFrame->scale_, weight));
  405. }
  406. else
  407. {
  408. const AnimationKeyFrame* nextKeyFrame = &track->keyFrames_[nextFrame];
  409. float timeInterval = nextKeyFrame->time_ - keyFrame->time_;
  410. if (timeInterval < 0.0f)
  411. timeInterval += animation_->GetLength();
  412. float t = timeInterval > 0.0f ? (time_ - keyFrame->time_) / timeInterval : 1.0f;
  413. // Interpolation, blend between old transform & animation
  414. if (channelMask & CHANNEL_POSITION)
  415. {
  416. node->SetPosition(node->GetPosition().Lerp(
  417. keyFrame->position_.Lerp(nextKeyFrame->position_, t), weight));
  418. }
  419. if (channelMask & CHANNEL_ROTATION)
  420. {
  421. node->SetRotation(node->GetRotation().Slerp(
  422. keyFrame->rotation_.Slerp(nextKeyFrame->rotation_, t), weight));
  423. }
  424. if (channelMask & CHANNEL_SCALE)
  425. {
  426. node->SetScale(node->GetScale().Lerp(
  427. keyFrame->scale_.Lerp(nextKeyFrame->scale_, t), weight));
  428. }
  429. }
  430. }
  431. }