AnimationController.cpp 22 KB

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