2
0

Game1.cs 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. using Microsoft.Xna.Framework;
  2. using Microsoft.Xna.Framework.Graphics;
  3. using Microsoft.Xna.Framework.Input;
  4. using MonoScene.Graphics;
  5. namespace Demo5
  6. {
  7. public class Game1 : Game
  8. {
  9. #region lifecycle
  10. public Game1()
  11. {
  12. _Graphics = new GraphicsDeviceManager(this);
  13. IsMouseVisible = true;
  14. }
  15. protected override void Initialize()
  16. {
  17. this.Window.Title = "SharpGLTF - MonoGame Demo 5";
  18. this.Window.AllowUserResizing = true;
  19. this.Window.AllowAltF4 = true;
  20. base.Initialize();
  21. }
  22. protected override void LoadContent()
  23. {
  24. var factory = new MonoScene.Graphics.Pipeline.GltfModelFactory(this.GraphicsDevice);
  25. factory.UseBasicEffects= true;
  26. var thisAssembly = typeof(Game1).Assembly;
  27. _ModelTemplate = factory.LoadModelFromEmbeddedResource(thisAssembly, "CesiumMan.glb");
  28. }
  29. protected override void UnloadContent()
  30. {
  31. base.UnloadContent();
  32. _ModelTemplate?.Dispose();
  33. _ModelTemplate = null;
  34. }
  35. #endregion
  36. #region data
  37. private GraphicsDeviceManager _Graphics;
  38. private PBREnvironment _LightsAndFog = PBREnvironment.CreateDefault();
  39. private DeviceModelCollection _ModelTemplate;
  40. #endregion
  41. #region Game Loop
  42. private ModelInstance _ModelView1;
  43. protected override void Update(GameTime gameTime)
  44. {
  45. if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape)) Exit();
  46. if (_ModelView1 == null) _ModelView1 = _ModelTemplate.DefaultModel.CreateInstance();
  47. var mdlPos = new Vector3(3.5f, 0, 0);
  48. _ModelView1.WorldMatrix = Matrix.CreateRotationY(0.25f * (float)gameTime.TotalGameTime.TotalSeconds) * Matrix.CreateTranslation(mdlPos);
  49. _ModelView1.Armature.SetAnimationFrame(0, (float)gameTime.TotalGameTime.TotalSeconds);
  50. base.Update(gameTime);
  51. }
  52. protected override void Draw(GameTime gameTime)
  53. {
  54. GraphicsDevice.Clear(Color.CornflowerBlue);
  55. var camPos = Vector3.Zero;
  56. var camX = Matrix.CreateWorld(Vector3.Zero, _ModelView1.WorldBounds.Center - camPos, Vector3.UnitY);
  57. var dc = new ModelDrawingContext(_Graphics.GraphicsDevice);
  58. dc.SetCamera(camX);
  59. dc.DrawModelInstance(_LightsAndFog, _ModelView1);
  60. base.Draw(gameTime);
  61. }
  62. #endregion
  63. }
  64. }