AnimationState.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  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 "AnimationState.h"
  27. #include "Deserializer.h"
  28. #include "Serializer.h"
  29. #include "XMLElement.h"
  30. #include "DebugNew.h"
  31. AnimationState::AnimationState(AnimatedModel* node, Animation* animation) :
  32. mNode(node),
  33. mStartBone(0),
  34. mLooped(false),
  35. mWeight(0.0f),
  36. mTime(0.0f),
  37. mPriority(0),
  38. mUseNlerp(false),
  39. mInterpolationFlags(0)
  40. {
  41. setAnimation(animation);
  42. }
  43. AnimationState::~AnimationState()
  44. {
  45. }
  46. void AnimationState::save(Serializer& dest)
  47. {
  48. dest.writeStringHash(mAnimation->getNameHash());
  49. dest.writeStringHash(mStartBone->getNameHash());
  50. dest.writeBool(mLooped);
  51. if (!mNode->isProxy())
  52. {
  53. dest.writeFloat(mWeight);
  54. dest.writeFloat(mTime);
  55. }
  56. else
  57. {
  58. dest.writeFloat(mInterpolationWeight);
  59. dest.writeFloat(mInterpolationTime);
  60. }
  61. dest.writeInt(mPriority);
  62. dest.writeBool(mUseNlerp);
  63. }
  64. void AnimationState::load(Deserializer& source)
  65. {
  66. // Loading the correct animation is handled by the AnimatedModel
  67. setStartBone(mNode->getSkeleton().getBone(source.readStringHash()));
  68. mLooped = source.readBool();
  69. if (!mNode->isProxy())
  70. {
  71. mWeight = clamp(source.readFloat(), 0.0f, 1.0f);
  72. mTime = clamp(source.readFloat(), 0.0f, mAnimation->getLength());
  73. }
  74. else
  75. {
  76. mInterpolationWeight = clamp(source.readFloat(), 0.0f, 1.0f);
  77. mInterpolationTime = clamp(source.readFloat(), 0.0f, mAnimation->getLength());
  78. mInterpolationFlags = INTERP_WEIGHT | INTERP_TIME;
  79. }
  80. mPriority = source.readInt();
  81. mUseNlerp = source.readBool();
  82. }
  83. void AnimationState::saveXML(XMLElement& element)
  84. {
  85. element.setString("name", mAnimation->getName());
  86. element.setString("startbone", mAnimation->getName());
  87. element.setBool("looped", isLooped());
  88. element.setFloat("weight", getWeight());
  89. element.setFloat("time", getTime());
  90. element.setInt("priority", getPriority());
  91. element.setBool("nlerp", getUseNlerp());
  92. }
  93. void AnimationState::loadXML(const XMLElement& element)
  94. {
  95. // Loading the correct animation is handled by the AnimatedModel
  96. setStartBone(mNode->getSkeleton().getBone(element.getString("startbone")));
  97. setLooped(element.getBool("looped"));
  98. setWeight(element.getFloat("weight"));
  99. setTime(element.getFloat("time"));
  100. setPriority(element.getInt("priority"));
  101. setUseNlerp(element.getBool("nlerp"));
  102. }
  103. void AnimationState::setAnimation(Animation* animation)
  104. {
  105. if (!animation)
  106. EXCEPTION("Null animation for AnimationState");
  107. mAnimation = animation;
  108. setStartBone(0);
  109. // Setup a cache for last keyframe of each track
  110. mLastKeyFrame.resize(mAnimation->getNumTracks());
  111. for (unsigned i = 0; i < mLastKeyFrame.size(); ++i)
  112. mLastKeyFrame[i] = 0;
  113. }
  114. void AnimationState::setStartBone(Bone* startBone)
  115. {
  116. Bone* rootBone = mNode->getSkeleton().getRootBone();
  117. if (!rootBone)
  118. return;
  119. if (!startBone)
  120. startBone = rootBone;
  121. if (mStartBone == startBone)
  122. return;
  123. mStartBone = startBone;
  124. mTrackToBoneMap.clear();
  125. const std::vector<AnimationTrack>& tracks = mAnimation->getTracks();
  126. for (unsigned i = 0; i < tracks.size(); ++i)
  127. {
  128. Bone* trackBone = 0;
  129. // Try to find a bone from the skeleton that corresponds to this track
  130. // with the limitation that it's startBone, or one of its children
  131. if (startBone->getNameHash() == tracks[i].mNameHash)
  132. trackBone = startBone;
  133. else
  134. {
  135. trackBone = dynamic_cast<Bone*>(startBone->getChild(tracks[i].mNameHash, true));
  136. // Make sure the child bone actually belongs to the skeleton
  137. if (trackBone)
  138. {
  139. if (trackBone->getRootBone() != rootBone)
  140. trackBone = 0;
  141. }
  142. }
  143. if (trackBone)
  144. mTrackToBoneMap[i] = trackBone;
  145. }
  146. mNode->markAnimationDirty();
  147. }
  148. void AnimationState::setLooped(bool looped)
  149. {
  150. mLooped = looped;
  151. }
  152. void AnimationState::setWeight(float weight)
  153. {
  154. weight = clamp(weight, 0.0f, 1.0f);
  155. // If we are a network proxy, always interpolate, regardless whether it is a server update or local prediction
  156. if (mNode->isProxy())
  157. {
  158. if (weight != mInterpolationWeight)
  159. {
  160. mInterpolationWeight = weight;
  161. mInterpolationFlags |= INTERP_WEIGHT;
  162. }
  163. }
  164. else
  165. {
  166. if (weight != mWeight)
  167. {
  168. mWeight = weight;
  169. mNode->markAnimationDirty();
  170. }
  171. }
  172. }
  173. void AnimationState::setTime(float time)
  174. {
  175. time = clamp(time, 0.0f, mAnimation->getLength());
  176. // If we are a network proxy, always interpolate, regardless whether it is a server update or local prediction
  177. if (mNode->isProxy())
  178. {
  179. if (time != mInterpolationTime)
  180. {
  181. mInterpolationTime = time;
  182. mInterpolationFlags |= INTERP_TIME;
  183. }
  184. }
  185. else
  186. {
  187. if (time != mTime)
  188. {
  189. mTime = time;
  190. mNode->markAnimationDirty();
  191. }
  192. }
  193. }
  194. void AnimationState::addWeight(float delta)
  195. {
  196. if (delta == 0.0f)
  197. return;
  198. // If we are a network proxy, use interpolation target weight as a base
  199. setWeight(getWeight() + delta);
  200. }
  201. void AnimationState::addTime(float delta)
  202. {
  203. float length = mAnimation->getLength();
  204. if ((delta == 0.0f) || (length == 0.0f))
  205. return;
  206. // If we are a network proxy, use interpolation target time as a base
  207. float time = getTime() + delta;
  208. if (mLooped)
  209. {
  210. while (time >= length)
  211. time -= length;
  212. while (time < 0.0f)
  213. time += length;
  214. }
  215. setTime(time);
  216. }
  217. void AnimationState::setPriority(int priority)
  218. {
  219. priority = clamp(priority, 0, 255);
  220. if (priority != mPriority)
  221. {
  222. mPriority = priority;
  223. mNode->markAnimationOrderDirty();
  224. }
  225. }
  226. void AnimationState::setUseNlerp(bool enable)
  227. {
  228. mUseNlerp = enable;
  229. }
  230. float AnimationState::getWeight() const
  231. {
  232. if (!mNode->isProxy())
  233. return mWeight;
  234. else
  235. return mInterpolationWeight;
  236. }
  237. float AnimationState::getTime() const
  238. {
  239. if (!mNode->isProxy())
  240. return mTime;
  241. else
  242. return mInterpolationTime;
  243. }
  244. void AnimationState::apply()
  245. {
  246. if (!isEnabled())
  247. return;
  248. // Check first if full weight or blending
  249. if (mWeight == 1.0f)
  250. {
  251. for (std::map<unsigned, Bone*>::const_iterator i = mTrackToBoneMap.begin(); i != mTrackToBoneMap.end(); ++i)
  252. {
  253. const AnimationTrack* track = mAnimation->getTrack(i->first);
  254. Bone* bone = i->second;
  255. if ((!bone->isAnimationEnabled()) || (!track->mKeyFrames.size()))
  256. continue;
  257. unsigned& frame = mLastKeyFrame[i->first];
  258. track->getKeyFrameIndex(mTime, frame);
  259. // Check if next frame to interpolate to is valid, or if wrapping is needed (looping animation only)
  260. unsigned nextFrame = frame + 1;
  261. bool interpolate = true;
  262. if (nextFrame >= track->mKeyFrames.size())
  263. {
  264. if (!mLooped)
  265. {
  266. nextFrame = frame;
  267. interpolate = false;
  268. }
  269. else
  270. nextFrame = 0;
  271. }
  272. const AnimationKeyFrame* keyFrame = &track->mKeyFrames[frame];
  273. unsigned char channelMask = track->mChannelMask;
  274. if (!interpolate)
  275. {
  276. // No interpolation, full weight
  277. if (channelMask & CHANNEL_POSITION)
  278. bone->setPosition(keyFrame->mPosition);
  279. if (channelMask & CHANNEL_ROTATION)
  280. bone->setRotation(keyFrame->mRotation);
  281. if (channelMask & CHANNEL_SCALE)
  282. bone->setScale(keyFrame->mScale);
  283. }
  284. else
  285. {
  286. const AnimationKeyFrame* nextKeyFrame = &track->mKeyFrames[nextFrame];
  287. float timeInterval = nextKeyFrame->mTime - keyFrame->mTime;
  288. if (timeInterval < 0.0f)
  289. timeInterval += mAnimation->getLength();
  290. float t = clamp((mTime - keyFrame->mTime) / timeInterval, 0.0f, 1.0f);
  291. // Interpolation, full weight
  292. if (channelMask & CHANNEL_POSITION)
  293. bone->setPosition(keyFrame->mPosition.lerp(nextKeyFrame->mPosition, t));
  294. if (channelMask & CHANNEL_ROTATION)
  295. {
  296. if (!mUseNlerp)
  297. bone->setRotation(keyFrame->mRotation.slerp(nextKeyFrame->mRotation, t));
  298. else
  299. bone->setRotation(keyFrame->mRotation.nlerpFast(nextKeyFrame->mRotation, t));
  300. }
  301. if (channelMask & CHANNEL_SCALE)
  302. bone->setScale(keyFrame->mScale.lerp(nextKeyFrame->mScale, t));
  303. }
  304. }
  305. }
  306. else
  307. {
  308. for (std::map<unsigned, Bone*>::const_iterator i = mTrackToBoneMap.begin(); i != mTrackToBoneMap.end(); ++i)
  309. {
  310. const AnimationTrack* track = mAnimation->getTrack(i->first);
  311. Bone* bone = i->second;
  312. if ((!bone->isAnimationEnabled()) || (!track->mKeyFrames.size()))
  313. continue;
  314. unsigned& frame = mLastKeyFrame[i->first];
  315. track->getKeyFrameIndex(mTime, frame);
  316. // Check if next frame to interpolate to is valid, or if wrapping is needed (looping animation only)
  317. unsigned nextFrame = frame + 1;
  318. bool interpolate = true;
  319. if (nextFrame >= track->mKeyFrames.size())
  320. {
  321. if (!mLooped)
  322. {
  323. nextFrame = frame;
  324. interpolate = false;
  325. }
  326. else
  327. nextFrame = 0;
  328. }
  329. const AnimationKeyFrame* keyFrame = &track->mKeyFrames[frame];
  330. unsigned char channelMask = track->mChannelMask;
  331. if (!interpolate)
  332. {
  333. // No interpolation, blend between old transform & animation
  334. if (channelMask & CHANNEL_POSITION)
  335. bone->setPosition(bone->getPosition().lerp(keyFrame->mPosition, mWeight));
  336. if (channelMask & CHANNEL_ROTATION)
  337. {
  338. if (!mUseNlerp)
  339. bone->setRotation(bone->getRotation().slerp(keyFrame->mRotation, mWeight));
  340. else
  341. bone->setRotation(bone->getRotation().nlerpFast(keyFrame->mRotation, mWeight));
  342. }
  343. if (channelMask & CHANNEL_SCALE)
  344. bone->setScale(bone->getScale().lerp(keyFrame->mScale, mWeight));
  345. }
  346. else
  347. {
  348. const AnimationKeyFrame* nextKeyFrame = &track->mKeyFrames[nextFrame];
  349. float timeInterval = nextKeyFrame->mTime - keyFrame->mTime;
  350. if (timeInterval < 0.0f)
  351. timeInterval += mAnimation->getLength();
  352. float t = clamp((mTime - keyFrame->mTime) / timeInterval, 0.0f, 1.0f);
  353. // Interpolation, blend between old transform & animation
  354. if (channelMask & CHANNEL_POSITION)
  355. {
  356. bone->setPosition(bone->getPosition().lerp(
  357. keyFrame->mPosition.lerp(nextKeyFrame->mPosition, t), mWeight));
  358. }
  359. if (channelMask & CHANNEL_ROTATION)
  360. {
  361. if (!mUseNlerp)
  362. {
  363. bone->setRotation(bone->getRotation().slerp(
  364. keyFrame->mRotation.slerp(nextKeyFrame->mRotation, t), mWeight));
  365. }
  366. else
  367. {
  368. bone->setRotation(bone->getRotation().nlerpFast(
  369. keyFrame->mRotation.nlerpFast(nextKeyFrame->mRotation, t), mWeight));
  370. }
  371. }
  372. if (channelMask & CHANNEL_SCALE)
  373. {
  374. bone->setScale(bone->getScale().lerp(
  375. keyFrame->mScale.lerp(nextKeyFrame->mScale, t), mWeight));
  376. }
  377. }
  378. }
  379. }
  380. }
  381. void AnimationState::sync(AnimationState* src)
  382. {
  383. Bone* srcStartBone = src->getStartBone();
  384. if (!srcStartBone)
  385. return;
  386. if (mStartBone->getName() != srcStartBone->getName())
  387. {
  388. // Note: this may return null, in that case new startbone is simply the rootbone
  389. // Downside is that we will attempt changing the startbone each time sync() is called
  390. Bone* newStartBone = mNode->getSkeleton().getBone(srcStartBone->getNameHash());
  391. setStartBone(newStartBone);
  392. }
  393. setLooped(src->isLooped());
  394. setPriority(src->getPriority());
  395. setTime(src->getTime());
  396. setWeight(src->getWeight());
  397. }
  398. void AnimationState::interpolate(bool snapToEnd, float t)
  399. {
  400. if (!mInterpolationFlags)
  401. return;
  402. if (!snapToEnd)
  403. {
  404. if (mInterpolationFlags & INTERP_WEIGHT)
  405. {
  406. if (fabsf(mWeight - mInterpolationWeight) < M_EPSILON)
  407. {
  408. mWeight = mInterpolationWeight;
  409. mInterpolationFlags &= ~INTERP_WEIGHT;
  410. }
  411. else
  412. mWeight = lerp(mWeight, mInterpolationWeight, t);
  413. }
  414. if (mInterpolationFlags & INTERP_TIME)
  415. {
  416. if (fabsf(mTime - mInterpolationTime) < M_EPSILON)
  417. {
  418. mTime = mInterpolationTime;
  419. mInterpolationFlags &= ~INTERP_TIME;
  420. }
  421. else
  422. {
  423. // If animation is looping, use the shortest path
  424. if (mLooped)
  425. {
  426. float lerpStartTime = mTime;
  427. float animLength = mAnimation->getLength();
  428. if (fabsf(mInterpolationTime - lerpStartTime) > (animLength * 0.5f))
  429. {
  430. if (lerpStartTime > mInterpolationTime)
  431. lerpStartTime -= animLength;
  432. else
  433. lerpStartTime += animLength;
  434. }
  435. mTime = lerp(lerpStartTime, mInterpolationTime, t);
  436. if (mTime < 0.0f)
  437. mTime += animLength;
  438. if (mTime > animLength)
  439. mTime -= animLength;
  440. }
  441. else
  442. mTime = lerp(mTime, mInterpolationTime, t);
  443. }
  444. }
  445. }
  446. else
  447. {
  448. if (mInterpolationFlags & INTERP_WEIGHT)
  449. mWeight = mInterpolationWeight;
  450. if (mInterpolationFlags & INTERP_TIME)
  451. mTime = mInterpolationTime;
  452. mInterpolationFlags = INTERP_NONE;
  453. }
  454. mNode->markAnimationDirty();
  455. }