MonoGameDrawingContext.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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.Schema2;
  9. using MODELINST = SharpGLTF.Runtime.MonoGameModelInstance;
  10. namespace SharpGLTF.Runtime
  11. {
  12. /// <summary>
  13. /// Helper class for rendering <see cref="ModelInstance"/> models.
  14. /// </summary>
  15. public class ModelDrawingContext
  16. {
  17. #region lifecycle
  18. public ModelDrawingContext(GraphicsDevice graphics)
  19. {
  20. _Device = graphics;
  21. _Device.DepthStencilState = DepthStencilState.Default;
  22. _View = Matrix.Invert(Matrix.Identity);
  23. _Projection = SceneUtils.CreatePerspectiveFieldOfView(_FieldOfView, _Device.Viewport.AspectRatio, _NearPlane);
  24. // _DistanceComparer = MODELINST.GetDistanceComparer(-_View.Translation);
  25. }
  26. #endregion
  27. #region data
  28. private GraphicsDevice _Device;
  29. private readonly Stack<_GraphicsState> _PreserveState = new Stack<_GraphicsState>();
  30. private float _FieldOfView = MathHelper.PiOver4;
  31. private float _NearPlane = 1f;
  32. private Matrix _View, _Projection;
  33. private IComparer<MODELINST> _DistanceComparer;
  34. private static readonly HashSet<Effect> _SceneEffects = new HashSet<Effect>();
  35. private static readonly List<MODELINST> _SceneInstances = new List<MODELINST>();
  36. #endregion
  37. #region properties
  38. public float FieldOfView
  39. {
  40. get => _FieldOfView;
  41. set => _FieldOfView = value;
  42. }
  43. public float NearPlane
  44. {
  45. get => _NearPlane;
  46. set => _NearPlane = value;
  47. }
  48. #endregion
  49. #region API
  50. protected void PushState()
  51. {
  52. var state = new _GraphicsState(_Device);
  53. _PreserveState.Push(state);
  54. }
  55. protected void PopState()
  56. {
  57. var state = _PreserveState.Pop();
  58. state.Apply(_Device);
  59. }
  60. public Matrix GetProjectionMatrix()
  61. {
  62. return _Projection;
  63. }
  64. public Matrix GetViewMatrix()
  65. {
  66. return _View;
  67. }
  68. public void SetProjectionMatrix(Matrix projectionMatrix)
  69. {
  70. _Projection = projectionMatrix;
  71. }
  72. public void SetCamera(Matrix cameraMatrix)
  73. {
  74. _View = Matrix.Invert(cameraMatrix);
  75. _Projection = SceneUtils.CreatePerspectiveFieldOfView(_FieldOfView, _Device.Viewport.AspectRatio, _NearPlane);
  76. // _DistanceComparer = MODELINST.GetDistanceComparer(-_View.Translation);
  77. }
  78. #endregion
  79. }
  80. /// <summary>
  81. /// Preserves all the monogame states that might be
  82. /// modified when rendering a model.
  83. /// </summary>
  84. readonly struct _GraphicsState
  85. {
  86. public _GraphicsState(GraphicsDevice graphics)
  87. {
  88. _Rasterizer = graphics.RasterizerState;
  89. _Blend = graphics.BlendState;
  90. _Sampler0 = graphics.SamplerStates[0];
  91. _Sampler1 = graphics.SamplerStates[1];
  92. }
  93. private readonly RasterizerState _Rasterizer;
  94. private readonly BlendState _Blend;
  95. private readonly SamplerState _Sampler0;
  96. private readonly SamplerState _Sampler1;
  97. public void Apply(GraphicsDevice graphics)
  98. {
  99. graphics.RasterizerState = _Rasterizer;
  100. graphics.BlendState = _Blend;
  101. graphics.SamplerStates[0] = _Sampler0;
  102. graphics.SamplerStates[1] = _Sampler1;
  103. }
  104. }
  105. }