【发布时间】: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。
【问题讨论】: