AnimationController.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736
  1. //
  2. // Copyright (c) 2008-2013 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 "AnimatedModel.h"
  24. #include "Animation.h"
  25. #include "AnimationController.h"
  26. #include "AnimationState.h"
  27. #include "Context.h"
  28. #include "Log.h"
  29. #include "MemoryBuffer.h"
  30. #include "Profiler.h"
  31. #include "ResourceCache.h"
  32. #include "Scene.h"
  33. #include "SceneEvents.h"
  34. #include "DebugNew.h"
  35. namespace Urho3D
  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 float EXTRA_ANIM_FADEOUT_TIME = 0.1f;
  43. static const float COMMAND_STAY_TIME = 0.25f;
  44. OBJECTTYPESTATIC(AnimationController);
  45. AnimationController::AnimationController(Context* context) :
  46. Component(context)
  47. {
  48. }
  49. AnimationController::~AnimationController()
  50. {
  51. }
  52. void AnimationController::RegisterObject(Context* context)
  53. {
  54. context->RegisterFactory<AnimationController>();
  55. ACCESSOR_ATTRIBUTE(AnimationController, VAR_BOOL, "Is Enabled", IsEnabled, SetEnabled, bool, true, AM_DEFAULT);
  56. ACCESSOR_ATTRIBUTE(AnimationController, VAR_VARIANTVECTOR, "Animations", GetAnimationsAttr, SetAnimationsAttr, VariantVector, Variant::emptyVariantVector, AM_FILE | AM_NOEDIT);
  57. REF_ACCESSOR_ATTRIBUTE(AnimationController, VAR_BUFFER, "Network Animations", GetNetAnimationsAttr, SetNetAnimationsAttr, PODVector<unsigned char>, Variant::emptyBuffer, AM_NET | AM_LATESTDATA | AM_NOEDIT);
  58. }
  59. void AnimationController::OnSetEnabled()
  60. {
  61. Scene* scene = GetScene();
  62. if (scene)
  63. {
  64. if (IsEnabledEffective())
  65. SubscribeToEvent(scene, E_SCENEPOSTUPDATE, HANDLER(AnimationController, HandleScenePostUpdate));
  66. else
  67. UnsubscribeFromEvent(scene, E_SCENEPOSTUPDATE);
  68. }
  69. }
  70. void AnimationController::Update(float timeStep)
  71. {
  72. AnimatedModel* model = GetComponent<AnimatedModel>();
  73. if (!model)
  74. return;
  75. // Loop through animations
  76. for (Vector<AnimationControl>::Iterator i = animations_.Begin(); i != animations_.End();)
  77. {
  78. bool remove = false;
  79. AnimationState* state = model->GetAnimationState(i->hash_);
  80. if (!state)
  81. remove = true;
  82. else
  83. {
  84. // Advance the animation
  85. if (i->speed_ != 0.0f)
  86. state->AddTime(i->speed_ * timeStep);
  87. float targetWeight = i->targetWeight_;
  88. float fadeTime = i->fadeTime_;
  89. // If non-looped animation at the end, activate autofade as applicable
  90. if (!state->IsLooped() && state->GetTime() >= state->GetLength() && i->autoFadeTime_ > 0.0f)
  91. {
  92. targetWeight = 0.0f;
  93. fadeTime = i->autoFadeTime_;
  94. }
  95. // Process weight fade
  96. float currentWeight = state->GetWeight();
  97. if (currentWeight != targetWeight)
  98. {
  99. if (fadeTime > 0.0f)
  100. {
  101. float weightDelta = 1.0f / fadeTime * timeStep;
  102. if (currentWeight < targetWeight)
  103. currentWeight = Min(currentWeight + weightDelta, targetWeight);
  104. else if (currentWeight > targetWeight)
  105. currentWeight = Max(currentWeight - weightDelta, targetWeight);
  106. state->SetWeight(currentWeight);
  107. }
  108. else
  109. state->SetWeight(targetWeight);
  110. }
  111. // Remove if weight zero and target weight zero
  112. if (state->GetWeight() == 0.0f && (targetWeight == 0.0f || fadeTime == 0.0f))
  113. remove = true;
  114. }
  115. // Decrement the command time-to-live values
  116. if (i->setTimeTtl_ > 0.0f)
  117. i->setTimeTtl_ = Max(i->setTimeTtl_ - timeStep, 0.0f);
  118. if (i->setWeightTtl_ > 0.0f)
  119. i->setWeightTtl_ = Max(i->setWeightTtl_ - timeStep, 0.0f);
  120. if (remove)
  121. {
  122. if (state)
  123. model->RemoveAnimationState(state);
  124. i = animations_.Erase(i);
  125. MarkNetworkUpdate();
  126. }
  127. else
  128. ++i;
  129. }
  130. }
  131. bool AnimationController::Play(const String& name, unsigned char layer, bool looped, float fadeInTime)
  132. {
  133. AnimatedModel* model = GetComponent<AnimatedModel>();
  134. if (!model)
  135. return false;
  136. // Check if already exists
  137. unsigned index;
  138. AnimationState* state;
  139. FindAnimation(name, index, state);
  140. if (!state)
  141. {
  142. Animation* newAnimation = GetSubsystem<ResourceCache>()->GetResource<Animation>(name);
  143. state = model->AddAnimationState(newAnimation);
  144. if (!state)
  145. return false;
  146. }
  147. if (index == M_MAX_UNSIGNED)
  148. {
  149. AnimationControl newControl;
  150. Animation* animation = state->GetAnimation();
  151. newControl.hash_ = animation->GetNameHash();
  152. animations_.Push(newControl);
  153. index = animations_.Size() - 1;
  154. }
  155. state->SetLayer(layer);
  156. state->SetLooped(looped);
  157. animations_[index].targetWeight_ = 1.0f;
  158. animations_[index].fadeTime_ = fadeInTime;
  159. MarkNetworkUpdate();
  160. return true;
  161. }
  162. bool AnimationController::PlayExclusive(const String& name, unsigned char layer, bool looped, float fadeTime)
  163. {
  164. FadeOthers(name, 0.0f, fadeTime);
  165. return Play(name, layer, looped, fadeTime);
  166. }
  167. bool AnimationController::Stop(const String& name, float fadeOutTime)
  168. {
  169. AnimatedModel* model = GetComponent<AnimatedModel>();
  170. if (!model)
  171. return false;
  172. unsigned index;
  173. AnimationState* state;
  174. FindAnimation(name, index, state);
  175. if (index != M_MAX_UNSIGNED)
  176. {
  177. animations_[index].targetWeight_ = 0.0f;
  178. animations_[index].fadeTime_ = fadeOutTime;
  179. MarkNetworkUpdate();
  180. }
  181. return index != M_MAX_UNSIGNED || state != 0;
  182. }
  183. void AnimationController::StopLayer(unsigned char layer, float fadeOutTime)
  184. {
  185. AnimatedModel* model = GetComponent<AnimatedModel>();
  186. if (!model)
  187. return;
  188. bool needUpdate = false;
  189. for (Vector<AnimationControl>::Iterator i = animations_.Begin(); i != animations_.End(); ++i)
  190. {
  191. AnimationState* state = model->GetAnimationState(i->hash_);
  192. if (state && state->GetLayer() == layer)
  193. {
  194. i->targetWeight_ = 0.0f;
  195. i->fadeTime_ = fadeOutTime;
  196. needUpdate = true;
  197. }
  198. }
  199. if (needUpdate)
  200. MarkNetworkUpdate();
  201. }
  202. void AnimationController::StopAll(float fadeOutTime)
  203. {
  204. AnimatedModel* model = GetComponent<AnimatedModel>();
  205. if (!model)
  206. return;
  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. AnimatedModel* model = GetComponent<AnimatedModel>();
  237. unsigned char layer = state->GetLayer();
  238. bool needUpdate = false;
  239. for (unsigned i = 0; i < animations_.Size(); ++i)
  240. {
  241. if (i != index)
  242. {
  243. AnimationControl& control = animations_[i];
  244. AnimationState* otherState = model->GetAnimationState(control.hash_);
  245. if (otherState && otherState->GetLayer() == layer)
  246. {
  247. control.targetWeight_ = Clamp(targetWeight, 0.0f, 1.0f);
  248. control.fadeTime_ = fadeTime;
  249. needUpdate = true;
  250. }
  251. }
  252. }
  253. if (needUpdate)
  254. MarkNetworkUpdate();
  255. return true;
  256. }
  257. bool AnimationController::SetLayer(const String& name, unsigned char layer)
  258. {
  259. AnimationState* state = FindAnimationState(name);
  260. if (!state)
  261. return false;
  262. state->SetLayer(layer);
  263. MarkNetworkUpdate();
  264. return true;
  265. }
  266. bool AnimationController::SetStartBone(const String& name, const String& startBoneName)
  267. {
  268. AnimationState* state = FindAnimationState(name);
  269. if (!state)
  270. return false;
  271. AnimatedModel* model = GetComponent<AnimatedModel>();
  272. Bone* bone = model->GetSkeleton().GetBone(startBoneName);
  273. state->SetStartBone(bone);
  274. MarkNetworkUpdate();
  275. return true;
  276. }
  277. bool AnimationController::SetTime(const String& name, float time)
  278. {
  279. unsigned index;
  280. AnimationState* state;
  281. FindAnimation(name, index, state);
  282. if (index == M_MAX_UNSIGNED || !state)
  283. return false;
  284. time = Clamp(time, 0.0f, state->GetLength());
  285. state->SetTime(time);
  286. // Prepare "set time" command for network replication
  287. animations_[index].setTime_ = (unsigned short)(time / state->GetLength() * 65535.0f);
  288. animations_[index].setTimeTtl_ = COMMAND_STAY_TIME;
  289. ++animations_[index].setTimeRev_;
  290. MarkNetworkUpdate();
  291. return true;
  292. }
  293. bool AnimationController::SetSpeed(const String& name, float speed)
  294. {
  295. unsigned index;
  296. AnimationState* state;
  297. FindAnimation(name, index, state);
  298. if (index == M_MAX_UNSIGNED)
  299. return false;
  300. animations_[index].speed_ = speed;
  301. MarkNetworkUpdate();
  302. return true;
  303. }
  304. bool AnimationController::SetWeight(const String& name, float weight)
  305. {
  306. unsigned index;
  307. AnimationState* state;
  308. FindAnimation(name, index, state);
  309. if (index == M_MAX_UNSIGNED || !state)
  310. return false;
  311. weight = Clamp(weight, 0.0f, 1.0f);
  312. state->SetWeight(weight);
  313. // Prepare "set weight" command for network replication
  314. animations_[index].setWeight_ = (unsigned char)(weight * 255.0f);
  315. animations_[index].setWeightTtl_ = COMMAND_STAY_TIME;
  316. ++animations_[index].setWeightRev_;
  317. MarkNetworkUpdate();
  318. return true;
  319. }
  320. bool AnimationController::SetLooped(const String& name, bool enable)
  321. {
  322. AnimationState* state = FindAnimationState(name);
  323. if (!state)
  324. return false;
  325. state->SetLooped(enable);
  326. MarkNetworkUpdate();
  327. return true;
  328. }
  329. bool AnimationController::SetAutoFade(const String& name, float fadeOutTime)
  330. {
  331. unsigned index;
  332. AnimationState* state;
  333. FindAnimation(name, index, state);
  334. if (index == M_MAX_UNSIGNED)
  335. return false;
  336. animations_[index].autoFadeTime_ = Max(fadeOutTime, 0.0f);
  337. MarkNetworkUpdate();
  338. return true;
  339. }
  340. bool AnimationController::IsPlaying(const String& name) const
  341. {
  342. unsigned index;
  343. AnimationState* state;
  344. FindAnimation(name, index, state);
  345. return index != M_MAX_UNSIGNED;
  346. }
  347. bool AnimationController::IsFadingIn(const String& name) const
  348. {
  349. unsigned index;
  350. AnimationState* state;
  351. FindAnimation(name, index, state);
  352. if (index == M_MAX_UNSIGNED || !state)
  353. return false;
  354. return animations_[index].fadeTime_ && animations_[index].targetWeight_ > state->GetWeight();
  355. }
  356. bool AnimationController::IsFadingOut(const String& name) const
  357. {
  358. unsigned index;
  359. AnimationState* state;
  360. FindAnimation(name, index, state);
  361. if (index == M_MAX_UNSIGNED || !state)
  362. return false;
  363. return (animations_[index].fadeTime_ && animations_[index].targetWeight_ < state->GetWeight())
  364. || (!state->IsLooped() && state->GetTime() >= state->GetLength() && animations_[index].autoFadeTime_);
  365. }
  366. unsigned char AnimationController::GetLayer(const String& name) const
  367. {
  368. AnimationState* state = FindAnimationState(name);
  369. return state ? state->GetLayer() : 0;
  370. }
  371. Bone* AnimationController::GetStartBone(const String& name) const
  372. {
  373. AnimationState* state = FindAnimationState(name);
  374. return state ? state->GetStartBone() : 0;
  375. }
  376. const String& AnimationController::GetStartBoneName(const String& name) const
  377. {
  378. Bone* bone = GetStartBone(name);
  379. return bone ? bone->name_ : String::EMPTY;
  380. }
  381. float AnimationController::GetTime(const String& name) const
  382. {
  383. AnimationState* state = FindAnimationState(name);
  384. return state ? state->GetTime() : 0.0f;
  385. }
  386. float AnimationController::GetWeight(const String& name) const
  387. {
  388. AnimationState* state = FindAnimationState(name);
  389. return state ? state->GetWeight() : 0.0f;
  390. }
  391. bool AnimationController::IsLooped(const String& name) const
  392. {
  393. AnimationState* state = FindAnimationState(name);
  394. return state ? state->IsLooped() : false;
  395. }
  396. float AnimationController::GetLength(const String& name) const
  397. {
  398. AnimationState* state = FindAnimationState(name);
  399. return state ? state->GetLength() : 0.0f;
  400. }
  401. float AnimationController::GetSpeed(const String& name) const
  402. {
  403. unsigned index;
  404. AnimationState* state;
  405. FindAnimation(name, index, state);
  406. return index != M_MAX_UNSIGNED ? animations_[index].speed_ : 0.0f;
  407. }
  408. float AnimationController::GetFadeTarget(const String& name) const
  409. {
  410. unsigned index;
  411. AnimationState* state;
  412. FindAnimation(name, index, state);
  413. return index != M_MAX_UNSIGNED ? animations_[index].targetWeight_ : 0.0f;
  414. }
  415. float AnimationController::GetFadeTime(const String& name) const
  416. {
  417. unsigned index;
  418. AnimationState* state;
  419. FindAnimation(name, index, state);
  420. return index != M_MAX_UNSIGNED ? animations_[index].targetWeight_ : 0.0f;
  421. }
  422. float AnimationController::GetAutoFade(const String& name) const
  423. {
  424. unsigned index;
  425. AnimationState* state;
  426. FindAnimation(name, index, state);
  427. return index != M_MAX_UNSIGNED ? animations_[index].autoFadeTime_ : 0.0f;
  428. }
  429. void AnimationController::SetAnimationsAttr(VariantVector value)
  430. {
  431. animations_.Clear();
  432. animations_.Reserve(value.Size() / 5); // Incomplete data is discarded
  433. unsigned index = 0;
  434. while (index + 4 < value.Size()) // Prevent out-of-bound index access
  435. {
  436. AnimationControl newControl;
  437. newControl.hash_ = value[index++].GetStringHash();
  438. newControl.speed_ = value[index++].GetFloat();
  439. newControl.targetWeight_ = value[index++].GetFloat();
  440. newControl.fadeTime_ = value[index++].GetFloat();
  441. newControl.autoFadeTime_ = value[index++].GetFloat();
  442. animations_.Push(newControl);
  443. }
  444. }
  445. void AnimationController::SetNetAnimationsAttr(const PODVector<unsigned char>& value)
  446. {
  447. // To apply animations, the model must exist first (practically, have been added before AnimationController)
  448. AnimatedModel* model = GetComponent<AnimatedModel>();
  449. if (!model)
  450. return;
  451. MemoryBuffer buf(value);
  452. // Check which animations we need to remove
  453. HashSet<StringHash> processedAnimations;
  454. unsigned numAnimations = buf.ReadVLE();
  455. while (numAnimations--)
  456. {
  457. StringHash animHash = buf.ReadStringHash();
  458. processedAnimations.Insert(animHash);
  459. // Check if the animation state exists. If not, add new
  460. AnimationState* state = model->GetAnimationState(animHash);
  461. if (!state)
  462. {
  463. Animation* newAnimation = GetSubsystem<ResourceCache>()->GetResource<Animation>(animHash);
  464. state = model->AddAnimationState(newAnimation);
  465. if (!state)
  466. {
  467. LOGERROR("Animation update applying aborted due to unknown animation");
  468. return;
  469. }
  470. }
  471. // Check if the internal control structure exists. If not, add new
  472. unsigned index;
  473. for (index = 0; index < animations_.Size(); ++index)
  474. {
  475. if (animations_[index].hash_ == animHash)
  476. break;
  477. }
  478. if (index == animations_.Size())
  479. {
  480. AnimationControl newControl;
  481. newControl.hash_ = animHash;
  482. animations_.Push(newControl);
  483. }
  484. unsigned char ctrl = buf.ReadUByte();
  485. state->SetLayer(buf.ReadUByte());
  486. state->SetLooped((ctrl & CTRL_LOOPED) != 0);
  487. animations_[index].speed_ = (float)buf.ReadShort() / 2048.0f; // 11 bits of decimal precision, max. 16x playback speed
  488. animations_[index].targetWeight_ = (float)buf.ReadUByte() / 255.0f; // 8 bits of decimal precision
  489. animations_[index].fadeTime_ = (float)buf.ReadUByte() / 64.0f; // 6 bits of decimal precision, max. 4 seconds fade
  490. if (ctrl & CTRL_STARTBONE)
  491. state->SetStartBone(model->GetSkeleton().GetBone(buf.ReadStringHash()));
  492. else
  493. state->SetStartBone(0);
  494. if (ctrl & CTRL_AUTOFADE)
  495. animations_[index].autoFadeTime_ = (float)buf.ReadUByte() / 64.0f; // 6 bits of decimal precision, max. 4 seconds fade
  496. else
  497. animations_[index].autoFadeTime_ = 0.0f;
  498. if (ctrl & CTRL_SETTIME)
  499. {
  500. unsigned char setTimeRev = buf.ReadUByte();
  501. unsigned short setTime = buf.ReadUShort();
  502. // Apply set time command only if revision differs
  503. if (setTimeRev != animations_[index].setTimeRev_)
  504. {
  505. state->SetTime(((float)setTime / 65535.0f) * state->GetLength());
  506. animations_[index].setTimeRev_ = setTimeRev;
  507. }
  508. }
  509. if (ctrl & CTRL_SETWEIGHT)
  510. {
  511. unsigned char setWeightRev = buf.ReadUByte();
  512. unsigned char setWeight = buf.ReadUByte();
  513. // Apply set weight command only if revision differs
  514. if (setWeightRev != animations_[index].setWeightRev_)
  515. {
  516. state->SetWeight((float)setWeight / 255.0f);
  517. animations_[index].setWeightRev_ = setWeightRev;
  518. }
  519. }
  520. }
  521. // Set any extra animations to fade out
  522. for (Vector<AnimationControl>::Iterator i = animations_.Begin(); i != animations_.End(); ++i)
  523. {
  524. if (!processedAnimations.Contains(i->hash_))
  525. {
  526. i->targetWeight_ = 0.0f;
  527. i->fadeTime_ = EXTRA_ANIM_FADEOUT_TIME;
  528. }
  529. }
  530. }
  531. VariantVector AnimationController::GetAnimationsAttr() const
  532. {
  533. VariantVector ret;
  534. ret.Reserve(animations_.Size() * 5);
  535. for (Vector<AnimationControl>::ConstIterator i = animations_.Begin(); i != animations_.End(); ++i)
  536. {
  537. ret.Push(i->hash_);
  538. ret.Push(i->speed_);
  539. ret.Push(i->targetWeight_);
  540. ret.Push(i->fadeTime_);
  541. ret.Push(i->autoFadeTime_);
  542. }
  543. return ret;
  544. }
  545. const PODVector<unsigned char>& AnimationController::GetNetAnimationsAttr() const
  546. {
  547. attrBuffer_.Clear();
  548. AnimatedModel* model = GetComponent<AnimatedModel>();
  549. if (!model)
  550. {
  551. attrBuffer_.WriteVLE(0);
  552. return attrBuffer_.GetBuffer();
  553. }
  554. unsigned validAnimations = 0;
  555. for (Vector<AnimationControl>::ConstIterator i = animations_.Begin(); i != animations_.End(); ++i)
  556. {
  557. if (model->GetAnimationState(i->hash_))
  558. ++validAnimations;
  559. }
  560. attrBuffer_.WriteVLE(validAnimations);
  561. for (Vector<AnimationControl>::ConstIterator i = animations_.Begin(); i != animations_.End(); ++i)
  562. {
  563. AnimationState* state = model->GetAnimationState(i->hash_);
  564. if (!state)
  565. continue;
  566. unsigned char ctrl = 0;
  567. Bone* startBone = state->GetStartBone();
  568. if (state->IsLooped())
  569. ctrl |= CTRL_LOOPED;
  570. if (startBone && startBone != model->GetSkeleton().GetRootBone())
  571. ctrl |= CTRL_STARTBONE;
  572. if (i->autoFadeTime_ > 0.0f)
  573. ctrl |= CTRL_AUTOFADE;
  574. if (i->setTimeTtl_ > 0.0f)
  575. ctrl |= CTRL_SETTIME;
  576. if (i->setWeightTtl_ > 0.0f)
  577. ctrl |= CTRL_SETWEIGHT;
  578. attrBuffer_.WriteStringHash(i->hash_);
  579. attrBuffer_.WriteUByte(ctrl);
  580. attrBuffer_.WriteUByte(state->GetLayer());
  581. attrBuffer_.WriteShort((short)Clamp(i->speed_ * 2048.0f, -32767.0f, 32767.0f));
  582. attrBuffer_.WriteUByte((unsigned char)(i->targetWeight_ * 255.0f));
  583. attrBuffer_.WriteUByte((unsigned char)Clamp(i->fadeTime_ * 64.0f, 0.0f, 255.0f));
  584. if (ctrl & CTRL_STARTBONE)
  585. attrBuffer_.WriteStringHash(startBone->nameHash_);
  586. if (ctrl & CTRL_AUTOFADE)
  587. attrBuffer_.WriteUByte((unsigned char)Clamp(i->autoFadeTime_ * 64.0f, 0.0f, 255.0f));
  588. if (ctrl & CTRL_SETTIME)
  589. {
  590. attrBuffer_.WriteUByte(i->setTimeRev_);
  591. attrBuffer_.WriteUShort(i->setTime_);
  592. }
  593. if (ctrl & CTRL_SETWEIGHT)
  594. {
  595. attrBuffer_.WriteUByte(i->setWeightRev_);
  596. attrBuffer_.WriteUByte(i->setWeight_);
  597. }
  598. }
  599. return attrBuffer_.GetBuffer();
  600. }
  601. void AnimationController::OnNodeSet(Node* node)
  602. {
  603. if (node)
  604. {
  605. Scene* scene = GetScene();
  606. if (scene && IsEnabledEffective())
  607. SubscribeToEvent(scene, E_SCENEPOSTUPDATE, HANDLER(AnimationController, HandleScenePostUpdate));
  608. }
  609. }
  610. void AnimationController::FindAnimation(const String& name, unsigned& index, AnimationState*& state) const
  611. {
  612. AnimatedModel* model = GetComponent<AnimatedModel>();
  613. StringHash nameHash(name);
  614. // Find the AnimationState
  615. state = model ? model->GetAnimationState(nameHash) : 0;
  616. if (state)
  617. {
  618. // Either a resource name or animation name may be specified. We store resource names, so correct the hash if necessary
  619. nameHash = state->GetAnimation()->GetNameHash();
  620. }
  621. // Find the internal control structure
  622. index = M_MAX_UNSIGNED;
  623. for (unsigned i = 0; i < animations_.Size(); ++i)
  624. {
  625. if (animations_[i].hash_ == nameHash)
  626. {
  627. index = i;
  628. break;
  629. }
  630. }
  631. }
  632. AnimationState* AnimationController::FindAnimationState(const String& name) const
  633. {
  634. AnimatedModel* model = GetComponent<AnimatedModel>();
  635. return model ? model->GetAnimationState(StringHash(name)) : 0;
  636. }
  637. void AnimationController::HandleScenePostUpdate(StringHash eventType, VariantMap& eventData)
  638. {
  639. using namespace ScenePostUpdate;
  640. Update(eventData[P_TIMESTEP].GetFloat());
  641. }
  642. }