AnimationController.cpp 22 KB

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