ParticleEffect.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599
  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. #include "Precompiled.h"
  23. #include "Context.h"
  24. #include "Log.h"
  25. #include "Material.h"
  26. #include "ParticleEffect.h"
  27. #include "ResourceCache.h"
  28. #include "XMLFile.h"
  29. #include "DebugNew.h"
  30. namespace Urho3D
  31. {
  32. static const char* emitterTypeNames[] =
  33. {
  34. "Sphere",
  35. "Box",
  36. 0
  37. };
  38. static const Vector2 DEFAULT_PARTICLE_SIZE(0.1f, 0.1f);
  39. static const float DEFAULT_EMISSION_RATE = 10.0f;
  40. static const float MIN_EMISSION_RATE = 0.01f;
  41. static const float DEFAULT_TIME_TO_LIVE = 1.0f;
  42. static const float DEFAULT_VELOCITY = 1.0f;
  43. static const Vector3 DEFAULT_DIRECTION_MIN(-1.0f, -1.0f, -1.0f);
  44. static const Vector3 DEFAULT_DIRECTION_MAX(1.0f, 1.0f, 1.0f);
  45. ParticleEffect::ParticleEffect(Context* context) :
  46. Resource(context),
  47. numParticles_(10),
  48. updateInvisible_(false),
  49. relative_(true),
  50. scaled_(true),
  51. sorted_(false),
  52. animationLodBias_(0.0f),
  53. emitterType_(EMITTER_SPHERE),
  54. emitterSize_(Vector3::ZERO),
  55. directionMin_(DEFAULT_DIRECTION_MIN),
  56. directionMax_(DEFAULT_DIRECTION_MAX),
  57. constantForce_(Vector3::ZERO),
  58. dampingForce_(0.0f),
  59. activeTime_(0.0f),
  60. inactiveTime_(0.0f),
  61. emissionRateMin_(DEFAULT_EMISSION_RATE),
  62. emissionRateMax_(DEFAULT_EMISSION_RATE),
  63. sizeMin_(DEFAULT_PARTICLE_SIZE),
  64. sizeMax_(DEFAULT_PARTICLE_SIZE),
  65. timeToLiveMin_(DEFAULT_TIME_TO_LIVE),
  66. timeToLiveMax_(DEFAULT_TIME_TO_LIVE),
  67. velocityMin_(DEFAULT_VELOCITY),
  68. velocityMax_(DEFAULT_VELOCITY),
  69. rotationMin_(0.0f),
  70. rotationMax_(0.0f),
  71. rotationSpeedMin_(0.0f),
  72. rotationSpeedMax_(0.0f),
  73. sizeAdd_(0.0f),
  74. sizeMul_(1.0f)
  75. {
  76. SetColor(Color::WHITE);
  77. }
  78. ParticleEffect::~ParticleEffect()
  79. {
  80. }
  81. void ParticleEffect::RegisterObject(Context* context)
  82. {
  83. context->RegisterFactory<ParticleEffect>();
  84. }
  85. bool ParticleEffect::Load(Deserializer& source)
  86. {
  87. XMLFile file(context_);
  88. if (!file.Load(source))
  89. {
  90. LOGERROR("Load particle effect file failed");
  91. return false;
  92. }
  93. XMLElement rootElem = file.GetRoot();
  94. if (!rootElem)
  95. {
  96. LOGERROR("Particle emitter parameter file does not have a valid root element");
  97. return false;
  98. }
  99. if (rootElem.HasChild("material"))
  100. SetMaterial(GetSubsystem<ResourceCache>()->GetResource<Material>(rootElem.GetChild("material").GetAttribute("name")));
  101. if (rootElem.HasChild("numparticles"))
  102. SetNumParticles(rootElem.GetChild("numparticles").GetInt("value"));
  103. if (rootElem.HasChild("updateinvisible"))
  104. updateInvisible_ = rootElem.GetChild("updateinvisible").GetBool("enable");
  105. if (rootElem.HasChild("relative"))
  106. relative_ = rootElem.GetChild("relative").GetBool("enable");
  107. if (rootElem.HasChild("scaled"))
  108. scaled_ = rootElem.GetChild("scaled").GetBool("enable");
  109. if (rootElem.HasChild("sorted"))
  110. sorted_ = rootElem.GetChild("sorted").GetBool("enable");
  111. if (rootElem.HasChild("animlodbias"))
  112. SetAnimationLodBias(rootElem.GetChild("animlodbias").GetFloat("value"));
  113. if (rootElem.HasChild("emittertype"))
  114. {
  115. String type = rootElem.GetChild("emittertype").GetAttributeLower("value");
  116. if (type == "point")
  117. {
  118. // Point emitter type is deprecated, handled as zero sized sphere
  119. emitterType_ = EMITTER_SPHERE;
  120. emitterSize_ = Vector3::ZERO;
  121. }
  122. else if (type == "box")
  123. emitterType_ = EMITTER_BOX;
  124. else if (type == "sphere")
  125. emitterType_ = EMITTER_SPHERE;
  126. else
  127. LOGERROR("Unknown particle emitter type " + type);
  128. }
  129. if (rootElem.HasChild("emittersize"))
  130. emitterSize_ = rootElem.GetChild("emittersize").GetVector3("value");
  131. if (rootElem.HasChild("emitterradius"))
  132. emitterSize_.x_ = emitterSize_.y_ = emitterSize_.z_ = rootElem.GetChild("emitterradius").GetFloat("value");
  133. if (rootElem.HasChild("direction"))
  134. GetVector3MinMax(rootElem.GetChild("direction"), directionMin_, directionMax_);
  135. if (rootElem.HasChild("constantforce"))
  136. constantForce_ = rootElem.GetChild("constantforce").GetVector3("value");
  137. if (rootElem.HasChild("dampingforce"))
  138. dampingForce_ = rootElem.GetChild("dampingforce").GetFloat("value");
  139. if (rootElem.HasChild("activetime"))
  140. activeTime_ = rootElem.GetChild("activetime").GetFloat("value");
  141. if (activeTime_ < 0.0f)
  142. activeTime_ = M_INFINITY;
  143. if (rootElem.HasChild("inactivetime"))
  144. inactiveTime_ = rootElem.GetChild("inactivetime").GetFloat("value");
  145. if (inactiveTime_ < 0.0f)
  146. inactiveTime_ = M_INFINITY;
  147. if (rootElem.HasChild("emissionrate"))
  148. GetFloatMinMax(rootElem.GetChild("emissionrate"), emissionRateMin_, emissionRateMax_);
  149. if (rootElem.HasChild("interval"))
  150. {
  151. float intervalMin = 0.0f;
  152. float intervalMax = 0.0f;
  153. GetFloatMinMax(rootElem.GetChild("interval"), intervalMin, intervalMax);
  154. emissionRateMax_ = 1.0f / intervalMin;
  155. emissionRateMin_ = 1.0f / intervalMax;
  156. }
  157. if (rootElem.HasChild("particlesize"))
  158. GetVector2MinMax(rootElem.GetChild("particlesize"), sizeMin_, sizeMax_);
  159. if (rootElem.HasChild("timetolive"))
  160. GetFloatMinMax(rootElem.GetChild("timetolive"), timeToLiveMin_, timeToLiveMax_);
  161. if (rootElem.HasChild("velocity"))
  162. GetFloatMinMax(rootElem.GetChild("velocity"), velocityMin_, velocityMax_);
  163. if (rootElem.HasChild("rotation"))
  164. GetFloatMinMax(rootElem.GetChild("rotation"), rotationMin_, rotationMax_);
  165. if (rootElem.HasChild("rotationspeed"))
  166. GetFloatMinMax(rootElem.GetChild("rotationspeed"), rotationSpeedMin_, rotationSpeedMax_);
  167. if (rootElem.HasChild("sizedelta"))
  168. {
  169. XMLElement deltaElem = rootElem.GetChild("sizedelta");
  170. if (deltaElem.HasAttribute("add"))
  171. sizeAdd_ = deltaElem.GetFloat("add");
  172. if (deltaElem.HasAttribute("mul"))
  173. sizeMul_ = deltaElem.GetFloat("mul");
  174. }
  175. if (rootElem.HasChild("color"))
  176. SetColor(rootElem.GetChild("color").GetColor("value"));
  177. if (rootElem.HasChild("colorfade"))
  178. {
  179. Vector<ColorFrame> fades;
  180. XMLElement colorFadeElem = rootElem.GetChild("colorfade");
  181. while (colorFadeElem)
  182. {
  183. fades.Push(ColorFrame(colorFadeElem.GetColor("color"), colorFadeElem.GetFloat("time")));
  184. colorFadeElem = colorFadeElem.GetNext("colorfade");
  185. }
  186. SetColors(fades);
  187. }
  188. if (rootElem.HasChild("texanim"))
  189. {
  190. Vector<TextureFrame> animations;
  191. XMLElement animElem = rootElem.GetChild("texanim");
  192. while (animElem)
  193. {
  194. TextureFrame animation;
  195. animation.uv_ = animElem.GetRect("uv");
  196. animation.time_ = animElem.GetFloat("time");
  197. animations.Push(animation);
  198. animElem = animElem.GetNext("texanim");
  199. }
  200. textureFrames_ = animations;
  201. }
  202. return true;
  203. }
  204. bool ParticleEffect::Save(Serializer& dest) const
  205. {
  206. XMLFile file(context_);
  207. XMLElement rootElem = file.CreateRoot("particleeffect");
  208. XMLElement childElem = rootElem.CreateChild("material");
  209. childElem.SetAttribute("name", GetResourceName(material_));
  210. childElem = rootElem.CreateChild("numparticles");
  211. childElem.SetInt("value", numParticles_);
  212. childElem = rootElem.CreateChild("updateinvisible");
  213. childElem.SetBool("enable", updateInvisible_);
  214. childElem = rootElem.CreateChild("relative");
  215. childElem.SetBool("enable", relative_);
  216. childElem = rootElem.CreateChild("scaled");
  217. childElem.SetBool("enable", scaled_);
  218. childElem = rootElem.CreateChild("sorted");
  219. childElem.SetBool("enable", sorted_);
  220. childElem = rootElem.CreateChild("animlodbias");
  221. childElem.SetFloat("value", animationLodBias_);
  222. childElem = rootElem.CreateChild("emittertype");
  223. childElem.SetAttribute("value", emitterTypeNames[emitterType_]);
  224. childElem = rootElem.CreateChild("emittersize");
  225. childElem.SetVector3("value", emitterSize_);
  226. childElem = rootElem.CreateChild("direction");
  227. childElem.SetVector3("min", directionMin_);
  228. childElem.SetVector3("max", directionMax_);
  229. childElem = rootElem.CreateChild("constantforce");
  230. childElem.SetVector3("value", constantForce_);
  231. childElem = rootElem.CreateChild("dampingforce");
  232. childElem.SetFloat("value", dampingForce_);
  233. childElem = rootElem.CreateChild("activetime");
  234. childElem.SetFloat("value", activeTime_);
  235. childElem = rootElem.CreateChild("inactivetime");
  236. childElem.SetFloat("value", inactiveTime_);
  237. childElem = rootElem.CreateChild("emissionrate");
  238. childElem.SetFloat("min", emissionRateMin_);
  239. childElem.SetFloat("max", emissionRateMax_);
  240. childElem = rootElem.CreateChild("particlesize");
  241. childElem.SetVector2("min", sizeMin_);
  242. childElem.SetVector2("max", sizeMax_);
  243. childElem = rootElem.CreateChild("timetolive");
  244. childElem.SetFloat("min", timeToLiveMin_);
  245. childElem.SetFloat("max", timeToLiveMax_);
  246. childElem = rootElem.CreateChild("velocity");
  247. childElem.SetFloat("min", velocityMin_);
  248. childElem.SetFloat("max", velocityMax_);
  249. childElem = rootElem.CreateChild("rotation");
  250. childElem.SetFloat("min", rotationMin_);
  251. childElem.SetFloat("max", rotationMax_);
  252. childElem = rootElem.CreateChild("rotationspeed");
  253. childElem.SetFloat("min", rotationSpeedMin_);
  254. childElem.SetFloat("max", rotationSpeedMax_);
  255. childElem = rootElem.CreateChild("sizedelta");
  256. childElem.SetFloat("add", sizeAdd_);
  257. childElem.SetFloat("mul", sizeMul_);
  258. if (colorFrames_.Size() == 1)
  259. {
  260. childElem = rootElem.CreateChild("color");
  261. childElem.SetColor("value", colorFrames_[0].color_);
  262. }
  263. if (colorFrames_.Size() > 1)
  264. {
  265. for (unsigned i = 0; i < colorFrames_.Size(); ++i)
  266. {
  267. childElem = rootElem.CreateChild("colorfade");
  268. childElem.SetColor("color", colorFrames_[i].color_);
  269. childElem.SetFloat("time", colorFrames_[i].time_);
  270. }
  271. }
  272. for (unsigned i = 0; i < textureFrames_.Size(); ++i)
  273. {
  274. childElem = rootElem.CreateChild("texanim");
  275. childElem.SetRect("uv", textureFrames_[i].uv_);
  276. childElem.SetFloat("time", textureFrames_[i].time_);
  277. }
  278. return file.Save(dest);
  279. }
  280. void ParticleEffect::SetMaterial(Material* material)
  281. {
  282. material_ = material;
  283. }
  284. void ParticleEffect::SetNumParticles(unsigned num)
  285. {
  286. numParticles_ = Max(0, num);
  287. }
  288. void ParticleEffect::SetUpdateInvisible(bool enable)
  289. {
  290. updateInvisible_ = enable;
  291. }
  292. void ParticleEffect::SetRelative(bool enable)
  293. {
  294. relative_ = enable;
  295. }
  296. void ParticleEffect::SetScaled(bool enable)
  297. {
  298. scaled_ = enable;
  299. }
  300. void ParticleEffect::SetSorted(bool enable)
  301. {
  302. sorted_ = enable;
  303. }
  304. void ParticleEffect::SetAnimationLodBias(float lodBias)
  305. {
  306. animationLodBias_ = lodBias;
  307. }
  308. void ParticleEffect::SetEmitterType(EmitterType type)
  309. {
  310. emitterType_ = type;
  311. }
  312. void ParticleEffect::SetEmitterSize(const Vector3& size)
  313. {
  314. emitterSize_ = size;
  315. }
  316. void ParticleEffect::SetMinDirection(const Vector3& direction)
  317. {
  318. directionMin_ = direction;
  319. }
  320. void ParticleEffect::SetMaxDirection(const Vector3& direction)
  321. {
  322. directionMax_ = direction;
  323. }
  324. void ParticleEffect::SetConstantForce(const Vector3& force)
  325. {
  326. constantForce_ = force;
  327. }
  328. void ParticleEffect::SetDampingForce(float force)
  329. {
  330. dampingForce_ = force;
  331. }
  332. void ParticleEffect::SetActiveTime(float time)
  333. {
  334. activeTime_ = time;
  335. }
  336. void ParticleEffect::SetInactiveTime(float time)
  337. {
  338. inactiveTime_ = time;
  339. }
  340. void ParticleEffect::SetMinEmissionRate(float rate)
  341. {
  342. emissionRateMin_ = Max(rate, MIN_EMISSION_RATE);
  343. }
  344. void ParticleEffect::SetMaxEmissionRate(float rate)
  345. {
  346. emissionRateMax_ = Max(rate, MIN_EMISSION_RATE);
  347. }
  348. void ParticleEffect::SetMinParticleSize(const Vector2& size)
  349. {
  350. sizeMin_ = size;
  351. }
  352. void ParticleEffect::SetMaxParticleSize(const Vector2& size)
  353. {
  354. sizeMax_ = size;
  355. }
  356. void ParticleEffect::SetMinTimeToLive(float time)
  357. {
  358. timeToLiveMin_ = Max(time, 0.0f);
  359. }
  360. void ParticleEffect::SetMaxTimeToLive(float time)
  361. {
  362. timeToLiveMax_ = Max(time, 0.0f);
  363. }
  364. void ParticleEffect::SetMinVelocity(float velocity)
  365. {
  366. velocityMin_ = velocity;
  367. }
  368. void ParticleEffect::SetMaxVelocity(float velocity)
  369. {
  370. velocityMax_ = velocity;
  371. }
  372. void ParticleEffect::SetMinRotation(float rotation)
  373. {
  374. rotationMin_ = rotation;
  375. }
  376. void ParticleEffect::SetMaxRotation(float rotation)
  377. {
  378. rotationMax_ = rotation;
  379. }
  380. void ParticleEffect::SetMinRotationSpeed(float speed)
  381. {
  382. rotationSpeedMin_ = speed;
  383. }
  384. void ParticleEffect::SetMaxRotationSpeed(float speed)
  385. {
  386. rotationSpeedMax_ = speed;
  387. }
  388. void ParticleEffect::SetSizeAdd(float sizeAdd)
  389. {
  390. sizeAdd_ = sizeAdd;
  391. }
  392. void ParticleEffect::SetSizeMul(float sizeMul)
  393. {
  394. sizeMul_ = sizeMul;
  395. }
  396. void ParticleEffect::SetColor(const Color& color)
  397. {
  398. colorFrames_.Clear();
  399. colorFrames_.Push(ColorFrame(color));
  400. }
  401. void ParticleEffect::SetColors(const Vector<ColorFrame>& colors)
  402. {
  403. if (!colors.Size())
  404. return;
  405. colorFrames_ = colors;
  406. }
  407. void ParticleEffect::SetNumColors(unsigned num)
  408. {
  409. colorFrames_.Resize(num);
  410. }
  411. void ParticleEffect::SetTextureFrames(const Vector<TextureFrame>& animation)
  412. {
  413. textureFrames_ = animation;
  414. }
  415. void ParticleEffect::SetNumTextureFrames(unsigned num)
  416. {
  417. textureFrames_.Resize(num);
  418. }
  419. Vector3 ParticleEffect::GetRandomDirection() const
  420. {
  421. return Vector3(
  422. Lerp(directionMin_.x_, directionMax_.x_, Random(1.0f)),
  423. Lerp(directionMin_.y_, directionMax_.y_, Random(1.0f)),
  424. Lerp(directionMin_.z_, directionMax_.z_, Random(1.0f))
  425. );
  426. }
  427. Vector2 ParticleEffect::GetRandomSize() const
  428. {
  429. return sizeMin_.Lerp(sizeMax_, Random(1.0f));
  430. }
  431. float ParticleEffect::GetRandomVelocity() const
  432. {
  433. return Lerp(velocityMin_, velocityMax_, Random(1.0f));
  434. }
  435. float ParticleEffect::GetRandomTimeToLive() const
  436. {
  437. return Lerp(timeToLiveMin_, timeToLiveMax_, Random(1.0f));
  438. }
  439. float ParticleEffect::GetRandomRotationSpeed() const
  440. {
  441. return Lerp(rotationSpeedMin_, rotationSpeedMax_, Random(1.0f));
  442. }
  443. float ParticleEffect::GetRandomRotation() const
  444. {
  445. return Lerp(rotationMin_, rotationMax_, Random(1.0f));
  446. }
  447. void ParticleEffect::GetFloatMinMax(const XMLElement& element, float& minValue, float& maxValue)
  448. {
  449. if (element.IsNull())
  450. return;
  451. if (element.HasAttribute("value"))
  452. minValue = maxValue = element.GetFloat("value");
  453. if (element.HasAttribute("min") && element.HasAttribute("max"))
  454. {
  455. minValue = element.GetFloat("min");
  456. maxValue = element.GetFloat("max");
  457. }
  458. }
  459. void ParticleEffect::GetVector2MinMax(const XMLElement& element, Vector2& minValue, Vector2& maxValue)
  460. {
  461. if (element.IsNull())
  462. return;
  463. if (element.HasAttribute("value"))
  464. minValue = maxValue = element.GetVector2("value");
  465. if (element.HasAttribute("min") && element.HasAttribute("max"))
  466. {
  467. minValue = element.GetVector2("min");
  468. maxValue = element.GetVector2("max");
  469. }
  470. }
  471. void ParticleEffect::GetVector3MinMax(const XMLElement& element, Vector3& minValue, Vector3& maxValue)
  472. {
  473. if (element.IsNull())
  474. return;
  475. if (element.HasAttribute("value"))
  476. minValue = maxValue = element.GetVector3("value");
  477. if (element.HasAttribute("min") && element.HasAttribute("max"))
  478. {
  479. minValue = element.GetVector3("min");
  480. maxValue = element.GetVector3("max");
  481. }
  482. }
  483. }