Sign in to follow this  

Dynamically Create ModelMesh (MeshContent->ModelMesh)

This topic is 1930 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am having a problem, I want to Dynamically Create a ModelMesh at runtime rather then loading a file.

[CODE]
public static ModelMesh BuildMesh(String name)
{
MeshBuilder builder = MeshBuilder.StartMesh(name);
// build the mesh
MeshContent finishedMesh = builder.FinishMesh();

return finishedMesh as a ModelMesh?
}
[/CODE]

But I don't know how to convert a MeshContent to a ModelMesh?
Does anyone have an idea?

Thanks

Share this post


Link to post
Share on other sites
Hi there, MeshBuilder is a part of the content pipeline, which depends on MsBuild. And due the way in which the mechanism works, it is only supposed to be used within the content pipeline (or a pipeline extension library), the information that MeshBuilder creates is intended for either the built in model processor, or your own custom processor. If you wish to be able to build the equivalent of the functionality ModelMesh provides, you may have to do this yourself, however there are plenty of tutorials on the internet that will show you how to do this.

Just incase this is new to you, here is a quick checklist for achieving this:

- Create an array of vertices (VertexPositionColor is one, but you can use other presets, and even create your own type).
- Create an array of indices for faces, it's normal to have them in multiples of 3, and each index refers to a vertex.
- In your draw call, call one of the appropriate Draw... methods, here's a quick example:

[CODE]
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
namespace FastTriExample
{
public class Game1 : Game
{
GraphicsDeviceManager graphics;
VertexBuffer vbo;
BasicEffect shader;

public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
}

protected override void LoadContent()
{
vbo = new VertexBuffer(GraphicsDevice, typeof(VertexPositionColor), 3, BufferUsage.WriteOnly);
vbo.SetData<VertexPositionColor>(new VertexPositionColor[]{
new VertexPositionColor(Vector3.Up, Color.White),
new VertexPositionColor(Vector3.Down + Vector3.Right, Color.Blue),
new VertexPositionColor(Vector3.Down + Vector3.Left, Color.Red)
});
float aspectRatio = GraphicsDevice.Viewport.AspectRatio;
shader = new BasicEffect(GraphicsDevice);
shader.World = Matrix.CreateScale(0.5f);
shader.View = Matrix.CreateLookAt(Vector3.Backward, Vector3.Zero, Vector3.Up);
shader.Projection = Matrix.CreatePerspectiveFieldOfView(MathHelper.PiOver2, aspectRatio, 0.1f, 2);
}

protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
shader.CurrentTechnique.Passes[0].Apply();
GraphicsDevice.SetVertexBuffer(vbo);
GraphicsDevice.DrawPrimitives(PrimitiveType.TriangleList, 0, 1);
}

}
}
[/CODE]

Have fun! Edited by AmzBee

Share this post


Link to post
Share on other sites
Sign in to follow this