2
\$\begingroup\$

I'm actually trying to replace black color of my texture by a transparent one.

How can I do to remove the black pixels, should I use a AlphaTestEffect, or can I use the BasicEffect?

enter image description here Thank you.

\$\endgroup\$
3
\$\begingroup\$

If your image background is already transparent, then you may need to enable alpha blending on the basic effect if its not already enabled.

If the textures background is not transparent, the easiest way would be to edit the texture outside of XNA, and replace the black background with a transparent one. If you have Gimp, Photoshop or Paint.NET installed, this is rather trivial.

If you are looking for a way to do this in code, one way will require you to use a custom shader in order to so so. My answer here describes how to do something similar with a different colour, and I've also copied across the code as well in case anything happens to that answer.

// WARNING! UNTESTED CODE FOLLOWS
void SpriteVertexShader(inout float4 color : COLOR0, inout float2 texCoord : TEXCOORD0,
                        inout float4 position : POSITION0)
{
    if (color == float4(0, 0, 0, 1)
        color = float4(0, 0, 0, 0)
}

Essentially what this psuedo shader code is meant to do is replace any black coloured pixel with a transparently one. If you are not familiar with custom shaders, or not comfortable using them, something similar can be done in C# code, but is best done when you load the texture, as it is not something you would want to run every draw call.

| improve this answer | |
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.