AnimationController.cpp 23 KB

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