ParticleEffect.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. //
  2. // Copyright (c) 2008-2014 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.h"
  24. namespace Urho3D
  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. class Material;
  84. class XMLFile;
  85. class XMLElement;
  86. /// %Particle emitter component.
  87. class URHO3D_API ParticleEffect : public Resource
  88. {
  89. OBJECT(ParticleEffect);
  90. friend class ParticleEmitter;
  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. Return true if successful.
  99. virtual bool Load(Deserializer& source);
  100. /// Save resource. Return true if successful.
  101. virtual bool Save(Serializer& dest) const;
  102. /// Set material.
  103. void SetMaterial(Material* material);
  104. /// Set maximum number of particles.
  105. void SetNumParticles(unsigned num);
  106. /// Set whether to update when particles are not visible.
  107. void SetUpdateInvisible(bool enable);
  108. /// Set whether billboards are relative to the scene node. Default true.
  109. void SetRelative(bool enable);
  110. /// Set scaled.
  111. void SetScaled(bool enable);
  112. /// Set sorted.
  113. void SetSorted(bool enable);
  114. /// Set animation LOD bias.
  115. void SetAnimationLodBias(float lodBias);
  116. /// Set emitter type.
  117. void SetEmitterType(EmitterType type);
  118. /// Set emitter size.
  119. void SetEmitterSize(const Vector3& size);
  120. /// Set negative direction limit.
  121. void SetMinDirection(const Vector3& direction);
  122. /// Set positive direction limit.
  123. void SetMaxDirection(const Vector3& direction);
  124. /// Set constant force acting on particles.
  125. void SetConstantForce(const Vector3& force);
  126. /// Set particle velocity damping force.
  127. void SetDampingForce(float force);
  128. /// Set emission active period length (0 = infinite.)
  129. void SetActiveTime(float time);
  130. /// Set emission inactive period length (0 = infinite.)
  131. void SetInactiveTime(float time);
  132. /// Set minimum emission rate.
  133. void SetMinEmissionRate(float rate);
  134. /// Set maximum emission rate.
  135. void SetMaxEmissionRate(float rate);
  136. /// Set particle minimum size.
  137. void SetMinParticleSize(const Vector2& size);
  138. /// Set particle maximum size.
  139. void SetMaxParticleSize(const Vector2& size);
  140. /// Set particle minimum time to live.
  141. void SetMinTimeToLive(float time);
  142. /// Set particle maximum time to live.
  143. void SetMaxTimeToLive(float time);
  144. /// Set particle minimum velocity.
  145. void SetMinVelocity(float velocity);
  146. /// Set particle maximum velocity.
  147. void SetMaxVelocity(float velocity);
  148. /// Set particle minimum rotation.
  149. void SetMinRotation(float rotation);
  150. /// Set particle maximum rotation.
  151. void SetMaxRotation(float rotation);
  152. /// Set particle minimum rotation speed.
  153. void SetMinRotationSpeed(float speed);
  154. /// Set particle maximum rotation speed.
  155. void SetMaxRotationSpeed(float speed);
  156. /// Set particle size additive modifier.
  157. void SetSizeAdd(float sizeAdd);
  158. /// Set particle size multiplicative modifier.
  159. void SetSizeMul(float sizeMul);
  160. /// Set color animation of particles.
  161. void SetColorFrames(const Vector<ColorFrame>& colorFrames);
  162. /// Set number of color animation frames.
  163. void SetColorFrame(unsigned index, const ColorFrame& colorFrame);
  164. /// Set particle texture animation.
  165. void SetTextureFrames(const Vector<TextureFrame>& animation);
  166. /// Set number of texture animation frames.
  167. void SetTextureFrame(unsigned index, const TextureFrame& textureFrame);
  168. /// Return material.
  169. Material* GetMaterial() const { return material_; }
  170. /// Return maximum number of particles.
  171. unsigned GetNumParticles() const { return numParticles_; }
  172. /// Return whether to update when particles are not visible.
  173. bool GetUpdateInvisible() const { return updateInvisible_; }
  174. /// Return whether billboards are relative to the scene node.
  175. bool IsRelative() const { return relative_; }
  176. /// Return whether scene node scale affects billboards' size.
  177. bool IsScaled() const { return scaled_; }
  178. /// Return whether billboards are sorted.
  179. bool IsSorted() const { return sorted_; }
  180. /// Return animation Lod bias.
  181. float GetAnimationLodBias() const { return animationLodBias_; }
  182. /// Return emitter type.
  183. EmitterType GetEmitterType() const { return emitterType_; }
  184. /// Return emitter size.
  185. const Vector3& GetEmitterSize() const { return emitterSize_; }
  186. /// Return negative direction limit.
  187. const Vector3& GetMinDirection() const { return directionMin_; }
  188. /// Return positive direction limit.
  189. const Vector3& GetMaxDirection() const { return directionMax_; }
  190. /// Return constant force acting on particles.
  191. const Vector3& GetConstantForce() const { return constantForce_; }
  192. /// Return particle velocity damping force.
  193. float GetDampingForce() const { return dampingForce_; }
  194. /// Return emission active period length (0 = infinite.)
  195. float GetActiveTime() const { return activeTime_; }
  196. /// Return emission inactive period length (0 = infinite.)
  197. float GetInactiveTime() const { return inactiveTime_; }
  198. /// Return minimum emission rate.
  199. float GetMinEmissionRate() const { return emissionRateMin_; }
  200. /// Return maximum emission rate.
  201. float GetMaxEmissionRate() const { return emissionRateMax_; }
  202. /// Return particle minimum size.
  203. const Vector2& GetMinParticleSize() const { return sizeMin_; }
  204. /// Return particle maximum size.
  205. const Vector2& GetMaxParticleSize() const { return sizeMax_; }
  206. /// Return particle minimum time to live.
  207. float GetMinTimeToLive() const { return timeToLiveMin_; }
  208. /// Return particle maximum time to live.
  209. float GetMaxTimeToLive() const { return timeToLiveMax_; }
  210. /// Return particle minimum velocity.
  211. float GetMinVelocity() const { return velocityMin_; }
  212. /// Return particle maximum velocity.
  213. float GetMaxVelocity() const { return velocityMax_; }
  214. /// Return particle minimum rotation.
  215. float GetMinRotation() const { return rotationMin_; }
  216. /// Return particle maximum rotation.
  217. float GetMaxRotation() const { return rotationMax_; }
  218. /// Return particle minimum rotation speed.
  219. float GetMinRotationSpeed() const { return rotationSpeedMin_; }
  220. /// Return particle maximum rotation speed.
  221. float GetMaxRotationSpeed() const { return rotationSpeedMax_; }
  222. /// Return particle size additive modifier.
  223. float GetSizeAdd() const { return sizeAdd_; }
  224. /// Return particle size multiplicative modifier.
  225. float GetSizeMul() const { return sizeMul_; }
  226. /// Return all color animation frames.
  227. const Vector<ColorFrame>& GetColorFrames() const { return colorFrames_; }
  228. /// Return number of color animation frames.
  229. unsigned GetNumColorFrames() const { return colorFrames_.Size(); }
  230. /// Return a color animation frame, or null if outside range.
  231. const ColorFrame* GetColorFrame(unsigned index) const;
  232. /// Return all texture animation frames.
  233. const Vector<TextureFrame>& GetTextureFrames() const { return textureFrames_; }
  234. /// Return number of texture animation frames.
  235. unsigned GetNumTextureFrames() const { return textureFrames_.Size(); }
  236. /// Return a texture animation frame, or null if outside range.
  237. const TextureFrame* GetTextureFrame(unsigned index) const;
  238. /// Return random direction.
  239. Vector3 GetRandomDirection() const;
  240. /// Return random size.
  241. Vector2 GetRandomSize() const;
  242. /// Return random velocity.
  243. float GetRandomVelocity() const;
  244. /// Return random timetolive.
  245. float GetRandomTimeToLive() const;
  246. /// Return random rotationspeed.
  247. float GetRandomRotationSpeed() const;
  248. /// Return random rotation.
  249. float GetRandomRotation() const;
  250. private:
  251. /// Read a float range from an XML element.
  252. void GetFloatMinMax(const XMLElement& element, float& minValue, float& maxValue);
  253. /// Read a Vector2 range from an XML element.
  254. void GetVector2MinMax(const XMLElement& element, Vector2& minValue, Vector2& maxValue);
  255. /// Read a Vector3 from an XML element.
  256. void GetVector3MinMax(const XMLElement& element, Vector3& minValue, Vector3& maxValue);
  257. /// Material.
  258. SharedPtr<Material> material_;
  259. /// Number of particles.
  260. unsigned numParticles_;
  261. /// Update when invisible flag.
  262. bool updateInvisible_;
  263. /// Billboards relative flag.
  264. bool relative_;
  265. /// Scale affects billboard scale flag.
  266. bool scaled_;
  267. /// Billboards sorted flag.
  268. bool sorted_;
  269. /// Animation LOD bias.
  270. float animationLodBias_;
  271. /// Emitter shape.
  272. EmitterType emitterType_;
  273. /// Emitter size.
  274. Vector3 emitterSize_;
  275. /// Particle direction minimum.
  276. Vector3 directionMin_;
  277. /// Particle direction maximum.
  278. Vector3 directionMax_;
  279. /// Particle constant force.
  280. Vector3 constantForce_;
  281. /// Particle velocity damping force.
  282. float dampingForce_;
  283. /// Active period.
  284. float activeTime_;
  285. /// Inactive period.
  286. float inactiveTime_;
  287. /// Particles per second minimum.
  288. float emissionRateMin_;
  289. /// Particles per second maximum.
  290. float emissionRateMax_;
  291. /// Particle size minimum.
  292. Vector2 sizeMin_;
  293. /// Particle size maximum.
  294. Vector2 sizeMax_;
  295. /// Particle time to live minimum.
  296. float timeToLiveMin_;
  297. /// Particle time to live maximum.
  298. float timeToLiveMax_;
  299. /// Particle velocity minimum.
  300. float velocityMin_;
  301. /// Particle velocity maximum.
  302. float velocityMax_;
  303. /// Particle rotation angle minimum.
  304. float rotationMin_;
  305. /// Particle rotation angle maximum.
  306. float rotationMax_;
  307. /// Particle rotation speed minimum.
  308. float rotationSpeedMin_;
  309. /// Particle rotation speed maximum.
  310. float rotationSpeedMax_;
  311. /// Particle size additive parameter.
  312. float sizeAdd_;
  313. /// Particle size multiplicative parameter.
  314. float sizeMul_;
  315. /// Particle color animation frames.
  316. Vector<ColorFrame> colorFrames_;
  317. /// Texture animation frames.
  318. Vector<TextureFrame> textureFrames_;
  319. };
  320. }