AnimationController.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2011 Lasse Öörni
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. //
  23. #include "Precompiled.h"
  24. #include "AnimatedModel.h"
  25. #include "Animation.h"
  26. #include "AnimationController.h"
  27. #include "AnimationState.h"
  28. #include "Context.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. static String noBoneName;
  36. OBJECTTYPESTATIC(AnimationController);
  37. AnimationController::AnimationController(Context* context) :
  38. Component(context)
  39. {
  40. }
  41. AnimationController::~AnimationController()
  42. {
  43. }
  44. void AnimationController::RegisterObject(Context* context)
  45. {
  46. context->RegisterFactory<AnimationController>();
  47. ACCESSOR_ATTRIBUTE(AnimationController, VAR_BUFFER, "Animations", GetAnimationsAttr, SetAnimationsAttr, PODVector<unsigned char>, PODVector<unsigned char>(), AM_FILE | AM_NOEDIT);
  48. }
  49. void AnimationController::Update(float timeStep)
  50. {
  51. AnimatedModel* model = GetComponent<AnimatedModel>();
  52. if (!model)
  53. return;
  54. PROFILE(UpdateAnimationController);
  55. // Loop through animations
  56. for (Vector<AnimationControl>::Iterator i = animations_.Begin(); i != animations_.End();)
  57. {
  58. bool remove = false;
  59. AnimationState* state = model->GetAnimationState(i->hash_);
  60. if (!state)
  61. remove = true;
  62. else
  63. {
  64. // Advance the animation
  65. if (i->speed_ != 0.0f)
  66. state->AddTime(i->speed_ * timeStep);
  67. float targetWeight = i->targetWeight_;
  68. float fadeTime = i->fadeTime_;
  69. // If non-looped animation at the end, activate autofade as applicable
  70. if (!state->IsLooped() && state->GetTime() >= state->GetLength() && i->autoFadeTime_ > 0.0f)
  71. {
  72. targetWeight = 0.0f;
  73. fadeTime = i->autoFadeTime_;
  74. }
  75. // Process weight fade
  76. float currentWeight = state->GetWeight();
  77. if (currentWeight != targetWeight && fadeTime > 0.0f)
  78. {
  79. float weightDelta = 1.0f / fadeTime * timeStep;
  80. if (currentWeight < targetWeight)
  81. currentWeight = Min(currentWeight + weightDelta, targetWeight);
  82. else if (currentWeight > targetWeight)
  83. currentWeight = Max(currentWeight - weightDelta, targetWeight);
  84. state->SetWeight(currentWeight);
  85. }
  86. // Remove if weight zero and target weight zero
  87. if (state->GetWeight() == 0.0f && (targetWeight == 0.0f || fadeTime == 0.0f))
  88. remove = true;
  89. }
  90. if (remove)
  91. {
  92. if (state)
  93. model->RemoveAnimationState(state);
  94. i = animations_.Erase(i);
  95. }
  96. else
  97. ++i;
  98. }
  99. }
  100. bool AnimationController::Play(const String& name, int layer, bool looped, float fadeInTime)
  101. {
  102. AnimatedModel* model = GetComponent<AnimatedModel>();
  103. if (!model)
  104. return false;
  105. // Check if already exists
  106. unsigned index;
  107. AnimationState* state;
  108. FindAnimation(name, index, state);
  109. if (!state)
  110. {
  111. Animation* newAnimation = GetSubsystem<ResourceCache>()->GetResource<Animation>(name);
  112. state = model->AddAnimationState(newAnimation);
  113. if (!state)
  114. return false;
  115. }
  116. if (index == M_MAX_UNSIGNED)
  117. {
  118. AnimationControl newControl;
  119. Animation* animation = state->GetAnimation();
  120. newControl.hash_ = animation->GetNameHash();
  121. animations_.Push(newControl);
  122. index = animations_.Size() - 1;
  123. }
  124. state->SetLayer(layer);
  125. state->SetLooped(looped);
  126. if (fadeInTime > 0.0f)
  127. {
  128. animations_[index].targetWeight_ = 1.0f;
  129. animations_[index].fadeTime_ = fadeInTime;
  130. }
  131. else
  132. state->SetWeight(1.0f);
  133. return true;
  134. }
  135. bool AnimationController::PlayExclusive(const String& name, int layer, bool looped, float fadeTime)
  136. {
  137. FadeOthers(name, 0.0f, fadeTime);
  138. return Play(name, layer, looped, fadeTime);
  139. }
  140. bool AnimationController::Stop(const String& name, float fadeOutTime)
  141. {
  142. AnimatedModel* model = GetComponent<AnimatedModel>();
  143. if (!model)
  144. return false;
  145. unsigned index;
  146. AnimationState* state;
  147. FindAnimation(name, index, state);
  148. if (fadeOutTime <= 0.0f)
  149. {
  150. if (index != M_MAX_UNSIGNED)
  151. animations_.Erase(animations_.Begin() + index);
  152. if (state)
  153. model->RemoveAnimationState(state);
  154. }
  155. else
  156. {
  157. if (index != M_MAX_UNSIGNED)
  158. {
  159. animations_[index].targetWeight_ = 0.0f;
  160. animations_[index].fadeTime_ = fadeOutTime;
  161. }
  162. }
  163. return index != M_MAX_UNSIGNED || state != 0;
  164. }
  165. void AnimationController::StopLayer(int layer, float fadeOutTime)
  166. {
  167. AnimatedModel* model = GetComponent<AnimatedModel>();
  168. if (!model)
  169. return;
  170. for (Vector<AnimationControl>::Iterator i = animations_.Begin(); i != animations_.End();)
  171. {
  172. AnimationState* state = model->GetAnimationState(i->hash_);
  173. bool remove = false;
  174. if (state && state->GetLayer() == layer)
  175. {
  176. if (fadeOutTime <= 0.0f)
  177. {
  178. remove = true;
  179. if (state)
  180. model->RemoveAnimationState(state);
  181. }
  182. else
  183. {
  184. i->targetWeight_ = 0.0f;
  185. i->fadeTime_ = fadeOutTime;
  186. }
  187. }
  188. if (remove)
  189. i = animations_.Erase(i);
  190. else
  191. ++i;
  192. }
  193. }
  194. void AnimationController::StopAll(float fadeOutTime)
  195. {
  196. AnimatedModel* model = GetComponent<AnimatedModel>();
  197. if (!model)
  198. return;
  199. for (Vector<AnimationControl>::Iterator i = animations_.Begin(); i != animations_.End();)
  200. {
  201. bool remove = false;
  202. if (fadeOutTime <= 0.0f)
  203. {
  204. remove = true;
  205. AnimationState* state = model->GetAnimationState(i->hash_);
  206. if (state)
  207. model->RemoveAnimationState(state);
  208. }
  209. else
  210. {
  211. i->targetWeight_ = 0.0f;
  212. i->fadeTime_ = fadeOutTime;
  213. }
  214. if (remove)
  215. i = animations_.Erase(i);
  216. else
  217. ++i;
  218. }
  219. }
  220. bool AnimationController::Fade(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)
  226. return false;
  227. animations_[index].targetWeight_ = Clamp(targetWeight, 0.0f, 1.0f);
  228. animations_[index].fadeTime_ = Max(fadeTime, M_EPSILON);
  229. return true;
  230. }
  231. bool AnimationController::FadeOthers(const String& name, float targetWeight, float fadeTime)
  232. {
  233. unsigned index;
  234. AnimationState* state;
  235. FindAnimation(name, index, state);
  236. if (index == M_MAX_UNSIGNED || !state)
  237. return false;
  238. AnimatedModel* model = GetComponent<AnimatedModel>();
  239. int layer = state->GetLayer();
  240. for (unsigned i = 0; i < animations_.Size(); ++i)
  241. {
  242. if (i != index)
  243. {
  244. AnimationControl& control = animations_[i];
  245. AnimationState* otherState = model->GetAnimationState(control.hash_);
  246. if (otherState && otherState->GetLayer() == layer)
  247. {
  248. control.targetWeight_ = Clamp(targetWeight, 0.0f, 1.0f);
  249. control.fadeTime_ = Max(fadeTime, M_EPSILON);
  250. }
  251. }
  252. }
  253. return true;
  254. }
  255. bool AnimationController::SetLayer(const String& name, int layer)
  256. {
  257. AnimationState* state = FindAnimationState(name);
  258. if (!state)
  259. return false;
  260. state->SetLayer(layer);
  261. return true;
  262. }
  263. bool AnimationController::SetStartBone(const String& name, const String& startBoneName)
  264. {
  265. AnimationState* state = FindAnimationState(name);
  266. if (!state)
  267. return false;
  268. AnimatedModel* model = GetComponent<AnimatedModel>();
  269. Bone* bone = model->GetSkeleton().GetBone(startBoneName);
  270. state->SetStartBone(bone);
  271. return true;
  272. }
  273. bool AnimationController::SetTime(const String& name, float time)
  274. {
  275. AnimationState* state = FindAnimationState(name);
  276. if (!state)
  277. return false;
  278. state->SetTime(time);
  279. return true;
  280. }
  281. bool AnimationController::SetSpeed(const String& name, float speed)
  282. {
  283. unsigned index;
  284. AnimationState* state;
  285. FindAnimation(name, index, state);
  286. if (index == M_MAX_UNSIGNED)
  287. return false;
  288. animations_[index].speed_ = speed;
  289. return true;
  290. }
  291. bool AnimationController::SetWeight(const String& name, float weight)
  292. {
  293. unsigned index;
  294. AnimationState* state;
  295. FindAnimation(name, index, state);
  296. if (index == M_MAX_UNSIGNED || !state)
  297. return false;
  298. state->SetWeight(weight);
  299. // Stop any ongoing fade
  300. animations_[index].fadeTime_ = 0.0f;
  301. return true;
  302. }
  303. bool AnimationController::SetLooped(const String& name, bool enable)
  304. {
  305. AnimationState* state = FindAnimationState(name);
  306. if (!state)
  307. return false;
  308. state->SetLooped(enable);
  309. return true;
  310. }
  311. bool AnimationController::SetAutoFade(const String& name, float fadeOutTime)
  312. {
  313. unsigned index;
  314. AnimationState* state;
  315. FindAnimation(name, index, state);
  316. if (index == M_MAX_UNSIGNED)
  317. return false;
  318. animations_[index].autoFadeTime_ = Max(fadeOutTime, 0.0f);
  319. return true;
  320. }
  321. bool AnimationController::IsPlaying(const String& name) const
  322. {
  323. unsigned index;
  324. AnimationState* state;
  325. FindAnimation(name, index, state);
  326. return index != M_MAX_UNSIGNED;
  327. }
  328. bool AnimationController::IsFadingIn(const String& name) const
  329. {
  330. unsigned index;
  331. AnimationState* state;
  332. FindAnimation(name, index, state);
  333. if (index == M_MAX_UNSIGNED || !state)
  334. return false;
  335. return animations_[index].fadeTime_ && animations_[index].targetWeight_ > state->GetWeight();
  336. }
  337. bool AnimationController::IsFadingOut(const String& name) const
  338. {
  339. unsigned index;
  340. AnimationState* state;
  341. FindAnimation(name, index, state);
  342. if (index == M_MAX_UNSIGNED || !state)
  343. return false;
  344. return (animations_[index].fadeTime_ && animations_[index].targetWeight_ < state->GetWeight())
  345. || (!state->IsLooped() && state->GetTime() >= state->GetLength() && animations_[index].autoFadeTime_);
  346. }
  347. int AnimationController::GetLayer(const String& name) const
  348. {
  349. AnimationState* state = FindAnimationState(name);
  350. if (!state)
  351. return 0;
  352. return state->GetLayer();
  353. }
  354. Bone* AnimationController::GetStartBone(const String& name) const
  355. {
  356. AnimationState* state = FindAnimationState(name);
  357. if (!state)
  358. return 0;
  359. return state->GetStartBone();
  360. }
  361. const String& AnimationController::GetStartBoneName(const String& name) const
  362. {
  363. Bone* bone = GetStartBone(name);
  364. return bone ? bone->name_ : noBoneName;
  365. }
  366. float AnimationController::GetTime(const String& name) const
  367. {
  368. AnimationState* state = FindAnimationState(name);
  369. return state ? state->GetTime() : 0.0f;
  370. }
  371. float AnimationController::GetWeight(const String& name) const
  372. {
  373. AnimationState* state = FindAnimationState(name);
  374. return state ? state->GetWeight() : 0.0f;
  375. }
  376. bool AnimationController::IsLooped(const String& name) const
  377. {
  378. AnimationState* state = FindAnimationState(name);
  379. return state ? state->IsLooped() : false;
  380. }
  381. float AnimationController::GetLength(const String& name) const
  382. {
  383. AnimationState* state = FindAnimationState(name);
  384. return state ? state->GetLength() : 0.0f;
  385. }
  386. float AnimationController::GetSpeed(const String& name) const
  387. {
  388. unsigned index;
  389. AnimationState* state;
  390. FindAnimation(name, index, state);
  391. if (index == M_MAX_UNSIGNED)
  392. return 0.0f;
  393. return animations_[index].speed_;
  394. }
  395. float AnimationController::GetFadeTarget(const String& name) const
  396. {
  397. unsigned index;
  398. AnimationState* state;
  399. FindAnimation(name, index, state);
  400. if (index == M_MAX_UNSIGNED)
  401. return 0.0f;
  402. return animations_[index].targetWeight_;
  403. }
  404. float AnimationController::GetFadeTime(const String& name) const
  405. {
  406. unsigned index;
  407. AnimationState* state;
  408. FindAnimation(name, index, state);
  409. if (index == M_MAX_UNSIGNED)
  410. return 0.0f;
  411. return animations_[index].targetWeight_;
  412. }
  413. float AnimationController::GetAutoFade(const String& name) const
  414. {
  415. unsigned index;
  416. AnimationState* state;
  417. FindAnimation(name, index, state);
  418. if (index == M_MAX_UNSIGNED)
  419. return 0.0f;
  420. return animations_[index].autoFadeTime_;
  421. }
  422. void AnimationController::SetAnimationsAttr(PODVector<unsigned char> value)
  423. {
  424. MemoryBuffer buf(value);
  425. animations_.Resize(buf.ReadVLE());
  426. for (Vector<AnimationControl>::Iterator i = animations_.Begin(); i != animations_.End(); ++i)
  427. {
  428. i->hash_ = buf.ReadStringHash();
  429. i->speed_ = buf.ReadFloat();
  430. i->targetWeight_ = buf.ReadFloat();
  431. i->fadeTime_ = buf.ReadFloat();
  432. i->autoFadeTime_ = buf.ReadFloat();
  433. }
  434. }
  435. PODVector<unsigned char> AnimationController::GetAnimationsAttr() const
  436. {
  437. VectorBuffer buf;
  438. buf.WriteVLE(animations_.Size());
  439. for (Vector<AnimationControl>::ConstIterator i = animations_.Begin(); i != animations_.End(); ++i)
  440. {
  441. buf.WriteStringHash(i->hash_);
  442. buf.WriteFloat(i->speed_);
  443. buf.WriteFloat(i->targetWeight_);
  444. buf.WriteFloat(i->fadeTime_);
  445. buf.WriteFloat(i->autoFadeTime_);
  446. }
  447. return buf.GetBuffer();
  448. }
  449. void AnimationController::OnNodeSet(Node* node)
  450. {
  451. if (node)
  452. {
  453. Scene* scene = node->GetScene();
  454. if (scene)
  455. SubscribeToEvent(scene, E_SCENEPOSTUPDATE, HANDLER(AnimationController, HandleScenePostUpdate));
  456. }
  457. }
  458. void AnimationController::FindAnimation(const String& name, unsigned& index, AnimationState*& state) const
  459. {
  460. AnimatedModel* model = GetComponent<AnimatedModel>();
  461. StringHash nameHash(name);
  462. // Find the AnimationState
  463. state = model ? model->GetAnimationState(nameHash) : 0;
  464. if (state)
  465. {
  466. // Either a resource name or animation name may be specified. We store resource names, so correct the hash if necessary
  467. nameHash = state->GetAnimation()->GetNameHash();
  468. }
  469. // Find the internal control structure
  470. index = M_MAX_UNSIGNED;
  471. for (unsigned i = 0; i < animations_.Size(); ++i)
  472. {
  473. if (animations_[i].hash_ == nameHash)
  474. {
  475. index = i;
  476. break;
  477. }
  478. }
  479. }
  480. AnimationState* AnimationController::FindAnimationState(const String& name) const
  481. {
  482. AnimatedModel* model = GetComponent<AnimatedModel>();
  483. return model ? model->GetAnimationState(name) : 0;
  484. }
  485. void AnimationController::HandleScenePostUpdate(StringHash eventType, VariantMap& eventData)
  486. {
  487. using namespace ScenePostUpdate;
  488. Update(eventData[P_TIMESTEP].GetFloat());
  489. }