Skip to main content
gamedev.net
🔒 Locked

color key transparency

Started by JohnnyCode Feb 24, 2009 at 3:20 PM 3 replies 900+ views
Original Post
JohnnyCode
JohnnyCode
hello, I use transparency in a way that in my shader, after a sample from my texture, I check wheather sampled pixel is pink (having value 255,0,255) and if so, I set pixels alpha value to 0. 1st question: is it a performance kill? I mean this code in my shader:

vec4 fvDiffuse = texture2D( baseMap, Texcoord );
if (fvDiffuse.r>0.9)
{
if (fvDiffuse.b>0.9) 
   if (fvDiffuse.g<0.1)
     fvTotalDiffuse.w = 0.0;
}
else
   fvTotalDiffuse.w = 1.0;



2nd question: this way I get very sharp edges of my opaque part,... how can I achieve smooth edges of my opaque part?
zedz
zedz
yes thats gonna be slow
quicker would be
if ( dot( vec3(1.0,0,1.0), fvDiffuse ) >= 0.999 )
discard;

but even that is not as good as not using a color key + instead using texture alpha glAlphaTest()
JohnnyCode
JohnnyCode
thanks zedz,
so how do I go about smoothing the edges of the opaque part? A simple pixel drop I think results in that. In dx I once made it but it was a long time ago and I cannot remember how I did it. It is a really ugly effect. How can I get rid of it?
zedz
zedz
sorry I misread your question ( I do that often )

you will want either

mix() or smoothstep() see glsl documentation

eg (off the top of my head, so youll have to tweak the pow values )

fvTotalDiffuse.w = mix( 0.0,1.0, pow(dot( vec3(1.0,0.0,1.0), fvDiffuse ),4.0) );
XTAL256
XTAL256
Why go to all that trouble when you can just use a texture with an alpha channel?
[Window Detective] - Windows UI spy utility for programmers

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.