AnimationController.cpp 28 KB

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