home/ modules/ lighting-and-shading-raymarched-surfaces

Lighting and shading raymarched SDF surfaces

  • learner can estimate SDF surface normals by finite differences and apply Lambert diffuse and Phong shading
  • learner can cast shadow rays and produce soft-shadow penumbra and ambient occlusion
  • learner can hand-paint fake bounce and outdoor three-point lighting for a cinematic look

Shade a raymarched outdoor SDF scene with finite-difference normals, three-point lighting, soft shadows with colorized penumbra, ambient occlusion, and hand-injected fake bounce lighting.

This module is where your sculpted SDF scene stops looking like a flat silhouette and starts looking like film. On a live-visuals rig — a single fullscreen fragment shader hot-reloading behind the music — lighting is the highest-leverage move you own: the same geometry reads as noon, dusk, or dream depending on how you shade it, and an audience feels that shift instantly even projected in a dark room.

The arc starts fully supported: light a single sphere. Since an SDF has no vertex normals, you first estimate them by sampling the gradient at nearby points (“Raymarching surface normals are estimated by sampling the SDF gradient”), then wire up the clamped normal-dot-light Lambert term and stack ambient, diffuse, and specular Phong-style. These two moves — normals and the diffuse dot product — recur in every shading line you will ever write, so they run as drills inside the whole task rather than in isolation. Next you march a second ray toward the light for shadows, upgrade it to soft penumbras by tracking how close the ray grazes occluders, and cheaply darken crevices with normal-offset ambient occlusion. Finally you go cinematic: the outdoor three-point rig (yellow sun key, blue sky fill, warm ground bounce), warm-tinted penumbras instead of grey ones, and hand-painted fake bounce light where physics would be too expensive.

Every required atom is a gate: skip any one and the capstone visibly fails — no normals, no shading; no penumbra tracking, no soft shadows; no painted light, no cinema. Supporting atoms deepen the look — colorized occlusion, fake subsurface rims, sky reflections, sane albedo values — and connect this shader craft to the BRDF and microfacet theory behind full physically based rendering.

Runnable examples

Generated from the context/ instrument corpus by concept (redistributable idioms only). Do not edit — regenerate with gen-module-examples.mjs.

gradient-ramp

gradient(0.3).out()

hydra-0170 · MIT

[fr, fr*0.5, 1-fr] >> rgb

punctual-0033 · CC0-1.0

surface-shading

float diff = max(dot(n, normalize(L)), 0.0); col *= diff;

glsl-0117 · public-domain

Atoms in this module

Required — these gate the capstone

The SDF gradient estimated by finite differences gives the surface normal needed for lighting
Concept L2 First instrument G
Raymarching surface normals are estimated by sampling the SDF gradient at nearby points
Procedure L3 Craft G
Diffuse (Lambertian) brightness is the clamped dot product of the surface normal and the light direction
Concept L2 First instrument G
The Phong model sums ambient, diffuse, and specular components to shade 3D surfaces
Concept L3 Craft G
Shadow rays use the same raymarching loop from the shading point toward the light to determine occlusion
Concept L2 First instrument G
SDF soft shadows track how close the shadow ray comes to occluders, not just whether it hits them
Concept L3 Craft G
SDF ambient occlusion samples the field at short offsets along the normal to detect nearby geometry
Concept L3 Craft G
Outdoor SDF shaders use three light sources: sun key light, sky dome fill, and ground bounce
Concept L3 Craft G
Injecting fake bounce/subsurface lighting by hand into a raymarched scene fakes realistic results without global illumination
Principle L3 Craft G
Colorizing the shadow penumbra with warm tones rather than grey makes rendered images more cinematic
Concept L3 Craft G

Supporting — enrichment, not gating

Raising per-surface occlusion to a power of the surface color simulates colorized secondary light bounces
Concept L3 Craft G
Procedurally painted occlusion using model-aware distance signals fills gaps left by sampled AO
Concept L3 Craft G
Fake subsurface scattering brightens silhouettes using the Fresnel term tinted by a flesh color
Concept L3 Craft G
Sky specular is computed by reflecting the view ray and checking if the reflection hits the sky
Concept L3 Craft G
The y-component of the ray direction provides a natural UV for sky gradient and cloud placement
Concept L2 First instrument G
A physically meaningful base albedo is around 0.18–0.2, not 1.0, for correct lighting response
Concept L2 First instrument G
BRDFs model how a surface reflects light as a function of incoming and outgoing directions
Concept L3 Craft G
Microfacet models represent rough surfaces as collections of tiny facets described by a normal distribution
Concept L3 Craft G