DeferredPointLightEffect.cs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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 (Stream stream = GetAssembly(typeof(DeferredPointLightEffect)).GetManifestResourceStream(name))
  47. {
  48. byte[] 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. string version = "";
  57. #if XNA
  58. platformName = ".xna.WinReach";
  59. #else
  60. switch (graphicsDevice.Adapter.Backend)
  61. {
  62. case GraphicsBackend.DirectX11:
  63. platformName = ".dx11.fxo";
  64. break;
  65. case GraphicsBackend.OpenGL:
  66. case GraphicsBackend.GLES:
  67. case GraphicsBackend.WebGL:
  68. platformName = ".ogl.fxo";
  69. break;
  70. default:
  71. throw new NotSupportedException("platform");
  72. }
  73. // Detect version
  74. version = ".10";
  75. Version kniVersion = GetAssembly(typeof(Effect)).GetName().Version;
  76. if (kniVersion.Major == 3)
  77. {
  78. if (kniVersion.Minor == 9)
  79. {
  80. version = ".10";
  81. }
  82. if (kniVersion.Minor == 10)
  83. {
  84. version = ".10";
  85. }
  86. }
  87. #endif
  88. return name + platformName + version;
  89. }
  90. private static Assembly GetAssembly(Type type)
  91. {
  92. #if W10
  93. return type.GetTypeInfo().Assembly;
  94. #else
  95. return type.Assembly;
  96. #endif
  97. }
  98. #endregion
  99. #region Public Properties
  100. public RenderTarget2D ColorMap
  101. {
  102. get { return colorMapParam.GetValueTexture2D() as RenderTarget2D; }
  103. set { colorMapParam.SetValue(value); }
  104. }
  105. public RenderTarget2D NormalMap
  106. {
  107. get { return normalMapParam.GetValueTexture2D() as RenderTarget2D; }
  108. set { normalMapParam.SetValue(value); }
  109. }
  110. public RenderTarget2D DepthMap
  111. {
  112. get { return depthMapParam.GetValueTexture2D() as RenderTarget2D; }
  113. set { depthMapParam.SetValue(value); }
  114. }
  115. public Vector2 HalfPixel
  116. {
  117. get { return halfPixelParam.GetValueVector2(); }
  118. set { halfPixelParam.SetValue(value); }
  119. }
  120. public Vector3 LightPosition
  121. {
  122. get { return lightPositionParam.GetValueVector3(); }
  123. set { lightPositionParam.SetValue(value); }
  124. }
  125. public Vector3 Color
  126. {
  127. get { return colorParam.GetValueVector3(); }
  128. set { colorParam.SetValue(value); }
  129. }
  130. public float LightRadius
  131. {
  132. get { return lightRadiusParam.GetValueSingle(); }
  133. set { lightRadiusParam.SetValue(value); }
  134. }
  135. public float LightIntensity
  136. {
  137. get { return lightIntensityParam.GetValueSingle(); }
  138. set { lightIntensityParam.SetValue(value); }
  139. }
  140. public Vector3 CameraPosition
  141. {
  142. get { return cameraPositionParam.GetValueVector3(); }
  143. set { cameraPositionParam.SetValue(value); }
  144. }
  145. public Matrix InvertViewProjection
  146. {
  147. get { return invertViewProjectionParam.GetValueMatrix(); }
  148. set { invertViewProjectionParam.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 DeferredPointLightEffect(GraphicsDevice graphicsDevice)
  168. : base(graphicsDevice, LoadEffectResource(graphicsDevice, ResourceName))
  169. {
  170. CacheEffectParameters(null);
  171. }
  172. public DeferredPointLightEffect(GraphicsDevice graphicsDevice, byte[] Bytecode): base(graphicsDevice, Bytecode)
  173. {
  174. CacheEffectParameters(null);
  175. }
  176. protected DeferredPointLightEffect(DeferredPointLightEffect cloneSource)
  177. : base(cloneSource)
  178. {
  179. CacheEffectParameters(cloneSource);
  180. }
  181. public override Effect Clone()
  182. {
  183. return new DeferredPointLightEffect(this);
  184. }
  185. void CacheEffectParameters(DeferredPointLightEffect cloneSource)
  186. {
  187. colorMapParam = Parameters["colorMap"];
  188. normalMapParam = Parameters["normalMap"];
  189. depthMapParam = Parameters["depthMap"];
  190. halfPixelParam = Parameters["halfPixel"];
  191. lightPositionParam = Parameters["lightPosition"];
  192. colorParam = Parameters["Color"];
  193. lightRadiusParam = Parameters["lightRadius"];
  194. lightIntensityParam = Parameters["lightIntensity"];
  195. cameraPositionParam = Parameters["cameraPosition"];
  196. invertViewProjectionParam = Parameters["InvertViewProjection"];
  197. // IEffectMatrices
  198. projectionParam = Parameters["Projection"];
  199. viewParam = Parameters["View"];
  200. worldParam = Parameters["World"];
  201. }
  202. #endregion
  203. }
  204. }