I'm currently working on a 2D game, and my current task is to outline selected object.
I basically use a blur shader to do it fully runtime. I first draw my sprite using a vertical gaussian blur shader, then I draw it with an horizontal gaussian blur shader, and then I draw my sprite normally.
Here is my blur shader: sampler TextureSampler : register(s0);
#define SAMPLE_COUNT 15
float2 SampleOffsets[SAMPLE_COUNT];
float SampleWeights[SAMPLE_COUNT];
float4 PixelShaderFunction(float2 texCoord : TEXCOORD0) : COLOR0
{
float4 c = 0;
// Combine a number of weighted image filter taps.
for (int i = 0; i < SAMPLE_COUNT; i++)
{
c += tex2D(TextureSampler, texCoord + SampleOffsets[i]) * SampleWeights[i];
}
return c;
}
SampleOffets are the offsets allowing me to test rounding pixels SampleWeights are weights calculate with gaussian function (http://en.wikipedia.org/wiki/Gaussian_blur)
Here is the result:
It's not that bad, quite smooth, but not enough visible in game... But I'd like to be able to control the thickness (to make it larger), and add a first opaque outline before fading it in alpha (to make it more visible). And i feel like gaussian blur is maybe not as good as I thought for my task :)
Another idea is to generate it automatically from the .png (using a photoshop script to make it automatic). I just need to fill the outline pixels with a custom color, storing the alpha value in the Red component for example, and set all other components to 0. Then I just have to apply a shader which replaces transparent pixels to the color I want
float4 PixelShaderFunction2(float2 texCoord : TEXCOORD0) : COLOR0
{
float4 color = tex2D(TextureSampler, texCoord);
float4 newColor = 0;
if (color.a == 0 && color.r != 0)
{
color.a = color.r;
// Blue outline
color.b = 1;
}
}
Problem is, I'm using DXT5 compression for my sprite, so I can't be sure the color I'll get in my shader will be the exact same I wrote. That's why I didn't even try it... Any thoughts?
Any advice would be welcome :)