ParticleEffect.cpp 17 KB

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