ParticleEffect.h 16 KB

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