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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
|
use std::sync::Arc;
use super::Material;
use crate::ray::Ray;
use crate::texture::SolidColor;
use crate::vec3::Color;
use crate::{hittable::HitRecord, texture::Texture, vec3::Vec3};
pub struct Lambertian {
pub albedo: Arc<dyn Texture>,
}
impl Lambertian {
pub fn from_color(color: Color) -> Self {
Self {
albedo: Arc::new(SolidColor {
color_value: color.clone(),
}),
}
}
}
impl Material for Lambertian {
fn scatter(
&self,
ray_in: &Ray,
hit_record: &HitRecord,
attenuation: &mut Color,
scattered: &mut Ray,
) -> bool {
let mut scatter_direction = &hit_record.normal + Vec3::random_unit_vector();
// Catch zero-vector scatter directions that will generate issues later
if scatter_direction.near_zero() {
scatter_direction = hit_record.normal.clone();
}
*scattered = Ray {
origin: hit_record.p.clone(),
direction: scatter_direction,
time: ray_in.time,
};
*attenuation = self.albedo.value(hit_record.u, hit_record.v, &hit_record.p);
true
}
}
|