AnimationController.cpp 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071
  1. //
  2. // Copyright (c) 2008-2017 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 "../Core/Context.h"
  24. #include "../Core/Profiler.h"
  25. #include "../Graphics/AnimatedModel.h"
  26. #include "../Graphics/Animation.h"
  27. #include "../Graphics/AnimationController.h"
  28. #include "../Graphics/AnimationState.h"
  29. #include "../IO/FileSystem.h"
  30. #include "../IO/Log.h"
  31. #include "../IO/MemoryBuffer.h"
  32. #include "../Resource/ResourceCache.h"
  33. #include "../Scene/Scene.h"
  34. #include "../Scene/SceneEvents.h"
  35. #include "../DebugNew.h"
  36. namespace Atomic
  37. {
  38. static const unsigned char CTRL_LOOPED = 0x1;
  39. static const unsigned char CTRL_STARTBONE = 0x2;
  40. static const unsigned char CTRL_AUTOFADE = 0x4;
  41. static const unsigned char CTRL_SETTIME = 0x08;
  42. static const unsigned char CTRL_SETWEIGHT = 0x10;
  43. static const unsigned char CTRL_REMOVEONCOMPLETION = 0x20;
  44. static const unsigned char CTRL_ADDITIVE = 0x40;
  45. static const float EXTRA_ANIM_FADEOUT_TIME = 0.1f;
  46. static const float COMMAND_STAY_TIME = 0.25f;
  47. static const unsigned MAX_NODE_ANIMATION_STATES = 256;
  48. extern const char* LOGIC_CATEGORY;
  49. AnimationController::AnimationController(Context* context) :
  50. Component(context),
  51. // ATOMIC BEGIN
  52. animationResourcesAttr_(Animation::GetTypeStatic()),
  53. autoPlay_(true),
  54. autoPlayed_(false)
  55. // ATOMIC END
  56. {
  57. }
  58. AnimationController::~AnimationController()
  59. {
  60. }
  61. void AnimationController::RegisterObject(Context* context)
  62. {
  63. context->RegisterFactory<AnimationController>(LOGIC_CATEGORY);
  64. ATOMIC_ACCESSOR_ATTRIBUTE("Is Enabled", IsEnabled, SetEnabled, bool, true, AM_DEFAULT);
  65. ATOMIC_MIXED_ACCESSOR_ATTRIBUTE("Animations", GetAnimationsAttr, SetAnimationsAttr, VariantVector, Variant::emptyVariantVector,
  66. AM_FILE | AM_NOEDIT);
  67. ATOMIC_ACCESSOR_ATTRIBUTE("Network Animations", GetNetAnimationsAttr, SetNetAnimationsAttr, PODVector<unsigned char>,
  68. Variant::emptyBuffer, AM_NET | AM_LATESTDATA | AM_NOEDIT);
  69. ATOMIC_MIXED_ACCESSOR_ATTRIBUTE("Node Animation States", GetNodeAnimationStatesAttr, SetNodeAnimationStatesAttr, VariantVector,
  70. Variant::emptyVariantVector, AM_FILE | AM_NOEDIT);
  71. // ATOMIC BEGIN
  72. ATOMIC_MIXED_ACCESSOR_ATTRIBUTE("Animation", GetAnimationAttr, SetAnimationAttr, ResourceRef, ResourceRef(Animation::GetTypeStatic()), AM_DEFAULT);
  73. ATOMIC_ATTRIBUTE("Autoplay", bool, autoPlay_, true, AM_DEFAULT);
  74. ATOMIC_ACCESSOR_ATTRIBUTE("AnimationResources", GetAnimationResourcesAttr, SetAnimationResourcesAttr, ResourceRefList, ResourceRefList(Animation::GetTypeStatic()), AM_DEFAULT);
  75. // ATOMIC END
  76. }
  77. void AnimationController::OnSetEnabled()
  78. {
  79. Scene* scene = GetScene();
  80. if (scene)
  81. {
  82. if (IsEnabledEffective())
  83. SubscribeToEvent(scene, E_SCENEPOSTUPDATE, ATOMIC_HANDLER(AnimationController, HandleScenePostUpdate));
  84. else
  85. UnsubscribeFromEvent(scene, E_SCENEPOSTUPDATE);
  86. }
  87. }
  88. void AnimationController::Update(float timeStep)
  89. {
  90. // ATOMIC BEGIN
  91. if (autoPlay_ && !autoPlayed_ && animation_.NotNull())
  92. {
  93. autoPlayed_ = true;
  94. Play(animation_->GetAnimationName(), 0, true);
  95. }
  96. // ATOMIC END
  97. // Loop through animations
  98. for (unsigned i = 0; i < animations_.Size();)
  99. {
  100. AnimationControl& ctrl = animations_[i];
  101. AnimationState* state = GetAnimationState(ctrl.hash_);
  102. bool remove = false;
  103. if (!state)
  104. remove = true;
  105. else
  106. {
  107. // Advance the animation
  108. if (ctrl.speed_ != 0.0f)
  109. state->AddTime(ctrl.speed_ * timeStep);
  110. float targetWeight = ctrl.targetWeight_;
  111. float fadeTime = ctrl.fadeTime_;
  112. // If non-looped animation at the end, activate autofade as applicable
  113. if (!state->IsLooped() && state->GetTime() >= state->GetLength() && ctrl.autoFadeTime_ > 0.0f)
  114. {
  115. targetWeight = 0.0f;
  116. fadeTime = ctrl.autoFadeTime_;
  117. }
  118. // Process weight fade
  119. float currentWeight = state->GetWeight();
  120. if (currentWeight != targetWeight)
  121. {
  122. if (fadeTime > 0.0f)
  123. {
  124. float weightDelta = 1.0f / fadeTime * timeStep;
  125. if (currentWeight < targetWeight)
  126. currentWeight = Min(currentWeight + weightDelta, targetWeight);
  127. else if (currentWeight > targetWeight)
  128. currentWeight = Max(currentWeight - weightDelta, targetWeight);
  129. state->SetWeight(currentWeight);
  130. }
  131. else
  132. state->SetWeight(targetWeight);
  133. }
  134. // Remove if weight zero and target weight zero
  135. if (state->GetWeight() == 0.0f && (targetWeight == 0.0f || fadeTime == 0.0f) && ctrl.removeOnCompletion_)
  136. remove = true;
  137. }
  138. // Decrement the command time-to-live values
  139. if (ctrl.setTimeTtl_ > 0.0f)
  140. ctrl.setTimeTtl_ = Max(ctrl.setTimeTtl_ - timeStep, 0.0f);
  141. if (ctrl.setWeightTtl_ > 0.0f)
  142. ctrl.setWeightTtl_ = Max(ctrl.setWeightTtl_ - timeStep, 0.0f);
  143. if (remove)
  144. {
  145. if (state)
  146. RemoveAnimationState(state);
  147. animations_.Erase(i);
  148. MarkNetworkUpdate();
  149. }
  150. else
  151. ++i;
  152. }
  153. // Node hierarchy animations need to be applied manually
  154. for (Vector<SharedPtr<AnimationState> >::Iterator i = nodeAnimationStates_.Begin(); i != nodeAnimationStates_.End(); ++i)
  155. (*i)->Apply();
  156. }
  157. bool AnimationController::Play(const String& name, unsigned char layer, bool looped, float fadeInTime)
  158. {
  159. // ATOMIC BEGIN
  160. Animation* newAnimation = 0;
  161. // Check if we're using attached animation resource
  162. if (animation_.NotNull() && animation_->GetAnimationName() == name)
  163. {
  164. newAnimation = animation_;
  165. }
  166. else
  167. {
  168. for (unsigned i = 0; i < animationResources_.Size(); i++)
  169. {
  170. // ATOMIC BEGIN
  171. // Check whether the animation resource isn't loaded
  172. if (animationResources_[i] == NULL)
  173. continue;
  174. // ATOMIC END
  175. if (name == animationResources_[i]->GetAnimationName())
  176. {
  177. newAnimation = animationResources_[i];
  178. break;
  179. }
  180. }
  181. }
  182. // Get the animation resource first to be able to get the canonical resource name
  183. // (avoids potential adding of duplicate animations)
  184. if (!newAnimation)
  185. newAnimation = GetSubsystem<ResourceCache>()->GetResource<Animation>(name);
  186. if (!newAnimation)
  187. return false;
  188. // ATOMIC END
  189. // Check if already exists
  190. unsigned index;
  191. AnimationState* state;
  192. FindAnimation(newAnimation->GetName(), index, state);
  193. if (!state)
  194. {
  195. state = AddAnimationState(newAnimation);
  196. if (!state)
  197. return false;
  198. }
  199. if (index == M_MAX_UNSIGNED)
  200. {
  201. AnimationControl newControl;
  202. newControl.name_ = newAnimation->GetName();
  203. newControl.hash_ = newAnimation->GetNameHash();
  204. animations_.Push(newControl);
  205. index = animations_.Size() - 1;
  206. }
  207. state->SetLayer(layer);
  208. state->SetLooped(looped);
  209. animations_[index].targetWeight_ = 1.0f;
  210. animations_[index].fadeTime_ = fadeInTime;
  211. MarkNetworkUpdate();
  212. return true;
  213. }
  214. bool AnimationController::PlayExclusive(const String& name, unsigned char layer, bool looped, float fadeTime)
  215. {
  216. bool success = Play(name, layer, looped, fadeTime);
  217. // Fade other animations only if successfully started the new one
  218. if (success)
  219. FadeOthers(name, 0.0f, fadeTime);
  220. return success;
  221. }
  222. bool AnimationController::Stop(const String& name, float fadeOutTime)
  223. {
  224. unsigned index;
  225. AnimationState* state;
  226. FindAnimation(name, index, state);
  227. if (index != M_MAX_UNSIGNED)
  228. {
  229. animations_[index].targetWeight_ = 0.0f;
  230. animations_[index].fadeTime_ = fadeOutTime;
  231. MarkNetworkUpdate();
  232. }
  233. return index != M_MAX_UNSIGNED || state != 0;
  234. }
  235. void AnimationController::StopLayer(unsigned char layer, float fadeOutTime)
  236. {
  237. bool needUpdate = false;
  238. for (Vector<AnimationControl>::Iterator i = animations_.Begin(); i != animations_.End(); ++i)
  239. {
  240. AnimationState* state = GetAnimationState(i->hash_);
  241. if (state && state->GetLayer() == layer)
  242. {
  243. i->targetWeight_ = 0.0f;
  244. i->fadeTime_ = fadeOutTime;
  245. needUpdate = true;
  246. }
  247. }
  248. if (needUpdate)
  249. MarkNetworkUpdate();
  250. }
  251. void AnimationController::StopAll(float fadeOutTime)
  252. {
  253. if (animations_.Size())
  254. {
  255. for (Vector<AnimationControl>::Iterator i = animations_.Begin(); i != animations_.End(); ++i)
  256. {
  257. i->targetWeight_ = 0.0f;
  258. i->fadeTime_ = fadeOutTime;
  259. }
  260. MarkNetworkUpdate();
  261. }
  262. }
  263. bool AnimationController::Fade(const String& name, float targetWeight, float fadeTime)
  264. {
  265. unsigned index;
  266. AnimationState* state;
  267. FindAnimation(name, index, state);
  268. if (index == M_MAX_UNSIGNED)
  269. return false;
  270. animations_[index].targetWeight_ = Clamp(targetWeight, 0.0f, 1.0f);
  271. animations_[index].fadeTime_ = fadeTime;
  272. MarkNetworkUpdate();
  273. return true;
  274. }
  275. bool AnimationController::FadeOthers(const String& name, float targetWeight, float fadeTime)
  276. {
  277. unsigned index;
  278. AnimationState* state;
  279. FindAnimation(name, index, state);
  280. if (index == M_MAX_UNSIGNED || !state)
  281. return false;
  282. unsigned char layer = state->GetLayer();
  283. bool needUpdate = false;
  284. for (unsigned i = 0; i < animations_.Size(); ++i)
  285. {
  286. if (i != index)
  287. {
  288. AnimationControl& control = animations_[i];
  289. AnimationState* otherState = GetAnimationState(control.hash_);
  290. if (otherState && otherState->GetLayer() == layer)
  291. {
  292. control.targetWeight_ = Clamp(targetWeight, 0.0f, 1.0f);
  293. control.fadeTime_ = fadeTime;
  294. needUpdate = true;
  295. }
  296. }
  297. }
  298. if (needUpdate)
  299. MarkNetworkUpdate();
  300. return true;
  301. }
  302. bool AnimationController::SetLayer(const String& name, unsigned char layer)
  303. {
  304. AnimationState* state = GetAnimationState(name);
  305. if (!state)
  306. return false;
  307. state->SetLayer(layer);
  308. MarkNetworkUpdate();
  309. return true;
  310. }
  311. bool AnimationController::SetStartBone(const String& name, const String& startBoneName)
  312. {
  313. // Start bone can only be set in model mode
  314. AnimatedModel* model = GetComponent<AnimatedModel>();
  315. if (!model)
  316. return false;
  317. AnimationState* state = model->GetAnimationState(name);
  318. if (!state)
  319. return false;
  320. Bone* bone = model->GetSkeleton().GetBone(startBoneName);
  321. state->SetStartBone(bone);
  322. MarkNetworkUpdate();
  323. return true;
  324. }
  325. bool AnimationController::SetTime(const String& name, float time)
  326. {
  327. unsigned index;
  328. AnimationState* state;
  329. FindAnimation(name, index, state);
  330. if (index == M_MAX_UNSIGNED || !state)
  331. return false;
  332. time = Clamp(time, 0.0f, state->GetLength());
  333. state->SetTime(time);
  334. // Prepare "set time" command for network replication
  335. animations_[index].setTime_ = (unsigned short)(time / state->GetLength() * 65535.0f);
  336. animations_[index].setTimeTtl_ = COMMAND_STAY_TIME;
  337. ++animations_[index].setTimeRev_;
  338. MarkNetworkUpdate();
  339. return true;
  340. }
  341. bool AnimationController::SetSpeed(const String& name, float speed)
  342. {
  343. unsigned index;
  344. AnimationState* state;
  345. FindAnimation(name, index, state);
  346. if (index == M_MAX_UNSIGNED)
  347. return false;
  348. animations_[index].speed_ = speed;
  349. MarkNetworkUpdate();
  350. return true;
  351. }
  352. bool AnimationController::SetWeight(const String& name, float weight)
  353. {
  354. unsigned index;
  355. AnimationState* state;
  356. FindAnimation(name, index, state);
  357. if (index == M_MAX_UNSIGNED || !state)
  358. return false;
  359. weight = Clamp(weight, 0.0f, 1.0f);
  360. state->SetWeight(weight);
  361. // Prepare "set weight" command for network replication
  362. animations_[index].setWeight_ = (unsigned char)(weight * 255.0f);
  363. animations_[index].setWeightTtl_ = COMMAND_STAY_TIME;
  364. ++animations_[index].setWeightRev_;
  365. // Cancel any ongoing weight fade
  366. animations_[index].targetWeight_ = weight;
  367. animations_[index].fadeTime_ = 0.0f;
  368. MarkNetworkUpdate();
  369. return true;
  370. }
  371. bool AnimationController::SetRemoveOnCompletion(const String& name, bool removeOnCompletion)
  372. {
  373. unsigned index;
  374. AnimationState* state;
  375. FindAnimation(name, index, state);
  376. if (index == M_MAX_UNSIGNED || !state)
  377. return false;
  378. animations_[index].removeOnCompletion_ = removeOnCompletion;
  379. MarkNetworkUpdate();
  380. return true;
  381. }
  382. bool AnimationController::SetLooped(const String& name, bool enable)
  383. {
  384. AnimationState* state = GetAnimationState(name);
  385. if (!state)
  386. return false;
  387. state->SetLooped(enable);
  388. MarkNetworkUpdate();
  389. return true;
  390. }
  391. bool AnimationController::SetBlendMode(const String& name, AnimationBlendMode mode)
  392. {
  393. AnimationState* state = GetAnimationState(name);
  394. if (!state)
  395. return false;
  396. state->SetBlendMode(mode);
  397. MarkNetworkUpdate();
  398. return true;
  399. }
  400. bool AnimationController::SetAutoFade(const String& name, float fadeOutTime)
  401. {
  402. unsigned index;
  403. AnimationState* state;
  404. FindAnimation(name, index, state);
  405. if (index == M_MAX_UNSIGNED)
  406. return false;
  407. animations_[index].autoFadeTime_ = Max(fadeOutTime, 0.0f);
  408. MarkNetworkUpdate();
  409. return true;
  410. }
  411. bool AnimationController::IsPlaying(const String& name) const
  412. {
  413. unsigned index;
  414. AnimationState* state;
  415. FindAnimation(name, index, state);
  416. return index != M_MAX_UNSIGNED;
  417. }
  418. bool AnimationController::IsPlaying(unsigned char layer) const
  419. {
  420. for (Vector<AnimationControl>::ConstIterator i = animations_.Begin(); i != animations_.End(); ++i)
  421. {
  422. AnimationState* state = GetAnimationState(i->hash_);
  423. if (state && state->GetLayer() == layer)
  424. return true;
  425. }
  426. return false;
  427. }
  428. bool AnimationController::IsFadingIn(const String& name) const
  429. {
  430. unsigned index;
  431. AnimationState* state;
  432. FindAnimation(name, index, state);
  433. if (index == M_MAX_UNSIGNED || !state)
  434. return false;
  435. return animations_[index].fadeTime_ && animations_[index].targetWeight_ > state->GetWeight();
  436. }
  437. bool AnimationController::IsFadingOut(const String& name) const
  438. {
  439. unsigned index;
  440. AnimationState* state;
  441. FindAnimation(name, index, state);
  442. if (index == M_MAX_UNSIGNED || !state)
  443. return false;
  444. return (animations_[index].fadeTime_ && animations_[index].targetWeight_ < state->GetWeight())
  445. || (!state->IsLooped() && state->GetTime() >= state->GetLength() && animations_[index].autoFadeTime_);
  446. }
  447. bool AnimationController::IsAtEnd(const String& name) const
  448. {
  449. unsigned index;
  450. AnimationState* state;
  451. FindAnimation(name, index, state);
  452. if (index == M_MAX_UNSIGNED || !state)
  453. return false;
  454. else
  455. return state->GetTime() >= state->GetLength();
  456. }
  457. unsigned char AnimationController::GetLayer(const String& name) const
  458. {
  459. AnimationState* state = GetAnimationState(name);
  460. return (unsigned char)(state ? state->GetLayer() : 0);
  461. }
  462. Bone* AnimationController::GetStartBone(const String& name) const
  463. {
  464. AnimationState* state = GetAnimationState(name);
  465. return state ? state->GetStartBone() : 0;
  466. }
  467. const String& AnimationController::GetStartBoneName(const String& name) const
  468. {
  469. Bone* bone = GetStartBone(name);
  470. return bone ? bone->name_ : String::EMPTY;
  471. }
  472. float AnimationController::GetTime(const String& name) const
  473. {
  474. AnimationState* state = GetAnimationState(name);
  475. return state ? state->GetTime() : 0.0f;
  476. }
  477. float AnimationController::GetWeight(const String& name) const
  478. {
  479. AnimationState* state = GetAnimationState(name);
  480. return state ? state->GetWeight() : 0.0f;
  481. }
  482. bool AnimationController::IsLooped(const String& name) const
  483. {
  484. AnimationState* state = GetAnimationState(name);
  485. return state ? state->IsLooped() : false;
  486. }
  487. AnimationBlendMode AnimationController::GetBlendMode(const String& name) const
  488. {
  489. AnimationState* state = GetAnimationState(name);
  490. return state ? state->GetBlendMode() : ABM_LERP;
  491. }
  492. float AnimationController::GetLength(const String& name) const
  493. {
  494. AnimationState* state = GetAnimationState(name);
  495. return state ? state->GetLength() : 0.0f;
  496. }
  497. float AnimationController::GetSpeed(const String& name) const
  498. {
  499. unsigned index;
  500. AnimationState* state;
  501. FindAnimation(name, index, state);
  502. return index != M_MAX_UNSIGNED ? animations_[index].speed_ : 0.0f;
  503. }
  504. float AnimationController::GetFadeTarget(const String& name) const
  505. {
  506. unsigned index;
  507. AnimationState* state;
  508. FindAnimation(name, index, state);
  509. return index != M_MAX_UNSIGNED ? animations_[index].targetWeight_ : 0.0f;
  510. }
  511. float AnimationController::GetFadeTime(const String& name) const
  512. {
  513. unsigned index;
  514. AnimationState* state;
  515. FindAnimation(name, index, state);
  516. return index != M_MAX_UNSIGNED ? animations_[index].targetWeight_ : 0.0f;
  517. }
  518. float AnimationController::GetAutoFade(const String& name) const
  519. {
  520. unsigned index;
  521. AnimationState* state;
  522. FindAnimation(name, index, state);
  523. return index != M_MAX_UNSIGNED ? animations_[index].autoFadeTime_ : 0.0f;
  524. }
  525. bool AnimationController::GetRemoveOnCompletion(const String& name) const
  526. {
  527. unsigned index;
  528. AnimationState* state;
  529. FindAnimation(name, index, state);
  530. return index != M_MAX_UNSIGNED ? animations_[index].removeOnCompletion_ : false;
  531. }
  532. AnimationState* AnimationController::GetAnimationState(const String& name) const
  533. {
  534. return GetAnimationState(StringHash(name));
  535. }
  536. AnimationState* AnimationController::GetAnimationState(StringHash nameHash) const
  537. {
  538. // Model mode
  539. AnimatedModel* model = GetComponent<AnimatedModel>();
  540. if (model)
  541. return model->GetAnimationState(nameHash);
  542. // Node hierarchy mode
  543. for (Vector<SharedPtr<AnimationState> >::ConstIterator i = nodeAnimationStates_.Begin(); i != nodeAnimationStates_.End(); ++i)
  544. {
  545. Animation* animation = (*i)->GetAnimation();
  546. if (animation->GetNameHash() == nameHash || animation->GetAnimationNameHash() == nameHash)
  547. return *i;
  548. }
  549. return 0;
  550. }
  551. void AnimationController::SetAnimationsAttr(const VariantVector& value)
  552. {
  553. animations_.Clear();
  554. animations_.Reserve(value.Size() / 5); // Incomplete data is discarded
  555. unsigned index = 0;
  556. while (index + 4 < value.Size()) // Prevent out-of-bound index access
  557. {
  558. AnimationControl newControl;
  559. newControl.name_ = value[index++].GetString();
  560. newControl.hash_ = StringHash(newControl.name_);
  561. newControl.speed_ = value[index++].GetFloat();
  562. newControl.targetWeight_ = value[index++].GetFloat();
  563. newControl.fadeTime_ = value[index++].GetFloat();
  564. newControl.autoFadeTime_ = value[index++].GetFloat();
  565. animations_.Push(newControl);
  566. }
  567. }
  568. void AnimationController::SetNetAnimationsAttr(const PODVector<unsigned char>& value)
  569. {
  570. MemoryBuffer buf(value);
  571. AnimatedModel* model = GetComponent<AnimatedModel>();
  572. // Check which animations we need to remove
  573. HashSet<StringHash> processedAnimations;
  574. unsigned numAnimations = buf.ReadVLE();
  575. while (numAnimations--)
  576. {
  577. String animName = buf.ReadString();
  578. StringHash animHash(animName);
  579. processedAnimations.Insert(animHash);
  580. // Check if the animation state exists. If not, add new
  581. AnimationState* state = GetAnimationState(animHash);
  582. if (!state)
  583. {
  584. Animation* newAnimation = GetSubsystem<ResourceCache>()->GetResource<Animation>(animName);
  585. state = AddAnimationState(newAnimation);
  586. if (!state)
  587. {
  588. ATOMIC_LOGERROR("Animation update applying aborted due to unknown animation");
  589. return;
  590. }
  591. }
  592. // Check if the internal control structure exists. If not, add new
  593. unsigned index;
  594. for (index = 0; index < animations_.Size(); ++index)
  595. {
  596. if (animations_[index].hash_ == animHash)
  597. break;
  598. }
  599. if (index == animations_.Size())
  600. {
  601. AnimationControl newControl;
  602. newControl.name_ = animName;
  603. newControl.hash_ = animHash;
  604. animations_.Push(newControl);
  605. }
  606. unsigned char ctrl = buf.ReadUByte();
  607. state->SetLayer(buf.ReadUByte());
  608. state->SetLooped((ctrl & CTRL_LOOPED) != 0);
  609. state->SetBlendMode((ctrl & CTRL_ADDITIVE) != 0 ? ABM_ADDITIVE : ABM_LERP);
  610. animations_[index].speed_ = (float)buf.ReadShort() / 2048.0f; // 11 bits of decimal precision, max. 16x playback speed
  611. animations_[index].targetWeight_ = (float)buf.ReadUByte() / 255.0f; // 8 bits of decimal precision
  612. animations_[index].fadeTime_ = (float)buf.ReadUByte() / 64.0f; // 6 bits of decimal precision, max. 4 seconds fade
  613. if (ctrl & CTRL_STARTBONE)
  614. {
  615. StringHash boneHash = buf.ReadStringHash();
  616. if (model)
  617. state->SetStartBone(model->GetSkeleton().GetBone(boneHash));
  618. }
  619. else
  620. state->SetStartBone(0);
  621. if (ctrl & CTRL_AUTOFADE)
  622. animations_[index].autoFadeTime_ = (float)buf.ReadUByte() / 64.0f; // 6 bits of decimal precision, max. 4 seconds fade
  623. else
  624. animations_[index].autoFadeTime_ = 0.0f;
  625. animations_[index].removeOnCompletion_ = (ctrl & CTRL_REMOVEONCOMPLETION) != 0;
  626. if (ctrl & CTRL_SETTIME)
  627. {
  628. unsigned char setTimeRev = buf.ReadUByte();
  629. unsigned short setTime = buf.ReadUShort();
  630. // Apply set time command only if revision differs
  631. if (setTimeRev != animations_[index].setTimeRev_)
  632. {
  633. state->SetTime(((float)setTime / 65535.0f) * state->GetLength());
  634. animations_[index].setTimeRev_ = setTimeRev;
  635. }
  636. }
  637. if (ctrl & CTRL_SETWEIGHT)
  638. {
  639. unsigned char setWeightRev = buf.ReadUByte();
  640. unsigned char setWeight = buf.ReadUByte();
  641. // Apply set weight command only if revision differs
  642. if (setWeightRev != animations_[index].setWeightRev_)
  643. {
  644. state->SetWeight((float)setWeight / 255.0f);
  645. animations_[index].setWeightRev_ = setWeightRev;
  646. }
  647. }
  648. }
  649. // Set any extra animations to fade out
  650. for (Vector<AnimationControl>::Iterator i = animations_.Begin(); i != animations_.End(); ++i)
  651. {
  652. if (!processedAnimations.Contains(i->hash_))
  653. {
  654. i->targetWeight_ = 0.0f;
  655. i->fadeTime_ = EXTRA_ANIM_FADEOUT_TIME;
  656. }
  657. }
  658. }
  659. void AnimationController::SetNodeAnimationStatesAttr(const VariantVector& value)
  660. {
  661. ResourceCache* cache = GetSubsystem<ResourceCache>();
  662. nodeAnimationStates_.Clear();
  663. unsigned index = 0;
  664. unsigned numStates = index < value.Size() ? value[index++].GetUInt() : 0;
  665. // Prevent negative or overly large value being assigned from the editor
  666. if (numStates > M_MAX_INT)
  667. numStates = 0;
  668. if (numStates > MAX_NODE_ANIMATION_STATES)
  669. numStates = MAX_NODE_ANIMATION_STATES;
  670. nodeAnimationStates_.Reserve(numStates);
  671. while (numStates--)
  672. {
  673. if (index + 2 < value.Size())
  674. {
  675. // Note: null animation is allowed here for editing
  676. const ResourceRef& animRef = value[index++].GetResourceRef();
  677. SharedPtr<AnimationState> newState(new AnimationState(GetNode(), cache->GetResource<Animation>(animRef.name_)));
  678. nodeAnimationStates_.Push(newState);
  679. newState->SetLooped(value[index++].GetBool());
  680. newState->SetTime(value[index++].GetFloat());
  681. }
  682. else
  683. {
  684. // If not enough data, just add an empty animation state
  685. SharedPtr<AnimationState> newState(new AnimationState(GetNode(), 0));
  686. nodeAnimationStates_.Push(newState);
  687. }
  688. }
  689. }
  690. VariantVector AnimationController::GetAnimationsAttr() const
  691. {
  692. VariantVector ret;
  693. ret.Reserve(animations_.Size() * 5);
  694. for (Vector<AnimationControl>::ConstIterator i = animations_.Begin(); i != animations_.End(); ++i)
  695. {
  696. ret.Push(i->name_);
  697. ret.Push(i->speed_);
  698. ret.Push(i->targetWeight_);
  699. ret.Push(i->fadeTime_);
  700. ret.Push(i->autoFadeTime_);
  701. }
  702. return ret;
  703. }
  704. const PODVector<unsigned char>& AnimationController::GetNetAnimationsAttr() const
  705. {
  706. attrBuffer_.Clear();
  707. AnimatedModel* model = GetComponent<AnimatedModel>();
  708. unsigned validAnimations = 0;
  709. for (Vector<AnimationControl>::ConstIterator i = animations_.Begin(); i != animations_.End(); ++i)
  710. {
  711. if (GetAnimationState(i->hash_))
  712. ++validAnimations;
  713. }
  714. attrBuffer_.WriteVLE(validAnimations);
  715. for (Vector<AnimationControl>::ConstIterator i = animations_.Begin(); i != animations_.End(); ++i)
  716. {
  717. AnimationState* state = GetAnimationState(i->hash_);
  718. if (!state)
  719. continue;
  720. unsigned char ctrl = 0;
  721. Bone* startBone = state->GetStartBone();
  722. if (state->IsLooped())
  723. ctrl |= CTRL_LOOPED;
  724. if (state->GetBlendMode() == ABM_ADDITIVE)
  725. ctrl |= CTRL_ADDITIVE;
  726. if (startBone && model && startBone != model->GetSkeleton().GetRootBone())
  727. ctrl |= CTRL_STARTBONE;
  728. if (i->autoFadeTime_ > 0.0f)
  729. ctrl |= CTRL_AUTOFADE;
  730. if (i->removeOnCompletion_)
  731. ctrl |= CTRL_REMOVEONCOMPLETION;
  732. if (i->setTimeTtl_ > 0.0f)
  733. ctrl |= CTRL_SETTIME;
  734. if (i->setWeightTtl_ > 0.0f)
  735. ctrl |= CTRL_SETWEIGHT;
  736. attrBuffer_.WriteString(i->name_);
  737. attrBuffer_.WriteUByte(ctrl);
  738. attrBuffer_.WriteUByte(state->GetLayer());
  739. attrBuffer_.WriteShort((short)Clamp(i->speed_ * 2048.0f, -32767.0f, 32767.0f));
  740. attrBuffer_.WriteUByte((unsigned char)(i->targetWeight_ * 255.0f));
  741. attrBuffer_.WriteUByte((unsigned char)Clamp(i->fadeTime_ * 64.0f, 0.0f, 255.0f));
  742. if (ctrl & CTRL_STARTBONE)
  743. attrBuffer_.WriteStringHash(startBone->nameHash_);
  744. if (ctrl & CTRL_AUTOFADE)
  745. attrBuffer_.WriteUByte((unsigned char)Clamp(i->autoFadeTime_ * 64.0f, 0.0f, 255.0f));
  746. if (ctrl & CTRL_SETTIME)
  747. {
  748. attrBuffer_.WriteUByte(i->setTimeRev_);
  749. attrBuffer_.WriteUShort(i->setTime_);
  750. }
  751. if (ctrl & CTRL_SETWEIGHT)
  752. {
  753. attrBuffer_.WriteUByte(i->setWeightRev_);
  754. attrBuffer_.WriteUByte(i->setWeight_);
  755. }
  756. }
  757. return attrBuffer_.GetBuffer();
  758. }
  759. VariantVector AnimationController::GetNodeAnimationStatesAttr() const
  760. {
  761. VariantVector ret;
  762. ret.Reserve(nodeAnimationStates_.Size() * 3 + 1);
  763. ret.Push(nodeAnimationStates_.Size());
  764. for (Vector<SharedPtr<AnimationState> >::ConstIterator i = nodeAnimationStates_.Begin(); i != nodeAnimationStates_.End(); ++i)
  765. {
  766. AnimationState* state = *i;
  767. Animation* animation = state->GetAnimation();
  768. ret.Push(GetResourceRef(animation, Animation::GetTypeStatic()));
  769. ret.Push(state->IsLooped());
  770. ret.Push(state->GetTime());
  771. }
  772. return ret;
  773. }
  774. void AnimationController::OnSceneSet(Scene* scene)
  775. {
  776. if (scene && IsEnabledEffective())
  777. SubscribeToEvent(scene, E_SCENEPOSTUPDATE, ATOMIC_HANDLER(AnimationController, HandleScenePostUpdate));
  778. else if (!scene)
  779. UnsubscribeFromEvent(E_SCENEPOSTUPDATE);
  780. }
  781. AnimationState* AnimationController::AddAnimationState(Animation* animation)
  782. {
  783. if (!animation)
  784. return 0;
  785. // Model mode
  786. AnimatedModel* model = GetComponent<AnimatedModel>();
  787. if (model)
  788. return model->AddAnimationState(animation);
  789. // Node hierarchy mode
  790. SharedPtr<AnimationState> newState(new AnimationState(node_, animation));
  791. nodeAnimationStates_.Push(newState);
  792. return newState;
  793. }
  794. void AnimationController::RemoveAnimationState(AnimationState* state)
  795. {
  796. if (!state)
  797. return;
  798. // Model mode
  799. AnimatedModel* model = GetComponent<AnimatedModel>();
  800. if (model)
  801. {
  802. model->RemoveAnimationState(state);
  803. return;
  804. }
  805. // Node hierarchy mode
  806. for (Vector<SharedPtr<AnimationState> >::Iterator i = nodeAnimationStates_.Begin(); i != nodeAnimationStates_.End(); ++i)
  807. {
  808. if ((*i) == state)
  809. {
  810. nodeAnimationStates_.Erase(i);
  811. return;
  812. }
  813. }
  814. }
  815. void AnimationController::FindAnimation(const String& name, unsigned& index, AnimationState*& state) const
  816. {
  817. StringHash nameHash(GetInternalPath(name));
  818. // ATOMIC BEGIN
  819. // Check if we're using attached animation resource
  820. if (animation_.NotNull() && animation_->GetAnimationName() == name)
  821. {
  822. nameHash = animation_->GetName();
  823. }
  824. else
  825. {
  826. for (unsigned i = 0; i < animationResources_.Size(); i++)
  827. {
  828. // ATOMIC BEGIN
  829. // Check whether the animation resource isn't loaded
  830. if (animationResources_[i] == NULL)
  831. continue;
  832. // ATOMIC END
  833. if (name == animationResources_[i]->GetAnimationName())
  834. {
  835. nameHash = animationResources_[i]->GetName();
  836. break;
  837. }
  838. }
  839. }
  840. // ATOMIC END
  841. // Find the AnimationState
  842. state = GetAnimationState(nameHash);
  843. if (state)
  844. {
  845. // Either a resource name or animation name may be specified. We store resource names, so correct the hash if necessary
  846. nameHash = state->GetAnimation()->GetNameHash();
  847. }
  848. // Find the internal control structure
  849. index = M_MAX_UNSIGNED;
  850. for (unsigned i = 0; i < animations_.Size(); ++i)
  851. {
  852. if (animations_[i].hash_ == nameHash)
  853. {
  854. index = i;
  855. break;
  856. }
  857. }
  858. }
  859. void AnimationController::HandleScenePostUpdate(StringHash eventType, VariantMap& eventData)
  860. {
  861. using namespace ScenePostUpdate;
  862. Update(eventData[P_TIMESTEP].GetFloat());
  863. }
  864. // ATOMIC BEGIN
  865. void AnimationController::SetAnimationAttr(const ResourceRef& value)
  866. {
  867. ResourceCache* cache = GetSubsystem<ResourceCache>();
  868. animation_ = cache->GetResource<Animation>(value.name_);
  869. }
  870. ResourceRef AnimationController::GetAnimationAttr() const
  871. {
  872. return GetResourceRef(animation_, Animation::GetTypeStatic());
  873. }
  874. void AnimationController::AddAnimationResource(Animation* animation)
  875. {
  876. if (!animation)
  877. return;
  878. SharedPtr<Animation> anim(animation);
  879. if (!animationResources_.Contains(anim))
  880. animationResources_.Push(anim);
  881. }
  882. void AnimationController::RemoveAnimationResource(Animation* animation)
  883. {
  884. if (!animation)
  885. return;
  886. animationResources_.Remove(SharedPtr<Animation>(animation));
  887. }
  888. void AnimationController::ClearAnimationResources()
  889. {
  890. animationResources_.Clear();
  891. }
  892. void AnimationController::SetAnimationResourcesAttr(const ResourceRefList& value)
  893. {
  894. animationResources_.Clear();
  895. ResourceCache* cache = GetSubsystem<ResourceCache>();
  896. for (unsigned i = 0; i < value.names_.Size(); ++i)
  897. {
  898. Animation* animation = cache->GetResource<Animation>(value.names_[i]);
  899. if (!animation)
  900. {
  901. //LOGERRORF("AnimationController::SetAnimationResourcesAttr - Unable to load animation: %s", value.names_[i].CString());
  902. animationResources_.Push(SharedPtr<Animation>(nullptr));
  903. }
  904. else
  905. {
  906. animationResources_.Push(SharedPtr<Animation>(animation));
  907. }
  908. }
  909. }
  910. const ResourceRefList& AnimationController::GetAnimationResourcesAttr() const
  911. {
  912. animationResourcesAttr_.names_.Resize(animationResources_.Size());
  913. for (unsigned i = 0; i < animationResources_.Size(); ++i)
  914. animationResourcesAttr_.names_[i] = GetResourceName(animationResources_[i]);
  915. return animationResourcesAttr_;
  916. }
  917. // ATOMIC END
  918. }