AnimationController.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2011 Lasse Öörni
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. //
  23. #include "Precompiled.h"
  24. #include "AnimatedModel.h"
  25. #include "Animation.h"
  26. #include "AnimationController.h"
  27. #include "AnimationState.h"
  28. #include "Context.h"
  29. #include "MemoryBuffer.h"
  30. #include "Profiler.h"
  31. #include "ResourceCache.h"
  32. #include "Scene.h"
  33. #include "SceneEvents.h"
  34. #include "VectorBuffer.h"
  35. #include "DebugNew.h"
  36. static std::string noBoneName;
  37. OBJECTTYPESTATIC(AnimationController);
  38. AnimationController::AnimationController(Context* context) :
  39. Component(context)
  40. {
  41. }
  42. AnimationController::~AnimationController()
  43. {
  44. }
  45. void AnimationController::RegisterObject(Context* context)
  46. {
  47. context->RegisterFactory<AnimationController>();
  48. ATTRIBUTE(AnimationController, VAR_BUFFER, "Animations", animations_, std::vector<unsigned char>());
  49. }
  50. void AnimationController::OnSetAttribute(const AttributeInfo& attr, const Variant& value)
  51. {
  52. switch (attr.offset_)
  53. {
  54. case offsetof(AnimationController, animations_):
  55. {
  56. MemoryBuffer buf(value.GetBuffer());
  57. animations_.resize(buf.ReadVLE());
  58. for (std::vector<AnimationControl>::iterator i = animations_.begin(); i != animations_.end(); ++i)
  59. {
  60. i->hash_ = buf.ReadStringHash();
  61. i->group_ = buf.ReadUByte();
  62. i->speed_ = buf.ReadFloat();
  63. i->targetWeight_ = buf.ReadFloat();
  64. i->fadeTime_ = buf.ReadFloat();
  65. i->autoFadeTime_ = buf.ReadFloat();
  66. }
  67. }
  68. break;
  69. default:
  70. Serializable::OnSetAttribute(attr, value);
  71. break;
  72. }
  73. }
  74. Variant AnimationController::OnGetAttribute(const AttributeInfo& attr)
  75. {
  76. switch (attr.offset_)
  77. {
  78. case offsetof(AnimationController, animations_):
  79. {
  80. VectorBuffer buf;
  81. buf.WriteVLE(animations_.size());
  82. for (std::vector<AnimationControl>::const_iterator i = animations_.begin(); i != animations_.end(); ++i)
  83. {
  84. buf.WriteStringHash(i->hash_);
  85. buf.WriteUByte(i->group_);
  86. buf.WriteFloat(i->speed_);
  87. buf.WriteFloat(i->targetWeight_);
  88. buf.WriteFloat(i->fadeTime_);
  89. buf.WriteFloat(i->autoFadeTime_);
  90. }
  91. return buf.GetBuffer();
  92. }
  93. default:
  94. return Serializable::OnGetAttribute(attr);
  95. }
  96. }
  97. void AnimationController::Update(float timeStep)
  98. {
  99. AnimatedModel* model = GetAnimatedModel();
  100. if (!model)
  101. return;
  102. PROFILE(UpdateAnimationController);
  103. // Loop through animations
  104. for (std::vector<AnimationControl>::iterator i = animations_.begin(); i != animations_.end();)
  105. {
  106. bool remove = false;
  107. AnimationState* state = model->GetAnimationState(i->hash_);
  108. if (!state)
  109. remove = true;
  110. else
  111. {
  112. // Advance the animation
  113. if (i->speed_ != 0.0f)
  114. state->AddTime(i->speed_ * timeStep);
  115. float targetWeight = i->targetWeight_;
  116. float fadeTime = i->fadeTime_;
  117. // If non-looped animation at the end, activate autofade as applicable
  118. if ((!state->IsLooped()) && (state->GetTime() >= state->GetLength()) && (i->autoFadeTime_ > 0.0f))
  119. {
  120. targetWeight = 0.0f;
  121. fadeTime = i->autoFadeTime_;
  122. }
  123. // Process weight fade
  124. float currentWeight = state->GetWeight();
  125. if ((currentWeight != targetWeight) && (fadeTime > 0.0f))
  126. {
  127. float weightDelta = 1.0f / fadeTime * timeStep;
  128. if (currentWeight < targetWeight)
  129. currentWeight = Min(currentWeight + weightDelta, targetWeight);
  130. else if (currentWeight > targetWeight)
  131. currentWeight = Max(currentWeight - weightDelta, targetWeight);
  132. state->SetWeight(currentWeight);
  133. }
  134. // Remove if weight zero and target weight zero
  135. if ((state->GetWeight() == 0.0f) && ((targetWeight == 0.0f) || (fadeTime == 0.0f)))
  136. remove = true;
  137. }
  138. if (remove)
  139. {
  140. if (state)
  141. model->RemoveAnimationState(state);
  142. i = animations_.erase(i);
  143. }
  144. else
  145. ++i;
  146. }
  147. }
  148. bool AnimationController::Play(const std::string& name, unsigned char group, bool looped, bool restart, float fadeInTime)
  149. {
  150. AnimatedModel* model = GetAnimatedModel();
  151. if (!model)
  152. return false;
  153. // Check if already exists
  154. unsigned index;
  155. AnimationState* state;
  156. FindAnimation(name, index, state);
  157. if (!state)
  158. {
  159. Animation* newAnimation = GetSubsystem<ResourceCache>()->GetResource<Animation>(name);
  160. state = model->AddAnimationState(newAnimation);
  161. if (!state)
  162. return false;
  163. }
  164. if (index == M_MAX_UNSIGNED)
  165. {
  166. AnimationControl newControl;
  167. Animation* animation = state->GetAnimation();
  168. newControl.hash_ = animation->GetNameHash();
  169. animations_.push_back(newControl);
  170. index = animations_.size() - 1;
  171. }
  172. animations_[index].group_ = group;
  173. if (restart)
  174. state->SetTime(0.0f);
  175. state->SetLooped(looped);
  176. if (fadeInTime > 0.0f)
  177. {
  178. animations_[index].targetWeight_ = 1.0f;
  179. animations_[index].fadeTime_ = fadeInTime;
  180. }
  181. else
  182. state->SetWeight(1.0f);
  183. return true;
  184. }
  185. bool AnimationController::PlayExclusive(const std::string& name, unsigned char group, bool looped, bool restart, float fadeInTime, float fadeOutTime)
  186. {
  187. FadeOthers(name, 0.0f, fadeOutTime);
  188. return Play(name, group, looped, restart, fadeInTime);
  189. }
  190. bool AnimationController::Stop(const std::string& name, float fadeOutTime)
  191. {
  192. AnimatedModel* model = GetAnimatedModel();
  193. if (!model)
  194. return false;
  195. unsigned index;
  196. AnimationState* state;
  197. FindAnimation(name, index, state);
  198. if (fadeOutTime <= 0.0f)
  199. {
  200. if (index != M_MAX_UNSIGNED)
  201. animations_.erase(animations_.begin() + index);
  202. if (state)
  203. model->RemoveAnimationState(state);
  204. }
  205. else
  206. {
  207. if (index != M_MAX_UNSIGNED)
  208. {
  209. animations_[index].targetWeight_ = 0.0f;
  210. animations_[index].fadeTime_ = fadeOutTime;
  211. }
  212. }
  213. return (index != M_MAX_UNSIGNED) || (state != 0);
  214. }
  215. void AnimationController::StopGroup(unsigned char group, float fadeOutTime)
  216. {
  217. AnimatedModel* model = GetAnimatedModel();
  218. if (!model)
  219. return;
  220. for (std::vector<AnimationControl>::iterator i = animations_.begin(); i != animations_.end();)
  221. {
  222. bool remove = false;
  223. if (i->group_ == group)
  224. {
  225. if (fadeOutTime <= 0.0f)
  226. {
  227. remove = true;
  228. AnimationState* state = model->GetAnimationState(i->hash_);
  229. if (state)
  230. model->RemoveAnimationState(state);
  231. }
  232. else
  233. {
  234. i->targetWeight_ = 0.0f;
  235. i->fadeTime_ = fadeOutTime;
  236. }
  237. }
  238. if (remove)
  239. i = animations_.erase(i);
  240. else
  241. ++i;
  242. }
  243. }
  244. void AnimationController::StopAll(float fadeOutTime)
  245. {
  246. AnimatedModel* model = GetAnimatedModel();
  247. if (!model)
  248. return;
  249. for (std::vector<AnimationControl>::iterator i = animations_.begin(); i != animations_.end();)
  250. {
  251. bool remove = false;
  252. if (fadeOutTime <= 0.0f)
  253. {
  254. remove = true;
  255. AnimationState* state = model->GetAnimationState(i->hash_);
  256. if (state)
  257. model->RemoveAnimationState(state);
  258. }
  259. else
  260. {
  261. i->targetWeight_ = 0.0f;
  262. i->fadeTime_ = fadeOutTime;
  263. }
  264. if (remove)
  265. i = animations_.erase(i);
  266. else
  267. ++i;
  268. }
  269. }
  270. bool AnimationController::Fade(const std::string& name, float targetWeight, float fadeTime)
  271. {
  272. unsigned index;
  273. AnimationState* state;
  274. FindAnimation(name, index, state);
  275. if (index == M_MAX_UNSIGNED)
  276. return false;
  277. animations_[index].targetWeight_ = Clamp(targetWeight, 0.0f, 1.0f);
  278. animations_[index].fadeTime_ = Max(fadeTime, M_EPSILON);
  279. return true;
  280. }
  281. bool AnimationController::FadeOthers(const std::string& name, float targetWeight, float fadeTime)
  282. {
  283. unsigned index;
  284. AnimationState* state;
  285. FindAnimation(name, index, state);
  286. if (index == M_MAX_UNSIGNED)
  287. return false;
  288. unsigned char group = animations_[index].group_;
  289. for (unsigned i = 0; i < animations_.size(); ++i)
  290. {
  291. AnimationControl& control = animations_[i];
  292. if ((control.group_ == group) && (i != index))
  293. {
  294. control.targetWeight_ = Clamp(targetWeight, 0.0f, 1.0f);
  295. control.fadeTime_ = Max(fadeTime, M_EPSILON);
  296. }
  297. }
  298. return true;
  299. }
  300. bool AnimationController::SetPriority(const std::string& name, int priority)
  301. {
  302. AnimationState* state = FindAnimationState(name);
  303. if (!state)
  304. return false;
  305. state->SetPriority(priority);
  306. return true;
  307. }
  308. bool AnimationController::SetStartBone(const std::string& name, const std::string& startBoneName)
  309. {
  310. AnimatedModel* model = GetAnimatedModel();
  311. if (!model)
  312. return false;
  313. AnimationState* state = FindAnimationState(name);
  314. if (!state)
  315. return false;
  316. Bone* bone = model->GetSkeleton().GetBone(startBoneName);
  317. state->SetStartBone(bone);
  318. return true;
  319. }
  320. bool AnimationController::SetBlending(const std::string& name, int priority, const std::string& startBoneName)
  321. {
  322. AnimatedModel* model = GetAnimatedModel();
  323. if (!model)
  324. return false;
  325. AnimationState* state = FindAnimationState(name);
  326. if (!state)
  327. return false;
  328. Bone* bone = model->GetSkeleton().GetBone(startBoneName);
  329. state->SetPriority(priority);
  330. state->SetStartBone(bone);
  331. return true;
  332. }
  333. bool AnimationController::SetTime(const std::string& name, float time)
  334. {
  335. AnimationState* state = FindAnimationState(name);
  336. if (!state)
  337. return false;
  338. state->SetTime(time);
  339. return true;
  340. }
  341. bool AnimationController::SetGroup(const std::string& name, unsigned char group)
  342. {
  343. unsigned index;
  344. AnimationState* state;
  345. FindAnimation(name, index, state);
  346. if (index == M_MAX_UNSIGNED)
  347. return false;
  348. animations_[index].group_ = group;
  349. return true;
  350. }
  351. bool AnimationController::SetSpeed(const std::string& name, float speed)
  352. {
  353. unsigned index;
  354. AnimationState* state;
  355. FindAnimation(name, index, state);
  356. if (index == M_MAX_UNSIGNED)
  357. return false;
  358. animations_[index].speed_ = speed;
  359. return true;
  360. }
  361. bool AnimationController::SetWeight(const std::string& name, float weight)
  362. {
  363. unsigned index;
  364. AnimationState* state;
  365. FindAnimation(name, index, state);
  366. if ((index == M_MAX_UNSIGNED) || (!state))
  367. return false;
  368. state->SetWeight(weight);
  369. // Stop any ongoing fade
  370. animations_[index].fadeTime_ = 0.0f;
  371. return true;
  372. }
  373. bool AnimationController::SetLooped(const std::string& name, bool enable)
  374. {
  375. AnimationState* state = FindAnimationState(name);
  376. if (!state)
  377. return false;
  378. state->SetLooped(enable);
  379. return true;
  380. }
  381. bool AnimationController::SetAutoFade(const std::string& name, float fadeOutTime)
  382. {
  383. unsigned index;
  384. AnimationState* state;
  385. FindAnimation(name, index, state);
  386. if (index == M_MAX_UNSIGNED)
  387. return false;
  388. animations_[index].autoFadeTime_ = Max(fadeOutTime, 0.0f);
  389. return true;
  390. }
  391. AnimatedModel* AnimationController::GetAnimatedModel() const
  392. {
  393. return GetComponent<AnimatedModel>();
  394. }
  395. bool AnimationController::IsPlaying(const std::string& name) const
  396. {
  397. unsigned index;
  398. AnimationState* state;
  399. FindAnimation(name, index, state);
  400. return index != M_MAX_UNSIGNED;
  401. }
  402. bool AnimationController::IsFadingIn(const std::string& name) const
  403. {
  404. unsigned index;
  405. AnimationState* state;
  406. FindAnimation(name, index, state);
  407. if ((index == M_MAX_UNSIGNED) || (!state))
  408. return false;
  409. return (animations_[index].fadeTime_) && (animations_[index].targetWeight_ > state->GetWeight());
  410. }
  411. bool AnimationController::IsFadingOut(const std::string& name) const
  412. {
  413. unsigned index;
  414. AnimationState* state;
  415. FindAnimation(name, index, state);
  416. if ((index == M_MAX_UNSIGNED) || (!state))
  417. return false;
  418. return ((animations_[index].fadeTime_) && (animations_[index].targetWeight_ < state->GetWeight()))
  419. || ((!state->IsLooped()) && (state->GetTime() >= state->GetLength()) && (animations_[index].autoFadeTime_));
  420. }
  421. int AnimationController::GetPriority(const std::string& name) const
  422. {
  423. AnimationState* state = FindAnimationState(name);
  424. if (!state)
  425. return 0;
  426. return state->GetPriority();
  427. }
  428. Bone* AnimationController::GetStartBone(const std::string& name) const
  429. {
  430. AnimationState* state = FindAnimationState(name);
  431. if (!state)
  432. return 0;
  433. return state->GetStartBone();
  434. }
  435. const std::string& AnimationController::GetStartBoneName(const std::string& name) const
  436. {
  437. Bone* bone = GetStartBone(name);
  438. return bone ? bone->name_ : noBoneName;
  439. }
  440. float AnimationController::GetTime(const std::string& name) const
  441. {
  442. AnimationState* state = FindAnimationState(name);
  443. if (!state)
  444. return 0.0f;
  445. return state->GetTime();
  446. }
  447. float AnimationController::GetWeight(const std::string& name) const
  448. {
  449. AnimationState* state = FindAnimationState(name);
  450. if (!state)
  451. return 0.0f;
  452. return state->GetWeight();
  453. }
  454. bool AnimationController::IsLooped(const std::string& name) const
  455. {
  456. AnimationState* state = FindAnimationState(name);
  457. if (!state)
  458. return false;
  459. return state->IsLooped();
  460. }
  461. float AnimationController::GetLength(const std::string& name) const
  462. {
  463. AnimationState* state = FindAnimationState(name);
  464. if (!state)
  465. return 0.0f;
  466. return state->GetLength();
  467. }
  468. unsigned char AnimationController::GetGroup(const std::string& name) const
  469. {
  470. unsigned index;
  471. AnimationState* state;
  472. FindAnimation(name, index, state);
  473. if (index == M_MAX_UNSIGNED)
  474. return 0;
  475. return animations_[index].group_;
  476. }
  477. float AnimationController::GetSpeed(const std::string& name) const
  478. {
  479. unsigned index;
  480. AnimationState* state;
  481. FindAnimation(name, index, state);
  482. if (index == M_MAX_UNSIGNED)
  483. return 0.0f;
  484. return animations_[index].speed_;
  485. }
  486. float AnimationController::GetFadeTarget(const std::string& name) const
  487. {
  488. unsigned index;
  489. AnimationState* state;
  490. FindAnimation(name, index, state);
  491. if (index == M_MAX_UNSIGNED)
  492. return 0.0f;
  493. return animations_[index].targetWeight_;
  494. }
  495. float AnimationController::GetFadeTime(const std::string& name) const
  496. {
  497. unsigned index;
  498. AnimationState* state;
  499. FindAnimation(name, index, state);
  500. if (index == M_MAX_UNSIGNED)
  501. return 0.0f;
  502. return animations_[index].targetWeight_;
  503. }
  504. float AnimationController::GetAutoFade(const std::string& name) const
  505. {
  506. unsigned index;
  507. AnimationState* state;
  508. FindAnimation(name, index, state);
  509. if (index == M_MAX_UNSIGNED)
  510. return 0.0f;
  511. return animations_[index].autoFadeTime_;
  512. }
  513. void AnimationController::OnNodeSet(Node* node)
  514. {
  515. if (node)
  516. {
  517. Scene* scene = node->GetScene();
  518. if (scene)
  519. SubscribeToEvent(scene, E_SCENEPOSTUPDATE, HANDLER(AnimationController, HandleScenePostUpdate));
  520. }
  521. }
  522. void AnimationController::FindAnimation(const std::string& name, unsigned& index, AnimationState*& state) const
  523. {
  524. AnimatedModel* model = GetAnimatedModel();
  525. StringHash nameHash(name);
  526. // Find the AnimationState
  527. state = model ? model->GetAnimationState(nameHash) : 0;
  528. if (state)
  529. {
  530. // Either a resource name or animation name may be specified. We store resource names, so correct the hash if necessary
  531. nameHash = state->GetAnimation()->GetNameHash();
  532. }
  533. // Find the internal control structure
  534. index = M_MAX_UNSIGNED;
  535. for (unsigned i = 0; i < animations_.size(); ++i)
  536. {
  537. if (animations_[i].hash_ == nameHash)
  538. {
  539. index = i;
  540. break;
  541. }
  542. }
  543. }
  544. AnimationState* AnimationController::FindAnimationState(const std::string& name) const
  545. {
  546. AnimatedModel* model = GetAnimatedModel();
  547. StringHash nameHash(name);
  548. return model ? model->GetAnimationState(nameHash) : 0;
  549. }
  550. void AnimationController::HandleScenePostUpdate(StringHash eventType, VariantMap& eventData)
  551. {
  552. using namespace ScenePostUpdate;
  553. Update(eventData[P_TIMESTEP].GetFloat());
  554. }