CCPURibbonTrailRender.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  1. /****************************************************************************
  2. Copyright (C) 2013 Henry van Merode. All rights reserved.
  3. Copyright (c) 2015-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 "extensions/Particle3D/CCParticleSystem3D.h"
  23. #include "extensions/Particle3D/PU/CCPURibbonTrailRender.h"
  24. #include "extensions/Particle3D/PU/CCPUUtil.h"
  25. #include "extensions/Particle3D/PU/CCPUSimpleSpline.h"
  26. #include "renderer/CCMeshCommand.h"
  27. #include "renderer/CCRenderer.h"
  28. #include "renderer/CCTextureCache.h"
  29. #include "renderer/CCGLProgramState.h"
  30. #include "renderer/CCGLProgramCache.h"
  31. #include "renderer/CCVertexIndexBuffer.h"
  32. #include "base/CCDirector.h"
  33. #include "3d/CCSprite3D.h"
  34. #include "3d/CCMesh.h"
  35. #include "2d/CCCamera.h"
  36. #include <sstream>
  37. NS_CC_BEGIN
  38. // Constants
  39. const bool PURibbonTrailRender::DEFAULT_USE_VERTEX_COLOURS = true;
  40. const size_t PURibbonTrailRender::DEFAULT_MAX_ELEMENTS = 10;
  41. const float PURibbonTrailRender::DEFAULT_LENGTH = 400;
  42. const float PURibbonTrailRender::DEFAULT_WIDTH = 5;
  43. const bool PURibbonTrailRender::DEFAULT_RANDOM_INITIAL_COLOUR = true;
  44. const Vec4 PURibbonTrailRender::DEFAULT_INITIAL_COLOUR(1, 1, 1, 1);
  45. const Vec4 PURibbonTrailRender::DEFAULT_COLOUR_CHANGE(0.5, 0.5, 0.5, 0.5);
  46. PURibbonTrailRender* PURibbonTrailRender::create( const std::string &texFile )
  47. {
  48. auto br = new (std::nothrow) PURibbonTrailRender();
  49. br->autorelease();
  50. br->_texFile = texFile;
  51. return br;
  52. }
  53. void PURibbonTrailRender::render( Renderer* renderer, const Mat4 &transform, ParticleSystem3D* particleSystem )
  54. {
  55. if (!_isVisible || !_trail)
  56. return;
  57. bool needDraw = false;
  58. const ParticlePool &particlePool = particleSystem->getParticlePool();
  59. if (!particlePool.empty()){
  60. updateParticles(particlePool);
  61. needDraw = true;
  62. }
  63. const PUParticleSystem3D::ParticlePoolMap &emitterPool = static_cast<PUParticleSystem3D *>(particleSystem)->getEmittedEmitterParticlePool();
  64. if (!emitterPool.empty()){
  65. for (auto iter : emitterPool){
  66. updateParticles(iter.second);
  67. needDraw = true;
  68. }
  69. }
  70. const PUParticleSystem3D::ParticlePoolMap &systemPool = static_cast<PUParticleSystem3D *>(particleSystem)->getEmittedSystemParticlePool();
  71. if (!systemPool.empty()){
  72. for (auto iter : systemPool){
  73. updateParticles(iter.second);
  74. needDraw = true;
  75. }
  76. }
  77. if (needDraw)
  78. _trail->render(renderer, transform, particleSystem);
  79. }
  80. PURibbonTrailRender::PURibbonTrailRender() :
  81. _quota(0),
  82. _trail(0),
  83. _useVertexColours(DEFAULT_USE_VERTEX_COLOURS),
  84. _maxChainElements(DEFAULT_MAX_ELEMENTS),
  85. _trailLength(DEFAULT_LENGTH),
  86. _trailWidth(DEFAULT_WIDTH),
  87. _randomInitialColor(DEFAULT_RANDOM_INITIAL_COLOUR),
  88. _setLength(false),
  89. _setWidth(false),
  90. _initialColor(DEFAULT_INITIAL_COLOUR),
  91. _colorChange(DEFAULT_COLOUR_CHANGE),
  92. _childNode(0)
  93. {
  94. std::stringstream ss;
  95. ss << this;
  96. _ribbonTrailName = "RibbonTrail" + ss.str();
  97. autoRotate = false;
  98. }
  99. PURibbonTrailRender::~PURibbonTrailRender()
  100. {
  101. if (!_particleSystem)
  102. return;
  103. destroyAll();
  104. }
  105. void PURibbonTrailRender::particleEmitted( PUParticleSystem3D* /*particleSystem*/, PUParticle3D* /*particle*/ )
  106. {
  107. // Assigning visual data already done in _updateRenderQueue()
  108. }
  109. void PURibbonTrailRender::particleExpired( PUParticleSystem3D* /*particleSystem*/, PUParticle3D* particle )
  110. {
  111. if (particle->visualData)
  112. {
  113. PURibbonTrailVisualData* ribbonTrailRendererVisualData = static_cast<PURibbonTrailVisualData*>(particle->visualData);
  114. ribbonTrailRendererVisualData->setVisible(false);
  115. }
  116. }
  117. //-----------------------------------------------------------------------
  118. bool PURibbonTrailRender::isUseVertexColors(void) const
  119. {
  120. return _useVertexColours;
  121. }
  122. //-----------------------------------------------------------------------
  123. void PURibbonTrailRender::setUseVertexColors(bool useVertexColours)
  124. {
  125. _useVertexColours = useVertexColours;
  126. }
  127. //-----------------------------------------------------------------------
  128. size_t PURibbonTrailRender::getMaxChainElements(void) const
  129. {
  130. return _maxChainElements;
  131. }
  132. //-----------------------------------------------------------------------
  133. void PURibbonTrailRender::setMaxChainElements(size_t maxChainElements)
  134. {
  135. _maxChainElements = maxChainElements;
  136. }
  137. //-----------------------------------------------------------------------
  138. float PURibbonTrailRender::getTrailLength(void) const
  139. {
  140. return _trailLength;
  141. }
  142. //-----------------------------------------------------------------------
  143. void PURibbonTrailRender::setTrailLength(float trailLength)
  144. {
  145. _trailLength = trailLength;
  146. _setLength = true;
  147. }
  148. //-----------------------------------------------------------------------
  149. float PURibbonTrailRender::getTrailWidth(void) const
  150. {
  151. return _trailWidth;
  152. }
  153. //-----------------------------------------------------------------------
  154. void PURibbonTrailRender::setTrailWidth(float trailWidth)
  155. {
  156. _trailWidth = trailWidth;
  157. _setWidth = true;
  158. }
  159. //-----------------------------------------------------------------------
  160. bool PURibbonTrailRender::isRandomInitialColor(void) const
  161. {
  162. return _randomInitialColor;
  163. }
  164. //-----------------------------------------------------------------------
  165. void PURibbonTrailRender::setRandomInitialColor(bool randomInitialColour)
  166. {
  167. _randomInitialColor = randomInitialColour;
  168. }
  169. //-----------------------------------------------------------------------
  170. const Vec4& PURibbonTrailRender::getInitialColor(void) const
  171. {
  172. return _initialColor;
  173. }
  174. //-----------------------------------------------------------------------
  175. void PURibbonTrailRender::setInitialColor(const Vec4& initialColour)
  176. {
  177. _initialColor = initialColour;
  178. }
  179. //-----------------------------------------------------------------------
  180. const Vec4& PURibbonTrailRender::getColorChange(void) const
  181. {
  182. return _colorChange;
  183. }
  184. //-----------------------------------------------------------------------
  185. void PURibbonTrailRender::setColorChange(const Vec4& colourChange)
  186. {
  187. _colorChange = colourChange;
  188. }
  189. //-----------------------------------------------------------------------
  190. void PURibbonTrailRender::prepare()
  191. {
  192. if (!_particleSystem)
  193. return;
  194. // Register itself to the technique
  195. if (_particleSystem)
  196. {
  197. // Although it is safe to assume that technique == mParentTechnique, use the mParentTechnique, because the mParentTechnique is
  198. // also used for unregistering.
  199. static_cast<PUParticleSystem3D *>(_particleSystem)->addListener(this);
  200. }
  201. _quota = _particleSystem->getParticleQuota();
  202. if (!_childNode)
  203. {
  204. // Create a childnode if not created earlier
  205. std::stringstream ss;
  206. ss << this;
  207. std::string childNodeNodeName = "ParticleUniverse" + ss.str();
  208. //_childNode = mParentTechnique->getParentSystem()->getParentSceneNode()->createChildSceneNode(childNodeNodeName);
  209. //_childNode->setInheritOrientation(false);
  210. auto parent = _particleSystem->getParent();
  211. if (parent)
  212. {
  213. _childNode = Node::create();
  214. parent->addChild(_childNode);
  215. }
  216. }
  217. if (_childNode)
  218. {
  219. // Create RibbonTrail
  220. //Ogre::SceneManager* sceneManager = mParentTechnique->getParentSystem()->getSceneManager();
  221. _trail = new (std::nothrow) PURibbonTrail(_ribbonTrailName, _texFile);
  222. _trail->setNumberOfChains(_quota);
  223. _trail->setMaxChainElements(_maxChainElements);
  224. if (_setLength)
  225. {
  226. _trail->setTrailLength(_rendererScale.y * _trailLength);
  227. }
  228. else
  229. {
  230. _trail->setTrailLength(_rendererScale.y * static_cast<PUParticleSystem3D *>(_particleSystem)->getDefaultHeight());
  231. }
  232. _trail->setUseVertexColours(_useVertexColours);
  233. // Create number of VisualData objects
  234. std::string sceneNodeName;
  235. std::stringstream ss;
  236. ss << this;
  237. for (size_t i = 0; i < _quota; i++)
  238. {
  239. //sceneNodeName = "ParticleUniverse" + ss.str() + StringConverter::toString(i);
  240. //RibbonTrailRendererVisualData* visualData =
  241. // PU_NEW_T(RibbonTrailRendererVisualData, MEMCATEGORY_SCENE_OBJECTS)(_childNode->createChildSceneNode(sceneNodeName), _trail);
  242. //visualData->node->setInheritOrientation(false);
  243. auto node = Node::create();
  244. _childNode->addChild(node);
  245. PURibbonTrailVisualData* visualData = new (std::nothrow) PURibbonTrailVisualData(node, _trail);
  246. visualData->index = i;
  247. _allVisualData.push_back(visualData); // Managed by this renderer
  248. _visualData.push_back(visualData); // Used to assign to a particle
  249. if (_randomInitialColor)
  250. {
  251. _trail->setInitialColour(i, CCRANDOM_0_1(), CCRANDOM_0_1(), CCRANDOM_0_1());
  252. }
  253. else
  254. {
  255. _trail->setInitialColour(i, _initialColor);
  256. }
  257. _trail->setColourChange(i, _colorChange);
  258. if (_setWidth)
  259. {
  260. _trail->setInitialWidth(i, _rendererScale.x * _trailWidth);
  261. }
  262. else
  263. {
  264. _trail->setInitialWidth(i, _rendererScale.x * static_cast<PUParticleSystem3D *>(_particleSystem)->getDefaultWidth());
  265. }
  266. }
  267. //_childNode->attachObject(_trail);
  268. _trail->setAttachedNode(_childNode);
  269. _trail->setDepthTest(_depthTest);
  270. _trail->setDepthWrite(_depthWrite);
  271. }
  272. }
  273. void PURibbonTrailRender::unPrepare()
  274. {
  275. destroyAll();
  276. }
  277. void PURibbonTrailRender::updateRender( PUParticle3D* /*particle*/, float deltaTime, bool /*firstParticle*/ )
  278. {
  279. if (_trail)
  280. _trail->update(deltaTime);
  281. }
  282. //-----------------------------------------------------------------------
  283. void PURibbonTrailRender::destroyAll(void)
  284. {
  285. if (!_particleSystem || !_trail || !_childNode)
  286. return;
  287. // Remove the listener
  288. static_cast<PUParticleSystem3D *>(_particleSystem)->removeListener(this);
  289. // Detach the Ribbontrail
  290. if (_childNode && _trail)
  291. {
  292. _trail->setAttachedNode(nullptr);
  293. //_childNode->detachObject(_trail);
  294. }
  295. // Delete the Ribbontrail
  296. CC_SAFE_DELETE(_trail);
  297. // Delete the visual data
  298. std::vector<PURibbonTrailVisualData*>::const_iterator it;
  299. std::vector<PURibbonTrailVisualData*>::const_iterator itEnd = _allVisualData.end();
  300. for (it = _allVisualData.begin(); it != itEnd; ++it)
  301. {
  302. delete *it;
  303. }
  304. _allVisualData.clear();
  305. _visualData.clear();
  306. // Destroy the children of the childnode, but not the childnode itself, because this gives a delete-order problem
  307. if (_childNode)
  308. {
  309. //_childNode->removeAndDestroyAllChildren();
  310. _childNode->removeAllChildren();
  311. if (_childNode->getParent())
  312. _childNode->getParent()->removeChild(_childNode);
  313. _childNode = nullptr;
  314. }
  315. }
  316. void PURibbonTrailRender::notifyRescaled( const Vec3& scale )
  317. {
  318. PURender::notifyRescaled(scale);
  319. if (_trail)
  320. {
  321. // Use the average length (is there a good alternative?
  322. _trail->setTrailLength(scale.y * _trailLength);
  323. size_t numberOfChains = _trail->getNumberOfChains();
  324. for (size_t i = 0; i < numberOfChains; ++i)
  325. {
  326. _trail->setInitialWidth(i, scale.x * _trailWidth);
  327. }
  328. }
  329. }
  330. PURibbonTrailRender* PURibbonTrailRender::clone()
  331. {
  332. auto tr = PURibbonTrailRender::create(_texFile);
  333. copyAttributesTo(tr);
  334. return tr;
  335. }
  336. void PURibbonTrailRender::copyAttributesTo(PURibbonTrailRender *trailRender)
  337. {
  338. PURender::copyAttributesTo(trailRender);
  339. trailRender->setUseVertexColors(_useVertexColours);
  340. trailRender->setMaxChainElements(_maxChainElements);
  341. trailRender->setTrailLength(_trailLength);
  342. trailRender->setTrailWidth(_trailWidth);
  343. trailRender->setRandomInitialColor(_randomInitialColor);
  344. trailRender->setInitialColor(_initialColor);
  345. trailRender->setColorChange(_colorChange);
  346. }
  347. void PURibbonTrailRender::updateParticles( const ParticlePool &pool )
  348. {
  349. PURibbonTrailVisualData* visualData = nullptr;
  350. Vec3 basePosition = static_cast<PUParticleSystem3D *>(_particleSystem)->getDerivedPosition();
  351. for (auto iter : pool.getActiveDataList())
  352. {
  353. auto particle = static_cast<PUParticle3D *>(iter);
  354. if (!particle->visualData && !_visualData.empty())
  355. {
  356. particle->visualData = _visualData.back();
  357. _visualData.pop_back();
  358. }
  359. visualData = static_cast<PURibbonTrailVisualData*>(particle->visualData);
  360. if (visualData)
  361. {
  362. Node* node = visualData->node;
  363. node->setPosition3D(particle->position);
  364. // Set the width of the trail if required
  365. if (particle->particleType == PUParticle3D::PT_VISUAL)
  366. {
  367. if (particle->ownDimensions)
  368. {
  369. _trail->setInitialWidth(visualData->index, particle->width);
  370. }
  371. }
  372. visualData->setVisible(true);
  373. }
  374. }
  375. }
  376. NS_CC_END