InfiniteGridEffect.cs 6.7 KB

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