c# – 为MonoGame编译着色器

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

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

刚刚跑步

2MGFX.exe AlphaMap.fx AlphaMap.fxg

Works和我得到我编译的AlphaMap.fxg文件.

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

The MGFX effect is the wrong profile for this platform!

对此的修复似乎是将/ DX11添加到2MGFX命令,但后来我得到此错误

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,AlphaShader,_Common.Camera.View);

    spriteBatch.Draw(Planet,new Vector2(0,0),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

解决方法

尝试更改此行:

PixelShader = compile ps_2_0 PixelShaderFunction();

成:

PixelShader = compile ps_4_0_level_9_1 PixelShaderFunction();

顺便说一下 – 你为什么不使用MonoGame内容模板

相关文章

目录简介使用JS互操作使用ClipLazor库创建项目使用方法简单测...
目录简介快速入门安装 NuGet 包实体类User数据库类DbFactory...
本文实现一个简单的配置类,原理比较简单,适用于一些小型项...
C#中Description特性主要用于枚举和属性,方法比较简单,记录...
[TOC] # 原理简介 本文参考[C#/WPF/WinForm/程序实现软件开机...
目录简介获取 HTML 文档解析 HTML 文档测试补充:使用 CSS 选...