ValueAnimation.cpp 14 KB

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