AnimationController.cpp 29 KB

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