AnimationController.cpp 27 KB

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