我只是在试验像素着色器。我找到了一个很好的模糊效果,现在我正在尝试创建一种一遍又一遍地模糊图像的效果。
我想怎么做:我想在应用模糊效果的 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);
在这一行中,您正在绘制纹理...自身...这不可能发生。
假设 buffer1
和 hellokittyTexture
已经正确初始化,替换这一行:
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/