AttributeAnimation.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  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. isInterpolatable_(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. InterpolationMethod method = IM_LINEAR;
  79. for (int i = 0; i <= IM_SPLINE; ++i)
  80. {
  81. if (interpMethodString == interpMethodNames[i])
  82. {
  83. method = (InterpolationMethod)i;
  84. break;
  85. }
  86. }
  87. SetInterpolationMethod(method);
  88. if (interpolationMethod_ == IM_SPLINE)
  89. splineTension_ = source.GetFloat("splinetension");
  90. XMLElement keyFrameEleme = source.GetChild("keyframe");
  91. while (keyFrameEleme)
  92. {
  93. float time = keyFrameEleme.GetFloat("time");
  94. Variant value = keyFrameEleme.GetVariant();
  95. SetKeyFrame(time, value);
  96. keyFrameEleme = keyFrameEleme.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::SetObjectAnimation(ObjectAnimation* objectAnimation)
  132. {
  133. objectAnimation_ = objectAnimation;
  134. }
  135. void AttributeAnimation::SetValueType(VariantType valueType)
  136. {
  137. if (valueType == valueType_)
  138. return;
  139. valueType_ = valueType;
  140. isInterpolatable_ = (valueType_ == VAR_FLOAT) || (valueType_ == VAR_VECTOR2) || (valueType_ == VAR_VECTOR3) || (valueType_ == VAR_VECTOR4) ||
  141. (valueType_ == VAR_QUATERNION) || (valueType_ == VAR_COLOR);
  142. if ((valueType_ == VAR_INTRECT) || (valueType_ == VAR_INTVECTOR2))
  143. {
  144. isInterpolatable_ = true;
  145. // Force linear interpolation for IntRect and IntVector2
  146. interpolationMethod_ = IM_LINEAR;
  147. }
  148. keyFrames_.Clear();
  149. beginTime_ = M_INFINITY;
  150. endTime_ = -M_INFINITY;
  151. }
  152. void AttributeAnimation::SetInterpolationMethod(InterpolationMethod method)
  153. {
  154. if (method == interpolationMethod_)
  155. return;
  156. // Force linear interpolation for IntRect and IntVector2
  157. if ((valueType_ == VAR_INTRECT) || (valueType_ == VAR_INTVECTOR2))
  158. method = IM_LINEAR;
  159. interpolationMethod_ = method;
  160. splineTangentsDirty_ = true;
  161. }
  162. void AttributeAnimation::SetSplineTension(float tension)
  163. {
  164. splineTension_ = tension;
  165. splineTangentsDirty_ = true;
  166. }
  167. bool AttributeAnimation::SetKeyFrame(float time, const Variant& value)
  168. {
  169. if (valueType_ == VAR_NONE)
  170. SetValueType(value.GetType());
  171. else if (value.GetType() != valueType_)
  172. return false;
  173. beginTime_ = Min(time, beginTime_);
  174. endTime_ = Max(time, endTime_);
  175. AttributeKeyFrame 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. if (time < keyFrames_[i].time_)
  185. {
  186. keyFrames_.Insert(i, keyFrame);
  187. break;
  188. }
  189. }
  190. }
  191. splineTangentsDirty_ = true;
  192. return true;
  193. }
  194. void AttributeAnimation::SetEventFrame(float time, const StringHash& eventType, const VariantMap& eventData)
  195. {
  196. AttributeEventFrame eventFrame;
  197. eventFrame.time_ = time;
  198. eventFrame.eventType_ = eventType;
  199. eventFrame.eventData_ = eventData;
  200. if (eventFrames_.Empty() || time >= eventFrames_.Back().time_)
  201. eventFrames_.Push(eventFrame);
  202. else
  203. {
  204. for (unsigned i = 0; i < eventFrames_.Size(); ++i)
  205. {
  206. if (time < eventFrames_[i].time_)
  207. {
  208. eventFrames_.Insert(i, eventFrame);
  209. break;
  210. }
  211. }
  212. }
  213. }
  214. bool AttributeAnimation::IsValid() const
  215. {
  216. return (interpolationMethod_ == IM_LINEAR && keyFrames_.Size() > 1) || (interpolationMethod_ == IM_SPLINE && keyFrames_.Size() > 2);
  217. }
  218. ObjectAnimation* AttributeAnimation::GetObjectAnimation() const
  219. {
  220. return objectAnimation_;
  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() || !isInterpolatable_)
  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, Vector<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. }
  287. LOGERROR("Invalid value type for linear interpolation");
  288. return Variant::EMPTY;
  289. }
  290. Variant AttributeAnimation::SplineInterpolation(unsigned index1, unsigned index2, float scaledTime)
  291. {
  292. if (splineTangentsDirty_)
  293. UpdateSplineTangents();
  294. const AttributeKeyFrame& keyFrame1 = keyFrames_[index1];
  295. const AttributeKeyFrame& keyFrame2 = keyFrames_[index2];
  296. float t = (scaledTime - keyFrame1.time_) / (keyFrame2.time_ - keyFrame1.time_);
  297. float tt = t * t;
  298. float ttt = t * tt;
  299. float h1 = 2.0f * ttt - 3.0f * tt + 1.0f;
  300. float h2 = -2.0f * ttt + 3.0f * tt;
  301. float h3 = ttt - 2.0f * tt + t;
  302. float h4 = ttt - tt;
  303. const Variant& v1 = keyFrame1.value_;
  304. const Variant& v2 = keyFrame2.value_;
  305. const Variant& t1 = splineTangents_[index1];
  306. const Variant& t2 = splineTangents_[index2];
  307. switch (valueType_)
  308. {
  309. case VAR_FLOAT:
  310. return v1.GetFloat() * h1 + v2.GetFloat() * h2 + t1.GetFloat() * h3 + t2.GetFloat() * h4;
  311. case VAR_VECTOR2:
  312. return v1.GetVector2() * h1 + v2.GetVector2() * h2 + t1.GetVector2() * h3 + t2.GetVector2() * h4;
  313. case VAR_VECTOR3:
  314. return v1.GetVector3() * h1 + v2.GetVector3() * h2 + t1.GetVector3() * h3 + t2.GetVector3() * h4;
  315. case VAR_VECTOR4:
  316. return v1.GetVector4() * h1 + v2.GetVector4() * h2 + t1.GetVector4() * h3 + t2.GetVector4() * h4;
  317. case VAR_QUATERNION:
  318. return v1.GetQuaternion() * h1 + v2.GetQuaternion() * h2 + t1.GetQuaternion() * h3 + t2.GetQuaternion() * h4;
  319. case VAR_COLOR:
  320. return v1.GetColor() * h1 + v2.GetColor() * h2 + t1.GetColor() * h3 + t2.GetColor() * h4;
  321. }
  322. LOGERROR("Invalid value type for spline interpolation");
  323. return Variant::EMPTY;
  324. }
  325. void AttributeAnimation::UpdateSplineTangents()
  326. {
  327. splineTangents_.Clear();
  328. if (!IsValid())
  329. return;
  330. unsigned size = keyFrames_.Size();
  331. splineTangents_.Resize(size);
  332. for (unsigned i = 1; i < size - 1; ++i)
  333. splineTangents_[i] = SubstractAndMultiply(keyFrames_[i + 1].value_, keyFrames_[i - 1].value_, splineTension_);
  334. // Make end point's tangent zero
  335. splineTangents_[0] = splineTangents_[size - 1] = SubstractAndMultiply(keyFrames_[0].value_, keyFrames_[0].value_, splineTension_);
  336. splineTangentsDirty_ = false;
  337. }
  338. Variant AttributeAnimation::SubstractAndMultiply(const Variant& value1, const Variant& value2, float t) const
  339. {
  340. switch (valueType_)
  341. {
  342. case VAR_FLOAT:
  343. return (value1.GetFloat() - value2.GetFloat()) * t;
  344. case VAR_VECTOR2:
  345. return (value1.GetVector2() - value2.GetVector2()) * t;
  346. case VAR_VECTOR3:
  347. return (value1.GetVector3() - value2.GetVector3()) * t;
  348. case VAR_VECTOR4:
  349. return (value1.GetVector4() - value2.GetVector4()) * t;
  350. case VAR_QUATERNION:
  351. return (value1.GetQuaternion() - value2.GetQuaternion()) * t;
  352. case VAR_COLOR:
  353. return (value1.GetColor() - value2.GetColor()) * t;
  354. }
  355. LOGERROR("Invalid value type for spline interpolation");
  356. return Variant::EMPTY;
  357. }
  358. }