blob: 2b3deae9dc92154e6df48a1f3de1d8f283c3f38f (
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
33
34
35
36
37
38
39
40
41
|
use crate::calculus::calculus::{Ray, Vec3};
use crate::common::Color;
use crate::object::HitRecord;
#[derive(Clone)]
pub enum MaterialType {
Diffuse,
Metal,
}
pub struct Material {
pub material_type: MaterialType,
pub albedo: Color
}
impl Material {
pub fn new(albedo: Color, material_type: MaterialType) -> Self {
Self { albedo, material_type }
}
pub fn scatter(&self, r_in: &Ray, rec: &HitRecord, attenuation: &mut Color, scattered: &mut Ray) -> bool {
scattered.direction = match self.material_type {
MaterialType::Diffuse => {
let scatter_direction = rec.normal.add(&Vec3::random_unit());
if scatter_direction.is_near_zero() {
rec.normal.clone()
} else {
scatter_direction
}
}
MaterialType::Metal => {
r_in.reflect(&rec.normal)
}
};
scattered.origin = rec.position;
attenuation.r = self.albedo.r;
attenuation.g = self.albedo.g;
attenuation.b = self.albedo.b;
true
}
}
|