AnimationController.cpp 23 KB

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