2
\$\begingroup\$

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

\$\endgroup\$

2 Answers 2

5
\$\begingroup\$

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); 
\$\endgroup\$
1
\$\begingroup\$

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.

\$\endgroup\$
2
  • \$\begingroup\$ 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). \$\endgroup\$ Commented Aug 6, 2012 at 12:56
  • \$\begingroup\$ Andrew is totally right, I've worked mostly with XNA 3 so I keep forgetting about premultiplied colours. \$\endgroup\$ Commented Aug 6, 2012 at 16:09

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.