AnimationState.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512
  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. weight = Clamp(weight, 0.0f, 1.0f);
  173. if (weight != stateTracks_[index].weight_)
  174. {
  175. stateTracks_[index].weight_ = weight;
  176. if (model_)
  177. model_->MarkAnimationDirty();
  178. }
  179. }
  180. void AnimationState::SetBoneWeight(const String& name, float weight)
  181. {
  182. SetBoneWeight(GetTrackIndex(name), weight);
  183. }
  184. void AnimationState::SetBoneWeight(StringHash nameHash, float weight)
  185. {
  186. SetBoneWeight(GetTrackIndex(nameHash), weight);
  187. }
  188. void AnimationState::AddWeight(float delta)
  189. {
  190. if (delta == 0.0f)
  191. return;
  192. SetWeight(GetWeight() + delta);
  193. }
  194. void AnimationState::AddTime(float delta)
  195. {
  196. if (!animation_ || (!model_ && !node_))
  197. return;
  198. float length = animation_->GetLength();
  199. if (delta == 0.0f || length == 0.0f)
  200. return;
  201. float oldTime = GetTime();
  202. float time = oldTime + delta;
  203. if (looped_)
  204. {
  205. while (time >= length)
  206. time -= length;
  207. while (time < 0.0f)
  208. time += length;
  209. }
  210. SetTime(time);
  211. // Process animation triggers
  212. if (animation_->GetNumTriggers())
  213. {
  214. if (delta > 0.0f)
  215. {
  216. if (oldTime > time)
  217. oldTime -= length;
  218. }
  219. if (delta < 0.0f)
  220. {
  221. if (time > oldTime)
  222. time -= length;
  223. }
  224. if (oldTime > time)
  225. Swap(oldTime, time);
  226. const Vector<AnimationTriggerPoint>& triggers = animation_->GetTriggers();
  227. for (Vector<AnimationTriggerPoint>::ConstIterator i = triggers.Begin(); i != triggers.End(); ++i)
  228. {
  229. if (oldTime <= i->time_ && time > i->time_)
  230. {
  231. using namespace AnimationTrigger;
  232. Node* senderNode = model_ ? model_->GetNode() : node_;
  233. VariantMap eventData;
  234. eventData[P_NODE] = (void*)senderNode;
  235. eventData[P_NAME] = animation_->GetAnimationName();
  236. eventData[P_TIME] = i->time_;
  237. eventData[P_DATA] = i->data_;
  238. senderNode->SendEvent(E_ANIMATIONTRIGGER, eventData);
  239. }
  240. }
  241. }
  242. }
  243. void AnimationState::SetLayer(unsigned char layer)
  244. {
  245. if (layer != layer_)
  246. {
  247. layer_ = layer;
  248. if (model_)
  249. model_->MarkAnimationOrderDirty();
  250. }
  251. }
  252. AnimatedModel* AnimationState::GetModel() const
  253. {
  254. return model_;
  255. }
  256. Node* AnimationState::GetNode() const
  257. {
  258. return node_;
  259. }
  260. Bone* AnimationState::GetStartBone() const
  261. {
  262. return model_ ? startBone_ : 0;
  263. }
  264. float AnimationState::GetBoneWeight(unsigned index) const
  265. {
  266. return index < stateTracks_.Size() ? stateTracks_[index].weight_ : 0.0f;
  267. }
  268. float AnimationState::GetBoneWeight(const String& name) const
  269. {
  270. return GetBoneWeight(GetTrackIndex(name));
  271. }
  272. float AnimationState::GetBoneWeight(StringHash nameHash) const
  273. {
  274. return GetBoneWeight(GetTrackIndex(nameHash));
  275. }
  276. unsigned AnimationState::GetTrackIndex(const String& name) const
  277. {
  278. for (unsigned i = 0; i < stateTracks_.Size(); ++i)
  279. {
  280. Node* node = stateTracks_[i].node_;
  281. if (node && node->GetName() == name)
  282. return i;
  283. }
  284. return M_MAX_UNSIGNED;
  285. }
  286. unsigned AnimationState::GetTrackIndex(StringHash nameHash) const
  287. {
  288. for (unsigned i = 0; i < stateTracks_.Size(); ++i)
  289. {
  290. Node* node = stateTracks_[i].node_;
  291. if (node && node->GetNameHash() == nameHash)
  292. return i;
  293. }
  294. return M_MAX_UNSIGNED;
  295. }
  296. float AnimationState::GetLength() const
  297. {
  298. return animation_ ? animation_->GetLength() : 0.0f;
  299. }
  300. void AnimationState::Apply()
  301. {
  302. if (!animation_ || !IsEnabled())
  303. return;
  304. if (model_)
  305. ApplyToModel();
  306. else
  307. ApplyToNodes();
  308. }
  309. void AnimationState::ApplyToModel()
  310. {
  311. for (Vector<AnimationStateTrack>::Iterator i = stateTracks_.Begin(); i != stateTracks_.End(); ++i)
  312. {
  313. AnimationStateTrack& stateTrack = *i;
  314. float finalWeight = weight_ * stateTrack.weight_;
  315. // Do not apply if zero effective weight or the bone has animation disabled
  316. if (Equals(finalWeight, 0.0f) || !stateTrack.bone_->animated_)
  317. continue;
  318. if (Equals(finalWeight, 1.0f))
  319. ApplyTrackFullWeight(stateTrack);
  320. else
  321. ApplyTrackBlended(stateTrack, finalWeight);
  322. }
  323. }
  324. void AnimationState::ApplyToNodes()
  325. {
  326. // When applying to a node hierarchy, can only use full weight (nothing to blend to)
  327. for (Vector<AnimationStateTrack>::Iterator i = stateTracks_.Begin(); i != stateTracks_.End(); ++i)
  328. ApplyTrackFullWeight(*i);
  329. }
  330. void AnimationState::ApplyTrackFullWeight(AnimationStateTrack& stateTrack)
  331. {
  332. const AnimationTrack* track = stateTrack.track_;
  333. Node* node = stateTrack.node_;
  334. if (track->keyFrames_.Empty() || !node)
  335. return;
  336. unsigned& frame = stateTrack.keyFrame_;
  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, full weight
  356. if (channelMask & CHANNEL_POSITION)
  357. node->SetPosition(keyFrame->position_);
  358. if (channelMask & CHANNEL_ROTATION)
  359. node->SetRotation(keyFrame->rotation_);
  360. if (channelMask & CHANNEL_SCALE)
  361. node->SetScale(keyFrame->scale_);
  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, full weight
  371. if (channelMask & CHANNEL_POSITION)
  372. node->SetPosition(keyFrame->position_.Lerp(nextKeyFrame->position_, t));
  373. if (channelMask & CHANNEL_ROTATION)
  374. node->SetRotation(keyFrame->rotation_.Slerp(nextKeyFrame->rotation_, t));
  375. if (channelMask & CHANNEL_SCALE)
  376. node->SetScale(keyFrame->scale_.Lerp(nextKeyFrame->scale_, t));
  377. }
  378. }
  379. void AnimationState::ApplyTrackBlended(AnimationStateTrack& stateTrack, float weight)
  380. {
  381. const AnimationTrack* track = stateTrack.track_;
  382. Node* node = stateTrack.node_;
  383. if (track->keyFrames_.Empty() || !node)
  384. return;
  385. unsigned& frame = stateTrack.keyFrame_;
  386. track->GetKeyFrameIndex(time_, frame);
  387. // Check if next frame to interpolate to is valid, or if wrapping is needed (looping animation only)
  388. unsigned nextFrame = frame + 1;
  389. bool interpolate = true;
  390. if (nextFrame >= track->keyFrames_.Size())
  391. {
  392. if (!looped_)
  393. {
  394. nextFrame = frame;
  395. interpolate = false;
  396. }
  397. else
  398. nextFrame = 0;
  399. }
  400. const AnimationKeyFrame* keyFrame = &track->keyFrames_[frame];
  401. unsigned char channelMask = track->channelMask_;
  402. if (!interpolate)
  403. {
  404. // No interpolation, blend between old transform & animation
  405. if (channelMask & CHANNEL_POSITION)
  406. node->SetPosition(node->GetPosition().Lerp(keyFrame->position_, weight));
  407. if (channelMask & CHANNEL_ROTATION)
  408. node->SetRotation(node->GetRotation().Slerp(keyFrame->rotation_, weight));
  409. if (channelMask & CHANNEL_SCALE)
  410. node->SetScale(node->GetScale().Lerp(keyFrame->scale_, weight));
  411. }
  412. else
  413. {
  414. const AnimationKeyFrame* nextKeyFrame = &track->keyFrames_[nextFrame];
  415. float timeInterval = nextKeyFrame->time_ - keyFrame->time_;
  416. if (timeInterval < 0.0f)
  417. timeInterval += animation_->GetLength();
  418. float t = timeInterval > 0.0f ? (time_ - keyFrame->time_) / timeInterval : 1.0f;
  419. // Interpolation, blend between old transform & animation
  420. if (channelMask & CHANNEL_POSITION)
  421. {
  422. node->SetPosition(node->GetPosition().Lerp(
  423. keyFrame->position_.Lerp(nextKeyFrame->position_, t), weight));
  424. }
  425. if (channelMask & CHANNEL_ROTATION)
  426. {
  427. node->SetRotation(node->GetRotation().Slerp(
  428. keyFrame->rotation_.Slerp(nextKeyFrame->rotation_, t), weight));
  429. }
  430. if (channelMask & CHANNEL_SCALE)
  431. {
  432. node->SetScale(node->GetScale().Lerp(
  433. keyFrame->scale_.Lerp(nextKeyFrame->scale_, t), weight));
  434. }
  435. }
  436. }
  437. }