/RainG2Engine/Effects/unused/DualTextureEffect.cs

http://raing2engine.codeplex.com · C# · 329 lines · 192 code · 74 blank · 63 comment · 10 complexity · f073065ca0cdc07b55b8bf4b014006b0 MD5 · raw file

  1. #region File Description
  2. //-----------------------------------------------------------------------------
  3. // DualTextureEffect.cs
  4. //
  5. // Microsoft XNA Community Game Platform
  6. // Copyright (C) Microsoft Corporation. All rights reserved.
  7. //-----------------------------------------------------------------------------
  8. #endregion
  9. #region Using Statements
  10. using Microsoft.Xna.Framework;
  11. using Microsoft.Xna.Framework.Graphics;
  12. #endregion
  13. namespace StockEffects
  14. {
  15. /// <summary>
  16. /// Built-in effect that supports two-layer multitexturing.
  17. /// </summary>
  18. public class DualTextureEffect : Effect, IEffectMatrices, IEffectFog
  19. {
  20. #region Effect Parameters
  21. EffectParameter textureParam;
  22. EffectParameter texture2Param;
  23. EffectParameter diffuseColorParam;
  24. EffectParameter fogColorParam;
  25. EffectParameter fogVectorParam;
  26. EffectParameter worldViewProjParam;
  27. EffectParameter shaderIndexParam;
  28. #endregion
  29. #region Fields
  30. bool fogEnabled;
  31. bool vertexColorEnabled;
  32. Matrix world = Matrix.Identity;
  33. Matrix view = Matrix.Identity;
  34. Matrix projection = Matrix.Identity;
  35. Matrix worldView;
  36. Vector3 diffuseColor = Vector3.One;
  37. float alpha = 1;
  38. float fogStart = 0;
  39. float fogEnd = 1;
  40. EffectDirtyFlags dirtyFlags = EffectDirtyFlags.All;
  41. #endregion
  42. #region Public Properties
  43. /// <summary>
  44. /// Gets or sets the world matrix.
  45. /// </summary>
  46. public Matrix World
  47. {
  48. get { return world; }
  49. set
  50. {
  51. world = value;
  52. dirtyFlags |= EffectDirtyFlags.WorldViewProj | EffectDirtyFlags.Fog;
  53. }
  54. }
  55. /// <summary>
  56. /// Gets or sets the view matrix.
  57. /// </summary>
  58. public Matrix View
  59. {
  60. get { return view; }
  61. set
  62. {
  63. view = value;
  64. dirtyFlags |= EffectDirtyFlags.WorldViewProj | EffectDirtyFlags.Fog;
  65. }
  66. }
  67. /// <summary>
  68. /// Gets or sets the projection matrix.
  69. /// </summary>
  70. public Matrix Projection
  71. {
  72. get { return projection; }
  73. set
  74. {
  75. projection = value;
  76. dirtyFlags |= EffectDirtyFlags.WorldViewProj;
  77. }
  78. }
  79. /// <summary>
  80. /// Gets or sets the material diffuse color (range 0 to 1).
  81. /// </summary>
  82. public Vector3 DiffuseColor
  83. {
  84. get { return diffuseColor; }
  85. set
  86. {
  87. diffuseColor = value;
  88. dirtyFlags |= EffectDirtyFlags.MaterialColor;
  89. }
  90. }
  91. /// <summary>
  92. /// Gets or sets the material alpha.
  93. /// </summary>
  94. public float Alpha
  95. {
  96. get { return alpha; }
  97. set
  98. {
  99. alpha = value;
  100. dirtyFlags |= EffectDirtyFlags.MaterialColor;
  101. }
  102. }
  103. /// <summary>
  104. /// Gets or sets the fog enable flag.
  105. /// </summary>
  106. public bool FogEnabled
  107. {
  108. get { return fogEnabled; }
  109. set
  110. {
  111. if (fogEnabled != value)
  112. {
  113. fogEnabled = value;
  114. dirtyFlags |= EffectDirtyFlags.ShaderIndex | EffectDirtyFlags.FogEnable;
  115. }
  116. }
  117. }
  118. /// <summary>
  119. /// Gets or sets the fog start distance.
  120. /// </summary>
  121. public float FogStart
  122. {
  123. get { return fogStart; }
  124. set
  125. {
  126. fogStart = value;
  127. dirtyFlags |= EffectDirtyFlags.Fog;
  128. }
  129. }
  130. /// <summary>
  131. /// Gets or sets the fog end distance.
  132. /// </summary>
  133. public float FogEnd
  134. {
  135. get { return fogEnd; }
  136. set
  137. {
  138. fogEnd = value;
  139. dirtyFlags |= EffectDirtyFlags.Fog;
  140. }
  141. }
  142. /// <summary>
  143. /// Gets or sets the fog color.
  144. /// </summary>
  145. public Vector3 FogColor
  146. {
  147. get { return fogColorParam.GetValueVector3(); }
  148. set { fogColorParam.SetValue(value); }
  149. }
  150. /// <summary>
  151. /// Gets or sets the current base texture.
  152. /// </summary>
  153. public Texture2D Texture
  154. {
  155. get { return textureParam.GetValueTexture2D(); }
  156. set { textureParam.SetValue(value); }
  157. }
  158. /// <summary>
  159. /// Gets or sets the current overlay texture.
  160. /// </summary>
  161. public Texture2D Texture2
  162. {
  163. get { return texture2Param.GetValueTexture2D(); }
  164. set { texture2Param.SetValue(value); }
  165. }
  166. /// <summary>
  167. /// Gets or sets whether vertex color is enabled.
  168. /// </summary>
  169. public bool VertexColorEnabled
  170. {
  171. get { return vertexColorEnabled; }
  172. set
  173. {
  174. if (vertexColorEnabled != value)
  175. {
  176. vertexColorEnabled = value;
  177. dirtyFlags |= EffectDirtyFlags.ShaderIndex;
  178. }
  179. }
  180. }
  181. #endregion
  182. #region Methods
  183. /// <summary>
  184. /// Creates a new DualTextureEffect with default parameter settings.
  185. /// </summary>
  186. public DualTextureEffect(GraphicsDevice device)
  187. : base(device, Resources.DualTextureEffect)
  188. {
  189. CacheEffectParameters();
  190. }
  191. /// <summary>
  192. /// Creates a new DualTextureEffect by cloning parameter settings from an existing instance.
  193. /// </summary>
  194. protected DualTextureEffect(DualTextureEffect cloneSource)
  195. : base(cloneSource)
  196. {
  197. CacheEffectParameters();
  198. fogEnabled = cloneSource.fogEnabled;
  199. vertexColorEnabled = cloneSource.vertexColorEnabled;
  200. world = cloneSource.world;
  201. view = cloneSource.view;
  202. projection = cloneSource.projection;
  203. diffuseColor = cloneSource.diffuseColor;
  204. alpha = cloneSource.alpha;
  205. fogStart = cloneSource.fogStart;
  206. fogEnd = cloneSource.fogEnd;
  207. }
  208. /// <summary>
  209. /// Creates a clone of the current DualTextureEffect instance.
  210. /// </summary>
  211. public override Effect Clone()
  212. {
  213. return new DualTextureEffect(this);
  214. }
  215. /// <summary>
  216. /// Looks up shortcut references to our effect parameters.
  217. /// </summary>
  218. void CacheEffectParameters()
  219. {
  220. textureParam = Parameters["Texture"];
  221. texture2Param = Parameters["Texture2"];
  222. diffuseColorParam = Parameters["DiffuseColor"];
  223. fogColorParam = Parameters["FogColor"];
  224. fogVectorParam = Parameters["FogVector"];
  225. worldViewProjParam = Parameters["WorldViewProj"];
  226. shaderIndexParam = Parameters["ShaderIndex"];
  227. }
  228. /// <summary>
  229. /// Lazily computes derived parameter values immediately before applying the effect.
  230. /// </summary>
  231. protected override void OnApply()
  232. {
  233. // Recompute the world+view+projection matrix or fog vector?
  234. dirtyFlags = EffectHelpers.SetWorldViewProjAndFog(dirtyFlags, ref world, ref view, ref projection, ref worldView, fogEnabled, fogStart, fogEnd, worldViewProjParam, fogVectorParam);
  235. // Recompute the diffuse/alpha material color parameter?
  236. if ((dirtyFlags & EffectDirtyFlags.MaterialColor) != 0)
  237. {
  238. diffuseColorParam.SetValue(new Vector4(diffuseColor * alpha, alpha));
  239. dirtyFlags &= ~EffectDirtyFlags.MaterialColor;
  240. }
  241. // Recompute the shader index?
  242. if ((dirtyFlags & EffectDirtyFlags.ShaderIndex) != 0)
  243. {
  244. int shaderIndex = 0;
  245. if (!fogEnabled)
  246. shaderIndex += 1;
  247. if (vertexColorEnabled)
  248. shaderIndex += 2;
  249. shaderIndexParam.SetValue(shaderIndex);
  250. dirtyFlags &= ~EffectDirtyFlags.ShaderIndex;
  251. }
  252. }
  253. #endregion
  254. }
  255. }