DeferredPointLightEffect.cs 7.1 KB

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