Monogame and .fx files?

断了今生、忘了曾经 提交于 2019-12-10 09:44:36

问题


I'm currently following this tutorial but using MonoGame :
http://www.riemers.net/eng/Tutorials/XNA/Csharp/Series1/Terrain_from_file.php
As said in the tutorial, my main goal is to render a terrain from an image file.

There is a .fx provided with the tutorial which I included in my project.

I had some issues using MonoGame to load the bmp file for example and now I managed to load it. The problem comes now from the fx file. MonoDevelop tells this : The MGX File is Corrupt !

Here is the original code from the writer of the article :
effect = Content.Load<Effect> ("effects");

And here is how I used it with MonoGame :
effect = Content.Load<Effect> ("effects.fx");

I am really lost about the usage of the effects file in MonoGame. Is there any good tutorial about this ? Anyway I'm really lost with MonoGame. How come there is no clear tutorials for MonoGame has it is widely used ?


回答1:


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.




回答2:


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);



回答3:


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");


来源:https://stackoverflow.com/questions/23733470/monogame-and-fx-files

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!