【问题标题】:XNA RenderTarget2D in Windows PhoneWindows Phone 中的 XNA RenderTarget2D
【发布时间】:2013-02-11 23:56:18
【问题描述】:

我想在我的 Windows Phone 7 XNA 应用程序中使用 RenderTarget2D。但是,我不成功,因为将绘图切换到我的渲染目标然后切换回来(通过使用 SetRenderTarget(null) )导致我的整个屏幕被绘制为蓝色,因此覆盖了在切换到我的渲染目标之前绘制的任何内容。我不确定这是否是预期的行为。

事实上,很容易重现这种行为。只需为 Windows Phone 7 创建一个简单的 XNA 游戏并使用以下代码:

protected override void Draw(GameTime gameTime)
{
   spriteBatch.Begin();
   spriteBatch.Draw(textureBackground, new Vector2(0,0), Color.White); // This is a 480x800 image and my resolution is set to 480x800
   spriteBatch.End(); // Testing this block of code confirms that the textureBackground is being drawn

   graphics.GraphicsDevice.SetRenderTarget(textureBuffer);

   spriteBatch.Begin();
   spriteBatch.Draw(textureSummer, new Vector2(0, 0), Color.White); // texture Summer is a 20x20 pixels texture while the screen resolution is 480 x 800
   spriteBatch.End();

   graphics.GraphicsDevice.SetRenderTarget(null); // switch back 

   spriteBatch.Begin();
   spriteBatch.Draw(textureBuffer, new Vector2(0,0), Color.White);
   spriteBatch.End(); // at this point, textureBuffer is drawn (the 20x20 pixeles image) in the upper left hand corner but the background of the whole screen is BLUE and so textureBackground texture has been overwritten by this color and is not showing anymore.

   // At this point all I see is a blue background with a 20x20 pixels image in the upper left hand corner.
}

我正在按如下方式创建我的渲染目标:

textureSummer = Content.Load<Texture2D>("summer_picture_icon"); // the 20x20 pixels texture
textureBuffer = new RenderTarget2D(graphics.GraphicsDevice, textureSummer.Width, textureSummer.Height); // the RenderTarget2D object. Note that this RenderTarget is only 20x20.

那么,我做错了什么?

谢谢。

【问题讨论】:

    标签: c# windows-phone-7 xna rendertarget


    【解决方案1】:

    “问题”是先绘制背景,然后更改rendertarget,然后渲染那个小方块,然后更改rendertarget,然后再次绘制小方块。按此顺序:

    • 在屏幕上渲染
    • 渲染目标更改
    • 渲染屏幕外的东西
    • 渲染目标更改
    • 在屏幕上渲染内容

    每次更改渲染目标都会清除它。

    你应该怎么做;

    • 渲染目标更改
    • 渲染屏幕外的东西
    • 渲染目标更改
    • 在屏幕上渲染内容

    像这样:

    GraphicsDevice.SetRenderTarget(textureBuffer);
    
    spriteBatch.Begin();
    spriteBatch.Draw(textureSummer, new Vector2(0, 0), Color.White); // texture Summer is a 20x20 pixels texture while the screen resolution is 480 x 800
    spriteBatch.End();
    
    GraphicsDevice.SetRenderTarget(null); // switch back 
    
    spriteBatch.Begin();
    spriteBatch.Draw(textureBackground, new Vector2(0,0), Color.White); // This is a 480x800 image and my resolution is set to 480x800
    spriteBatch.Draw(textureBuffer, new Vector2(0,0), Color.White);
    spriteBatch.End();
    

    【讨论】:

    • 没问题,乐于助人:)
    猜你喜欢
    • 2023-03-09
    • 1970-01-01
    • 2012-04-13
    • 2013-07-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-26
    相关资源
    最近更新 更多