AnimationController.cpp 27 KB

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