Post Process Negative
You are reading the XNA 3.1 version of this tutorial.
This tutorial builds on Tutorial 9, post process wiggling. If you haven't read that yet, you should in order to fully understand what's going on here.
The source-code and binaries can be found in the end of the chapter.
The source-code and binaries can be found in the end of the chapter.
Negative Image
This shader is also very simple, but, I got a lot of questions about this so I decided I wanted to write a short tutorial on it.. and it's really short ;)
To get the color from a texture sampler, you usually do this:
To get the color from a texture sampler, you usually do this:
float4 Color = tex2D(ColorMapSampler, Tex); |
To get the negative color, you simply subtracts each channel on Color with one, or, simply:
float4 ColorInverse = 1.0f - tex2D(ColorMapSampler, Tex); |
And, thats it!
Implementing the shader
Let's start by implementing the shader. Its pretty short so I'll just add the code:
sampler ColorMapSampler : register(s0); // Negative image float4 PixelShaderGo(float2 Tex: TEXCOORD0) : COLOR { float4 Color = 1.0f - tex2D(ColorMapSampler, Tex); // Keep our alphachannel at 1. Color.a = 1.0f; return Color; } technique PostProcess { pass P0 { // A post process shader only needs a pixel shader. PixelShader = compile ps_2_0 PixelShaderGo(); } } |
First, we subtract the color from our sampler, from 1.0f to get the inverse.
But, when doing this, we are inverting the Alpha channel as well. If this is what you want, go ahead, but if you want to keep the alpha-values from the original image, you must set it back to what it was, or set it to something manually.
In this case, we want the image to have the alpha of 1.0, so we set the a component of Color to 1.0, making our image fully opaque if we have shading enabled.
That's it for this tutorial. I'll keep adding some post process effects during the next days so keep coming back :)
NOTE:
You might have noticed that I have not used effect.commitChanges(); in this code. If you are rendering many objects using this shader, you should add this code in the pass.Begin() part so the changed will get affected in the current pass, and not in the next pass. This should be done if you set any shader parameters inside the pass.
Download Sample
But, when doing this, we are inverting the Alpha channel as well. If this is what you want, go ahead, but if you want to keep the alpha-values from the original image, you must set it back to what it was, or set it to something manually.
In this case, we want the image to have the alpha of 1.0, so we set the a component of Color to 1.0, making our image fully opaque if we have shading enabled.
That's it for this tutorial. I'll keep adding some post process effects during the next days so keep coming back :)
NOTE:
You might have noticed that I have not used effect.commitChanges(); in this code. If you are rendering many objects using this shader, you should add this code in the pass.Begin() part so the changed will get affected in the current pass, and not in the next pass. This should be done if you set any shader parameters inside the pass.
Download Sample