多次应用同样的效果?

时间:2013-02-05 14:16:05

标签: xna shader

我遇到了一个我无法解决的问题。我必须多次使用相同的效果绘制纹理。我正在努力实现这样的目标:

spriteBatch.Begin(); 

// Use the effect multiple times with a different parameter:
lightEffect.Parameters["parameter"].SetValue(value1);
lightEffect.CurrentTechnique.Passes[0].Apply();      
lightEffect.Parameters["parameter"].SetValue(value2);
lightEffect.CurrentTechnique.Passes[0].Apply();                                        

spriteBatch.Draw(texture, new Vector2(0, 0), Color.White);

spriteBatch.End(); 

仅使用第二种效果。这就是为什么我决定每次使用效果多次将RenderTarget重绘为自身,但这也失败了,因为"The render target must not be set on the device when it is used as a texture"

graphicsDevice.SetRenderTarget(tempRenderTarget);

spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.AlphaBlend);
spriteBatch.Draw(texture, new Vector2(0,0), Color.White);
spriteBatch.End();           

foreach (Value value in values)
{
    spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.AlphaBlend);            

    lightEffect.Parameters["paramter"].SetValue(value);
    lightEffect.CurrentTechnique.Passes[0].Apply();                    

    spriteBatch.Draw(tempRenderTarget, new Vector2(0, 0), Color.White);

    spriteBatch.End();  
}

1 个答案:

答案 0 :(得分:2)

您必须使用两个交替渲染目标:

RenderTarget input;
RenderTarget output;

for (int i = 0; i < numberOfPasses; i++)
{
    // Bind the target that is currently the output
    graphicsDevice.SetRenderTarget(output);

    // Use the current input target as texture in your effect
    // ...

    // Swap input and output
    RenderTarget temp = input;
    input = output;
    output = temp;
}