AnimationController.cpp 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903
  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::IsFadingIn(const String& name) const
  374. {
  375. unsigned index;
  376. AnimationState* state;
  377. FindAnimation(name, index, state);
  378. if (index == M_MAX_UNSIGNED || !state)
  379. return false;
  380. return animations_[index].fadeTime_ && animations_[index].targetWeight_ > state->GetWeight();
  381. }
  382. bool AnimationController::IsFadingOut(const String& name) const
  383. {
  384. unsigned index;
  385. AnimationState* state;
  386. FindAnimation(name, index, state);
  387. if (index == M_MAX_UNSIGNED || !state)
  388. return false;
  389. return (animations_[index].fadeTime_ && animations_[index].targetWeight_ < state->GetWeight())
  390. || (!state->IsLooped() && state->GetTime() >= state->GetLength() && animations_[index].autoFadeTime_);
  391. }
  392. bool AnimationController::IsAtEnd(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. else
  400. return state->GetTime() >= state->GetLength();
  401. }
  402. unsigned char AnimationController::GetLayer(const String& name) const
  403. {
  404. AnimationState* state = GetAnimationState(name);
  405. return (unsigned char)(state ? state->GetLayer() : 0);
  406. }
  407. Bone* AnimationController::GetStartBone(const String& name) const
  408. {
  409. AnimationState* state = GetAnimationState(name);
  410. return state ? state->GetStartBone() : 0;
  411. }
  412. const String& AnimationController::GetStartBoneName(const String& name) const
  413. {
  414. Bone* bone = GetStartBone(name);
  415. return bone ? bone->name_ : String::EMPTY;
  416. }
  417. float AnimationController::GetTime(const String& name) const
  418. {
  419. AnimationState* state = GetAnimationState(name);
  420. return state ? state->GetTime() : 0.0f;
  421. }
  422. float AnimationController::GetWeight(const String& name) const
  423. {
  424. AnimationState* state = GetAnimationState(name);
  425. return state ? state->GetWeight() : 0.0f;
  426. }
  427. bool AnimationController::IsLooped(const String& name) const
  428. {
  429. AnimationState* state = GetAnimationState(name);
  430. return state ? state->IsLooped() : false;
  431. }
  432. AnimationBlendMode AnimationController::GetBlendMode(const String& name) const
  433. {
  434. AnimationState* state = GetAnimationState(name);
  435. return state ? state->GetBlendMode() : ABM_LERP;
  436. }
  437. float AnimationController::GetLength(const String& name) const
  438. {
  439. AnimationState* state = GetAnimationState(name);
  440. return state ? state->GetLength() : 0.0f;
  441. }
  442. float AnimationController::GetSpeed(const String& name) const
  443. {
  444. unsigned index;
  445. AnimationState* state;
  446. FindAnimation(name, index, state);
  447. return index != M_MAX_UNSIGNED ? animations_[index].speed_ : 0.0f;
  448. }
  449. float AnimationController::GetFadeTarget(const String& name) const
  450. {
  451. unsigned index;
  452. AnimationState* state;
  453. FindAnimation(name, index, state);
  454. return index != M_MAX_UNSIGNED ? animations_[index].targetWeight_ : 0.0f;
  455. }
  456. float AnimationController::GetFadeTime(const String& name) const
  457. {
  458. unsigned index;
  459. AnimationState* state;
  460. FindAnimation(name, index, state);
  461. return index != M_MAX_UNSIGNED ? animations_[index].targetWeight_ : 0.0f;
  462. }
  463. float AnimationController::GetAutoFade(const String& name) const
  464. {
  465. unsigned index;
  466. AnimationState* state;
  467. FindAnimation(name, index, state);
  468. return index != M_MAX_UNSIGNED ? animations_[index].autoFadeTime_ : 0.0f;
  469. }
  470. bool AnimationController::GetRemoveOnCompletion(const String& name) const
  471. {
  472. unsigned index;
  473. AnimationState* state;
  474. FindAnimation(name, index, state);
  475. return index != M_MAX_UNSIGNED ? animations_[index].removeOnCompletion_ : false;
  476. }
  477. AnimationState* AnimationController::GetAnimationState(const String& name) const
  478. {
  479. return GetAnimationState(StringHash(name));
  480. }
  481. AnimationState* AnimationController::GetAnimationState(StringHash nameHash) const
  482. {
  483. // Model mode
  484. AnimatedModel* model = GetComponent<AnimatedModel>();
  485. if (model)
  486. return model->GetAnimationState(nameHash);
  487. // Node hierarchy mode
  488. for (Vector<SharedPtr<AnimationState> >::ConstIterator i = nodeAnimationStates_.Begin(); i != nodeAnimationStates_.End(); ++i)
  489. {
  490. Animation* animation = (*i)->GetAnimation();
  491. if (animation->GetNameHash() == nameHash || animation->GetAnimationNameHash() == nameHash)
  492. return *i;
  493. }
  494. return 0;
  495. }
  496. void AnimationController::SetAnimationsAttr(const VariantVector& value)
  497. {
  498. animations_.Clear();
  499. animations_.Reserve(value.Size() / 5); // Incomplete data is discarded
  500. unsigned index = 0;
  501. while (index + 4 < value.Size()) // Prevent out-of-bound index access
  502. {
  503. AnimationControl newControl;
  504. newControl.name_ = value[index++].GetString();
  505. newControl.hash_ = StringHash(newControl.name_);
  506. newControl.speed_ = value[index++].GetFloat();
  507. newControl.targetWeight_ = value[index++].GetFloat();
  508. newControl.fadeTime_ = value[index++].GetFloat();
  509. newControl.autoFadeTime_ = value[index++].GetFloat();
  510. animations_.Push(newControl);
  511. }
  512. }
  513. void AnimationController::SetNetAnimationsAttr(const PODVector<unsigned char>& value)
  514. {
  515. MemoryBuffer buf(value);
  516. AnimatedModel* model = GetComponent<AnimatedModel>();
  517. // Check which animations we need to remove
  518. HashSet<StringHash> processedAnimations;
  519. unsigned numAnimations = buf.ReadVLE();
  520. while (numAnimations--)
  521. {
  522. String animName = buf.ReadString();
  523. StringHash animHash(animName);
  524. processedAnimations.Insert(animHash);
  525. // Check if the animation state exists. If not, add new
  526. AnimationState* state = GetAnimationState(animHash);
  527. if (!state)
  528. {
  529. Animation* newAnimation = GetSubsystem<ResourceCache>()->GetResource<Animation>(animName);
  530. state = AddAnimationState(newAnimation);
  531. if (!state)
  532. {
  533. URHO3D_LOGERROR("Animation update applying aborted due to unknown animation");
  534. return;
  535. }
  536. }
  537. // Check if the internal control structure exists. If not, add new
  538. unsigned index;
  539. for (index = 0; index < animations_.Size(); ++index)
  540. {
  541. if (animations_[index].hash_ == animHash)
  542. break;
  543. }
  544. if (index == animations_.Size())
  545. {
  546. AnimationControl newControl;
  547. newControl.name_ = animName;
  548. newControl.hash_ = animHash;
  549. animations_.Push(newControl);
  550. }
  551. unsigned char ctrl = buf.ReadUByte();
  552. state->SetLayer(buf.ReadUByte());
  553. state->SetLooped((ctrl & CTRL_LOOPED) != 0);
  554. state->SetBlendMode((ctrl & CTRL_ADDITIVE) != 0 ? ABM_ADDITIVE : ABM_LERP);
  555. animations_[index].speed_ = (float)buf.ReadShort() / 2048.0f; // 11 bits of decimal precision, max. 16x playback speed
  556. animations_[index].targetWeight_ = (float)buf.ReadUByte() / 255.0f; // 8 bits of decimal precision
  557. animations_[index].fadeTime_ = (float)buf.ReadUByte() / 64.0f; // 6 bits of decimal precision, max. 4 seconds fade
  558. if (ctrl & CTRL_STARTBONE)
  559. {
  560. StringHash boneHash = buf.ReadStringHash();
  561. if (model)
  562. state->SetStartBone(model->GetSkeleton().GetBone(boneHash));
  563. }
  564. else
  565. state->SetStartBone(0);
  566. if (ctrl & CTRL_AUTOFADE)
  567. animations_[index].autoFadeTime_ = (float)buf.ReadUByte() / 64.0f; // 6 bits of decimal precision, max. 4 seconds fade
  568. else
  569. animations_[index].autoFadeTime_ = 0.0f;
  570. animations_[index].removeOnCompletion_ = (ctrl & CTRL_REMOVEONCOMPLETION) != 0;
  571. if (ctrl & CTRL_SETTIME)
  572. {
  573. unsigned char setTimeRev = buf.ReadUByte();
  574. unsigned short setTime = buf.ReadUShort();
  575. // Apply set time command only if revision differs
  576. if (setTimeRev != animations_[index].setTimeRev_)
  577. {
  578. state->SetTime(((float)setTime / 65535.0f) * state->GetLength());
  579. animations_[index].setTimeRev_ = setTimeRev;
  580. }
  581. }
  582. if (ctrl & CTRL_SETWEIGHT)
  583. {
  584. unsigned char setWeightRev = buf.ReadUByte();
  585. unsigned char setWeight = buf.ReadUByte();
  586. // Apply set weight command only if revision differs
  587. if (setWeightRev != animations_[index].setWeightRev_)
  588. {
  589. state->SetWeight((float)setWeight / 255.0f);
  590. animations_[index].setWeightRev_ = setWeightRev;
  591. }
  592. }
  593. }
  594. // Set any extra animations to fade out
  595. for (Vector<AnimationControl>::Iterator i = animations_.Begin(); i != animations_.End(); ++i)
  596. {
  597. if (!processedAnimations.Contains(i->hash_))
  598. {
  599. i->targetWeight_ = 0.0f;
  600. i->fadeTime_ = EXTRA_ANIM_FADEOUT_TIME;
  601. }
  602. }
  603. }
  604. void AnimationController::SetNodeAnimationStatesAttr(const VariantVector& value)
  605. {
  606. ResourceCache* cache = GetSubsystem<ResourceCache>();
  607. nodeAnimationStates_.Clear();
  608. unsigned index = 0;
  609. unsigned numStates = index < value.Size() ? value[index++].GetUInt() : 0;
  610. // Prevent negative or overly large value being assigned from the editor
  611. if (numStates > M_MAX_INT)
  612. numStates = 0;
  613. if (numStates > MAX_NODE_ANIMATION_STATES)
  614. numStates = MAX_NODE_ANIMATION_STATES;
  615. nodeAnimationStates_.Reserve(numStates);
  616. while (numStates--)
  617. {
  618. if (index + 2 < value.Size())
  619. {
  620. // Note: null animation is allowed here for editing
  621. const ResourceRef& animRef = value[index++].GetResourceRef();
  622. SharedPtr<AnimationState> newState(new AnimationState(GetNode(), cache->GetResource<Animation>(animRef.name_)));
  623. nodeAnimationStates_.Push(newState);
  624. newState->SetLooped(value[index++].GetBool());
  625. newState->SetTime(value[index++].GetFloat());
  626. }
  627. else
  628. {
  629. // If not enough data, just add an empty animation state
  630. SharedPtr<AnimationState> newState(new AnimationState(GetNode(), 0));
  631. nodeAnimationStates_.Push(newState);
  632. }
  633. }
  634. }
  635. VariantVector AnimationController::GetAnimationsAttr() const
  636. {
  637. VariantVector ret;
  638. ret.Reserve(animations_.Size() * 5);
  639. for (Vector<AnimationControl>::ConstIterator i = animations_.Begin(); i != animations_.End(); ++i)
  640. {
  641. ret.Push(i->name_);
  642. ret.Push(i->speed_);
  643. ret.Push(i->targetWeight_);
  644. ret.Push(i->fadeTime_);
  645. ret.Push(i->autoFadeTime_);
  646. }
  647. return ret;
  648. }
  649. const PODVector<unsigned char>& AnimationController::GetNetAnimationsAttr() const
  650. {
  651. attrBuffer_.Clear();
  652. AnimatedModel* model = GetComponent<AnimatedModel>();
  653. unsigned validAnimations = 0;
  654. for (Vector<AnimationControl>::ConstIterator i = animations_.Begin(); i != animations_.End(); ++i)
  655. {
  656. if (GetAnimationState(i->hash_))
  657. ++validAnimations;
  658. }
  659. attrBuffer_.WriteVLE(validAnimations);
  660. for (Vector<AnimationControl>::ConstIterator i = animations_.Begin(); i != animations_.End(); ++i)
  661. {
  662. AnimationState* state = GetAnimationState(i->hash_);
  663. if (!state)
  664. continue;
  665. unsigned char ctrl = 0;
  666. Bone* startBone = state->GetStartBone();
  667. if (state->IsLooped())
  668. ctrl |= CTRL_LOOPED;
  669. if (state->GetBlendMode() == ABM_ADDITIVE)
  670. ctrl |= CTRL_ADDITIVE;
  671. if (startBone && model && startBone != model->GetSkeleton().GetRootBone())
  672. ctrl |= CTRL_STARTBONE;
  673. if (i->autoFadeTime_ > 0.0f)
  674. ctrl |= CTRL_AUTOFADE;
  675. if (i->removeOnCompletion_)
  676. ctrl |= CTRL_REMOVEONCOMPLETION;
  677. if (i->setTimeTtl_ > 0.0f)
  678. ctrl |= CTRL_SETTIME;
  679. if (i->setWeightTtl_ > 0.0f)
  680. ctrl |= CTRL_SETWEIGHT;
  681. attrBuffer_.WriteString(i->name_);
  682. attrBuffer_.WriteUByte(ctrl);
  683. attrBuffer_.WriteUByte(state->GetLayer());
  684. attrBuffer_.WriteShort((short)Clamp(i->speed_ * 2048.0f, -32767.0f, 32767.0f));
  685. attrBuffer_.WriteUByte((unsigned char)(i->targetWeight_ * 255.0f));
  686. attrBuffer_.WriteUByte((unsigned char)Clamp(i->fadeTime_ * 64.0f, 0.0f, 255.0f));
  687. if (ctrl & CTRL_STARTBONE)
  688. attrBuffer_.WriteStringHash(startBone->nameHash_);
  689. if (ctrl & CTRL_AUTOFADE)
  690. attrBuffer_.WriteUByte((unsigned char)Clamp(i->autoFadeTime_ * 64.0f, 0.0f, 255.0f));
  691. if (ctrl & CTRL_SETTIME)
  692. {
  693. attrBuffer_.WriteUByte(i->setTimeRev_);
  694. attrBuffer_.WriteUShort(i->setTime_);
  695. }
  696. if (ctrl & CTRL_SETWEIGHT)
  697. {
  698. attrBuffer_.WriteUByte(i->setWeightRev_);
  699. attrBuffer_.WriteUByte(i->setWeight_);
  700. }
  701. }
  702. return attrBuffer_.GetBuffer();
  703. }
  704. VariantVector AnimationController::GetNodeAnimationStatesAttr() const
  705. {
  706. VariantVector ret;
  707. ret.Reserve(nodeAnimationStates_.Size() * 3 + 1);
  708. ret.Push(nodeAnimationStates_.Size());
  709. for (Vector<SharedPtr<AnimationState> >::ConstIterator i = nodeAnimationStates_.Begin(); i != nodeAnimationStates_.End(); ++i)
  710. {
  711. AnimationState* state = *i;
  712. Animation* animation = state->GetAnimation();
  713. ret.Push(GetResourceRef(animation, Animation::GetTypeStatic()));
  714. ret.Push(state->IsLooped());
  715. ret.Push(state->GetTime());
  716. }
  717. return ret;
  718. }
  719. void AnimationController::OnSceneSet(Scene* scene)
  720. {
  721. if (scene && IsEnabledEffective())
  722. SubscribeToEvent(scene, E_SCENEPOSTUPDATE, URHO3D_HANDLER(AnimationController, HandleScenePostUpdate));
  723. else if (!scene)
  724. UnsubscribeFromEvent(E_SCENEPOSTUPDATE);
  725. }
  726. AnimationState* AnimationController::AddAnimationState(Animation* animation)
  727. {
  728. if (!animation)
  729. return 0;
  730. // Model mode
  731. AnimatedModel* model = GetComponent<AnimatedModel>();
  732. if (model)
  733. return model->AddAnimationState(animation);
  734. // Node hierarchy mode
  735. SharedPtr<AnimationState> newState(new AnimationState(node_, animation));
  736. nodeAnimationStates_.Push(newState);
  737. return newState;
  738. }
  739. void AnimationController::RemoveAnimationState(AnimationState* state)
  740. {
  741. if (!state)
  742. return;
  743. // Model mode
  744. AnimatedModel* model = GetComponent<AnimatedModel>();
  745. if (model)
  746. {
  747. model->RemoveAnimationState(state);
  748. return;
  749. }
  750. // Node hierarchy mode
  751. for (Vector<SharedPtr<AnimationState> >::Iterator i = nodeAnimationStates_.Begin(); i != nodeAnimationStates_.End(); ++i)
  752. {
  753. if ((*i) == state)
  754. {
  755. nodeAnimationStates_.Erase(i);
  756. return;
  757. }
  758. }
  759. }
  760. void AnimationController::FindAnimation(const String& name, unsigned& index, AnimationState*& state) const
  761. {
  762. StringHash nameHash(GetInternalPath(name));
  763. // Find the AnimationState
  764. state = GetAnimationState(nameHash);
  765. if (state)
  766. {
  767. // Either a resource name or animation name may be specified. We store resource names, so correct the hash if necessary
  768. nameHash = state->GetAnimation()->GetNameHash();
  769. }
  770. // Find the internal control structure
  771. index = M_MAX_UNSIGNED;
  772. for (unsigned i = 0; i < animations_.Size(); ++i)
  773. {
  774. if (animations_[i].hash_ == nameHash)
  775. {
  776. index = i;
  777. break;
  778. }
  779. }
  780. }
  781. void AnimationController::HandleScenePostUpdate(StringHash eventType, VariantMap& eventData)
  782. {
  783. using namespace ScenePostUpdate;
  784. Update(eventData[P_TIMESTEP].GetFloat());
  785. }
  786. }