CCAnimation3D.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. /****************************************************************************
  2. Copyright (c) 2014-2017 Chukong Technologies Inc.
  3. http://www.cocos2d-x.org
  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. The above copyright notice and this permission notice shall be included in
  11. all copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  15. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  18. THE SOFTWARE.
  19. ****************************************************************************/
  20. #include "3d/CCAnimation3D.h"
  21. #include "3d/CCBundle3D.h"
  22. #include "platform/CCFileUtils.h"
  23. NS_CC_BEGIN
  24. Animation3D* Animation3D::create(const std::string& fileName, const std::string& animationName)
  25. {
  26. std::string fullPath = FileUtils::getInstance()->fullPathForFilename(fileName);
  27. std::string key = fullPath + "#" + animationName;
  28. auto animation = Animation3DCache::getInstance()->getAnimation(key);
  29. if (animation != nullptr)
  30. return animation;
  31. animation = new (std::nothrow) Animation3D();
  32. if(animation->initWithFile(fileName, animationName))
  33. {
  34. animation->autorelease();
  35. }
  36. else
  37. {
  38. CC_SAFE_DELETE(animation);
  39. }
  40. return animation;
  41. }
  42. bool Animation3D::initWithFile(const std::string& filename, const std::string& animationName)
  43. {
  44. std::string fullPath = FileUtils::getInstance()->fullPathForFilename(filename);
  45. //load animation here
  46. auto bundle = Bundle3D::createBundle();
  47. Animation3DData animationdata;
  48. if (bundle->load(fullPath) && bundle->loadAnimationData(animationName, &animationdata) && init(animationdata))
  49. {
  50. std::string key = fullPath + "#" + animationName;
  51. Animation3DCache::getInstance()->addAnimation(key, this);
  52. Bundle3D::destroyBundle(bundle);
  53. return true;
  54. }
  55. Bundle3D::destroyBundle(bundle);
  56. return false;
  57. }
  58. Animation3D::Curve* Animation3D::getBoneCurveByName(const std::string& name) const
  59. {
  60. auto it = _boneCurves.find(name);
  61. if (it != _boneCurves.end())
  62. return it->second;
  63. return nullptr;
  64. }
  65. Animation3D::Animation3D()
  66. : _duration(0)
  67. {
  68. }
  69. Animation3D::~Animation3D()
  70. {
  71. for (auto itor : _boneCurves) {
  72. CC_SAFE_DELETE(itor.second);
  73. }
  74. }
  75. Animation3D::Curve::Curve()
  76. : translateCurve(nullptr)
  77. , rotCurve(nullptr)
  78. , scaleCurve(nullptr)
  79. {
  80. }
  81. Animation3D::Curve::~Curve()
  82. {
  83. CC_SAFE_RELEASE_NULL(translateCurve);
  84. CC_SAFE_RELEASE_NULL(rotCurve);
  85. CC_SAFE_RELEASE_NULL(scaleCurve);
  86. }
  87. bool Animation3D::init(const Animation3DData &data)
  88. {
  89. _duration = data._totalTime;
  90. for(const auto& iter : data._translationKeys)
  91. {
  92. Curve* curve = _boneCurves[iter.first];
  93. if( curve == nullptr)
  94. {
  95. curve = new (std::nothrow) Curve();
  96. _boneCurves[iter.first] = curve;
  97. }
  98. if(iter.second.size() == 0) continue;
  99. std::vector<float> keys;
  100. std::vector<float> values;
  101. for(const auto& keyIter : iter.second)
  102. {
  103. keys.push_back(keyIter._time);
  104. values.push_back(keyIter._key.x);
  105. values.push_back(keyIter._key.y);
  106. values.push_back(keyIter._key.z);
  107. }
  108. curve->translateCurve = Curve::AnimationCurveVec3::create(&keys[0], &values[0], (int)keys.size());
  109. if(curve->translateCurve) curve->translateCurve->retain();
  110. }
  111. for(const auto& iter : data._rotationKeys)
  112. {
  113. Curve* curve = _boneCurves[iter.first];
  114. if( curve == nullptr)
  115. {
  116. curve = new (std::nothrow) Curve();
  117. _boneCurves[iter.first] = curve;
  118. }
  119. if(iter.second.size() == 0) continue;
  120. std::vector<float> keys;
  121. std::vector<float> values;
  122. for(const auto& keyIter : iter.second)
  123. {
  124. keys.push_back(keyIter._time);
  125. values.push_back(keyIter._key.x);
  126. values.push_back(keyIter._key.y);
  127. values.push_back(keyIter._key.z);
  128. values.push_back(keyIter._key.w);
  129. }
  130. curve->rotCurve = Curve::AnimationCurveQuat::create(&keys[0], &values[0], (int)keys.size());
  131. if(curve->rotCurve) curve->rotCurve->retain();
  132. }
  133. for(const auto& iter : data._scaleKeys)
  134. {
  135. Curve* curve = _boneCurves[iter.first];
  136. if( curve == nullptr)
  137. {
  138. curve = new (std::nothrow) Curve();
  139. _boneCurves[iter.first] = curve;
  140. }
  141. if(iter.second.size() == 0) continue;
  142. std::vector<float> keys;
  143. std::vector<float> values;
  144. for(const auto& keyIter : iter.second)
  145. {
  146. keys.push_back(keyIter._time);
  147. values.push_back(keyIter._key.x);
  148. values.push_back(keyIter._key.y);
  149. values.push_back(keyIter._key.z);
  150. }
  151. curve->scaleCurve = Curve::AnimationCurveVec3::create(&keys[0], &values[0], (int)keys.size());
  152. if(curve->scaleCurve) curve->scaleCurve->retain();
  153. }
  154. return true;
  155. }
  156. ////////////////////////////////////////////////////////////////
  157. Animation3DCache* Animation3DCache::_cacheInstance = nullptr;
  158. Animation3DCache* Animation3DCache::getInstance()
  159. {
  160. if (_cacheInstance == nullptr)
  161. _cacheInstance = new (std::nothrow) Animation3DCache();
  162. return _cacheInstance;
  163. }
  164. void Animation3DCache::destroyInstance()
  165. {
  166. CC_SAFE_DELETE(_cacheInstance);
  167. }
  168. Animation3D* Animation3DCache::getAnimation(const std::string& key)
  169. {
  170. auto it = _animations.find(key);
  171. if (it != _animations.end())
  172. return it->second;
  173. return nullptr;
  174. }
  175. void Animation3DCache::addAnimation(const std::string& key, Animation3D* animation)
  176. {
  177. const auto& it = _animations.find(key);
  178. if (it != _animations.end())
  179. {
  180. return; // already have this key
  181. }
  182. _animations[key] = animation;
  183. animation->retain();
  184. }
  185. void Animation3DCache::removeAllAnimations()
  186. {
  187. for (auto itor : _animations) {
  188. CC_SAFE_RELEASE(itor.second);
  189. }
  190. _animations.clear();
  191. }
  192. void Animation3DCache::removeUnusedAnimation()
  193. {
  194. for (auto itor = _animations.begin(); itor != _animations.end(); ) {
  195. if (itor->second->getReferenceCount() == 1)
  196. {
  197. itor->second->release();
  198. itor = _animations.erase(itor);
  199. }
  200. else
  201. ++itor;
  202. }
  203. }
  204. Animation3DCache::Animation3DCache()
  205. {
  206. }
  207. Animation3DCache::~Animation3DCache()
  208. {
  209. removeAllAnimations();
  210. }
  211. NS_CC_END