AnimationResource.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. // Copyright (C) 2009-2021, Panagiotis Christopoulos Charitos and contributors.
  2. // All rights reserved.
  3. // Code licensed under the BSD License.
  4. // http://www.anki3d.org/LICENSE
  5. #include <AnKi/Resource/AnimationResource.h>
  6. #include <AnKi/Util/Xml.h>
  7. namespace anki {
  8. AnimationResource::AnimationResource(ResourceManager* manager)
  9. : ResourceObject(manager)
  10. {
  11. }
  12. AnimationResource::~AnimationResource()
  13. {
  14. for(AnimationChannel& ch : m_channels)
  15. {
  16. ch.destroy(getAllocator());
  17. }
  18. m_channels.destroy(getAllocator());
  19. }
  20. Error AnimationResource::load(const ResourceFilename& filename, Bool async)
  21. {
  22. XmlElement el;
  23. m_startTime = MAX_SECOND;
  24. Second maxTime = MIN_SECOND;
  25. // Document
  26. XmlDocument doc;
  27. ANKI_CHECK(openFileParseXml(filename, doc));
  28. XmlElement rootel;
  29. ANKI_CHECK(doc.getChildElement("animation", rootel));
  30. // Count the number of identity keys. If all of the keys are identities drop a vector
  31. U identPosCount = 0;
  32. U identRotCount = 0;
  33. U identScaleCount = 0;
  34. // <channels>
  35. XmlElement channelsEl;
  36. ANKI_CHECK(rootel.getChildElement("channels", channelsEl));
  37. XmlElement chEl;
  38. ANKI_CHECK(channelsEl.getChildElement("channel", chEl));
  39. U32 channelCount = 0;
  40. ANKI_CHECK(chEl.getSiblingElementsCount(channelCount));
  41. ++channelCount;
  42. if(channelCount == 0)
  43. {
  44. ANKI_RESOURCE_LOGE("Didn't found any channels");
  45. return Error::USER_DATA;
  46. }
  47. m_channels.create(getAllocator(), channelCount);
  48. // For all channels
  49. channelCount = 0;
  50. do
  51. {
  52. AnimationChannel& ch = m_channels[channelCount];
  53. // <name>
  54. CString strtmp;
  55. ANKI_CHECK(chEl.getAttributeText("name", strtmp));
  56. ch.m_name.create(getAllocator(), strtmp);
  57. XmlElement keysEl, keyEl;
  58. // <positionKeys>
  59. ANKI_CHECK(chEl.getChildElementOptional("positionKeys", keysEl));
  60. if(keysEl)
  61. {
  62. ANKI_CHECK(keysEl.getChildElement("key", keyEl));
  63. U32 count = 0;
  64. ANKI_CHECK(keyEl.getSiblingElementsCount(count));
  65. ++count;
  66. ch.m_positions.create(getAllocator(), count);
  67. count = 0;
  68. do
  69. {
  70. AnimationKeyframe<Vec3>& key = ch.m_positions[count++];
  71. // time
  72. ANKI_CHECK(keyEl.getAttributeNumber("time", key.m_time));
  73. m_startTime = min(m_startTime, key.m_time);
  74. maxTime = max(maxTime, key.m_time);
  75. // value
  76. ANKI_CHECK(keyEl.getNumbers(key.m_value));
  77. // Check ident
  78. if(key.m_value == Vec3(0.0))
  79. {
  80. ++identPosCount;
  81. }
  82. // Move to next
  83. ANKI_CHECK(keyEl.getNextSiblingElement("key", keyEl));
  84. } while(keyEl);
  85. }
  86. // <rotationKeys>
  87. ANKI_CHECK(chEl.getChildElementOptional("rotationKeys", keysEl));
  88. if(keysEl)
  89. {
  90. ANKI_CHECK(keysEl.getChildElement("key", keyEl));
  91. U32 count = 0;
  92. ANKI_CHECK(keyEl.getSiblingElementsCount(count));
  93. ++count;
  94. ch.m_rotations.create(getAllocator(), count);
  95. count = 0;
  96. do
  97. {
  98. AnimationKeyframe<Quat>& key = ch.m_rotations[count++];
  99. // time
  100. ANKI_CHECK(keyEl.getAttributeNumber("time", key.m_time));
  101. m_startTime = min(m_startTime, key.m_time);
  102. maxTime = max(maxTime, key.m_time);
  103. // value
  104. ANKI_CHECK(keyEl.getNumbers(key.m_value));
  105. // Check ident
  106. if(key.m_value == Quat::getIdentity())
  107. {
  108. ++identRotCount;
  109. }
  110. // Move to next
  111. ANKI_CHECK(keyEl.getNextSiblingElement("key", keyEl));
  112. } while(keyEl);
  113. }
  114. // <scalingKeys>
  115. ANKI_CHECK(chEl.getChildElementOptional("scalingKeys", keysEl));
  116. if(keysEl)
  117. {
  118. ANKI_CHECK(keysEl.getChildElement("key", keyEl));
  119. U32 count = 0;
  120. ANKI_CHECK(keyEl.getSiblingElementsCount(count));
  121. ++count;
  122. ch.m_scales.create(getAllocator(), count);
  123. count = 0;
  124. do
  125. {
  126. AnimationKeyframe<F32>& key = ch.m_scales[count++];
  127. // time
  128. ANKI_CHECK(keyEl.getAttributeNumber("time", key.m_time));
  129. m_startTime = std::min(m_startTime, key.m_time);
  130. maxTime = std::max(maxTime, key.m_time);
  131. // value
  132. ANKI_CHECK(keyEl.getChildElement("value", el));
  133. ANKI_CHECK(keyEl.getNumber(key.m_value));
  134. // Check ident
  135. if(isZero(key.m_value - 1.0f))
  136. {
  137. ++identScaleCount;
  138. }
  139. // Move to next
  140. ANKI_CHECK(keyEl.getNextSiblingElement("key", keyEl));
  141. } while(keyEl);
  142. }
  143. // Remove identity vectors
  144. if(identPosCount == ch.m_positions.getSize())
  145. {
  146. ch.m_positions.destroy(getAllocator());
  147. }
  148. if(identRotCount == ch.m_rotations.getSize())
  149. {
  150. ch.m_rotations.destroy(getAllocator());
  151. }
  152. if(identScaleCount == ch.m_scales.getSize())
  153. {
  154. ch.m_scales.destroy(getAllocator());
  155. }
  156. // Move to next channel
  157. ++channelCount;
  158. ANKI_CHECK(chEl.getNextSiblingElement("channel", chEl));
  159. } while(chEl);
  160. m_duration = maxTime - m_startTime;
  161. return Error::NONE;
  162. }
  163. void AnimationResource::interpolate(U32 channelIndex, Second time, Vec3& pos, Quat& rot, F32& scale) const
  164. {
  165. pos = Vec3(0.0f);
  166. rot = Quat::getIdentity();
  167. scale = 1.0f;
  168. if(ANKI_UNLIKELY(time < m_startTime))
  169. {
  170. return;
  171. }
  172. // Audjust time
  173. if(time > m_startTime + m_duration)
  174. {
  175. time = mod(time - m_startTime, m_duration) + m_startTime;
  176. }
  177. ANKI_ASSERT(time >= m_startTime && time <= m_startTime + m_duration);
  178. ANKI_ASSERT(channelIndex < m_channels.getSize());
  179. const AnimationChannel& channel = m_channels[channelIndex];
  180. // Position
  181. if(channel.m_positions.getSize() > 1)
  182. {
  183. for(U32 i = 0; i < channel.m_positions.getSize() - 1; ++i)
  184. {
  185. const AnimationKeyframe<Vec3>& left = channel.m_positions[i];
  186. const AnimationKeyframe<Vec3>& right = channel.m_positions[i + 1];
  187. if(time >= left.m_time && time <= right.m_time)
  188. {
  189. const Second u = (time - left.m_time) / (right.m_time - left.m_time);
  190. pos = linearInterpolate(left.m_value, right.m_value, F32(u));
  191. break;
  192. }
  193. }
  194. }
  195. // Rotation
  196. if(channel.m_rotations.getSize() > 1)
  197. {
  198. for(U32 i = 0; i < channel.m_rotations.getSize() - 1; ++i)
  199. {
  200. const AnimationKeyframe<Quat>& left = channel.m_rotations[i];
  201. const AnimationKeyframe<Quat>& right = channel.m_rotations[i + 1];
  202. if(time >= left.m_time && time <= right.m_time)
  203. {
  204. const Second u = (time - left.m_time) / (right.m_time - left.m_time);
  205. rot = left.m_value.slerp(right.m_value, F32(u));
  206. break;
  207. }
  208. }
  209. }
  210. // Scale
  211. if(channel.m_scales.getSize() > 1)
  212. {
  213. for(U32 i = 0; i < channel.m_scales.getSize() - 1; ++i)
  214. {
  215. const AnimationKeyframe<F32>& left = channel.m_scales[i];
  216. const AnimationKeyframe<F32>& right = channel.m_scales[i + 1];
  217. if(time >= left.m_time && time <= right.m_time)
  218. {
  219. const Second u = (time - left.m_time) / (right.m_time - left.m_time);
  220. scale = linearInterpolate(left.m_value, right.m_value, F32(u));
  221. break;
  222. }
  223. }
  224. }
  225. }
  226. } // end namespace anki