AnimationController.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738
  1. //
  2. // Copyright (c) 2008-2013 the Urho3D project.
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #include "Precompiled.h"
  23. #include "AnimatedModel.h"
  24. #include "Animation.h"
  25. #include "AnimationController.h"
  26. #include "AnimationState.h"
  27. #include "Context.h"
  28. #include "Log.h"
  29. #include "MemoryBuffer.h"
  30. #include "Profiler.h"
  31. #include "ResourceCache.h"
  32. #include "Scene.h"
  33. #include "SceneEvents.h"
  34. #include "DebugNew.h"
  35. namespace Urho3D
  36. {
  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. extern const char* ANIMATION_CATEGORY;
  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>(ANIMATION_CATEGORY);
  56. ACCESSOR_ATTRIBUTE(AnimationController, VAR_BOOL, "Is Enabled", IsEnabled, SetEnabled, bool, true, AM_DEFAULT);
  57. ACCESSOR_ATTRIBUTE(AnimationController, VAR_VARIANTVECTOR, "Animations", GetAnimationsAttr, SetAnimationsAttr, VariantVector, Variant::emptyVariantVector, AM_FILE | AM_NOEDIT);
  58. REF_ACCESSOR_ATTRIBUTE(AnimationController, VAR_BUFFER, "Network Animations", GetNetAnimationsAttr, SetNetAnimationsAttr, PODVector<unsigned char>, Variant::emptyBuffer, AM_NET | AM_LATESTDATA | AM_NOEDIT);
  59. }
  60. void AnimationController::OnSetEnabled()
  61. {
  62. Scene* scene = GetScene();
  63. if (scene)
  64. {
  65. if (IsEnabledEffective())
  66. SubscribeToEvent(scene, E_SCENEPOSTUPDATE, HANDLER(AnimationController, HandleScenePostUpdate));
  67. else
  68. UnsubscribeFromEvent(scene, E_SCENEPOSTUPDATE);
  69. }
  70. }
  71. void AnimationController::Update(float timeStep)
  72. {
  73. AnimatedModel* model = GetComponent<AnimatedModel>();
  74. if (!model)
  75. return;
  76. // Loop through animations
  77. for (Vector<AnimationControl>::Iterator i = animations_.Begin(); i != animations_.End();)
  78. {
  79. bool remove = false;
  80. AnimationState* state = model->GetAnimationState(i->hash_);
  81. if (!state)
  82. remove = true;
  83. else
  84. {
  85. // Advance the animation
  86. if (i->speed_ != 0.0f)
  87. state->AddTime(i->speed_ * timeStep);
  88. float targetWeight = i->targetWeight_;
  89. float fadeTime = i->fadeTime_;
  90. // If non-looped animation at the end, activate autofade as applicable
  91. if (!state->IsLooped() && state->GetTime() >= state->GetLength() && i->autoFadeTime_ > 0.0f)
  92. {
  93. targetWeight = 0.0f;
  94. fadeTime = i->autoFadeTime_;
  95. }
  96. // Process weight fade
  97. float currentWeight = state->GetWeight();
  98. if (currentWeight != targetWeight)
  99. {
  100. if (fadeTime > 0.0f)
  101. {
  102. float weightDelta = 1.0f / fadeTime * timeStep;
  103. if (currentWeight < targetWeight)
  104. currentWeight = Min(currentWeight + weightDelta, targetWeight);
  105. else if (currentWeight > targetWeight)
  106. currentWeight = Max(currentWeight - weightDelta, targetWeight);
  107. state->SetWeight(currentWeight);
  108. }
  109. else
  110. state->SetWeight(targetWeight);
  111. }
  112. // Remove if weight zero and target weight zero
  113. if (state->GetWeight() == 0.0f && (targetWeight == 0.0f || fadeTime == 0.0f))
  114. remove = true;
  115. }
  116. // Decrement the command time-to-live values
  117. if (i->setTimeTtl_ > 0.0f)
  118. i->setTimeTtl_ = Max(i->setTimeTtl_ - timeStep, 0.0f);
  119. if (i->setWeightTtl_ > 0.0f)
  120. i->setWeightTtl_ = Max(i->setWeightTtl_ - timeStep, 0.0f);
  121. if (remove)
  122. {
  123. if (state)
  124. model->RemoveAnimationState(state);
  125. i = animations_.Erase(i);
  126. MarkNetworkUpdate();
  127. }
  128. else
  129. ++i;
  130. }
  131. }
  132. bool AnimationController::Play(const String& name, unsigned char layer, bool looped, float fadeInTime)
  133. {
  134. AnimatedModel* model = GetComponent<AnimatedModel>();
  135. if (!model)
  136. return false;
  137. // Check if already exists
  138. unsigned index;
  139. AnimationState* state;
  140. FindAnimation(name, index, state);
  141. if (!state)
  142. {
  143. Animation* newAnimation = GetSubsystem<ResourceCache>()->GetResource<Animation>(name);
  144. state = model->AddAnimationState(newAnimation);
  145. if (!state)
  146. return false;
  147. }
  148. if (index == M_MAX_UNSIGNED)
  149. {
  150. AnimationControl newControl;
  151. Animation* animation = state->GetAnimation();
  152. newControl.hash_ = animation->GetNameHash();
  153. animations_.Push(newControl);
  154. index = animations_.Size() - 1;
  155. }
  156. state->SetLayer(layer);
  157. state->SetLooped(looped);
  158. animations_[index].targetWeight_ = 1.0f;
  159. animations_[index].fadeTime_ = fadeInTime;
  160. MarkNetworkUpdate();
  161. return true;
  162. }
  163. bool AnimationController::PlayExclusive(const String& name, unsigned char layer, bool looped, float fadeTime)
  164. {
  165. FadeOthers(name, 0.0f, fadeTime);
  166. return Play(name, layer, looped, fadeTime);
  167. }
  168. bool AnimationController::Stop(const String& name, float fadeOutTime)
  169. {
  170. AnimatedModel* model = GetComponent<AnimatedModel>();
  171. if (!model)
  172. return false;
  173. unsigned index;
  174. AnimationState* state;
  175. FindAnimation(name, index, state);
  176. if (index != M_MAX_UNSIGNED)
  177. {
  178. animations_[index].targetWeight_ = 0.0f;
  179. animations_[index].fadeTime_ = fadeOutTime;
  180. MarkNetworkUpdate();
  181. }
  182. return index != M_MAX_UNSIGNED || state != 0;
  183. }
  184. void AnimationController::StopLayer(unsigned char layer, float fadeOutTime)
  185. {
  186. AnimatedModel* model = GetComponent<AnimatedModel>();
  187. if (!model)
  188. return;
  189. bool needUpdate = false;
  190. for (Vector<AnimationControl>::Iterator i = animations_.Begin(); i != animations_.End(); ++i)
  191. {
  192. AnimationState* state = model->GetAnimationState(i->hash_);
  193. if (state && state->GetLayer() == layer)
  194. {
  195. i->targetWeight_ = 0.0f;
  196. i->fadeTime_ = fadeOutTime;
  197. needUpdate = true;
  198. }
  199. }
  200. if (needUpdate)
  201. MarkNetworkUpdate();
  202. }
  203. void AnimationController::StopAll(float fadeOutTime)
  204. {
  205. AnimatedModel* model = GetComponent<AnimatedModel>();
  206. if (!model)
  207. return;
  208. if (animations_.Size())
  209. {
  210. for (Vector<AnimationControl>::Iterator i = animations_.Begin(); i != animations_.End(); ++i)
  211. {
  212. i->targetWeight_ = 0.0f;
  213. i->fadeTime_ = fadeOutTime;
  214. }
  215. MarkNetworkUpdate();
  216. }
  217. }
  218. bool AnimationController::Fade(const String& name, float targetWeight, float fadeTime)
  219. {
  220. unsigned index;
  221. AnimationState* state;
  222. FindAnimation(name, index, state);
  223. if (index == M_MAX_UNSIGNED)
  224. return false;
  225. animations_[index].targetWeight_ = Clamp(targetWeight, 0.0f, 1.0f);
  226. animations_[index].fadeTime_ = fadeTime;
  227. MarkNetworkUpdate();
  228. return true;
  229. }
  230. bool AnimationController::FadeOthers(const String& name, float targetWeight, float fadeTime)
  231. {
  232. unsigned index;
  233. AnimationState* state;
  234. FindAnimation(name, index, state);
  235. if (index == M_MAX_UNSIGNED || !state)
  236. return false;
  237. AnimatedModel* model = GetComponent<AnimatedModel>();
  238. unsigned char layer = state->GetLayer();
  239. bool needUpdate = false;
  240. for (unsigned i = 0; i < animations_.Size(); ++i)
  241. {
  242. if (i != index)
  243. {
  244. AnimationControl& control = animations_[i];
  245. AnimationState* otherState = model->GetAnimationState(control.hash_);
  246. if (otherState && otherState->GetLayer() == layer)
  247. {
  248. control.targetWeight_ = Clamp(targetWeight, 0.0f, 1.0f);
  249. control.fadeTime_ = fadeTime;
  250. needUpdate = true;
  251. }
  252. }
  253. }
  254. if (needUpdate)
  255. MarkNetworkUpdate();
  256. return true;
  257. }
  258. bool AnimationController::SetLayer(const String& name, unsigned char layer)
  259. {
  260. AnimationState* state = FindAnimationState(name);
  261. if (!state)
  262. return false;
  263. state->SetLayer(layer);
  264. MarkNetworkUpdate();
  265. return true;
  266. }
  267. bool AnimationController::SetStartBone(const String& name, const String& startBoneName)
  268. {
  269. AnimationState* state = FindAnimationState(name);
  270. if (!state)
  271. return false;
  272. AnimatedModel* model = GetComponent<AnimatedModel>();
  273. Bone* bone = model->GetSkeleton().GetBone(startBoneName);
  274. state->SetStartBone(bone);
  275. MarkNetworkUpdate();
  276. return true;
  277. }
  278. bool AnimationController::SetTime(const String& name, float time)
  279. {
  280. unsigned index;
  281. AnimationState* state;
  282. FindAnimation(name, index, state);
  283. if (index == M_MAX_UNSIGNED || !state)
  284. return false;
  285. time = Clamp(time, 0.0f, state->GetLength());
  286. state->SetTime(time);
  287. // Prepare "set time" command for network replication
  288. animations_[index].setTime_ = (unsigned short)(time / state->GetLength() * 65535.0f);
  289. animations_[index].setTimeTtl_ = COMMAND_STAY_TIME;
  290. ++animations_[index].setTimeRev_;
  291. MarkNetworkUpdate();
  292. return true;
  293. }
  294. bool AnimationController::SetSpeed(const String& name, float speed)
  295. {
  296. unsigned index;
  297. AnimationState* state;
  298. FindAnimation(name, index, state);
  299. if (index == M_MAX_UNSIGNED)
  300. return false;
  301. animations_[index].speed_ = speed;
  302. MarkNetworkUpdate();
  303. return true;
  304. }
  305. bool AnimationController::SetWeight(const String& name, float weight)
  306. {
  307. unsigned index;
  308. AnimationState* state;
  309. FindAnimation(name, index, state);
  310. if (index == M_MAX_UNSIGNED || !state)
  311. return false;
  312. weight = Clamp(weight, 0.0f, 1.0f);
  313. state->SetWeight(weight);
  314. // Prepare "set weight" command for network replication
  315. animations_[index].setWeight_ = (unsigned char)(weight * 255.0f);
  316. animations_[index].setWeightTtl_ = COMMAND_STAY_TIME;
  317. ++animations_[index].setWeightRev_;
  318. MarkNetworkUpdate();
  319. return true;
  320. }
  321. bool AnimationController::SetLooped(const String& name, bool enable)
  322. {
  323. AnimationState* state = FindAnimationState(name);
  324. if (!state)
  325. return false;
  326. state->SetLooped(enable);
  327. MarkNetworkUpdate();
  328. return true;
  329. }
  330. bool AnimationController::SetAutoFade(const String& name, float fadeOutTime)
  331. {
  332. unsigned index;
  333. AnimationState* state;
  334. FindAnimation(name, index, state);
  335. if (index == M_MAX_UNSIGNED)
  336. return false;
  337. animations_[index].autoFadeTime_ = Max(fadeOutTime, 0.0f);
  338. MarkNetworkUpdate();
  339. return true;
  340. }
  341. bool AnimationController::IsPlaying(const String& name) const
  342. {
  343. unsigned index;
  344. AnimationState* state;
  345. FindAnimation(name, index, state);
  346. return index != M_MAX_UNSIGNED;
  347. }
  348. bool AnimationController::IsFadingIn(const String& name) const
  349. {
  350. unsigned index;
  351. AnimationState* state;
  352. FindAnimation(name, index, state);
  353. if (index == M_MAX_UNSIGNED || !state)
  354. return false;
  355. return animations_[index].fadeTime_ && animations_[index].targetWeight_ > state->GetWeight();
  356. }
  357. bool AnimationController::IsFadingOut(const String& name) const
  358. {
  359. unsigned index;
  360. AnimationState* state;
  361. FindAnimation(name, index, state);
  362. if (index == M_MAX_UNSIGNED || !state)
  363. return false;
  364. return (animations_[index].fadeTime_ && animations_[index].targetWeight_ < state->GetWeight())
  365. || (!state->IsLooped() && state->GetTime() >= state->GetLength() && animations_[index].autoFadeTime_);
  366. }
  367. unsigned char AnimationController::GetLayer(const String& name) const
  368. {
  369. AnimationState* state = FindAnimationState(name);
  370. return state ? state->GetLayer() : 0;
  371. }
  372. Bone* AnimationController::GetStartBone(const String& name) const
  373. {
  374. AnimationState* state = FindAnimationState(name);
  375. return state ? state->GetStartBone() : 0;
  376. }
  377. const String& AnimationController::GetStartBoneName(const String& name) const
  378. {
  379. Bone* bone = GetStartBone(name);
  380. return bone ? bone->name_ : String::EMPTY;
  381. }
  382. float AnimationController::GetTime(const String& name) const
  383. {
  384. AnimationState* state = FindAnimationState(name);
  385. return state ? state->GetTime() : 0.0f;
  386. }
  387. float AnimationController::GetWeight(const String& name) const
  388. {
  389. AnimationState* state = FindAnimationState(name);
  390. return state ? state->GetWeight() : 0.0f;
  391. }
  392. bool AnimationController::IsLooped(const String& name) const
  393. {
  394. AnimationState* state = FindAnimationState(name);
  395. return state ? state->IsLooped() : false;
  396. }
  397. float AnimationController::GetLength(const String& name) const
  398. {
  399. AnimationState* state = FindAnimationState(name);
  400. return state ? state->GetLength() : 0.0f;
  401. }
  402. float AnimationController::GetSpeed(const String& name) const
  403. {
  404. unsigned index;
  405. AnimationState* state;
  406. FindAnimation(name, index, state);
  407. return index != M_MAX_UNSIGNED ? animations_[index].speed_ : 0.0f;
  408. }
  409. float AnimationController::GetFadeTarget(const String& name) const
  410. {
  411. unsigned index;
  412. AnimationState* state;
  413. FindAnimation(name, index, state);
  414. return index != M_MAX_UNSIGNED ? animations_[index].targetWeight_ : 0.0f;
  415. }
  416. float AnimationController::GetFadeTime(const String& name) const
  417. {
  418. unsigned index;
  419. AnimationState* state;
  420. FindAnimation(name, index, state);
  421. return index != M_MAX_UNSIGNED ? animations_[index].targetWeight_ : 0.0f;
  422. }
  423. float AnimationController::GetAutoFade(const String& name) const
  424. {
  425. unsigned index;
  426. AnimationState* state;
  427. FindAnimation(name, index, state);
  428. return index != M_MAX_UNSIGNED ? animations_[index].autoFadeTime_ : 0.0f;
  429. }
  430. void AnimationController::SetAnimationsAttr(VariantVector value)
  431. {
  432. animations_.Clear();
  433. animations_.Reserve(value.Size() / 5); // Incomplete data is discarded
  434. unsigned index = 0;
  435. while (index + 4 < value.Size()) // Prevent out-of-bound index access
  436. {
  437. AnimationControl newControl;
  438. newControl.hash_ = value[index++].GetStringHash();
  439. newControl.speed_ = value[index++].GetFloat();
  440. newControl.targetWeight_ = value[index++].GetFloat();
  441. newControl.fadeTime_ = value[index++].GetFloat();
  442. newControl.autoFadeTime_ = value[index++].GetFloat();
  443. animations_.Push(newControl);
  444. }
  445. }
  446. void AnimationController::SetNetAnimationsAttr(const PODVector<unsigned char>& value)
  447. {
  448. // To apply animations, the model must exist first (practically, have been added before AnimationController)
  449. AnimatedModel* model = GetComponent<AnimatedModel>();
  450. if (!model)
  451. return;
  452. MemoryBuffer buf(value);
  453. // Check which animations we need to remove
  454. HashSet<StringHash> processedAnimations;
  455. unsigned numAnimations = buf.ReadVLE();
  456. while (numAnimations--)
  457. {
  458. StringHash animHash = buf.ReadStringHash();
  459. processedAnimations.Insert(animHash);
  460. // Check if the animation state exists. If not, add new
  461. AnimationState* state = model->GetAnimationState(animHash);
  462. if (!state)
  463. {
  464. Animation* newAnimation = GetSubsystem<ResourceCache>()->GetResource<Animation>(animHash);
  465. state = model->AddAnimationState(newAnimation);
  466. if (!state)
  467. {
  468. LOGERROR("Animation update applying aborted due to unknown animation");
  469. return;
  470. }
  471. }
  472. // Check if the internal control structure exists. If not, add new
  473. unsigned index;
  474. for (index = 0; index < animations_.Size(); ++index)
  475. {
  476. if (animations_[index].hash_ == animHash)
  477. break;
  478. }
  479. if (index == animations_.Size())
  480. {
  481. AnimationControl newControl;
  482. newControl.hash_ = animHash;
  483. animations_.Push(newControl);
  484. }
  485. unsigned char ctrl = buf.ReadUByte();
  486. state->SetLayer(buf.ReadUByte());
  487. state->SetLooped((ctrl & CTRL_LOOPED) != 0);
  488. animations_[index].speed_ = (float)buf.ReadShort() / 2048.0f; // 11 bits of decimal precision, max. 16x playback speed
  489. animations_[index].targetWeight_ = (float)buf.ReadUByte() / 255.0f; // 8 bits of decimal precision
  490. animations_[index].fadeTime_ = (float)buf.ReadUByte() / 64.0f; // 6 bits of decimal precision, max. 4 seconds fade
  491. if (ctrl & CTRL_STARTBONE)
  492. state->SetStartBone(model->GetSkeleton().GetBone(buf.ReadStringHash()));
  493. else
  494. state->SetStartBone(0);
  495. if (ctrl & CTRL_AUTOFADE)
  496. animations_[index].autoFadeTime_ = (float)buf.ReadUByte() / 64.0f; // 6 bits of decimal precision, max. 4 seconds fade
  497. else
  498. animations_[index].autoFadeTime_ = 0.0f;
  499. if (ctrl & CTRL_SETTIME)
  500. {
  501. unsigned char setTimeRev = buf.ReadUByte();
  502. unsigned short setTime = buf.ReadUShort();
  503. // Apply set time command only if revision differs
  504. if (setTimeRev != animations_[index].setTimeRev_)
  505. {
  506. state->SetTime(((float)setTime / 65535.0f) * state->GetLength());
  507. animations_[index].setTimeRev_ = setTimeRev;
  508. }
  509. }
  510. if (ctrl & CTRL_SETWEIGHT)
  511. {
  512. unsigned char setWeightRev = buf.ReadUByte();
  513. unsigned char setWeight = buf.ReadUByte();
  514. // Apply set weight command only if revision differs
  515. if (setWeightRev != animations_[index].setWeightRev_)
  516. {
  517. state->SetWeight((float)setWeight / 255.0f);
  518. animations_[index].setWeightRev_ = setWeightRev;
  519. }
  520. }
  521. }
  522. // Set any extra animations to fade out
  523. for (Vector<AnimationControl>::Iterator i = animations_.Begin(); i != animations_.End(); ++i)
  524. {
  525. if (!processedAnimations.Contains(i->hash_))
  526. {
  527. i->targetWeight_ = 0.0f;
  528. i->fadeTime_ = EXTRA_ANIM_FADEOUT_TIME;
  529. }
  530. }
  531. }
  532. VariantVector AnimationController::GetAnimationsAttr() const
  533. {
  534. VariantVector ret;
  535. ret.Reserve(animations_.Size() * 5);
  536. for (Vector<AnimationControl>::ConstIterator i = animations_.Begin(); i != animations_.End(); ++i)
  537. {
  538. ret.Push(i->hash_);
  539. ret.Push(i->speed_);
  540. ret.Push(i->targetWeight_);
  541. ret.Push(i->fadeTime_);
  542. ret.Push(i->autoFadeTime_);
  543. }
  544. return ret;
  545. }
  546. const PODVector<unsigned char>& AnimationController::GetNetAnimationsAttr() const
  547. {
  548. attrBuffer_.Clear();
  549. AnimatedModel* model = GetComponent<AnimatedModel>();
  550. if (!model)
  551. {
  552. attrBuffer_.WriteVLE(0);
  553. return attrBuffer_.GetBuffer();
  554. }
  555. unsigned validAnimations = 0;
  556. for (Vector<AnimationControl>::ConstIterator i = animations_.Begin(); i != animations_.End(); ++i)
  557. {
  558. if (model->GetAnimationState(i->hash_))
  559. ++validAnimations;
  560. }
  561. attrBuffer_.WriteVLE(validAnimations);
  562. for (Vector<AnimationControl>::ConstIterator i = animations_.Begin(); i != animations_.End(); ++i)
  563. {
  564. AnimationState* state = model->GetAnimationState(i->hash_);
  565. if (!state)
  566. continue;
  567. unsigned char ctrl = 0;
  568. Bone* startBone = state->GetStartBone();
  569. if (state->IsLooped())
  570. ctrl |= CTRL_LOOPED;
  571. if (startBone && startBone != model->GetSkeleton().GetRootBone())
  572. ctrl |= CTRL_STARTBONE;
  573. if (i->autoFadeTime_ > 0.0f)
  574. ctrl |= CTRL_AUTOFADE;
  575. if (i->setTimeTtl_ > 0.0f)
  576. ctrl |= CTRL_SETTIME;
  577. if (i->setWeightTtl_ > 0.0f)
  578. ctrl |= CTRL_SETWEIGHT;
  579. attrBuffer_.WriteStringHash(i->hash_);
  580. attrBuffer_.WriteUByte(ctrl);
  581. attrBuffer_.WriteUByte(state->GetLayer());
  582. attrBuffer_.WriteShort((short)Clamp(i->speed_ * 2048.0f, -32767.0f, 32767.0f));
  583. attrBuffer_.WriteUByte((unsigned char)(i->targetWeight_ * 255.0f));
  584. attrBuffer_.WriteUByte((unsigned char)Clamp(i->fadeTime_ * 64.0f, 0.0f, 255.0f));
  585. if (ctrl & CTRL_STARTBONE)
  586. attrBuffer_.WriteStringHash(startBone->nameHash_);
  587. if (ctrl & CTRL_AUTOFADE)
  588. attrBuffer_.WriteUByte((unsigned char)Clamp(i->autoFadeTime_ * 64.0f, 0.0f, 255.0f));
  589. if (ctrl & CTRL_SETTIME)
  590. {
  591. attrBuffer_.WriteUByte(i->setTimeRev_);
  592. attrBuffer_.WriteUShort(i->setTime_);
  593. }
  594. if (ctrl & CTRL_SETWEIGHT)
  595. {
  596. attrBuffer_.WriteUByte(i->setWeightRev_);
  597. attrBuffer_.WriteUByte(i->setWeight_);
  598. }
  599. }
  600. return attrBuffer_.GetBuffer();
  601. }
  602. void AnimationController::OnNodeSet(Node* node)
  603. {
  604. if (node)
  605. {
  606. Scene* scene = GetScene();
  607. if (scene && IsEnabledEffective())
  608. SubscribeToEvent(scene, E_SCENEPOSTUPDATE, HANDLER(AnimationController, HandleScenePostUpdate));
  609. }
  610. }
  611. void AnimationController::FindAnimation(const String& name, unsigned& index, AnimationState*& state) const
  612. {
  613. AnimatedModel* model = GetComponent<AnimatedModel>();
  614. StringHash nameHash(name);
  615. // Find the AnimationState
  616. state = model ? model->GetAnimationState(nameHash) : 0;
  617. if (state)
  618. {
  619. // Either a resource name or animation name may be specified. We store resource names, so correct the hash if necessary
  620. nameHash = state->GetAnimation()->GetNameHash();
  621. }
  622. // Find the internal control structure
  623. index = M_MAX_UNSIGNED;
  624. for (unsigned i = 0; i < animations_.Size(); ++i)
  625. {
  626. if (animations_[i].hash_ == nameHash)
  627. {
  628. index = i;
  629. break;
  630. }
  631. }
  632. }
  633. AnimationState* AnimationController::FindAnimationState(const String& name) const
  634. {
  635. AnimatedModel* model = GetComponent<AnimatedModel>();
  636. return model ? model->GetAnimationState(StringHash(name)) : 0;
  637. }
  638. void AnimationController::HandleScenePostUpdate(StringHash eventType, VariantMap& eventData)
  639. {
  640. using namespace ScenePostUpdate;
  641. Update(eventData[P_TIMESTEP].GetFloat());
  642. }
  643. }