DeferredPointLightEffect.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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 nkast.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. static readonly String ResourceName = "nkast.Aether.Shaders.Resources.DeferredPointLight";
  43. internal static byte[] LoadEffectResource(GraphicsDevice graphicsDevice, string name)
  44. {
  45. name = GetResourceName(graphicsDevice, name);
  46. using (var stream = GetAssembly(typeof(DeferredPointLightEffect)).GetManifestResourceStream(name))
  47. {
  48. var bytecode = new byte[stream.Length];
  49. stream.Read(bytecode, 0, (int)stream.Length);
  50. return bytecode;
  51. }
  52. }
  53. private static string GetResourceName(GraphicsDevice graphicsDevice, string name)
  54. {
  55. String platformName = "";
  56. var version = "";
  57. #if XNA
  58. platformName = ".xna.WinReach";
  59. #else
  60. switch (MonoGame.Framework.Utilities.PlatformInfo.GraphicsBackend)
  61. {
  62. case MonoGame.Framework.Utilities.GraphicsBackend.DirectX:
  63. platformName = ".dx11.fxo";
  64. break;
  65. case MonoGame.Framework.Utilities.GraphicsBackend.OpenGL:
  66. platformName = ".ogl.fxo";
  67. break;
  68. default:
  69. throw new NotSupportedException("platform");
  70. }
  71. // Detect version
  72. version = ".10";
  73. var kniVersion = GetAssembly(typeof(Effect)).GetName().Version;
  74. if (kniVersion.Major == 3)
  75. {
  76. if (kniVersion.Minor == 10)
  77. {
  78. version = ".10";
  79. }
  80. }
  81. #endif
  82. return name + platformName + version;
  83. }
  84. private static Assembly GetAssembly(Type type)
  85. {
  86. #if 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 Matrix Projection
  145. {
  146. get { return projectionParam.GetValueMatrix(); }
  147. set { projectionParam.SetValue(value); }
  148. }
  149. public Matrix View
  150. {
  151. get { return viewParam.GetValueMatrix(); }
  152. set { viewParam.SetValue(value); }
  153. }
  154. public Matrix World
  155. {
  156. get { return worldParam.GetValueMatrix(); }
  157. set { worldParam.SetValue(value); }
  158. }
  159. #endregion
  160. #region Methods
  161. public DeferredPointLightEffect(GraphicsDevice graphicsDevice)
  162. : base(graphicsDevice, LoadEffectResource(graphicsDevice, ResourceName))
  163. {
  164. CacheEffectParameters(null);
  165. }
  166. public DeferredPointLightEffect(GraphicsDevice graphicsDevice, byte[] Bytecode): base(graphicsDevice, Bytecode)
  167. {
  168. CacheEffectParameters(null);
  169. }
  170. protected DeferredPointLightEffect(DeferredPointLightEffect cloneSource)
  171. : base(cloneSource)
  172. {
  173. CacheEffectParameters(cloneSource);
  174. }
  175. public override Effect Clone()
  176. {
  177. return new DeferredPointLightEffect(this);
  178. }
  179. void CacheEffectParameters(DeferredPointLightEffect cloneSource)
  180. {
  181. colorMapParam = Parameters["colorMap"];
  182. normalMapParam = Parameters["normalMap"];
  183. depthMapParam = Parameters["depthMap"];
  184. halfPixelParam = Parameters["halfPixel"];
  185. lightPositionParam = Parameters["lightPosition"];
  186. colorParam = Parameters["Color"];
  187. lightRadiusParam = Parameters["lightRadius"];
  188. lightIntensityParam = Parameters["lightIntensity"];
  189. cameraPositionParam = Parameters["cameraPosition"];
  190. invertViewProjectionParam = Parameters["InvertViewProjection"];
  191. // IEffectMatrices
  192. projectionParam = Parameters["Projection"];
  193. viewParam = Parameters["View"];
  194. worldParam = Parameters["World"];
  195. }
  196. #endregion
  197. }
  198. }