AnimationController.cpp 23 KB

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