Alpha Mapping

Textures can be used for many different visual effects. Often the texture provides the surface's albedo. But it could instead be used to alter a vertex's position or a fragment's shading or depth in myriad ways. For example, if the texture has an alpha channel, it could be used as a mask, with the shader discarding any fragment whose alpha value is below some threshold.

A texture that is used to throw out some fragments and retain others is called an alpha map. The underlying geometry to which an alpha map is applied is often a simple triangle or quadrilateral, while the fragments that survive the alpha test form an intricate shape that would be too costly to express through vertices.

Consider this snowflake, which is modeled with just four vertices and an alpha map:

The quadrilateral bearing the texture is rendered in the usual way. The fragment shader looks up the color in the texture and discards the fragment if the alpha value is less than 0.5:

uniform sampler2D alphamap;
in vec2 mixTexPosition;
out vec4 fragmentColor;

void main() {
  vec4 alpha = texture(alphamap, mixTexPosition).a;
  if (alpha < 0.5) {
    discard;
  } else {
    fragmentColor = vec4(1.0);
  }
}

By itself, the alpha test leaves a harsh edge at the interface between discarded and retained fragments. If you prefer a smoother transition, you can assign the alpha intensity to fragmentColor instead of discarding. You'll also have to enable blending and deal with the issues that come with it. Namely, you'll need to render the scene in back to front order so that the semi-transparent fragments will properly mix with the surfaces behind them.

Alpha mapping is frequently used in particle systems, which are data structures that manage the properties of many small projectiles over time. So that many particles may be rendered quickly, each is rendered with simple geometry to which an alpha map is applied. The particles are often too small for the viewer to notice the ragged edges of the alpha test.