Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

Is there a way to change the opacity or transparency for a texture or sprite?

share|improve this question

2 Answers 2

up vote 4 down vote accepted

Yes. You can make a semi-transparent version of any colour by multiplying it by a floating point number. For example:

Color halfOpacityWhite = Color.White * 0.5f;

spriteBatch.Draw(myTexture, Vector2.Zero, halfOpacityWhite);
share|improve this answer

Yes!

You need to use this overload of the SpriteBatch (or another overload that has the blend state option) with the blend state set to the Alpha Blend blend state. Then in the SpriteBatch.Draw method use a white color with the alpha (transparency) value you would like to have. You can use the Color.FromARGB() method to construct a semi transparent white color.

share|improve this answer
    
It's worth pointing out that BlendState.AlphaBlend is the default, so you don't need to specify it. And that Color.FromARGB (as far as I can tell) doesn't exist. I think you might have meant FromNonPremultiplied (noting that the default AlphaBlend blend mode requires colours to be in premultiplied format). –  Andrew Russell Aug 6 '12 at 12:56
    
Andrew is totally right, I've worked mostly with XNA 3 so I keep forgetting about premultiplied colours. –  Roy T. Aug 6 '12 at 16:09

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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