ValueAnimation.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  1. //
  2. // Copyright (c) 2008-2015 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 "../Core/Context.h"
  24. #include "../IO/Deserializer.h"
  25. #include "../IO/Log.h"
  26. #include "../IO/Serializer.h"
  27. #include "../Resource/XMLFile.h"
  28. #include "../Scene/Animatable.h"
  29. #include "../Scene/ObjectAnimation.h"
  30. #include "../Scene/ValueAnimation.h"
  31. #include "../DebugNew.h"
  32. namespace Atomic
  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::BeginLoad(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_ =
  138. (valueType_ == VAR_FLOAT) || (valueType_ == VAR_VECTOR2) || (valueType_ == VAR_VECTOR3) || (valueType_ == VAR_VECTOR4) ||
  139. (valueType_ == VAR_QUATERNION) || (valueType_ == VAR_COLOR);
  140. if ((valueType_ == VAR_INTRECT) || (valueType_ == VAR_INTVECTOR2))
  141. {
  142. interpolatable_ = true;
  143. // Force linear interpolation for IntRect and IntVector2
  144. interpolationMethod_ = IM_LINEAR;
  145. }
  146. keyFrames_.Clear();
  147. eventFrames_.Clear();
  148. beginTime_ = M_INFINITY;
  149. endTime_ = -M_INFINITY;
  150. }
  151. void ValueAnimation::SetOwner(void* owner)
  152. {
  153. owner_ = owner;
  154. }
  155. void ValueAnimation::SetInterpolationMethod(InterpMethod method)
  156. {
  157. if (method == interpolationMethod_)
  158. return;
  159. // Force linear interpolation for IntRect and IntVector2
  160. if ((valueType_ == VAR_INTRECT) || (valueType_ == VAR_INTVECTOR2))
  161. method = IM_LINEAR;
  162. interpolationMethod_ = method;
  163. splineTangentsDirty_ = true;
  164. }
  165. void ValueAnimation::SetSplineTension(float tension)
  166. {
  167. splineTension_ = tension;
  168. splineTangentsDirty_ = true;
  169. }
  170. bool ValueAnimation::SetKeyFrame(float time, const Variant& value)
  171. {
  172. if (valueType_ == VAR_NONE)
  173. SetValueType(value.GetType());
  174. else if (value.GetType() != valueType_)
  175. return false;
  176. VAnimKeyFrame keyFrame;
  177. keyFrame.time_ = time;
  178. keyFrame.value_ = value;
  179. if (keyFrames_.Empty() || time > keyFrames_.Back().time_)
  180. keyFrames_.Push(keyFrame);
  181. else
  182. {
  183. for (unsigned i = 0; i < keyFrames_.Size(); ++i)
  184. {
  185. // Guard against interpolation error caused by division by error due to 0 delta time between two key frames
  186. if (time == keyFrames_[i].time_)
  187. return false;
  188. if (time < keyFrames_[i].time_)
  189. {
  190. keyFrames_.Insert(i, keyFrame);
  191. break;
  192. }
  193. }
  194. }
  195. beginTime_ = Min(time, beginTime_);
  196. endTime_ = Max(time, endTime_);
  197. splineTangentsDirty_ = true;
  198. return true;
  199. }
  200. void ValueAnimation::SetEventFrame(float time, const StringHash& eventType, const VariantMap& eventData)
  201. {
  202. VAnimEventFrame eventFrame;
  203. eventFrame.time_ = time;
  204. eventFrame.eventType_ = eventType;
  205. eventFrame.eventData_ = eventData;
  206. if (eventFrames_.Empty() || time >= eventFrames_.Back().time_)
  207. eventFrames_.Push(eventFrame);
  208. else
  209. {
  210. for (unsigned i = 0; i < eventFrames_.Size(); ++i)
  211. {
  212. if (time < eventFrames_[i].time_)
  213. {
  214. eventFrames_.Insert(i, eventFrame);
  215. break;
  216. }
  217. }
  218. }
  219. }
  220. bool ValueAnimation::IsValid() const
  221. {
  222. return (interpolationMethod_ == IM_LINEAR && keyFrames_.Size() > 1) ||
  223. (interpolationMethod_ == IM_SPLINE && keyFrames_.Size() > 2);
  224. }
  225. Variant ValueAnimation::GetAnimationValue(float scaledTime)
  226. {
  227. unsigned index = 1;
  228. for (; index < keyFrames_.Size(); ++index)
  229. {
  230. if (scaledTime < keyFrames_[index].time_)
  231. break;
  232. }
  233. if (index >= keyFrames_.Size() || !interpolatable_)
  234. return keyFrames_[index - 1].value_;
  235. else
  236. {
  237. if (interpolationMethod_ == IM_LINEAR)
  238. return LinearInterpolation(index - 1, index, scaledTime);
  239. else
  240. return SplineInterpolation(index - 1, index, scaledTime);
  241. }
  242. }
  243. void ValueAnimation::GetEventFrames(float beginTime, float endTime, PODVector<const VAnimEventFrame*>& eventFrames) const
  244. {
  245. for (unsigned i = 0; i < eventFrames_.Size(); ++i)
  246. {
  247. const VAnimEventFrame& eventFrame = eventFrames_[i];
  248. if (eventFrame.time_ > endTime)
  249. break;
  250. if (eventFrame.time_ >= beginTime)
  251. eventFrames.Push(&eventFrame);
  252. }
  253. }
  254. Variant ValueAnimation::LinearInterpolation(unsigned index1, unsigned index2, float scaledTime) const
  255. {
  256. const VAnimKeyFrame& keyFrame1 = keyFrames_[index1];
  257. const VAnimKeyFrame& keyFrame2 = keyFrames_[index2];
  258. float t = (scaledTime - keyFrame1.time_) / (keyFrame2.time_ - keyFrame1.time_);
  259. const Variant& value1 = keyFrame1.value_;
  260. const Variant& value2 = keyFrame2.value_;
  261. switch (valueType_)
  262. {
  263. case VAR_FLOAT:
  264. return Lerp(value1.GetFloat(), value2.GetFloat(), t);
  265. case VAR_VECTOR2:
  266. return value1.GetVector2().Lerp(value2.GetVector2(), t);
  267. case VAR_VECTOR3:
  268. return value1.GetVector3().Lerp(value2.GetVector3(), t);
  269. case VAR_VECTOR4:
  270. return value1.GetVector4().Lerp(value2.GetVector4(), t);
  271. case VAR_QUATERNION:
  272. return value1.GetQuaternion().Slerp(value2.GetQuaternion(), t);
  273. case VAR_COLOR:
  274. return value1.GetColor().Lerp(value2.GetColor(), t);
  275. case VAR_INTRECT:
  276. {
  277. float s = 1.0f - t;
  278. const IntRect& r1 = value1.GetIntRect();
  279. const IntRect& r2 = value2.GetIntRect();
  280. return IntRect((int)(r1.left_ * s + r2.left_ * t), (int)(r1.top_ * s + r2.top_ * t), (int)(r1.right_ * s + r2.right_ * t),
  281. (int)(r1.bottom_ * s + r2.bottom_ * t));
  282. }
  283. case VAR_INTVECTOR2:
  284. {
  285. float s = 1.0f - t;
  286. const IntVector2& v1 = value1.GetIntVector2();
  287. const IntVector2& v2 = value2.GetIntVector2();
  288. return IntVector2((int)(v1.x_ * s + v2.x_ * t), (int)(v1.y_ * s + v2.y_ * t));
  289. }
  290. case VAR_DOUBLE:
  291. return value1.GetDouble() * (1.0f - t) + value2.GetDouble() * t;
  292. default:
  293. LOGERROR("Invalid value type for linear interpolation");
  294. return Variant::EMPTY;
  295. }
  296. }
  297. Variant ValueAnimation::SplineInterpolation(unsigned index1, unsigned index2, float scaledTime)
  298. {
  299. if (splineTangentsDirty_)
  300. UpdateSplineTangents();
  301. const VAnimKeyFrame& keyFrame1 = keyFrames_[index1];
  302. const VAnimKeyFrame& keyFrame2 = keyFrames_[index2];
  303. float t = (scaledTime - keyFrame1.time_) / (keyFrame2.time_ - keyFrame1.time_);
  304. float tt = t * t;
  305. float ttt = t * tt;
  306. float h1 = 2.0f * ttt - 3.0f * tt + 1.0f;
  307. float h2 = -2.0f * ttt + 3.0f * tt;
  308. float h3 = ttt - 2.0f * tt + t;
  309. float h4 = ttt - tt;
  310. const Variant& v1 = keyFrame1.value_;
  311. const Variant& v2 = keyFrame2.value_;
  312. const Variant& t1 = splineTangents_[index1];
  313. const Variant& t2 = splineTangents_[index2];
  314. switch (valueType_)
  315. {
  316. case VAR_FLOAT:
  317. return v1.GetFloat() * h1 + v2.GetFloat() * h2 + t1.GetFloat() * h3 + t2.GetFloat() * h4;
  318. case VAR_VECTOR2:
  319. return v1.GetVector2() * h1 + v2.GetVector2() * h2 + t1.GetVector2() * h3 + t2.GetVector2() * h4;
  320. case VAR_VECTOR3:
  321. return v1.GetVector3() * h1 + v2.GetVector3() * h2 + t1.GetVector3() * h3 + t2.GetVector3() * h4;
  322. case VAR_VECTOR4:
  323. return v1.GetVector4() * h1 + v2.GetVector4() * h2 + t1.GetVector4() * h3 + t2.GetVector4() * h4;
  324. case VAR_QUATERNION:
  325. return v1.GetQuaternion() * h1 + v2.GetQuaternion() * h2 + t1.GetQuaternion() * h3 + t2.GetQuaternion() * h4;
  326. case VAR_COLOR:
  327. return v1.GetColor() * h1 + v2.GetColor() * h2 + t1.GetColor() * h3 + t2.GetColor() * h4;
  328. case VAR_DOUBLE:
  329. return v1.GetDouble() * h1 + v2.GetDouble() * h2 + t1.GetDouble() * h3 + t2.GetDouble() * h4;
  330. default:
  331. LOGERROR("Invalid value type for spline interpolation");
  332. return Variant::EMPTY;
  333. }
  334. }
  335. void ValueAnimation::UpdateSplineTangents()
  336. {
  337. splineTangents_.Clear();
  338. if (!IsValid())
  339. return;
  340. unsigned size = keyFrames_.Size();
  341. splineTangents_.Resize(size);
  342. for (unsigned i = 1; i < size - 1; ++i)
  343. splineTangents_[i] = SubstractAndMultiply(keyFrames_[i + 1].value_, keyFrames_[i - 1].value_, splineTension_);
  344. // If spline is not closed, make end point's tangent zero
  345. if (keyFrames_[0].value_ != keyFrames_[size - 1].value_)
  346. splineTangents_[0] = splineTangents_[size - 1] =
  347. SubstractAndMultiply(keyFrames_[0].value_, keyFrames_[0].value_, splineTension_);
  348. else
  349. splineTangents_[0] = splineTangents_[size - 1] =
  350. SubstractAndMultiply(keyFrames_[1].value_, keyFrames_[size - 2].value_, splineTension_);
  351. splineTangentsDirty_ = false;
  352. }
  353. Variant ValueAnimation::SubstractAndMultiply(const Variant& value1, const Variant& value2, float t) const
  354. {
  355. switch (valueType_)
  356. {
  357. case VAR_FLOAT:
  358. return (value1.GetFloat() - value2.GetFloat()) * t;
  359. case VAR_VECTOR2:
  360. return (value1.GetVector2() - value2.GetVector2()) * t;
  361. case VAR_VECTOR3:
  362. return (value1.GetVector3() - value2.GetVector3()) * t;
  363. case VAR_VECTOR4:
  364. return (value1.GetVector4() - value2.GetVector4()) * t;
  365. case VAR_QUATERNION:
  366. return (value1.GetQuaternion() - value2.GetQuaternion()) * t;
  367. case VAR_COLOR:
  368. return (value1.GetColor() - value2.GetColor()) * t;
  369. case VAR_DOUBLE:
  370. return (value1.GetDouble() - value2.GetDouble()) * t;
  371. default:
  372. LOGERROR("Invalid value type for spline interpolation's substract and multiply operation");
  373. return Variant::EMPTY;
  374. }
  375. }
  376. }