From 08063b5aa3d3b268380e1e576eae7dcf4c7e71f6 Mon Sep 17 00:00:00 2001 From: Bruno BELANYI Date: Wed, 25 Mar 2020 00:08:34 +0100 Subject: [PATCH] library: render: scene: add background color --- pathtracer/src/render/scene.rs | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/pathtracer/src/render/scene.rs b/pathtracer/src/render/scene.rs index 2952ffa..adebd8b 100644 --- a/pathtracer/src/render/scene.rs +++ b/pathtracer/src/render/scene.rs @@ -21,6 +21,7 @@ pub struct Scene { lights: LightAggregate, objects: Vec, bvh: BVH, + background: LinearColor, aliasing_limit: u32, reflection_limit: u32, diffraction_index: f32, @@ -55,6 +56,7 @@ impl Scene { /// UniformTexture::new(LinearColor::new(0.5, 0.5, 0.5)).into(), /// ), /// ], + /// LinearColor::black(), // Background color /// 5, // aliasing limit /// 3, // reflection recursion limit /// 0.0, // diffraction index @@ -64,6 +66,7 @@ impl Scene { camera: Camera, lights: LightAggregate, mut objects: Vec, + background: LinearColor, aliasing_limit: u32, reflection_limit: u32, diffraction_index: f32, @@ -75,6 +78,7 @@ impl Scene { lights, objects, bvh, + background, aliasing_limit, reflection_limit, diffraction_index, @@ -121,8 +125,9 @@ impl Scene { let pixel = self.camera.film().pixel_at_ratio(x, y); let direction = Unit::new_normalize(pixel - self.camera.origin()); let indices = RefractionInfo::with_index(self.diffraction_index); - self.cast_ray(Ray::new(pixel, direction)) - .map_or_else(LinearColor::black, |(t, obj)| { + self.cast_ray(Ray::new(pixel, direction)).map_or_else( + || self.background.clone(), + |(t, obj)| { self.color_at( pixel + direction.as_ref() * t, obj, @@ -130,7 +135,8 @@ impl Scene { self.reflection_limit, indices, ) - }) + }, + ) } /// Get pixel color with anti-aliasing @@ -297,6 +303,8 @@ struct SerializedScene { #[serde(default)] objects: Vec, #[serde(default)] + background: LinearColor, + #[serde(default)] aliasing_limit: u32, #[serde(default)] reflection_limit: u32, @@ -310,6 +318,7 @@ impl From for Scene { scene.camera, scene.lights, scene.objects, + scene.background, scene.aliasing_limit, scene.reflection_limit, scene.starting_diffraction, @@ -347,10 +356,11 @@ mod test { let _scene = Scene::new( Camera::default(), LightAggregate::empty(), - Vec::new(), // Objects list - 5, // aliasing limit - 3, // reflection recursion limit - 0.0, // diffraction index + Vec::new(), // Objects list + LinearColor::black(), // Background color + 5, // aliasing limit + 3, // reflection recursion limit + 0.0, // diffraction index ); } }