Move horizontally to shift the window.

0:01 / 0:30
Playback speed

Fresnel reflections

Sweep the window across the spheres. Why can you still see it at the rim when face-on reflectance is low?

Interactive demo#LightAndGlow3D lighting

A glazed surface reflects more of its surroundings when you see it at a grazing angle.

Trace a reflected view direction into a simple studio environment with a bright window. Schlick’s approximation blends this reflection into a teal material. Its face-on reflectance is adjustable, while the rim approaches a mirror at grazing angles.

Code

src/demos/fresnel_reflection.goose
namespace fresnel_reflection_lesson;
import stage;
import std;
import math;

var pixels: u8[64000] = [0;64000];

export "fresnel-reflection" fn draw(time: f64, dt: f64, px: f64, py: f64, hover: bool) {
    load_texture("studio");
    let reflectance = number_control("reflectance", 0.04);
    let window_x = if hover { px / 80 - 1 } else { 0.55 * sin(time * 0.55) };
    for y in 100 {
        for x in 160 {
            let n = sphere_surface(x, y);
            if n.radius == 0 {
                let backdrop = studio_colour(x, y);
                put_rgb(pixels[..], x, y, backdrop[0], backdrop[1], backdrop[2]);
                continue;
            }
            // Reflect the view ray around the normal and sample a studio window.
            let rx = 2 * n.nz * n.nx;
            let ry = 2 * n.nz * n.ny;
            let rz = 2 * n.nz * n.nz - 1;
            let u = (rx - window_x) / (1.32 + rz * 0.32);
            let in_window = abs(u) < 0.44 && ry > -0.32 && ry < 0.9;
            let pane = in_window && abs(u) > 0.021 && abs(ry - 0.26) > 0.025;
            let env = if pane { [251.0, 238.0, 204.0] }
                      else if ry < -0.32 { [139.0, 83.0, 49.0] }
                      else { [43.0 + 16 * ry, 74.0 + 24 * ry, 83.0 + 32 * ry] };
            let fresnel = reflectance + (1 - reflectance) * pow(1.0 - n.nz, 5.0);
            let base = 0.29 + 0.64 * max(0.0, n.ny * 0.76 - n.nx * 0.32 + n.nz * 0.37);
            put_rgb(pixels[..], x, y, mix_value(34 * base, env[0], fresnel),
                mix_value(160 * base, env[1], fresnel), mix_value(155 * base, env[2], fresnel));
        }
    }
    pixel_buffer(pixels[..], 160, 100);
}

Stage drawing helpers · All demos on GitHub

Comments

No comments yet.