ParticleEffect.cpp 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962
  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 "../Core/Context.h"
  24. #include "../IO/Log.h"
  25. #include "../Graphics/Material.h"
  26. #include "../Atomic3D/ParticleEffect.h"
  27. #include "../Resource/ResourceCache.h"
  28. #include "../Resource/XMLFile.h"
  29. #include "../DebugNew.h"
  30. namespace Atomic
  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_(DEFAULT_NUM_PARTICLES),
  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::BeginLoad(Deserializer& source)
  85. {
  86. loadMaterialName_.Clear();
  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. // Reset to defaults first so that missing parameters in case of a live reload behave as expected
  100. material_.Reset();
  101. numParticles_ = DEFAULT_NUM_PARTICLES;
  102. updateInvisible_ = false;
  103. relative_ = true;
  104. scaled_ = true;
  105. sorted_ = false;
  106. animationLodBias_ = 0.0f;
  107. emitterType_ = EMITTER_SPHERE;
  108. emitterSize_ = Vector3::ZERO;
  109. directionMin_ = DEFAULT_DIRECTION_MIN;
  110. directionMax_ = DEFAULT_DIRECTION_MAX;
  111. constantForce_ = Vector3::ZERO;
  112. dampingForce_ = 0.0f;
  113. activeTime_ = 0.0f;
  114. inactiveTime_ = 0.0;
  115. emissionRateMin_ = DEFAULT_EMISSION_RATE;
  116. emissionRateMax_ = DEFAULT_EMISSION_RATE;
  117. sizeMin_ = DEFAULT_PARTICLE_SIZE;
  118. sizeMax_ = DEFAULT_PARTICLE_SIZE;
  119. timeToLiveMin_ = DEFAULT_TIME_TO_LIVE;
  120. timeToLiveMax_ = DEFAULT_TIME_TO_LIVE;
  121. velocityMin_ = DEFAULT_VELOCITY;
  122. velocityMax_ = DEFAULT_VELOCITY;
  123. rotationMin_ = 0.0f;
  124. rotationMax_ = 0.0f;
  125. rotationSpeedMin_ = 0.0f;
  126. rotationSpeedMax_ = 0.0f;
  127. sizeAdd_ = 0.0f;
  128. sizeMul_ = 1.0f;
  129. colorFrames_.Clear();
  130. textureFrames_.Clear();
  131. if (rootElem.HasChild("material"))
  132. {
  133. loadMaterialName_ = rootElem.GetChild("material").GetAttribute("name");
  134. // If async loading, can not GetResource() the material. But can do a background request for it
  135. if (GetAsyncLoadState() == ASYNC_LOADING)
  136. GetSubsystem<ResourceCache>()->BackgroundLoadResource<Material>(loadMaterialName_, true, this);
  137. }
  138. if (rootElem.HasChild("numparticles"))
  139. SetNumParticles(rootElem.GetChild("numparticles").GetInt("value"));
  140. if (rootElem.HasChild("updateinvisible"))
  141. updateInvisible_ = rootElem.GetChild("updateinvisible").GetBool("enable");
  142. if (rootElem.HasChild("relative"))
  143. relative_ = rootElem.GetChild("relative").GetBool("enable");
  144. if (rootElem.HasChild("scaled"))
  145. scaled_ = rootElem.GetChild("scaled").GetBool("enable");
  146. if (rootElem.HasChild("sorted"))
  147. sorted_ = rootElem.GetChild("sorted").GetBool("enable");
  148. if (rootElem.HasChild("animlodbias"))
  149. SetAnimationLodBias(rootElem.GetChild("animlodbias").GetFloat("value"));
  150. if (rootElem.HasChild("emittertype"))
  151. {
  152. String type = rootElem.GetChild("emittertype").GetAttributeLower("value");
  153. if (type == "point")
  154. {
  155. // Point emitter type is deprecated, handled as zero sized sphere
  156. emitterType_ = EMITTER_SPHERE;
  157. emitterSize_ = Vector3::ZERO;
  158. }
  159. else if (type == "box")
  160. emitterType_ = EMITTER_BOX;
  161. else if (type == "sphere")
  162. emitterType_ = EMITTER_SPHERE;
  163. else
  164. LOGERROR("Unknown particle emitter type " + type);
  165. }
  166. if (rootElem.HasChild("emittersize"))
  167. emitterSize_ = rootElem.GetChild("emittersize").GetVector3("value");
  168. if (rootElem.HasChild("emitterradius"))
  169. emitterSize_.x_ = emitterSize_.y_ = emitterSize_.z_ = rootElem.GetChild("emitterradius").GetFloat("value");
  170. if (rootElem.HasChild("direction"))
  171. GetVector3MinMax(rootElem.GetChild("direction"), directionMin_, directionMax_);
  172. if (rootElem.HasChild("constantforce"))
  173. constantForce_ = rootElem.GetChild("constantforce").GetVector3("value");
  174. if (rootElem.HasChild("dampingforce"))
  175. dampingForce_ = rootElem.GetChild("dampingforce").GetFloat("value");
  176. if (rootElem.HasChild("activetime"))
  177. activeTime_ = rootElem.GetChild("activetime").GetFloat("value");
  178. if (activeTime_ < 0.0f)
  179. activeTime_ = M_INFINITY;
  180. if (rootElem.HasChild("inactivetime"))
  181. inactiveTime_ = rootElem.GetChild("inactivetime").GetFloat("value");
  182. if (inactiveTime_ < 0.0f)
  183. inactiveTime_ = M_INFINITY;
  184. if (rootElem.HasChild("emissionrate"))
  185. GetFloatMinMax(rootElem.GetChild("emissionrate"), emissionRateMin_, emissionRateMax_);
  186. if (rootElem.HasChild("interval"))
  187. {
  188. float intervalMin = 0.0f;
  189. float intervalMax = 0.0f;
  190. GetFloatMinMax(rootElem.GetChild("interval"), intervalMin, intervalMax);
  191. emissionRateMax_ = 1.0f / intervalMin;
  192. emissionRateMin_ = 1.0f / intervalMax;
  193. }
  194. if (rootElem.HasChild("particlesize"))
  195. GetVector2MinMax(rootElem.GetChild("particlesize"), sizeMin_, sizeMax_);
  196. if (rootElem.HasChild("timetolive"))
  197. GetFloatMinMax(rootElem.GetChild("timetolive"), timeToLiveMin_, timeToLiveMax_);
  198. if (rootElem.HasChild("velocity"))
  199. GetFloatMinMax(rootElem.GetChild("velocity"), velocityMin_, velocityMax_);
  200. if (rootElem.HasChild("rotation"))
  201. GetFloatMinMax(rootElem.GetChild("rotation"), rotationMin_, rotationMax_);
  202. if (rootElem.HasChild("rotationspeed"))
  203. GetFloatMinMax(rootElem.GetChild("rotationspeed"), rotationSpeedMin_, rotationSpeedMax_);
  204. if (rootElem.HasChild("sizedelta"))
  205. {
  206. XMLElement deltaElem = rootElem.GetChild("sizedelta");
  207. if (deltaElem.HasAttribute("add"))
  208. sizeAdd_ = deltaElem.GetFloat("add");
  209. if (deltaElem.HasAttribute("mul"))
  210. sizeMul_ = deltaElem.GetFloat("mul");
  211. }
  212. if (rootElem.HasChild("color"))
  213. {
  214. ColorFrame colorFrame(rootElem.GetChild("color").GetColor("value"));
  215. SetColorFrame(0, colorFrame);
  216. }
  217. if (rootElem.HasChild("colorfade"))
  218. {
  219. Vector<ColorFrame> fades;
  220. for (XMLElement colorFadeElem = rootElem.GetChild("colorfade"); colorFadeElem; colorFadeElem = colorFadeElem.GetNext("colorfade"))
  221. fades.Push(ColorFrame(colorFadeElem.GetColor("color"), colorFadeElem.GetFloat("time")));
  222. SetColorFrames(fades);
  223. }
  224. if (colorFrames_.Empty())
  225. colorFrames_.Push(ColorFrame(Color::WHITE));
  226. if (rootElem.HasChild("texanim"))
  227. {
  228. Vector<TextureFrame> animations;
  229. for (XMLElement animElem = rootElem.GetChild("texanim"); animElem; animElem = animElem.GetNext("texanim"))
  230. {
  231. TextureFrame animation;
  232. animation.uv_ = animElem.GetRect("uv");
  233. animation.time_ = animElem.GetFloat("time");
  234. animations.Push(animation);
  235. }
  236. SetTextureFrames(animations);
  237. }
  238. return true;
  239. }
  240. bool ParticleEffect::EndLoad()
  241. {
  242. // Apply the material now
  243. if (!loadMaterialName_.Empty())
  244. {
  245. SetMaterial(GetSubsystem<ResourceCache>()->GetResource<Material>(loadMaterialName_));
  246. loadMaterialName_.Clear();
  247. }
  248. return true;
  249. }
  250. bool ParticleEffect::Load(const XMLElement& source)
  251. {
  252. // Reset to defaults first so that missing parameters in case of a live reload behave as expected
  253. material_.Reset();
  254. numParticles_ = DEFAULT_NUM_PARTICLES;
  255. updateInvisible_ = false;
  256. relative_ = true;
  257. scaled_ = true;
  258. sorted_ = false;
  259. animationLodBias_ = 0.0f;
  260. emitterType_ = EMITTER_SPHERE;
  261. emitterSize_ = Vector3::ZERO;
  262. directionMin_ = DEFAULT_DIRECTION_MIN;
  263. directionMax_ = DEFAULT_DIRECTION_MAX;
  264. constantForce_ = Vector3::ZERO;
  265. dampingForce_ = 0.0f;
  266. activeTime_ = 0.0f;
  267. inactiveTime_ = 0.0;
  268. emissionRateMin_ = DEFAULT_EMISSION_RATE;
  269. emissionRateMax_ = DEFAULT_EMISSION_RATE;
  270. sizeMin_ = DEFAULT_PARTICLE_SIZE;
  271. sizeMax_ = DEFAULT_PARTICLE_SIZE;
  272. timeToLiveMin_ = DEFAULT_TIME_TO_LIVE;
  273. timeToLiveMax_ = DEFAULT_TIME_TO_LIVE;
  274. velocityMin_ = DEFAULT_VELOCITY;
  275. velocityMax_ = DEFAULT_VELOCITY;
  276. rotationMin_ = 0.0f;
  277. rotationMax_ = 0.0f;
  278. rotationSpeedMin_ = 0.0f;
  279. rotationSpeedMax_ = 0.0f;
  280. sizeAdd_ = 0.0f;
  281. sizeMul_ = 1.0f;
  282. colorFrames_.Clear();
  283. textureFrames_.Clear();
  284. if (source.IsNull())
  285. {
  286. LOGERROR("Can not load particle effect from null XML element");
  287. return false;
  288. }
  289. if (source.HasChild("material"))
  290. {
  291. material_ = GetSubsystem<ResourceCache>()->GetResource<Material>(source.GetChild("material").GetAttribute("name"));
  292. }
  293. if (source.HasChild("numparticles"))
  294. SetNumParticles(source.GetChild("numparticles").GetInt("value"));
  295. if (source.HasChild("updateinvisible"))
  296. updateInvisible_ = source.GetChild("updateinvisible").GetBool("enable");
  297. if (source.HasChild("relative"))
  298. relative_ = source.GetChild("relative").GetBool("enable");
  299. if (source.HasChild("scaled"))
  300. scaled_ = source.GetChild("scaled").GetBool("enable");
  301. if (source.HasChild("sorted"))
  302. sorted_ = source.GetChild("sorted").GetBool("enable");
  303. if (source.HasChild("animlodbias"))
  304. SetAnimationLodBias(source.GetChild("animlodbias").GetFloat("value"));
  305. if (source.HasChild("emittertype"))
  306. {
  307. String type = source.GetChild("emittertype").GetAttributeLower("value");
  308. if (type == "point")
  309. {
  310. // Point emitter type is deprecated, handled as zero sized sphere
  311. emitterType_ = EMITTER_SPHERE;
  312. emitterSize_ = Vector3::ZERO;
  313. }
  314. else if (type == "box")
  315. emitterType_ = EMITTER_BOX;
  316. else if (type == "sphere")
  317. emitterType_ = EMITTER_SPHERE;
  318. else
  319. LOGERROR("Unknown particle emitter type " + type);
  320. }
  321. if (source.HasChild("emittersize"))
  322. emitterSize_ = source.GetChild("emittersize").GetVector3("value");
  323. if (source.HasChild("emitterradius"))
  324. emitterSize_.x_ = emitterSize_.y_ = emitterSize_.z_ = source.GetChild("emitterradius").GetFloat("value");
  325. if (source.HasChild("direction"))
  326. GetVector3MinMax(source.GetChild("direction"), directionMin_, directionMax_);
  327. if (source.HasChild("constantforce"))
  328. constantForce_ = source.GetChild("constantforce").GetVector3("value");
  329. if (source.HasChild("dampingforce"))
  330. dampingForce_ = source.GetChild("dampingforce").GetFloat("value");
  331. if (source.HasChild("activetime"))
  332. activeTime_ = source.GetChild("activetime").GetFloat("value");
  333. if (activeTime_ < 0.0f)
  334. activeTime_ = M_INFINITY;
  335. if (source.HasChild("inactivetime"))
  336. inactiveTime_ = source.GetChild("inactivetime").GetFloat("value");
  337. if (inactiveTime_ < 0.0f)
  338. inactiveTime_ = M_INFINITY;
  339. if (source.HasChild("emissionrate"))
  340. GetFloatMinMax(source.GetChild("emissionrate"), emissionRateMin_, emissionRateMax_);
  341. if (source.HasChild("interval"))
  342. {
  343. float intervalMin = 0.0f;
  344. float intervalMax = 0.0f;
  345. GetFloatMinMax(source.GetChild("interval"), intervalMin, intervalMax);
  346. emissionRateMax_ = 1.0f / intervalMin;
  347. emissionRateMin_ = 1.0f / intervalMax;
  348. }
  349. if (source.HasChild("particlesize"))
  350. GetVector2MinMax(source.GetChild("particlesize"), sizeMin_, sizeMax_);
  351. if (source.HasChild("timetolive"))
  352. GetFloatMinMax(source.GetChild("timetolive"), timeToLiveMin_, timeToLiveMax_);
  353. if (source.HasChild("velocity"))
  354. GetFloatMinMax(source.GetChild("velocity"), velocityMin_, velocityMax_);
  355. if (source.HasChild("rotation"))
  356. GetFloatMinMax(source.GetChild("rotation"), rotationMin_, rotationMax_);
  357. if (source.HasChild("rotationspeed"))
  358. GetFloatMinMax(source.GetChild("rotationspeed"), rotationSpeedMin_, rotationSpeedMax_);
  359. if (source.HasChild("sizedelta"))
  360. {
  361. XMLElement deltaElem = source.GetChild("sizedelta");
  362. if (deltaElem.HasAttribute("add"))
  363. sizeAdd_ = deltaElem.GetFloat("add");
  364. if (deltaElem.HasAttribute("mul"))
  365. sizeMul_ = deltaElem.GetFloat("mul");
  366. }
  367. if (source.HasChild("color"))
  368. {
  369. ColorFrame colorFrame(source.GetChild("color").GetColor("value"));
  370. SetColorFrame(0, colorFrame);
  371. }
  372. if (source.HasChild("colorfade"))
  373. {
  374. Vector<ColorFrame> fades;
  375. for (XMLElement colorFadeElem = source.GetChild("colorfade"); colorFadeElem; colorFadeElem = colorFadeElem.GetNext("colorfade"))
  376. fades.Push(ColorFrame(colorFadeElem.GetColor("color"), colorFadeElem.GetFloat("time")));
  377. SetColorFrames(fades);
  378. }
  379. if (colorFrames_.Empty())
  380. colorFrames_.Push(ColorFrame(Color::WHITE));
  381. if (source.HasChild("texanim"))
  382. {
  383. Vector<TextureFrame> animations;
  384. for (XMLElement animElem = source.GetChild("texanim"); animElem; animElem = animElem.GetNext("texanim"))
  385. {
  386. TextureFrame animation;
  387. animation.uv_ = animElem.GetRect("uv");
  388. animation.time_ = animElem.GetFloat("time");
  389. animations.Push(animation);
  390. }
  391. SetTextureFrames(animations);
  392. }
  393. return true;
  394. }
  395. bool ParticleEffect::Save(Serializer& dest) const
  396. {
  397. SharedPtr<XMLFile> xml(new XMLFile(context_));
  398. XMLElement materialElem = xml->CreateRoot("particleeffect");
  399. Save(materialElem);
  400. return xml->Save(dest);
  401. }
  402. bool ParticleEffect::Save(XMLElement& dest) const
  403. {
  404. if (dest.IsNull())
  405. {
  406. LOGERROR("Can not save particle effect to null XML element");
  407. return false;
  408. }
  409. XMLElement childElem = dest.CreateChild("material");
  410. childElem.SetAttribute("name", GetResourceName(material_));
  411. childElem = dest.CreateChild("numparticles");
  412. childElem.SetInt("value", numParticles_);
  413. childElem = dest.CreateChild("updateinvisible");
  414. childElem.SetBool("enable", updateInvisible_);
  415. childElem = dest.CreateChild("relative");
  416. childElem.SetBool("enable", relative_);
  417. childElem = dest.CreateChild("scaled");
  418. childElem.SetBool("enable", scaled_);
  419. childElem = dest.CreateChild("sorted");
  420. childElem.SetBool("enable", sorted_);
  421. childElem = dest.CreateChild("animlodbias");
  422. childElem.SetFloat("value", animationLodBias_);
  423. childElem = dest.CreateChild("emittertype");
  424. childElem.SetAttribute("value", emitterTypeNames[emitterType_]);
  425. childElem = dest.CreateChild("emittersize");
  426. childElem.SetVector3("value", emitterSize_);
  427. childElem = dest.CreateChild("direction");
  428. childElem.SetVector3("min", directionMin_);
  429. childElem.SetVector3("max", directionMax_);
  430. childElem = dest.CreateChild("constantforce");
  431. childElem.SetVector3("value", constantForce_);
  432. childElem = dest.CreateChild("dampingforce");
  433. childElem.SetFloat("value", dampingForce_);
  434. childElem = dest.CreateChild("activetime");
  435. childElem.SetFloat("value", activeTime_);
  436. childElem = dest.CreateChild("inactivetime");
  437. childElem.SetFloat("value", inactiveTime_);
  438. childElem = dest.CreateChild("emissionrate");
  439. childElem.SetFloat("min", emissionRateMin_);
  440. childElem.SetFloat("max", emissionRateMax_);
  441. childElem = dest.CreateChild("particlesize");
  442. childElem.SetVector2("min", sizeMin_);
  443. childElem.SetVector2("max", sizeMax_);
  444. childElem = dest.CreateChild("timetolive");
  445. childElem.SetFloat("min", timeToLiveMin_);
  446. childElem.SetFloat("max", timeToLiveMax_);
  447. childElem = dest.CreateChild("velocity");
  448. childElem.SetFloat("min", velocityMin_);
  449. childElem.SetFloat("max", velocityMax_);
  450. childElem = dest.CreateChild("rotation");
  451. childElem.SetFloat("min", rotationMin_);
  452. childElem.SetFloat("max", rotationMax_);
  453. childElem = dest.CreateChild("rotationspeed");
  454. childElem.SetFloat("min", rotationSpeedMin_);
  455. childElem.SetFloat("max", rotationSpeedMax_);
  456. childElem = dest.CreateChild("sizedelta");
  457. childElem.SetFloat("add", sizeAdd_);
  458. childElem.SetFloat("mul", sizeMul_);
  459. if (colorFrames_.Size() == 1)
  460. {
  461. childElem = dest.CreateChild("color");
  462. childElem.SetColor("value", colorFrames_[0].color_);
  463. }
  464. if (colorFrames_.Size() > 1)
  465. {
  466. for (unsigned i = 0; i < colorFrames_.Size(); ++i)
  467. {
  468. childElem = dest.CreateChild("colorfade");
  469. childElem.SetColor("color", colorFrames_[i].color_);
  470. childElem.SetFloat("time", colorFrames_[i].time_);
  471. }
  472. }
  473. for (unsigned i = 0; i < textureFrames_.Size(); ++i)
  474. {
  475. childElem = dest.CreateChild("texanim");
  476. childElem.SetRect("uv", textureFrames_[i].uv_);
  477. childElem.SetFloat("time", textureFrames_[i].time_);
  478. }
  479. return true;
  480. }
  481. void ParticleEffect::SetMaterial(Material* material)
  482. {
  483. material_ = material;
  484. }
  485. void ParticleEffect::SetNumParticles(unsigned num)
  486. {
  487. numParticles_ = Max(0, num);
  488. }
  489. void ParticleEffect::SetUpdateInvisible(bool enable)
  490. {
  491. updateInvisible_ = enable;
  492. }
  493. void ParticleEffect::SetRelative(bool enable)
  494. {
  495. relative_ = enable;
  496. }
  497. void ParticleEffect::SetScaled(bool enable)
  498. {
  499. scaled_ = enable;
  500. }
  501. void ParticleEffect::SetSorted(bool enable)
  502. {
  503. sorted_ = enable;
  504. }
  505. void ParticleEffect::SetAnimationLodBias(float lodBias)
  506. {
  507. animationLodBias_ = lodBias;
  508. }
  509. void ParticleEffect::SetEmitterType(EmitterType type)
  510. {
  511. emitterType_ = type;
  512. }
  513. void ParticleEffect::SetEmitterSize(const Vector3& size)
  514. {
  515. emitterSize_ = size;
  516. }
  517. void ParticleEffect::SetMinDirection(const Vector3& direction)
  518. {
  519. directionMin_ = direction;
  520. }
  521. void ParticleEffect::SetMaxDirection(const Vector3& direction)
  522. {
  523. directionMax_ = direction;
  524. }
  525. void ParticleEffect::SetConstantForce(const Vector3& force)
  526. {
  527. constantForce_ = force;
  528. }
  529. void ParticleEffect::SetDampingForce(float force)
  530. {
  531. dampingForce_ = force;
  532. }
  533. void ParticleEffect::SetActiveTime(float time)
  534. {
  535. activeTime_ = time;
  536. }
  537. void ParticleEffect::SetInactiveTime(float time)
  538. {
  539. inactiveTime_ = time;
  540. }
  541. void ParticleEffect::SetMinEmissionRate(float rate)
  542. {
  543. emissionRateMin_ = Max(rate, MIN_EMISSION_RATE);
  544. }
  545. void ParticleEffect::SetMaxEmissionRate(float rate)
  546. {
  547. emissionRateMax_ = Max(rate, MIN_EMISSION_RATE);
  548. }
  549. void ParticleEffect::SetMinParticleSize(const Vector2& size)
  550. {
  551. sizeMin_ = size;
  552. }
  553. void ParticleEffect::SetMaxParticleSize(const Vector2& size)
  554. {
  555. sizeMax_ = size;
  556. }
  557. void ParticleEffect::SetMinTimeToLive(float time)
  558. {
  559. timeToLiveMin_ = Max(time, 0.0f);
  560. }
  561. void ParticleEffect::SetMaxTimeToLive(float time)
  562. {
  563. timeToLiveMax_ = Max(time, 0.0f);
  564. }
  565. void ParticleEffect::SetMinVelocity(float velocity)
  566. {
  567. velocityMin_ = velocity;
  568. }
  569. void ParticleEffect::SetMaxVelocity(float velocity)
  570. {
  571. velocityMax_ = velocity;
  572. }
  573. void ParticleEffect::SetMinRotation(float rotation)
  574. {
  575. rotationMin_ = rotation;
  576. }
  577. void ParticleEffect::SetMaxRotation(float rotation)
  578. {
  579. rotationMax_ = rotation;
  580. }
  581. void ParticleEffect::SetMinRotationSpeed(float speed)
  582. {
  583. rotationSpeedMin_ = speed;
  584. }
  585. void ParticleEffect::SetMaxRotationSpeed(float speed)
  586. {
  587. rotationSpeedMax_ = speed;
  588. }
  589. void ParticleEffect::SetSizeAdd(float sizeAdd)
  590. {
  591. sizeAdd_ = sizeAdd;
  592. }
  593. void ParticleEffect::SetSizeMul(float sizeMul)
  594. {
  595. sizeMul_ = sizeMul;
  596. }
  597. void ParticleEffect::AddColorTime(const Color& color, const float time)
  598. {
  599. unsigned s = colorFrames_.Size();
  600. colorFrames_.Resize(s + 1);
  601. for (unsigned i = 0; i < s; i++)
  602. {
  603. if (colorFrames_[i].time_ > time)
  604. {
  605. for (unsigned j = s; j > i; j--)
  606. {
  607. colorFrames_[j].color_ = colorFrames_[j - 1].color_;
  608. colorFrames_[j].time_ = colorFrames_[j - 1].time_;
  609. }
  610. colorFrames_[i].color_ = color;
  611. colorFrames_[i].time_ = time;
  612. return;
  613. }
  614. }
  615. // highest time, add last:
  616. colorFrames_[s].color_ = color;
  617. colorFrames_[s].time_ = time;
  618. }
  619. void ParticleEffect::AddColorFrame(const ColorFrame& colorFrame)
  620. {
  621. AddColorTime(colorFrame.color_, colorFrame.time_);
  622. }
  623. void ParticleEffect::RemoveColorFrame(unsigned index)
  624. {
  625. unsigned s = colorFrames_.Size();
  626. for (unsigned i = index; i < s - 1 ; i++)
  627. {
  628. colorFrames_[i].color_ = colorFrames_[i + 1].color_;
  629. colorFrames_[i].time_ = colorFrames_[i + 1].time_;
  630. }
  631. colorFrames_.Resize(s - 1);
  632. }
  633. void ParticleEffect::SetColorFrames(const Vector<ColorFrame>& colorFrames)
  634. {
  635. colorFrames_ = colorFrames;
  636. }
  637. void ParticleEffect::SetColorFrame(unsigned index, const ColorFrame& colorFrame)
  638. {
  639. if (colorFrames_.Size() < index + 1)
  640. colorFrames_.Resize(index + 1);
  641. colorFrames_[index] = colorFrame;
  642. }
  643. void ParticleEffect::SetNumColorFrames(unsigned number)
  644. {
  645. unsigned s = colorFrames_.Size();
  646. if (s != number)
  647. colorFrames_.Resize(number);
  648. }
  649. void ParticleEffect::SortColorFrames()
  650. {
  651. Vector<ColorFrame> cf = colorFrames_;
  652. colorFrames_.Clear();
  653. for (unsigned i = 0; i < cf.Size(); i++)
  654. AddColorFrame(cf[i]);
  655. }
  656. void ParticleEffect::AddTextureTime(const Rect& uv, const float time)
  657. {
  658. unsigned s = textureFrames_.Size();
  659. textureFrames_.Resize(s + 1);
  660. for (unsigned i = 0; i < s; i++)
  661. {
  662. if (textureFrames_[i].time_ > time)
  663. {
  664. for (unsigned j = s; j > i; j--)
  665. {
  666. textureFrames_[j].uv_ = textureFrames_[j - 1].uv_;
  667. textureFrames_[j].time_ = textureFrames_[j - 1].time_;
  668. }
  669. textureFrames_[i].uv_ = uv;
  670. textureFrames_[i].time_ = time;
  671. return;
  672. }
  673. }
  674. // highest time, add last:
  675. textureFrames_[s].uv_ = uv;
  676. textureFrames_[s].time_ = time;
  677. }
  678. void ParticleEffect::AddTextureFrame(const TextureFrame& textureFrame)
  679. {
  680. AddTextureTime(textureFrame.uv_, textureFrame.time_);
  681. }
  682. void ParticleEffect::RemoveTextureFrame(unsigned index)
  683. {
  684. unsigned s = textureFrames_.Size();
  685. for (unsigned i = index; i < s - 1 ; i++)
  686. {
  687. textureFrames_[i].uv_ = textureFrames_[i + 1].uv_;
  688. textureFrames_[i].time_ = textureFrames_[i + 1].time_;
  689. }
  690. textureFrames_.Resize(s - 1);
  691. }
  692. void ParticleEffect::SetTextureFrames(const Vector<TextureFrame>& textureFrames)
  693. {
  694. textureFrames_ = textureFrames;
  695. }
  696. void ParticleEffect::SetTextureFrame(unsigned index, const TextureFrame& textureFrame)
  697. {
  698. if (textureFrames_.Size() < index + 1)
  699. textureFrames_.Resize(index + 1);
  700. textureFrames_[index] = textureFrame;
  701. }
  702. void ParticleEffect::SetNumTextureFrames(unsigned number)
  703. {
  704. unsigned s = textureFrames_.Size();
  705. if (s != number)
  706. textureFrames_.Resize(number);
  707. }
  708. void ParticleEffect::SortTextureFrames()
  709. {
  710. Vector<TextureFrame> tf = textureFrames_;
  711. textureFrames_.Clear();
  712. for (unsigned i = 0; i < tf.Size(); i++)
  713. AddTextureFrame(tf[i]);
  714. }
  715. const ColorFrame* ParticleEffect::GetColorFrame(unsigned index) const
  716. {
  717. return index < colorFrames_.Size() ? &colorFrames_[index] : (ColorFrame*)0;
  718. }
  719. const TextureFrame* ParticleEffect::GetTextureFrame(unsigned index) const
  720. {
  721. return index < textureFrames_.Size() ? &textureFrames_[index] : (TextureFrame*)0;
  722. }
  723. Vector3 ParticleEffect::GetRandomDirection() const
  724. {
  725. return Vector3(
  726. Lerp(directionMin_.x_, directionMax_.x_, Random(1.0f)),
  727. Lerp(directionMin_.y_, directionMax_.y_, Random(1.0f)),
  728. Lerp(directionMin_.z_, directionMax_.z_, Random(1.0f))
  729. );
  730. }
  731. Vector2 ParticleEffect::GetRandomSize() const
  732. {
  733. return sizeMin_.Lerp(sizeMax_, Random(1.0f));
  734. }
  735. float ParticleEffect::GetRandomVelocity() const
  736. {
  737. return Lerp(velocityMin_, velocityMax_, Random(1.0f));
  738. }
  739. float ParticleEffect::GetRandomTimeToLive() const
  740. {
  741. return Lerp(timeToLiveMin_, timeToLiveMax_, Random(1.0f));
  742. }
  743. float ParticleEffect::GetRandomRotationSpeed() const
  744. {
  745. return Lerp(rotationSpeedMin_, rotationSpeedMax_, Random(1.0f));
  746. }
  747. float ParticleEffect::GetRandomRotation() const
  748. {
  749. return Lerp(rotationMin_, rotationMax_, Random(1.0f));
  750. }
  751. void ParticleEffect::GetFloatMinMax(const XMLElement& element, float& minValue, float& maxValue)
  752. {
  753. if (element.IsNull())
  754. return;
  755. if (element.HasAttribute("value"))
  756. minValue = maxValue = element.GetFloat("value");
  757. if (element.HasAttribute("min") && element.HasAttribute("max"))
  758. {
  759. minValue = element.GetFloat("min");
  760. maxValue = element.GetFloat("max");
  761. }
  762. }
  763. void ParticleEffect::GetVector2MinMax(const XMLElement& element, Vector2& minValue, Vector2& maxValue)
  764. {
  765. if (element.IsNull())
  766. return;
  767. if (element.HasAttribute("value"))
  768. minValue = maxValue = element.GetVector2("value");
  769. if (element.HasAttribute("min") && element.HasAttribute("max"))
  770. {
  771. minValue = element.GetVector2("min");
  772. maxValue = element.GetVector2("max");
  773. }
  774. }
  775. void ParticleEffect::GetVector3MinMax(const XMLElement& element, Vector3& minValue, Vector3& maxValue)
  776. {
  777. if (element.IsNull())
  778. return;
  779. if (element.HasAttribute("value"))
  780. minValue = maxValue = element.GetVector3("value");
  781. if (element.HasAttribute("min") && element.HasAttribute("max"))
  782. {
  783. minValue = element.GetVector3("min");
  784. maxValue = element.GetVector3("max");
  785. }
  786. }
  787. }