Search code examples
c#model3dxnamonogame

Loading .FBX file without Content Pipeline in Monogame


I was wondering if there was a way to load a simple .fbx file with just a cube into my game without using the monogame content pipeline. Here's the code I have currently, but I want to essentially just get rid of the requirement for the file to be .xnb

    public static Model LoadModel(string model)
    {
        Model outModel = null;
        // If model is already in memory, reference that instead of having to load it again
        if (Loaded3DModels.ContainsKey(model))
        {
            Loaded3DModels.TryGetValue(model, out outModel);
        }
        else
        {
            if (File.Exists(Game.gameWindow.Content.RootDirectory + "/" + model + ".fbx"))
            {
                outModel = Game.gameWindow.Content.Load<Model>(model + ".fbx");
                Loaded3DModels.Add(model, outModel);
            }
            else
            {
                Debug.LogError("The Model \"" + model + ".fbx\" does not exist!", true, 2);
            }
        }

        return outModel;
    }

Solution

  • So I managed to use AssImp to load a mesh, and then "Convert" it to something Monogame could render

        public Renderer3DComponent(Mesh mesh)
        {
            // Set up material
            Material = new BasicEffect(Game.graphics.GraphicsDevice);
            Material.Alpha = 1;
            Material.VertexColorEnabled = true;
            Material.LightingEnabled = false;
    
            // Tris
            for (int i = mesh.VertexCount - 1; i >= 0; i--)
            {
                var Vert = mesh.Vertices[i];
                Verts.Add(new VertexPositionColor(new Vector3(Vert.X, Vert.Y, Vert.Z), Color.White));
            }
    
            // Buffer
            Buffer = new VertexBuffer(Game.graphics.GraphicsDevice, typeof(VertexPositionColor), Verts.Count, BufferUsage.WriteOnly);
            Buffer.SetData(Verts.ToArray());
        }