Particle.cs 52 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329
  1. #region File Description
  2. //-----------------------------------------------------------------------------
  3. // Particle.cs
  4. //
  5. // Microsoft XNA Community Game Platform
  6. // Copyright (C) Microsoft Corporation. All rights reserved.
  7. //-----------------------------------------------------------------------------
  8. #endregion
  9. #region Using Statements
  10. using System;
  11. using System.Collections;
  12. using System.Collections.Generic;
  13. using System.IO;
  14. using System.Text;
  15. using Microsoft.Xna.Framework;
  16. using Microsoft.Xna.Framework.Graphics;
  17. using RobotGameData.GameObject;
  18. using RobotGameData.Render;
  19. using RobotGameData.GameInterface;
  20. using RobotGameData.Resource;
  21. using RobotGameData.Helper;
  22. #endregion
  23. namespace RobotGameData.ParticleSystem
  24. {
  25. #region Particle Object
  26. /// <summary>
  27. /// this object has information of one particle.
  28. /// </summary>
  29. public class ParticleObject
  30. {
  31. [Flags]
  32. public enum States
  33. {
  34. None = 0x00000000,
  35. Enable = 0x00000001,
  36. Initialized = 0x00000002,
  37. Fired = 0x00000004,
  38. FirstTime = 0x00000008,
  39. }
  40. public uint state = 0;
  41. public float time = 0.0f;
  42. public Vector3 emitDir = Vector3.Zero;
  43. public float posInterval = 0.0f;
  44. public float[] posArg = new float[2];
  45. public Vector3 startPos = Vector3.Zero;
  46. public Vector3 targetPos = Vector3.Zero;
  47. public Vector3[] pos = new Vector3[2];
  48. public float posRandomInterval = 0.0f;
  49. public float posRandom = 0.0f;
  50. public float mass = 0.0f;
  51. public Vector3 gravity = Vector3.Zero;
  52. public float scaleInterval = 0.0f;
  53. public float[] scaleArg = new float[2];
  54. public Vector3 startScale = Vector3.One;
  55. public Vector3 targetScale = Vector3.One;
  56. public Vector3 scale = Vector3.One;
  57. public float scaleRandom = 0.0f;
  58. public float rotateAccm = 0.0f;
  59. public float rotateArg = 0.0f;
  60. public Vector3 rotateDir = Vector3.Zero;
  61. public float rotateRandom = 0.0f;
  62. public float textureSequenceInterval = 0.0f;
  63. public Color color = Color.Black;
  64. public bool IsState(States state)
  65. {
  66. return ((this.state & (uint)state) > 0);
  67. }
  68. public bool IsState(uint state)
  69. {
  70. return ((this.state & state) > 0);
  71. }
  72. }
  73. #endregion
  74. #region Particle
  75. /// <summary>
  76. /// It means a single individual particle.
  77. /// The number of Particle class and the number of TimeSequenceData class,
  78. /// which contains the time information of the Particle class, are equal.
  79. /// Each particle may have only one of the following scene properties:
  80. /// GameMesh, GamePointSprite, GameSprite3D, GameBillboard.
  81. /// </summary>
  82. public class Particle : GameSceneNode
  83. {
  84. #region Fields
  85. static Vector3 globalGravityDirection = new Vector3(0.0f, -1.0f, 0.0f);
  86. static float globalGravityFactor = 98.0f;
  87. ParticleInfo sequenceInfo = null;
  88. TextureSequence textureSequence = null;
  89. string resourcePath = String.Empty;
  90. GameSceneNode sceneRoot = null;
  91. GameMesh sceneMesh = null;
  92. GamePointSprite scenePointSprite = null;
  93. GameSprite3D sceneSprite3D = null;
  94. GameBillboard sceneBillboard = null;
  95. bool isStarting = false;
  96. bool isStopping = false;
  97. bool changeTime = false;
  98. int posArgCount = 0;
  99. int scaleArgCount = 0;
  100. float localTime = 0.0f;
  101. Vector3 emitRight = Vector3.Zero;
  102. Vector3 emitUp = Vector3.Zero;
  103. Vector3 upRight = Vector3.Zero;
  104. Vector3 upAt = Vector3.Zero;
  105. float emitInterval = 0.0f;
  106. float posRinterval = 0.0f;
  107. float scaleRinterval = 0.0f;
  108. List<ParticleObject> particleObjectList = new List<ParticleObject>();
  109. Texture2D texture2D = null;
  110. KeyFrameTable.Interpolation positionInterpolate =
  111. KeyFrameTable.Interpolation.None;
  112. KeyFrameTable.Interpolation scaleInterpolate =
  113. KeyFrameTable.Interpolation.None;
  114. KeyFrameTable.Interpolation colorInterpolate =
  115. KeyFrameTable.Interpolation.None;
  116. bool enableRotate = false;
  117. float rObjectLifeTime = 0.0f;
  118. Matrix? refMatrix = null;
  119. bool refEnable = false;
  120. #endregion
  121. #region Properties
  122. public ParticleInfo Info
  123. {
  124. get { return sequenceInfo; }
  125. set { sequenceInfo = value; }
  126. }
  127. public bool IsPlaying
  128. {
  129. get { return isStarting; }
  130. }
  131. public TextureSequence TextureSequence
  132. {
  133. get { return textureSequence; }
  134. set { textureSequence = value; }
  135. }
  136. public Texture2D Texture
  137. {
  138. get { return texture2D; }
  139. set
  140. {
  141. texture2D = value;
  142. }
  143. }
  144. public GameSceneNode SceneMesh
  145. {
  146. get { return sceneMesh; }
  147. }
  148. #endregion
  149. /// <summary>
  150. /// Constructor.
  151. /// </summary>
  152. /// <param name="resourcePath">texture resource path</param>
  153. /// <param name="info">particle information structure</param>
  154. /// <param name="textureSequence">texture sequence class</param>
  155. public Particle(string resourcePath, ParticleInfo info,
  156. TextureSequence textureSequence) : base()
  157. {
  158. if (String.IsNullOrEmpty(resourcePath))
  159. throw new ArgumentNullException("resourcePath");
  160. if (info == null)
  161. throw new ArgumentNullException("info");
  162. this.sequenceInfo = info;
  163. this.textureSequence = textureSequence;
  164. this.resourcePath = resourcePath;
  165. // Create particle's scene root
  166. this.sceneRoot = new GameSceneNode();
  167. this.sceneRoot.Name = "Particle Scene Root";
  168. AddChild(this.sceneRoot);
  169. Create();
  170. }
  171. public static void SetGravityDirection(Vector3 vec)
  172. {
  173. globalGravityDirection = vec;
  174. }
  175. public static void SetGravityFactor(float val)
  176. {
  177. globalGravityFactor = val;
  178. }
  179. public void SetRefMatrixEnable(bool enabled)
  180. {
  181. refEnable = enabled;
  182. }
  183. public void SetRefMatrix(Matrix? matrix)
  184. {
  185. refMatrix = matrix;
  186. }
  187. /// <summary>
  188. /// enables/disables a particle’s scene object.
  189. /// </summary>
  190. /// <param name="enable"></param>
  191. public void SetSceneEnable(bool enable)
  192. {
  193. sceneRoot.Enabled = enable;
  194. sceneRoot.Visible = enable;
  195. }
  196. /// <summary>
  197. /// by using the particle information, a particle object is created
  198. /// and initialized.
  199. /// </summary>
  200. public void Create()
  201. {
  202. // Initialize Particle Information
  203. Info.Initialize();
  204. Name = sequenceInfo.Name;
  205. sceneRoot.RemoveAllChild(false);
  206. if (sequenceInfo.TextureFileName.Length > 0)
  207. {
  208. string resourceFullPath =
  209. Path.Combine(resourcePath, sequenceInfo.TextureFileName);
  210. GameResourceTexture2D resource =
  211. FrameworkCore.ResourceManager.LoadTexture(resourceFullPath);
  212. // Set to texture
  213. Texture = resource.Texture2D;
  214. // Set to texture in TextureSequence
  215. if (TextureSequence != null)
  216. TextureSequence.SetTexture(Texture);
  217. }
  218. enableRotate = false;
  219. switch (Info.ParticleType)
  220. {
  221. // PointSprite
  222. case ParticleInfo.ParticleObjectType.PointSprite:
  223. {
  224. scenePointSprite = new GamePointSprite();
  225. scenePointSprite.Create(Info.MaxObjectCount, Texture,
  226. RenderingSpace.World, false);
  227. scenePointSprite.AlphaBlendEnable =
  228. sequenceInfo.AlphaBlendEnable;
  229. scenePointSprite.DepthBufferEnable =
  230. sequenceInfo.DepthBufferEnable;
  231. scenePointSprite.SourceBlend =
  232. sequenceInfo.SourceBlend;
  233. scenePointSprite.DestinationBlend =
  234. sequenceInfo.DestinationBlend;
  235. scenePointSprite.BlendFunction =
  236. sequenceInfo.BlendFunction;
  237. scenePointSprite.DepthBufferEnable = true;
  238. scenePointSprite.DepthBufferWriteEnable = false;
  239. scenePointSprite.DepthBufferFunction = CompareFunction.Less;
  240. scenePointSprite.AlphaFunction = CompareFunction.Greater;
  241. scenePointSprite.CullMode = CullMode.None;
  242. sceneRoot.AddChild(scenePointSprite);
  243. }
  244. break;
  245. // Sprite3D
  246. case ParticleInfo.ParticleObjectType.Sprite:
  247. {
  248. sceneSprite3D = new GameSprite3D();
  249. sceneSprite3D.Create(Info.MaxObjectCount, Texture,
  250. RenderingSpace.World, false);
  251. sceneSprite3D.AlphaBlendEnable =
  252. sequenceInfo.AlphaBlendEnable;
  253. sceneSprite3D.DepthBufferEnable =
  254. sequenceInfo.DepthBufferEnable;
  255. sceneSprite3D.SourceBlend =
  256. sequenceInfo.SourceBlend;
  257. sceneSprite3D.DestinationBlend =
  258. sequenceInfo.DestinationBlend;
  259. sceneSprite3D.BlendFunction =
  260. sequenceInfo.BlendFunction;
  261. sceneSprite3D.DepthBufferEnable = true;
  262. sceneSprite3D.DepthBufferWriteEnable = false;
  263. sceneSprite3D.DepthBufferFunction = CompareFunction.Less;
  264. sceneSprite3D.AlphaFunction = CompareFunction.Greater;
  265. sceneSprite3D.CullMode = CullMode.None;
  266. sceneRoot.AddChild(sceneSprite3D);
  267. enableRotate = true; // Possible to rotation
  268. }
  269. break;
  270. // Billboard
  271. case ParticleInfo.ParticleObjectType.AnchorBillboard:
  272. case ParticleInfo.ParticleObjectType.Billboard:
  273. {
  274. sceneBillboard = new GameBillboard();
  275. sceneBillboard.Create(Info.MaxObjectCount, Texture,
  276. RenderingSpace.World, false);
  277. sceneBillboard.AlphaBlendEnable =
  278. sequenceInfo.AlphaBlendEnable;
  279. sceneBillboard.DepthBufferEnable =
  280. sequenceInfo.DepthBufferEnable;
  281. sceneBillboard.SourceBlend =
  282. sequenceInfo.SourceBlend;
  283. sceneBillboard.DestinationBlend =
  284. sequenceInfo.DestinationBlend;
  285. sceneBillboard.BlendFunction =
  286. sequenceInfo.BlendFunction;
  287. sceneBillboard.DepthBufferEnable = true;
  288. sceneBillboard.DepthBufferWriteEnable = false;
  289. sceneBillboard.DepthBufferFunction = CompareFunction.Less;
  290. sceneBillboard.AlphaFunction = CompareFunction.Greater;
  291. sceneBillboard.CullMode = CullMode.None;
  292. sceneRoot.AddChild(sceneBillboard);
  293. }
  294. break;
  295. // Mesh
  296. case ParticleInfo.ParticleObjectType.Scene:
  297. {
  298. if( Info.MeshData != null)
  299. {
  300. int vertextCount = Info.MeshData.Position.Count;
  301. int indexCount = 0;
  302. if (Info.MeshData.Index != null)
  303. indexCount = Info.MeshData.Index.Count;
  304. sceneMesh = new GameMesh();
  305. sceneMesh.Create(vertextCount, indexCount, Texture);
  306. sceneMesh.AlphaBlendEnable =
  307. sequenceInfo.AlphaBlendEnable;
  308. sceneMesh.DepthBufferEnable =
  309. sequenceInfo.DepthBufferEnable;
  310. sceneMesh.SourceBlend =
  311. sequenceInfo.SourceBlend;
  312. sceneMesh.DestinationBlend =
  313. sequenceInfo.DestinationBlend;
  314. sceneMesh.BlendFunction =
  315. sequenceInfo.BlendFunction;
  316. sceneMesh.DepthBufferEnable = true;
  317. sceneMesh.DepthBufferWriteEnable = false;
  318. sceneMesh.DepthBufferFunction = CompareFunction.Less;
  319. sceneMesh.AlphaFunction = CompareFunction.Greater;
  320. sceneMesh.CullMode = CullMode.None;
  321. // Has position data
  322. if (Info.MeshData.HasPosition )
  323. {
  324. sceneMesh.SetPositionData(
  325. Info.MeshData.Position.ToArray());
  326. }
  327. // Has color data
  328. if (Info.MeshData.HasColor )
  329. {
  330. sceneMesh.SetColorData(
  331. Info.MeshData.Color.ToArray());
  332. }
  333. // Has texture coordinate data
  334. if (Info.MeshData.HasTextureCoord )
  335. {
  336. sceneMesh.SetTextureCoordData(
  337. Info.MeshData.TextureCoord.ToArray());
  338. }
  339. // Has index data
  340. if (Info.MeshData.HasIndex )
  341. {
  342. sceneMesh.SetIndexData(Info.MeshData.Index.ToArray());
  343. }
  344. // Use VB and IB
  345. if (sceneMesh.userPrimitive == false)
  346. {
  347. sceneMesh.BindVertexBuffer();
  348. sceneMesh.BindIndexBuffer();
  349. }
  350. sceneRoot.AddChild(sceneMesh);
  351. }
  352. enableRotate = true; // can rotate
  353. }
  354. break;
  355. }
  356. SetSceneEnable(false);
  357. // Particle Object
  358. particleObjectList.Clear();
  359. for (int i = 0; i < Info.MaxObjectCount; i++)
  360. {
  361. ParticleObject obj = new ParticleObject();
  362. particleObjectList.Add(obj);
  363. }
  364. // Emit Right & Up Vector
  365. {
  366. Matrix mtx = Helper3D.MakeMatrixWithAt(Info.EmitDirection,
  367. Info.UpVector);
  368. emitRight = mtx.Right;
  369. emitUp = mtx.Up;
  370. }
  371. // Up Right & At Vector
  372. {
  373. Matrix mtx = Helper3D.MakeMatrixWithUp(Info.UpVector,
  374. Matrix.Identity.Forward);
  375. upRight = mtx.Right;
  376. upAt = mtx.Forward;
  377. }
  378. posArgCount = 0;
  379. for (int i = ParticleInfo.FuncCount - 1; i >= 0; i--)
  380. {
  381. if (Info.PositionFunc[i] != ParticleInfo.FuncType.None)
  382. {
  383. posArgCount = i + 1;
  384. break;
  385. }
  386. }
  387. scaleArgCount = 0;
  388. for (int i = ParticleInfo.FuncCount - 1; i >= 0; i--)
  389. {
  390. if (Info.ScaleFunc[i] != ParticleInfo.FuncType.None)
  391. {
  392. scaleArgCount = i + 1;
  393. break;
  394. }
  395. }
  396. if( Info.IsPositionStyle( ParticleInfo.ParamStyles.Interpolate))
  397. positionInterpolate = KeyFrameTable.Interpolation.Lerp;
  398. else
  399. positionInterpolate = KeyFrameTable.Interpolation.None;
  400. if (Info.IsScaleStyle(ParticleInfo.ParamStyles.Interpolate))
  401. scaleInterpolate = KeyFrameTable.Interpolation.Lerp;
  402. else
  403. scaleInterpolate = KeyFrameTable.Interpolation.None;
  404. if (Info.IsColorStyle(ParticleInfo.ParamStyles.Interpolate))
  405. colorInterpolate = KeyFrameTable.Interpolation.Lerp;
  406. else
  407. colorInterpolate = KeyFrameTable.Interpolation.None;
  408. rObjectLifeTime = 1.0f / Info.ObjectLifeTime;
  409. isStarting = false;
  410. isStopping = false;
  411. }
  412. /// <summary>
  413. /// starts and enables the particle objects.
  414. /// </summary>
  415. public void Start()
  416. {
  417. SetSceneEnable(true);
  418. for (int i = 0; i < Info.MaxObjectCount; i++)
  419. particleObjectList[i].state = 0;
  420. for (int i = 0; i < Info.InitialObjectCount; i++)
  421. {
  422. ParticleObject obj = particleObjectList[i];
  423. EmitParticle(ref obj);
  424. }
  425. emitInterval = Info.EmitInterval;
  426. if (Info.PositionUpdateInterval > 0.0f)
  427. posRinterval = 1.0f / Info.PositionUpdateInterval;
  428. else
  429. posRinterval = 0.0f;
  430. if (Info.ScaleUpdateInterval > 0.0f)
  431. scaleRinterval = 1.0f / Info.ScaleUpdateInterval;
  432. else
  433. scaleRinterval = 0.0f;
  434. isStarting = true;
  435. isStopping = false;
  436. localTime = 0.0f;
  437. }
  438. /// <summary>
  439. /// disables the particle objects.
  440. /// </summary>
  441. public void Stop()
  442. {
  443. isStopping = true;
  444. localTime = 0.0f;
  445. }
  446. /// <summary>
  447. /// emits the specified particle object according to the emit type.
  448. /// </summary>
  449. /// <param name="obj">a particle object</param>
  450. protected void EmitParticle(ref ParticleObject obj)
  451. {
  452. float massRange = 0.0f;
  453. Matrix mtx1 = Matrix.Identity;
  454. float rnd1 = 0.0f, rnd2 = 0.0f;
  455. obj.state = (uint)(ParticleObject.States.Enable |
  456. ParticleObject.States.Fired |
  457. ParticleObject.States.FirstTime);
  458. obj.time = 0.0f;
  459. obj.posInterval = Info.PositionUpdateInterval;
  460. obj.scaleInterval = Info.ScaleUpdateInterval;
  461. obj.textureSequenceInterval = 0.0f;
  462. massRange = Info.MassMax - Info.MassMin;
  463. obj.mass = Info.MassMin + (HelperMath.RandomNormal() * massRange);
  464. obj.gravity = Vector3.Zero;
  465. obj.posRandomInterval = Info.PositionRandomInterval;
  466. switch (Info.EmitType)
  467. {
  468. // CONE type : changes tje direction randomly
  469. // from the default EmitDirection.
  470. case ParticleInfo.EmitObjectType.Cone:
  471. {
  472. rnd1 = HelperMath.RandomNormal2() * Info.EmitAngle;
  473. rnd2 = HelperMath.RandomNormal2() * Info.EmitAngle;
  474. mtx1 = Matrix.CreateFromAxisAngle(emitRight,
  475. MathHelper.ToRadians(rnd1));
  476. mtx1 = mtx1 * Matrix.CreateFromAxisAngle(emitUp,
  477. MathHelper.ToRadians(rnd2));
  478. obj.emitDir = Vector3.TransformNormal(Info.EmitDirection, mtx1);
  479. obj.startPos = Info.EmitPosition;
  480. }
  481. break;
  482. // SPHERE type : make the emit direction to the total random direction.
  483. case ParticleInfo.EmitObjectType.Sphere:
  484. {
  485. obj.emitDir = HelperMath.RandomVector2();
  486. obj.emitDir.Normalize();
  487. obj.startPos = Info.EmitPosition;
  488. }
  489. break;
  490. // Disk type: basically same as the cone,
  491. // (however, except the rotation around the EmitUpVector as the axis)
  492. // it makes the direction which is randomly turned
  493. // as the UpVector as the axis.
  494. case ParticleInfo.EmitObjectType.Disk:
  495. {
  496. rnd1 = HelperMath.RandomNormal2() * Info.EmitAngle;
  497. mtx1 = Matrix.CreateFromAxisAngle(emitRight,
  498. MathHelper.ToRadians(rnd1));
  499. mtx1 = mtx1 * Matrix.CreateFromAxisAngle(Info.UpVector,
  500. HelperMath.RandomNormal() *
  501. 2.0f *
  502. MathHelper.Pi);
  503. obj.emitDir = Vector3.TransformNormal(Info.EmitDirection, mtx1);
  504. obj.startPos = Info.EmitPosition;
  505. }
  506. break;
  507. // RECT_PLANE type : The EmitAngle will follow the size
  508. // of a square area, not an angle.
  509. // The EmitDirection is always constant.
  510. case ParticleInfo.EmitObjectType.RectPlane:
  511. {
  512. obj.emitDir = Info.EmitDirection;
  513. rnd1 = (HelperMath.RandomNormal2() * 0.5f) * Info.EmitAngle;
  514. rnd2 = (HelperMath.RandomNormal2() * 0.5f) * Info.EmitAngle;
  515. obj.pos[0] = upRight * rnd1;
  516. obj.pos[0] += upAt * rnd2;
  517. obj.startPos = obj.pos[0] + Info.EmitPosition;
  518. }
  519. break;
  520. // CIRCLE_PLANE type : The circular area whose radius is the EmitAngle.
  521. case ParticleInfo.EmitObjectType.CirclePlane:
  522. {
  523. obj.emitDir = Info.EmitDirection;
  524. rnd1 = HelperMath.RandomNormal() * Info.EmitAngle;
  525. rnd2 = HelperMath.RandomNormal() * MathHelper.Pi * 2.0f;
  526. obj.pos[0] = upRight * rnd1; // Scaling
  527. mtx1 = Matrix.CreateFromAxisAngle(Info.UpVector, rnd2);
  528. obj.pos[0] = Helper3D.TransformCoord(obj.pos[0], mtx1);
  529. obj.startPos = obj.pos[0] + Info.EmitPosition;
  530. }
  531. break;
  532. }
  533. if (refEnable)
  534. {
  535. obj.startPos = Helper3D.TransformCoord(obj.startPos, (Matrix)refMatrix);
  536. obj.emitDir = Vector3.TransformNormal(obj.emitDir, (Matrix)refMatrix);
  537. }
  538. obj.pos[0] = obj.startPos;
  539. obj.scale = Vector3.Zero;
  540. obj.posRandom = 1.0f + ((HelperMath.RandomNormal2() * 0.5f) *
  541. Info.PositionInitialRandomFactor);
  542. obj.scaleRandom = 1.0f + ((HelperMath.RandomNormal2() * 0.5f) *
  543. Info.ScaleInitialRandomFactor);
  544. obj.rotateRandom = HelperMath.RandomNormal2();
  545. switch (Info.ParticleType)
  546. {
  547. case ParticleInfo.ParticleObjectType.AnchorBillboard:
  548. {
  549. obj.pos[1] = obj.pos[0];
  550. }
  551. break;
  552. case ParticleInfo.ParticleObjectType.Scene:
  553. {
  554. obj.rotateDir = HelperMath.RandomVector2();
  555. }
  556. break;
  557. }
  558. {
  559. for (int i = 0; i < posArgCount; i++)
  560. obj.posArg[i] = Info.PositionInit[i];
  561. for (int i = 0; i < scaleArgCount; i++)
  562. obj.scaleArg[i] = Info.ScaleInit[i];
  563. obj.rotateAccm = Info.RotateInit + (HelperMath.RandomNormal2() *
  564. Info.RotateRandomFactor);
  565. }
  566. }
  567. /// <summary>
  568. /// It calculates the local time and controls the particle’s
  569. /// start and stop functions.
  570. /// </summary>
  571. /// <param name="gameTime">game time</param>
  572. protected override void OnUpdate(GameTime gameTime)
  573. {
  574. if (localTime == 0.0f && isStarting == false)
  575. {
  576. // Starting particle
  577. Start();
  578. }
  579. // If life time is 0, it's infinity time
  580. if (Info.LifeTime > 0.0f)
  581. {
  582. // time goes
  583. localTime += (float)gameTime.ElapsedGameTime.TotalSeconds;
  584. changeTime = true;
  585. if (localTime > Info.LifeTime)
  586. {
  587. // Stopping particle.
  588. // It will be not finish this particle until stop \
  589. // all active particle object
  590. Stop();
  591. }
  592. }
  593. else
  594. {
  595. // forever active that particle's LifeTime is zero
  596. changeTime = true;
  597. }
  598. // Update particle transform
  599. {
  600. if (scenePointSprite != null)
  601. {
  602. scenePointSprite.RootAxis = this.RootAxis;
  603. scenePointSprite.WorldTransform = this.WorldTransform;
  604. }
  605. if (sceneSprite3D != null)
  606. {
  607. sceneSprite3D.RootAxis = this.RootAxis;
  608. sceneSprite3D.WorldTransform = this.WorldTransform;
  609. }
  610. if (sceneBillboard != null)
  611. {
  612. sceneBillboard.RootAxis = this.RootAxis;
  613. sceneBillboard.WorldTransform = this.WorldTransform;
  614. }
  615. if (sceneMesh != null)
  616. {
  617. sceneMesh.RootAxis = this.RootAxis;
  618. sceneMesh.WorldTransform = this.WorldTransform;
  619. }
  620. }
  621. }
  622. /// <summary>
  623. /// It calls Drive() function by using the data of the ParticleInfo to update
  624. /// the information of each ParticleObject’s position, rotation, and scale.
  625. /// </summary>
  626. /// <param name="renderTracer">render information in the RenderContext</param>
  627. protected override void OnDraw(RenderTracer renderTracer)
  628. {
  629. // Update particle info
  630. Drive(renderTracer);
  631. }
  632. /// <summary>
  633. /// When the information of each ParticleObject gets updated,
  634. /// the vertex values of the GameMesh, GamePointSprite, GameSprite3D, and
  635. /// GameBillboard classes are updated.
  636. /// </summary>
  637. /// <param name="renderTracer">render information in the RenderContext</param>
  638. protected void Drive(RenderTracer renderTracer)
  639. {
  640. int emitCount = 0, aliveCount = 0;
  641. float time = 0, s = 0.0f;
  642. float lifeTime = 0.0f;
  643. Vector3 dir = Vector3.Zero;;
  644. bool isEnable = false;
  645. bool isEmit = false;
  646. bool isInterpolate = false;
  647. Matrix mtx = Matrix.Identity;
  648. Vector3 vGravDir = Vector3.Zero;
  649. bool isUv = false;
  650. Vector2 uv1 = Vector2.Zero;
  651. Vector2 uv2 = Vector2.Zero;
  652. // If the changeTime is false, no update
  653. if (changeTime == false) return;
  654. else if (changeTime ) changeTime = false;
  655. time = (float)renderTracer.GameTime.ElapsedGameTime.TotalSeconds;
  656. emitInterval -= time;
  657. if (isStopping == false && emitInterval <= 0.0f)
  658. {
  659. // New particle object emit
  660. isEmit = true;
  661. emitInterval = Info.EmitInterval;
  662. }
  663. mtx = this.TransformedMatrix * renderTracer.View;
  664. mtx = Helper3D.Transpose(mtx);
  665. // Calculate gravity direction
  666. vGravDir = Vector3.TransformNormal(globalGravityDirection, mtx );
  667. for (int i = 0; i < Info.MaxObjectCount; i++)
  668. {
  669. ParticleObject obj = particleObjectList[i];
  670. isEnable = true;
  671. isUv = false;
  672. lifeTime = 0.0f;
  673. // If the object state is deactivate
  674. if (!obj.IsState(ParticleObject.States.Enable))
  675. {
  676. if ((isEmit ) && (emitCount < Info.EmitCount))
  677. {
  678. if (!((Info.Volatile ) &&
  679. obj.IsState(ParticleObject.States.Fired)))
  680. {
  681. emitCount++;
  682. // Emit a new particle object
  683. EmitParticle(ref obj);
  684. }
  685. else
  686. {
  687. isEnable = false;
  688. }
  689. }
  690. else
  691. {
  692. isEnable = false;
  693. }
  694. }
  695. else
  696. {
  697. // If the object state is activate
  698. obj.time += time;
  699. lifeTime = obj.time * rObjectLifeTime;
  700. if (lifeTime >= 1.0f)
  701. {
  702. // The object is finished
  703. if ((isEmit ) && (Info.Volatile == false))
  704. {
  705. // Finished particle object is restarting!
  706. emitCount++;
  707. // Emit a new particle object
  708. EmitParticle(ref obj);
  709. lifeTime = 0.0f;
  710. }
  711. // Has to deactivate
  712. else
  713. {
  714. obj.state &= ~((uint)ParticleObject.States.Enable);
  715. isEnable = false;
  716. }
  717. }
  718. }
  719. if (isEnable == false)
  720. {
  721. switch (Info.ParticleType)
  722. {
  723. case ParticleInfo.ParticleObjectType.PointSprite:
  724. {
  725. scenePointSprite.SetObjectEnable(i, false);
  726. }
  727. break;
  728. case ParticleInfo.ParticleObjectType.Sprite:
  729. {
  730. sceneSprite3D.SetObjectEnable(i, false);
  731. }
  732. break;
  733. case ParticleInfo.ParticleObjectType.AnchorBillboard:
  734. case ParticleInfo.ParticleObjectType.Billboard:
  735. {
  736. sceneBillboard.SetUpdateType(i, false);
  737. }
  738. break;
  739. case ParticleInfo.ParticleObjectType.Scene:
  740. {
  741. sceneMesh.Enabled = false;
  742. sceneMesh.Visible = false;
  743. }
  744. break;
  745. }
  746. }
  747. else
  748. {
  749. float tempInterval = 0.0f;
  750. isInterpolate = true;
  751. aliveCount++;
  752. //////////////////////////////////////////////////////////
  753. // Position
  754. obj.posInterval += time;
  755. tempInterval = obj.posInterval;
  756. if (tempInterval >= Info.PositionUpdateInterval)
  757. {
  758. int last = posArgCount - 1;
  759. if (Info.PositionUpdateInterval < time)
  760. {
  761. tempInterval = obj.posInterval = time;
  762. isInterpolate = false;
  763. }
  764. switch (Info.PositionFunc[last])
  765. {
  766. case ParticleInfo.FuncType.Constant:
  767. {
  768. obj.posArg[last] = Info.PositionInit[last];
  769. }
  770. break;
  771. case ParticleInfo.FuncType.Sin:
  772. {
  773. obj.posArg[last] =
  774. (float)Math.Sin(MathHelper.PiOver2 * lifeTime) *
  775. Info.PositionFactor[last];
  776. }
  777. break;
  778. case ParticleInfo.FuncType.Cos:
  779. {
  780. obj.posArg[last] =
  781. (float)Math.Cos(MathHelper.PiOver2 * lifeTime) *
  782. Info.PositionFactor[last];
  783. }
  784. break;
  785. case ParticleInfo.FuncType.Rnd:
  786. {
  787. obj.posArg[last] = HelperMath.RandomNormal2();
  788. }
  789. break;
  790. case ParticleInfo.FuncType.Table:
  791. {
  792. KeyFrameTable table = Info.PositionTable[last];
  793. obj.posArg[last] = table.GetKeyFrame(lifeTime,
  794. positionInterpolate);
  795. }
  796. break;
  797. }
  798. obj.posArg[last] *= obj.posRandom;
  799. if (last == 1)
  800. {
  801. // Accelation...v = v0 + at
  802. obj.posArg[0] += obj.posArg[1] * tempInterval;
  803. }
  804. if (Info.IsPositionStyle(ParticleInfo.ParamStyles.Clamp))
  805. {
  806. obj.posArg[0] = MathHelper.Clamp(obj.posArg[0],
  807. Info.PositionMin,
  808. Info.PositionMax);
  809. }
  810. // Move distance...s = vt
  811. s = obj.posArg[0] * tempInterval;
  812. // Calculate move amount
  813. dir = obj.emitDir * s;
  814. // Applies acceleration of gravity
  815. if (Info.IsPositionStyle(ParticleInfo.ParamStyles.Gravity))
  816. {
  817. float _fact = globalGravityFactor * (obj.mass *
  818. (tempInterval * tempInterval));
  819. obj.gravity += vGravDir * _fact;
  820. dir += obj.gravity;
  821. }
  822. obj.targetPos = obj.pos[0] + dir;
  823. obj.startPos = obj.pos[0];
  824. obj.posInterval -= Info.PositionUpdateInterval;
  825. }
  826. // If random type
  827. if (Info.IsPositionStyle(ParticleInfo.ParamStyles.Random))
  828. {
  829. obj.posRandomInterval -= time;
  830. if (obj.posRandomInterval <= 0.0f)
  831. {
  832. Vector3 rndVec = HelperMath.RandomVector2();
  833. rndVec *= Info.PositionRandomFactor;
  834. obj.targetPos += rndVec;
  835. obj.posRandomInterval = Info.PositionRandomInterval;
  836. }
  837. }
  838. // Trace to position
  839. if (Info.ParticleType == ParticleInfo.ParticleObjectType.Billboard)
  840. {
  841. obj.pos[1] = obj.pos[0];
  842. }
  843. // Position interpolate
  844. if (isInterpolate)
  845. {
  846. obj.pos[0] = Vector3.Lerp(obj.startPos, obj.targetPos,
  847. (obj.posInterval * posRinterval));
  848. }
  849. else
  850. {
  851. obj.pos[0] = obj.targetPos;
  852. }
  853. //////////////////////////////////////////////////////////
  854. // Scale
  855. isInterpolate = true;
  856. obj.scaleInterval += time;
  857. tempInterval = obj.scaleInterval;
  858. if (tempInterval >= Info.ScaleUpdateInterval)
  859. {
  860. int last = scaleArgCount - 1;
  861. if (Info.ScaleUpdateInterval < time)
  862. {
  863. tempInterval = obj.scaleInterval = time;
  864. isInterpolate = false;
  865. }
  866. switch (Info.ScaleFunc[last])
  867. {
  868. case ParticleInfo.FuncType.Constant:
  869. {
  870. obj.scaleArg[last] = Info.ScaleInit[last];
  871. }
  872. break;
  873. case ParticleInfo.FuncType.Sin:
  874. {
  875. obj.scaleArg[last] =
  876. (float)Math.Sin(MathHelper.PiOver2 * lifeTime) *
  877. Info.ScaleFactor[last];
  878. }
  879. break;
  880. case ParticleInfo.FuncType.Cos:
  881. {
  882. obj.scaleArg[last] =
  883. (float)Math.Cos(MathHelper.PiOver2 * lifeTime) *
  884. Info.ScaleFactor[last];
  885. }
  886. break;
  887. case ParticleInfo.FuncType.Rnd:
  888. {
  889. obj.scaleArg[last] = HelperMath.RandomNormal2();
  890. }
  891. break;
  892. case ParticleInfo.FuncType.Table:
  893. {
  894. KeyFrameTable table = Info.ScaleTable[last];
  895. obj.scaleArg[last] = table.GetKeyFrame(lifeTime,
  896. scaleInterpolate);
  897. }
  898. break;
  899. }
  900. // Applies random value
  901. obj.scaleArg[last] *= obj.scaleRandom;
  902. if( last == 1)
  903. obj.scaleArg[0] += obj.scaleArg[1] * tempInterval;
  904. if (Info.IsScaleStyle(ParticleInfo.ParamStyles.Clamp))
  905. {
  906. obj.scaleArg[0] = MathHelper.Clamp(obj.scaleArg[0],
  907. Info.ScaleMin, Info.ScaleMax);
  908. }
  909. obj.targetScale = Info.ScaleMask * obj.scaleArg[0];
  910. // back up the previous scale value
  911. obj.startScale = obj.scale;
  912. obj.scaleInterval -= Info.ScaleUpdateInterval;
  913. }
  914. if (isInterpolate)
  915. {
  916. obj.scale = Vector3.Lerp(obj.startScale, obj.targetScale,
  917. (obj.scaleInterval * scaleRinterval));
  918. }
  919. else
  920. {
  921. obj.scale = obj.targetScale;
  922. }
  923. //////////////////////////////////////////////////////////
  924. // Rotation
  925. if( enableRotate)
  926. {
  927. switch(Info.RotateFunc)
  928. {
  929. case ParticleInfo.FuncType.None:
  930. {
  931. obj.rotateArg = 0.0f;
  932. }
  933. break;
  934. case ParticleInfo.FuncType.Constant:
  935. {
  936. obj.rotateArg = Info.RotateInit * time;
  937. }
  938. break;
  939. case ParticleInfo.FuncType.Sin:
  940. {
  941. obj.rotateArg =
  942. (float)Math.Sin(MathHelper.PiOver2 * lifeTime) *
  943. Info.RotateFactor;
  944. }
  945. break;
  946. case ParticleInfo.FuncType.Cos:
  947. {
  948. obj.rotateArg =
  949. (float)Math.Sin(MathHelper.PiOver2 * lifeTime) *
  950. Info.RotateFactor;
  951. }
  952. break;
  953. case ParticleInfo.FuncType.Rnd:
  954. {
  955. obj.rotateArg = HelperMath.RandomNormal2();
  956. }
  957. break;
  958. case ParticleInfo.FuncType.Table:
  959. {
  960. KeyFrameTable table = Info.RotateTable;
  961. obj.rotateArg = table.GetKeyFrame(lifeTime,
  962. KeyFrameTable.Interpolation.Lerp);
  963. }
  964. break;
  965. }
  966. obj.rotateAccm += obj.rotateArg * obj.rotateRandom;
  967. }
  968. //////////////////////////////////////////////////////////
  969. // Color
  970. {
  971. byte r = obj.color.R;
  972. byte g = obj.color.G;
  973. byte b = obj.color.B;
  974. byte a = obj.color.A;
  975. if (!Info.IsColorStyle(ParticleInfo.ParamStyles.Random) ||
  976. obj.IsState(ParticleObject.States.FirstTime))
  977. {
  978. switch (Info.RgbFunc)
  979. {
  980. case ParticleInfo.FuncType.Constant:
  981. {
  982. r = Info.RgbInitValue.R;
  983. g = Info.RgbInitValue.G;
  984. b = Info.RgbInitValue.B;
  985. }
  986. break;
  987. case ParticleInfo.FuncType.Table:
  988. {
  989. float rt = 0.0f;
  990. if (Info.IsColorStyle(
  991. ParticleInfo.ParamStyles.Random))
  992. {
  993. rt = HelperMath.RandomNormal();
  994. }
  995. else
  996. {
  997. rt = lifeTime;
  998. }
  999. r = (byte)Info.Rtable.GetKeyFrame(rt,
  1000. colorInterpolate);
  1001. g = (byte)Info.Gtable.GetKeyFrame(rt,
  1002. colorInterpolate);
  1003. b = (byte)Info.Btable.GetKeyFrame(rt,
  1004. colorInterpolate);
  1005. }
  1006. break;
  1007. default:
  1008. {
  1009. r = 255;
  1010. g = 255;
  1011. b = 255;
  1012. }
  1013. break;
  1014. }
  1015. }
  1016. float alpha = 255f;
  1017. switch (Info.AlphaFunc)
  1018. {
  1019. case ParticleInfo.FuncType.Constant:
  1020. alpha = (float)Info.AlphaInit;
  1021. break;
  1022. case ParticleInfo.FuncType.Sin:
  1023. alpha = 255f * (float)Math.Sin(
  1024. MathHelper.PiOver2 * lifeTime);
  1025. break;
  1026. case ParticleInfo.FuncType.Cos:
  1027. alpha = 255f * (float)Math.Cos(
  1028. MathHelper.PiOver2 * lifeTime);
  1029. break;
  1030. case ParticleInfo.FuncType.Table:
  1031. alpha = Info.Atable.GetKeyFrame(lifeTime,
  1032. colorInterpolate);
  1033. break;
  1034. default:
  1035. alpha = 255f;
  1036. break;
  1037. }
  1038. a = (byte)alpha;
  1039. obj.color = new Color(r, g, b, a);
  1040. }
  1041. //////////////////////////////////////////////////////////
  1042. // TextureSequence
  1043. if (textureSequence != null)
  1044. {
  1045. obj.textureSequenceInterval -= time;
  1046. // Calculate texture sequence
  1047. if( (obj.textureSequenceInterval <= 0.0f) &&
  1048. ((textureSequence.IsFixedFrameMode == false) ||
  1049. (obj.IsState( ParticleObject.States.FirstTime))))
  1050. {
  1051. obj.textureSequenceInterval = textureSequence.StaticInterval;
  1052. textureSequence.GetUV(obj.time, out uv1, out uv2);
  1053. isUv = true;
  1054. }
  1055. }
  1056. switch (Info.ParticleType)
  1057. {
  1058. case ParticleInfo.ParticleObjectType.PointSprite:
  1059. {
  1060. scenePointSprite.SetObjectEnable(i, true);
  1061. scenePointSprite.SetCenter(i, obj.pos[0]);
  1062. scenePointSprite.SetSize(i, obj.scale.X, obj.scale.Y);
  1063. scenePointSprite.SetColor(i, obj.color);
  1064. if (isUv)
  1065. scenePointSprite.SetTextureCoord(i, uv1, uv2);
  1066. }
  1067. break;
  1068. case ParticleInfo.ParticleObjectType.Sprite:
  1069. {
  1070. sceneSprite3D.SetObjectEnable(i, true);
  1071. sceneSprite3D.SetCenter(i, obj.pos[0]);
  1072. sceneSprite3D.SetSize(i, obj.scale.X, obj.scale.Y);
  1073. sceneSprite3D.SetColor(i, obj.color);
  1074. sceneSprite3D.SetRotation(i, obj.rotateAccm);
  1075. if (isUv)
  1076. sceneSprite3D.SetTextureCoord(i, uv1, uv2);
  1077. }
  1078. break;
  1079. case ParticleInfo.ParticleObjectType.Billboard:
  1080. {
  1081. Vector3 diff = Vector3.Zero;
  1082. diff = obj.pos[0] - obj.pos[1];
  1083. diff *= Info.ScaleBillboardFactor;
  1084. diff += obj.pos[0];
  1085. sceneBillboard.SetUpdateType(i, true);
  1086. sceneBillboard.SetStart(i, obj.pos[0]);
  1087. sceneBillboard.SetEnd(i, diff);
  1088. sceneBillboard.SetSize(i, obj.scale.Y);
  1089. sceneBillboard.SetColor(i, obj.color);
  1090. if(isUv)
  1091. sceneBillboard.SetTextureCoord(i, uv1, uv2);
  1092. }
  1093. break;
  1094. case ParticleInfo.ParticleObjectType.AnchorBillboard:
  1095. {
  1096. sceneBillboard.SetUpdateType(i, true);
  1097. sceneBillboard.SetStart(i, obj.pos[1]);
  1098. sceneBillboard.SetEnd(i, obj.pos[0]);
  1099. sceneBillboard.SetSize(i, obj.scale.Y);
  1100. sceneBillboard.SetColor(i, obj.color);
  1101. if (isUv)
  1102. sceneBillboard.SetTextureCoord(i, uv1, uv2);
  1103. }
  1104. break;
  1105. case ParticleInfo.ParticleObjectType.Scene:
  1106. {
  1107. sceneMesh.Enabled = true;
  1108. sceneMesh.Visible = true;
  1109. // Rotation
  1110. Matrix localTransform = Matrix.CreateFromAxisAngle(
  1111. obj.rotateDir, obj.rotateAccm);
  1112. // Scale
  1113. localTransform = Helper3D.PostScale(localTransform,
  1114. obj.scale);
  1115. // Position
  1116. localTransform.Translation = obj.pos[0];
  1117. // Set transform
  1118. sceneMesh.WorldTransform = localTransform *
  1119. sceneMesh.WorldTransform;
  1120. }
  1121. break;
  1122. }
  1123. }
  1124. obj.state &= ~((uint)ParticleObject.States.FirstTime);
  1125. }
  1126. if (isStopping && aliveCount == 0)
  1127. {
  1128. // Particle disabled
  1129. SetSceneEnable(false);
  1130. }
  1131. }
  1132. }
  1133. #endregion
  1134. }