AnimationController.cpp 27 KB

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