AnimationController.cpp 25 KB

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