AnimationController.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591
  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 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_, 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 (Vector<AnimationControl>::Iterator i = animations_.Begin(); i != animations_.End(); ++i)
  59. {
  60. i->hash_ = buf.ReadStringHash();
  61. i->speed_ = buf.ReadFloat();
  62. i->targetWeight_ = buf.ReadFloat();
  63. i->fadeTime_ = buf.ReadFloat();
  64. i->autoFadeTime_ = buf.ReadFloat();
  65. }
  66. }
  67. break;
  68. default:
  69. Serializable::OnSetAttribute(attr, value);
  70. break;
  71. }
  72. }
  73. Variant AnimationController::OnGetAttribute(const AttributeInfo& attr)
  74. {
  75. switch (attr.offset_)
  76. {
  77. case offsetof(AnimationController, animations_):
  78. {
  79. VectorBuffer buf;
  80. buf.WriteVLE(animations_.Size());
  81. for (Vector<AnimationControl>::ConstIterator i = animations_.Begin(); i != animations_.End(); ++i)
  82. {
  83. buf.WriteStringHash(i->hash_);
  84. buf.WriteFloat(i->speed_);
  85. buf.WriteFloat(i->targetWeight_);
  86. buf.WriteFloat(i->fadeTime_);
  87. buf.WriteFloat(i->autoFadeTime_);
  88. }
  89. return buf.GetBuffer();
  90. }
  91. default:
  92. return Serializable::OnGetAttribute(attr);
  93. }
  94. }
  95. void AnimationController::Update(float timeStep)
  96. {
  97. AnimatedModel* model = GetComponent<AnimatedModel>();
  98. if (!model)
  99. return;
  100. PROFILE(UpdateAnimationController);
  101. // Loop through animations
  102. for (Vector<AnimationControl>::Iterator i = animations_.Begin(); i != animations_.End();)
  103. {
  104. bool remove = false;
  105. AnimationState* state = model->GetAnimationState(i->hash_);
  106. if (!state)
  107. remove = true;
  108. else
  109. {
  110. // Advance the animation
  111. if (i->speed_ != 0.0f)
  112. state->AddTime(i->speed_ * timeStep);
  113. float targetWeight = i->targetWeight_;
  114. float fadeTime = i->fadeTime_;
  115. // If non-looped animation at the end, activate autofade as applicable
  116. if ((!state->IsLooped()) && (state->GetTime() >= state->GetLength()) && (i->autoFadeTime_ > 0.0f))
  117. {
  118. targetWeight = 0.0f;
  119. fadeTime = i->autoFadeTime_;
  120. }
  121. // Process weight fade
  122. float currentWeight = state->GetWeight();
  123. if ((currentWeight != targetWeight) && (fadeTime > 0.0f))
  124. {
  125. float weightDelta = 1.0f / fadeTime * timeStep;
  126. if (currentWeight < targetWeight)
  127. currentWeight = Min(currentWeight + weightDelta, targetWeight);
  128. else if (currentWeight > targetWeight)
  129. currentWeight = Max(currentWeight - weightDelta, targetWeight);
  130. state->SetWeight(currentWeight);
  131. }
  132. // Remove if weight zero and target weight zero
  133. if ((state->GetWeight() == 0.0f) && ((targetWeight == 0.0f) || (fadeTime == 0.0f)))
  134. remove = true;
  135. }
  136. if (remove)
  137. {
  138. if (state)
  139. model->RemoveAnimationState(state);
  140. i = animations_.Erase(i);
  141. }
  142. else
  143. ++i;
  144. }
  145. }
  146. bool AnimationController::Play(const String& name, int layer, bool looped, float fadeInTime)
  147. {
  148. AnimatedModel* model = GetComponent<AnimatedModel>();
  149. if (!model)
  150. return false;
  151. // Check if already exists
  152. unsigned index;
  153. AnimationState* state;
  154. FindAnimation(name, index, state);
  155. if (!state)
  156. {
  157. Animation* newAnimation = GetSubsystem<ResourceCache>()->GetResource<Animation>(name);
  158. state = model->AddAnimationState(newAnimation);
  159. if (!state)
  160. return false;
  161. }
  162. if (index == M_MAX_UNSIGNED)
  163. {
  164. AnimationControl newControl;
  165. Animation* animation = state->GetAnimation();
  166. newControl.hash_ = animation->GetNameHash();
  167. animations_.Push(newControl);
  168. index = animations_.Size() - 1;
  169. }
  170. state->SetLayer(layer);
  171. state->SetLooped(looped);
  172. if (fadeInTime > 0.0f)
  173. {
  174. animations_[index].targetWeight_ = 1.0f;
  175. animations_[index].fadeTime_ = fadeInTime;
  176. }
  177. else
  178. state->SetWeight(1.0f);
  179. return true;
  180. }
  181. bool AnimationController::PlayExclusive(const String& name, int layer, bool looped, float fadeTime)
  182. {
  183. FadeOthers(name, 0.0f, fadeTime);
  184. return Play(name, layer, looped, fadeTime);
  185. }
  186. bool AnimationController::Stop(const String& name, float fadeOutTime)
  187. {
  188. AnimatedModel* model = GetComponent<AnimatedModel>();
  189. if (!model)
  190. return false;
  191. unsigned index;
  192. AnimationState* state;
  193. FindAnimation(name, index, state);
  194. if (fadeOutTime <= 0.0f)
  195. {
  196. if (index != M_MAX_UNSIGNED)
  197. animations_.Erase(animations_.Begin() + index);
  198. if (state)
  199. model->RemoveAnimationState(state);
  200. }
  201. else
  202. {
  203. if (index != M_MAX_UNSIGNED)
  204. {
  205. animations_[index].targetWeight_ = 0.0f;
  206. animations_[index].fadeTime_ = fadeOutTime;
  207. }
  208. }
  209. return (index != M_MAX_UNSIGNED) || (state != 0);
  210. }
  211. void AnimationController::StopLayer(int layer, float fadeOutTime)
  212. {
  213. AnimatedModel* model = GetComponent<AnimatedModel>();
  214. if (!model)
  215. return;
  216. for (Vector<AnimationControl>::Iterator i = animations_.Begin(); i != animations_.End();)
  217. {
  218. AnimationState* state = model->GetAnimationState(i->hash_);
  219. bool remove = false;
  220. if ((state) && (state->GetLayer() == layer))
  221. {
  222. if (fadeOutTime <= 0.0f)
  223. {
  224. remove = true;
  225. if (state)
  226. model->RemoveAnimationState(state);
  227. }
  228. else
  229. {
  230. i->targetWeight_ = 0.0f;
  231. i->fadeTime_ = fadeOutTime;
  232. }
  233. }
  234. if (remove)
  235. i = animations_.Erase(i);
  236. else
  237. ++i;
  238. }
  239. }
  240. void AnimationController::StopAll(float fadeOutTime)
  241. {
  242. AnimatedModel* model = GetComponent<AnimatedModel>();
  243. if (!model)
  244. return;
  245. for (Vector<AnimationControl>::Iterator i = animations_.Begin(); i != animations_.End();)
  246. {
  247. bool remove = false;
  248. if (fadeOutTime <= 0.0f)
  249. {
  250. remove = true;
  251. AnimationState* state = model->GetAnimationState(i->hash_);
  252. if (state)
  253. model->RemoveAnimationState(state);
  254. }
  255. else
  256. {
  257. i->targetWeight_ = 0.0f;
  258. i->fadeTime_ = fadeOutTime;
  259. }
  260. if (remove)
  261. i = animations_.Erase(i);
  262. else
  263. ++i;
  264. }
  265. }
  266. bool AnimationController::Fade(const String& name, float targetWeight, float fadeTime)
  267. {
  268. unsigned index;
  269. AnimationState* state;
  270. FindAnimation(name, index, state);
  271. if (index == M_MAX_UNSIGNED)
  272. return false;
  273. animations_[index].targetWeight_ = Clamp(targetWeight, 0.0f, 1.0f);
  274. animations_[index].fadeTime_ = Max(fadeTime, M_EPSILON);
  275. return true;
  276. }
  277. bool AnimationController::FadeOthers(const String& name, float targetWeight, float fadeTime)
  278. {
  279. unsigned index;
  280. AnimationState* state;
  281. FindAnimation(name, index, state);
  282. if ((index == M_MAX_UNSIGNED) || (!state))
  283. return false;
  284. AnimatedModel* model = GetComponent<AnimatedModel>();
  285. int layer = state->GetLayer();
  286. for (unsigned i = 0; i < animations_.Size(); ++i)
  287. {
  288. if (i != index)
  289. {
  290. AnimationControl& control = animations_[i];
  291. AnimationState* otherState = model->GetAnimationState(control.hash_);
  292. if ((otherState) && (otherState->GetLayer() == layer))
  293. {
  294. control.targetWeight_ = Clamp(targetWeight, 0.0f, 1.0f);
  295. control.fadeTime_ = Max(fadeTime, M_EPSILON);
  296. }
  297. }
  298. }
  299. return true;
  300. }
  301. bool AnimationController::SetLayer(const String& name, int layer)
  302. {
  303. AnimationState* state = FindAnimationState(name);
  304. if (!state)
  305. return false;
  306. state->SetLayer(layer);
  307. return true;
  308. }
  309. bool AnimationController::SetStartBone(const String& name, const String& startBoneName)
  310. {
  311. AnimationState* state = FindAnimationState(name);
  312. if (!state)
  313. return false;
  314. AnimatedModel* model = GetComponent<AnimatedModel>();
  315. Bone* bone = model->GetSkeleton().GetBone(startBoneName);
  316. state->SetStartBone(bone);
  317. return true;
  318. }
  319. bool AnimationController::SetTime(const String& name, float time)
  320. {
  321. AnimationState* state = FindAnimationState(name);
  322. if (!state)
  323. return false;
  324. state->SetTime(time);
  325. return true;
  326. }
  327. bool AnimationController::SetSpeed(const String& name, float speed)
  328. {
  329. unsigned index;
  330. AnimationState* state;
  331. FindAnimation(name, index, state);
  332. if (index == M_MAX_UNSIGNED)
  333. return false;
  334. animations_[index].speed_ = speed;
  335. return true;
  336. }
  337. bool AnimationController::SetWeight(const String& name, float weight)
  338. {
  339. unsigned index;
  340. AnimationState* state;
  341. FindAnimation(name, index, state);
  342. if ((index == M_MAX_UNSIGNED) || (!state))
  343. return false;
  344. state->SetWeight(weight);
  345. // Stop any ongoing fade
  346. animations_[index].fadeTime_ = 0.0f;
  347. return true;
  348. }
  349. bool AnimationController::SetLooped(const String& name, bool enable)
  350. {
  351. AnimationState* state = FindAnimationState(name);
  352. if (!state)
  353. return false;
  354. state->SetLooped(enable);
  355. return true;
  356. }
  357. bool AnimationController::SetAutoFade(const String& name, float fadeOutTime)
  358. {
  359. unsigned index;
  360. AnimationState* state;
  361. FindAnimation(name, index, state);
  362. if (index == M_MAX_UNSIGNED)
  363. return false;
  364. animations_[index].autoFadeTime_ = Max(fadeOutTime, 0.0f);
  365. return true;
  366. }
  367. bool AnimationController::IsPlaying(const String& name) const
  368. {
  369. unsigned index;
  370. AnimationState* state;
  371. FindAnimation(name, index, state);
  372. return index != M_MAX_UNSIGNED;
  373. }
  374. bool AnimationController::IsFadingIn(const String& name) const
  375. {
  376. unsigned index;
  377. AnimationState* state;
  378. FindAnimation(name, index, state);
  379. if ((index == M_MAX_UNSIGNED) || (!state))
  380. return false;
  381. return (animations_[index].fadeTime_) && (animations_[index].targetWeight_ > state->GetWeight());
  382. }
  383. bool AnimationController::IsFadingOut(const String& name) const
  384. {
  385. unsigned index;
  386. AnimationState* state;
  387. FindAnimation(name, index, state);
  388. if ((index == M_MAX_UNSIGNED) || (!state))
  389. return false;
  390. return ((animations_[index].fadeTime_) && (animations_[index].targetWeight_ < state->GetWeight()))
  391. || ((!state->IsLooped()) && (state->GetTime() >= state->GetLength()) && (animations_[index].autoFadeTime_));
  392. }
  393. int AnimationController::GetLayer(const String& name) const
  394. {
  395. AnimationState* state = FindAnimationState(name);
  396. if (!state)
  397. return 0;
  398. return state->GetLayer();
  399. }
  400. Bone* AnimationController::GetStartBone(const String& name) const
  401. {
  402. AnimationState* state = FindAnimationState(name);
  403. if (!state)
  404. return 0;
  405. return state->GetStartBone();
  406. }
  407. const String& AnimationController::GetStartBoneName(const String& name) const
  408. {
  409. Bone* bone = GetStartBone(name);
  410. return bone ? bone->name_ : noBoneName;
  411. }
  412. float AnimationController::GetTime(const String& name) const
  413. {
  414. AnimationState* state = FindAnimationState(name);
  415. return state ? state->GetTime() : 0.0f;
  416. }
  417. float AnimationController::GetWeight(const String& name) const
  418. {
  419. AnimationState* state = FindAnimationState(name);
  420. return state ? state->GetWeight() : 0.0f;
  421. }
  422. bool AnimationController::IsLooped(const String& name) const
  423. {
  424. AnimationState* state = FindAnimationState(name);
  425. return state ? state->IsLooped() : false;
  426. }
  427. float AnimationController::GetLength(const String& name) const
  428. {
  429. AnimationState* state = FindAnimationState(name);
  430. return state ? state->GetLength() : 0.0f;
  431. }
  432. float AnimationController::GetSpeed(const String& name) const
  433. {
  434. unsigned index;
  435. AnimationState* state;
  436. FindAnimation(name, index, state);
  437. if (index == M_MAX_UNSIGNED)
  438. return 0.0f;
  439. return animations_[index].speed_;
  440. }
  441. float AnimationController::GetFadeTarget(const String& name) const
  442. {
  443. unsigned index;
  444. AnimationState* state;
  445. FindAnimation(name, index, state);
  446. if (index == M_MAX_UNSIGNED)
  447. return 0.0f;
  448. return animations_[index].targetWeight_;
  449. }
  450. float AnimationController::GetFadeTime(const String& name) const
  451. {
  452. unsigned index;
  453. AnimationState* state;
  454. FindAnimation(name, index, state);
  455. if (index == M_MAX_UNSIGNED)
  456. return 0.0f;
  457. return animations_[index].targetWeight_;
  458. }
  459. float AnimationController::GetAutoFade(const String& name) const
  460. {
  461. unsigned index;
  462. AnimationState* state;
  463. FindAnimation(name, index, state);
  464. if (index == M_MAX_UNSIGNED)
  465. return 0.0f;
  466. return animations_[index].autoFadeTime_;
  467. }
  468. void AnimationController::OnNodeSet(Node* node)
  469. {
  470. if (node)
  471. {
  472. Scene* scene = node->GetScene();
  473. if (scene)
  474. SubscribeToEvent(scene, E_SCENEPOSTUPDATE, HANDLER(AnimationController, HandleScenePostUpdate));
  475. }
  476. }
  477. void AnimationController::FindAnimation(const String& name, unsigned& index, AnimationState*& state) const
  478. {
  479. AnimatedModel* model = GetComponent<AnimatedModel>();
  480. StringHash nameHash(name);
  481. // Find the AnimationState
  482. state = model ? model->GetAnimationState(nameHash) : 0;
  483. if (state)
  484. {
  485. // Either a resource name or animation name may be specified. We store resource names, so correct the hash if necessary
  486. nameHash = state->GetAnimation()->GetNameHash();
  487. }
  488. // Find the internal control structure
  489. index = M_MAX_UNSIGNED;
  490. for (unsigned i = 0; i < animations_.Size(); ++i)
  491. {
  492. if (animations_[i].hash_ == nameHash)
  493. {
  494. index = i;
  495. break;
  496. }
  497. }
  498. }
  499. AnimationState* AnimationController::FindAnimationState(const String& name) const
  500. {
  501. AnimatedModel* model = GetComponent<AnimatedModel>();
  502. return model ? model->GetAnimationState(name) : 0;
  503. }
  504. void AnimationController::HandleScenePostUpdate(StringHash eventType, VariantMap& eventData)
  505. {
  506. using namespace ScenePostUpdate;
  507. Update(eventData[P_TIMESTEP].GetFloat());
  508. }