ParticleEmitter.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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. /// Determines the emitter shape.
  27. enum EmitterType
  28. {
  29. EMITTER_POINT,
  30. EMITTER_SPHERE,
  31. EMITTER_BOX
  32. };
  33. /// One particle in the particle system.
  34. struct Particle
  35. {
  36. /// Velocity.
  37. Vector3 velocity_;
  38. /// Original billboard size.
  39. Vector2 size_;
  40. /// Time elapsed from creation.
  41. float timer_;
  42. /// Lifetime.
  43. float timeToLive_;
  44. /// Size scaling value.
  45. float scale_;
  46. /// Rotation speed.
  47. float rotationSpeed_;
  48. /// Current color fade index.
  49. unsigned colorIndex_;
  50. /// Current texture animation index.
  51. unsigned texIndex_;
  52. };
  53. /// %Texture animation definition.
  54. struct TextureAnimation
  55. {
  56. /// UV coordinates.
  57. Rect uv_;
  58. /// Time.
  59. float time_;
  60. };
  61. class XMLFile;
  62. class XMLElement;
  63. /// %Particle emitter component.
  64. class ParticleEmitter : public BillboardSet
  65. {
  66. OBJECT(ParticleEmitter);
  67. public:
  68. /// Construct.
  69. ParticleEmitter(Context* context);
  70. /// Destruct.
  71. virtual ~ParticleEmitter();
  72. /// Register object factory.
  73. static void RegisterObject(Context* context);
  74. /// Update before octree reinsertion. Is called from a worker thread. Needs to be requested with MarkForUpdate().
  75. virtual void Update(const FrameInfo& frame);
  76. /// Load emitter parameters from an XML file. Return true if successful.
  77. bool LoadParameters(XMLFile* file);
  78. /// Set emitter active/inactive state and optionally reset active/inactive timer.
  79. void SetActive(bool enable, bool resetPeriod = false);
  80. /// Return parameter XML file.
  81. XMLFile* GetParameters() const { return parameterSource_; }
  82. /// Return number of particles.
  83. unsigned GetNumParticles() const { return particles_.Size(); }
  84. /// Return whether emitter is active.
  85. bool IsActive() const { return active_; }
  86. /// Set parameter source attribute.
  87. void SetParameterSourceAttr(ResourceRef value);
  88. /// Set particles attribute.
  89. void SetParticlesAttr(VariantVector value);
  90. /// Return parameter source attribute.
  91. ResourceRef GetParameterSourceAttr() const;
  92. /// Return particles attribute.
  93. VariantVector GetParticlesAttr() const;
  94. protected:
  95. /// Handle node being assigned.
  96. virtual void OnNodeSet(Node* node);
  97. /// Set number of particles.
  98. void SetNumParticles(int num);
  99. /// Set color of particles.
  100. void SetParticleColor(const Color& color);
  101. /// Set color fade of particles.
  102. void SetParticleColors(const Vector<ColorFade>& colors);
  103. /// Create a new particle. Return true if there was room.
  104. bool EmitNewParticle();
  105. /// Return a free particle index.
  106. unsigned GetFreeParticle() const;
  107. /// Read a float range from an XML element.
  108. void GetFloatMinMax(const XMLElement& element, float& minValue, float& maxValue);
  109. /// Read a Vector2 range from an XML element.
  110. void GetVector2MinMax(const XMLElement& element, Vector2& minValue, Vector2& maxValue);
  111. /// Read a Vector3 from an XML element.
  112. void GetVector3MinMax(const XMLElement& element, Vector3& minValue, Vector3& maxValue);
  113. private:
  114. /// Handle scene post-update event.
  115. void HandleScenePostUpdate(StringHash eventType, VariantMap& eventData);
  116. /// Parameter XML file.
  117. SharedPtr<XMLFile> parameterSource_;
  118. /// Particles.
  119. PODVector<Particle> particles_;
  120. /// Color fade range.
  121. Vector<ColorFade> colors_;
  122. /// Texture animation.
  123. Vector<TextureAnimation> textureAnimation_;
  124. /// Emitter shape.
  125. EmitterType emitterType_;
  126. /// Emitter size.
  127. Vector3 emitterSize_;
  128. /// Particle direction minimum.
  129. Vector3 directionMin_;
  130. /// Particle direction maximum.
  131. Vector3 directionMax_;
  132. /// Particle constant force.
  133. Vector3 constanceForce_;
  134. /// Particle size minimum.
  135. Vector2 sizeMin_;
  136. /// Particle size maximum.
  137. Vector2 sizeMax_;
  138. /// Particle velocity damping force.
  139. float dampingForce_;
  140. /// Active/inactive period timer.
  141. float periodTimer_;
  142. /// New particle emission timer.
  143. float emissionTimer_;
  144. /// Active period.
  145. float activeTime_;
  146. /// Inactive period.
  147. float inactiveTime_;
  148. /// Emission interval minimum.
  149. float intervalMin_;
  150. /// Emission interval maximum.
  151. float intervalMax_;
  152. /// Particle time to live minimum.
  153. float timeToLiveMin_;
  154. /// Particle time to live maximum.
  155. float timeToLiveMax_;
  156. /// Particle velocity minimum.
  157. float velocityMin_;
  158. /// Particle velocityy maximum.
  159. float velocityMax_;
  160. /// Particle rotation angle minimum.
  161. float rotationMin_;
  162. /// Particle rotation angle maximum.
  163. float rotationMax_;
  164. /// Particle rotation speed minimum.
  165. float rotationSpeedMin_;
  166. /// Particle rotation speed maximum.
  167. float rotationSpeedMax_;
  168. /// Particle size additive parameter.
  169. float sizeAdd_;
  170. /// Particle size multiplicative parameter.
  171. float sizeMul_;
  172. /// Emitter active/inactive state.
  173. bool active_;
  174. /// Update when invisible flag.
  175. bool updateInvisible_;
  176. /// Last scene timestep.
  177. float lastTimeStep_;
  178. /// Rendering framenumber on which was last updated.
  179. unsigned lastUpdateFrameNumber_;
  180. };
  181. }