ShatterEffectGame.cs 6.3 KB

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