ParticleEmitter.h 4.8 KB

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