【问题标题】:Compiling shader for MonoGame为 MonoGame 编译着色器
【发布时间】:2014-01-03 20:15:47
【问题描述】:

我正在使用 VS 2013 并试图让像素着色器正常工作。我已经让这个着色器在 XNA 4 中工作,所以我很确定它没问题。

我正在尝试使用 2MGFX 工具编译着色器

只是跑步

2MGFX.exe AlphaMap.fx AlphaMap.fxg

工作,我得到我编译的 AlphaMap.fxg 文件。

但是,当尝试在 MonoGame 中使用/加载此文件时,我得到:

MGFX 效果是这个平台的错误配置文件!

解决此问题的方法似乎是将 /DX11 添加到 2MGFX 命令,但后来我收到此错误:

像素着色器“PixelShaderFunction”必须是 SM 4.0 级别 9.1 或更高! 输入文件“AlphaMap.fx”编译失败!

我做错了什么?

着色器代码。

uniform extern texture ScreenTexture;  
sampler screen = sampler_state 
{
    // get the texture we are trying to render.
    Texture = <ScreenTexture>;
};

uniform extern texture MaskTexture;  
sampler mask = sampler_state
{
    Texture = <MaskTexture>;
};

// here we do the real work. 
float4 PixelShaderFunction(float2 inCoord: TEXCOORD0) : COLOR
{

    float4 color = tex2D(screen, inCoord);
    color.rgba = color.rgba - tex2D(mask, inCoord).r;
    return color;
}

technique
{
    pass P0
    {
        // changed this to reflect fex answer
        PixelShader = compile ps_4_0_level_9_1 PixelShaderFunction();
    }
}

编辑

fex 的回答让我能够加载效果,但它现在似乎不起作用。

我是这样使用它的:

    Texture2D Planet = _Common.ContentManager.Load<Texture2D>("Materials/RedPlanet512");
    Texture2D AlphaMapp = _Common.ContentManager.Load<Texture2D>("Materials/Dots2");
    Effect AlphaShader = _Common.ContentManager.Load<Effect>("Effects/AlphaMap");

    AlphaShader.Parameters["MaskTexture"].SetValue(AlphaMapp);

    spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend, null, null, null, AlphaShader, _Common.Camera.View);

    spriteBatch.Draw(Planet,
        new Vector2(0, 0),
        null, Color.White, 0f,
        new Vector2(Planet.Width / 2, Planet.Height / 2),
        1f, SpriteEffects.None, 1f);
    spriteBatch.End();

这些是我使用的纹理:

http://www.syntaxwarriors.com/wp-content/gallery/alphamapping-gallery/redplanet512.png http://www.syntaxwarriors.com/wp-content/gallery/alphamapping-gallery/whitedots_0.png

【问题讨论】:

    标签: c# .net monogame


    【解决方案1】:

    尝试改变这一行:

        PixelShader = compile ps_2_0 PixelShaderFunction();
    

    进入:

        PixelShader = compile ps_4_0_level_9_1 PixelShaderFunction();
    

    顺便说一句 - 你为什么不使用 MonoGame 内容模板?

    【讨论】:

    • 我会使用内容模板,但我无法让它在 VS 2013 中工作。
    • 此更改使错误消失,我现在可以加载效果文件。然而,它似乎不起作用。使用效果时屏幕上没有任何显示(不使用效果我可以很好地绘制纹理)
    • 询问您对 VS 2013 和内容模板有什么问题 - 我也遇到了 2MFGX 的问题。我也遇到了 VS 2013 内容模板的问题,但我想出了如何完成它。
    • 在此处询问有关内容管道的单独问题:stackoverflow.com/questions/20903903/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多