SkinnedDemo.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. //-----------------------------------------------------------------------------
  2. // SkinnedDemo.cs
  3. //
  4. // Microsoft XNA Community Game Platform
  5. // Copyright (C) Microsoft Corporation. All rights reserved.
  6. //-----------------------------------------------------------------------------
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Linq;
  10. using System.Text;
  11. using Microsoft.Xna.Framework;
  12. using Microsoft.Xna.Framework.Graphics;
  13. using SkinnedModel;
  14. using GeneratedGeometry;
  15. namespace XnaGraphicsDemo
  16. {
  17. /// <summary>
  18. /// Demo shows how to use SkinnedEffect.
  19. /// </summary>
  20. class SkinnedDemo : MenuComponent
  21. {
  22. // Fields.
  23. Sky sky;
  24. Model dude;
  25. AnimationPlayer animationPlayer;
  26. float cameraRotation = 0;
  27. float cameraArc = 0;
  28. /// <summary>
  29. /// Initializes a new instance of the <see cref="SkinnedDemo"/> class and sets up menu entries.
  30. /// </summary>
  31. /// <param name="game">The game instance.</param>
  32. public SkinnedDemo(DemoGame game)
  33. : base(game)
  34. {
  35. Entries.Add(new MenuEntry { Text = "back", Clicked = delegate { Game.SetActiveMenu(0); } });
  36. }
  37. /// <summary>
  38. /// Resets the menu state and camera rotation values.
  39. /// </summary>
  40. public override void Reset()
  41. {
  42. cameraRotation = 0;
  43. cameraArc = 0;
  44. base.Reset();
  45. }
  46. /// <summary>
  47. /// Loads content for this demo, including the sky and animated model.
  48. /// </summary>
  49. /// <summary>
  50. /// Loads content for this demo, including the sky and animated model.
  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 player and menu state.
  68. /// </summary>
  69. /// <param name="gameTime">The current game time.</param>
  70. public override void Update(GameTime gameTime)
  71. {
  72. animationPlayer.Update(gameTime.ElapsedGameTime, true, Matrix.Identity);
  73. base.Update(gameTime);
  74. }
  75. /// <summary>
  76. /// Draws the SkinnedEffect demo, including the sky and animated character.
  77. /// </summary>
  78. /// <param name="gameTime">The current game time.</param>
  79. public override void Draw(GameTime gameTime)
  80. {
  81. // Compute camera matrices.
  82. const float cameraDistance = 100;
  83. Matrix view = Matrix.CreateTranslation(0, -40, 0) *
  84. Matrix.CreateRotationY(MathHelper.ToRadians(cameraRotation)) *
  85. Matrix.CreateRotationX(MathHelper.ToRadians(cameraArc)) *
  86. Matrix.CreateLookAt(new Vector3(0, 0, -cameraDistance),
  87. new Vector3(0, 0, 0), Vector3.Up);
  88. Matrix projection = Matrix.CreatePerspectiveFieldOfView(MathHelper.PiOver4,
  89. GraphicsDevice.Viewport.AspectRatio,
  90. 1,
  91. 10000);
  92. // Draw the background.
  93. GraphicsDevice.Clear(Color.Black);
  94. sky.Draw(view, projection);
  95. DrawTitle("skinned effect", null, new Color(127, 112, 104));
  96. // Draw the animating character.
  97. GraphicsDevice.BlendState = BlendState.Opaque;
  98. GraphicsDevice.RasterizerState = RasterizerState.CullCounterClockwise;
  99. GraphicsDevice.DepthStencilState = DepthStencilState.Default;
  100. GraphicsDevice.SamplerStates[0] = SamplerState.LinearWrap;
  101. Matrix[] bones = animationPlayer.GetSkinTransforms();
  102. foreach (ModelMesh mesh in dude.Meshes)
  103. {
  104. foreach (SkinnedEffect effect in mesh.Effects)
  105. {
  106. effect.SetBoneTransforms(bones);
  107. effect.View = view;
  108. effect.Projection = projection;
  109. effect.EnableDefaultLighting();
  110. effect.SpecularColor = Vector3.Zero;
  111. }
  112. mesh.Draw();
  113. }
  114. base.Draw(gameTime);
  115. }
  116. /// <summary>
  117. /// Dragging on the menu background rotates the camera.
  118. /// </summary>
  119. /// <param name="delta">The amount the pointer has moved since the last drag event.</param>
  120. protected override void OnDrag(Vector2 delta)
  121. {
  122. cameraRotation += delta.X / 4;
  123. cameraArc = MathHelper.Clamp(cameraArc - delta.Y / 4, -70, 70);
  124. }
  125. }
  126. }