AnimationController.cpp 25 KB

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