Transformations.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. #region File Description
  2. //-----------------------------------------------------------------------------
  3. // A class containing all the basic transformations a renderable object can have.
  4. // This include: Translation, Rotation, and Scale.
  5. //
  6. // Author: Ronen Ness.
  7. // Since: 2017.
  8. //-----------------------------------------------------------------------------
  9. #endregion
  10. using Microsoft.Xna.Framework;
  11. using Microsoft.Xna.Framework.Graphics;
  12. namespace MonoGameSceneGraph
  13. {
  14. /// <summary>
  15. /// MonoGameSceneGraph is the main namespace that contains all the MonoGame-SceneGraph entities.
  16. /// </summary>
  17. [System.Runtime.CompilerServices.CompilerGenerated]
  18. class NamespaceDoc
  19. {
  20. }
  21. /// <summary>
  22. /// Different way to build matrix from transformations.
  23. /// </summary>
  24. public enum TransformOrder
  25. {
  26. /// <summary>
  27. /// Apply position, then rotation, then scale.
  28. /// </summary>
  29. PositionRotationScale,
  30. /// <summary>
  31. /// Apply position, then scale, then rotation.
  32. /// </summary>
  33. PositionScaleRotation,
  34. /// <summary>
  35. /// Apply scale, then position, then rotation.
  36. /// </summary>
  37. ScalePositionRotation,
  38. /// <summary>
  39. /// Apply scale, then rotation, then position.
  40. /// </summary>
  41. ScaleRotationPosition,
  42. /// <summary>
  43. /// Apply rotation, then scale, then position.
  44. /// </summary>
  45. RotationScalePosition,
  46. /// <summary>
  47. /// Apply rotation, then position, then scale.
  48. /// </summary>
  49. RotationPositionScale,
  50. }
  51. /// <summary>
  52. /// Different ways to apply rotation (order in which we rotate the different axis).
  53. /// </summary>
  54. public enum RotationOrder
  55. {
  56. /// <summary>
  57. /// Rotate by axis order X, Y, Z.
  58. /// </summary>
  59. RotateXYZ,
  60. /// <summary>
  61. /// Rotate by axis order X, Z, Y.
  62. /// </summary>
  63. RotateXZY,
  64. /// <summary>
  65. /// Rotate by axis order Y, X, Z.
  66. /// </summary>
  67. RotateYXZ,
  68. /// <summary>
  69. /// Rotate by axis order Y, Z, X.
  70. /// </summary>
  71. RotateYZX,
  72. /// <summary>
  73. /// Rotate by axis order Z, X, Y.
  74. /// </summary>
  75. RotateZXY,
  76. /// <summary>
  77. /// Rotate by axis order Z, Y, X.
  78. /// </summary>
  79. RotateZYX,
  80. }
  81. /// <summary>
  82. /// Contain all the possible node transformations.
  83. /// </summary>
  84. public class Transformations
  85. {
  86. /// <summary>
  87. /// Node position / translation.
  88. /// </summary>
  89. public Vector3 Position;
  90. /// <summary>
  91. /// Node rotation.
  92. /// </summary>
  93. public Vector3 Rotation;
  94. /// <summary>
  95. /// Node scale.
  96. /// </summary>
  97. public Vector3 Scale;
  98. /// <summary>
  99. /// Create new default transformations.
  100. /// </summary>
  101. public Transformations()
  102. {
  103. Position = Vector3.Zero;
  104. Rotation = Vector3.Zero;
  105. Scale = Vector3.One;
  106. }
  107. /// <summary>
  108. /// Clone transformations.
  109. /// </summary>
  110. public Transformations(Transformations other)
  111. {
  112. Position = other.Position;
  113. Rotation = other.Rotation;
  114. Scale = other.Scale;
  115. }
  116. /// <summary>
  117. /// Build and return just the rotation matrix for this treansformations.
  118. /// </summary>
  119. /// <param name="rotationOrder">In which order to apply rotation (axis order) when applying rotation.</param>
  120. /// <returns></returns>
  121. public Matrix BuildRotationMatrix(RotationOrder rotationOrder = RotationOrder.RotateYXZ)
  122. {
  123. switch (rotationOrder)
  124. {
  125. case RotationOrder.RotateXYZ:
  126. return Matrix.CreateRotationX(Rotation.X) * Matrix.CreateRotationY(Rotation.Y) * Matrix.CreateRotationZ(Rotation.Z);
  127. case RotationOrder.RotateXZY:
  128. return Matrix.CreateRotationX(Rotation.X) * Matrix.CreateRotationZ(Rotation.Z) * Matrix.CreateRotationY(Rotation.Y);
  129. case RotationOrder.RotateYXZ:
  130. return Matrix.CreateFromYawPitchRoll(Rotation.Y, Rotation.X, Rotation.Z);
  131. case RotationOrder.RotateYZX:
  132. return Matrix.CreateRotationY(Rotation.Y) * Matrix.CreateRotationZ(Rotation.Z) * Matrix.CreateRotationX(Rotation.X);
  133. case RotationOrder.RotateZXY:
  134. return Matrix.CreateRotationZ(Rotation.Z) * Matrix.CreateRotationX(Rotation.X) * Matrix.CreateRotationY(Rotation.Y);
  135. case RotationOrder.RotateZYX:
  136. return Matrix.CreateRotationZ(Rotation.Z) * Matrix.CreateRotationY(Rotation.Y) * Matrix.CreateRotationX(Rotation.X);
  137. default:
  138. throw new System.Exception("Unknown rotation order!");
  139. }
  140. }
  141. /// <summary>
  142. /// Build and return a matrix from current transformations.
  143. /// </summary>
  144. /// <param name="transformOrder">In which order to apply transformations to produce final matrix.</param>
  145. /// <param name="rotationOrder">In which order to apply rotation (axis order) when applying rotation.</param>
  146. /// <returns>Matrix with all transformations applied.</returns>
  147. public Matrix BuildMatrix(TransformOrder transformOrder = TransformOrder.ScaleRotationPosition,
  148. RotationOrder rotationOrder = RotationOrder.RotateYXZ)
  149. {
  150. // create the matrix parts
  151. Matrix pos = Matrix.CreateTranslation(Position);
  152. Matrix rot = BuildRotationMatrix(rotationOrder);
  153. Matrix scale = Matrix.CreateScale(Scale);
  154. // build and return matrix based on order
  155. switch (transformOrder)
  156. {
  157. case TransformOrder.PositionRotationScale:
  158. return pos * rot * scale;
  159. case TransformOrder.PositionScaleRotation:
  160. return pos * scale * rot;
  161. case TransformOrder.ScalePositionRotation:
  162. return scale * pos * rot;
  163. case TransformOrder.ScaleRotationPosition:
  164. return scale * rot * pos;
  165. case TransformOrder.RotationScalePosition:
  166. return rot * scale * pos;
  167. case TransformOrder.RotationPositionScale:
  168. return rot * pos * scale;
  169. default:
  170. throw new System.Exception("Unknown build matrix order!");
  171. }
  172. }
  173. }
  174. }