InfiniteGridComponent.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. using System;
  2. using Microsoft.Xna.Framework;
  3. using Microsoft.Xna.Framework.Content;
  4. using Microsoft.Xna.Framework.Graphics;
  5. namespace tainicom.Aether.Shaders.Components
  6. {
  7. public partial class InfiniteGridComponent : IDrawable, IGameComponent
  8. {
  9. private readonly GraphicsDevice GraphicsDevice;
  10. private readonly ContentManager _content;
  11. private VertexBuffer _quadVertexBuffer;
  12. private IndexBuffer _quadIndexBuffer;
  13. private InfiniteGridEffect _gridEffect;
  14. public int DrawOrder { get; set; }
  15. public bool Visible { get; set; }
  16. // TODO: Implement IDrawable Events
  17. public event EventHandler<EventArgs> DrawOrderChanged;
  18. public event EventHandler<EventArgs> VisibleChanged;
  19. public Matrix Projection;
  20. public Matrix View;
  21. public Matrix EditMatrix;
  22. public InfiniteGridComponent(GraphicsDevice graphicsDevice, ContentManager content)
  23. {
  24. this.GraphicsDevice = graphicsDevice;
  25. this._content = content;
  26. }
  27. public void Initialize()
  28. {
  29. this.LoadContent();
  30. }
  31. protected void LoadContent()
  32. {
  33. _gridEffect = new InfiniteGridEffect(this.GraphicsDevice);
  34. var vertices = new VertexPositionNormalTexture[4];
  35. vertices[0].Position = new Vector3(-1f, -1f, 1f);
  36. vertices[1].Position = new Vector3(+1f, -1f, 1f);
  37. vertices[2].Position = new Vector3(+1f, +1f, 1f);
  38. vertices[3].Position = new Vector3(-1f, +1f, 1f);
  39. vertices[0].Normal = Vector3.Forward;
  40. vertices[1].Normal = Vector3.Forward;
  41. vertices[2].Normal = Vector3.Forward;
  42. vertices[3].Normal = Vector3.Forward;
  43. vertices[0].TextureCoordinate = new Vector2(0,0);
  44. vertices[1].TextureCoordinate = new Vector2(0,1);
  45. vertices[2].TextureCoordinate = new Vector2(1,0);
  46. vertices[3].TextureCoordinate = new Vector2(1,1);
  47. short[] indices = new short[] { 0, 1, 2, 2, 3, 0 };
  48. _quadVertexBuffer = new VertexBuffer(GraphicsDevice, VertexPositionNormalTexture.VertexDeclaration, 4, BufferUsage.None);
  49. _quadIndexBuffer = new IndexBuffer(GraphicsDevice, IndexElementSize.SixteenBits, indices.Length, BufferUsage.None);
  50. _quadVertexBuffer.SetData(vertices);
  51. _quadIndexBuffer.SetData(indices);
  52. }
  53. protected void UnloadContent()
  54. {
  55. }
  56. public virtual void Draw(GameTime gameTime)
  57. {
  58. this.GraphicsDevice.BlendState = BlendState.AlphaBlend;
  59. this.GraphicsDevice.DepthStencilState = DepthStencilState.None;
  60. this.GraphicsDevice.RasterizerState = RasterizerState.CullNone;
  61. this.GraphicsDevice.SamplerStates[0] = SamplerState.LinearClamp;
  62. this._gridEffect.SetDefaultParameters(GraphicsDevice.Viewport, this.Projection, this.View, this.EditMatrix);
  63. this.GraphicsDevice.SetVertexBuffer(_quadVertexBuffer);
  64. this.GraphicsDevice.Indices = _quadIndexBuffer;
  65. if (GraphicsDevice.GraphicsProfile == GraphicsProfile.Reach)
  66. {
  67. this._gridEffect.CurrentTechnique = this._gridEffect.Techniques["HorzLinesATechnique"];
  68. this._gridEffect.CurrentTechnique.Passes[0].Apply();
  69. this.GraphicsDevice.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, 4, 0, 2);
  70. this._gridEffect.CurrentTechnique = this._gridEffect.Techniques["VertLinesATechnique"];
  71. this._gridEffect.CurrentTechnique.Passes[0].Apply();
  72. this.GraphicsDevice.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, 4, 0, 2);
  73. this._gridEffect.CurrentTechnique = this._gridEffect.Techniques["HorzLinesBTechnique"];
  74. this._gridEffect.CurrentTechnique.Passes[0].Apply();
  75. this.GraphicsDevice.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, 4, 0, 2);
  76. this._gridEffect.CurrentTechnique = this._gridEffect.Techniques["VertLinesBTechnique"];
  77. this._gridEffect.CurrentTechnique.Passes[0].Apply();
  78. this.GraphicsDevice.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, 4, 0, 2);
  79. }
  80. else
  81. {
  82. this._gridEffect.CurrentTechnique = this._gridEffect.Techniques["GridTechnique"];
  83. this._gridEffect.CurrentTechnique.Passes[0].Apply();
  84. this.GraphicsDevice.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, 4, 0, 2);
  85. }
  86. return;
  87. }
  88. }
  89. }