如何在不丢失数据的情况下将一个矩形从一个渲染目标绘制到另一个渲染目标上? [英] How do I draw a rectangle from one render target onto another without losing data?

查看:26
本文介绍了如何在不丢失数据的情况下将一个矩形从一个渲染目标绘制到另一个渲染目标上?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在本周早些时候(此处)问了一个关于将数据从渲染目标传输到 CPU 以便可能发生碰撞的问题对其进行了测试.根据我得到的答复,我决定将渲染目标的一部分绘制到较小的渲染目标,然后检索该数据将是一个有效的解决方案,并尝试实施该方案.

I asked a question earlier in the week (here) regarding transferring data from a rendertarget to the CPU so that collisions could be tested on it. From the responses I got there, I decided that drawing a section of the render target to a smaller render target and then retrieving that data would be a valid solution and tried to implement that.

绘制完整大小的渲染目标 (1280x1024) 后,我将活动渲染目标切换为较小的渲染目标(59X47 - 我的玩家的碰撞大小)并尝试绘制位于玩家下方的大渲染目标部分到较小的目标.起初,我认为这行得通,但后来我注意到碰撞有时会非常不准确.

After drawing the full sized render target (1280x1024), I switch the active render target to a smaller one (59X47 - the collision size of my players) and try to draw the section of the large render target which falls under the player onto the smaller target. At first, I thought this had worked, but then I noticed that collisions would sometimes be wildly inaccurate.

我将较小的渲染目标绘制到屏幕上以检查其内容,发现它每次只占用少量像素,并且这些像素始终位于左上角.

I drew the smaller render target to the screen to inspect its contents and found that it only had a handful of pixels occupied at any one time, and that these were always in the top left corner.

            // Render rectangle under player from foreground to collision layer
            Rectangle sourceRectangle = new Rectangle(sourceX, sourceY,
                                                       (int)sizeX, (int)sizeY);
            gd.SetRenderTarget(collisionLayer[i]);
            gd.Clear(Color.Transparent);
            spriteBatch.Draw(foregroundLayer, collisionLayer[i].Bounds,
                                                sourceRectangle, Color.White);
            gd.SetRenderTarget(null);

将完整的大型渲染目标绘制到较小的目标会导致数据成功传输(尽管显然已缩放),所以我不确定出了什么问题.

Drawing the full large render target to the smaller target results in the data carrying over successfully (though scaled, obviously), so I'm not sure what is going wrong.

            // Render full target onto collision layer
            gd.SetRenderTarget(collisionLayer[i]);
            gd.Clear(Color.Transparent);
            spriteBatch.Draw(foregroundLayer,new Vector2(0.0f),Color.White);
            gd.SetRenderTarget(null);

下图显示了大目标,小目标叠加在左上角.大目标的完整数据似乎绘制正确.

The image below shows the large target with the small target superimposed on the top left corner. The large target's full data seems to be drawn correctly.

推荐答案

你没有显示你的 spriteBatch.BeginspriteBatch.End 调用,但它看起来像你需要移动它们.具体来说,您在调用 spriteBatch.End 之前将渲染目标设置为 null,或者至少在您的示例代码中是这样.

You're not showing your spriteBatch.Begin and spriteBatch.End calls, but it looks like you need to move them. Specifically, you're setting your render target to null before calling spriteBatch.End, or at least it appears so from your sample code.

我设置了一个示例项目,当我执行以下操作时,一切都按预期进行...

I set up a sample project, and when I do the following everything works as expected...

  GraphicsDevice.SetRenderTarget(target);
  GraphicsDevice.Clear(Color.Transparent);
  spriteBatch.Begin();
  spriteBatch.Draw(texture, target.Bounds, new Rectangle(50, 500, 100, 100), Color.White);
  spriteBatch.End();

  GraphicsDevice.SetRenderTarget(null);
  GraphicsDevice.Clear(Color.CornflowerBlue);
  spriteBatch.Begin();
  spriteBatch.Draw(texture, new Rectangle(0, 0, 640, 480), texture.Bounds, Color.White);
  spriteBatch.Draw(target, Vector2.Zero, target.Bounds, Color.White);
  spriteBatch.End();

但是如果我在 SetRenderTarget(null) 之后移动 spriteBatch.End 我的渲染目标是空的.

But if I move spriteBatch.End after SetRenderTarget(null) my render target is empty.

这篇关于如何在不丢失数据的情况下将一个矩形从一个渲染目标绘制到另一个渲染目标上?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆