CCParticleSystem3D.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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. #ifndef __CC_PARTICLE_SYSTEM_3D_H__
  22. #define __CC_PARTICLE_SYSTEM_3D_H__
  23. #include "2d/CCNode.h"
  24. #include "math/CCMath.h"
  25. #include <vector>
  26. #include <map>
  27. #include <list>
  28. NS_CC_BEGIN
  29. /**
  30. * 3d particle system
  31. */
  32. class Particle3DEmitter;
  33. class Particle3DAffector;
  34. class Particle3DRender;
  35. struct CC_DLL Particle3D
  36. {
  37. Particle3D();
  38. virtual ~Particle3D();
  39. // property of particles
  40. Vec3 position; // position
  41. Quaternion orientation;// Orientation of the particle.
  42. Vec4 color; // particle color
  43. Vec2 lb_uv; // left bottom uv
  44. Vec2 rt_uv; // right top uv
  45. float width;//Own width
  46. float height;//Own height
  47. float depth;//Own depth
  48. //user defined property
  49. std::unordered_map<std::string, void*> userDefs;
  50. };
  51. template<typename T>
  52. class CC_DLL DataPool
  53. {
  54. public:
  55. typedef typename std::list<T*> PoolList;
  56. typedef typename std::list<T*>::iterator PoolIterator;
  57. DataPool(){};
  58. ~DataPool(){};
  59. T* createData(){
  60. if (_locked.empty()) return nullptr;
  61. T* p = _locked.front();
  62. //_released.push_back(p);
  63. //_locked.erase(_locked.begin());
  64. _released.splice(_released.end(), _locked, _locked.begin());
  65. return p;
  66. }
  67. void lockLatestData(){
  68. _locked.push_back(*_releasedIter);
  69. _releasedIter = _released.erase(_releasedIter);
  70. if (_releasedIter != _released.begin() && _releasedIter != _released.end())
  71. {
  72. --_releasedIter;
  73. }
  74. }
  75. void lockData(T *data){
  76. PoolIterator tempIter = _releasedIter;
  77. T *ptr = getFirst();
  78. while (ptr)
  79. {
  80. if (ptr == data) break;
  81. ptr = getNext();
  82. }
  83. if (ptr)
  84. lockLatestData();
  85. _releasedIter = tempIter;
  86. }
  87. void lockAllDatas(){
  88. _locked.splice(_locked.end(), _released);
  89. //_locked.insert(_locked.end(), _released.begin(), _released.end());
  90. //_released.clear();
  91. _releasedIter = _released.begin();
  92. }
  93. T* getFirst(){
  94. _releasedIter = _released.begin();
  95. if (_releasedIter == _released.end()) return nullptr;
  96. return *_releasedIter;
  97. }
  98. T* getNext(){
  99. if (_releasedIter == _released.end()) return nullptr;
  100. ++_releasedIter;
  101. if (_releasedIter == _released.end()) return nullptr;
  102. return *_releasedIter;
  103. }
  104. const PoolList& getActiveDataList() const { return _released; };
  105. const PoolList& getUnActiveDataList() const { return _locked; };
  106. void addData(T* data){
  107. _locked.push_back(data);
  108. }
  109. bool empty() const { return _released.empty(); };
  110. void removeAllDatas(){
  111. lockAllDatas();
  112. for (auto iter : _locked){
  113. delete iter;
  114. }
  115. _locked.clear();
  116. }
  117. private:
  118. PoolIterator _releasedIter;
  119. PoolList _released;
  120. PoolList _locked;
  121. };
  122. typedef DataPool<Particle3D> ParticlePool;
  123. class CC_DLL ParticleSystem3D : public Node, public BlendProtocol
  124. {
  125. public:
  126. enum class State
  127. {
  128. STOP,
  129. RUNNING,
  130. PAUSE,
  131. };
  132. /**
  133. * override function
  134. */
  135. virtual void update(float delta) override;
  136. /**
  137. * override function
  138. */
  139. virtual void draw(Renderer *renderer, const Mat4 &transform, uint32_t flags) override;
  140. /**
  141. * override function
  142. */
  143. virtual void setBlendFunc(const BlendFunc &blendFunc) override;
  144. /**
  145. * override function
  146. */
  147. virtual const BlendFunc &getBlendFunc() const override;
  148. /**
  149. * particle system play control
  150. */
  151. virtual void startParticleSystem();
  152. /**
  153. * stop particle
  154. */
  155. virtual void stopParticleSystem();
  156. /**
  157. * pause particle
  158. */
  159. virtual void pauseParticleSystem();
  160. /**
  161. * resume particle
  162. */
  163. virtual void resumeParticleSystem();
  164. /**
  165. * set emitter for particle system, can set your own particle emitter
  166. */
  167. void setEmitter(Particle3DEmitter* emitter);
  168. /**
  169. * set particle render, can set your own particle render
  170. */
  171. void setRender(Particle3DRender* render);
  172. /**
  173. * return particle render
  174. */
  175. Particle3DRender* getRender(){ return _render; }
  176. /**
  177. * add particle affector
  178. */
  179. void addAffector(Particle3DAffector* affector);
  180. /**
  181. * remove affector by index
  182. */
  183. void removeAffector(int index);
  184. /**
  185. * remove all particle affector
  186. */
  187. void removeAllAffector();
  188. /**
  189. * get particle quota
  190. */
  191. unsigned int getParticleQuota() const;
  192. /**
  193. * set particle quota
  194. */
  195. void setParticleQuota(unsigned int quota);
  196. /**
  197. * get particle affector by index
  198. */
  199. Particle3DAffector* getAffector(int index);
  200. /**
  201. * get particle pool
  202. */
  203. const ParticlePool& getParticlePool()
  204. {
  205. return _particlePool;
  206. }
  207. /**
  208. * get alive particles count
  209. */
  210. virtual int getAliveParticleCount() const
  211. {
  212. return 0;
  213. }
  214. /**
  215. * get particle playing state
  216. */
  217. State getState() const { return _state; }
  218. bool isKeepLocal(void) const { return _keepLocal; }
  219. void setKeepLocal(bool keepLocal);
  220. /**
  221. *Enables or disables the system.
  222. */
  223. void setEnabled (bool enabled);
  224. /**
  225. * is enabled
  226. */
  227. bool isEnabled(void) const { return _isEnabled; }
  228. CC_CONSTRUCTOR_ACCESS:
  229. ParticleSystem3D();
  230. virtual ~ParticleSystem3D();
  231. protected:
  232. State _state;
  233. Particle3DEmitter* _emitter;
  234. std::vector<Particle3DAffector*> _affectors;
  235. Particle3DRender* _render;
  236. //particles
  237. ParticlePool _particlePool;
  238. int _aliveParticlesCnt;
  239. unsigned int _particleQuota;
  240. BlendFunc _blend;
  241. bool _keepLocal;
  242. bool _isEnabled;
  243. };
  244. NS_CC_END
  245. #endif