AnimationController.cpp 27 KB

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