Importing meshes at runtime

Hello everyone again, I’m in the process to finally add asset importers in my project and need a bit of help of where to look for.

I use custom binary formats for meshes and parse them at runtime to get the info I need to make a mesh, and that is where I’m getting some problem as I don’t know where to look for t ocreate a mesh that I can feed to Draw, not even sure monogame has any class for that. Th parsing in itself isn’t a problem, all my mesh format has is series of Vector3 for vertices, normals and Vector2 for uvs, (also an array of ints for tris).

All the inf oI find around internet is about using the Content pipeline which I want to avoid as I want users to be able to throw their own assets in the game, any of you can direct me on some info on how to draw a custom mesh or if there is some helper in monogame to do so?

Thanks.

If you can export your “custom binary format” to what MG is supported, that would be nice and less hassle Assimp can import most of popular mesh formats ^_^y

But if you really can’t and want to roll your own it’s not that hard :

_"Th parsing in itself isn’t a problem, all my mesh format has is series of Vector3 for vertices, normals and Vector2 for uvs, _(also an array of ints for tris). "

CREATION :

  1. Save your for vertices, normals and UV to __Vertices array with specific vertex type.
  2. Create a __Vertex Buffer.
  3. Set Data to the __Vertex Buffer from __Vertices array you created.

RENREDING :

  1. Set your __Verter Buffer to GraphicsDevice using SetVertexBuffer( __Vertex_Buffer);
  2. Draw in draw pass using DrawPrimitives( Type, 0, count );

This is just pseudo guide you have to set your basic effect or custom effect on your rendering routine.

1 Like

Thank you that is what I was looking for.

1 Like

It’s about as simple as @DexterZ describes. If you’re using Effects then don’t forget to set them up Begin/End etc.

    // Just draws the mesh with the current Effect state.
    // Effect state is set beforehand.
    // Shipped.
    public void Draw(GraphicsDevice device, Effect effect)
    {
        device.BlendState = BlendState.NonPremultiplied;
        device.SetVertexBuffer(VertexBuffer);
        device.Indices = IndexBuffer;
        foreach (EffectPass pass in effect.CurrentTechnique.Passes)
        {
            pass.Apply();
            if (IndexBuffer == null || IndexBuffer.GraphicsDevice == null || device == null)
                break;
            device.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, IndexBuffer.IndexCount / 3);
        }

        device.SetVertexBuffer(null);
        device.Indices = null;
    }
2 Likes

Thnak you, out of curiosity, why do you null indices and buffer at the end of the call?

@Neurological, that’s just specific to my stuff. Where that’s used there’s a lot of procedural mesh generation so null’ing those out keeps as few GC handles as possible around. Usually wasn’t a problem but for some more intense models (~60k vertices) it was problematic to leave anything living for even just a little too long on old Intel HDs.

It isn’t strictly necessary, nor is the if check before hand.