PolyParticleEmitter.h 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. /*
  2. Copyright (C) 2011 by Ivan Safrin
  3. Permission is hereby granted, free of charge, to any person obtaining a copy
  4. of this software and associated documentation files (the "Software"), to deal
  5. in the Software without restriction, including without limitation the rights
  6. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. copies of the Software, and to permit persons to whom the Software is
  8. furnished to do so, subject to the following conditions:
  9. The above copyright notice and this permission notice shall be included in
  10. all copies or substantial portions of the Software.
  11. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  12. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  13. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  14. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  15. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  16. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  17. THE SOFTWARE.
  18. */
  19. #pragma once
  20. #include "PolyGlobals.h"
  21. #include "PolyString.h"
  22. #include "PolyVector3.h"
  23. #include "PolyMatrix4.h"
  24. #include "PolyBezierCurve.h"
  25. #include "PolySceneEntity.h"
  26. #include "PolyScreenEntity.h"
  27. namespace Polycode {
  28. class Entity;
  29. class Material;
  30. class Mesh;
  31. class Particle;
  32. class Perlin;
  33. class Scene;
  34. class SceneMesh;
  35. class Screen;
  36. class ScreenMesh;
  37. class Texture;
  38. class Timer;
  39. /**
  40. * Particle emitter base.
  41. */
  42. class _PolyExport ParticleEmitter {
  43. public:
  44. ParticleEmitter(const String& imageFile, Mesh *particleMesh, int particleType, int emitterType, Number lifespan, unsigned int numParticles, Vector3 direction, Vector3 gravity, Vector3 deviation, Vector3 emitterRadius);
  45. virtual ~ParticleEmitter();
  46. virtual void dispatchTriggerCompleteEvent();
  47. void createParticles();
  48. /**
  49. * Sets the speed at which particles rotate
  50. * @param speed New rotation speed.
  51. */
  52. void setRotationSpeed(Number speed);
  53. /**
  54. * Sets the blending mode used for the particles. See documentation for the Entity for information on blending modes.
  55. * @param mode New blending mode.
  56. */
  57. void setParticleBlendingMode(int mode);
  58. int getParticleBlendingMode() const;
  59. /**
  60. * Turns depth write on and off for particles.
  61. */
  62. void setDepthWrite(bool val);
  63. /**
  64. * Turns depth test on and off for particles.
  65. */
  66. void setDepthTest(bool val);
  67. /**
  68. * Turns alpha testing on and off for particles.
  69. */
  70. void setAlphaTest(bool val);
  71. /**
  72. * Enables perlin noise movement for particles.
  73. */
  74. void enablePerlin(bool val);
  75. /**
  76. * Sets visibility of all particles in the system
  77. */
  78. void setParticleVisibility(bool val);
  79. /**
  80. * Enables perlin noise movement size.
  81. */
  82. void setPerlinModSize(Number size);
  83. /**
  84. * Enables or disables billboard mode for particles.
  85. */
  86. void setBillboardMode(bool mode);
  87. /**
  88. * Enables or disables the emitter
  89. */
  90. void enableEmitter(bool val);
  91. /**
  92. * Returns true if the emitter is enabled, false otherwise.
  93. */
  94. bool emitterEnabled();
  95. /**
  96. * Sets the emitter radius on all 3 axises.
  97. */
  98. void setEmitterRadius(Vector3 rad);
  99. /**
  100. * If set to true, will release all particles at once.
  101. */
  102. void setAllAtOnce(bool val);
  103. unsigned int getNumParticles() const;
  104. Particle *getParticleAtIndex(unsigned int index) const;
  105. /**
  106. * If emitter mode is TRIGGERED_EMITTER, calling this method will trigger particle emission.
  107. */
  108. void resetAll();
  109. void Trigger();
  110. void resetParticle(Particle *particle);
  111. /**
  112. * Changes the particle count in the emitter.
  113. */
  114. void setParticleCount(int count);
  115. virtual Vector3 getParticleCompoundScale();
  116. virtual void addParticleBody(Entity *particleBody);
  117. virtual Matrix4 getBaseMatrix();
  118. /**
  119. * Particle movement speed multiplier
  120. */
  121. Number particleSpeedMod;
  122. /**
  123. * Particle brightness deviation
  124. */
  125. Number brightnessDeviation;
  126. void updateEmitter();
  127. /**
  128. * Continuous emitter setting.
  129. */
  130. static const int CONTINUOUS_EMITTER = 0;
  131. /**
  132. * Triggered emitter setting.
  133. */
  134. static const int TRIGGERED_EMITTER = 1;
  135. /**
  136. * Particle direction deviation
  137. */
  138. Vector3 deviation;
  139. /**
  140. * Particle direction and emission strength vector
  141. */
  142. Vector3 dirVector;
  143. /**
  144. * Particle gravity strength vector
  145. */
  146. Vector3 gravVector;
  147. /**
  148. * Lifespan of particles.
  149. */
  150. Number lifespan;
  151. /**
  152. * If set to true, particles' rotation will follow their movement.
  153. */
  154. bool rotationFollowsPath;
  155. /**
  156. * Bezier curve that controls the scale of the particles.
  157. */
  158. BezierCurve scaleCurve;
  159. /**
  160. * Bezier curve that controls the red component of particles' color.
  161. */
  162. BezierCurve colorCurveR;
  163. /**
  164. * Bezier curve that controls the green component of particles' color.
  165. */
  166. BezierCurve colorCurveG;
  167. /**
  168. * Bezier curve that controls the blue component of particles' color.
  169. */
  170. BezierCurve colorCurveB;
  171. /**
  172. * Bezier curve that controls the alpha component of particles' color.
  173. */
  174. BezierCurve colorCurveA;
  175. /**
  176. * If set to true, will use the color curves to control particle color. False by default.
  177. */
  178. bool useColorCurves;
  179. /**
  180. * If set to true, will use the scale curve to control particle scale. False by default.
  181. */
  182. bool useScaleCurves;
  183. Number particleSize;
  184. Texture *getParticleTexture();
  185. void setParticleTexture(Texture *texture);
  186. Vector3 emitterRadius;
  187. Number perlinModSize;
  188. bool perlinEnabled;
  189. Number rotationSpeed;
  190. int emitterType;
  191. protected:
  192. int blendingMode;
  193. bool isScreenEmitter;
  194. Mesh *pMesh;
  195. bool allAtOnce;
  196. int particleType;
  197. Material *particleMaterial;
  198. Texture *particleTexture;
  199. String textureFile;
  200. bool isEmitterEnabled;
  201. Perlin *motionPerlin;
  202. Number numParticles;
  203. std::vector<Particle*> particles;
  204. Number emitSpeed;
  205. Timer *timer;
  206. };
  207. /**
  208. * 3D particle emitter.
  209. */
  210. class _PolyExport SceneParticleEmitter : public SceneEntity, public ParticleEmitter {
  211. public:
  212. /**
  213. * Constructor.
  214. * @param materialName Name of the material to use for particles.
  215. * @param particleParentScene Scene to create particles in.
  216. * @param particleType Type of particles to create. Can be Particle::BILLBOARD_PARTICLE or Particle::MESH_PARTICLE
  217. * @param emitterType Type of emitter to create. Can be ParticleEmitter::CONTINUOUS_EMITTER or ParticleEmitter::TRIGGERED_EMITTER
  218. * @param lifespan Lifetime of particles in seconds.
  219. * @param numParticles Total number of particles to create.
  220. * @param direction Direction of the emitter, length of this vector controls emitter strength
  221. * @param gravity Gravity direction and strength
  222. * @param deviation Emitter deviation on each axis
  223. * @param particleMesh If particle type is Particle::MESH_PARTICLE, this must be set to the mesh to use for each particle
  224. * @param emitter If this is specified, particles will be emitted from this meshe's vertices.
  225. */
  226. SceneParticleEmitter(const String& materialName, int particleType, int emitterType, Number lifespan, unsigned int numParticles, Vector3 direction, Vector3 gravity, Vector3 deviation, Vector3 emitterRadius, Mesh *particleMesh = NULL, SceneMesh *emitter = NULL);
  227. virtual ~SceneParticleEmitter();
  228. /**
  229. * Returns the emitter (helper method for LUA).
  230. */
  231. ParticleEmitter *getEmitter() { return this; }
  232. void respawnSceneParticles();
  233. void addParticleBody(Entity *particleBody);
  234. Matrix4 getBaseMatrix();
  235. void Update();
  236. Vector3 getParticleCompoundScale();
  237. void dispatchTriggerCompleteEvent();
  238. /**
  239. * Continuous emitter setting.
  240. */
  241. static const int CONTINUOUS_EMITTER = 0;
  242. /**
  243. * Triggered emitter setting.
  244. */
  245. static const int TRIGGERED_EMITTER = 1;
  246. protected:
  247. SceneMesh *emitterMesh;
  248. };
  249. /**
  250. * 3D particle emitter.
  251. */
  252. class _PolyExport ScreenParticleEmitter : public ScreenEntity, public ParticleEmitter {
  253. public:
  254. ScreenParticleEmitter(const String& imageFile, int particleType, int emitterType, Number lifespan, unsigned int numParticles, Vector3 direction, Vector3 gravity, Vector3 deviation, Vector3 emitterRadius, Mesh *particleMesh = NULL, ScreenMesh *emitter = NULL);
  255. virtual ~ScreenParticleEmitter();
  256. virtual Entity *Clone(bool deepClone, bool ignoreEditorOnly);
  257. virtual void applyClone(Entity *clone, bool deepClone, bool ignoreEditorOnly);
  258. /**
  259. * Returns the emitter (helper method for LUA).
  260. */
  261. ParticleEmitter *getEmitter() { return this; }
  262. void dispatchTriggerCompleteEvent();
  263. void addParticleBody(Entity *particleBody);
  264. Matrix4 getBaseMatrix();
  265. void Update();
  266. Vector3 getParticleCompoundScale();
  267. /**
  268. * Continuous emitter setting.
  269. */
  270. static const int CONTINUOUS_EMITTER = 0;
  271. /**
  272. * Triggered emitter setting.
  273. */
  274. static const int TRIGGERED_EMITTER = 1;
  275. protected:
  276. ScreenMesh *emitterMesh;
  277. };
  278. }