c# - 从 XNA 4.0 中的 RenderTarget2D 对象获取 Texture2D

标签 c# xna rendering pixel-shader

我只是在试验像素着色器。我找到了一个很好的模糊效果,现在我正在尝试创建一种一遍又一遍地模糊图像的效果。

我想怎么做:我想在应用模糊效果的 RenderTarget 中渲染我的图像 hellokittyTexture,然后用渲染结果替换 hellokittyTexture 并执行在每次 Draw 迭代中一遍又一遍:

protected override void Draw(GameTime gameTime)
    {

        GraphicsDevice.Clear(Color.CornflowerBlue);


        GraphicsDevice.SetRenderTarget(buffer1);

        // Begin the sprite batch, using our custom effect.
        spriteBatch.Begin(0, null, null, null, null, blur);
        spriteBatch.Draw(hellokittyTexture , Vector2.Zero, Color.White);
        spriteBatch.End();
        GraphicsDevice.SetRenderTarget(null);

        hellokittyTexture = (Texture2D) buffer1;

        // Draw the texture in the screen
        spriteBatch.Begin(0, null, null, null, null, null);
        spriteBatch.Draw(hellokittyTexture , Vector2.Zero, Color.White);
        spriteBatch.End();

        base.Draw(gameTime);
    }

但我收到此错误“当设备用作纹理时,不得在设备上设置渲染目标。”因为hellokittyTexture = (Texture2D) buffer1;不是复制纹理,而是复制对 RenderTarget 的引用(基本上它们在分配后是同一个对象)

您知道在 RenderTarget 中获取纹理的好方法吗?或者更优雅的方式来做我正在尝试的事情?

最佳答案

    spriteBatch.Draw(hellokittyTexture , Vector2.Zero, Color.White);

在这一行中,您正在绘制纹理...自身...这不可能发生。

假设 buffer1hellokittyTexture 已经正确初始化,替换这一行:

    hellokittyTexture = (Texture2D) buffer1;

用这个:

        Color[] texdata = new Color[hellokittyTexture.Width * hellokittyTexture.Height];
        buffer1.GetData(texdata);
        hellokittyTexture.SetData(texdata);

这样,hellokittyTexture 将被设置为 buffer1副本,而不是指向它的指针。

关于c# - 从 XNA 4.0 中的 RenderTarget2D 对象获取 Texture2D,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17603919/

相关文章:

c# - 无法从媒体库获取图片

c# - 重复单步调试≠运行调试

Android 应用程序标题栏在测试应用程序时不显示?

c# - 在 powerpoint c# 中写入文本

c# - 路径中的非法字符。将 Skip and Take 与 IEnumerable 一起使用时出错

C# Await、空合并、字符串插值无法编译

c# - xna 防止下坡或上坡太陡

java - 渲染到 JPanel 的最快方法是什么?

ios - iOS 默认使用脏区域吗?

c# - 如何将数据库中的数据插入到Jquery脚本中