DeferredSpotLightEffect.cs 8.1 KB

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