DefaultEffect.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. using System.Collections.Specialized;
  2. using Microsoft.Xna.Framework;
  3. using Microsoft.Xna.Framework.Graphics;
  4. namespace MonoGame.Extended.Graphics.Effects
  5. {
  6. /// <summary>
  7. /// An <see cref="Effect" /> that allows objects, within a 3D context, to be represented on a 2D monitor.
  8. /// </summary>
  9. /// <seealso cref="MatrixChainEffect" />
  10. /// <seealso cref="ITextureEffect" />
  11. public class DefaultEffect : MatrixChainEffect, ITextureEffect
  12. {
  13. /// <summary>
  14. /// The bitmask for use with <see cref="MatrixChainEffect.Flags" /> indicating wether <see cref="Texture" /> has
  15. /// changed in the last frame.
  16. /// </summary>
  17. protected static int DirtyTextureBitMask = BitVector32.CreateMask(UseDefaultProjectionBitMask);
  18. /// <summary>
  19. /// The bitmask for use with <see cref="MatrixChainEffect.Flags" /> indicating wether the underlying vertex shader and
  20. /// fragment (pixel) shaders have changed to one of the pre-defined shaders in the last frame.
  21. /// </summary>
  22. protected static int DirtyShaderIndexBitMask = BitVector32.CreateMask(DirtyTextureBitMask);
  23. /// <summary>
  24. /// The bitmask for use with <see cref="MatrixChainEffect.Flags" /> indicating wether the material color has changed in
  25. /// the last frame.
  26. /// </summary>
  27. public static int DirtyMaterialColorBitMask = BitVector32.CreateMask(DirtyShaderIndexBitMask);
  28. private Texture2D _texture;
  29. private EffectParameter _textureParameter;
  30. private float _alpha = 1;
  31. private Color _diffuseColor = Color.White;
  32. private EffectParameter _diffuseColorParameter;
  33. private bool _textureEnabled;
  34. private bool _vertexColorEnabled;
  35. /// <summary>
  36. /// Gets or sets the material <see cref="Texture2D" />.
  37. /// </summary>
  38. /// <value>
  39. /// The material <see cref="Texture2D" />.
  40. /// </value>
  41. public Texture2D Texture
  42. {
  43. get { return _texture; }
  44. set
  45. {
  46. _texture = value;
  47. Flags[DirtyTextureBitMask] = true;
  48. }
  49. }
  50. /// <summary>
  51. /// Gets or sets the material color alpha.
  52. /// </summary>
  53. /// <remarks>
  54. /// <para>
  55. /// The alpha channel uses the premultiplied (associated) representation. This means that the RGB components of a
  56. /// color represent
  57. /// the color of the object of pixel, adjusted for its opacity by multiplication of <see cref="Alpha" />.
  58. /// </para>
  59. /// </remarks>
  60. public float Alpha
  61. {
  62. get { return _alpha; }
  63. set
  64. {
  65. _alpha = value;
  66. Flags[DirtyMaterialColorBitMask] = true;
  67. }
  68. }
  69. /// <summary>
  70. /// Gets or sets whether texturing is enabled.
  71. /// </summary>
  72. public bool TextureEnabled
  73. {
  74. get { return _textureEnabled; }
  75. set
  76. {
  77. if (_textureEnabled == value)
  78. return;
  79. _textureEnabled = value;
  80. Flags[DirtyShaderIndexBitMask] = true;
  81. }
  82. }
  83. /// <summary>
  84. /// Gets or sets whether vertex color is enabled.
  85. /// </summary>
  86. public bool VertexColorEnabled
  87. {
  88. get { return _vertexColorEnabled; }
  89. set
  90. {
  91. if (_vertexColorEnabled == value)
  92. return;
  93. _vertexColorEnabled = value;
  94. Flags[DirtyShaderIndexBitMask] = true;
  95. }
  96. }
  97. /// <summary>
  98. /// Initializes a new instance of the <see cref="DefaultEffect" /> class.
  99. /// </summary>
  100. /// <param name="graphicsDevice">The graphics device.</param>
  101. public DefaultEffect(GraphicsDevice graphicsDevice)
  102. : base(graphicsDevice, EffectResource.DefaultEffect.Bytecode)
  103. {
  104. Initialize();
  105. }
  106. /// <summary>
  107. /// Initializes a new instance of the <see cref="DefaultEffect" /> class.
  108. /// </summary>
  109. /// <param name="graphicsDevice">The graphics device.</param>
  110. /// <param name="byteCode">The byte code of the shader program.</param>
  111. public DefaultEffect(GraphicsDevice graphicsDevice, byte[] byteCode)
  112. : base(graphicsDevice, byteCode)
  113. {
  114. Initialize();
  115. }
  116. /// <summary>
  117. /// Initializes a new instance of the <see cref="DefaultEffect" /> class.
  118. /// </summary>
  119. /// <param name="cloneSource">The clone source.</param>
  120. public DefaultEffect(Effect cloneSource)
  121. : base(cloneSource)
  122. {
  123. Initialize();
  124. }
  125. private void Initialize()
  126. {
  127. Flags[DirtyMaterialColorBitMask] = true;
  128. _textureParameter = Parameters["Texture"];
  129. _diffuseColorParameter = Parameters["DiffuseColor"];
  130. }
  131. /// <summary>
  132. /// Computes derived parameter values immediately before applying the effect.
  133. /// </summary>
  134. protected override void OnApply()
  135. {
  136. base.OnApply();
  137. if (Flags[DirtyTextureBitMask])
  138. {
  139. _textureParameter.SetValue(_texture);
  140. Flags[DirtyTextureBitMask] = false;
  141. }
  142. // ReSharper disable once InvertIf
  143. if (Flags[DirtyMaterialColorBitMask])
  144. {
  145. UpdateMaterialColor();
  146. Flags[DirtyMaterialColorBitMask] = false;
  147. }
  148. // ReSharper disable once InvertIf
  149. if (Flags[DirtyShaderIndexBitMask])
  150. {
  151. var shaderIndex = 0;
  152. if (_textureEnabled)
  153. shaderIndex += 1;
  154. if (_vertexColorEnabled)
  155. shaderIndex += 2;
  156. Flags[DirtyShaderIndexBitMask] = false;
  157. CurrentTechnique = Techniques[shaderIndex];
  158. }
  159. }
  160. /// <summary>
  161. /// Updates the material color parameters associated with this <see cref="DefaultEffect" />.
  162. /// </summary>
  163. protected virtual void UpdateMaterialColor()
  164. {
  165. var diffuseColorVector3 = _diffuseColor.ToVector3();
  166. var diffuseColorVector4 = new Vector4()
  167. {
  168. X = diffuseColorVector3.X * Alpha,
  169. Y = diffuseColorVector3.Y * Alpha,
  170. Z = diffuseColorVector3.Z * Alpha,
  171. W = Alpha,
  172. };
  173. _diffuseColorParameter.SetValue(diffuseColorVector4);
  174. }
  175. }
  176. }