AnimationController.cpp 29 KB

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