Skip to content

Commit

Permalink
fix: use unit disk samples for defocus blur with blue sampler
Browse files Browse the repository at this point in the history
  • Loading branch information
Walther committed Jun 9, 2024
1 parent 41d56de commit da689f8
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions clovers-cli/src/sampler/blue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//!
//! Utilizes library code from <https://github.com/Jasper-Bekkers/blue-noise-sampler>.

use clovers::{wavelength::sample_wavelength, Float, RenderOpts, Vec2};
use clovers::{wavelength::sample_wavelength, Float, RenderOpts, Vec2, PI};

use super::*;

Expand Down Expand Up @@ -36,7 +36,7 @@ impl<'scene> SamplerTrait<'scene> for BlueSampler {
(self.get)(i, j, index, SamplerDimension::PixelOffsetX),
(self.get)(i, j, index, SamplerDimension::PixelOffsetY),
);
let lens_offset = Vec2::new(
let lens_offset = in_unit_disk(
(self.get)(i, j, index, SamplerDimension::LensOffsetX),
(self.get)(i, j, index, SamplerDimension::LensOffsetY),
);
Expand Down Expand Up @@ -116,3 +116,15 @@ define_blue_sampler!(spp32);
define_blue_sampler!(spp64);
define_blue_sampler!(spp128);
define_blue_sampler!(spp256);

/// Given two samples in range `[0..1]`, return a sample within `[-0.5 .. 0.5]` unit disk.
/// Based on <https://stackoverflow.com/a/50746409>
fn in_unit_disk(x: Float, y: Float) -> Vec2 {
// Polar coordinates + correcting for the distribution using sqrt
let r = x.sqrt();
let theta = y * 2.0 * PI;
// Conversion to Cartesian coordinates
let x = r * theta.cos();
let y = r * theta.sin();
Vec2::new(x, y)
}

0 comments on commit da689f8

Please sign in to comment.