ParticleEmitter.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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 definition.
  53. struct TextureAnimation
  54. {
  55. /// UV coordinates.
  56. Rect uv_;
  57. /// Time.
  58. float time_;
  59. };
  60. class ResourceCache;
  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. /// Emitter radius if spherical.
  141. float emitterRadius_;
  142. /// Active/inactive period timer.
  143. float periodTimer_;
  144. /// New particle emission timer.
  145. float emissionTimer_;
  146. /// Active period.
  147. float activeTime_;
  148. /// Inactive period.
  149. float inactiveTime_;
  150. /// Emission interval minimum.
  151. float intervalMin_;
  152. /// Emission interval maximum.
  153. float intervalMax_;
  154. /// Particle time to live minimum.
  155. float timeToLiveMin_;
  156. /// Particle time to live maximum.
  157. float timeToLiveMax_;
  158. /// Particle velocity minimum.
  159. float velocityMin_;
  160. /// Particle velocityy maximum.
  161. float velocityMax_;
  162. /// Particle rotation angle minimum.
  163. float rotationMin_;
  164. /// Particle rotation angle maximum.
  165. float rotationMax_;
  166. /// Particle rotation speed minimum.
  167. float rotationSpeedMin_;
  168. /// Particle rotation speed maximum.
  169. float rotationSpeedMax_;
  170. /// Particle size additive parameter.
  171. float sizeAdd_;
  172. /// Particle size multiplicative parameter.
  173. float sizeMul_;
  174. /// Emitter active/inactive state.
  175. bool active_;
  176. /// Update when invisible flag.
  177. bool updateInvisible_;
  178. /// Rendering renderer framenumber on which was last updated.
  179. unsigned lastUpdateFrameNumber_;
  180. };