Export (0) Print
Expand All

MeshBuilder Class

Provides support for writing a custom importer for mesh objects.

Namespace: Microsoft.Xna.Framework.Content.Pipeline.Graphics
Assembly: Microsoft.Xna.Framework.Content.Pipeline (in microsoft.xna.framework.content.pipeline.dll)

public sealed class MeshBuilder

MeshBuilder is designed to make it easier to write importers. It creates an abstraction of the internal workings of the MeshContent and GeometryContent classes and provides a simple way to create a mesh object.

There are three steps to creating a mesh.

  1. Call StartMesh to get a MeshBuilder object. To initialize this object, fill the positions buffer with data by using the CreatePosition function. After retrieving the positions data, call the CreateVertexChannel generic function to specify the types of vertex channels needed—for example, normals, UVs, and color channels.

    Bb197972.note(en-US,XNAGameStudio.10).gifNote
    The PositionCollection property of a MeshContent object is created automatically. Therefore, there is no need to explicitly declare a VertexChannel object for positions.
  2. After setting up the position and vertex data channel buffers, begin creating triangles. Use SetMaterial and SetOpaqueData to set the data of each triangle, and use SetVertexChannelData to set the individual vertex data of each triangle. After setting this data, call AddTriangleVertex for each vertex of each triangle.

    Bb197972.note(en-US,XNAGameStudio.10).gifNote
    If you call CreatePosition or CreateVertexChannel after calling AddTriangleVertex, an InvalidOperationException exception is thrown.
  3. In the final step, call FinishMesh. After this call, the mesh is optimized automatically with calls to MergeDuplicateVertices and CalculateNormals.

The following code creates a simple textured quad using the procedure described above.


// Step 1 - Create the mesh builder and create vertex data channels and positions.
MeshBuilder builder = MeshBuilder.StartMesh( "Plane" );
int texCoordChannel = builder.CreateVertexChannel<Vector2>
    (VertexChannelNames.TextureCoordinate( 0 ));

builder.CreatePosition( left, 0, bottom );
builder.CreatePosition( left, 0, top );
builder.CreatePosition( right, 0, top );
builder.CreatePosition( right, 0, bottom );

// Step 2 - Begin defining triangles. Set the per-triangle and per-vertex data first....
BasicMaterialContent material = new BasicMaterialContent();
material.DiffuseColor = new Vector3( 1, 1, 1 );
material.SpecularPower = 0.0f;
material.SpecularColor = new Vector3( 1, 1, 1 );
builder.SetMaterial( material );
builder.SetVertexChannelData( texCoordChannel, new Vector2( 0, 1 ) );

// ... and then begin adding triangle vertices. Note that MeshBuilder supports only triangle lists.
builder.AddTriangleVertex(0);     // creates a vertex with a uv of 0, 1
builder.SetVertexChannelData( texCoordChannel, new Vector2( 0, 0 ) );
builder.AddTriangleVertex(1);     // creates a vertex with a uv of 0, 0
builder.SetVertexChannelData( texCoordChannel, new Vector2( 1, 0 ) );
builder.AddTriangleVertex(2);

// The first triangle is finished; SetMaterial or SetOpaqueData could be called here.
builder.SetVertexChannelData( texCoordChannel, new Vector2( 0, 1 ) );
builder.AddTriangleVertex(0);
builder.SetVertexChannelData( texCoordChannel, new Vector2( 1, 0 ) );
builder.AddTriangleVertex(2);
builder.SetVertexChannelData( texCoordChannel, new Vector2( 1, 1 ) );
builder.AddTriangleVertex(3);

// Step 3 - Finalize the mesh object.
MeshContent finishedMesh = builder.FinishMesh();
      

MeshBuilder can also preserve topology information. This means that the same position can be referenced twice with two different per-vertex data values. The following code demonstrates this approach.


meshBuilder.SetVertexChannelData( normalChannelIndex, Vector3( 0, 1, 0) );
meshBuilder.AddTriangleVertex( myIndexBuffer[0] );
meshBuilder.SetVertexChannelData( normalChannelIndex, Vector3( 1, 0, 0) );
meshBuilder.AddTriangleVertex( myIndexBuffer[0] );      // same index as before
    

Windows XP SP2, Windows Vista

Show:
© 2014 Microsoft