我正在将XNA 3.0云效果转换为XNA 4.0但我收到错误
"The render target must not be set on the device when it is used as a texture."
它发生在这一行的第二个循环中:
mover.Parameters["PressureMap"].SetValue(PressureOffsets);
代码:
for (int i = 0; i < 10; i++)
{
graf.SetRenderTarget(rt5);
mover.Parameters["PressureMap"].SetValue(PressureOffsets);
mover.Parameters["DivergenceMap"].SetValue(Divergence);
mover.CurrentTechnique = mover.Techniques["Jacobi"];
mover.CurrentTechnique.Passes[0].Apply();
sp.Begin(SpriteSortMode.Deferred, BlendState.Opaque, SamplerState.PointWrap, DepthStencilState.DepthRead, RasterizerState.CullNone);
sp.Draw(Velocity, new Vector2(0, 0), Color.White);
sp.End();
graf.SetRenderTarget(null);
PressureOffsets = rt5;
}
我似乎无法在XNA 4.0中将纹理设置为效果参数,如果它已设置为rendertarget。 但我不知道如何将其转换为在XNA 4.0中工作:(
答案 0 :(得分:0)
我解决了。 必须使用新的rendertar更改rendertarget。 我使用了一个帮助器类“RenderTargetState”。
public RenderTargetState SetToDevice()
{
oldState = new RenderTargetState(GraphicsDevice);
GraphicsDevice.SetRenderTarget(new RenderTarget2D(GraphicsDevice, renderTargetWidth, renderTargetHeight));
return oldState;
}
public RenderTargetState BeginRenderToTexture()
{
oldState = SetToDevice();
return oldState;
}
public Texture2D EndRenderGetTexture()
{
RenderTargetState renderBuffer = oldState.SetToDevice();
oldState = null;
return renderBuffer.RenderTarget;
}
所以代码现在看起来像这样:
for (int i = 0; i < 10; i++)
{
rt5.BeginRenderToTexture();
graf.SetRenderTarget(rt5.RenderTarget);
mover.Parameters["PressureMap"].SetValue(PressureOffsets);
mover.Parameters["DivergenceMap"].SetValue(Divergence);
mover.CurrentTechnique = mover.Techniques["Jacobi"];
mover.CurrentTechnique.Passes[0].Apply();
sp.Begin(SpriteSortMode.Deferred, BlendState.Opaque);
sp.Draw(Velocity, new Vector2(0, 0), Color.White);
sp.End();
graf.SetRenderTarget(null);
PressureOffsets = rt5.EndRenderGetTexture();
}
答案 1 :(得分:0)
有一种更简单的方法可以做到这一点。
rt5 = PressureOffsets as RenderTarget2D
graf.SetRenderTarget(null);
PressureOffsets = rt5;