AnimationController.cpp 27 KB

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