2

Hi I'm trying to follow an answer about making part of a texture transparent when using Alpha Blending from this question The only problem is that this only works in XNA 3.1 and I'm working in XNA 4.0 so stuff like RenderState doesn't exist in the same context, and I have no idea where to find the GfxComponent class library.

I still want to do the same thing as in the example question, a circular area radiating from the mouse position that makes the covering texture transparent when the mouse is hovering over it.

Community
  • 1
  • 1
ShadowsEdge19
  • 21
  • 1
  • 2
  • Personally I'd use a pixel shader, to which you pass your mouse position, to add the transparency when you draw the original texture. You could also do various blending trickery. Or you could go via a render target. – Andrew Russell Jun 15 '11 at 01:50

1 Answers1

2

3.5

GraphicsDevice.RenderState.AlphaBlendEnable = true;

4.0

GraphicsDevice.BlendState = BlendState.AlphaBlend;

See Shawn Hargreaves post for more info: http://blogs.msdn.com/b/shawnhar/archive/2010/06/18/spritebatch-and-renderstates-in-xna-game-studio-4-0.aspx

EDIT: In the post you can see Shawn using BlendState. You create a new instance of this, set it up however you like, and pass this to the graphics device. Like so:

BlendState bs = new BlendState();
bs.AlphaSourceBlend = Blend.One;
bs.AlphaDestinationBlend = Blend.Zero;
bs.ColorSourceBlend = Blend.Zero;
bs.ColorDestinationBlend = Blend.One;
bs.AlphaBlendFunction = BlendFunction.Add;
graphicsDevice.BlendState = bs;

That clearer?

James Hull
  • 3,669
  • 2
  • 27
  • 36
  • That's not exactly helpful you know. – ShadowsEdge19 Jun 13 '11 at 18:58
  • Okay thanks, I've added that in between the spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.Opaque); spriteBatch.Draw(background, new Rectangle(0, 0, GraphicsDevice.Viewport.Width, GraphicsDevice.Viewport.Height), Color.White); ... spriteBatch.Draw(blank, flashlightArea, Color.White); spriteBatch.End(); However the blank texture is invisible but it doesn't make any difference to the image behind it as I want to see the blue background behind the image through the hole the rectangle made. – ShadowsEdge19 Jun 13 '11 at 23:13
  • `GraphicsDevice.Clear(Color.CornflowerBlue); spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.Opaque); spriteBatch.Draw(background, new Rectangle(0, 0, GraphicsDevice.Viewport.Width, GraphicsDevice.Viewport.Height), Color.White);` ..your code... `spriteBatch.Draw(blank, flashlightArea, Color.White); spriteBatch.End();` – ShadowsEdge19 Jun 13 '11 at 23:22
  • @ShadowsEdge19: you should probably be passing the `BlendState bs` object to `SpriteBatch.Begin`, where you are currently passing in `BlendState.Opaque`, rather than setting it on the `GraphicsDevice`. That way you can also avoid using `SpriteSortMode.Immediate` (which disables batching, use `Deferred`). – Andrew Russell Jun 14 '11 at 04:55
  • Okay I've done that but I'm still hiding one of the sprites but its not affecting the other. `spriteBatch.Begin(); spriteBatch.Draw(background, new Rectangle(0, 0, GraphicsDevice.Viewport.Width, GraphicsDevice.Viewport.Height), Color.White); spriteBatch.End();` your bs code `spriteBatch.Begin(SpriteSortMode.Deferred, bs); spriteBatch.Draw(blank, flashlightArea, Color.White); spriteBatch.End();` – ShadowsEdge19 Jun 14 '11 at 10:35
  • @AndrewRussell sorry I'm still not used to this forum's formatting. – ShadowsEdge19 Jun 14 '11 at 14:37
  • Upload your project to drop box or something and I'll take a look. – James Hull Jun 14 '11 at 20:30
  • @ShadowsEdge19: I should point out that my comment was for style and performance, rather than fixing a bug or answering your question. – Andrew Russell Jun 15 '11 at 01:47
  • @Bigfellahull & @AndrewRussell: I've got it up on dropbox but I need to share the folder using an email address of yours for you to get an invite. – ShadowsEdge19 Jun 15 '11 at 13:27
  • @Bigfellahull can you please post on here if you've made any significant improvements to the FlashlightExample project? – ShadowsEdge19 Jun 18 '11 at 10:13
  • @Bigfellahull Is it possible to see the cornflowerblue screen background through the rectangle area? – ShadowsEdge19 Jun 18 '11 at 18:19
  • I had a quick look as you have probably seen, however I am still trying to get my head around 4's changes as well. I'm a bit busy at the moment, but I'll have a proper look at the weekend. – James Hull Jun 22 '11 at 19:58