InfiniteGridEffect.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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(string name, GraphicsDevice graphicsDevice)
  38. {
  39. string profileName = (graphicsDevice.GraphicsProfile == GraphicsProfile.Reach) ? ".Reach" : ".HiDef";
  40. String platformName = "";
  41. var version = "";
  42. #if XNA
  43. platformName = ".xna";
  44. #else
  45. platformName = ".dx11.fxo";
  46. // Detect version
  47. version = ".10";
  48. var mgVersion = GetAssembly(typeof(Effect)).GetName().Version;
  49. if (mgVersion.Major == 3)
  50. {
  51. if (mgVersion.Minor == 6)
  52. version = ".8";
  53. if (mgVersion.Minor == 7)
  54. version = ".8";
  55. if (mgVersion.Minor == 8)
  56. {
  57. version = ".9";
  58. if (mgVersion.Build == 1 || mgVersion.Build >= 9100)
  59. version = ".10";
  60. }
  61. }
  62. #endif
  63. return name + profileName + platformName + version;
  64. }
  65. internal static byte[] LoadEffectResource(string name, GraphicsDevice graphicsDevice)
  66. {
  67. name = GetResourceName(name, graphicsDevice);
  68. using (var stream = GetAssembly(typeof(InfiniteGridEffect)).GetManifestResourceStream(name))
  69. {
  70. var bytecode = new byte[stream.Length];
  71. stream.Read(bytecode, 0, (int)stream.Length);
  72. return bytecode;
  73. }
  74. }
  75. private static Assembly GetAssembly(Type type)
  76. {
  77. #if W10
  78. return type.GetTypeInfo().Assembly;
  79. #else
  80. return type.Assembly;
  81. #endif
  82. }
  83. #endregion
  84. #region Public Properties
  85. public Vector4 DiffuseColor
  86. {
  87. get { return diffuseColorParam.GetValueVector4(); }
  88. set { diffuseColorParam.SetValue(value); }
  89. }
  90. public Vector2 TexelSize
  91. {
  92. get { return texelSizeParam.GetValueVector2(); }
  93. set { texelSizeParam.SetValue(value); }
  94. }
  95. public Matrix InvProjection
  96. {
  97. get { return invProjectionParam.GetValueMatrix(); }
  98. set { invProjectionParam.SetValue(value); }
  99. }
  100. public Matrix InvView
  101. {
  102. get { return InvViewParam.GetValueMatrix(); }
  103. set { InvViewParam.SetValue(value); }
  104. }
  105. public Matrix InvPlaneMatrix
  106. {
  107. get { return InvPlaneMatrixParam.GetValueMatrix(); }
  108. set { InvPlaneMatrixParam.SetValue(value); }
  109. }
  110. public Vector3 PlaneNormal
  111. {
  112. get { return planeNormalParam.GetValueVector3(); }
  113. set { planeNormalParam.SetValue(value); }
  114. }
  115. public float PlaneD
  116. {
  117. get { return planeDParam.GetValueSingle(); }
  118. set { planeDParam.SetValue(value); }
  119. }
  120. #endregion
  121. #region Methods
  122. public InfiniteGridEffect(GraphicsDevice graphicsDevice)
  123. : base(graphicsDevice, LoadEffectResource(ResourceName, graphicsDevice))
  124. {
  125. CacheEffectParameters(null);
  126. worldViewProjectionParam.SetValue(Matrix.Identity);
  127. DiffuseColor = new Vector4(1.0f, 0.64f, 0.0f, 1.0f);
  128. }
  129. public InfiniteGridEffect(GraphicsDevice graphicsDevice, byte[] Bytecode): base(graphicsDevice, Bytecode)
  130. {
  131. CacheEffectParameters(null);
  132. worldViewProjectionParam.SetValue(Matrix.Identity);
  133. DiffuseColor = new Vector4(1.0f, 0.64f, 0.0f, 1.0f);
  134. }
  135. protected InfiniteGridEffect(InfiniteGridEffect cloneSource) : base(cloneSource)
  136. {
  137. CacheEffectParameters(cloneSource);
  138. worldViewProjectionParam.SetValue(Matrix.Identity);
  139. DiffuseColor = cloneSource.DiffuseColor;
  140. TexelSize = cloneSource.TexelSize;
  141. InvProjection = cloneSource.InvProjection;
  142. InvView = cloneSource.InvView;
  143. InvPlaneMatrix = cloneSource.InvPlaneMatrix;
  144. PlaneNormal = cloneSource.PlaneNormal;
  145. PlaneD = cloneSource.PlaneD;
  146. }
  147. public override Effect Clone()
  148. {
  149. return new InfiniteGridEffect(this);
  150. }
  151. void CacheEffectParameters(InfiniteGridEffect cloneSource)
  152. {
  153. diffuseColorParam = Parameters["DiffuseColor"];
  154. worldViewProjectionParam = Parameters["WorldViewProjection"];
  155. texelSizeParam = Parameters["TexelSize"];
  156. invProjectionParam = Parameters["InvProjection"];
  157. InvViewParam = Parameters["InvView"];
  158. planeNormalParam = Parameters["PlaneNormal"];
  159. planeDParam = Parameters["PlaneD"];
  160. InvPlaneMatrixParam = Parameters["InvPlaneMatrix"];
  161. }
  162. internal void SetDefaultParameters(Viewport viewport, Matrix projection, Matrix view, Matrix EditMatrix)
  163. {
  164. var editPlane = new Plane(EditMatrix.Forward, Vector3.Dot(EditMatrix.Forward, EditMatrix.Translation));
  165. TexelSize = new Vector2(1f / viewport.Width, 1f / viewport.Height);
  166. InvProjection = Matrix.Invert(projection);
  167. InvView = Matrix.Invert(view);
  168. InvPlaneMatrix = Matrix.Invert(EditMatrix);
  169. PlaneNormal = editPlane.Normal;
  170. PlaneD = editPlane.D;
  171. }
  172. #endregion
  173. }
  174. }