ParticleEmitter2D.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  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 "Camera.h"
  24. #include "Context.h"
  25. #include "ParticleModel2D.h"
  26. #include "ParticleEmitter2D.h"
  27. #include "ResourceCache.h"
  28. #include "Scene.h"
  29. #include "SceneEvents.h"
  30. #include "DebugNew.h"
  31. namespace Urho3D
  32. {
  33. extern const char* URHO2D_CATEGORY;
  34. ParticleEmitter2D::ParticleEmitter2D(Context* context) :
  35. Drawable2D(context),
  36. lifeTime_(0.0f),
  37. numParticles_(0),
  38. emitParticleTime_(0.0f),
  39. timeBetweenParticles_(1.0f)
  40. {
  41. }
  42. ParticleEmitter2D::~ParticleEmitter2D()
  43. {
  44. }
  45. void ParticleEmitter2D::RegisterObject(Context* context)
  46. {
  47. context->RegisterFactory<ParticleEmitter2D>(URHO2D_CATEGORY);
  48. ACCESSOR_ATTRIBUTE(ParticleEmitter2D, VAR_RESOURCEREF, "Particle Model", GetParticleModelAttr, SetParticleModelAttr, ResourceRef, ResourceRef(ParticleModel2D::GetTypeStatic()), AM_DEFAULT);
  49. COPY_BASE_ATTRIBUTES(ParticleEmitter2D, Drawable2D);
  50. }
  51. void ParticleEmitter2D::OnSetEnabled()
  52. {
  53. Drawable2D::OnSetEnabled();
  54. Scene* scene = GetScene();
  55. if (scene)
  56. {
  57. if (IsEnabledEffective())
  58. SubscribeToEvent(scene, E_SCENEPOSTUPDATE, HANDLER(ParticleEmitter2D, HandleScenePostUpdate));
  59. else
  60. UnsubscribeFromEvent(scene, E_SCENEPOSTUPDATE);
  61. }
  62. }
  63. void ParticleEmitter2D::UpdateBatches(const FrameInfo& frame)
  64. {
  65. // const Matrix3x4& worldTransform = node_->GetWorldTransform();
  66. distance_ = frame.camera_->GetDistance(GetWorldBoundingBox().Center());
  67. batches_[0].distance_ = distance_;
  68. batches_[0].worldTransform_ = &Matrix3x4::IDENTITY;
  69. }
  70. void ParticleEmitter2D::Update(const FrameInfo& frame)
  71. {
  72. if (!model_)
  73. return;
  74. float timeStep = frame.timeStep_;
  75. Vector3 worldPosition = GetNode()->GetWorldPosition();
  76. float worldScale = GetNode()->GetWorldScale().x_;
  77. unsigned particleIndex = 0;
  78. while (particleIndex < numParticles_)
  79. {
  80. Particle2D& currentParticle = particles_[particleIndex];
  81. if (currentParticle.timeToLive_ > timeStep)
  82. {
  83. UpdateParticle(currentParticle, timeStep, worldPosition, worldScale);
  84. ++particleIndex;
  85. }
  86. else
  87. {
  88. if (particleIndex != numParticles_ - 1)
  89. particles_[particleIndex] = particles_[numParticles_ - 1];
  90. --numParticles_;
  91. }
  92. }
  93. if (lifeTime_< 0.0f || lifeTime_ > 0.0f)
  94. {
  95. float worldAngle = GetNode()->GetWorldRotation().RollAngle();
  96. emitParticleTime_ += timeStep;
  97. while (emitParticleTime_ > 0.0f)
  98. {
  99. EmitParticle(worldPosition, worldAngle, worldScale);
  100. emitParticleTime_ -= timeBetweenParticles_;
  101. }
  102. if (lifeTime_ > 0.0f)
  103. lifeTime_ = Max(0.0f, lifeTime_ - timeStep);
  104. }
  105. verticesDirty_ = true;
  106. OnMarkedDirty(node_);
  107. }
  108. void ParticleEmitter2D::SetModel(ParticleModel2D* model)
  109. {
  110. if (model == model_)
  111. return;
  112. model_ = model;
  113. MarkNetworkUpdate();
  114. if (!model_)
  115. return;
  116. SetSprite(model_->GetSprite());
  117. SetBlendMode(model_->GetBlendMode());
  118. lifeTime_ = model_->GetDuration();
  119. numParticles_ = Min((int)model_->GetMaxParticles(), (int)numParticles_);
  120. particles_.Resize(model_->GetMaxParticles());
  121. vertices_.Reserve(model_->GetMaxParticles() * 4);
  122. emitParticleTime_ = 0.0f;
  123. timeBetweenParticles_ = model_->GetParticleLifeSpan() / model_->GetMaxParticles();
  124. }
  125. ParticleModel2D* ParticleEmitter2D::GetModel() const
  126. {
  127. return model_;
  128. }
  129. void ParticleEmitter2D::SetParticleModelAttr(ResourceRef value)
  130. {
  131. ResourceCache* cache = GetSubsystem<ResourceCache>();
  132. SetModel(cache->GetResource<ParticleModel2D>(value.name_));
  133. }
  134. Urho3D::ResourceRef ParticleEmitter2D::GetParticleModelAttr() const
  135. {
  136. return GetResourceRef(model_, ParticleModel2D::GetTypeStatic());
  137. }
  138. void ParticleEmitter2D::OnNodeSet(Node* node)
  139. {
  140. Drawable2D::OnNodeSet(node);
  141. if (node)
  142. {
  143. Scene* scene = GetScene();
  144. if (scene && IsEnabledEffective())
  145. SubscribeToEvent(scene, E_SCENEPOSTUPDATE, HANDLER(ParticleEmitter2D, HandleScenePostUpdate));
  146. }
  147. }
  148. void ParticleEmitter2D::OnWorldBoundingBoxUpdate()
  149. {
  150. if (verticesDirty_)
  151. {
  152. UpdateVertices();
  153. boundingBox_.Clear();
  154. for (unsigned i = 0; i < vertices_.Size(); ++i)
  155. boundingBox_.Merge(vertices_[i].position_);
  156. }
  157. worldBoundingBox_ = boundingBox_;
  158. }
  159. void ParticleEmitter2D::UpdateVertices()
  160. {
  161. if (!verticesDirty_)
  162. return;
  163. vertices_.Clear();
  164. if (!sprite_)
  165. return;
  166. Texture2D* texture = sprite_->GetTexture();
  167. if (!texture)
  168. return;
  169. const IntRect& rectangle_ = sprite_->GetRectangle();
  170. if (rectangle_.Width() == 0 || rectangle_.Height() == 0)
  171. return;
  172. Vertex2D vertex0;
  173. Vertex2D vertex1;
  174. Vertex2D vertex2;
  175. Vertex2D vertex3;
  176. vertex0.uv_ = Vector2(0.0f, 1.0f);
  177. vertex1.uv_ = Vector2(0.0f, 0.0f);
  178. vertex2.uv_ = Vector2(1.0f, 0.0f);
  179. vertex3.uv_ = Vector2(1.0f, 1.0f);
  180. for (unsigned i = 0; i < numParticles_; ++i)
  181. {
  182. Particle2D& p = particles_[i];
  183. float c = Cos(p.rotation_);
  184. float s = Sin(p.rotation_);
  185. float add = (c + s) * p.size_ * 0.5f;
  186. float sub = (c - s) * p.size_ * 0.5f;
  187. vertex0.position_ = Vector3(p.position_.x_ - sub, p.position_.y_ - add, zValue_) * unitPerPixel_;
  188. vertex1.position_ = Vector3(p.position_.x_ - add, p.position_.y_ + sub, zValue_) * unitPerPixel_;
  189. vertex2.position_ = Vector3(p.position_.x_ + sub, p.position_.y_ + add, zValue_) * unitPerPixel_;
  190. vertex3.position_ = Vector3(p.position_.x_ + add, p.position_.y_ - sub, zValue_) * unitPerPixel_;
  191. vertex0.color_ = vertex1.color_ = vertex2.color_ = vertex3.color_ = p.color_.ToUInt();
  192. vertices_.Push(vertex0);
  193. vertices_.Push(vertex1);
  194. vertices_.Push(vertex2);
  195. vertices_.Push(vertex3);
  196. }
  197. geometryDirty_ = true;
  198. verticesDirty_ = false;
  199. }
  200. void ParticleEmitter2D::HandleScenePostUpdate(StringHash eventType, VariantMap& eventData)
  201. {
  202. MarkForUpdate();
  203. }
  204. void ParticleEmitter2D::EmitParticle(const Vector3& worldPosition, float worldAngle, float worldScale)
  205. {
  206. if (numParticles_ >= model_->GetMaxParticles())
  207. return;
  208. float lifespan = model_->GetParticleLifeSpan() + model_->GetParticleLifeSpanVariance() * Random(-1.0f, 1.0f);
  209. if (lifespan <= 0.0f)
  210. return;
  211. float invLifespan = 1.0f / lifespan;
  212. Particle2D& particle = particles_[numParticles_++];
  213. particle.timeToLive_ = lifespan;
  214. particle.startPos_ = Vector2(worldPosition.x_, worldPosition.y_);
  215. particle.position_ = particle.startPos_ + model_->GetSourcePositionVariance() * Vector2(Random(-1.0f, 1.0f), Random(-1.0f, 1.0f)) * worldScale;
  216. float emitAngle = worldAngle + model_->GetEmitAngle() + model_->GetEmitAngleVariance() * Random(-1.0f, 1.0f);
  217. float speed = worldScale * (model_->GetSpeed() + model_->GetSpeedVariance() * Random(-1.0f, 1.0f));
  218. particle.velocity_ = Vector2(Cos(emitAngle), Sin(emitAngle)) * speed;
  219. particle.radius_ = worldScale * (model_->GetMaxRadius() + model_->GetMaxRadiusVariance() * Random(-1.0f, 1.0f));
  220. particle.radiusDelta_ = worldScale * model_->GetMaxRadius() * invLifespan;
  221. particle.rotation_ = emitAngle;
  222. particle.rotationDelta_ = model_->GetRotatePerSecond() + model_->GetRotatePerSecondVariance() * Random(-1.0f, 1.0f);
  223. particle.radialAccel_ = worldScale * (model_->GetRadialAcceleration() + model_->GetRadialAccelerationVariance() * Random(-1.0f, 1.0f));
  224. particle.tangentialAccel_ = worldScale * (model_->GetTangentialAcceleration() + model_->GetTangentialAccelerationVariance() * Random(-1.0f, 1.0f));
  225. particle.size_ = worldScale * Max(0.1f, model_->GetStartParticleSize() + model_->GetStartParticleSizeVariance() * Random(-1.0f, 1.0f));
  226. float endParticleSize = worldScale * Max(0.1f, model_->GetEndParticleSize() + model_->GetEndParticleSizeVariance() * Random(-1.0f, 1.0f));
  227. particle.sizeDelta_ = (endParticleSize - particle.size_) * invLifespan;
  228. particle.color_ = model_->GetStartColor() + model_->GetStartColorVariance() * Random(-1.0f, 1.0f);
  229. Color endColor = model_->GetEndColor() + model_->GetEndColorVariance() * Random(-1.0f, 1.0f);
  230. particle.colorDelta_ = (endColor - particle.color_) * invLifespan;
  231. }
  232. void ParticleEmitter2D::UpdateParticle(Particle2D& particle, float timeStep, const Vector3& worldPosition, float worldScale)
  233. {
  234. timeStep = Min(timeStep, particle.timeToLive_);
  235. particle.timeToLive_ -= timeStep;
  236. if (model_->GetEmitterType() == EMITTER_TYPE_RADIAL)
  237. {
  238. particle.rotation_ += particle.rotationDelta_ * timeStep;
  239. particle.radius_ -= particle.radiusDelta_ * timeStep;
  240. particle.position_.x_ = worldPosition.x_ - Cos(particle.rotation_) * particle.radius_;
  241. particle.position_.y_ = worldPosition.y_ - Sin(particle.rotation_) * particle.radius_;
  242. if (particle.radius_ < model_->GetMinRadius() * worldScale)
  243. particle.timeToLive_ = 0.0f;
  244. }
  245. else
  246. {
  247. Vector2 radial = particle.position_ - particle.startPos_;
  248. radial.Normalize();
  249. Vector2 tangential(-radial.y_, radial.x_);
  250. radial *= particle.radialAccel_;
  251. tangential *= particle.tangentialAccel_;
  252. particle.velocity_ += (model_->GetGravity() * worldScale + radial + tangential) * timeStep;
  253. particle.position_ += particle.velocity_ * timeStep;
  254. }
  255. particle.size_ += particle.sizeDelta_ * timeStep;
  256. particle.color_ += particle.colorDelta_ * timeStep;
  257. }
  258. }