AnimationController.cpp 30 KB

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