Hello,
I am starting out with pixel shaders and wondering if anyone can help explain a behavior I am failing to understand.
I have a basic shader that draws a sprite, using another texture’s alpha value as a mask.
float4 MainPS(VertexShaderOutput input) : COLOR
{
float4 Color = tex2D(SpriteSampler, input.texCoord) * input.Color;
float4 maskColor = tex2D(MaskSampler, input.texCoord);
Color.a = maskColor.a;
return Color;
}
However this result completely changes if I swap the order of the two lines…
float4 maskColor = tex2D(MaskSampler, input.texCoord);
float4 Color = tex2D(SpriteSampler, input.texCoord) * input.Color;
I was wondering why this order matters? The full code is below.
Cheers!
#if OPENGL
#define SV_POSITION POSITION
#define VS_SHADERMODEL vs_3_0
#define PS_SHADERMODEL ps_3_0
#else
#define VS_SHADERMODEL vs_4_0_level_9_1
#define PS_SHADERMODEL ps_4_0_level_9_1
#endif
sampler2D SpriteSampler;
Texture2D Mask;
sampler2D MaskSampler
{
Texture = <Mask>;
};
struct VertexShaderOutput
{
float4 Position : SV_POSITION;
float4 Color : COLOR0;
float2 texCoord : TEXCOORD0;
};
float4 MainPS(VertexShaderOutput input) : COLOR
{
float4 Color = tex2D(SpriteSampler, input.texCoord) * input.Color;
float4 maskColor = tex2D(MaskSampler, input.texCoord);
Color.a = maskColor.a;
return Color;
}
technique SpriteDrawing
{
pass P0
{
PixelShader = compile PS_SHADERMODEL MainPS();
}
};
////////////////////////////
frag_shaders.AlphaShader.Parameters["Mask"].SetValue(glow_img);
spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.NonPremultiplied, effect: frag_shaders.AlphaShader);
spriteBatch.Draw(wall_img, cursor_pos, null, Color.White, 0, Vector2.Zero, 4, SpriteEffects.None, 0);
spriteBatch.End();