ZMLParticleSystem.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. //
  2. // ZMLParticleSystem.h
  3. // Billiards
  4. //
  5. // Created by zhu on 2019/2/15.
  6. //
  7. #ifndef __RUZMLParticleSystem_h
  8. #define __RUZMLParticleSystem_h
  9. #include <stdio.h>
  10. #include "cocos2d.h"
  11. #include "ParticleSystemModule.h"
  12. #include "RUDefine.h"
  13. #ifndef PARTICLE_EDITOR_MODE
  14. #define PARTICLE_EDITOR_MODE false
  15. #endif
  16. NS_RU_BEGIN
  17. enum ZMLShaderType {
  18. SHADER_COLOR = 0, //默认shader
  19. SHADER_ALPHA_BLEND,
  20. SHADER_DISTORT,
  21. SHADER_NORMAL
  22. };
  23. typedef enum ZMLShaderType ZMLParticle_Shader_Type;
  24. class ClightSprite:public Sprite
  25. {
  26. public:
  27. ClightSprite(){
  28. m_diffuse=Color4F(1, 1, 1, 1);
  29. m_ambient=Color4F(1, 1, 1, 1);
  30. m_z=0;
  31. }
  32. virtual~ClightSprite(){
  33. }
  34. bool init(const string&texFileName){
  35. Sprite::initWithFile(texFileName.c_str());
  36. return true;
  37. }
  38. Color4F getDiffuse()const{return m_diffuse;}
  39. void setDiffuse(const Color4F& diffuse){
  40. m_diffuse=diffuse;
  41. setColor(Color3B(m_diffuse.r*255,m_diffuse.g*255,m_diffuse.b*255));
  42. }
  43. Color4F getAmbient()const{return m_ambient;}
  44. void setAmbient(const Color4F& ambient){m_ambient=ambient;}
  45. float getZ()const{return m_z;}
  46. void setZ(float z){m_z=z;}
  47. protected:
  48. Color4F m_diffuse;
  49. Color4F m_ambient;
  50. float m_z;//z value of the light
  51. };
  52. class Cmaterial{
  53. public:
  54. Color4F m_diffuse;
  55. Color4F m_ambient;
  56. Cmaterial(){
  57. m_diffuse=Color4F(1, 1, 1, 1);
  58. m_ambient=Color4F(0.5, 0.5, 0.5, 1);
  59. }
  60. };
  61. class ZMLParticleDataExpansion
  62. {
  63. public:
  64. float* startColorR;
  65. float* startColorG;
  66. float* startColorB;
  67. float* startColorA;
  68. float* deltaColorRAllLife;
  69. float* deltaColorGAllLife;
  70. float* deltaColorBAllLife;
  71. float* deltaColorAAllLife;
  72. float* sizeWidth;
  73. float* startSizeWidth;
  74. float* deltaSizeWidthAllLife;
  75. float* sizeHeight;
  76. float* startSizeHeighth;
  77. float* deltaSizeHeightAllLife;
  78. float* startRotation;
  79. float* deltaRotationAllLife;
  80. float* deltaLifePercentage;
  81. float* lifePercentage;
  82. int* textureIndex;
  83. int* startTextureIndex;
  84. float* posz;
  85. float* maxSpeendZOffset;
  86. float* life;
  87. unsigned int maxCount;
  88. ZMLParticleDataExpansion();
  89. bool init(int count);
  90. void release();
  91. unsigned int getMaxCount() { return maxCount; }
  92. void copyParticle(int p1, int p2)
  93. {
  94. startColorR[p1] = startColorR[p2];
  95. startColorG[p1] = startColorG[p2];
  96. startColorB[p1] = startColorB[p2];
  97. startColorA[p1] = startColorA[p2];
  98. deltaColorRAllLife[p1] = deltaColorRAllLife[p2];
  99. deltaColorGAllLife[p1] = deltaColorGAllLife[p2];
  100. deltaColorBAllLife[p1] = deltaColorBAllLife[p2];
  101. deltaColorAAllLife[p1] = deltaColorAAllLife[p2];
  102. sizeWidth[p1] = sizeWidth[p2];
  103. startSizeHeighth[p1] = startSizeHeighth[p2];
  104. deltaSizeWidthAllLife[p1] = deltaSizeWidthAllLife[p2];
  105. sizeHeight[p1] = sizeHeight[p2];
  106. startSizeHeighth[p1] = startSizeHeighth[p2];
  107. deltaSizeHeightAllLife[p1] = deltaSizeHeightAllLife[p2];
  108. startRotation[p1] = startRotation[p2];
  109. deltaRotationAllLife[p1] = deltaRotationAllLife[p2];
  110. deltaLifePercentage[p1] = deltaLifePercentage[p2];
  111. lifePercentage[p1] = lifePercentage[p2];
  112. textureIndex[p1] = textureIndex[p2];
  113. startTextureIndex[p1] = startTextureIndex[p2];
  114. posz[p1] = posz[p2];
  115. maxSpeendZOffset[p1] = maxSpeendZOffset[p2];
  116. life[p1] = life[p2];
  117. }
  118. };
  119. class ZMLParticleSystem : public ParticleSystem{
  120. protected:
  121. enum {
  122. //纹理旋转结束和起始相同
  123. START_ROTATION_EQUAL_TO_END_ROTATION = -1,
  124. //序列帧动画在生命周期内只播放一遍
  125. FRAM_RATE_PLAY_ONCE_IN_ALL_LIFE = -1,
  126. //序列帧动画在生命周期内只播放一遍
  127. FRAM_RATE_PLAY_CYCLE = -1,
  128. };
  129. public:
  130. ZMLParticleSystem();
  131. ~ZMLParticleSystem();
  132. static ZMLParticleSystem *createWithJsonFile(string path);
  133. static ZMLParticleSystem *createWithJsonString(string jsonStr);
  134. bool initWithJsonFile(string path);
  135. bool initWithJsonString(string jsonStr);
  136. public:
  137. virtual void update(float dt) override;
  138. virtual void addParticles(int count);
  139. void SetColorPower(float colorPower);
  140. void addModule(ParticleSystemModule* module);
  141. ParticleSystemModule* getModule(ParticleSystemModuleFlag flag);
  142. protected:
  143. ZMLParticleDataExpansion _particleDataExpansion;
  144. //喷发控制
  145. float _explosiveness_ratio;
  146. float* _sizeWidthChangeLaw;
  147. float* _sizeHeightChangeLaw;
  148. float* _spinChangeLaw;
  149. float* _colorAChangeLaw;
  150. float* _speedXOffset;
  151. float* _speedYOffset;
  152. float* _speedZOffset;
  153. float _angleEven;
  154. float _maxSpeendZOffset;
  155. float _posZVar;
  156. Vec2 _oldPos;
  157. float _launcherangle;
  158. float _launcherV;
  159. int _texNumber;
  160. float _frameRate;
  161. float _startSizeWidth;
  162. float _startSizeWidthVar;
  163. float _endSizeWidth;
  164. float _endSizeWidthVar;
  165. float _startSizeHeight;
  166. float _startSizeHeightVar;
  167. float _endSizeHeight;
  168. float _endSizeHeightVar;
  169. bool _lockAspectRatio;
  170. float _aspectRatio;
  171. // 风有关的力和变化曲线时间
  172. float _maxWindX;
  173. float _maxWindY;
  174. float _windCycle;
  175. float* _windOffset;
  176. float _windTimelinePostion;
  177. float _winfTimelineDposPreS;
  178. Vec2 _lastAddParticleWordPostion;
  179. string _mainTexName;
  180. //默认的shader有一定的局限性,添加了一张可选的mask图
  181. string _maskName;
  182. float _colorPower;
  183. int _shaderType;
  184. int moduleFlag;
  185. std::map<ParticleSystemModuleFlag, ParticleSystemModule*> modules;
  186. };
  187. NS_RU_END
  188. #endif /* __RUZMLParticleSystem_h */