Chapter 01Materials

As mentioned in Understanding Meshes, shaders (vertex shaders and pixel shaders) are pieces of code that are executed on the GPU to project a 3D scene onto a 2D screen.

When a scene is rendered, the rendering engine processes every facets defined in the index buffer. As you recall, the index buffer specifies the indices of the 3 vertices that make up each facet. Recall also, that given the index of a vertex, we can determine the position (in local space) of the vertex using the vertex buffer.

The rendering engine computes a world matrix position of the 3 vertices in hand, the rendering engine executes the vertex shader (on the GPU) for each of the 3 vertices, passing to the vertex shader information about the vertex, such as its position, color, etc.

Once the pixel locations for the 3 vertices of a facet are determined by the vertex shader, the GPU interpolates the set of all pixels that will be used to render the facet and executes the pixel shader for each pixel in that set to determine their color and luminosity.

There are many ways for us to choose a set of shaders to use. As we saw in the previous section, one way is to simply specify a color for each vertex and let the default shaders choose the pixels and interpolate the colors. Another way is to provide custom shaders code. More common, however, is to use the built in BJS material classes.

Materials

BJS provides a number of classes, called materials, that encapsulate a vertex shader and pixel shader. Materials provides an easy way to configure the shaders and allow a particular set of shaders to be used by multiple meshes.

We can apply materials to facets and for meshes that have identifiable faces, we can apply materials to individual faces.

References