FXAASampleComponent.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. using System;
  2. using System.Collections.Generic;
  3. using Microsoft.Xna.Framework;
  4. using Microsoft.Xna.Framework.Audio;
  5. using Microsoft.Xna.Framework.Content;
  6. using Microsoft.Xna.Framework.Graphics;
  7. using Microsoft.Xna.Framework.Input;
  8. namespace Samples.FXAA
  9. {
  10. internal class FXAASampleComponent : DrawableGameComponent
  11. {
  12. ContentManager Content;
  13. SpriteBatch spriteBatch;
  14. SpriteFont font;
  15. KeyboardState previousKeyboardState;
  16. bool useFXAA = true;
  17. bool rotate = true;
  18. Vector3 cameraPosition;
  19. Matrix world;
  20. Matrix projection;
  21. Matrix view;
  22. Spaceship spaceship;
  23. Vector3 spaceshipPos = Vector3.Zero;
  24. float time = 0;
  25. AntiAliasing _antiAliasing;
  26. public FXAASampleComponent(Game game) : base(game)
  27. {
  28. }
  29. /// <summary>Initializes the component. Used to load non-graphical resources.</summary>
  30. public override void Initialize()
  31. {
  32. Content = new ContentManager(Game.Services, "Content");
  33. base.Initialize();
  34. }
  35. /// <summary>Load graphical resources needed by this component.</summary>
  36. protected override void LoadContent()
  37. {
  38. // Create and load our tank
  39. spaceship = new Spaceship();
  40. spaceship.Load(Content);
  41. spriteBatch = new SpriteBatch(GraphicsDevice);
  42. font = Content.Load<SpriteFont>("font");
  43. projection = Matrix.CreatePerspectiveFieldOfView(
  44. MathHelper.PiOver4, GraphicsDevice.Viewport.AspectRatio, 10f, 10000f);
  45. _antiAliasing = new AntiAliasing(GraphicsDevice);
  46. }
  47. /// <summary>Unload graphical resources needed by this component.</summary>
  48. protected override void UnloadContent()
  49. {
  50. Content.Unload();
  51. }
  52. /// <summary>Update the component.</summary>
  53. /// <param name="gameTime">GameTime of the Game.</param>
  54. public override void Update(GameTime gameTime)
  55. {
  56. KeyboardState keyState = Keyboard.GetState();
  57. GamePadState gamePadState = GamePad.GetState(PlayerIndex.One);
  58. if (keyState.IsKeyDown(Keys.F1) && !previousKeyboardState.IsKeyDown(Keys.F1))
  59. useFXAA = !useFXAA;
  60. if (keyState.IsKeyDown(Keys.F2) && !previousKeyboardState.IsKeyDown(Keys.F2))
  61. rotate = !rotate;
  62. if (rotate)
  63. time += (float)gameTime.ElapsedGameTime.TotalSeconds;
  64. world = Matrix.CreateFromAxisAngle(Vector3.Up, time);
  65. cameraPosition = new Vector3(0, 2800f, 2800f);
  66. view = Matrix.CreateLookAt(
  67. cameraPosition,
  68. Vector3.Zero,
  69. Vector3.Up);
  70. previousKeyboardState = keyState;
  71. }
  72. /// <summary>Draw this component.</summary>
  73. /// <param name="gameTime">The time elapsed since the last call to Draw.</param>
  74. public override void Draw(GameTime gameTime)
  75. {
  76. GraphicsDevice.BlendState = BlendState.Opaque;
  77. GraphicsDevice.DepthStencilState = DepthStencilState.Default;
  78. _antiAliasing.SetRenderTarget(GraphicsDevice.Viewport, Color.Black);
  79. spaceship.Draw(world, view, projection);
  80. GraphicsDevice.SetRenderTarget(null);
  81. _antiAliasing.DrawRenderTarget( (useFXAA ? 3 : 0), GraphicsDevice.Viewport, false);
  82. spriteBatch.Begin();
  83. spriteBatch.DrawString(font, String.Format("[F1] FXAA - ({0})", useFXAA ? "ON" : "OFF"), new Vector2(20, 20), Color.White);
  84. spriteBatch.DrawString(font, String.Format("[F2] Rotate - ({0})", rotate ? "ON" : "OFF"), new Vector2(20, 40), Color.White);
  85. spriteBatch.End();
  86. }
  87. }
  88. }