TExcelTAD
Home › Shaders & Graphics Programming › Rim Light and Fresnel

Rim Light and Fresnel

Look at a glass of water, a car, or a wet road at a low angle: surfaces reflect much more light at grazing angles than when you look straight at them. This is the Fresnel effect, and it is everywhere in real-world materials. Games use it twice: physically, inside PBR lighting (see PBR), and artistically, as a rim light that outlines characters against the background, highlights a selected unit, or makes a force field glow at its edges.

The facing ratio

Both effects start with one number: how much the surface faces the camera. With n the surface normal and v the direction from the surface to the camera, dot(n, v) is 1 at the center of a sphere (facing you) and falls to 0 at its silhouette. Invert it and you have a rim mask:

glsl
float facing = max(dot(n, v), 0.0);
float rim = pow(1.0 - facing, power);   // 0 in the middle, 1 at the edges

The power controls how thin the rim is: 1 gives a wide gradient, 4–8 a thin edge.

glsl · try it yourself
float map(vec3 p) { return length(p) - 1.0; }
vec3 calcNormal(vec3 p) {
    vec2 e = vec2(0.001, 0.0);
    return normalize(vec3(map(p + e.xyy) - map(p - e.xyy),
                          map(p + e.yxy) - map(p - e.yxy),
                          map(p + e.yyx) - map(p - e.yyx)));
}

void mainImage(out vec4 fragColor, in vec2 fragCoord) {
    vec2 p = (fragCoord - 0.5 * iResolution.xy) / iResolution.y;
    vec3 ro = vec3(0.0, 0.0, 3.0);
    vec3 rd = normalize(vec3(p, -1.5));

    vec3 color = vec3(0.02, 0.02, 0.05);
    float t = 0.0;
    for (int i = 0; i < 80; i++) {
        float d = map(ro + rd * t);
        if (d < 0.001 || t > 10.0) break;
        t += d;
    }
    if (t < 10.0) {
        vec3 n = calcNormal(ro + rd * t);
        vec3 v = -rd;                                   // toward the camera
        float power = 1.0 + 3.0 * (0.5 + 0.5 * sin(iTime));   // animates 1..4
        float rim = pow(1.0 - max(dot(n, v), 0.0), power);
        color = vec3(rim);
    }
    fragColor = vec4(color, 1.0);
}

A rim-lit character

Add the rim on top of normal lighting, tinted with a color. A common refinement is to mask the rim so it only appears on the side facing the light (or only on the lower half), which reads as a backlight rather than a uniform halo:

glsl · try it yourself
float smin(float a, float b, float k) {
    float h = clamp(0.5 + 0.5 * (b - a) / k, 0.0, 1.0);
    return mix(b, a, h) - k * h * (1.0 - h);
}
float map(vec3 p) {
    float body = length(p - vec3(0.0, -0.2, 0.0)) - 0.7;
    float head = length(p - vec3(0.0, 0.75, 0.0)) - 0.45;
    return smin(body, head, 0.2);
}
vec3 calcNormal(vec3 p) {
    vec2 e = vec2(0.001, 0.0);
    return normalize(vec3(map(p + e.xyy) - map(p - e.xyy),
                          map(p + e.yxy) - map(p - e.yxy),
                          map(p + e.yyx) - map(p - e.yyx)));
}

void mainImage(out vec4 fragColor, in vec2 fragCoord) {
    vec2 p = (fragCoord - 0.5 * iResolution.xy) / iResolution.y;
    vec3 ro = vec3(0.0, 0.2, 3.5);
    vec3 rd = normalize(vec3(p, -1.6));
    vec3 keyDir = normalize(vec3(-0.6, 0.5, 0.6));          // front-left key light
    vec3 backDir = normalize(vec3(cos(iTime), 0.3, -0.8));  // moving back light

    vec3 color = mix(vec3(0.05, 0.05, 0.1), vec3(0.15, 0.1, 0.25), fragCoord.y / iResolution.y);
    float t = 0.0;
    for (int i = 0; i < 100; i++) {
        float d = map(ro + rd * t);
        if (d < 0.001 || t > 10.0) break;
        t += d;
    }
    if (t < 10.0) {
        vec3 n = calcNormal(ro + rd * t);
        vec3 v = -rd;
        float diffuse = max(dot(n, keyDir), 0.0);
        float rim = pow(1.0 - max(dot(n, v), 0.0), 3.0);
        float rimMask = smoothstep(-0.2, 0.4, dot(n, backDir));     // only where the back light reaches
        vec3 base = vec3(0.5, 0.45, 0.6) * (0.1 + 0.8 * diffuse);
        color = base + vec3(0.4, 0.8, 1.0) * rim * rimMask * 1.5;
    }
    fragColor = vec4(pow(color, vec3(1.0 / 2.2)), 1.0);
}

Schlick's approximation

The physically based version of the same curve is Schlick's approximation of the Fresnel equations. It says how much light a surface reflects at a given viewing angle, starting from F0, the reflectance when looking straight on:

glsl
vec3 fresnelSchlick(float cosTheta, vec3 F0) {
    return F0 + (1.0 - F0) * pow(1.0 - cosTheta, 5.0);
}
Material F0 (linear)
water about 0.02
plastic, glass, most non-metals about 0.04
gold about (1.0, 0.71, 0.29)
silver about (0.95, 0.93, 0.88)

Notice the shape: pow(1 - cosθ, 5) is our rim mask with power 5. Every material, even a dull one, reflects close to 100% at grazing angles. The next shader uses Schlick to blend between a dark, see-through water-like surface and a reflected sky:

glsl · try it yourself
float map(vec3 p) { return length(p) - 1.0; }
vec3 calcNormal(vec3 p) {
    vec2 e = vec2(0.001, 0.0);
    return normalize(vec3(map(p + e.xyy) - map(p - e.xyy),
                          map(p + e.yxy) - map(p - e.yxy),
                          map(p + e.yyx) - map(p - e.yyx)));
}
vec3 sky(vec3 d) {
    return mix(vec3(0.9, 0.8, 0.7), vec3(0.2, 0.4, 0.8), clamp(d.y * 1.5 + 0.3, 0.0, 1.0));
}

void mainImage(out vec4 fragColor, in vec2 fragCoord) {
    vec2 p = (fragCoord - 0.5 * iResolution.xy) / iResolution.y;
    float a = iTime * 0.4;
    vec3 ro = vec3(3.0 * sin(a), 0.5, 3.0 * cos(a));
    vec3 fwd = normalize(-ro), right = normalize(cross(fwd, vec3(0, 1, 0))), up = cross(right, fwd);
    vec3 rd = normalize(p.x * right + p.y * up + 1.5 * fwd);

    vec3 color = sky(rd);
    float t = 0.0;
    for (int i = 0; i < 80; i++) {
        float d = map(ro + rd * t);
        if (d < 0.001 || t > 10.0) break;
        t += d;
    }
    if (t < 10.0) {
        vec3 n = calcNormal(ro + rd * t);
        float cosTheta = max(dot(n, -rd), 0.0);
        float F0 = 0.04;
        float F = F0 + (1.0 - F0) * pow(1.0 - cosTheta, 5.0);
        vec3 inside = vec3(0.02, 0.08, 0.1);              // what refracted light would show
        color = mix(inside, sky(reflect(rd, n)), F);
    }
    fragColor = vec4(pow(color, vec3(1.0 / 2.2)), 1.0);
}

The center of the sphere is nearly black (4% reflection), while the edges mirror the sky. That is why lakes look dark when you look down into them and bright toward the horizon.

Rim vs Fresnel: which one?

Artistic rim Schlick Fresnel
purpose readability, style, selection highlight physical reflection strength
inputs power, color, mask F0 from the material
added to final color (emissive) weights the specular / reflection term
in engines Shader Graph Fresnel Effect node, Unreal Fresnel node, Godot pow(1.0 - dot(NORMAL, VIEW), p) built into the PBR lighting models (URP Lit, Unreal Default Lit, Godot StandardMaterial3D)

Godot's VIEW and Unity's view direction both point from the surface toward the camera, matching v above. If your rim lights the center instead of the edges, your view vector points the other way.

Test yourself

With n the normal and v the direction to the camera, which value is largest at a sphere's silhouette?

  1. dot(n, v)
  2. 1.0 - dot(n, v)
  3. length(n)
  4. dot(n, n)

In Schlick's approximation, what is F0?

  1. The reflectance at grazing angles
  2. The reflectance when viewed straight on (normal incidence)
  3. The roughness
  4. The light intensity

What does raising the rim exponent from 1 to 6 do?

  1. Makes the rim wider
  2. Makes the rim thinner, concentrated at the silhouette
  3. Changes its color
  4. Inverts it