Skip to content

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])?;

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>

sample/disk_concentric preview

Uniform sample on the unit disk via sqrt warp.

fn disk_uniform(rnd: vec2<f32>) -> vec2<f32>

sample/disk_uniform preview

1D low-discrepancy Halton sample for index i and prime base.

fn halton(i: u32, base: u32) -> f32

sample/halton preview

2D Hammersley sample (i / n, van der Corput base-2).

fn hammersley(i: u32, n: u32) -> vec2<f32>

sample/hammersley preview

Cosine-weighted hemisphere direction around normal n. Use for diffuse-ish importance sampling.

fn hemisphere_cosine(rnd: vec2<f32>, n: vec3<f32>) -> vec3<f32>

sample/hemisphere_cosine preview

Uniform hemisphere direction around normal n.

fn hemisphere_uniform(rnd: vec2<f32>, n: vec3<f32>) -> vec3<f32>

sample/hemisphere_uniform preview

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

sample/interleaved_gradient_noise preview

Uniform direction on the unit sphere.

fn sphere_uniform(rnd: vec2<f32>) -> vec3<f32>

sample/sphere_uniform preview

Base-2 van der Corput radical inverse of a u32 index.

fn van_der_corput(i: u32) -> f32

sample/van_der_corput preview

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>

sample/vogel_disk preview