Game1.cs 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. using System;
  2. using System.IO;
  3. using System.Linq;
  4. using Microsoft.Xna.Framework;
  5. using Microsoft.Xna.Framework.Graphics;
  6. using Microsoft.Xna.Framework.Input;
  7. using MonoScene.Graphics;
  8. namespace Demo1
  9. {
  10. public class Game1 : Game
  11. {
  12. #region lifecycle
  13. public Game1()
  14. {
  15. _Graphics = new GraphicsDeviceManager(this);
  16. IsMouseVisible = true;
  17. }
  18. protected override void Initialize()
  19. {
  20. this.Window.Title = "SharpGLTF - MonoGame Demo 1";
  21. this.Window.AllowUserResizing = true;
  22. this.Window.AllowAltF4 = true;
  23. base.Initialize();
  24. }
  25. protected override void LoadContent()
  26. {
  27. var gltfFactory = new MonoScene.Graphics.Pipeline.GltfModelFactory(this.GraphicsDevice);
  28. var model = SharpGLTF.Schema2.ModelRoot.Load($"Content{Path.DirectorySeparatorChar}WaterBottle.glb");
  29. var contentMeshes = gltfFactory.ReadMeshContent(model.LogicalMeshes.Take(1));
  30. var factory = new MonoScene.Graphics.Pipeline.PBRMeshFactory(this.GraphicsDevice);
  31. _MeshCollection = factory.CreateMeshCollection(contentMeshes.Materials, contentMeshes.Meshes);
  32. }
  33. protected override void UnloadContent()
  34. {
  35. base.UnloadContent();
  36. _MeshCollection?.Dispose();
  37. _MeshCollection = null;
  38. }
  39. #endregion
  40. #region data
  41. private GraphicsDeviceManager _Graphics;
  42. private PBREnvironment _LightsAndFog = PBREnvironment.CreateDefault();
  43. private MeshCollection _MeshCollection;
  44. #endregion
  45. #region Game Loop
  46. protected override void Update(GameTime gameTime)
  47. {
  48. if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape)) Exit();
  49. base.Update(gameTime);
  50. }
  51. protected override void Draw(GameTime gameTime)
  52. {
  53. GraphicsDevice.Clear(Color.CornflowerBlue);
  54. var camPos = Vector3.Zero;
  55. var mdlPos = new Vector3(0.5f, 0, 0);
  56. var camX = Matrix.CreateWorld(Vector3.Zero, mdlPos - camPos, Vector3.UnitY);
  57. var mdlX = Matrix.CreateRotationY(0.25f * (float)gameTime.TotalGameTime.TotalSeconds) * Matrix.CreateTranslation(mdlPos);
  58. var dc = new ModelDrawingContext(_Graphics.GraphicsDevice);
  59. dc.NearPlane = 0.1f; // we need to make near plane small because the object is very very close.
  60. dc.SetCamera(camX);
  61. dc.DrawMesh(_LightsAndFog, _MeshCollection[0], mdlX);
  62. base.Draw(gameTime);
  63. }
  64. #endregion
  65. }
  66. }