2011-08-25 60 views
0

我已經實現了使用像素着色器文件的效果和過渡。當我單獨應用效果和轉換時,它工作正常,但如果我同時應用它,它不起作用。如何將多個着色器應用於精靈。下面是代碼我正在做什麼。對精靈的多重影響

_effect = Effect.FromFile(_parentRVRenderer.Device, path, null, ShaderFlags.None, null); 
_effect1 = Effect.FromFile(_parentRVRenderer.Device, path1, null, ShaderFlags.None, null); 
_effect.Technique = "TransformTexture"; 
_effect1.Technique = "TransformTexture"; 

_effect1.Begin(0); 
_effect1.BeginPass(0); 
_effect.Begin(0); 
_effect.BeginPass(0); 
sprint.Begin() 
Sprite.Draw(); 
.... 

回答

1

將兩個像素着色器函數放在同一着色器中,並使用兩遍技術在每遍中應用不同的像素着色器。

如上所述,您仍然必須使用兩個渲染目標來將輸出從第一遍反轉到另一個渲染目標,但最好使用雙通道方法而不是將渲染目標發送到其他着色器技術。

僞代碼:

RenderTarget2D[2] targets; 

// (Draw all your sprites to target 0) 

// target 1 will be empty, will be used in pass 0 (even pass) 

effect.Technique = "TwoPassTechnique"; 

for (int i = 0; i < effect.Passes.Count; i++) 
{ 
    // Even pass sets target 1, odd pass sets target 0 
    GraphicsDevice.setRenderTarget(targets[1 - i % 2]); 
    effect(i).BeginPass; 

    // Even pass samples texture from target 0, odd pass uses target 1 
    effect(i).Parameters["texture"].SetValue(targets[i % 2]); 

    // Draw a 2D quad with extents (-1, -1), (1, 1) in screen space 
} 
// Final contents are now stored in target 0 

// (Draw target 0's texture to the screen, using a sprite or another 2D quad) 
0

我不確定是否可以同時應用2個着色器。但是我要做的是使用第一個着色器將精靈繪製到渲染目標,然後使用第二個着色器將結果圖像繪製到屏幕上。

很顯然,如果你可以將效果結合到一個着色器中,那將是理想的,但這並不總是可能的。這可能不是最好的解決方案,但它應該做到這一點。

+0

發您好,感謝您的答覆,你可以請這方面的任何代碼示例。 – Firoz

+0

只需使用RenderTarget2D創建渲染目標MyRenderTarget = new RenderTarget2D(graphicsDevice,width,height);然後使用GraphicsDevice.SetRenderTarget(MyRenderTarget);像正常一樣繪製,然後將渲染目標設置爲空(將其設置到後臺緩衝區)並再次繪製,這次使用MyRenderTarget作爲繪製中的Texture參數。 –

+0

http://pastebin.com/kRJLm3r2繼承人一個非常粗糙的例子。 –