DeferredSpotLightEffect.cs 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. #region License
  2. // Copyright 2014-2016 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 DeferredSpotLightEffect : Effect, IEffectMatrices
  24. {
  25. #region Effect Parameters
  26. EffectParameter colorMapParam;
  27. EffectParameter normalMapParam;
  28. EffectParameter depthMapParam;
  29. EffectParameter halfPixelParam;
  30. EffectParameter lightPositionParam;
  31. EffectParameter colorParam;
  32. EffectParameter lightRadiusParam;
  33. EffectParameter lightIntensityParam;
  34. EffectParameter cameraPositionParam;
  35. EffectParameter invertViewProjectionParam;
  36. EffectParameter lightDirectionParam;
  37. EffectParameter innerAngleCosParam;
  38. EffectParameter outerAngleCosParam;
  39. // IEffectMatrices
  40. EffectParameter projectionParam;
  41. EffectParameter viewParam;
  42. EffectParameter worldParam;
  43. #endregion
  44. #region Fields
  45. static readonly String ResourceName = "nkast.Aether.Shaders.Resources.DeferredSpotLight";
  46. internal static byte[] LoadEffectResource(string name)
  47. {
  48. name = GetResourceName(name);
  49. using (var stream = GetAssembly(typeof(DeferredPointLightEffect)).GetManifestResourceStream(name))
  50. {
  51. var bytecode = new byte[stream.Length];
  52. stream.Read(bytecode, 0, (int)stream.Length);
  53. return bytecode;
  54. }
  55. }
  56. private static string GetResourceName(string name)
  57. {
  58. String platformName = "";
  59. var version = "";
  60. #if XNA
  61. platformName = ".xna.WinReach";
  62. #else
  63. switch (MonoGame.Framework.Utilities.PlatformInfo.GraphicsBackend)
  64. {
  65. case MonoGame.Framework.Utilities.GraphicsBackend.DirectX:
  66. platformName = ".dx11.fxo";
  67. break;
  68. case MonoGame.Framework.Utilities.GraphicsBackend.OpenGL:
  69. platformName = ".ogl.fxo";
  70. break;
  71. default:
  72. throw new NotSupportedException("platform");
  73. }
  74. // Detect version
  75. version = ".10";
  76. var mgVersion = GetAssembly(typeof(Effect)).GetName().Version;
  77. if (mgVersion.Major == 3)
  78. {
  79. if (mgVersion.Minor == 6)
  80. version = ".8";
  81. if (mgVersion.Minor == 7)
  82. version = ".8";
  83. if (mgVersion.Minor == 8)
  84. {
  85. version = ".9";
  86. if (mgVersion.Build == 1 || mgVersion.Build >= 9100)
  87. version = ".10";
  88. }
  89. }
  90. #endif
  91. return name + platformName + version;
  92. }
  93. private static Assembly GetAssembly(Type type)
  94. {
  95. #if W10
  96. return type.GetTypeInfo().Assembly;
  97. #else
  98. return type.Assembly;
  99. #endif
  100. }
  101. #endregion
  102. #region Public Properties
  103. public RenderTarget2D ColorMap
  104. {
  105. get { return colorMapParam.GetValueTexture2D() as RenderTarget2D; }
  106. set { colorMapParam.SetValue(value); }
  107. }
  108. public RenderTarget2D NormalMap
  109. {
  110. get { return normalMapParam.GetValueTexture2D() as RenderTarget2D; }
  111. set { normalMapParam.SetValue(value); }
  112. }
  113. public RenderTarget2D DepthMap
  114. {
  115. get { return depthMapParam.GetValueTexture2D() as RenderTarget2D; }
  116. set { depthMapParam.SetValue(value); }
  117. }
  118. public Vector2 HalfPixel
  119. {
  120. get { return halfPixelParam.GetValueVector2(); }
  121. set { halfPixelParam.SetValue(value); }
  122. }
  123. public Vector3 LightPosition
  124. {
  125. get { return lightPositionParam.GetValueVector3(); }
  126. set { lightPositionParam.SetValue(value); }
  127. }
  128. public Vector3 Color
  129. {
  130. get { return colorParam.GetValueVector3(); }
  131. set { colorParam.SetValue(value); }
  132. }
  133. public float LightRadius
  134. {
  135. get { return lightRadiusParam.GetValueSingle(); }
  136. set { lightRadiusParam.SetValue(value); }
  137. }
  138. public float LightIntensity
  139. {
  140. get { return lightIntensityParam.GetValueSingle(); }
  141. set { lightIntensityParam.SetValue(value); }
  142. }
  143. public Vector3 CameraPosition
  144. {
  145. get { return cameraPositionParam.GetValueVector3(); }
  146. set { cameraPositionParam.SetValue(value); }
  147. }
  148. public Matrix InvertViewProjection
  149. {
  150. get { return invertViewProjectionParam.GetValueMatrix(); }
  151. set { invertViewProjectionParam.SetValue(value); }
  152. }
  153. public Vector3 LightDirection
  154. {
  155. get { return lightDirectionParam.GetValueVector3(); }
  156. set { lightDirectionParam.SetValue(value); }
  157. }
  158. public float InnerAngleCos
  159. {
  160. get { return innerAngleCosParam.GetValueSingle(); }
  161. set { innerAngleCosParam.SetValue(value); }
  162. }
  163. public float OuterAngleCos
  164. {
  165. get { return outerAngleCosParam.GetValueSingle(); }
  166. set { outerAngleCosParam.SetValue(value); }
  167. }
  168. public Matrix Projection
  169. {
  170. get { return projectionParam.GetValueMatrix(); }
  171. set { projectionParam.SetValue(value); }
  172. }
  173. public Matrix View
  174. {
  175. get { return viewParam.GetValueMatrix(); }
  176. set { viewParam.SetValue(value); }
  177. }
  178. public Matrix World
  179. {
  180. get { return worldParam.GetValueMatrix(); }
  181. set { worldParam.SetValue(value); }
  182. }
  183. #endregion
  184. #region Methods
  185. public DeferredSpotLightEffect(GraphicsDevice graphicsDevice)
  186. : base(graphicsDevice, LoadEffectResource(ResourceName))
  187. {
  188. CacheEffectParameters(null);
  189. }
  190. public DeferredSpotLightEffect(GraphicsDevice graphicsDevice, byte[] Bytecode): base(graphicsDevice, Bytecode)
  191. {
  192. CacheEffectParameters(null);
  193. }
  194. protected DeferredSpotLightEffect(DeferredSpotLightEffect cloneSource)
  195. : base(cloneSource)
  196. {
  197. CacheEffectParameters(cloneSource);
  198. }
  199. public override Effect Clone()
  200. {
  201. return new DeferredSpotLightEffect(this);
  202. }
  203. void CacheEffectParameters(DeferredSpotLightEffect cloneSource)
  204. {
  205. colorMapParam = Parameters["colorMap"];
  206. normalMapParam = Parameters["normalMap"];
  207. depthMapParam = Parameters["depthMap"];
  208. halfPixelParam = Parameters["halfPixel"];
  209. lightPositionParam = Parameters["lightPosition"];
  210. colorParam = Parameters["Color"];
  211. lightRadiusParam = Parameters["lightRadius"];
  212. lightIntensityParam = Parameters["lightIntensity"];
  213. cameraPositionParam = Parameters["cameraPosition"];
  214. invertViewProjectionParam = Parameters["InvertViewProjection"];
  215. lightDirectionParam = Parameters["lightDirection"];
  216. innerAngleCosParam = Parameters["innerAngleCos"];
  217. outerAngleCosParam = Parameters["outerAngleCos"];
  218. // IEffectMatrices
  219. projectionParam = Parameters["Projection"];
  220. viewParam = Parameters["View"];
  221. worldParam = Parameters["World"];
  222. }
  223. #endregion
  224. }
  225. }