【问题标题】:Raycasting Engine with multiple layers具有多层的光线投射引擎
【发布时间】:2021-05-17 12:32:14
【问题描述】:

我现在就从我的情况开始。

我从https://github.com/ChrisSerpico/raycasting下载了raycast项目

这是基于这里的教程:https://lodev.org/cgtutor/raycasting.html

在我让项目开始工作、玩了一会儿并修改了一些东西之后,我目前坚持添加多个图层(基于每个图层一个地图)。我在 Internet 上阅读了很多内容,但未能实现该功能。

在这个项目中:https://github.com/Owlzy/OwlRaycastEngine

添加了多个图层,但这是通过切片完成的,我不知道如何在 Serpico 项目中实现这一点(之所以选择这个是因为那里的地板/天花板绘图效果更好)。纹理保存如下:

    Texture2D canvas;  // used to convert the buffer to a single texture to be drawn
    Color[] buffer;  // screen buffer with raw color data to be drawn
    Color[][] rawData;  // raw data of the individual external textures

    // initialize graphics rendering objects
    canvas = new Texture2D(GraphicsDevice, SCREEN_WIDTH, SCREEN_HEIGHT);
    buffer = new Color[SCREEN_WIDTH * SCREEN_HEIGHT];

    rawData = new Color[NUM_TEXTURES][]; //number of Textures

        for (int i = 0; i < NUM_TEXTURES; i++)
        {
            rawData[i] = new Color[TEXTURE_WIDTH * TEXTURE_HEIGHT];
        }

缓冲区在 Wallcasting 循环中以这种方式填充:

        if (TEXTURE_WIDTH * texY + texX <= rawData[texNum].Length - 1)
        {
                buffer[SCREEN_WIDTH * y + x] = rawData[texNum][TEXTURE_WIDTH * texY + texX];
        }
        else //avoid crash when running into walls
        {
                buffer[SCREEN_WIDTH * y + x] = rawData[texNum][rawData[texNum].Length - 1];
        }

最后是这样画的:

        canvas.SetData<Color>(buffer);
        b.Draw(canvas, new Rectangle(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT), Color.White);

代码直接来自 lodev 教程。我尝试了变量 lineHeight , texY 等等,但没有结果。纹理会被拉伸、截断,或者屏幕会产生糟糕的效果。

有人可以帮忙吗?我真的很绝望……

非常感谢!

【问题讨论】:

    标签: c# layer monogame raycasting


    【解决方案1】:

    问题出在 Draw() 中的调用canvas.SetData&lt;Color&gt;(buffer);

    将此行移至Update(),它将“大部分”工作。纹理内存在 CPU 和 GPU 之间共享。在调用 Draw() 时,预计纹理已经存在于 GPU 内存中。在绘制期间传输数据会导致随机撕裂。

    主要来自 Update() 和 Draw() 之间的不确定性延迟以及 PCIE 内存传输。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-08
      相关资源
      最近更新 更多