ParticleEffect.h 12 KB

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