- Color Map
- Alpha Map
Let us start by adding these to the shader:
texture ColorMap;
sampler ColorMapSampler = sampler_state
{
Texture = <ColorMap>;
MinFilter = Linear;
MagFilter = Linear;
MipFilter = Linear;
AddressU = Mirror;
AddressV = Mirror;
};
texture AlphaMap;
sampler AlphaMapSampler = sampler_state
{
Texture = <AlphaMap>;
MinFilter = Linear;
MagFilter = Linear;
MipFilter = Linear;
AddressU = Mirror;
AddressV = Mirror;
};
Now we got our textures. Then we need to set the alpha channel of the color we return to the value stored in the Alpha map. This is done in the Pixel Shader:
Color = (Ai*Ac*Color)+(Color*Di*Dd);
Color.a = tex2D(AlphaMapSampler, Tex).r;
return Color;
Here we calculate the diffuse color as we usually do, and put it in color.
After this, we go in to the Colors .a component, that is the Colors alpha channel, and set it to the value stored in the alpha map. All color-channels in the alpha map are the same( because its a grayscale texture ), so it does not matter if you use the r,g or b channel. I’m just using the .r channel in this example.
Our technique for todays lesson looks like this:
technique DiffuseShader
{
pass P0
{
AlphaBlendEnable = True;
SrcBlend = SrcAlpha;
DestBlend = InvSrcAlpha;
Sampler[0] = (ColorMapSampler);
Sampler[1] = (AlphaMapSampler);
VertexShader = compile vs_2_0 VertexShader();
PixelShader = compile ps_2_0 PixelShader();
}
}
As you can see, we set AlphaBlendEnable to true, and use det SrcAlpha/InvSrcAlpha as the blending function. This means that we use the alphachannel to make things transparent.
Using the shader
Nothing new on how to use the shader. We just got to remember to pass the color and alpha textures to the shader.
I have also added an overlay texture named m_Overlay. This is used to render the overlay in front of the whole screen, using the alpha values found in a .PNG file. These alpha values can be set by using Photoshop or many other picture editor programs.
spriteBatch.Begin(SpriteBlendMode.AlphaBlend, SpriteSortMode.Immediate, SaveStateMode.SaveState);
{
spriteBatch.Draw(m_Overlay, new Rectangle(0, 0, 800, 600), Color.White);
}
spriteBatch.End();
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 paramteres inside the pass.
YouTube – XNA Shader programming, Tutorial 13 – Alpha mapping
Taaanxx!!!!!!!
Why don\’t you use the alpha channel of the diffuse texture instead of a completly new one?
I was thinking I want to use a seperate texture for alpha in this case, as i use the alpha from the alpha channel to render the overlay. Many 3D software, and people I work with like to have their seperate alpha map so it\’s easy to manipulate without having to edit the color map. This is just to seperate those two. But, you could have used the alpha channel as well, no problem 🙂
Hi, sorry for my bad english, i\’m italian.I have a little problem and a picture is more explicative for me 🙂 http://www.wilez.it/alpha_problem1.jpg
Ok i solved 🙂
Ok, good 🙂 It was probably the order you rendered your models?
Yes 😉