ParticleEffect.h 14 KB

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