2
0

InfiniteGridEffect.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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 = GetAssembly(typeof(Effect)).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 = GetAssembly(typeof(InfiniteGridEffect)).GetManifestResourceStream(name))
  79. {
  80. byte[] bytecode = new byte[stream.Length];
  81. stream.Read(bytecode, 0, (int)stream.Length);
  82. return bytecode;
  83. }
  84. }
  85. private static Assembly GetAssembly(Type type)
  86. {
  87. #if W10
  88. return type.GetTypeInfo().Assembly;
  89. #else
  90. return type.Assembly;
  91. #endif
  92. }
  93. #endregion
  94. #region Public Properties
  95. public Vector4 DiffuseColor
  96. {
  97. get { return diffuseColorParam.GetValueVector4(); }
  98. set { diffuseColorParam.SetValue(value); }
  99. }
  100. public Vector2 TexelSize
  101. {
  102. get { return texelSizeParam.GetValueVector2(); }
  103. set { texelSizeParam.SetValue(value); }
  104. }
  105. public Matrix InvProjection
  106. {
  107. get { return invProjectionParam.GetValueMatrix(); }
  108. set { invProjectionParam.SetValue(value); }
  109. }
  110. public Matrix InvView
  111. {
  112. get { return InvViewParam.GetValueMatrix(); }
  113. set { InvViewParam.SetValue(value); }
  114. }
  115. public Matrix InvPlaneMatrix
  116. {
  117. get { return InvPlaneMatrixParam.GetValueMatrix(); }
  118. set { InvPlaneMatrixParam.SetValue(value); }
  119. }
  120. public Vector3 PlaneNormal
  121. {
  122. get { return planeNormalParam.GetValueVector3(); }
  123. set { planeNormalParam.SetValue(value); }
  124. }
  125. public float PlaneD
  126. {
  127. get { return planeDParam.GetValueSingle(); }
  128. set { planeDParam.SetValue(value); }
  129. }
  130. #endregion
  131. #region Methods
  132. public InfiniteGridEffect(GraphicsDevice graphicsDevice)
  133. : base(graphicsDevice, LoadEffectResource(graphicsDevice, ResourceName))
  134. {
  135. CacheEffectParameters(null);
  136. worldViewProjectionParam.SetValue(Matrix.Identity);
  137. DiffuseColor = new Vector4(1.0f, 0.64f, 0.0f, 1.0f);
  138. }
  139. public InfiniteGridEffect(GraphicsDevice graphicsDevice, byte[] Bytecode): base(graphicsDevice, Bytecode)
  140. {
  141. CacheEffectParameters(null);
  142. worldViewProjectionParam.SetValue(Matrix.Identity);
  143. DiffuseColor = new Vector4(1.0f, 0.64f, 0.0f, 1.0f);
  144. }
  145. protected InfiniteGridEffect(InfiniteGridEffect cloneSource) : base(cloneSource)
  146. {
  147. CacheEffectParameters(cloneSource);
  148. worldViewProjectionParam.SetValue(Matrix.Identity);
  149. DiffuseColor = cloneSource.DiffuseColor;
  150. TexelSize = cloneSource.TexelSize;
  151. InvProjection = cloneSource.InvProjection;
  152. InvView = cloneSource.InvView;
  153. InvPlaneMatrix = cloneSource.InvPlaneMatrix;
  154. //PlaneNormal = cloneSource.PlaneNormal;
  155. //PlaneD = cloneSource.PlaneD;
  156. }
  157. public override Effect Clone()
  158. {
  159. return new InfiniteGridEffect(this);
  160. }
  161. void CacheEffectParameters(InfiniteGridEffect cloneSource)
  162. {
  163. diffuseColorParam = Parameters["DiffuseColor"];
  164. worldViewProjectionParam = Parameters["WorldViewProjection"];
  165. texelSizeParam = Parameters["TexelSize"];
  166. invProjectionParam = Parameters["InvProjection"];
  167. InvViewParam = Parameters["InvView"];
  168. planeNormalParam = Parameters["PlaneNormal"];
  169. planeDParam = Parameters["PlaneD"];
  170. InvPlaneMatrixParam = Parameters["InvPlaneMatrix"];
  171. }
  172. internal void SetDefaultParameters(Viewport viewport, Matrix projection, Matrix view, Matrix EditMatrix)
  173. {
  174. Plane editPlane = new Plane(EditMatrix.Forward, Vector3.Dot(EditMatrix.Forward, EditMatrix.Translation));
  175. if (this.GraphicsDevice.GraphicsProfile == GraphicsProfile.Reach)
  176. TexelSize = new Vector2(1f / viewport.Width, 1f / viewport.Height);
  177. InvProjection = Matrix.Invert(projection);
  178. InvView = Matrix.Invert(view);
  179. InvPlaneMatrix = Matrix.Invert(EditMatrix);
  180. //PlaneNormal = editPlane.Normal;
  181. //PlaneD = editPlane.D;
  182. }
  183. #endregion
  184. }
  185. }