AnimationController.cpp 30 KB

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