CCParticle3DRender.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. /****************************************************************************
  2. Copyright (c) 2015-2016 Chukong Technologies Inc.
  3. Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
  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/CCParticle3DRender.h"
  23. #include "renderer/CCMeshCommand.h"
  24. #include "renderer/CCRenderer.h"
  25. #include "renderer/CCTextureCache.h"
  26. #include "renderer/CCGLProgramState.h"
  27. #include "renderer/CCGLProgramCache.h"
  28. #include "renderer/CCVertexIndexBuffer.h"
  29. #include "renderer/CCVertexAttribBinding.h"
  30. #include "base/CCDirector.h"
  31. #include "3d/CCSprite3D.h"
  32. #include "2d/CCCamera.h"
  33. NS_CC_BEGIN
  34. Particle3DQuadRender::Particle3DQuadRender()
  35. : _meshCommand(nullptr)
  36. , _texture(nullptr)
  37. , _glProgramState(nullptr)
  38. , _indexBuffer(nullptr)
  39. , _vertexBuffer(nullptr)
  40. , _texFile("")
  41. {
  42. }
  43. Particle3DQuadRender::~Particle3DQuadRender()
  44. {
  45. CC_SAFE_DELETE(_meshCommand);
  46. //CC_SAFE_RELEASE(_texture);
  47. CC_SAFE_RELEASE(_glProgramState);
  48. CC_SAFE_RELEASE(_vertexBuffer);
  49. CC_SAFE_RELEASE(_indexBuffer);
  50. }
  51. Particle3DQuadRender* Particle3DQuadRender::create(const std::string& texFile)
  52. {
  53. auto ret = new (std::nothrow)Particle3DQuadRender();
  54. if (ret && ret->initQuadRender(texFile))
  55. {
  56. ret->_texFile = texFile;
  57. ret->autorelease();
  58. }
  59. else
  60. {
  61. CC_SAFE_DELETE(ret);
  62. }
  63. return ret;
  64. }
  65. void Particle3DQuadRender::render(Renderer* renderer, const Mat4 &transform, ParticleSystem3D* particleSystem)
  66. {
  67. //batch and generate draw
  68. const ParticlePool &particlePool = particleSystem->getParticlePool();
  69. if (!_isVisible || particlePool.empty())
  70. return;
  71. if (_vertexBuffer == nullptr){
  72. GLsizei stride = sizeof(Particle3DQuadRender::posuvcolor);
  73. _vertexBuffer = VertexBuffer::create(stride, 4 * particleSystem->getParticleQuota());
  74. if (_vertexBuffer == nullptr)
  75. {
  76. CCLOG("Particle3DQuadRender::render create vertex buffer failed");
  77. return;
  78. }
  79. _vertexBuffer->retain();
  80. }
  81. if (_indexBuffer == nullptr){
  82. _indexBuffer = IndexBuffer::create(IndexBuffer::IndexType::INDEX_TYPE_SHORT_16, 6 * particleSystem->getParticleQuota());
  83. if (_indexBuffer == nullptr)
  84. {
  85. CCLOG("Particle3DQuadRender::render create index buffer failed");
  86. return;
  87. }
  88. _indexBuffer->retain();
  89. }
  90. ParticlePool::PoolList activeParticleList = particlePool.getActiveDataList();
  91. if (_posuvcolors.size() < activeParticleList.size() * 4)
  92. {
  93. _posuvcolors.resize(activeParticleList.size() * 4);
  94. _indexData.resize(activeParticleList.size() * 6);
  95. }
  96. auto camera = Camera::getVisitingCamera();
  97. auto cameraMat = camera->getNodeToWorldTransform();
  98. const Mat4 &viewMat = cameraMat.getInversed();
  99. Mat4 pRotMat;
  100. Vec3 right(cameraMat.m[0], cameraMat.m[1], cameraMat.m[2]);
  101. Vec3 up(cameraMat.m[4], cameraMat.m[5], cameraMat.m[6]);
  102. Vec3 backward(cameraMat.m[8], cameraMat.m[9], cameraMat.m[10]);
  103. Vec3 position; //particle position
  104. int vertexindex = 0;
  105. int index = 0;
  106. for (auto iter : activeParticleList)
  107. {
  108. auto particle = iter;
  109. Vec3 halfwidth = particle->width * 0.5f * right;
  110. Vec3 halfheight = particle->height * 0.5f * up;
  111. //transform.transformPoint(particle->position, &position);
  112. position = particle->position;
  113. _posuvcolors[vertexindex].position = (position + (- halfwidth - halfheight));
  114. _posuvcolors[vertexindex].color = particle->color;
  115. _posuvcolors[vertexindex].uv.set(particle->lb_uv);
  116. _posuvcolors[vertexindex + 1].position = (position + (halfwidth - halfheight));
  117. _posuvcolors[vertexindex + 1].color = particle->color;
  118. _posuvcolors[vertexindex + 1].uv.set(particle->rt_uv.x, particle->lb_uv.y);
  119. _posuvcolors[vertexindex + 2].position = (position + (- halfwidth + halfheight));
  120. _posuvcolors[vertexindex + 2].color = particle->color;
  121. _posuvcolors[vertexindex + 2].uv.set(particle->lb_uv.x, particle->rt_uv.y);
  122. _posuvcolors[vertexindex + 3].position = (position + (halfwidth + halfheight));
  123. _posuvcolors[vertexindex + 3].color = particle->color;
  124. _posuvcolors[vertexindex + 3].uv.set(particle->rt_uv);
  125. _indexData[index] = vertexindex;
  126. _indexData[index + 1] = vertexindex + 1;
  127. _indexData[index + 2] = vertexindex + 3;
  128. _indexData[index + 3] = vertexindex;
  129. _indexData[index + 4] = vertexindex + 3;
  130. _indexData[index + 5] = vertexindex + 2;
  131. index += 6;
  132. vertexindex += 4;
  133. }
  134. _posuvcolors.erase(_posuvcolors.begin() + vertexindex, _posuvcolors.end());
  135. _indexData.erase(_indexData.begin() + index, _indexData.end());
  136. _vertexBuffer->updateVertices(&_posuvcolors[0], vertexindex/* * sizeof(_posuvcolors[0])*/, 0);
  137. _indexBuffer->updateIndices(&_indexData[0], index/* * sizeof(unsigned short)*/, 0);
  138. GLuint texId = (_texture ? _texture->getName() : 0);
  139. float depthZ = -(viewMat.m[2] * transform.m[12] + viewMat.m[6] * transform.m[13] + viewMat.m[10] * transform.m[14] + viewMat.m[14]);
  140. _meshCommand->init(
  141. depthZ,
  142. texId,
  143. _glProgramState,
  144. _stateBlock,
  145. _vertexBuffer->getVBO(),
  146. _indexBuffer->getVBO(),
  147. GL_TRIANGLES,
  148. GL_UNSIGNED_SHORT,
  149. index,
  150. transform,
  151. 0);
  152. _glProgramState->setUniformVec4("u_color", Vec4(1,1,1,1));
  153. renderer->addCommand(_meshCommand);
  154. }
  155. bool Particle3DQuadRender::initQuadRender( const std::string& texFile )
  156. {
  157. GLProgram* glProgram = GLProgramCache::getInstance()->getGLProgram(GLProgram::SHADER_3D_PARTICLE_COLOR);
  158. if (!texFile.empty())
  159. {
  160. auto tex = Director::getInstance()->getTextureCache()->addImage(texFile);
  161. if (tex)
  162. {
  163. _texture = tex;
  164. glProgram = GLProgramCache::getInstance()->getGLProgram(GLProgram::SHADER_3D_PARTICLE_TEXTURE);
  165. }
  166. else
  167. _texture = nullptr;
  168. }
  169. auto glProgramState = GLProgramState::create(glProgram);
  170. glProgramState->retain();
  171. GLsizei stride = sizeof(Particle3DQuadRender::posuvcolor);
  172. glProgramState->setVertexAttribPointer(s_attributeNames[GLProgram::VERTEX_ATTRIB_POSITION], 3, GL_FLOAT, GL_FALSE, stride, (GLvoid*)offsetof(posuvcolor, position));
  173. glProgramState->setVertexAttribPointer(s_attributeNames[GLProgram::VERTEX_ATTRIB_TEX_COORD], 2, GL_FLOAT, GL_FALSE, stride, (GLvoid*)offsetof(posuvcolor, uv));
  174. glProgramState->setVertexAttribPointer(s_attributeNames[GLProgram::VERTEX_ATTRIB_COLOR], 4, GL_FLOAT, GL_FALSE, stride, (GLvoid*)offsetof(posuvcolor, color));
  175. _glProgramState = glProgramState;
  176. //ret->_vertexBuffer = VertexBuffer::create(stride, 4 * 10000);
  177. //ret->_vertexBuffer->retain();
  178. //ret->_indexBuffer = IndexBuffer::create(IndexBuffer::IndexType::INDEX_TYPE_SHORT_16, 6 * 10000);
  179. //ret->_indexBuffer->retain();
  180. _meshCommand = new (std::nothrow) MeshCommand();
  181. _meshCommand->setSkipBatching(true);
  182. _meshCommand->setTransparent(true);
  183. _stateBlock->setDepthTest(_depthTest);
  184. _stateBlock->setDepthWrite(_depthWrite);
  185. _stateBlock->setCullFace(true);
  186. _stateBlock->setCullFaceSide(RenderState::CULL_FACE_SIDE_BACK);
  187. return true;
  188. }
  189. void Particle3DQuadRender::reset()
  190. {
  191. this->initQuadRender(_texFile);
  192. }
  193. //////////////////////////////////////////////////////////////////////////////
  194. Particle3DModelRender::Particle3DModelRender()
  195. : _spriteSize(Vec3::ONE)
  196. {
  197. }
  198. Particle3DModelRender::~Particle3DModelRender()
  199. {
  200. for (auto iter : _spriteList){
  201. iter->release();
  202. }
  203. }
  204. Particle3DModelRender* Particle3DModelRender::create(const std::string& modelFile, const std::string &texFile)
  205. {
  206. auto ret = new (std::nothrow) Particle3DModelRender();
  207. ret->_modelFile = modelFile;
  208. ret->_texFile = texFile;
  209. return ret;
  210. }
  211. void Particle3DModelRender::render(Renderer* renderer, const Mat4 &transform, ParticleSystem3D* particleSystem)
  212. {
  213. if (!_isVisible)
  214. return;
  215. if (_spriteList.empty()){
  216. for (unsigned int i = 0; i < particleSystem->getParticleQuota(); ++i){
  217. Sprite3D *sprite = Sprite3D::create(_modelFile);
  218. if (sprite == nullptr)
  219. {
  220. CCLOG("failed to load file %s", _modelFile.c_str());
  221. continue;
  222. }
  223. sprite->setTexture(_texFile);
  224. sprite->retain();
  225. _spriteList.push_back(sprite);
  226. }
  227. if (!_spriteList.empty()){
  228. const AABB &aabb = _spriteList[0]->getAABB();
  229. Vec3 corners[8];
  230. aabb.getCorners(corners);
  231. _spriteSize = corners[3] - corners[6];
  232. }
  233. }
  234. const ParticlePool& particlePool = particleSystem->getParticlePool();
  235. ParticlePool::PoolList activeParticleList = particlePool.getActiveDataList();
  236. Mat4 mat;
  237. Mat4 rotMat;
  238. Mat4 sclMat;
  239. Quaternion q;
  240. transform.decompose(nullptr, &q, nullptr);
  241. unsigned int index = 0;
  242. for (auto iter : activeParticleList)
  243. {
  244. auto particle = iter;
  245. Mat4::createRotation(q * particle->orientation, &rotMat);
  246. sclMat.m[0] = particle->width / _spriteSize.x;
  247. sclMat.m[5] = particle->height / _spriteSize.y;
  248. sclMat.m[10] = particle->depth / _spriteSize.z;
  249. mat = rotMat * sclMat;
  250. mat.m[12] = particle->position.x;
  251. mat.m[13] = particle->position.y;
  252. mat.m[14] = particle->position.z;
  253. _spriteList[index++]->draw(renderer, mat, 0);
  254. }
  255. }
  256. void Particle3DModelRender::reset()
  257. {
  258. for (auto iter : _spriteList){
  259. iter->release();
  260. }
  261. _spriteList.clear();
  262. }
  263. // MARK: Particle3DRender
  264. Particle3DRender::Particle3DRender()
  265. : _particleSystem(nullptr)
  266. , _isVisible(true)
  267. , _rendererScale(Vec3::ONE)
  268. , _depthTest(true)
  269. , _depthWrite(false)
  270. {
  271. _stateBlock = RenderState::StateBlock::create();
  272. _stateBlock->retain();
  273. _stateBlock->setCullFace(false);
  274. _stateBlock->setCullFaceSide(RenderState::CULL_FACE_SIDE_BACK);
  275. _stateBlock->setDepthTest(false);
  276. _stateBlock->setDepthWrite(false);
  277. _stateBlock->setBlend(true);
  278. };
  279. Particle3DRender::~Particle3DRender()
  280. {
  281. _stateBlock->release();
  282. }
  283. void Particle3DRender::copyAttributesTo (Particle3DRender *render)
  284. {
  285. CC_SAFE_RELEASE(render->_stateBlock);
  286. render->_stateBlock = _stateBlock;
  287. CC_SAFE_RETAIN(render->_stateBlock);
  288. render->_isVisible = _isVisible;
  289. render->_rendererScale = _rendererScale;
  290. render->_depthTest = _depthTest;
  291. render->_depthWrite = _depthWrite;
  292. }
  293. void Particle3DRender::notifyStart()
  294. {
  295. setVisible(true);
  296. }
  297. void Particle3DRender::notifyStop()
  298. {
  299. setVisible(false);
  300. }
  301. void Particle3DRender::notifyRescaled( const Vec3& scale )
  302. {
  303. _rendererScale = scale;
  304. }
  305. void Particle3DRender::setDepthTest( bool isDepthTest )
  306. {
  307. _depthTest = isDepthTest;
  308. _stateBlock->setDepthTest(_depthTest);
  309. }
  310. void Particle3DRender::setDepthWrite( bool isDepthWrite )
  311. {
  312. _depthWrite = isDepthWrite;
  313. _stateBlock->setDepthWrite(_depthWrite);
  314. }
  315. void Particle3DRender::setBlendFunc(const BlendFunc &blendFunc)
  316. {
  317. _stateBlock->setBlendFunc(blendFunc);
  318. }
  319. NS_CC_END