AttributeAnimation.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  1. //
  2. // Copyright (c) 2008-2014 the Urho3D project.
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #include "Precompiled.h"
  23. #include "Animatable.h"
  24. #include "AttributeAnimation.h"
  25. #include "Context.h"
  26. #include "Deserializer.h"
  27. #include "Log.h"
  28. #include "ObjectAnimation.h"
  29. #include "Serializer.h"
  30. #include "XMLFile.h"
  31. #include "DebugNew.h"
  32. namespace Urho3D
  33. {
  34. AttributeAnimation::AttributeAnimation(Context* context) :
  35. Resource(context),
  36. interpolationMethod_(IM_LINEAR),
  37. splineTension_(0.5f),
  38. valueType_(VAR_NONE),
  39. isInterpolatable_(false),
  40. beginTime_(M_INFINITY),
  41. endTime_(-M_INFINITY),
  42. splineTangentsDirty_(false)
  43. {
  44. }
  45. AttributeAnimation::~AttributeAnimation()
  46. {
  47. }
  48. void AttributeAnimation::RegisterObject(Context* context)
  49. {
  50. context->RegisterFactory<AttributeAnimation>();
  51. }
  52. bool AttributeAnimation::Load(Deserializer& source)
  53. {
  54. XMLFile xmlFile(context_);
  55. if (!xmlFile.Load(source))
  56. return false;
  57. return LoadXML(xmlFile.GetRoot());
  58. }
  59. bool AttributeAnimation::Save(Serializer& dest) const
  60. {
  61. XMLFile xmlFile(context_);
  62. XMLElement rootElem = xmlFile.CreateRoot("attributeanimation");
  63. if (!SaveXML(rootElem))
  64. return false;
  65. return xmlFile.Save(dest);
  66. }
  67. bool AttributeAnimation::LoadXML(const XMLElement& source)
  68. {
  69. valueType_ = VAR_NONE;
  70. eventFrames_.Clear();
  71. XMLElement keyFrameEleme = source.GetChild("keyframe");
  72. while (keyFrameEleme)
  73. {
  74. float time = keyFrameEleme.GetFloat("time");
  75. Variant value = keyFrameEleme.GetVariant();
  76. SetKeyFrame(time, value);
  77. keyFrameEleme = keyFrameEleme.GetNext("keyframe");
  78. }
  79. XMLElement eventFrameElem = source.GetChild("eventframe");
  80. while (eventFrameElem)
  81. {
  82. float time = eventFrameElem.GetFloat("time");
  83. unsigned eventType = eventFrameElem.GetUInt("eventtype");
  84. VariantMap eventData = eventFrameElem.GetChild("eventdata").GetVariantMap();
  85. SetEventFrame(time, StringHash(eventType), eventData);
  86. eventFrameElem = eventFrameElem.GetNext("eventframe");
  87. }
  88. return true;
  89. }
  90. bool AttributeAnimation::SaveXML(XMLElement& dest) const
  91. {
  92. for (unsigned i = 0; i < keyFrames_.Size(); ++i)
  93. {
  94. const AttributeKeyFrame& keyFrame = keyFrames_[i];
  95. XMLElement keyFrameEleme = dest.CreateChild("keyframe");
  96. keyFrameEleme.SetFloat("time", keyFrame.time_);
  97. keyFrameEleme.SetVariant(keyFrame.value_);
  98. }
  99. for (unsigned i = 0; i < eventFrames_.Size(); ++i)
  100. {
  101. const AttributeEventFrame& eventFrame = eventFrames_[i];
  102. XMLElement eventFrameElem = dest.CreateChild("eventframe");
  103. eventFrameElem.SetFloat("time", eventFrame.time_);
  104. eventFrameElem.SetUInt("eventtype", eventFrame.eventType_.Value());
  105. eventFrameElem.CreateChild("eventdata").SetVariantMap(eventFrame.eventData_);
  106. }
  107. return true;
  108. }
  109. void AttributeAnimation::SetObjectAnimation(ObjectAnimation* objectAnimation)
  110. {
  111. objectAnimation_ = objectAnimation;
  112. }
  113. void AttributeAnimation::SetValueType(VariantType valueType)
  114. {
  115. if (valueType == valueType_)
  116. return;
  117. valueType_ = valueType;
  118. isInterpolatable_ = (valueType_ == VAR_FLOAT) || (valueType_ == VAR_VECTOR2) || (valueType_ == VAR_VECTOR3) || (valueType_ == VAR_VECTOR4) ||
  119. (valueType_ == VAR_QUATERNION) || (valueType_ == VAR_COLOR);
  120. if ((valueType_ == VAR_INTRECT) || (valueType_ == VAR_INTVECTOR2))
  121. {
  122. isInterpolatable_ = true;
  123. // Force linear interpolation for IntRect and IntVector2
  124. interpolationMethod_ = IM_LINEAR;
  125. }
  126. keyFrames_.Clear();
  127. beginTime_ = M_INFINITY;
  128. endTime_ = -M_INFINITY;
  129. }
  130. void AttributeAnimation::SetInterpolationMethod(InterpolationMethod method)
  131. {
  132. if (method == interpolationMethod_)
  133. return;
  134. // Force linear interpolation for IntRect and IntVector2
  135. if ((valueType_ == VAR_INTRECT) || (valueType_ == VAR_INTVECTOR2))
  136. method = IM_LINEAR;
  137. interpolationMethod_ = method;
  138. splineTangentsDirty_ = true;
  139. }
  140. void AttributeAnimation::SetSplineTension(float tension)
  141. {
  142. splineTension_ = tension;
  143. splineTangentsDirty_ = true;
  144. }
  145. bool AttributeAnimation::SetKeyFrame(float time, const Variant& value)
  146. {
  147. if (valueType_ == VAR_NONE)
  148. SetValueType(value.GetType());
  149. else if (value.GetType() != valueType_)
  150. return false;
  151. beginTime_ = Min(time, beginTime_);
  152. endTime_ = Max(time, endTime_);
  153. AttributeKeyFrame keyFrame;
  154. keyFrame.time_ = time;
  155. keyFrame.value_ = value;
  156. if (keyFrames_.Empty() || time >= keyFrames_.Back().time_)
  157. keyFrames_.Push(keyFrame);
  158. else
  159. {
  160. for (unsigned i = 0; i < keyFrames_.Size(); ++i)
  161. {
  162. if (time < keyFrames_[i].time_)
  163. {
  164. keyFrames_.Insert(i, keyFrame);
  165. break;
  166. }
  167. }
  168. }
  169. splineTangentsDirty_ = true;
  170. return true;
  171. }
  172. void AttributeAnimation::SetEventFrame(float time, const StringHash& eventType, const VariantMap& eventData)
  173. {
  174. AttributeEventFrame eventFrame;
  175. eventFrame.time_ = time;
  176. eventFrame.eventType_ = eventType;
  177. eventFrame.eventData_ = eventData;
  178. if (eventFrames_.Empty() || time >= eventFrames_.Back().time_)
  179. eventFrames_.Push(eventFrame);
  180. else
  181. {
  182. for (unsigned i = 0; i < eventFrames_.Size(); ++i)
  183. {
  184. if (time < eventFrames_[i].time_)
  185. {
  186. eventFrames_.Insert(i, eventFrame);
  187. break;
  188. }
  189. }
  190. }
  191. }
  192. bool AttributeAnimation::IsValid() const
  193. {
  194. return (interpolationMethod_ == IM_LINEAR && keyFrames_.Size() > 1) || (interpolationMethod_ == IM_SPLINE && keyFrames_.Size() > 2);
  195. }
  196. ObjectAnimation* AttributeAnimation::GetObjectAnimation() const
  197. {
  198. return objectAnimation_;
  199. }
  200. void AttributeAnimation::UpdateAttributeValue(Animatable* animatable, const AttributeInfo& attributeInfo, float scaledTime)
  201. {
  202. unsigned index = 1;
  203. for (; index < keyFrames_.Size(); ++index)
  204. {
  205. if (scaledTime < keyFrames_[index].time_)
  206. break;
  207. }
  208. if (index >= keyFrames_.Size() || !isInterpolatable_)
  209. animatable->OnSetAttribute(attributeInfo, keyFrames_[index - 1].value_);
  210. else
  211. {
  212. if (interpolationMethod_ == IM_LINEAR)
  213. animatable->OnSetAttribute(attributeInfo, LinearInterpolation(index - 1, index, scaledTime));
  214. else
  215. animatable->OnSetAttribute(attributeInfo, SplineInterpolation(index - 1, index, scaledTime));
  216. }
  217. }
  218. void AttributeAnimation::GetEventFrames(float beginTime, float endTime, Vector<const AttributeEventFrame*>& eventFrames) const
  219. {
  220. for (unsigned i = 0; i < eventFrames_.Size(); ++i)
  221. {
  222. const AttributeEventFrame& eventFrame = eventFrames_[i];
  223. if (eventFrame.time_ >= endTime)
  224. break;
  225. if (eventFrame.time_ >= beginTime)
  226. eventFrames.Push(&eventFrame);
  227. }
  228. }
  229. Variant AttributeAnimation::LinearInterpolation(unsigned index1, unsigned index2, float scaledTime) const
  230. {
  231. const AttributeKeyFrame& keyFrame1 = keyFrames_[index1];
  232. const AttributeKeyFrame& keyFrame2 = keyFrames_[index2];
  233. float t = (scaledTime - keyFrame1.time_) / (keyFrame2.time_ - keyFrame1.time_);
  234. return LinearInterpolation(keyFrame1.value_, keyFrame2.value_, t);
  235. }
  236. Variant AttributeAnimation::LinearInterpolation(const Variant& value1, const Variant& value2, float t) const
  237. {
  238. switch (valueType_)
  239. {
  240. case VAR_FLOAT:
  241. return Lerp(value1.GetFloat(), value2.GetFloat(), t);
  242. case VAR_VECTOR2:
  243. return value1.GetVector2().Lerp(value2.GetVector2(), t);
  244. case VAR_VECTOR3:
  245. return value1.GetVector3().Lerp(value2.GetVector3(), t);
  246. case VAR_VECTOR4:
  247. return value1.GetVector4().Lerp(value2.GetVector4(), t);
  248. case VAR_QUATERNION:
  249. return value1.GetQuaternion().Slerp(value2.GetQuaternion(), t);
  250. case VAR_COLOR:
  251. return value1.GetColor().Lerp(value2.GetColor(), t);
  252. case VAR_INTRECT:
  253. {
  254. float s = 1.0f - t;
  255. const IntRect& r1 = value1.GetIntRect();
  256. const IntRect& r2 = value2.GetIntRect();
  257. return IntRect((int)(r1.left_ * s + r2.left_ * t), (int)(r1.top_ * s + r2.top_ * t), (int)(r1.right_ * s + r2.right_ * t), (int)(r1.bottom_ * s + r2.bottom_ * t));
  258. }
  259. case VAR_INTVECTOR2:
  260. {
  261. float s = 1.0f - t;
  262. const IntVector2& v1 = value1.GetIntVector2();
  263. const IntVector2& v2 = value2.GetIntVector2();
  264. return IntVector2((int)(v1.x_ * s + v2.x_ * t), (int)(v1.y_ * s + v2.y_ * t));
  265. }
  266. }
  267. LOGERROR("Invalid value type for linear interpolation");
  268. return Variant::EMPTY;
  269. }
  270. Variant AttributeAnimation::SplineInterpolation(unsigned index1, unsigned index2, float scaledTime)
  271. {
  272. if (splineTangentsDirty_)
  273. UpdateSplineTangents();
  274. const AttributeKeyFrame& keyFrame1 = keyFrames_[index1];
  275. const AttributeKeyFrame& keyFrame2 = keyFrames_[index2];
  276. float t = (scaledTime - keyFrame1.time_) / (keyFrame2.time_ - keyFrame1.time_);
  277. float tt = t * t;
  278. float ttt = t * tt;
  279. float h1 = 2.0f * ttt - 3.0f * tt + 1.0f;
  280. float h2 = -2.0f * ttt + 3.0f * tt;
  281. float h3 = ttt - 2.0f * tt + t;
  282. float h4 = ttt - tt;
  283. const Variant& v1 = keyFrame1.value_;
  284. const Variant& v2 = keyFrame2.value_;
  285. const Variant& t1 = splineTangents_[index1];
  286. const Variant& t2 = splineTangents_[index2];
  287. switch (valueType_)
  288. {
  289. case VAR_FLOAT:
  290. return v1.GetFloat() * h1 + v2.GetFloat() * h2 + t1.GetFloat() * h3 + t2.GetFloat() * h4;
  291. case VAR_VECTOR2:
  292. return v1.GetVector2() * h1 + v2.GetVector2() * h2 + t1.GetVector2() * h3 + t2.GetVector2() * h4;
  293. case VAR_VECTOR3:
  294. return v1.GetVector3() * h1 + v2.GetVector3() * h2 + t1.GetVector3() * h3 + t2.GetVector3() * h4;
  295. case VAR_VECTOR4:
  296. return v1.GetVector4() * h1 + v2.GetVector4() * h2 + t1.GetVector4() * h3 + t2.GetVector4() * h4;
  297. case VAR_QUATERNION:
  298. return v1.GetQuaternion() * h1 + v2.GetQuaternion() * h2 + t1.GetQuaternion() * h3 + t2.GetQuaternion() * h4;
  299. case VAR_COLOR:
  300. return v1.GetColor() * h1 + v2.GetColor() * h2 + t1.GetColor() * h3 + t2.GetColor() * h4;
  301. }
  302. LOGERROR("Invalid value type for spline interpolation");
  303. return Variant::EMPTY;
  304. }
  305. void AttributeAnimation::UpdateSplineTangents()
  306. {
  307. splineTangents_.Clear();
  308. if (!IsValid())
  309. return;
  310. unsigned size = keyFrames_.Size();
  311. splineTangents_.Resize(size);
  312. for (unsigned i = 1; i < size - 1; ++i)
  313. splineTangents_[i] = SubstractAndMultiply(keyFrames_[i + 1].value_, keyFrames_[i - 1].value_, splineTension_);
  314. // Make end point's tangent zero
  315. splineTangents_[0] = splineTangents_[size - 1] = SubstractAndMultiply(keyFrames_[0].value_, keyFrames_[0].value_, splineTension_);
  316. splineTangentsDirty_ = false;
  317. }
  318. Variant AttributeAnimation::SubstractAndMultiply(const Variant& value1, const Variant& value2, float t) const
  319. {
  320. switch (valueType_)
  321. {
  322. case VAR_FLOAT:
  323. return (value1.GetFloat() - value2.GetFloat()) * t;
  324. case VAR_VECTOR2:
  325. return (value1.GetVector2() - value2.GetVector2()) * t;
  326. case VAR_VECTOR3:
  327. return (value1.GetVector3() - value2.GetVector3()) * t;
  328. case VAR_VECTOR4:
  329. return (value1.GetVector4() - value2.GetVector4()) * t;
  330. case VAR_QUATERNION:
  331. return (value1.GetQuaternion() - value2.GetQuaternion()) * t;
  332. case VAR_COLOR:
  333. return (value1.GetColor() - value2.GetColor()) * t;
  334. }
  335. LOGERROR("Invalid value type for spline interpolation");
  336. return Variant::EMPTY;
  337. }
  338. }