PageRenderTime 32ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/cpp-tests/Classes/Particle3DTest/Particle3DTest.cpp

https://github.com/dumganhar/cocos2d-x
C++ | 480 lines | 333 code | 100 blank | 47 comment | 21 complexity | c8d13aa2445f7ed7da210f20a00ef0a3 MD5 | raw file
  1. /****************************************************************************
  2. Copyright (c) 2012 cocos2d-x.org
  3. Copyright (c) 2013-2016 Chukong Technologies Inc.
  4. Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
  5. http://www.cocos2d-x.org
  6. Permission is hereby granted, free of charge, to any person obtaining a copy
  7. of this software and associated documentation files (the "Software"), to deal
  8. in the Software without restriction, including without limitation the rights
  9. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. copies of the Software, and to permit persons to whom the Software is
  11. furnished to do so, subject to the following conditions:
  12. The above copyright notice and this permission notice shall be included in
  13. all copies or substantial portions of the Software.
  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 "Particle3DTest.h"
  23. #include "Particle3D/CCParticleSystem3D.h"
  24. #include "Particle3D/PU/CCPUParticleSystem3D.h"
  25. USING_NS_CC;
  26. #define PARTICLE_SYSTEM_TAG 0x0001
  27. Particle3DTests::Particle3DTests()
  28. {
  29. ADD_TEST_CASE(Particle3DExplosionSystemDemo);
  30. ADD_TEST_CASE(Particle3DLineStreakDemo);
  31. ADD_TEST_CASE(Particle3DBlackHoleDemo);
  32. ADD_TEST_CASE(Particle3DHypnoDemo);
  33. ADD_TEST_CASE(Particle3DAdvancedLodSystemDemo);
  34. ADD_TEST_CASE(Particle3DTimeShiftDemo);
  35. ADD_TEST_CASE(Particle3DUVAnimDemo);
  36. ADD_TEST_CASE(Particle3DFirePlaceDemo);
  37. ADD_TEST_CASE(Particle3DElectricBeamSystemDemo);
  38. ADD_TEST_CASE(Particle3DFlareShieldDemo);
  39. ADD_TEST_CASE(Particle3DLightningBoltDemo);
  40. ADD_TEST_CASE(Particle3DCanOfWormsDemo);
  41. ADD_TEST_CASE(Particle3DRibbonTrailDemo);
  42. ADD_TEST_CASE(Particle3DWeaponTrailDemo);
  43. ADD_TEST_CASE(Particle3DWithSprite3DDemo);
  44. }
  45. std::string Particle3DTestDemo::title() const
  46. {
  47. return "Particle3D Test";
  48. }
  49. bool Particle3DTestDemo::init()
  50. {
  51. if (!TestCase::init()) return false;
  52. FileUtils::getInstance()->addSearchPath("Particle3D/materials");
  53. FileUtils::getInstance()->addSearchPath("Particle3D/scripts");
  54. FileUtils::getInstance()->addSearchPath("Sprite3DTest");
  55. //FileUtils::getInstance()->addSearchPath("Particle3D/textures");
  56. Size size = Director::getInstance()->getWinSize();
  57. _camera = Camera::createPerspective(30.0f, size.width / size.height, 1.0f, 1000.0f);
  58. _camera->setPosition3D(Vec3(0.0f, 0.0f, 100.0f));
  59. _camera->lookAt(Vec3(0.0f, 0.0f, 0.0f), Vec3(0.0f, 1.0f, 0.0f));
  60. _camera->setCameraFlag(CameraFlag::USER1);
  61. this->addChild(_camera);
  62. auto listener = EventListenerTouchAllAtOnce::create();
  63. listener->onTouchesBegan = CC_CALLBACK_2(Particle3DTestDemo::onTouchesBegan, this);
  64. listener->onTouchesMoved = CC_CALLBACK_2(Particle3DTestDemo::onTouchesMoved, this);
  65. listener->onTouchesEnded = CC_CALLBACK_2(Particle3DTestDemo::onTouchesEnded, this);
  66. _eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);
  67. TTFConfig config("fonts/tahoma.ttf",10);
  68. _particleLab = Label::createWithTTF(config,"Particle Count: 0",TextHAlignment::LEFT);
  69. _particleLab->retain();
  70. _particleLab->setPosition(Vec2(0.0f, size.height / 6.0f));
  71. _particleLab->setAnchorPoint(Vec2(0.0f, 0.0f));
  72. this->addChild(_particleLab);
  73. scheduleUpdate();
  74. return true;
  75. }
  76. void Particle3DTestDemo::onTouchesBegan(const std::vector<Touch*>& touches, cocos2d::Event *event)
  77. {
  78. }
  79. void Particle3DTestDemo::onTouchesMoved(const std::vector<Touch*>& touches, cocos2d::Event *event)
  80. {
  81. if (touches.size())
  82. {
  83. auto touch = touches[0];
  84. auto delta = touch->getDelta();
  85. _angle -= CC_DEGREES_TO_RADIANS(delta.x);
  86. _camera->setPosition3D(Vec3(100.0f * sinf(_angle), 0.0f, 100.0f * cosf(_angle)));
  87. _camera->lookAt(Vec3(0.0f, 0.0f, 0.0f), Vec3(0.0f, 1.0f, 0.0f));
  88. }
  89. }
  90. void Particle3DTestDemo::onTouchesEnded(const std::vector<Touch*>& touches, cocos2d::Event *event)
  91. {
  92. }
  93. Particle3DTestDemo::Particle3DTestDemo( void )
  94. : _angle(0.0f)
  95. {
  96. }
  97. void Particle3DTestDemo::update( float delta )
  98. {
  99. ParticleSystem3D *ps = static_cast<ParticleSystem3D *>(this->getChildByTag(PARTICLE_SYSTEM_TAG));
  100. if (ps){
  101. unsigned int count = 0;
  102. auto children = ps->getChildren();
  103. for (auto iter : children){
  104. ParticleSystem3D *child = dynamic_cast<ParticleSystem3D *>(iter);
  105. if (child){
  106. count += child->getAliveParticleCount();
  107. }
  108. }
  109. char str[128];
  110. sprintf(str, "Particle Count: %d", count);
  111. _particleLab->setString(str);
  112. }
  113. }
  114. Particle3DTestDemo::~Particle3DTestDemo( void )
  115. {
  116. _particleLab->release();
  117. }
  118. std::string Particle3DAdvancedLodSystemDemo::subtitle() const
  119. {
  120. return "AdvancedSystem";
  121. }
  122. bool Particle3DAdvancedLodSystemDemo::init()
  123. {
  124. if (!Particle3DTestDemo::init())
  125. return false;
  126. auto rootps = PUParticleSystem3D::create("advancedLodSystem.pu");
  127. rootps->setCameraMask((unsigned short)CameraFlag::USER1);
  128. auto scale = ScaleBy::create(1.0f, 2.0f, 2.0f, 2.0f);
  129. auto rotate = RotateBy::create(1.0f, Vec3(0.0f, 0.0f, 100.0f));
  130. rootps->runAction(RepeatForever::create(Sequence::create(rotate, nullptr)));
  131. rootps->runAction(RepeatForever::create(Sequence::create(scale, scale->reverse(), nullptr)));
  132. rootps->startParticleSystem();
  133. this->addChild(rootps, 0, PARTICLE_SYSTEM_TAG);
  134. return true;
  135. }
  136. std::string Particle3DBlackHoleDemo::subtitle() const
  137. {
  138. return "BlackHole";
  139. }
  140. bool Particle3DBlackHoleDemo::init()
  141. {
  142. if (!Particle3DTestDemo::init())
  143. return false;
  144. auto rootps = PUParticleSystem3D::create("blackHole.pu", "pu_mediapack_01.material");
  145. rootps->setCameraMask((unsigned short)CameraFlag::USER1);
  146. rootps->setPosition(-25.0f, 0.0f);
  147. auto moveby = MoveBy::create(2.0f, Vec2(50.0f, 0.0f));
  148. auto moveby1 = MoveBy::create(2.0f, Vec2(-50.0f, 0.0f));
  149. // auto scale = ScaleBy::create(1.0f, 2.0f, 2.0f, 2.0f);
  150. // auto rotate = RotateBy::create(1.0f, Vec3(100.0f, 100.0f, 100.0f));
  151. rootps->runAction(RepeatForever::create(Sequence::create(moveby, moveby1, nullptr)));
  152. //rootps->runAction(RepeatForever::create(Sequence::create(scale, scale->reverse(), nullptr)));
  153. //rootps->runAction(RepeatForever::create(Sequence::create(rotate, nullptr)));
  154. rootps->startParticleSystem();
  155. this->addChild(rootps, 0, PARTICLE_SYSTEM_TAG);
  156. return true;
  157. }
  158. std::string Particle3DHypnoDemo::subtitle() const
  159. {
  160. return "Hypno";
  161. }
  162. bool Particle3DHypnoDemo::init()
  163. {
  164. if (!Particle3DTestDemo::init())
  165. return false;
  166. auto rootps = PUParticleSystem3D::create("hypno.pu", "pu_mediapack_01.material");
  167. rootps->setCameraMask((unsigned short)CameraFlag::USER1);
  168. // auto scale = ScaleBy::create(1.0f, 2.0f, 2.0f, 2.0f);
  169. // auto rotate = RotateBy::create(1.0f, Vec3(0.0, 100.0f, 0.0f));
  170. //rootps->runAction(RepeatForever::create(Sequence::create(scale, scale->reverse(), nullptr)));
  171. //rootps->runAction(RepeatForever::create(Sequence::create(rotate, nullptr)));
  172. rootps->startParticleSystem();
  173. this->addChild(rootps, 0, PARTICLE_SYSTEM_TAG);
  174. return true;
  175. }
  176. std::string Particle3DTimeShiftDemo::subtitle() const
  177. {
  178. return "TimeShift";
  179. }
  180. bool Particle3DTimeShiftDemo::init()
  181. {
  182. if (!Particle3DTestDemo::init())
  183. return false;
  184. auto rootps = PUParticleSystem3D::create("timeShift.pu", "pu_mediapack_01.material");
  185. rootps->setScale(2.0f);
  186. rootps->setCameraMask((unsigned short)CameraFlag::USER1);
  187. rootps->startParticleSystem();
  188. this->addChild(rootps, 0, PARTICLE_SYSTEM_TAG);
  189. return true;
  190. }
  191. std::string Particle3DUVAnimDemo::subtitle() const
  192. {
  193. return "UVAnim";
  194. }
  195. bool Particle3DUVAnimDemo::init()
  196. {
  197. if (!Particle3DTestDemo::init())
  198. return false;
  199. auto rootps = PUParticleSystem3D::create("UVAnimation.pu", "pu_mediapack_01.material");
  200. rootps->setCameraMask((unsigned short)CameraFlag::USER1);
  201. rootps->startParticleSystem();
  202. this->addChild(rootps, 0, PARTICLE_SYSTEM_TAG);
  203. return true;
  204. }
  205. std::string Particle3DFirePlaceDemo::subtitle() const
  206. {
  207. return "Fire";
  208. }
  209. bool Particle3DFirePlaceDemo::init()
  210. {
  211. if (!Particle3DTestDemo::init())
  212. return false;
  213. auto rootps = PUParticleSystem3D::create("mp_torch.pu", "pu_mediapack_01.material");
  214. rootps->setCameraMask((unsigned short)CameraFlag::USER1);
  215. rootps->setScale(5.0f);
  216. rootps->startParticleSystem();
  217. this->addChild(rootps, 0, PARTICLE_SYSTEM_TAG);
  218. return true;
  219. }
  220. std::string Particle3DLineStreakDemo::subtitle() const
  221. {
  222. return "LineStreak";
  223. }
  224. bool Particle3DLineStreakDemo::init()
  225. {
  226. if (!Particle3DTestDemo::init())
  227. return false;
  228. auto rootps = PUParticleSystem3D::create("lineStreak.pu", "pu_mediapack_01.material");
  229. rootps->setCameraMask((unsigned short)CameraFlag::USER1);
  230. rootps->setScale(5.0f);
  231. //rootps->runAction(RepeatForever::create(Sequence::create(rotate, nullptr)));
  232. rootps->startParticleSystem();
  233. this->addChild(rootps, 0, PARTICLE_SYSTEM_TAG);
  234. //auto sprite = Sprite::create("pump_streak_04.png");
  235. //sprite->setCameraMask((unsigned short)CameraFlag::USER1);
  236. //sprite->setScale(0.5f);
  237. //this->addChild(sprite);
  238. return true;
  239. }
  240. std::string Particle3DElectricBeamSystemDemo::subtitle() const
  241. {
  242. return "ElectricBeamSystem";
  243. }
  244. bool Particle3DElectricBeamSystemDemo::init()
  245. {
  246. if (!Particle3DTestDemo::init())
  247. return false;
  248. auto rootps = PUParticleSystem3D::create("electricBeamSystem.pu");
  249. rootps->setCameraMask((unsigned short)CameraFlag::USER1);
  250. rootps->startParticleSystem();
  251. this->addChild(rootps, 0, PARTICLE_SYSTEM_TAG);
  252. return true;
  253. }
  254. std::string Particle3DFlareShieldDemo::subtitle() const
  255. {
  256. return "flareShield";
  257. }
  258. bool Particle3DFlareShieldDemo::init()
  259. {
  260. if (!Particle3DTestDemo::init())
  261. return false;
  262. auto rootps = PUParticleSystem3D::create("flareShield.pu");
  263. rootps->setCameraMask((unsigned short)CameraFlag::USER1);
  264. //rootps->setScale(0.25f);
  265. rootps->startParticleSystem();
  266. this->addChild(rootps, 0, PARTICLE_SYSTEM_TAG);
  267. return true;
  268. }
  269. std::string Particle3DLightningBoltDemo::subtitle() const
  270. {
  271. return "LightningBolt";
  272. }
  273. bool Particle3DLightningBoltDemo::init()
  274. {
  275. if (!Particle3DTestDemo::init())
  276. return false;
  277. auto rootps = PUParticleSystem3D::create("lightningBolt.pu");
  278. rootps->setCameraMask((unsigned short)CameraFlag::USER1);
  279. //rootps->setScale(0.25f);
  280. rootps->startParticleSystem();
  281. this->addChild(rootps, 0, PARTICLE_SYSTEM_TAG);
  282. return true;
  283. }
  284. std::string Particle3DExplosionSystemDemo::subtitle() const
  285. {
  286. return "ExplosionSystem";
  287. }
  288. bool Particle3DExplosionSystemDemo::init()
  289. {
  290. if (!Particle3DTestDemo::init())
  291. return false;
  292. auto rootps = PUParticleSystem3D::create("explosionSystem.pu");
  293. rootps->setCameraMask((unsigned short)CameraFlag::USER1);
  294. //rootps->setScale(5.0f);
  295. //rootps->setPosition(-25.0f, 0.0f);
  296. //auto moveby = MoveBy::create(2.0f, Vec2(50.0f, 0.0f));
  297. //auto moveby1 = MoveBy::create(2.0f, Vec2(-50.0f, 0.0f));
  298. //rootps->runAction(RepeatForever::create(Sequence::create(moveby, moveby1, nullptr)));
  299. rootps->startParticleSystem();
  300. this->addChild(rootps, 0, PARTICLE_SYSTEM_TAG);
  301. return true;
  302. }
  303. std::string Particle3DCanOfWormsDemo::subtitle() const
  304. {
  305. return "CanOfWorms";
  306. }
  307. bool Particle3DCanOfWormsDemo::init()
  308. {
  309. if (!Particle3DTestDemo::init())
  310. return false;
  311. auto rootps = PUParticleSystem3D::create("canOfWorms.pu");
  312. rootps->setCameraMask((unsigned short)CameraFlag::USER1);
  313. rootps->startParticleSystem();
  314. this->addChild(rootps, 0, PARTICLE_SYSTEM_TAG);
  315. return true;
  316. }
  317. std::string Particle3DRibbonTrailDemo::subtitle() const
  318. {
  319. return "RibbonTrailTest";
  320. }
  321. bool Particle3DRibbonTrailDemo::init()
  322. {
  323. if (!Particle3DTestDemo::init())
  324. return false;
  325. auto rootps = PUParticleSystem3D::create("ribbonTrailTest.pu");
  326. rootps->setCameraMask((unsigned short)CameraFlag::USER1);
  327. rootps->startParticleSystem();
  328. this->addChild(rootps, 0, PARTICLE_SYSTEM_TAG);
  329. return true;
  330. }
  331. std::string Particle3DWeaponTrailDemo::subtitle() const
  332. {
  333. return "WeaponTrail";
  334. }
  335. bool Particle3DWeaponTrailDemo::init()
  336. {
  337. if (!Particle3DTestDemo::init())
  338. return false;
  339. auto rootps = PUParticleSystem3D::create("weaponTrail.pu");
  340. rootps->setCameraMask((unsigned short)CameraFlag::USER1);
  341. rootps->startParticleSystem();
  342. this->addChild(rootps, 0, PARTICLE_SYSTEM_TAG);
  343. return true;
  344. }
  345. std::string Particle3DWithSprite3DDemo::subtitle() const
  346. {
  347. return "Particle3DWithSprite3D";
  348. }
  349. bool Particle3DWithSprite3DDemo::init()
  350. {
  351. if (!Particle3DTestDemo::init())
  352. return false;
  353. std::string c3bfileName = "Sprite3DTest/orc.c3b";
  354. auto sprite = Sprite3D::create(c3bfileName);
  355. this->addChild(sprite);
  356. sprite->setPosition3D(Vec3(-20.0f, 0.0f, 0.0f));
  357. sprite->setRotation3D(Vec3(0, 180, 0));
  358. sprite->setCameraMask((unsigned short)CameraFlag::USER1);
  359. //sprite->setOpacity(100);
  360. auto animation = Animation3D::create(c3bfileName);
  361. if (animation)
  362. {
  363. auto animate = Animate3D::create(animation);
  364. sprite->runAction(RepeatForever::create(animate));
  365. }
  366. auto billboard = BillBoard::create("Images/Icon.png");
  367. billboard->setPosition3D(Vec3(20.0f, 0.0f, 0.0f));
  368. billboard->setScale(0.2f);
  369. billboard->setCameraMask((unsigned short)CameraFlag::USER1);
  370. this->addChild(billboard);
  371. auto rootps = PUParticleSystem3D::create("lineStreak.pu");
  372. rootps->setCameraMask((unsigned short)CameraFlag::USER1);
  373. rootps->startParticleSystem();
  374. this->addChild(rootps, 0, PARTICLE_SYSTEM_TAG);
  375. return true;
  376. }