AnimationController.cpp 29 KB

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