AvatarBlendedAnimation.cs 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. #region File Description
  2. //-----------------------------------------------------------------------------
  3. // AvatarBlendedAnimation.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.ObjectModel;
  12. using Microsoft.Xna.Framework;
  13. using Microsoft.Xna.Framework.GamerServices;
  14. #endregion
  15. namespace AvatarAnimationBlendingSample
  16. {
  17. public class AvatarBlendedAnimation : IAvatarAnimation
  18. {
  19. #region Properties
  20. /// <summary>
  21. /// The current position of the bones
  22. /// </summary>
  23. public ReadOnlyCollection<Matrix> BoneTransforms
  24. {
  25. get
  26. {
  27. return boneTransforms;
  28. }
  29. }
  30. private Matrix[] avatarBones = new Matrix[AvatarRenderer.BoneCount];
  31. private ReadOnlyCollection<Matrix> boneTransforms;
  32. /// <summary>
  33. /// The current expression of the avatar in the animation
  34. /// We do not blend the expression and only use the current animations
  35. /// expression
  36. /// </summary>
  37. public AvatarExpression Expression
  38. {
  39. get
  40. {
  41. return currentAnimation.Expression;
  42. }
  43. }
  44. #endregion
  45. #region Fields
  46. // The current animation to play and blend from
  47. private AvatarAnimation currentAnimation;
  48. // The next animation to play and blend target
  49. private AvatarAnimation targetAnimation;
  50. // The amount of time to take to blend between animations
  51. private TimeSpan blendTotalTime = new TimeSpan(0, 0, 0, 0, 250);
  52. // The current time in the blend cycle
  53. private TimeSpan blendCurrentTime;
  54. #endregion
  55. #region Initialization
  56. /// <summary>
  57. /// Constructor
  58. /// </summary>
  59. /// <param name="startingAnimation">The first animation to play</param>
  60. public AvatarBlendedAnimation(AvatarAnimation startingAnimation)
  61. {
  62. // Set the first animation
  63. currentAnimation = startingAnimation;
  64. // Create collection we will use for blended transfroms
  65. boneTransforms = new ReadOnlyCollection<Matrix>(avatarBones);
  66. }
  67. #endregion
  68. #region Update and Play
  69. /// <summary>
  70. /// Updates the animation and blends with the next animation if there is one.
  71. /// </summary>
  72. /// <param name="elapsedAnimationTime">Time since the last update</param>
  73. /// <param name="loop">Should the animation loop</param>
  74. public void Update(TimeSpan elapsedAnimationTime, bool loop)
  75. {
  76. // Update the current animation
  77. currentAnimation.Update(elapsedAnimationTime, loop);
  78. // Check to see if we need to blend animations or not
  79. if (targetAnimation == null)
  80. {
  81. // We are not blending so copy the current animations bone transforms
  82. currentAnimation.BoneTransforms.CopyTo(avatarBones, 0);
  83. }
  84. else
  85. {
  86. // Update the target animation
  87. targetAnimation.Update(elapsedAnimationTime, loop);
  88. ReadOnlyCollection<Matrix> currentBoneTransforms =
  89. currentAnimation.BoneTransforms;
  90. ReadOnlyCollection<Matrix> targetBoneTransforms =
  91. targetAnimation.BoneTransforms;
  92. // Update the current blended time
  93. blendCurrentTime += elapsedAnimationTime;
  94. // Calculate blend factor
  95. float blendFactor = (float)(blendCurrentTime.TotalSeconds /
  96. blendTotalTime.TotalSeconds);
  97. // Check to see if we are done blending
  98. if (blendFactor >= 1.0f)
  99. {
  100. // Set the current animtion and remove the target animation
  101. currentAnimation = targetAnimation;
  102. targetAnimation = null;
  103. blendFactor = 1.0f;
  104. }
  105. // Variables to hold the rotations and translations for the
  106. // current, target, and blended transforms
  107. Quaternion currentRotation, targetRotation, finalRotation;
  108. Vector3 currentTranslation, targetTranslation, finalTranslation;
  109. // Loop all of the bones in the avatar
  110. for (int i = 0; i < avatarBones.Length; ++i)
  111. {
  112. // Find the rotation of the current and target bone transforms
  113. currentRotation =
  114. Quaternion.CreateFromRotationMatrix(currentBoneTransforms[i]);
  115. targetRotation =
  116. Quaternion.CreateFromRotationMatrix(targetBoneTransforms[i]);
  117. // Calculate the blended rotation from the current to the target
  118. Quaternion.Slerp(ref currentRotation, ref targetRotation,
  119. blendFactor, out finalRotation);
  120. // Find the translation of the current and target bone transforms
  121. currentTranslation = currentBoneTransforms[i].Translation;
  122. targetTranslation = targetBoneTransforms[i].Translation;
  123. // Calculate the blended translation from the current to the target
  124. Vector3.Lerp(ref currentTranslation, ref targetTranslation,
  125. blendFactor, out finalTranslation);
  126. // Build the final bone transform
  127. avatarBones[i] = Matrix.CreateFromQuaternion(finalRotation) *
  128. Matrix.CreateTranslation(finalTranslation);
  129. }
  130. }
  131. }
  132. /// <summary>
  133. /// Plays a new animation and blends from the current animation.
  134. /// </summary>
  135. /// <param name="nextAnimation">Next animation to play</param>
  136. public void Play(AvatarAnimation nextAnimation)
  137. {
  138. // Check to make sure we are not already playing the animation passed in
  139. if (currentAnimation == nextAnimation)
  140. return;
  141. // Set the next animation
  142. targetAnimation = nextAnimation;
  143. // Reset the animation to the start
  144. targetAnimation.CurrentPosition = TimeSpan.Zero;
  145. // Reset the blend current time to zero
  146. blendCurrentTime = TimeSpan.Zero;
  147. }
  148. #endregion
  149. /// <summary>
  150. /// The current position in the animation
  151. /// Uses the target animation while blending to it
  152. /// </summary>
  153. public TimeSpan CurrentPosition
  154. {
  155. get
  156. {
  157. if (targetAnimation != null)
  158. return targetAnimation.CurrentPosition;
  159. else
  160. return currentAnimation.CurrentPosition;
  161. }
  162. set
  163. {
  164. if (targetAnimation != null)
  165. targetAnimation.CurrentPosition = value;
  166. else
  167. currentAnimation.CurrentPosition = value;
  168. }
  169. }
  170. /// <summary>
  171. /// The length of the animation
  172. /// Uses the target animation while blending to it
  173. /// </summary>
  174. public TimeSpan Length
  175. {
  176. get
  177. {
  178. if (targetAnimation != null)
  179. return targetAnimation.Length;
  180. else
  181. return currentAnimation.Length;
  182. }
  183. }
  184. }
  185. }