Monogame and .fx files?

徘徊边缘 提交于 2019-12-06 01:26:10

You need to convert your shader .fx to appropriate file format for monogame using 2MGFX tool. You can find the tool inside installed monogame directory C:\Program Files (x86)\MSBuild\MonoGame\v3.0

How to use:

  1. Create a .bat file and write code as shown below:
  2. 2MGFX.exe effects.fx effects.mgfxo pause
  3. Execute the .bat file

Note that the shader file, .bat file and 2MGFX.exe must be in same directory.

Here is how to use compiled .mgfxo file as effect:

  1. Put the effects.mgfxo into Assets\Content folder of your project
  2. Load a file as shown below

Stream s = Assembly.GetExecutingAssembly().GetManifestResourceStream("ProjectNameSpace.Assets.Content.effects.mgfxo");
BinaryReader Reader = new BinaryReader(s);
Effect effect = new Effect(graphics, Reader.ReadBytes((int)Reader.BaseStream.Length)); 

If you have problems converting a shader .fx to .mgfxo please leave comments.

I've been trying to follow Riemers tutorial myself, like you, I struggled with the effects.

I had to make a couple changes to the original effects file before I successfully managed to compile and use it without any exceptions.

Renamed the following:

  • vs_2_0 to vs_4_0
  • ps_2_0 to ps_4_0
  • POSITION to SV_POSITION

Once these changes were made I used the compile tool like following:

2MGFX.exe effects.fx effects.mgfxo /Profile:DirectX_11

Once compiled I moved the mgfxo file into my contents folder and assigned following parameters:

  • Build action: Embedded resource
  • Copy to output directory: Copy always

It took me a couple of attempts until I managed to use the shader without MonoGame throwing any exceptions at me.

byte[] bytes = File.ReadAllBytes("Content/effects.mgfxo");
effect = new Effect(GraphicsDevice, bytes);

Using the 2MGFX tool is optional, you can either use the tool or the Content pipeline, personally I prefer the Content pipeline because it will automatically process the shader file everytime I (re)build the Content project.

How to do this?

  1. First: add a MonoGame Content project,
  2. Then add the .FX file in this project
  3. Set the Content processor to: "MonoGame effect content processor" in properties
  4. Then, in your game project Add a Reference to this Content project.

And use the shader like so:

var myEffect = Content.Load<Effect>("shaderFileNameWithoutExtension"); 

or if you have folders in your content project:

var myEffect = Content.Load<Effect>("FolderName\\shaderFileNameWithoutExtension");
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!