User Tools

Site Tools


Glossy Highlights

This is a simple trick to create nice 'glossy' highlights in your shader.

To get the result in the example picture, we apply a smoothstep function to the phong light factor.

\[f = {smoothstep} ( {0.72-w, 0.72+w, {max}(0, N \cdot H)^{\frac{1}{roughness} }})\]

Where w = 0.18 * (1-sharpness). sharpness and roughness are parameters for tweaking the highlight and should be uniforms. roughness = 0.3 and sharpness = 0.63 are good default values. These parameters should be between 0 and 1.

The above term is only valid when $N \cdot L$ is positive. Otherwise you should clamp it to zero.

The smoothstep function can be defined like this:

static float smoothstep(float edge0, float edge1, float x) {
    // Scale, and clamp x to 0..1 range
    x = Math.min(1, Math.max(0, (x - edge0)/(edge1 - edge0)));
    // Evaluate polynomial
    return x*x*x*(x*(x*6 - 15) + 10);

Since this method produces very strong highlights, specular reflections for rim and fill lights might be undesirable. You might want to write your shader so that only a single key light-source creates a highlight. You could do this by only using the first light source in the _lights or by adding a uniform that defines the index of the used light. You should still add all lights together for diffuse lighting.


students/glossy-highlights.txt · Last modified: 2015/01/07 14:19 (external edit)