AnimationState.cpp 15 KB

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