ParticleEmitter.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620
  1. //
  2. // Copyright (c) 2008-2020 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 "../Core/Profiler.h"
  25. #include "../Graphics/DrawableEvents.h"
  26. #include "../Graphics/ParticleEffect.h"
  27. #include "../Graphics/ParticleEmitter.h"
  28. #include "../Resource/ResourceCache.h"
  29. #include "../Resource/ResourceEvents.h"
  30. #include "../Scene/Scene.h"
  31. #include "../Scene/SceneEvents.h"
  32. #include "../DebugNew.h"
  33. namespace Urho3D
  34. {
  35. extern const char* GEOMETRY_CATEGORY;
  36. extern const char* faceCameraModeNames[];
  37. static const unsigned MAX_PARTICLES_IN_FRAME = 100;
  38. extern const char* autoRemoveModeNames[];
  39. ParticleEmitter::ParticleEmitter(Context* context) :
  40. BillboardSet(context),
  41. periodTimer_(0.0f),
  42. emissionTimer_(0.0f),
  43. lastTimeStep_(0.0f),
  44. lastUpdateFrameNumber_(M_MAX_UNSIGNED),
  45. emitting_(true),
  46. needUpdate_(false),
  47. serializeParticles_(true),
  48. sendFinishedEvent_(true),
  49. autoRemove_(REMOVE_DISABLED)
  50. {
  51. SetNumParticles(DEFAULT_NUM_PARTICLES);
  52. }
  53. ParticleEmitter::~ParticleEmitter() = default;
  54. void ParticleEmitter::RegisterObject(Context* context)
  55. {
  56. context->RegisterFactory<ParticleEmitter>(GEOMETRY_CATEGORY);
  57. URHO3D_ACCESSOR_ATTRIBUTE("Is Enabled", IsEnabled, SetEnabled, bool, true, AM_DEFAULT);
  58. URHO3D_MIXED_ACCESSOR_ATTRIBUTE("Effect", GetEffectAttr, SetEffectAttr, ResourceRef, ResourceRef(ParticleEffect::GetTypeStatic()),
  59. AM_DEFAULT);
  60. URHO3D_ACCESSOR_ATTRIBUTE("Can Be Occluded", IsOccludee, SetOccludee, bool, true, AM_DEFAULT);
  61. URHO3D_ATTRIBUTE("Cast Shadows", bool, castShadows_, false, AM_DEFAULT);
  62. URHO3D_ACCESSOR_ATTRIBUTE("Draw Distance", GetDrawDistance, SetDrawDistance, float, 0.0f, AM_DEFAULT);
  63. URHO3D_ACCESSOR_ATTRIBUTE("Shadow Distance", GetShadowDistance, SetShadowDistance, float, 0.0f, AM_DEFAULT);
  64. URHO3D_ACCESSOR_ATTRIBUTE("Animation LOD Bias", GetAnimationLodBias, SetAnimationLodBias, float, 1.0f, AM_DEFAULT);
  65. URHO3D_ATTRIBUTE("Is Emitting", bool, emitting_, true, AM_FILE);
  66. URHO3D_ATTRIBUTE("Period Timer", float, periodTimer_, 0.0f, AM_FILE | AM_NOEDIT);
  67. URHO3D_ATTRIBUTE("Emission Timer", float, emissionTimer_, 0.0f, AM_FILE | AM_NOEDIT);
  68. URHO3D_ENUM_ATTRIBUTE("Autoremove Mode", autoRemove_, autoRemoveModeNames, REMOVE_DISABLED, AM_DEFAULT);
  69. URHO3D_COPY_BASE_ATTRIBUTES(Drawable);
  70. URHO3D_MIXED_ACCESSOR_ATTRIBUTE("Particles", GetParticlesAttr, SetParticlesAttr, VariantVector, Variant::emptyVariantVector,
  71. AM_FILE | AM_NOEDIT);
  72. URHO3D_MIXED_ACCESSOR_ATTRIBUTE("Billboards", GetParticleBillboardsAttr, SetBillboardsAttr, VariantVector, Variant::emptyVariantVector,
  73. AM_FILE | AM_NOEDIT);
  74. URHO3D_ATTRIBUTE("Serialize Particles", bool, serializeParticles_, true, AM_FILE);
  75. }
  76. void ParticleEmitter::OnSetEnabled()
  77. {
  78. BillboardSet::OnSetEnabled();
  79. Scene* scene = GetScene();
  80. if (scene)
  81. {
  82. if (IsEnabledEffective())
  83. SubscribeToEvent(scene, E_SCENEPOSTUPDATE, URHO3D_HANDLER(ParticleEmitter, HandleScenePostUpdate));
  84. else
  85. UnsubscribeFromEvent(scene, E_SCENEPOSTUPDATE);
  86. }
  87. }
  88. void ParticleEmitter::Update(const FrameInfo& frame)
  89. {
  90. if (!effect_)
  91. return;
  92. // Cancel update if has only moved but does not actually need to animate the particles
  93. if (!needUpdate_)
  94. return;
  95. // If there is an amount mismatch between particles and billboards, correct it
  96. if (particles_.Size() != billboards_.Size())
  97. SetNumBillboards(particles_.Size());
  98. bool needCommit = false;
  99. // Check active/inactive period switching
  100. periodTimer_ += lastTimeStep_;
  101. if (emitting_)
  102. {
  103. float activeTime = effect_->GetActiveTime();
  104. if (activeTime && periodTimer_ >= activeTime)
  105. {
  106. emitting_ = false;
  107. periodTimer_ -= activeTime;
  108. }
  109. }
  110. else
  111. {
  112. float inactiveTime = effect_->GetInactiveTime();
  113. if (inactiveTime && periodTimer_ >= inactiveTime)
  114. {
  115. emitting_ = true;
  116. sendFinishedEvent_ = true;
  117. periodTimer_ -= inactiveTime;
  118. }
  119. // If emitter has an indefinite stop interval, keep period timer reset to allow restarting emission in the editor
  120. if (inactiveTime == 0.0f)
  121. periodTimer_ = 0.0f;
  122. }
  123. // Check for emitting new particles
  124. if (emitting_)
  125. {
  126. emissionTimer_ += lastTimeStep_;
  127. float intervalMin = 1.0f / effect_->GetMaxEmissionRate();
  128. float intervalMax = 1.0f / effect_->GetMinEmissionRate();
  129. // If emission timer has a longer delay than max. interval, clamp it
  130. if (emissionTimer_ < -intervalMax)
  131. emissionTimer_ = -intervalMax;
  132. unsigned counter = MAX_PARTICLES_IN_FRAME;
  133. while (emissionTimer_ > 0.0f && counter)
  134. {
  135. emissionTimer_ -= Lerp(intervalMin, intervalMax, Random(1.0f));
  136. if (EmitNewParticle())
  137. {
  138. --counter;
  139. needCommit = true;
  140. }
  141. else
  142. break;
  143. }
  144. }
  145. // Update existing particles
  146. Vector3 relativeConstantForce = node_->GetWorldRotation().Inverse() * effect_->GetConstantForce();
  147. // If billboards are not relative, apply scaling to the position update
  148. Vector3 scaleVector = Vector3::ONE;
  149. if (scaled_ && !relative_)
  150. scaleVector = node_->GetWorldScale();
  151. for (unsigned i = 0; i < particles_.Size(); ++i)
  152. {
  153. Particle& particle = particles_[i];
  154. Billboard& billboard = billboards_[i];
  155. if (billboard.enabled_)
  156. {
  157. needCommit = true;
  158. // Time to live
  159. if (particle.timer_ >= particle.timeToLive_)
  160. {
  161. billboard.enabled_ = false;
  162. continue;
  163. }
  164. particle.timer_ += lastTimeStep_;
  165. // Velocity & position
  166. const Vector3& constantForce = effect_->GetConstantForce();
  167. if (constantForce != Vector3::ZERO)
  168. {
  169. if (relative_)
  170. particle.velocity_ += lastTimeStep_ * relativeConstantForce;
  171. else
  172. particle.velocity_ += lastTimeStep_ * constantForce;
  173. }
  174. float dampingForce = effect_->GetDampingForce();
  175. if (dampingForce != 0.0f)
  176. {
  177. Vector3 force = -dampingForce * particle.velocity_;
  178. particle.velocity_ += lastTimeStep_ * force;
  179. }
  180. billboard.position_ += lastTimeStep_ * particle.velocity_ * scaleVector;
  181. billboard.direction_ = particle.velocity_.Normalized();
  182. // Rotation
  183. billboard.rotation_ += lastTimeStep_ * particle.rotationSpeed_;
  184. // Scaling
  185. float sizeAdd = effect_->GetSizeAdd();
  186. float sizeMul = effect_->GetSizeMul();
  187. if (sizeAdd != 0.0f || sizeMul != 1.0f)
  188. {
  189. particle.scale_ += lastTimeStep_ * sizeAdd;
  190. if (particle.scale_ < 0.0f)
  191. particle.scale_ = 0.0f;
  192. if (sizeMul != 1.0f)
  193. particle.scale_ *= (lastTimeStep_ * (sizeMul - 1.0f)) + 1.0f;
  194. billboard.size_ = particle.size_ * particle.scale_;
  195. }
  196. // Color interpolation
  197. unsigned& index = particle.colorIndex_;
  198. const Vector<ColorFrame>& colorFrames_ = effect_->GetColorFrames();
  199. if (index < colorFrames_.Size())
  200. {
  201. if (index < colorFrames_.Size() - 1)
  202. {
  203. if (particle.timer_ >= colorFrames_[index + 1].time_)
  204. ++index;
  205. }
  206. if (index < colorFrames_.Size() - 1)
  207. billboard.color_ = colorFrames_[index].Interpolate(colorFrames_[index + 1], particle.timer_);
  208. else
  209. billboard.color_ = colorFrames_[index].color_;
  210. }
  211. // Texture animation
  212. unsigned& texIndex = particle.texIndex_;
  213. const Vector<TextureFrame>& textureFrames_ = effect_->GetTextureFrames();
  214. if (textureFrames_.Size() && texIndex < textureFrames_.Size() - 1)
  215. {
  216. if (particle.timer_ >= textureFrames_[texIndex + 1].time_)
  217. {
  218. billboard.uv_ = textureFrames_[texIndex + 1].uv_;
  219. ++texIndex;
  220. }
  221. }
  222. }
  223. }
  224. if (needCommit)
  225. Commit();
  226. needUpdate_ = false;
  227. }
  228. void ParticleEmitter::SetEffect(ParticleEffect* effect)
  229. {
  230. if (effect == effect_)
  231. return;
  232. Reset();
  233. // Unsubscribe from the reload event of previous effect (if any), then subscribe to the new
  234. if (effect_)
  235. UnsubscribeFromEvent(effect_, E_RELOADFINISHED);
  236. effect_ = effect;
  237. if (effect_)
  238. SubscribeToEvent(effect_, E_RELOADFINISHED, URHO3D_HANDLER(ParticleEmitter, HandleEffectReloadFinished));
  239. ApplyEffect();
  240. MarkNetworkUpdate();
  241. }
  242. void ParticleEmitter::SetNumParticles(unsigned num)
  243. {
  244. // Prevent negative value being assigned from the editor
  245. if (num > M_MAX_INT)
  246. num = 0;
  247. particles_.Resize(num);
  248. SetNumBillboards(num);
  249. }
  250. void ParticleEmitter::SetEmitting(bool enable)
  251. {
  252. if (enable != emitting_)
  253. {
  254. emitting_ = enable;
  255. // If stopping emission now, and there are active particles, send finish event once they are gone
  256. sendFinishedEvent_ = enable || CheckActiveParticles();
  257. periodTimer_ = 0.0f;
  258. // Note: network update does not need to be marked as this is a file only attribute
  259. }
  260. }
  261. void ParticleEmitter::SetSerializeParticles(bool enable)
  262. {
  263. serializeParticles_ = enable;
  264. // Note: network update does not need to be marked as this is a file only attribute
  265. }
  266. void ParticleEmitter::SetAutoRemoveMode(AutoRemoveMode mode)
  267. {
  268. autoRemove_ = mode;
  269. MarkNetworkUpdate();
  270. }
  271. void ParticleEmitter::ResetEmissionTimer()
  272. {
  273. emissionTimer_ = 0.0f;
  274. }
  275. void ParticleEmitter::RemoveAllParticles()
  276. {
  277. for (PODVector<Billboard>::Iterator i = billboards_.Begin(); i != billboards_.End(); ++i)
  278. i->enabled_ = false;
  279. Commit();
  280. }
  281. void ParticleEmitter::Reset()
  282. {
  283. RemoveAllParticles();
  284. ResetEmissionTimer();
  285. SetEmitting(true);
  286. }
  287. void ParticleEmitter::ApplyEffect()
  288. {
  289. if (!effect_)
  290. return;
  291. SetMaterial(effect_->GetMaterial());
  292. SetNumParticles(effect_->GetNumParticles());
  293. SetRelative(effect_->IsRelative());
  294. SetScaled(effect_->IsScaled());
  295. SetSorted(effect_->IsSorted());
  296. SetFixedScreenSize(effect_->IsFixedScreenSize());
  297. SetAnimationLodBias(effect_->GetAnimationLodBias());
  298. SetFaceCameraMode(effect_->GetFaceCameraMode());
  299. }
  300. ParticleEffect* ParticleEmitter::GetEffect() const
  301. {
  302. return effect_;
  303. }
  304. void ParticleEmitter::SetEffectAttr(const ResourceRef& value)
  305. {
  306. auto* cache = GetSubsystem<ResourceCache>();
  307. SetEffect(cache->GetResource<ParticleEffect>(value.name_));
  308. }
  309. ResourceRef ParticleEmitter::GetEffectAttr() const
  310. {
  311. return GetResourceRef(effect_, ParticleEffect::GetTypeStatic());
  312. }
  313. void ParticleEmitter::SetParticlesAttr(const VariantVector& value)
  314. {
  315. unsigned index = 0;
  316. SetNumParticles(index < value.Size() ? value[index++].GetUInt() : 0);
  317. for (PODVector<Particle>::Iterator i = particles_.Begin(); i != particles_.End() && index < value.Size(); ++i)
  318. {
  319. i->velocity_ = value[index++].GetVector3();
  320. i->size_ = value[index++].GetVector2();
  321. i->timer_ = value[index++].GetFloat();
  322. i->timeToLive_ = value[index++].GetFloat();
  323. i->scale_ = value[index++].GetFloat();
  324. i->rotationSpeed_ = value[index++].GetFloat();
  325. i->colorIndex_ = (unsigned)value[index++].GetInt();
  326. i->texIndex_ = (unsigned)value[index++].GetInt();
  327. }
  328. }
  329. VariantVector ParticleEmitter::GetParticlesAttr() const
  330. {
  331. VariantVector ret;
  332. if (!serializeParticles_)
  333. {
  334. ret.Push(particles_.Size());
  335. return ret;
  336. }
  337. ret.Reserve(particles_.Size() * 8 + 1);
  338. ret.Push(particles_.Size());
  339. for (PODVector<Particle>::ConstIterator i = particles_.Begin(); i != particles_.End(); ++i)
  340. {
  341. ret.Push(i->velocity_);
  342. ret.Push(i->size_);
  343. ret.Push(i->timer_);
  344. ret.Push(i->timeToLive_);
  345. ret.Push(i->scale_);
  346. ret.Push(i->rotationSpeed_);
  347. ret.Push(i->colorIndex_);
  348. ret.Push(i->texIndex_);
  349. }
  350. return ret;
  351. }
  352. VariantVector ParticleEmitter::GetParticleBillboardsAttr() const
  353. {
  354. VariantVector ret;
  355. if (!serializeParticles_)
  356. {
  357. ret.Push(billboards_.Size());
  358. return ret;
  359. }
  360. ret.Reserve(billboards_.Size() * 7 + 1);
  361. ret.Push(billboards_.Size());
  362. for (PODVector<Billboard>::ConstIterator i = billboards_.Begin(); i != billboards_.End(); ++i)
  363. {
  364. ret.Push(i->position_);
  365. ret.Push(i->size_);
  366. ret.Push(Vector4(i->uv_.min_.x_, i->uv_.min_.y_, i->uv_.max_.x_, i->uv_.max_.y_));
  367. ret.Push(i->color_);
  368. ret.Push(i->rotation_);
  369. ret.Push(i->direction_);
  370. ret.Push(i->enabled_);
  371. }
  372. return ret;
  373. }
  374. void ParticleEmitter::OnSceneSet(Scene* scene)
  375. {
  376. BillboardSet::OnSceneSet(scene);
  377. if (scene && IsEnabledEffective())
  378. SubscribeToEvent(scene, E_SCENEPOSTUPDATE, URHO3D_HANDLER(ParticleEmitter, HandleScenePostUpdate));
  379. else if (!scene)
  380. UnsubscribeFromEvent(E_SCENEPOSTUPDATE);
  381. }
  382. bool ParticleEmitter::EmitNewParticle()
  383. {
  384. unsigned index = GetFreeParticle();
  385. if (index == M_MAX_UNSIGNED)
  386. return false;
  387. assert(index < particles_.Size());
  388. Particle& particle = particles_[index];
  389. Billboard& billboard = billboards_[index];
  390. Vector3 startDir;
  391. Vector3 startPos;
  392. startDir = effect_->GetRandomDirection();
  393. startDir.Normalize();
  394. switch (effect_->GetEmitterType())
  395. {
  396. case EMITTER_SPHERE:
  397. {
  398. Vector3 dir(
  399. Random(2.0f) - 1.0f,
  400. Random(2.0f) - 1.0f,
  401. Random(2.0f) - 1.0f
  402. );
  403. dir.Normalize();
  404. startPos = effect_->GetEmitterSize() * dir * 0.5f;
  405. }
  406. break;
  407. case EMITTER_BOX:
  408. {
  409. const Vector3& emitterSize = effect_->GetEmitterSize();
  410. startPos = Vector3(
  411. Random(emitterSize.x_) - emitterSize.x_ * 0.5f,
  412. Random(emitterSize.y_) - emitterSize.y_ * 0.5f,
  413. Random(emitterSize.z_) - emitterSize.z_ * 0.5f
  414. );
  415. }
  416. break;
  417. case EMITTER_SPHEREVOLUME:
  418. {
  419. Vector3 dir(
  420. Random(2.0f) - 1.0f,
  421. Random(2.0f) - 1.0f,
  422. Random(2.0f) - 1.0f
  423. );
  424. dir.Normalize();
  425. startPos = effect_->GetEmitterSize() * dir * Pow(Random(), 1.0f / 3.0f) * 0.5f;
  426. }
  427. break;
  428. case EMITTER_CYLINDER:
  429. {
  430. float angle = Random(360.0f);
  431. float radius = Sqrt(Random()) * 0.5f;
  432. startPos = Vector3(Cos(angle) * radius, Random() - 0.5f, Sin(angle) * radius) * effect_->GetEmitterSize();
  433. }
  434. break;
  435. case EMITTER_RING:
  436. {
  437. float angle = Random(360.0f);
  438. startPos = Vector3(Cos(angle), Random(2.0f) - 1.0f, Sin(angle)) * effect_->GetEmitterSize() * 0.5f;
  439. }
  440. break;
  441. }
  442. particle.size_ = effect_->GetRandomSize();
  443. particle.timer_ = 0.0f;
  444. particle.timeToLive_ = effect_->GetRandomTimeToLive();
  445. particle.scale_ = 1.0f;
  446. particle.rotationSpeed_ = effect_->GetRandomRotationSpeed();
  447. particle.colorIndex_ = 0;
  448. particle.texIndex_ = 0;
  449. if (faceCameraMode_ == FC_DIRECTION)
  450. {
  451. startPos += startDir * particle.size_.y_;
  452. }
  453. if (!relative_)
  454. {
  455. startPos = node_->GetWorldTransform() * startPos;
  456. startDir = node_->GetWorldRotation() * startDir;
  457. };
  458. particle.velocity_ = effect_->GetRandomVelocity() * startDir;
  459. billboard.position_ = startPos;
  460. billboard.size_ = particles_[index].size_;
  461. const Vector<TextureFrame>& textureFrames_ = effect_->GetTextureFrames();
  462. billboard.uv_ = textureFrames_.Size() ? textureFrames_[0].uv_ : Rect::POSITIVE;
  463. billboard.rotation_ = effect_->GetRandomRotation();
  464. const Vector<ColorFrame>& colorFrames_ = effect_->GetColorFrames();
  465. billboard.color_ = colorFrames_.Size() ? colorFrames_[0].color_ : Color();
  466. billboard.enabled_ = true;
  467. billboard.direction_ = startDir;
  468. return true;
  469. }
  470. unsigned ParticleEmitter::GetFreeParticle() const
  471. {
  472. for (unsigned i = 0; i < billboards_.Size(); ++i)
  473. {
  474. if (!billboards_[i].enabled_)
  475. return i;
  476. }
  477. return M_MAX_UNSIGNED;
  478. }
  479. bool ParticleEmitter::CheckActiveParticles() const
  480. {
  481. for (unsigned i = 0; i < billboards_.Size(); ++i)
  482. {
  483. if (billboards_[i].enabled_)
  484. {
  485. return true;
  486. break;
  487. }
  488. }
  489. return false;
  490. }
  491. void ParticleEmitter::HandleScenePostUpdate(StringHash eventType, VariantMap& eventData)
  492. {
  493. // Store scene's timestep and use it instead of global timestep, as time scale may be other than 1
  494. using namespace ScenePostUpdate;
  495. lastTimeStep_ = eventData[P_TIMESTEP].GetFloat();
  496. // If no invisible update, check that the billboardset is in view (framenumber has changed)
  497. if ((effect_ && effect_->GetUpdateInvisible()) || viewFrameNumber_ != lastUpdateFrameNumber_)
  498. {
  499. lastUpdateFrameNumber_ = viewFrameNumber_;
  500. needUpdate_ = true;
  501. MarkForUpdate();
  502. }
  503. // Send finished event only once all particles are gone
  504. if (node_ && !emitting_ && sendFinishedEvent_ && !CheckActiveParticles())
  505. {
  506. sendFinishedEvent_ = false;
  507. // Make a weak pointer to self to check for destruction during event handling
  508. WeakPtr<ParticleEmitter> self(this);
  509. using namespace ParticleEffectFinished;
  510. VariantMap& eventData = GetEventDataMap();
  511. eventData[P_NODE] = node_;
  512. eventData[P_EFFECT] = effect_;
  513. node_->SendEvent(E_PARTICLEEFFECTFINISHED, eventData);
  514. if (self.Expired())
  515. return;
  516. DoAutoRemove(autoRemove_);
  517. }
  518. }
  519. void ParticleEmitter::HandleEffectReloadFinished(StringHash eventType, VariantMap& eventData)
  520. {
  521. // When particle effect file is live-edited, remove existing particles and reapply the effect parameters
  522. Reset();
  523. ApplyEffect();
  524. }
  525. }