ParticleEffect.cpp 27 KB

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