InfiniteGridComponent.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. #region License
  2. // Copyright 2017 Kastellanos Nikolaos
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at
  7. //
  8. // http://www.apache.org/licenses/LICENSE-2.0
  9. //
  10. // Unless required by applicable law or agreed to in writing, software
  11. // distributed under the License is distributed on an "AS IS" BASIS,
  12. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. // See the License for the specific language governing permissions and
  14. // limitations under the License.
  15. #endregion
  16. using System;
  17. using Microsoft.Xna.Framework;
  18. using Microsoft.Xna.Framework.Content;
  19. using Microsoft.Xna.Framework.Graphics;
  20. namespace nkast.Aether.Shaders.Components
  21. {
  22. public partial class InfiniteGridComponent : IDrawable, IGameComponent
  23. {
  24. private readonly GraphicsDevice GraphicsDevice;
  25. private readonly ContentManager _content;
  26. private VertexBuffer _quadVertexBuffer;
  27. private IndexBuffer _quadIndexBuffer;
  28. private InfiniteGridEffect _gridEffect;
  29. public int DrawOrder { get; set; }
  30. public bool Visible { get; set; }
  31. // TODO: Implement IDrawable Events
  32. public event EventHandler<EventArgs> DrawOrderChanged;
  33. public event EventHandler<EventArgs> VisibleChanged;
  34. public Matrix Projection;
  35. public Matrix View;
  36. public Matrix EditMatrix;
  37. public Color Color = Color.FromNonPremultiplied(255, 163, 0, 255);
  38. public InfiniteGridComponent(GraphicsDevice graphicsDevice, ContentManager content)
  39. {
  40. this.GraphicsDevice = graphicsDevice;
  41. this._content = content;
  42. }
  43. public void Initialize()
  44. {
  45. this.LoadContent();
  46. }
  47. protected void LoadContent()
  48. {
  49. _gridEffect = new InfiniteGridEffect(this.GraphicsDevice);
  50. var vertices = new VertexPositionNormalTexture[4];
  51. vertices[0].Position = new Vector3(-1f, -1f, 1f);
  52. vertices[1].Position = new Vector3(+1f, -1f, 1f);
  53. vertices[2].Position = new Vector3(+1f, +1f, 1f);
  54. vertices[3].Position = new Vector3(-1f, +1f, 1f);
  55. vertices[0].Normal = Vector3.Forward;
  56. vertices[1].Normal = Vector3.Forward;
  57. vertices[2].Normal = Vector3.Forward;
  58. vertices[3].Normal = Vector3.Forward;
  59. vertices[0].TextureCoordinate = new Vector2(0,0);
  60. vertices[1].TextureCoordinate = new Vector2(0,1);
  61. vertices[2].TextureCoordinate = new Vector2(1,0);
  62. vertices[3].TextureCoordinate = new Vector2(1,1);
  63. short[] indices = new short[] { 0, 1, 2, 2, 3, 0 };
  64. _quadVertexBuffer = new VertexBuffer(GraphicsDevice, VertexPositionNormalTexture.VertexDeclaration, 4, BufferUsage.None);
  65. _quadIndexBuffer = new IndexBuffer(GraphicsDevice, IndexElementSize.SixteenBits, indices.Length, BufferUsage.None);
  66. _quadVertexBuffer.SetData(vertices);
  67. _quadIndexBuffer.SetData(indices);
  68. }
  69. protected void UnloadContent()
  70. {
  71. }
  72. public virtual void Draw(GameTime gameTime)
  73. {
  74. this.GraphicsDevice.BlendState = BlendState.AlphaBlend;
  75. this.GraphicsDevice.DepthStencilState = DepthStencilState.None;
  76. this.GraphicsDevice.RasterizerState = RasterizerState.CullNone;
  77. this.GraphicsDevice.SamplerStates[0] = SamplerState.LinearClamp;
  78. this._gridEffect.SetDefaultParameters(GraphicsDevice.Viewport, this.Projection, this.View, this.EditMatrix);
  79. this._gridEffect.DiffuseColor = this.Color.ToVector4();
  80. this.GraphicsDevice.SetVertexBuffer(_quadVertexBuffer);
  81. this.GraphicsDevice.Indices = _quadIndexBuffer;
  82. if (GraphicsDevice.GraphicsProfile == GraphicsProfile.Reach)
  83. {
  84. this._gridEffect.CurrentTechnique = this._gridEffect.Techniques["HorzLinesATechnique"];
  85. this._gridEffect.CurrentTechnique.Passes[0].Apply();
  86. this.GraphicsDevice.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, 4, 0, 2);
  87. this._gridEffect.CurrentTechnique = this._gridEffect.Techniques["VertLinesATechnique"];
  88. this._gridEffect.CurrentTechnique.Passes[0].Apply();
  89. this.GraphicsDevice.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, 4, 0, 2);
  90. this._gridEffect.CurrentTechnique = this._gridEffect.Techniques["HorzLinesBTechnique"];
  91. this._gridEffect.CurrentTechnique.Passes[0].Apply();
  92. this.GraphicsDevice.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, 4, 0, 2);
  93. this._gridEffect.CurrentTechnique = this._gridEffect.Techniques["VertLinesBTechnique"];
  94. this._gridEffect.CurrentTechnique.Passes[0].Apply();
  95. this.GraphicsDevice.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, 4, 0, 2);
  96. }
  97. else
  98. {
  99. this._gridEffect.CurrentTechnique = this._gridEffect.Techniques["GridTechnique"];
  100. this._gridEffect.CurrentTechnique.Passes[0].Apply();
  101. this.GraphicsDevice.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, 4, 0, 2);
  102. }
  103. return;
  104. }
  105. }
  106. }