ParticleEmitter.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. //
  2. // Copyright (c) 2008-2013 the Urho3D project.
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #pragma once
  23. #include "BillboardSet.h"
  24. namespace Urho3D
  25. {
  26. /// Particle emitter shapes.
  27. enum EmitterType
  28. {
  29. EMITTER_SPHERE,
  30. EMITTER_BOX
  31. };
  32. /// One particle in the particle system.
  33. struct Particle
  34. {
  35. /// Velocity.
  36. Vector3 velocity_;
  37. /// Original billboard size.
  38. Vector2 size_;
  39. /// Time elapsed from creation.
  40. float timer_;
  41. /// Lifetime.
  42. float timeToLive_;
  43. /// Size scaling value.
  44. float scale_;
  45. /// Rotation speed.
  46. float rotationSpeed_;
  47. /// Current color animation index.
  48. unsigned colorIndex_;
  49. /// Current texture animation index.
  50. unsigned texIndex_;
  51. };
  52. /// %Color animation frame definition.
  53. struct ColorFrame
  54. {
  55. /// Construct with default values.
  56. ColorFrame() :
  57. time_(0.0f)
  58. {
  59. }
  60. /// Construct with a color and zero time.
  61. ColorFrame(const Color& color) :
  62. color_(color),
  63. time_(0.0f)
  64. {
  65. }
  66. /// Construct from a color and time.
  67. ColorFrame(const Color& color, float time) :
  68. color_(color),
  69. time_(time)
  70. {
  71. }
  72. /// Return interpolated value with another color-time pair at the time specified.
  73. Color Interpolate(const ColorFrame& next, float time)
  74. {
  75. float timeInterval = next.time_ - time_;
  76. if (timeInterval > 0.0f)
  77. {
  78. float t = (time - time_) / timeInterval;
  79. return color_.Lerp(next.color_, t);
  80. }
  81. else
  82. return next.color_;
  83. }
  84. /// Color.
  85. Color color_;
  86. /// Time.
  87. float time_;
  88. };
  89. /// %Texture animation frame definition.
  90. struct TextureFrame
  91. {
  92. /// Construct with default values.
  93. TextureFrame() :
  94. uv_(0.0f, 0.0f, 1.0f, 1.0f),
  95. time_(0.0f)
  96. {
  97. }
  98. /// UV coordinates.
  99. Rect uv_;
  100. /// Time.
  101. float time_;
  102. };
  103. class XMLFile;
  104. class XMLElement;
  105. /// %Particle emitter component.
  106. class URHO3D_API ParticleEmitter : public BillboardSet
  107. {
  108. OBJECT(ParticleEmitter);
  109. public:
  110. /// Construct.
  111. ParticleEmitter(Context* context);
  112. /// Destruct.
  113. virtual ~ParticleEmitter();
  114. /// Register object factory.
  115. static void RegisterObject(Context* context);
  116. /// Handle enabled/disabled state change.
  117. virtual void OnSetEnabled();
  118. /// Update before octree reinsertion. Is called from a worker thread.
  119. virtual void Update(const FrameInfo& frame);
  120. /// Load emitter parameters from an XML file.
  121. bool Load(XMLFile* file);
  122. /// Set maximum number of particles.
  123. void SetNumParticles(unsigned num);
  124. /// Set emission rate (both minimum and maximum.)
  125. void SetEmissionRate(float rate);
  126. /// Set minimum emission rate.
  127. void SetMinEmissionRate(float rate);
  128. /// Set maximum emission rate.
  129. void SetMaxEmissionRate(float rate);
  130. /// Set emitter type.
  131. void SetEmitterType(EmitterType type);
  132. /// Set emitter size.
  133. void SetEmitterSize(const Vector3& size);
  134. /// Set emission active period length (0 = infinite.)
  135. void SetActiveTime(float time);
  136. /// Set emission inactive period length (0 = infinite.)
  137. void SetInactiveTime(float time);
  138. /// Set whether should be emitting and optionally reset emission period.
  139. void SetEmitting(bool enable, bool resetPeriod = false);
  140. /// Set whether to update when particles are not visible.
  141. void SetUpdateInvisible(bool enable);
  142. /// Set particle time to live (both minimum and maximum.)
  143. void SetTimeToLive(float time);
  144. /// Set particle minimum time to live.
  145. void SetMinTimeToLive(float time);
  146. /// Set particle maximum time to live.
  147. void SetMaxTimeToLive(float time);
  148. /// Set particle size (both minimum and maximum.)
  149. void SetParticleSize(const Vector2& size);
  150. /// Set particle minimum size.
  151. void SetMinParticleSize(const Vector2& size);
  152. /// Set particle maximum size.
  153. void SetMaxParticleSize(const Vector2& size);
  154. /// Set negative direction limit.
  155. void SetMinDirection(const Vector3& direction);
  156. /// Set positive direction limit.
  157. void SetMaxDirection(const Vector3& direction);
  158. /// Set particle velocity (both minimum and maximum.)
  159. void SetVelocity(float velocity);
  160. /// Set particle minimum velocity.
  161. void SetMinVelocity(float velocity);
  162. /// Set particle maximum velocity.
  163. void SetMaxVelocity(float velocity);
  164. /// Set particle rotation (both minimum and maximum.)
  165. void SetRotation(float rotation);
  166. /// Set particle minimum rotation.
  167. void SetMinRotation(float rotation);
  168. /// Set particle maximum rotation.
  169. void SetMaxRotation(float rotation);
  170. /// Set particle rotation speed (both minimum and maximum.)
  171. void SetRotationSpeed(float speed);
  172. /// Set particle minimum rotation speed.
  173. void SetMinRotationSpeed(float speed);
  174. /// Set particle maximum rotation speed.
  175. void SetMaxRotationSpeed(float speed);
  176. /// Set constant force acting on particles.
  177. void SetConstantForce(const Vector3& force);
  178. /// Set particle velocity damping force.
  179. void SetDampingForce(float force);
  180. /// Set particle size additive modifier.
  181. void SetSizeAdd(float sizeAdd);
  182. /// Set particle size multiplicative modifier.
  183. void SetSizeMul(float sizeMul);
  184. /// Set color of particles.
  185. void SetColor(const Color& color);
  186. /// Set color animation of particles.
  187. void SetColors(const Vector<ColorFrame>& colors);
  188. /// Set number of color animation frames.
  189. void SetNumColors(unsigned num);
  190. /// Set particle texture animation.
  191. void SetTextureFrames(const Vector<TextureFrame>& animation);
  192. /// Set number of texture animation frames.
  193. void SetNumTextureFrames(unsigned num);
  194. /// Return maximum number of particles.
  195. unsigned GetNumParticles() const { return particles_.Size(); }
  196. /// Return whether is currently emitting.
  197. bool IsEmitting() const { return emitting_; }
  198. /// Return whether to update when particles are not visible.
  199. bool GetUpdateInvisible() const { return updateInvisible_; }
  200. /// Return minimum emission rate.
  201. float GetMinEmissionRate() const { return emissionRateMin_; }
  202. /// Return maximum emission rate.
  203. float GetMaxEmissionRate() const { return emissionRateMax_; }
  204. /// Return emitter type.
  205. EmitterType GetEmitterType() const { return emitterType_; }
  206. /// Return emitter size.
  207. const Vector3& GetEmitterSize() const { return emitterSize_; }
  208. /// Return emission active period length (0 = infinite.)
  209. float GetActiveTime() const { return activeTime_; }
  210. /// Return emission inactive period length (0 = infinite.)
  211. float GetInactiveTime() const { return inactiveTime_; }
  212. /// Return particle minimum time to live.
  213. float GetMinTimeToLive() const { return timeToLiveMin_; }
  214. /// Return particle maximum time to live.
  215. float GetMaxTimeToLive() const { return timeToLiveMax_; }
  216. /// Return particle minimum size.
  217. const Vector2& GetMinParticleSize() const { return sizeMin_; }
  218. /// Return particle maximum size.
  219. const Vector2& GetMaxParticleSize() const { return sizeMax_; }
  220. /// Return negative direction limit.
  221. const Vector3& GetMinDirection() const { return directionMin_; }
  222. /// Return positive direction limit.
  223. const Vector3& GetMaxDirection() const { return directionMax_; }
  224. /// Return particle minimum velocity.
  225. float GetMinVelocity() const { return velocityMin_; }
  226. /// Return particle maximum velocity.
  227. float GetMaxVelocity() const { return velocityMax_; }
  228. /// Return particle minimum rotation.
  229. float GetMinRotation() const { return rotationMin_; }
  230. /// Return particle maximum rotation.
  231. float GetMaxRotation() const { return rotationMax_; }
  232. /// Return particle minimum rotation speed.
  233. float GetMinRotationSpeed() const { return rotationSpeedMin_; }
  234. /// Return particle maximum rotation speed.
  235. float GetMaxRotationSpeed() const { return rotationSpeedMax_; }
  236. /// Return constant force acting on particles.
  237. const Vector3& GetConstantForce() const { return constantForce_; }
  238. /// Return particle velocity damping force.
  239. float GetDampingForce() const { return dampingForce_; }
  240. /// Return particle size additive modifier.
  241. float GetSizeAdd() const { return sizeAdd_; }
  242. /// Return particle size multiplicative modifier.
  243. float GetSizeMul() const { return sizeMul_; }
  244. /// Return all color animation frames.
  245. Vector<ColorFrame>& GetColors() { return colorFrames_; }
  246. /// Return number of color animation frames.
  247. unsigned GetNumColors() const { return colorFrames_.Size(); }
  248. /// Return a color animation frame, or null if outside range.
  249. ColorFrame* GetColor(unsigned index) { return index < colorFrames_.Size() ? &colorFrames_[index] : (ColorFrame*)0; }
  250. /// Return all texture animation frames.
  251. Vector<TextureFrame>& GetTextureFrame() { return textureFrames_; }
  252. /// Return number of texture animation frames.
  253. unsigned GetNumTextureFrames() const { return textureFrames_.Size(); }
  254. /// Return a texture animation frame, or null if outside range.
  255. TextureFrame* GetTextureFrame(unsigned index) { return index < colorFrames_.Size() ? &textureFrames_[index] : (TextureFrame*)0; }
  256. /// Set particles attribute.
  257. void SetParticlesAttr(VariantVector value);
  258. /// Return particles attribute.
  259. VariantVector GetParticlesAttr() const;
  260. /// Set particle colors attribute.
  261. void SetColorsAttr(VariantVector value);
  262. /// Return particle colors attribute.
  263. VariantVector GetColorsAttr() const;
  264. /// Set texture animation attribute.
  265. void SetTextureFramesAttr(VariantVector value);
  266. /// Return texture animation attribute.
  267. VariantVector GetTextureFramesAttr() const;
  268. protected:
  269. /// Handle node being assigned.
  270. virtual void OnNodeSet(Node* node);
  271. /// Create a new particle. Return true if there was room.
  272. bool EmitNewParticle();
  273. /// Return a free particle index.
  274. unsigned GetFreeParticle() const;
  275. /// Read a float range from an XML element.
  276. void GetFloatMinMax(const XMLElement& element, float& minValue, float& maxValue);
  277. /// Read a Vector2 range from an XML element.
  278. void GetVector2MinMax(const XMLElement& element, Vector2& minValue, Vector2& maxValue);
  279. /// Read a Vector3 from an XML element.
  280. void GetVector3MinMax(const XMLElement& element, Vector3& minValue, Vector3& maxValue);
  281. private:
  282. /// Handle scene post-update event.
  283. void HandleScenePostUpdate(StringHash eventType, VariantMap& eventData);
  284. /// Particles.
  285. PODVector<Particle> particles_;
  286. /// Particle color animation frames.
  287. Vector<ColorFrame> colorFrames_;
  288. /// Texture animation frames.
  289. Vector<TextureFrame> textureFrames_;
  290. /// Emitter shape.
  291. EmitterType emitterType_;
  292. /// Emitter size.
  293. Vector3 emitterSize_;
  294. /// Particle direction minimum.
  295. Vector3 directionMin_;
  296. /// Particle direction maximum.
  297. Vector3 directionMax_;
  298. /// Particle constant force.
  299. Vector3 constantForce_;
  300. /// Particle size minimum.
  301. Vector2 sizeMin_;
  302. /// Particle size maximum.
  303. Vector2 sizeMax_;
  304. /// Particle velocity damping force.
  305. float dampingForce_;
  306. /// Active/inactive period timer.
  307. float periodTimer_;
  308. /// New particle emission timer.
  309. float emissionTimer_;
  310. /// Active period.
  311. float activeTime_;
  312. /// Inactive period.
  313. float inactiveTime_;
  314. /// Particles per second minimum.
  315. float emissionRateMin_;
  316. /// Particles per second maximum.
  317. float emissionRateMax_;
  318. /// Particle time to live minimum.
  319. float timeToLiveMin_;
  320. /// Particle time to live maximum.
  321. float timeToLiveMax_;
  322. /// Particle velocity minimum.
  323. float velocityMin_;
  324. /// Particle velocity maximum.
  325. float velocityMax_;
  326. /// Particle rotation angle minimum.
  327. float rotationMin_;
  328. /// Particle rotation angle maximum.
  329. float rotationMax_;
  330. /// Particle rotation speed minimum.
  331. float rotationSpeedMin_;
  332. /// Particle rotation speed maximum.
  333. float rotationSpeedMax_;
  334. /// Particle size additive parameter.
  335. float sizeAdd_;
  336. /// Particle size multiplicative parameter.
  337. float sizeMul_;
  338. /// Last scene timestep.
  339. float lastTimeStep_;
  340. /// Rendering framenumber on which was last updated.
  341. unsigned lastUpdateFrameNumber_;
  342. /// Currently emitting flag.
  343. bool emitting_;
  344. /// Update when invisible flag.
  345. bool updateInvisible_;
  346. /// Need update flag.
  347. bool needUpdate_;
  348. };
  349. }