2015-09-07 47 views
0

我想要做的是能夠在spriteBatch中使用添加混合繪製一組特定的精靈。問題是,抽籤順序,他們在需求正在拉動而保留,我不能得出一切在SpriteBatch與添加劑混合,所以我不能只是這樣做:C#XNA EffectPass.Apply()不做任何事情

spriteBatch.Begin(SpriteSortMode.FrontToBack, BlendState.AlphaBlend); 
    //Draw some stuff here 
spriteBatch.End(); 

spriteBatch.Begin(SpriteSortMode.FrontToBack, BlendState.Additive); 
    //Draw stuff with additive blending here 
spriteBatch.End(); 

所以我解決辦法是寫一個着色器做什麼,我需要,只是這樣做:

spriteBatch.Begin(SpriteSortMode.FrontToBack, BlendState.AlphaBlend); 
    //Draw some stuff here 

    foreach(EffectPass pass in AdditiveShader.CurrentTechnique.Passes) 
    { 
     pass.Apply() 
     //Draw stuff with additive shader applied here 
    } 
spriteBatch.End() 

pass.Apply()簡直是無所事事。即使我嘗試使用BasicEffect並旋轉幾度,它也什麼都不做。我能得到它做任何事情的唯一方法是調用它是這樣的:

spriteBatch.Begin(SpriteSortMode.FrontToBack, BlendState.AlphaBlend, 
        null, null, null, AdditiveShader); 

然後它實際上做的東西精靈,但是這並不能真正幫助我,因爲我只想把它應用到具體的精靈並仍然保留抽籤順序。

我在使用pass.Apply()時做錯了什麼?有沒有一種方法可以通過添加混合來繪製一組精靈,而另一套精靈則可以通過alpha混合繪製並仍然保持繪製順序?任何幫助將不勝感激。謝謝。

編輯:澄清,我正在二維工作。

+0

您試過使用與後臺緩衝區不同的RenderTarget?這樣你就可以改變任何狀態(混合,深度,模板)而不會產生干擾,然後只是一個接一個地渲染結果。 – LInsoDeTeh

+0

@LInsoDeTeh這部分代碼已經渲染到名爲'GameRenderTarget'的RenderTarget2D。然後,我將所有目標層疊起來,並在繪圖方法結束時將其推送到後緩衝區。你能給出一個你的意思的代碼示例嗎?我不完全明白。你的意思是我可以在不干擾的情況下改變'spriteBatch'的狀態嗎?示例代碼會很好。 –

+0

我的意思是你可以做一些像(僞代碼,因爲我不在家):graphicsDevice.SetRenderTarget(target1); graphicsDevice.BlendState = blendState1,graphicsDevice.StencilState = stencilState1; spriteBatch.Begin(); DrawWhatever(); spriteBatch.End(); graphicsDevice.SetRenderTarget(TARGET2); graphicsDevice.BlendState = blendState2,graphicsDevice.StencilState = stencilState2; spriteBatch.Begin(); DrawSomethingElse(); spriteBatch.End(); graphicsDevice.SetRenderTarget(NULL); spriteBatch.Begin(); spriteBatch.Draw((紋理)目標1); spriteBatch.Draw((紋理)TARGET2); spriteBatch.End(); – LInsoDeTeh

回答

0

好的,所以我發現(感謝肖恩哈格里夫斯 - http://blogs.msdn.com/b/shawnhar/archive/2010/04/05/spritebatch-and-custom-renderstates-in-xna-game-studio-4-0.aspx)是我可以使用GraphicsDevice.BlendState = BlendState.Additive在運行中更改BlendState

因此,我所做的是:

spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.Alpha); 
    //Draw with alpha blending here 

    GraphicsDevice.BlendState = BlendState.Additive;   
    //Draw specific sprites with additive blending here 

    GraphicsDevice.BlendState = BlendState.Alpha; //Revert the blendstate to Alpha 
    //Draw more stuff here with alpha blending 

spriteBatch.End(); 

的問題是,SpriteSortModespriteBatch已被設置爲ImmediateGraphicsDevice.BlendState = BlendState.Additive不會做任何事情。

所以我想我必須使用自定義DepthStencilState來複制SpriteSortMode.FrontToBack的功能。