为MonoGame编译着色器

4
我正在使用VS 2013尝试使像素着色器正常工作。我已经在XNA 4中使该着色器工作了,所以我确信它是没问题的。我正在尝试使用2MGFX工具编译着色器。只需要运行以下代码:2MGFX.exe AlphaMap.fx AlphaMap.fxg,即可生成已编译的AlphaMap.fxg文件。但是,当我尝试在MonoGame中使用/加载此文件时,会出现"The MGFX effect is the wrong profile for this platform!"的错误。解决此问题的方法似乎是在2MGFX命令中添加/DX11参数,但这样会出现"Pixel shader 'PixelShaderFunction' must be SM 4.0 level 9.1 or higher! Failed to compile the input file '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

1个回答

7
尝试更改这行代码:
    PixelShader = compile ps_2_0 PixelShaderFunction();

into:

    PixelShader = compile ps_4_0_level_9_1 PixelShaderFunction();

顺便问一下,你为什么不使用MonoGame内容模板?


我会使用内容模板,但我无法让它在VS 2013中工作。 - JensB
这个更改使错误消失了,我现在可以加载效果文件。然而,它似乎并不起作用。使用效果时屏幕上没有显示任何内容(如果不使用效果,我可以很好地绘制纹理)。 - JensB
请问您在使用VS 2013和内容模板时遇到了什么问题 - 我也曾经遇到过2MFGX的问题。我之前也遇到了VS 2013内容模板的问题,但是我找到了解决方法。 - fex
在这里提出了有关内容管道的单独问题:https://dev59.com/sXrZa4cB1Zd3GeqP7-DM - JensB

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接