AnimationController.cpp 32 KB

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