KeyFrameTable.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. #region File Description
  2. //-----------------------------------------------------------------------------
  3. // KeyFrameTable.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.Generic;
  12. using System.Text;
  13. using Microsoft.Xna.Framework;
  14. #endregion
  15. namespace RobotGameData.ParticleSystem
  16. {
  17. /// <summary>
  18. /// this structure has a key value which varies along time.
  19. /// </summary>
  20. [Serializable]
  21. public class KeyFrameTable
  22. {
  23. #region Enum Interpolation
  24. public enum Interpolation
  25. {
  26. /// <summary>
  27. /// no interpolate
  28. /// </summary>
  29. None,
  30. /// <summary>
  31. /// interpolate using Lerp
  32. /// </summary>
  33. Lerp,
  34. /// <summary>
  35. /// interpolate using Slerp
  36. /// </summary>
  37. Slerp,
  38. /// <summary>
  39. /// interpolate using Spline
  40. /// </summary>
  41. Spline,
  42. };
  43. #endregion
  44. #region Fields
  45. public int Count = 0;
  46. public bool IsFixedInterval = true;
  47. public List<float> Table = null;
  48. public List<float> Time = null;
  49. // Volatile Members
  50. float step = 0.0f;
  51. #endregion
  52. /// <summary>
  53. /// Initialize members
  54. /// </summary>
  55. public void Initialize()
  56. {
  57. if (Count == 0)
  58. step = 0.0f;
  59. else
  60. step = 1.0f / (float) Count;
  61. }
  62. /// <summary>
  63. /// gets a key value of defined time using interpolation method.
  64. /// </summary>
  65. /// <param name="t">defined time</param>
  66. /// <param name="mode">interpolation method</param>
  67. /// <returns>key value</returns>
  68. public float GetKeyFrame(float t, Interpolation mode)
  69. {
  70. int idx = -1;
  71. float s = 0.0f;
  72. if (Time != null && Time.Count > 0)
  73. idx = GetTimeIndex(t);
  74. else
  75. idx = GetIndex(t);
  76. if (idx >= Count)
  77. idx = Count - 1;
  78. switch (mode)
  79. {
  80. case Interpolation.Lerp:
  81. {
  82. if (idx < (Count - 1))
  83. {
  84. if( Time != null && Time.Count > 0)
  85. s = (t - Time[idx]) / (Time[idx + 1] - Time[idx]);
  86. else
  87. s = (t - ((float)idx * step)) * (float)Count;
  88. return Table[idx] + ((Table[idx + 1] - Table[idx]) * s);
  89. }
  90. return Table[idx];
  91. }
  92. default:
  93. {
  94. return Table[idx];
  95. }
  96. }
  97. }
  98. /// <summary>
  99. /// gets a key index of defined time.
  100. /// </summary>
  101. /// <param name="t">defined time</param>
  102. /// <returns>key index</returns>
  103. protected int GetIndex(float t)
  104. {
  105. return (int)(t * Count);
  106. }
  107. /// <summary>
  108. /// gets a time index of defined time.
  109. /// </summary>
  110. /// <param name="t">defined time</param>
  111. /// <returns>time index</returns>
  112. protected int GetTimeIndex(float t)
  113. {
  114. int si = 0;
  115. int ei = Count - 1;
  116. int mi = 0;
  117. if (t >= Time[ei])
  118. {
  119. return ei;
  120. }
  121. do
  122. {
  123. mi = (si + ei) / 2;
  124. if ((ei - si) <= 1)
  125. {
  126. break;
  127. }
  128. else if (Time[mi] < t)
  129. {
  130. si = mi;
  131. }
  132. else if (Time[mi] > t)
  133. {
  134. ei = mi;
  135. }
  136. else
  137. {
  138. si = mi;
  139. break;
  140. }
  141. } while ((ei - si) > 1);
  142. return si;
  143. }
  144. }
  145. }