AnimationState.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511
  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 = 1.0f;
  291. if (timeInterval > 0.0f)
  292. t = (mTime - keyFrame->mTime) / timeInterval;
  293. // Interpolation, full weight
  294. if (channelMask & CHANNEL_POSITION)
  295. bone->setPosition(keyFrame->mPosition.lerp(nextKeyFrame->mPosition, t));
  296. if (channelMask & CHANNEL_ROTATION)
  297. {
  298. if (!mUseNlerp)
  299. bone->setRotation(keyFrame->mRotation.slerp(nextKeyFrame->mRotation, t));
  300. else
  301. bone->setRotation(keyFrame->mRotation.nlerpFast(nextKeyFrame->mRotation, t));
  302. }
  303. if (channelMask & CHANNEL_SCALE)
  304. bone->setScale(keyFrame->mScale.lerp(nextKeyFrame->mScale, t));
  305. }
  306. }
  307. }
  308. else
  309. {
  310. for (std::map<unsigned, Bone*>::const_iterator i = mTrackToBoneMap.begin(); i != mTrackToBoneMap.end(); ++i)
  311. {
  312. const AnimationTrack* track = mAnimation->getTrack(i->first);
  313. Bone* bone = i->second;
  314. if ((!bone->isAnimationEnabled()) || (!track->mKeyFrames.size()))
  315. continue;
  316. unsigned& frame = mLastKeyFrame[i->first];
  317. track->getKeyFrameIndex(mTime, frame);
  318. // Check if next frame to interpolate to is valid, or if wrapping is needed (looping animation only)
  319. unsigned nextFrame = frame + 1;
  320. bool interpolate = true;
  321. if (nextFrame >= track->mKeyFrames.size())
  322. {
  323. if (!mLooped)
  324. {
  325. nextFrame = frame;
  326. interpolate = false;
  327. }
  328. else
  329. nextFrame = 0;
  330. }
  331. const AnimationKeyFrame* keyFrame = &track->mKeyFrames[frame];
  332. unsigned char channelMask = track->mChannelMask;
  333. if (!interpolate)
  334. {
  335. // No interpolation, blend between old transform & animation
  336. if (channelMask & CHANNEL_POSITION)
  337. bone->setPosition(bone->getPosition().lerp(keyFrame->mPosition, mWeight));
  338. if (channelMask & CHANNEL_ROTATION)
  339. {
  340. if (!mUseNlerp)
  341. bone->setRotation(bone->getRotation().slerp(keyFrame->mRotation, mWeight));
  342. else
  343. bone->setRotation(bone->getRotation().nlerpFast(keyFrame->mRotation, mWeight));
  344. }
  345. if (channelMask & CHANNEL_SCALE)
  346. bone->setScale(bone->getScale().lerp(keyFrame->mScale, mWeight));
  347. }
  348. else
  349. {
  350. const AnimationKeyFrame* nextKeyFrame = &track->mKeyFrames[nextFrame];
  351. float timeInterval = nextKeyFrame->mTime - keyFrame->mTime;
  352. if (timeInterval < 0.0f)
  353. timeInterval += mAnimation->getLength();
  354. float t = 1.0f;
  355. if (timeInterval > 0.0f)
  356. t = (mTime - keyFrame->mTime) / timeInterval;
  357. // Interpolation, blend between old transform & animation
  358. if (channelMask & CHANNEL_POSITION)
  359. {
  360. bone->setPosition(bone->getPosition().lerp(
  361. keyFrame->mPosition.lerp(nextKeyFrame->mPosition, t), mWeight));
  362. }
  363. if (channelMask & CHANNEL_ROTATION)
  364. {
  365. if (!mUseNlerp)
  366. {
  367. bone->setRotation(bone->getRotation().slerp(
  368. keyFrame->mRotation.slerp(nextKeyFrame->mRotation, t), mWeight));
  369. }
  370. else
  371. {
  372. bone->setRotation(bone->getRotation().nlerpFast(
  373. keyFrame->mRotation.nlerpFast(nextKeyFrame->mRotation, t), mWeight));
  374. }
  375. }
  376. if (channelMask & CHANNEL_SCALE)
  377. {
  378. bone->setScale(bone->getScale().lerp(
  379. keyFrame->mScale.lerp(nextKeyFrame->mScale, t), mWeight));
  380. }
  381. }
  382. }
  383. }
  384. }
  385. void AnimationState::sync(AnimationState* src)
  386. {
  387. Bone* srcStartBone = src->getStartBone();
  388. if (!srcStartBone)
  389. return;
  390. if (mStartBone->getName() != srcStartBone->getName())
  391. {
  392. // Note: this may return null, in that case new startbone is simply the rootbone
  393. // Downside is that we will attempt changing the startbone each time sync() is called
  394. Bone* newStartBone = mNode->getSkeleton().getBone(srcStartBone->getNameHash());
  395. setStartBone(newStartBone);
  396. }
  397. setLooped(src->isLooped());
  398. setPriority(src->getPriority());
  399. setTime(src->getTime());
  400. setWeight(src->getWeight());
  401. }
  402. void AnimationState::interpolate(bool snapToEnd, float t)
  403. {
  404. if (!mInterpolationFlags)
  405. return;
  406. if (!snapToEnd)
  407. {
  408. if (mInterpolationFlags & INTERP_WEIGHT)
  409. {
  410. if (fabsf(mWeight - mInterpolationWeight) < M_EPSILON)
  411. {
  412. mWeight = mInterpolationWeight;
  413. mInterpolationFlags &= ~INTERP_WEIGHT;
  414. }
  415. else
  416. mWeight = lerp(mWeight, mInterpolationWeight, t);
  417. }
  418. if (mInterpolationFlags & INTERP_TIME)
  419. {
  420. if (fabsf(mTime - mInterpolationTime) < M_EPSILON)
  421. {
  422. mTime = mInterpolationTime;
  423. mInterpolationFlags &= ~INTERP_TIME;
  424. }
  425. else
  426. {
  427. // If animation is looping, use the shortest path
  428. if (mLooped)
  429. {
  430. float lerpStartTime = mTime;
  431. float animLength = mAnimation->getLength();
  432. if (fabsf(mInterpolationTime - lerpStartTime) > (animLength * 0.5f))
  433. {
  434. if (lerpStartTime > mInterpolationTime)
  435. lerpStartTime -= animLength;
  436. else
  437. lerpStartTime += animLength;
  438. }
  439. mTime = lerp(lerpStartTime, mInterpolationTime, t);
  440. if (mTime < 0.0f)
  441. mTime += animLength;
  442. if (mTime > animLength)
  443. mTime -= animLength;
  444. }
  445. else
  446. mTime = lerp(mTime, mInterpolationTime, t);
  447. }
  448. }
  449. }
  450. else
  451. {
  452. if (mInterpolationFlags & INTERP_WEIGHT)
  453. mWeight = mInterpolationWeight;
  454. if (mInterpolationFlags & INTERP_TIME)
  455. mTime = mInterpolationTime;
  456. mInterpolationFlags = INTERP_NONE;
  457. }
  458. mNode->markAnimationDirty();
  459. }