AnimationController.cpp 29 KB

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