2011-11-15 70 views
0

我有一個非常簡單的像素着色器:爲什麼我的xna像素着色器將此紋理變成藍色?

float4 PixelShaderFunction(float2 uv : TEXCOORD0) : COLOR0 
{ 
    return float4(0, 1, 0, 1); 
} 

technique Technique1 
{ 
    pass Pass1 
    { 
     PixelShader = compile ps_3_0 PixelShaderFunction(); 
    } 
} 

我有一個質地:

 Vector4[] textureData = new Vector4[width * height]; 
     for (int y = 0; y < height; y++) 
     { 
      for (int x = 0; x < width; x++) 
      { 
       textureData[y * width + x] = new Vector4(1, 0, 0, 1); 
      } 
     } 
     myTexture = new Texture2D(GraphicsDevice, width, height, false, SurfaceFormat.Vector4); 
     myTexture.SetData(textureData); 

,我用這個代碼繪製:

 spriteBatch.Begin(SpriteSortMode.Texture, 
          BlendState.Additive, 
          SamplerState.PointWrap, 
          DepthStencilState.DepthRead, 
          RasterizerState.CullNone); 

     myEffect.CurrentTechnique.Passes[0].Apply(); 
     spriteBatch.Draw(myTexture, new Rectangle(0, 0, width, height), Color.White); 

     spriteBatch.End(); 

我會通過已經想通在像素着色器上調用.Apply(),隨後的spriteBatch.Draw()調用將通過我的像素着色器發送myTexure。由於像素着色器函數總是返回float4(0,1,0,1),所以我期望結果是一個綠色方塊,但是它呈現紅色,就好像像素着色器沒有觸摸它一樣。

我錯過了什麼?

回答

0

貌似我只需要改變SpriteSortMode爲Immediate,從3更改像素着色器版本2

0

你永遠不會在着色器上調用Begin(),所以它仍然會使用默認着色器。

此外,現在有一個更乾淨的方法。您可以將您的效果作爲參數傳遞給SpriteBatch開始呼叫,as detailed here

+1

無論是對他們的影響不是通行證對象已經開始()方法。它看起來像現在,而不是調用Begin(),你調用Pass對象上的.Apply() –

相關問題