AnimationController.cpp 23 KB

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