是否有一种廉价的方法可以在每帧的基础上从 RenderTarget2D 传输颜色数据?

编程入门 行业动态 更新时间:2024-10-28 21:16:02
本文介绍了是否有一种廉价的方法可以在每帧的基础上从 RenderTarget2D 传输颜色数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

直到最近,我们的游戏通过从场景背景纹理的一部分获取颜色数据来检查碰撞.这很有效,但随着设计的变化,我们需要检查多个纹理,因此决定将这些全部渲染到单个 RenderTarget2D 并检查其上的碰撞.

Until recently, our game checked collisions by getting the colour data from a section of the background texture of the scene. This worked very well, but as the design changed, we needed to check against multiple textures and it was decided to render these all to a single RenderTarget2D and check collisions on that.

public bool TouchingBlackPixel(GameObject p)
    {
        /*
        Calculate rectangle under the player...
        SourceX,SourceY: Position of top left corner of rectangle
        SizeX,SizeY: Aproximated (cast to int from float) size of box
        */

        Rectangle sourceRectangle = new Rectangle(sourceX, sourceY,
                                                   (int)sizeX, (int)sizeY);

        Color[] retrievedColor = new Color[(int)(sizeX * sizeY)];

        p.tileCurrentlyOn.collisionLayer.GetData(0, sourceRectangle, retrievedColor,
                                                     0, retrievedColor.Count());

        /*
        Check collisions
        */
    }

我们一直遇到的问题是,自从移至渲染目标后,我们的 FPS 大幅下降.

The problem that we've been having is that, since moving to the render target, we are experiencing massive reductions in FPS.

从我们读到的内容来看,问题似乎是为了从 RenderTarget2D 获取数据,您需要将数据从 GPU 传输到 CPU,这很慢.由于我们需要运行相同的函数两次(每个玩家一次)并且无法保留相同的数据(它们可能不在同一个图块上),这使情况变得更加复杂.

From what we've read, it seems as if the issue is that in order to get data from the RenderTarget2D, you need to transfer data from the GPU to the CPU and that this is slow. This is compounded by us needing to run the same function twice (once for each player) and not being able to keep the same data (they may not be on the same tile).

我们已经尝试将 GetData 调用移动到磁贴的 Draw 函数并将数据存储在成员数组中,但这似乎并没有解决问题(因为我们仍然定期在磁贴上调用 GetData - 从每次更新两次到每次抽奖一次).

We've tried moving the GetData calls to the tile's Draw function and storing the data in a member array, but this does not seem to have solved the problem (As we are still calling GetData on a tile quite regularly - down from twice every update to once every draw).

您能给我们的任何帮助都会很棒,因为这个碰撞系统为我们提供的能力非常棒,但是渲染目标引入的开销将使其无法保持.

Any help which you could give us would be great as the power that this collision system affords us is quite fantastic, but the overhead which render targets have introduced will make it impossible to keep.

推荐答案

简单的答案是:不要那样做.

听起来像是将碰撞数据的合成卸载到 GPU 是一种不起作用的性能优化 - 因此正确的做法是还原更改.

It sounds like offloading the compositing of your collision data to the GPU was a performance optimisation that didn't work - so the correct course of action would be to revert the change.

您应该简单地在 CPU 上进行碰撞检查.而且我还建议,多次运行碰撞算法并通过组合结果来确定碰撞响应可能会更快,而不是将整个场景合成到单个图层上并运行一次碰撞检测.

You should simply do your collision checks all on the CPU. And I would further suggest that it is probably faster to run your collision algorithm multiple times and determine a collision response by combining the results, rather than compositing the whole scene onto a single layer and running collision detection once.

如果您在进行碰撞之前使用渲染目标来支持转换,则尤其如此.

This is particularly the case if you are using the render target to support transformations before doing collision.

(有关简单的 2D 像素碰撞检测,请参阅此示例. 如果您需要转换支持,请参阅此示例好吧.)

(For simple 2D pixel collision detection, see this sample. If you need support for transformations, see this sample as well.)

这篇关于是否有一种廉价的方法可以在每帧的基础上从 RenderTarget2D 传输颜色数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

更多推荐

[db:关键词]

本文发布于:2023-04-25 20:15:55,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1123389.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:基础上   有一种   廉价   颜色   方法

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!