AnimationState.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610
  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 "../Graphics/AnimatedModel.h"
  24. #include "../Graphics/Animation.h"
  25. #include "../Graphics/AnimationState.h"
  26. #include "../Graphics/DrawableEvents.h"
  27. #include "../IO/Log.h"
  28. #include "../DebugNew.h"
  29. namespace Urho3D
  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 HashMap<StringHash, AnimationTrack>& tracks = animation_->GetTracks();
  68. stateTracks_.Clear();
  69. for (HashMap<StringHash, AnimationTrack>::ConstIterator i = tracks.Begin(); i != tracks.End(); ++i)
  70. {
  71. const StringHash& nameHash = i->second_.nameHash_;
  72. AnimationStateTrack stateTrack;
  73. stateTrack.track_ = &i->second_;
  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. URHO3D_LOGWARNING("Node " + i->second_.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 HashMap<StringHash, AnimationTrack>& tracks = animation_->GetTracks();
  110. stateTracks_.Clear();
  111. if (!startBone->node_)
  112. return;
  113. for (HashMap<StringHash, AnimationTrack>::ConstIterator i = tracks.Begin(); i != tracks.End(); ++i)
  114. {
  115. AnimationStateTrack stateTrack;
  116. stateTrack.track_ = &i->second_;
  117. // Include those tracks that are either the start bone itself, or its children
  118. Bone* trackBone = 0;
  119. const StringHash& nameHash = i->second_.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. WeakPtr<AnimationState> self(this);
  255. WeakPtr<Node> senderNode(model_ ? model_->GetNode() : node_);
  256. VariantMap& eventData = senderNode->GetEventDataMap();
  257. eventData[P_NODE] = senderNode;
  258. eventData[P_NAME] = animation_->GetAnimationName();
  259. eventData[P_TIME] = i->time_;
  260. eventData[P_DATA] = i->data_;
  261. // Note: this may cause arbitrary deletion of animation states, including the one we are currently processing
  262. senderNode->SendEvent(E_ANIMATIONTRIGGER, eventData);
  263. if (senderNode.Expired() || self.Expired())
  264. return;
  265. }
  266. }
  267. }
  268. }
  269. void AnimationState::SetLayer(unsigned char layer)
  270. {
  271. if (layer != layer_)
  272. {
  273. layer_ = layer;
  274. if (model_)
  275. model_->MarkAnimationOrderDirty();
  276. }
  277. }
  278. AnimatedModel* AnimationState::GetModel() const
  279. {
  280. return model_;
  281. }
  282. Node* AnimationState::GetNode() const
  283. {
  284. return node_;
  285. }
  286. Bone* AnimationState::GetStartBone() const
  287. {
  288. return model_ ? startBone_ : 0;
  289. }
  290. float AnimationState::GetBoneWeight(unsigned index) const
  291. {
  292. return index < stateTracks_.Size() ? stateTracks_[index].weight_ : 0.0f;
  293. }
  294. float AnimationState::GetBoneWeight(const String& name) const
  295. {
  296. return GetBoneWeight(GetTrackIndex(name));
  297. }
  298. float AnimationState::GetBoneWeight(StringHash nameHash) const
  299. {
  300. return GetBoneWeight(GetTrackIndex(nameHash));
  301. }
  302. unsigned AnimationState::GetTrackIndex(const String& name) const
  303. {
  304. for (unsigned i = 0; i < stateTracks_.Size(); ++i)
  305. {
  306. Node* node = stateTracks_[i].node_;
  307. if (node && node->GetName() == name)
  308. return i;
  309. }
  310. return M_MAX_UNSIGNED;
  311. }
  312. unsigned AnimationState::GetTrackIndex(Node* node) const
  313. {
  314. for (unsigned i = 0; i < stateTracks_.Size(); ++i)
  315. {
  316. if (stateTracks_[i].node_ == node)
  317. return i;
  318. }
  319. return M_MAX_UNSIGNED;
  320. }
  321. unsigned AnimationState::GetTrackIndex(StringHash nameHash) const
  322. {
  323. for (unsigned i = 0; i < stateTracks_.Size(); ++i)
  324. {
  325. Node* node = stateTracks_[i].node_;
  326. if (node && node->GetNameHash() == nameHash)
  327. return i;
  328. }
  329. return M_MAX_UNSIGNED;
  330. }
  331. float AnimationState::GetLength() const
  332. {
  333. return animation_ ? animation_->GetLength() : 0.0f;
  334. }
  335. void AnimationState::Apply()
  336. {
  337. if (!animation_ || !IsEnabled())
  338. return;
  339. if (model_)
  340. ApplyToModel();
  341. else
  342. ApplyToNodes();
  343. }
  344. void AnimationState::ApplyToModel()
  345. {
  346. for (Vector<AnimationStateTrack>::Iterator i = stateTracks_.Begin(); i != stateTracks_.End(); ++i)
  347. {
  348. AnimationStateTrack& stateTrack = *i;
  349. float finalWeight = weight_ * stateTrack.weight_;
  350. // Do not apply if zero effective weight or the bone has animation disabled
  351. if (Equals(finalWeight, 0.0f) || !stateTrack.bone_->animated_)
  352. continue;
  353. if (Equals(finalWeight, 1.0f))
  354. ApplyTrackFullWeightSilent(stateTrack);
  355. else
  356. ApplyTrackBlendedSilent(stateTrack, finalWeight);
  357. }
  358. }
  359. void AnimationState::ApplyToNodes()
  360. {
  361. // When applying to a node hierarchy, can only use full weight (nothing to blend to)
  362. for (Vector<AnimationStateTrack>::Iterator i = stateTracks_.Begin(); i != stateTracks_.End(); ++i)
  363. ApplyTrackFullWeight(*i);
  364. }
  365. void AnimationState::ApplyTrackFullWeight(AnimationStateTrack& stateTrack)
  366. {
  367. const AnimationTrack* track = stateTrack.track_;
  368. Node* node = stateTrack.node_;
  369. if (track->keyFrames_.Empty() || !node)
  370. return;
  371. unsigned& frame = stateTrack.keyFrame_;
  372. track->GetKeyFrameIndex(time_, frame);
  373. // Check if next frame to interpolate to is valid, or if wrapping is needed (looping animation only)
  374. unsigned nextFrame = frame + 1;
  375. bool interpolate = true;
  376. if (nextFrame >= track->keyFrames_.Size())
  377. {
  378. if (!looped_)
  379. {
  380. nextFrame = frame;
  381. interpolate = false;
  382. }
  383. else
  384. nextFrame = 0;
  385. }
  386. const AnimationKeyFrame* keyFrame = &track->keyFrames_[frame];
  387. unsigned char channelMask = track->channelMask_;
  388. if (!interpolate)
  389. {
  390. // No interpolation, full weight
  391. if (channelMask & CHANNEL_POSITION)
  392. node->SetPosition(keyFrame->position_);
  393. if (channelMask & CHANNEL_ROTATION)
  394. node->SetRotation(keyFrame->rotation_);
  395. if (channelMask & CHANNEL_SCALE)
  396. node->SetScale(keyFrame->scale_);
  397. }
  398. else
  399. {
  400. const AnimationKeyFrame* nextKeyFrame = &track->keyFrames_[nextFrame];
  401. float timeInterval = nextKeyFrame->time_ - keyFrame->time_;
  402. if (timeInterval < 0.0f)
  403. timeInterval += animation_->GetLength();
  404. float t = timeInterval > 0.0f ? (time_ - keyFrame->time_) / timeInterval : 1.0f;
  405. // Interpolation, full weight
  406. if (channelMask & CHANNEL_POSITION)
  407. node->SetPosition(keyFrame->position_.Lerp(nextKeyFrame->position_, t));
  408. if (channelMask & CHANNEL_ROTATION)
  409. node->SetRotation(keyFrame->rotation_.Slerp(nextKeyFrame->rotation_, t));
  410. if (channelMask & CHANNEL_SCALE)
  411. node->SetScale(keyFrame->scale_.Lerp(nextKeyFrame->scale_, t));
  412. }
  413. }
  414. void AnimationState::ApplyTrackFullWeightSilent(AnimationStateTrack& stateTrack)
  415. {
  416. const AnimationTrack* track = stateTrack.track_;
  417. Node* node = stateTrack.node_;
  418. if (track->keyFrames_.Empty() || !node)
  419. return;
  420. unsigned& frame = stateTrack.keyFrame_;
  421. track->GetKeyFrameIndex(time_, frame);
  422. // Check if next frame to interpolate to is valid, or if wrapping is needed (looping animation only)
  423. unsigned nextFrame = frame + 1;
  424. bool interpolate = true;
  425. if (nextFrame >= track->keyFrames_.Size())
  426. {
  427. if (!looped_)
  428. {
  429. nextFrame = frame;
  430. interpolate = false;
  431. }
  432. else
  433. nextFrame = 0;
  434. }
  435. const AnimationKeyFrame* keyFrame = &track->keyFrames_[frame];
  436. unsigned char channelMask = track->channelMask_;
  437. if (!interpolate)
  438. {
  439. // No interpolation, full weight
  440. if (channelMask & CHANNEL_POSITION)
  441. node->SetPositionSilent(keyFrame->position_);
  442. if (channelMask & CHANNEL_ROTATION)
  443. node->SetRotationSilent(keyFrame->rotation_);
  444. if (channelMask & CHANNEL_SCALE)
  445. node->SetScaleSilent(keyFrame->scale_);
  446. }
  447. else
  448. {
  449. const AnimationKeyFrame* nextKeyFrame = &track->keyFrames_[nextFrame];
  450. float timeInterval = nextKeyFrame->time_ - keyFrame->time_;
  451. if (timeInterval < 0.0f)
  452. timeInterval += animation_->GetLength();
  453. float t = timeInterval > 0.0f ? (time_ - keyFrame->time_) / timeInterval : 1.0f;
  454. // Interpolation, full weight
  455. if (channelMask & CHANNEL_POSITION)
  456. node->SetPositionSilent(keyFrame->position_.Lerp(nextKeyFrame->position_, t));
  457. if (channelMask & CHANNEL_ROTATION)
  458. node->SetRotationSilent(keyFrame->rotation_.Slerp(nextKeyFrame->rotation_, t));
  459. if (channelMask & CHANNEL_SCALE)
  460. node->SetScaleSilent(keyFrame->scale_.Lerp(nextKeyFrame->scale_, t));
  461. }
  462. }
  463. void AnimationState::ApplyTrackBlendedSilent(AnimationStateTrack& stateTrack, float weight)
  464. {
  465. const AnimationTrack* track = stateTrack.track_;
  466. Node* node = stateTrack.node_;
  467. if (track->keyFrames_.Empty() || !node)
  468. return;
  469. unsigned& frame = stateTrack.keyFrame_;
  470. track->GetKeyFrameIndex(time_, frame);
  471. // Check if next frame to interpolate to is valid, or if wrapping is needed (looping animation only)
  472. unsigned nextFrame = frame + 1;
  473. bool interpolate = true;
  474. if (nextFrame >= track->keyFrames_.Size())
  475. {
  476. if (!looped_)
  477. {
  478. nextFrame = frame;
  479. interpolate = false;
  480. }
  481. else
  482. nextFrame = 0;
  483. }
  484. const AnimationKeyFrame* keyFrame = &track->keyFrames_[frame];
  485. unsigned char channelMask = track->channelMask_;
  486. if (!interpolate)
  487. {
  488. // No interpolation, blend between old transform & animation
  489. if (channelMask & CHANNEL_POSITION)
  490. node->SetPositionSilent(node->GetPosition().Lerp(keyFrame->position_, weight));
  491. if (channelMask & CHANNEL_ROTATION)
  492. node->SetRotationSilent(node->GetRotation().Slerp(keyFrame->rotation_, weight));
  493. if (channelMask & CHANNEL_SCALE)
  494. node->SetScaleSilent(node->GetScale().Lerp(keyFrame->scale_, weight));
  495. }
  496. else
  497. {
  498. const AnimationKeyFrame* nextKeyFrame = &track->keyFrames_[nextFrame];
  499. float timeInterval = nextKeyFrame->time_ - keyFrame->time_;
  500. if (timeInterval < 0.0f)
  501. timeInterval += animation_->GetLength();
  502. float t = timeInterval > 0.0f ? (time_ - keyFrame->time_) / timeInterval : 1.0f;
  503. // Interpolation, blend between old transform & animation
  504. if (channelMask & CHANNEL_POSITION)
  505. {
  506. node->SetPositionSilent(node->GetPosition().Lerp(
  507. keyFrame->position_.Lerp(nextKeyFrame->position_, t), weight));
  508. }
  509. if (channelMask & CHANNEL_ROTATION)
  510. {
  511. node->SetRotationSilent(node->GetRotation().Slerp(
  512. keyFrame->rotation_.Slerp(nextKeyFrame->rotation_, t), weight));
  513. }
  514. if (channelMask & CHANNEL_SCALE)
  515. {
  516. node->SetScaleSilent(node->GetScale().Lerp(
  517. keyFrame->scale_.Lerp(nextKeyFrame->scale_, t), weight));
  518. }
  519. }
  520. }
  521. }