CCPUBeamRender.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. /****************************************************************************
  2. Copyright (C) 2013 Henry van Merode. All rights reserved.
  3. Copyright (c) 2015-2017 Chukong Technologies Inc.
  4. http://www.cocos2d-x.org
  5. Permission is hereby granted, free of charge, to any person obtaining a copy
  6. of this software and associated documentation files (the "Software"), to deal
  7. in the Software without restriction, including without limitation the rights
  8. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. copies of the Software, and to permit persons to whom the Software is
  10. furnished to do so, subject to the following conditions:
  11. The above copyright notice and this permission notice shall be included in
  12. all copies or substantial portions of the Software.
  13. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. THE SOFTWARE.
  20. ****************************************************************************/
  21. #include "extensions/Particle3D/CCParticleSystem3D.h"
  22. #include "extensions/Particle3D/PU/CCPUBeamRender.h"
  23. #include "extensions/Particle3D/PU/CCPUParticleSystem3D.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 PUBeamRender::DEFAULT_USE_VERTEX_COLOURS = false;
  40. const size_t PUBeamRender::DEFAULT_MAX_ELEMENTS = 10;
  41. const float PUBeamRender::DEFAULT_UPDATE_INTERVAL = 0.1f;
  42. const float PUBeamRender::DEFAULT_DEVIATION = 300;
  43. const size_t PUBeamRender::DEFAULT_NUMBER_OF_SEGMENTS = 2;
  44. const PUBillboardChain::TexCoordDirection PUBeamRender::DEFAULT_TEXTURE_DIRECTION = PUBillboardChain::TCD_V;
  45. PUBeamRender* PUBeamRender::create( const std::string &texFile )
  46. {
  47. auto br = new (std::nothrow) PUBeamRender();
  48. br->autorelease();
  49. br->_texFile = texFile;
  50. return br;
  51. }
  52. void PUBeamRender::render( Renderer* renderer, const Mat4 &transform, ParticleSystem3D* particleSystem )
  53. {
  54. const ParticlePool &particlePool = particleSystem->getParticlePool();
  55. if (!_isVisible || particlePool.empty() || !_billboardChain)
  56. return;
  57. Vec3 basePosition = static_cast<PUParticleSystem3D *>(_particleSystem)->getDerivedPosition();
  58. for (auto iter : particlePool.getActiveDataList())
  59. {
  60. auto particle = static_cast<PUParticle3D *>(iter);
  61. auto visualData = static_cast<PUParticle3DBeamVisualData*>(particle->visualData);
  62. if (visualData){
  63. Vec3 end = particle->position - basePosition;
  64. PUSimpleSpline spline;
  65. // Add points
  66. spline.addPoint(Vec3::ZERO);
  67. for (size_t numDev = 0; numDev < _numberOfSegments; ++numDev)
  68. {
  69. spline.addPoint(visualData->half[numDev]);
  70. }
  71. spline.addPoint(end);
  72. // Loop through all chain elements
  73. for (size_t j = 0; j < _maxChainElements; ++j)
  74. {
  75. PUBillboardChain::Element element = _billboardChain->getChainElement(visualData->chainIndex, j);
  76. // 1. Set the width of the chain if required
  77. if (particle->ownDimensions)
  78. {
  79. element.width = _rendererScale.x * particle->width;
  80. }
  81. // 2. Set positions of the elements
  82. element.position = spline.interpolate((float)j / (float)_maxChainElements);
  83. // 3. Set the colour
  84. element.color = particle->color;
  85. // 4. Update
  86. _billboardChain->updateChainElement(visualData->chainIndex, j, element);
  87. }
  88. visualData->setVisible(true);
  89. }
  90. }
  91. _billboardChain->render(renderer, transform, particleSystem);
  92. }
  93. PUBeamRender::PUBeamRender() :
  94. _billboardChain(0),
  95. _quota(0),
  96. _useVertexColours(DEFAULT_USE_VERTEX_COLOURS),
  97. _maxChainElements(DEFAULT_MAX_ELEMENTS),
  98. _updateInterval(DEFAULT_UPDATE_INTERVAL),
  99. _deviation(DEFAULT_DEVIATION),
  100. _numberOfSegments(DEFAULT_NUMBER_OF_SEGMENTS),
  101. _jump(false),
  102. _texCoordDirection(DEFAULT_TEXTURE_DIRECTION)
  103. {
  104. autoRotate = true;
  105. }
  106. PUBeamRender::~PUBeamRender()
  107. {
  108. if (!_particleSystem)
  109. return;
  110. destroyAll();
  111. }
  112. void PUBeamRender::particleEmitted( PUParticleSystem3D* particleSystem, PUParticle3D* particle )
  113. {
  114. if (!particle->visualData && !_visualData.empty() && particle->particleType == PUParticle3D::PT_VISUAL)
  115. {
  116. particle->visualData = _visualData.back();
  117. PUParticle3DBeamVisualData* beamRendererVisualData = static_cast<PUParticle3DBeamVisualData*>(particle->visualData);
  118. beamRendererVisualData->setVisible(true, _rendererScale.x * particleSystem->getDefaultWidth()); // PU 1.4
  119. _visualData.pop_back();
  120. }
  121. }
  122. void PUBeamRender::particleExpired( PUParticleSystem3D* /*particleSystem*/, PUParticle3D* particle )
  123. {
  124. if (particle->visualData)
  125. {
  126. PUParticle3DBeamVisualData* beamRendererVisualData = static_cast<PUParticle3DBeamVisualData*>(particle->visualData);
  127. beamRendererVisualData->setVisible(false, 0); // PU 1.4
  128. _visualData.push_back(beamRendererVisualData);
  129. particle->visualData = nullptr;
  130. }
  131. }
  132. //-----------------------------------------------------------------------
  133. bool PUBeamRender::isUseVertexColours(void) const
  134. {
  135. return _useVertexColours;
  136. }
  137. //-----------------------------------------------------------------------
  138. void PUBeamRender::setUseVertexColours(bool useVertexColours)
  139. {
  140. _useVertexColours = useVertexColours;
  141. if (!_billboardChain)
  142. return;
  143. _billboardChain->setUseVertexColours(_useVertexColours);
  144. _billboardChain->setUseTextureCoords(!_useVertexColours);
  145. }
  146. //-----------------------------------------------------------------------
  147. size_t PUBeamRender::getMaxChainElements(void) const
  148. {
  149. return _maxChainElements;
  150. }
  151. //-----------------------------------------------------------------------
  152. void PUBeamRender::setMaxChainElements(size_t maxChainElements)
  153. {
  154. _maxChainElements = maxChainElements;
  155. }
  156. //-----------------------------------------------------------------------
  157. float PUBeamRender::getUpdateInterval(void) const
  158. {
  159. return _updateInterval;
  160. }
  161. //-----------------------------------------------------------------------
  162. void PUBeamRender::setUpdateInterval(float updateInterval)
  163. {
  164. _updateInterval = updateInterval;
  165. }
  166. //-----------------------------------------------------------------------
  167. float PUBeamRender::getDeviation(void) const
  168. {
  169. return _deviation;
  170. }
  171. //-----------------------------------------------------------------------
  172. void PUBeamRender::setDeviation(float deviation)
  173. {
  174. _deviation = deviation;
  175. }
  176. //-----------------------------------------------------------------------
  177. size_t PUBeamRender::getNumberOfSegments(void) const
  178. {
  179. return _numberOfSegments;
  180. }
  181. //-----------------------------------------------------------------------
  182. void PUBeamRender::setNumberOfSegments(size_t numberOfSegments)
  183. {
  184. _numberOfSegments = numberOfSegments;
  185. }
  186. //-----------------------------------------------------------------------
  187. bool PUBeamRender::isJump(void) const
  188. {
  189. return _jump;
  190. }
  191. //-----------------------------------------------------------------------
  192. void PUBeamRender::setJump(bool jump)
  193. {
  194. _jump = jump;
  195. }
  196. //-----------------------------------------------------------------------
  197. PUBillboardChain::TexCoordDirection PUBeamRender::getTexCoordDirection(void) const
  198. {
  199. return _texCoordDirection;
  200. }
  201. //-----------------------------------------------------------------------
  202. void PUBeamRender::setTexCoordDirection(PUBillboardChain::TexCoordDirection texCoordDirection)
  203. {
  204. _texCoordDirection = texCoordDirection;
  205. }
  206. //-----------------------------------------------------------------------
  207. void PUBeamRender::prepare()
  208. {
  209. if (!_particleSystem)
  210. return;
  211. // Register itself to the technique
  212. if (_particleSystem)
  213. {
  214. // Although it is safe to assume that technique == mParentTechnique, use the mParentTechnique, because the mParentTechnique is
  215. // also used for unregistering.
  216. static_cast<PUParticleSystem3D *>(_particleSystem)->addListener(this);
  217. }
  218. _quota = _particleSystem->getParticleQuota();
  219. // Create BillboardChain
  220. std::stringstream ss;
  221. ss << this;
  222. _billboardChainName = "Beam" + ss.str();
  223. _billboardChain = new (std::nothrow) PUBillboardChain(_billboardChainName, _texFile);
  224. _billboardChain->setDynamic(true);
  225. _billboardChain->setNumberOfChains(_quota);
  226. _billboardChain->setMaxChainElements(_maxChainElements);
  227. _billboardChain->setTextureCoordDirection(_texCoordDirection);
  228. setUseVertexColours(_useVertexColours);
  229. _billboardChain->setOtherTextureCoordRange(0.0f, 1.0f);
  230. _billboardChain->setDepthTest(_depthTest);
  231. _billboardChain->setDepthWrite(_depthWrite);
  232. // Create number of VisualData objects
  233. for (size_t i = 0; i < _quota; i++)
  234. {
  235. for (size_t j = 0; j < _maxChainElements; j++)
  236. {
  237. PUBillboardChain::Element element;
  238. element = PUBillboardChain::Element(Vec3::ZERO, _rendererScale.x * static_cast<PUParticleSystem3D *>(_particleSystem)->getDefaultWidth(), 0.0f, Vec4::ONE, Quaternion::identity()); // V1.51
  239. _billboardChain->addChainElement(i, element);
  240. }
  241. PUParticle3DBeamVisualData* visualData = new (std::nothrow) PUParticle3DBeamVisualData(i, _billboardChain);
  242. for (size_t numDev = 0; numDev < _numberOfSegments; ++numDev)
  243. {
  244. // Initialise the positions
  245. visualData->half[numDev].setZero();
  246. visualData->destinationHalf[numDev].setZero();
  247. }
  248. _allVisualData.push_back(visualData); // Managed by this renderer
  249. _visualData.push_back(visualData); // Used to assign to a particle
  250. }
  251. }
  252. void PUBeamRender::unPrepare()
  253. {
  254. destroyAll();
  255. }
  256. void PUBeamRender::updateRender( PUParticle3D *particle, float deltaTime, bool /*firstParticle*/ )
  257. {
  258. if (!particle->visualData)
  259. return;
  260. PUParticle3DBeamVisualData* beamRendererVisualData = static_cast<PUParticle3DBeamVisualData*>(particle->visualData);
  261. beamRendererVisualData->timeSinceLastUpdate -= deltaTime;
  262. if (beamRendererVisualData->timeSinceLastUpdate < 0)
  263. {
  264. Vec3 end = particle->position - static_cast<PUParticleSystem3D *>(_particleSystem)->getDerivedPosition();
  265. Vec3 perpendicular;
  266. float divide = (float)_numberOfSegments + 1.0f;
  267. for (size_t numDev = 0; numDev < _numberOfSegments; ++numDev)
  268. {
  269. Vec3::cross(end, Vec3(CCRANDOM_MINUS1_1(), CCRANDOM_MINUS1_1(), CCRANDOM_MINUS1_1()), &perpendicular);
  270. perpendicular.normalize();
  271. beamRendererVisualData->destinationHalf[numDev] = (((float)numDev + 1.0f) / divide) * end
  272. + Vec3(_rendererScale.x * _deviation * perpendicular.x
  273. , _rendererScale.y * _deviation * perpendicular.y
  274. , _rendererScale.z * _deviation * perpendicular.z);
  275. }
  276. beamRendererVisualData->timeSinceLastUpdate += _updateInterval;
  277. }
  278. Vec3 diff;
  279. for (size_t numDev = 0; numDev < _numberOfSegments; ++numDev)
  280. {
  281. if (_jump)
  282. {
  283. beamRendererVisualData->half[numDev] = beamRendererVisualData->destinationHalf[numDev];
  284. }
  285. else
  286. {
  287. diff = beamRendererVisualData->destinationHalf[numDev] - beamRendererVisualData->half[numDev];
  288. beamRendererVisualData->half[numDev] = beamRendererVisualData->half[numDev] + deltaTime * diff;
  289. }
  290. }
  291. }
  292. //-----------------------------------------------------------------------
  293. void PUBeamRender::destroyAll(void)
  294. {
  295. if (!_particleSystem || !_billboardChain)
  296. return;
  297. // Remove the listener
  298. static_cast<PUParticleSystem3D *>(_particleSystem)->removeListener(this);
  299. // Delete the BillboardChain
  300. CC_SAFE_DELETE(_billboardChain);
  301. // Delete the visual data
  302. std::vector<PUParticle3DBeamVisualData*>::const_iterator it;
  303. std::vector<PUParticle3DBeamVisualData*>::const_iterator itEnd = _allVisualData.end();
  304. for (it = _allVisualData.begin(); it != itEnd; ++it)
  305. {
  306. delete *it;
  307. }
  308. _allVisualData.clear();
  309. _visualData.clear();
  310. }
  311. PUBeamRender* PUBeamRender::clone()
  312. {
  313. auto br = PUBeamRender::create(_texFile);
  314. copyAttributesTo(br);
  315. return br;
  316. }
  317. void PUBeamRender::copyAttributesTo(PUBeamRender *beamRender)
  318. {
  319. PURender::copyAttributesTo(beamRender);
  320. beamRender->setUseVertexColours(_useVertexColours);
  321. beamRender->setMaxChainElements(_maxChainElements);
  322. beamRender->setUpdateInterval(_updateInterval);
  323. beamRender->setDeviation(_deviation);
  324. beamRender->setNumberOfSegments(_numberOfSegments);
  325. beamRender->setJump(_jump);
  326. beamRender->setTexCoordDirection(_texCoordDirection);
  327. }
  328. NS_CC_END