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