簡體   English   中英

MonoGame 2D PixelShader無法正常工作

[英]MonoGame 2D PixelShader not working

我正在嘗試在Monogame中實現PixelShader。

着色器(目前)應該只是獲取一個Texture,然后不做任何操作就將其返回。

sampler MyTex : register(s0);

float4 PixelShaderFunction( float2 coords: TEXCOORD0 ) : COLOR0
{
    float4 Color = tex2D(MyTex, coords);
    return Color;
}

technique tech
{
    pass Pass1
    {
        PixelShader = compile ps_4_0_level_9_1 PixelShaderFunction();
    }
}

我的Monogame實現看起來像這樣:

在LoadContent中:

spriteBatch = new SpriteBatch(GraphicsDevice);
texture = Content.Load<Texture2D>("surge");
GraphicsDevice.Textures[0] = texture;
effect = Content.Load<Effect>("sha");

並在Draw方法中:

GraphicsDevice.Clear(Color.Aquamarine);

spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend,null, null, null, effect, null);
spriteBatch.Draw(texture, new Vector2(150, 150), Color.White);
spriteBatch.End();

但是它什么也沒顯示。 將BlendState更改為“不透明”會在紋理應該放置的地方給我一個黑色的矩形。 我還嘗試將其更改為其他Sortmodes和BlendStates失敗。 我似乎找不到問題。 因此,任何有助於解決問題或減少自我仇恨的答案都將受到高度贊賞!

編輯:在整個着色器模型版本中,資源綁定(注冊)是否發生更改的問題? 有人請幫忙!

嗨,Steffen,我已經嘗試過使用此代碼實現灰度效果

texture Texture;
sampler TextureSampler = sampler_state
 {
   Texture = <Texture>;
 };

//此數據來自精靈批量頂點着色器

struct VertexShaderOutput
{
  float4 Position : TEXCOORD0;
  float4 Color : COLOR0;
  float2 TextureCordinate : TEXCOORD0;
};

//我們的像素着色器

float4 PixelShaderFunction(VertexShaderOutput input) : COLOR0
{
   float4 color = tex2D(TextureSampler, input.TextureCordinate);

   float value = 0.299*color.r +  0.587*color.g + 0.114*color.b;
   color.r = value;
   color.g = value;
   color.b = value;
   color.a = 1.0f;

   return color;
}

//編譯着色器

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

或者,你可以嘗試此鏈接

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM