AnimationController.cpp 23 KB

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