AnimationController.cpp 28 KB

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