Animation.cpp 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2012 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 "Animation.h"
  25. #include "Context.h"
  26. #include "Deserializer.h"
  27. #include "FileSystem.h"
  28. #include "Log.h"
  29. #include "Profiler.h"
  30. #include "ResourceCache.h"
  31. #include "Serializer.h"
  32. #include "XMLFile.h"
  33. #include "DebugNew.h"
  34. inline bool CompareTriggers(AnimationTriggerPoint& lhs, AnimationTriggerPoint& rhs)
  35. {
  36. return lhs.time_ < rhs.time_;
  37. }
  38. void AnimationTrack::GetKeyFrameIndex(float time, unsigned& index) const
  39. {
  40. if (time < 0.0f)
  41. time = 0.0f;
  42. if (index >= keyFrames_.Size())
  43. index = keyFrames_.Size() - 1;
  44. // Check for being too far ahead
  45. while (index && time < keyFrames_[index].time_)
  46. index--;
  47. // Check for being too far behind
  48. while (index < keyFrames_.Size() - 1 && time >= keyFrames_[index + 1].time_)
  49. index++;
  50. }
  51. OBJECTTYPESTATIC(Animation);
  52. Animation::Animation(Context* context) :
  53. Resource(context)
  54. {
  55. }
  56. Animation::~Animation()
  57. {
  58. }
  59. void Animation::RegisterObject(Context* context)
  60. {
  61. context->RegisterFactory<Animation>();
  62. }
  63. bool Animation::Load(Deserializer& source)
  64. {
  65. PROFILE(LoadAnimation);
  66. unsigned memoryUse = sizeof(Animation);
  67. // Check ID
  68. if (source.ReadFileID() != "UANI")
  69. {
  70. LOGERROR(source.GetName() + " is not a valid animation file");
  71. return false;
  72. }
  73. // Read name and length
  74. animationName_ = source.ReadString();
  75. animationNameHash_ = StringHash(animationName_);
  76. length_ = source.ReadFloat();
  77. tracks_.Clear();
  78. unsigned tracks = source.ReadUInt();
  79. tracks_.Resize(tracks);
  80. memoryUse += tracks * sizeof(AnimationTrack);
  81. // Read tracks
  82. for (unsigned i = 0; i < tracks; ++i)
  83. {
  84. AnimationTrack& newTrack = tracks_[i];
  85. newTrack.name_ = source.ReadString();
  86. newTrack.nameHash_ = StringHash(newTrack.name_);
  87. newTrack.channelMask_ = source.ReadUByte();
  88. unsigned keyFrames = source.ReadUInt();
  89. newTrack.keyFrames_.Resize(keyFrames);
  90. memoryUse += keyFrames * sizeof(AnimationKeyFrame);
  91. // Read keyframes of the track
  92. for (unsigned j = 0; j < keyFrames; ++j)
  93. {
  94. AnimationKeyFrame& newKeyFrame = newTrack.keyFrames_[j];
  95. newKeyFrame.time_ = source.ReadFloat();
  96. if (newTrack.channelMask_ & CHANNEL_POSITION)
  97. newKeyFrame.position_ = source.ReadVector3();
  98. if (newTrack.channelMask_ & CHANNEL_ROTATION)
  99. newKeyFrame.rotation_ = source.ReadQuaternion();
  100. if (newTrack.channelMask_ & CHANNEL_SCALE)
  101. newKeyFrame.scale_ = source.ReadVector3();
  102. }
  103. }
  104. // Optionally read triggers from an XML file
  105. ResourceCache* cache = GetSubsystem<ResourceCache>();
  106. String xmlName = ReplaceExtension(GetName(), ".xml");
  107. if (cache->Exists(xmlName))
  108. {
  109. XMLFile* file = cache->GetResource<XMLFile>(xmlName);
  110. if (file)
  111. {
  112. XMLElement rootElem = file->GetRoot();
  113. XMLElement triggerElem = rootElem.GetChild("trigger");
  114. while (triggerElem)
  115. {
  116. if (triggerElem.HasAttribute("normalizedtime"))
  117. AddTrigger(triggerElem.GetFloat("normalizedtime"), true, triggerElem.GetVariant());
  118. else if (triggerElem.HasAttribute("time"))
  119. AddTrigger(triggerElem.GetFloat("time"), false, triggerElem.GetVariant());
  120. triggerElem = triggerElem.GetNext("trigger");
  121. }
  122. memoryUse += triggers_.Size() * sizeof(AnimationTriggerPoint);
  123. }
  124. }
  125. SetMemoryUse(memoryUse);
  126. return true;
  127. }
  128. bool Animation::Save(Serializer& dest)
  129. {
  130. // Write ID, name and length
  131. dest.WriteFileID("UANI");
  132. dest.WriteString(animationName_);
  133. dest.WriteFloat(length_);
  134. // Write tracks
  135. dest.WriteUInt(tracks_.Size());
  136. for (unsigned i = 0; i < tracks_.Size(); ++i)
  137. {
  138. const AnimationTrack& track = tracks_[i];
  139. dest.WriteString(track.name_);
  140. dest.WriteUByte(track.channelMask_);
  141. dest.WriteUInt(track.keyFrames_.Size());
  142. // Write keyframes of the track
  143. for (unsigned j = 0; j < track.keyFrames_.Size(); ++j)
  144. {
  145. const AnimationKeyFrame& keyFrame = track.keyFrames_[j];
  146. dest.WriteFloat(keyFrame.time_);
  147. if (track.channelMask_ & CHANNEL_POSITION)
  148. dest.WriteVector3(keyFrame.position_);
  149. if (track.channelMask_ & CHANNEL_ROTATION)
  150. dest.WriteQuaternion(keyFrame.rotation_);
  151. if (track.channelMask_ & CHANNEL_SCALE)
  152. dest.WriteVector3(keyFrame.scale_);
  153. }
  154. }
  155. // If triggers have been defined, write an XML file for them
  156. if (triggers_.Size())
  157. {
  158. File* destFile = dynamic_cast<File*>(&dest);
  159. if (destFile)
  160. {
  161. String xmlName = ReplaceExtension(destFile->GetName(), ".xml");
  162. SharedPtr<XMLFile> xml(new XMLFile(context_));
  163. XMLElement rootElem = xml->CreateRoot("animation");
  164. for (unsigned i = 0; i < triggers_.Size(); ++i)
  165. {
  166. XMLElement triggerElem = rootElem.CreateChild("trigger");
  167. triggerElem.SetFloat("time", triggers_[i].time_);
  168. triggerElem.SetVariant(triggers_[i].data_);
  169. }
  170. File xmlFile(context_, xmlName, FILE_WRITE);
  171. xml->Save(xmlFile);
  172. }
  173. else
  174. LOGWARNING("Can not save animation trigger data when not saving into a file");
  175. }
  176. return true;
  177. }
  178. void Animation::SetAnimationName(const String& name)
  179. {
  180. animationName_ = name;
  181. animationNameHash_ = StringHash(name);
  182. }
  183. void Animation::SetLength(float length)
  184. {
  185. length_ = Max(length, 0.0f);
  186. }
  187. void Animation::SetTracks(const Vector<AnimationTrack>& tracks)
  188. {
  189. tracks_ = tracks;
  190. }
  191. void Animation::AddTrigger(float time, bool timeIsNormalized, const Variant& data)
  192. {
  193. AnimationTriggerPoint newTrigger;
  194. newTrigger.time_ = timeIsNormalized ? time * length_ : time;
  195. newTrigger.data_ = data;
  196. triggers_.Push(newTrigger);
  197. Sort(triggers_.Begin(), triggers_.End(), CompareTriggers);
  198. }
  199. void Animation::RemoveTrigger(unsigned index)
  200. {
  201. if (index < triggers_.Size())
  202. triggers_.Erase(index);
  203. }
  204. void Animation::RemoveAllTriggers()
  205. {
  206. triggers_.Clear();
  207. }
  208. const AnimationTrack* Animation::GetTrack(unsigned index) const
  209. {
  210. return index < tracks_.Size() ? &tracks_[index] : 0;
  211. }
  212. const AnimationTrack* Animation::GetTrack(const String& name) const
  213. {
  214. for (Vector<AnimationTrack>::ConstIterator i = tracks_.Begin(); i != tracks_.End(); ++i)
  215. {
  216. if (i->name_ == name)
  217. return &(*i);
  218. }
  219. return 0;
  220. }
  221. const AnimationTrack* Animation::GetTrack(StringHash nameHash) const
  222. {
  223. for (Vector<AnimationTrack>::ConstIterator i = tracks_.Begin(); i != tracks_.End(); ++i)
  224. {
  225. if (i->nameHash_ == nameHash)
  226. return &(*i);
  227. }
  228. return 0;
  229. }