AnimationController.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712
  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. REF_ACCESSOR_ATTRIBUTE(AnimationController, VAR_BUFFER, "Animations", GetAnimationsAttr, SetAnimationsAttr, PODVector<unsigned char>, PODVector<unsigned char>(), 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(const PODVector<unsigned char>& value)
  409. {
  410. MemoryBuffer buf(value);
  411. animations_.Resize(buf.ReadVLE());
  412. for (Vector<AnimationControl>::Iterator i = animations_.Begin(); i != animations_.End(); ++i)
  413. {
  414. i->hash_ = buf.ReadStringHash();
  415. i->speed_ = buf.ReadFloat();
  416. i->targetWeight_ = buf.ReadFloat();
  417. i->fadeTime_ = buf.ReadFloat();
  418. i->autoFadeTime_ = buf.ReadFloat();
  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. state->SetUseNlerp((ctrl & CTRL_NLERP) != 0);
  465. animations_[index].speed_ = (float)buf.ReadShort() / 2048.0f; // 11 bits of decimal precision, max. 16x playback speed
  466. animations_[index].targetWeight_ = (float)buf.ReadUByte() / 255.0f; // 8 bits of decimal precision
  467. animations_[index].fadeTime_ = (float)buf.ReadUByte() / 64.0f; // 6 bits of decimal precision, max. 4 seconds fade
  468. if (ctrl & CTRL_STARTBONE)
  469. state->SetStartBone(model->GetSkeleton().GetBone(buf.ReadStringHash()));
  470. else
  471. state->SetStartBone(0);
  472. if (ctrl & CTRL_AUTOFADE)
  473. animations_[index].autoFadeTime_ = (float)buf.ReadUByte() / 64.0f; // 6 bits of decimal precision, max. 4 seconds fade
  474. else
  475. animations_[index].autoFadeTime_ = 0.0f;
  476. if (ctrl & CTRL_SETTIME)
  477. {
  478. unsigned char setTimeRev = buf.ReadUByte();
  479. unsigned short setTime = buf.ReadUShort();
  480. // Apply set time command only if revision differs
  481. if (setTimeRev != animations_[index].setTimeRev_)
  482. {
  483. state->SetTime(((float)setTime / 65535.0f) * state->GetLength());
  484. animations_[index].setTimeRev_ = setTimeRev;
  485. }
  486. }
  487. if (ctrl & CTRL_SETWEIGHT)
  488. {
  489. unsigned char setWeightRev = buf.ReadUByte();
  490. unsigned char setWeight = buf.ReadUByte();
  491. // Apply set weight command only if revision differs
  492. if (setWeightRev != animations_[index].setWeightRev_)
  493. {
  494. state->SetWeight((float)setWeight / 255.0f);
  495. animations_[index].setWeightRev_ = setWeightRev;
  496. }
  497. }
  498. }
  499. // Set any extra animations to fade out
  500. for (Vector<AnimationControl>::Iterator i = animations_.Begin(); i != animations_.End(); ++i)
  501. {
  502. if (!processedAnimations.Contains(i->hash_))
  503. {
  504. i->targetWeight_ = 0.0f;
  505. i->fadeTime_ = EXTRA_ANIM_FADEOUT_TIME;
  506. }
  507. }
  508. }
  509. const PODVector<unsigned char>& AnimationController::GetAnimationsAttr() const
  510. {
  511. attrBuffer_.Clear();
  512. attrBuffer_.WriteVLE(animations_.Size());
  513. for (Vector<AnimationControl>::ConstIterator i = animations_.Begin(); i != animations_.End(); ++i)
  514. {
  515. attrBuffer_.WriteStringHash(i->hash_);
  516. attrBuffer_.WriteFloat(i->speed_);
  517. attrBuffer_.WriteFloat(i->targetWeight_);
  518. attrBuffer_.WriteFloat(i->fadeTime_);
  519. attrBuffer_.WriteFloat(i->autoFadeTime_);
  520. }
  521. return attrBuffer_.GetBuffer();
  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. }