AnimationController.cpp 29 KB

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