1

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

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();  
}
4

1 回答 1

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;
}
于 2013-02-05T14:26:39.713 回答