AnimationController.cpp 27 KB

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