简体   繁体   English

xna 4.0如何使前景纹理透明以显示背景纹理

[英]xna 4.0 how to have foreground texture transparent to show background texture

i'm hoping this is a simple question and answer, but then xna and microsoft seem to complicate things needlessly. 我希望这是一个简单的问答,但是xna和microsoft似乎不必要地使事情复杂化。

I have a background texture that covers the entire screen. 我的背景纹理覆盖了整个屏幕。 I then have a foreground texture that I want to draw ontop of the background texture. 然后,我有一个要在背景纹理上绘制的前景纹理。 As you can expect, the foreground texture has transparent areas. 如您所料,前景纹理具有透明区域。 the problem that i have is that whereever the foreground texture is transparent, I see the graphic device color of blue and not the background image. 我的问题是,无论前景纹理是透明的,我看到的图形设备颜色都是蓝色,而不是背景图像。

I have tracked down further information. 我已找到更多信息。 I am resizing the foreground texture and it seems that the resize is what is causing the grief. 我正在调整前景纹理的大小,似乎调整大小是造成悲伤的原因。

Any suggestions or pointers are well appreciated. 任何建议或指针都很好。

Draw(...)
{
ScreenManager.Game.GraphicsDevice.Clear(Color.DarkBlue);
SpriteBatch spriteBatch = this.ScreenManager.SpriteBatch;
spriteBatch.Begin();

// draw the background
spriteBatch.Draw(_backgroundTexture, _backgroundPosition, null, Color.White, 0f,
                 Vector2.Zero, 1.0f, SpriteEffects.None, 0f);
spriteBatch.Draw(Resize(_foregroundTexture,100,100), _foregroundPosition, null, Color.White, 0f,
                 Vector2.Zero, 1.0f, SpriteEffects.None, 0f);

spriteBatch.End();
}

Texture2D Resize(texture2D, float newWidth, float newHeight)
{
RenderTarget2D renderTarget;
Rectangle destinationRectangle;
SpriteBatch spriteBatch;

renderTarget = new RenderTarget2D(graphicsDevice, (int)newWidth, (int)newHeight);
destinationRectangle = new Rectangle(0, 0, (int)newWidth, (int)newHeight);
spriteBatch = new SpriteBatch(graphicsDevice);

graphicsDevice.SetRenderTarget(renderTarget);

spriteBatch.Begin();
spriteBatch.Draw(texture2D, destinationRectangle, Color.White);
spriteBatch.End();

graphicsDevice.SetRenderTarget(null);

return renderTarget;
}

when i resize, i need to specify the following sprite batch parameters... 调整大小时,我需要指定以下精灵批处理参数...

spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.Opaque); spriteBatch.Begin(SpriteSortMode.Deferred,BlendState.Opaque);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM