AttributeAnimation.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  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. const char* interpMethodNames[] =
  35. {
  36. "Linear",
  37. "Spline",
  38. 0
  39. };
  40. AttributeAnimation::AttributeAnimation(Context* context) :
  41. Resource(context),
  42. interpolationMethod_(IM_LINEAR),
  43. splineTension_(0.5f),
  44. valueType_(VAR_NONE),
  45. interpolatable_(false),
  46. beginTime_(M_INFINITY),
  47. endTime_(-M_INFINITY),
  48. splineTangentsDirty_(false)
  49. {
  50. }
  51. AttributeAnimation::~AttributeAnimation()
  52. {
  53. }
  54. void AttributeAnimation::RegisterObject(Context* context)
  55. {
  56. context->RegisterFactory<AttributeAnimation>();
  57. }
  58. bool AttributeAnimation::Load(Deserializer& source)
  59. {
  60. XMLFile xmlFile(context_);
  61. if (!xmlFile.Load(source))
  62. return false;
  63. return LoadXML(xmlFile.GetRoot());
  64. }
  65. bool AttributeAnimation::Save(Serializer& dest) const
  66. {
  67. XMLFile xmlFile(context_);
  68. XMLElement rootElem = xmlFile.CreateRoot("attributeanimation");
  69. if (!SaveXML(rootElem))
  70. return false;
  71. return xmlFile.Save(dest);
  72. }
  73. bool AttributeAnimation::LoadXML(const XMLElement& source)
  74. {
  75. valueType_ = VAR_NONE;
  76. eventFrames_.Clear();
  77. String interpMethodString = source.GetAttribute("interpolationmethod");
  78. InterpMethod method = IM_LINEAR;
  79. for (int i = 0; i <= IM_SPLINE; ++i)
  80. {
  81. if (interpMethodString == interpMethodNames[i])
  82. {
  83. method = (InterpMethod)i;
  84. break;
  85. }
  86. }
  87. SetInterpolationMethod(method);
  88. if (interpolationMethod_ == IM_SPLINE)
  89. splineTension_ = source.GetFloat("splinetension");
  90. XMLElement keyFrameElem = source.GetChild("keyframe");
  91. while (keyFrameElem)
  92. {
  93. float time = keyFrameElem.GetFloat("time");
  94. Variant value = keyFrameElem.GetVariant();
  95. SetKeyFrame(time, value);
  96. keyFrameElem = keyFrameElem.GetNext("keyframe");
  97. }
  98. XMLElement eventFrameElem = source.GetChild("eventframe");
  99. while (eventFrameElem)
  100. {
  101. float time = eventFrameElem.GetFloat("time");
  102. unsigned eventType = eventFrameElem.GetUInt("eventtype");
  103. VariantMap eventData = eventFrameElem.GetChild("eventdata").GetVariantMap();
  104. SetEventFrame(time, StringHash(eventType), eventData);
  105. eventFrameElem = eventFrameElem.GetNext("eventframe");
  106. }
  107. return true;
  108. }
  109. bool AttributeAnimation::SaveXML(XMLElement& dest) const
  110. {
  111. dest.SetAttribute("interpolationmethod", interpMethodNames[interpolationMethod_]);
  112. if (interpolationMethod_ == IM_SPLINE)
  113. dest.SetFloat("splinetension", splineTension_);
  114. for (unsigned i = 0; i < keyFrames_.Size(); ++i)
  115. {
  116. const AttributeKeyFrame& keyFrame = keyFrames_[i];
  117. XMLElement keyFrameEleme = dest.CreateChild("keyframe");
  118. keyFrameEleme.SetFloat("time", keyFrame.time_);
  119. keyFrameEleme.SetVariant(keyFrame.value_);
  120. }
  121. for (unsigned i = 0; i < eventFrames_.Size(); ++i)
  122. {
  123. const AttributeEventFrame& eventFrame = eventFrames_[i];
  124. XMLElement eventFrameElem = dest.CreateChild("eventframe");
  125. eventFrameElem.SetFloat("time", eventFrame.time_);
  126. eventFrameElem.SetUInt("eventtype", eventFrame.eventType_.Value());
  127. eventFrameElem.CreateChild("eventdata").SetVariantMap(eventFrame.eventData_);
  128. }
  129. return true;
  130. }
  131. void AttributeAnimation::SetValueType(VariantType valueType)
  132. {
  133. if (valueType == valueType_)
  134. return;
  135. valueType_ = valueType;
  136. interpolatable_ = (valueType_ == VAR_FLOAT) || (valueType_ == VAR_VECTOR2) || (valueType_ == VAR_VECTOR3) || (valueType_ == VAR_VECTOR4) ||
  137. (valueType_ == VAR_QUATERNION) || (valueType_ == VAR_COLOR);
  138. if ((valueType_ == VAR_INTRECT) || (valueType_ == VAR_INTVECTOR2))
  139. {
  140. interpolatable_ = true;
  141. // Force linear interpolation for IntRect and IntVector2
  142. interpolationMethod_ = IM_LINEAR;
  143. }
  144. keyFrames_.Clear();
  145. eventFrames_.Clear();
  146. beginTime_ = M_INFINITY;
  147. endTime_ = -M_INFINITY;
  148. }
  149. void AttributeAnimation::SetObjectAnimation(ObjectAnimation* objectAnimation)
  150. {
  151. objectAnimation_ = objectAnimation;
  152. }
  153. void AttributeAnimation::SetInterpolationMethod(InterpMethod method)
  154. {
  155. if (method == interpolationMethod_)
  156. return;
  157. // Force linear interpolation for IntRect and IntVector2
  158. if ((valueType_ == VAR_INTRECT) || (valueType_ == VAR_INTVECTOR2))
  159. method = IM_LINEAR;
  160. interpolationMethod_ = method;
  161. splineTangentsDirty_ = true;
  162. }
  163. void AttributeAnimation::SetSplineTension(float tension)
  164. {
  165. splineTension_ = tension;
  166. splineTangentsDirty_ = true;
  167. }
  168. bool AttributeAnimation::SetKeyFrame(float time, const Variant& value)
  169. {
  170. if (valueType_ == VAR_NONE)
  171. SetValueType(value.GetType());
  172. else if (value.GetType() != valueType_)
  173. return false;
  174. AttributeKeyFrame keyFrame;
  175. keyFrame.time_ = time;
  176. keyFrame.value_ = value;
  177. if (keyFrames_.Empty() || time > keyFrames_.Back().time_)
  178. keyFrames_.Push(keyFrame);
  179. else
  180. {
  181. for (unsigned i = 0; i < keyFrames_.Size(); ++i)
  182. {
  183. // Guard against interpolation error caused by division by error due to 0 delta time between two key frames
  184. if (time == keyFrames_[i].time_)
  185. return false;
  186. if (time < keyFrames_[i].time_)
  187. {
  188. keyFrames_.Insert(i, keyFrame);
  189. break;
  190. }
  191. }
  192. }
  193. beginTime_ = Min(time, beginTime_);
  194. endTime_ = Max(time, endTime_);
  195. splineTangentsDirty_ = true;
  196. return true;
  197. }
  198. void AttributeAnimation::SetEventFrame(float time, const StringHash& eventType, const VariantMap& eventData)
  199. {
  200. AttributeEventFrame eventFrame;
  201. eventFrame.time_ = time;
  202. eventFrame.eventType_ = eventType;
  203. eventFrame.eventData_ = eventData;
  204. if (eventFrames_.Empty() || time >= eventFrames_.Back().time_)
  205. eventFrames_.Push(eventFrame);
  206. else
  207. {
  208. for (unsigned i = 0; i < eventFrames_.Size(); ++i)
  209. {
  210. if (time < eventFrames_[i].time_)
  211. {
  212. eventFrames_.Insert(i, eventFrame);
  213. break;
  214. }
  215. }
  216. }
  217. }
  218. bool AttributeAnimation::IsValid() const
  219. {
  220. return (interpolationMethod_ == IM_LINEAR && keyFrames_.Size() > 1) || (interpolationMethod_ == IM_SPLINE && keyFrames_.Size() > 2);
  221. }
  222. void AttributeAnimation::UpdateAttributeValue(Animatable* animatable, const AttributeInfo& attributeInfo, float scaledTime)
  223. {
  224. unsigned index = 1;
  225. for (; index < keyFrames_.Size(); ++index)
  226. {
  227. if (scaledTime < keyFrames_[index].time_)
  228. break;
  229. }
  230. if (index >= keyFrames_.Size() || !interpolatable_)
  231. animatable->OnSetAttribute(attributeInfo, keyFrames_[index - 1].value_);
  232. else
  233. {
  234. if (interpolationMethod_ == IM_LINEAR)
  235. animatable->OnSetAttribute(attributeInfo, LinearInterpolation(index - 1, index, scaledTime));
  236. else
  237. animatable->OnSetAttribute(attributeInfo, SplineInterpolation(index - 1, index, scaledTime));
  238. }
  239. }
  240. void AttributeAnimation::GetEventFrames(float beginTime, float endTime, PODVector<const AttributeEventFrame*>& eventFrames) const
  241. {
  242. for (unsigned i = 0; i < eventFrames_.Size(); ++i)
  243. {
  244. const AttributeEventFrame& eventFrame = eventFrames_[i];
  245. if (eventFrame.time_ >= endTime)
  246. break;
  247. if (eventFrame.time_ >= beginTime)
  248. eventFrames.Push(&eventFrame);
  249. }
  250. }
  251. Variant AttributeAnimation::LinearInterpolation(unsigned index1, unsigned index2, float scaledTime) const
  252. {
  253. const AttributeKeyFrame& keyFrame1 = keyFrames_[index1];
  254. const AttributeKeyFrame& keyFrame2 = keyFrames_[index2];
  255. float t = (scaledTime - keyFrame1.time_) / (keyFrame2.time_ - keyFrame1.time_);
  256. const Variant& value1 = keyFrame1.value_;
  257. const Variant& value2 = keyFrame2.value_;
  258. switch (valueType_)
  259. {
  260. case VAR_FLOAT:
  261. return Lerp(value1.GetFloat(), value2.GetFloat(), t);
  262. case VAR_VECTOR2:
  263. return value1.GetVector2().Lerp(value2.GetVector2(), t);
  264. case VAR_VECTOR3:
  265. return value1.GetVector3().Lerp(value2.GetVector3(), t);
  266. case VAR_VECTOR4:
  267. return value1.GetVector4().Lerp(value2.GetVector4(), t);
  268. case VAR_QUATERNION:
  269. return value1.GetQuaternion().Slerp(value2.GetQuaternion(), t);
  270. case VAR_COLOR:
  271. return value1.GetColor().Lerp(value2.GetColor(), t);
  272. case VAR_INTRECT:
  273. {
  274. float s = 1.0f - t;
  275. const IntRect& r1 = value1.GetIntRect();
  276. const IntRect& r2 = value2.GetIntRect();
  277. 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));
  278. }
  279. case VAR_INTVECTOR2:
  280. {
  281. float s = 1.0f - t;
  282. const IntVector2& v1 = value1.GetIntVector2();
  283. const IntVector2& v2 = value2.GetIntVector2();
  284. return IntVector2((int)(v1.x_ * s + v2.x_ * t), (int)(v1.y_ * s + v2.y_ * t));
  285. }
  286. default:
  287. LOGERROR("Invalid value type for linear interpolation");
  288. return Variant::EMPTY;
  289. }
  290. }
  291. Variant AttributeAnimation::SplineInterpolation(unsigned index1, unsigned index2, float scaledTime)
  292. {
  293. if (splineTangentsDirty_)
  294. UpdateSplineTangents();
  295. const AttributeKeyFrame& keyFrame1 = keyFrames_[index1];
  296. const AttributeKeyFrame& keyFrame2 = keyFrames_[index2];
  297. float t = (scaledTime - keyFrame1.time_) / (keyFrame2.time_ - keyFrame1.time_);
  298. float tt = t * t;
  299. float ttt = t * tt;
  300. float h1 = 2.0f * ttt - 3.0f * tt + 1.0f;
  301. float h2 = -2.0f * ttt + 3.0f * tt;
  302. float h3 = ttt - 2.0f * tt + t;
  303. float h4 = ttt - tt;
  304. const Variant& v1 = keyFrame1.value_;
  305. const Variant& v2 = keyFrame2.value_;
  306. const Variant& t1 = splineTangents_[index1];
  307. const Variant& t2 = splineTangents_[index2];
  308. switch (valueType_)
  309. {
  310. case VAR_FLOAT:
  311. return v1.GetFloat() * h1 + v2.GetFloat() * h2 + t1.GetFloat() * h3 + t2.GetFloat() * h4;
  312. case VAR_VECTOR2:
  313. return v1.GetVector2() * h1 + v2.GetVector2() * h2 + t1.GetVector2() * h3 + t2.GetVector2() * h4;
  314. case VAR_VECTOR3:
  315. return v1.GetVector3() * h1 + v2.GetVector3() * h2 + t1.GetVector3() * h3 + t2.GetVector3() * h4;
  316. case VAR_VECTOR4:
  317. return v1.GetVector4() * h1 + v2.GetVector4() * h2 + t1.GetVector4() * h3 + t2.GetVector4() * h4;
  318. case VAR_QUATERNION:
  319. return v1.GetQuaternion() * h1 + v2.GetQuaternion() * h2 + t1.GetQuaternion() * h3 + t2.GetQuaternion() * h4;
  320. case VAR_COLOR:
  321. return v1.GetColor() * h1 + v2.GetColor() * h2 + t1.GetColor() * h3 + t2.GetColor() * h4;
  322. default:
  323. LOGERROR("Invalid value type for spline interpolation");
  324. return Variant::EMPTY;
  325. }
  326. }
  327. void AttributeAnimation::UpdateSplineTangents()
  328. {
  329. splineTangents_.Clear();
  330. if (!IsValid())
  331. return;
  332. unsigned size = keyFrames_.Size();
  333. splineTangents_.Resize(size);
  334. for (unsigned i = 1; i < size - 1; ++i)
  335. splineTangents_[i] = SubstractAndMultiply(keyFrames_[i + 1].value_, keyFrames_[i - 1].value_, splineTension_);
  336. // If spline is not closed, make end point's tangent zero
  337. if (keyFrames_[0].value_ != keyFrames_[size - 1].value_)
  338. splineTangents_[0] = splineTangents_[size - 1] = SubstractAndMultiply(keyFrames_[0].value_, keyFrames_[0].value_, splineTension_);
  339. else
  340. splineTangents_[0] = splineTangents_[size - 1] = SubstractAndMultiply(keyFrames_[1].value_, keyFrames_[size - 2].value_, splineTension_);
  341. splineTangentsDirty_ = false;
  342. }
  343. Variant AttributeAnimation::SubstractAndMultiply(const Variant& value1, const Variant& value2, float t) const
  344. {
  345. switch (valueType_)
  346. {
  347. case VAR_FLOAT:
  348. return (value1.GetFloat() - value2.GetFloat()) * t;
  349. case VAR_VECTOR2:
  350. return (value1.GetVector2() - value2.GetVector2()) * t;
  351. case VAR_VECTOR3:
  352. return (value1.GetVector3() - value2.GetVector3()) * t;
  353. case VAR_VECTOR4:
  354. return (value1.GetVector4() - value2.GetVector4()) * t;
  355. case VAR_QUATERNION:
  356. return (value1.GetQuaternion() - value2.GetQuaternion()) * t;
  357. case VAR_COLOR:
  358. return (value1.GetColor() - value2.GetColor()) * t;
  359. default:
  360. LOGERROR("Invalid value type for spline interpolation's substract and multiply operation");
  361. return Variant::EMPTY;
  362. }
  363. }
  364. }