CCPUBeamRender.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  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/CCPUBeamRender.h"
  24. #include "extensions/Particle3D/PU/CCPUParticleSystem3D.h"
  25. #include "extensions/Particle3D/PU/CCPUUtil.h"
  26. #include "extensions/Particle3D/PU/CCPUSimpleSpline.h"
  27. #include "renderer/CCMeshCommand.h"
  28. #include "renderer/CCRenderer.h"
  29. #include "renderer/CCTextureCache.h"
  30. #include "renderer/CCGLProgramState.h"
  31. #include "renderer/CCGLProgramCache.h"
  32. #include "renderer/CCVertexIndexBuffer.h"
  33. #include "base/CCDirector.h"
  34. #include "3d/CCSprite3D.h"
  35. #include "3d/CCMesh.h"
  36. #include "2d/CCCamera.h"
  37. #include <sstream>
  38. NS_CC_BEGIN
  39. // Constants
  40. const bool PUBeamRender::DEFAULT_USE_VERTEX_COLOURS = false;
  41. const size_t PUBeamRender::DEFAULT_MAX_ELEMENTS = 10;
  42. const float PUBeamRender::DEFAULT_UPDATE_INTERVAL = 0.1f;
  43. const float PUBeamRender::DEFAULT_DEVIATION = 300;
  44. const size_t PUBeamRender::DEFAULT_NUMBER_OF_SEGMENTS = 2;
  45. const PUBillboardChain::TexCoordDirection PUBeamRender::DEFAULT_TEXTURE_DIRECTION = PUBillboardChain::TCD_V;
  46. PUBeamRender* PUBeamRender::create( const std::string &texFile )
  47. {
  48. auto br = new (std::nothrow) PUBeamRender();
  49. br->autorelease();
  50. br->_texFile = texFile;
  51. return br;
  52. }
  53. void PUBeamRender::render( Renderer* renderer, const Mat4 &transform, ParticleSystem3D* particleSystem )
  54. {
  55. const ParticlePool &particlePool = particleSystem->getParticlePool();
  56. if (!_isVisible || particlePool.empty() || !_billboardChain)
  57. return;
  58. Vec3 basePosition = static_cast<PUParticleSystem3D *>(_particleSystem)->getDerivedPosition();
  59. for (auto iter : particlePool.getActiveDataList())
  60. {
  61. auto particle = static_cast<PUParticle3D *>(iter);
  62. auto visualData = static_cast<PUParticle3DBeamVisualData*>(particle->visualData);
  63. if (visualData){
  64. Vec3 end = particle->position - basePosition;
  65. PUSimpleSpline spline;
  66. // Add points
  67. spline.addPoint(Vec3::ZERO);
  68. for (size_t numDev = 0; numDev < _numberOfSegments; ++numDev)
  69. {
  70. spline.addPoint(visualData->half[numDev]);
  71. }
  72. spline.addPoint(end);
  73. // Loop through all chain elements
  74. for (size_t j = 0; j < _maxChainElements; ++j)
  75. {
  76. PUBillboardChain::Element element = _billboardChain->getChainElement(visualData->chainIndex, j);
  77. // 1. Set the width of the chain if required
  78. if (particle->ownDimensions)
  79. {
  80. element.width = _rendererScale.x * particle->width;
  81. }
  82. // 2. Set positions of the elements
  83. element.position = spline.interpolate((float)j / (float)_maxChainElements);
  84. // 3. Set the colour
  85. element.color = particle->color;
  86. // 4. Update
  87. _billboardChain->updateChainElement(visualData->chainIndex, j, element);
  88. }
  89. visualData->setVisible(true);
  90. }
  91. }
  92. _billboardChain->render(renderer, transform, particleSystem);
  93. }
  94. PUBeamRender::PUBeamRender() :
  95. _billboardChain(0),
  96. _quota(0),
  97. _useVertexColours(DEFAULT_USE_VERTEX_COLOURS),
  98. _maxChainElements(DEFAULT_MAX_ELEMENTS),
  99. _updateInterval(DEFAULT_UPDATE_INTERVAL),
  100. _deviation(DEFAULT_DEVIATION),
  101. _numberOfSegments(DEFAULT_NUMBER_OF_SEGMENTS),
  102. _jump(false),
  103. _texCoordDirection(DEFAULT_TEXTURE_DIRECTION)
  104. {
  105. autoRotate = true;
  106. }
  107. PUBeamRender::~PUBeamRender()
  108. {
  109. if (!_particleSystem)
  110. return;
  111. destroyAll();
  112. }
  113. void PUBeamRender::particleEmitted( PUParticleSystem3D* particleSystem, PUParticle3D* particle )
  114. {
  115. if (!particle->visualData && !_visualData.empty() && particle->particleType == PUParticle3D::PT_VISUAL)
  116. {
  117. particle->visualData = _visualData.back();
  118. PUParticle3DBeamVisualData* beamRendererVisualData = static_cast<PUParticle3DBeamVisualData*>(particle->visualData);
  119. beamRendererVisualData->setVisible(true, _rendererScale.x * particleSystem->getDefaultWidth()); // PU 1.4
  120. _visualData.pop_back();
  121. }
  122. }
  123. void PUBeamRender::particleExpired( PUParticleSystem3D* /*particleSystem*/, PUParticle3D* particle )
  124. {
  125. if (particle->visualData)
  126. {
  127. PUParticle3DBeamVisualData* beamRendererVisualData = static_cast<PUParticle3DBeamVisualData*>(particle->visualData);
  128. beamRendererVisualData->setVisible(false, 0); // PU 1.4
  129. _visualData.push_back(beamRendererVisualData);
  130. particle->visualData = nullptr;
  131. }
  132. }
  133. //-----------------------------------------------------------------------
  134. bool PUBeamRender::isUseVertexColours(void) const
  135. {
  136. return _useVertexColours;
  137. }
  138. //-----------------------------------------------------------------------
  139. void PUBeamRender::setUseVertexColours(bool useVertexColours)
  140. {
  141. _useVertexColours = useVertexColours;
  142. if (!_billboardChain)
  143. return;
  144. _billboardChain->setUseVertexColours(_useVertexColours);
  145. _billboardChain->setUseTextureCoords(!_useVertexColours);
  146. }
  147. //-----------------------------------------------------------------------
  148. size_t PUBeamRender::getMaxChainElements(void) const
  149. {
  150. return _maxChainElements;
  151. }
  152. //-----------------------------------------------------------------------
  153. void PUBeamRender::setMaxChainElements(size_t maxChainElements)
  154. {
  155. _maxChainElements = maxChainElements;
  156. }
  157. //-----------------------------------------------------------------------
  158. float PUBeamRender::getUpdateInterval(void) const
  159. {
  160. return _updateInterval;
  161. }
  162. //-----------------------------------------------------------------------
  163. void PUBeamRender::setUpdateInterval(float updateInterval)
  164. {
  165. _updateInterval = updateInterval;
  166. }
  167. //-----------------------------------------------------------------------
  168. float PUBeamRender::getDeviation(void) const
  169. {
  170. return _deviation;
  171. }
  172. //-----------------------------------------------------------------------
  173. void PUBeamRender::setDeviation(float deviation)
  174. {
  175. _deviation = deviation;
  176. }
  177. //-----------------------------------------------------------------------
  178. size_t PUBeamRender::getNumberOfSegments(void) const
  179. {
  180. return _numberOfSegments;
  181. }
  182. //-----------------------------------------------------------------------
  183. void PUBeamRender::setNumberOfSegments(size_t numberOfSegments)
  184. {
  185. _numberOfSegments = numberOfSegments;
  186. }
  187. //-----------------------------------------------------------------------
  188. bool PUBeamRender::isJump(void) const
  189. {
  190. return _jump;
  191. }
  192. //-----------------------------------------------------------------------
  193. void PUBeamRender::setJump(bool jump)
  194. {
  195. _jump = jump;
  196. }
  197. //-----------------------------------------------------------------------
  198. PUBillboardChain::TexCoordDirection PUBeamRender::getTexCoordDirection(void) const
  199. {
  200. return _texCoordDirection;
  201. }
  202. //-----------------------------------------------------------------------
  203. void PUBeamRender::setTexCoordDirection(PUBillboardChain::TexCoordDirection texCoordDirection)
  204. {
  205. _texCoordDirection = texCoordDirection;
  206. }
  207. //-----------------------------------------------------------------------
  208. void PUBeamRender::prepare()
  209. {
  210. if (!_particleSystem)
  211. return;
  212. // Register itself to the technique
  213. if (_particleSystem)
  214. {
  215. // Although it is safe to assume that technique == mParentTechnique, use the mParentTechnique, because the mParentTechnique is
  216. // also used for unregistering.
  217. static_cast<PUParticleSystem3D *>(_particleSystem)->addListener(this);
  218. }
  219. _quota = _particleSystem->getParticleQuota();
  220. // Create BillboardChain
  221. std::stringstream ss;
  222. ss << this;
  223. _billboardChainName = "Beam" + ss.str();
  224. _billboardChain = new (std::nothrow) PUBillboardChain(_billboardChainName, _texFile);
  225. _billboardChain->setDynamic(true);
  226. _billboardChain->setNumberOfChains(_quota);
  227. _billboardChain->setMaxChainElements(_maxChainElements);
  228. _billboardChain->setTextureCoordDirection(_texCoordDirection);
  229. setUseVertexColours(_useVertexColours);
  230. _billboardChain->setOtherTextureCoordRange(0.0f, 1.0f);
  231. _billboardChain->setDepthTest(_depthTest);
  232. _billboardChain->setDepthWrite(_depthWrite);
  233. // Create number of VisualData objects
  234. for (size_t i = 0; i < _quota; i++)
  235. {
  236. for (size_t j = 0; j < _maxChainElements; j++)
  237. {
  238. PUBillboardChain::Element element;
  239. element = PUBillboardChain::Element(Vec3::ZERO, _rendererScale.x * static_cast<PUParticleSystem3D *>(_particleSystem)->getDefaultWidth(), 0.0f, Vec4::ONE, Quaternion::identity()); // V1.51
  240. _billboardChain->addChainElement(i, element);
  241. }
  242. PUParticle3DBeamVisualData* visualData = new (std::nothrow) PUParticle3DBeamVisualData(i, _billboardChain);
  243. for (size_t numDev = 0; numDev < _numberOfSegments; ++numDev)
  244. {
  245. // Initialise the positions
  246. visualData->half[numDev].setZero();
  247. visualData->destinationHalf[numDev].setZero();
  248. }
  249. _allVisualData.push_back(visualData); // Managed by this renderer
  250. _visualData.push_back(visualData); // Used to assign to a particle
  251. }
  252. }
  253. void PUBeamRender::unPrepare()
  254. {
  255. destroyAll();
  256. }
  257. void PUBeamRender::updateRender( PUParticle3D *particle, float deltaTime, bool /*firstParticle*/ )
  258. {
  259. if (!particle->visualData)
  260. return;
  261. PUParticle3DBeamVisualData* beamRendererVisualData = static_cast<PUParticle3DBeamVisualData*>(particle->visualData);
  262. beamRendererVisualData->timeSinceLastUpdate -= deltaTime;
  263. if (beamRendererVisualData->timeSinceLastUpdate < 0)
  264. {
  265. Vec3 end = particle->position - static_cast<PUParticleSystem3D *>(_particleSystem)->getDerivedPosition();
  266. Vec3 perpendicular;
  267. float divide = (float)_numberOfSegments + 1.0f;
  268. for (size_t numDev = 0; numDev < _numberOfSegments; ++numDev)
  269. {
  270. Vec3::cross(end, Vec3(CCRANDOM_MINUS1_1(), CCRANDOM_MINUS1_1(), CCRANDOM_MINUS1_1()), &perpendicular);
  271. perpendicular.normalize();
  272. beamRendererVisualData->destinationHalf[numDev] = (((float)numDev + 1.0f) / divide) * end
  273. + Vec3(_rendererScale.x * _deviation * perpendicular.x
  274. , _rendererScale.y * _deviation * perpendicular.y
  275. , _rendererScale.z * _deviation * perpendicular.z);
  276. }
  277. beamRendererVisualData->timeSinceLastUpdate += _updateInterval;
  278. }
  279. Vec3 diff;
  280. for (size_t numDev = 0; numDev < _numberOfSegments; ++numDev)
  281. {
  282. if (_jump)
  283. {
  284. beamRendererVisualData->half[numDev] = beamRendererVisualData->destinationHalf[numDev];
  285. }
  286. else
  287. {
  288. diff = beamRendererVisualData->destinationHalf[numDev] - beamRendererVisualData->half[numDev];
  289. beamRendererVisualData->half[numDev] = beamRendererVisualData->half[numDev] + deltaTime * diff;
  290. }
  291. }
  292. }
  293. //-----------------------------------------------------------------------
  294. void PUBeamRender::destroyAll(void)
  295. {
  296. if (!_particleSystem || !_billboardChain)
  297. return;
  298. // Remove the listener
  299. static_cast<PUParticleSystem3D *>(_particleSystem)->removeListener(this);
  300. // Delete the BillboardChain
  301. CC_SAFE_DELETE(_billboardChain);
  302. // Delete the visual data
  303. std::vector<PUParticle3DBeamVisualData*>::const_iterator it;
  304. std::vector<PUParticle3DBeamVisualData*>::const_iterator itEnd = _allVisualData.end();
  305. for (it = _allVisualData.begin(); it != itEnd; ++it)
  306. {
  307. delete *it;
  308. }
  309. _allVisualData.clear();
  310. _visualData.clear();
  311. }
  312. PUBeamRender* PUBeamRender::clone()
  313. {
  314. auto br = PUBeamRender::create(_texFile);
  315. copyAttributesTo(br);
  316. return br;
  317. }
  318. void PUBeamRender::copyAttributesTo(PUBeamRender *beamRender)
  319. {
  320. PURender::copyAttributesTo(beamRender);
  321. beamRender->setUseVertexColours(_useVertexColours);
  322. beamRender->setMaxChainElements(_maxChainElements);
  323. beamRender->setUpdateInterval(_updateInterval);
  324. beamRender->setDeviation(_deviation);
  325. beamRender->setNumberOfSegments(_numberOfSegments);
  326. beamRender->setJump(_jump);
  327. beamRender->setTexCoordDirection(_texCoordDirection);
  328. }
  329. NS_CC_END