AnimationController.cpp 23 KB

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