Log inGet started
Package

shadows.shaderModule

Newest version

This package holds more than this page lists. See all of its files in its world.

README.md

shadows

The shadow-sampling interface every surface shader calls instead of writing its own PCF loop: compute_directional_shadow_factor for the sun (its virtual shadow map's pages), compute_spot_shadow_factor, compute_point_shadow_factor and compute_area_shadow_factor for punctual lights. Each answers 1.0 for fully lit and 0.0 for fully shadowed.

A shadow map stores the geometry, so a lookup pushes the receiver off the surface it stands on and biases its depth by that surface's slope. A lookup at the fragment a surface shader is shading (within a screen pixel of its input.world_position) reads the maps with the normal the fragment's mesh gives it (input.world_normal), turned to the side the normal it is handed faces, so a shading normal a normal map turned toward the light takes nothing away from that push. A lookup at any other point takes the normal it is handed as the receiver's surface normal. A point light's lookup takes the normal through compute_point_shadow_factor_at_normal(index, world_pos, normal). compute_point_shadow_factor(index, world_pos, n_dot_l) takes the cosine of the surface to the light alone, for a shader that holds no normal, and pushes the receiver along the ray to the light.

A shader that walks the punctual lights (zero_point_light(i)) rather than naming one light asks compute_punctual_shadow_factor(index, world_pos, normal) instead, which reads the row's kind and answers for whatever shape it casts in, and weighs the answers with zshadow_light_reach(index, world_pos, normal, object_channels) — how much direct light that row actually puts on a surface on those light channels, zero exactly where it puts none, the channel mask included. zshadow_punctual_probe and zshadow_directional_probe go one level under a factor, returning the depth the fragment carries in a light's map beside the depth the map already held there, loaded rather than compared.

A shader that looks up more than one light for the same fragment shares the filter's per-fragment rotation between the lookups: it holds one var kernel = zshadow_kernel(); and passes &kernel to the _with_kernel form of each call (compute_directional_shadow_factor_with_kernel, compute_directional_shadow_and_transmittance_with_kernel, compute_spot_shadow_factor_with_kernel, compute_point_shadow_factor_with_kernel, compute_point_shadow_factor_at_normal_with_kernel, compute_area_shadow_factor_with_kernel, compute_punctual_shadow_factor_with_kernel). The first lookup that filters derives the rotation and the rest read it back, so the fragment hashes its position once however many lights it is shadowed by. The light loop zero_pbr_direct runs does this, and looks a light up only where the light puts light on the fragment.

Routing through these is what keeps a shader in step with the renderer — the page levels, map resolution and filter width come from the bound frame data rather than from constants baked into the shader. The directional call selects the level the receiver's screen footprint asks for, and inside a hero box (renderer.setShadowHero) a level finer by the box's level bias.

// @domain: surface
#include "@builtin::shaderModules.shadows"

fn fragment(input: FragmentData) -> vec4<f32> {
    let n = normalize(input.world_normal);
    let lit = compute_directional_shadow_factor(input.world_position, n, uniforms.light_direction);
    return vec4<f32>(vec3<f32>(lit), 1.0);
}