ParticleEmitter.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. //
  2. // Copyright (c) 2008-2017 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 "../Graphics/BillboardSet.h"
  24. namespace Atomic
  25. {
  26. class ParticleEffect;
  27. /// One particle in the particle system.
  28. struct Particle
  29. {
  30. /// Velocity.
  31. Vector3 velocity_;
  32. /// Original billboard size.
  33. Vector2 size_;
  34. /// Time elapsed from creation.
  35. float timer_;
  36. /// Lifetime.
  37. float timeToLive_;
  38. /// Size scaling value.
  39. float scale_;
  40. /// Rotation speed.
  41. float rotationSpeed_;
  42. /// Current color animation index.
  43. unsigned colorIndex_;
  44. /// Current texture animation index.
  45. unsigned texIndex_;
  46. };
  47. /// %Particle emitter component.
  48. class ATOMIC_API ParticleEmitter : public BillboardSet
  49. {
  50. ATOMIC_OBJECT(ParticleEmitter, BillboardSet);
  51. public:
  52. /// Construct.
  53. ParticleEmitter(Context* context);
  54. /// Destruct.
  55. virtual ~ParticleEmitter();
  56. /// Register object factory.
  57. static void RegisterObject(Context* context);
  58. /// Handle enabled/disabled state change.
  59. virtual void OnSetEnabled();
  60. /// Update before octree reinsertion. Is called from a worker thread.
  61. virtual void Update(const FrameInfo& frame);
  62. /// Set particle effect.
  63. void SetEffect(ParticleEffect* effect);
  64. /// Set maximum number of particles.
  65. void SetNumParticles(unsigned num);
  66. /// Set whether should be emitting. If the state was changed, also resets the emission period timer.
  67. void SetEmitting(bool enable);
  68. /// Set whether particles should be serialized. Default true, set false to reduce scene file size.
  69. void SetSerializeParticles(bool enable);
  70. //// Set to remove either the emitter component or its owner node from the scene automatically on particle effect completion. Disabled by default.
  71. void SetAutoRemoveMode(AutoRemoveMode mode);
  72. /// Reset the emission period timer.
  73. void ResetEmissionTimer();
  74. /// Remove all current particles.
  75. void RemoveAllParticles();
  76. /// Reset the particle emitter completely. Removes current particles, sets emitting state on, and resets the emission timer.
  77. void Reset();
  78. /// Apply not continuously updated values such as the material, the number of particles and sorting mode from the particle effect. Call this if you change the effect programmatically.
  79. void ApplyEffect();
  80. /// Return particle effect.
  81. ParticleEffect* GetEffect() const;
  82. /// Return maximum number of particles.
  83. unsigned GetNumParticles() const { return particles_.Size(); }
  84. /// Return whether is currently emitting.
  85. bool IsEmitting() const { return emitting_; }
  86. /// Return whether particles are to be serialized.
  87. bool GetSerializeParticles() const { return serializeParticles_; }
  88. /// Return automatic removal mode on particle effect completion.
  89. AutoRemoveMode GetAutoRemoveMode() const { return autoRemove_; }
  90. /// Set particles effect attribute.
  91. void SetEffectAttr(const ResourceRef& value);
  92. /// Set particles effect attribute.
  93. ResourceRef GetEffectAttr() const;
  94. /// Set particles attribute.
  95. void SetParticlesAttr(const VariantVector& value);
  96. /// Return particles attribute. Returns particle amount only if particles are not to be serialized.
  97. VariantVector GetParticlesAttr() const;
  98. /// Return billboards attribute. Returns billboard amount only if particles are not to be serialized.
  99. VariantVector GetParticleBillboardsAttr() const;
  100. protected:
  101. /// Handle scene being assigned.
  102. virtual void OnSceneSet(Scene* scene);
  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. /// Return whether has active particles.
  108. bool CheckActiveParticles() const;
  109. private:
  110. /// Handle scene post-update event.
  111. void HandleScenePostUpdate(StringHash eventType, VariantMap& eventData);
  112. /// Handle live reload of the particle effect.
  113. void HandleEffectReloadFinished(StringHash eventType, VariantMap& eventData);
  114. /// Particle effect.
  115. SharedPtr<ParticleEffect> effect_;
  116. /// Particles.
  117. PODVector<Particle> particles_;
  118. /// Active/inactive period timer.
  119. float periodTimer_;
  120. /// New particle emission timer.
  121. float emissionTimer_;
  122. /// Last scene timestep.
  123. float lastTimeStep_;
  124. /// Rendering framenumber on which was last updated.
  125. unsigned lastUpdateFrameNumber_;
  126. /// Currently emitting flag.
  127. bool emitting_;
  128. /// Need update flag.
  129. bool needUpdate_;
  130. /// Serialize particles flag.
  131. bool serializeParticles_;
  132. /// Ready to send effect finish event flag.
  133. bool sendFinishedEvent_;
  134. /// Automatic removal mode.
  135. AutoRemoveMode autoRemove_;
  136. };
  137. }