ParticleEmitter.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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. /// Update the particle system. Is called from HandleScenePostUpdate()
  73. void Update(float timeStep);
  74. /// Load emitter parameters from an XML file. Return true if successful
  75. bool LoadParameters(XMLFile* file);
  76. /// Set emitter active/inactive state and optionally reset active/inactive timer
  77. void SetActive(bool enable, bool resetPeriod = false);
  78. /// Return parameter XML file
  79. XMLFile* GetParameters() const { return parameterSource_; }
  80. /// Return number of particles
  81. unsigned GetNumParticles() const { return particles_.Size(); }
  82. /// Return whether emitter is active
  83. bool IsActive() const { return active_; }
  84. /// Set parameter source attribute
  85. void SetParameterSourceAttr(ResourceRef value);
  86. /// Set particles attribute
  87. void SetParticlesAttr(PODVector<unsigned char> value);
  88. /// Return parameter source attribute
  89. ResourceRef GetParameterSourceAttr() const;
  90. /// Return particles attribute
  91. PODVector<unsigned char> GetParticlesAttr() const;
  92. protected:
  93. /// Handle node being assigned
  94. virtual void OnNodeSet(Node* node);
  95. /// Set number of particles
  96. void SetNumParticles(int num);
  97. /// Set color of particles
  98. void SetParticleColor(const Color& color);
  99. /// Set color fade of particles
  100. void SetParticleColors(const Vector<ColorFade>& colors);
  101. /// Create a new particle. Return true if there was room
  102. bool EmitNewParticle();
  103. /// Return a free particle index
  104. unsigned GetFreeParticle() const;
  105. /// Read a float range from an XML element
  106. void GetFloatMinMax(const XMLElement& element, float& minValue, float& maxValue);
  107. /// Read a Vector2 range from an XML element
  108. void GetVector2MinMax(const XMLElement& element, Vector2& minValue, Vector2& maxValue);
  109. /// Read a Vector3 from an XML element
  110. void GetVector3MinMax(const XMLElement& element, Vector3& minValue, Vector3& maxValue);
  111. private:
  112. /// Handle scene post-update event
  113. void HandleScenePostUpdate(StringHash eventType, VariantMap& eventData);
  114. /// Parameter XML file
  115. SharedPtr<XMLFile> parameterSource_;
  116. /// Particles
  117. PODVector<Particle> particles_;
  118. /// Color fade range
  119. Vector<ColorFade> colors_;
  120. /// Texture animation
  121. Vector<TextureAnimation> textureAnimation_;
  122. /// Emitter shape
  123. EmitterType emitterType_;
  124. /// Emitter size
  125. Vector3 emitterSize_;
  126. /// Particle direction minimum
  127. Vector3 directionMin_;
  128. /// Particle direction maximum
  129. Vector3 directionMax_;
  130. /// Particle constant force
  131. Vector3 constanceForce_;
  132. /// Particle size minimum
  133. Vector2 sizeMin_;
  134. /// Particle size maximum
  135. Vector2 sizeMax_;
  136. /// Particle velocity damping force
  137. float dampingForce_;
  138. /// Emitter radius if spherical
  139. float emitterRadius_;
  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. /// Rendering renderer framenumber on which was last updated
  177. unsigned lastUpdateFrameNumber_;
  178. };