ParticleEffect.h 15 KB

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