AnimationState.cpp 17 KB

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