How it works...

The fragment shader starts by retrieving a noise value from the noise texture (NoiseTex), and storing the result in the noise variable. We want noise that has a large amount of high-frequency fluctuation, so we choose four-octave noise, which is stored in the alpha channel (noise.a).

We then discard the fragment if the noise value is below LowThreshold or above HighThreshold. As the discard keyword causes the execution of the shader to stop, the rest of the shader will not execute if the fragment is discarded.

The discard operation can have a performance impact due to how it might affect early depth tests.

Finally, we compute the shading model and apply the result to the fragment.

Get OpenGL 4 Shading Language Cookbook - Third Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.