我正在使用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