Loading a .X model from file at runtime

I’m converting an XNA 3.1 game over to MonoGame and have been converting/replacing old components as I encounter them, and I’ve got to my .X model loader. I’m wondering if there’s an established way to load a .X model from a file on disk into a MonoGame Model object.

My old XNA 3.1 model loader was a dirty hack that loaded the model with SlimDX and then used reflection to stuff the data into an XNA Model object. That also involved using reflection to pull out the raw pointer to the Direct3D9 graphics device the XNA GraphicsDevice was bound to and use that to build a SlimDX graphics device, very messy business and I’d like to avoid it in MonoGame if possible.


you don’t actually need to code an importer yourself.
You can handle it with the content pipeline tool.
Here is an example how I used Quake 4/DOOM 3 MD5 files with it.
When you load it into the tool make sure to select Open Asset Import thing (Assimp) as importer and processor (you can check it on my image).
My model code is based on the example from the Xamarin website Drawing 3D Graphics with Vertices in MonoGame

I hope I’d you help somehow :slight_smile:

Cheers :slight_smile:

Hey Memorix101,

Thanks for the response! I’m actually looking specifically for a way to load models at runtime from unprocessed model files (particularly .X). I’ve already got them loading via the pipeline tool and that works fine, but to help make my game more easily moddable I want it to be able to load user-supplied models that haven’t gone through the pipeline.

I achieved this in XNA 3.1 by loading .X format models via SlimDX and using reflection to stuff the SlimDX Model data into an XNA Model container, which worked fine. I could do the same again in MonoGame using SlimDX or SharpDX and it’ll probably work, but it’s a bit of a hack and I’m hoping there’s a better way.

Cheers :slight_smile:

You can include the importer and processor (and the assimp assemblies for your target platforms + the C# bindings) in your game and build, then load the included assets when the game starts up or when the modded assets are loaded in.

You don’t have to use our Model class. It is essentially just a hierarchical collection of vertex and index buffers with transform matrices and materials. You could write your own Model-like class structure and populate it more easily rather than going through reflection.

I don’t know if SharpDX has a .X loader, but we don’t use it. We use Open Asset Importer (AssImp) which is then processed into a platform-specific format. These are the steps you would have to replicate if you did not want to include the bulk of the content build pipeline into your project. Including the content build pipeline into your project will limit your game to 64-bit desktop platforms, but that may not be an issue.

Thanks man, good to know! Limiting modding to 64-bit desktops won’t be a problem as my game is already Windows-only and 64-bit adoption rates on Steam are now over 97%, so that sounds like a good plan. I recall there being some licensing problems in XNA where the content pipeline wasn’t technically allowed to be distributed, yet another benefit of having an open source replacement.