ParticleEffect.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  1. // Copyright (c) 2008-2023 the Urho3D project
  2. // License: MIT
  3. /// \file
  4. #pragma once
  5. #include "../GraphicsAPI/GraphicsDefs.h"
  6. #include "../Resource/Resource.h"
  7. namespace Urho3D
  8. {
  9. /// Particle emitter shapes.
  10. enum EmitterType
  11. {
  12. EMITTER_SPHERE = 0,
  13. EMITTER_BOX,
  14. EMITTER_SPHEREVOLUME,
  15. EMITTER_CYLINDER,
  16. EMITTER_RING
  17. };
  18. /// %Color animation frame definition.
  19. /// @nocount
  20. struct ColorFrame
  21. {
  22. /// Construct with default values.
  23. ColorFrame() :
  24. time_(0.0f)
  25. {
  26. }
  27. /// Construct with a color and zero time.
  28. explicit ColorFrame(const Color& color) :
  29. color_(color),
  30. time_(0.0f)
  31. {
  32. }
  33. /// Construct from a color and time.
  34. ColorFrame(const Color& color, float time) :
  35. color_(color),
  36. time_(time)
  37. {
  38. }
  39. /// Return interpolated value with another color-time pair at the time specified.
  40. Color Interpolate(const ColorFrame& next, float time) const
  41. {
  42. float timeInterval = next.time_ - time_;
  43. if (timeInterval > 0.0f)
  44. {
  45. float t = (time - time_) / timeInterval;
  46. return color_.Lerp(next.color_, t);
  47. }
  48. else
  49. return next.color_;
  50. }
  51. /// Color.
  52. Color color_;
  53. /// Time.
  54. float time_;
  55. };
  56. /// %Texture animation frame definition.
  57. /// @nocount
  58. struct TextureFrame
  59. {
  60. /// Construct with default values.
  61. TextureFrame() :
  62. uv_(0.0f, 0.0f, 1.0f, 1.0f),
  63. time_(0.0f)
  64. {
  65. }
  66. /// UV coordinates.
  67. Rect uv_;
  68. /// Time.
  69. float time_;
  70. };
  71. static const unsigned DEFAULT_NUM_PARTICLES = 10;
  72. class Material;
  73. class XMLFile;
  74. class XMLElement;
  75. /// %Particle effect definition.
  76. class URHO3D_API ParticleEffect : public Resource
  77. {
  78. URHO3D_OBJECT(ParticleEffect, Resource);
  79. public:
  80. /// Construct.
  81. explicit ParticleEffect(Context* context);
  82. /// Destruct.
  83. ~ParticleEffect() override;
  84. /// Register object factory.
  85. /// @nobind
  86. static void RegisterObject(Context* context);
  87. /// Load resource from stream. May be called from a worker thread. Return true if successful.
  88. bool BeginLoad(Deserializer& source) override;
  89. /// Finish resource loading. Always called from the main thread. Return true if successful.
  90. bool EndLoad() override;
  91. /// Save resource. Return true if successful.
  92. bool Save(Serializer& dest) const override;
  93. using Resource::Load;
  94. /// Save resource to XMLElement. Return true if successful.
  95. bool Save(XMLElement& dest) const;
  96. /// Load resource from XMLElement synchronously. Return true if successful.
  97. bool Load(const XMLElement& source);
  98. /// Set material.
  99. /// @property
  100. void SetMaterial(Material* material);
  101. /// Set maximum number of particles.
  102. /// @property
  103. void SetNumParticles(unsigned num);
  104. /// Set whether to update when particles are not visible.
  105. /// @property
  106. void SetUpdateInvisible(bool enable);
  107. /// Set whether billboards are relative to the scene node.
  108. /// @property
  109. void SetRelative(bool enable);
  110. /// Set whether scene node scale affects billboards' size.
  111. /// @property
  112. void SetScaled(bool enable);
  113. /// Set whether billboards are sorted by distance.
  114. /// @property
  115. void SetSorted(bool enable);
  116. /// Set whether billboards have fixed size on screen (measured in pixels) regardless of distance to camera.
  117. /// @property
  118. void SetFixedScreenSize(bool enable);
  119. /// Set animation LOD bias.
  120. /// @property
  121. void SetAnimationLodBias(float lodBias);
  122. /// Set emitter type.
  123. /// @property
  124. void SetEmitterType(EmitterType type);
  125. /// Set emitter size.
  126. /// @property
  127. void SetEmitterSize(const Vector3& size);
  128. /// Set negative direction limit.
  129. /// @property
  130. void SetMinDirection(const Vector3& direction);
  131. /// Set positive direction limit.
  132. /// @property
  133. void SetMaxDirection(const Vector3& direction);
  134. /// Set constant force acting on particles.
  135. /// @property
  136. void SetConstantForce(const Vector3& force);
  137. /// Set particle velocity damping force.
  138. /// @property
  139. void SetDampingForce(float force);
  140. /// Set emission active period length (0 = infinite).
  141. /// @property
  142. void SetActiveTime(float time);
  143. /// Set emission inactive period length (0 = infinite).
  144. /// @property
  145. void SetInactiveTime(float time);
  146. /// Set minimum emission rate.
  147. /// @property
  148. void SetMinEmissionRate(float rate);
  149. /// Set maximum emission rate.
  150. /// @property
  151. void SetMaxEmissionRate(float rate);
  152. /// Set particle minimum size.
  153. /// @property
  154. void SetMinParticleSize(const Vector2& size);
  155. /// Set particle maximum size.
  156. /// @property
  157. void SetMaxParticleSize(const Vector2& size);
  158. /// Set particle minimum time to live.
  159. /// @property
  160. void SetMinTimeToLive(float time);
  161. /// Set particle maximum time to live.
  162. /// @property
  163. void SetMaxTimeToLive(float time);
  164. /// Set particle minimum velocity.
  165. /// @property
  166. void SetMinVelocity(float velocity);
  167. /// Set particle maximum velocity.
  168. /// @property
  169. void SetMaxVelocity(float velocity);
  170. /// Set particle minimum rotation.
  171. /// @property
  172. void SetMinRotation(float rotation);
  173. /// Set particle maximum rotation.
  174. /// @property
  175. void SetMaxRotation(float rotation);
  176. /// Set particle minimum rotation speed.
  177. /// @property
  178. void SetMinRotationSpeed(float speed);
  179. /// Set particle maximum rotation speed.
  180. /// @property
  181. void SetMaxRotationSpeed(float speed);
  182. /// Set particle size additive modifier.
  183. /// @property
  184. void SetSizeAdd(float sizeAdd);
  185. /// Set particle size multiplicative modifier.
  186. /// @property
  187. void SetSizeMul(float sizeMul);
  188. /// Set how the particles should rotate in relation to the camera. Default is to follow camera rotation on all axes (FC_ROTATE_XYZ).
  189. /// @property
  190. void SetFaceCameraMode(FaceCameraMode mode);
  191. /// Add a color frame sorted in the correct position based on time.
  192. void AddColorTime(const Color& color, float time);
  193. /// Add a color frame sorted in the correct position based on time.
  194. void AddColorFrame(const ColorFrame& colorFrame);
  195. /// Remove color frame at index.
  196. void RemoveColorFrame(unsigned index);
  197. /// Set color animation of particles.
  198. /// @nobindtemp
  199. void SetColorFrames(const Vector<ColorFrame>& colorFrames);
  200. /// Set color animation frame at index. If index is greater than number of color frames, new color frames are added.
  201. void SetColorFrame(unsigned index, const ColorFrame& colorFrame);
  202. /// Set number of color frames.
  203. /// @property
  204. void SetNumColorFrames(unsigned number);
  205. /// Sort the list of color frames based on time.
  206. void SortColorFrames();
  207. /// Add a texture frame sorted in the correct position based on time.
  208. void AddTextureTime(const Rect& uv, float time);
  209. /// Add a texture frame sorted in the correct position based on time.
  210. void AddTextureFrame(const TextureFrame& textureFrame);
  211. /// Remove texture frame at index.
  212. void RemoveTextureFrame(unsigned index);
  213. /// Set particle texture animation.
  214. /// @nobindtemp
  215. void SetTextureFrames(const Vector<TextureFrame>& textureFrames);
  216. /// Set number of texture animation frames.
  217. void SetTextureFrame(unsigned index, const TextureFrame& textureFrame);
  218. /// Set number of texture frames.
  219. /// @property
  220. void SetNumTextureFrames(unsigned number);
  221. /// Sort the list of texture frames based on time.
  222. void SortTextureFrames();
  223. /// Clone the particle effect.
  224. SharedPtr<ParticleEffect> Clone(const String& cloneName = String::EMPTY) const;
  225. /// Return material.
  226. /// @property
  227. Material* GetMaterial() const { return material_; }
  228. /// Return maximum number of particles.
  229. /// @property
  230. unsigned GetNumParticles() const { return numParticles_; }
  231. /// Return whether to update when particles are not visible.
  232. /// @property
  233. bool GetUpdateInvisible() const { return updateInvisible_; }
  234. /// Return whether billboards are relative to the scene node.
  235. /// @property
  236. bool IsRelative() const { return relative_; }
  237. /// Return whether scene node scale affects billboards' size.
  238. /// @property
  239. bool IsScaled() const { return scaled_; }
  240. /// Return whether billboards are sorted.
  241. /// @property
  242. bool IsSorted() const { return sorted_; }
  243. /// Return whether billboards are fixed screen size.
  244. /// @property
  245. bool IsFixedScreenSize() const { return fixedScreenSize_; }
  246. /// Return animation Lod bias.
  247. /// @property
  248. float GetAnimationLodBias() const { return animationLodBias_; }
  249. /// Return emitter type.
  250. /// @property
  251. EmitterType GetEmitterType() const { return emitterType_; }
  252. /// Return emitter size.
  253. /// @property
  254. const Vector3& GetEmitterSize() const { return emitterSize_; }
  255. /// Return negative direction limit.
  256. /// @property
  257. const Vector3& GetMinDirection() const { return directionMin_; }
  258. /// Return positive direction limit.
  259. /// @property
  260. const Vector3& GetMaxDirection() const { return directionMax_; }
  261. /// Return constant force acting on particles.
  262. /// @property
  263. const Vector3& GetConstantForce() const { return constantForce_; }
  264. /// Return particle velocity damping force.
  265. /// @property
  266. float GetDampingForce() const { return dampingForce_; }
  267. /// Return emission active period length (0 = infinite).
  268. /// @property
  269. float GetActiveTime() const { return activeTime_; }
  270. /// Return emission inactive period length (0 = infinite).
  271. /// @property
  272. float GetInactiveTime() const { return inactiveTime_; }
  273. /// Return minimum emission rate.
  274. /// @property
  275. float GetMinEmissionRate() const { return emissionRateMin_; }
  276. /// Return maximum emission rate.
  277. /// @property
  278. float GetMaxEmissionRate() const { return emissionRateMax_; }
  279. /// Return particle minimum size.
  280. /// @property
  281. const Vector2& GetMinParticleSize() const { return sizeMin_; }
  282. /// Return particle maximum size.
  283. /// @property
  284. const Vector2& GetMaxParticleSize() const { return sizeMax_; }
  285. /// Return particle minimum time to live.
  286. /// @property
  287. float GetMinTimeToLive() const { return timeToLiveMin_; }
  288. /// Return particle maximum time to live.
  289. /// @property
  290. float GetMaxTimeToLive() const { return timeToLiveMax_; }
  291. /// Return particle minimum velocity.
  292. /// @property
  293. float GetMinVelocity() const { return velocityMin_; }
  294. /// Return particle maximum velocity.
  295. /// @property
  296. float GetMaxVelocity() const { return velocityMax_; }
  297. /// Return particle minimum rotation.
  298. /// @property
  299. float GetMinRotation() const { return rotationMin_; }
  300. /// Return particle maximum rotation.
  301. /// @property
  302. float GetMaxRotation() const { return rotationMax_; }
  303. /// Return particle minimum rotation speed.
  304. /// @property
  305. float GetMinRotationSpeed() const { return rotationSpeedMin_; }
  306. /// Return particle maximum rotation speed.
  307. /// @property
  308. float GetMaxRotationSpeed() const { return rotationSpeedMax_; }
  309. /// Return particle size additive modifier.
  310. /// @property
  311. float GetSizeAdd() const { return sizeAdd_; }
  312. /// Return particle size multiplicative modifier.
  313. /// @property
  314. float GetSizeMul() const { return sizeMul_; }
  315. /// Return all color animation frames.
  316. /// @nobindtemp
  317. const Vector<ColorFrame>& GetColorFrames() const { return colorFrames_; }
  318. /// Return number of color animation frames.
  319. /// @property
  320. unsigned GetNumColorFrames() const { return colorFrames_.Size(); }
  321. /// Return a color animation frame, or null if outside range.
  322. const ColorFrame* GetColorFrame(unsigned index) const;
  323. /// Return all texture animation frames.
  324. /// @nobindtemp
  325. const Vector<TextureFrame>& GetTextureFrames() const { return textureFrames_; }
  326. /// Return number of texture animation frames.
  327. /// @property
  328. unsigned GetNumTextureFrames() const { return textureFrames_.Size(); }
  329. /// Return a texture animation frame, or null if outside range.
  330. const TextureFrame* GetTextureFrame(unsigned index) const;
  331. /// Return how the particles rotate in relation to the camera.
  332. /// @property
  333. FaceCameraMode GetFaceCameraMode() const { return faceCameraMode_; }
  334. /// Return random direction.
  335. Vector3 GetRandomDirection() const;
  336. /// Return random size.
  337. Vector2 GetRandomSize() const;
  338. /// Return random velocity.
  339. float GetRandomVelocity() const;
  340. /// Return random timetolive.
  341. float GetRandomTimeToLive() const;
  342. /// Return random rotationspeed.
  343. float GetRandomRotationSpeed() const;
  344. /// Return random rotation.
  345. float GetRandomRotation() const;
  346. private:
  347. /// Read a float range from an XML element.
  348. void GetFloatMinMax(const XMLElement& element, float& minValue, float& maxValue);
  349. /// Read a Vector2 range from an XML element.
  350. void GetVector2MinMax(const XMLElement& element, Vector2& minValue, Vector2& maxValue);
  351. /// Read a Vector3 from an XML element.
  352. void GetVector3MinMax(const XMLElement& element, Vector3& minValue, Vector3& maxValue);
  353. /// Material.
  354. SharedPtr<Material> material_;
  355. /// Number of particles.
  356. unsigned numParticles_;
  357. /// Update when invisible flag.
  358. bool updateInvisible_;
  359. /// Billboards relative flag.
  360. bool relative_;
  361. /// Scale affects billboard scale flag.
  362. bool scaled_;
  363. /// Billboards sorted flag.
  364. bool sorted_;
  365. /// Billboards fixed screen size flag.
  366. bool fixedScreenSize_;
  367. /// Animation LOD bias.
  368. float animationLodBias_;
  369. /// Emitter shape.
  370. EmitterType emitterType_;
  371. /// Emitter size.
  372. Vector3 emitterSize_;
  373. /// Particle direction minimum.
  374. Vector3 directionMin_;
  375. /// Particle direction maximum.
  376. Vector3 directionMax_;
  377. /// Particle constant force.
  378. Vector3 constantForce_;
  379. /// Particle velocity damping force.
  380. float dampingForce_;
  381. /// Active period.
  382. float activeTime_;
  383. /// Inactive period.
  384. float inactiveTime_;
  385. /// Particles per second minimum.
  386. float emissionRateMin_;
  387. /// Particles per second maximum.
  388. float emissionRateMax_;
  389. /// Particle size minimum.
  390. Vector2 sizeMin_;
  391. /// Particle size maximum.
  392. Vector2 sizeMax_;
  393. /// Particle time to live minimum.
  394. float timeToLiveMin_;
  395. /// Particle time to live maximum.
  396. float timeToLiveMax_;
  397. /// Particle velocity minimum.
  398. float velocityMin_;
  399. /// Particle velocity maximum.
  400. float velocityMax_;
  401. /// Particle rotation angle minimum.
  402. float rotationMin_;
  403. /// Particle rotation angle maximum.
  404. float rotationMax_;
  405. /// Particle rotation speed minimum.
  406. float rotationSpeedMin_;
  407. /// Particle rotation speed maximum.
  408. float rotationSpeedMax_;
  409. /// Particle size additive parameter.
  410. float sizeAdd_;
  411. /// Particle size multiplicative parameter.
  412. float sizeMul_;
  413. /// Particle color animation frames.
  414. Vector<ColorFrame> colorFrames_;
  415. /// Texture animation frames.
  416. Vector<TextureFrame> textureFrames_;
  417. /// Material name acquired during BeginLoad().
  418. String loadMaterialName_;
  419. /// Particle rotation mode in relation to the camera.
  420. FaceCameraMode faceCameraMode_;
  421. };
  422. }