AnimationController.cpp 28 KB

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