AnimationController.cpp 30 KB

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