Advertisement
Guest User

Untitled

a guest
Feb 9th, 2021
31
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.45 KB | None
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using Microsoft.Xna.Framework;
  5. using Microsoft.Xna.Framework.Audio;
  6. using Microsoft.Xna.Framework.Content;
  7. //using Microsoft.Xna.Framework.GamerServices;
  8. using Microsoft.Xna.Framework.Graphics;
  9. using Microsoft.Xna.Framework.Input;
  10. using Microsoft.Xna.Framework.Media;
  11. using BootHillHeroes.GameScreens;
  12. using BootHillHeroes.Components;
  13. //using Microsoft.Xna.Framework.Net;
  14. //using Microsoft.Xna.Framework.Storage;
  15.  
  16. namespace BHB_Code.Hall.FirstShader
  17. {
  18. /// <summary>
  19. /// This is the main type for your game
  20. /// </summary>
  21. public class FirstShader
  22. {
  23. GraphicsDeviceManager graphics;
  24. SpriteBatch spriteBatch;
  25.  
  26. Matrix world = Matrix.CreateTranslation(0, 0, 0);
  27. Matrix view = Matrix.CreateLookAt(new Vector3(0, 0, 10), new Vector3(0, 0, 0), new Vector3(0, 1, 0));
  28. Matrix projection = Matrix.CreatePerspectiveFieldOfView(MathHelper.ToRadians(45), 800f / 600f, 0.1f, 100f);
  29. Effect effect;
  30. float angle = 0;
  31. float distance = 10;
  32.  
  33. Model model;
  34. Vector3 viewVector;
  35.  
  36. //public FirstShader(GraphicsDeviceManager Graphics, SpriteBatch SpriteBatch)
  37. public FirstShader()
  38. {
  39. //graphics = new GraphicsDeviceManager(this);
  40. //spriteBatch = SpriteBatch;
  41. model = DataManager.GetModel("Helicopter2", BaseGameState.PlayerContent);
  42. effect = DataManager.GetEffect("Specular", BaseGameState.PlayerContent);
  43. }
  44.  
  45.  
  46.  
  47. public void Update(GameTime gameTime)
  48. {
  49. angle += 0.01f;
  50. view = Matrix.CreateLookAt(distance * new Vector3((float)Math.Sin(angle), 0, (float)Math.Cos(angle)), new Vector3(0, 0, 0), new Vector3(0, 1, 0));
  51.  
  52.  
  53. Vector3 cameraLocation = distance * new Vector3((float)Math.Sin(angle), 0, (float)Math.Cos(angle));
  54. Vector3 cameraTarget = new Vector3(0, 0, 0);
  55. viewVector = Vector3.Transform(cameraTarget - cameraLocation, Matrix.CreateRotationY(0));
  56. viewVector.Normalize();
  57. view = Matrix.CreateLookAt(cameraLocation, cameraTarget, new Vector3(0, 1, 0));
  58.  
  59.  
  60. }
  61. public void Draw(GameTime gameTime)
  62. {
  63. //GraphicsDevice.Clear(Color.Black);
  64.  
  65. DrawModelWithEffect(model, world, view, projection);
  66.  
  67. }
  68.  
  69. private void DrawModel(Model model, Matrix world, Matrix view, Matrix projection)
  70. {
  71. foreach (ModelMesh mesh in model.Meshes)
  72. {
  73. foreach (BasicEffect effect in mesh.Effects)
  74. {
  75. effect.EnableDefaultLighting();
  76. effect.PreferPerPixelLighting = true;
  77. effect.World = world * mesh.ParentBone.Transform;
  78. effect.View = view;
  79. effect.Projection = projection;
  80. }
  81. mesh.Draw();
  82. }
  83. }
  84.  
  85. private void DrawModelWithEffect(Model model, Matrix world, Matrix view, Matrix projection)
  86. {
  87. foreach (ModelMesh mesh in model.Meshes)
  88. {
  89. foreach (ModelMeshPart part in mesh.MeshParts)
  90. {
  91. part.Effect = effect;
  92. effect.Parameters["World"].SetValue(world * mesh.ParentBone.Transform);
  93. effect.Parameters["View"].SetValue(view);
  94. effect.Parameters["Projection"].SetValue(projection);
  95. effect.Parameters["ViewVector"].SetValue(viewVector);
  96.  
  97. //Ambient
  98. // effect.Parameters["AmbientColor"].SetValue(Color.Green.ToVector4());
  99. // effect.Parameters["AmbientIntensity"].SetValue(0.5f);
  100. //Diffuse
  101. Matrix worldInverseTransposeMatrix = Matrix.Transpose(Matrix.Invert(mesh.ParentBone.Transform * world));
  102. effect.Parameters["WorldInverseTranspose"].SetValue(worldInverseTransposeMatrix);
  103.  
  104.  
  105. }
  106. mesh.Draw();
  107. }
  108. }
  109. }
  110. }
Advertisement
RAW Paste Data Copied
Advertisement