SkinnedDemo.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. #region File Description
  2. //-----------------------------------------------------------------------------
  3. // SkinnedDemo.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.Linq;
  13. using System.Text;
  14. using Microsoft.Xna.Framework;
  15. using Microsoft.Xna.Framework.Graphics;
  16. using SkinnedModel;
  17. using GeneratedGeometry;
  18. #endregion
  19. namespace XnaGraphicsDemo
  20. {
  21. /// <summary>
  22. /// Demo shows how to use SkinnedEffect.
  23. /// </summary>
  24. class SkinnedDemo : MenuComponent
  25. {
  26. // Fields.
  27. Sky sky;
  28. Model dude;
  29. AnimationPlayer animationPlayer;
  30. float cameraRotation = 0;
  31. float cameraArc = 0;
  32. /// <summary>
  33. /// Constructor.
  34. /// </summary>
  35. public SkinnedDemo(DemoGame game)
  36. : base(game)
  37. {
  38. Entries.Add(new MenuEntry { Text = "back", Clicked = delegate { Game.SetActiveMenu(0); } });
  39. }
  40. /// <summary>
  41. /// Resets the menu state.
  42. /// </summary>
  43. public override void Reset()
  44. {
  45. cameraRotation = 0;
  46. cameraArc = 0;
  47. base.Reset();
  48. }
  49. /// <summary>
  50. /// Loads content for this demo.
  51. /// </summary>
  52. protected override void LoadContent()
  53. {
  54. sky = Game.Content.Load<Sky>("sky");
  55. dude = Game.Content.Load<Model>("dude");
  56. // Look up our custom skinning information.
  57. SkinningData skinningData = dude.Tag as SkinningData;
  58. if (skinningData == null)
  59. throw new InvalidOperationException
  60. ("This model does not contain a SkinningData tag.");
  61. // Create an animation player, and start decoding an animation clip.
  62. animationPlayer = new AnimationPlayer(skinningData);
  63. AnimationClip clip = skinningData.AnimationClips["Take 001"];
  64. animationPlayer.StartClip(clip);
  65. }
  66. /// <summary>
  67. /// Updates the animation.
  68. /// </summary>
  69. public override void Update(GameTime gameTime)
  70. {
  71. animationPlayer.Update(gameTime.ElapsedGameTime, true, Matrix.Identity);
  72. base.Update(gameTime);
  73. }
  74. /// <summary>
  75. /// Draws the SkinnedEffect demo.
  76. /// </summary>
  77. public override void Draw(GameTime gameTime)
  78. {
  79. // Compute camera matrices.
  80. const float cameraDistance = 100;
  81. Matrix view = Matrix.CreateTranslation(0, -40, 0) *
  82. Matrix.CreateRotationY(MathHelper.ToRadians(cameraRotation)) *
  83. Matrix.CreateRotationX(MathHelper.ToRadians(cameraArc)) *
  84. Matrix.CreateLookAt(new Vector3(0, 0, -cameraDistance),
  85. new Vector3(0, 0, 0), Vector3.Up);
  86. Matrix projection = Matrix.CreatePerspectiveFieldOfView(MathHelper.PiOver4,
  87. GraphicsDevice.Viewport.AspectRatio,
  88. 1,
  89. 10000);
  90. // Draw the background.
  91. GraphicsDevice.Clear(Color.Black);
  92. sky.Draw(view, projection);
  93. DrawTitle("skinned effect", null, new Color(127, 112, 104));
  94. // Draw the animating character.
  95. GraphicsDevice.BlendState = BlendState.Opaque;
  96. GraphicsDevice.RasterizerState = RasterizerState.CullCounterClockwise;
  97. GraphicsDevice.DepthStencilState = DepthStencilState.Default;
  98. GraphicsDevice.SamplerStates[0] = SamplerState.LinearWrap;
  99. Matrix[] bones = animationPlayer.GetSkinTransforms();
  100. foreach (ModelMesh mesh in dude.Meshes)
  101. {
  102. foreach (SkinnedEffect effect in mesh.Effects)
  103. {
  104. effect.SetBoneTransforms(bones);
  105. effect.View = view;
  106. effect.Projection = projection;
  107. effect.EnableDefaultLighting();
  108. effect.SpecularColor = Vector3.Zero;
  109. }
  110. mesh.Draw();
  111. }
  112. base.Draw(gameTime);
  113. }
  114. /// <summary>
  115. /// Dragging on the menu background rotates the camera.
  116. /// </summary>
  117. protected override void OnDrag(Vector2 delta)
  118. {
  119. cameraRotation += delta.X / 4;
  120. cameraArc = MathHelper.Clamp(cameraArc - delta.Y / 4, -70, 70);
  121. }
  122. }
  123. }