MSDN Library

How to: Create a Mesh

[This documentation is for preview only, and is subject to change in later releases. Blank topics are included as placeholders.]

You can create a mesh in four basic ways:

  • By loading the mesh data from a file.

  • By cloning or optimizing an existing mesh.

  • By using a shape creation function and specifying the size and the number of triangles that will be used to create the shape.

  • By using the Mesh constructor.

Note Note

Managed Direct3D mobile applications require Windows Mobile version 5.0 software for Pocket PCs and Smartphones. See External Resources for the .NET Compact Framework for information about Windows Mobile software and SDKs.

To create a mesh from a file

  • Load the mesh data from a file, and then fill a mesh with the data. The .NET Compact Framework does not directly support loading a mesh from a file, but the Direct3D Mobile Meshes Sample defines a class to load a mesh.

To create a mesh from an existing mesh

  • Use the Optimize method to create a new mesh with optimized data.


    Use the OptimizeInPlace method to optimize the current mesh.

    The primary use for cloning is to convert the mesh from floating-point to fixed-point format. The primary use for optimizing is to create a mesh that is faster to draw. Mesh optimization rearranges the triangles in the mesh so that drawing calls on the mesh perform faster. Mesh optimization also generates an attribute table, which is used to identify areas of the mesh that need to be drawn with different textures, render states, and materials.

To create a mesh by using a shape-creation function

  • Use one of the following static methods of the Mesh class to create a mesh with position and normals specified in floating-point math:

To create a mesh by using the Mesh constructor

  1. Call the Mesh constructor with the desired arguments.

  2. Set the index buffer, vertex buffer, and attribute table data. The data in this case is often generated at run time. The following example shows the steps to create a mesh in this manner.

The following code example creates a heightfield mesh on the x-y plane with the z-coordinate representing the vertical dimension. The particular mesh created runs from (0, 0) to (1, 1) and has a height specified by the GetHeight method. This mesh is also textured with a single texture over the entire mesh. The tessellation parameter, defined by the example, is used to control how many points along the edge of the mesh are used.

Class Form1

    Private Sub New()
        ' In this example, initialize the mesh with
        ' 4 tessellations
    End Sub

    Private Sub InitializeMesh(ByVal tessellation As Integer)
        Dim mesh1 As Mesh = CreateHeightfieldMesh(tessellation)
    End Sub

    Private Function GetHeight(ByVal x As Single, ByVal y As Single) As Single
        Return 0

    End Function

    Private Function CreateHeightfieldMesh(ByVal tessellation As Integer) As Mesh
        Dim mesh As Mesh

        Dim device As Device = Nothing
        Dim arrayIndices((tessellation - 1) * (tessellation - 1) * 6) As Short
        Dim arrayVertices(tessellation * tessellation) As CustomVertex.PositionTextured
        Dim attributeRange As New AttributeRange()

        ' Create mesh with desired vertex format and desired size.
        mesh = New Mesh(arrayIndices.Length / 3, arrayVertices.Length, MeshFlags.SystemMemory, CustomVertex.PositionTextured.Format, device)

        ' For each point in the height field calculate the x, y, z and
        ' texture coordinates.
        Dim y As Integer
        For y = 0 To tessellation
            Dim x As Integer
            For x = 0 To tessellation
                Dim arrayIndex As Integer = y * tessellation + x
                Dim xCoordinate As Single = System.Convert.ToSingle(x) / System.Convert.ToSingle(tessellation - 1)
                Dim yCoordinate As Single = System.Convert.ToSingle(y) / System.Convert.ToSingle(tessellation - 1)
                Dim vertex As New CustomVertex.PositionTextured(xCoordinate, yCoordinate, GetHeight(xCoordinate, yCoordinate), xCoordinate, yCoordinate)
                arrayVertices(arrayIndex) = vertex
            Next x
        Next y

        ' Calculate the index buffer.
        Dim z As Integer
        For z = 0 To (tessellation - 1)
            Dim x As Integer
            For x = 0 To (tessellation - 1)
                Dim arrayIndex As Integer = (z * (tessellation - 1) + x) * 6
                Dim vertexIndex As Integer = z * tessellation + x

                arrayIndices(arrayIndex) = Fix(vertexIndex)
                arrayIndices((arrayIndex + 1)) = Fix(vertexIndex + 1)
                arrayIndices((arrayIndex + 2)) = Fix(vertexIndex + tessellation)
                arrayIndices((arrayIndex + 3)) = Fix(vertexIndex + tessellation)
                arrayIndices((arrayIndex + 4)) = Fix(vertexIndex + 1)
                arrayIndices((arrayIndex + 5)) = Fix(vertexIndex + tessellation + 1)
            Next x
        Next z

        ' There is only one attribute value for this mesh.
        ' By specifying an attribute range the DrawSubset function
        ' does not have to scan the entire mesh for all faces that are
        ' are marked with a particular attribute ID.
        attributeRange.AttributeId = 0
        attributeRange.FaceStart = 0
        attributeRange.FaceCount = arrayIndices.Length / 3
        attributeRange.VertexStart = 0
        attributeRange.VertexCount = arrayVertices.Length

        mesh.VertexBuffer.SetData(arrayVertices, 0, LockFlags.None)
        mesh.IndexBuffer.SetData(arrayIndices, 0, LockFlags.None)
        mesh.SetAttributeTable(New AttributeRange() {attributeRange})

        Return mesh

    End Function

    Public Shared Sub Main()
            Dim Form1 As New Form()
        Catch e As NotSupportedException

            MsgBox("Your device does not have the " + _
                "needed 3d support to run this sample")

        Catch e As DriverUnsupportedException

            MsgBox("Your device does not have the " + _
                "needed 3d driver support to run this sample")

        Catch e As Exception

            MsgBox("The sample has run into an error and " + _
                "needs to close: " + e.Message)
        End Try

    End Sub
End Class

© 2016 Microsoft