【问题标题】:MonoGame 2D PixelShader not workingMonoGame 2D PixelShader 不工作
【发布时间】:2017-05-29 08:55:34
【问题描述】:

我正在尝试在 Monogame 中实现一个 PixelShader。

着色器(目前)应该只获取一个纹理并在未经处理的情况下将其返回。

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 实现如下所示:

在加载内容中:

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

在绘制方法中:

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 更改为 Opaque 会给我一个黑色的 Rectangle 纹理应该在的位置。我还尝试更改为其他 Sortmodes 和 BlendStates,但没有成功。我似乎无法找到问题。 任何有助于解决问题或减少我因此而产生的自我仇恨的答案都非常感谢!

编辑:资源绑定(注册)是否在整个着色器模型版本中发生了变化?! 有人请帮忙!

【问题讨论】:

    标签: c# xna 2d shader monogame


    【解决方案1】:

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

    或者你可以试试这个linkthis

    【讨论】:

    • 感谢您的回答,但使用该着色器文件我什至无法编译。 MSBuild\MonoGame\v3.0\MonoGame.Content.Builder.targets(90,5): 错误 MSB3073:[..] 以代码 1 结束
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多