AnimationState.cpp 18 KB

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