ShatterEffectGame.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. //-----------------------------------------------------------------------------
  2. // ShatterEffectGame.cs
  3. //
  4. // Microsoft XNA Community Game Platform
  5. // Copyright (C) Microsoft Corporation. All rights reserved.
  6. //-----------------------------------------------------------------------------
  7. using System;
  8. using Microsoft.Xna.Framework;
  9. using Microsoft.Xna.Framework.Content;
  10. using Microsoft.Xna.Framework.Graphics;
  11. using Microsoft.Xna.Framework.Input;
  12. namespace ShatterEffect
  13. {
  14. /// <summary>
  15. /// This is the main type for your game
  16. /// </summary>
  17. public class ShatterEffectGame : Game
  18. {
  19. GraphicsDeviceManager graphics;
  20. Vector3 lightPosition = Vector3.UnitY;
  21. Vector4 ambientColor = Color.DarkGray.ToVector4();
  22. Vector4 diffuseColor = Color.White.ToVector4();
  23. Vector4 specularColor = Color.White.ToVector4();
  24. float specularPower = 50;
  25. float time;
  26. const float translationRate = 50;
  27. const float rotationRate = MathHelper.Pi * 3;
  28. const float duration = 2.0f;
  29. Model model;
  30. SpriteFont font;
  31. SpriteBatch spriteBatch;
  32. Matrix view;
  33. Matrix projection;
  34. Vector3 cameraPosition = new Vector3(-696,429, 835);
  35. Vector3 targetPosition = new Vector3(0, 60, 0);
  36. public ShatterEffectGame()
  37. {
  38. graphics = new GraphicsDeviceManager(this);
  39. Content.RootDirectory = "Content";
  40. }
  41. /// <summary>
  42. /// Load your graphics content.
  43. /// </summary>
  44. protected override void LoadContent()
  45. {
  46. spriteBatch = new SpriteBatch(this.graphics.GraphicsDevice);
  47. font = Content.Load<SpriteFont>("font");
  48. model = Content.Load<Model>("model/tank");
  49. // Calculate View/Projection Matrices.
  50. view = Matrix.CreateLookAt(cameraPosition, targetPosition, Vector3.Up);
  51. projection = Matrix.CreatePerspectiveFieldOfView(
  52. MathHelper.ToRadians(45.0f), GraphicsDevice.Viewport.AspectRatio, 1.0f, 10000.0f);
  53. }
  54. /// <summary>
  55. /// Allows the game to run logic such as updating the world,
  56. /// checking for collisions, gathering input and playing audio.
  57. /// </summary>
  58. /// <param name="gameTime">Provides a snapshot of timing values.</param>
  59. protected override void Update(GameTime gameTime)
  60. {
  61. // Handle input
  62. float elapsedTime = (float)gameTime.ElapsedGameTime.TotalSeconds;
  63. KeyboardState keyboardState = Keyboard.GetState();
  64. GamePadState gamePadState = GamePad.GetState(PlayerIndex.One);
  65. // Allows the default game to exit on Xbox 360 and Windows
  66. if (gamePadState.Buttons.Back == ButtonState.Pressed
  67. || keyboardState.IsKeyDown(Keys.Escape))
  68. this.Exit();
  69. // Pressing the Up arrow or the A button on controller will Shatter the
  70. // model
  71. if (keyboardState.IsKeyDown(Keys.Up) ||
  72. gamePadState.Buttons.A == ButtonState.Pressed)
  73. {
  74. time = Math.Min(duration, time + elapsedTime);
  75. }
  76. // Pressing the Down arrow or the B button on controller will reverse the
  77. // Shatter effect
  78. if (keyboardState.IsKeyDown(Keys.Down) ||
  79. gamePadState.Buttons.B == ButtonState.Pressed)
  80. {
  81. time = Math.Max(0.0f, time - elapsedTime);
  82. }
  83. base.Update(gameTime);
  84. }
  85. /// <summary>
  86. /// This is called when the game should draw itself.
  87. /// </summary>
  88. /// <param name="gameTime">Provides a snapshot of timing values.</param>
  89. protected override void Draw(GameTime gameTime)
  90. {
  91. graphics.GraphicsDevice.Clear(Color.CornflowerBlue);
  92. GraphicsDevice.BlendState = BlendState.Opaque;
  93. GraphicsDevice.DepthStencilState = DepthStencilState.Default;
  94. GraphicsDevice.RasterizerState = RasterizerState.CullNone;
  95. Matrix[] transforms = new Matrix[model.Bones.Count];
  96. model.CopyAbsoluteBoneTransformsTo(transforms);
  97. foreach (ModelMesh mesh in model.Meshes)
  98. {
  99. foreach (ModelMeshPart part in mesh.MeshParts)
  100. {
  101. SetupEffect(transforms, mesh, part);
  102. }
  103. mesh.Draw();
  104. }
  105. GraphicsDevice.RasterizerState = RasterizerState.CullCounterClockwise;
  106. // Draw instructions
  107. spriteBatch.Begin();
  108. spriteBatch.DrawString(font,
  109. @"Shatter Model: Hold Arrow Up or A button."+
  110. "\nReverse Shatter: Hold Arrow Down or B button.", new Vector2(50, 380),
  111. Color.White);
  112. spriteBatch.End();
  113. base.Draw(gameTime);
  114. }
  115. // Set the required values in the shader.
  116. private void SetupEffect(Matrix[] transforms, ModelMesh mesh,
  117. ModelMeshPart part)
  118. {
  119. Effect effect = part.Effect;
  120. effect.Parameters["TranslationAmount"].SetValue(translationRate * time);
  121. effect.Parameters["RotationAmount"].SetValue(rotationRate * time);
  122. effect.Parameters["time"].SetValue(time);
  123. effect.Parameters["WorldViewProjection"].SetValue(
  124. transforms[mesh.ParentBone.Index] * view * projection);
  125. effect.Parameters["World"].SetValue(transforms[mesh.ParentBone.Index]);
  126. effect.Parameters["eyePosition"].SetValue(cameraPosition);
  127. effect.Parameters["lightPosition"].SetValue(lightPosition);
  128. effect.Parameters["ambientColor"].SetValue(ambientColor);
  129. effect.Parameters["diffuseColor"].SetValue(diffuseColor);
  130. effect.Parameters["specularColor"].SetValue(specularColor);
  131. effect.Parameters["specularPower"].SetValue(specularPower);
  132. }
  133. }
  134. }