A custom vertex can include any standard and custom data types. This enables you to provide functionality beyond the standard XNA Framework vertex declarations, declarations such as VertexPositionColor.

This topic shows you the concept with a custom vertex (of type VertexPositionColoredNormal) with three properties: a Vector3 position, a Color color, and a Vector3 normal. With this information, you can render 3D primitives with basic color and lighting effects.

The Complete Sample

The code in this topic shows you the technique. You can download a complete code sample for this topic, including full source code and any additional supporting files required by the sample.

Declaring the Custom Vertex

The requirements for a custom vertex are:

Properties, implemented using both standard and user-defined data types, containing the values for the custom vertex. For this example, three properties are declared representing the position, color, and normal of the vertex.

A public method called SizeInBytes.

An array of type VertexElement with each element representing a property of the custom vertex. For this example, the properties are represented by two Vector3 types and a Color type.

In addition, you can implement some standard methods, such as constructors and public property accessors. For this example, a constructor and public methods for the three properties are implemented.

To declare the custom vertex

At the top of the source file declaring your game class, declare a structure containing the necessary components, based on the requirements listed previously.

This part of the structure declaration declares a custom vertex with properties for the position, color, and vertex normal.

The custom vertex structure declaration is now complete. Initializing the basic effect is next.

Initializing the Effect and Camera

The next step initializes an effect (of type BasicEffect) and the related camera. These are used for basic coloring and lighting effects, and for viewing the 3D object. Although this step is not strictly necessary for implementing a custom vertex, it is provided to show you a common usage scenario for this custom vertex, which is the rendering of a simple 3D object with color and light effects.

To initialize the basic effect and camera

Declare variables for the basic effect and various projection matrices.

This completes the initialization of the camera and basic effect. The next step sets up a basic triangle strip using the custom vertex declared earlier in this topic.

Initializing the 3D Primitive

In this step, you initialize a vertex array with custom vertices implemented by VertexPositionColoredNormal. In addition to an indices array, the vertex array later renders a colored triangle strip with default lighting effects.

To initialize the triangle strip

Declare an array (of type VertexPositionColoredNormal), and then initialize the various properties of the vertex elements.

In this topic, a triangle strip (from 0,0 to 3,0 and on the plane z = 0) is drawn.

These simple lighting effects are the reason the VertexPositionColoredNormal custom vertex type was created. Because each vertex has a normal, along with a position and color, the basic effect determines where the triangle strip is highlighted and where any shadows lie. If you had instead used vertices, of type VertexPositionColor, there would be no normals to determine any lighting effects.