ParticleModel2D.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. //
  2. // Copyright (c) 2008-2014 the Urho3D project.
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #include "Precompiled.h"
  23. #include "Context.h"
  24. #include "FileSystem.h"
  25. #include "ParticleModel2D.h"
  26. #include "ResourceCache.h"
  27. #include "StringUtils.h"
  28. #include "Sprite2D.h"
  29. #include "XMLFile.h"
  30. #include "DebugNew.h"
  31. namespace Urho3D
  32. {
  33. static const int srcBlendFuncs[] =
  34. {
  35. 1, // GL_ONE
  36. 1, // GL_ONE
  37. 0x0306, // GL_DST_COLOR
  38. 0x0302, // GL_SRC_ALPHA
  39. 0x0302, // GL_SRC_ALPHA
  40. 1, // GL_ONE
  41. 0x0305 // GL_ONE_MINUS_DST_ALPHA
  42. };
  43. static const int destBlendFuncs[] =
  44. {
  45. 0, // GL_ZERO
  46. 1, // GL_ONE
  47. 0, // GL_ZERO
  48. 0x0303, // GL_ONE_MINUS_SRC_ALPHA
  49. 1, // GL_ONE
  50. 0x0303, // GL_ONE_MINUS_SRC_ALPHA
  51. 0x0304 // GL_DST_ALPHA
  52. };
  53. ParticleModel2D::ParticleModel2D(Context* context) :
  54. Resource(context),
  55. sourcePosition_(157.97f, 228.41f),// Values from sun.pex
  56. sourcePositionVariance_(7.0f, 7.0f),
  57. speed_(260.0f),
  58. speedVariance_(10.0f),
  59. particleLifeSpan_(1.000f),
  60. particleLifespanVariance_(0.700f),
  61. angle_(0.0f),
  62. angleVariance_(360.0f),
  63. gravity_(0.0f, 0.0f),
  64. radialAcceleration_(-380.0f),
  65. tangentialAcceleration_(-140.0f),
  66. radialAccelVariance_(0.0f),
  67. tangentialAccelVariance_(0.0f),
  68. startColor_(1.0f, 0.0f, 0.0f, 1.0f),
  69. startColorVariance_(0.0f, 0.0f, 0.0f, 0.0f),
  70. finishColor_(1.0f, 1.0f, 0.0f, 1.0f),
  71. finishColorVariance_(0.0f, 0.0f, 0.0f, 0.0f),
  72. maxParticles_(600),
  73. startParticleSize_(60.0f),
  74. startParticleSizeVariance_(40.0f),
  75. finishParticleSize_(5.0f),
  76. FinishParticleSizeVariance_(5.0f),
  77. duration_(-1.0f),
  78. emitterType_(EMITTER_TYPE_GRAVITY),
  79. maxRadius_(100.0f),
  80. maxRadiusVariance_(0.0f),
  81. minRadius_(0.0f),
  82. rotatePerSecond_(0.0f),
  83. rotatePerSecondVariance_(0.0f),
  84. blendMode_(BLEND_ALPHA),
  85. rotationStart_(0.0f),
  86. rotationStartVariance_(0.0f),
  87. rotationEnd_(0.0f),
  88. rotationEndVariance_(0.0f)
  89. {
  90. }
  91. ParticleModel2D::~ParticleModel2D()
  92. {
  93. }
  94. void ParticleModel2D::RegisterObject(Context* context)
  95. {
  96. context->RegisterFactory<ParticleModel2D>();
  97. }
  98. bool ParticleModel2D::Load(Deserializer& source)
  99. {
  100. XMLFile xmlFile(context_);
  101. if (!xmlFile.Load(source))
  102. return false;
  103. XMLElement rootElem = xmlFile.GetRoot("particleEmitterConfig");
  104. if (!rootElem)
  105. return false;
  106. String texture = rootElem.GetChild("texture").GetAttribute("name");
  107. ResourceCache* cache = GetSubsystem<ResourceCache>();
  108. sprite_= cache->GetResource<Sprite2D>(GetParentPath(GetName()) + texture);
  109. if (!sprite_)
  110. return false;
  111. sourcePosition_ = ReadVector2(rootElem.GetChild("sourcePosition"));
  112. sourcePositionVariance_ = ReadVector2(rootElem.GetChild("sourcePositionVariance"));
  113. speed_ = rootElem.GetChild("speed").GetFloat("value");
  114. speedVariance_ = rootElem.GetChild("speedVariance").GetFloat("value");
  115. particleLifeSpan_ = rootElem.GetChild("particleLifeSpan").GetFloat("value");
  116. particleLifespanVariance_ = rootElem.GetChild("particleLifespanVariance").GetFloat("value");
  117. angle_ = 360.0f - rootElem.GetChild("angle").GetFloat("value");
  118. angleVariance_ = rootElem.GetChild("angleVariance").GetFloat("value");
  119. gravity_ = ReadVector2(rootElem.GetChild("gravity"));
  120. radialAcceleration_ = rootElem.GetChild("radialAcceleration").GetFloat("value");
  121. tangentialAcceleration_ = -rootElem.GetChild("tangentialAcceleration").GetFloat("value");
  122. radialAccelVariance_ = rootElem.GetChild("radialAccelVariance").GetFloat("value");
  123. tangentialAccelVariance_ = rootElem.GetChild("tangentialAccelVariance").GetFloat("value");
  124. startColor_ = ReadColor(rootElem.GetChild("startColor"));
  125. startColorVariance_ = ReadColor(rootElem.GetChild("startColorVariance"));
  126. finishColor_ = ReadColor(rootElem.GetChild("finishColor"));
  127. finishColorVariance_ = ReadColor(rootElem.GetChild("finishColorVariance"));
  128. maxParticles_ = rootElem.GetChild("maxParticles").GetInt("value");
  129. startParticleSize_ = rootElem.GetChild("startParticleSize").GetFloat("value");
  130. startParticleSizeVariance_ = rootElem.GetChild("startParticleSizeVariance").GetFloat("value");
  131. finishParticleSize_ = rootElem.GetChild("finishParticleSize").GetFloat("value");
  132. FinishParticleSizeVariance_ = rootElem.GetChild("FinishParticleSizeVariance").GetFloat("value");
  133. duration_ = rootElem.GetChild("duration").GetFloat("value");
  134. emitterType_ = (EmitterType2D)rootElem.GetChild("emitterType").GetInt("value");
  135. maxRadius_ = rootElem.GetChild("maxRadius").GetFloat("value");
  136. maxRadiusVariance_ = rootElem.GetChild("maxRadiusVariance").GetFloat("value");
  137. minRadius_ = rootElem.GetChild("minRadius").GetFloat("value");
  138. rotatePerSecond_ = -rootElem.GetChild("rotatePerSecond").GetFloat("value");
  139. rotatePerSecondVariance_ = rootElem.GetChild("rotatePerSecondVariance").GetFloat("value");
  140. int blendFuncSource = rootElem.GetChild("blendFuncSource").GetInt("value");
  141. int blendFuncDestination = rootElem.GetChild("blendFuncDestination").GetInt("value");
  142. blendMode_ = BLEND_ALPHA;
  143. for (int i = 0; i < MAX_BLENDMODES; ++i)
  144. {
  145. if (blendFuncSource == srcBlendFuncs[i] && blendFuncDestination == destBlendFuncs[i])
  146. {
  147. blendMode_ = (BlendMode)i;
  148. break;
  149. }
  150. }
  151. rotationStart_ = -rootElem.GetChild("rotationStart").GetFloat("value");
  152. rotationStartVariance_ = rootElem.GetChild("rotationStartVariance").GetFloat("value");
  153. rotationEnd_ = -rootElem.GetChild("rotationEnd").GetFloat("value");
  154. rotationEndVariance_ = rootElem.GetChild("rotationEndVariance").GetFloat("value");
  155. return true;
  156. }
  157. bool ParticleModel2D::Save(Serializer& dest) const
  158. {
  159. return false;
  160. }
  161. void ParticleModel2D::SetSourcePosition(const Vector2& sourcePosition)
  162. {
  163. sourcePosition_ = sourcePosition;
  164. }
  165. void ParticleModel2D::SetSourcePositionVariance(const Vector2& sourcePositionVariance)
  166. {
  167. sourcePositionVariance_ = sourcePositionVariance;
  168. }
  169. void ParticleModel2D::SetSpeed(float speed)
  170. {
  171. speed_ = speed;
  172. }
  173. void ParticleModel2D::SetSpeedVariance(float speedVariance)
  174. {
  175. speedVariance_ = speedVariance;
  176. }
  177. void ParticleModel2D::SetParticleLifeSpan(float particleLifeSpan)
  178. {
  179. particleLifeSpan_ = particleLifeSpan;
  180. }
  181. void ParticleModel2D::SetParticleLifespanVariance(float particleLifespanVariance)
  182. {
  183. particleLifespanVariance_ = particleLifespanVariance;
  184. }
  185. void ParticleModel2D::SetAngle(float angle)
  186. {
  187. angle_ = angle;
  188. }
  189. void ParticleModel2D::SetAngleVariance(float angleVariance)
  190. {
  191. angleVariance_ = angleVariance;
  192. }
  193. void ParticleModel2D::SetGravity(const Vector2& gravity)
  194. {
  195. gravity_ = gravity;
  196. }
  197. void ParticleModel2D::SetRadialAcceleration(float radialAcceleration)
  198. {
  199. radialAcceleration_ = radialAcceleration;
  200. }
  201. void ParticleModel2D::SetTangentialAcceleration(float tangentialAcceleration)
  202. {
  203. tangentialAcceleration_ = tangentialAcceleration;
  204. }
  205. void ParticleModel2D::SetRadialAccelVariance(float radialAccelVariance)
  206. {
  207. radialAccelVariance_ = radialAccelVariance;
  208. }
  209. void ParticleModel2D::SetTangentialAccelVariance(float tangentialAccelVariance)
  210. {
  211. tangentialAccelVariance_ = tangentialAccelVariance;
  212. }
  213. void ParticleModel2D::SetStartColor(const Color& startColor)
  214. {
  215. startColor_ = startColor;
  216. }
  217. void ParticleModel2D::SetStartColorVariance(const Color& startColorVariance)
  218. {
  219. startColorVariance_ = startColorVariance;
  220. }
  221. void ParticleModel2D::SetFinishColor(const Color& finishColor)
  222. {
  223. finishColor_ = finishColor;
  224. }
  225. void ParticleModel2D::SetFinishColorVariance(const Color& finishColorVariance)
  226. {
  227. finishColorVariance_ = finishColorVariance;
  228. }
  229. void ParticleModel2D::SetMaxParticles(int maxParticles)
  230. {
  231. maxParticles_ = maxParticles;
  232. }
  233. void ParticleModel2D::SetStartParticleSize(float startParticleSize)
  234. {
  235. startParticleSize_ = startParticleSize;
  236. }
  237. void ParticleModel2D::SetStartParticleSizeVariance(float startParticleSizeVariance)
  238. {
  239. startParticleSizeVariance_ = startParticleSizeVariance;
  240. }
  241. void ParticleModel2D::SetFinishParticleSize(float finishParticleSize)
  242. {
  243. finishParticleSize_ = finishParticleSize;
  244. }
  245. void ParticleModel2D::SetFinishParticleSizeVariance(float FinishParticleSizeVariance)
  246. {
  247. FinishParticleSizeVariance_ = FinishParticleSizeVariance;
  248. }
  249. void ParticleModel2D::SetDuration(float duration)
  250. {
  251. duration_ = duration;
  252. }
  253. void ParticleModel2D::SetEmitterType(EmitterType2D emitterType)
  254. {
  255. emitterType_ = emitterType;
  256. }
  257. void ParticleModel2D::SetMaxRadius(float maxRadius)
  258. {
  259. maxRadius_ = maxRadius;
  260. }
  261. void ParticleModel2D::SetMaxRadiusVariance(float maxRadiusVariance)
  262. {
  263. maxRadiusVariance_ = maxRadiusVariance;
  264. }
  265. void ParticleModel2D::SetMinRadius(float minRadius)
  266. {
  267. minRadius_ = minRadius;
  268. }
  269. void ParticleModel2D::SetRotatePerSecond(float rotatePerSecond)
  270. {
  271. rotatePerSecond_ = rotatePerSecond;
  272. }
  273. void ParticleModel2D::SetRotatePerSecondVariance(float rotatePerSecondVariance)
  274. {
  275. rotatePerSecondVariance_ = rotatePerSecondVariance;
  276. }
  277. void ParticleModel2D::SetBlendMode(BlendMode blendMode)
  278. {
  279. blendMode_ = blendMode;
  280. }
  281. void ParticleModel2D::SetRotationStart(float rotationStart)
  282. {
  283. rotationStart_ = rotationStart;
  284. }
  285. void ParticleModel2D::SetRotationStartVariance(float rotationStartVariance)
  286. {
  287. rotationStartVariance_ = rotationStartVariance;
  288. }
  289. void ParticleModel2D::SetRotationEnd(float rotationEnd)
  290. {
  291. rotationEnd_ = rotationEnd;
  292. }
  293. void ParticleModel2D::SetRotationEndVariance(float rotationEndVariance)
  294. {
  295. rotationEndVariance_ = rotationEndVariance;
  296. }
  297. Color ParticleModel2D::ReadColor(const XMLElement& element) const
  298. {
  299. Color color;
  300. color.r_ = element.GetFloat("red");
  301. color.g_ = element.GetFloat("green");
  302. color.b_ = element.GetFloat("blue");
  303. color.a_ = element.GetFloat("alpha");
  304. return color;
  305. }
  306. Vector2 ParticleModel2D::ReadVector2(const XMLElement& element) const
  307. {
  308. // Flip y.
  309. return Vector2(element.GetFloat("x"), -element.GetFloat("y"));
  310. }
  311. }