ParticleEffect.h 15 KB

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