1
0

CCAnimate3D.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533
  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/CCAnimate3D.h"
  21. #include "3d/CCSprite3D.h"
  22. #include "3d/CCSkeleton3D.h"
  23. #include "platform/CCFileUtils.h"
  24. #include "base/CCConfiguration.h"
  25. #include "base/CCEventCustom.h"
  26. #include "base/CCDirector.h"
  27. #include "base/CCEventDispatcher.h"
  28. NS_CC_BEGIN
  29. std::unordered_map<Node*, Animate3D*> Animate3D::s_fadeInAnimates;
  30. std::unordered_map<Node*, Animate3D*> Animate3D::s_fadeOutAnimates;
  31. std::unordered_map<Node*, Animate3D*> Animate3D::s_runningAnimates;
  32. float Animate3D::_transTime = 0.1f;
  33. //create Animate3D using Animation.
  34. Animate3D* Animate3D::create(Animation3D* animation)
  35. {
  36. auto animate = new (std::nothrow) Animate3D();
  37. animate->init(animation);
  38. animate->autorelease();
  39. return animate;
  40. }
  41. Animate3D* Animate3D::create(Animation3D* animation, float fromTime, float duration)
  42. {
  43. auto animate = new (std::nothrow) Animate3D();
  44. animate->init(animation, fromTime, duration);
  45. animate->autorelease();
  46. return animate;
  47. }
  48. Animate3D* Animate3D::createWithFrames(Animation3D* animation, int startFrame, int endFrame, float frameRate)
  49. {
  50. auto animate = new (std::nothrow) Animate3D();
  51. animate->initWithFrames(animation, startFrame, endFrame, frameRate);
  52. animate->autorelease();
  53. return animate;
  54. }
  55. bool Animate3D::init(Animation3D* animation)
  56. {
  57. _animation = animation;
  58. animation->retain();
  59. setDuration(animation->getDuration());
  60. setOriginInterval(animation->getDuration());
  61. setQuality(Configuration::getInstance()->getAnimate3DQuality());
  62. return true;
  63. }
  64. bool Animate3D::init(Animation3D* animation, float fromTime, float duration)
  65. {
  66. float fullDuration = animation->getDuration();
  67. if (duration > fullDuration - fromTime)
  68. duration = fullDuration - fromTime;
  69. _start = fromTime / fullDuration;
  70. _last = duration / fullDuration;
  71. setDuration(duration);
  72. setOriginInterval(duration);
  73. _animation = animation;
  74. animation->retain();
  75. setQuality(Configuration::getInstance()->getAnimate3DQuality());
  76. return true;
  77. }
  78. bool Animate3D::initWithFrames(Animation3D* animation, int startFrame, int endFrame, float frameRate)
  79. {
  80. float perFrameTime = 1.f / frameRate;
  81. float fromTime = startFrame * perFrameTime;
  82. float duration = (endFrame - startFrame) * perFrameTime;
  83. _frameRate = frameRate;
  84. init(animation, fromTime, duration);
  85. return true;
  86. }
  87. /** returns a clone of action */
  88. Animate3D* Animate3D::clone() const
  89. {
  90. auto animate = const_cast<Animate3D*>(this);
  91. auto copy = Animate3D::create(animate->_animation);
  92. copy->_absSpeed = _absSpeed;
  93. copy->_weight = _weight;
  94. copy->_elapsed = _elapsed;
  95. copy->_start = _start;
  96. copy->_last = _last;
  97. copy->_playReverse = _playReverse;
  98. copy->setDuration(animate->getDuration());
  99. copy->setOriginInterval(animate->getOriginInterval());
  100. return copy;
  101. }
  102. /** returns a new action that performs the exactly the reverse action */
  103. Animate3D* Animate3D::reverse() const
  104. {
  105. auto animate = clone();
  106. animate->_playReverse = !animate->_playReverse;
  107. return animate;
  108. }
  109. Node* findChildByNameRecursively(Node* node, const std::string &childName)
  110. {
  111. const std::string& name = node->getName();
  112. if (name == childName)
  113. return node;
  114. const Vector<Node*>& children = node->getChildren();
  115. for (const auto& child : children)
  116. {
  117. Node* findNode = findChildByNameRecursively(child, childName);
  118. if (findNode)
  119. return findNode;
  120. }
  121. return nullptr;
  122. }
  123. //! called before the action start. It will also set the target.
  124. void Animate3D::startWithTarget(Node *target)
  125. {
  126. bool needReMap = (_target != target);
  127. ActionInterval::startWithTarget(target);
  128. if (needReMap)
  129. {
  130. _boneCurves.clear();
  131. _nodeCurves.clear();
  132. bool hasCurve = false;
  133. Sprite3D* sprite = dynamic_cast<Sprite3D*>(target);
  134. if(sprite)
  135. {
  136. if (_animation)
  137. {
  138. const std::unordered_map<std::string, Animation3D::Curve*>& boneCurves = _animation->getBoneCurves();
  139. for (const auto& iter: boneCurves)
  140. {
  141. const std::string& boneName = iter.first;
  142. auto skin = sprite->getSkeleton();
  143. if(skin)
  144. {
  145. auto bone = skin->getBoneByName(boneName);
  146. if (bone)
  147. {
  148. auto curve = _animation->getBoneCurveByName(boneName);
  149. _boneCurves[bone] = curve;
  150. hasCurve = true;
  151. }
  152. else
  153. {
  154. Node* node = nullptr;
  155. if (target->getName() == boneName)
  156. node = target;
  157. else
  158. node = findChildByNameRecursively(target, boneName);
  159. if (node)
  160. {
  161. auto curve = _animation->getBoneCurveByName(boneName);
  162. if (curve)
  163. {
  164. _nodeCurves[node] = curve;
  165. hasCurve = true;
  166. }
  167. }
  168. }
  169. }
  170. }
  171. }
  172. }
  173. else
  174. {
  175. const std::unordered_map<std::string, Animation3D::Curve*>& boneCurves = _animation->getBoneCurves();
  176. for (const auto& iter: boneCurves)
  177. {
  178. const std::string& boneName = iter.first;
  179. Node* node = nullptr;
  180. if (target->getName() == boneName)
  181. node = target;
  182. else
  183. node = findChildByNameRecursively(target, boneName);
  184. if (node)
  185. {
  186. auto curve = _animation->getBoneCurveByName(boneName);
  187. if (curve)
  188. {
  189. _nodeCurves[node] = curve;
  190. hasCurve = true;
  191. }
  192. }
  193. }
  194. }
  195. if (!hasCurve)
  196. {
  197. CCLOG("warning: no animation found for the skeleton");
  198. }
  199. }
  200. auto runningAction = s_runningAnimates.find(target);
  201. if (runningAction != s_runningAnimates.end())
  202. {
  203. //make the running action fade out
  204. auto action = (*runningAction).second;
  205. if (action != this)
  206. {
  207. if (_transTime < 0.001f)
  208. {
  209. s_runningAnimates[target] = this;
  210. _state = Animate3D::Animate3DState::Running;
  211. _weight = 1.0f;
  212. }
  213. else
  214. {
  215. s_fadeOutAnimates[target] = action;
  216. action->_state = Animate3D::Animate3DState::FadeOut;
  217. action->_accTransTime = 0.0f;
  218. action->_weight = 1.0f;
  219. action->_lastTime = 0.f;
  220. s_runningAnimates.erase(target);
  221. s_fadeInAnimates[target] = this;
  222. _accTransTime = 0.0f;
  223. _state = Animate3D::Animate3DState::FadeIn;
  224. _weight = 0.f;
  225. _lastTime = 0.f;
  226. }
  227. }
  228. }
  229. else
  230. {
  231. auto it = s_fadeInAnimates.find(target);
  232. if (it != s_fadeInAnimates.end())
  233. {
  234. s_fadeInAnimates.erase(it);
  235. }
  236. s_runningAnimates[target] = this;
  237. _state = Animate3D::Animate3DState::Running;
  238. _weight = 1.0f;
  239. }
  240. }
  241. void Animate3D::stop()
  242. {
  243. removeFromMap();
  244. ActionInterval::stop();
  245. }
  246. //! called every frame with it's delta time. DON'T override unless you know what you are doing.
  247. void Animate3D::step(float dt)
  248. {
  249. ActionInterval::step(dt);
  250. }
  251. bool cmpEventInfoAsc(Animate3D::Animate3DDisplayedEventInfo* info1, Animate3D::Animate3DDisplayedEventInfo* info2)
  252. {
  253. return info1->frame < info2->frame;
  254. }
  255. bool cmpEventInfoDes(Animate3D::Animate3DDisplayedEventInfo* info1, Animate3D::Animate3DDisplayedEventInfo* info2)
  256. {
  257. return info1->frame > info2->frame;
  258. }
  259. void Animate3D::update(float t)
  260. {
  261. if (_target)
  262. {
  263. if (_state == Animate3D::Animate3DState::FadeIn && _lastTime > 0.f)
  264. {
  265. _accTransTime += (t - _lastTime) * getDuration();
  266. _weight = _accTransTime / _transTime;
  267. if (_weight >= 1.0f)
  268. {
  269. _accTransTime = _transTime;
  270. _weight = 1.0f;
  271. _state = Animate3D::Animate3DState::Running;
  272. s_fadeInAnimates.erase(_target);
  273. s_runningAnimates[_target] = this;
  274. }
  275. }
  276. else if (_state == Animate3D::Animate3DState::FadeOut && _lastTime > 0.f)
  277. {
  278. _accTransTime += (t - _lastTime) * getDuration();
  279. _weight = 1 - _accTransTime / _transTime;
  280. if (_weight <= 0.0f)
  281. {
  282. _accTransTime = _transTime;
  283. _weight = 0.0f;
  284. s_fadeOutAnimates.erase(_target);
  285. _target->stopAction(this);
  286. return;
  287. }
  288. }
  289. float lastTime = _lastTime;
  290. _lastTime = t;
  291. if (_quality != Animate3DQuality::QUALITY_NONE)
  292. {
  293. if (_weight > 0.0f)
  294. {
  295. float transDst[3], rotDst[4], scaleDst[3];
  296. float* trans = nullptr, *rot = nullptr, *scale = nullptr;
  297. if (_playReverse){
  298. t = 1 - t;
  299. lastTime = 1.0f - lastTime;
  300. }
  301. t = _start + t * _last;
  302. lastTime = _start + lastTime * _last;
  303. for (const auto& it : _boneCurves) {
  304. auto bone = it.first;
  305. auto curve = it.second;
  306. if (curve->translateCurve)
  307. {
  308. curve->translateCurve->evaluate(t, transDst, _translateEvaluate);
  309. trans = &transDst[0];
  310. }
  311. if (curve->rotCurve)
  312. {
  313. curve->rotCurve->evaluate(t, rotDst, _roteEvaluate);
  314. rot = &rotDst[0];
  315. }
  316. if (curve->scaleCurve)
  317. {
  318. curve->scaleCurve->evaluate(t, scaleDst, _scaleEvaluate);
  319. scale = &scaleDst[0];
  320. }
  321. bone->setAnimationValue(trans, rot, scale, this, _weight);
  322. }
  323. for (const auto& it : _nodeCurves)
  324. {
  325. auto node = it.first;
  326. auto curve = it.second;
  327. Mat4 transform;
  328. if (curve->translateCurve)
  329. {
  330. curve->translateCurve->evaluate(t, transDst, _translateEvaluate);
  331. transform.translate(transDst[0], transDst[1], transDst[2]);
  332. }
  333. if (curve->rotCurve)
  334. {
  335. curve->rotCurve->evaluate(t, rotDst, _roteEvaluate);
  336. Quaternion qua(rotDst[0], rotDst[1], rotDst[2], rotDst[3]);
  337. transform.rotate(qua);
  338. }
  339. if (curve->scaleCurve)
  340. {
  341. curve->scaleCurve->evaluate(t, scaleDst, _scaleEvaluate);
  342. transform.scale(scaleDst[0], scaleDst[1], scaleDst[2]);
  343. }
  344. node->setAdditionalTransform(&transform);
  345. }
  346. if (!_keyFrameUserInfos.empty()){
  347. float prekeyTime = lastTime * getDuration() * _frameRate;
  348. float keyTime = t * getDuration() * _frameRate;
  349. std::vector<Animate3DDisplayedEventInfo*> eventInfos;
  350. for (auto keyFrame : _keyFrameUserInfos)
  351. {
  352. if ((!_playReverse && keyFrame.first >= prekeyTime && keyFrame.first < keyTime)
  353. || (_playReverse && keyFrame.first >= keyTime && keyFrame.first < prekeyTime))
  354. {
  355. auto& frameEvent = _keyFrameEvent[keyFrame.first];
  356. if (frameEvent == nullptr)
  357. frameEvent = new (std::nothrow) EventCustom(Animate3DDisplayedNotification);
  358. auto eventInfo = &_displayedEventInfo[keyFrame.first];
  359. eventInfo->target = _target;
  360. eventInfo->frame = keyFrame.first;
  361. eventInfo->userInfo = &_keyFrameUserInfos[keyFrame.first];
  362. eventInfos.push_back(eventInfo);
  363. frameEvent->setUserData((void*)eventInfo);
  364. }
  365. }
  366. std::sort(eventInfos.begin(), eventInfos.end(), _playReverse ? cmpEventInfoDes : cmpEventInfoAsc);
  367. for (auto eventInfo : eventInfos) {
  368. Director::getInstance()->getEventDispatcher()->dispatchEvent(_keyFrameEvent[eventInfo->frame]);
  369. }
  370. }
  371. }
  372. }
  373. }
  374. }
  375. float Animate3D::getSpeed() const
  376. {
  377. return _playReverse ? -_absSpeed : _absSpeed;
  378. }
  379. void Animate3D::setSpeed(float speed)
  380. {
  381. _absSpeed = fabsf(speed);
  382. _playReverse = speed < 0;
  383. _duration = _originInterval / _absSpeed;
  384. }
  385. void Animate3D::setWeight(float weight)
  386. {
  387. CCASSERT(weight >= 0.0f, "invalid weight");
  388. _weight = fabsf(weight);
  389. }
  390. void Animate3D::setOriginInterval(float interval)
  391. {
  392. _originInterval = interval;
  393. }
  394. void Animate3D::setQuality(Animate3DQuality quality)
  395. {
  396. if (quality == Animate3DQuality::QUALITY_HIGH)
  397. {
  398. _translateEvaluate = EvaluateType::INT_LINEAR;
  399. _roteEvaluate = EvaluateType::INT_QUAT_SLERP;
  400. _scaleEvaluate = EvaluateType::INT_LINEAR;
  401. }
  402. else if(quality == Animate3DQuality::QUALITY_LOW)
  403. {
  404. _translateEvaluate = EvaluateType::INT_NEAR;
  405. _roteEvaluate = EvaluateType::INT_NEAR;
  406. _scaleEvaluate = EvaluateType::INT_NEAR;
  407. }
  408. _quality = quality;
  409. }
  410. Animate3DQuality Animate3D::getQuality() const
  411. {
  412. return _quality;
  413. }
  414. const ValueMap* Animate3D::getKeyFrameUserInfo(int keyFrame) const
  415. {
  416. auto iter = _keyFrameUserInfos.find(keyFrame);
  417. if (iter != _keyFrameUserInfos.end())
  418. return &iter->second;
  419. return nullptr;
  420. }
  421. ValueMap* Animate3D::getKeyFrameUserInfo(int keyFrame)
  422. {
  423. auto iter = _keyFrameUserInfos.find(keyFrame);
  424. if (iter != _keyFrameUserInfos.end())
  425. return &iter->second;
  426. return nullptr;
  427. }
  428. void Animate3D::setKeyFrameUserInfo(int keyFrame, const ValueMap &userInfo)
  429. {
  430. _keyFrameUserInfos[keyFrame] = userInfo;
  431. }
  432. Animate3D::Animate3D()
  433. : _state(Animate3D::Animate3DState::Running)
  434. , _animation(nullptr)
  435. , _absSpeed(1.f)
  436. , _weight(1.f)
  437. , _start(0.f)
  438. , _last(1.f)
  439. , _playReverse(false)
  440. , _accTransTime(0.0f)
  441. , _lastTime(0.0f)
  442. , _originInterval(0.0f)
  443. , _frameRate(30.0f)
  444. {
  445. setQuality(Animate3DQuality::QUALITY_HIGH);
  446. }
  447. Animate3D::~Animate3D()
  448. {
  449. removeFromMap();
  450. for (auto& it : _keyFrameEvent) {
  451. delete it.second;
  452. }
  453. _keyFrameEvent.clear();
  454. CC_SAFE_RELEASE(_animation);
  455. }
  456. void Animate3D::removeFromMap()
  457. {
  458. //remove this action from map
  459. if (_target)
  460. {
  461. auto it = s_fadeInAnimates.find(_target);
  462. if (it != s_fadeInAnimates.end() && it->second == this)
  463. s_fadeInAnimates.erase(it);
  464. it = s_fadeOutAnimates.find(_target);
  465. if (it != s_fadeOutAnimates.end() && it->second == this)
  466. s_fadeOutAnimates.erase(it);
  467. it = s_runningAnimates.find(_target);
  468. if (it != s_runningAnimates.end() && it->second == this)
  469. s_runningAnimates.erase(it);
  470. }
  471. }
  472. NS_CC_END