2011-06-12 77 views
2

嗨我试图从this question 使用Alpha混合时使一部分纹理透明的答案唯一的问题是,这只适用于XNA 3.1和I在XNA 4.0中工作,所以像RenderState这样的东西不存在于相同的上下文中,我不知道在哪里可以找到GfxComponent类库。XNA Alpha在Game Studio 4.0中混合纹理的一部分

我仍然想要做与示例问题相同的事情,从鼠标位置发出一个圆形区域,当鼠标悬停在其上时,覆盖纹理会变得透明。

+0

就个人而言,我会使用一个像素着色器,您将鼠标位置传递给它,以在绘制原始纹理时添加透明度。你也可以做各种混合欺骗。或者你可以通过渲染目标。 – 2011-06-15 01:50:40

回答

2

3.5

GraphicsDevice.RenderState.AlphaBlendEnable = true; 

4.0

GraphicsDevice.BlendState = BlendState.AlphaBlend; 

见肖恩·哈格里夫斯后获得更多信息:http://blogs.msdn.com/b/shawnhar/archive/2010/06/18/spritebatch-and-renderstates-in-xna-game-studio-4-0.aspx

编辑:在后,你可以用肖恩看到BlendState。您可以创建一个新的实例,然后按照您的喜好进行设置,然后将其传递给图形设备。像这样:

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; 

那更清晰?

+0

这并不完全有用,你知道。 – ShadowsEdge19 2011-06-13 18:58:45

+0

好的,谢谢,我在 之间添加了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(); 但是,空白纹理是不可见的,但它不会对它后面的图像产生任何影响,因为我想通过矩形的孔看到图像后面的蓝色背景。 – ShadowsEdge19 2011-06-13 23:13:02

+0

'GraphicsDevice.Clear(Color.CornflowerBlue); spriteBatch.Begin(SpriteSortMode.Immediate,BlendState.Opaque); spriteBatch.Draw(背景,新的Rectangle(0,0,GraphicsDevice.Viewport.Width,GraphicsDevice.Viewport.Height),Color.White);' ..your代码... 'spriteBatch.Draw(空白,flashlightArea,Color.White); spriteBatch.End();' – ShadowsEdge19 2011-06-13 23:22:56