【问题标题】:RenderTargets draw order in XNAXNA 中的 RenderTargets 绘制顺序
【发布时间】:2011-08-29 20:36:16
【问题描述】:

我认为我从根本上误解了渲染目标的工作方式。据我了解,RenderTargets 只是 spritebatch 绘制调用绘制的纹理。

所以我尝试使用这段代码来渲染 GUI 窗口,以确保它们只能在客户区域内绘制并在其外部进行裁剪。

 for (int i = Controls.Count - 1; i >= 0; i--)
        {
            RenderTarget2D oldTarget;
            if (graphics.GetRenderTargets().Count() == 0) oldTarget = null;
            else oldTarget = (RenderTarget2D)graphics.GetRenderTargets()[0].RenderTarget; // Get the old target being used.
            graphics.SetRenderTarget(canvas); //set the target to a temporary RT
            graphics.Clear(Color.Black); // Clear it
            Control c = Controls[i]; // Get the current control (a form in this case)
            c.Draw(spriteBatch, gameTime); // Draw it to the temp RT
            graphics.SetRenderTarget(oldTarget); // Set the RT back to the main RT
            Vector2 dest = c.DrawCoOrds(); // Gets the draw coordinates of the control
            spriteBatch.Begin();
            spriteBatch.Draw(canvas, new Rectangle((int)dest.X, (int)dest.Y, c.Bounds.Width, c.Bounds.Height), new Rectangle((int)dest.X, (int)dest.Y, c.Bounds.Width, c.Bounds.Height), Color.White); 
// take the rect from the temp RT and draw it to the main RT.
            spriteBatch.End();
        }

然而,这段代码只绘制了列表中的最后一个表单,这意味着它必须以某种方式清除主 RT,但我不明白为什么。我只在 RT 设置为临时画布时才调用 clear。

【问题讨论】:

    标签: c# xna xna-4.0


    【解决方案1】:

    我认为绘制 gui 控件的最佳方法是使用 ScissorRectangle,因为我们只在该矩形内绘制,它可以是 gui 控件的客户区域。

    MSDN: GraphicsDevice.ScissorRectangle

    您需要通过 RasterizerState 启用此功能。

     RasterizerState ScissorState = new RasterizerState() 
     { 
         ScissorTestEnabled = true; 
     }
    

    在绘制之前,使用此状态调用 SpriteBatch.Begin。

    A video of my own gui running in a xbox360 :)

    【讨论】:

      【解决方案2】:

      您是如何创建渲染目标和后台缓冲区的?默认情况下,您不能在更改为不同的渲染目标后多次写入渲染目标。这就是为什么:

      http://blogs.msdn.com/b/shawnhar/archive/2007/11/21/rendertarget-changes-in-xna-game-studio-2-0.aspx

      您可以通过使用RenderTargetUsage.PreserveContents. 创建渲染目标来更改默认行为,并通过覆盖GraphicsDeviceManager.PrepareDeviceSettings. 来更改后台缓冲区,更改GraphicsDeviceInformation.PresentationParameters.RenderTargetUsage,如链接中所述。虽然我相信在 XNA 4 中覆盖这些设置的方式有所不同。

      话虽如此,改变默认行为是出于性能考虑,因此不建议这样做。你应该找到一种不同的方法来做到这一点。一种可能性是为每个窗口创建一个单独的渲染目标,绘制所有窗口,切换到后台缓冲区并将渲染目标绘制到它。

      更好的选择是使用@Blau 建议的剪刀矩形光栅化状态。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-04-11
        • 2015-10-04
        相关资源
        最近更新 更多