3

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 ?

Depado
  • 4,811
  • 3
  • 41
  • 63

4 Answers4

2

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.

Wallstrider
  • 856
  • 1
  • 7
  • 22
  • I'm currently using Linux. I tried to recompile the tool "2MGFX" but sadly it didn't seem to work... Any workaround using Linux ? – Depado May 19 '14 at 14:14
  • Unfortunately I have no experience with linux. :( – Wallstrider May 19 '14 at 14:53
  • 1
    Having got this working with your answer (thanks!) I've now added the conversion as a pre-build event in VS. Also note that the `mgfxo` file needs to have its Build Action set to `EmbeddedResource`. – Mark Pattison Sep 29 '14 at 16:33
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);
Olivier
  • 571
  • 5
  • 8
1

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");
deherch
  • 659
  • 4
  • 9
1

I'm seeing you're working on Linux. Compiling shaders on Linux is a little difficult. In my own projects, I find the following resource especially helpful.

http://www.infinitespace-studios.co.uk/general/monogame-building-fx-shaders-on-a-mac-and-linux/

Following this, you're able to build shaders using the MonoGame Pipeline as normal (provided you have added the pipeline reference).

Hope this helps!

Lance K.
  • 53
  • 7
  • Please describe the example if possible. Link answers are generally not accepted unless retyping the entire page is the only way the idea will get across. If there is a specific thing that needs to be done, please write that in your answer, instead of the link. – Vendetta Feb 05 '20 at 16:05