DeferredSpotLightEffect.cs 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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 = ".8";
  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. }
  77. name = name + version;
  78. #endif
  79. Stream stream = GetAssembly(typeof(DeferredPointLightEffect)).GetManifestResourceStream(name);
  80. return stream;
  81. }
  82. private static Assembly GetAssembly(Type type)
  83. {
  84. #if W8_1 || W10
  85. return type.GetTypeInfo().Assembly;
  86. #else
  87. return type.Assembly;
  88. #endif
  89. }
  90. #endregion
  91. #region Public Properties
  92. public RenderTarget2D ColorMap
  93. {
  94. get { return colorMapParam.GetValueTexture2D() as RenderTarget2D; }
  95. set { colorMapParam.SetValue(value); }
  96. }
  97. public RenderTarget2D NormalMap
  98. {
  99. get { return normalMapParam.GetValueTexture2D() as RenderTarget2D; }
  100. set { normalMapParam.SetValue(value); }
  101. }
  102. public RenderTarget2D DepthMap
  103. {
  104. get { return depthMapParam.GetValueTexture2D() as RenderTarget2D; }
  105. set { depthMapParam.SetValue(value); }
  106. }
  107. public Vector2 HalfPixel
  108. {
  109. get { return halfPixelParam.GetValueVector2(); }
  110. set { halfPixelParam.SetValue(value); }
  111. }
  112. public Vector3 LightPosition
  113. {
  114. get { return lightPositionParam.GetValueVector3(); }
  115. set { lightPositionParam.SetValue(value); }
  116. }
  117. public Vector3 Color
  118. {
  119. get { return colorParam.GetValueVector3(); }
  120. set { colorParam.SetValue(value); }
  121. }
  122. public float LightRadius
  123. {
  124. get { return lightRadiusParam.GetValueSingle(); }
  125. set { lightRadiusParam.SetValue(value); }
  126. }
  127. public float LightIntensity
  128. {
  129. get { return lightIntensityParam.GetValueSingle(); }
  130. set { lightIntensityParam.SetValue(value); }
  131. }
  132. public Vector3 CameraPosition
  133. {
  134. get { return cameraPositionParam.GetValueVector3(); }
  135. set { cameraPositionParam.SetValue(value); }
  136. }
  137. public Matrix InvertViewProjection
  138. {
  139. get { return invertViewProjectionParam.GetValueMatrix(); }
  140. set { invertViewProjectionParam.SetValue(value); }
  141. }
  142. public Vector3 LightDirection
  143. {
  144. get { return lightDirectionParam.GetValueVector3(); }
  145. set { lightDirectionParam.SetValue(value); }
  146. }
  147. public float InnerAngleCos
  148. {
  149. get { return innerAngleCosParam.GetValueSingle(); }
  150. set { innerAngleCosParam.SetValue(value); }
  151. }
  152. public float OuterAngleCos
  153. {
  154. get { return outerAngleCosParam.GetValueSingle(); }
  155. set { outerAngleCosParam.SetValue(value); }
  156. }
  157. public Matrix Projection
  158. {
  159. get { return projectionParam.GetValueMatrix(); }
  160. set { projectionParam.SetValue(value); }
  161. }
  162. public Matrix View
  163. {
  164. get { return viewParam.GetValueMatrix(); }
  165. set { viewParam.SetValue(value); }
  166. }
  167. public Matrix World
  168. {
  169. get { return worldParam.GetValueMatrix(); }
  170. set { worldParam.SetValue(value); }
  171. }
  172. #endregion
  173. #region Methods
  174. public DeferredSpotLightEffect(GraphicsDevice graphicsDevice)
  175. : base(graphicsDevice,
  176. #if NETFX_CORE || WP8
  177. LoadEffectResourceStream(resourceName), true
  178. #else
  179. LoadEffectResource(resourceName)
  180. #endif
  181. )
  182. {
  183. CacheEffectParameters(null);
  184. }
  185. public DeferredSpotLightEffect(GraphicsDevice graphicsDevice, byte[] Bytecode): base(graphicsDevice, Bytecode)
  186. {
  187. CacheEffectParameters(null);
  188. }
  189. protected DeferredSpotLightEffect(DeferredSpotLightEffect cloneSource)
  190. : base(cloneSource)
  191. {
  192. CacheEffectParameters(cloneSource);
  193. }
  194. public override Effect Clone()
  195. {
  196. return new DeferredSpotLightEffect(this);
  197. }
  198. void CacheEffectParameters(DeferredSpotLightEffect cloneSource)
  199. {
  200. colorMapParam = Parameters["colorMap"];
  201. normalMapParam = Parameters["normalMap"];
  202. depthMapParam = Parameters["depthMap"];
  203. halfPixelParam = Parameters["halfPixel"];
  204. lightPositionParam = Parameters["lightPosition"];
  205. colorParam = Parameters["Color"];
  206. lightRadiusParam = Parameters["lightRadius"];
  207. lightIntensityParam = Parameters["lightIntensity"];
  208. cameraPositionParam = Parameters["cameraPosition"];
  209. invertViewProjectionParam = Parameters["InvertViewProjection"];
  210. lightDirectionParam = Parameters["lightDirection"];
  211. innerAngleCosParam = Parameters["innerAngleCos"];
  212. outerAngleCosParam = Parameters["outerAngleCos"];
  213. // IEffectMatrices
  214. projectionParam = Parameters["Projection"];
  215. viewParam = Parameters["View"];
  216. worldParam = Parameters["World"];
  217. }
  218. #endregion
  219. }
  220. }