use crate::renderer::ToneMapping;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum BackgroundMode {
#[default]
Color,
Environment,
Transparent,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum Aov {
#[default]
Beauty,
Albedo,
Normal,
Depth,
}
#[derive(Debug, Clone)]
pub struct RaytraceSettings {
pub samples_per_pixel: u32,
pub max_bounces: u32,
pub min_bounces: u32,
pub transparent_max_bounces: u32,
pub clamp_indirect: f32,
pub clamp_direct: f32,
pub ray_epsilon: f32,
pub seed: u64,
pub tone_mapping: ToneMapping,
pub exposure: f32,
pub background: BackgroundMode,
pub environment_intensity: f32,
pub sun_angular_radius: f32,
pub light_radius: f32,
pub aperture: f32,
pub focus_distance: f32,
pub adaptive_threshold: f32,
pub adaptive_min_samples: u32,
pub denoise: bool,
pub aov: Aov,
}
impl Default for RaytraceSettings {
fn default() -> Self {
Self {
samples_per_pixel: 32,
max_bounces: 6,
min_bounces: 3,
transparent_max_bounces: 8,
clamp_indirect: 10.0,
clamp_direct: 0.0,
ray_epsilon: 1e-4,
seed: 0x5eed_1234_abcd_0001,
tone_mapping: ToneMapping::AcesFilmic,
exposure: 1.0,
background: BackgroundMode::Color,
environment_intensity: 1.0,
sun_angular_radius: 0.0,
light_radius: 0.0,
aperture: 0.0,
focus_distance: 0.0,
adaptive_threshold: 0.01,
adaptive_min_samples: 16,
denoise: true,
aov: Aov::Beauty,
}
}
}
impl RaytraceSettings {
pub fn preview() -> Self {
Self {
samples_per_pixel: 16,
max_bounces: 4,
denoise: true,
..Self::default()
}
}
pub fn final_quality() -> Self {
Self {
samples_per_pixel: 512,
max_bounces: 12,
min_bounces: 4,
clamp_indirect: 0.0,
adaptive_threshold: 0.0,
denoise: false,
..Self::default()
}
}
pub fn with_samples(mut self, n: u32) -> Self {
self.samples_per_pixel = n.max(1);
self
}
pub fn with_bounces(mut self, n: u32) -> Self {
self.max_bounces = n;
self.min_bounces = self.min_bounces.min(n);
self
}
pub fn with_background(mut self, mode: BackgroundMode) -> Self {
self.background = mode;
self
}
pub fn with_tone_mapping(mut self, mode: ToneMapping, exposure: f32) -> Self {
self.tone_mapping = mode;
self.exposure = exposure;
self
}
pub fn with_depth_of_field(mut self, aperture: f32, focus_distance: f32) -> Self {
self.aperture = aperture.max(0.0);
self.focus_distance = focus_distance.max(0.0);
self
}
pub fn with_adaptive(mut self, threshold: f32) -> Self {
self.adaptive_threshold = threshold.max(0.0);
self
}
pub fn with_denoise(mut self, on: bool) -> Self {
self.denoise = on;
self
}
pub fn with_seed(mut self, seed: u64) -> Self {
self.seed = seed;
self
}
pub fn with_aov(mut self, aov: Aov) -> Self {
self.aov = aov;
self
}
pub fn with_soft_shadows(mut self, sun_angular_radius: f32, light_radius: f32) -> Self {
self.sun_angular_radius = sun_angular_radius.max(0.0);
self.light_radius = light_radius.max(0.0);
self
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn builders_compose() {
let s = RaytraceSettings::default()
.with_samples(64)
.with_bounces(2)
.with_depth_of_field(0.05, 7.0)
.with_denoise(false);
assert_eq!(s.samples_per_pixel, 64);
assert_eq!(s.max_bounces, 2);
assert!(s.min_bounces <= s.max_bounces);
assert_eq!(s.aperture, 0.05);
assert!(!s.denoise);
}
#[test]
fn zero_samples_is_clamped_up() {
assert_eq!(
RaytraceSettings::default()
.with_samples(0)
.samples_per_pixel,
1
);
}
#[test]
fn final_quality_drops_every_approximation() {
let s = RaytraceSettings::final_quality();
assert_eq!(s.clamp_indirect, 0.0);
assert_eq!(
s.adaptive_threshold, 0.0,
"a reference render must not stop early"
);
assert!(!s.denoise);
}
#[test]
fn adaptive_can_be_turned_off() {
assert_eq!(
RaytraceSettings::default()
.with_adaptive(0.0)
.adaptive_threshold,
0.0
);
assert!(RaytraceSettings::default().adaptive_threshold > 0.0);
}
}