DeferredSpotLightEffect.cs 7.8 KB

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