Move horizontally to rotate the light.

0:01 / 0:30
Playback speed

Dithered 3D lighting

Toggle the pattern while looking at the curve between light and shade. Which parts still use just one ink?

Interactive demo#LightAndGlow3D lighting

A repeating threshold pattern turns smooth 3D lighting into two ink colours.

Compute diffuse illumination on the sphere, then compare each value to a rank in a four-by-four Bayer matrix. More bright pixels survive on the lit side. Switch the pattern off to compare the same geometry and light against a single hard threshold. The studio backdrop bypasses this material shader.

Code

src/demos/dithered_lighting.goose
namespace dithered_lighting_lesson;
import stage;
import std;
import math;

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

export "dithered-lighting" fn draw(time: f64, dt: f64, px: f64, py: f64, hover: bool) {
    load_texture("studio");
    let angle = if hover { (px / 160 - 0.5) * PI } else { sin(time * 0.7) };
    let lx = sin(angle) * 0.8;
    let lz = cos(angle) * 0.8;
    let patterned = toggle("pattern");
    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;
            }
            let lambert = clamp(n.nx * lx + n.ny * 0.6 + n.nz * lz, 0.0, 1.0);
            let threshold = if patterned { bayer_threshold(x, y) } else { 0.5 };
            if lambert > threshold {
                put_rgb(pixels[..], x, y, 248, 208, 134);
            } else {
                put_rgb(pixels[..], x, y, 69, 47, 103);
            }
        }
    }
    pixel_buffer(pixels[..], 160, 100);
}

Stage drawing helpers · All demos on GitHub

Comments

No comments yet.