2013-02-26 61 views
2

我想在我的Windows Phone 7 XNA應用程序中使用RenderTarget2D。但是,我沒有成功,因爲將繪圖切換到我的渲染目標,然後切換回來(通過使用SetRenderTarget(null))導致我的整個畫面以藍色繪製,因此會覆蓋在切換到渲染目標之前繪製的任何內容。我不確定這是否是預期的行爲。Windows Phone中的XNA RenderTarget2D

事實上,重現這種行爲是非常容易的。只要創建了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. 

因此,我究竟做錯了什麼?

謝謝。

回答

3

「問題」是先繪製背景,然後更改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(); 
+0

謝謝。您的解決方案正確。 – MariusVE 2013-02-26 14:24:22

+0

沒問題,高興幫忙:) – 2013-02-26 14:32:47