summaryrefslogtreecommitdiff
path: root/racer-tracer/src/material/lambertian.rs
blob: d31e3e2b27871219c257d30eaeed9b11854ad120 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
use crate::{
    material::Material,
    ray::Ray,
    vec3::{random_unit_vector, Color},
};

pub struct Lambertian {
    color: Color,
}

impl Lambertian {
    pub fn new(color: Color) -> Self {
        Self { color }
    }
}

impl Material for Lambertian {
    fn scatter(
        &self,
        _ray: &crate::ray::Ray,
        rec: &crate::geometry::HitRecord,
    ) -> Option<(Ray, Color)> {
        let mut scatter_direction = rec.normal + random_unit_vector();

        // Catch bad scatter direction
        if scatter_direction.near_zero() {
            scatter_direction = rec.normal;
        }

        Some((Ray::new(rec.point, scatter_direction), self.color))
    }
}