2
0

AnimationState.cpp 14 KB

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