ModelDrawContext.cs 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using Microsoft.Xna.Framework;
  7. using Microsoft.Xna.Framework.Graphics;
  8. using SharpGLTF.Runtime;
  9. namespace MonoGameScene
  10. {
  11. /// <summary>
  12. /// Small helper for rendering MonoGame models.
  13. /// </summary>
  14. class ModelDrawContext
  15. {
  16. #region lifecycle
  17. public ModelDrawContext(GraphicsDeviceManager graphics, Matrix cameraMatrix)
  18. {
  19. _Device = graphics.GraphicsDevice;
  20. _Device.DepthStencilState = DepthStencilState.Default;
  21. _View = Matrix.Invert(cameraMatrix);
  22. float fieldOfView = MathHelper.PiOver4;
  23. float nearClipPlane = 0.01f;
  24. float farClipPlane = 1000;
  25. _Projection = Matrix.CreatePerspectiveFieldOfView(fieldOfView, graphics.GraphicsDevice.Viewport.AspectRatio, nearClipPlane, farClipPlane);
  26. }
  27. #endregion
  28. #region data
  29. private GraphicsDevice _Device;
  30. private Matrix _Projection;
  31. private Matrix _View;
  32. #endregion
  33. #region API
  34. public void DrawModelInstance(MonoGameModelInstance model, Matrix world)
  35. {
  36. foreach (var e in model.Template.Effects) UpdateMaterial(e);
  37. model.Draw(_Projection, _View, world);
  38. }
  39. public static void UpdateMaterial(Effect effect)
  40. {
  41. if (effect is IEffectLights lights)
  42. {
  43. lights.EnableDefaultLighting();
  44. }
  45. if (effect is IEffectFog fog)
  46. {
  47. fog.FogEnabled = false;
  48. }
  49. }
  50. #endregion
  51. }
  52. }