Sampling
These helpers generate sample points — on the disk, on the hemisphere,
across a low-discrepancy sequence — that you can use as offsets into a
texture, as importance-sampled directions for shading, or as the input
side of a Monte Carlo estimator. Inputs are typically a (rnd, n) pair;
outputs are positions or directions.
Pull one in by slug and use it inside your fragment:
let main = r#"@fragment fn fs_main(in: VertexOutput) -> @location(0) vec4<f32> { let p = vogel_disk(u32(in.position.x) % 128u, 128u); let d = length(in.uv * 2.0 - 1.0 - p); return vec4<f32>(vec3<f32>(1.0 - smoothstep(0.0, 0.04, d)), 1.0);}"#;let shader = Shader::new(&["sample/vogel_disk", main])?;disk_concentric
Section titled “disk_concentric”Shirley’s concentric mapping from [0, 1]^2 to the unit disk. Preserves
relative position better than naive polar sampling.
fn disk_concentric(rnd: vec2<f32>) -> vec2<f32>
disk_uniform
Section titled “disk_uniform”Uniform sample on the unit disk via sqrt warp.
fn disk_uniform(rnd: vec2<f32>) -> vec2<f32>
halton
Section titled “halton”1D low-discrepancy Halton sample for index i and prime base.
fn halton(i: u32, base: u32) -> f32
hammersley
Section titled “hammersley”2D Hammersley sample (i / n, van der Corput base-2).
fn hammersley(i: u32, n: u32) -> vec2<f32>
hemisphere_cosine
Section titled “hemisphere_cosine”Cosine-weighted hemisphere direction around normal n. Use for
diffuse-ish importance sampling.
fn hemisphere_cosine(rnd: vec2<f32>, n: vec3<f32>) -> vec3<f32>
hemisphere_uniform
Section titled “hemisphere_uniform”Uniform hemisphere direction around normal n.
fn hemisphere_uniform(rnd: vec2<f32>, n: vec3<f32>) -> vec3<f32>
interleaved_gradient_noise
Section titled “interleaved_gradient_noise”Jorge Jimenez’s IGN. Pixel-coord in, scalar in [0, 1) out. Useful as
per-pixel decorrelation for any of the sample sequences above.
fn interleaved_gradient_noise(p: vec2<f32>) -> f32
sphere_uniform
Section titled “sphere_uniform”Uniform direction on the unit sphere.
fn sphere_uniform(rnd: vec2<f32>) -> vec3<f32>
van_der_corput
Section titled “van_der_corput”Base-2 van der Corput radical inverse of a u32 index.
fn van_der_corput(i: u32) -> f32
vogel_disk
Section titled “vogel_disk”k-th of n Vogel sunflower disk samples. The golden-angle spiral with a
sqrt-r radius gives even areal coverage with no clustering.
fn vogel_disk(k: u32, n: u32) -> vec2<f32>