AnimationController.cpp 27 KB

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