DeferredPointLightEffect.cs 7.2 KB

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