ParticleEmitter.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2011 Lasse Öörni
  4. //
  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. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. //
  23. #pragma once
  24. #include "BillboardSet.h"
  25. /// Determines the emitter shape
  26. enum EmitterType
  27. {
  28. EMITTER_POINT,
  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 fade index
  48. unsigned colorIndex_;
  49. /// Current texture animation index
  50. unsigned texIndex_;
  51. };
  52. /// Texture animation
  53. struct TextureAnimation
  54. {
  55. Rect uv_;
  56. float time_;
  57. };
  58. class ResourceCache;
  59. class XMLFile;
  60. class XMLElement;
  61. /// Particle emitter component
  62. class ParticleEmitter : public BillboardSet
  63. {
  64. OBJECT(ParticleEmitter);
  65. public:
  66. /// Construct
  67. ParticleEmitter(Context* context);
  68. /// Destruct
  69. virtual ~ParticleEmitter();
  70. /// Register object factory
  71. static void RegisterObject(Context* context);
  72. /// Handle attribute write access
  73. virtual void OnSetAttribute(const AttributeInfo& attr, const Variant& value);
  74. /// Handle attribute read access
  75. virtual Variant OnGetAttribute(const AttributeInfo& attr);
  76. /// Update the particle system. Is called from HandleScenePostUpdate()
  77. void Update(float timeStep);
  78. /// Load emitter parameters from an XML file. Return true if successful
  79. bool LoadParameters(XMLFile* file);
  80. /// Set emitter active/inactive state and optionally reset active/inactive timer
  81. void SetActive(bool enable, bool resetPeriod = false);
  82. /// Return parameter XML file
  83. XMLFile* GetParameters() const { return parameterSource_; }
  84. /// Return number of particles
  85. unsigned GetNumParticles() const { return particles_.size(); }
  86. /// Return whether emitter is active
  87. bool IsActive() const { return active_; }
  88. protected:
  89. /// Set number of particles
  90. void SetNumParticles(int num);
  91. /// Set color of particles
  92. void SetParticleColor(const Color& color);
  93. /// Set color fade of particles
  94. void SetParticleColors(const std::vector<ColorFade>& colors);
  95. /// Create a new particle. Return true if there was room
  96. bool EmitNewParticle();
  97. /// Return a free particle index
  98. unsigned GetFreeParticle() const;
  99. /// Read a float range from an XML element
  100. void GetFloatMinMax(const XMLElement& element, float& minValue, float& maxValue);
  101. /// Read a Vector2 range from an XML element
  102. void GetVector2MinMax(const XMLElement& element, Vector2& minValue, Vector2& maxValue);
  103. /// Read a Vector3 from an XML element
  104. void GetVector3MinMax(const XMLElement& element, Vector3& minValue, Vector3& maxValue);
  105. protected:
  106. /// Handle node being assigned
  107. virtual void OnNodeSet(Node* node);
  108. private:
  109. /// Handle scene post-update event
  110. void HandleScenePostUpdate(StringHash eventType, VariantMap& eventData);
  111. /// Parameter XML file
  112. SharedPtr<XMLFile> parameterSource_;
  113. /// Particles
  114. std::vector<Particle> particles_;
  115. /// Color fade range
  116. std::vector<ColorFade> colors_;
  117. /// Texture animation
  118. std::vector<TextureAnimation> textureAnimation_;
  119. /// Emitter shape
  120. EmitterType emitterType_;
  121. /// Emitter size
  122. Vector3 emitterSize_;
  123. /// Particle direction minimum
  124. Vector3 directionMin_;
  125. /// Particle direction maximum
  126. Vector3 directionMax_;
  127. /// Particle constant force
  128. Vector3 constanceForce_;
  129. /// Particle size minimum
  130. Vector2 sizeMin_;
  131. /// Particle size maximum
  132. Vector2 sizeMax_;
  133. /// Particle velocity damping force
  134. float dampingForce_;
  135. /// Emitter radius if spherical
  136. float emitterRadius_;
  137. /// Active/inactive period timer
  138. float periodTimer_;
  139. /// New particle emission timer
  140. float emissionTimer_;
  141. /// Active period
  142. float activeTime_;
  143. /// Inactive period
  144. float inactiveTime_;
  145. /// Emission interval minimum
  146. float intervalMin_;
  147. /// Emission interval maximum
  148. float intervalMax_;
  149. /// Particle time to live minimum
  150. float timeToLiveMin_;
  151. /// Particle time to live maximum
  152. float timeToLiveMax_;
  153. /// Particle velocity minimum
  154. float velocityMin_;
  155. /// Particle velocityy maximum
  156. float velocityMax_;
  157. /// Particle rotation angle minimum
  158. float rotationMin_;
  159. /// Particle rotation angle maximum
  160. float rotationMax_;
  161. /// Particle rotation speed minimum
  162. float rotationSpeedMin_;
  163. /// Particle rotation speed maximum
  164. float rotationSpeedMax_;
  165. /// Particle size additive parameter
  166. float sizeAdd_;
  167. /// Particle size multiplicative parameter
  168. float sizeMul_;
  169. /// Emitter active/inactive state
  170. bool active_;
  171. /// Update when invisible flag
  172. bool updateInvisible_;
  173. /// Rendering renderer framenumber on which was last updated
  174. unsigned lastUpdateFrameNumber_;
  175. };