【问题标题】:Storing render from RenderTarget2D to Color Array Error将渲染从 RenderTarget2D 存储到颜色数组错误
【发布时间】:2012-06-29 13:07:09
【问题描述】:

所以我试图将渲染目标的结果存储到颜色数组中,以便我可以使用迭代器单独操作它们。出于某种原因,下面的代码将数组中的每个值都设置为 R=0、G=0、B=0、A=0。好像它从未初始化过一样。 GetData 方法是否使用不当?是别的吗?它应该是一个非常彩色的图像,并且绝对不透明。

问题不在于纹理没有正确绘制,我在没有设置渲染目标的情况下执行了代码(使用默认的后备缓冲区)并且游戏运行得非常好。

            //Creating a new render target and setting it
        RenderTarget2D unprocessed = new RenderTarget2D(graphics.GraphicsDevice, Window.ClientBounds.Width, Window.ClientBounds.Height,false,SurfaceFormat.Color,DepthFormat.Depth24,4,RenderTargetUsage.PreserveContents);
        graphics.GraphicsDevice.SetRenderTarget(unprocessed);

        //Drawing background and main player
        spriteBatch.Draw(bgTex,new Rectangle(0,0,Window.ClientBounds.Width,Window.ClientBounds.Height),Color.White);
        mainPlayer.Draw(spriteBatch);

        //resetting render target
        graphics.GraphicsDevice.SetRenderTarget(null);

        //creating array of Color to copy render to
        Color[] baseImage = new Color[Window.ClientBounds.Width * Window.ClientBounds.Height];
        //I pause the program here and baseImage is an array of black transparent colors
        unprocessed.GetData<Color>(baseImage);

        spriteBatch.End();

【问题讨论】:

    标签: c# xna render rendertarget


    【解决方案1】:

    我相信有几个问题。第一个是您需要在访问渲染目标之前调用 spriteBatch.End() 。我还注意到没有调用 spriteBatch.Begin()。

    RenderTarget2D unprocessed = new RenderTarget2D( /*...*/);
    graphics.GraphicsDevice.SetRenderTarget(unprocessed);
    graphics.GraphicsDevice.Clear(Color.Black);
    
    //Drawing
    spriteBatch.Begin();
    spriteBatch.Draw(/*...*/);
    spriteBatch.End();
    
    //Getting
    graphics.GraphicsDevice.SetRenderTarget(null);
    

    你只是回答了你自己的问题,所以.... 没关系。

    【讨论】:

    • 哈哈谢谢!我注意到代码缺少一个 spritebatch 开始,但这只是因为我忘记将它包含在我复制的代码中。
    【解决方案2】:

    问题解决了。 基本上它是黑色的原因是因为 spritebatch 需要在渲染目标“获取”数据之前首先结束。这就是为什么在 spritebatch 过程中颜色值仍然是黑色的原因。我所做的解决方法是在绘图发生之前和之后使 spritebatch 开始和结束,并立即解决了问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-29
      • 2020-06-19
      • 1970-01-01
      • 2014-04-01
      • 2015-02-20
      相关资源
      最近更新 更多