CCPURender.cpp 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981
  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/CCPURender.h"
  23. #include "extensions/Particle3D/PU/CCPUParticleSystem3D.h"
  24. #include "extensions/Particle3D/PU/CCPUUtil.h"
  25. #include "renderer/CCMeshCommand.h"
  26. #include "renderer/CCRenderer.h"
  27. #include "renderer/CCTextureCache.h"
  28. #include "renderer/CCGLProgramState.h"
  29. #include "renderer/CCGLProgramCache.h"
  30. #include "renderer/CCVertexIndexBuffer.h"
  31. #include "renderer/CCVertexAttribBinding.h"
  32. #include "base/CCDirector.h"
  33. #include "3d/CCSprite3D.h"
  34. #include "3d/CCMesh.h"
  35. #include "2d/CCCamera.h"
  36. NS_CC_BEGIN
  37. void PURender::updateRender(PUParticle3D* /*particle*/, float /*deltaTime*/, bool /*firstParticle*/)
  38. {}
  39. void PURender::copyAttributesTo( PURender *render )
  40. {
  41. Particle3DRender::copyAttributesTo(render);
  42. render->_renderType = _renderType;
  43. }
  44. //static bool compareParticle3D(PUParticle3D* left, PUParticle3D* right)
  45. //{
  46. // return left->depthInView > right->depthInView;
  47. //}
  48. PUParticle3DQuadRender* PUParticle3DQuadRender::create(const std::string& texFile)
  49. {
  50. auto ret = new (std::nothrow) PUParticle3DQuadRender();
  51. if (ret && ret->initRender(texFile))
  52. {
  53. ret->_texFile = texFile;
  54. ret->autorelease();
  55. }
  56. else
  57. {
  58. CC_SAFE_DELETE(ret);
  59. }
  60. return ret;
  61. }
  62. void PUParticle3DQuadRender::render(Renderer* renderer, const Mat4 &transform, ParticleSystem3D* particleSystem)
  63. {
  64. //batch and generate draw
  65. const ParticlePool &particlePool = particleSystem->getParticlePool();
  66. if (!_isVisible || particlePool.empty())
  67. return;
  68. if (_vertexBuffer == nullptr){
  69. GLsizei stride = sizeof(VertexInfo);
  70. _vertexBuffer = VertexBuffer::create(stride, 4 * particleSystem->getParticleQuota());
  71. if (_vertexBuffer == nullptr)
  72. {
  73. CCLOG("PUParticle3DQuadRender::render create vertex buffer failed");
  74. return;
  75. }
  76. _vertexBuffer->retain();
  77. }
  78. if (_indexBuffer == nullptr){
  79. _indexBuffer = IndexBuffer::create(IndexBuffer::IndexType::INDEX_TYPE_SHORT_16, 6 * particleSystem->getParticleQuota());
  80. if (_indexBuffer == nullptr)
  81. {
  82. CCLOG("PUParticle3DQuadRender::render create index buffer failed");
  83. return;
  84. }
  85. _indexBuffer->retain();
  86. }
  87. const ParticlePool::PoolList &activeParticleList = particlePool.getActiveDataList();
  88. if (_vertices.size() < activeParticleList.size() * 4)
  89. {
  90. _vertices.resize(activeParticleList.size() * 4);
  91. _indices.resize(activeParticleList.size() * 6);
  92. }
  93. auto camera = Camera::getVisitingCamera();
  94. auto cameraMat = camera->getNodeToWorldTransform();
  95. //for (auto iter : activeParticleList){
  96. // iter->depthInView = -(viewMat.m[2] * iter->positionInWorld.x + viewMat.m[6] * iter->positionInWorld.y + viewMat.m[10] * iter->positionInWorld.z + viewMat.m[14]);
  97. //}
  98. //std::sort(activeParticleList.begin(), activeParticleList.end(), compareParticle3D);
  99. Vec3 right(cameraMat.m[0], cameraMat.m[1], cameraMat.m[2]);
  100. Vec3 up(cameraMat.m[4], cameraMat.m[5], cameraMat.m[6]);
  101. Vec3 backward(cameraMat.m[8], cameraMat.m[9], cameraMat.m[10]);
  102. Mat4 pRotMat;
  103. Vec3 position; //particle position
  104. int vertexindex = 0;
  105. int index = 0;
  106. int offsetX,offsetY;
  107. getOriginOffset(offsetX, offsetY);
  108. if (_type == PERPENDICULAR_COMMON){
  109. up = _commonUp;
  110. up.normalize();
  111. Vec3::cross(up, _commonDir, &right);
  112. right.normalize();
  113. backward = _commonDir;
  114. }else if (_type == ORIENTED_COMMON){
  115. up = _commonDir;
  116. up.normalize();
  117. Vec3::cross(up, backward, &right);
  118. right.normalize();
  119. }
  120. for (auto iter : activeParticleList)
  121. {
  122. auto particle = static_cast<PUParticle3D *>(iter);
  123. determineUVCoords(particle);
  124. if (_type == ORIENTED_SELF){
  125. Vec3 direction = particle->direction;
  126. //transform.transformVector(particle->direction, &direction);
  127. up = direction;
  128. up.normalize();
  129. Vec3::cross(direction, backward, &right);
  130. right.normalize();
  131. }else if (_type == PERPENDICULAR_SELF){
  132. Vec3 direction = particle->direction;
  133. //transform.transformVector(particle->direction, &direction);
  134. direction.normalize();
  135. //up = PUUtil::perpendicular(direction);
  136. //up.normalize();
  137. Vec3::cross(_commonUp, direction, &right);
  138. right.normalize();
  139. Vec3::cross(direction, right, &up);
  140. up.normalize();
  141. backward = direction;
  142. }else if (_type == ORIENTED_SHAPE){
  143. up.set(particle->orientation.x, particle->orientation.y, particle->orientation.z);
  144. up.normalize();
  145. Vec3::cross(up, backward, &right);
  146. right.normalize();
  147. }
  148. Vec3 halfwidth = particle->width * 0.5f * right;
  149. Vec3 halfheight = particle->height * 0.5f * up;
  150. Vec3 offset = halfwidth * offsetX + halfheight * offsetY;
  151. //transform.transformPoint(particle->position, &position);
  152. position = particle->position;
  153. if (_rotateType == TEXTURE_COORDS){
  154. float costheta = cosf(-particle->zRotation);
  155. float sintheta = sinf(-particle->zRotation);
  156. Vec2 texOffset = 0.5f * (particle->lb_uv + particle->rt_uv);
  157. Vec2 val;
  158. val.set((particle->lb_uv.x - texOffset.x), (particle->lb_uv.y - texOffset.y));
  159. val.set(val.x * costheta - val.y * sintheta, val.x * sintheta + val.y * costheta);
  160. fillVertex(vertexindex, (position + (-halfwidth - halfheight + offset)), particle->color, val + texOffset);
  161. val.set(particle->rt_uv.x - texOffset.x, particle->lb_uv.y - texOffset.y);
  162. val.set(val.x * costheta - val.y * sintheta, val.x * sintheta + val.y * costheta);
  163. fillVertex(vertexindex + 1, (position + (halfwidth - halfheight + offset)), particle->color, val + texOffset);
  164. val.set(particle->lb_uv.x - texOffset.x, particle->rt_uv.y - texOffset.y);
  165. val.set(val.x * costheta - val.y * sintheta, val.x * sintheta + val.y * costheta);
  166. fillVertex(vertexindex + 2, (position + (-halfwidth + halfheight + offset)), particle->color, val + texOffset);
  167. val.set(particle->rt_uv.x - texOffset.x, particle->rt_uv.y - texOffset.y);
  168. val.set(val.x * costheta - val.y * sintheta, val.x * sintheta + val.y * costheta);
  169. fillVertex(vertexindex + 3, (position + (halfwidth + halfheight + offset)), particle->color, val + texOffset);
  170. }else{
  171. Mat4::createRotation(backward, -particle->zRotation, &pRotMat);
  172. fillVertex(vertexindex , (position + pRotMat * (- halfwidth - halfheight + offset)), particle->color, particle->lb_uv);
  173. fillVertex(vertexindex + 1, (position + pRotMat * (halfwidth - halfheight + offset)), particle->color, Vec2(particle->rt_uv.x, particle->lb_uv.y));
  174. fillVertex(vertexindex + 2, (position + pRotMat * (-halfwidth + halfheight + offset)), particle->color, Vec2(particle->lb_uv.x, particle->rt_uv.y));
  175. fillVertex(vertexindex + 3, (position + pRotMat * (halfwidth + halfheight + offset)), particle->color, particle->rt_uv);
  176. }
  177. fillTriangle(index, vertexindex, vertexindex + 1, vertexindex + 3);
  178. fillTriangle(index + 3, vertexindex, vertexindex + 3, vertexindex + 2);
  179. //_posuvcolors[vertexindex].position = (position + (- halfwidth - halfheight + halfwidth * offsetX + halfheight * offsetY));
  180. //_posuvcolors[vertexindex].color = particle->color;
  181. //_posuvcolors[vertexindex].uv.set(val.x + texOffset.x, val.y + texOffset.y);
  182. //val.set(particle->rt_uv.x - texOffset.x, particle->lb_uv.y - texOffset.y);
  183. //val.set(val.x * costheta - val.y * sintheta, val.x * sintheta + val.y * costheta);
  184. //_posuvcolors[vertexindex + 1].position = (position + (halfwidth - halfheight + halfwidth * offsetX + halfheight * offsetY));
  185. //_posuvcolors[vertexindex + 1].color = particle->color;
  186. //_posuvcolors[vertexindex + 1].uv.set(val.x + texOffset.x, val.y + texOffset.y);
  187. //
  188. //val.set(particle->lb_uv.x - texOffset.x, particle->rt_uv.y - texOffset.y);
  189. //val.set(val.x * costheta - val.y * sintheta, val.x * sintheta + val.y * costheta);
  190. //_posuvcolors[vertexindex + 2].position = (position + (- halfwidth + halfheight + halfwidth * offsetX + halfheight * offsetY));
  191. //_posuvcolors[vertexindex + 2].color = particle->color;
  192. //_posuvcolors[vertexindex + 2].uv.set(val.x + texOffset.x, val.y + texOffset.y);
  193. //
  194. //val.set(particle->rt_uv.x - texOffset.x, particle->rt_uv.y - texOffset.y);
  195. //val.set(val.x * costheta - val.y * sintheta, val.x * sintheta + val.y * costheta);
  196. //_posuvcolors[vertexindex + 3].position = (position + (halfwidth + halfheight + halfwidth * offsetX + halfheight * offsetY));
  197. //_posuvcolors[vertexindex + 3].color = particle->color;
  198. //_posuvcolors[vertexindex + 3].uv.set(val.x + texOffset.x, val.y + texOffset.y);
  199. //
  200. //
  201. //_indexData[index] = vertexindex;
  202. //_indexData[index + 1] = vertexindex + 1;
  203. //_indexData[index + 2] = vertexindex + 3;
  204. //_indexData[index + 3] = vertexindex;
  205. //_indexData[index + 4] = vertexindex + 3;
  206. //_indexData[index + 5] = vertexindex + 2;
  207. index += 6;
  208. vertexindex += 4;
  209. }
  210. _vertices.erase(_vertices.begin() + vertexindex, _vertices.end());
  211. _indices.erase(_indices.begin() + index, _indices.end());
  212. if (!_vertices.empty() && !_indices.empty()){
  213. _vertexBuffer->updateVertices(&_vertices[0], vertexindex/* * sizeof(_posuvcolors[0])*/, 0);
  214. _indexBuffer->updateIndices(&_indices[0], index/* * sizeof(unsigned short)*/, 0);
  215. _stateBlock->setBlendFunc(particleSystem->getBlendFunc());
  216. GLuint texId = (_texture ? _texture->getName() : 0);
  217. _meshCommand->init(0,
  218. texId,
  219. _glProgramState,
  220. _stateBlock,
  221. _vertexBuffer->getVBO(),
  222. _indexBuffer->getVBO(),
  223. GL_TRIANGLES,
  224. GL_UNSIGNED_SHORT,
  225. index,
  226. transform,
  227. Node::FLAGS_RENDER_AS_3D);
  228. _meshCommand->setSkipBatching(true);
  229. _meshCommand->setTransparent(true);
  230. _glProgramState->setUniformVec4("u_color", Vec4(1,1,1,1));
  231. renderer->addCommand(_meshCommand);
  232. }
  233. }
  234. PUParticle3DQuadRender::PUParticle3DQuadRender()
  235. : _type(POINT)
  236. , _origin(CENTER)
  237. , _rotateType(TEXTURE_COORDS)
  238. , _commonDir(Vec3(0.0f, 0.0f, 1.0f))
  239. , _commonUp(Vec3(0.0f, 1.0f, 0.0f))
  240. , _textureCoordsRows(1)
  241. , _textureCoordsColumns(1)
  242. , _textureCoordsRowStep(1.0f)
  243. , _textureCoordsColStep(1.0f)
  244. {
  245. autoRotate = false;
  246. }
  247. PUParticle3DQuadRender::~PUParticle3DQuadRender()
  248. {
  249. }
  250. void PUParticle3DQuadRender::getOriginOffset( int &offsetX, int &offsetY )
  251. {
  252. switch (_origin)
  253. {
  254. case TOP_LEFT:
  255. {
  256. offsetX = 1;
  257. offsetY = -1;
  258. }
  259. break;
  260. case TOP_CENTER:
  261. {
  262. offsetX = 0;
  263. offsetY = -1;
  264. }
  265. break;
  266. case TOP_RIGHT:
  267. {
  268. offsetX = -1;
  269. offsetY = -1;
  270. }
  271. break;
  272. case CENTER_LEFT:
  273. {
  274. offsetX = 1;
  275. offsetY = 0;
  276. }
  277. break;
  278. case CENTER:
  279. {
  280. offsetX = 0;
  281. offsetY = 0;
  282. }
  283. break;
  284. case CENTER_RIGHT:
  285. {
  286. offsetX = -1;
  287. offsetY = 0;
  288. }
  289. break;
  290. case BOTTOM_LEFT:
  291. {
  292. offsetX = 1;
  293. offsetY = 1;
  294. }
  295. break;
  296. case BOTTOM_CENTER:
  297. {
  298. offsetX = 0;
  299. offsetY = 1;
  300. }
  301. break;
  302. case BOTTOM_RIGHT:
  303. {
  304. offsetX = -1;
  305. offsetY = 1;
  306. }
  307. break;
  308. }
  309. }
  310. unsigned short PUParticle3DQuadRender::getTextureCoordsRows() const
  311. {
  312. return _textureCoordsRows;
  313. }
  314. void PUParticle3DQuadRender::setTextureCoordsRows( unsigned short textureCoordsRows )
  315. {
  316. _textureCoordsRows = textureCoordsRows;
  317. _textureCoordsRowStep = 1.0f / (float)_textureCoordsRows;
  318. }
  319. unsigned short PUParticle3DQuadRender::getTextureCoordsColumns() const
  320. {
  321. return _textureCoordsColumns;
  322. }
  323. void PUParticle3DQuadRender::setTextureCoordsColumns( unsigned short textureCoordsColumns )
  324. {
  325. _textureCoordsColumns = textureCoordsColumns;
  326. _textureCoordsColStep = 1.0f / (float)_textureCoordsColumns;
  327. }
  328. unsigned int PUParticle3DQuadRender::getNumTextureCoords()
  329. {
  330. return _textureCoordsColumns * _textureCoordsRows;
  331. }
  332. void PUParticle3DQuadRender::determineUVCoords( PUParticle3D *particle )
  333. {
  334. if (_textureCoordsColumns == 1 && _textureCoordsRows == 1) return;
  335. unsigned short currentRow = particle->textureCoordsCurrent / _textureCoordsColumns;
  336. unsigned short currentCol = particle->textureCoordsCurrent - _textureCoordsColumns * currentRow;
  337. currentRow = _textureCoordsRows - currentRow - 1;
  338. particle->lb_uv.set(_textureCoordsColStep * currentCol, _textureCoordsRowStep * currentRow);
  339. particle->rt_uv = particle->lb_uv + Vec2(_textureCoordsColStep, _textureCoordsRowStep);
  340. }
  341. void PUParticle3DQuadRender::fillVertex( unsigned short index, const Vec3 &pos, const Vec4 &color, const Vec2 &uv )
  342. {
  343. _vertices[index].position = pos;
  344. _vertices[index].color = color;
  345. _vertices[index].uv = uv;
  346. }
  347. void PUParticle3DQuadRender::fillTriangle( unsigned short index, unsigned short v0, unsigned short v1, unsigned short v2 )
  348. {
  349. _indices[index] = v0;
  350. _indices[index + 1] = v1;
  351. _indices[index + 2] = v2;
  352. }
  353. void PUParticle3DQuadRender::setType( Type type )
  354. {
  355. _type = type;
  356. if (_type == PERPENDICULAR_COMMON || _type == PERPENDICULAR_SELF){
  357. _stateBlock->setCullFace(false);
  358. }else{
  359. _stateBlock->setCullFace(true);
  360. }
  361. }
  362. void PUParticle3DQuadRender::copyAttributesTo(PUParticle3DQuadRender *quadRender)
  363. {
  364. PURender::copyAttributesTo(quadRender);
  365. quadRender->_type = _type;
  366. quadRender->_origin = _origin;
  367. quadRender->_rotateType = _rotateType;
  368. quadRender->_commonDir = _commonDir;
  369. quadRender->_commonUp = _commonUp;
  370. quadRender->_textureCoordsRows = _textureCoordsRows;
  371. quadRender->_textureCoordsColumns = _textureCoordsColumns;
  372. quadRender->_textureCoordsRowStep = _textureCoordsRowStep;
  373. quadRender->_textureCoordsColStep = _textureCoordsColStep;
  374. }
  375. PUParticle3DQuadRender* PUParticle3DQuadRender::clone()
  376. {
  377. auto render = PUParticle3DQuadRender::create(_texFile);
  378. copyAttributesTo(render);
  379. return render;
  380. }
  381. PUParticle3DModelRender* PUParticle3DModelRender::create( const std::string& modelFile, const std::string &texFile /*= ""*/ )
  382. {
  383. auto ret = new (std::nothrow) PUParticle3DModelRender();
  384. ret->_modelFile = modelFile;
  385. ret->_texFile = texFile;
  386. return ret;
  387. }
  388. void PUParticle3DModelRender::render( Renderer* renderer, const Mat4 &transform, ParticleSystem3D* particleSystem )
  389. {
  390. if (!_isVisible)
  391. return;
  392. if (_spriteList.empty()){
  393. for (unsigned int i = 0; i < particleSystem->getParticleQuota(); ++i){
  394. Sprite3D *sprite = Sprite3D::create(_modelFile);
  395. if (sprite == nullptr)
  396. {
  397. CCLOG("failed to load file %s", _modelFile.c_str());
  398. continue;
  399. }
  400. sprite->setTexture(_texFile);
  401. sprite->setBlendFunc(particleSystem->getBlendFunc());
  402. sprite->setCullFaceEnabled(false);
  403. sprite->retain();
  404. _spriteList.push_back(sprite);
  405. }
  406. if (!_spriteList.empty()){
  407. const AABB &aabb = _spriteList[0]->getAABB();
  408. Vec3 corners[8];
  409. aabb.getCorners(corners);
  410. _spriteSize = corners[3] - corners[6];
  411. }else{
  412. _isVisible = false;
  413. return;
  414. }
  415. }
  416. const ParticlePool& particlePool = particleSystem->getParticlePool();
  417. ParticlePool::PoolList activeParticleList = particlePool.getActiveDataList();
  418. Mat4 mat;
  419. Mat4 rotMat;
  420. Mat4 sclMat;
  421. Quaternion q;
  422. transform.decompose(nullptr, &q, nullptr);
  423. unsigned int index = 0;
  424. for (auto iter : activeParticleList)
  425. {
  426. auto particle = static_cast<PUParticle3D *>(iter);
  427. Mat4::createRotation(q * particle->orientation, &rotMat);
  428. sclMat.m[0] = particle->width / _spriteSize.x;
  429. sclMat.m[5] = particle->height / _spriteSize.y;
  430. sclMat.m[10] = particle->depth / _spriteSize.z;
  431. mat = rotMat * sclMat;
  432. mat.m[12] = particle->position.x;
  433. mat.m[13] = particle->position.y;
  434. mat.m[14] = particle->position.z;
  435. if (_spriteList[index]->getCameraMask() != particleSystem->getCameraMask())
  436. _spriteList[index]->setCameraMask(particleSystem->getCameraMask());
  437. _spriteList[index]->setColor(Color3B(particle->color.x * 255, particle->color.y * 255, particle->color.z * 255));
  438. _spriteList[index]->setOpacity(particle->color.w * 255);
  439. _spriteList[index]->visit(renderer, mat, Node::FLAGS_DIRTY_MASK);
  440. ++index;
  441. }
  442. }
  443. PUParticle3DModelRender::PUParticle3DModelRender()
  444. {
  445. autoRotate = true;
  446. }
  447. PUParticle3DModelRender::~PUParticle3DModelRender()
  448. {
  449. for (auto iter : _spriteList){
  450. iter->release();
  451. }
  452. }
  453. void PUParticle3DModelRender::copyAttributesTo(PUParticle3DModelRender *render)
  454. {
  455. PURender::copyAttributesTo(render);
  456. }
  457. PUParticle3DModelRender* PUParticle3DModelRender::clone()
  458. {
  459. auto mr = PUParticle3DModelRender::create(_modelFile, _texFile);
  460. copyAttributesTo(mr);
  461. return mr;
  462. }
  463. void PUParticle3DModelRender::reset()
  464. {
  465. for (auto iter : _spriteList){
  466. iter->release();
  467. }
  468. _spriteList.clear();
  469. }
  470. PUParticle3DEntityRender::PUParticle3DEntityRender()
  471. : _meshCommand(nullptr)
  472. , _texture(nullptr)
  473. , _glProgramState(nullptr)
  474. , _indexBuffer(nullptr)
  475. , _vertexBuffer(nullptr)
  476. {
  477. _stateBlock = RenderState::StateBlock::create();
  478. CC_SAFE_RETAIN(_stateBlock);
  479. _stateBlock->setCullFace(false);
  480. _stateBlock->setCullFaceSide(RenderState::CULL_FACE_SIDE_BACK);
  481. _stateBlock->setDepthTest(false);
  482. _stateBlock->setDepthWrite(false);
  483. _stateBlock->setBlend(true);
  484. }
  485. PUParticle3DEntityRender::~PUParticle3DEntityRender()
  486. {
  487. CC_SAFE_DELETE(_meshCommand);
  488. CC_SAFE_RELEASE(_stateBlock);
  489. //CC_SAFE_RELEASE(_texture);
  490. CC_SAFE_RELEASE(_glProgramState);
  491. CC_SAFE_RELEASE(_vertexBuffer);
  492. CC_SAFE_RELEASE(_indexBuffer);
  493. }
  494. bool PUParticle3DEntityRender::initRender( const std::string &texFile )
  495. {
  496. GLProgram* glProgram = GLProgramCache::getInstance()->getGLProgram(GLProgram::SHADER_3D_PARTICLE_COLOR);
  497. if (!texFile.empty())
  498. {
  499. auto tex = Director::getInstance()->getTextureCache()->addImage(texFile);
  500. if (tex)
  501. {
  502. _texture = tex;
  503. glProgram = GLProgramCache::getInstance()->getGLProgram(GLProgram::SHADER_3D_PARTICLE_TEXTURE);
  504. }
  505. else
  506. _texture = nullptr;
  507. }
  508. auto glProgramState = GLProgramState::create(glProgram);
  509. glProgramState->retain();
  510. GLsizei stride = sizeof(VertexInfo);
  511. glProgramState->setVertexAttribPointer(s_attributeNames[GLProgram::VERTEX_ATTRIB_POSITION], 3, GL_FLOAT, GL_FALSE, stride, (GLvoid*)offsetof(VertexInfo, position));
  512. glProgramState->setVertexAttribPointer(s_attributeNames[GLProgram::VERTEX_ATTRIB_TEX_COORD], 2, GL_FLOAT, GL_FALSE, stride, (GLvoid*)offsetof(VertexInfo, uv));
  513. glProgramState->setVertexAttribPointer(s_attributeNames[GLProgram::VERTEX_ATTRIB_COLOR], 4, GL_FLOAT, GL_FALSE, stride, (GLvoid*)offsetof(VertexInfo, color));
  514. _glProgramState = glProgramState;
  515. _meshCommand = new (std::nothrow) MeshCommand();
  516. _meshCommand->setSkipBatching(true);
  517. _meshCommand->setTransparent(true);
  518. _stateBlock->setDepthTest(_depthTest);
  519. _stateBlock->setDepthWrite(_depthWrite);
  520. _stateBlock->setCullFaceSide(RenderState::CULL_FACE_SIDE_BACK);
  521. _stateBlock->setCullFace(true);
  522. return true;
  523. }
  524. void PUParticle3DEntityRender::copyAttributesTo(PUParticle3DEntityRender *render)
  525. {
  526. PURender::copyAttributesTo(render);
  527. }
  528. void PUParticle3DEntityRender::reset()
  529. {
  530. this->initRender(_texFile);
  531. }
  532. PUParticle3DBoxRender::PUParticle3DBoxRender()
  533. {
  534. autoRotate = false;
  535. }
  536. PUParticle3DBoxRender::~PUParticle3DBoxRender()
  537. {
  538. }
  539. PUParticle3DBoxRender* PUParticle3DBoxRender::create( const std::string &texFile )
  540. {
  541. auto ret = new (std::nothrow) PUParticle3DBoxRender();
  542. if (ret && ret->initRender(texFile))
  543. {
  544. ret->autorelease();
  545. }
  546. else
  547. {
  548. CC_SAFE_DELETE(ret);
  549. }
  550. return ret;
  551. }
  552. void PUParticle3DBoxRender::render( Renderer* renderer, const Mat4 &transform, ParticleSystem3D* particleSystem )
  553. {
  554. //batch and generate draw
  555. const ParticlePool &particlePool = particleSystem->getParticlePool();
  556. if (!_isVisible || particlePool.empty())
  557. return;
  558. auto camera = Camera::getVisitingCamera();
  559. auto cameraMat = camera->getNodeToWorldTransform();
  560. Vec3 backward(cameraMat.m[8], cameraMat.m[9], cameraMat.m[10]);
  561. if (_vertexBuffer == nullptr && _indexBuffer == nullptr){
  562. GLsizei stride = sizeof(VertexInfo);
  563. _vertexBuffer = VertexBuffer::create(stride, 8 * particleSystem->getParticleQuota());
  564. if (_vertexBuffer == nullptr)
  565. {
  566. CCLOG("PUParticle3DBoxRender::render create vertex buffer failed");
  567. return;
  568. }
  569. _vertexBuffer->retain();
  570. _vertices.resize(8 * particleSystem->getParticleQuota());
  571. _indexBuffer = IndexBuffer::create(IndexBuffer::IndexType::INDEX_TYPE_SHORT_16, 36 * particleSystem->getParticleQuota());
  572. if (_indexBuffer == nullptr)
  573. {
  574. CCLOG("PUParticle3DBoxRender::render create index buffer failed");
  575. return;
  576. }
  577. _indexBuffer->retain();
  578. _indices.resize(36 * particleSystem->getParticleQuota());
  579. reBuildIndices(particleSystem->getParticleQuota());
  580. }
  581. unsigned int vertexindex = 0;
  582. unsigned int index = 0;
  583. Mat4 texRot;
  584. Vec3 val;
  585. for (auto iter : particlePool.getActiveDataList())
  586. {
  587. auto particle = static_cast<PUParticle3D *>(iter);
  588. float halfHeight = particle->height * 0.5f;
  589. float halfWidth = particle->width * 0.5f;
  590. float halfDepth = particle->depth * 0.5f;
  591. Mat4::createRotation(backward, particle->zRotation, &texRot);
  592. val = texRot * Vec3(0.0f, 0.75f, 0.0);
  593. _vertices[vertexindex + 0].position = particle->position + Vec3(-halfWidth, -halfHeight, halfDepth);
  594. _vertices[vertexindex + 0].color = particle->color;
  595. _vertices[vertexindex + 0].uv.x = val.x; _vertices[vertexindex + 0].uv.y = val.y;
  596. val = texRot * Vec3(0.0f, 0.25f, 0.0);
  597. _vertices[vertexindex + 1].position = particle->position + Vec3(halfWidth, -halfHeight, halfDepth);
  598. _vertices[vertexindex + 1].color = particle->color;
  599. _vertices[vertexindex + 1].uv.x = val.x; _vertices[vertexindex + 1].uv.y = val.y;
  600. val = texRot * Vec3(0.5f, 0.25f, 0.0);
  601. _vertices[vertexindex + 2].position = particle->position + Vec3(halfWidth, halfHeight, halfDepth);
  602. _vertices[vertexindex + 2].color = particle->color;
  603. _vertices[vertexindex + 2].uv.x = val.x; _vertices[vertexindex + 2].uv.y = val.y;
  604. val = texRot * Vec3(0.5f, 0.75f, 0.0);
  605. _vertices[vertexindex + 3].position = particle->position + Vec3(-halfWidth, halfHeight, halfDepth);
  606. _vertices[vertexindex + 3].color = particle->color;
  607. _vertices[vertexindex + 3].uv.x = val.x; _vertices[vertexindex + 3].uv.y = val.y;
  608. val = texRot * Vec3(0.0f, 0.0f, 0.0);
  609. _vertices[vertexindex + 4].position = particle->position + Vec3(halfWidth, -halfHeight, -halfDepth);
  610. _vertices[vertexindex + 4].color = particle->color;
  611. _vertices[vertexindex + 4].uv.x = val.x; _vertices[vertexindex + 4].uv.y = val.y;
  612. val = texRot * Vec3(0.0f, 1.0f, 0.0);
  613. _vertices[vertexindex + 5].position = particle->position + Vec3(-halfWidth, -halfHeight, -halfDepth);
  614. _vertices[vertexindex + 5].color = particle->color;
  615. _vertices[vertexindex + 5].uv.x = val.x; _vertices[vertexindex + 5].uv.y = val.y;
  616. val = texRot * Vec3(0.5f, 1.0f, 0.0);
  617. _vertices[vertexindex + 6].position = particle->position + Vec3(-halfWidth, halfHeight, -halfDepth);
  618. _vertices[vertexindex + 6].color = particle->color;
  619. _vertices[vertexindex + 6].uv.x = val.x; _vertices[vertexindex + 6].uv.y = val.y;
  620. val = texRot * Vec3(0.5f, 0.0f, 0.0);
  621. _vertices[vertexindex + 7].position = particle->position + Vec3(halfWidth, halfHeight, -halfDepth);
  622. _vertices[vertexindex + 7].color = particle->color;
  623. _vertices[vertexindex + 7].uv.x = val.x; _vertices[vertexindex + 7].uv.y = val.y;
  624. vertexindex += 8;
  625. index += 36;
  626. }
  627. if (!_vertices.empty() && !_indices.empty()){
  628. _vertexBuffer->updateVertices(&_vertices[0], vertexindex/* * sizeof(_posuvcolors[0])*/, 0);
  629. _indexBuffer->updateIndices(&_indices[0], index/* * sizeof(unsigned short)*/, 0);
  630. GLuint texId = (_texture ? _texture->getName() : 0);
  631. _stateBlock->setBlendFunc(_particleSystem->getBlendFunc());
  632. _meshCommand->init(0,
  633. texId,
  634. _glProgramState,
  635. _stateBlock,
  636. _vertexBuffer->getVBO(),
  637. _indexBuffer->getVBO(),
  638. GL_TRIANGLES,
  639. GL_UNSIGNED_SHORT,
  640. index,
  641. transform,
  642. Node::FLAGS_RENDER_AS_3D);
  643. _meshCommand->setSkipBatching(true);
  644. _meshCommand->setTransparent(true);
  645. _glProgramState->setUniformVec4("u_color", Vec4(1,1,1,1));
  646. renderer->addCommand(_meshCommand);
  647. }
  648. }
  649. void PUParticle3DBoxRender::reBuildIndices(unsigned short count)
  650. {
  651. unsigned short vertexIndex = 0;
  652. for (unsigned short i = 0; i < 36 * count;){
  653. //front
  654. _indices[i++] = vertexIndex + 0;
  655. _indices[i++] = vertexIndex + 2;
  656. _indices[i++] = vertexIndex + 3;
  657. _indices[i++] = vertexIndex + 0;
  658. _indices[i++] = vertexIndex + 1;
  659. _indices[i++] = vertexIndex + 2;
  660. //right
  661. _indices[i++] = vertexIndex + 1;
  662. _indices[i++] = vertexIndex + 7;
  663. _indices[i++] = vertexIndex + 2;
  664. _indices[i++] = vertexIndex + 1;
  665. _indices[i++] = vertexIndex + 4;
  666. _indices[i++] = vertexIndex + 7;
  667. //back
  668. _indices[i++] = vertexIndex + 4;
  669. _indices[i++] = vertexIndex + 6;
  670. _indices[i++] = vertexIndex + 7;
  671. _indices[i++] = vertexIndex + 4;
  672. _indices[i++] = vertexIndex + 5;
  673. _indices[i++] = vertexIndex + 6;
  674. //left
  675. _indices[i++] = vertexIndex + 5;
  676. _indices[i++] = vertexIndex + 3;
  677. _indices[i++] = vertexIndex + 6;
  678. _indices[i++] = vertexIndex + 5;
  679. _indices[i++] = vertexIndex + 0;
  680. _indices[i++] = vertexIndex + 3;
  681. //top
  682. _indices[i++] = vertexIndex + 3;
  683. _indices[i++] = vertexIndex + 7;
  684. _indices[i++] = vertexIndex + 6;
  685. _indices[i++] = vertexIndex + 3;
  686. _indices[i++] = vertexIndex + 2;
  687. _indices[i++] = vertexIndex + 7;
  688. //bottom
  689. _indices[i++] = vertexIndex + 5;
  690. _indices[i++] = vertexIndex + 1;
  691. _indices[i++] = vertexIndex + 0;
  692. _indices[i++] = vertexIndex + 5;
  693. _indices[i++] = vertexIndex + 4;
  694. _indices[i++] = vertexIndex + 1;
  695. vertexIndex += 8;
  696. }
  697. }
  698. PUParticle3DBoxRender* PUParticle3DBoxRender::clone()
  699. {
  700. auto render = PUParticle3DBoxRender::create(_texFile);
  701. copyAttributesTo(render);
  702. return render;
  703. }
  704. PUSphereRender* PUSphereRender::create( const std::string &texFile)
  705. {
  706. auto ret = new (std::nothrow) PUSphereRender();
  707. if (ret && ret->initRender(texFile))
  708. {
  709. ret->autorelease();
  710. }
  711. else
  712. {
  713. CC_SAFE_DELETE(ret);
  714. }
  715. return ret;
  716. }
  717. void PUSphereRender::render( Renderer* renderer, const Mat4 &transform, ParticleSystem3D* particleSystem )
  718. {
  719. //batch and generate draw
  720. const ParticlePool &particlePool = particleSystem->getParticlePool();
  721. if (!_isVisible || particlePool.empty())
  722. return;
  723. auto camera = Camera::getVisitingCamera();
  724. auto cameraMat = camera->getNodeToWorldTransform();
  725. Vec3 backward(cameraMat.m[8], cameraMat.m[9], cameraMat.m[10]);
  726. unsigned int vertexCount = (_numberOfRings + 1) * (_numberOfSegments + 1);
  727. unsigned int indexCount = 6 * _numberOfRings * (_numberOfSegments + 1);
  728. if (_vertexBuffer == nullptr && _indexBuffer == nullptr){
  729. GLsizei stride = sizeof(VertexInfo);
  730. _vertexBuffer = VertexBuffer::create(stride, vertexCount * particleSystem->getParticleQuota());
  731. if (_vertexBuffer == nullptr)
  732. {
  733. CCLOG("PUSphereRender::render create vertex buffer failed");
  734. return;
  735. }
  736. _vertexBuffer->retain();
  737. _vertices.resize(vertexCount * particleSystem->getParticleQuota());
  738. _indexBuffer = IndexBuffer::create(IndexBuffer::IndexType::INDEX_TYPE_SHORT_16, indexCount * particleSystem->getParticleQuota());
  739. if (_indexBuffer == nullptr)
  740. {
  741. CCLOG("PUSphereRender::render create index buffer failed");
  742. return;
  743. }
  744. _indexBuffer->retain();
  745. _indices.resize(indexCount * particleSystem->getParticleQuota());
  746. buildBuffers(particleSystem->getParticleQuota());
  747. }
  748. unsigned int vertexindex = 0;
  749. unsigned int index = 0;
  750. Mat4 mat;
  751. Mat4 rotMat;
  752. Mat4 sclMat;
  753. Mat4 texRot;
  754. Vec3 val;
  755. for (auto iter : particlePool.getActiveDataList())
  756. {
  757. auto particle = static_cast<PUParticle3D *>(iter);
  758. float radius = particle->width * 0.5f;
  759. Mat4::createRotation(particle->orientation, &rotMat);
  760. Mat4::createScale(radius, radius, radius, &sclMat);
  761. Mat4::createRotation(backward, particle->zRotation, &texRot);
  762. mat = rotMat * sclMat;
  763. mat.m[12] = particle->position.x;
  764. mat.m[13] = particle->position.y;
  765. mat.m[14] = particle->position.z;
  766. for (unsigned int i = 0; i < vertexCount; ++i){
  767. val = texRot * Vec3(_vertexTemplate[vertexindex + i].uv.x, _vertexTemplate[vertexindex + i].uv.y, 0.0f);
  768. mat.transformPoint(_vertexTemplate[vertexindex + i].position, &_vertices[vertexindex + i].position);
  769. _vertices[vertexindex + i].color = particle->color;
  770. _vertices[vertexindex + i].uv.x = val.x; _vertices[vertexindex + i].uv.y = val.y;
  771. }
  772. vertexindex += vertexCount;
  773. index += indexCount;
  774. }
  775. if (!_vertices.empty() && !_indices.empty()){
  776. _vertexBuffer->updateVertices(&_vertices[0], vertexindex/* * sizeof(_posuvcolors[0])*/, 0);
  777. _indexBuffer->updateIndices(&_indices[0], index/* * sizeof(unsigned short)*/, 0);
  778. GLuint texId = (_texture ? _texture->getName() : 0);
  779. _stateBlock->setBlendFunc(particleSystem->getBlendFunc());
  780. _meshCommand->init(
  781. 0,
  782. texId,
  783. _glProgramState,
  784. _stateBlock,
  785. _vertexBuffer->getVBO(),
  786. _indexBuffer->getVBO(),
  787. GL_TRIANGLES,
  788. GL_UNSIGNED_SHORT,
  789. index,
  790. transform,
  791. Node::FLAGS_RENDER_AS_3D);
  792. _meshCommand->setSkipBatching(true);
  793. _meshCommand->setTransparent(true);
  794. _glProgramState->setUniformVec4("u_color", Vec4(1,1,1,1));
  795. renderer->addCommand(_meshCommand);
  796. }
  797. }
  798. void PUSphereRender::buildBuffers( unsigned short count )
  799. {
  800. float stepRingAngle = (M_PI / _numberOfRings);
  801. float stepSegmentAngle = (2.0 * M_PI / _numberOfSegments);
  802. unsigned short vertexIndex = 0;
  803. unsigned short index = 0;
  804. for (unsigned short i = 0; i < count; ++i){
  805. for(unsigned int ring = 0; ring <= _numberOfRings; ring++)
  806. {
  807. float r0 = sinf (ring * stepRingAngle);
  808. float y0 = cosf (ring * stepRingAngle);
  809. for(unsigned int segment = 0; segment <= _numberOfSegments; segment++)
  810. {
  811. VertexInfo vi;
  812. float x0 = r0 * sinf(segment * stepSegmentAngle);
  813. float z0 = r0 * cosf(segment * stepSegmentAngle);
  814. // Vertex
  815. vi.position.set(x0, y0, z0);
  816. // Colour
  817. vi.color = Vec4::ONE;
  818. // Texture Coordinates
  819. vi.uv.x = (float) segment / (float) _numberOfSegments;
  820. vi.uv.y = 1.0f - (float) ring / (float) _numberOfRings;
  821. if (ring != _numberOfRings)
  822. {
  823. // each vertex (except the last) has six indices pointing to it
  824. _indices[index++] = vertexIndex + _numberOfSegments + 1;
  825. _indices[index++] = vertexIndex;
  826. _indices[index++] = vertexIndex + _numberOfSegments;
  827. _indices[index++] = vertexIndex + _numberOfSegments + 1;
  828. _indices[index++] = vertexIndex + 1;
  829. _indices[index++] = vertexIndex;
  830. }
  831. ++vertexIndex;
  832. _vertexTemplate.push_back(vi);
  833. }
  834. }
  835. }
  836. }
  837. PUSphereRender::PUSphereRender()
  838. : _numberOfRings(16)
  839. , _numberOfSegments(16)
  840. {
  841. autoRotate = false;
  842. }
  843. PUSphereRender::~PUSphereRender()
  844. {
  845. }
  846. void PUSphereRender::copyAttributesTo(PUSphereRender *sphereRender)
  847. {
  848. PURender::copyAttributesTo(sphereRender);
  849. sphereRender->_numberOfRings = _numberOfRings;
  850. sphereRender->_numberOfSegments = _numberOfSegments;
  851. }
  852. PUSphereRender* PUSphereRender::clone()
  853. {
  854. auto render = PUSphereRender::create(_texFile);
  855. copyAttributesTo(render);
  856. return render;
  857. }
  858. NS_CC_END