Vertex Color (XNA Game Studio 4.0 Programming)

Now it’s time to add a little color to the quad. The last example outputted a single red color for each pixel. Let’s take the color for each vertex and pass it through the pixel shader so it can output the color.

The vertices that make up the quad have colors defined at each vertex, so the input data is already available—you are not taking advantage of the colors in the effect file.

To add vertex color support to the effect, update the VertexShaderInput to include a new field for the color.


The VertexShaderOutput also needs to be updated with the new Color field.


In the vertex shader, copy the input color into the output structure.


The color interpolates across the triangles and blends as the color changes from one vertex to another.

Finally, the color needs to return from the pixel shader. Remove the existing return statement and replace it with the following line:


Running the game now displays the quad with four unique colors for each vertex. The colors are interpolated across the face of the two triangles and should look like Figure 8.4.

Quad rendered with vertex coloring

Figure 8.4 Quad rendered with vertex coloring

Next post:

Previous post: