ParticleEmitter.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. //
  2. // Copyright (c) 2008-2014 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. /// Save particle emitter parameters to an XML file. Return true if successful.
  123. bool Save(XMLFile* file) const;
  124. /// Set maximum number of particles.
  125. void SetNumParticles(unsigned num);
  126. /// Set emission rate (both minimum and maximum.)
  127. void SetEmissionRate(float rate);
  128. /// Set minimum emission rate.
  129. void SetMinEmissionRate(float rate);
  130. /// Set maximum emission rate.
  131. void SetMaxEmissionRate(float rate);
  132. /// Set emitter type.
  133. void SetEmitterType(EmitterType type);
  134. /// Set emitter size.
  135. void SetEmitterSize(const Vector3& size);
  136. /// Set emission active period length (0 = infinite.)
  137. void SetActiveTime(float time);
  138. /// Set emission inactive period length (0 = infinite.)
  139. void SetInactiveTime(float time);
  140. /// Set whether should be emitting and optionally reset emission period.
  141. void SetEmitting(bool enable, bool resetPeriod = false);
  142. /// Set whether to update when particles are not visible.
  143. void SetUpdateInvisible(bool enable);
  144. /// Set particle time to live (both minimum and maximum.)
  145. void SetTimeToLive(float time);
  146. /// Set particle minimum time to live.
  147. void SetMinTimeToLive(float time);
  148. /// Set particle maximum time to live.
  149. void SetMaxTimeToLive(float time);
  150. /// Set particle size (both minimum and maximum.)
  151. void SetParticleSize(const Vector2& size);
  152. /// Set particle minimum size.
  153. void SetMinParticleSize(const Vector2& size);
  154. /// Set particle maximum size.
  155. void SetMaxParticleSize(const Vector2& size);
  156. /// Set negative direction limit.
  157. void SetMinDirection(const Vector3& direction);
  158. /// Set positive direction limit.
  159. void SetMaxDirection(const Vector3& direction);
  160. /// Set particle velocity (both minimum and maximum.)
  161. void SetVelocity(float velocity);
  162. /// Set particle minimum velocity.
  163. void SetMinVelocity(float velocity);
  164. /// Set particle maximum velocity.
  165. void SetMaxVelocity(float velocity);
  166. /// Set particle rotation (both minimum and maximum.)
  167. void SetRotation(float rotation);
  168. /// Set particle minimum rotation.
  169. void SetMinRotation(float rotation);
  170. /// Set particle maximum rotation.
  171. void SetMaxRotation(float rotation);
  172. /// Set particle rotation speed (both minimum and maximum.)
  173. void SetRotationSpeed(float speed);
  174. /// Set particle minimum rotation speed.
  175. void SetMinRotationSpeed(float speed);
  176. /// Set particle maximum rotation speed.
  177. void SetMaxRotationSpeed(float speed);
  178. /// Set constant force acting on particles.
  179. void SetConstantForce(const Vector3& force);
  180. /// Set particle velocity damping force.
  181. void SetDampingForce(float force);
  182. /// Set particle size additive modifier.
  183. void SetSizeAdd(float sizeAdd);
  184. /// Set particle size multiplicative modifier.
  185. void SetSizeMul(float sizeMul);
  186. /// Set color of particles.
  187. void SetColor(const Color& color);
  188. /// Set color animation of particles.
  189. void SetColors(const Vector<ColorFrame>& colors);
  190. /// Set number of color animation frames.
  191. void SetNumColors(unsigned num);
  192. /// Set particle texture animation.
  193. void SetTextureFrames(const Vector<TextureFrame>& animation);
  194. /// Set number of texture animation frames.
  195. void SetNumTextureFrames(unsigned num);
  196. /// Return maximum number of particles.
  197. unsigned GetNumParticles() const { return particles_.Size(); }
  198. /// Return whether is currently emitting.
  199. bool IsEmitting() const { return emitting_; }
  200. /// Return whether to update when particles are not visible.
  201. bool GetUpdateInvisible() const { return updateInvisible_; }
  202. /// Return minimum emission rate.
  203. float GetMinEmissionRate() const { return emissionRateMin_; }
  204. /// Return maximum emission rate.
  205. float GetMaxEmissionRate() const { return emissionRateMax_; }
  206. /// Return emitter type.
  207. EmitterType GetEmitterType() const { return emitterType_; }
  208. /// Return emitter size.
  209. const Vector3& GetEmitterSize() const { return emitterSize_; }
  210. /// Return emission active period length (0 = infinite.)
  211. float GetActiveTime() const { return activeTime_; }
  212. /// Return emission inactive period length (0 = infinite.)
  213. float GetInactiveTime() const { return inactiveTime_; }
  214. /// Return particle minimum time to live.
  215. float GetMinTimeToLive() const { return timeToLiveMin_; }
  216. /// Return particle maximum time to live.
  217. float GetMaxTimeToLive() const { return timeToLiveMax_; }
  218. /// Return particle minimum size.
  219. const Vector2& GetMinParticleSize() const { return sizeMin_; }
  220. /// Return particle maximum size.
  221. const Vector2& GetMaxParticleSize() const { return sizeMax_; }
  222. /// Return negative direction limit.
  223. const Vector3& GetMinDirection() const { return directionMin_; }
  224. /// Return positive direction limit.
  225. const Vector3& GetMaxDirection() const { return directionMax_; }
  226. /// Return particle minimum velocity.
  227. float GetMinVelocity() const { return velocityMin_; }
  228. /// Return particle maximum velocity.
  229. float GetMaxVelocity() const { return velocityMax_; }
  230. /// Return particle minimum rotation.
  231. float GetMinRotation() const { return rotationMin_; }
  232. /// Return particle maximum rotation.
  233. float GetMaxRotation() const { return rotationMax_; }
  234. /// Return particle minimum rotation speed.
  235. float GetMinRotationSpeed() const { return rotationSpeedMin_; }
  236. /// Return particle maximum rotation speed.
  237. float GetMaxRotationSpeed() const { return rotationSpeedMax_; }
  238. /// Return constant force acting on particles.
  239. const Vector3& GetConstantForce() const { return constantForce_; }
  240. /// Return particle velocity damping force.
  241. float GetDampingForce() const { return dampingForce_; }
  242. /// Return particle size additive modifier.
  243. float GetSizeAdd() const { return sizeAdd_; }
  244. /// Return particle size multiplicative modifier.
  245. float GetSizeMul() const { return sizeMul_; }
  246. /// Return all color animation frames.
  247. Vector<ColorFrame>& GetColors() { return colorFrames_; }
  248. /// Return number of color animation frames.
  249. unsigned GetNumColors() const { return colorFrames_.Size(); }
  250. /// Return a color animation frame, or null if outside range.
  251. ColorFrame* GetColor(unsigned index) { return index < colorFrames_.Size() ? &colorFrames_[index] : (ColorFrame*)0; }
  252. /// Return all texture animation frames.
  253. Vector<TextureFrame>& GetTextureFrame() { return textureFrames_; }
  254. /// Return number of texture animation frames.
  255. unsigned GetNumTextureFrames() const { return textureFrames_.Size(); }
  256. /// Return a texture animation frame, or null if outside range.
  257. TextureFrame* GetTextureFrame(unsigned index) { return index < colorFrames_.Size() ? &textureFrames_[index] : (TextureFrame*)0; }
  258. /// Set particles attribute.
  259. void SetParticlesAttr(VariantVector value);
  260. /// Return particles attribute.
  261. VariantVector GetParticlesAttr() const;
  262. /// Set particle colors attribute.
  263. void SetColorsAttr(VariantVector value);
  264. /// Return particle colors attribute.
  265. VariantVector GetColorsAttr() const;
  266. /// Set texture animation attribute.
  267. void SetTextureFramesAttr(VariantVector value);
  268. /// Return texture animation attribute.
  269. VariantVector GetTextureFramesAttr() const;
  270. protected:
  271. /// Handle node being assigned.
  272. virtual void OnNodeSet(Node* node);
  273. /// Create a new particle. Return true if there was room.
  274. bool EmitNewParticle();
  275. /// Return a free particle index.
  276. unsigned GetFreeParticle() const;
  277. /// Read a float range from an XML element.
  278. void GetFloatMinMax(const XMLElement& element, float& minValue, float& maxValue);
  279. /// Read a Vector2 range from an XML element.
  280. void GetVector2MinMax(const XMLElement& element, Vector2& minValue, Vector2& maxValue);
  281. /// Read a Vector3 from an XML element.
  282. void GetVector3MinMax(const XMLElement& element, Vector3& minValue, Vector3& maxValue);
  283. private:
  284. /// Handle scene post-update event.
  285. void HandleScenePostUpdate(StringHash eventType, VariantMap& eventData);
  286. /// Particles.
  287. PODVector<Particle> particles_;
  288. /// Particle color animation frames.
  289. Vector<ColorFrame> colorFrames_;
  290. /// Texture animation frames.
  291. Vector<TextureFrame> textureFrames_;
  292. /// Emitter shape.
  293. EmitterType emitterType_;
  294. /// Emitter size.
  295. Vector3 emitterSize_;
  296. /// Particle direction minimum.
  297. Vector3 directionMin_;
  298. /// Particle direction maximum.
  299. Vector3 directionMax_;
  300. /// Particle constant force.
  301. Vector3 constantForce_;
  302. /// Particle size minimum.
  303. Vector2 sizeMin_;
  304. /// Particle size maximum.
  305. Vector2 sizeMax_;
  306. /// Particle velocity damping force.
  307. float dampingForce_;
  308. /// Active/inactive period timer.
  309. float periodTimer_;
  310. /// New particle emission timer.
  311. float emissionTimer_;
  312. /// Active period.
  313. float activeTime_;
  314. /// Inactive period.
  315. float inactiveTime_;
  316. /// Particles per second minimum.
  317. float emissionRateMin_;
  318. /// Particles per second maximum.
  319. float emissionRateMax_;
  320. /// Particle time to live minimum.
  321. float timeToLiveMin_;
  322. /// Particle time to live maximum.
  323. float timeToLiveMax_;
  324. /// Particle velocity minimum.
  325. float velocityMin_;
  326. /// Particle velocity maximum.
  327. float velocityMax_;
  328. /// Particle rotation angle minimum.
  329. float rotationMin_;
  330. /// Particle rotation angle maximum.
  331. float rotationMax_;
  332. /// Particle rotation speed minimum.
  333. float rotationSpeedMin_;
  334. /// Particle rotation speed maximum.
  335. float rotationSpeedMax_;
  336. /// Particle size additive parameter.
  337. float sizeAdd_;
  338. /// Particle size multiplicative parameter.
  339. float sizeMul_;
  340. /// Last scene timestep.
  341. float lastTimeStep_;
  342. /// Rendering framenumber on which was last updated.
  343. unsigned lastUpdateFrameNumber_;
  344. /// Currently emitting flag.
  345. bool emitting_;
  346. /// Update when invisible flag.
  347. bool updateInvisible_;
  348. /// Need update flag.
  349. bool needUpdate_;
  350. };
  351. }