InfiniteGridEffect.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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 System.IO;
  18. using System.Reflection;
  19. using Microsoft.Xna.Framework;
  20. using Microsoft.Xna.Framework.Graphics;
  21. namespace nkast.Aether.Shaders
  22. {
  23. public class InfiniteGridEffect : Effect //, IEffectMatrices
  24. {
  25. #region Effect Parameters
  26. EffectParameter diffuseColorParam;
  27. EffectParameter worldViewProjectionParam;
  28. EffectParameter texelSizeParam;
  29. EffectParameter invProjectionParam;
  30. EffectParameter InvViewParam;
  31. EffectParameter planeNormalParam;
  32. EffectParameter planeDParam;
  33. EffectParameter InvPlaneMatrixParam;
  34. #endregion
  35. #region Fields
  36. static readonly string ResourceName = "nkast.Aether.Shaders.Resources.InfiniteGridEffect";
  37. private static string GetResourceName(GraphicsDevice graphicsDevice, string name)
  38. {
  39. string profileName = (graphicsDevice.GraphicsProfile == GraphicsProfile.Reach) ? ".Reach" : ".HiDef";
  40. string platformName = "";
  41. string version = "";
  42. #if XNA
  43. platformName = ".xna";
  44. #else
  45. switch (graphicsDevice.Adapter.Backend)
  46. {
  47. case GraphicsBackend.DirectX11:
  48. platformName = ".dx11.fxo";
  49. break;
  50. case GraphicsBackend.OpenGL:
  51. case GraphicsBackend.GLES:
  52. case GraphicsBackend.WebGL:
  53. platformName = ".ogl.fxo";
  54. break;
  55. default:
  56. throw new NotSupportedException("platform");
  57. }
  58. // Detect version
  59. version = ".10";
  60. Version kniVersion = typeof(Effect).Assembly.GetName().Version;
  61. if (kniVersion.Major == 3)
  62. {
  63. if (kniVersion.Minor == 9)
  64. {
  65. version = ".10";
  66. }
  67. if (kniVersion.Minor == 10)
  68. {
  69. version = ".10";
  70. }
  71. }
  72. #endif
  73. return name + profileName + platformName + version;
  74. }
  75. internal static byte[] LoadEffectResource(GraphicsDevice graphicsDevice, string name)
  76. {
  77. name = GetResourceName(graphicsDevice, name);
  78. using (Stream stream = typeof(InfiniteGridEffect).Assembly.GetManifestResourceStream(name))
  79. {
  80. byte[] bytecode = new byte[stream.Length];
  81. stream.Read(bytecode, 0, (int)stream.Length);
  82. return bytecode;
  83. }
  84. }
  85. #endregion
  86. #region Public Properties
  87. public Vector4 DiffuseColor
  88. {
  89. get { return diffuseColorParam.GetValueVector4(); }
  90. set { diffuseColorParam.SetValue(value); }
  91. }
  92. public Vector2 TexelSize
  93. {
  94. get { return texelSizeParam.GetValueVector2(); }
  95. set { texelSizeParam.SetValue(value); }
  96. }
  97. public Matrix InvProjection
  98. {
  99. get { return invProjectionParam.GetValueMatrix(); }
  100. set { invProjectionParam.SetValue(value); }
  101. }
  102. public Matrix InvView
  103. {
  104. get { return InvViewParam.GetValueMatrix(); }
  105. set { InvViewParam.SetValue(value); }
  106. }
  107. public Matrix InvPlaneMatrix
  108. {
  109. get { return InvPlaneMatrixParam.GetValueMatrix(); }
  110. set { InvPlaneMatrixParam.SetValue(value); }
  111. }
  112. public Vector3 PlaneNormal
  113. {
  114. get { return planeNormalParam.GetValueVector3(); }
  115. set { planeNormalParam.SetValue(value); }
  116. }
  117. public float PlaneD
  118. {
  119. get { return planeDParam.GetValueSingle(); }
  120. set { planeDParam.SetValue(value); }
  121. }
  122. #endregion
  123. #region Methods
  124. public InfiniteGridEffect(GraphicsDevice graphicsDevice)
  125. : base(graphicsDevice, LoadEffectResource(graphicsDevice, ResourceName))
  126. {
  127. CacheEffectParameters(null);
  128. worldViewProjectionParam.SetValue(Matrix.Identity);
  129. DiffuseColor = new Vector4(1.0f, 0.64f, 0.0f, 1.0f);
  130. }
  131. public InfiniteGridEffect(GraphicsDevice graphicsDevice, byte[] Bytecode): base(graphicsDevice, Bytecode)
  132. {
  133. CacheEffectParameters(null);
  134. worldViewProjectionParam.SetValue(Matrix.Identity);
  135. DiffuseColor = new Vector4(1.0f, 0.64f, 0.0f, 1.0f);
  136. }
  137. protected InfiniteGridEffect(InfiniteGridEffect cloneSource) : base(cloneSource)
  138. {
  139. CacheEffectParameters(cloneSource);
  140. worldViewProjectionParam.SetValue(Matrix.Identity);
  141. DiffuseColor = cloneSource.DiffuseColor;
  142. TexelSize = cloneSource.TexelSize;
  143. InvProjection = cloneSource.InvProjection;
  144. InvView = cloneSource.InvView;
  145. InvPlaneMatrix = cloneSource.InvPlaneMatrix;
  146. //PlaneNormal = cloneSource.PlaneNormal;
  147. //PlaneD = cloneSource.PlaneD;
  148. }
  149. public override Effect Clone()
  150. {
  151. return new InfiniteGridEffect(this);
  152. }
  153. void CacheEffectParameters(InfiniteGridEffect cloneSource)
  154. {
  155. diffuseColorParam = Parameters["DiffuseColor"];
  156. worldViewProjectionParam = Parameters["WorldViewProjection"];
  157. texelSizeParam = Parameters["TexelSize"];
  158. invProjectionParam = Parameters["InvProjection"];
  159. InvViewParam = Parameters["InvView"];
  160. planeNormalParam = Parameters["PlaneNormal"];
  161. planeDParam = Parameters["PlaneD"];
  162. InvPlaneMatrixParam = Parameters["InvPlaneMatrix"];
  163. }
  164. internal void SetDefaultParameters(Viewport viewport, Matrix projection, Matrix view, Matrix EditMatrix)
  165. {
  166. Plane editPlane = new Plane(EditMatrix.Forward, Vector3.Dot(EditMatrix.Forward, EditMatrix.Translation));
  167. if (this.GraphicsDevice.GraphicsProfile == GraphicsProfile.Reach)
  168. TexelSize = new Vector2(1f / viewport.Width, 1f / viewport.Height);
  169. InvProjection = Matrix.Invert(projection);
  170. InvView = Matrix.Invert(view);
  171. InvPlaneMatrix = Matrix.Invert(EditMatrix);
  172. //PlaneNormal = editPlane.Normal;
  173. //PlaneD = editPlane.D;
  174. }
  175. #endregion
  176. }
  177. }