Hi everyone, I am new to HLSL and shaders.
I installed monogame 3.4 working with VS2015, started a Windows DirectX test project.
I am just starting to play around with pixel shaders (I am not interested in 3D graphics…).
My Draw logic simply draws the sprite of a funny drake over a background:
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend, SamplerState.PointClamp, DepthStencilState.Default,
RasterizerState.CullNone, _effect);
spriteBatch.Draw(_background, new Rectangle(0, 0, 1600, 900), Color.White);
spriteBatch.Draw(_drake, new Vector2(200, 400), Color.White);
spriteBatch.End();
base.Draw(gameTime);
}
Without applying any shaders the output is this:
Now this is the shader code:
Texture2D SpriteTexture;
sampler2D SpriteTextureSampler = sampler_state
{
Texture = <SpriteTexture>;
};
struct VertexShaderOutput
{
float4 Position : SV_POSITION;
float4 Color : COLOR0;
float2 TextureCoordinates : TEXCOORD0;
};
float4 MainPS(VertexShaderOutput input) : COLOR
{
float4 color = tex2D(SpriteTextureSampler,input.TextureCoordinates);
// grey scale
float value = 0.299*color.r + 0.587*color.g + 0.114*color.b;
color.r = value;
color.g = value;
color.b = value;
return color;
// negative
/*color.r = 1 - color.r;
color.g = 1 - color.g;
color.b = 1 - color.b;
return color;*/
}
technique SpriteDrawing
{
pass P0
{
PixelShader = compile PS_SHADERMODEL MainPS();
}
};
Yuppie, the gray scale effect works perfectly…
But when I decomment the grayscale code in the shader and I put the negative one…
float4 MainPS(VertexShaderOutput input) : COLOR
{
float4 color = tex2D(SpriteTextureSampler,input.TextureCoordinates);
// grey scale
/*float value = 0.299*color.r + 0.587*color.g + 0.114*color.b;
color.r = value;
color.g = value;
color.b = value;
return color;*/
// negative
color.r = 1 - color.r;
color.g = 1 - color.g;
color.b = 1 - color.b;
return color;
}
…yikes, the output is this…
It’s like the shader has inverted the alpha channel too…
Am I missing something?
I think there is some error some way and it is very stupid…
Please I wanted to understand what’s is going on…
Thank you so much for your attention…
Bye!